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