1 | #include <string.h>
|
---|
2 | #include <stdlib.h>
|
---|
3 | #include "globals.h"
|
---|
4 | #include "module-cccam.h"
|
---|
5 | #include <time.h>
|
---|
6 | #include "reader-common.h"
|
---|
7 | #include <poll.h>
|
---|
8 |
|
---|
9 | extern int pthread_mutexattr_settype(pthread_mutexattr_t *__attr, int __kind); //Needs extern defined???
|
---|
10 |
|
---|
11 | //Mode names for CMD_05 command:
|
---|
12 | const char *cmd05_mode_name[] = { "UNKNOWN", "PLAIN", "AES", "CC_CRYPT", "RC4",
|
---|
13 | "LEN=0" };
|
---|
14 |
|
---|
15 | //Mode names for CMD_0C command:
|
---|
16 | const char *cmd0c_mode_name[] = { "NONE", "RC6", "RC4", "CC_CRYPT", "AES", "IDEA" };
|
---|
17 |
|
---|
18 | static uint8 cc_node_id[8];
|
---|
19 |
|
---|
20 | #define getprefix() ((struct cc_data *)(cl->cc))->prefix
|
---|
21 | #define QUITERROR 1
|
---|
22 |
|
---|
23 | void cc_init_crypt(struct cc_crypt_block *block, uint8 *key, int len) {
|
---|
24 | int i = 0;
|
---|
25 | uint8 j = 0;
|
---|
26 |
|
---|
27 | for (i = 0; i < 256; i++) {
|
---|
28 | block->keytable[i] = i;
|
---|
29 | }
|
---|
30 |
|
---|
31 | for (i = 0; i < 256; i++) {
|
---|
32 | j += key[i % len] + block->keytable[i];
|
---|
33 | SWAPC(&block->keytable[i], &block->keytable[j]);
|
---|
34 | }
|
---|
35 |
|
---|
36 | block->state = *key;
|
---|
37 | block->counter = 0;
|
---|
38 | block->sum = 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void cc_crypt(struct cc_crypt_block *block, uint8 *data, int len,
|
---|
42 | cc_crypt_mode_t mode) {
|
---|
43 | int i;
|
---|
44 | uint8 z;
|
---|
45 |
|
---|
46 | for (i = 0; i < len; i++) {
|
---|
47 | block->counter++;
|
---|
48 | block->sum += block->keytable[block->counter];
|
---|
49 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
50 | z = data[i];
|
---|
51 | data[i] = z ^ block->keytable[(block->keytable[block->counter]
|
---|
52 | + block->keytable[block->sum]) & 0xff];
|
---|
53 | data[i] ^= block->state;
|
---|
54 | if (!mode)
|
---|
55 | z = data[i];
|
---|
56 | block->state = block->state ^ z;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | void cc_rc4_crypt(struct cc_crypt_block *block, uint8 *data, int len,
|
---|
61 | cc_crypt_mode_t mode) {
|
---|
62 | int i;
|
---|
63 | uint8 z;
|
---|
64 |
|
---|
65 | for (i = 0; i < len; i++) {
|
---|
66 | block->counter++;
|
---|
67 | block->sum += block->keytable[block->counter];
|
---|
68 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
69 | z = data[i];
|
---|
70 | data[i] = z ^ block->keytable[(block->keytable[block->counter]
|
---|
71 | + block->keytable[block->sum]) & 0xff];
|
---|
72 | if (!mode)
|
---|
73 | z = data[i];
|
---|
74 | block->state = block->state ^ z;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | void cc_xor(uint8 *buf) {
|
---|
79 | const char cccam[] = "CCcam";
|
---|
80 | uint8 i;
|
---|
81 |
|
---|
82 | for (i = 0; i < 8; i++) {
|
---|
83 | buf[8 + i] = i * buf[i];
|
---|
84 | if (i <= 5) {
|
---|
85 | buf[i] ^= cccam[i];
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void cc_cw_crypt(struct s_client *cl, uint8 *cws, uint32 cardid) {
|
---|
91 | struct cc_data *cc = cl->cc;
|
---|
92 | int64 node_id;
|
---|
93 | uint8 tmp;
|
---|
94 | int i;
|
---|
95 |
|
---|
96 | if (cl->typ != 'c') {
|
---|
97 | node_id = b2ll(8, cc->node_id);
|
---|
98 | } else {
|
---|
99 | node_id = b2ll(8, cc->peer_node_id);
|
---|
100 | }
|
---|
101 |
|
---|
102 | for (i = 0; i < 16; i++) {
|
---|
103 | tmp = cws[i] ^ (node_id >> (4 * i));
|
---|
104 | if (i & 1)
|
---|
105 | tmp = ~tmp;
|
---|
106 | cws[i] = (cardid >> (2 * i)) ^ tmp;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** swap endianness (int) */
|
---|
111 | static void SwapLBi(unsigned char *buff, int len)
|
---|
112 | {
|
---|
113 | #if __BYTE_ORDER != __BIG_ENDIAN
|
---|
114 | return;
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | int i;
|
---|
118 | unsigned char swap[4];
|
---|
119 | for (i = 0; i < len / 4; i++) {
|
---|
120 | memcpy(swap, buff, 4);
|
---|
121 | buff[0] = swap[3];
|
---|
122 | buff[1] = swap[2];
|
---|
123 | buff[2] = swap[1];
|
---|
124 | buff[3] = swap[0];
|
---|
125 | buff += 4;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | void cc_crypt_cmd0c(struct s_client *cl, uint8 *buf, int len) {
|
---|
130 | struct cc_data *cc = cl->cc;
|
---|
131 | uint8 *out = cs_malloc(&out, len, QUITERROR);
|
---|
132 |
|
---|
133 | switch (cc->cmd0c_mode) {
|
---|
134 | case MODE_CMD_0x0C_NONE: { // none additional encryption
|
---|
135 | memcpy(out, buf, len);
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | case MODE_CMD_0x0C_RC6 : { //RC6
|
---|
139 | int i;
|
---|
140 | SwapLBi(buf, len);
|
---|
141 | for (i = 0; i < len / 16; i++)
|
---|
142 | rc6_block_decrypt((unsigned int*)(buf+i*16), (unsigned int*)(out+i*16), 1, cc->cmd0c_RC6_cryptkey);
|
---|
143 | SwapLBi(out, len);
|
---|
144 | break;
|
---|
145 | }
|
---|
146 | case MODE_CMD_0x0C_RC4: { // RC4
|
---|
147 | cc_rc4_crypt(&cc->cmd0c_cryptkey, buf, len, ENCRYPT);
|
---|
148 | memcpy(out, buf, len);
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | case MODE_CMD_0x0C_CC_CRYPT: { // cc_crypt
|
---|
152 | cc_crypt(&cc->cmd0c_cryptkey, buf, len, DECRYPT);
|
---|
153 | memcpy(out, buf, len);
|
---|
154 | break;
|
---|
155 | }
|
---|
156 | case MODE_CMD_0x0C_AES: { // AES
|
---|
157 | int i;
|
---|
158 | for (i = 0; i<len / 16; i++)
|
---|
159 | AES_decrypt((unsigned char *) buf + i * 16,
|
---|
160 | (unsigned char *) out + i * 16, &cc->cmd0c_AES_key);
|
---|
161 | break;
|
---|
162 | }
|
---|
163 | case MODE_CMD_0x0C_IDEA : { //IDEA
|
---|
164 | int i=0;
|
---|
165 | int j;
|
---|
166 |
|
---|
167 | while (i < len) {
|
---|
168 | idea_ecb_encrypt(buf + i, out + i, &cc->cmd0c_IDEA_dkey);
|
---|
169 | i += 8;
|
---|
170 | }
|
---|
171 |
|
---|
172 | i = 8;
|
---|
173 | while (i < len) {
|
---|
174 | for (j=0; j < 8; j++)
|
---|
175 | out[j+i] ^= buf[j+i-8];
|
---|
176 | i += 8;
|
---|
177 | }
|
---|
178 |
|
---|
179 | break;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | memcpy(buf, out, len);
|
---|
183 | free(out);
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 |
|
---|
188 | void set_cmd0c_cryptkey(struct s_client *cl, uint8 *key, uint8 len) {
|
---|
189 | struct cc_data *cc = cl->cc;
|
---|
190 | uint8 key_buf[32];
|
---|
191 |
|
---|
192 | memset(&key_buf, 0, sizeof(key_buf));
|
---|
193 |
|
---|
194 | if (len > 32)
|
---|
195 | len = 32;
|
---|
196 |
|
---|
197 | memcpy(key_buf, key, len);
|
---|
198 |
|
---|
199 | switch (cc->cmd0c_mode) {
|
---|
200 |
|
---|
201 | case MODE_CMD_0x0C_NONE : { //NONE
|
---|
202 | break;
|
---|
203 | }
|
---|
204 |
|
---|
205 | case MODE_CMD_0x0C_RC6 : { //RC6
|
---|
206 | rc6_key_setup(key_buf, 32, cc->cmd0c_RC6_cryptkey);
|
---|
207 | break;
|
---|
208 | }
|
---|
209 |
|
---|
210 | case MODE_CMD_0x0C_RC4: //RC4
|
---|
211 | case MODE_CMD_0x0C_CC_CRYPT: { //CC_CRYPT
|
---|
212 | cc_init_crypt(&cc->cmd0c_cryptkey, key_buf, 32);
|
---|
213 | break;
|
---|
214 | }
|
---|
215 |
|
---|
216 | case MODE_CMD_0x0C_AES: { //AES
|
---|
217 | memset(&cc->cmd0c_AES_key, 0, sizeof(cc->cmd0c_AES_key));
|
---|
218 | AES_set_decrypt_key((unsigned char *) key_buf, 256, &cc->cmd0c_AES_key);
|
---|
219 | break;
|
---|
220 | }
|
---|
221 |
|
---|
222 | case MODE_CMD_0x0C_IDEA : { //IDEA
|
---|
223 | uint8 key_buf_idea[16];
|
---|
224 | memcpy(key_buf_idea, key_buf, 16);
|
---|
225 | IDEA_KEY_SCHEDULE ekey;
|
---|
226 |
|
---|
227 | idea_set_encrypt_key(key_buf_idea, &ekey);
|
---|
228 | idea_set_decrypt_key(&ekey,&cc->cmd0c_IDEA_dkey);
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | int sid_eq(struct cc_srvid *srvid1, struct cc_srvid *srvid2) {
|
---|
235 | return (srvid1->sid == srvid2->sid && (srvid1->ecmlen == srvid2->ecmlen || !srvid1->ecmlen || !srvid2->ecmlen));
|
---|
236 | }
|
---|
237 |
|
---|
238 | int is_sid_blocked(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
239 | LL_ITER *it = ll_iter_create(card->badsids);
|
---|
240 | struct cc_srvid *srvid;
|
---|
241 | while ((srvid = ll_iter_next(it))) {
|
---|
242 | if (sid_eq(srvid, srvid_blocked)) {
|
---|
243 | break;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | ll_iter_release(it);
|
---|
247 | return (srvid != 0);
|
---|
248 | }
|
---|
249 |
|
---|
250 | int is_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
251 | LL_ITER *it = ll_iter_create(card->goodsids);
|
---|
252 | struct cc_srvid *srvid;
|
---|
253 | while ((srvid = ll_iter_next(it))) {
|
---|
254 | if (sid_eq(srvid, srvid_good)) {
|
---|
255 | break;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | ll_iter_release(it);
|
---|
259 | return (srvid != 0);
|
---|
260 | }
|
---|
261 |
|
---|
262 | void add_sid_block(struct s_client *cl __attribute__((unused)), struct cc_card *card,
|
---|
263 | struct cc_srvid *srvid_blocked) {
|
---|
264 | if (is_sid_blocked(card, srvid_blocked))
|
---|
265 | return;
|
---|
266 |
|
---|
267 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
268 | *srvid = *srvid_blocked;
|
---|
269 | ll_append(card->badsids, srvid);
|
---|
270 | cs_debug_mask(D_READER, "%s added sid block %04X(%d) for card %08x",
|
---|
271 | getprefix(), srvid_blocked->sid, srvid_blocked->ecmlen,
|
---|
272 | card->id);
|
---|
273 | }
|
---|
274 |
|
---|
275 | void remove_sid_block(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
276 | LL_ITER *it = ll_iter_create(card->badsids);
|
---|
277 | struct cc_srvid *srvid;
|
---|
278 | while ((srvid = ll_iter_next(it)))
|
---|
279 | if (sid_eq(srvid, srvid_blocked))
|
---|
280 | ll_iter_remove_data(it);
|
---|
281 | ll_iter_release(it);
|
---|
282 | }
|
---|
283 |
|
---|
284 | void remove_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
285 | LL_ITER *it = ll_iter_create(card->goodsids);
|
---|
286 | struct cc_srvid *srvid;
|
---|
287 | while ((srvid = ll_iter_next(it)))
|
---|
288 | if (sid_eq(srvid, srvid_good))
|
---|
289 | ll_iter_remove_data(it);
|
---|
290 | ll_iter_release(it);
|
---|
291 | }
|
---|
292 |
|
---|
293 | void add_good_sid(struct s_client *cl __attribute__((unused)), struct cc_card *card,
|
---|
294 | struct cc_srvid *srvid_good) {
|
---|
295 | if (is_good_sid(card, srvid_good))
|
---|
296 | return;
|
---|
297 |
|
---|
298 | remove_sid_block(card, srvid_good);
|
---|
299 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
300 | memcpy(srvid, srvid_good, sizeof(struct cc_srvid));
|
---|
301 | ll_append(card->goodsids, srvid);
|
---|
302 | cs_debug_mask(D_READER, "%s added good sid %04X(%d) for card %08x",
|
---|
303 | getprefix(), srvid_good->sid, srvid_good->ecmlen, card->id);
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * reader
|
---|
308 | * clears and frees values for reinit
|
---|
309 | */
|
---|
310 | void cc_cli_close(struct s_client *cl, int call_conclose) {
|
---|
311 | struct s_reader *rdr = cl->reader;
|
---|
312 | struct cc_data *cc = cl->cc;
|
---|
313 | if (!rdr || !cc)
|
---|
314 | return;
|
---|
315 |
|
---|
316 | rdr->tcp_connected = 0;
|
---|
317 | rdr->card_status = NO_CARD;
|
---|
318 | rdr->available = 0;
|
---|
319 | rdr->ncd_msgid = 0;
|
---|
320 | rdr->last_s = rdr->last_g = 0;
|
---|
321 |
|
---|
322 | if (cc->mode == CCCAM_MODE_NORMAL && call_conclose)
|
---|
323 | network_tcp_connection_close(cl, cl->udp_fd);
|
---|
324 | else {
|
---|
325 | if (cl->udp_fd)
|
---|
326 | close(cl->udp_fd);
|
---|
327 | cl->udp_fd = 0;
|
---|
328 | cl->pfd = 0;
|
---|
329 | }
|
---|
330 |
|
---|
331 | cc->just_logged_in = 0;
|
---|
332 | }
|
---|
333 |
|
---|
334 | struct cc_extended_ecm_idx *add_extended_ecm_idx(struct s_client *cl,
|
---|
335 | uint8 send_idx, ushort ecm_idx, struct cc_card *card,
|
---|
336 | struct cc_srvid srvid) {
|
---|
337 | struct cc_data *cc = cl->cc;
|
---|
338 | struct cc_extended_ecm_idx *eei =
|
---|
339 | cs_malloc(&eei, sizeof(struct cc_extended_ecm_idx), QUITERROR);
|
---|
340 | eei->send_idx = send_idx;
|
---|
341 | eei->ecm_idx = ecm_idx;
|
---|
342 | eei->card = card;
|
---|
343 | eei->srvid = srvid;
|
---|
344 | ll_append(cc->extended_ecm_idx, eei);
|
---|
345 | //cs_debug_mask(D_TRACE, "%s add extended ecm-idx: %d:%d", getprefix(), send_idx, ecm_idx);
|
---|
346 | return eei;
|
---|
347 | }
|
---|
348 |
|
---|
349 | struct cc_extended_ecm_idx *get_extended_ecm_idx(struct s_client *cl,
|
---|
350 | uint8 send_idx, int remove) {
|
---|
351 | struct cc_data *cc = cl->cc;
|
---|
352 | struct cc_extended_ecm_idx *eei;
|
---|
353 | LL_ITER *it = ll_iter_create(cc->extended_ecm_idx);
|
---|
354 | while ((eei = ll_iter_next(it))) {
|
---|
355 | if (eei->send_idx == send_idx) {
|
---|
356 | if (remove)
|
---|
357 | ll_iter_remove(it);
|
---|
358 | //cs_debug_mask(D_TRACE, "%s get by send-idx: %d FOUND: %d",
|
---|
359 | // getprefix(), send_idx, eei->ecm_idx);
|
---|
360 | ll_iter_release(it);
|
---|
361 | return eei;
|
---|
362 | }
|
---|
363 | }
|
---|
364 | ll_iter_release(it);
|
---|
365 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s get by send-idx: %d NOT FOUND", getprefix(),
|
---|
366 | send_idx);
|
---|
367 | return NULL;
|
---|
368 | }
|
---|
369 |
|
---|
370 | struct cc_extended_ecm_idx *get_extended_ecm_idx_by_idx(struct s_client *cl,
|
---|
371 | ushort ecm_idx, int remove) {
|
---|
372 | struct cc_data *cc = cl->cc;
|
---|
373 | struct cc_extended_ecm_idx *eei;
|
---|
374 | LL_ITER *it = ll_iter_create(cc->extended_ecm_idx);
|
---|
375 | while ((eei = ll_iter_next(it))) {
|
---|
376 | if (eei->ecm_idx == ecm_idx) {
|
---|
377 | if (remove)
|
---|
378 | ll_iter_remove(it);
|
---|
379 | //cs_debug_mask(D_TRACE, "%s get by ecm-idx: %d FOUND: %d",
|
---|
380 | // getprefix(), ecm_idx, eei->send_idx);
|
---|
381 | ll_iter_release(it);
|
---|
382 | return eei;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | ll_iter_release(it);
|
---|
386 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s get by ecm-idx: %d NOT FOUND", getprefix(),
|
---|
387 | ecm_idx);
|
---|
388 | return NULL;
|
---|
389 | }
|
---|
390 |
|
---|
391 | void cc_reset_pending(struct s_client *cl, int ecm_idx) {
|
---|
392 | int i = 0;
|
---|
393 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
394 | if (cl->ecmtask[i].idx == ecm_idx && cl->ecmtask[i].rc == 101)
|
---|
395 | cl->ecmtask[i].rc = 100; //Mark unused
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | void free_extended_ecm_idx_by_card(struct s_client *cl, struct cc_card *card) {
|
---|
400 | struct cc_data *cc = cl->cc;
|
---|
401 | struct cc_extended_ecm_idx *eei;
|
---|
402 | LL_ITER *it = ll_iter_create(cc->extended_ecm_idx);
|
---|
403 | while ((eei = ll_iter_next(it))) {
|
---|
404 | if (eei->card == card) {
|
---|
405 | cc_reset_pending(cl, eei->ecm_idx);
|
---|
406 | ll_iter_remove_data(it);
|
---|
407 | }
|
---|
408 | }
|
---|
409 | ll_iter_release(it);
|
---|
410 | }
|
---|
411 |
|
---|
412 | void free_extended_ecm_idx(struct cc_data *cc) {
|
---|
413 | struct cc_extended_ecm_idx *eei;
|
---|
414 | LL_ITER *it = ll_iter_create(cc->extended_ecm_idx);
|
---|
415 | while ((eei = ll_iter_next(it)))
|
---|
416 | ll_iter_remove_data(it);
|
---|
417 | ll_iter_release(it);
|
---|
418 | }
|
---|
419 |
|
---|
420 | int cc_recv_to(struct s_client *cl, uint8 *buf, int len) {
|
---|
421 | fd_set fds;
|
---|
422 | struct timeval timeout;
|
---|
423 | int rc;
|
---|
424 |
|
---|
425 | timeout.tv_sec = 2;
|
---|
426 | timeout.tv_usec = 0;
|
---|
427 |
|
---|
428 | while (1) {
|
---|
429 | FD_ZERO(&fds);
|
---|
430 | FD_SET(cl->udp_fd, &fds);
|
---|
431 |
|
---|
432 | rc=select(cl->udp_fd+1, &fds, 0, 0, &timeout);
|
---|
433 | if (rc<0) {
|
---|
434 | if (errno==EINTR) continue;
|
---|
435 | else return(-1); //error!!
|
---|
436 | }
|
---|
437 |
|
---|
438 | if(FD_ISSET(cl->udp_fd,&fds))
|
---|
439 | break;
|
---|
440 | else
|
---|
441 | return (-2); //timeout!!
|
---|
442 | }
|
---|
443 | return recv(cl->udp_fd, buf, len, MSG_WAITALL);
|
---|
444 | }
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * reader
|
---|
448 | * closes the connection and reopens it.
|
---|
449 | */
|
---|
450 | //static void cc_cycle_connection() {
|
---|
451 | // cc_cli_close();
|
---|
452 | // cc_cli_init();
|
---|
453 | //}
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * reader+server:
|
---|
457 | * receive a message
|
---|
458 | */
|
---|
459 | int cc_msg_recv(struct s_client *cl, uint8 *buf, int maxlen) {
|
---|
460 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
461 |
|
---|
462 | int len;
|
---|
463 | struct cc_data *cc = cl->cc;
|
---|
464 |
|
---|
465 | int handle = cl->udp_fd;
|
---|
466 |
|
---|
467 | if (handle <= 0 || maxlen < 4)
|
---|
468 | return -1;
|
---|
469 |
|
---|
470 | len = recv(handle, buf, 4, MSG_WAITALL);
|
---|
471 | if (cl->typ != 'c')
|
---|
472 | rdr->last_g = time(NULL);
|
---|
473 |
|
---|
474 | if (len != 4) { // invalid header length read
|
---|
475 | if (len <= 0)
|
---|
476 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s disconnected by remote server", getprefix());
|
---|
477 | else
|
---|
478 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s invalid header length (expected 4, read %d)", getprefix(), len);
|
---|
479 | return -1;
|
---|
480 | }
|
---|
481 |
|
---|
482 | cc_crypt(&cc->block[DECRYPT], buf, 4, DECRYPT);
|
---|
483 | //cs_ddump_mask(D_CLIENT, buf, 4, "cccam: decrypted header:");
|
---|
484 |
|
---|
485 | cc->g_flag = buf[0];
|
---|
486 |
|
---|
487 | int size = (buf[2] << 8) | buf[3];
|
---|
488 | if (size) { // check if any data is expected in msg
|
---|
489 | if (size > maxlen) {
|
---|
490 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s message too big (size=%d max=%d)", getprefix(), size, maxlen);
|
---|
491 | return 0;
|
---|
492 | }
|
---|
493 |
|
---|
494 | len = recv(handle, buf + 4, size, MSG_WAITALL); // read rest of msg
|
---|
495 | if (cl->typ != 'c')
|
---|
496 | rdr->last_g = time(NULL);
|
---|
497 |
|
---|
498 | if (len != size) {
|
---|
499 | if (len <= 0)
|
---|
500 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s disconnected by remote", getprefix());
|
---|
501 | else
|
---|
502 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s invalid message length read (expected %d, read %d)",
|
---|
503 | getprefix(), size, len);
|
---|
504 | return -1;
|
---|
505 | }
|
---|
506 |
|
---|
507 | cc_crypt(&cc->block[DECRYPT], buf + 4, len, DECRYPT);
|
---|
508 | len += 4;
|
---|
509 | }
|
---|
510 |
|
---|
511 | //cs_ddump_mask(cl->typ=='c'?D_CLIENT:D_READER, buf, len, "cccam: full decrypted msg, len=%d:", len);
|
---|
512 |
|
---|
513 | return len;
|
---|
514 | }
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * reader+server
|
---|
518 | * send a message
|
---|
519 | */
|
---|
520 | int cc_cmd_send(struct s_client *cl, uint8 *buf, int len, cc_msg_type_t cmd) {
|
---|
521 | if (!cl->udp_fd) //disconnected
|
---|
522 | return -1;
|
---|
523 |
|
---|
524 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
525 |
|
---|
526 | int n;
|
---|
527 | uint8 netbuf[len + 4];
|
---|
528 | struct cc_data *cc = cl->cc;
|
---|
529 |
|
---|
530 | memset(netbuf, 0, len + 4);
|
---|
531 |
|
---|
532 | if (cmd == MSG_NO_HEADER) {
|
---|
533 | memcpy(netbuf, buf, len);
|
---|
534 | } else {
|
---|
535 | // build command message
|
---|
536 | netbuf[0] = cc->g_flag; // flags??
|
---|
537 | netbuf[1] = cmd & 0xff;
|
---|
538 | netbuf[2] = len >> 8;
|
---|
539 | netbuf[3] = len & 0xff;
|
---|
540 | if (buf)
|
---|
541 | memcpy(netbuf + 4, buf, len);
|
---|
542 | len += 4;
|
---|
543 | }
|
---|
544 |
|
---|
545 | //cs_ddump_mask(D_CLIENT, netbuf, len, "cccam: send:");
|
---|
546 | cc_crypt(&cc->block[ENCRYPT], netbuf, len, ENCRYPT);
|
---|
547 |
|
---|
548 | n = send(cl->udp_fd, netbuf, len, 0);
|
---|
549 | if (rdr)
|
---|
550 | rdr->last_s = time(NULL);
|
---|
551 |
|
---|
552 | if (n != len) {
|
---|
553 | if (rdr)
|
---|
554 | cc_cli_close(cl, TRUE);
|
---|
555 | }
|
---|
556 |
|
---|
557 | return n;
|
---|
558 | }
|
---|
559 |
|
---|
560 | #define CC_DEFAULT_VERSION 1
|
---|
561 | char *version[] = { "2.0.11", "2.1.1", "2.1.2", "2.1.3", "2.1.4", "2.2.0", "2.2.1", "" };
|
---|
562 | char *build[] = { "2892", "2971", "3094", "3165", "3191", "3290", "3316", "" };
|
---|
563 |
|
---|
564 | /**
|
---|
565 | * reader+server
|
---|
566 | * checks the cccam-version in the configuration
|
---|
567 | */
|
---|
568 | void cc_check_version(char *cc_version, char *cc_build) {
|
---|
569 | int i;
|
---|
570 | for (i = 0; strlen(version[i]); i++) {
|
---|
571 | if (!memcmp(cc_version, version[i], strlen(version[i]))) {
|
---|
572 | memcpy(cc_build, build[i], strlen(build[i]) + 1);
|
---|
573 | cs_debug_mask(D_CLIENT, "cccam: auto build set for version: %s build: %s",
|
---|
574 | cc_version, cc_build);
|
---|
575 | return;
|
---|
576 | }
|
---|
577 | }
|
---|
578 | memcpy(cc_version, version[CC_DEFAULT_VERSION], strlen(
|
---|
579 | version[CC_DEFAULT_VERSION]));
|
---|
580 | memcpy(cc_build, build[CC_DEFAULT_VERSION], strlen(
|
---|
581 | build[CC_DEFAULT_VERSION]));
|
---|
582 |
|
---|
583 | cs_debug_mask(D_CLIENT, "cccam: auto version set: %s build: %s", cc_version, cc_build);
|
---|
584 | }
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * reader
|
---|
588 | * sends own version information to the CCCam server
|
---|
589 | */
|
---|
590 | int cc_send_cli_data(struct s_client *cl) {
|
---|
591 | struct s_reader *rdr = cl->reader;
|
---|
592 |
|
---|
593 | int i;
|
---|
594 | struct cc_data *cc = cl->cc;
|
---|
595 | const int size = 20 + 8 + 6 + 26 + 4 + 28 + 1;
|
---|
596 |
|
---|
597 | cs_debug_mask(D_READER, "cccam: send client data");
|
---|
598 |
|
---|
599 | memcpy(cc->node_id, cc_node_id, sizeof(cc_node_id));
|
---|
600 |
|
---|
601 | uint8 buf[size];
|
---|
602 | memset(buf, 0, size);
|
---|
603 |
|
---|
604 | memcpy(buf, rdr->r_usr, sizeof(rdr->r_usr));
|
---|
605 | memcpy(buf + 20, cc->node_id, 8);
|
---|
606 | buf[28] = rdr->cc_want_emu; // <-- Client want to have EMUs, 0 - NO; 1 - YES
|
---|
607 | memcpy(buf + 29, rdr->cc_version, sizeof(rdr->cc_version)); // cccam version (ascii)
|
---|
608 | memcpy(buf + 61, rdr->cc_build, sizeof(rdr->cc_build)); // build number (ascii)
|
---|
609 |
|
---|
610 | cs_debug_mask(D_READER, "%s sending own version: %s, build: %s", getprefix(),
|
---|
611 | rdr->cc_version, rdr->cc_build);
|
---|
612 |
|
---|
613 | i = cc_cmd_send(cl, buf, size, MSG_CLI_DATA);
|
---|
614 |
|
---|
615 | return i;
|
---|
616 | }
|
---|
617 |
|
---|
618 | /**
|
---|
619 | * server
|
---|
620 | * sends version information to the client
|
---|
621 | */
|
---|
622 | int cc_send_srv_data(struct s_client *cl) {
|
---|
623 | struct cc_data *cc = cl->cc;
|
---|
624 |
|
---|
625 | cs_debug_mask(D_CLIENT, "cccam: send server data");
|
---|
626 |
|
---|
627 | memcpy(cc->node_id, cc_node_id, sizeof(cc_node_id));
|
---|
628 |
|
---|
629 | uint8 buf[0x48];
|
---|
630 | memset(buf, 0, 0x48);
|
---|
631 |
|
---|
632 | if (cfg.cc_stealth)
|
---|
633 | {
|
---|
634 | int i;
|
---|
635 | for (i=0;i<8;i++)
|
---|
636 | buf[i] = fast_rnd();
|
---|
637 | }
|
---|
638 | else
|
---|
639 | memcpy(buf, cc->node_id, 8);
|
---|
640 | char cc_build[7];
|
---|
641 | cc_check_version((char *) cfg.cc_version, cc_build);
|
---|
642 | memcpy(buf + 8, cfg.cc_version, sizeof(cfg.cc_version)); // cccam version (ascii)
|
---|
643 | memcpy(buf + 40, cc_build, sizeof(cc_build)); // build number (ascii)
|
---|
644 |
|
---|
645 | cs_debug_mask(D_CLIENT, "%s version: %s, build: %s nodeid: %s", getprefix(),
|
---|
646 | cfg.cc_version, cc_build, cs_hexdump(0, cc->peer_node_id, 8));
|
---|
647 |
|
---|
648 | return cc_cmd_send(cl, buf, 0x48, MSG_SRV_DATA);
|
---|
649 | }
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * reader
|
---|
653 | * retrieves the next waiting ecm request
|
---|
654 | */
|
---|
655 | int cc_get_nxt_ecm(struct s_client *cl) {
|
---|
656 | int n, i;
|
---|
657 | time_t t;
|
---|
658 |
|
---|
659 | t = time(NULL);
|
---|
660 | n = -1;
|
---|
661 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
662 | if ((t - (ulong) cl->ecmtask[i].tps.time > ((cfg.ctimeout + 500)
|
---|
663 | / 1000) + 1) && (cl->ecmtask[i].rc >= 10)) // drop timeouts
|
---|
664 | {
|
---|
665 | cl->ecmtask[i].rc = 0;
|
---|
666 | }
|
---|
667 |
|
---|
668 | if (cl->ecmtask[i].rc >= 10 && cl->ecmtask[i].rc != 101) { // stil active and waiting
|
---|
669 | // search for the ecm with the lowest time, this should be the next to go
|
---|
670 | if (n < 0 || cl->ecmtask[n].tps.time - cl->ecmtask[i].tps.time < 0) {
|
---|
671 |
|
---|
672 | //check for already pending:
|
---|
673 | if (((struct cc_data*)cl->cc)->extended_mode) {
|
---|
674 | int j,found;
|
---|
675 | for (found=j=0;j<CS_MAXPENDING;j++) {
|
---|
676 | if (i!=j && cl->ecmtask[j].rc == 101 &&
|
---|
677 | cl->ecmtask[i].caid==cl->ecmtask[j].caid &&
|
---|
678 | cl->ecmtask[i].ecmd5==cl->ecmtask[j].ecmd5) {
|
---|
679 | found=1;
|
---|
680 | break;
|
---|
681 | }
|
---|
682 | }
|
---|
683 | if (!found)
|
---|
684 | n = i;
|
---|
685 | }
|
---|
686 | else
|
---|
687 | n = i;
|
---|
688 | }
|
---|
689 | }
|
---|
690 | }
|
---|
691 | return n;
|
---|
692 | }
|
---|
693 |
|
---|
694 | /**
|
---|
695 | * sends the secret cmd05 answer to the server
|
---|
696 | */
|
---|
697 | int send_cmd05_answer(struct s_client *cl) {
|
---|
698 | struct s_reader *rdr = cl->reader;
|
---|
699 | struct cc_data *cc = cl->cc;
|
---|
700 | if (!cc->cmd05_active || !rdr->available) //exit if not in cmd05 or waiting for ECM answer
|
---|
701 | return 0;
|
---|
702 |
|
---|
703 | cc->cmd05_active--;
|
---|
704 | if (cc->cmd05_active)
|
---|
705 | return 0;
|
---|
706 |
|
---|
707 | uint8 *data = cc->cmd05_data;
|
---|
708 | cc_cmd05_mode cmd05_mode = MODE_UNKNOWN;
|
---|
709 |
|
---|
710 | // by Project:Keynation
|
---|
711 | switch (cc->cmd05_data_len) {
|
---|
712 | case 0: { //payload 0, return with payload 0!
|
---|
713 | cc_cmd_send(cl, NULL, 0, MSG_CMD_05);
|
---|
714 | cmd05_mode = MODE_LEN0;
|
---|
715 | break;
|
---|
716 | }
|
---|
717 | case 256: {
|
---|
718 | cmd05_mode = cc->cmd05_mode;
|
---|
719 | switch (cmd05_mode) {
|
---|
720 | case MODE_PLAIN: { //Send plain unencrypted back
|
---|
721 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
722 | break;
|
---|
723 | }
|
---|
724 | case MODE_AES: { //encrypt with received aes128 key:
|
---|
725 | AES_KEY key;
|
---|
726 | uint8 aeskey[16];
|
---|
727 | uint8 out[256];
|
---|
728 |
|
---|
729 | memcpy(aeskey, cc->cmd05_aeskey, 16);
|
---|
730 | memset(&key, 0, sizeof(key));
|
---|
731 |
|
---|
732 | AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
|
---|
733 | int i;
|
---|
734 | for (i = 0; i < 256; i += 16)
|
---|
735 | AES_encrypt((unsigned char *) data + i, (unsigned char *) &out
|
---|
736 | + i, &key);
|
---|
737 |
|
---|
738 | cc_cmd_send(cl, out, 256, MSG_CMD_05);
|
---|
739 | break;
|
---|
740 | }
|
---|
741 | case MODE_CC_CRYPT: { //encrypt with cc_crypt:
|
---|
742 | cc_crypt(&cc->cmd05_cryptkey, data, 256, ENCRYPT);
|
---|
743 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
744 | break;
|
---|
745 | }
|
---|
746 | case MODE_RC4_CRYPT: {//special xor crypt:
|
---|
747 | cc_rc4_crypt(&cc->cmd05_cryptkey, data, 256, DECRYPT);
|
---|
748 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
749 | break;
|
---|
750 | }
|
---|
751 | default:
|
---|
752 | cmd05_mode = MODE_UNKNOWN;
|
---|
753 | }
|
---|
754 | break;
|
---|
755 | }
|
---|
756 | default:
|
---|
757 | cmd05_mode = MODE_UNKNOWN;
|
---|
758 | }
|
---|
759 |
|
---|
760 | //unhandled types always needs cycle connection after 50 ECMs!!
|
---|
761 | if (cmd05_mode == MODE_UNKNOWN) {
|
---|
762 | cc_cmd_send(cl, NULL, 0, MSG_CMD_05);
|
---|
763 | if (!cc->max_ecms) { //max_ecms already set?
|
---|
764 | cc->max_ecms = 50;
|
---|
765 | cc->ecm_counter = 0;
|
---|
766 | }
|
---|
767 | }
|
---|
768 | cs_debug_mask(D_READER, "%s sending CMD_05 back! MODE: %s len=%d",
|
---|
769 | getprefix(), cmd05_mode_name[cmd05_mode], cc->cmd05_data_len);
|
---|
770 |
|
---|
771 | cc->cmd05NOK = 1;
|
---|
772 | return 1;
|
---|
773 | }
|
---|
774 |
|
---|
775 | int get_UA_ofs(uint16 caid) {
|
---|
776 | int ofs = 0;
|
---|
777 | switch (caid >> 8) {
|
---|
778 | case 0x05: //VIACCESS:
|
---|
779 | case 0x0D: //CRYPTOWORKS:
|
---|
780 | ofs = 1;
|
---|
781 | break;
|
---|
782 | case 0x4B: //TONGFANG:
|
---|
783 | case 0x09: //VIDEOGUARD:
|
---|
784 | case 0x0B: //CONAX:
|
---|
785 | case 0x18: //NAGRA:
|
---|
786 | case 0x17: //BETACRYPT
|
---|
787 | case 0x06: //IRDETO:
|
---|
788 | ofs = 2;
|
---|
789 | break;
|
---|
790 | }
|
---|
791 | return ofs;
|
---|
792 | }
|
---|
793 |
|
---|
794 | int UA_len(uint8 *ua) {
|
---|
795 | int i, len=0;
|
---|
796 | for (i=0;i<8;i++)
|
---|
797 | if (ua[i]) len++;
|
---|
798 | return len;
|
---|
799 | }
|
---|
800 |
|
---|
801 | void UA_left(uint8 *in, uint8 *out, int len) {
|
---|
802 | int ofs = 0;
|
---|
803 | int maxlen = 8;
|
---|
804 | int orglen = len;
|
---|
805 | while (len) {
|
---|
806 | memset(out, 0, orglen);
|
---|
807 | memcpy(out, in+ofs, len);
|
---|
808 | if (out[0]) break;
|
---|
809 | ofs++;
|
---|
810 | maxlen--;
|
---|
811 | if (len>maxlen)
|
---|
812 | len=maxlen;
|
---|
813 | }
|
---|
814 | }
|
---|
815 |
|
---|
816 | void UA_right(uint8 *in, uint8 *out, int len) {
|
---|
817 | int ofs = 0;
|
---|
818 | while (len) {
|
---|
819 | memcpy(out+ofs, in, len);
|
---|
820 | len--;
|
---|
821 | if (out[len]) break;
|
---|
822 | ofs++;
|
---|
823 | out[0]=0;
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 | /**
|
---|
828 | * cccam uses UA right justified
|
---|
829 | **/
|
---|
830 | void cc_UA_oscam2cccam(uint8 *in, uint8 *out, uint16 caid) {
|
---|
831 | uint8 tmp[8];
|
---|
832 | memset(out, 0, 8);
|
---|
833 | memset(tmp, 0, 8);
|
---|
834 | //switch (caid>>8) {
|
---|
835 | // case 0x17: //IRDETO/Betacrypt:
|
---|
836 | // //oscam: AA BB CC DD 00 00 00 00
|
---|
837 | // //cccam: 00 00 00 00 DD AA BB CC
|
---|
838 | // out[4] = in[3]; //Hexbase
|
---|
839 | // out[5] = in[0];
|
---|
840 | // out[6] = in[1];
|
---|
841 | // out[7] = in[2];
|
---|
842 | // return;
|
---|
843 | //
|
---|
844 | // //Place here your own adjustments!
|
---|
845 | //}
|
---|
846 | hexserial_to_newcamd(in, tmp+2, caid);
|
---|
847 | UA_right(tmp, out, 8);
|
---|
848 | }
|
---|
849 |
|
---|
850 | /**
|
---|
851 | * oscam has a special format, depends on offset or type:
|
---|
852 | **/
|
---|
853 | void cc_UA_cccam2oscam(uint8 *in, uint8 *out, uint16 caid) {
|
---|
854 | uint8 tmp[8];
|
---|
855 | memset(out, 0, 8);
|
---|
856 | memset(tmp, 0, 8);
|
---|
857 | //switch(caid>>8) {
|
---|
858 | // case 0x17: //IRDETO/Betacrypt:
|
---|
859 | // //cccam: 00 00 00 00 DD AA BB CC
|
---|
860 | // //oscam: AA BB CC DD 00 00 00 00
|
---|
861 | // out[0] = in[5];
|
---|
862 | // out[1] = in[6];
|
---|
863 | // out[2] = in[7];
|
---|
864 | // out[3] = in[4]; //Hexbase
|
---|
865 | // return;
|
---|
866 |
|
---|
867 | // //Place here your own adjustments!
|
---|
868 | //}
|
---|
869 | int ofs = get_UA_ofs(caid);
|
---|
870 | int len = 8-ofs;
|
---|
871 | UA_left(in, tmp+ofs, len);
|
---|
872 | newcamd_to_hexserial(tmp, out, caid);
|
---|
873 | }
|
---|
874 |
|
---|
875 | void cc_SA_oscam2cccam(uint8 *in, uint8 *out) {
|
---|
876 | memcpy(out, in, 4);
|
---|
877 | }
|
---|
878 |
|
---|
879 | void cc_SA_cccam2oscam(uint8 *in, uint8 *out) {
|
---|
880 | memcpy(out, in, 4);
|
---|
881 | }
|
---|
882 |
|
---|
883 | int cc_UA_valid(uint8 *ua) {
|
---|
884 | int i;
|
---|
885 | for (i = 0; i < 8; i++)
|
---|
886 | if (ua[i])
|
---|
887 | return 1;
|
---|
888 | return 0;
|
---|
889 | }
|
---|
890 |
|
---|
891 | /**
|
---|
892 | * Updates AU Data: UA (Unique ID / Hexserial) und SA (Shared ID - Provider)
|
---|
893 | */
|
---|
894 | void set_au_data(struct s_client *cl, struct s_reader *rdr, struct cc_card *card, ECM_REQUEST *cur_er) {
|
---|
895 | if (rdr->audisabled || !cc_UA_valid(card->hexserial))
|
---|
896 | return;
|
---|
897 |
|
---|
898 | struct cc_data *cc = cl->cc;
|
---|
899 | cc->last_emm_card = card;
|
---|
900 |
|
---|
901 | cc_UA_cccam2oscam(card->hexserial, rdr->hexserial, rdr->caid);
|
---|
902 |
|
---|
903 | cs_debug_mask(D_EMM,
|
---|
904 | "%s au info: caid %04X UA: %s",
|
---|
905 | getprefix(), card->caid, cs_hexdump(0,
|
---|
906 | rdr->hexserial, 8));
|
---|
907 |
|
---|
908 | rdr->nprov = 0;
|
---|
909 | LL_ITER *it2 = ll_iter_create(card->providers);
|
---|
910 | struct cc_provider *provider;
|
---|
911 | int p = 0;
|
---|
912 | while ((provider = ll_iter_next(it2))) {
|
---|
913 | if (!cur_er || provider->prov == cur_er->prid || !provider->prov || !cur_er->prid) {
|
---|
914 | rdr->prid[p][0] = provider->prov >> 24;
|
---|
915 | rdr->prid[p][1] = provider->prov >> 16;
|
---|
916 | rdr->prid[p][2] = provider->prov >> 8;
|
---|
917 | rdr->prid[p][3] = provider->prov & 0xFF;
|
---|
918 | cc_SA_cccam2oscam(provider->sa, rdr->sa[p]);
|
---|
919 |
|
---|
920 | cs_debug_mask(D_EMM, "%s au info: provider: %06lX:%02X%02X%02X%02X", getprefix(),
|
---|
921 | provider->prov,
|
---|
922 | provider->sa[0], provider->sa[1], provider->sa[2], provider->sa[3]);
|
---|
923 |
|
---|
924 | p++;
|
---|
925 | rdr->nprov = p;
|
---|
926 | if (p >= CS_MAXPROV) break;
|
---|
927 | }
|
---|
928 | }
|
---|
929 | ll_iter_release(it2);
|
---|
930 |
|
---|
931 | if (!rdr->nprov) { //No Providers? Add null-provider:
|
---|
932 | memset(rdr->prid[0], 0, sizeof(rdr->prid[0]));
|
---|
933 | rdr->nprov = 1;
|
---|
934 | }
|
---|
935 |
|
---|
936 | rdr->caid = card->caid;
|
---|
937 | if (cur_er)
|
---|
938 | rdr->auprovid = cur_er->prid;
|
---|
939 | }
|
---|
940 |
|
---|
941 | int same_first_node(struct cc_card *card1, struct cc_card *card2) {
|
---|
942 | uint8 * node1 = ll_has_elements(card1->remote_nodes);
|
---|
943 | uint8 * node2 = ll_has_elements(card2->remote_nodes);
|
---|
944 |
|
---|
945 | if (!node1 && !node2) return 1; //both NULL, same!
|
---|
946 |
|
---|
947 | if (!node1 || !node2) return 0; //one NULL, not same!
|
---|
948 |
|
---|
949 | return !memcmp(node1, node2, 8); //same?
|
---|
950 | }
|
---|
951 |
|
---|
952 | int same_card(struct cc_card *card1, struct cc_card *card2) {
|
---|
953 | return (card1->caid == card2->caid &&
|
---|
954 | card1->remote_id == card2->remote_id &&
|
---|
955 | same_first_node(card1, card2));
|
---|
956 | }
|
---|
957 |
|
---|
958 | /**
|
---|
959 | * reader
|
---|
960 | * sends a ecm request to the connected CCCam Server
|
---|
961 | */
|
---|
962 | int cc_send_ecm(struct s_client *cl, ECM_REQUEST *er, uchar *buf) {
|
---|
963 | struct s_reader *rdr = cl->reader;
|
---|
964 |
|
---|
965 | //cs_debug_mask(D_TRACE, "%s cc_send_ecm", getprefix());
|
---|
966 | if (!rdr->tcp_connected)
|
---|
967 | cc_cli_connect(cl);
|
---|
968 |
|
---|
969 | int n, h = -1;
|
---|
970 | struct cc_data *cc = cl->cc;
|
---|
971 | struct cc_card *card = NULL;
|
---|
972 | LL_ITER *it;
|
---|
973 | ECM_REQUEST *cur_er;
|
---|
974 | struct timeb cur_time;
|
---|
975 | cs_ftime(&cur_time);
|
---|
976 |
|
---|
977 | if (!cc || (cl->pfd < 1) || !rdr->tcp_connected) {
|
---|
978 | if (er) {
|
---|
979 | er->rc = E_RDR_NOTFOUND;
|
---|
980 | er->rcEx = 0x27;
|
---|
981 | cs_debug_mask(D_READER, "%s server not init! ccinit=%d pfd=%d",
|
---|
982 | rdr->label, cc ? 1 : 0, cl->pfd);
|
---|
983 | write_ecm_answer(rdr, er);
|
---|
984 | }
|
---|
985 | //cc_cli_close(cl);
|
---|
986 | return 0;
|
---|
987 | }
|
---|
988 |
|
---|
989 | if (rdr->tcp_connected != 2) {
|
---|
990 | cs_debug_mask(D_READER, "%s Waiting for CARDS", getprefix());
|
---|
991 | return 0;
|
---|
992 | }
|
---|
993 |
|
---|
994 | //No Card? Waiting for shares
|
---|
995 | if (!ll_has_elements(cc->cards)) {
|
---|
996 | rdr->fd_error++;
|
---|
997 | cs_debug_mask(D_READER, "%s NO CARDS!", getprefix());
|
---|
998 | return 0;
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | cc->just_logged_in = 0;
|
---|
1002 |
|
---|
1003 | if (!cc->extended_mode) {
|
---|
1004 | //Without extended mode, only one ecm at a time could be send
|
---|
1005 | //this is a limitation of "O" CCCam
|
---|
1006 | if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) { //Unlock by NOK or ECM ACK
|
---|
1007 | cs_debug_mask(D_READER,
|
---|
1008 | "%s ecm trylock: ecm busy, retrying later after msg-receive",
|
---|
1009 | getprefix());
|
---|
1010 |
|
---|
1011 | struct timeb timeout;
|
---|
1012 | timeout = cc->ecm_time;
|
---|
1013 | unsigned int tt = cfg.ctimeout * 4;
|
---|
1014 | timeout.time += tt / 1000;
|
---|
1015 | timeout.millitm += tt % 1000;
|
---|
1016 |
|
---|
1017 | if (comp_timeb(&cur_time, &timeout) < 0) { //TODO: Configuration?
|
---|
1018 | return 0; //pending send...
|
---|
1019 | } else {
|
---|
1020 | cs_debug_mask(D_READER,
|
---|
1021 | "%s unlocked-cycleconnection! timeout %ds",
|
---|
1022 | getprefix(), tt / 1000);
|
---|
1023 | //cc_cycle_connection();
|
---|
1024 | cc_cli_close(cl, TRUE);
|
---|
1025 | return 0;
|
---|
1026 | }
|
---|
1027 | }
|
---|
1028 | cs_debug_mask(D_READER, "cccam: ecm trylock: got lock");
|
---|
1029 | }
|
---|
1030 | do {
|
---|
1031 | cc->ecm_time = cur_time;
|
---|
1032 | rdr->available = cc->extended_mode;
|
---|
1033 |
|
---|
1034 | //Search next ECM to send:
|
---|
1035 | if ((n = cc_get_nxt_ecm(cl)) < 0) {
|
---|
1036 | if (!cc->extended_mode) {
|
---|
1037 | rdr->available = 1;
|
---|
1038 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1039 | }
|
---|
1040 | cs_debug_mask(D_READER, "%s no ecm pending!", getprefix());
|
---|
1041 | if (!cc_send_pending_emms(cl))
|
---|
1042 | send_cmd05_answer(cl);
|
---|
1043 | return 0; // no queued ecms
|
---|
1044 | }
|
---|
1045 | cur_er = &cl->ecmtask[n];
|
---|
1046 | cur_er->rc = 101; //mark ECM as already send
|
---|
1047 | cs_debug_mask(D_READER, "cccam: ecm-task %d", cur_er->idx);
|
---|
1048 |
|
---|
1049 | if (buf)
|
---|
1050 | memcpy(buf, cur_er->ecm, cur_er->l);
|
---|
1051 |
|
---|
1052 | struct cc_srvid cur_srvid;
|
---|
1053 | cur_srvid.sid = cur_er->srvid;
|
---|
1054 | cur_srvid.ecmlen = cur_er->l;
|
---|
1055 |
|
---|
1056 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1057 | it = ll_iter_create(cc->cards);
|
---|
1058 | struct cc_card *ncard;
|
---|
1059 | while ((ncard = ll_iter_next(it))) {
|
---|
1060 | if (ncard->caid == cur_er->caid) { // caid matches
|
---|
1061 | if (is_sid_blocked(ncard, &cur_srvid))
|
---|
1062 | continue;
|
---|
1063 |
|
---|
1064 | if (!ncard->providers || !ncard->providers->initial) { //card has no providers:
|
---|
1065 | if (h < 0 || ncard->hop < h || (ncard->hop == h
|
---|
1066 | && cc_UA_valid(ncard->hexserial))) {
|
---|
1067 | // ncard is closer
|
---|
1068 | card = ncard;
|
---|
1069 | h = ncard->hop; // ncard has been matched
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | }
|
---|
1073 | else { //card has providers
|
---|
1074 | LL_ITER *it2 = ll_iter_create(ncard->providers);
|
---|
1075 | struct cc_provider *provider;
|
---|
1076 | while ((provider = ll_iter_next(it2))) {
|
---|
1077 | if (!cur_er->prid || !provider->prov || provider->prov
|
---|
1078 | == cur_er->prid) { // provid matches
|
---|
1079 | if (h < 0 || ncard->hop < h || (ncard->hop == h
|
---|
1080 | && cc_UA_valid(ncard->hexserial))) {
|
---|
1081 | // ncard is closer
|
---|
1082 | card = ncard;
|
---|
1083 | h = ncard->hop; // ncard has been matched
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 | ll_iter_release(it2);
|
---|
1088 | }
|
---|
1089 | }
|
---|
1090 | }
|
---|
1091 | ll_iter_release(it);
|
---|
1092 |
|
---|
1093 | if (card) {
|
---|
1094 | card->time = time((time_t) 0);
|
---|
1095 | uint8 ecmbuf[255+13];
|
---|
1096 | memset(ecmbuf, 0, 255+13);
|
---|
1097 |
|
---|
1098 | // build ecm message
|
---|
1099 | ecmbuf[0] = card->caid >> 8;
|
---|
1100 | ecmbuf[1] = card->caid & 0xff;
|
---|
1101 | ecmbuf[2] = cur_er->prid >> 24;
|
---|
1102 | ecmbuf[3] = cur_er->prid >> 16;
|
---|
1103 | ecmbuf[4] = cur_er->prid >> 8;
|
---|
1104 | ecmbuf[5] = cur_er->prid & 0xff;
|
---|
1105 | ecmbuf[6] = card->id >> 24;
|
---|
1106 | ecmbuf[7] = card->id >> 16;
|
---|
1107 | ecmbuf[8] = card->id >> 8;
|
---|
1108 | ecmbuf[9] = card->id & 0xff;
|
---|
1109 | ecmbuf[10] = cur_er->srvid >> 8;
|
---|
1110 | ecmbuf[11] = cur_er->srvid & 0xff;
|
---|
1111 | ecmbuf[12] = cur_er->l & 0xff;
|
---|
1112 | memcpy(ecmbuf + 13, cur_er->ecm, cur_er->l);
|
---|
1113 |
|
---|
1114 | uint8 send_idx = 1;
|
---|
1115 | if (cc->extended_mode) {
|
---|
1116 | cc->server_ecm_idx++;
|
---|
1117 | if (cc->server_ecm_idx >= 256)
|
---|
1118 | cc->server_ecm_idx = 1;
|
---|
1119 | cc->g_flag = cc->server_ecm_idx; //Flag is used as index!
|
---|
1120 | send_idx = cc->g_flag;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | add_extended_ecm_idx(cl, send_idx, cur_er->idx, card, cur_srvid);
|
---|
1124 |
|
---|
1125 | rdr->cc_currenthops = card->hop;
|
---|
1126 |
|
---|
1127 | cs_debug_mask(D_READER,
|
---|
1128 | "%s sending ecm for sid %04X(%d) to card %08x, hop %d, ecmtask %d",
|
---|
1129 | getprefix(), cur_er->srvid, cur_er->l, card->id, card->hop,
|
---|
1130 | cur_er->idx);
|
---|
1131 | cc_cmd_send(cl, ecmbuf, cur_er->l + 13, MSG_CW_ECM); // send ecm
|
---|
1132 |
|
---|
1133 | //For EMM
|
---|
1134 | set_au_data(cl, rdr, card, cur_er);
|
---|
1135 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1136 |
|
---|
1137 | if (cc->extended_mode)
|
---|
1138 | continue; //process next pending ecm!
|
---|
1139 | return 0;
|
---|
1140 | } else {
|
---|
1141 | //When connecting, it could happen than ecm requests come before all cards are received.
|
---|
1142 | //So if the last Message was a MSG_NEW_CARD, this "card receiving" is not already done
|
---|
1143 | //if this happens, we do not autoblock it and do not set rc status
|
---|
1144 | //So fallback could resolve it
|
---|
1145 | if (cc->last_msg != MSG_NEW_CARD && cc->last_msg != MSG_NEW_CARD_SIDINFO && !cc->just_logged_in) {
|
---|
1146 | cs_debug_mask(D_READER, "%s no suitable card on server", getprefix());
|
---|
1147 |
|
---|
1148 | cur_er->rc = E_RDR_NOTFOUND;
|
---|
1149 | cur_er->rcEx = 0x27;
|
---|
1150 | write_ecm_answer(rdr, cur_er);
|
---|
1151 | //cur_er->rc = 1;
|
---|
1152 | //cur_er->rcEx = 0;
|
---|
1153 | //cs_sleepms(300);
|
---|
1154 | rdr->last_s = rdr->last_g;
|
---|
1155 |
|
---|
1156 | //reopen all blocked sids for this srvid:
|
---|
1157 | it = ll_iter_create(cc->cards);
|
---|
1158 | while ((card = ll_iter_next(it))) {
|
---|
1159 | if (card->caid == cur_er->caid) { // caid matches
|
---|
1160 | LL_ITER *it2 = ll_iter_create(card->badsids);
|
---|
1161 | struct cc_srvid *srvid;
|
---|
1162 | while ((srvid = ll_iter_next(it2)))
|
---|
1163 | if (sid_eq(srvid, &cur_srvid))
|
---|
1164 | ll_iter_remove_data(it2);
|
---|
1165 | ll_iter_release(it2);
|
---|
1166 | }
|
---|
1167 | }
|
---|
1168 | ll_iter_release(it);
|
---|
1169 | }
|
---|
1170 | }
|
---|
1171 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1172 |
|
---|
1173 | //process next pending ecm!
|
---|
1174 | } while (cc->extended_mode);
|
---|
1175 |
|
---|
1176 | if (!cc->extended_mode) {
|
---|
1177 | rdr->available = 1;
|
---|
1178 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | return -1;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | /*
|
---|
1185 | int cc_abort_user_ecms(){
|
---|
1186 | int n, i;
|
---|
1187 | time_t t;//, tls;
|
---|
1188 | struct cc_data *cc = rdr->cc;
|
---|
1189 |
|
---|
1190 | t=time((time_t *)0);
|
---|
1191 | for (i=1,n=1; i<CS_MAXPENDING; i++)
|
---|
1192 | {
|
---|
1193 | if ((t-cl->ecmtask[i].tps.time > ((cfg.ctimeout + 500) / 1000) + 1) &&
|
---|
1194 | (cl->ecmtask[i].rc>=10)) // drop timeouts
|
---|
1195 | {
|
---|
1196 | cl->ecmtask[i].rc=0;
|
---|
1197 | }
|
---|
1198 | int td=abs(1000*(ecmtask[i].tps.time-cc->found->tps.time)+ecmtask[i].tps.millitm-cc->found->tps.millitm);
|
---|
1199 | if (ecmtask[i].rc>=10 && ecmtask[i].cidx==cc->found->cidx && &ecmtask[i]!=cc->found){
|
---|
1200 | cs_log("aborting idx:%d caid:%04x client:%d timedelta:%d",ecmtask[i].idx,ecmtask[i].caid,ecmtask[i].cidx,td);
|
---|
1201 | ecmtask[i].rc=0;
|
---|
1202 | ecmtask[i].rcEx=7;
|
---|
1203 | write_ecm_answer(rdr, fd_c2m, &ecmtask[i]);
|
---|
1204 | }
|
---|
1205 | }
|
---|
1206 | return n;
|
---|
1207 |
|
---|
1208 | }
|
---|
1209 | */
|
---|
1210 |
|
---|
1211 | int cc_send_pending_emms(struct s_client *cl) {
|
---|
1212 | struct s_reader *rdr = cl->reader;
|
---|
1213 | struct cc_data *cc = cl->cc;
|
---|
1214 |
|
---|
1215 | LL_ITER *it = ll_iter_create(cc->pending_emms);
|
---|
1216 | uint8 *emmbuf;
|
---|
1217 | int size = 0;
|
---|
1218 | if ((emmbuf = ll_iter_next(it))) {
|
---|
1219 | if (!cc->extended_mode) {
|
---|
1220 | if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) { //Unlock by NOK or ECM ACK
|
---|
1221 | ll_iter_release(it);
|
---|
1222 | return 0; //send later with cc_send_ecm
|
---|
1223 | }
|
---|
1224 | rdr->available = 0;
|
---|
1225 | }
|
---|
1226 | size = emmbuf[11] + 12;
|
---|
1227 |
|
---|
1228 | cc->just_logged_in = 0;
|
---|
1229 | cs_ftime(&cc->ecm_time);
|
---|
1230 |
|
---|
1231 | cs_debug_mask(D_EMM, "%s emm send for card %08X", getprefix(), b2i(4,
|
---|
1232 | emmbuf + 7));
|
---|
1233 |
|
---|
1234 | cc_cmd_send(cl, emmbuf, size, MSG_EMM_ACK); // send emm
|
---|
1235 |
|
---|
1236 | ll_iter_remove_data(it);
|
---|
1237 | }
|
---|
1238 | ll_iter_release(it);
|
---|
1239 |
|
---|
1240 | return size;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | /**
|
---|
1244 | * READER only:
|
---|
1245 | * find card by hexserial
|
---|
1246 | * */
|
---|
1247 | struct cc_card *get_card_by_hexserial(struct s_client *cl, uint8 *hexserial,
|
---|
1248 | uint16 caid) {
|
---|
1249 | struct cc_data *cc = cl->cc;
|
---|
1250 | LL_ITER *it = ll_iter_create(cc->cards);
|
---|
1251 | struct cc_card *card;
|
---|
1252 | while ((card = ll_iter_next(it)))
|
---|
1253 | if (card->caid == caid && memcmp(card->hexserial, hexserial, 8) == 0) { //found it!
|
---|
1254 | break;
|
---|
1255 | }
|
---|
1256 | ll_iter_release(it);
|
---|
1257 | return card;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | /**
|
---|
1261 | * EMM Procession
|
---|
1262 | * Copied from http://85.17.209.13:6100/file/8ec3c0c5d257/systems/cardclient/cccam2.c
|
---|
1263 | * ProcessEmm
|
---|
1264 | * */
|
---|
1265 | int cc_send_emm(EMM_PACKET *ep) {
|
---|
1266 | struct s_client *cl = cur_client();
|
---|
1267 | struct s_reader *rdr = cl->reader;
|
---|
1268 |
|
---|
1269 | if (!rdr->tcp_connected)
|
---|
1270 | cc_cli_connect(cl);
|
---|
1271 |
|
---|
1272 | struct cc_data *cc = cl->cc;
|
---|
1273 |
|
---|
1274 | if (!cc || (cl->pfd < 1) || !rdr->tcp_connected) {
|
---|
1275 | cs_debug_mask(D_READER, "%s server not init! ccinit=%d pfd=%d", getprefix(), cc ? 1 : 0,
|
---|
1276 | cl->pfd);
|
---|
1277 | return 0;
|
---|
1278 | }
|
---|
1279 | if (rdr->audisabled) {
|
---|
1280 | cs_debug_mask(D_READER, "%s au is disabled", getprefix());
|
---|
1281 | return 0;
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | ushort caid = b2i(2, ep->caid);
|
---|
1285 |
|
---|
1286 | //Last used card is first card of current_cards:
|
---|
1287 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1288 |
|
---|
1289 | struct cc_card *emm_card = cc->last_emm_card;
|
---|
1290 |
|
---|
1291 | if (!emm_card) {
|
---|
1292 | uint8 hs[8];
|
---|
1293 | cc_UA_oscam2cccam(ep->hexserial, hs, caid);
|
---|
1294 | cs_debug_mask(D_EMM,
|
---|
1295 | "%s au info: searching card for caid %04X oscam-UA: %s",
|
---|
1296 | getprefix(), b2i(2, ep->caid), cs_hexdump(0, ep->hexserial, 8));
|
---|
1297 | cs_debug_mask(D_EMM,
|
---|
1298 | "%s au info: searching card for caid %04X cccam-UA: %s",
|
---|
1299 | getprefix(), b2i(2, ep->caid), cs_hexdump(0, hs, 8));
|
---|
1300 |
|
---|
1301 | emm_card = get_card_by_hexserial(cl, hs, caid);
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | if (!emm_card) { //Card for emm not found!
|
---|
1305 | cs_debug_mask(D_EMM, "%s emm for client %8X not possible, no card found!",
|
---|
1306 | getprefix(), ep->client->thread);
|
---|
1307 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1308 | return 0;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | cs_debug_mask(D_EMM,
|
---|
1312 | "%s emm received for client %8X caid %04X for card %08X",
|
---|
1313 | getprefix(), ep->client->thread, caid, emm_card->id);
|
---|
1314 |
|
---|
1315 | int size = ep->l + 12;
|
---|
1316 | uint8 *emmbuf = cs_malloc(&emmbuf, size, QUITERROR);
|
---|
1317 | memset(emmbuf, 0, size);
|
---|
1318 |
|
---|
1319 | // build ecm message
|
---|
1320 | emmbuf[0] = ep->caid[0];
|
---|
1321 | emmbuf[1] = ep->caid[1];
|
---|
1322 | emmbuf[2] = 0;
|
---|
1323 | emmbuf[3] = ep->provid[0];
|
---|
1324 | emmbuf[4] = ep->provid[1];
|
---|
1325 | emmbuf[5] = ep->provid[2];
|
---|
1326 | emmbuf[6] = ep->provid[3];
|
---|
1327 | emmbuf[7] = emm_card->id >> 24;
|
---|
1328 | emmbuf[8] = emm_card->id >> 16;
|
---|
1329 | emmbuf[9] = emm_card->id >> 8;
|
---|
1330 | emmbuf[10] = emm_card->id & 0xff;
|
---|
1331 | emmbuf[11] = ep->l;
|
---|
1332 | memcpy(emmbuf + 12, ep->emm, ep->l);
|
---|
1333 |
|
---|
1334 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1335 |
|
---|
1336 | ll_append(cc->pending_emms, emmbuf);
|
---|
1337 | cc_send_pending_emms(cl);
|
---|
1338 |
|
---|
1339 | #ifdef WEBIF
|
---|
1340 | rdr->emmwritten[ep->type]++;
|
---|
1341 | #endif
|
---|
1342 | return 1;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | void cc_free_card(struct cc_card *card) {
|
---|
1346 | if (!card)
|
---|
1347 | return;
|
---|
1348 |
|
---|
1349 | ll_destroy_data(card->providers);
|
---|
1350 | ll_destroy_data(card->badsids);
|
---|
1351 | ll_destroy_data(card->goodsids);
|
---|
1352 | ll_destroy_data(card->remote_nodes);
|
---|
1353 |
|
---|
1354 | free(card);
|
---|
1355 | }
|
---|
1356 |
|
---|
1357 | /**
|
---|
1358 | * Server:
|
---|
1359 | * Adds a cccam-carddata buffer to the list of reported carddatas
|
---|
1360 | */
|
---|
1361 | void cc_add_reported_carddata(LLIST *reported_carddatas, struct cc_card *card) {
|
---|
1362 | ll_append(reported_carddatas, card);
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | struct cc_card *cc_get_card_by_id(uint32 card_id, LLIST *cards) {
|
---|
1366 | if (!cards)
|
---|
1367 | return NULL;
|
---|
1368 | LL_ITER *it = ll_iter_create(cards);
|
---|
1369 | struct cc_card *card;
|
---|
1370 | while ((card=ll_iter_next(it))) {
|
---|
1371 | if (card->id==card_id) {
|
---|
1372 | break;
|
---|
1373 | }
|
---|
1374 | }
|
---|
1375 | ll_iter_release(it);
|
---|
1376 | return card;
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | int cc_clear_reported_carddata(struct s_client *cl, LLIST *reported_carddatas, LLIST *except,
|
---|
1380 | int send_removed) {
|
---|
1381 | int i=0;
|
---|
1382 | LL_ITER *it = ll_iter_create(reported_carddatas);
|
---|
1383 | struct cc_card *card;
|
---|
1384 | while ((card = ll_iter_next(it))) {
|
---|
1385 | struct cc_card *card2 = NULL;
|
---|
1386 | if (except) {
|
---|
1387 | LL_ITER *it2 = ll_iter_create(except);
|
---|
1388 | while ((card2 = ll_iter_next(it2))) {
|
---|
1389 | if (card == card2)
|
---|
1390 | break;
|
---|
1391 | }
|
---|
1392 | ll_iter_release(it2);
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | if (!card2) {
|
---|
1396 | if (send_removed) {
|
---|
1397 | uint8 buf[4];
|
---|
1398 | buf[0] = card->id >>24;
|
---|
1399 | buf[1] = card->id >>16;
|
---|
1400 | buf[2] = card->id >>8;
|
---|
1401 | buf[3] = card->id & 0xFF;
|
---|
1402 | cc_cmd_send(cl, buf, 4, MSG_CARD_REMOVED);
|
---|
1403 | }
|
---|
1404 | cc_free_card(card);
|
---|
1405 | i++;
|
---|
1406 | }
|
---|
1407 | ll_iter_remove(it);
|
---|
1408 | }
|
---|
1409 | ll_iter_release(it);
|
---|
1410 | return i;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | int cc_free_reported_carddata(struct s_client *cl, LLIST *reported_carddatas, LLIST *except,
|
---|
1414 | int send_removed) {
|
---|
1415 | int i=0;
|
---|
1416 | if (reported_carddatas) {
|
---|
1417 | i = cc_clear_reported_carddata(cl, reported_carddatas, except, send_removed);
|
---|
1418 | ll_destroy(reported_carddatas);
|
---|
1419 | }
|
---|
1420 | return i;
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | void cc_free_cardlist(LLIST *card_list, int destroy_list) {
|
---|
1424 | if (card_list) {
|
---|
1425 | LL_ITER *it = ll_iter_create(card_list);
|
---|
1426 | struct cc_card *card;
|
---|
1427 | while ((card = ll_iter_next(it))) {
|
---|
1428 | cc_free_card(card);
|
---|
1429 | ll_iter_remove(it);
|
---|
1430 | }
|
---|
1431 | ll_iter_release(it);
|
---|
1432 | if (destroy_list)
|
---|
1433 | ll_destroy_data(card_list);
|
---|
1434 | }
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | /**
|
---|
1438 | * Clears and free the cc datas
|
---|
1439 | */
|
---|
1440 | void cc_free(struct s_client *cl) {
|
---|
1441 | struct cc_data *cc = cl->cc;
|
---|
1442 | if (!cc) return;
|
---|
1443 |
|
---|
1444 | pthread_mutex_trylock(&cc->cards_busy);
|
---|
1445 | if (!cl->cc) return;
|
---|
1446 | cl->cc=NULL;
|
---|
1447 | cc_free_cardlist(cc->cards, TRUE);
|
---|
1448 | cc_free_reported_carddata(cl, cc->reported_carddatas, NULL, FALSE);
|
---|
1449 | ll_destroy_data(cc->pending_emms);
|
---|
1450 | if (cc->extended_ecm_idx)
|
---|
1451 | free_extended_ecm_idx(cc);
|
---|
1452 | ll_destroy_data(cc->extended_ecm_idx);
|
---|
1453 |
|
---|
1454 | pthread_mutex_unlock(&cc->lock);
|
---|
1455 | pthread_mutex_destroy(&cc->lock);
|
---|
1456 |
|
---|
1457 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1458 | pthread_mutex_destroy(&cc->ecm_busy);
|
---|
1459 |
|
---|
1460 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1461 | pthread_mutex_destroy(&cc->cards_busy);
|
---|
1462 | free(cc->prefix);
|
---|
1463 | free(cc);
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | int is_null_dcw(uint8 *dcw) {
|
---|
1467 | int i;
|
---|
1468 | for (i = 0; i < 15; i++)
|
---|
1469 | if (dcw[i])
|
---|
1470 | return 0;
|
---|
1471 | return 1;
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | /*int is_dcw_corrupted(uchar *dcw)
|
---|
1475 | {
|
---|
1476 | int i;
|
---|
1477 | int c, cs;
|
---|
1478 |
|
---|
1479 | for (i=0; i<16; i+=4)
|
---|
1480 | {
|
---|
1481 | c = (dcw[i] + dcw[i+1] + dcw[i+2]) & 0xFF;
|
---|
1482 | cs = dcw[i+3];
|
---|
1483 | if (cs!=c) return (1);
|
---|
1484 | }
|
---|
1485 | return 0;
|
---|
1486 | }
|
---|
1487 | */
|
---|
1488 |
|
---|
1489 | int check_extended_mode(struct s_client *cl, char *msg) {
|
---|
1490 | //Extended mode: if PARTNER String is ending with [PARAM], extended mode is activated
|
---|
1491 | //For future compatibilty the syntax should be compatible with
|
---|
1492 | //[PARAM1,PARAM2...PARAMn]
|
---|
1493 | //
|
---|
1494 | // EXT: Extended ECM Mode: Multiple ECMs could be send and received
|
---|
1495 | // ECMs are numbered, Flag (byte[0] is the index
|
---|
1496 | //
|
---|
1497 | // SID: Exchange of good sids/bad sids activated (like cccam 2.2.x)
|
---|
1498 | // card exchange command MSG_NEW_CARD_SIDINFO instead MSG_NEW_CARD is used
|
---|
1499 | //
|
---|
1500 |
|
---|
1501 | struct cc_data *cc = cl->cc;
|
---|
1502 | int has_param = 0;
|
---|
1503 | char *p = strtok(msg, "[");
|
---|
1504 | while (p) {
|
---|
1505 | p = strtok(NULL, ",]");
|
---|
1506 | if (p && strncmp(p, "EXT", 3) == 0) {
|
---|
1507 | cc->extended_mode = 1;
|
---|
1508 | cs_debug_mask(D_CLIENT, "%s extended ECM mode", getprefix());
|
---|
1509 | has_param = 1;
|
---|
1510 | }
|
---|
1511 | else if (p && strncmp(p, "SID", 3)==0) {
|
---|
1512 | cc->cccam220 = 1;
|
---|
1513 | cs_debug_mask(D_CLIENT, "%s extra SID mode", getprefix());
|
---|
1514 | has_param = 1;
|
---|
1515 | }
|
---|
1516 | }
|
---|
1517 | return has_param;
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | void cc_idle() {
|
---|
1521 | struct s_client *cl = cur_client();
|
---|
1522 | struct s_reader *rdr = cl->reader;
|
---|
1523 | struct cc_data *cc = cl->cc;
|
---|
1524 | if (!rdr || !rdr->tcp_connected || !cl || !cc)
|
---|
1525 | return;
|
---|
1526 |
|
---|
1527 | if (rdr->cc_keepalive) {
|
---|
1528 | if (cc->answer_on_keepalive + 55 <= time(NULL)) {
|
---|
1529 | cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE);
|
---|
1530 | cs_debug_mask(D_READER, "cccam: keepalive");
|
---|
1531 | cc->answer_on_keepalive = time(NULL);
|
---|
1532 | }
|
---|
1533 | }
|
---|
1534 | else
|
---|
1535 | {
|
---|
1536 | int rto = abs(rdr->last_s - rdr->last_g);
|
---|
1537 | if (rto >= (rdr->tcp_rto*60))
|
---|
1538 | network_tcp_connection_close(cl, cl->udp_fd);
|
---|
1539 | }
|
---|
1540 | }
|
---|
1541 |
|
---|
1542 | struct cc_card *read_card(uint8 *buf, int ext) {
|
---|
1543 | struct cc_card *card = cs_malloc(&card, sizeof(struct cc_card), QUITERROR);
|
---|
1544 | memset(card, 0, sizeof(struct cc_card));
|
---|
1545 |
|
---|
1546 | int nprov, nassign = 0, nreject = 0, offset = 21;
|
---|
1547 |
|
---|
1548 | card->providers = ll_create();
|
---|
1549 | card->badsids = ll_create();
|
---|
1550 | card->goodsids = ll_create();
|
---|
1551 | card->remote_nodes = ll_create();
|
---|
1552 | card->id = b2i(4, buf);
|
---|
1553 | card->remote_id = b2i(4, buf + 4);
|
---|
1554 | card->caid = b2i(2, buf + 8);
|
---|
1555 | card->hop = buf[10];
|
---|
1556 | card->maxdown = buf[11];
|
---|
1557 | memcpy(card->hexserial, buf + 12, 8); //HEXSERIAL!!
|
---|
1558 |
|
---|
1559 | //cs_debug_mask(D_CLIENT, "cccam: card %08x added, caid %04X, hop %d, key %s, count %d",
|
---|
1560 | // card->id, card->caid, card->hop, cs_hexdump(0, card->hexserial, 8),
|
---|
1561 | // ll_count(cc->cards));
|
---|
1562 |
|
---|
1563 | nprov = buf[20];
|
---|
1564 |
|
---|
1565 | if (ext) {
|
---|
1566 | nassign = buf[21];
|
---|
1567 | nreject = buf[22];
|
---|
1568 |
|
---|
1569 | offset += 2;
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 | int i;
|
---|
1573 | for (i = 0; i < nprov; i++) { // providers
|
---|
1574 | struct cc_provider *prov = cs_malloc(&prov, sizeof(struct cc_provider), QUITERROR);
|
---|
1575 | prov->prov = b2i(3, buf + offset);
|
---|
1576 | if (prov->prov == 0xFFFFFF && (card->caid >> 8) == 0x17)
|
---|
1577 | prov->prov = i;
|
---|
1578 | memcpy(prov->sa, buf + offset + 3, 4);
|
---|
1579 | //cs_debug_mask(D_CLIENT, " prov %d, %06x, sa %08x", i + 1, prov->prov, b2i(4,
|
---|
1580 | // prov->sa));
|
---|
1581 |
|
---|
1582 | ll_append(card->providers, prov);
|
---|
1583 | offset+=7;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | uint8 *ptr = buf + offset;
|
---|
1587 |
|
---|
1588 | if (ext) {
|
---|
1589 | for (i = 0; i < nassign; i++) {
|
---|
1590 | uint16_t sid = b2i(2, ptr);
|
---|
1591 | //cs_debug_mask(D_CLIENT, " assigned sid = %04X, added to good sid list", sid);
|
---|
1592 |
|
---|
1593 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
1594 | srvid->sid = sid;
|
---|
1595 | srvid->ecmlen = 0;
|
---|
1596 | ll_append(card->goodsids, srvid);
|
---|
1597 | ptr+=2;
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | for (i = 0; i < nreject; i++) {
|
---|
1601 | uint16_t sid = b2i(2, ptr);
|
---|
1602 | //cs_debug_mask(D_CLIENT, " rejected sid = %04X, added to sid block list", sid);
|
---|
1603 |
|
---|
1604 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
1605 | srvid->sid = sid;
|
---|
1606 | srvid->ecmlen = 0;
|
---|
1607 | ll_append(card->badsids, srvid);
|
---|
1608 | ptr+=2;
|
---|
1609 | }
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | int remote_count = ptr[0];
|
---|
1613 | ptr++;
|
---|
1614 | for (i = 0; i < remote_count; i++) {
|
---|
1615 | uint8 *remote_node = cs_malloc(&remote_node, 8, QUITERROR);
|
---|
1616 | memcpy(remote_node, ptr, 8);
|
---|
1617 | ll_append(card->remote_nodes, remote_node);
|
---|
1618 | ptr+=8;
|
---|
1619 | }
|
---|
1620 | return card;
|
---|
1621 | }
|
---|
1622 |
|
---|
1623 | #define READ_CARD_TIMEOUT 100
|
---|
1624 |
|
---|
1625 | int write_card(struct cc_data *cc, uint8 *buf, struct cc_card *card, int add_own, int ext, int au_allowed) {
|
---|
1626 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
1627 | buf[0] = card->id >> 24;
|
---|
1628 | buf[1] = card->id >> 16;
|
---|
1629 | buf[2] = card->id >> 8;
|
---|
1630 | buf[3] = card->id & 0xff;
|
---|
1631 | buf[4] = card->remote_id >> 24;
|
---|
1632 | buf[5] = card->remote_id >> 16;
|
---|
1633 | buf[6] = card->remote_id >> 8;
|
---|
1634 | buf[7] = card->remote_id & 0xFF;
|
---|
1635 | buf[8] = card->caid >> 8;
|
---|
1636 | buf[9] = card->caid & 0xff;
|
---|
1637 | buf[10] = card->hop;
|
---|
1638 | buf[11] = card->maxdown;
|
---|
1639 | if (au_allowed)
|
---|
1640 | memcpy(buf + 12, card->hexserial, 8);
|
---|
1641 |
|
---|
1642 | //with cccam 2.2.0 we have assigned and rejected sids:
|
---|
1643 | int ofs = ext?23:21;
|
---|
1644 |
|
---|
1645 | //write providers:
|
---|
1646 | LL_ITER *it = ll_iter_create(card->providers);
|
---|
1647 | struct cc_provider *prov;
|
---|
1648 | while ((prov = ll_iter_next(it))) {
|
---|
1649 | ulong prid = prov->prov;
|
---|
1650 | buf[ofs+0] = prid >> 16;
|
---|
1651 | buf[ofs+1] = prid >> 8;
|
---|
1652 | buf[ofs+2] = prid & 0xFF;
|
---|
1653 | memcpy(buf + ofs + 3, prov->sa, 4);
|
---|
1654 | buf[20]++;
|
---|
1655 | ofs+=7;
|
---|
1656 | }
|
---|
1657 | ll_iter_release(it);
|
---|
1658 |
|
---|
1659 | //write sids only if cccam 2.2.x:
|
---|
1660 | if (ext) {
|
---|
1661 | //assigned sids:
|
---|
1662 | it = ll_iter_create(card->goodsids);
|
---|
1663 | struct cc_srvid *srvid;
|
---|
1664 | while ((srvid = ll_iter_next(it))) {
|
---|
1665 | buf[ofs+0] = srvid->sid >> 8;
|
---|
1666 | buf[ofs+1] = srvid->sid & 0xFF;
|
---|
1667 | ofs+=2;
|
---|
1668 | buf[21]++; //nassign
|
---|
1669 | if (buf[21] > 250)
|
---|
1670 | break;
|
---|
1671 | }
|
---|
1672 | ll_iter_release(it);
|
---|
1673 |
|
---|
1674 | //reject sids:
|
---|
1675 | it = ll_iter_create(card->badsids);
|
---|
1676 | while ((srvid = ll_iter_next(it))) {
|
---|
1677 | buf[ofs+0] = srvid->sid >> 8;
|
---|
1678 | buf[ofs+1] = srvid->sid & 0xFF;
|
---|
1679 | ofs+=2;
|
---|
1680 | buf[22]++; //nreject
|
---|
1681 | if (buf[22] > 250)
|
---|
1682 | break;
|
---|
1683 | }
|
---|
1684 | ll_iter_release(it);
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | //write remote nodes
|
---|
1688 | int nremote_ofs = ofs;
|
---|
1689 | ofs++;
|
---|
1690 | it = ll_iter_create(card->remote_nodes);
|
---|
1691 | uint8 *remote_node;
|
---|
1692 | while ((remote_node = ll_iter_next(it))) {
|
---|
1693 | memcpy(buf+ofs, remote_node, 8);
|
---|
1694 | ofs+=8;
|
---|
1695 | buf[nremote_ofs]++;
|
---|
1696 | }
|
---|
1697 | ll_iter_release(it);
|
---|
1698 | if (add_own) {
|
---|
1699 | memcpy(buf+ofs, cc->node_id, 8);
|
---|
1700 | ofs+=8;
|
---|
1701 | buf[nremote_ofs]++;
|
---|
1702 | }
|
---|
1703 | return ofs;
|
---|
1704 | }
|
---|
1705 |
|
---|
1706 | void cc_card_removed(struct s_client *cl, uint32 shareid) {
|
---|
1707 | struct cc_data *cc = cl->cc;
|
---|
1708 | struct cc_card *card;
|
---|
1709 | LL_ITER *it = ll_iter_create(cc->cards);
|
---|
1710 |
|
---|
1711 | while ((card = ll_iter_next(it))) {
|
---|
1712 | if (card->id == shareid) {// && card->sub_id == b2i (3, buf + 9)) {
|
---|
1713 | //cs_debug_mask(D_CLIENT, "cccam: card %08x removed, caid %04X, count %d",
|
---|
1714 | // card->id, card->caid, ll_count(cc->cards));
|
---|
1715 | ll_iter_remove(it);
|
---|
1716 | if (cc->last_emm_card == card) {
|
---|
1717 | cc->last_emm_card = NULL;
|
---|
1718 | cs_debug_mask(D_READER, "%s current card %08x removed!",
|
---|
1719 | getprefix(), card->id);
|
---|
1720 | }
|
---|
1721 | free_extended_ecm_idx_by_card(cl, card);
|
---|
1722 | cc_free_card(card);
|
---|
1723 | cc->cards_modified++;
|
---|
1724 | //break;
|
---|
1725 | }
|
---|
1726 | }
|
---|
1727 | ll_iter_release(it);
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | void move_card_to_end(struct s_client * cl, struct cc_card *card_to_move) {
|
---|
1731 |
|
---|
1732 | struct cc_data *cc = cl->cc;
|
---|
1733 |
|
---|
1734 | LL_ITER *it = ll_iter_create(cc->cards);
|
---|
1735 | struct cc_card *card;
|
---|
1736 | while ((card = ll_iter_next(it))) {
|
---|
1737 | if (card == card_to_move) {
|
---|
1738 | ll_iter_remove(it);
|
---|
1739 | break;
|
---|
1740 | }
|
---|
1741 | }
|
---|
1742 | ll_iter_release(it);
|
---|
1743 | if (card) {
|
---|
1744 | cs_debug_mask(D_READER, "%s CMD05: Moving card %08X to the end...", getprefix(), card_to_move->id);
|
---|
1745 | free_extended_ecm_idx_by_card(cl, card);
|
---|
1746 | ll_append(cc->cards, card_to_move);
|
---|
1747 | }
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 |
|
---|
1751 | /**
|
---|
1752 | * if idents defined on an cccam reader, the cards caid+provider are checked.
|
---|
1753 | * return 1 a) if no ident defined b) card is in identlist
|
---|
1754 | * 0 if card is not in identlist
|
---|
1755 | *
|
---|
1756 | * a card is in the identlist, if the cards caid is matching and mininum a provider is matching
|
---|
1757 | **/
|
---|
1758 | int chk_ident(FTAB *ftab, struct cc_card *card) {
|
---|
1759 |
|
---|
1760 | int j, k;
|
---|
1761 | int res = 1;
|
---|
1762 |
|
---|
1763 | if (ftab && ftab->filts) {
|
---|
1764 | for (j = 0; j < ftab->nfilts; j++) {
|
---|
1765 | if (ftab->filts[j].caid) {
|
---|
1766 | res = 0;
|
---|
1767 | if (ftab->filts[j].caid==card->caid) { //caid matches!
|
---|
1768 |
|
---|
1769 | int nprids = ftab->filts[j].nprids;
|
---|
1770 | if (!nprids) // No Provider ->Ok
|
---|
1771 | return 1;
|
---|
1772 |
|
---|
1773 |
|
---|
1774 | LL_ITER *it = ll_iter_create(card->providers);
|
---|
1775 | struct cc_provider *prov;
|
---|
1776 |
|
---|
1777 | while ((prov = ll_iter_next(it))) {
|
---|
1778 | for (k = 0; k < nprids; k++) {
|
---|
1779 | ulong prid = ftab->filts[j].prids[k];
|
---|
1780 | if (prid == prov->prov) { //Provider matches
|
---|
1781 | ll_iter_release(it);
|
---|
1782 | return 1;
|
---|
1783 | }
|
---|
1784 | }
|
---|
1785 | }
|
---|
1786 | ll_iter_release(it);
|
---|
1787 | }
|
---|
1788 | }
|
---|
1789 | }
|
---|
1790 | }
|
---|
1791 | return res;
|
---|
1792 | }
|
---|
1793 |
|
---|
1794 |
|
---|
1795 | /*void fix_dcw(uchar *dcw)
|
---|
1796 | {
|
---|
1797 | int i;
|
---|
1798 | for (i=0; i<16; i+=4)
|
---|
1799 | {
|
---|
1800 | dcw[i+3] = (dcw[i] + dcw[i+1] + dcw[i+2]) & 0xFF;
|
---|
1801 | }
|
---|
1802 | }*/
|
---|
1803 |
|
---|
1804 | void addParam(char *param, char *value)
|
---|
1805 | {
|
---|
1806 | if (strlen(param) < 4)
|
---|
1807 | strcat(param, value);
|
---|
1808 | else {
|
---|
1809 | strcat(param, ",");
|
---|
1810 | strcat(param, value);
|
---|
1811 | }
|
---|
1812 | }
|
---|
1813 |
|
---|
1814 | int cc_parse_msg(struct s_client *cl, uint8 *buf, int l) {
|
---|
1815 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
1816 |
|
---|
1817 | int ret = buf[1];
|
---|
1818 | struct cc_data *cc = cl->cc;
|
---|
1819 |
|
---|
1820 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s parse_msg=%d", getprefix(), buf[1]);
|
---|
1821 |
|
---|
1822 | uint8 *data = buf + 4;
|
---|
1823 | memcpy(&cc->receive_buffer, data, l - 4);
|
---|
1824 | cc->last_msg = buf[1];
|
---|
1825 | switch (buf[1]) {
|
---|
1826 | case MSG_CLI_DATA:
|
---|
1827 | cs_debug_mask(D_CLIENT, "cccam: client data ack");
|
---|
1828 | break;
|
---|
1829 | case MSG_SRV_DATA:
|
---|
1830 | l -= 4;
|
---|
1831 | cs_debug_mask(D_READER, "%s MSG_SRV_DATA (payload=%d, hex=%02X)", getprefix(), l, l);
|
---|
1832 | data = cc->receive_buffer;
|
---|
1833 |
|
---|
1834 | if (l == 0x48) { //72 bytes: normal server data
|
---|
1835 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1836 | cc_free_cardlist(cc->cards, FALSE);
|
---|
1837 | cc->last_emm_card = NULL;
|
---|
1838 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1839 |
|
---|
1840 | memcpy(cc->peer_node_id, data, 8);
|
---|
1841 | memcpy(cc->peer_version, data + 8, 8);
|
---|
1842 |
|
---|
1843 | memcpy(cc->cmd0b_aeskey, cc->peer_node_id, 8);
|
---|
1844 | memcpy(cc->cmd0b_aeskey + 8, cc->peer_version, 8);
|
---|
1845 |
|
---|
1846 | strncpy(cc->remote_version, (char*)data+8, sizeof(cc->remote_version)-1);
|
---|
1847 | strncpy(cc->remote_build, (char*)data+40, sizeof(cc->remote_build)-1);
|
---|
1848 |
|
---|
1849 | cs_debug_mask(D_READER, "%s remove server %s running v%s (%s)", getprefix(), cs_hexdump(0,
|
---|
1850 | cc->peer_node_id, 8), cc->remote_version, cc->remote_build);
|
---|
1851 |
|
---|
1852 | if (!cc->is_oscam_cccam) {//Allready discovered oscam-cccam:
|
---|
1853 | uint16 sum = 0x1234;
|
---|
1854 | uint16 recv_sum = (cc->peer_node_id[6] << 8)
|
---|
1855 | | cc->peer_node_id[7];
|
---|
1856 | int i;
|
---|
1857 | for (i = 0; i < 6; i++) {
|
---|
1858 | sum += cc->peer_node_id[i];
|
---|
1859 | }
|
---|
1860 | //Create special data to detect oscam-cccam:
|
---|
1861 | cc->is_oscam_cccam = sum == recv_sum;
|
---|
1862 | }
|
---|
1863 | //Trick: when discovered partner is an Oscam Client, then we send him our version string:
|
---|
1864 | if (cc->is_oscam_cccam) {
|
---|
1865 | sprintf((char*) buf,
|
---|
1866 | "PARTNER: OSCam v%s, build #%s (%s) [EXT,SID]", CS_VERSION,
|
---|
1867 | CS_SVN_VERSION, CS_OSTYPE);
|
---|
1868 | cc_cmd_send(cl, buf, strlen((char*) buf) + 1, MSG_CW_NOK1);
|
---|
1869 | }
|
---|
1870 |
|
---|
1871 | cc->cmd05_mode = MODE_PLAIN;
|
---|
1872 | //
|
---|
1873 | //Keyoffset is payload-size:
|
---|
1874 | //
|
---|
1875 | } else if (l >= 0x00 && l <= 0x0F) {
|
---|
1876 | cc->cmd05_offset = l;
|
---|
1877 | //
|
---|
1878 | //16..43 bytes: RC4 encryption:
|
---|
1879 | //
|
---|
1880 | } else if ((l >= 0x10 && l <= 0x1f) || (l >= 0x24 && l <= 0x2b)) {
|
---|
1881 | cc_init_crypt(&cc->cmd05_cryptkey, data, l);
|
---|
1882 | cc->cmd05_mode = MODE_RC4_CRYPT;
|
---|
1883 | //
|
---|
1884 | //32 bytes: set AES128 key for CMD_05, Key=16 bytes offset keyoffset
|
---|
1885 | //
|
---|
1886 | } else if (l == 0x20) {
|
---|
1887 | memcpy(cc->cmd05_aeskey, data + cc->cmd05_offset, 16);
|
---|
1888 | cc->cmd05_mode = MODE_AES;
|
---|
1889 | //
|
---|
1890 | //33 bytes: xor-algo mit payload-bytes, offset keyoffset
|
---|
1891 | //
|
---|
1892 | } else if (l == 0x21) {
|
---|
1893 | cc_init_crypt(&cc->cmd05_cryptkey, data + cc->cmd05_offset, l);
|
---|
1894 | cc->cmd05_mode = MODE_CC_CRYPT;
|
---|
1895 | //
|
---|
1896 | //34 bytes: cmd_05 plain back
|
---|
1897 | //
|
---|
1898 | } else if (l == 0x22) {
|
---|
1899 | cc->cmd05_mode = MODE_PLAIN;
|
---|
1900 | //
|
---|
1901 | //35 bytes: Unknown!! 2 256 byte keys exchange
|
---|
1902 | //
|
---|
1903 | } else if (l == 0x23) {
|
---|
1904 | cc->cmd05_mode = MODE_UNKNOWN;
|
---|
1905 | //cycle_connection(); //Absolute unknown handling!
|
---|
1906 | cc_cli_close(cl, TRUE);
|
---|
1907 | //
|
---|
1908 | //44 bytes: set aes128 key, Key=16 bytes [Offset=len(password)]
|
---|
1909 | //
|
---|
1910 | } else if (l == 0x2c) {
|
---|
1911 | memcpy(cc->cmd05_aeskey, data + strlen(rdr->r_pwd), 16);
|
---|
1912 | cc->cmd05_mode = MODE_AES;
|
---|
1913 | //
|
---|
1914 | //45 bytes: set aes128 key, Key=16 bytes [Offset=len(username)]
|
---|
1915 | //
|
---|
1916 | } else if (l == 0x2d) {
|
---|
1917 | memcpy(cc->cmd05_aeskey, data + strlen(rdr->r_usr), 16);
|
---|
1918 | cc->cmd05_mode = MODE_AES;
|
---|
1919 | //
|
---|
1920 | //Unknown!!
|
---|
1921 | //
|
---|
1922 | } else {
|
---|
1923 | cs_debug_mask(D_READER,
|
---|
1924 | "%s received improper MSG_SRV_DATA! No change to current mode, mode=%d",
|
---|
1925 | getprefix(), cc->cmd05_mode);
|
---|
1926 | break;
|
---|
1927 | }
|
---|
1928 | cs_debug_mask(D_READER, "%s MSG_SRV_DATA MODE=%s, len=%d", getprefix(),
|
---|
1929 | cmd05_mode_name[cc->cmd05_mode], l);
|
---|
1930 |
|
---|
1931 | break;
|
---|
1932 | case MSG_NEW_CARD_SIDINFO:
|
---|
1933 | case MSG_NEW_CARD: {
|
---|
1934 | if (buf[14] >= rdr->cc_maxhop)
|
---|
1935 | break;
|
---|
1936 |
|
---|
1937 | if (!chk_ctab(b2i(2, buf + 12), &rdr->ctab))
|
---|
1938 | break;
|
---|
1939 |
|
---|
1940 | rdr->tcp_connected = 2; //we have card
|
---|
1941 | rdr->card_status = CARD_INSERTED;
|
---|
1942 |
|
---|
1943 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1944 |
|
---|
1945 | struct cc_card *card = read_card(data, buf[1]==MSG_NEW_CARD_SIDINFO);
|
---|
1946 |
|
---|
1947 | //Check if this card is from us:
|
---|
1948 | LL_ITER *it = ll_iter_create(card->remote_nodes);
|
---|
1949 | uint8 *node_id;
|
---|
1950 | while ((node_id = ll_iter_next(it))) {
|
---|
1951 | if (memcmp(node_id, cc_node_id, sizeof(cc_node_id)) == 0) { //this card is from us!
|
---|
1952 | cs_debug_mask(D_READER, "filtered card because of recursive nodeid: id=%08X, caid=%04X", card->id, card->caid);
|
---|
1953 | cc_free_card(card);
|
---|
1954 | card=NULL;
|
---|
1955 | break;
|
---|
1956 | }
|
---|
1957 | }
|
---|
1958 | ll_iter_release(it);
|
---|
1959 |
|
---|
1960 | //Check Ident filter:
|
---|
1961 | if (card) {
|
---|
1962 | if (!chk_ident(&rdr->ftab, card)) {
|
---|
1963 | cc_free_card(card);
|
---|
1964 | card=NULL;
|
---|
1965 | }
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 | if (card) {
|
---|
1969 | //Check if we already have this card:
|
---|
1970 | it = ll_iter_create(cc->cards);
|
---|
1971 | struct cc_card *old_card;
|
---|
1972 | while ((old_card = ll_iter_next(it))) {
|
---|
1973 | if (old_card->id == card->id || //we aready have this card, delete it
|
---|
1974 | same_card(old_card, card)) {
|
---|
1975 | cc_free_card(card);
|
---|
1976 | card = old_card;
|
---|
1977 | break;
|
---|
1978 | }
|
---|
1979 | }
|
---|
1980 | ll_iter_release(it);
|
---|
1981 |
|
---|
1982 | card->time = time((time_t) 0);
|
---|
1983 | if (!old_card) {
|
---|
1984 | card->hop++; //inkrementing hop
|
---|
1985 | ll_append(cc->cards, card);
|
---|
1986 | set_au_data(cl, rdr, card, NULL);
|
---|
1987 | cc->cards_modified++;
|
---|
1988 | }
|
---|
1989 | }
|
---|
1990 |
|
---|
1991 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1992 |
|
---|
1993 | break;
|
---|
1994 | }
|
---|
1995 |
|
---|
1996 | case MSG_CARD_REMOVED: {
|
---|
1997 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1998 | cc_card_removed(cl, b2i(4, buf + 4));
|
---|
1999 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
2000 | break;
|
---|
2001 | }
|
---|
2002 |
|
---|
2003 | case MSG_CW_NOK1:
|
---|
2004 | case MSG_CW_NOK2:
|
---|
2005 | if (l > 4) {
|
---|
2006 | //Received NOK with payload:
|
---|
2007 | char *msg = (char*) buf + 4;
|
---|
2008 |
|
---|
2009 | //Check for PARTNER connection:
|
---|
2010 | if (strncmp(msg, "PARTNER:", 8) == 0) {
|
---|
2011 | //When Data starts with "PARTNER:" we have an Oscam-cccam-compatible client/server!
|
---|
2012 |
|
---|
2013 | strncpy(cc->remote_oscam, msg+9, sizeof(cc->remote_oscam)-1);
|
---|
2014 | int has_param = check_extended_mode(cl, msg);
|
---|
2015 | if (!cc->is_oscam_cccam) {
|
---|
2016 | cc->is_oscam_cccam = 1;
|
---|
2017 |
|
---|
2018 | //send params back. At the moment there is only "EXT"
|
---|
2019 | char param[14];
|
---|
2020 | if (!has_param)
|
---|
2021 | param[0] = 0;
|
---|
2022 | else {
|
---|
2023 | strcpy(param, " [");
|
---|
2024 | if (cc->extended_mode)
|
---|
2025 | addParam(param, "EXT");
|
---|
2026 | if (cc->cccam220)
|
---|
2027 | addParam(param, "SID");
|
---|
2028 | strcat(param, "]");
|
---|
2029 | }
|
---|
2030 |
|
---|
2031 | sprintf((char*) buf, "PARTNER: OSCam v%s, build #%s (%s)%s",
|
---|
2032 | CS_VERSION, CS_SVN_VERSION, CS_OSTYPE, param);
|
---|
2033 | cc_cmd_send(cl, buf, strlen((char*) buf) + 1, MSG_CW_NOK1);
|
---|
2034 | }
|
---|
2035 | }
|
---|
2036 | return ret;
|
---|
2037 | }
|
---|
2038 |
|
---|
2039 | if (cl->typ == 'c') //for reader only
|
---|
2040 | return ret;
|
---|
2041 |
|
---|
2042 | cc->recv_ecmtask = -1;
|
---|
2043 |
|
---|
2044 | if (cc->just_logged_in)
|
---|
2045 | return -1; // reader restart needed
|
---|
2046 |
|
---|
2047 | pthread_mutex_lock(&cc->cards_busy);
|
---|
2048 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx(cl,
|
---|
2049 | cc->extended_mode ? cc->g_flag : 1, TRUE);
|
---|
2050 | if (!eei) {
|
---|
2051 | cs_debug_mask(D_READER, "%s received extended ecm NOK id %d but not found!",
|
---|
2052 | getprefix(), cc->g_flag);
|
---|
2053 | }
|
---|
2054 | else
|
---|
2055 | {
|
---|
2056 | ushort ecm_idx = eei->ecm_idx;
|
---|
2057 | cc->recv_ecmtask = ecm_idx;
|
---|
2058 | struct cc_card *card = eei->card;
|
---|
2059 | struct cc_srvid srvid = eei->srvid;
|
---|
2060 | free(eei);
|
---|
2061 |
|
---|
2062 | if (card) {
|
---|
2063 | if (buf[1] == MSG_CW_NOK1) //MSG_CW_NOK1: share no more available
|
---|
2064 | cc_card_removed(cl, card->id);
|
---|
2065 | else if (cc->cmd05NOK) {
|
---|
2066 | move_card_to_end(cl, card);
|
---|
2067 | add_sid_block(cl, card, &srvid);
|
---|
2068 | }
|
---|
2069 | else if (!is_good_sid(card, &srvid)) //MSG_CW_NOK2: can't decode
|
---|
2070 | add_sid_block(cl, card, &srvid);
|
---|
2071 | else
|
---|
2072 | remove_good_sid(card, &srvid);
|
---|
2073 |
|
---|
2074 | //retry ecm:
|
---|
2075 | cc_reset_pending(cl, ecm_idx);
|
---|
2076 | } else
|
---|
2077 | cs_debug_mask(D_READER, "%S NOK: NO CARD!", getprefix());
|
---|
2078 | }
|
---|
2079 | cc->cmd05NOK = 0;
|
---|
2080 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
2081 |
|
---|
2082 | if (!cc->extended_mode) {
|
---|
2083 | rdr->available = 1;
|
---|
2084 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
2085 | }
|
---|
2086 |
|
---|
2087 | cc_send_ecm(cl, NULL, NULL);
|
---|
2088 | break;
|
---|
2089 |
|
---|
2090 | case MSG_CW_ECM:
|
---|
2091 | cc->just_logged_in = 0;
|
---|
2092 | if (cl->typ == 'c') { //SERVER:
|
---|
2093 | ECM_REQUEST *er;
|
---|
2094 |
|
---|
2095 | struct cc_card *server_card = cs_malloc(&server_card, sizeof(struct cc_card), QUITERROR);
|
---|
2096 | memset(server_card, 0, sizeof(struct cc_card));
|
---|
2097 | server_card->id = buf[10] << 24 | buf[11] << 16 | buf[12] << 8
|
---|
2098 | | buf[13];
|
---|
2099 | server_card->caid = b2i(2, data);
|
---|
2100 |
|
---|
2101 | if ((er = get_ecmtask())) {
|
---|
2102 | er->caid = b2i(2, buf + 4);
|
---|
2103 | er->srvid = b2i(2, buf + 14);
|
---|
2104 | er->l = buf[16];
|
---|
2105 | memcpy(er->ecm, buf + 17, er->l);
|
---|
2106 | er->prid = b2i(4, buf + 6);
|
---|
2107 | cc->server_ecm_pending++;
|
---|
2108 | er->idx = ++cc->server_ecm_idx;
|
---|
2109 |
|
---|
2110 | cs_debug_mask(
|
---|
2111 | D_CLIENT,
|
---|
2112 | "%s ECM request from client: caid %04x srvid %04x(%d) prid %06x",
|
---|
2113 | getprefix(), er->caid, er->srvid, er->l, er->prid);
|
---|
2114 |
|
---|
2115 | struct cc_srvid srvid;
|
---|
2116 | srvid.sid = er->srvid;
|
---|
2117 | srvid.ecmlen = er->l;
|
---|
2118 | add_extended_ecm_idx(cl, cc->extended_mode ? cc->g_flag : 1,
|
---|
2119 | er->idx, server_card, srvid);
|
---|
2120 |
|
---|
2121 | get_cw(cl, er);
|
---|
2122 |
|
---|
2123 | } else {
|
---|
2124 | cs_debug_mask(D_CLIENT, "%s NO ECMTASK!!!!", getprefix());
|
---|
2125 | free(server_card);
|
---|
2126 | }
|
---|
2127 |
|
---|
2128 | } else { //READER:
|
---|
2129 | pthread_mutex_lock(&cc->cards_busy);
|
---|
2130 | cc->recv_ecmtask = -1;
|
---|
2131 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx(cl,
|
---|
2132 | cc->extended_mode ? cc->g_flag : 1, TRUE);
|
---|
2133 | if (!eei) {
|
---|
2134 | cs_debug_mask(D_READER, "%s received extended ecm id %d but not found!",
|
---|
2135 | getprefix(), cc->g_flag);
|
---|
2136 | }
|
---|
2137 | else
|
---|
2138 | {
|
---|
2139 | ushort ecm_idx = eei->ecm_idx;
|
---|
2140 | cc->recv_ecmtask = ecm_idx;
|
---|
2141 | struct cc_card *card = eei->card;
|
---|
2142 | struct cc_srvid srvid = eei->srvid;
|
---|
2143 | free(eei);
|
---|
2144 |
|
---|
2145 | if (card) {
|
---|
2146 |
|
---|
2147 | if (!cc->extended_mode) {
|
---|
2148 | cc_cw_crypt(cl, buf + 4, card->id);
|
---|
2149 | cc_crypt_cmd0c(cl, buf + 4, 16);
|
---|
2150 | }
|
---|
2151 |
|
---|
2152 | memcpy(cc->dcw, buf + 4, 16);
|
---|
2153 | //fix_dcw(cc->dcw);
|
---|
2154 | if (!cc->extended_mode)
|
---|
2155 | cc_crypt(&cc->block[DECRYPT], buf + 4, l - 4, ENCRYPT); // additional crypto step
|
---|
2156 |
|
---|
2157 | if (is_null_dcw(cc->dcw)) {
|
---|
2158 | cs_debug_mask(D_READER, "%s null dcw received! sid=%04X(%d)", getprefix(),
|
---|
2159 | srvid.sid, srvid.ecmlen);
|
---|
2160 | add_sid_block(cl, card, &srvid);
|
---|
2161 | //ecm retry:
|
---|
2162 | cc_reset_pending(cl, ecm_idx);
|
---|
2163 | buf[1] = MSG_CW_NOK2; //So it's really handled like a nok!
|
---|
2164 | } else {
|
---|
2165 | cs_debug_mask(D_READER, "%s cws: %d %s", getprefix(),
|
---|
2166 | ecm_idx, cs_hexdump(0, cc->dcw, 16));
|
---|
2167 | add_good_sid(cl, card, &srvid);
|
---|
2168 |
|
---|
2169 | //check response time, if > fallbacktime, switch cards!
|
---|
2170 | struct timeb tpe;
|
---|
2171 | cs_ftime(&tpe);
|
---|
2172 | ulong cwlastresptime = 1000*(tpe.time-cc->ecm_time.time)+tpe.millitm-cc->ecm_time.millitm;
|
---|
2173 | if (cwlastresptime > cfg.ftimeout && !cc->extended_mode) {
|
---|
2174 | cs_debug_mask(D_READER, "%s card %04X is too slow, moving to the end...", getprefix(), card->id);
|
---|
2175 | move_card_to_end(cl, card);
|
---|
2176 | }
|
---|
2177 |
|
---|
2178 | }
|
---|
2179 | } else {
|
---|
2180 | cs_debug_mask(D_READER,
|
---|
2181 | "%s warning: ECM-CWS respond by CCCam server without current card!",
|
---|
2182 | getprefix());
|
---|
2183 | }
|
---|
2184 | }
|
---|
2185 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
2186 |
|
---|
2187 | if (!cc->extended_mode) {
|
---|
2188 | rdr->available = 1;
|
---|
2189 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
2190 | }
|
---|
2191 |
|
---|
2192 | //cc_abort_user_ecms();
|
---|
2193 |
|
---|
2194 | cc_send_ecm(cl, NULL, NULL);
|
---|
2195 |
|
---|
2196 | if (cc->max_ecms)
|
---|
2197 | cc->ecm_counter++;
|
---|
2198 | }
|
---|
2199 | break;
|
---|
2200 |
|
---|
2201 | case MSG_KEEPALIVE:
|
---|
2202 | cc->just_logged_in = 0;
|
---|
2203 | if (cl->typ != 'c') {
|
---|
2204 | cs_debug_mask(D_READER, "cccam: keepalive ack");
|
---|
2205 | } else {
|
---|
2206 | //Checking if last answer is one minute ago:
|
---|
2207 | if (cc->answer_on_keepalive + 55 <= time(NULL)) {
|
---|
2208 | cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE);
|
---|
2209 | cs_debug_mask(D_CLIENT, "cccam: keepalive");
|
---|
2210 | cc->answer_on_keepalive = time(NULL);
|
---|
2211 | }
|
---|
2212 | }
|
---|
2213 | break;
|
---|
2214 |
|
---|
2215 | case MSG_CMD_05:
|
---|
2216 | if (cl->typ != 'c') {
|
---|
2217 | cc->just_logged_in = 0;
|
---|
2218 | l = l - 4;//Header Length=4 Byte
|
---|
2219 |
|
---|
2220 | cs_debug_mask(D_READER, "%s MSG_CMD_05 recvd, payload length=%d mode=%d",
|
---|
2221 | getprefix(), l, cc->cmd05_mode);
|
---|
2222 | cc->cmd05_active = 1;
|
---|
2223 | cc->cmd05_data_len = l;
|
---|
2224 | memcpy(&cc->cmd05_data, buf + 4, l);
|
---|
2225 | if (rdr->available && ll_has_elements(cc->cards))
|
---|
2226 | send_cmd05_answer(cl);
|
---|
2227 | }
|
---|
2228 | break;
|
---|
2229 | case MSG_CMD_0B: {
|
---|
2230 | // by Project:Keynation
|
---|
2231 | cs_debug_mask(D_READER, "%s MSG_CMD_0B received (payload=%d)!",
|
---|
2232 | getprefix(), l - 4);
|
---|
2233 |
|
---|
2234 | AES_KEY key;
|
---|
2235 | uint8 aeskey[16];
|
---|
2236 | uint8 out[16];
|
---|
2237 |
|
---|
2238 | memcpy(aeskey, cc->cmd0b_aeskey, 16);
|
---|
2239 | memset(&key, 0, sizeof(key));
|
---|
2240 |
|
---|
2241 | //cs_ddump_mask(D_READER, aeskey, 16, "%s CMD_0B AES key:", getprefix());
|
---|
2242 | //cs_ddump_mask(D_READER, data, 16, "%s CMD_0B received data:", getprefix());
|
---|
2243 |
|
---|
2244 | AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
|
---|
2245 | AES_encrypt((unsigned char *) data, (unsigned char *) &out, &key);
|
---|
2246 |
|
---|
2247 | cs_debug_mask(D_TRACE, "%s sending CMD_0B! ", getprefix());
|
---|
2248 | //cs_ddump_mask(D_READER, out, 16, "%s CMD_0B out:", getprefix());
|
---|
2249 | cc_cmd_send(cl, out, 16, MSG_CMD_0B);
|
---|
2250 |
|
---|
2251 | break;
|
---|
2252 | }
|
---|
2253 |
|
---|
2254 | case MSG_CMD_0C: { //New CCCAM 2.2.0 Server/Client fake check!
|
---|
2255 | int len = l-4;
|
---|
2256 |
|
---|
2257 | if (cl->typ == 'c') { //Only im comming from "client"
|
---|
2258 | cs_debug_mask(D_CLIENT, "%s MSG_CMD_0C received (payload=%d)!", getprefix(), len);
|
---|
2259 |
|
---|
2260 | uint8 bytes[0x20];
|
---|
2261 | if (len < 0x20) //if less then 0x20 bytes, clear others:
|
---|
2262 | memset(data+len, 0, 0x20-len);
|
---|
2263 |
|
---|
2264 | //change first 0x10 bytes to the second:
|
---|
2265 | memcpy(bytes, data+0x10, 0x10);
|
---|
2266 | memcpy(bytes+0x10, data, 0x10);
|
---|
2267 |
|
---|
2268 | //xor data:
|
---|
2269 | int i;
|
---|
2270 | for (i=0;i<0x20;i++)
|
---|
2271 | bytes[i] ^= (data[i] & 0x7F);
|
---|
2272 |
|
---|
2273 | //key is now the 16bit hash of md5:
|
---|
2274 | uint8 md5hash[0x10];
|
---|
2275 | MD5(data, 0x20, md5hash);
|
---|
2276 | memcpy(bytes, md5hash, 0x10);
|
---|
2277 |
|
---|
2278 | cs_debug_mask(D_CLIENT, "%s sending CMD_0C! ", getprefix());
|
---|
2279 | //cs_ddump_mask(D_CLIENT, bytes, 0x20, "%s CMD_0C out:", getprefix());
|
---|
2280 | cc_cmd_send(cl, bytes, 0x20, MSG_CMD_0C);
|
---|
2281 | }
|
---|
2282 | else //reader
|
---|
2283 | {
|
---|
2284 | // by Project:Keynation + Oscam team
|
---|
2285 | cc_crypt_cmd0c(cl, data, len);
|
---|
2286 |
|
---|
2287 | uint8 CMD_0x0C_Command = data[0];
|
---|
2288 |
|
---|
2289 | switch (CMD_0x0C_Command) {
|
---|
2290 |
|
---|
2291 | case 0 : { //RC6
|
---|
2292 | cc->cmd0c_mode = MODE_CMD_0x0C_RC6;
|
---|
2293 | break;
|
---|
2294 | }
|
---|
2295 |
|
---|
2296 | case 1: { //RC4
|
---|
2297 | cc->cmd0c_mode = MODE_CMD_0x0C_RC4;
|
---|
2298 | break;
|
---|
2299 | }
|
---|
2300 |
|
---|
2301 | case 2: { //CC_CRYPT
|
---|
2302 | cc->cmd0c_mode = MODE_CMD_0x0C_CC_CRYPT;
|
---|
2303 | break;
|
---|
2304 | }
|
---|
2305 |
|
---|
2306 | case 3: { //AES
|
---|
2307 | cc->cmd0c_mode = MODE_CMD_0x0C_AES;
|
---|
2308 | break;
|
---|
2309 | }
|
---|
2310 |
|
---|
2311 | case 4 : { //IDEA
|
---|
2312 | cc->cmd0c_mode = MODE_CMD_0x0C_IDEA;
|
---|
2313 | break;
|
---|
2314 | }
|
---|
2315 |
|
---|
2316 | default: {
|
---|
2317 | cc->cmd0c_mode = MODE_CMD_0x0C_NONE;
|
---|
2318 | }
|
---|
2319 | }
|
---|
2320 |
|
---|
2321 | set_cmd0c_cryptkey(cl, data, len);
|
---|
2322 |
|
---|
2323 | cs_debug_mask(D_READER, "%s received MSG_CMD_0C from server! CMD_0x0C_CMD=%d, MODE=%s",
|
---|
2324 | getprefix(), CMD_0x0C_Command, cmd0c_mode_name[cc->cmd0c_mode]);
|
---|
2325 | }
|
---|
2326 | break;
|
---|
2327 | }
|
---|
2328 |
|
---|
2329 | case MSG_CMD_0D: { //key update for the active cmd0x0c algo
|
---|
2330 | int len = l-4;
|
---|
2331 | if (cc->cmd0c_mode == MODE_CMD_0x0C_NONE)
|
---|
2332 | break;
|
---|
2333 |
|
---|
2334 | cc_crypt_cmd0c(cl, data, len);
|
---|
2335 | set_cmd0c_cryptkey(cl, data, len);
|
---|
2336 |
|
---|
2337 | cs_debug_mask(D_READER, "%s received MSG_CMD_0D from server! MODE=%s",
|
---|
2338 | getprefix(), cmd0c_mode_name[cc->cmd0c_mode]);
|
---|
2339 | break;
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 | case MSG_CMD_0E: {
|
---|
2343 | cs_debug_mask(D_READER, "cccam 2.2.x commands not implemented: 0x%02X", buf[1]);
|
---|
2344 | //Unkwon commands...need workout algo
|
---|
2345 | if (cl->typ == 'c') //client connection
|
---|
2346 | {
|
---|
2347 | //switching to an oder version and then disconnect...
|
---|
2348 | strcpy(cfg.cc_version, version[0]);
|
---|
2349 | ret = -1;
|
---|
2350 | }
|
---|
2351 | else //reader connection
|
---|
2352 | {
|
---|
2353 | strcpy(cl->reader->cc_version, version[0]);
|
---|
2354 | strcpy(cl->reader->cc_build, build[0]);
|
---|
2355 | cc_cli_close(cl, TRUE);
|
---|
2356 | }
|
---|
2357 | break;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | case MSG_EMM_ACK: {
|
---|
2361 | cc->just_logged_in = 0;
|
---|
2362 | if (cl->typ == 'c') { //EMM Request received
|
---|
2363 | cc_cmd_send(cl, NULL, 0, MSG_EMM_ACK); //Send back ACK
|
---|
2364 | if (l > 4) {
|
---|
2365 | cs_debug_mask(D_EMM, "%s EMM Request received!", getprefix());
|
---|
2366 |
|
---|
2367 | if (!ll_count(cl->aureader_list)) {
|
---|
2368 | cs_debug_mask(
|
---|
2369 | D_EMM,
|
---|
2370 | "%s EMM Request discarded because au is not assigned to an reader!",
|
---|
2371 | getprefix());
|
---|
2372 | return MSG_EMM_ACK;
|
---|
2373 | }
|
---|
2374 |
|
---|
2375 | EMM_PACKET *emm = cs_malloc(&emm, sizeof(EMM_PACKET), QUITERROR);
|
---|
2376 | memset(emm, 0, sizeof(EMM_PACKET));
|
---|
2377 | emm->caid[0] = buf[4];
|
---|
2378 | emm->caid[1] = buf[5];
|
---|
2379 | emm->provid[0] = buf[7];
|
---|
2380 | emm->provid[1] = buf[8];
|
---|
2381 | emm->provid[2] = buf[9];
|
---|
2382 | emm->provid[3] = buf[10];
|
---|
2383 | //emm->hexserial[0] = buf[11];
|
---|
2384 | //emm->hexserial[1] = buf[12];
|
---|
2385 | //emm->hexserial[2] = buf[13];
|
---|
2386 | //emm->hexserial[3] = buf[14];
|
---|
2387 | emm->l = buf[15];
|
---|
2388 | memcpy(emm->emm, buf + 16, emm->l);
|
---|
2389 | //emm->type = UNKNOWN;
|
---|
2390 | //emm->cidx = cs_idx;
|
---|
2391 | do_emm(cl, emm);
|
---|
2392 | free(emm);
|
---|
2393 | }
|
---|
2394 | } else { //Our EMM Request Ack!
|
---|
2395 | cs_debug_mask(D_EMM, "%s EMM ACK!", getprefix());
|
---|
2396 | if (!cc->extended_mode) {
|
---|
2397 | rdr->available = 1;
|
---|
2398 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
2399 | }
|
---|
2400 | cc_send_ecm(cl, NULL, NULL);
|
---|
2401 | }
|
---|
2402 | break;
|
---|
2403 | }
|
---|
2404 | default:
|
---|
2405 | //cs_ddump_mask(D_CLIENT, buf, l, "%s unhandled msg: %d len=%d", getprefix(), buf[1], l);
|
---|
2406 | break;
|
---|
2407 | }
|
---|
2408 |
|
---|
2409 | if (cc->max_ecms && (cc->ecm_counter > cc->max_ecms)) {
|
---|
2410 | cs_debug_mask(D_READER, "%s max ecms (%d) reached, cycle connection!", getprefix(),
|
---|
2411 | cc->max_ecms);
|
---|
2412 | //cc_cycle_connection();
|
---|
2413 | cc_cli_close(cl, TRUE);
|
---|
2414 | //cc_send_ecm(NULL, NULL);
|
---|
2415 | }
|
---|
2416 | return ret;
|
---|
2417 | }
|
---|
2418 |
|
---|
2419 | /**
|
---|
2420 | * Reader: write dcw to receive
|
---|
2421 | */
|
---|
2422 | int cc_recv_chk(struct s_client *cl, uchar *dcw, int *rc, uchar *buf, int UNUSED(n)) {
|
---|
2423 | struct cc_data *cc = cl->cc;
|
---|
2424 |
|
---|
2425 | if (buf[1] == MSG_CW_ECM) {
|
---|
2426 | memcpy(dcw, cc->dcw, 16);
|
---|
2427 | //cs_debug_mask(D_CLIENT, "cccam: recv chk - MSG_CW %d - %s", cc->recv_ecmtask,
|
---|
2428 | // cs_hexdump(0, dcw, 16));
|
---|
2429 | *rc = 1;
|
---|
2430 | return (cc->recv_ecmtask);
|
---|
2431 | } else if ((buf[1] == (MSG_CW_NOK1)) || (buf[1] == (MSG_CW_NOK2))) {
|
---|
2432 | *rc = 0;
|
---|
2433 | if (cc->is_oscam_cccam)
|
---|
2434 | return (cc->recv_ecmtask);
|
---|
2435 | else
|
---|
2436 | return -1;
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 | return (-1);
|
---|
2440 | }
|
---|
2441 |
|
---|
2442 | //int is_softfail(int rc)
|
---|
2443 | //{
|
---|
2444 | // //see oscam.c send_dcw() for a full list
|
---|
2445 | // switch(rc)
|
---|
2446 | // {
|
---|
2447 | // case 5: // 5 = timeout
|
---|
2448 | // case 6: // 6 = sleeping
|
---|
2449 | // case 7: // 7 = fake
|
---|
2450 | // case 10:// 10= no card
|
---|
2451 | // case 11:// 11= expdate
|
---|
2452 | // case 12:// 12= disabled
|
---|
2453 | // case 13:// 13= stopped
|
---|
2454 | // case 14:// 100= unhandled
|
---|
2455 | // return TRUE;
|
---|
2456 | // }
|
---|
2457 | // return FALSE;
|
---|
2458 | //}
|
---|
2459 |
|
---|
2460 |
|
---|
2461 |
|
---|
2462 | /**
|
---|
2463 | * Server: send DCW to client
|
---|
2464 | */
|
---|
2465 | void cc_send_dcw(struct s_client *cl, ECM_REQUEST *er) {
|
---|
2466 | uchar buf[16];
|
---|
2467 | struct cc_data *cc = cl->cc;
|
---|
2468 |
|
---|
2469 | memset(buf, 0, sizeof(buf));
|
---|
2470 |
|
---|
2471 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx_by_idx(cl, er->idx,
|
---|
2472 | TRUE);
|
---|
2473 |
|
---|
2474 | if (er->rc < E_NOTFOUND && eei && eei->card) {
|
---|
2475 | memcpy(buf, er->cw, sizeof(buf));
|
---|
2476 | //fix_dcw(buf);
|
---|
2477 | //cs_debug_mask(D_TRACE, "%s send cw: %s cpti: %d", getprefix(),
|
---|
2478 | // cs_hexdump(0, buf, 16), er->cpti);
|
---|
2479 | if (!cc->extended_mode)
|
---|
2480 | cc_cw_crypt(cl, buf, eei->card->id);
|
---|
2481 | else
|
---|
2482 | cc->g_flag = eei->send_idx;
|
---|
2483 | cc_cmd_send(cl, buf, 16, MSG_CW_ECM);
|
---|
2484 | if (!cc->extended_mode)
|
---|
2485 | cc_crypt(&cc->block[ENCRYPT], buf, 16, ENCRYPT); // additional crypto step
|
---|
2486 | free(eei->card);
|
---|
2487 | } else {
|
---|
2488 | //cs_debug_mask(D_TRACE, "%s send cw: NOK cpti: %d", getprefix(),
|
---|
2489 | // er->cpti);
|
---|
2490 |
|
---|
2491 | if (eei && cc->extended_mode)
|
---|
2492 | cc->g_flag = eei->send_idx;
|
---|
2493 |
|
---|
2494 | int nok;
|
---|
2495 | if (!eei || !eei->card)
|
---|
2496 | nok = MSG_CW_NOK1; //share no more available
|
---|
2497 | else
|
---|
2498 | nok = MSG_CW_NOK2; //can't decode
|
---|
2499 | cc_cmd_send(cl, NULL, 0, nok);
|
---|
2500 | }
|
---|
2501 | cc->server_ecm_pending--;
|
---|
2502 | free(eei);
|
---|
2503 | }
|
---|
2504 |
|
---|
2505 | int cc_recv(struct s_client *cl, uchar *buf, int l) {
|
---|
2506 | int n;
|
---|
2507 | uchar *cbuf;
|
---|
2508 | struct cc_data *cc = cl->cc;
|
---|
2509 |
|
---|
2510 | if (buf == NULL || l <= 0)
|
---|
2511 | return -1;
|
---|
2512 | cbuf = cs_malloc(&cbuf, l, QUITERROR);
|
---|
2513 | memcpy(cbuf, buf, l); // make a copy of buf
|
---|
2514 |
|
---|
2515 | pthread_mutex_lock(&cc->lock);
|
---|
2516 |
|
---|
2517 | n = cc_msg_recv(cl, cbuf, l); // recv and decrypt msg
|
---|
2518 |
|
---|
2519 | //cs_ddump_mask(D_CLIENT, cbuf, n, "cccam: received %d bytes from %s", n, remote_txt());
|
---|
2520 | cl->last = time((time_t *) 0);
|
---|
2521 |
|
---|
2522 | if (n <= 0) {
|
---|
2523 | cs_log("%s connection closed by %s", getprefix(), remote_txt());
|
---|
2524 | n = -1;
|
---|
2525 | } else if (n < 4) {
|
---|
2526 | cs_log("%s packet to small (%d bytes)", getprefix(), n);
|
---|
2527 | n = -1;
|
---|
2528 | } else {
|
---|
2529 | // parse it and write it back, if we have received something of value
|
---|
2530 | n = cc_parse_msg(cl, cbuf, n);
|
---|
2531 | memcpy(buf, cbuf, l);
|
---|
2532 | }
|
---|
2533 |
|
---|
2534 | pthread_mutex_unlock(&cc->lock);
|
---|
2535 |
|
---|
2536 | NULLFREE(cbuf);
|
---|
2537 |
|
---|
2538 | if (n == -1) {
|
---|
2539 | if (cl->typ != 'c')
|
---|
2540 | cc_cli_close(cl, TRUE);
|
---|
2541 | }
|
---|
2542 |
|
---|
2543 | return n;
|
---|
2544 | }
|
---|
2545 |
|
---|
2546 | /**
|
---|
2547 | * This function checks for hexserial changes on cards.
|
---|
2548 | * We update the share-list if a card has changed
|
---|
2549 | */
|
---|
2550 | ulong get_reader_hexserial_crc(struct s_client *UNUSED(cl)) {
|
---|
2551 | ulong crc = 0;
|
---|
2552 | struct s_reader *rdr;
|
---|
2553 | for (rdr = first_active_reader; rdr; rdr = rdr->next) {
|
---|
2554 | if (rdr->client && !rdr->audisabled)
|
---|
2555 | crc += crc32(0, rdr->hexserial, 8);
|
---|
2556 | }
|
---|
2557 | return crc;
|
---|
2558 | }
|
---|
2559 |
|
---|
2560 | ulong get_reader_prid(struct s_reader *rdr, int j) {
|
---|
2561 | return b2i(4, rdr->prid[j]);
|
---|
2562 | }
|
---|
2563 | //ulong get_reader_prid(struct s_reader *rdr, int j) {
|
---|
2564 | // ulong prid;
|
---|
2565 | // if (!(rdr->typ & R_IS_CASCADING)) { // Real cardreaders have 4-byte Providers
|
---|
2566 | // prid = b2i(4, &rdr->prid[j][0]);
|
---|
2567 | // //prid = (rdr->prid[j][0] << 24) | (rdr->prid[j][1] << 16)
|
---|
2568 | // // | (rdr->prid[j][2] << 8) | (rdr->prid[j][3] & 0xFF);
|
---|
2569 | // } else { // Cascading/Network-reader 3-bytes Providers
|
---|
2570 | // prid = b2i(3, &rdr->prid[j][0]);
|
---|
2571 | // //prid = (rdr->prid[j][0] << 16) | (rdr->prid[j][1] << 8)
|
---|
2572 | // // | (rdr->prid[j][2] & 0xFF);
|
---|
2573 | //
|
---|
2574 | // }
|
---|
2575 | // return prid;
|
---|
2576 | //}
|
---|
2577 |
|
---|
2578 | void copy_sids(LLIST *dst, LLIST *src) {
|
---|
2579 | LL_ITER *it_src = ll_iter_create(src);
|
---|
2580 | LL_ITER *it_dst = ll_iter_create(dst);
|
---|
2581 | struct cc_srvid *srvid_src;
|
---|
2582 | struct cc_srvid *srvid_dst;
|
---|
2583 | while ((srvid_src=ll_iter_next(it_src))) {
|
---|
2584 | ll_iter_reset(it_dst);
|
---|
2585 | while ((srvid_dst=ll_iter_next(it_dst))) {
|
---|
2586 | if (sid_eq(srvid_src, srvid_dst))
|
---|
2587 | break;
|
---|
2588 | }
|
---|
2589 | if (!srvid_dst) {
|
---|
2590 | srvid_dst = cs_malloc(&srvid_dst, sizeof(struct cc_srvid), QUITERROR);
|
---|
2591 | memcpy(srvid_dst, srvid_src, sizeof(struct cc_srvid));
|
---|
2592 | ll_iter_insert(it_dst, srvid_dst);
|
---|
2593 | }
|
---|
2594 | }
|
---|
2595 | ll_iter_release(it_dst);
|
---|
2596 | ll_iter_release(it_src);
|
---|
2597 | }
|
---|
2598 |
|
---|
2599 | int add_card_providers(struct cc_card *dest_card, struct cc_card *card,
|
---|
2600 | int copy_remote_nodes) {
|
---|
2601 | int modified = 0;
|
---|
2602 |
|
---|
2603 | //1. Copy nonexisting providers, ignore double:
|
---|
2604 | struct cc_provider *prov_info;
|
---|
2605 | LL_ITER *it_src = ll_iter_create(card->providers);
|
---|
2606 | LL_ITER *it_dst = ll_iter_create(dest_card->providers);
|
---|
2607 |
|
---|
2608 | struct cc_provider *provider;
|
---|
2609 | while ((provider = ll_iter_next(it_src))) {
|
---|
2610 | ll_iter_reset(it_dst);
|
---|
2611 | while ((prov_info = ll_iter_next(it_dst))) {
|
---|
2612 | if (prov_info->prov == provider->prov)
|
---|
2613 | break;
|
---|
2614 | }
|
---|
2615 | if (!prov_info) {
|
---|
2616 | struct cc_provider *prov_new = cs_malloc(&prov_new, sizeof(struct cc_provider), QUITERROR);
|
---|
2617 | memcpy(prov_new, provider, sizeof(struct cc_provider));
|
---|
2618 | ll_iter_insert(it_dst, prov_new);
|
---|
2619 | modified = 1;
|
---|
2620 | }
|
---|
2621 | }
|
---|
2622 | ll_iter_release(it_dst);
|
---|
2623 | ll_iter_release(it_src);
|
---|
2624 |
|
---|
2625 | if (copy_remote_nodes) {
|
---|
2626 | //2. Copy nonexisting remote_nodes, ignoring existing:
|
---|
2627 | it_src = ll_iter_create(card->remote_nodes);
|
---|
2628 | it_dst = ll_iter_create(dest_card->remote_nodes);
|
---|
2629 | uint8 *remote_node;
|
---|
2630 | uint8 *remote_node2;
|
---|
2631 | while ((remote_node = ll_iter_next(it_src))) {
|
---|
2632 | ll_iter_reset(it_dst);
|
---|
2633 | while ((remote_node2 = ll_iter_next(it_dst))) {
|
---|
2634 | if (memcmp(remote_node, remote_node2, 8) == 0)
|
---|
2635 | break;
|
---|
2636 | }
|
---|
2637 | if (!remote_node2) {
|
---|
2638 | uint8* remote_node_new = cs_malloc(&remote_node_new, 8, QUITERROR);
|
---|
2639 | memcpy(remote_node_new, remote_node, 8);
|
---|
2640 | ll_iter_insert(it_dst, remote_node_new);
|
---|
2641 | modified = 1;
|
---|
2642 | }
|
---|
2643 | }
|
---|
2644 | ll_iter_release(it_dst);
|
---|
2645 | ll_iter_release(it_src);
|
---|
2646 | }
|
---|
2647 | return modified;
|
---|
2648 | }
|
---|
2649 |
|
---|
2650 | struct cc_card *create_card(struct cc_card *card) {
|
---|
2651 | struct cc_card *card2 = cs_malloc(&card2, sizeof(struct cc_card), QUITERROR);
|
---|
2652 | if (card)
|
---|
2653 | memcpy(card2, card, sizeof(struct cc_card));
|
---|
2654 | else
|
---|
2655 | memset(card2, 0, sizeof(struct cc_card));
|
---|
2656 | card2->providers = ll_create();
|
---|
2657 | card2->badsids = ll_create();
|
---|
2658 | card2->goodsids = ll_create();
|
---|
2659 | card2->remote_nodes = ll_create();
|
---|
2660 |
|
---|
2661 | if (card) {
|
---|
2662 | copy_sids(card2->goodsids, card->goodsids);
|
---|
2663 | copy_sids(card2->badsids, card->badsids);
|
---|
2664 | }
|
---|
2665 |
|
---|
2666 | return card2;
|
---|
2667 | }
|
---|
2668 |
|
---|
2669 | struct cc_card *create_card2(struct s_reader *rdr, int j, uint16 caid, uint8 hop, uint8 reshare) {
|
---|
2670 |
|
---|
2671 | struct cc_card *card = create_card(NULL);
|
---|
2672 | card->remote_id = (rdr?(rdr->cc_id << 16):0x7F)|j;
|
---|
2673 | card->caid = caid;
|
---|
2674 | card->hop = hop;
|
---|
2675 | card->maxdown = reshare;
|
---|
2676 | card->origin_reader = rdr;
|
---|
2677 | return card;
|
---|
2678 | }
|
---|
2679 |
|
---|
2680 | /**
|
---|
2681 | * num_same_providers checks if card1 has exactly the same providers as card2
|
---|
2682 | * returns same provider count
|
---|
2683 | **/
|
---|
2684 | int num_same_providers(struct cc_card *card1, struct cc_card *card2) {
|
---|
2685 |
|
---|
2686 | int found=0;
|
---|
2687 |
|
---|
2688 | LL_ITER *it1 = ll_iter_create(card1->providers);
|
---|
2689 | LL_ITER *it2 = ll_iter_create(card2->providers);
|
---|
2690 |
|
---|
2691 | struct cc_provider *prov1, *prov2;
|
---|
2692 |
|
---|
2693 | while ((prov1=ll_iter_next(it1))) {
|
---|
2694 |
|
---|
2695 | ll_iter_reset(it2);
|
---|
2696 | while ((prov2=ll_iter_next(it2))) {
|
---|
2697 | if (prov1->prov==prov2->prov) {
|
---|
2698 | found++;
|
---|
2699 | break;
|
---|
2700 | }
|
---|
2701 |
|
---|
2702 | }
|
---|
2703 | }
|
---|
2704 |
|
---|
2705 | ll_iter_release(it2);
|
---|
2706 | ll_iter_release(it1);
|
---|
2707 |
|
---|
2708 | return found;
|
---|
2709 | }
|
---|
2710 |
|
---|
2711 | /**
|
---|
2712 | * equal_providers checks if card1 has exactly the same providers as card2
|
---|
2713 | * returns 1=equal 0=different
|
---|
2714 | **/
|
---|
2715 | int equal_providers(struct cc_card *card1, struct cc_card *card2) {
|
---|
2716 |
|
---|
2717 | if (ll_count(card1->providers) != ll_count(card2->providers))
|
---|
2718 | return 0;
|
---|
2719 |
|
---|
2720 | LL_ITER *it1 = ll_iter_create(card1->providers);
|
---|
2721 | LL_ITER *it2 = ll_iter_create(card2->providers);
|
---|
2722 |
|
---|
2723 | struct cc_provider *prov1, *prov2;
|
---|
2724 |
|
---|
2725 | while ((prov1=ll_iter_next(it1))) {
|
---|
2726 |
|
---|
2727 | ll_iter_reset(it2);
|
---|
2728 | while ((prov2=ll_iter_next(it2))) {
|
---|
2729 | if (prov1->prov==prov2->prov) {
|
---|
2730 | break;
|
---|
2731 | }
|
---|
2732 |
|
---|
2733 | }
|
---|
2734 | if (!prov2) break;
|
---|
2735 | }
|
---|
2736 |
|
---|
2737 | ll_iter_release(it2);
|
---|
2738 | ll_iter_release(it1);
|
---|
2739 |
|
---|
2740 | return (prov1 == NULL);
|
---|
2741 | }
|
---|
2742 |
|
---|
2743 | /**
|
---|
2744 | * Adds a new card to a cardlist.
|
---|
2745 | */
|
---|
2746 | int add_card_to_serverlist(struct s_reader *rdr, struct s_client *cl, LLIST *cardlist, struct cc_card *card, int reshare, int au_allowed) {
|
---|
2747 |
|
---|
2748 | if (!chk_ident(&cl->ftab, card))
|
---|
2749 | return 0;
|
---|
2750 | if (rdr && !chk_ident(&rdr->client->ftab, card))
|
---|
2751 | return 0;
|
---|
2752 |
|
---|
2753 | if (!ll_has_elements(card->providers)) { //No providers? Add null-provider:
|
---|
2754 | struct cc_provider *prov = cs_malloc(&prov, sizeof(struct cc_provider), QUITERROR);
|
---|
2755 | memset(prov, 0, sizeof(struct cc_provider));
|
---|
2756 | ll_append(card->providers, prov);
|
---|
2757 | }
|
---|
2758 |
|
---|
2759 | struct cc_data *cc = cl->cc;
|
---|
2760 | int modified = 0;
|
---|
2761 | LL_ITER *it = ll_iter_create(cardlist);
|
---|
2762 | struct cc_card *card2;
|
---|
2763 |
|
---|
2764 | //Minimize all, transmit just CAID, merge providers:
|
---|
2765 | if (cfg.cc_minimize_cards == MINIMIZE_CAID) {
|
---|
2766 | while ((card2 = ll_iter_next(it)))
|
---|
2767 | if (card2->caid == card->caid &&
|
---|
2768 | !memcmp(card->hexserial, card2->hexserial, sizeof(card->hexserial))) {
|
---|
2769 |
|
---|
2770 | //Merge cards only if resulting providercount is smaller than CS_MAXPROV
|
---|
2771 | int nsame, ndiff, nnew;
|
---|
2772 |
|
---|
2773 | nsame = num_same_providers(card, card2); //count same cards
|
---|
2774 | ndiff = ll_count(card->providers)-nsame; //cound different cards, this cound will be added
|
---|
2775 | nnew = ndiff + ll_count(card2->providers); //new card count after add. because its limited to CS_MAXPROV, dont add it
|
---|
2776 |
|
---|
2777 | if (nnew <= CS_MAXPROV)
|
---|
2778 | break;
|
---|
2779 | }
|
---|
2780 | if (!card2) {
|
---|
2781 | card2 = create_card(card);
|
---|
2782 | if (!au_allowed)
|
---|
2783 | memset(card2->hexserial, 0, 8);
|
---|
2784 | card2->hop = 0;
|
---|
2785 | card2->remote_id = card->remote_id;
|
---|
2786 | card2->maxdown = reshare;
|
---|
2787 | ll_clear_data(card2->badsids);
|
---|
2788 | ll_iter_insert(it, card2);
|
---|
2789 | modified = 1;
|
---|
2790 |
|
---|
2791 | }
|
---|
2792 | else cc->card_dup_count++;
|
---|
2793 |
|
---|
2794 | add_card_providers(card2, card, 0); //merge all providers
|
---|
2795 |
|
---|
2796 | }
|
---|
2797 |
|
---|
2798 | //Removed duplicate cards, keeping card with lower hop:
|
---|
2799 | else if (cfg.cc_minimize_cards == MINIMIZE_HOPS) {
|
---|
2800 | while ((card2 = ll_iter_next(it))) {
|
---|
2801 | if (card2->caid == card->caid &&
|
---|
2802 | !memcmp(card->hexserial, card2->hexserial, sizeof(card->hexserial)) &&
|
---|
2803 | equal_providers(card, card2)) {
|
---|
2804 | break;
|
---|
2805 | }
|
---|
2806 | }
|
---|
2807 |
|
---|
2808 | if (card2 && card2->hop > card->hop) { //hop is smaller, drop old card
|
---|
2809 | cc_free_card(card2);
|
---|
2810 | ll_iter_remove(it);
|
---|
2811 | card2 = NULL;
|
---|
2812 | cc->card_dup_count++;
|
---|
2813 | }
|
---|
2814 |
|
---|
2815 | if (!card2) {
|
---|
2816 | card2 = create_card(card);
|
---|
2817 | if (!au_allowed)
|
---|
2818 | memset(card2->hexserial, 0, 8);
|
---|
2819 | card2->hop = card->hop;
|
---|
2820 | card2->remote_id = card->remote_id;
|
---|
2821 | card2->maxdown = reshare;
|
---|
2822 | ll_clear_data(card2->badsids);
|
---|
2823 | ll_iter_insert(it, card2);
|
---|
2824 | add_card_providers(card2, card, 1);
|
---|
2825 | modified = 1;
|
---|
2826 | }
|
---|
2827 | else cc->card_dup_count++;
|
---|
2828 |
|
---|
2829 | }
|
---|
2830 |
|
---|
2831 | //like cccam:
|
---|
2832 | else { //just remove duplicate cards (same ids)
|
---|
2833 | while ((card2 = ll_iter_next(it))) {
|
---|
2834 | if (same_card(card, card2))
|
---|
2835 | break;
|
---|
2836 | }
|
---|
2837 | if (card2 && card2->hop > card->hop) {
|
---|
2838 | cc_free_card(card2);
|
---|
2839 | ll_iter_remove(it);
|
---|
2840 | card2 = NULL;
|
---|
2841 | cc->card_dup_count++;
|
---|
2842 | }
|
---|
2843 | if (!card2) {
|
---|
2844 | card2 = create_card(card);
|
---|
2845 | if (!au_allowed)
|
---|
2846 | memset(card2->hexserial, 0, 8);
|
---|
2847 | card2->hop = card->hop;
|
---|
2848 | card2->remote_id = card->remote_id;
|
---|
2849 | card2->maxdown = reshare;
|
---|
2850 | card2->origin_reader = rdr;
|
---|
2851 | ll_iter_insert(it, card2);
|
---|
2852 | add_card_providers(card2, card, 1);
|
---|
2853 | modified = 1;
|
---|
2854 | }
|
---|
2855 | else cc->card_dup_count++;
|
---|
2856 | }
|
---|
2857 | ll_iter_release(it);
|
---|
2858 | return modified;
|
---|
2859 | }
|
---|
2860 |
|
---|
2861 | int find_reported_card(struct s_client * cl, struct cc_card *card1)
|
---|
2862 | {
|
---|
2863 | struct cc_data *cc = cl->cc;
|
---|
2864 |
|
---|
2865 | LL_ITER *it = ll_iter_create(cc->reported_carddatas);
|
---|
2866 | struct cc_card *card2;
|
---|
2867 | while ((card2 = ll_iter_next(it))) {
|
---|
2868 | if (same_card(card1, card2)) {
|
---|
2869 | card1->id = card2->id; //Set old id !!
|
---|
2870 | cc_free_card(card2);
|
---|
2871 | ll_iter_remove(it);
|
---|
2872 | ll_iter_release(it);
|
---|
2873 | return 1; //Old card and new card are equal!
|
---|
2874 | }
|
---|
2875 | }
|
---|
2876 | ll_iter_release(it);
|
---|
2877 | return 0; //Card not found
|
---|
2878 | }
|
---|
2879 |
|
---|
2880 | int report_card(struct s_client *cl, struct cc_card *card, LLIST *new_reported_carddatas, int au_allowed)
|
---|
2881 | {
|
---|
2882 | int res = 0;
|
---|
2883 | struct cc_data *cc = cl->cc;
|
---|
2884 | int ext = cc->cccam220;
|
---|
2885 | if (!find_reported_card(cl, card)) { //Add new card:
|
---|
2886 | uint8 *buf = cs_malloc(&buf, CC_MAXMSGSIZE, QUITERROR);
|
---|
2887 | if (!cc->report_carddata_id)
|
---|
2888 | cc->report_carddata_id = 0x64;
|
---|
2889 | card->id = cc->report_carddata_id;
|
---|
2890 | cc->report_carddata_id++;
|
---|
2891 |
|
---|
2892 | int len = write_card(cc, buf, card, TRUE, ext, au_allowed);
|
---|
2893 | res = cc_cmd_send(cl, buf, len, ext?MSG_NEW_CARD_SIDINFO:MSG_NEW_CARD);
|
---|
2894 | cc->card_added_count++;
|
---|
2895 | free(buf);
|
---|
2896 | }
|
---|
2897 | cc_add_reported_carddata(new_reported_carddatas, card);
|
---|
2898 | return res;
|
---|
2899 | }
|
---|
2900 |
|
---|
2901 | void add_good_bad_sids(struct s_sidtab *ptr, struct s_client *cl, struct cc_card *card) {
|
---|
2902 | struct cc_data *cc = cl->cc;
|
---|
2903 | if (cc->cccam220) {
|
---|
2904 | //good sids:
|
---|
2905 | int l;
|
---|
2906 | for (l=0;l<ptr->num_srvid;l++) {
|
---|
2907 | struct cc_srvid *srvid = malloc(sizeof(struct cc_srvid));
|
---|
2908 | srvid->sid = ptr->srvid[l];
|
---|
2909 | srvid->ecmlen = 0; //0=undefined, also not used with "O" CCcam
|
---|
2910 | ll_append(card->goodsids, srvid);
|
---|
2911 | }
|
---|
2912 |
|
---|
2913 | //bad sids:
|
---|
2914 | if (cl->sidtabno) {
|
---|
2915 | struct s_sidtab *ptr_no;
|
---|
2916 | int n;
|
---|
2917 | for (n=0,ptr_no=cfg.sidtab; ptr_no; ptr_no=ptr_no->next,n++) {
|
---|
2918 | if (cl->sidtabno&((SIDTABBITS)1<<n)) {
|
---|
2919 | int m;
|
---|
2920 | int ok_caid = FALSE;
|
---|
2921 | for (m=0;m<ptr_no->num_caid;m++) { //search bad sids for this caid:
|
---|
2922 | if (ptr_no->caid[m] == card->caid) {
|
---|
2923 | ok_caid = TRUE;
|
---|
2924 | break;
|
---|
2925 | }
|
---|
2926 | }
|
---|
2927 | if (ok_caid) {
|
---|
2928 | for (l=0;l<ptr_no->num_srvid;l++) {
|
---|
2929 | struct cc_srvid *srvid = malloc(sizeof(struct cc_srvid));
|
---|
2930 | srvid->sid = ptr_no->srvid[l];
|
---|
2931 | srvid->ecmlen = 0; //0=undefined, also not used with "O" CCcam
|
---|
2932 | ll_append(card->badsids, srvid);
|
---|
2933 | }
|
---|
2934 | }
|
---|
2935 | }
|
---|
2936 | }
|
---|
2937 | }
|
---|
2938 | }
|
---|
2939 | }
|
---|
2940 |
|
---|
2941 | void add_good_bad_sids_for_card(struct s_client *cl, struct cc_card *card) {
|
---|
2942 | struct cc_data *cc = cl->cc;
|
---|
2943 | if (cc->cccam220) {
|
---|
2944 | struct s_sidtab *ptr;
|
---|
2945 | int j;
|
---|
2946 | for (j=0,ptr=cfg.sidtab; ptr; ptr=ptr->next,j++) {
|
---|
2947 | if (cl->sidtabok&((SIDTABBITS)1<<j)) {
|
---|
2948 | int m;
|
---|
2949 | int ok_caid = FALSE;
|
---|
2950 | for (m=0;m<ptr->num_caid;m++) { //search good sids for this caid:
|
---|
2951 | if (ptr->caid[m] == card->caid) {
|
---|
2952 | ok_caid = TRUE;
|
---|
2953 | break;
|
---|
2954 | }
|
---|
2955 | }
|
---|
2956 | if (ok_caid) {
|
---|
2957 | add_good_bad_sids(ptr, cl, card);
|
---|
2958 | }
|
---|
2959 | }
|
---|
2960 | }
|
---|
2961 | }
|
---|
2962 | }
|
---|
2963 |
|
---|
2964 | /**
|
---|
2965 | * Server:
|
---|
2966 | * Reports all caid/providers to the connected clients
|
---|
2967 | * returns 1=ok, 0=error
|
---|
2968 | *
|
---|
2969 | * cfg.cc_reshare_services=0 CCCAM reader reshares only received cards
|
---|
2970 | * =1 CCCAM reader reshares received cards + defined services
|
---|
2971 | * =2 CCCAM reader reshares only defined reader-services as virtual cards
|
---|
2972 | * =3 CCCAM reader reshares only defined user-services as virtual cards
|
---|
2973 | */
|
---|
2974 | int cc_srv_report_cards(struct s_client *cl) {
|
---|
2975 | int j;
|
---|
2976 | uint k;
|
---|
2977 | uint8 hop = 0;
|
---|
2978 | int reshare, usr_reshare, usr_ignorereshare, reader_reshare, maxhops, flt = 0;
|
---|
2979 |
|
---|
2980 | struct cc_data *cc = cl->cc;
|
---|
2981 |
|
---|
2982 | maxhops = cl->account->cccmaxhops;
|
---|
2983 | usr_reshare = cl->account->cccreshare;
|
---|
2984 | usr_ignorereshare = cl->account->cccignorereshare;
|
---|
2985 |
|
---|
2986 | LLIST *server_cards = ll_create();
|
---|
2987 | if (!cc->reported_carddatas)
|
---|
2988 | cc->reported_carddatas = ll_create();
|
---|
2989 | LLIST *new_reported_carddatas = ll_create();
|
---|
2990 |
|
---|
2991 | cc->card_added_count = 0;
|
---|
2992 | cc->card_removed_count = 0;
|
---|
2993 | cc->card_dup_count = 0;
|
---|
2994 |
|
---|
2995 | int isau = (ll_count(cl->aureader_list))?1:0;
|
---|
2996 |
|
---|
2997 | //User-Services:
|
---|
2998 | if (cfg.cc_reshare_services==3 && cfg.sidtab && cl->sidtabok) {
|
---|
2999 | struct s_sidtab *ptr;
|
---|
3000 | for (j=0,ptr=cfg.sidtab; ptr; ptr=ptr->next,j++) {
|
---|
3001 | if (cl->sidtabok&((SIDTABBITS)1<<j)) {
|
---|
3002 | int k;
|
---|
3003 | for (k=0;k<ptr->num_caid;k++) {
|
---|
3004 | struct cc_card *card = create_card2(NULL, (j<<8)|k, ptr->caid[k], hop, usr_reshare);
|
---|
3005 | int l;
|
---|
3006 | for (l=0;l<ptr->num_provid;l++) {
|
---|
3007 | struct cc_provider *prov = cs_malloc(&prov, sizeof(struct cc_provider), QUITERROR);
|
---|
3008 | memset(prov, 0, sizeof(struct cc_provider));
|
---|
3009 | prov->prov = ptr->provid[l];
|
---|
3010 | ll_append(card->providers, prov);
|
---|
3011 | }
|
---|
3012 |
|
---|
3013 | //CCcam 2.2.x proto can transfer good and bad sids:
|
---|
3014 | add_good_bad_sids(ptr, cl, card);
|
---|
3015 |
|
---|
3016 | add_card_to_serverlist(NULL, cl, server_cards, card, usr_reshare, isau);
|
---|
3017 | }
|
---|
3018 | flt=1;
|
---|
3019 | }
|
---|
3020 | }
|
---|
3021 | }
|
---|
3022 | else
|
---|
3023 | {
|
---|
3024 | struct s_reader *rdr;
|
---|
3025 | int r = 0;
|
---|
3026 | for (rdr = first_active_reader; rdr; rdr = rdr->next) {
|
---|
3027 | if (!rdr->fd)
|
---|
3028 | continue;
|
---|
3029 | if (!(rdr->grp & cl->grp))
|
---|
3030 | continue;
|
---|
3031 | reader_reshare = rdr->cc_reshare;
|
---|
3032 |
|
---|
3033 | reshare = (reader_reshare < usr_reshare) ? reader_reshare : usr_reshare;
|
---|
3034 | if (reshare < 0)
|
---|
3035 | continue;
|
---|
3036 |
|
---|
3037 | //Generate a uniq reader id:
|
---|
3038 | if (!rdr->cc_id) {
|
---|
3039 | rdr->cc_id = ++r;
|
---|
3040 | struct s_reader *rdr2;
|
---|
3041 | for (rdr2 = first_active_reader; rdr2; rdr2 = rdr2->next) {
|
---|
3042 | if (rdr2 != rdr && rdr2->cc_id == rdr->cc_id) {
|
---|
3043 | rdr2 = first_active_reader;
|
---|
3044 | rdr->cc_id=++r;
|
---|
3045 | }
|
---|
3046 | }
|
---|
3047 | }
|
---|
3048 |
|
---|
3049 | int au_allowed = !rdr->audisabled && isau;
|
---|
3050 |
|
---|
3051 | flt = 0;
|
---|
3052 |
|
---|
3053 | //Reader-Services:
|
---|
3054 | if ((cfg.cc_reshare_services==1||cfg.cc_reshare_services==2) && cfg.sidtab && rdr->sidtabok) {
|
---|
3055 | struct s_sidtab *ptr;
|
---|
3056 | for (j=0,ptr=cfg.sidtab; ptr; ptr=ptr->next,j++) {
|
---|
3057 | if (rdr->sidtabok&((SIDTABBITS)1<<j)) {
|
---|
3058 | int k;
|
---|
3059 | for (k=0;k<ptr->num_caid;k++) {
|
---|
3060 | struct cc_card *card = create_card2(rdr, (j<<8)|k, ptr->caid[k], hop, reshare);
|
---|
3061 | int l;
|
---|
3062 | for (l=0;l<ptr->num_provid;l++) {
|
---|
3063 | struct cc_provider *prov = cs_malloc(&prov, sizeof(struct cc_provider), QUITERROR);
|
---|
3064 | memset(prov, 0, sizeof(struct cc_provider));
|
---|
3065 | prov->prov = ptr->provid[l];
|
---|
3066 | ll_append(card->providers, prov);
|
---|
3067 | }
|
---|
3068 |
|
---|
3069 | //CCcam 2.2.x proto can transfer good and bad sids:
|
---|
3070 | add_good_bad_sids(ptr, cl, card);
|
---|
3071 |
|
---|
3072 | add_card_to_serverlist(rdr, cl, server_cards, card, reshare, au_allowed);
|
---|
3073 | }
|
---|
3074 | flt=1;
|
---|
3075 | }
|
---|
3076 | }
|
---|
3077 | }
|
---|
3078 |
|
---|
3079 | //Filts by Hardware readers:
|
---|
3080 | if ((rdr->typ != R_CCCAM) && rdr->ftab.filts && !flt) {
|
---|
3081 | for (j = 0; j < CS_MAXFILTERS; j++) {
|
---|
3082 | if (rdr->ftab.filts[j].caid &&
|
---|
3083 | chk_ctab(rdr->ftab.filts[j].caid, &cl->ctab)) {
|
---|
3084 | int ignore = 0;
|
---|
3085 | ushort caid = rdr->ftab.filts[j].caid;
|
---|
3086 | struct cc_card *card = create_card2(rdr, j, caid, hop, reshare);
|
---|
3087 | //Setting UA: (Unique Address):
|
---|
3088 | if (au_allowed)
|
---|
3089 | cc_UA_oscam2cccam(rdr->hexserial, card->hexserial, caid);
|
---|
3090 | //cs_log("Ident CCcam card report caid: %04X readr %s subid: %06X", rdr->ftab.filts[j].caid, rdr->label, rdr->cc_id);
|
---|
3091 | for (k = 0; k < rdr->ftab.filts[j].nprids; k++) {
|
---|
3092 | struct cc_provider *prov = cs_malloc(&prov, sizeof(struct cc_provider), QUITERROR);
|
---|
3093 | memset(prov, 0, sizeof(struct cc_provider));
|
---|
3094 | prov->prov = rdr->ftab.filts[j].prids[k];
|
---|
3095 |
|
---|
3096 | if (!chk_srvid_by_caid_prov(cl, caid, prov->prov)) {
|
---|
3097 | ignore = 1;
|
---|
3098 | }
|
---|
3099 | //cs_log("Ident CCcam card report provider: %02X%02X%02X", buf[21 + (k*7)]<<16, buf[22 + (k*7)], buf[23 + (k*7)]);
|
---|
3100 | if (au_allowed) {
|
---|
3101 | int l;
|
---|
3102 | for (l = 0; l < rdr->nprov; l++) {
|
---|
3103 | ulong rprid = get_reader_prid(rdr, l);
|
---|
3104 | if (rprid == prov->prov)
|
---|
3105 | cc_SA_oscam2cccam(&rdr->sa[l][0], prov->sa);
|
---|
3106 | }
|
---|
3107 | }
|
---|
3108 |
|
---|
3109 | ll_append(card->providers, prov);
|
---|
3110 | }
|
---|
3111 |
|
---|
3112 | //check remote node id, do not send if this card is from there:
|
---|
3113 | if (!ignore) {
|
---|
3114 | LL_ITER *it = ll_iter_create(card->remote_nodes);
|
---|
3115 | uint8 * node;
|
---|
3116 | while (!ignore && (node=ll_iter_next(it))) {
|
---|
3117 | if (!memcmp(node, cc->peer_node_id, 8))
|
---|
3118 | ignore = TRUE;
|
---|
3119 | }
|
---|
3120 | ll_iter_release(it);
|
---|
3121 | }
|
---|
3122 |
|
---|
3123 | if (!ignore) {
|
---|
3124 | //CCcam 2.2.x proto can transfer good and bad sids:
|
---|
3125 | add_good_bad_sids_for_card(cl, card);
|
---|
3126 |
|
---|
3127 | add_card_to_serverlist(rdr, cl, server_cards, card, reshare, au_allowed);
|
---|
3128 | }
|
---|
3129 | else cc_free_card(card);
|
---|
3130 | flt = 1;
|
---|
3131 | }
|
---|
3132 | }
|
---|
3133 | }
|
---|
3134 |
|
---|
3135 | if ((rdr->typ != R_CCCAM) && !rdr->caid && !flt) {
|
---|
3136 | for (j = 0; j < CS_MAXCAIDTAB; j++) {
|
---|
3137 | //cs_log("CAID map CCcam card report caid: %04X cmap: %04X", rdr->ctab.caid[j], rdr->ctab.cmap[j]);
|
---|
3138 | ushort lcaid = rdr->ctab.caid[j];
|
---|
3139 |
|
---|
3140 | if (!lcaid || (lcaid == 0xFFFF))
|
---|
3141 | lcaid = rdr->ctab.cmap[j];
|
---|
3142 |
|
---|
3143 | if (lcaid && (lcaid != 0xFFFF) && chk_ctab(lcaid, &cl->ctab)) {
|
---|
3144 | struct cc_card *card = create_card2(rdr, j, lcaid, hop, reshare);
|
---|
3145 | if (au_allowed)
|
---|
3146 | cc_UA_oscam2cccam(rdr->hexserial, card->hexserial, lcaid);
|
---|
3147 |
|
---|
3148 | //CCcam 2.2.x proto can transfer good and bad sids:
|
---|
3149 | add_good_bad_sids_for_card(cl, card);
|
---|
3150 |
|
---|
3151 | add_card_to_serverlist(rdr, cl, server_cards, card, reshare, au_allowed);
|
---|
3152 | flt = 1;
|
---|
3153 | }
|
---|
3154 | }
|
---|
3155 | }
|
---|
3156 |
|
---|
3157 | if ((rdr->typ != R_CCCAM) && rdr->caid && !flt && chk_ctab(rdr->caid, &cl->ctab)) {
|
---|
3158 | //cs_log("tcp_connected: %d card_status: %d ", rdr->tcp_connected, rdr->card_status);
|
---|
3159 | ushort caid = rdr->caid;
|
---|
3160 | struct cc_card *card = create_card2(rdr, 0, caid, hop, reshare);
|
---|
3161 | if (au_allowed)
|
---|
3162 | cc_UA_oscam2cccam(rdr->hexserial, card->hexserial, caid);
|
---|
3163 | for (j = 0; j < rdr->nprov; j++) {
|
---|
3164 | ulong prid = get_reader_prid(rdr, j);
|
---|
3165 | struct cc_provider *prov = cs_malloc(&prov, sizeof(struct cc_provider), QUITERROR);
|
---|
3166 | memset(prov, 0, sizeof(struct cc_provider));
|
---|
3167 | prov->prov = prid;
|
---|
3168 | //cs_log("Ident CCcam card report provider: %02X%02X%02X", buf[21 + (k*7)]<<16, buf[22 + (k*7)], buf[23 + (k*7)]);
|
---|
3169 | if (au_allowed) {
|
---|
3170 | //Setting SA (Shared Addresses):
|
---|
3171 | cc_SA_oscam2cccam(rdr->sa[j], prov->sa);
|
---|
3172 | }
|
---|
3173 | ll_append(card->providers, prov);
|
---|
3174 | //cs_log("Main CCcam card report provider: %02X%02X%02X%02X", buf[21+(j*7)], buf[22+(j*7)], buf[23+(j*7)], buf[24+(j*7)]);
|
---|
3175 | }
|
---|
3176 | if (rdr->tcp_connected || rdr->card_status == CARD_INSERTED) {
|
---|
3177 |
|
---|
3178 | //CCcam 2.2.x proto can transfer good and bad sids:
|
---|
3179 | add_good_bad_sids_for_card(cl, card);
|
---|
3180 |
|
---|
3181 | add_card_to_serverlist(rdr, cl, server_cards, card, reshare, au_allowed);
|
---|
3182 | }
|
---|
3183 | else
|
---|
3184 | cc_free_card(card);
|
---|
3185 | }
|
---|
3186 |
|
---|
3187 | if (rdr->typ == R_CCCAM && cfg.cc_reshare_services<2 && rdr->card_status != CARD_FAILURE) {
|
---|
3188 |
|
---|
3189 | cs_debug_mask(D_CLIENT, "%s asking reader %s for cards...",
|
---|
3190 | getprefix(), rdr->label);
|
---|
3191 |
|
---|
3192 | struct cc_card *card;
|
---|
3193 | struct s_client *rc = rdr->client;
|
---|
3194 | struct cc_data *rcc = rc?rc->cc:NULL;
|
---|
3195 |
|
---|
3196 | int count = 0;
|
---|
3197 | if (rcc && rcc->cards && rcc->mode == CCCAM_MODE_NORMAL) {
|
---|
3198 | if (!pthread_mutex_trylock(&rcc->cards_busy)) {
|
---|
3199 | LL_ITER *it = ll_iter_create(rcc->cards);
|
---|
3200 | while ((card = ll_iter_next(it))) {
|
---|
3201 | if (card->hop <= maxhops && chk_ctab(card->caid, &cl->ctab)
|
---|
3202 | && chk_ctab(card->caid, &rdr->ctab)) {
|
---|
3203 |
|
---|
3204 | if ((cfg.cc_ignore_reshare || usr_ignorereshare || card->maxdown > 0)) {
|
---|
3205 | int ignore = 0;
|
---|
3206 |
|
---|
3207 | LL_ITER *it2 = ll_iter_create(card->providers);
|
---|
3208 | struct cc_provider *prov;
|
---|
3209 | while ((prov = ll_iter_next(it2))) {
|
---|
3210 | ulong prid = prov->prov;
|
---|
3211 | if (!chk_srvid_by_caid_prov(cl, card->caid,
|
---|
3212 | prid) || !chk_srvid_by_caid_prov(
|
---|
3213 | rdr->client, card->caid, prid)) {
|
---|
3214 | ignore = 1;
|
---|
3215 | break;
|
---|
3216 | }
|
---|
3217 | }
|
---|
3218 | ll_iter_release(it2);
|
---|
3219 |
|
---|
3220 | if (!ignore) { //Filtered by service
|
---|
3221 | int new_reshare =
|
---|
3222 | ( cfg.cc_ignore_reshare || usr_ignorereshare ) ? reshare
|
---|
3223 | : (card->maxdown - 1);
|
---|
3224 | if (new_reshare > reshare)
|
---|
3225 | new_reshare = reshare;
|
---|
3226 | add_card_to_serverlist(rdr, cl, server_cards, card,
|
---|
3227 | new_reshare, au_allowed);
|
---|
3228 | count++;
|
---|
3229 | }
|
---|
3230 | }
|
---|
3231 | }
|
---|
3232 | }
|
---|
3233 | ll_iter_release(it);
|
---|
3234 | pthread_mutex_unlock(&rcc->cards_busy);
|
---|
3235 | }
|
---|
3236 | }
|
---|
3237 | cs_debug_mask(D_CLIENT, "%s got %d cards from %s", getprefix(),
|
---|
3238 | count, rdr->label);
|
---|
3239 | }
|
---|
3240 | }
|
---|
3241 | }
|
---|
3242 |
|
---|
3243 | int ok = TRUE;
|
---|
3244 | //report reshare cards:
|
---|
3245 | //cs_debug_mask(D_TRACE, "%s reporting %d cards", getprefix(), ll_count(server_cards));
|
---|
3246 | LL_ITER *it = ll_iter_create(server_cards);
|
---|
3247 | struct cc_card *card;
|
---|
3248 | while (ok && (card = ll_iter_next(it))) {
|
---|
3249 | //cs_debug_mask(D_TRACE, "%s card %d caid %04X hop %d", getprefix(), card->id, card->caid, card->hop);
|
---|
3250 |
|
---|
3251 | ok = report_card(cl, card, new_reported_carddatas, isau) >= 0;
|
---|
3252 | ll_iter_remove(it);
|
---|
3253 | }
|
---|
3254 | ll_iter_release(it);
|
---|
3255 | cc_free_cardlist(server_cards, TRUE);
|
---|
3256 |
|
---|
3257 | //remove unsed, remaining cards:
|
---|
3258 | cc->card_removed_count += cc_free_reported_carddata(cl, cc->reported_carddatas, new_reported_carddatas, ok);
|
---|
3259 | cc->reported_carddatas = new_reported_carddatas;
|
---|
3260 |
|
---|
3261 | cs_debug_mask(D_CLIENT, "%s reported/updated +%d/-%d/dup %d of %d cards to client (ext=%d)", getprefix(),
|
---|
3262 | cc->card_added_count, cc->card_removed_count, cc->card_dup_count, ll_count(cc->reported_carddatas), cc->cccam220);
|
---|
3263 | return ok;
|
---|
3264 | }
|
---|
3265 |
|
---|
3266 | void cc_init_cc(struct cc_data *cc) {
|
---|
3267 | pthread_mutex_init(&cc->lock, NULL); //No recursive lock
|
---|
3268 | pthread_mutex_init(&cc->ecm_busy, NULL); //No recusive lock
|
---|
3269 | pthread_mutex_init(&cc->cards_busy, NULL); //No (more) recursive lock
|
---|
3270 | }
|
---|
3271 |
|
---|
3272 | /**
|
---|
3273 | * Starting readers to get cards:
|
---|
3274 | **/
|
---|
3275 | int cc_srv_wakeup_readers(struct s_client *cl) {
|
---|
3276 | int wakeup = 0;
|
---|
3277 | struct s_reader *rdr;
|
---|
3278 | for (rdr = first_active_reader; rdr; rdr = rdr->next) {
|
---|
3279 | if (rdr->typ != R_CCCAM)
|
---|
3280 | continue;
|
---|
3281 | if (!rdr->fd || rdr->tcp_connected == 2)
|
---|
3282 | continue;
|
---|
3283 | if (!(rdr->grp & cl->grp))
|
---|
3284 | continue;
|
---|
3285 | if (rdr->cc_keepalive) //if reader has keepalive but is NOT connected, reader can't connect. so don't ask him
|
---|
3286 | continue;
|
---|
3287 |
|
---|
3288 | //This wakeups the reader:
|
---|
3289 | uchar dummy;
|
---|
3290 | write_to_pipe(rdr->fd, PIP_ID_CIN, &dummy, sizeof(dummy));
|
---|
3291 | wakeup++;
|
---|
3292 | }
|
---|
3293 | return wakeup;
|
---|
3294 | }
|
---|
3295 |
|
---|
3296 | int cc_cards_modified() {
|
---|
3297 | int modified = 0;
|
---|
3298 | struct s_reader *rdr;
|
---|
3299 | for (rdr = first_active_reader; rdr; rdr = rdr->next) {
|
---|
3300 | if (rdr->typ == R_CCCAM && rdr->fd) {
|
---|
3301 | struct s_client *clr = rdr->client;
|
---|
3302 | if (clr && clr->cc) {
|
---|
3303 | struct cc_data *ccr = clr->cc;
|
---|
3304 | modified += ccr->cards_modified;
|
---|
3305 | }
|
---|
3306 | }
|
---|
3307 | }
|
---|
3308 | return modified;
|
---|
3309 | }
|
---|
3310 |
|
---|
3311 | int check_cccam_compat(struct cc_data *cc) {
|
---|
3312 | int res = 0;
|
---|
3313 | if (strcmp(cfg.cc_version, "2.2.0") == 0 || strcmp(cfg.cc_version, "2.2.1") == 0) {
|
---|
3314 |
|
---|
3315 | if (strcmp(cc->remote_version, "2.2.0") == 0 || strcmp(cc->remote_version, "2.2.1") == 0) {
|
---|
3316 | res = 1;
|
---|
3317 | }
|
---|
3318 | }
|
---|
3319 | return res;
|
---|
3320 | }
|
---|
3321 |
|
---|
3322 | int cc_srv_connect(struct s_client *cl) {
|
---|
3323 | int i, wait_for_keepalive;
|
---|
3324 | ulong cmi;
|
---|
3325 | uint8 buf[CC_MAXMSGSIZE];
|
---|
3326 | uint8 data[16];
|
---|
3327 | char usr[21], pwd[65];
|
---|
3328 | struct s_auth *account;
|
---|
3329 | struct cc_data *cc = cl->cc;
|
---|
3330 | uchar mbuf[1024];
|
---|
3331 |
|
---|
3332 | memset(usr, 0, sizeof(usr));
|
---|
3333 | memset(pwd, 0, sizeof(pwd));
|
---|
3334 |
|
---|
3335 | //SS: Use last cc data for faster reconnects:
|
---|
3336 | if (!cc) {
|
---|
3337 | // init internals data struct
|
---|
3338 | cc = cs_malloc(&cc, sizeof(struct cc_data), QUITERROR);
|
---|
3339 | cl->cc = cc;
|
---|
3340 | memset(cl->cc, 0, sizeof(struct cc_data));
|
---|
3341 | cc->extended_ecm_idx = ll_create();
|
---|
3342 |
|
---|
3343 | cc_init_cc(cc);
|
---|
3344 | }
|
---|
3345 | cc->server_ecm_pending = 0;
|
---|
3346 | cc->extended_mode = 0;
|
---|
3347 |
|
---|
3348 | //Create checksum for "O" cccam:
|
---|
3349 | for (i = 0; i < 12; i++) {
|
---|
3350 | data[i] = fast_rnd();
|
---|
3351 | }
|
---|
3352 | for (i = 0; i < 4; i++) {
|
---|
3353 | data[12 + i] = (data[i] + data[4 + i] + data[8 + i]) & 0xff;
|
---|
3354 | }
|
---|
3355 |
|
---|
3356 | send(cl->udp_fd, data, 16, 0);
|
---|
3357 |
|
---|
3358 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
3359 |
|
---|
3360 | SHA_CTX ctx;
|
---|
3361 | SHA1_Init(&ctx);
|
---|
3362 | SHA1_Update(&ctx, data, 16);
|
---|
3363 | SHA1_Final(buf, &ctx);
|
---|
3364 |
|
---|
3365 | cc_init_crypt(&cc->block[ENCRYPT], buf, 20);
|
---|
3366 | cc_crypt(&cc->block[ENCRYPT], data, 16, DECRYPT);
|
---|
3367 | cc_init_crypt(&cc->block[DECRYPT], data, 16);
|
---|
3368 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
3369 |
|
---|
3370 | if ((i = cc_recv_to(cl, buf, 20)) == 20) {
|
---|
3371 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: recv:");
|
---|
3372 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
3373 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: hash:");
|
---|
3374 | } else
|
---|
3375 | return -1;
|
---|
3376 |
|
---|
3377 | // receive username
|
---|
3378 | memset(buf, 0, sizeof(buf));
|
---|
3379 | if ((i = cc_recv_to(cl, buf, 20)) == 20) {
|
---|
3380 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
3381 |
|
---|
3382 | strncpy(usr, (char *) buf, sizeof(usr));
|
---|
3383 |
|
---|
3384 | //test for nonprintable characters:
|
---|
3385 | for (i = 0; i < 20; i++) {
|
---|
3386 | if (usr[i] > 0 && usr[i] < 0x20) { //found nonprintable char
|
---|
3387 | cs_log("illegal username received");
|
---|
3388 | return -3;
|
---|
3389 | }
|
---|
3390 | }
|
---|
3391 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: username '%s':", usr);
|
---|
3392 | } else
|
---|
3393 | return -2;
|
---|
3394 |
|
---|
3395 | cl->crypted = 1;
|
---|
3396 |
|
---|
3397 | //CCCam only supports len=20 usr/pass. So we could have more than one user that matches the first 20 chars.
|
---|
3398 |
|
---|
3399 | //receive password-CCCam encrypted Hash:
|
---|
3400 | if (cc_recv_to(cl, buf, 6) != 6)
|
---|
3401 | return -2;
|
---|
3402 |
|
---|
3403 | account = cfg.account;
|
---|
3404 | struct cc_crypt_block *save_block = cs_malloc(&save_block, sizeof(struct cc_crypt_block), QUITERROR);
|
---|
3405 | memcpy(save_block, cc->block, sizeof(struct cc_crypt_block));
|
---|
3406 | int found = 0;
|
---|
3407 | while (1) {
|
---|
3408 | while (account) {
|
---|
3409 | if (strncmp(usr, account->usr, 20) == 0) {
|
---|
3410 | memset(pwd, 0, sizeof(pwd));
|
---|
3411 | strncpy(pwd, account->pwd, sizeof(pwd));
|
---|
3412 | found=1;
|
---|
3413 | break;
|
---|
3414 | }
|
---|
3415 | account = account->next;
|
---|
3416 | }
|
---|
3417 |
|
---|
3418 | if (!account)
|
---|
3419 | break;
|
---|
3420 |
|
---|
3421 | // receive passwd / 'CCcam'
|
---|
3422 | memcpy(cc->block, save_block, sizeof(struct cc_crypt_block));
|
---|
3423 | cc_crypt(&cc->block[DECRYPT], (uint8 *) pwd, strlen(pwd), ENCRYPT);
|
---|
3424 | cc_crypt(&cc->block[DECRYPT], buf, 6, DECRYPT);
|
---|
3425 | //cs_ddump_mask(D_CLIENT, buf, 6, "cccam: pwd check '%s':", buf); //illegal buf-bytes could kill the logger!
|
---|
3426 | if (memcmp(buf, "CCcam\0", 6) == 0) //Password Hash OK!
|
---|
3427 | break; //account is set
|
---|
3428 |
|
---|
3429 | account = account->next;
|
---|
3430 | }
|
---|
3431 | free(save_block);
|
---|
3432 |
|
---|
3433 | if (cs_auth_client(cl, account, NULL)) { //cs_auth_client returns 0 if account is valid/active/accessible
|
---|
3434 | if (!found)
|
---|
3435 | cs_log("account '%s' not found!", usr);
|
---|
3436 | else
|
---|
3437 | cs_log("password for '%s' invalid!", usr);
|
---|
3438 | return -2;
|
---|
3439 | }
|
---|
3440 | if (cl->dup) {
|
---|
3441 | cs_log("account '%s' duplicate login, disconnect!", usr);
|
---|
3442 | return -3;
|
---|
3443 | }
|
---|
3444 | if (account->cccmaxhops<0) {
|
---|
3445 | cs_log("account '%s' has cccmaxhops<0, cccam can't handle this, disconnect!", usr);
|
---|
3446 | return -3;
|
---|
3447 | }
|
---|
3448 |
|
---|
3449 |
|
---|
3450 | if (!cc->prefix)
|
---|
3451 | cc->prefix = cs_malloc(&cc->prefix, strlen(cl->account->usr)+20, QUITERROR);
|
---|
3452 | sprintf(cc->prefix, "cccam(s) %s: ", cl->account->usr);
|
---|
3453 |
|
---|
3454 |
|
---|
3455 | //Starting readers to get cards:
|
---|
3456 | int wakeup = cc_srv_wakeup_readers(cl);
|
---|
3457 |
|
---|
3458 | // send passwd ack
|
---|
3459 | memset(buf, 0, 20);
|
---|
3460 | memcpy(buf, "CCcam\0", 6);
|
---|
3461 | cs_ddump_mask(D_CLIENT, buf, 20, "cccam: send ack:");
|
---|
3462 | cc_crypt(&cc->block[ENCRYPT], buf, 20, ENCRYPT);
|
---|
3463 | send(cl->pfd, buf, 20, 0);
|
---|
3464 |
|
---|
3465 | // recv cli data
|
---|
3466 | memset(buf, 0, sizeof(buf));
|
---|
3467 | i = cc_msg_recv(cl, buf, sizeof(buf));
|
---|
3468 | if (i < 0)
|
---|
3469 | return -1;
|
---|
3470 | cs_ddump_mask(D_CLIENT, buf, i, "cccam: cli data:");
|
---|
3471 | memcpy(cc->peer_node_id, buf + 24, 8);
|
---|
3472 |
|
---|
3473 | strncpy(cc->remote_version, (char*)buf+33, sizeof(cc->remote_version)-1);
|
---|
3474 | strncpy(cc->remote_build, (char*)buf+65, sizeof(cc->remote_build)-1);
|
---|
3475 |
|
---|
3476 | cs_debug_mask(D_CLIENT, "%s client '%s' (%s) running v%s (%s)", getprefix(), buf + 4,
|
---|
3477 | cs_hexdump(0, cc->peer_node_id, 8), cc->remote_version, cc->remote_build);
|
---|
3478 |
|
---|
3479 | // send cli data ack
|
---|
3480 | cc_cmd_send(cl, NULL, 0, MSG_CLI_DATA);
|
---|
3481 |
|
---|
3482 | if (cc_send_srv_data(cl) < 0)
|
---|
3483 | return -1;
|
---|
3484 |
|
---|
3485 | cc->cccam220 = check_cccam_compat(cc);
|
---|
3486 |
|
---|
3487 | //Wait for Partner detection (NOK1 with data) before reporting cards
|
---|
3488 | //When Partner is detected, cccam220=1 is set. then we can report extended card data
|
---|
3489 | i = process_input(mbuf, sizeof(mbuf), 1);
|
---|
3490 | if (i<=0 && i != -9)
|
---|
3491 | return 0; //disconnected
|
---|
3492 | if (cc->cccam220)
|
---|
3493 | cs_debug_mask(D_CLIENT, "%s extended sid mode activated", getprefix());
|
---|
3494 | else
|
---|
3495 | cs_debug_mask(D_CLIENT, "%s 2.1.x compatibility mode", getprefix());
|
---|
3496 |
|
---|
3497 | // report cards
|
---|
3498 | ulong hexserial_crc = get_reader_hexserial_crc(cl);
|
---|
3499 |
|
---|
3500 | if (wakeup > 0) //give readers time to get cards:
|
---|
3501 | cs_sleepms(500);
|
---|
3502 |
|
---|
3503 | if (!cc_srv_report_cards(cl))
|
---|
3504 | return -1;
|
---|
3505 | cs_ftime(&cc->ecm_time);
|
---|
3506 |
|
---|
3507 | cmi = 0;
|
---|
3508 | wait_for_keepalive = 100;
|
---|
3509 | //some clients, e.g. mgcamd, does not support keepalive. So if not answered, keep connection
|
---|
3510 | // check for client timeout, if timeout occurs try to send keepalive
|
---|
3511 | while (cl->pfd)
|
---|
3512 | {
|
---|
3513 | i = process_input(mbuf, sizeof(mbuf), 10);
|
---|
3514 | if (i == -9) {
|
---|
3515 | cmi += 10;
|
---|
3516 | if (cmi >= cfg.cmaxidle) {
|
---|
3517 | if (cfg.cc_keep_connected || cl->account->ncd_keepalive) {
|
---|
3518 | if (wait_for_keepalive<3 || wait_for_keepalive == 100) {
|
---|
3519 | if (cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE) < 0)
|
---|
3520 | break;
|
---|
3521 | cs_debug_mask(D_CLIENT, "cccam: keepalive");
|
---|
3522 | cc->answer_on_keepalive = time(NULL);
|
---|
3523 | wait_for_keepalive++;
|
---|
3524 | }
|
---|
3525 | else if (wait_for_keepalive<100) break;
|
---|
3526 | } else {
|
---|
3527 | cs_debug_mask(D_CLIENT, "%s keepalive after maxidle is reached",
|
---|
3528 | getprefix());
|
---|
3529 | break; //Disconnect client
|
---|
3530 | }
|
---|
3531 | }
|
---|
3532 |
|
---|
3533 | } else if (i <= 0)
|
---|
3534 | break; //Disconnected by client
|
---|
3535 | else { //data is parsed!
|
---|
3536 | cmi = 0;
|
---|
3537 | if (i == MSG_KEEPALIVE)
|
---|
3538 | wait_for_keepalive = 0;
|
---|
3539 | }
|
---|
3540 |
|
---|
3541 | if (cc->mode != CCCAM_MODE_NORMAL || cl->dup)
|
---|
3542 | break; //mode wrong or duplicate user -->disconect
|
---|
3543 |
|
---|
3544 | if (!cc->server_ecm_pending) {
|
---|
3545 | struct timeb timeout;
|
---|
3546 | struct timeb cur_time;
|
---|
3547 | cs_ftime(&cur_time);
|
---|
3548 | timeout = cc->ecm_time;
|
---|
3549 | timeout.time += cfg.cc_update_interval;
|
---|
3550 |
|
---|
3551 | int needs_card_updates = (cfg.cc_update_interval >= 0)
|
---|
3552 | && comp_timeb(&cur_time, &timeout) > 0;
|
---|
3553 |
|
---|
3554 | if (needs_card_updates || !cc->cards_modified) {
|
---|
3555 | cc->ecm_time = cur_time;
|
---|
3556 | ulong new_hexserial_crc = get_reader_hexserial_crc(cl);
|
---|
3557 | int cards_modified = cc_cards_modified();
|
---|
3558 | if (new_hexserial_crc != hexserial_crc || cards_modified
|
---|
3559 | != cc->cards_modified) {
|
---|
3560 | cs_debug_mask(D_CLIENT, "%s update share list",
|
---|
3561 | getprefix());
|
---|
3562 |
|
---|
3563 | hexserial_crc = new_hexserial_crc;
|
---|
3564 | cc->cards_modified = cards_modified;
|
---|
3565 |
|
---|
3566 | if (!cc_srv_report_cards(cl))
|
---|
3567 | return -1;
|
---|
3568 | }
|
---|
3569 | }
|
---|
3570 | }
|
---|
3571 | }
|
---|
3572 |
|
---|
3573 | return 0;
|
---|
3574 | }
|
---|
3575 |
|
---|
3576 | void * cc_srv_init(struct s_client *cl) {
|
---|
3577 | cl->thread = pthread_self();
|
---|
3578 | pthread_setspecific(getclient, cl);
|
---|
3579 |
|
---|
3580 | cl->pfd = cl->udp_fd;
|
---|
3581 | int ret;
|
---|
3582 | if ((ret=cc_srv_connect(cl)) < 0) {
|
---|
3583 | if (errno != 0)
|
---|
3584 | cs_debug_mask(D_CLIENT, "cccam: failed errno: %d (%s)", errno, strerror(errno));
|
---|
3585 | else
|
---|
3586 | cs_debug_mask(D_CLIENT, "cccam: failed ret: %d", ret);
|
---|
3587 | if (ret == -2)
|
---|
3588 | cs_add_violation((uint)cl->ip);
|
---|
3589 | }
|
---|
3590 | cc_cleanup(cl);
|
---|
3591 | cs_disconnect_client(cl);
|
---|
3592 | return NULL; //suppress compiler warning
|
---|
3593 | }
|
---|
3594 |
|
---|
3595 | int cc_cli_connect(struct s_client *cl) {
|
---|
3596 | struct s_reader *rdr = cl->reader;
|
---|
3597 | struct cc_data *cc = cl->cc;
|
---|
3598 | rdr->card_status = CARD_FAILURE;
|
---|
3599 |
|
---|
3600 | if (cc && cc->mode != CCCAM_MODE_NORMAL)
|
---|
3601 | return -99;
|
---|
3602 |
|
---|
3603 | if (!cl->udp_fd) {
|
---|
3604 | cc_cli_init_int(cl);
|
---|
3605 | return -1; // cc_cli_init_int calls cc_cli_connect, so exit here!
|
---|
3606 | }
|
---|
3607 |
|
---|
3608 | if (is_connect_blocked(rdr)) {
|
---|
3609 | cs_log("%s connection blocked, retrying later", rdr->label);
|
---|
3610 | return -1;
|
---|
3611 | }
|
---|
3612 |
|
---|
3613 | int handle, n;
|
---|
3614 | uint8 data[20];
|
---|
3615 | uint8 hash[SHA_DIGEST_LENGTH];
|
---|
3616 | uint8 buf[CC_MAXMSGSIZE];
|
---|
3617 | char pwd[64];
|
---|
3618 |
|
---|
3619 | // check cred config
|
---|
3620 | if (rdr->device[0] == 0 || rdr->r_pwd[0] == 0 || rdr->r_usr[0] == 0
|
---|
3621 | || rdr->r_port == 0) {
|
---|
3622 | cs_log("%s configuration error!", rdr->label);
|
---|
3623 | return -5;
|
---|
3624 | }
|
---|
3625 |
|
---|
3626 | // connect
|
---|
3627 | handle = network_tcp_connection_open();
|
---|
3628 | if (handle <= 0) {
|
---|
3629 | cs_log("%s network connect error!", rdr->label);
|
---|
3630 | return -1;
|
---|
3631 | }
|
---|
3632 | if (errno == EISCONN) {
|
---|
3633 | cc_cli_close(cl, FALSE);
|
---|
3634 |
|
---|
3635 | block_connect(rdr);
|
---|
3636 | return -1;
|
---|
3637 | }
|
---|
3638 |
|
---|
3639 | // get init seed
|
---|
3640 | if ((n = cc_recv_to(cl, data, 16)) != 16) {
|
---|
3641 | int err = errno;
|
---|
3642 | if (n <= 0)
|
---|
3643 | cs_log("%s server blocked connection!", rdr->label);
|
---|
3644 | else
|
---|
3645 | cs_log("%s server does not return 16 bytes (n=%d, errno=%d)",
|
---|
3646 | rdr->label, n, err);
|
---|
3647 | block_connect(rdr);
|
---|
3648 | return -2;
|
---|
3649 | }
|
---|
3650 |
|
---|
3651 | if (!cc) {
|
---|
3652 | // init internals data struct
|
---|
3653 | cc = cs_malloc(&cc, sizeof(struct cc_data), QUITERROR);
|
---|
3654 | memset(cc, 0, sizeof(struct cc_data));
|
---|
3655 | cc->cards = ll_create();
|
---|
3656 | cl->cc = cc;
|
---|
3657 | cc->pending_emms = ll_create();
|
---|
3658 | cc->extended_ecm_idx = ll_create();
|
---|
3659 | cc_init_cc(cc);
|
---|
3660 | } else {
|
---|
3661 | if (cc->cards) {
|
---|
3662 | LL_ITER *it = ll_iter_create(cc->cards);
|
---|
3663 | struct cc_card *card;
|
---|
3664 | while ((card = ll_iter_next(it))) {
|
---|
3665 | cc_free_card(card);
|
---|
3666 | ll_iter_remove(it);
|
---|
3667 | }
|
---|
3668 | ll_iter_release(it);
|
---|
3669 | }
|
---|
3670 | if (cc->extended_ecm_idx)
|
---|
3671 | free_extended_ecm_idx(cc);
|
---|
3672 |
|
---|
3673 | pthread_mutex_trylock(&cc->ecm_busy);
|
---|
3674 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
3675 | }
|
---|
3676 | if (!cc->prefix)
|
---|
3677 | cc->prefix = cs_malloc(&cc->prefix, strlen(cl->reader->label)+20, QUITERROR);
|
---|
3678 | sprintf(cc->prefix, "cccam(r) %s: ", cl->reader->label);
|
---|
3679 |
|
---|
3680 | cc->ecm_counter = 0;
|
---|
3681 | cc->max_ecms = 0;
|
---|
3682 | cc->cmd05_mode = MODE_UNKNOWN;
|
---|
3683 | cc->cmd05_offset = 0;
|
---|
3684 | cc->cmd05_active = 0;
|
---|
3685 | cc->cmd05_data_len = 0;
|
---|
3686 | cc->answer_on_keepalive = time(NULL);
|
---|
3687 | cc->extended_mode = 0;
|
---|
3688 | cc->last_emm_card = NULL;
|
---|
3689 |
|
---|
3690 | memset(&cc->cmd05_data, 0, sizeof(cc->cmd05_data));
|
---|
3691 | memset(&cc->receive_buffer, 0, sizeof(cc->receive_buffer));
|
---|
3692 | cc->cmd0c_mode = MODE_CMD_0x0C_NONE;
|
---|
3693 |
|
---|
3694 | cs_ddump_mask(D_CLIENT, data, 16, "cccam: server init seed:");
|
---|
3695 |
|
---|
3696 | uint16 sum = 0x1234;
|
---|
3697 | uint16 recv_sum = (data[14] << 8) | data[15];
|
---|
3698 | int i;
|
---|
3699 | for (i = 0; i < 14; i++) {
|
---|
3700 | sum += data[i];
|
---|
3701 | }
|
---|
3702 | //Create special data to detect oscam-cccam:
|
---|
3703 | cc->is_oscam_cccam = sum == recv_sum;
|
---|
3704 |
|
---|
3705 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
3706 |
|
---|
3707 | SHA_CTX ctx;
|
---|
3708 | SHA1_Init(&ctx);
|
---|
3709 | SHA1_Update(&ctx, data, 16);
|
---|
3710 | SHA1_Final(hash, &ctx);
|
---|
3711 |
|
---|
3712 | cs_ddump_mask(D_CLIENT, hash, sizeof(hash), "cccam: sha1 hash:");
|
---|
3713 |
|
---|
3714 | //initialisate crypto states
|
---|
3715 | cc_init_crypt(&cc->block[DECRYPT], hash, 20);
|
---|
3716 | cc_crypt(&cc->block[DECRYPT], data, 16, DECRYPT);
|
---|
3717 | cc_init_crypt(&cc->block[ENCRYPT], data, 16);
|
---|
3718 | cc_crypt(&cc->block[ENCRYPT], hash, 20, DECRYPT);
|
---|
3719 |
|
---|
3720 | cc_cmd_send(cl, hash, 20, MSG_NO_HEADER); // send crypted hash to server
|
---|
3721 |
|
---|
3722 | memset(buf, 0, sizeof(buf));
|
---|
3723 | memcpy(buf, rdr->r_usr, strlen(rdr->r_usr));
|
---|
3724 | cs_ddump_mask(D_CLIENT, buf, 20, "cccam: username '%s':", buf);
|
---|
3725 | cc_cmd_send(cl, buf, 20, MSG_NO_HEADER); // send usr '0' padded -> 20 bytes
|
---|
3726 |
|
---|
3727 | memset(buf, 0, sizeof(buf));
|
---|
3728 | memset(pwd, 0, sizeof(pwd));
|
---|
3729 |
|
---|
3730 | //cs_debug_mask(D_CLIENT, "cccam: 'CCcam' xor");
|
---|
3731 | memcpy(buf, "CCcam", 5);
|
---|
3732 | strncpy(pwd, rdr->r_pwd, sizeof(pwd) - 1);
|
---|
3733 | cc_crypt(&cc->block[ENCRYPT], (uint8 *) pwd, strlen(pwd), ENCRYPT);
|
---|
3734 | cc_cmd_send(cl, buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd
|
---|
3735 |
|
---|
3736 | if ((n = cc_recv_to(cl, data, 20)) != 20) {
|
---|
3737 | cs_log("%s login failed, pwd ack not received (n = %d)", getprefix(), n);
|
---|
3738 | return -2;
|
---|
3739 | }
|
---|
3740 | cc_crypt(&cc->block[DECRYPT], data, 20, DECRYPT);
|
---|
3741 | cs_ddump_mask(D_CLIENT, data, 20, "cccam: pwd ack received:");
|
---|
3742 |
|
---|
3743 | if (memcmp(data, buf, 5)) { // check server response
|
---|
3744 | cs_log("%s login failed, usr/pwd invalid", getprefix());
|
---|
3745 | return -2;
|
---|
3746 | } else {
|
---|
3747 | cs_debug_mask(D_READER, "%s login succeeded", getprefix());
|
---|
3748 | }
|
---|
3749 |
|
---|
3750 | cs_debug_mask(D_READER, "cccam: last_s=%d, last_g=%d", rdr->last_s, rdr->last_g);
|
---|
3751 |
|
---|
3752 | cl->pfd = cl->udp_fd;
|
---|
3753 | cs_debug_mask(D_READER, "cccam: pfd=%d", cl->pfd);
|
---|
3754 |
|
---|
3755 | if (cc_send_cli_data(cl) <= 0) {
|
---|
3756 | cs_log("%s login failed, could not send client data", getprefix());
|
---|
3757 | return -3;
|
---|
3758 | }
|
---|
3759 |
|
---|
3760 | rdr->caid = rdr->ftab.filts[0].caid;
|
---|
3761 | rdr->nprov = rdr->ftab.filts[0].nprids;
|
---|
3762 | for (n = 0; n < rdr->nprov; n++) {
|
---|
3763 | rdr->availkeys[n][0] = 1;
|
---|
3764 | rdr->prid[n][0] = rdr->ftab.filts[0].prids[n] >> 24;
|
---|
3765 | rdr->prid[n][1] = rdr->ftab.filts[0].prids[n] >> 16;
|
---|
3766 | rdr->prid[n][2] = rdr->ftab.filts[0].prids[n] >> 8;
|
---|
3767 | rdr->prid[n][3] = rdr->ftab.filts[0].prids[n] & 0xff;
|
---|
3768 | }
|
---|
3769 |
|
---|
3770 | rdr->card_status = CARD_NEED_INIT;
|
---|
3771 | rdr->last_g = rdr->last_s = time((time_t *) 0);
|
---|
3772 | rdr->tcp_connected = 1;
|
---|
3773 | rdr->available = 1;
|
---|
3774 |
|
---|
3775 | cc->just_logged_in = 1;
|
---|
3776 | cl->crypted = 1;
|
---|
3777 |
|
---|
3778 | return 0;
|
---|
3779 | }
|
---|
3780 |
|
---|
3781 | int cc_cli_init_int(struct s_client *cl) {
|
---|
3782 | struct s_reader *rdr = cl->reader;
|
---|
3783 | if (rdr->tcp_connected)
|
---|
3784 | return 1;
|
---|
3785 |
|
---|
3786 | struct protoent *ptrp;
|
---|
3787 | int p_proto;
|
---|
3788 |
|
---|
3789 | if (cl->pfd) {
|
---|
3790 | close(cl->pfd);
|
---|
3791 | if (cl->pfd == cl->udp_fd)
|
---|
3792 | cl->udp_fd = 0;
|
---|
3793 | cl->pfd = 0;
|
---|
3794 | }
|
---|
3795 |
|
---|
3796 | if (cl->udp_fd) {
|
---|
3797 | close(cl->udp_fd);
|
---|
3798 | cl->udp_fd = 0;
|
---|
3799 | }
|
---|
3800 |
|
---|
3801 | if (rdr->r_port <= 0) {
|
---|
3802 | cs_log("%s invalid port %d for server %s", rdr->label, rdr->r_port,
|
---|
3803 | rdr->device);
|
---|
3804 | return 1;
|
---|
3805 | }
|
---|
3806 | if ((ptrp = getprotobyname("tcp")))
|
---|
3807 | p_proto = ptrp->p_proto;
|
---|
3808 | else
|
---|
3809 | p_proto = 6;
|
---|
3810 |
|
---|
3811 | // cl->ip = 0;
|
---|
3812 | // memset((char *) &loc_sa, 0, sizeof(loc_sa));
|
---|
3813 | // loc_sa.sin_family = AF_INET;
|
---|
3814 | //#ifdef LALL
|
---|
3815 | // if (cfg.serverip[0])
|
---|
3816 | // loc_sa.sin_addr.s_addr = inet_addr(cfg.serverip);
|
---|
3817 | // else
|
---|
3818 | //#endif
|
---|
3819 | // loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
3820 | // loc_sa.sin_port = htons(rdr->l_port);
|
---|
3821 |
|
---|
3822 |
|
---|
3823 | if ((cl->udp_fd = socket(PF_INET, SOCK_STREAM, p_proto)) <= 0) {
|
---|
3824 | cs_log("%s Socket creation failed (errno=%d, socket=%d)", rdr->label,
|
---|
3825 | errno, cl->udp_fd);
|
---|
3826 | return 1;
|
---|
3827 | }
|
---|
3828 | //cs_log("%s 1 socket created: cs_idx=%d, fd=%d errno=%d", getprefix(), cs_idx, cl->udp_fd, errno);
|
---|
3829 |
|
---|
3830 | #ifdef SO_PRIORITY
|
---|
3831 | if (cfg.netprio)
|
---|
3832 | setsockopt(cl->udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
3833 | (void *)&cfg.netprio, sizeof(ulong));
|
---|
3834 | #endif
|
---|
3835 | rdr->tcp_ito = 1; //60sec...This now invokes ph_idle()
|
---|
3836 | if (rdr->cc_maxhop < 0)
|
---|
3837 | rdr->cc_maxhop = 10;
|
---|
3838 |
|
---|
3839 | memset((char *) &cl->udp_sa, 0, sizeof(cl->udp_sa));
|
---|
3840 | cl->udp_sa.sin_family = AF_INET;
|
---|
3841 | cl->udp_sa.sin_port = htons((u_short) rdr->r_port);
|
---|
3842 |
|
---|
3843 | if (rdr->tcp_rto <= 2)
|
---|
3844 | rdr->tcp_rto = 2; // timeout to 120s
|
---|
3845 | cs_debug_mask(D_READER, "cccam: timeout set to: %d", rdr->tcp_rto);
|
---|
3846 | cc_check_version(rdr->cc_version, rdr->cc_build);
|
---|
3847 | cs_debug_mask(D_READER, "proxy reader: %s (%s:%d) cccam v%s build %s, maxhop: %d",
|
---|
3848 | rdr->label, rdr->device, rdr->r_port, rdr->cc_version,
|
---|
3849 | rdr->cc_build, rdr->cc_maxhop);
|
---|
3850 |
|
---|
3851 | return 0;
|
---|
3852 | }
|
---|
3853 |
|
---|
3854 | int cc_cli_init(struct s_client *cl) {
|
---|
3855 | struct cc_data *cc = cl->cc;
|
---|
3856 | struct s_reader *reader = cl->reader;
|
---|
3857 |
|
---|
3858 | if ((cc && cc->mode == CCCAM_MODE_SHUTDOWN))
|
---|
3859 | return -1;
|
---|
3860 |
|
---|
3861 | int res = cc_cli_init_int(cl); //Create socket
|
---|
3862 |
|
---|
3863 | if (res == 0 && reader && (reader->cc_keepalive || !cl->cc) && !reader->tcp_connected) {
|
---|
3864 |
|
---|
3865 | cc_cli_connect(cl); //connect to remote server
|
---|
3866 |
|
---|
3867 | while (!reader->tcp_connected && reader->cc_keepalive && cfg.reader_restart_seconds > 0) {
|
---|
3868 |
|
---|
3869 | if ((cc && cc->mode == CCCAM_MODE_SHUTDOWN))
|
---|
3870 | return -1;
|
---|
3871 |
|
---|
3872 | if (!reader->tcp_connected) {
|
---|
3873 | cc_cli_close(cl, FALSE);
|
---|
3874 | res = cc_cli_init_int(cl);
|
---|
3875 | if (res)
|
---|
3876 | return res;
|
---|
3877 | }
|
---|
3878 | cs_debug_mask(D_READER, "%s restarting reader in %d seconds", reader->label, cfg.reader_restart_seconds);
|
---|
3879 | cs_sleepms(cfg.reader_restart_seconds*1000);
|
---|
3880 | cs_debug_mask(D_READER, "%s restarting reader...", reader->label);
|
---|
3881 | cc_cli_connect(cl);
|
---|
3882 | }
|
---|
3883 | }
|
---|
3884 | return res;
|
---|
3885 | }
|
---|
3886 |
|
---|
3887 | /**
|
---|
3888 | * return 1 if we are able to send requests:
|
---|
3889 | *
|
---|
3890 | */
|
---|
3891 | int cc_available(struct s_reader *rdr, int checktype) {
|
---|
3892 | if (!rdr || !rdr->client) return 0;
|
---|
3893 |
|
---|
3894 | struct s_client *cl = rdr->client;
|
---|
3895 | //cs_debug_mask(D_TRACE, "checking reader %s availibility", rdr->label);
|
---|
3896 | if (!cl->cc || rdr->tcp_connected != 2 || rdr->card_status != CARD_INSERTED) {
|
---|
3897 | //Two cases:
|
---|
3898 | // 1. Keepalive ON but not connected: Do NOT send requests,
|
---|
3899 | // because we can't connect - problem of full running pipes
|
---|
3900 | // 2. Keepalive OFF but not connected: Send requests to connect
|
---|
3901 | // pipe won't run full, because we are reading from pipe to
|
---|
3902 | // get the ecm request
|
---|
3903 | if (rdr->cc_keepalive)
|
---|
3904 | return 0;
|
---|
3905 | }
|
---|
3906 |
|
---|
3907 | if (checktype == AVAIL_CHECK_LOADBALANCE && !rdr->available) {
|
---|
3908 | cs_debug_mask(D_TRACE, "checking reader %s availibility=0 (unavail)",
|
---|
3909 | rdr->label);
|
---|
3910 | return 0; //We are processing EMMs/ECMs
|
---|
3911 | }
|
---|
3912 |
|
---|
3913 | return 1;
|
---|
3914 | }
|
---|
3915 |
|
---|
3916 | /**
|
---|
3917 | *
|
---|
3918 | *
|
---|
3919 | **/
|
---|
3920 | void cc_card_info() {
|
---|
3921 | struct s_client *cl = cur_client();
|
---|
3922 | struct s_reader *rdr = cl->reader;
|
---|
3923 |
|
---|
3924 | if (rdr && !rdr->tcp_connected)
|
---|
3925 | cc_cli_connect(cl);
|
---|
3926 | }
|
---|
3927 |
|
---|
3928 | void cc_cleanup(struct s_client *cl) {
|
---|
3929 | struct cc_data *cc = cl->cc;
|
---|
3930 | if (cc) cc->mode = CCCAM_MODE_SHUTDOWN;
|
---|
3931 |
|
---|
3932 | if (cl->typ != 'c') {
|
---|
3933 | cc_cli_close(cl, FALSE); // we need to close open fd's
|
---|
3934 | }
|
---|
3935 | cc_free(cl);
|
---|
3936 | }
|
---|
3937 |
|
---|
3938 | void module_cccam(struct s_module *ph) {
|
---|
3939 | strcpy(ph->desc, "cccam");
|
---|
3940 | ph->type = MOD_CONN_TCP;
|
---|
3941 | ph->logtxt = ", crypted";
|
---|
3942 | ph->watchdog = 1;
|
---|
3943 | ph->recv = cc_recv;
|
---|
3944 | ph->cleanup = cc_cleanup;
|
---|
3945 | ph->multi = 1;
|
---|
3946 | ph->c_multi = 1;
|
---|
3947 | ph->c_init = cc_cli_init;
|
---|
3948 | ph->c_idle = cc_idle;
|
---|
3949 | ph->c_recv_chk = cc_recv_chk;
|
---|
3950 | ph->c_send_ecm = cc_send_ecm;
|
---|
3951 | ph->c_send_emm = cc_send_emm;
|
---|
3952 | ph->s_ip = cfg.cc_srvip;
|
---|
3953 | ph->s_handler = cc_srv_init;
|
---|
3954 | ph->send_dcw = cc_send_dcw;
|
---|
3955 | ph->c_available = cc_available;
|
---|
3956 | ph->c_card_info = cc_card_info;
|
---|
3957 | static PTAB ptab; //since there is always only 1 cccam server running, this is threadsafe
|
---|
3958 | ptab.ports[0].s_port = cfg.cc_port;
|
---|
3959 | ph->ptab = &ptab;
|
---|
3960 | ph->ptab->nports = 1;
|
---|
3961 | ph->num = R_CCCAM;
|
---|
3962 |
|
---|
3963 | //Partner Detection:
|
---|
3964 | init_rnd();
|
---|
3965 | uint16 sum = 0x1234; //This is our checksum
|
---|
3966 | int i;
|
---|
3967 | for (i = 0; i < 6; i++) {
|
---|
3968 | cc_node_id[i] = fast_rnd();
|
---|
3969 | sum += cc_node_id[i];
|
---|
3970 | }
|
---|
3971 | cc_node_id[6] = sum >> 8;
|
---|
3972 | cc_node_id[7] = sum & 0xff;
|
---|
3973 | }
|
---|