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