1 |
|
---|
2 | #include <string.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include "globals.h"
|
---|
5 | #include "module-cccam.h"
|
---|
6 | #include "module-obj-llist.h"
|
---|
7 | #include <time.h>
|
---|
8 | #include "reader-common.h"
|
---|
9 | #include <poll.h>
|
---|
10 |
|
---|
11 | extern struct s_reader *reader;
|
---|
12 |
|
---|
13 | //Mode names for CMD_05 command:
|
---|
14 | char *cmd05_mode_name[] = { "UNKNOWN", "PLAIN", "AES", "CC_CRYPT", "RC4", "LEN=0" };
|
---|
15 |
|
---|
16 | static unsigned int seed;
|
---|
17 | static uchar fast_rnd() {
|
---|
18 | unsigned int offset = 12923;
|
---|
19 | unsigned int multiplier = 4079;
|
---|
20 |
|
---|
21 | seed = seed * multiplier + offset;
|
---|
22 | return (uchar)(seed % 0xFF);
|
---|
23 | }
|
---|
24 |
|
---|
25 | static int cc_cli_init();
|
---|
26 | static int cc_get_nxt_ecm();
|
---|
27 | static int cc_send_pending_emms();
|
---|
28 | static void cc_rc4_crypt(struct cc_crypt_block *block, uint8 *data, int len,
|
---|
29 | cc_crypt_mode_t mode);
|
---|
30 |
|
---|
31 | static char *getprefix() {
|
---|
32 | if (client[cs_idx].prefix)
|
---|
33 | return client[cs_idx].prefix;
|
---|
34 |
|
---|
35 | client[cs_idx].prefix = malloc(100);
|
---|
36 | if (client[cs_idx].is_server)
|
---|
37 | sprintf(client[cs_idx].prefix, "cccam(s) %s: ", client[cs_idx].usr);
|
---|
38 | else
|
---|
39 | sprintf(client[cs_idx].prefix, "cccam(r) %s: ", reader[client[cs_idx].ridx].label);
|
---|
40 | while (strlen(client[cs_idx].prefix) < 22)
|
---|
41 | strcat(client[cs_idx].prefix, " ");
|
---|
42 | return client[cs_idx].prefix;
|
---|
43 | }
|
---|
44 |
|
---|
45 | static int comp_timeb(struct timeb *tpa, struct timeb *tpb)
|
---|
46 | {
|
---|
47 | if (tpa->time>tpb->time) return(1);
|
---|
48 | if (tpa->time<tpb->time) return(-1);
|
---|
49 | if (tpa->millitm>tpb->millitm) return(1);
|
---|
50 | if (tpa->millitm<tpb->millitm) return(-1);
|
---|
51 | return(0);
|
---|
52 | }
|
---|
53 |
|
---|
54 | static void cc_init_crypt(struct cc_crypt_block *block, uint8 *key, int len) {
|
---|
55 | int i = 0;
|
---|
56 | uint8 j = 0;
|
---|
57 |
|
---|
58 | for (i = 0; i < 256; i++) {
|
---|
59 | block->keytable[i] = i;
|
---|
60 | }
|
---|
61 |
|
---|
62 | for (i = 0; i < 256; i++) {
|
---|
63 | j += key[i % len] + block->keytable[i];
|
---|
64 | SWAPC(&block->keytable[i], &block->keytable[j]);
|
---|
65 | }
|
---|
66 |
|
---|
67 | block->state = *key;
|
---|
68 | block->counter = 0;
|
---|
69 | block->sum = 0;
|
---|
70 | }
|
---|
71 |
|
---|
72 | static void cc_crypt(struct cc_crypt_block *block, uint8 *data, int len,
|
---|
73 | cc_crypt_mode_t mode) {
|
---|
74 | int i;
|
---|
75 | uint8 z;
|
---|
76 |
|
---|
77 | for (i = 0; i < len; i++) {
|
---|
78 | block->counter++;
|
---|
79 | block->sum += block->keytable[block->counter];
|
---|
80 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
81 | z = data[i];
|
---|
82 | data[i] = z ^ block->keytable[(block->keytable[block->counter]
|
---|
83 | + block->keytable[block->sum]) & 0xff];
|
---|
84 | if (!client[cs_idx].cc_use_rc4)
|
---|
85 | data[i] ^= block->state;
|
---|
86 | if (!mode)
|
---|
87 | z = data[i];
|
---|
88 | block->state = block->state ^ z;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | static void cc_rc4_crypt(struct cc_crypt_block *block, uint8 *data, int len,
|
---|
93 | cc_crypt_mode_t mode) {
|
---|
94 | int i;
|
---|
95 | uint8 z;
|
---|
96 |
|
---|
97 | for (i = 0; i < len; i++) {
|
---|
98 | block->counter++;
|
---|
99 | block->sum += block->keytable[block->counter];
|
---|
100 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
101 | z = data[i];
|
---|
102 | data[i] = z ^ block->keytable[(block->keytable[block->counter]
|
---|
103 | + block->keytable[block->sum]) & 0xff];
|
---|
104 | if (!mode)
|
---|
105 | z = data[i];
|
---|
106 | block->state = block->state ^ z;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | static void cc_xor(uint8 *buf) {
|
---|
112 | const char cccam[] = "CCcam";
|
---|
113 | uint8 i;
|
---|
114 |
|
---|
115 | for (i = 0; i < 8; i++) {
|
---|
116 | buf[8 + i] = i * buf[i];
|
---|
117 | if (i <= 5) {
|
---|
118 | buf[i] ^= cccam[i];
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | static void cc_cw_crypt(uint8 *cws, uint32 cardid) {
|
---|
124 | struct cc_data *cc;
|
---|
125 | uint64 node_id;
|
---|
126 | uint8 tmp;
|
---|
127 | int i;
|
---|
128 |
|
---|
129 | if (!client[cs_idx].is_server) {
|
---|
130 | cc = reader[client[cs_idx].ridx].cc;
|
---|
131 | node_id = b2ll(8, cc->node_id);
|
---|
132 | } else {
|
---|
133 | cc = client[cs_idx].cc;
|
---|
134 | node_id = b2ll(8, cc->peer_node_id);
|
---|
135 | }
|
---|
136 |
|
---|
137 | for (i = 0; i < 16; i++) {
|
---|
138 | tmp = cws[i] ^ (node_id >> (4 * i));
|
---|
139 | if (i & 1)
|
---|
140 | tmp = ~tmp;
|
---|
141 | cws[i] = (cardid >> (2 * i)) ^ tmp;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * reader
|
---|
147 | * cleans autoblock list
|
---|
148 | * list is not disposed
|
---|
149 | */
|
---|
150 | static void cc_clear_auto_blocked(LLIST *cc_auto_blocked_list) {
|
---|
151 | LLIST_ITR itr;
|
---|
152 | struct cc_auto_blocked *auto_blocked = llist_itr_init(cc_auto_blocked_list,
|
---|
153 | &itr);
|
---|
154 | while (auto_blocked) {
|
---|
155 | free(auto_blocked);
|
---|
156 | auto_blocked = llist_itr_remove(&itr);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | static void cc_free_auto_blocked(LLIST *cc_auto_blocked_list) {
|
---|
161 | if (cc_auto_blocked_list) {
|
---|
162 | cc_clear_auto_blocked(cc_auto_blocked_list);
|
---|
163 | llist_destroy(cc_auto_blocked_list);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | /**
|
---|
167 | * reader
|
---|
168 | * removed all caid:prov:xxx from the auto blocked list.
|
---|
169 | * This function is called, when a new card arrives
|
---|
170 | */
|
---|
171 | static int cc_remove_from_auto_blocked(LLIST *cc_auto_blocked_list,
|
---|
172 | uint16 caid, uint32 prov) {
|
---|
173 | int found = 0;
|
---|
174 | LLIST_ITR itr;
|
---|
175 | struct cc_auto_blocked *auto_blocked = llist_itr_init(cc_auto_blocked_list,
|
---|
176 | &itr);
|
---|
177 | while (auto_blocked) {
|
---|
178 | if (auto_blocked->caid == caid && auto_blocked->prov == prov) {
|
---|
179 | free(auto_blocked);
|
---|
180 | auto_blocked = llist_itr_remove(&itr);
|
---|
181 | found++;
|
---|
182 | } else
|
---|
183 | auto_blocked = llist_itr_next(&itr);
|
---|
184 | }
|
---|
185 | return found;
|
---|
186 | }
|
---|
187 |
|
---|
188 | static int sid_eq(struct cc_srvid *srvid1, struct cc_srvid *srvid2) {
|
---|
189 | return (srvid1->sid == srvid2->sid && srvid1->ecmlen == srvid2->ecmlen);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * reader
|
---|
195 | * add caid:prov:sid to the autoblock list
|
---|
196 | */
|
---|
197 | static int cc_add_auto_blocked(LLIST *cc_auto_blocked_list, uint16 caid,
|
---|
198 | uint32 prov, struct cc_srvid *srvid) {
|
---|
199 | LLIST_ITR itr;
|
---|
200 | struct cc_auto_blocked *auto_blocked = llist_itr_init(cc_auto_blocked_list,
|
---|
201 | &itr);
|
---|
202 | while (auto_blocked) {
|
---|
203 | if (auto_blocked->caid == caid && auto_blocked->prov == prov
|
---|
204 | && sid_eq(&auto_blocked->srvid, srvid)) {
|
---|
205 | auto_blocked->time = time((time_t*) 0);
|
---|
206 | return 0; //Already blocked
|
---|
207 | }
|
---|
208 | auto_blocked = llist_itr_next(&itr);
|
---|
209 | }
|
---|
210 | //Add auto-block:
|
---|
211 | auto_blocked = malloc(sizeof(struct cc_auto_blocked));
|
---|
212 | auto_blocked->caid = caid;
|
---|
213 | auto_blocked->prov = prov;
|
---|
214 | auto_blocked->srvid = *srvid;
|
---|
215 | auto_blocked->time = time((time_t*) 0);
|
---|
216 | llist_append(cc_auto_blocked_list, auto_blocked);
|
---|
217 | cs_debug_mask(D_TRACE, "%s adding %04X:%04X:%04X(%d) to auto block list", getprefix(), caid,
|
---|
218 | prov, srvid->sid, srvid->ecmlen);
|
---|
219 | return 1;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * reader
|
---|
224 | * checks if caid:prov:sid is on the autoblock list
|
---|
225 | */
|
---|
226 | static int cc_is_auto_blocked(LLIST *cc_auto_blocked_list, uint16 caid,
|
---|
227 | uint32 prov, struct cc_srvid *srvid, int timeout) {
|
---|
228 | LLIST_ITR itr;
|
---|
229 | struct cc_auto_blocked *auto_blocked = llist_itr_init(cc_auto_blocked_list,
|
---|
230 | &itr);
|
---|
231 | while (auto_blocked) {
|
---|
232 | if (auto_blocked->caid == caid && auto_blocked->prov == prov
|
---|
233 | && sid_eq(&auto_blocked->srvid, srvid)) {
|
---|
234 | if (auto_blocked->time < time((time_t*) 0) - timeout) {
|
---|
235 | free(auto_blocked);
|
---|
236 | llist_itr_remove(&itr);
|
---|
237 | } else {
|
---|
238 | auto_blocked->time = time((time_t*) 0);
|
---|
239 | return 1; //Already blocked
|
---|
240 | }
|
---|
241 | }
|
---|
242 | auto_blocked = llist_itr_next(&itr);
|
---|
243 | }
|
---|
244 | return 0;
|
---|
245 | }
|
---|
246 |
|
---|
247 | static int is_sid_blocked(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
248 | LLIST_ITR sitr;
|
---|
249 | struct cc_srvid *srvid = llist_itr_init(card->badsids, &sitr);
|
---|
250 | while (srvid) {
|
---|
251 | if (sid_eq(srvid, srvid_blocked)) {
|
---|
252 | return 1;
|
---|
253 | }
|
---|
254 | srvid = llist_itr_next(&sitr);
|
---|
255 | }
|
---|
256 | return 0;
|
---|
257 | }
|
---|
258 |
|
---|
259 | static int is_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
260 | LLIST_ITR sitr;
|
---|
261 | struct cc_srvid *srvid = llist_itr_init(card->goodsids, &sitr);
|
---|
262 | while (srvid) {
|
---|
263 | if (sid_eq(srvid, srvid_good)) {
|
---|
264 | return 1;
|
---|
265 | }
|
---|
266 | srvid = llist_itr_next(&sitr);
|
---|
267 | }
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | static void add_sid_block(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
272 | if (is_sid_blocked(card, srvid_blocked))
|
---|
273 | return;
|
---|
274 |
|
---|
275 | struct cc_srvid *srvid = malloc(sizeof(struct cc_srvid));
|
---|
276 | if (srvid) {
|
---|
277 | *srvid = *srvid_blocked;
|
---|
278 | llist_append(card->badsids, srvid);
|
---|
279 | cs_debug_mask(D_TRACE, "%s added sid block %04X(%d) for card %08x",
|
---|
280 | getprefix(), srvid_blocked->sid, srvid_blocked->ecmlen, card->id);
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | static void remove_sid_block(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
285 | LLIST_ITR sitr;
|
---|
286 | struct cc_srvid *srvid = llist_itr_init(card->badsids, &sitr);
|
---|
287 | while (srvid) {
|
---|
288 | if (sid_eq(srvid, srvid_blocked)) {
|
---|
289 | free(srvid);
|
---|
290 | srvid = llist_itr_remove(&sitr);
|
---|
291 | }
|
---|
292 | else
|
---|
293 | srvid = llist_itr_next(&sitr);
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | static void remove_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
298 | LLIST_ITR sitr;
|
---|
299 | struct cc_srvid *srvid = llist_itr_init(card->goodsids, &sitr);
|
---|
300 | while (srvid) {
|
---|
301 | if (sid_eq(srvid, srvid_good)) {
|
---|
302 | free(srvid);
|
---|
303 | srvid = llist_itr_remove(&sitr);
|
---|
304 | }
|
---|
305 | else
|
---|
306 | srvid = llist_itr_next(&sitr);
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | static void add_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
311 | if (is_good_sid(card, srvid_good))
|
---|
312 | return;
|
---|
313 |
|
---|
314 | remove_sid_block(card, srvid_good);
|
---|
315 | struct cc_srvid *srvid = malloc(sizeof(struct cc_srvid));
|
---|
316 | if (srvid) {
|
---|
317 | *srvid = *srvid_good;
|
---|
318 | llist_append(card->goodsids, srvid);
|
---|
319 | cs_debug_mask(D_TRACE, "%s added good sid %04X(%d) for card %08x",
|
---|
320 | getprefix(), srvid_good->sid, srvid_good->ecmlen, card->id);
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * reader
|
---|
327 | * clears and frees values for reinit
|
---|
328 | */
|
---|
329 | static void cc_cli_close() {
|
---|
330 | reader[client[cs_idx].ridx].tcp_connected = 0;
|
---|
331 | reader[client[cs_idx].ridx].card_status = CARD_FAILURE;
|
---|
332 | reader[client[cs_idx].ridx].available = 0;
|
---|
333 | reader[client[cs_idx].ridx].card_system = 0;
|
---|
334 |
|
---|
335 | //cs_sleepms(100);
|
---|
336 | if (client[cs_idx].pfd) {
|
---|
337 | close(client[cs_idx].pfd);
|
---|
338 | client[cs_idx].pfd = 0;
|
---|
339 | client[cs_idx].udp_fd = 0;
|
---|
340 | }
|
---|
341 | else if (client[cs_idx].udp_fd) {
|
---|
342 | close(client[cs_idx].udp_fd);
|
---|
343 | client[cs_idx].udp_fd = 0;
|
---|
344 | client[cs_idx].pfd = 0;
|
---|
345 | }
|
---|
346 | //cs_sleepms(100);
|
---|
347 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
348 | if (cc) {
|
---|
349 | pthread_mutex_unlock(&cc->lock);
|
---|
350 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
351 | pthread_mutex_destroy(&cc->lock);
|
---|
352 | pthread_mutex_destroy(&cc->ecm_busy);
|
---|
353 | cc_clear_auto_blocked(cc->auto_blocked);
|
---|
354 | cc->just_logged_in = 0;
|
---|
355 | //cc->current_ecm_cidx = 0;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * reader
|
---|
361 | * closes the connection and reopens it.
|
---|
362 | */
|
---|
363 | static void cc_cycle_connection() {
|
---|
364 | cc_cli_close();
|
---|
365 | cc_cli_init();
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * reader+server:
|
---|
370 | * receive a message
|
---|
371 | */
|
---|
372 | static int cc_msg_recv(uint8 *buf) {
|
---|
373 | int len;
|
---|
374 | uint8 netbuf[CC_MAXMSGSIZE + 4];
|
---|
375 | struct cc_data *cc;
|
---|
376 |
|
---|
377 | if (!client[cs_idx].is_server)
|
---|
378 | cc = reader[client[cs_idx].ridx].cc;
|
---|
379 | else
|
---|
380 | cc = client[cs_idx].cc;
|
---|
381 |
|
---|
382 | int handle = client[cs_idx].udp_fd;
|
---|
383 |
|
---|
384 | if (handle < 0)
|
---|
385 | return -1;
|
---|
386 |
|
---|
387 | len = recv(handle, netbuf, 4, MSG_WAITALL);
|
---|
388 | if (!client[cs_idx].is_server)
|
---|
389 | reader[client[cs_idx].ridx].last_g = time(NULL);
|
---|
390 |
|
---|
391 | if (!len)
|
---|
392 | return 0;
|
---|
393 |
|
---|
394 | if (len != 4) { // invalid header length read
|
---|
395 | cs_log("%s invalid header length (expected 4, read %d)", getprefix(), len);
|
---|
396 | return -1;
|
---|
397 | }
|
---|
398 |
|
---|
399 | cc_crypt(&cc->block[DECRYPT], netbuf, 4, DECRYPT);
|
---|
400 | cs_ddump(netbuf, 4, "cccam: decrypted header:");
|
---|
401 |
|
---|
402 | client[cs_idx].g_flag = netbuf[0];
|
---|
403 |
|
---|
404 | int size = (netbuf[2] << 8) | netbuf[3];
|
---|
405 | if (size) { // check if any data is expected in msg
|
---|
406 | if (size > CC_MAXMSGSIZE - 2) {
|
---|
407 | cs_log("%s message too big (size=%d)", getprefix(), size);
|
---|
408 | return 0;
|
---|
409 | }
|
---|
410 |
|
---|
411 | len = recv(handle, netbuf + 4, size, MSG_WAITALL); // read rest of msg
|
---|
412 | if (!client[cs_idx].is_server)
|
---|
413 | reader[client[cs_idx].ridx].last_g = time(NULL);
|
---|
414 |
|
---|
415 | if (len != size) {
|
---|
416 | cs_log("%s invalid message length read (expected %d, read %d)", getprefix(), size, len);
|
---|
417 | return -1;
|
---|
418 | }
|
---|
419 |
|
---|
420 | cc_crypt(&cc->block[DECRYPT], netbuf + 4, len, DECRYPT);
|
---|
421 | len += 4;
|
---|
422 | }
|
---|
423 |
|
---|
424 | cs_ddump(netbuf, len, "cccam: full decrypted msg, len=%d:", len);
|
---|
425 |
|
---|
426 | memcpy(buf, netbuf, len);
|
---|
427 | return len;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * reader+server
|
---|
432 | * send a message
|
---|
433 | */
|
---|
434 | static int cc_cmd_send(uint8 *buf, int len, cc_msg_type_t cmd) {
|
---|
435 | int n;
|
---|
436 | uint8 netbuf[len + 4];
|
---|
437 | struct cc_data *cc;
|
---|
438 |
|
---|
439 | if (!client[cs_idx].is_server)
|
---|
440 | cc = reader[client[cs_idx].ridx].cc;
|
---|
441 | else
|
---|
442 | cc = client[cs_idx].cc;
|
---|
443 |
|
---|
444 | memset(netbuf, 0, len + 4);
|
---|
445 |
|
---|
446 | if (cmd == MSG_NO_HEADER) {
|
---|
447 | memcpy(netbuf, buf, len);
|
---|
448 | } else {
|
---|
449 | // build command message
|
---|
450 | netbuf[0] = client[cs_idx].g_flag; // flags??
|
---|
451 | netbuf[1] = cmd & 0xff;
|
---|
452 | netbuf[2] = len >> 8;
|
---|
453 | netbuf[3] = len & 0xff;
|
---|
454 | if (buf)
|
---|
455 | memcpy(netbuf + 4, buf, len);
|
---|
456 | len += 4;
|
---|
457 | }
|
---|
458 |
|
---|
459 | cs_ddump(netbuf, len, "cccam: send:");
|
---|
460 | cc_crypt(&cc->block[ENCRYPT], netbuf, len, ENCRYPT);
|
---|
461 |
|
---|
462 | n = send(client[cs_idx].udp_fd, netbuf, len, 0);
|
---|
463 | if (!client[cs_idx].is_server)
|
---|
464 | reader[client[cs_idx].ridx].last_s = time(NULL);
|
---|
465 |
|
---|
466 | if (n < 0 && client[cs_idx].is_server) {
|
---|
467 | cs_disconnect_client();
|
---|
468 | }
|
---|
469 |
|
---|
470 | return n;
|
---|
471 | }
|
---|
472 |
|
---|
473 | #define CC_DEFAULT_VERSION 1
|
---|
474 | char *version[] = { "2.0.11", "2.1.1", "2.1.2", "2.1.3", "2.1.4", "" };
|
---|
475 | char *build[] = { "2892", "2971", "3094", "3165", "3191", "" };
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * reader+server
|
---|
479 | * checks the cccam-version in the configuration
|
---|
480 | */
|
---|
481 | static void cc_check_version(char *cc_version, char *cc_build) {
|
---|
482 | int i;
|
---|
483 | for (i = 0; strlen(version[i]); i++) {
|
---|
484 | if (!memcmp(cc_version, version[i], strlen(version[i]))) {
|
---|
485 | memcpy(cc_build, build[i], strlen(build[i]));
|
---|
486 | cs_debug("cccam: auto build set for version: %s build: %s",
|
---|
487 | cc_version, cc_build);
|
---|
488 | return;
|
---|
489 | }
|
---|
490 | }
|
---|
491 | memcpy(cc_version, version[CC_DEFAULT_VERSION], strlen(
|
---|
492 | version[CC_DEFAULT_VERSION]));
|
---|
493 | memcpy(cc_build, build[CC_DEFAULT_VERSION], strlen(
|
---|
494 | build[CC_DEFAULT_VERSION]));
|
---|
495 |
|
---|
496 | cs_debug("cccam: auto version set: %s build: %s", cc_version, cc_build);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /**
|
---|
500 | * reader
|
---|
501 | * sends own version information to the CCCam server
|
---|
502 | */
|
---|
503 | static int cc_send_cli_data() {
|
---|
504 | int i;
|
---|
505 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
506 |
|
---|
507 | cs_debug("cccam: send client data");
|
---|
508 |
|
---|
509 | seed = (unsigned int) time((time_t*) 0);
|
---|
510 | for (i = 0; i < 8; i++)
|
---|
511 | cc->node_id[i] = fast_rnd();
|
---|
512 |
|
---|
513 | uint8 buf[CC_MAXMSGSIZE];
|
---|
514 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
515 |
|
---|
516 | memcpy(buf, reader[client[cs_idx].ridx].r_usr, sizeof(reader[client[cs_idx].ridx].r_usr));
|
---|
517 | memcpy(buf + 20, cc->node_id, 8);
|
---|
518 | buf[28] = reader[client[cs_idx].ridx].cc_want_emu; // <-- Client want to have EMUs, 0 - NO; 1 - YES
|
---|
519 | memcpy(buf + 29, reader[client[cs_idx].ridx].cc_version, sizeof(reader[client[cs_idx].ridx].cc_version)); // cccam version (ascii)
|
---|
520 | memcpy(buf + 61, reader[client[cs_idx].ridx].cc_build, sizeof(reader[client[cs_idx].ridx].cc_build)); // build number (ascii)
|
---|
521 |
|
---|
522 | cs_log("%s sending own version: %s, build: %s", getprefix(),
|
---|
523 | reader[client[cs_idx].ridx].cc_version, reader[client[cs_idx].ridx].cc_build);
|
---|
524 |
|
---|
525 | i = cc_cmd_send(buf, 20 + 8 + 6 + 26 + 4 + 28 + 1, MSG_CLI_DATA);
|
---|
526 |
|
---|
527 | return i;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * server
|
---|
532 | * sends version information to the client
|
---|
533 | */
|
---|
534 | static int cc_send_srv_data() {
|
---|
535 | int i;
|
---|
536 | struct cc_data *cc = client[cs_idx].cc;
|
---|
537 |
|
---|
538 | cs_debug("cccam: send server data");
|
---|
539 |
|
---|
540 | seed = (unsigned int) time((time_t*) 0);
|
---|
541 | for (i = 0; i < 8; i++)
|
---|
542 | cc->node_id[i] = fast_rnd();
|
---|
543 |
|
---|
544 | uint8 buf[CC_MAXMSGSIZE];
|
---|
545 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
546 |
|
---|
547 | memcpy(buf, cc->node_id, 8);
|
---|
548 | cc_check_version((char *) cfg->cc_version, reader[client[cs_idx].ridx].cc_build);
|
---|
549 | memcpy(buf + 8, cfg->cc_version, sizeof(reader[client[cs_idx].ridx].cc_version)); // cccam version (ascii)
|
---|
550 | memcpy(buf + 40, reader[client[cs_idx].ridx].cc_build, sizeof(reader[client[cs_idx].ridx].cc_build)); // build number (ascii)
|
---|
551 |
|
---|
552 | cs_log("%s version: %s, build: %s nodeid: %s", getprefix(),
|
---|
553 | cfg->cc_version, reader[client[cs_idx].ridx].cc_build, cs_hexdump(0, cc->peer_node_id, 8));
|
---|
554 |
|
---|
555 | return cc_cmd_send(buf, 0x48, MSG_SRV_DATA);
|
---|
556 | }
|
---|
557 |
|
---|
558 | /**
|
---|
559 | * reader
|
---|
560 | * retrieves the next waiting ecm request
|
---|
561 | */
|
---|
562 | static int cc_get_nxt_ecm() {
|
---|
563 | int n, i;
|
---|
564 | time_t t;
|
---|
565 | // struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
566 |
|
---|
567 | t = time(NULL);
|
---|
568 | n = -1;
|
---|
569 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
570 | if ((t - (ulong) client[cs_idx].ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000)
|
---|
571 | + 1) && (client[cs_idx].ecmtask[i].rc >= 10)) // drop timeouts
|
---|
572 | {
|
---|
573 | client[cs_idx].ecmtask[i].rc = 0;
|
---|
574 | }
|
---|
575 |
|
---|
576 | if (client[cs_idx].ecmtask[i].rc >= 10 && client[cs_idx].ecmtask[i].rc != 99) { // stil active and waiting
|
---|
577 | // search for the ecm with the lowest time, this should be the next to go
|
---|
578 | if ((n < 0 || client[cs_idx].ecmtask[n].tps.time - client[cs_idx].ecmtask[i].tps.time < 0)
|
---|
579 | && &client[cs_idx].ecmtask[n])
|
---|
580 | n = i;
|
---|
581 | }
|
---|
582 | }
|
---|
583 | return n;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * sends the secret cmd05 answer to the server
|
---|
588 | */
|
---|
589 | static int send_cmd05_answer()
|
---|
590 | {
|
---|
591 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
592 | if (!cc->cmd05_active || cc->current_ecm_cidx) //exit if not in cmd05 or waiting for ECM answer
|
---|
593 | return 0;
|
---|
594 |
|
---|
595 | cc->cmd05_active--;
|
---|
596 | if (cc->cmd05_active)
|
---|
597 | return 0;
|
---|
598 |
|
---|
599 | uint8 *data = cc->cmd05_data;
|
---|
600 | cc_cmd05_mode cmd05_mode = MODE_UNKNOWN;
|
---|
601 |
|
---|
602 | // by Project:Keynation
|
---|
603 | switch (cc->cmd05_data_len) {
|
---|
604 | case 0: { //payload 0, return with payload 0!
|
---|
605 | cc_cmd_send(NULL, 0, MSG_CMD_05);
|
---|
606 | cmd05_mode = MODE_LEN0;
|
---|
607 | break;
|
---|
608 | }
|
---|
609 | case 256: {
|
---|
610 | cmd05_mode = cc->cmd05_mode;
|
---|
611 | switch (cmd05_mode) {
|
---|
612 | case MODE_PLAIN: { //Send plain unencrypted back
|
---|
613 | cc_cmd_send(data, 256, MSG_CMD_05);
|
---|
614 | break;
|
---|
615 | }
|
---|
616 | case MODE_AES: { //encrypt with received aes128 key:
|
---|
617 | AES_KEY key;
|
---|
618 | uint8 aeskey[16];
|
---|
619 | uint8 out[256];
|
---|
620 |
|
---|
621 | memcpy(aeskey, cc->cmd05_aeskey, 16);
|
---|
622 | memset(&key, 0, sizeof(key));
|
---|
623 |
|
---|
624 | AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
|
---|
625 | int i;
|
---|
626 | for (i = 0; i < 256; i+=16)
|
---|
627 | AES_encrypt((unsigned char *) data+i, (unsigned char *) &out+i, &key);
|
---|
628 |
|
---|
629 | cc_cmd_send(out, 256, MSG_CMD_05);
|
---|
630 | break;
|
---|
631 | }
|
---|
632 | case MODE_CC_CRYPT: { //encrypt with cc_crypt:
|
---|
633 | cc_crypt(&cc->cmd05_cryptkey, data, 256, ENCRYPT);
|
---|
634 | cc_cmd_send(data, 256, MSG_CMD_05);
|
---|
635 | break;
|
---|
636 | }
|
---|
637 | case MODE_RC4_CRYPT: {//special xor crypt:
|
---|
638 | cc_rc4_crypt(&cc->cmd05_cryptkey, data, 256, DECRYPT);
|
---|
639 | cc_cmd_send(data, 256, MSG_CMD_05);
|
---|
640 | break;
|
---|
641 | }
|
---|
642 | default:
|
---|
643 | cmd05_mode = MODE_UNKNOWN;
|
---|
644 | }
|
---|
645 | break;
|
---|
646 | }
|
---|
647 | default:
|
---|
648 | cmd05_mode = MODE_UNKNOWN;
|
---|
649 | }
|
---|
650 |
|
---|
651 | //unhandled types always needs cycle connection after 50 ECMs!!
|
---|
652 | if (cmd05_mode == MODE_UNKNOWN) {
|
---|
653 | cc_cmd_send(NULL, 0, MSG_CMD_05);
|
---|
654 | if (!cc->max_ecms) { //max_ecms already set?
|
---|
655 | cc->max_ecms = 50;
|
---|
656 | cc->ecm_counter = 0;
|
---|
657 | }
|
---|
658 | }
|
---|
659 | cs_debug_mask(D_TRACE, "%s sending CMD_05 back! MODE: %s len=%d", getprefix(), cmd05_mode_name[cmd05_mode], cc->cmd05_data_len);
|
---|
660 |
|
---|
661 | return 1;
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | /**
|
---|
666 | * reader
|
---|
667 | * sends a ecm request to the connected CCCam Server
|
---|
668 | */
|
---|
669 | static int cc_send_ecm(ECM_REQUEST *er, uchar *buf) {
|
---|
670 | int n, h = -1;
|
---|
671 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
672 | struct cc_card *card;
|
---|
673 | struct cc_current_card *current_card;
|
---|
674 | LLIST_ITR itr;
|
---|
675 | ECM_REQUEST *cur_er;
|
---|
676 | struct timeb cur_time;
|
---|
677 | cs_ftime(&cur_time);
|
---|
678 |
|
---|
679 | if (!cc || (client[cs_idx].pfd < 1) || !reader[client[cs_idx].ridx].tcp_connected) {
|
---|
680 | if (er) {
|
---|
681 | er->rc = 0;
|
---|
682 | er->rcEx = 0x27;
|
---|
683 | cs_debug_mask(D_TRACE, "%s server not init! ccinit=%d pfd=%d", getprefix(), cc ? 1
|
---|
684 | : 0, client[cs_idx].pfd);
|
---|
685 | write_ecm_answer(&reader[client[cs_idx].ridx], fd_c2m, er);
|
---|
686 | }
|
---|
687 | return -1;
|
---|
688 | }
|
---|
689 |
|
---|
690 | if (!llist_count(cc->cards))
|
---|
691 | return 0;
|
---|
692 |
|
---|
693 | cc->just_logged_in = 0;
|
---|
694 |
|
---|
695 | int force_resend_ecm = 0;
|
---|
696 |
|
---|
697 | if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) { //Unlock by NOK or ECM ACK
|
---|
698 | cs_debug_mask(D_TRACE, "%s ecm trylock: ecm busy, retrying later after msg-receive",
|
---|
699 | getprefix());
|
---|
700 |
|
---|
701 | //force resend ecm and break lock when fallbacktimeout occured
|
---|
702 | struct timeb timeout = cur_time;
|
---|
703 | timeout.millitm += cfg->ftimeout;
|
---|
704 | timeout.time += timeout.millitm / 1000;
|
---|
705 | timeout.millitm = timeout.millitm % 1000;
|
---|
706 | force_resend_ecm = reader[client[cs_idx].ridx].cc_force_resend_ecm && comp_timeb(&cur_time, &timeout) > 0;
|
---|
707 |
|
---|
708 | if (force_resend_ecm) {
|
---|
709 | cs_debug_mask(D_TRACE, "%s force_resend");
|
---|
710 | }
|
---|
711 | else {
|
---|
712 | timeout = cur_time;
|
---|
713 | timeout.millitm += cfg->ctimeout*4;
|
---|
714 | timeout.time += timeout.millitm / 1000;
|
---|
715 | timeout.millitm = timeout.millitm % 1000;
|
---|
716 |
|
---|
717 | if (comp_timeb(&cur_time, &timeout) > 0) { //TODO: Configuration?
|
---|
718 | cs_debug_mask(D_TRACE, "%s unlocked-cycleconnection! timeout %ds", getprefix(),
|
---|
719 | cfg->ctimeout*4/1000);
|
---|
720 | cc_cycle_connection();
|
---|
721 | }
|
---|
722 | return 0; //pending send...
|
---|
723 | }
|
---|
724 | }
|
---|
725 | cs_debug("cccam: ecm trylock: got lock");
|
---|
726 | cc->ecm_time = cur_time;
|
---|
727 | reader[client[cs_idx].ridx].available = 0;
|
---|
728 |
|
---|
729 | //Search next ECM to send:
|
---|
730 | int found = 0;
|
---|
731 | while (!found) {
|
---|
732 | if ((n = cc_get_nxt_ecm()) < 0) {
|
---|
733 | reader[client[cs_idx].ridx].available = 1;
|
---|
734 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
735 | cs_debug("%s no ecm pending!", getprefix());
|
---|
736 | cc->current_ecm_cidx = 0;
|
---|
737 | if (!cc_send_pending_emms())
|
---|
738 | send_cmd05_answer();
|
---|
739 | return 0; // no queued ecms
|
---|
740 | }
|
---|
741 | //cs_debug("cccam: ecm-task-idx = %d", n);
|
---|
742 | cur_er = &client[cs_idx].ecmtask[n];
|
---|
743 |
|
---|
744 | if (!force_resend_ecm && crc32(0, cur_er->ecm, cur_er->l) == cc->crc) {
|
---|
745 | //cs_log("%s cur_er->rc=%d", getprefix(), cur_er->rc);
|
---|
746 | cur_er->rc = 99; //ECM already send
|
---|
747 | }
|
---|
748 | cc->crc = crc32(0, cur_er->ecm, cur_er->l);
|
---|
749 |
|
---|
750 | //cs_debug("cccam: ecm crc = 0x%lx", cc->crc);
|
---|
751 |
|
---|
752 | if (cur_er->rc != 99)
|
---|
753 | found = 1;
|
---|
754 | }
|
---|
755 |
|
---|
756 | if (buf)
|
---|
757 | memcpy(buf, cur_er->ecm, cur_er->l);
|
---|
758 |
|
---|
759 | struct cc_srvid cur_srvid;
|
---|
760 | cur_srvid.sid = cur_er->srvid;
|
---|
761 | cur_srvid.ecmlen = cur_er->l;
|
---|
762 |
|
---|
763 | //First check last used card:
|
---|
764 | cc->current_ecm_cidx = cur_er->cidx;
|
---|
765 | current_card = &cc->current_card[cur_er->cidx];
|
---|
766 | if (current_card->card && current_card->prov == cur_er->prid &&
|
---|
767 | sid_eq(¤t_card->srvid, &cur_srvid))
|
---|
768 | card = current_card->card;
|
---|
769 | else
|
---|
770 | {
|
---|
771 | card = NULL;
|
---|
772 | current_card->prov = cur_er->prid;
|
---|
773 | current_card->srvid = cur_srvid;
|
---|
774 | }
|
---|
775 |
|
---|
776 | //then check all other cards
|
---|
777 | int is_auto_blocked = 0;
|
---|
778 | if (!card) {
|
---|
779 | //check if auto blocked:
|
---|
780 | if (!reader[client[cs_idx].ridx].cc_disable_auto_block &&
|
---|
781 | cc_is_auto_blocked(
|
---|
782 | cc->auto_blocked, cur_er->caid, cur_er->prid, &cur_srvid, 60*60*1)) { //TODO: Timeout 60*60*1 = 1h, Config?
|
---|
783 | is_auto_blocked = 1;
|
---|
784 | current_card->card = NULL;
|
---|
785 | }
|
---|
786 | else
|
---|
787 | {
|
---|
788 | card = llist_itr_init(cc->cards, &itr);
|
---|
789 | while (card) {
|
---|
790 | if (card->caid == cur_er->caid) { // caid matches
|
---|
791 | int s = is_sid_blocked(card, &cur_srvid);
|
---|
792 |
|
---|
793 | LLIST_ITR pitr;
|
---|
794 | uint8 *prov = llist_itr_init(card->provs, &pitr);
|
---|
795 | while (prov && !s) {
|
---|
796 | ulong card_prov = b2i(3, prov);
|
---|
797 | if (!cur_er->prid || !card_prov || card_prov == cur_er->prid) { // provid matches
|
---|
798 | if (((h < 0) || (card->hop < h)) && (card->hop
|
---|
799 | <= reader[client[cs_idx].ridx].cc_maxhop)) { // card is closer and doesn't exceed max hop
|
---|
800 | //cc->cur_card = card;
|
---|
801 | current_card->card = card;
|
---|
802 | h = card->hop; // card has been matched
|
---|
803 | }
|
---|
804 | }
|
---|
805 | prov = llist_itr_next(&pitr);
|
---|
806 | }
|
---|
807 | }
|
---|
808 | card = llist_itr_next(&itr);
|
---|
809 | }
|
---|
810 | }
|
---|
811 | }
|
---|
812 |
|
---|
813 | if (current_card->card) {
|
---|
814 | card = current_card->card;
|
---|
815 | current_card->prov = cur_er->prid;
|
---|
816 | current_card->srvid = cur_srvid;
|
---|
817 |
|
---|
818 | card->time = time((time_t) 0);
|
---|
819 | uint8 ecmbuf[CC_MAXMSGSIZE];
|
---|
820 | memset(ecmbuf, 0, CC_MAXMSGSIZE);
|
---|
821 |
|
---|
822 | // build ecm message
|
---|
823 | ecmbuf[0] = card->caid >> 8;
|
---|
824 | ecmbuf[1] = card->caid & 0xff;
|
---|
825 | ecmbuf[2] = cur_er->prid >> 24;
|
---|
826 | ecmbuf[3] = cur_er->prid >> 16;
|
---|
827 | ecmbuf[4] = cur_er->prid >> 8;
|
---|
828 | ecmbuf[5] = cur_er->prid & 0xff;
|
---|
829 | ecmbuf[6] = card->id >> 24;
|
---|
830 | ecmbuf[7] = card->id >> 16;
|
---|
831 | ecmbuf[8] = card->id >> 8;
|
---|
832 | ecmbuf[9] = card->id & 0xff;
|
---|
833 | ecmbuf[10] = cur_er->srvid >> 8;
|
---|
834 | ecmbuf[11] = cur_er->srvid & 0xff;
|
---|
835 | ecmbuf[12] = cur_er->l & 0xff;
|
---|
836 | memcpy(ecmbuf + 13, cur_er->ecm, cur_er->l);
|
---|
837 |
|
---|
838 | cc->send_ecmtask = cur_er->idx;
|
---|
839 | reader[client[cs_idx].ridx].cc_currenthops = card->hop + 1;
|
---|
840 |
|
---|
841 | cs_log("%s sending ecm for sid %04X(%d) to card %08x, hop %d, ecmtask %d",
|
---|
842 | getprefix(), cur_er->srvid, cur_er->l, card->id, card->hop
|
---|
843 | + 1, cc->send_ecmtask);
|
---|
844 | cc_cmd_send(ecmbuf, cur_er->l + 13, MSG_CW_ECM); // send ecm
|
---|
845 |
|
---|
846 | //For EMM
|
---|
847 | if (!reader[client[cs_idx].ridx].audisabled) {
|
---|
848 | reader[client[cs_idx].ridx].card_system = get_cardsystem(card->caid);
|
---|
849 | memcpy(reader[client[cs_idx].ridx].hexserial, card->hexserial, sizeof(card->hexserial));
|
---|
850 | cs_debug_mask(D_EMM, "%s au info: caid %04X card system: %d serial: %s",
|
---|
851 | getprefix(), card->caid, reader[client[cs_idx].ridx].card_system, cs_hexdump(0, card->hexserial, 8));
|
---|
852 | }
|
---|
853 |
|
---|
854 | return 0;
|
---|
855 | } else {
|
---|
856 | if (is_auto_blocked)
|
---|
857 | cs_log("%s no suitable card on server (auto blocked)", getprefix());
|
---|
858 | else
|
---|
859 | cs_log("%s no suitable card on server", getprefix());
|
---|
860 | cur_er->rc = 0;
|
---|
861 | cur_er->rcEx = 0x27;
|
---|
862 | write_ecm_answer(&reader[client[cs_idx].ridx], fd_c2m, cur_er);
|
---|
863 | //cur_er->rc = 1;
|
---|
864 | //cur_er->rcEx = 0;
|
---|
865 | //cs_sleepms(300);
|
---|
866 | reader[client[cs_idx].ridx].last_s = reader[client[cs_idx].ridx].last_g;
|
---|
867 |
|
---|
868 | if (!force_resend_ecm) {
|
---|
869 | card = llist_itr_init(cc->cards, &itr);
|
---|
870 | while (card) {
|
---|
871 | if (card->caid == cur_er->caid) { // caid matches
|
---|
872 | LLIST_ITR sitr;
|
---|
873 | struct cc_srvid *srvid = llist_itr_init(card->badsids, &sitr);
|
---|
874 | while (srvid) {
|
---|
875 | if (sid_eq(srvid, &cur_srvid)) {
|
---|
876 | free(srvid);
|
---|
877 | srvid = llist_itr_remove(&sitr);
|
---|
878 | }
|
---|
879 | else
|
---|
880 | srvid = llist_itr_next(&sitr);
|
---|
881 | }
|
---|
882 | }
|
---|
883 | card = llist_itr_next(&itr);
|
---|
884 | }
|
---|
885 | }
|
---|
886 |
|
---|
887 | if (!reader[client[cs_idx].ridx].cc_disable_auto_block) {
|
---|
888 | cc_add_auto_blocked(cc->auto_blocked, cur_er->caid, cur_er->prid,
|
---|
889 | &cur_srvid);
|
---|
890 | }
|
---|
891 | reader[client[cs_idx].ridx].available = 1;
|
---|
892 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
893 | cc->current_ecm_cidx = 0;
|
---|
894 |
|
---|
895 | return -1;
|
---|
896 | }
|
---|
897 | }
|
---|
898 |
|
---|
899 | /*
|
---|
900 | static int cc_abort_user_ecms(){
|
---|
901 | int n, i;
|
---|
902 | time_t t;//, tls;
|
---|
903 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
904 |
|
---|
905 | t=time((time_t *)0);
|
---|
906 | for (i=1,n=1; i<CS_MAXPENDING; i++)
|
---|
907 | {
|
---|
908 | if ((t-client[cs_idx].ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
909 | (client[cs_idx].ecmtask[i].rc>=10)) // drop timeouts
|
---|
910 | {
|
---|
911 | client[cs_idx].ecmtask[i].rc=0;
|
---|
912 | }
|
---|
913 | int td=abs(1000*(client[cs_idx].ecmtask[i].tps.time-cc->found->tps.time)+client[cs_idx].ecmtask[i].tps.millitm-cc->found->tps.millitm);
|
---|
914 | if (client[cs_idx].ecmtask[i].rc>=10 && client[cs_idx].ecmtask[i].cidx==cc->found->cidx && &client[cs_idx].ecmtask[i]!=cc->found){
|
---|
915 | cs_log("aborting idx:%d caid:%04x client:%d timedelta:%d",client[cs_idx].ecmtask[i].idx,client[cs_idx].ecmtask[i].caid,client[cs_idx].ecmtask[i].cidx,td);
|
---|
916 | client[cs_idx].ecmtask[i].rc=0;
|
---|
917 | client[cs_idx].ecmtask[i].rcEx=7;
|
---|
918 | write_ecm_answer(&reader[client[cs_idx].ridx], fd_c2m, &client[cs_idx].ecmtask[i]);
|
---|
919 | }
|
---|
920 | }
|
---|
921 | return n;
|
---|
922 |
|
---|
923 | }
|
---|
924 | */
|
---|
925 |
|
---|
926 | static int cc_send_pending_emms() {
|
---|
927 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
928 |
|
---|
929 | LLIST_ITR itr;
|
---|
930 | uint8 *emmbuf = llist_itr_init(cc->pending_emms, &itr);
|
---|
931 | if (emmbuf) {
|
---|
932 | if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) { //Unlock by NOK or ECM ACK
|
---|
933 | return 0; //send later with cc_send_ecm
|
---|
934 | }
|
---|
935 | int size = emmbuf[11]+12;
|
---|
936 |
|
---|
937 | reader[client[cs_idx].ridx].available = 0;
|
---|
938 | cc->current_ecm_cidx = 0;
|
---|
939 | cc->just_logged_in = 0;
|
---|
940 | cs_ftime(&cc->ecm_time);
|
---|
941 |
|
---|
942 | cs_debug_mask(D_EMM, "%s emm send for card %08X", getprefix(), b2i(4, emmbuf+7));
|
---|
943 |
|
---|
944 | cc_cmd_send(emmbuf, size, MSG_EMM_ACK); // send emm
|
---|
945 | free(emmbuf);
|
---|
946 | llist_itr_remove(&itr);
|
---|
947 | return size;
|
---|
948 | }
|
---|
949 | return 0;
|
---|
950 | }
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * READER only:
|
---|
954 | * find card by hexserial
|
---|
955 | * */
|
---|
956 | struct cc_card *get_card_by_hexserial(uint8 *hexserial, uint16 caid) {
|
---|
957 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
958 | LLIST_ITR itr;
|
---|
959 | struct cc_card *card = llist_itr_init(cc->cards, &itr);
|
---|
960 | while (card) {
|
---|
961 | if (memcmp(card->hexserial, hexserial, 8) == 0 && card->caid == caid) { //found it!
|
---|
962 | return card;
|
---|
963 | }
|
---|
964 | card = llist_itr_next(&itr);
|
---|
965 | }
|
---|
966 | return NULL;
|
---|
967 | }
|
---|
968 |
|
---|
969 | /**
|
---|
970 | * EMM Procession
|
---|
971 | * Copied from http://85.17.209.13:6100/file/8ec3c0c5d257/systems/cardclient/cccam2.c
|
---|
972 | * ProcessEmm
|
---|
973 | * */
|
---|
974 | static int cc_send_emm(EMM_PACKET *ep) {
|
---|
975 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
976 | if (!cc || (client[cs_idx].pfd < 1) || !reader[client[cs_idx].ridx].tcp_connected) {
|
---|
977 | cs_log("%s server not init! ccinit=%d pfd=%d", getprefix(), cc ? 1 : 0,
|
---|
978 | client[cs_idx].pfd);
|
---|
979 | return 0;
|
---|
980 | }
|
---|
981 | if (reader[client[cs_idx].ridx].audisabled) {
|
---|
982 | cs_log("%s au is disabled", getprefix());
|
---|
983 | return 0;
|
---|
984 | }
|
---|
985 |
|
---|
986 |
|
---|
987 |
|
---|
988 | struct cc_card *emm_card = cc->current_card[ep->cidx].card;
|
---|
989 |
|
---|
990 | if (!emm_card || emm_card->caid != b2i(2, ep->caid)) {
|
---|
991 | emm_card = get_card_by_hexserial(ep->hexserial, b2i(2, ep->caid));
|
---|
992 | }
|
---|
993 |
|
---|
994 | if (!emm_card) { //Card for emm not found!
|
---|
995 | cs_log("%s emm for client %d not possible, no card found!", getprefix(), ep->cidx);
|
---|
996 | return 0;
|
---|
997 | }
|
---|
998 |
|
---|
999 | cs_debug_mask(D_EMM, "%s emm received for client %d caid %04X for card %08X", getprefix(), ep->cidx,
|
---|
1000 | b2i(2, ep->caid), emm_card->id);
|
---|
1001 |
|
---|
1002 | int size = ep->l+12;
|
---|
1003 | uint8 *emmbuf = malloc(size);
|
---|
1004 | memset(emmbuf, 0, size);
|
---|
1005 |
|
---|
1006 | // build ecm message
|
---|
1007 | emmbuf[0] = ep->caid[0];
|
---|
1008 | emmbuf[1] = ep->caid[1];
|
---|
1009 | emmbuf[2] = 0;
|
---|
1010 | emmbuf[3] = ep->provid[0];
|
---|
1011 | emmbuf[4] = ep->provid[1];
|
---|
1012 | emmbuf[5] = ep->provid[2];
|
---|
1013 | emmbuf[6] = ep->provid[3];
|
---|
1014 | emmbuf[7] = emm_card->id >> 24;
|
---|
1015 | emmbuf[8] = emm_card->id >> 16;
|
---|
1016 | emmbuf[9] = emm_card->id >> 8;
|
---|
1017 | emmbuf[10] = emm_card->id & 0xff;
|
---|
1018 | emmbuf[11] = ep->l;
|
---|
1019 | memcpy(emmbuf + 12, ep->emm, ep->l);
|
---|
1020 |
|
---|
1021 | llist_append(cc->pending_emms, emmbuf);
|
---|
1022 | cc_send_pending_emms();
|
---|
1023 | return 1;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | //SS: Hack
|
---|
1027 | static void cc_free_card(struct cc_card *card) {
|
---|
1028 | if (!card)
|
---|
1029 | return;
|
---|
1030 |
|
---|
1031 | if (card->provs)
|
---|
1032 | llist_destroy(card->provs);
|
---|
1033 | if (card->badsids)
|
---|
1034 | llist_destroy(card->badsids);
|
---|
1035 | if (card->goodsids)
|
---|
1036 | llist_destroy(card->goodsids);
|
---|
1037 | free(card);
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | static void freeCaidInfos(LLIST *caid_infos) {
|
---|
1041 | if (caid_infos) {
|
---|
1042 | LLIST_ITR itr;
|
---|
1043 | struct cc_caid_info *caid_info = llist_itr_init(caid_infos, &itr);
|
---|
1044 | while (caid_info) {
|
---|
1045 | llist_destroy(caid_info->provs);
|
---|
1046 | free(caid_info);
|
---|
1047 | caid_info = llist_itr_remove(&itr);
|
---|
1048 | }
|
---|
1049 | llist_destroy(caid_infos);
|
---|
1050 | }
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | /**
|
---|
1054 | * Server:
|
---|
1055 | * Adds a cccam-carddata buffer to the list of reported carddatas
|
---|
1056 | */
|
---|
1057 | static void cc_add_reported_carddata(LLIST *reported_carddatas, uint8 *buf,
|
---|
1058 | int len) {
|
---|
1059 | struct cc_reported_carddata *carddata = malloc(
|
---|
1060 | sizeof(struct cc_reported_carddata));
|
---|
1061 | uint8 *buf_copy = malloc(len);
|
---|
1062 | memcpy(buf_copy, buf, len);
|
---|
1063 | carddata->buf = buf_copy;
|
---|
1064 | carddata->len = len;
|
---|
1065 | llist_append(reported_carddatas, carddata);
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | static void cc_clear_reported_carddata(LLIST *reported_carddatas,
|
---|
1069 | int send_removed) {
|
---|
1070 | LLIST_ITR itr;
|
---|
1071 | struct cc_reported_carddata *carddata = llist_itr_init(reported_carddatas,
|
---|
1072 | &itr);
|
---|
1073 | while (carddata) {
|
---|
1074 | if (send_removed)
|
---|
1075 | cc_cmd_send(carddata->buf, carddata->len, MSG_CARD_REMOVED);
|
---|
1076 | free(carddata->buf);
|
---|
1077 | free(carddata);
|
---|
1078 | carddata = llist_itr_remove(&itr);
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | static void cc_free_reported_carddata(LLIST *reported_carddatas,
|
---|
1084 | int send_removed) {
|
---|
1085 | if (reported_carddatas) {
|
---|
1086 | cc_clear_reported_carddata(reported_carddatas, send_removed);
|
---|
1087 | llist_destroy(reported_carddatas);
|
---|
1088 | }
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | /**
|
---|
1092 | * Clears and free the cc datas
|
---|
1093 | */
|
---|
1094 | static void cc_free(struct cc_data *cc) {
|
---|
1095 | if (!cc)
|
---|
1096 | return;
|
---|
1097 | freeCaidInfos(cc->caid_infos);
|
---|
1098 |
|
---|
1099 | if (cc->cards) {
|
---|
1100 | LLIST_ITR itr;
|
---|
1101 | struct cc_card *card = llist_itr_init(cc->cards, &itr);
|
---|
1102 | while (card) {
|
---|
1103 | cc_free_card(card);
|
---|
1104 | card = llist_itr_remove(&itr);
|
---|
1105 | }
|
---|
1106 | llist_destroy(cc->cards);
|
---|
1107 | cc->cards = NULL;
|
---|
1108 | }
|
---|
1109 | cc_free_reported_carddata(cc->reported_carddatas, 0);
|
---|
1110 | cc_free_auto_blocked(cc->auto_blocked);
|
---|
1111 | if (cc->current_card)
|
---|
1112 | free(cc->current_card);
|
---|
1113 | if (cc->server_card)
|
---|
1114 | free(cc->server_card);
|
---|
1115 | if (cc->pending_emms) {
|
---|
1116 | LLIST_ITR itr;
|
---|
1117 | uint8 *ep = llist_itr_init(cc->pending_emms, &itr);
|
---|
1118 | while (ep) {
|
---|
1119 | free(ep);
|
---|
1120 | ep = llist_itr_remove(&itr);
|
---|
1121 | }
|
---|
1122 | llist_destroy(cc->pending_emms);
|
---|
1123 | cc->pending_emms = NULL;
|
---|
1124 | }
|
---|
1125 | free(cc);
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | static void fname_caidinfos(char *fname, int index) {
|
---|
1129 | sprintf(fname, "%s/caidinfos.%d", get_tmp_dir(), index);
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | static int checkCaidInfos(int index, long *lastSize) {
|
---|
1133 | char fname[40];
|
---|
1134 | fname_caidinfos(fname, index);
|
---|
1135 | struct stat st;
|
---|
1136 | stat(fname, &st);
|
---|
1137 | long current_size = st.st_size;
|
---|
1138 | int result = (current_size != *lastSize);
|
---|
1139 | cs_debug("checkCaidInfos %d: cur=%ld last=%ld", index, current_size,
|
---|
1140 | *lastSize);
|
---|
1141 | *lastSize = current_size;
|
---|
1142 | return result;
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | /**
|
---|
1146 | * Saves caidinfos to /tmp/caidinfos.<readerindex>
|
---|
1147 | */
|
---|
1148 | static void saveCaidInfos(int index, LLIST *caid_infos) {
|
---|
1149 | char fname[40];
|
---|
1150 | fname_caidinfos(fname, index);
|
---|
1151 | FILE *file = fopen(fname, "w");
|
---|
1152 | LLIST_ITR itr;
|
---|
1153 | LLIST_ITR itr_prov;
|
---|
1154 | int caid_count = 0;
|
---|
1155 | int prov_count = 0;
|
---|
1156 | struct cc_caid_info *caid_info = llist_itr_init(caid_infos, &itr);
|
---|
1157 | while (caid_info) {
|
---|
1158 | fwrite(&caid_info->caid, 1, sizeof(uint16), file);
|
---|
1159 | fwrite(&caid_info->hop, 1, sizeof(uint8), file);
|
---|
1160 |
|
---|
1161 | caid_count++;
|
---|
1162 | uint8 count = 0;
|
---|
1163 | uint8 *prov = llist_itr_init(caid_info->provs, &itr_prov);
|
---|
1164 | while (prov) {
|
---|
1165 | count++;
|
---|
1166 | prov = llist_itr_next(&itr_prov);
|
---|
1167 | }
|
---|
1168 | fwrite(&count, 1, sizeof(uint8), file);
|
---|
1169 | prov = llist_itr_init(caid_info->provs, &itr_prov);
|
---|
1170 | while (prov) {
|
---|
1171 | fwrite(prov, 1, 3, file);
|
---|
1172 | prov = llist_itr_next(&itr_prov);
|
---|
1173 | }
|
---|
1174 | prov_count += count;
|
---|
1175 |
|
---|
1176 | caid_info = llist_itr_next(&itr);
|
---|
1177 | }
|
---|
1178 | fclose(file);
|
---|
1179 | cs_debug("saveCaidInfos %d: CAIDS: %d PROVIDERS: %d", index, caid_count,
|
---|
1180 | prov_count);
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /**
|
---|
1184 | * Loads caidinfos from /tmp/caidinfos.<readerindex>
|
---|
1185 | */
|
---|
1186 | static LLIST *loadCaidInfos(int index) {
|
---|
1187 | char fname[40];
|
---|
1188 | fname_caidinfos(fname, index);
|
---|
1189 | FILE *file = fopen(fname, "r");
|
---|
1190 | if (!file)
|
---|
1191 | return NULL;
|
---|
1192 |
|
---|
1193 | int caid_count = 0;
|
---|
1194 | int prov_count = 0;
|
---|
1195 |
|
---|
1196 | uint16 caid = 0;
|
---|
1197 | uint8 hop = 0;
|
---|
1198 | LLIST *caid_infos = llist_create();
|
---|
1199 | do {
|
---|
1200 | if (fread(&caid, 1, sizeof(uint16), file) <= 0)
|
---|
1201 | break;
|
---|
1202 | if (fread(&hop, 1, sizeof(uint8), file) <= 0)
|
---|
1203 | break;
|
---|
1204 | caid_count++;
|
---|
1205 | uint8 count = 0;
|
---|
1206 | if (fread(&count, 1, sizeof(uint8), file) <= 0)
|
---|
1207 | break;
|
---|
1208 | struct cc_caid_info *caid_info = malloc(sizeof(struct cc_caid_info));
|
---|
1209 | caid_info->caid = caid;
|
---|
1210 | caid_info->hop = hop;
|
---|
1211 | caid_info->provs = llist_create();
|
---|
1212 | uint8 *prov;
|
---|
1213 | while (count > 0) {
|
---|
1214 | prov = malloc(3);
|
---|
1215 | if (fread(prov, 1, 3, file) <= 0)
|
---|
1216 | break;
|
---|
1217 | llist_append(caid_info->provs, prov);
|
---|
1218 | count--;
|
---|
1219 | prov_count++;
|
---|
1220 | }
|
---|
1221 | llist_append(caid_infos, caid_info);
|
---|
1222 | } while (1);
|
---|
1223 | fclose(file);
|
---|
1224 | cs_debug("loadCaidInfos %d: CAIDS: %d PROVIDERS: %d", index, caid_count,
|
---|
1225 | prov_count);
|
---|
1226 | return caid_infos;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | /**
|
---|
1230 | * Adds a new card to the caid_infos. Only caid/provs are used
|
---|
1231 | * Return 0 if caid already exists, 1 ist this is a new card or provider
|
---|
1232 | */
|
---|
1233 | static int add_card_to_caidinfo(struct cc_data *cc, struct cc_card *card) {
|
---|
1234 | int doSaveCaidInfos = 0;
|
---|
1235 | LLIST_ITR itr;
|
---|
1236 | struct cc_caid_info *caid_info = llist_itr_init(cc->caid_infos, &itr);
|
---|
1237 | while (caid_info) {
|
---|
1238 | if (caid_info->caid == card->caid && caid_info->hop == card->hop)
|
---|
1239 | if (llist_count(caid_info->provs) < CS_MAXPROV)
|
---|
1240 | break;
|
---|
1241 | caid_info = llist_itr_next(&itr);
|
---|
1242 | }
|
---|
1243 | if (!caid_info) {
|
---|
1244 | caid_info = malloc(sizeof(struct cc_caid_info));
|
---|
1245 | caid_info->caid = card->caid;
|
---|
1246 | caid_info->provs = llist_create();
|
---|
1247 | caid_info->hop = card->hop;
|
---|
1248 | llist_append(cc->caid_infos, caid_info);
|
---|
1249 | doSaveCaidInfos = 1;
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | uint8 *prov_info;
|
---|
1253 | uint8 *prov_card;
|
---|
1254 | LLIST_ITR itr_info;
|
---|
1255 | LLIST_ITR itr_card;
|
---|
1256 | prov_card = llist_itr_init(card->provs, &itr_card);
|
---|
1257 | while (prov_card) {
|
---|
1258 | prov_info = llist_itr_init(caid_info->provs, &itr_info);
|
---|
1259 | while (prov_info) {
|
---|
1260 | if (b2i(3, prov_info) == b2i(3, prov_card))
|
---|
1261 | break;
|
---|
1262 | prov_info = llist_itr_next(&itr_info);
|
---|
1263 | }
|
---|
1264 | if (!prov_info) {
|
---|
1265 | uint8 *prov_new = malloc(3);
|
---|
1266 | memcpy(prov_new, prov_card, 3);
|
---|
1267 | llist_append(caid_info->provs, prov_new);
|
---|
1268 | doSaveCaidInfos = 1;
|
---|
1269 | }
|
---|
1270 | prov_card = llist_itr_next(&itr_card);
|
---|
1271 | }
|
---|
1272 | return doSaveCaidInfos;
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | static void cc_clear_current_card(struct cc_data *cc, int cidx) {
|
---|
1276 | memset(&cc->current_card[cidx], 0, sizeof(struct cc_current_card));
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | static int cc_remove_current_card(struct cc_data *cc, struct cc_card *card) {
|
---|
1280 | int i, c = 0;
|
---|
1281 | for (i = 0; i<CS_MAXPID; i++) {
|
---|
1282 | if (cc->current_card[i].card == card) {
|
---|
1283 | cc_clear_current_card(cc, i);
|
---|
1284 | c++;
|
---|
1285 | }
|
---|
1286 | }
|
---|
1287 | return c;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | static struct cc_current_card *cc_find_current_card(struct cc_data *cc, struct cc_card *card) {
|
---|
1291 | int i;
|
---|
1292 | for (i = 0; i<CS_MAXPID; i++) {
|
---|
1293 | if (cc->current_card[i].card == card) {
|
---|
1294 | return &cc->current_card[i];
|
---|
1295 | }
|
---|
1296 | }
|
---|
1297 | return NULL;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | static void rebuild_caidinfos(struct cc_data *cc) {
|
---|
1301 | freeCaidInfos(cc->caid_infos);
|
---|
1302 | cc->caid_infos = llist_create();
|
---|
1303 | LLIST_ITR itr;
|
---|
1304 | struct cc_card *card = llist_itr_init(cc->cards, &itr);
|
---|
1305 | while (card) {
|
---|
1306 | add_card_to_caidinfo(cc, card);
|
---|
1307 | card = llist_itr_next(&itr);
|
---|
1308 | }
|
---|
1309 | cc->needs_rebuild_caidinfo = 0;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | static void cleanup_old_cards(struct cc_data *cc) {
|
---|
1313 | time_t clean_time = time((time_t) 0) - 60 * 60 * 48; //TODO: Timeout old cards 60*60*48=48h Config
|
---|
1314 | LLIST_ITR itr;
|
---|
1315 | struct cc_card *card = llist_itr_init(cc->cards, &itr);
|
---|
1316 | while (card) {
|
---|
1317 | if (card->time < clean_time) {
|
---|
1318 | //cs_log("cccam: old card removed %08x, count %d", card->id,
|
---|
1319 | // llist_count(cc->cards));
|
---|
1320 | cc_remove_current_card(cc, card);
|
---|
1321 | cc_free_card(card);
|
---|
1322 | card = llist_itr_remove(&itr);
|
---|
1323 | } else
|
---|
1324 | card = llist_itr_next(&itr);
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | static int caid_filtered(int ridx, int caid) {
|
---|
1329 | int defined = 0;
|
---|
1330 | int i;
|
---|
1331 | for (i = 0; i < CS_MAXREADERCAID; i++) {
|
---|
1332 | if (reader[ridx].caid[i]) {
|
---|
1333 | if (reader[ridx].caid[i] == caid)
|
---|
1334 | return 0;
|
---|
1335 | defined = 1;
|
---|
1336 | }
|
---|
1337 | }
|
---|
1338 | return defined;
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | static int is_null_dcw(uint8 *dcw)
|
---|
1342 | {
|
---|
1343 | int i;
|
---|
1344 | for (i = 0; i < 15; i++)
|
---|
1345 | if (dcw[i])
|
---|
1346 | return 0;
|
---|
1347 | return 1;
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 | /*static int is_dcw_corrupted(uchar *dcw)
|
---|
1351 | {
|
---|
1352 | int i;
|
---|
1353 | int c, cs;
|
---|
1354 |
|
---|
1355 | for (i=0; i<16; i+=4)
|
---|
1356 | {
|
---|
1357 | c = (dcw[i] + dcw[i+1] + dcw[i+2]) & 0xFF;
|
---|
1358 | cs = dcw[i+3];
|
---|
1359 | if (cs!=c) return (1);
|
---|
1360 | }
|
---|
1361 | return 0;
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | static void fix_dcw(uchar *dcw)
|
---|
1365 | {
|
---|
1366 | int i;
|
---|
1367 | for (i=0; i<16; i+=4)
|
---|
1368 | {
|
---|
1369 | dcw[i+3] = (dcw[i] + dcw[i+1] + dcw[i+2]) & 0xFF;
|
---|
1370 | }
|
---|
1371 | }*/
|
---|
1372 |
|
---|
1373 | static void cc_idle() {
|
---|
1374 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
1375 | if (!reader[client[cs_idx].ridx].tcp_connected)
|
---|
1376 | return;
|
---|
1377 |
|
---|
1378 | cs_debug("%s IDLE", getprefix());
|
---|
1379 | if (cc->answer_on_keepalive + 55 < time(NULL)) {
|
---|
1380 | cc_cmd_send(NULL, 0, MSG_KEEPALIVE);
|
---|
1381 | cs_debug("cccam: keepalive");
|
---|
1382 | cc->answer_on_keepalive = time(NULL);
|
---|
1383 | }
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | static int cc_parse_msg(uint8 *buf, int l) {
|
---|
1387 | int ret = buf[1];
|
---|
1388 | struct cc_data *cc;
|
---|
1389 |
|
---|
1390 | if (!client[cs_idx].is_server)
|
---|
1391 | cc = reader[client[cs_idx].ridx].cc;
|
---|
1392 | else
|
---|
1393 | cc = client[cs_idx].cc;
|
---|
1394 |
|
---|
1395 | cs_debug("%s parse_msg=%d", getprefix(), buf[1]);
|
---|
1396 |
|
---|
1397 | uint8 *data = buf+4;
|
---|
1398 | memcpy(&cc->receive_buffer, data, l-4);
|
---|
1399 |
|
---|
1400 | switch (buf[1]) {
|
---|
1401 | case MSG_CLI_DATA:
|
---|
1402 | cs_debug("cccam: client data ack");
|
---|
1403 | break;
|
---|
1404 | case MSG_SRV_DATA:
|
---|
1405 | l -= 4;
|
---|
1406 | cs_log("%s MSG_SRV_DATA (payload=%d, hex=%02X)", getprefix(), l, l);
|
---|
1407 | data = (uint8*)&cc->receive_buffer;
|
---|
1408 |
|
---|
1409 | if (l == 0x48) { //72 bytes: normal server data
|
---|
1410 | memcpy(cc->peer_node_id, data, 8);
|
---|
1411 | memcpy(cc->peer_version, data+8, 8);
|
---|
1412 |
|
---|
1413 | memcpy(cc->cmd0b_aeskey, cc->peer_node_id, 8);
|
---|
1414 | memcpy(cc->cmd0b_aeskey + 8, cc->peer_version, 8);
|
---|
1415 | cs_log("%s srv %s running v%s (%s)", getprefix(),
|
---|
1416 | cs_hexdump(0, cc->peer_node_id, 8), data + 8, data + 40);
|
---|
1417 | cc->cmd05_mode = MODE_PLAIN;
|
---|
1418 | //
|
---|
1419 | //Keyoffset is payload-size:
|
---|
1420 | //
|
---|
1421 | } else if (l >= 0x00 && l <= 0x0F) {
|
---|
1422 | cc->cmd05_offset = l;
|
---|
1423 | //
|
---|
1424 | //16..43 bytes: RC4 encryption:
|
---|
1425 | //
|
---|
1426 | } else if ((l >= 0x10 && l <= 0x1f) || (l >= 0x24 && l <= 0x2b)) {
|
---|
1427 | cc_init_crypt(&cc->cmd05_cryptkey, data, l);
|
---|
1428 | cc->cmd05_mode = MODE_RC4_CRYPT;
|
---|
1429 | //
|
---|
1430 | //32 bytes: set AES128 key for CMD_05, Key=16 bytes offset keyoffset
|
---|
1431 | //
|
---|
1432 | } else if (l == 0x20) {
|
---|
1433 | memcpy(cc->cmd05_aeskey, data+cc->cmd05_offset, 16);
|
---|
1434 | cc->cmd05_mode = MODE_AES;
|
---|
1435 | //
|
---|
1436 | //33 bytes: xor-algo mit payload-bytes, offset keyoffset
|
---|
1437 | //
|
---|
1438 | } else if (l == 0x21) {
|
---|
1439 | cc_init_crypt(&cc->cmd05_cryptkey, data+cc->cmd05_offset, l);
|
---|
1440 | cc->cmd05_mode = MODE_CC_CRYPT;
|
---|
1441 | //
|
---|
1442 | //34 bytes: cmd_05 plain back
|
---|
1443 | //
|
---|
1444 | } else if (l == 0x22) {
|
---|
1445 | cc->cmd05_mode = MODE_PLAIN;
|
---|
1446 | //
|
---|
1447 | //35 bytes: Unknown!! 2 256 byte keys exchange
|
---|
1448 | //
|
---|
1449 | } else if (l == 0x23) {
|
---|
1450 | cc->cmd05_mode = MODE_UNKNOWN;
|
---|
1451 | cc_cycle_connection(); //Absolute unknown handling!
|
---|
1452 | //
|
---|
1453 | //44 bytes: set aes128 key, Key=16 bytes [Offset=len(password)]
|
---|
1454 | //
|
---|
1455 | } else if (l == 0x2c) {
|
---|
1456 | memcpy(cc->cmd05_aeskey, data+strlen(reader[client[cs_idx].ridx].r_pwd), 16);
|
---|
1457 | cc->cmd05_mode = MODE_AES;
|
---|
1458 | //
|
---|
1459 | //45 bytes: set aes128 key, Key=16 bytes [Offset=len(username)]
|
---|
1460 | //
|
---|
1461 | } else if (l == 0x2d) {
|
---|
1462 | memcpy(cc->cmd05_aeskey, data+strlen(reader[client[cs_idx].ridx].r_usr), 16);
|
---|
1463 | cc->cmd05_mode = MODE_AES;
|
---|
1464 | //
|
---|
1465 | //Unknown!!
|
---|
1466 | //
|
---|
1467 | } else {
|
---|
1468 | cs_log("%s received improper MSG_SRV_DATA! No change to current mode, mode=%d", getprefix(), cc->cmd05_mode);
|
---|
1469 | break;
|
---|
1470 | }
|
---|
1471 | cs_debug_mask(D_TRACE, "%s MSG_SRV_DATA MODE=%s, len=%d", getprefix(), cmd05_mode_name[cc->cmd05_mode], l);
|
---|
1472 |
|
---|
1473 | break;
|
---|
1474 | case MSG_NEW_CARD: {
|
---|
1475 | int i = 0;
|
---|
1476 | if (buf[14] > reader[client[cs_idx].ridx].cc_maxhop)
|
---|
1477 | break;
|
---|
1478 |
|
---|
1479 | if (caid_filtered(client[cs_idx].ridx, b2i(2, buf + 12)))
|
---|
1480 | break;
|
---|
1481 |
|
---|
1482 | struct cc_card *card = malloc(sizeof(struct cc_card));
|
---|
1483 | if (!card)
|
---|
1484 | break;
|
---|
1485 |
|
---|
1486 | reader[client[cs_idx].ridx].tcp_connected = 2; //we have card
|
---|
1487 | reader[client[cs_idx].ridx].card_status = CARD_INSERTED;
|
---|
1488 | memset(card, 0, sizeof(struct cc_card));
|
---|
1489 |
|
---|
1490 | card->provs = llist_create();
|
---|
1491 | card->badsids = llist_create();
|
---|
1492 | card->goodsids = llist_create();
|
---|
1493 | card->id = b2i(4, buf + 4);
|
---|
1494 | card->sub_id = b2i(3, buf + 9);
|
---|
1495 | card->caid = b2i(2, buf + 12);
|
---|
1496 | card->hop = buf[14];
|
---|
1497 | card->maxdown = buf[15];
|
---|
1498 | memcpy(card->hexserial, buf + 16, 8); //HEXSERIAL!!
|
---|
1499 |
|
---|
1500 | //cs_debug("cccam: card %08x added, caid %04X, hop %d, key %s, count %d",
|
---|
1501 | // card->id, card->caid, card->hop, cs_hexdump(0, card->hexserial, 8),
|
---|
1502 | // llist_count(cc->cards));
|
---|
1503 |
|
---|
1504 | for (i = 0; i < buf[24]; i++) { // providers
|
---|
1505 | uint8 *prov = malloc(3);
|
---|
1506 | if (prov) {
|
---|
1507 | memcpy(prov, buf + 25 + (7 * i), 3);
|
---|
1508 | cs_debug(" prov %d, %06x", i + 1, b2i(3, prov));
|
---|
1509 |
|
---|
1510 | llist_append(card->provs, prov);
|
---|
1511 | }
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | //SS: Hack:
|
---|
1515 | //Check if we already have this card:
|
---|
1516 | LLIST_ITR itr;
|
---|
1517 | struct cc_card *old_card = llist_itr_init(cc->cards, &itr);
|
---|
1518 | while (old_card) {
|
---|
1519 | if (old_card->id == card->id) { //we aready have this card, delete it
|
---|
1520 | cc_free_card(card);
|
---|
1521 | old_card->time = time((time_t) 0);
|
---|
1522 | return 0;
|
---|
1523 | }
|
---|
1524 | old_card = llist_itr_next(&itr);
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | card->time = time((time_t) 0);
|
---|
1528 | llist_append(cc->cards, card);
|
---|
1529 |
|
---|
1530 | //build own card/provs list:
|
---|
1531 | cc->needs_rebuild_caidinfo++;
|
---|
1532 | if (cc->needs_rebuild_caidinfo > CC_CAIDINFO_REBUILD) {
|
---|
1533 | cleanup_old_cards(cc);
|
---|
1534 | rebuild_caidinfos(cc);
|
---|
1535 | saveCaidInfos(client[cs_idx].ridx, cc->caid_infos);
|
---|
1536 | } else {
|
---|
1537 | if (cc->caid_infos && checkCaidInfos(client[cs_idx].ridx, &cc->caid_size)) {
|
---|
1538 | freeCaidInfos(cc->caid_infos);
|
---|
1539 | cc->caid_infos = NULL;
|
---|
1540 | }
|
---|
1541 | if (!cc->caid_infos)
|
---|
1542 | cc->caid_infos = loadCaidInfos(client[cs_idx].ridx);
|
---|
1543 | if (!cc->caid_infos)
|
---|
1544 | cc->caid_infos = llist_create();
|
---|
1545 |
|
---|
1546 | if (add_card_to_caidinfo(cc, card))
|
---|
1547 | saveCaidInfos(client[cs_idx].ridx, cc->caid_infos);
|
---|
1548 | }
|
---|
1549 | uint8 *prov = llist_itr_init(card->provs, &itr);
|
---|
1550 | while (prov) {
|
---|
1551 | cc_remove_from_auto_blocked(cc->auto_blocked, card->caid, b2i(3,
|
---|
1552 | prov));
|
---|
1553 | prov = llist_itr_next(&itr);
|
---|
1554 | }
|
---|
1555 | //SS: Hack end
|
---|
1556 | }
|
---|
1557 | break;
|
---|
1558 | case MSG_CARD_REMOVED: {
|
---|
1559 | struct cc_card *card;
|
---|
1560 | LLIST_ITR itr;
|
---|
1561 |
|
---|
1562 | card = llist_itr_init(cc->cards, &itr);
|
---|
1563 | while (card) {
|
---|
1564 | if (card->id == b2i(4, buf + 4)) {// && card->sub_id == b2i (3, buf + 9)) {
|
---|
1565 | //cs_debug("cccam: card %08x removed, caid %04X, count %d",
|
---|
1566 | // card->id, card->caid, llist_count(cc->cards));
|
---|
1567 | struct cc_card *next_card = llist_itr_remove(&itr);
|
---|
1568 | struct cc_current_card *current_card;
|
---|
1569 | while ((current_card = cc_find_current_card(cc, card))) {
|
---|
1570 | cs_debug_mask(D_TRACE, "%s current card %08x removed!", getprefix(), card->id);
|
---|
1571 |
|
---|
1572 | current_card->card = NULL;
|
---|
1573 |
|
---|
1574 | }
|
---|
1575 | cc_free_card(card);
|
---|
1576 |
|
---|
1577 | card = next_card;
|
---|
1578 | //break;
|
---|
1579 | } else {
|
---|
1580 | card = llist_itr_next(&itr);
|
---|
1581 | }
|
---|
1582 | }
|
---|
1583 | cc->needs_rebuild_caidinfo++;
|
---|
1584 |
|
---|
1585 | ret = 0;
|
---|
1586 | }
|
---|
1587 | break;
|
---|
1588 |
|
---|
1589 | case MSG_CW_NOK1:
|
---|
1590 | case MSG_CW_NOK2:
|
---|
1591 | if (l > 4) {
|
---|
1592 | //Received NOK with payload:
|
---|
1593 | cs_log("%s %s", getprefix(), (char*) buf+4);
|
---|
1594 |
|
---|
1595 | //Check for PARTNER connection:
|
---|
1596 | if (!cc->is_oscam_cccam && strncmp((char*)buf+4, "PARTNER:",8) == 0) {
|
---|
1597 | //When Data starts with "PARTNER:" we have an Oscam-cccam-compatible client/server!
|
---|
1598 | cc->is_oscam_cccam = 1;
|
---|
1599 | sprintf((char*)buf, "PARTNER: OSCam v%s, build #%s (%s)", CS_VERSION, CS_SVN_VERSION, CS_OSTYPE);
|
---|
1600 | cc_cmd_send(buf, strlen((char*)buf)+1, MSG_CW_NOK1);
|
---|
1601 | }
|
---|
1602 | return 0;
|
---|
1603 | }
|
---|
1604 |
|
---|
1605 | if (client[cs_idx].is_server) //for reader only
|
---|
1606 | return 0;
|
---|
1607 |
|
---|
1608 | if (cc->just_logged_in)
|
---|
1609 | return -1; // reader restart needed
|
---|
1610 |
|
---|
1611 | if (cc->current_ecm_cidx) {
|
---|
1612 | struct cc_current_card *current_card = &cc->current_card[cc->current_ecm_cidx];
|
---|
1613 |
|
---|
1614 | cs_debug_mask(D_TRACE, "%s cw nok (%d), sid = %04X(%d)", getprefix(), buf[1],
|
---|
1615 | current_card->srvid.sid, current_card->srvid.ecmlen);
|
---|
1616 | struct cc_card *card = current_card->card;
|
---|
1617 | if (card) {
|
---|
1618 | if (!is_good_sid(card, ¤t_card->srvid)) {
|
---|
1619 | add_sid_block(card, ¤t_card->srvid);
|
---|
1620 | current_card->card = NULL;
|
---|
1621 | }
|
---|
1622 | else
|
---|
1623 | remove_good_sid(card, ¤t_card->srvid);
|
---|
1624 | }
|
---|
1625 | else
|
---|
1626 | current_card = NULL;
|
---|
1627 | }
|
---|
1628 | reader[client[cs_idx].ridx].available = 1;
|
---|
1629 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1630 |
|
---|
1631 | if (!reader[client[cs_idx].ridx].cc_disable_retry_ecm) {
|
---|
1632 | cc->crc++;
|
---|
1633 | }
|
---|
1634 | cc_send_ecm(NULL, NULL);
|
---|
1635 |
|
---|
1636 | ret = 0;
|
---|
1637 |
|
---|
1638 | break;
|
---|
1639 | case MSG_CW_ECM:
|
---|
1640 | cc->just_logged_in = 0;
|
---|
1641 | if (client[cs_idx].is_server) { //SERVER:
|
---|
1642 | ECM_REQUEST *er;
|
---|
1643 |
|
---|
1644 | memset(cc->server_card, 0, sizeof(struct cc_card));
|
---|
1645 | cc->server_card->id = buf[10] << 24 | buf[11] << 16 | buf[12] << 8 | buf[13];
|
---|
1646 | cc->server_card->caid = b2i(2, data);
|
---|
1647 |
|
---|
1648 | if ((er = get_ecmtask())) {
|
---|
1649 | er->caid = b2i(2, buf + 4);
|
---|
1650 | er->srvid = b2i(2, buf + 14);
|
---|
1651 | er->l = buf[16];
|
---|
1652 | memcpy(er->ecm, buf + 17, er->l);
|
---|
1653 | er->prid = b2i(4, buf + 6);
|
---|
1654 | cc->server_ecm_pending = 1;
|
---|
1655 | get_cw(er);
|
---|
1656 | cs_debug_mask(
|
---|
1657 | D_TRACE,
|
---|
1658 | "%s ECM request from client: caid %04x srvid %04x(%d) prid %06x",
|
---|
1659 | getprefix(), er->caid, er->srvid, er->l, er->prid);
|
---|
1660 | } else
|
---|
1661 | cs_debug_mask(D_TRACE, "%s NO ECMTASK!!!!", getprefix());
|
---|
1662 |
|
---|
1663 | } else { //READER:
|
---|
1664 | struct cc_current_card *current_card = &cc->current_card[cc->current_ecm_cidx];
|
---|
1665 | struct cc_card *card = current_card->card;
|
---|
1666 | if (card) {
|
---|
1667 | cc_cw_crypt(buf + 4, card->id);
|
---|
1668 | memcpy(cc->dcw, buf + 4, 16);
|
---|
1669 | cc_crypt(&cc->block[DECRYPT], buf + 4, l - 4, ENCRYPT); // additional crypto step
|
---|
1670 |
|
---|
1671 | if (is_null_dcw(cc->dcw)) {
|
---|
1672 | cs_log("%s null dcw received! sid=%04X(%d)", getprefix(),
|
---|
1673 | current_card->srvid.sid, current_card->srvid.ecmlen);
|
---|
1674 | add_sid_block(card, ¤t_card->srvid);
|
---|
1675 | current_card->card = NULL;
|
---|
1676 | cc->crc++; //So ecm could retryied
|
---|
1677 | buf[1] = MSG_CW_NOK1; //So it's really handled like a nok!
|
---|
1678 | }
|
---|
1679 | else {
|
---|
1680 | cc->recv_ecmtask = cc->send_ecmtask;
|
---|
1681 | int i = 0;
|
---|
1682 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
1683 | if (client[cs_idx].ecmtask[i].idx == cc->send_ecmtask)
|
---|
1684 | client[cs_idx].ecmtask[i].rc = 99; //Mark as received
|
---|
1685 | }
|
---|
1686 | cs_debug_mask(D_TRACE, "%s cws: %d %s", getprefix(),
|
---|
1687 | cc->send_ecmtask, cs_hexdump(0, cc->dcw, 16));
|
---|
1688 | add_good_sid(card, ¤t_card->srvid);
|
---|
1689 | }
|
---|
1690 | }
|
---|
1691 | else {
|
---|
1692 | cs_log("%s warning: ECM-CWS respond by CCCam server without current card!", getprefix());
|
---|
1693 | current_card = NULL;
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 | cc->current_ecm_cidx = 0;
|
---|
1697 | reader[client[cs_idx].ridx].available = 1;
|
---|
1698 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1699 |
|
---|
1700 | //cc_abort_user_ecms();
|
---|
1701 |
|
---|
1702 | cc_send_ecm(NULL, NULL);
|
---|
1703 |
|
---|
1704 | if (cc->max_ecms)
|
---|
1705 | cc->ecm_counter++;
|
---|
1706 | }
|
---|
1707 | ret = 0;
|
---|
1708 | break;
|
---|
1709 | case MSG_KEEPALIVE:
|
---|
1710 | cc->just_logged_in = 0;
|
---|
1711 | if (!client[cs_idx].is_server) {
|
---|
1712 | cs_debug("cccam: keepalive ack");
|
---|
1713 | } else {
|
---|
1714 | //Checking if last answer is one minute ago:
|
---|
1715 | if (cc->answer_on_keepalive+55 < time(NULL)) {
|
---|
1716 | cc_cmd_send(NULL, 0, MSG_KEEPALIVE);
|
---|
1717 | cs_debug("cccam: keepalive");
|
---|
1718 | cc->answer_on_keepalive = time(NULL);
|
---|
1719 | }
|
---|
1720 | }
|
---|
1721 | break;
|
---|
1722 | case MSG_CMD_05:
|
---|
1723 | if (!client[cs_idx].is_server) {
|
---|
1724 | cc->just_logged_in = 0;
|
---|
1725 | l = l - 4;//Header Length=4 Byte
|
---|
1726 |
|
---|
1727 | cs_log("%s MSG_CMD_05 recvd, payload length=%d mode=%d", getprefix(), l, cc->cmd05_mode);
|
---|
1728 | cc->cmd05_active = 1;
|
---|
1729 | cc->cmd05_data_len = l;
|
---|
1730 | memcpy(&cc->cmd05_data, buf+4, l);
|
---|
1731 | if (!cc->current_ecm_cidx)
|
---|
1732 | send_cmd05_answer();
|
---|
1733 | cc->current_ecm_cidx = 0; //After CMD_05 is always a NOK! So retry ECM
|
---|
1734 | }
|
---|
1735 | ret = 0;
|
---|
1736 | break;
|
---|
1737 | case MSG_CMD_0B: {
|
---|
1738 | // by Project:Keynation
|
---|
1739 | /*cs_log("%s MSG_CMD_0B received, cycle connection (payload=%d)!", getprefix(), l-4);*/
|
---|
1740 | cs_debug_mask(D_TRACE, "%s MSG_CMD_0B received (payload=%d)!", getprefix(), l-4);
|
---|
1741 | cs_ddump(buf, l, "%s content: len=%d", getprefix(), l);
|
---|
1742 |
|
---|
1743 | AES_KEY key;
|
---|
1744 | uint8 aeskey[16];
|
---|
1745 | uint8 out[16];
|
---|
1746 |
|
---|
1747 | memcpy(aeskey, cc->cmd0b_aeskey, 16);
|
---|
1748 | memset(&key, 0, sizeof(key));
|
---|
1749 |
|
---|
1750 | cs_ddump(aeskey, 16, "%s CMD_0B AES key:", getprefix());
|
---|
1751 | cs_ddump(buf + 4, 16, "%s CMD_0B received data:", getprefix());
|
---|
1752 |
|
---|
1753 | AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
|
---|
1754 | AES_encrypt((unsigned char *) buf + 4, (unsigned char *) &out, &key);
|
---|
1755 |
|
---|
1756 | cs_debug_mask(D_TRACE, "%s sending CMD_0B! ", getprefix());
|
---|
1757 | cs_ddump(out, 16, "%s CMD_0B out:", getprefix());
|
---|
1758 | cc_cmd_send(out, 16, MSG_CMD_0B);
|
---|
1759 |
|
---|
1760 | ret = 0;
|
---|
1761 | break;
|
---|
1762 | }
|
---|
1763 | case MSG_EMM_ACK: {
|
---|
1764 | cc->just_logged_in = 0;
|
---|
1765 | if (client[cs_idx].is_server) { //EMM Request received
|
---|
1766 | cc_cmd_send(NULL, 0, MSG_EMM_ACK); //Send back ACK
|
---|
1767 | if (l > 4) {
|
---|
1768 | cs_debug_mask(D_EMM, "%s EMM Request received!", getprefix());
|
---|
1769 |
|
---|
1770 | int au = client[cs_idx].au;
|
---|
1771 | if ((au < 0) || (au > CS_MAXREADER)) {
|
---|
1772 | cs_debug_mask(D_EMM, "%s EMM Request discarded because au is not assigned to an reader!", getprefix());
|
---|
1773 | return 0;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | EMM_PACKET *emm = malloc(sizeof(EMM_PACKET));
|
---|
1777 | memset(emm, 0, sizeof(EMM_PACKET));
|
---|
1778 | emm->caid[0] = buf[4];
|
---|
1779 | emm->caid[1] = buf[5];
|
---|
1780 | emm->provid[0] = buf[7];
|
---|
1781 | emm->provid[1] = buf[8];
|
---|
1782 | emm->provid[2] = buf[9];
|
---|
1783 | emm->provid[3] = buf[10];
|
---|
1784 | //emm->hexserial[0] = buf[11];
|
---|
1785 | //emm->hexserial[1] = buf[12];
|
---|
1786 | //emm->hexserial[2] = buf[13];
|
---|
1787 | //emm->hexserial[3] = buf[14];
|
---|
1788 | emm->l = buf[15];
|
---|
1789 | memcpy(emm->emm, buf + 16, emm->l);
|
---|
1790 | //emm->type = UNKNOWN;
|
---|
1791 | //emm->cidx = cs_idx;
|
---|
1792 | do_emm(emm);
|
---|
1793 | free(emm);
|
---|
1794 | }
|
---|
1795 | } else { //Our EMM Request Ack!
|
---|
1796 | cs_debug_mask(D_EMM, "%s EMM ACK!", getprefix());
|
---|
1797 | reader[client[cs_idx].ridx].available = 1;
|
---|
1798 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1799 | cc->current_ecm_cidx = 0;
|
---|
1800 | cc_send_ecm(NULL, NULL);
|
---|
1801 | }
|
---|
1802 | ret = 0;
|
---|
1803 | break;
|
---|
1804 | }
|
---|
1805 | default:
|
---|
1806 | cs_ddump(buf, l, "%s unhandled msg: %d len=%d", getprefix(), buf[1], l);
|
---|
1807 | break;
|
---|
1808 | }
|
---|
1809 |
|
---|
1810 | if (cc->max_ecms && (cc->ecm_counter > cc->max_ecms)) {
|
---|
1811 | cs_log("%s max ecms (%d) reached, cycle connection!", getprefix(),
|
---|
1812 | cc->max_ecms);
|
---|
1813 | cc_cycle_connection();
|
---|
1814 | cc_send_ecm(NULL, NULL);
|
---|
1815 | ret = 0;
|
---|
1816 | }
|
---|
1817 | return ret;
|
---|
1818 | }
|
---|
1819 |
|
---|
1820 | static int cc_recv_chk(uchar *dcw, int *rc, uchar *buf) {
|
---|
1821 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
1822 |
|
---|
1823 | if (buf[1] == MSG_CW_ECM) {
|
---|
1824 | memcpy(dcw, cc->dcw, 16);
|
---|
1825 | cs_debug("cccam: recv chk - MSG_CW %d - %s", cc->recv_ecmtask,
|
---|
1826 | cs_hexdump(0, dcw, 16));
|
---|
1827 | *rc = 1;
|
---|
1828 | return (cc->recv_ecmtask);
|
---|
1829 | } else if ((buf[1] == (MSG_CW_NOK1)) || (buf[1] == (MSG_CW_NOK2))) {
|
---|
1830 | //memset(dcw, 0, 16);
|
---|
1831 | //return *rc = 0;
|
---|
1832 | return -1;
|
---|
1833 | }
|
---|
1834 |
|
---|
1835 | return (-1);
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | static void cc_send_dcw(ECM_REQUEST *er) {
|
---|
1839 | uchar buf[16];
|
---|
1840 | struct cc_data *cc;
|
---|
1841 |
|
---|
1842 | memset(buf, 0, sizeof(buf));
|
---|
1843 |
|
---|
1844 | if (er->rc <= 3) {
|
---|
1845 | cc = client[cs_idx].cc;
|
---|
1846 | memcpy(buf, er->cw, sizeof(buf));
|
---|
1847 | cs_debug_mask(D_TRACE, "%s send cw: %s cpti: %d", getprefix(),
|
---|
1848 | cs_hexdump(0, buf, 16), er->cpti);
|
---|
1849 | cc_cw_crypt(buf, cc->server_card->id);
|
---|
1850 | cc_cmd_send(buf, 16, MSG_CW_ECM);
|
---|
1851 | cc_crypt(&cc->block[ENCRYPT], buf, 16, ENCRYPT); // additional crypto step
|
---|
1852 | cc->server_ecm_pending = 0;
|
---|
1853 | } else {
|
---|
1854 | cs_debug_mask(D_TRACE, "%s send cw: NOK cpti: %d", getprefix(),
|
---|
1855 | er->cpti);
|
---|
1856 | cc_cmd_send(NULL, 0, MSG_CW_NOK1);
|
---|
1857 | }
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 | int cc_recv(uchar *buf, int l) {
|
---|
1861 | int n;
|
---|
1862 | uchar *cbuf;
|
---|
1863 | struct cc_data *cc;
|
---|
1864 |
|
---|
1865 | if (!client[cs_idx].is_server)
|
---|
1866 | cc = reader[client[cs_idx].ridx].cc;
|
---|
1867 | else
|
---|
1868 | cc = client[cs_idx].cc;
|
---|
1869 |
|
---|
1870 | if (buf == NULL || l <= 0)
|
---|
1871 | return -1;
|
---|
1872 | cbuf = malloc(l);
|
---|
1873 | memcpy(cbuf, buf, l); // make a copy of buf
|
---|
1874 |
|
---|
1875 | pthread_mutex_lock(&cc->lock);
|
---|
1876 |
|
---|
1877 | n = cc_msg_recv(cbuf); // recv and decrypt msg
|
---|
1878 |
|
---|
1879 | cs_ddump(cbuf, n, "cccam: received %d bytes from %s", n, remote_txt());
|
---|
1880 | client[cs_idx].last = time((time_t *) 0);
|
---|
1881 |
|
---|
1882 | if (n <= 0) {
|
---|
1883 | cs_log("%s connection closed to %s", getprefix(), remote_txt());
|
---|
1884 | n = -1;
|
---|
1885 | } else if (n < 4) {
|
---|
1886 | cs_log("%s packet to small (%d bytes)", getprefix(), n);
|
---|
1887 | n = -1;
|
---|
1888 | } else {
|
---|
1889 | // parse it and write it back, if we have received something of value
|
---|
1890 | if (cc_parse_msg(cbuf, n) == -1) //aston
|
---|
1891 | n = -2;
|
---|
1892 | memcpy(buf, cbuf, l);
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | pthread_mutex_unlock(&cc->lock);
|
---|
1896 |
|
---|
1897 | NULLFREE(cbuf);
|
---|
1898 |
|
---|
1899 | if (!client[cs_idx].is_server && (n == -1)) {
|
---|
1900 | cs_debug_mask(D_TRACE, "%s cc_recv: cycle connection", getprefix());
|
---|
1901 | cc_cycle_connection();
|
---|
1902 | }
|
---|
1903 |
|
---|
1904 | return n;
|
---|
1905 | }
|
---|
1906 |
|
---|
1907 | static int cc_cli_connect(void) {
|
---|
1908 | int handle, n;
|
---|
1909 | uint8 data[20];
|
---|
1910 | uint8 hash[SHA_DIGEST_LENGTH];
|
---|
1911 | uint8 buf[CC_MAXMSGSIZE];
|
---|
1912 | char pwd[64];
|
---|
1913 |
|
---|
1914 | // check cred config
|
---|
1915 | if (reader[client[cs_idx].ridx].device[0] == 0 || reader[client[cs_idx].ridx].r_pwd[0] == 0
|
---|
1916 | || reader[client[cs_idx].ridx].r_usr[0] == 0 || reader[client[cs_idx].ridx].r_port == 0) {
|
---|
1917 | cs_log("%s configuration error!", getprefix());
|
---|
1918 | return -5;
|
---|
1919 | }
|
---|
1920 |
|
---|
1921 | // connect
|
---|
1922 | handle = network_tcp_connection_open();
|
---|
1923 | if (handle <= 0) {
|
---|
1924 | cs_log("%s network connect error!", getprefix());
|
---|
1925 | return -1;
|
---|
1926 | }
|
---|
1927 |
|
---|
1928 | // get init seed
|
---|
1929 | //if ((n = recv(handle, data, 16, MSG_WAITALL)) != 16) {
|
---|
1930 | if ((n = read(handle, data, 16)) != 16) {
|
---|
1931 | int err = errno;
|
---|
1932 | cs_log("%s server does not return 16 bytes (n=%d, handle=%d, udp_fd=%d, cs_idx=%d, errno=%d)",
|
---|
1933 | getprefix(), n, handle, client[cs_idx].udp_fd, cs_idx, err);
|
---|
1934 | return -2;
|
---|
1935 | }
|
---|
1936 | struct cc_data *cc = reader[client[cs_idx].ridx].cc;
|
---|
1937 |
|
---|
1938 | if (!cc) {
|
---|
1939 | // init internals data struct
|
---|
1940 | cc = malloc(sizeof(struct cc_data));
|
---|
1941 | if (cc == NULL) {
|
---|
1942 | cs_log("%s cannot allocate memory", getprefix());
|
---|
1943 | return -1;
|
---|
1944 | }
|
---|
1945 | memset(cc, 0, sizeof(struct cc_data));
|
---|
1946 | cc->cards = llist_create();
|
---|
1947 | reader[client[cs_idx].ridx].cc = cc;
|
---|
1948 | cc->auto_blocked = llist_create();
|
---|
1949 | cc->current_card = malloc(sizeof(struct cc_current_card)*CS_MAXPID);
|
---|
1950 | memset(cc->current_card, 0, sizeof(struct cc_current_card)*CS_MAXPID);
|
---|
1951 | cc->pending_emms = llist_create();
|
---|
1952 | }
|
---|
1953 | cc->ecm_counter = 0;
|
---|
1954 | cc->max_ecms = 0;
|
---|
1955 | //cc->current_ecm_cidx = 0;
|
---|
1956 | cc->cmd05_mode = MODE_UNKNOWN;
|
---|
1957 | cc->cmd05_offset = 0;
|
---|
1958 | cc->cmd05_active = 0;
|
---|
1959 | cc->cmd05_data_len = 0;
|
---|
1960 | cc->answer_on_keepalive = time(NULL);
|
---|
1961 | memset(&cc->cmd05_data, 0, sizeof(cc->cmd05_data));
|
---|
1962 |
|
---|
1963 | cs_ddump(data, 16, "cccam: server init seed:");
|
---|
1964 |
|
---|
1965 | uint16 sum = 0x1234;
|
---|
1966 | uint16 recv_sum = (data[14] << 8) | data[15];
|
---|
1967 | int i;
|
---|
1968 | for (i = 0; i < 14; i++) {
|
---|
1969 | sum += data[i];
|
---|
1970 | }
|
---|
1971 | //Create special data to detect oscam-cccam:
|
---|
1972 | cc->is_oscam_cccam = sum==recv_sum;
|
---|
1973 |
|
---|
1974 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
1975 |
|
---|
1976 | SHA_CTX ctx;
|
---|
1977 | SHA1_Init(&ctx);
|
---|
1978 | SHA1_Update(&ctx, data, 16);
|
---|
1979 | SHA1_Final(hash, &ctx);
|
---|
1980 |
|
---|
1981 | cs_ddump(hash, sizeof(hash), "cccam: sha1 hash:");
|
---|
1982 |
|
---|
1983 | //initialisate crypto states
|
---|
1984 | cc_init_crypt(&cc->block[DECRYPT], hash, 20);
|
---|
1985 | cc_crypt(&cc->block[DECRYPT], data, 16, DECRYPT);
|
---|
1986 | cc_init_crypt(&cc->block[ENCRYPT], data, 16);
|
---|
1987 | cc_crypt(&cc->block[ENCRYPT], hash, 20, DECRYPT);
|
---|
1988 |
|
---|
1989 | cc_cmd_send(hash, 20, MSG_NO_HEADER); // send crypted hash to server
|
---|
1990 |
|
---|
1991 | memset(buf, 0, sizeof(buf));
|
---|
1992 | memcpy(buf, reader[client[cs_idx].ridx].r_usr, strlen(reader[client[cs_idx].ridx].r_usr));
|
---|
1993 | cs_ddump(buf, 20, "cccam: username '%s':", buf);
|
---|
1994 | cc_cmd_send(buf, 20, MSG_NO_HEADER); // send usr '0' padded -> 20 bytes
|
---|
1995 |
|
---|
1996 | memset(buf, 0, sizeof(buf));
|
---|
1997 | memset(pwd, 0, sizeof(pwd));
|
---|
1998 |
|
---|
1999 | cs_debug("cccam: 'CCcam' xor");
|
---|
2000 | memcpy(buf, "CCcam", 5);
|
---|
2001 | strncpy(pwd, reader[client[cs_idx].ridx].r_pwd, sizeof(pwd) - 1);
|
---|
2002 | cc_crypt(&cc->block[ENCRYPT], (uint8 *) pwd, strlen(pwd), ENCRYPT);
|
---|
2003 | cc_cmd_send(buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd
|
---|
2004 |
|
---|
2005 | //if ((n = recv(handle, data, 20, MSG_WAITALL)) != 20) {
|
---|
2006 | if ((n = read(handle, data, 20)) != 20) {
|
---|
2007 | cs_log("%s login failed, pwd ack not received (n = %d)", getprefix(), n);
|
---|
2008 | return -2;
|
---|
2009 | }
|
---|
2010 | cc_crypt(&cc->block[DECRYPT], data, 20, DECRYPT);
|
---|
2011 | cs_ddump(data, 20, "cccam: pwd ack received:");
|
---|
2012 |
|
---|
2013 | if (memcmp(data, buf, 5)) { // check server response
|
---|
2014 | cs_log("%s login failed, usr/pwd invalid", getprefix());
|
---|
2015 | return -2;
|
---|
2016 | } else {
|
---|
2017 | cs_debug_mask(D_TRACE, "%s login succeeded", getprefix());
|
---|
2018 | }
|
---|
2019 |
|
---|
2020 | cs_debug("cccam: last_s=%d, last_g=%d", reader[client[cs_idx].ridx].last_s,
|
---|
2021 | reader[client[cs_idx].ridx].last_g);
|
---|
2022 |
|
---|
2023 | client[cs_idx].pfd = client[cs_idx].udp_fd;
|
---|
2024 | cs_debug("cccam: pfd=%d", client[cs_idx].pfd);
|
---|
2025 |
|
---|
2026 | if (cc_send_cli_data() <= 0) {
|
---|
2027 | cs_log("%s login failed, could not send client data", getprefix());
|
---|
2028 | return -3;
|
---|
2029 | }
|
---|
2030 |
|
---|
2031 | //Trick: when discovered partner is an Oscam Client, then we send him our version string:
|
---|
2032 | if (cc->is_oscam_cccam) {
|
---|
2033 | sprintf((char*)buf, "PARTNER: OSCam v%s, build #%s (%s)", CS_VERSION, CS_SVN_VERSION, CS_OSTYPE);
|
---|
2034 | cc_cmd_send(buf, strlen((char*)buf)+1, MSG_CW_NOK1);
|
---|
2035 | }
|
---|
2036 |
|
---|
2037 | pthread_mutex_init(&cc->lock, NULL);
|
---|
2038 | pthread_mutex_init(&cc->ecm_busy, NULL);
|
---|
2039 |
|
---|
2040 | reader[client[cs_idx].ridx].caid[0] = reader[client[cs_idx].ridx].ftab.filts[0].caid;
|
---|
2041 | reader[client[cs_idx].ridx].nprov = reader[client[cs_idx].ridx].ftab.filts[0].nprids;
|
---|
2042 | for (n = 0; n < reader[client[cs_idx].ridx].nprov; n++) {
|
---|
2043 | reader[client[cs_idx].ridx].availkeys[n][0] = 1;
|
---|
2044 | reader[client[cs_idx].ridx].prid[n][0] = reader[client[cs_idx].ridx].ftab.filts[0].prids[n] >> 24;
|
---|
2045 | reader[client[cs_idx].ridx].prid[n][1] = reader[client[cs_idx].ridx].ftab.filts[0].prids[n] >> 16;
|
---|
2046 | reader[client[cs_idx].ridx].prid[n][2] = reader[client[cs_idx].ridx].ftab.filts[0].prids[n] >> 8;
|
---|
2047 | reader[client[cs_idx].ridx].prid[n][3] = reader[client[cs_idx].ridx].ftab.filts[0].prids[n] & 0xff;
|
---|
2048 | }
|
---|
2049 |
|
---|
2050 | reader[client[cs_idx].ridx].card_status = CARD_NEED_INIT;
|
---|
2051 | reader[client[cs_idx].ridx].last_g = reader[client[cs_idx].ridx].last_s = time((time_t *) 0);
|
---|
2052 | reader[client[cs_idx].ridx].tcp_connected = 1;
|
---|
2053 | reader[client[cs_idx].ridx].available = 1;
|
---|
2054 |
|
---|
2055 | cc->just_logged_in = 1;
|
---|
2056 |
|
---|
2057 | return 0;
|
---|
2058 | }
|
---|
2059 |
|
---|
2060 | struct s_auth *get_account(char *usr) {
|
---|
2061 | struct s_auth *account;
|
---|
2062 | for (account = cfg->account; account; account = account->next) {
|
---|
2063 | if (strcmp(usr, account->usr) == 0) {
|
---|
2064 | return account;
|
---|
2065 | }
|
---|
2066 | }
|
---|
2067 | return NULL;
|
---|
2068 | }
|
---|
2069 |
|
---|
2070 | static ulong get_reader_hexserial_crc()
|
---|
2071 | {
|
---|
2072 | ulong crc = 0;
|
---|
2073 | int r;
|
---|
2074 | for (r = 0; r < CS_MAXREADER; r++) {
|
---|
2075 | crc += crc32(0, reader[r].hexserial, 8);
|
---|
2076 | }
|
---|
2077 | return crc;
|
---|
2078 | }
|
---|
2079 |
|
---|
2080 | /**
|
---|
2081 | * Server:
|
---|
2082 | * Reports all caid/providers to the connected clients
|
---|
2083 | * returns total count of reported cards
|
---|
2084 | */
|
---|
2085 | static int cc_srv_report_cards() {
|
---|
2086 | int j;
|
---|
2087 | uint id, r, k;
|
---|
2088 | uint8 hop = 0;
|
---|
2089 | int reshare, usr_reshare, reader_reshare, maxhops, flt = 0;
|
---|
2090 | uint8 buf[CC_MAXMSGSIZE];
|
---|
2091 | struct cc_data *cc = client[cs_idx].cc;
|
---|
2092 |
|
---|
2093 | struct s_auth *account = get_account(client[cs_idx].usr);
|
---|
2094 | if (account) {
|
---|
2095 | maxhops = account->cccmaxhops;
|
---|
2096 | usr_reshare = account->cccreshare;
|
---|
2097 | } else {
|
---|
2098 | maxhops = 10;
|
---|
2099 | usr_reshare = cfg->cc_reshare;
|
---|
2100 | }
|
---|
2101 |
|
---|
2102 | if (!cc->report_carddata_id)
|
---|
2103 | id = 0x64;
|
---|
2104 | else
|
---|
2105 | id = cc->report_carddata_id;
|
---|
2106 |
|
---|
2107 | LLIST *reported_carddatas = llist_create();
|
---|
2108 |
|
---|
2109 | for (r = 0; r < CS_MAXREADER; r++) {
|
---|
2110 | if (!(reader[r].grp & client[cs_idx].grp)) continue;
|
---|
2111 | reader_reshare = reader[r].cc_reshare;
|
---|
2112 |
|
---|
2113 | reshare = (reader_reshare < usr_reshare) ? reader_reshare : usr_reshare;
|
---|
2114 | if (reshare < 0)
|
---|
2115 | continue;
|
---|
2116 |
|
---|
2117 | flt = 0;
|
---|
2118 | if (/*!reader[r].caid[0] && */reader[r].ftab.filts) {
|
---|
2119 | for (j = 0; j < CS_MAXFILTERS; j++) {
|
---|
2120 | if (reader[r].ftab.filts[j].caid) {
|
---|
2121 | memset(buf, 0, sizeof(buf));
|
---|
2122 | buf[0] = id >> 24;
|
---|
2123 | buf[1] = id >> 16;
|
---|
2124 | buf[2] = id >> 8;
|
---|
2125 | buf[3] = id & 0xff;
|
---|
2126 | //if (!reader[r].cc_id)
|
---|
2127 | {
|
---|
2128 | buf[6] = 0x49 + r;
|
---|
2129 | buf[7] = 0x10 + j;
|
---|
2130 | reader[r].cc_id = b2i(3, buf + 5);
|
---|
2131 | }
|
---|
2132 | //else
|
---|
2133 | //reader[r].cc_id++;
|
---|
2134 | buf[5] = reader[r].cc_id >> 16;
|
---|
2135 | buf[6] = reader[r].cc_id >> 8;
|
---|
2136 | buf[7] = reader[r].cc_id & 0xFF;
|
---|
2137 | buf[8] = reader[r].ftab.filts[j].caid >> 8;
|
---|
2138 | buf[9] = reader[r].ftab.filts[j].caid & 0xff;
|
---|
2139 | buf[10] = hop;
|
---|
2140 | buf[11] = reshare;
|
---|
2141 | if (!reader[r].audisabled)
|
---|
2142 | memcpy(buf + 12, reader[r].hexserial, 8);
|
---|
2143 | buf[20] = reader[r].ftab.filts[j].nprids;
|
---|
2144 | //cs_log("Ident CCcam card report caid: %04X readr %s subid: %06X", reader[r].ftab.filts[j].caid, reader[r].label, reader[r].cc_id);
|
---|
2145 | for (k = 0; k < reader[r].ftab.filts[j].nprids; k++) {
|
---|
2146 | buf[21 + (k * 7)] = reader[r].ftab.filts[j].prids[k]
|
---|
2147 | >> 16;
|
---|
2148 | buf[22 + (k * 7)] = reader[r].ftab.filts[j].prids[k]
|
---|
2149 | >> 8;
|
---|
2150 | buf[23 + (k * 7)] = reader[r].ftab.filts[j].prids[k]
|
---|
2151 | & 0xFF;
|
---|
2152 | //cs_log("Ident CCcam card report provider: %02X%02X%02X", buf[21 + (k*7)]<<16, buf[22 + (k*7)], buf[23 + (k*7)]);
|
---|
2153 | }
|
---|
2154 | buf[21 + (k * 7)] = 1;
|
---|
2155 | memcpy(buf + 22 + (k * 7), cc->node_id, 8);
|
---|
2156 | /*
|
---|
2157 | buf[21 + (k*7)+8] = 1;
|
---|
2158 | memcpy(buf + 22 + (k*7)+8, cc->node_id, 7);//8);
|
---|
2159 | cc_cmd_send(buf, 30 + (k*7) + 9, MSG_NEW_CARD);
|
---|
2160 | */
|
---|
2161 | int len = 30 + (k * 7);
|
---|
2162 | cc_cmd_send(buf, len, MSG_NEW_CARD);
|
---|
2163 | cc_add_reported_carddata(reported_carddatas, buf, len);
|
---|
2164 |
|
---|
2165 | id++;
|
---|
2166 | flt = 1;
|
---|
2167 | }
|
---|
2168 | }
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 | if (!reader[r].caid[0] && !flt) {
|
---|
2172 | flt = 0;
|
---|
2173 | for (j = 0; j < CS_MAXCAIDTAB; j++) {
|
---|
2174 | //cs_log("CAID map CCcam card report caid: %04X cmap: %04X", reader[r].ctab.caid[j], reader[r].ctab.cmap[j]);
|
---|
2175 | ushort lcaid = reader[r].ctab.caid[j];
|
---|
2176 |
|
---|
2177 | if (!lcaid || (lcaid == 0xFFFF))
|
---|
2178 | lcaid = reader[r].ctab.cmap[j];
|
---|
2179 |
|
---|
2180 | if (lcaid && (lcaid != 0xFFFF)) {
|
---|
2181 | memset(buf, 0, sizeof(buf));
|
---|
2182 | buf[0] = id >> 24;
|
---|
2183 | buf[1] = id >> 16;
|
---|
2184 | buf[2] = id >> 8;
|
---|
2185 | buf[3] = id & 0xff;
|
---|
2186 | //if (!reader[r].cc_id)
|
---|
2187 | {
|
---|
2188 | buf[6] = 0x48 + r;
|
---|
2189 | buf[7] = 0x63 + j;
|
---|
2190 | reader[r].cc_id = b2i(3, buf + 5);
|
---|
2191 | }
|
---|
2192 | //else
|
---|
2193 | //reader[r].cc_id++;
|
---|
2194 | buf[5] = reader[r].cc_id >> 16;
|
---|
2195 | buf[6] = reader[r].cc_id >> 8;
|
---|
2196 | buf[7] = reader[r].cc_id & 0xFF;
|
---|
2197 | buf[8] = lcaid >> 8;
|
---|
2198 | buf[9] = lcaid & 0xff;
|
---|
2199 | buf[10] = hop;
|
---|
2200 | buf[11] = reshare;
|
---|
2201 | if (!reader[r].audisabled)
|
---|
2202 | memcpy(buf + 12, reader[r].hexserial, 8);
|
---|
2203 | buf[20] = 1;
|
---|
2204 | //cs_log("CAID map CCcam card report caid: %04X nodeid: %s subid: %06X", lcaid, cs_hexdump(0, cc->peer_node_id, 8), reader[r].cc_id);
|
---|
2205 | //buf[21] = 0;
|
---|
2206 | //buf[22] = 0;
|
---|
2207 | //buf[23] = 0;
|
---|
2208 | buf[21 + 7] = 1;
|
---|
2209 | memcpy(buf + 22 + 7, cc->node_id, 8);
|
---|
2210 | int len = 30 + 7;
|
---|
2211 | cc_cmd_send(buf, len, MSG_NEW_CARD);
|
---|
2212 | cc_add_reported_carddata(reported_carddatas, buf, len);
|
---|
2213 | id++;
|
---|
2214 |
|
---|
2215 | flt = 1;
|
---|
2216 | }
|
---|
2217 | }
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 | if (reader[r].caid[0] && !flt) {
|
---|
2221 | //cs_log("tcp_connected: %d card_status: %d ", reader[r].tcp_connected, reader[r].card_status);
|
---|
2222 | memset(buf, 0, sizeof(buf));
|
---|
2223 | buf[0] = id >> 24;
|
---|
2224 | buf[1] = id >> 16;
|
---|
2225 | buf[2] = id >> 8;
|
---|
2226 | buf[3] = id & 0xff;
|
---|
2227 | buf[5] = reader[r].cc_id >> 16;
|
---|
2228 | buf[6] = reader[r].cc_id >> 8;
|
---|
2229 | buf[7] = reader[r].cc_id & 0xFF;
|
---|
2230 | if (!reader[r].cc_id) {
|
---|
2231 | buf[6] = 0x99;
|
---|
2232 | buf[7] = 0x63 + r;
|
---|
2233 | }
|
---|
2234 | buf[8] = reader[r].caid[0] >> 8;
|
---|
2235 | buf[9] = reader[r].caid[0] & 0xff;
|
---|
2236 | buf[10] = hop;
|
---|
2237 | buf[11] = reshare;
|
---|
2238 | if (!reader[r].audisabled)
|
---|
2239 | memcpy(buf + 12, reader[r].hexserial, 8);
|
---|
2240 | buf[20] = reader[r].nprov;
|
---|
2241 | for (j = 0; j < reader[r].nprov; j++) {
|
---|
2242 | if (!(reader[r].typ & R_IS_CASCADING)) //(reader[r].card_status == CARD_INSERTED)
|
---|
2243 | memcpy(buf + 21 + (j * 7), reader[r].prid[j] + 1, 3);
|
---|
2244 | else
|
---|
2245 | memcpy(buf + 21 + (j * 7), reader[r].prid[j], 3);
|
---|
2246 | //cs_log("Main CCcam card report provider: %02X%02X%02X%02X", buf[21+(j*7)], buf[22+(j*7)], buf[23+(j*7)], buf[24+(j*7)]);
|
---|
2247 | }
|
---|
2248 |
|
---|
2249 | buf[21 + (j * 7)] = 1;
|
---|
2250 | memcpy(buf + 22 + (j * 7), cc->node_id, 8);
|
---|
2251 | id++;
|
---|
2252 |
|
---|
2253 | if ((reader[r].tcp_connected || reader[r].card_status
|
---|
2254 | == CARD_INSERTED) /*&& !reader[r].cc_id*/) {
|
---|
2255 | reader[r].cc_id = b2i(3, buf + 5);
|
---|
2256 | int len = 30 + (j * 7);
|
---|
2257 | cc_add_reported_carddata(reported_carddatas, buf, len);
|
---|
2258 | cc_cmd_send(buf, len, MSG_NEW_CARD);
|
---|
2259 | //cs_log("CCcam: local card or newcamd reader %02X report ADD caid: %02X%02X %d %d %s subid: %06X", buf[7], buf[8], buf[9], reader[r].card_status, reader[r].tcp_connected, reader[r].label, reader[r].cc_id);
|
---|
2260 | } else if ((reader[r].card_status != CARD_INSERTED)
|
---|
2261 | && (!reader[r].tcp_connected) && reader[r].cc_id) {
|
---|
2262 | reader[r].cc_id = 0;
|
---|
2263 | cc_cmd_send(buf, 30 + (j * 7), MSG_CARD_REMOVED);
|
---|
2264 | //cs_log("CCcam: local card or newcamd reader %02X report REMOVE caid: %02X%02X %s", buf[7], buf[8], buf[9], reader[r].label);
|
---|
2265 | }
|
---|
2266 | }
|
---|
2267 |
|
---|
2268 | //SS: Hack:
|
---|
2269 | if (reader[r].typ == R_CCCAM && !flt) {
|
---|
2270 | if (cc->caid_infos && checkCaidInfos(r, &cc->caid_size)) {
|
---|
2271 | freeCaidInfos(cc->caid_infos);
|
---|
2272 | cc->caid_infos = NULL;
|
---|
2273 | }
|
---|
2274 | if (!cc->caid_infos)
|
---|
2275 | cc->caid_infos = loadCaidInfos(r);
|
---|
2276 | if (cc->caid_infos) {
|
---|
2277 | LLIST_ITR itr;
|
---|
2278 | struct cc_caid_info *caid_info = llist_itr_init(cc->caid_infos,
|
---|
2279 | &itr);
|
---|
2280 | while (caid_info) {
|
---|
2281 | if (caid_info->hop <= maxhops) {
|
---|
2282 | memset(buf, 0, sizeof(buf));
|
---|
2283 | buf[0] = id >> 24;
|
---|
2284 | buf[1] = id >> 16;
|
---|
2285 | buf[2] = id >> 8;
|
---|
2286 | buf[3] = id & 0xff;
|
---|
2287 | buf[5] = reader[r].cc_id >> 16;
|
---|
2288 | buf[6] = reader[r].cc_id >> 8;
|
---|
2289 | buf[7] = reader[r].cc_id & 0xFF;
|
---|
2290 | if (!reader[r].cc_id) {
|
---|
2291 | buf[6] = 0x99;
|
---|
2292 | buf[7] = 0x63 + r;
|
---|
2293 | }
|
---|
2294 | buf[8] = caid_info->caid >> 8;
|
---|
2295 | buf[9] = caid_info->caid & 0xff;
|
---|
2296 | buf[10] = caid_info->hop+1;
|
---|
2297 | buf[11] = reshare;
|
---|
2298 | //memcpy(buf + 12, caid_info->hexserial, 8);
|
---|
2299 | int j = 0;
|
---|
2300 | LLIST_ITR itr_prov;
|
---|
2301 | uint8 *prov = llist_itr_init(caid_info->provs, &itr_prov);
|
---|
2302 | while (prov) {
|
---|
2303 | memcpy(buf + 21 + (j * 7), prov, 3);
|
---|
2304 | prov = llist_itr_next(&itr_prov);
|
---|
2305 | j++;
|
---|
2306 | }
|
---|
2307 | buf[20] = j;
|
---|
2308 |
|
---|
2309 | buf[21 + (j * 7)] = 1;
|
---|
2310 | memcpy(buf + 22 + (j * 7), cc->node_id, 8);
|
---|
2311 | id++;
|
---|
2312 |
|
---|
2313 | reader[r].cc_id = b2i(3, buf + 5);
|
---|
2314 | int len = 30 + (j * 7);
|
---|
2315 | cc_cmd_send(buf, len, MSG_NEW_CARD);
|
---|
2316 | cc_add_reported_carddata(reported_carddatas, buf, len);
|
---|
2317 | }
|
---|
2318 | caid_info = llist_itr_next(&itr);
|
---|
2319 | }
|
---|
2320 | }
|
---|
2321 | }
|
---|
2322 | //SS: Hack end
|
---|
2323 | }
|
---|
2324 | cc->report_carddata_id = id;
|
---|
2325 |
|
---|
2326 | //Reported deleted cards:
|
---|
2327 | cc_free_reported_carddata(cc->reported_carddatas, 1);
|
---|
2328 | cc->reported_carddatas = reported_carddatas;
|
---|
2329 |
|
---|
2330 | int count = llist_count(reported_carddatas);
|
---|
2331 | cs_log("%s reported %d cards to client", getprefix(), count);
|
---|
2332 | return count;
|
---|
2333 | }
|
---|
2334 |
|
---|
2335 | static int cc_srv_connect() {
|
---|
2336 | int i;
|
---|
2337 | ulong cmi;
|
---|
2338 | uint seed;
|
---|
2339 | uint8 buf[CC_MAXMSGSIZE];
|
---|
2340 | uint8 data[16];
|
---|
2341 | char usr[21], pwd[21];
|
---|
2342 | struct s_auth *account;
|
---|
2343 | struct cc_data *cc;
|
---|
2344 |
|
---|
2345 | memset(usr, 0, sizeof(usr));
|
---|
2346 | memset(pwd, 0, sizeof(pwd));
|
---|
2347 |
|
---|
2348 | //SS: Use last cc data for faster reconnects:
|
---|
2349 | cc = client[cs_idx].cc;
|
---|
2350 | if (!cc) {
|
---|
2351 | // init internals data struct
|
---|
2352 | cc = malloc(sizeof(struct cc_data));
|
---|
2353 | if (cc == NULL) {
|
---|
2354 | cs_log("%s cannot allocate memory", getprefix());
|
---|
2355 | return -1;
|
---|
2356 | }
|
---|
2357 |
|
---|
2358 | client[cs_idx].cc = cc;
|
---|
2359 | memset(client[cs_idx].cc, 0, sizeof(struct cc_data));
|
---|
2360 | cc->server_card = malloc(sizeof(struct cc_card));
|
---|
2361 | }
|
---|
2362 | client[cs_idx].cc_use_rc4 = 0;
|
---|
2363 | client[cs_idx].is_server = 1;
|
---|
2364 |
|
---|
2365 | // calc + send random seed
|
---|
2366 | seed = (unsigned int) time((time_t*) 0);
|
---|
2367 | uint16 sum = 0x1234;
|
---|
2368 | for (i = 0; i < 14; i++) {
|
---|
2369 | data[i] = fast_rnd();
|
---|
2370 | sum += data[i];
|
---|
2371 | }
|
---|
2372 | //Create special data to detect oscam-cccam:
|
---|
2373 | data[14] = sum >> 8;
|
---|
2374 | data[15] = sum & 0xff;
|
---|
2375 |
|
---|
2376 | send(client[cs_idx].udp_fd, data, 16, 0);
|
---|
2377 |
|
---|
2378 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
2379 |
|
---|
2380 | SHA_CTX ctx;
|
---|
2381 | SHA1_Init(&ctx);
|
---|
2382 | SHA1_Update(&ctx, data, 16);
|
---|
2383 | SHA1_Final(buf, &ctx);
|
---|
2384 |
|
---|
2385 | //Special check for 2.0.11 clients
|
---|
2386 | //2.0.11 uses rc4 crypt
|
---|
2387 | //So we need to test data for validity:
|
---|
2388 | struct cc_crypt_block *block_rc4 = malloc(sizeof(struct cc_crypt_block)*2);
|
---|
2389 | uint8 *data_rc4 = malloc(16);
|
---|
2390 | uint8 *buf_rc4 = malloc(CC_MAXMSGSIZE);
|
---|
2391 | memcpy(data_rc4, data, sizeof(data));
|
---|
2392 | memcpy(buf_rc4, buf, CC_MAXMSGSIZE);
|
---|
2393 | memcpy(block_rc4, cc->block, sizeof(struct cc_crypt_block)*2);
|
---|
2394 | char usr_rc4[21];
|
---|
2395 | memset(usr_rc4, 0, sizeof(usr_rc4));
|
---|
2396 |
|
---|
2397 | //2.1.1 and newer clients:
|
---|
2398 | cc_init_crypt(&cc->block[ENCRYPT], buf, 20);
|
---|
2399 | cc_crypt(&cc->block[ENCRYPT], data, 16, DECRYPT);
|
---|
2400 | cc_init_crypt(&cc->block[DECRYPT], data, 16);
|
---|
2401 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2402 |
|
---|
2403 | //2.0.11 client:
|
---|
2404 | cc_init_crypt(&block_rc4[ENCRYPT], buf_rc4, 20);
|
---|
2405 | cc_rc4_crypt(&block_rc4[ENCRYPT], data_rc4, 16, DECRYPT);
|
---|
2406 | cc_init_crypt(&block_rc4[DECRYPT], data_rc4, 16);
|
---|
2407 | cc_rc4_crypt(&block_rc4[DECRYPT], buf_rc4, 20, DECRYPT);
|
---|
2408 |
|
---|
2409 | if ((i = recv(client[cs_idx].pfd, buf, 20, MSG_WAITALL)) == 20) {
|
---|
2410 | cs_ddump(buf, 20, "cccam: recv:");
|
---|
2411 | memcpy(buf_rc4, buf, CC_MAXMSGSIZE);
|
---|
2412 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2413 | cc_rc4_crypt(&block_rc4[DECRYPT], buf_rc4, 20, DECRYPT);
|
---|
2414 | cs_ddump(buf, 20, "cccam: hash:");
|
---|
2415 | cs_ddump(buf_rc4, 20, "cccam: hash rc4:");
|
---|
2416 | }
|
---|
2417 |
|
---|
2418 | // receive username
|
---|
2419 | if ((i = recv(client[cs_idx].pfd, buf, 20, MSG_WAITALL)) == 20) {
|
---|
2420 | memcpy(buf_rc4, buf, CC_MAXMSGSIZE);
|
---|
2421 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2422 | cc_rc4_crypt(&block_rc4[DECRYPT], buf_rc4, 20, DECRYPT);
|
---|
2423 |
|
---|
2424 | strncpy(usr, (char *) buf, sizeof(usr));
|
---|
2425 | strncpy(usr_rc4, (char *) buf_rc4, sizeof(usr_rc4));
|
---|
2426 |
|
---|
2427 | //test for nonprintable characters:
|
---|
2428 | client[cs_idx].cc_use_rc4 = -1;
|
---|
2429 | for (i = 0; i < 20; i++)
|
---|
2430 | {
|
---|
2431 | if (usr[i] > 0 && usr[i] < 0x20) { //found nonprintable char
|
---|
2432 | client[cs_idx].cc_use_rc4 = 1;
|
---|
2433 | break;
|
---|
2434 | }
|
---|
2435 | if (usr_rc4[i] > 0 && usr_rc4[i] < 0x20) { //found nonprintable char
|
---|
2436 | client[cs_idx].cc_use_rc4 = 0;
|
---|
2437 | break;
|
---|
2438 | }
|
---|
2439 | }
|
---|
2440 | if (client[cs_idx].cc_use_rc4 == 0)
|
---|
2441 | cs_ddump(buf, 20, "cccam: username '%s':", usr);
|
---|
2442 | else if (client[cs_idx].cc_use_rc4 == 1)
|
---|
2443 | cs_ddump(buf_rc4, 20, "cccam: username rc4 '%s':", usr_rc4);
|
---|
2444 | else
|
---|
2445 | cs_debug("illegal username received");
|
---|
2446 | }
|
---|
2447 |
|
---|
2448 | for (account = cfg->account; account; account = account->next) {
|
---|
2449 | if (strcmp(usr, account->usr) == 0) {
|
---|
2450 | strncpy(pwd, account->pwd, sizeof(pwd));
|
---|
2451 | client[cs_idx].cc_use_rc4 = 0; //We found a user by cc_crypt
|
---|
2452 | break;
|
---|
2453 | }
|
---|
2454 | if (strcmp(usr_rc4, account->usr) == 0) {
|
---|
2455 | strncpy(pwd, account->pwd, sizeof(pwd));
|
---|
2456 | client[cs_idx].cc_use_rc4 = 1; //We found a user by cc_rc4_crypt
|
---|
2457 | break;
|
---|
2458 | }
|
---|
2459 | }
|
---|
2460 |
|
---|
2461 | if (!account || client[cs_idx].cc_use_rc4 == -1) {
|
---|
2462 | cs_log("account '%s' not found!", client[cs_idx].cc_use_rc4?usr_rc4:usr);
|
---|
2463 | return -1;
|
---|
2464 | }
|
---|
2465 |
|
---|
2466 | if (client[cs_idx].cc_use_rc4) {
|
---|
2467 | cs_log("%s client is using version 2.0.11 rc4", getprefix());
|
---|
2468 | memcpy(cc->block, block_rc4, sizeof(struct cc_crypt_block)*2);
|
---|
2469 | }
|
---|
2470 | free(block_rc4);
|
---|
2471 | free(data_rc4);
|
---|
2472 | free(buf_rc4);
|
---|
2473 |
|
---|
2474 | // receive passwd / 'CCcam'
|
---|
2475 | cc_crypt(&cc->block[DECRYPT], (uint8 *) pwd, strlen(pwd), DECRYPT);
|
---|
2476 | if ((i = recv(client[cs_idx].pfd, buf, 6, MSG_WAITALL)) == 6) {
|
---|
2477 | cc_crypt(&cc->block[DECRYPT], buf, 6, DECRYPT);
|
---|
2478 | cs_ddump(buf, 6, "cccam: pwd check '%s':", buf);
|
---|
2479 | } else
|
---|
2480 | return -1;
|
---|
2481 |
|
---|
2482 | client[cs_idx].crypted = 1;
|
---|
2483 | if (cs_auth_client(account, NULL))
|
---|
2484 | return -1;
|
---|
2485 | //cs_auth_client((struct s_auth *)(-1), NULL);
|
---|
2486 |
|
---|
2487 | // send passwd ack
|
---|
2488 | memset(buf, 0, 20);
|
---|
2489 | memcpy(buf, "CCcam\0", 6);
|
---|
2490 | cs_ddump(buf, 20, "cccam: send ack:");
|
---|
2491 | cc_crypt(&cc->block[ENCRYPT], buf, 20, ENCRYPT);
|
---|
2492 | send(client[cs_idx].pfd, buf, 20, 0);
|
---|
2493 |
|
---|
2494 | // recv cli data
|
---|
2495 | memset(buf, 0, sizeof(buf));
|
---|
2496 | i = cc_msg_recv(buf);
|
---|
2497 | if (i < 0)
|
---|
2498 | return -1;
|
---|
2499 | cs_ddump(buf, i, "cccam: cli data:");
|
---|
2500 | memcpy(cc->peer_node_id, buf + 24, 8);
|
---|
2501 | cs_log("%s client '%s' (%s) running v%s (%s)", getprefix(), buf + 4,
|
---|
2502 | cs_hexdump(0, cc->peer_node_id, 8), buf + 33, buf + 65);
|
---|
2503 |
|
---|
2504 | // send cli data ack
|
---|
2505 | cc_cmd_send(NULL, 0, MSG_CLI_DATA);
|
---|
2506 |
|
---|
2507 | if (cc_send_srv_data() < 0)
|
---|
2508 | return -1;
|
---|
2509 |
|
---|
2510 | // report cards
|
---|
2511 | ulong hexserial_crc = get_reader_hexserial_crc();
|
---|
2512 | cc_srv_report_cards();
|
---|
2513 | int caid_info_count = cc->caid_infos ? llist_count(cc->caid_infos) : 0;
|
---|
2514 |
|
---|
2515 | cmi = 0;
|
---|
2516 | // check for client timeout, if timeout occurs try to send keepalive
|
---|
2517 | for (;;) {
|
---|
2518 | i = process_input(client[cs_idx].mbuf, sizeof(client[cs_idx].mbuf), 10); //cfg->cmaxidle);
|
---|
2519 | //cs_log("srv process input i=%d cmi=%d", i, cmi);
|
---|
2520 | if (i == -9) {
|
---|
2521 | cmi += 10;
|
---|
2522 | if (cfg->cmaxidle && cmi >= cfg->cmaxidle) {
|
---|
2523 | cmi = 0;
|
---|
2524 | cs_debug_mask(D_TRACE, "%s keepalive after maxidle is reached", getprefix());
|
---|
2525 | break;
|
---|
2526 | }
|
---|
2527 | } else if (i <= 0)
|
---|
2528 | break;
|
---|
2529 | else {
|
---|
2530 | cmi = 0;
|
---|
2531 | if (!cc->server_ecm_pending) {
|
---|
2532 | int new_caid_info_count = cc->caid_infos ? llist_count(cc->caid_infos) : 0;
|
---|
2533 | ulong new_hexserial_crc = get_reader_hexserial_crc();
|
---|
2534 | if (new_caid_info_count != caid_info_count || new_hexserial_crc != hexserial_crc) {
|
---|
2535 | cc_srv_report_cards();
|
---|
2536 | caid_info_count = new_caid_info_count;
|
---|
2537 | hexserial_crc = new_hexserial_crc;
|
---|
2538 | }
|
---|
2539 | }
|
---|
2540 | }
|
---|
2541 | }
|
---|
2542 | return 0;
|
---|
2543 | }
|
---|
2544 |
|
---|
2545 | void cc_srv_init(void *idx) {
|
---|
2546 | int cidx=(int)idx;
|
---|
2547 | client[cidx].thread=pthread_self();
|
---|
2548 |
|
---|
2549 | client[cs_idx].pfd = client[cs_idx].udp_fd;
|
---|
2550 | //cc_auth_client(client[cs_idx].ip);
|
---|
2551 | if (cc_srv_connect() < 0)
|
---|
2552 | cs_log("cccam: %d failed errno: %d (%s)", __LINE__, errno, strerror(
|
---|
2553 | errno));
|
---|
2554 | cs_disconnect_client();
|
---|
2555 |
|
---|
2556 | cs_exit(1);
|
---|
2557 | }
|
---|
2558 |
|
---|
2559 | int cc_cli_init_int() {
|
---|
2560 |
|
---|
2561 | if (reader[client[cs_idx].ridx].tcp_connected)
|
---|
2562 | return -1;
|
---|
2563 |
|
---|
2564 | struct protoent *ptrp;
|
---|
2565 | int p_proto;
|
---|
2566 |
|
---|
2567 | client[cs_idx].pfd = 0;
|
---|
2568 | if (reader[client[cs_idx].ridx].r_port <= 0) {
|
---|
2569 | cs_log("%s invalid port %d for server %s", getprefix(),
|
---|
2570 | reader[client[cs_idx].ridx].r_port, reader[client[cs_idx].ridx].device);
|
---|
2571 | return (1);
|
---|
2572 | }
|
---|
2573 | if ((ptrp = getprotobyname("tcp")))
|
---|
2574 | p_proto = ptrp->p_proto;
|
---|
2575 | else
|
---|
2576 | p_proto = 6;
|
---|
2577 |
|
---|
2578 | // client[cs_idx].ip = 0;
|
---|
2579 | // memset((char *) &loc_sa, 0, sizeof(loc_sa));
|
---|
2580 | // loc_sa.sin_family = AF_INET;
|
---|
2581 | //#ifdef LALL
|
---|
2582 | // if (cfg->serverip[0])
|
---|
2583 | // loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip);
|
---|
2584 | // else
|
---|
2585 | //#endif
|
---|
2586 | // loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
2587 | // loc_sa.sin_port = htons(reader[client[cs_idx].ridx].l_port);
|
---|
2588 |
|
---|
2589 | if ((client[cs_idx].udp_fd = socket(PF_INET, SOCK_STREAM, p_proto)) <= 0) {
|
---|
2590 | cs_log("%s Socket creation failed (errno=%d, socket=%d)", getprefix(), errno, client[cs_idx].udp_fd);
|
---|
2591 | return -10;
|
---|
2592 | }
|
---|
2593 | //cs_log("%s 1 socket created: cs_idx=%d, fd=%d errno=%d", getprefix(), cs_idx, client[cs_idx].udp_fd, errno);
|
---|
2594 |
|
---|
2595 | #ifdef SO_PRIORITY
|
---|
2596 | if (cfg->netprio)
|
---|
2597 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
2598 | (void *)&cfg->netprio, sizeof(ulong));
|
---|
2599 | #endif
|
---|
2600 | reader[client[cs_idx].ridx].tcp_ito = 1; //60sec...This now invokes ph_idle()
|
---|
2601 |
|
---|
2602 | memset((char *) &client[cs_idx].udp_sa, 0, sizeof(client[cs_idx].udp_sa));
|
---|
2603 | client[cs_idx].udp_sa.sin_family = AF_INET;
|
---|
2604 | client[cs_idx].udp_sa.sin_port = htons((u_short) reader[client[cs_idx].ridx].r_port);
|
---|
2605 |
|
---|
2606 | if (reader[client[cs_idx].ridx].tcp_rto <= 0)
|
---|
2607 | reader[client[cs_idx].ridx].tcp_rto = 60 * 60 * 10; // timeout to 10 hours
|
---|
2608 | cs_debug("cccam: reconnect timeout set to: %d", reader[client[cs_idx].ridx].tcp_rto);
|
---|
2609 | cc_check_version(reader[client[cs_idx].ridx].cc_version, reader[client[cs_idx].ridx].cc_build);
|
---|
2610 | cs_log(
|
---|
2611 | "proxy reader: %s (%s:%d) cccam v%s build %s, maxhop: %d, retry ecm: %d, auto block: %d",
|
---|
2612 | reader[client[cs_idx].ridx].label, reader[client[cs_idx].ridx].device, reader[client[cs_idx].ridx].r_port,
|
---|
2613 | reader[client[cs_idx].ridx].cc_version, reader[client[cs_idx].ridx].cc_build,
|
---|
2614 | reader[client[cs_idx].ridx].cc_maxhop, !reader[client[cs_idx].ridx].cc_disable_retry_ecm,
|
---|
2615 | !reader[client[cs_idx].ridx].cc_disable_auto_block);
|
---|
2616 |
|
---|
2617 | return cc_cli_connect();
|
---|
2618 | }
|
---|
2619 |
|
---|
2620 | static int cc_cli_init()
|
---|
2621 | {
|
---|
2622 | while (cc_cli_init_int() != 0) {
|
---|
2623 | network_tcp_connection_close(&reader[client[cs_idx].ridx], client[cs_idx].udp_fd);
|
---|
2624 |
|
---|
2625 | cs_sleepms(cfg->reader_restart_seconds * 1000); // SS: wait
|
---|
2626 | cs_log("restarting reader %s (index=%d)", reader[client[cs_idx].ridx].label, client[cs_idx].ridx);
|
---|
2627 | }
|
---|
2628 | return 0;
|
---|
2629 | }
|
---|
2630 |
|
---|
2631 | /**
|
---|
2632 | * return 1 if we are able to send requests:
|
---|
2633 | */
|
---|
2634 | int cc_available(int ridx, int checktype) {
|
---|
2635 | //cs_debug_mask(D_TRACE, "checking reader %s availibility", reader[ridx].label);
|
---|
2636 | if (!reader[ridx].cc || reader[ridx].tcp_connected != 2 || reader[ridx].card_status != CARD_INSERTED)
|
---|
2637 | return 0;
|
---|
2638 |
|
---|
2639 | if (checktype == AVAIL_CHECK_LOADBALANCE && !reader[ridx].available) {
|
---|
2640 | cs_debug_mask(D_TRACE, "checking reader %s availibility=0 (unavail)", reader[ridx].label);
|
---|
2641 | return 0; //We are not initialized or not connected!
|
---|
2642 | }
|
---|
2643 |
|
---|
2644 | return 1;
|
---|
2645 | }
|
---|
2646 |
|
---|
2647 | void cc_cleanup(void) {
|
---|
2648 | if (!client[cs_idx].is_server) {
|
---|
2649 | cc_cli_close(); // we need to close open fd's
|
---|
2650 | cc_free(reader[client[cs_idx].ridx].cc);
|
---|
2651 | reader[client[cs_idx].ridx].cc = NULL;
|
---|
2652 | } else {
|
---|
2653 | cc_free(client[cs_idx].cc);
|
---|
2654 | client[cs_idx].cc = NULL;
|
---|
2655 | }
|
---|
2656 | }
|
---|
2657 |
|
---|
2658 | void module_cccam(struct s_module *ph) {
|
---|
2659 | strcpy(ph->desc, "cccam");
|
---|
2660 | ph->type = MOD_CONN_TCP;
|
---|
2661 | ph->logtxt = ", crypted";
|
---|
2662 | ph->watchdog = 1;
|
---|
2663 | ph->recv = cc_recv;
|
---|
2664 | ph->cleanup = cc_cleanup;
|
---|
2665 | ph->c_multi = 1;
|
---|
2666 | ph->c_init = cc_cli_init;
|
---|
2667 | ph->c_idle = cc_idle;
|
---|
2668 | ph->c_recv_chk = cc_recv_chk;
|
---|
2669 | ph->c_send_ecm = cc_send_ecm;
|
---|
2670 | ph->c_send_emm = cc_send_emm;
|
---|
2671 | ph->s_ip = cfg->cc_srvip;
|
---|
2672 | ph->s_handler = cc_srv_init;
|
---|
2673 | ph->send_dcw = cc_send_dcw;
|
---|
2674 | ph->c_available = cc_available;
|
---|
2675 | static PTAB ptab;
|
---|
2676 | ptab.ports[0].s_port = cfg->cc_port;
|
---|
2677 | ph->ptab = &ptab;
|
---|
2678 | ph->ptab->nports = 1;
|
---|
2679 | }
|
---|