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