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