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