1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
2 | #include "globals.h"
|
---|
3 | extern struct s_reader * reader;
|
---|
4 |
|
---|
5 | static int radegast_send(uchar *buf)
|
---|
6 | {
|
---|
7 | int l=buf[1]+2;
|
---|
8 | return(send(client[cs_idx].pfd, buf, l, 0));
|
---|
9 | }
|
---|
10 |
|
---|
11 | static int radegast_recv(uchar *buf, int l)
|
---|
12 | {
|
---|
13 | int n;
|
---|
14 | if (!client[cs_idx].pfd) return(-1);
|
---|
15 | if (client[cs_idx].is_server) { // server code
|
---|
16 | if ((n=recv(client[cs_idx].pfd, buf, l, 0))>0)
|
---|
17 | client[cs_idx].last=time((time_t *) 0);
|
---|
18 | } else { // client code
|
---|
19 | if ((n=recv(client[cs_idx].pfd, buf, l, 0))>0) {
|
---|
20 | cs_ddump(buf, n, "radegast: received %d bytes from %s", n, remote_txt());
|
---|
21 | client[cs_idx].last = time((time_t *) 0);
|
---|
22 |
|
---|
23 | if (buf[0] == 2) { // dcw received
|
---|
24 | if (buf[3] != 0x10) { // dcw ok
|
---|
25 | cs_log("radegast: no dcw");
|
---|
26 | n = -1;
|
---|
27 | }
|
---|
28 | }
|
---|
29 | }
|
---|
30 | }
|
---|
31 | return(n);
|
---|
32 | }
|
---|
33 |
|
---|
34 | static int radegast_recv_chk(uchar *dcw, int *rc, uchar *buf)
|
---|
35 | {
|
---|
36 | if ((buf[0] == 2) && (buf[1] == 0x12)) {
|
---|
37 | memcpy(dcw, buf+4, 16);
|
---|
38 | cs_debug("radegast: recv chk - %s", cs_hexdump(0, dcw, 16));
|
---|
39 | *rc = 1;
|
---|
40 | return(reader[client[cs_idx].ridx].msg_idx);
|
---|
41 | }
|
---|
42 |
|
---|
43 | return (-1);
|
---|
44 | }
|
---|
45 |
|
---|
46 | static void radegast_auth_client(in_addr_t ip)
|
---|
47 | {
|
---|
48 | int ok;
|
---|
49 | struct s_auth *account;
|
---|
50 | struct s_ip *p_ip;
|
---|
51 |
|
---|
52 | for (ok=0, p_ip=cfg->rad_allowed; (p_ip) && (!ok); p_ip=p_ip->next)
|
---|
53 | ok=((ip>=p_ip->ip[0]) && (ip<=p_ip->ip[1]));
|
---|
54 |
|
---|
55 | if (!ok)
|
---|
56 | {
|
---|
57 | cs_auth_client((struct s_auth *)0, NULL);
|
---|
58 | cs_exit(0);
|
---|
59 | }
|
---|
60 |
|
---|
61 | for (ok=0, account=cfg->account; (cfg->rad_usr[0]) && (account) && (!ok); account=account->next)
|
---|
62 | {
|
---|
63 | ok=(!strcmp(cfg->rad_usr, account->usr));
|
---|
64 | if (ok && cs_auth_client(account, NULL))
|
---|
65 | cs_exit(0);
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (!ok)
|
---|
69 | cs_auth_client((struct s_auth *)(-1), NULL);
|
---|
70 | }
|
---|
71 |
|
---|
72 | static int get_request(uchar *buf)
|
---|
73 | {
|
---|
74 | int n, rc=0;
|
---|
75 | if ((n=process_input(buf, 2, cfg->cmaxidle))==2)
|
---|
76 | {
|
---|
77 | if ((n=process_input(buf+2, buf[1], 0))>=0)
|
---|
78 | n+=2;
|
---|
79 | if (n-2==buf[1])
|
---|
80 | rc=n;
|
---|
81 | else
|
---|
82 | cs_log("WARNING: protocol error (garbage)");
|
---|
83 | }
|
---|
84 | if (n>0)
|
---|
85 | cs_ddump(buf, n, "received %d bytes from client", n);
|
---|
86 | return(rc);
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void radegast_send_dcw(ECM_REQUEST *er)
|
---|
90 | {
|
---|
91 | uchar mbuf[1024];
|
---|
92 | mbuf[0]=0x02; // DCW
|
---|
93 | if (er->rc<4)
|
---|
94 | {
|
---|
95 | mbuf[1]=0x12; // len (overall)
|
---|
96 | mbuf[2]=0x05; // ACCESS
|
---|
97 | mbuf[3]=0x10; // len
|
---|
98 | memcpy(mbuf+4, er->cw, 16);
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | mbuf[1]=0x02; // len (overall)
|
---|
103 | mbuf[2]=0x04; // NO ACCESS
|
---|
104 | mbuf[3]=0x00; // len
|
---|
105 | }
|
---|
106 | radegast_send(mbuf);
|
---|
107 | }
|
---|
108 |
|
---|
109 | static void radegast_process_ecm(uchar *buf, int l)
|
---|
110 | {
|
---|
111 | int i, n, sl;
|
---|
112 | ECM_REQUEST *er;
|
---|
113 |
|
---|
114 | if (!(er=get_ecmtask()))
|
---|
115 | return;
|
---|
116 | for (i=0; i<l; i+=(sl+2))
|
---|
117 | {
|
---|
118 | sl=buf[i+1];
|
---|
119 | switch(buf[i])
|
---|
120 | {
|
---|
121 | case 2: // CAID (upper byte only, oldstyle)
|
---|
122 | er->caid=buf[i+2]<<8;
|
---|
123 | break;
|
---|
124 | case 10: // CAID
|
---|
125 | er->caid=b2i(2, buf+i+2);
|
---|
126 | break;
|
---|
127 | case 3: // ECM DATA
|
---|
128 | er->l=sl;
|
---|
129 | memcpy(er->ecm, buf+i+2, er->l);
|
---|
130 | break;
|
---|
131 | case 6: // PROVID (ASCII)
|
---|
132 | n=(sl>6) ? 3 : (sl>>1);
|
---|
133 | er->prid=cs_atoi((char *) buf+i+2+sl-(n<<1), n, 0);
|
---|
134 | break;
|
---|
135 | case 7: // KEYNR (ASCII), not needed
|
---|
136 | break;
|
---|
137 | case 8: // ECM PROCESS PID ?? don't know, not needed
|
---|
138 | break;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | if (l!=i)
|
---|
142 | cs_log("WARNING: ECM-request corrupt");
|
---|
143 | else
|
---|
144 | get_cw(er);
|
---|
145 | }
|
---|
146 |
|
---|
147 | static void radegast_process_unknown(uchar *buf)
|
---|
148 | {
|
---|
149 | uchar answer[2]={0x81, 0x00};
|
---|
150 | radegast_send(answer);
|
---|
151 | cs_log("unknown request %02X, len=%d", buf[0], buf[1]);
|
---|
152 | }
|
---|
153 |
|
---|
154 | static void radegast_server(void *idx)
|
---|
155 | {
|
---|
156 | int n;
|
---|
157 | uchar mbuf[1024];
|
---|
158 |
|
---|
159 | int cidx=(int)idx;
|
---|
160 | client[cidx].thread=pthread_self();
|
---|
161 |
|
---|
162 | radegast_auth_client(client[cs_idx].ip);
|
---|
163 | while ((n=get_request(mbuf))>0)
|
---|
164 | {
|
---|
165 | switch(mbuf[0])
|
---|
166 | {
|
---|
167 | case 1:
|
---|
168 | radegast_process_ecm(mbuf+2, mbuf[1]);
|
---|
169 | break;
|
---|
170 | default:
|
---|
171 | radegast_process_unknown(mbuf);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | cs_disconnect_client();
|
---|
175 | }
|
---|
176 |
|
---|
177 | static int radegast_send_ecm(ECM_REQUEST *er)
|
---|
178 | {
|
---|
179 | int n;
|
---|
180 | uchar provid_buf[8];
|
---|
181 | uchar header[22] = "\x02\x01\x00\x06\x08\x30\x30\x30\x30\x30\x30\x30\x30\x07\x04\x30\x30\x30\x38\x08\x01\x02";
|
---|
182 | uchar *ecmbuf = malloc(er->l + 30);
|
---|
183 | memset(ecmbuf, 0, er->l + 30);
|
---|
184 |
|
---|
185 | ecmbuf[0] = 1;
|
---|
186 | ecmbuf[1] = er->l + 30 - 2;
|
---|
187 | memcpy(ecmbuf + 2, header, sizeof(header));
|
---|
188 | for(n = 0; n < 4; n++) {
|
---|
189 | sprintf((char*)provid_buf+(n*2), "%02X", ((uchar *)(&er->prid))[4 - 1 - n]);
|
---|
190 | }
|
---|
191 | ecmbuf[7] = provid_buf[0];
|
---|
192 | ecmbuf[8] = provid_buf[1];
|
---|
193 | ecmbuf[9] = provid_buf[2];
|
---|
194 | ecmbuf[10] = provid_buf[3];
|
---|
195 | ecmbuf[11] = provid_buf[4];
|
---|
196 | ecmbuf[12] = provid_buf[5];
|
---|
197 | ecmbuf[13] = provid_buf[6];
|
---|
198 | ecmbuf[14] = provid_buf[7];
|
---|
199 | ecmbuf[2 + sizeof(header)] = 0xa;
|
---|
200 | ecmbuf[3 + sizeof(header)] = 2;
|
---|
201 | ecmbuf[4 + sizeof(header)] = er->caid >> 8;
|
---|
202 | ecmbuf[5 + sizeof(header)] = er->caid & 0xff;
|
---|
203 | ecmbuf[6 + sizeof(header)] = 3;
|
---|
204 | ecmbuf[7 + sizeof(header)] = er->l;
|
---|
205 | memcpy(ecmbuf + 8 + sizeof(header), er->ecm, er->l);
|
---|
206 | ecmbuf[4] = er->caid >> 8;
|
---|
207 |
|
---|
208 | reader[client[cs_idx].ridx].msg_idx = er->idx;
|
---|
209 | n = send(client[cs_idx].pfd, ecmbuf, er->l + 30, 0);
|
---|
210 |
|
---|
211 | cs_log("radegast: sending ecm");
|
---|
212 | cs_ddump(ecmbuf, er->l + 30, "ecm:");
|
---|
213 |
|
---|
214 | free(ecmbuf);
|
---|
215 |
|
---|
216 | return 0;
|
---|
217 | }
|
---|
218 |
|
---|
219 | int radegast_cli_init(void)
|
---|
220 | {
|
---|
221 | struct sockaddr_in loc_sa;
|
---|
222 | struct protoent *ptrp;
|
---|
223 | int p_proto, handle;
|
---|
224 |
|
---|
225 | client[cs_idx].pfd=0;
|
---|
226 | if (reader[client[cs_idx].ridx].r_port<=0)
|
---|
227 | {
|
---|
228 | cs_log("radegast: invalid port %d for server %s", reader[client[cs_idx].ridx].r_port, reader[client[cs_idx].ridx].device);
|
---|
229 | return(1);
|
---|
230 | }
|
---|
231 | if( (ptrp=getprotobyname("tcp")) )
|
---|
232 | p_proto=ptrp->p_proto;
|
---|
233 | else
|
---|
234 | p_proto=6;
|
---|
235 |
|
---|
236 | client[cs_idx].ip=0;
|
---|
237 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
238 | loc_sa.sin_family = AF_INET;
|
---|
239 | #ifdef LALL
|
---|
240 | if (cfg->serverip[0])
|
---|
241 | loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip);
|
---|
242 | else
|
---|
243 | #endif
|
---|
244 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
245 | loc_sa.sin_port = htons(reader[client[cs_idx].ridx].l_port);
|
---|
246 |
|
---|
247 | if ((client[cs_idx].udp_fd=socket(PF_INET, SOCK_STREAM, p_proto))<0)
|
---|
248 | {
|
---|
249 | cs_log("radegast: Socket creation failed (errno=%d)", errno);
|
---|
250 | cs_exit(1);
|
---|
251 | }
|
---|
252 |
|
---|
253 | #ifdef SO_PRIORITY
|
---|
254 | if (cfg->netprio)
|
---|
255 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
256 | (void *)&cfg->netprio, sizeof(ulong));
|
---|
257 | #endif
|
---|
258 | if (!reader[client[cs_idx].ridx].tcp_ito) {
|
---|
259 | ulong keep_alive = reader[client[cs_idx].ridx].tcp_ito?1:0;
|
---|
260 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
261 | (void *)&keep_alive, sizeof(ulong));
|
---|
262 | }
|
---|
263 |
|
---|
264 | memset((char *)&client[cs_idx].udp_sa,0,sizeof(client[cs_idx].udp_sa));
|
---|
265 | client[cs_idx].udp_sa.sin_family = AF_INET;
|
---|
266 | client[cs_idx].udp_sa.sin_port = htons((u_short)reader[client[cs_idx].ridx].r_port);
|
---|
267 |
|
---|
268 | cs_log("radegast: proxy %s:%d (fd=%d)",
|
---|
269 | reader[client[cs_idx].ridx].device, reader[client[cs_idx].ridx].r_port, client[cs_idx].udp_fd);
|
---|
270 |
|
---|
271 | handle = network_tcp_connection_open();
|
---|
272 | if(handle < 0) return -1;
|
---|
273 |
|
---|
274 | reader[client[cs_idx].ridx].tcp_connected = 2;
|
---|
275 | reader[client[cs_idx].ridx].card_status = CARD_INSERTED;
|
---|
276 | reader[client[cs_idx].ridx].last_g = reader[client[cs_idx].ridx].last_s = time((time_t *)0);
|
---|
277 |
|
---|
278 | cs_debug("radegast: last_s=%d, last_g=%d", reader[client[cs_idx].ridx].last_s, reader[client[cs_idx].ridx].last_g);
|
---|
279 |
|
---|
280 | client[cs_idx].pfd=client[cs_idx].udp_fd;
|
---|
281 |
|
---|
282 | return(0);
|
---|
283 | }
|
---|
284 |
|
---|
285 | void module_radegast(struct s_module *ph)
|
---|
286 | {
|
---|
287 | static PTAB ptab;
|
---|
288 | ptab.ports[0].s_port = cfg->rad_port;
|
---|
289 | ph->ptab = &ptab;
|
---|
290 | ph->ptab->nports = 1;
|
---|
291 |
|
---|
292 | strcpy(ph->desc, "radegast");
|
---|
293 | ph->type=MOD_CONN_TCP;
|
---|
294 | ph->multi=0;
|
---|
295 | ph->watchdog=1;
|
---|
296 | ph->s_ip=cfg->rad_srvip;
|
---|
297 | ph->s_handler=radegast_server;
|
---|
298 | ph->recv=radegast_recv;
|
---|
299 | ph->send_dcw=radegast_send_dcw;
|
---|
300 | ph->c_multi=0;
|
---|
301 | ph->c_init=radegast_cli_init;
|
---|
302 | ph->c_recv_chk=radegast_recv_chk;
|
---|
303 | ph->c_send_ecm=radegast_send_ecm;
|
---|
304 | ph->num=R_RADEGAST;
|
---|
305 | }
|
---|