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