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