1 | #include "globals.h"
|
---|
2 | extern struct s_reader *reader;
|
---|
3 |
|
---|
4 | //CMD00 - ECM (request)
|
---|
5 | //CMD01 - ECM (response)
|
---|
6 | //CMD02 - EMM (in clientmode - set EMM, in server mode - EMM data) - obsolete
|
---|
7 | //CMD03 - ECM (cascading request)
|
---|
8 | //CMD04 - ECM (cascading response)
|
---|
9 | //CMD05 - EMM (emm request) send cardata/cardinfo to client
|
---|
10 | //CMD06 - EMM (incomming EMM in server mode)
|
---|
11 | //CMD19 - EMM (incomming EMM in server mode) only seen with caid 0x1830
|
---|
12 | //CMD08 - Stop sending requests to the server for current srvid,prvid,caid
|
---|
13 | //CMD44 - MPCS/OScam internal error notification
|
---|
14 |
|
---|
15 | #define REQ_SIZE 328 // 256 + 20 + 0x34
|
---|
16 |
|
---|
17 | static int camd35_send(uchar *buf)
|
---|
18 | {
|
---|
19 | int l;
|
---|
20 | unsigned char rbuf[REQ_SIZE+15+4], *sbuf = rbuf + 4;
|
---|
21 |
|
---|
22 | if (!client[cs_idx].udp_fd) return(-1);
|
---|
23 | l = 20 + buf[1] + (((buf[0] == 3) || (buf[0] == 4)) ? 0x34 : 0);
|
---|
24 | memcpy(rbuf, client[cs_idx].ucrc, 4);
|
---|
25 | memcpy(sbuf, buf, l);
|
---|
26 | memset(sbuf + l, 0xff, 15); // set unused space to 0xff for newer camd3's
|
---|
27 | memcpy(sbuf + 4, i2b(4, crc32(0L, sbuf+20, sbuf[1])), 4);
|
---|
28 | l = boundary(4, l);
|
---|
29 | cs_ddump(sbuf, l, "send %d bytes to %s", l, remote_txt());
|
---|
30 | aes_encrypt(sbuf, l);
|
---|
31 |
|
---|
32 | int status;
|
---|
33 | if (client[cs_idx].is_udp) {
|
---|
34 | status = sendto(client[cs_idx].udp_fd, rbuf, l+4, 0,
|
---|
35 | (struct sockaddr *)&client[cs_idx].udp_sa,
|
---|
36 | sizeof(client[cs_idx].udp_sa));
|
---|
37 | if (status == -1) client[cs_idx].udp_sa.sin_addr.s_addr = 0;
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | status = send(client[cs_idx].udp_fd, rbuf, l + 4, 0);
|
---|
41 | if (status == -1) network_tcp_connection_close(&reader[client[cs_idx].ridx], client[cs_idx].pfd);
|
---|
42 | }
|
---|
43 | return status;
|
---|
44 | }
|
---|
45 |
|
---|
46 | static int camd35_auth_client(uchar *ucrc)
|
---|
47 | {
|
---|
48 | int rc=1;
|
---|
49 | ulong crc;
|
---|
50 | struct s_auth *account;
|
---|
51 |
|
---|
52 | if (client[cs_idx].upwd[0])
|
---|
53 | return(memcmp(client[cs_idx].ucrc, ucrc, 4) ? 1 : 0);
|
---|
54 | client[cs_idx].crypted=1;
|
---|
55 | crc=(((ucrc[0]<<24) | (ucrc[1]<<16) | (ucrc[2]<<8) | ucrc[3]) & 0xffffffffL);
|
---|
56 | for (account=cfg->account; (account) && (!client[cs_idx].upwd[0]); account=account->next)
|
---|
57 | if (crc==crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), client[cs_idx].dump), 16))
|
---|
58 | {
|
---|
59 | memcpy(client[cs_idx].ucrc, ucrc, 4);
|
---|
60 | strcpy((char *)client[cs_idx].upwd, account->pwd);
|
---|
61 | aes_set_key((char *) MD5(client[cs_idx].upwd, strlen((char *)client[cs_idx].upwd), client[cs_idx].dump));
|
---|
62 | rc=cs_auth_client(&client[cs_idx], account, NULL);
|
---|
63 | }
|
---|
64 | return(rc);
|
---|
65 | }
|
---|
66 |
|
---|
67 | static int camd35_recv(uchar *buf, int l)
|
---|
68 | {
|
---|
69 | int rc, s, rs, n=0;
|
---|
70 | unsigned char recrc[4];
|
---|
71 | for (rc=rs=s=0; !rc; s++) switch(s)
|
---|
72 | {
|
---|
73 | case 0:
|
---|
74 | if (client[cs_idx].is_server)
|
---|
75 | {
|
---|
76 | if (!client[cs_idx].udp_fd) return(-9);
|
---|
77 | if (client[cs_idx].is_udp)
|
---|
78 | rs=recv_from_udpipe(buf);
|
---|
79 | else
|
---|
80 | rs=recv(client[cs_idx].udp_fd, buf, l, 0);
|
---|
81 | }
|
---|
82 | else
|
---|
83 | {
|
---|
84 | if (!client[cs_idx].udp_fd) return(-9);
|
---|
85 | rs = recv(client[cs_idx].udp_fd, buf, l, 0);
|
---|
86 | }
|
---|
87 | if (rs < 24) rc = -1;
|
---|
88 | break;
|
---|
89 | case 1:
|
---|
90 | memcpy(recrc, buf, 4);
|
---|
91 | memmove(buf, buf+4, rs-=4);
|
---|
92 | switch (camd35_auth_client(recrc))
|
---|
93 | {
|
---|
94 | case 0: break; // ok
|
---|
95 | case 1: rc=-2; break; // unknown user
|
---|
96 | default: rc=-9; break; // error's from cs_auth()
|
---|
97 | }
|
---|
98 | break;
|
---|
99 | case 2:
|
---|
100 | aes_decrypt(buf, rs);
|
---|
101 | cs_ddump(buf, rs, "received %d bytes from %s", rs, remote_txt());
|
---|
102 | if (rs!=boundary(4, rs))
|
---|
103 | {
|
---|
104 | cs_debug("WARNING: packet size has wrong decryption boundary");
|
---|
105 | }
|
---|
106 | //n=(buf[0]==3) ? n=0x34 : 0; this was original, but statement below seems more logical -- dingo35
|
---|
107 | n=(buf[0]==3) ? 0x34 : 0;
|
---|
108 | n=boundary(4, n+20+buf[1]);
|
---|
109 | if (n<rs)
|
---|
110 | {
|
---|
111 | cs_debug("ignoring %d bytes of garbage", rs-n);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | if (n>rs) rc=-3;
|
---|
115 | break;
|
---|
116 | case 3:
|
---|
117 | if (crc32(0L, buf+20, buf[1])!=b2i(4, buf+4)) rc=-4;
|
---|
118 | if (!rc) rc=n;
|
---|
119 | break;
|
---|
120 | }
|
---|
121 | if ((rs>0) && ((rc==-1)||(rc==-2)))
|
---|
122 | {
|
---|
123 | cs_ddump(buf, rs, "received %d bytes from %s (native)", rs, remote_txt);
|
---|
124 | }
|
---|
125 | client[cs_idx].last=time((time_t *) 0);
|
---|
126 | switch(rc)
|
---|
127 | {
|
---|
128 | case -1: cs_log("packet to small (%d bytes)", rs);
|
---|
129 | break;
|
---|
130 | case -2: cs_auth_client(&client[cs_idx], 0, "unknown user");
|
---|
131 | break;
|
---|
132 | case -3: cs_log("incomplete request !");
|
---|
133 | break;
|
---|
134 | case -4: cs_log("checksum error (wrong password ?)");
|
---|
135 | break;
|
---|
136 | }
|
---|
137 | return(rc);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * server functions
|
---|
142 | */
|
---|
143 |
|
---|
144 | static void camd35_request_emm(ECM_REQUEST *er)
|
---|
145 | {
|
---|
146 | int i, au;
|
---|
147 | time_t now;
|
---|
148 | uchar mbuf[1024];
|
---|
149 |
|
---|
150 | au = client[cs_idx].au;
|
---|
151 | if ((au < 0) || (au > CS_MAXREADER))
|
---|
152 | return; // TODO
|
---|
153 |
|
---|
154 | time(&now);
|
---|
155 | if (!memcmp(client[cs_idx].lastserial, reader[au].hexserial, 8))
|
---|
156 | if (abs(now-client[cs_idx].last) < 180) return;
|
---|
157 |
|
---|
158 | memcpy(client[cs_idx].lastserial, reader[au].hexserial, 8);
|
---|
159 | client[cs_idx].last = now;
|
---|
160 |
|
---|
161 | if (reader[au].caid[0])
|
---|
162 | {
|
---|
163 | client[cs_idx].disable_counter = 0;
|
---|
164 | log_emm_request(au);
|
---|
165 | }
|
---|
166 | else
|
---|
167 | if (client[cs_idx].disable_counter > 2)
|
---|
168 | return;
|
---|
169 | else
|
---|
170 | client[cs_idx].disable_counter++;
|
---|
171 |
|
---|
172 | memset(mbuf, 0, sizeof(mbuf));
|
---|
173 | mbuf[2] = mbuf[3] = 0xff; // must not be zero
|
---|
174 | memcpy(mbuf + 8, i2b(2, er->srvid), 2);
|
---|
175 |
|
---|
176 | //override request provid with auprovid if set in CMD05
|
---|
177 | if(reader[au].auprovid) {
|
---|
178 | if(reader[au].auprovid != er->prid)
|
---|
179 | memcpy(mbuf + 12, i2b(4, reader[au].auprovid), 4);
|
---|
180 | else
|
---|
181 | memcpy(mbuf + 12, i2b(4, er->prid), 4);
|
---|
182 | } else {
|
---|
183 | memcpy(mbuf + 12, i2b(4, er->prid), 4);
|
---|
184 | }
|
---|
185 |
|
---|
186 | memcpy(mbuf + 16, i2b(2, er->pid), 2);
|
---|
187 | mbuf[0] = 5;
|
---|
188 | mbuf[1] = 111;
|
---|
189 | if (reader[au].caid[0])
|
---|
190 | {
|
---|
191 | mbuf[39] = 1; // no. caids
|
---|
192 | mbuf[20] = reader[au].caid[0]>>8; // caid's (max 8)
|
---|
193 | mbuf[21] = reader[au].caid[0]&0xff;
|
---|
194 | memcpy(mbuf + 40, reader[au].hexserial, 6); // serial now 6 bytes
|
---|
195 | mbuf[47] = reader[au].nprov;
|
---|
196 | for (i = 0; i < reader[au].nprov; i++)
|
---|
197 | {
|
---|
198 | if (((reader[au].caid[0] >= 0x1700) && (reader[au].caid[0] <= 0x1799)) || // Betacrypt
|
---|
199 | ((reader[au].caid[0] >= 0x0600) && (reader[au].caid[0] <= 0x0699))) // Irdeto (don't know if this is correct, cause I don't own a IRDETO-Card)
|
---|
200 | {
|
---|
201 | mbuf[48 + (i*5)] = reader[au].prid[i][0];
|
---|
202 | memcpy(&mbuf[50 + (i*5)], &reader[au].prid[i][1], 3);
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | mbuf[48 + (i * 5)] = reader[au].prid[i][2];
|
---|
207 | mbuf[49 + (i * 5)] =reader[au].prid[i][3];
|
---|
208 | memcpy(&mbuf[50 + (i * 5)], &reader[au].sa[i][0],4); // for conax we need at least 4 Bytes
|
---|
209 | }
|
---|
210 | }
|
---|
211 | //we think client/server protocols should deliver all information, and only readers should discard EMM
|
---|
212 | mbuf[128] = (reader[au].blockemm_g == 1) ? 0: 1;
|
---|
213 | mbuf[129] = (reader[au].blockemm_s == 1) ? 0: 1;
|
---|
214 | mbuf[130] = (reader[au].blockemm_u == 1) ? 0: 1;
|
---|
215 | //mbuf[131] = reader[au].card_system; //Cardsystem for Oscam client
|
---|
216 | }
|
---|
217 | else // disable emm
|
---|
218 | mbuf[20] = mbuf[39] = mbuf[40] = mbuf[47] = mbuf[49] = 1;
|
---|
219 |
|
---|
220 | memcpy(mbuf + 10, mbuf + 20, 2);
|
---|
221 | camd35_send(mbuf); // send with data-len 111 for camd3 > 3.890
|
---|
222 | mbuf[1]++;
|
---|
223 | camd35_send(mbuf); // send with data-len 112 for camd3 < 3.890
|
---|
224 | }
|
---|
225 |
|
---|
226 | static void camd35_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
227 | {
|
---|
228 | uchar *buf;
|
---|
229 | buf = client->req + (er->cpti * REQ_SIZE); // get orig request
|
---|
230 |
|
---|
231 | if (((er->rcEx > 0) || (er->rc == 8)) && !client[cs_idx].c35_suppresscmd08)
|
---|
232 | {
|
---|
233 | buf[0] = 0x08;
|
---|
234 | buf[1] = 2;
|
---|
235 | memset(buf + 20, 0, buf[1]);
|
---|
236 | }
|
---|
237 | else if (er->rc == 13)
|
---|
238 | {
|
---|
239 | buf[0] = 0x08;
|
---|
240 | buf[1] = 2;
|
---|
241 | buf[20] = 0;
|
---|
242 | /*
|
---|
243 | * the second Databyte should be forseen for a sleeptime in minutes
|
---|
244 | * whoever knows the camd3 protocol related to CMD08 - please help!
|
---|
245 | * on tests this don't work with native camd3
|
---|
246 | */
|
---|
247 | buf[21] = client->c35_sleepsend;
|
---|
248 | cs_log("%s stop request send", client->usr);
|
---|
249 | }
|
---|
250 | else
|
---|
251 | {
|
---|
252 | // Send CW
|
---|
253 | if ((er->rc < 4) || (er->rc == 7))
|
---|
254 | {
|
---|
255 | if (buf[0]==3)
|
---|
256 | memmove(buf + 20 + 16, buf + 20 + buf[1], 0x34);
|
---|
257 | buf[0]++;
|
---|
258 | buf[1] = 16;
|
---|
259 | memcpy(buf+20, er->cw, buf[1]);
|
---|
260 | }
|
---|
261 | else
|
---|
262 | {
|
---|
263 | // Send old CMD44 to prevent cascading problems with older mpcs/oscam versions
|
---|
264 | buf[0] = 0x44;
|
---|
265 | buf[1] = 0;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | camd35_send(buf);
|
---|
269 | camd35_request_emm(er);
|
---|
270 | }
|
---|
271 |
|
---|
272 | static void camd35_process_ecm(uchar *buf)
|
---|
273 | {
|
---|
274 | ECM_REQUEST *er;
|
---|
275 | if (!(er = get_ecmtask()))
|
---|
276 | return;
|
---|
277 | er->l = buf[1];
|
---|
278 | memcpy(client[cs_idx].req + (er->cpti*REQ_SIZE), buf, 0x34 + 20 + er->l); // save request
|
---|
279 | er->srvid = b2i(2, buf+ 8);
|
---|
280 | er->caid = b2i(2, buf+10);
|
---|
281 | er->prid = b2i(4, buf+12);
|
---|
282 | er->pid = b2i(2, buf+16);
|
---|
283 | memcpy(er->ecm, buf + 20, er->l);
|
---|
284 | get_cw(&client[cs_idx], er);
|
---|
285 | }
|
---|
286 |
|
---|
287 | static void camd35_process_emm(uchar *buf)
|
---|
288 | {
|
---|
289 | int au;
|
---|
290 | EMM_PACKET epg;
|
---|
291 | memset(&epg, 0, sizeof(epg));
|
---|
292 | au = client[cs_idx].au;
|
---|
293 | if ((au < 0) || (au > CS_MAXREADER)) return; // TODO
|
---|
294 | epg.l = buf[1];
|
---|
295 | memcpy(epg.caid, buf + 10, 2);
|
---|
296 | memcpy(epg.provid, buf + 12 , 4);
|
---|
297 | memcpy(epg.emm, buf + 20, epg.l);
|
---|
298 | do_emm(&client[cs_idx], &epg);
|
---|
299 | }
|
---|
300 |
|
---|
301 | static void * camd35_server(void *cli)
|
---|
302 | {
|
---|
303 | int n;
|
---|
304 | uchar mbuf[1024];
|
---|
305 |
|
---|
306 | struct s_client * client = (struct s_client *) cli;
|
---|
307 | client->thread=pthread_self();
|
---|
308 |
|
---|
309 | client->req=(uchar *)malloc(CS_MAXPENDING*REQ_SIZE);
|
---|
310 | if (!client->req)
|
---|
311 | {
|
---|
312 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
313 | cs_exit(1);
|
---|
314 | }
|
---|
315 | memset(client->req, 0, CS_MAXPENDING*REQ_SIZE);
|
---|
316 |
|
---|
317 | client->is_udp = (ph[client->ctyp].type == MOD_CONN_UDP);
|
---|
318 |
|
---|
319 | while ((n=process_input(mbuf, sizeof(mbuf), cfg->cmaxidle))>0)
|
---|
320 | {
|
---|
321 | switch(mbuf[0])
|
---|
322 | {
|
---|
323 | case 0: // ECM
|
---|
324 | case 3: // ECM (cascading)
|
---|
325 | camd35_process_ecm(mbuf);
|
---|
326 | break;
|
---|
327 | case 6: // EMM
|
---|
328 | case 19: // EMM
|
---|
329 | camd35_process_emm(mbuf);
|
---|
330 | break;
|
---|
331 | default:
|
---|
332 | cs_log("unknown camd35 command! (%d)", mbuf[0]);
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | if(client->req) { free(client->req); client->req=0;}
|
---|
337 |
|
---|
338 | cs_disconnect_client(client);
|
---|
339 | return NULL; //to prevent compiler message
|
---|
340 | }
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * client functions
|
---|
344 | */
|
---|
345 |
|
---|
346 | static void casc_set_account()
|
---|
347 | {
|
---|
348 | strcpy((char *)client[cs_idx].upwd, reader[client[cs_idx].ridx].r_pwd);
|
---|
349 | memcpy(client[cs_idx].ucrc, i2b(4, crc32(0L, MD5((unsigned char *)reader[client[cs_idx].ridx].r_usr, strlen(reader[client[cs_idx].ridx].r_usr), client[cs_idx].dump), 16)), 4);
|
---|
350 | aes_set_key((char *)MD5(client[cs_idx].upwd, strlen((char *)client[cs_idx].upwd), client[cs_idx].dump));
|
---|
351 | client[cs_idx].crypted=1;
|
---|
352 | }
|
---|
353 |
|
---|
354 | int camd35_client_init()
|
---|
355 | {
|
---|
356 | struct sockaddr_in loc_sa;
|
---|
357 | struct protoent *ptrp;
|
---|
358 | int p_proto;//, sock_type;
|
---|
359 | char ptxt[16];
|
---|
360 |
|
---|
361 | client[cs_idx].pfd=0;
|
---|
362 | if (reader[client[cs_idx].ridx].r_port<=0)
|
---|
363 | {
|
---|
364 | cs_log("invalid port %d for server %s", reader[client[cs_idx].ridx].r_port, reader[client[cs_idx].ridx].device);
|
---|
365 | return(1);
|
---|
366 | }
|
---|
367 | client[cs_idx].is_udp=(reader[client[cs_idx].ridx].typ==R_CAMD35);
|
---|
368 | if( (ptrp=getprotobyname(client[cs_idx].is_udp ? "udp" : "tcp")) )
|
---|
369 | p_proto=ptrp->p_proto;
|
---|
370 | else
|
---|
371 | p_proto=(client[cs_idx].is_udp) ? 17 : 6; // use defaults on error
|
---|
372 |
|
---|
373 | client[cs_idx].ip=0;
|
---|
374 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
375 | loc_sa.sin_family = AF_INET;
|
---|
376 | #ifdef LALL
|
---|
377 | if (cfg->serverip[0])
|
---|
378 | loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip);
|
---|
379 | else
|
---|
380 | #endif
|
---|
381 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
382 | loc_sa.sin_port = htons(reader[client[cs_idx].ridx].l_port);
|
---|
383 |
|
---|
384 | if ((client[cs_idx].udp_fd=socket(PF_INET, client[cs_idx].is_udp ? SOCK_DGRAM : SOCK_STREAM, p_proto))<0)
|
---|
385 | {
|
---|
386 | cs_log("Socket creation failed (errno=%d)", errno);
|
---|
387 | cs_exit(1);
|
---|
388 | }
|
---|
389 |
|
---|
390 | #ifdef SO_PRIORITY
|
---|
391 | if (cfg->netprio)
|
---|
392 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg->netprio, sizeof(ulong));
|
---|
393 | #endif
|
---|
394 |
|
---|
395 | if (reader[client[cs_idx].ridx].l_port>0)
|
---|
396 | {
|
---|
397 | if (bind(client[cs_idx].udp_fd, (struct sockaddr *)&loc_sa, sizeof (loc_sa))<0)
|
---|
398 | {
|
---|
399 | cs_log("bind failed (errno=%d)", errno);
|
---|
400 | close(client[cs_idx].udp_fd);
|
---|
401 | return(1);
|
---|
402 | }
|
---|
403 | sprintf(ptxt, ", port=%d", reader[client[cs_idx].ridx].l_port);
|
---|
404 | }
|
---|
405 | else
|
---|
406 | ptxt[0]='\0';
|
---|
407 |
|
---|
408 | casc_set_account();
|
---|
409 | memset((char *)&client[cs_idx].udp_sa, 0, sizeof(client[cs_idx].udp_sa));
|
---|
410 | client[cs_idx].udp_sa.sin_family=AF_INET;
|
---|
411 | client[cs_idx].udp_sa.sin_port=htons((u_short)reader[client[cs_idx].ridx].r_port);
|
---|
412 |
|
---|
413 | cs_log("proxy %s:%d (fd=%d%s)",
|
---|
414 | reader[client[cs_idx].ridx].device, reader[client[cs_idx].ridx].r_port,
|
---|
415 | client[cs_idx].udp_fd, ptxt);
|
---|
416 |
|
---|
417 | if (client[cs_idx].is_udp) {
|
---|
418 | client[cs_idx].pfd=client[cs_idx].udp_fd;
|
---|
419 | }
|
---|
420 |
|
---|
421 | return(0);
|
---|
422 | }
|
---|
423 |
|
---|
424 | int camd35_client_init_log()
|
---|
425 | {
|
---|
426 | struct sockaddr_in loc_sa;
|
---|
427 | struct protoent *ptrp;
|
---|
428 | int p_proto;
|
---|
429 |
|
---|
430 | if (reader[client[cs_idx].ridx].log_port<=0)
|
---|
431 | {
|
---|
432 | cs_log("invalid port %d for camd3-loghost", reader[client[cs_idx].ridx].log_port);
|
---|
433 | return(1);
|
---|
434 | }
|
---|
435 |
|
---|
436 | ptrp=getprotobyname("udp");
|
---|
437 | if (ptrp)
|
---|
438 | p_proto=ptrp->p_proto;
|
---|
439 | else
|
---|
440 | p_proto=17; // use defaults on error
|
---|
441 |
|
---|
442 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
443 | loc_sa.sin_family = AF_INET;
|
---|
444 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
445 | loc_sa.sin_port = htons(reader[client[cs_idx].ridx].log_port);
|
---|
446 |
|
---|
447 | if ((logfd=socket(PF_INET, SOCK_DGRAM, p_proto))<0)
|
---|
448 | {
|
---|
449 | cs_log("Socket creation failed (errno=%d)", errno);
|
---|
450 | return(1);
|
---|
451 | }
|
---|
452 |
|
---|
453 | if (bind(logfd, (struct sockaddr *)&loc_sa, sizeof(loc_sa))<0)
|
---|
454 | {
|
---|
455 | cs_log("bind failed (errno=%d)", errno);
|
---|
456 | close(logfd);
|
---|
457 | return(1);
|
---|
458 | }
|
---|
459 |
|
---|
460 | cs_log("camd3 loghost initialized (fd=%d, port=%d)",
|
---|
461 | logfd, reader[client[cs_idx].ridx].log_port);
|
---|
462 |
|
---|
463 | return(0);
|
---|
464 | }
|
---|
465 |
|
---|
466 | static int tcp_connect()
|
---|
467 | {
|
---|
468 | if (!reader[client[cs_idx].ridx].tcp_connected)
|
---|
469 | {
|
---|
470 | int handle=0;
|
---|
471 | handle = network_tcp_connection_open();
|
---|
472 | if (handle<0) return(0);
|
---|
473 |
|
---|
474 | reader[client[cs_idx].ridx].tcp_connected = 1;
|
---|
475 | reader[client[cs_idx].ridx].card_status = CARD_INSERTED;
|
---|
476 | reader[client[cs_idx].ridx].last_s = reader[client[cs_idx].ridx].last_g = time((time_t *)0);
|
---|
477 | client[cs_idx].pfd = client[cs_idx].udp_fd = handle;
|
---|
478 | }
|
---|
479 | if (!client[cs_idx].udp_fd) return(0);
|
---|
480 | return(1);
|
---|
481 | }
|
---|
482 |
|
---|
483 | static int camd35_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *buf)
|
---|
484 | {
|
---|
485 | static const char *typtext[]={"ok", "invalid", "sleeping"};
|
---|
486 |
|
---|
487 | if (client->stopped) {
|
---|
488 | if (er->srvid == client->lastsrvid && er->caid == client->lastcaid && er->pid == client->lastpid){
|
---|
489 | cs_log("%s is stopped - requested by server (%s)",
|
---|
490 | reader[client->ridx].label, typtext[client->stopped]);
|
---|
491 | return(-1);
|
---|
492 | }
|
---|
493 | else {
|
---|
494 | client->stopped = 0;
|
---|
495 | }
|
---|
496 | }
|
---|
497 |
|
---|
498 | client->lastsrvid = er->srvid;
|
---|
499 | client->lastcaid = er->caid;
|
---|
500 | client->lastpid = er->pid;
|
---|
501 |
|
---|
502 | if (client->is_udp) {
|
---|
503 | if (!client->udp_sa.sin_addr.s_addr || reader[client->ridx].last_s-reader[client->ridx].last_g > reader[client->ridx].tcp_rto)
|
---|
504 | if (!hostResolve(client->ridx)) return -1;
|
---|
505 | }
|
---|
506 | else {
|
---|
507 | if (!tcp_connect()) return -1;
|
---|
508 | }
|
---|
509 |
|
---|
510 | reader[client->ridx].card_status = CARD_INSERTED; //for udp
|
---|
511 |
|
---|
512 | memset(buf, 0, 20);
|
---|
513 | memset(buf + 20, 0xff, er->l+15);
|
---|
514 | buf[1]=er->l;
|
---|
515 | memcpy(buf + 8, i2b(2, er->srvid), 2);
|
---|
516 | memcpy(buf + 10, i2b(2, er->caid ), 2);
|
---|
517 | memcpy(buf + 12, i2b(4, er->prid ), 4);
|
---|
518 | // memcpy(buf+16, i2b(2, er->pid ), 2);
|
---|
519 | // memcpy(buf+16, &er->idx , 2);
|
---|
520 | memcpy(buf + 16, i2b(2, er->idx ), 2);
|
---|
521 | buf[18] = 0xff;
|
---|
522 | buf[19] = 0xff;
|
---|
523 | memcpy(buf + 20, er->ecm , er->l);
|
---|
524 | return((camd35_send(buf) < 1) ? (-1) : 0);
|
---|
525 | }
|
---|
526 |
|
---|
527 | static int camd35_send_emm(EMM_PACKET *ep)
|
---|
528 | {
|
---|
529 | uchar buf[512];
|
---|
530 |
|
---|
531 | if (client[cs_idx].is_udp) {
|
---|
532 | if (!client[cs_idx].udp_sa.sin_addr.s_addr || reader[client[cs_idx].ridx].last_s-reader[client[cs_idx].ridx].last_g > reader[client[cs_idx].ridx].tcp_rto)
|
---|
533 | if (!hostResolve(client[cs_idx].ridx)) return -1;
|
---|
534 | }
|
---|
535 | else {
|
---|
536 | if (!tcp_connect()) return -1;
|
---|
537 | }
|
---|
538 |
|
---|
539 | memset(buf, 0, 20);
|
---|
540 | memset(buf+20, 0xff, ep->l+15);
|
---|
541 |
|
---|
542 | buf[0]=0x06;
|
---|
543 | buf[1]=ep->l;
|
---|
544 | memcpy(buf+10, ep->caid, 2);
|
---|
545 | memcpy(buf+12, ep->provid, 4);
|
---|
546 | memcpy(buf+20, ep->emm, ep->l);
|
---|
547 |
|
---|
548 | return((camd35_send(buf)<1) ? 0 : 1);
|
---|
549 | }
|
---|
550 |
|
---|
551 | static int camd35_recv_chk(uchar *dcw, int *rc, uchar *buf)
|
---|
552 | {
|
---|
553 | ushort idx;
|
---|
554 | char *typtext[]={"ok", "invalid", "sleeping"};
|
---|
555 |
|
---|
556 | // reading CMD05 Emm request and set serial
|
---|
557 | if (buf[0] == 0x05 && buf[1] == 111) {
|
---|
558 |
|
---|
559 | //cs_log("CMD05: %s", cs_hexdump(1, buf, buf[1]));
|
---|
560 | reader[client[cs_idx].ridx].nprov = 0; //reset if number changes on reader change
|
---|
561 | reader[client[cs_idx].ridx].nprov = buf[47];
|
---|
562 | reader[client[cs_idx].ridx].caid[0] = b2i(2, buf + 20);
|
---|
563 | reader[client[cs_idx].ridx].auprovid = b2i(4, buf + 12);
|
---|
564 |
|
---|
565 | int i;
|
---|
566 | for (i=0; i<reader[client[cs_idx].ridx].nprov; i++) {
|
---|
567 | if (((reader[client[cs_idx].ridx].caid[0] >= 0x1700) && (reader[client[cs_idx].ridx].caid[0] <= 0x1799)) || // Betacrypt
|
---|
568 | ((reader[client[cs_idx].ridx].caid[0] >= 0x0600) && (reader[client[cs_idx].ridx].caid[0] <= 0x0699))) // Irdeto (don't know if this is correct, cause I don't own a IRDETO-Card)
|
---|
569 | {
|
---|
570 | reader[client[cs_idx].ridx].prid[i][0] = buf[48 + (i*5)];
|
---|
571 | memcpy(&reader[client[cs_idx].ridx].prid[i][1], &buf[50 + (i * 5)], 3);
|
---|
572 | } else {
|
---|
573 | reader[client[cs_idx].ridx].prid[i][2] = buf[48 + (i * 5)];
|
---|
574 | reader[client[cs_idx].ridx].prid[i][3] = buf[49+ (i * 5)];
|
---|
575 | memcpy(&reader[client[cs_idx].ridx].sa[i][0], &buf[50 + (i * 5)], 4);
|
---|
576 | }
|
---|
577 | }
|
---|
578 |
|
---|
579 | memcpy(reader[client[cs_idx].ridx].hexserial, buf + 40, 6);
|
---|
580 | reader[client[cs_idx].ridx].hexserial[6] = 0;
|
---|
581 | reader[client[cs_idx].ridx].hexserial[7] = 0;
|
---|
582 |
|
---|
583 | reader[client[cs_idx].ridx].blockemm_g = (buf[128]==1) ? 0: 1;
|
---|
584 | reader[client[cs_idx].ridx].blockemm_s = (buf[129]==1) ? 0: 1;
|
---|
585 | reader[client[cs_idx].ridx].blockemm_u = (buf[130]==1) ? 0: 1;
|
---|
586 | reader[client[cs_idx].ridx].card_system = get_cardsystem(reader[client[cs_idx].ridx].caid[0]);
|
---|
587 | cs_log("%s CMD05 AU request for caid: %04X auprovid: %06lX",
|
---|
588 | reader[client[cs_idx].ridx].label,
|
---|
589 | reader[client[cs_idx].ridx].caid[0],
|
---|
590 | reader[client[cs_idx].ridx].auprovid);
|
---|
591 | }
|
---|
592 |
|
---|
593 | if (buf[0] == 0x08) {
|
---|
594 | if(buf[21] == 0xFF) {
|
---|
595 | client[cs_idx].stopped = 2; // server says sleep
|
---|
596 | reader[client[cs_idx].ridx].card_status = NO_CARD;
|
---|
597 | } else {
|
---|
598 | client[cs_idx].stopped = 1; // server says invalid
|
---|
599 | reader[client[cs_idx].ridx].card_status = CARD_FAILURE;
|
---|
600 | }
|
---|
601 | cs_log("%s CMD08 (%02X - %d) stop request by server (%s)",
|
---|
602 | reader[client[cs_idx].ridx].label, buf[21], buf[21], typtext[client[cs_idx].stopped]);
|
---|
603 | }
|
---|
604 |
|
---|
605 | // CMD44: old reject command introduced in mpcs
|
---|
606 | // keeping this for backward compatibility
|
---|
607 | if ((buf[0] != 1) && (buf[0] != 0x44) && (buf[0] != 0x08))
|
---|
608 | return(-1);
|
---|
609 |
|
---|
610 | idx = b2i(2, buf+16);
|
---|
611 |
|
---|
612 | *rc = ((buf[0] != 0x44) && (buf[0] != 0x08));
|
---|
613 |
|
---|
614 | memcpy(dcw, buf+20, 16);
|
---|
615 | return(idx);
|
---|
616 | }
|
---|
617 |
|
---|
618 | static int camd35_recv_log(ushort *caid, ulong *provid, ushort *srvid)
|
---|
619 | {
|
---|
620 | int i;
|
---|
621 | uchar buf[512], *ptr, *ptr2;
|
---|
622 | ushort idx=0;
|
---|
623 | if (!logfd) return(-1);
|
---|
624 | if ((i=recv(logfd, buf, sizeof(buf), 0))<=0) return(-1);
|
---|
625 | buf[i]=0;
|
---|
626 |
|
---|
627 | if (!(ptr=(uchar *)strstr((char *)buf, " -> "))) return(-1);
|
---|
628 | ptr+=4;
|
---|
629 | if (strstr((char *)ptr, " decoded ")) return(-1); // skip "found"s
|
---|
630 | if (!(ptr2=(uchar *)strchr((char *)ptr, ' '))) return(-1); // corrupt
|
---|
631 | *ptr2=0;
|
---|
632 |
|
---|
633 | for (i=0, ptr2=(uchar *)strtok((char *)ptr, ":"); ptr2; i++, ptr2=(uchar *)strtok(NULL, ":"))
|
---|
634 | {
|
---|
635 | trim((char *)ptr2);
|
---|
636 | switch(i)
|
---|
637 | {
|
---|
638 | case 0: *caid =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
639 | case 1: *provid=cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
640 | case 2: *srvid =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
641 | case 3: idx =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
642 | }
|
---|
643 | if (errno) return(-1);
|
---|
644 | }
|
---|
645 | return(idx&0x1FFF);
|
---|
646 | }
|
---|
647 |
|
---|
648 | /*
|
---|
649 | * module definitions
|
---|
650 | */
|
---|
651 |
|
---|
652 | void module_camd35(struct s_module *ph)
|
---|
653 | {
|
---|
654 | static PTAB ptab; //since there is always only 1 camd35 server running, this is threadsafe
|
---|
655 | ptab.ports[0].s_port = cfg->c35_port;
|
---|
656 | ph->ptab = &ptab;
|
---|
657 | ph->ptab->nports = 1;
|
---|
658 |
|
---|
659 | strcpy(ph->desc, "camd 3.5x");
|
---|
660 | ph->type=MOD_CONN_UDP;
|
---|
661 | ph->multi=1;
|
---|
662 | ph->watchdog=1;
|
---|
663 | ph->s_ip=cfg->c35_srvip;
|
---|
664 | ph->s_handler=camd35_server;
|
---|
665 | ph->recv=camd35_recv;
|
---|
666 | ph->send_dcw=camd35_send_dcw;
|
---|
667 | ph->c_multi=1;
|
---|
668 | ph->c_init=camd35_client_init;
|
---|
669 | ph->c_recv_chk=camd35_recv_chk;
|
---|
670 | ph->c_send_ecm=camd35_send_ecm;
|
---|
671 | ph->c_send_emm=camd35_send_emm;
|
---|
672 | ph->c_init_log=camd35_client_init_log;
|
---|
673 | ph->c_recv_log=camd35_recv_log;
|
---|
674 | ph->num=R_CAMD35;
|
---|
675 | }
|
---|
676 |
|
---|
677 | void module_camd35_tcp(struct s_module *ph)
|
---|
678 | {
|
---|
679 | strcpy(ph->desc, "cs378x");
|
---|
680 | ph->type=MOD_CONN_TCP;
|
---|
681 | ph->multi=1;
|
---|
682 | ph->watchdog=1;
|
---|
683 | ph->ptab=&cfg->c35_tcp_ptab;
|
---|
684 | if (ph->ptab->nports==0)
|
---|
685 | ph->ptab->nports=1; // show disabled in log
|
---|
686 | ph->s_ip=cfg->c35_tcp_srvip;
|
---|
687 | ph->s_handler=camd35_server;
|
---|
688 | ph->recv=camd35_recv;
|
---|
689 | ph->send_dcw=camd35_send_dcw;
|
---|
690 | ph->c_multi=1;
|
---|
691 | ph->c_init=camd35_client_init;
|
---|
692 | ph->c_recv_chk=camd35_recv_chk;
|
---|
693 | ph->c_send_ecm=camd35_send_ecm;
|
---|
694 | ph->c_send_emm=camd35_send_emm;
|
---|
695 | ph->c_init_log=camd35_client_init_log;
|
---|
696 | ph->c_recv_log=camd35_recv_log;
|
---|
697 | ph->num=R_CS378X;
|
---|
698 | }
|
---|