1 | #include "globals.h"
|
---|
2 |
|
---|
3 | /******************************** */
|
---|
4 | /* LINKED LIST CODE - IF IT'S USEFUL ELSEWHERE, IT SHOULD BE SPLIT OFF INTO linkedlist.h/.c */
|
---|
5 | /******************************** */
|
---|
6 |
|
---|
7 | // Simple, doubly linked
|
---|
8 | // This is thread-safe, so requires pthread. Also expect locking if iterators are not destroyed.
|
---|
9 |
|
---|
10 | #include <pthread.h>
|
---|
11 |
|
---|
12 | struct llist_node {
|
---|
13 | void *obj;
|
---|
14 | struct llist_node *prv;
|
---|
15 | struct llist_node *nxt;
|
---|
16 | };
|
---|
17 |
|
---|
18 | typedef struct llist {
|
---|
19 | struct llist_node *first;
|
---|
20 | struct llist_node *last;
|
---|
21 | int items;
|
---|
22 | pthread_mutex_t lock;
|
---|
23 | } LLIST;
|
---|
24 |
|
---|
25 | typedef struct llist_itr {
|
---|
26 | LLIST *l;
|
---|
27 | struct llist_node *cur;
|
---|
28 | } LLIST_ITR;
|
---|
29 |
|
---|
30 | LLIST *llist_create(void); // init linked list
|
---|
31 | void llist_destroy(LLIST *l); // de-init linked list - frees all objects on the list
|
---|
32 |
|
---|
33 | void *llist_append(LLIST *l, void *o); // append object onto bottom of list, returns ptr to obj
|
---|
34 |
|
---|
35 | void *llist_itr_init(LLIST *l, LLIST_ITR *itr); // linked list iterator, returns ptr to first obj
|
---|
36 | //void llist_itr_release(LLIST_ITR *itr); // release iterator
|
---|
37 | void *llist_itr_next(LLIST_ITR *itr); // iterates, returns ptr to next obj
|
---|
38 |
|
---|
39 | void *llist_itr_insert(LLIST_ITR *itr, void *o); // insert object at itr point, iterates to and returns ptr to new obj
|
---|
40 | void *llist_itr_remove(LLIST_ITR *itr); // remove obj at itr, iterates to and returns ptr to next obj
|
---|
41 |
|
---|
42 | int llist_count(LLIST *l); // returns number of obj in list
|
---|
43 |
|
---|
44 | /******************************** */
|
---|
45 |
|
---|
46 | #include <string.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 |
|
---|
49 | LLIST *llist_create(void)
|
---|
50 | {
|
---|
51 | LLIST *l = malloc(sizeof(LLIST));
|
---|
52 | bzero(l, sizeof(LLIST));
|
---|
53 |
|
---|
54 | pthread_mutex_init(&l->lock, NULL);
|
---|
55 |
|
---|
56 | l->items = 0;
|
---|
57 |
|
---|
58 | return l;
|
---|
59 | }
|
---|
60 |
|
---|
61 | void llist_destroy(LLIST *l)
|
---|
62 | {
|
---|
63 | LLIST_ITR itr;
|
---|
64 | void *o = llist_itr_init(l, &itr);
|
---|
65 | while (o) {
|
---|
66 | free(o);
|
---|
67 | o = llist_itr_remove(&itr);
|
---|
68 | }
|
---|
69 | // llist_itr_release(&itr);
|
---|
70 | }
|
---|
71 |
|
---|
72 | void *llist_append(LLIST *l, void *o)
|
---|
73 | {
|
---|
74 | pthread_mutex_lock(&l->lock);
|
---|
75 | if (o) {
|
---|
76 | struct llist_node *ln = malloc(sizeof(struct llist_node));
|
---|
77 |
|
---|
78 | bzero(ln, sizeof(struct llist_node));
|
---|
79 | ln->obj = o;
|
---|
80 |
|
---|
81 | if (l->last) {
|
---|
82 | ln->prv = l->last;
|
---|
83 | ln->prv->nxt = ln;
|
---|
84 | } else {
|
---|
85 | l->first = ln;
|
---|
86 | }
|
---|
87 | l->last = ln;
|
---|
88 |
|
---|
89 | l->items++;
|
---|
90 | }
|
---|
91 | pthread_mutex_unlock(&l->lock);
|
---|
92 |
|
---|
93 | return o;
|
---|
94 | }
|
---|
95 |
|
---|
96 | void *llist_itr_init(LLIST *l, LLIST_ITR *itr)
|
---|
97 | {
|
---|
98 | // pthread_mutex_lock(&l->lock);
|
---|
99 | if (l->first) {
|
---|
100 |
|
---|
101 | bzero(itr, sizeof(LLIST_ITR));
|
---|
102 | itr->cur = l->first;
|
---|
103 | itr->l = l;
|
---|
104 |
|
---|
105 | return itr->cur->obj;
|
---|
106 | }
|
---|
107 |
|
---|
108 | return NULL;
|
---|
109 | }
|
---|
110 | /*
|
---|
111 | void llist_itr_release(LLIST_ITR *itr)
|
---|
112 | {
|
---|
113 | // pthread_mutex_unlock(&itr->l->lock);
|
---|
114 | }
|
---|
115 | */
|
---|
116 | void *llist_itr_next(LLIST_ITR *itr)
|
---|
117 | {
|
---|
118 | if (itr->cur->nxt) {
|
---|
119 | itr->cur = itr->cur->nxt;
|
---|
120 | return itr->cur->obj;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return NULL;
|
---|
124 | }
|
---|
125 |
|
---|
126 | void *llist_itr_remove(LLIST_ITR *itr) // this needs cleaning - I was lazy
|
---|
127 | {
|
---|
128 | itr->l->items--;
|
---|
129 | if ((itr->cur == itr->l->first) && (itr->cur == itr->l->last)) {
|
---|
130 | free(itr->cur);
|
---|
131 | itr->l->first = NULL;
|
---|
132 | itr->l->last = NULL;
|
---|
133 | return NULL;
|
---|
134 | } else if (itr->cur == itr->l->first) {
|
---|
135 | struct llist_node *nxt = itr->cur->nxt;
|
---|
136 | free(itr->cur);
|
---|
137 | nxt->prv = NULL;
|
---|
138 | itr->l->first = nxt;
|
---|
139 | itr->cur = nxt;
|
---|
140 | } else if (itr->cur == itr->l->last) {
|
---|
141 | itr->l->last = itr->cur->prv;
|
---|
142 | itr->l->last->nxt = NULL;
|
---|
143 | free(itr->cur);
|
---|
144 | return NULL;
|
---|
145 | } else {
|
---|
146 | struct llist_node *nxt = itr->cur->nxt;
|
---|
147 | itr->cur->prv->nxt = itr->cur->nxt;
|
---|
148 | itr->cur->nxt->prv = itr->cur->prv;
|
---|
149 | free(itr->cur);
|
---|
150 | itr->cur = nxt;
|
---|
151 | }
|
---|
152 |
|
---|
153 | return itr->cur->obj;
|
---|
154 | }
|
---|
155 |
|
---|
156 | int llist_count(LLIST *l)
|
---|
157 | {
|
---|
158 | return l->items;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /******************************** */
|
---|
162 |
|
---|
163 | #define CC_MAXMSGSIZE 512
|
---|
164 | #define CC_MAX_PROV 16
|
---|
165 |
|
---|
166 | #define SWAPC(X, Y) do { char p; p = *X; *X = *Y; *Y = p; } while(0)
|
---|
167 | #define NULLFREE(X) do { if (X) { free(X); X = NULL; } } while(0)
|
---|
168 |
|
---|
169 | #if (defined(WIN32) || defined(OS_CYGWIN32)) && !defined(MSG_WAITALL)
|
---|
170 | # define MSG_WAITALL 0
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | typedef enum {
|
---|
174 | DECRYPT,
|
---|
175 | ENCRYPT
|
---|
176 | } cc_crypt_mode_t;
|
---|
177 |
|
---|
178 | typedef enum
|
---|
179 | {
|
---|
180 | MSG_CLI_DATA,
|
---|
181 | MSG_CW,
|
---|
182 | MSG_ECM = 1,
|
---|
183 | MSG_CARD_REMOVED = 4,
|
---|
184 | MSG_DCW_SOMETHING, // this still needs to be worked out
|
---|
185 | MSG_PING,
|
---|
186 | MSG_NEW_CARD,
|
---|
187 | MSG_SRV_DATA,
|
---|
188 | MSG_CW_NOK1 = 0xfe,
|
---|
189 | MSG_CW_NOK2 = 0xff,
|
---|
190 | MSG_NO_HEADER = 0xffff
|
---|
191 | } cc_msg_type_t;
|
---|
192 |
|
---|
193 | struct cc_crypt_block
|
---|
194 | {
|
---|
195 | uint8 keytable[256];
|
---|
196 | uint8 state;
|
---|
197 | uint8 counter;
|
---|
198 | uint8 sum;
|
---|
199 | };
|
---|
200 |
|
---|
201 | struct cc_card {
|
---|
202 | uint32 id; // cccam card (share) id
|
---|
203 | uint16 caid;
|
---|
204 | uint8 hop;
|
---|
205 | uint8 key[8]; // card serial (for au)
|
---|
206 | LLIST *provs; // providers
|
---|
207 | LLIST *badsids; // sids that have failed to decode
|
---|
208 | };
|
---|
209 |
|
---|
210 | struct cc_data {
|
---|
211 | struct cc_crypt_block block[2]; // crypto state blocks
|
---|
212 |
|
---|
213 | uint8 node_id[8], // client node id
|
---|
214 | server_node_id[8], // server node id
|
---|
215 | dcw[16]; // control words
|
---|
216 |
|
---|
217 | struct cc_card *cur_card; // ptr to selected card
|
---|
218 | LLIST *cards; // cards list
|
---|
219 |
|
---|
220 | uint32 count;
|
---|
221 | uint16 cur_sid;
|
---|
222 |
|
---|
223 | int last_nok;
|
---|
224 | ECM_REQUEST *found;
|
---|
225 |
|
---|
226 | unsigned long crc;
|
---|
227 |
|
---|
228 | pthread_mutex_t lock;
|
---|
229 | pthread_mutex_t ecm_busy;
|
---|
230 | };
|
---|
231 |
|
---|
232 | static unsigned int seed;
|
---|
233 | static uchar fast_rnd()
|
---|
234 | {
|
---|
235 | unsigned int offset = 12923;
|
---|
236 | unsigned int multiplier = 4079;
|
---|
237 |
|
---|
238 | seed = seed * multiplier + offset;
|
---|
239 | return (uchar)(seed % 0xFF);
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | static void cc_init_crypt(struct cc_crypt_block *block, uint8 *key, int len)
|
---|
244 | {
|
---|
245 | int i = 0 ;
|
---|
246 | uint8 j = 0;
|
---|
247 |
|
---|
248 | for (i=0; i<256; i++) {
|
---|
249 | block->keytable[i] = i;
|
---|
250 | }
|
---|
251 |
|
---|
252 | for (i=0; i<256; i++) {
|
---|
253 | j += key[i % len] + block->keytable[i];
|
---|
254 | SWAPC(&block->keytable[i], &block->keytable[j]);
|
---|
255 | }
|
---|
256 |
|
---|
257 | block->state = *key;
|
---|
258 | block->counter=0;
|
---|
259 | block->sum=0;
|
---|
260 | }
|
---|
261 |
|
---|
262 | static void cc_crypt(struct cc_crypt_block *block, uint8 *data, int len, cc_crypt_mode_t mode)
|
---|
263 | {
|
---|
264 | int i;
|
---|
265 | uint8 z;
|
---|
266 |
|
---|
267 | for (i = 0; i < len; i++) {
|
---|
268 | block->counter++;
|
---|
269 | block->sum += block->keytable[block->counter];
|
---|
270 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
271 | z = data[i];
|
---|
272 | data[i] = z ^ block->keytable[(block->keytable[block->counter] + block->keytable[block->sum]) & 0xff] ^ block->state;
|
---|
273 | if (!mode) z = data[i];
|
---|
274 | block->state = block->state ^ z;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | static void cc_xor(uint8 *buf)
|
---|
279 | {
|
---|
280 | const char cccam[] = "CCcam";
|
---|
281 | uint8 i;
|
---|
282 |
|
---|
283 | for ( i = 0; i < 8; i++ ) {
|
---|
284 | buf[8 + i] = i * buf[i];
|
---|
285 | if ( i <= 5 ) {
|
---|
286 | buf[i] ^= cccam[i];
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | static void cc_cw_decrypt(uint8 *cws)
|
---|
292 | {
|
---|
293 | struct cc_data *cc = reader[ridx].cc;
|
---|
294 |
|
---|
295 | uint32 cur_card = cc->cur_card->id;
|
---|
296 | uint32 node_id_1 = b2i(4, cc->node_id);
|
---|
297 | uint32 node_id_2 = b2i(4, cc->node_id + 4);
|
---|
298 | uint32 tmp;
|
---|
299 | int i;
|
---|
300 |
|
---|
301 | for (i = 0; i < 16; i++) {
|
---|
302 | tmp = cws[i] ^ node_id_2;
|
---|
303 | if (i & 1) {
|
---|
304 | tmp = ~tmp;
|
---|
305 | }
|
---|
306 | cws[i] = cur_card ^ tmp;
|
---|
307 | node_id_2 = (node_id_2 >> 4) | (node_id_1 << 28);
|
---|
308 | node_id_1 >>= 4;
|
---|
309 | cur_card >>= 2;
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | static int cc_msg_recv(uint8 *buf)
|
---|
314 | {
|
---|
315 | int len, flags;
|
---|
316 | uint8 netbuf[CC_MAXMSGSIZE];
|
---|
317 |
|
---|
318 | struct cc_data *cc = reader[ridx].cc;
|
---|
319 | int handle = client[cs_idx].udp_fd;
|
---|
320 |
|
---|
321 | if (handle < 0) return -1;
|
---|
322 |
|
---|
323 | len = recv(handle, netbuf, 4, MSG_WAITALL);
|
---|
324 |
|
---|
325 | if (!len) return 0;
|
---|
326 |
|
---|
327 | if (len != 4) { // invalid header length read
|
---|
328 | cs_log("cccam: invalid header length");
|
---|
329 | return -1;
|
---|
330 | }
|
---|
331 |
|
---|
332 | cc_crypt(&cc->block[DECRYPT], netbuf, 4, DECRYPT);
|
---|
333 | //cs_ddump(netbuf, 4, "cccam: decrypted header:");
|
---|
334 |
|
---|
335 | flags = netbuf[0];
|
---|
336 |
|
---|
337 | if (((netbuf[2] << 8) | netbuf[3]) != 0) { // check if any data is expected in msg
|
---|
338 | if (((netbuf[2] << 8) | netbuf[3]) > CC_MAXMSGSIZE - 2) {
|
---|
339 | cs_log("cccam: message too big");
|
---|
340 | return -1;
|
---|
341 | }
|
---|
342 |
|
---|
343 | len = recv(handle, netbuf+4, (netbuf[2] << 8) | netbuf[3], MSG_WAITALL); // read rest of msg
|
---|
344 |
|
---|
345 | if (len != ((netbuf[2] << 8) | netbuf[3])) {
|
---|
346 | cs_log("cccam: invalid message length read");
|
---|
347 | return -1;
|
---|
348 | }
|
---|
349 |
|
---|
350 | cc_crypt(&cc->block[DECRYPT], netbuf+4, len, DECRYPT);
|
---|
351 | len += 4;
|
---|
352 | }
|
---|
353 |
|
---|
354 | //cs_ddump(netbuf, len, "cccam: full decrypted msg, len=%d:", len);
|
---|
355 |
|
---|
356 | memcpy(buf, netbuf, len);
|
---|
357 | return len;
|
---|
358 | }
|
---|
359 |
|
---|
360 | static int cc_cmd_send(uint8 *buf, int len, cc_msg_type_t cmd)
|
---|
361 | {
|
---|
362 | int n;
|
---|
363 | uint8 *netbuf = malloc(len+4);
|
---|
364 | struct cc_data *cc = reader[ridx].cc;
|
---|
365 |
|
---|
366 | bzero(netbuf, len+4);
|
---|
367 |
|
---|
368 | if (cmd == MSG_NO_HEADER) {
|
---|
369 | memcpy(netbuf, buf, len);
|
---|
370 | } else {
|
---|
371 | // build command message
|
---|
372 | netbuf[0] = 0; // flags??
|
---|
373 | netbuf[1] = cmd & 0xff;
|
---|
374 | netbuf[2] = len >> 8;
|
---|
375 | netbuf[3] = len & 0xff;
|
---|
376 | if (buf) memcpy(netbuf+4, buf, len);
|
---|
377 | len += 4;
|
---|
378 | }
|
---|
379 |
|
---|
380 | cs_ddump(netbuf, len, "cccam: send:");
|
---|
381 | cc_crypt(&cc->block[ENCRYPT], netbuf, len, ENCRYPT);
|
---|
382 |
|
---|
383 | n = send(client[cs_idx].udp_fd, netbuf, len, 0);
|
---|
384 |
|
---|
385 | NULLFREE(netbuf);
|
---|
386 |
|
---|
387 | return n;
|
---|
388 | }
|
---|
389 |
|
---|
390 | static int cc_send_cli_data()
|
---|
391 | {
|
---|
392 | int i;
|
---|
393 | struct cc_data *cc = reader[ridx].cc;
|
---|
394 |
|
---|
395 | cs_debug("cccam: send client data");
|
---|
396 |
|
---|
397 | //memcpy(cc->node_id, "\x7A\xDD\xAB\x28\xC9\x39\x4A\x2F", 8);
|
---|
398 |
|
---|
399 | seed = (unsigned int) time((time_t*)0);
|
---|
400 | for( i=0; i<8; i++ ) cc->node_id[i]=fast_rnd();
|
---|
401 |
|
---|
402 | uint8 buf[CC_MAXMSGSIZE];
|
---|
403 | bzero(buf, CC_MAXMSGSIZE);
|
---|
404 |
|
---|
405 | memcpy(buf, reader[ridx].r_usr, sizeof(reader[ridx].r_usr));
|
---|
406 | memcpy(buf + 20, cc->node_id, 8 );
|
---|
407 | memcpy(buf + 29, reader[ridx].cc_version, sizeof(reader[ridx].cc_version)); // cccam version (ascii)
|
---|
408 | memcpy(buf + 61, reader[ridx].cc_build, sizeof(reader[ridx].cc_build)); // build number (ascii)
|
---|
409 |
|
---|
410 | cs_log ("cccam: user: %s, version: %s, build: %s", reader[ridx].r_usr, reader[ridx].cc_version, reader[ridx].cc_build);
|
---|
411 |
|
---|
412 |
|
---|
413 | return cc_cmd_send(buf, 20 + 8 + 6 + 26 + 4 + 28 + 1, MSG_CLI_DATA);
|
---|
414 | }
|
---|
415 |
|
---|
416 | static int cc_get_nxt_ecm()
|
---|
417 | {
|
---|
418 | int n, i;
|
---|
419 | time_t t;
|
---|
420 | // struct cc_data *cc = reader[ridx].cc;
|
---|
421 |
|
---|
422 | t=time((time_t *)0);
|
---|
423 | for (i = 1, n = 1; i < CS_MAXPENDING; i++)
|
---|
424 | {
|
---|
425 | if ((t-(ulong)ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
426 | (ecmtask[i].rc >= 10)) // drop timeouts
|
---|
427 | {
|
---|
428 | ecmtask[i].rc=0;
|
---|
429 | }
|
---|
430 |
|
---|
431 | if (ecmtask[i].rc >= 10) { // stil active and waiting
|
---|
432 | // search for the ecm with the lowest time, this should be the next to go
|
---|
433 | if ((!n || ecmtask[n].tps.time-ecmtask[i].tps.time < 0) && &ecmtask[n]) n = i;
|
---|
434 | }
|
---|
435 | }
|
---|
436 | return n;
|
---|
437 | }
|
---|
438 |
|
---|
439 | static int cc_send_ecm(ECM_REQUEST *er, uchar *buf)
|
---|
440 | {
|
---|
441 | int n, h = -1;
|
---|
442 | struct cc_data *cc = reader[ridx].cc;
|
---|
443 | struct cc_card *card;
|
---|
444 | LLIST_ITR itr;
|
---|
445 | ECM_REQUEST *cur_er;
|
---|
446 |
|
---|
447 | if (!cc || (pfd < 1)) {
|
---|
448 | if (er) {
|
---|
449 | er->rc = 0;
|
---|
450 | er->rcEx = 0x27;
|
---|
451 | cs_log("cccam: server not init!");
|
---|
452 | write_ecm_answer(fd_c2m, er);
|
---|
453 | }
|
---|
454 | return 0;
|
---|
455 | }
|
---|
456 |
|
---|
457 | // pthread_mutex_lock(&cc->ecm_busy);
|
---|
458 | if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) {
|
---|
459 | cs_debug("cccam: ecm trylock: failed to get lock");
|
---|
460 | return 0;
|
---|
461 | } else {
|
---|
462 | cs_debug("cccam: ecm trylock: got lock");
|
---|
463 | }
|
---|
464 | // pthread_mutex_lock(&cc->lock);
|
---|
465 |
|
---|
466 | if ((n = cc_get_nxt_ecm()) < 0) {
|
---|
467 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
468 | pthread_mutex_unlock(&cc->lock);
|
---|
469 | return 0; // no queued ecms
|
---|
470 | }
|
---|
471 | cur_er = &ecmtask[n];
|
---|
472 |
|
---|
473 | if (crc32(0, cur_er->ecm, cur_er->l) == cc->crc) cur_er->rc = 99;
|
---|
474 | cc->crc = crc32(0, cur_er->ecm, cur_er->l);
|
---|
475 |
|
---|
476 | cs_debug("cccam: ecm crc = 0x%lx", cc->crc);
|
---|
477 |
|
---|
478 | if (cur_er->rc == 99) {
|
---|
479 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
480 | pthread_mutex_unlock(&cc->lock);
|
---|
481 | return 0; // ecm already sent
|
---|
482 | }
|
---|
483 |
|
---|
484 | //cc->found = cur_er;
|
---|
485 |
|
---|
486 | if (buf) memcpy(buf, cur_er->ecm, cur_er->l);
|
---|
487 |
|
---|
488 | cc->cur_card = NULL;
|
---|
489 | cc->cur_sid = cur_er->srvid;
|
---|
490 |
|
---|
491 | card = llist_itr_init(cc->cards, &itr);
|
---|
492 |
|
---|
493 | while (card) {
|
---|
494 | if (card->caid == cur_er->caid) { // caid matches
|
---|
495 | int s = 0;
|
---|
496 |
|
---|
497 | LLIST_ITR sitr;
|
---|
498 | uint16 *sid = llist_itr_init(card->badsids, &sitr);
|
---|
499 | while (sid) {
|
---|
500 | if (*sid == cc->cur_sid) {
|
---|
501 | s = 1;
|
---|
502 | break;
|
---|
503 | }
|
---|
504 | sid = llist_itr_next(&sitr);
|
---|
505 | }
|
---|
506 | // llist_itr_release(&sitr);
|
---|
507 |
|
---|
508 | LLIST_ITR pitr;
|
---|
509 | uint8 *prov = llist_itr_init(card->provs, &pitr);
|
---|
510 | while (prov && !s) {
|
---|
511 | if (b2i(3, prov) == cur_er->prid) { // provid matches
|
---|
512 | if (((h < 0) || (card->hop < h)) && (card->hop <= reader[ridx].cc_maxhop - 1)) { // card is closer and doesn't exceed max hop
|
---|
513 | cc->cur_card = card;
|
---|
514 | h = card->hop; // card has been matched
|
---|
515 | }
|
---|
516 | }
|
---|
517 | prov = llist_itr_next(&pitr);
|
---|
518 | }
|
---|
519 | // llist_itr_release(&pitr);
|
---|
520 | }
|
---|
521 | card = llist_itr_next(&itr);
|
---|
522 | }
|
---|
523 | // llist_itr_release(&itr);
|
---|
524 |
|
---|
525 | if (cc->cur_card) {
|
---|
526 | uint8 *ecmbuf = malloc(cur_er->l+13);
|
---|
527 | bzero(ecmbuf, cur_er->l+13);
|
---|
528 |
|
---|
529 | // build ecm message
|
---|
530 | ecmbuf[0] = cc->cur_card->caid >> 8;
|
---|
531 | ecmbuf[1] = cc->cur_card->caid & 0xff;
|
---|
532 | ecmbuf[2] = cur_er->prid >> 24;
|
---|
533 | ecmbuf[3] = cur_er->prid >> 16;
|
---|
534 | ecmbuf[4] = cur_er->prid >> 8;
|
---|
535 | ecmbuf[5] = cur_er->prid & 0xff;
|
---|
536 | ecmbuf[6] = cc->cur_card->id >> 24;
|
---|
537 | ecmbuf[7] = cc->cur_card->id >> 16;
|
---|
538 | ecmbuf[8] = cc->cur_card->id >> 8;
|
---|
539 | ecmbuf[9] = cc->cur_card->id & 0xff;
|
---|
540 | ecmbuf[10] = cur_er->srvid >> 8;
|
---|
541 | ecmbuf[11] = cur_er->srvid & 0xff;
|
---|
542 | ecmbuf[12] = cur_er->l & 0xff;
|
---|
543 | memcpy(ecmbuf+13, cur_er->ecm, cur_er->l);
|
---|
544 |
|
---|
545 | cc->count = cur_er->idx;
|
---|
546 |
|
---|
547 | cs_log("cccam: sending ecm for sid %04x to card %08x, hop %d", cur_er->srvid, cc->cur_card->id, cc->cur_card->hop + 1);
|
---|
548 | n = cc_cmd_send(ecmbuf, cur_er->l+13, MSG_ECM); // send ecm
|
---|
549 |
|
---|
550 | NULLFREE(ecmbuf);
|
---|
551 | } else {
|
---|
552 | n = -1;
|
---|
553 | cs_log("cccam: no suitable card on server");
|
---|
554 | cur_er->rc = 0;
|
---|
555 | cur_er->rcEx = 0x27;
|
---|
556 | //cur_er->rc = 1;
|
---|
557 | //cur_er->rcEx = 0;
|
---|
558 | usleep(100000);
|
---|
559 | write_ecm_answer(fd_c2m, cur_er);
|
---|
560 | //reader[ridx].last_s = reader[ridx].last_g;
|
---|
561 |
|
---|
562 | card = llist_itr_init(cc->cards, &itr);
|
---|
563 | while (card) {
|
---|
564 | if (card->caid == cur_er->caid) { // caid matches
|
---|
565 | LLIST_ITR sitr;
|
---|
566 | uint16 *sid = llist_itr_init(card->badsids, &sitr);
|
---|
567 | while (sid) {
|
---|
568 | if (*sid == cur_er->srvid)
|
---|
569 | sid = llist_itr_remove(&sitr);
|
---|
570 | else sid = llist_itr_next(&sitr);
|
---|
571 | }
|
---|
572 | // llist_itr_release(&sitr);
|
---|
573 | }
|
---|
574 | card = llist_itr_next(&itr);
|
---|
575 | }
|
---|
576 | // llist_itr_release(&itr);
|
---|
577 |
|
---|
578 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
579 | }
|
---|
580 |
|
---|
581 | return 0;
|
---|
582 | }
|
---|
583 | /*
|
---|
584 | static int cc_abort_user_ecms(){
|
---|
585 | int n, i;
|
---|
586 | time_t t;//, tls;
|
---|
587 | struct cc_data *cc = reader[ridx].cc;
|
---|
588 |
|
---|
589 | t=time((time_t *)0);
|
---|
590 | for (i=1,n=1; i<CS_MAXPENDING; i++)
|
---|
591 | {
|
---|
592 | if ((t-ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
593 | (ecmtask[i].rc>=10)) // drop timeouts
|
---|
594 | {
|
---|
595 | ecmtask[i].rc=0;
|
---|
596 | }
|
---|
597 | int td=abs(1000*(ecmtask[i].tps.time-cc->found->tps.time)+ecmtask[i].tps.millitm-cc->found->tps.millitm);
|
---|
598 | if (ecmtask[i].rc>=10 && ecmtask[i].cidx==cc->found->cidx && &ecmtask[i]!=cc->found){
|
---|
599 | cs_log("aborting idx:%d caid:%04x client:%d timedelta:%d",ecmtask[i].idx,ecmtask[i].caid,ecmtask[i].cidx,td);
|
---|
600 | ecmtask[i].rc=0;
|
---|
601 | ecmtask[i].rcEx=7;
|
---|
602 | write_ecm_answer(fd_c2m, &ecmtask[i]);
|
---|
603 | }
|
---|
604 | }
|
---|
605 | return n;
|
---|
606 |
|
---|
607 | }
|
---|
608 | */
|
---|
609 | static cc_msg_type_t cc_parse_msg(uint8 *buf, int l)
|
---|
610 | {
|
---|
611 | int ret = buf[1];
|
---|
612 | struct cc_data *cc = reader[ridx].cc;
|
---|
613 |
|
---|
614 | switch (buf[1]) {
|
---|
615 | case MSG_CLI_DATA:
|
---|
616 | cs_debug("cccam: client data ack");
|
---|
617 | break;
|
---|
618 | case MSG_SRV_DATA:
|
---|
619 | memcpy(cc->server_node_id, buf+4, 8);
|
---|
620 | cs_log("cccam: srv %s running v%s (%s)", cs_hexdump(0, cc->server_node_id, 8), buf+12, buf+44);
|
---|
621 | break;
|
---|
622 | case MSG_NEW_CARD:
|
---|
623 | {
|
---|
624 | int i = 0;
|
---|
625 | struct cc_card *card = malloc(sizeof(struct cc_card));
|
---|
626 |
|
---|
627 | bzero(card, sizeof(struct cc_card));
|
---|
628 |
|
---|
629 | card->provs = llist_create();
|
---|
630 | card->badsids = llist_create();
|
---|
631 | card->id = b2i(4, buf+4);
|
---|
632 | card->caid = b2i(2, buf+12);
|
---|
633 | card->hop = buf[14];
|
---|
634 | memcpy(card->key, buf+16, 8);
|
---|
635 |
|
---|
636 | cs_debug("cccam: card %08x added, caid %04x, hop %d, key %s",
|
---|
637 | card->id, card->caid, card->hop, cs_hexdump(0, card->key, 8));
|
---|
638 |
|
---|
639 | for (i = 0; i < buf[24]; i++) { // providers
|
---|
640 | uint8 *prov = malloc(3);
|
---|
641 |
|
---|
642 | memcpy(prov, buf+25+(7*i), 3);
|
---|
643 | cs_debug(" prov %d, %06x", i+1, b2i(3, prov));
|
---|
644 |
|
---|
645 | llist_append(card->provs, prov);
|
---|
646 | }
|
---|
647 |
|
---|
648 | llist_append(cc->cards, card);
|
---|
649 | if (!cc->cur_card) cc->cur_card = card;
|
---|
650 | }
|
---|
651 | break;
|
---|
652 | case MSG_CARD_REMOVED:
|
---|
653 | {
|
---|
654 | struct cc_card *card;
|
---|
655 | LLIST_ITR itr;
|
---|
656 |
|
---|
657 | card = llist_itr_init(cc->cards, &itr);
|
---|
658 | while (card) {
|
---|
659 | if (card->id == b2i(4, buf+4)) {
|
---|
660 | cs_debug("cccam: card %08x removed, caid %04x", card->id, card->caid);
|
---|
661 |
|
---|
662 | llist_destroy(card->provs);
|
---|
663 | llist_destroy(card->badsids);
|
---|
664 | free(card);
|
---|
665 |
|
---|
666 | card = llist_itr_remove(&itr);
|
---|
667 | break;
|
---|
668 | } else {
|
---|
669 | card = llist_itr_next(&itr);
|
---|
670 | }
|
---|
671 | }
|
---|
672 | // llist_itr_release(&itr);
|
---|
673 | }
|
---|
674 | break;
|
---|
675 | case MSG_CW_NOK1:
|
---|
676 | case MSG_CW_NOK2:
|
---|
677 | cs_log("cccam: cw nok, sid = %x", cc->cur_sid);
|
---|
678 |
|
---|
679 | int f = 0;
|
---|
680 | LLIST_ITR itr;
|
---|
681 | uint16 *sid = llist_itr_init(cc->cur_card->badsids, &itr);
|
---|
682 | while (sid && !f) {
|
---|
683 | if (*sid == cc->cur_sid) {
|
---|
684 | // llist_itr_release(&itr);
|
---|
685 | f = 1;
|
---|
686 | }
|
---|
687 | sid = llist_itr_next(&itr);
|
---|
688 | }
|
---|
689 | // llist_itr_release(&itr);
|
---|
690 |
|
---|
691 | if (!f) {
|
---|
692 | sid = malloc(sizeof(uint16));
|
---|
693 | *sid = cc->cur_sid;
|
---|
694 |
|
---|
695 | sid = llist_append(cc->cur_card->badsids, sid);
|
---|
696 | cs_debug(" added sid block for card %08x", cc->cur_card->id);
|
---|
697 | }
|
---|
698 | bzero(cc->dcw, 16);
|
---|
699 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
700 | cc_send_ecm(NULL, NULL);
|
---|
701 | ret = 0;
|
---|
702 | break;
|
---|
703 | case MSG_CW:
|
---|
704 | cc_cw_decrypt(buf+4);
|
---|
705 | memcpy(cc->dcw, buf+4, 16);
|
---|
706 | cs_debug("cccam: cws: %s", cs_hexdump(0, cc->dcw, 16));
|
---|
707 | cc_crypt(&cc->block[DECRYPT], buf+4, l-4, ENCRYPT); // additional crypto step
|
---|
708 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
709 | //cc_abort_user_ecms();
|
---|
710 | cc_send_ecm(NULL, NULL);
|
---|
711 | ret = 0;
|
---|
712 | break;
|
---|
713 | case MSG_PING:
|
---|
714 | cc_cmd_send(NULL, 0, MSG_PING);
|
---|
715 | break;
|
---|
716 | default:
|
---|
717 | break;
|
---|
718 | }
|
---|
719 |
|
---|
720 | return ret;
|
---|
721 | }
|
---|
722 |
|
---|
723 | static int cc_recv_chk(uchar *dcw, int *rc, uchar *buf)
|
---|
724 | {
|
---|
725 | struct cc_data *cc = reader[ridx].cc;
|
---|
726 |
|
---|
727 | if (buf[1] == MSG_CW) {
|
---|
728 | memcpy(dcw, cc->dcw, 16);
|
---|
729 | cs_debug("cccam: recv chk - MSG_CW %d - %s", cc->count, cs_hexdump(0, dcw, 16));
|
---|
730 | *rc = 1;
|
---|
731 | return(cc->count);
|
---|
732 | } else if ((buf[1] == (MSG_CW_NOK1)) || (buf[1] == (MSG_CW_NOK2))) {
|
---|
733 | memset(dcw, 0, 16);
|
---|
734 | return *rc = 0;
|
---|
735 | return(cc->count);
|
---|
736 | }
|
---|
737 |
|
---|
738 | return (-1);
|
---|
739 | }
|
---|
740 |
|
---|
741 | int cc_recv(uchar *buf, int l)
|
---|
742 | {
|
---|
743 | int n;
|
---|
744 | uchar *cbuf;
|
---|
745 | struct cc_data *cc = reader[ridx].cc;
|
---|
746 |
|
---|
747 | if (buf==NULL) return -1;
|
---|
748 | cbuf = malloc(l);
|
---|
749 | if (cbuf==NULL) return -1;
|
---|
750 |
|
---|
751 | memcpy(cbuf, buf, l); // make a copy of buf
|
---|
752 |
|
---|
753 | pthread_mutex_lock(&cc->lock);
|
---|
754 |
|
---|
755 | if (!is_server) {
|
---|
756 | if (!client[cs_idx].udp_fd) return(-1);
|
---|
757 | n = cc_msg_recv(cbuf); // recv and decrypt msg
|
---|
758 | } else {
|
---|
759 | return -2;
|
---|
760 | }
|
---|
761 |
|
---|
762 | cs_ddump(cbuf, n, "cccam: received %d bytes from %s", n, remote_txt());
|
---|
763 | client[cs_idx].last = time((time_t *) 0);
|
---|
764 |
|
---|
765 | if (n == 0) {
|
---|
766 | cs_log("cccam: Connection closed to %s", remote_txt());
|
---|
767 | n = -1;
|
---|
768 | } else if (n < 4) {
|
---|
769 | cs_log("cccam: packet to small (%d bytes)", n);
|
---|
770 | n = -1;
|
---|
771 | } else {
|
---|
772 | // parse it and write it back, if we have received something of value
|
---|
773 | cc_parse_msg(cbuf, n);
|
---|
774 | memcpy(buf, cbuf, l);
|
---|
775 | }
|
---|
776 |
|
---|
777 | NULLFREE(cbuf);
|
---|
778 |
|
---|
779 | pthread_mutex_unlock(&cc->lock);
|
---|
780 |
|
---|
781 | return(n);
|
---|
782 | }
|
---|
783 |
|
---|
784 | static int cc_cli_connect(void)
|
---|
785 | {
|
---|
786 | int handle, n;
|
---|
787 | uint8 data[20];
|
---|
788 | uint8 hash[SHA_DIGEST_LENGTH];
|
---|
789 | uint8 buf[CC_MAXMSGSIZE];
|
---|
790 | char pwd[64];
|
---|
791 | struct cc_data *cc;
|
---|
792 |
|
---|
793 | if (reader[ridx].cc) NULLFREE(reader[ridx].cc);
|
---|
794 |
|
---|
795 | // init internals data struct
|
---|
796 | cc = malloc(sizeof(struct cc_data));
|
---|
797 | if (cc==NULL) {
|
---|
798 | cs_log("cccam: cannot allocate memory");
|
---|
799 | return -1;
|
---|
800 | }
|
---|
801 | reader[ridx].cc = cc;
|
---|
802 | bzero(reader[ridx].cc, sizeof(struct cc_data));
|
---|
803 | cc->cards = llist_create();
|
---|
804 |
|
---|
805 | // check cred config
|
---|
806 | if(reader[ridx].device[0] == 0 || reader[ridx].r_pwd[0] == 0 ||
|
---|
807 | reader[ridx].r_usr[0] == 0 || reader[ridx].r_port == 0)
|
---|
808 | return -5;
|
---|
809 |
|
---|
810 | // connect
|
---|
811 | handle = network_tcp_connection_open();
|
---|
812 | if(handle < 0) return -1;
|
---|
813 |
|
---|
814 | // get init seed
|
---|
815 | if((n = recv(handle, data, 16, MSG_WAITALL)) != 16) {
|
---|
816 | cs_log("cccam: server does not return 16 bytes");
|
---|
817 | network_tcp_connection_close(handle);
|
---|
818 | return -2;
|
---|
819 | }
|
---|
820 | cs_ddump(data, 16, "cccam: server init seed:");
|
---|
821 |
|
---|
822 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
823 |
|
---|
824 | SHA_CTX ctx;
|
---|
825 | SHA1_Init(&ctx);
|
---|
826 | SHA1_Update(&ctx, data, 16);
|
---|
827 | SHA1_Final(hash, &ctx);
|
---|
828 |
|
---|
829 | cs_ddump(hash, sizeof(hash), "cccam: sha1 hash:");
|
---|
830 |
|
---|
831 | //initialisate crypto states
|
---|
832 | cc_init_crypt(&cc->block[DECRYPT], hash, 20);
|
---|
833 | cc_crypt(&cc->block[DECRYPT], data, 16, DECRYPT);
|
---|
834 | cc_init_crypt(&cc->block[ENCRYPT], data, 16);
|
---|
835 | cc_crypt(&cc->block[ENCRYPT], hash, 20, DECRYPT);
|
---|
836 |
|
---|
837 | cc_cmd_send(hash, 20, MSG_NO_HEADER); // send crypted hash to server
|
---|
838 |
|
---|
839 | bzero(buf, sizeof(buf));
|
---|
840 | memcpy(buf, reader[ridx].r_usr, strlen(reader[ridx].r_usr));
|
---|
841 | cs_ddump(buf, 20, "cccam: username '%s':", buf);
|
---|
842 | cc_cmd_send(buf, 20, MSG_NO_HEADER); // send usr '0' padded -> 20 bytes
|
---|
843 |
|
---|
844 | bzero(buf, sizeof(buf));
|
---|
845 | bzero(pwd, sizeof(pwd));
|
---|
846 |
|
---|
847 | cs_debug("cccam: 'CCcam' xor");
|
---|
848 | memcpy(buf, "CCcam", 5);
|
---|
849 | strncpy(pwd, reader[ridx].r_pwd, 63);
|
---|
850 | cc_crypt(&cc->block[ENCRYPT], (uint8 *)pwd, strlen(pwd), ENCRYPT);
|
---|
851 | cc_cmd_send(buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd
|
---|
852 |
|
---|
853 | if ((n = recv(handle, data, 20, MSG_WAITALL)) != 20) {
|
---|
854 | cs_log("cccam: login failed, pwd ack not received (n = %d)", n);
|
---|
855 | return -2;
|
---|
856 | }
|
---|
857 | cc_crypt(&cc->block[DECRYPT], data, 20, DECRYPT);
|
---|
858 | cs_ddump(data, 20, "cccam: pwd ack received:");
|
---|
859 |
|
---|
860 | if (memcmp(data, buf, 5)) { // check server response
|
---|
861 | cs_log("cccam: login failed, usr/pwd invalid");
|
---|
862 | return -2;
|
---|
863 | } else {
|
---|
864 | cs_debug("cccam: login succeeded");
|
---|
865 | }
|
---|
866 |
|
---|
867 | reader[ridx].tcp_connected = 1;
|
---|
868 | reader[ridx].last_g = reader[ridx].last_s = time((time_t *)0);
|
---|
869 |
|
---|
870 | cs_debug("cccam: last_s=%d, last_g=%d", reader[ridx].last_s, reader[ridx].last_g);
|
---|
871 |
|
---|
872 | pfd=client[cs_idx].udp_fd;
|
---|
873 |
|
---|
874 | if (cc_send_cli_data()<=0) {
|
---|
875 | cs_log("cccam: login failed, could not send client data");
|
---|
876 | return -3;
|
---|
877 | }
|
---|
878 |
|
---|
879 | pthread_mutex_init(&cc->lock, NULL);
|
---|
880 | pthread_mutex_init(&cc->ecm_busy, NULL);
|
---|
881 |
|
---|
882 | return 0;
|
---|
883 | }
|
---|
884 |
|
---|
885 | int cc_cli_init(void)
|
---|
886 | {
|
---|
887 | if (!reader[ridx].tcp_connected) {
|
---|
888 | static struct sockaddr_in loc_sa;
|
---|
889 | struct protoent *ptrp;
|
---|
890 | int p_proto;
|
---|
891 |
|
---|
892 | pfd=0;
|
---|
893 | if (reader[ridx].r_port<=0)
|
---|
894 | {
|
---|
895 | cs_log("cccam: invalid port %d for server %s", reader[ridx].r_port, reader[ridx].device);
|
---|
896 | return(1);
|
---|
897 | }
|
---|
898 | if( (ptrp=getprotobyname("tcp")) )
|
---|
899 | p_proto=ptrp->p_proto;
|
---|
900 | else
|
---|
901 | p_proto=6;
|
---|
902 |
|
---|
903 | client[cs_idx].ip=0;
|
---|
904 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
905 | loc_sa.sin_family = AF_INET;
|
---|
906 | #ifdef LALL
|
---|
907 | if (cfg->serverip[0])
|
---|
908 | loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip);
|
---|
909 | else
|
---|
910 | #endif
|
---|
911 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
912 | loc_sa.sin_port = htons(reader[ridx].l_port);
|
---|
913 |
|
---|
914 | if ((client[cs_idx].udp_fd=socket(PF_INET, SOCK_STREAM, p_proto))<0)
|
---|
915 | {
|
---|
916 | cs_log("cccam: Socket creation failed (errno=%d)", errno);
|
---|
917 | cs_exit(1);
|
---|
918 | }
|
---|
919 |
|
---|
920 | #ifdef SO_PRIORITY
|
---|
921 | if (cfg->netprio)
|
---|
922 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
923 | (void *)&cfg->netprio, sizeof(ulong));
|
---|
924 | #endif
|
---|
925 | if (!reader[ridx].tcp_ito) {
|
---|
926 | ulong keep_alive = reader[ridx].tcp_ito?1:0;
|
---|
927 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
928 | (void *)&keep_alive, sizeof(ulong));
|
---|
929 | }
|
---|
930 |
|
---|
931 | memset((char *)&client[cs_idx].udp_sa,0,sizeof(client[cs_idx].udp_sa));
|
---|
932 | client[cs_idx].udp_sa.sin_family = AF_INET;
|
---|
933 | client[cs_idx].udp_sa.sin_port = htons((u_short)reader[ridx].r_port);
|
---|
934 |
|
---|
935 | struct hostent *server;
|
---|
936 | server = gethostbyname(reader[ridx].device);
|
---|
937 | bcopy((char *)server->h_addr, (char *)&client[cs_idx].udp_sa.sin_addr.s_addr, server->h_length);
|
---|
938 |
|
---|
939 | if (reader[ridx].tcp_rto <= 0) reader[ridx].tcp_rto = 60 * 60 * 10; // timeout to 10 hours
|
---|
940 | cs_debug("cccam: reconnect timeout set to: %d", reader[ridx].tcp_rto);
|
---|
941 | if (!reader[ridx].cc_maxhop) reader[ridx].cc_maxhop = 10; // default maxhop to 10 if not configured
|
---|
942 |
|
---|
943 | cs_log("cccam: proxy %s:%d cccam v%s (%s), maxhop = %d (fd=%d, ridx=%d)",
|
---|
944 | reader[ridx].device, reader[ridx].r_port, reader[ridx].cc_version,
|
---|
945 | reader[ridx].cc_build, reader[ridx].cc_maxhop, client[cs_idx].udp_fd, ridx);
|
---|
946 |
|
---|
947 | cc_cli_connect();
|
---|
948 |
|
---|
949 | return(0);
|
---|
950 | }
|
---|
951 | return(-1);
|
---|
952 | }
|
---|
953 |
|
---|
954 | void cc_cleanup(void)
|
---|
955 | {
|
---|
956 |
|
---|
957 | }
|
---|
958 |
|
---|
959 | void module_cccam(struct s_module *ph)
|
---|
960 | {
|
---|
961 | strcpy(ph->desc, "cccam");
|
---|
962 | ph->type=MOD_CONN_TCP;
|
---|
963 | ph->logtxt = ", crypted";
|
---|
964 | ph->watchdog=1;
|
---|
965 | ph->recv=cc_recv;
|
---|
966 | ph->cleanup=cc_cleanup;
|
---|
967 | ph->c_multi=1;
|
---|
968 | ph->c_init=cc_cli_init;
|
---|
969 | ph->c_recv_chk=cc_recv_chk;
|
---|
970 | ph->c_send_ecm=cc_send_ecm;
|
---|
971 | }
|
---|