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