1 | #include "globals.h"
|
---|
2 | #ifdef MODULE_GBOX
|
---|
3 |
|
---|
4 | // The following headers are used in parsing mg-encrypted parameter
|
---|
5 | #if defined(__APPLE__) || defined(__FreeBSD__)
|
---|
6 | #include <net/if_dl.h>
|
---|
7 | #include <ifaddrs.h>
|
---|
8 | #elif defined(__SOLARIS__)
|
---|
9 | #include <net/if.h>
|
---|
10 | #include <net/if_arp.h>
|
---|
11 | #include <sys/sockio.h>
|
---|
12 | #else
|
---|
13 | #include <net/if.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include "minilzo/minilzo.h"
|
---|
17 | #include "module-gbox.h"
|
---|
18 | #include "oscam-failban.h"
|
---|
19 | #include "oscam-client.h"
|
---|
20 | #include "oscam-ecm.h"
|
---|
21 | #include "oscam-lock.h"
|
---|
22 | #include "oscam-net.h"
|
---|
23 | #include "oscam-string.h"
|
---|
24 | #include "oscam-reader.h"
|
---|
25 | #include "oscam-garbage.h"
|
---|
26 |
|
---|
27 | #define FILE_GBOX_VERSION "/tmp/gbx.ver"
|
---|
28 | #define FILE_SHARED_CARDS_INFO "/tmp/gbx_card.info"
|
---|
29 | #define FILE_ATTACK_INFO "/tmp/gbx_attack.txt"
|
---|
30 |
|
---|
31 | enum {
|
---|
32 | MSG_ECM = 0x445c,
|
---|
33 | MSG_CW = 0x4844,
|
---|
34 | MSG_HELLO = 0xddab,
|
---|
35 | MSG_HELLO1 = 0x4849,
|
---|
36 | MSG_CHECKCODE = 0x41c0,
|
---|
37 | MSG_GOODBYE = 0x9091,
|
---|
38 | MSG_GSMS_ACK = 0x9098,
|
---|
39 | MSG_GSMS = 0xff0,
|
---|
40 | MSG_BOXINFO = 0xa0a1,
|
---|
41 | MSG_UNKNWN1 = 0x9099,
|
---|
42 | MSG_UNKNWN2 = 0x48F9,
|
---|
43 | };
|
---|
44 |
|
---|
45 | struct gbox_srvid {
|
---|
46 | uint16_t sid;
|
---|
47 | uint16_t peer_idcard;
|
---|
48 | uint32_t provid_id;
|
---|
49 | };
|
---|
50 |
|
---|
51 | struct gbox_card {
|
---|
52 | uint16_t peer_id;
|
---|
53 | uint16_t caid;
|
---|
54 | uint32_t provid;
|
---|
55 | uint32_t provid_1;
|
---|
56 | int32_t slot;
|
---|
57 | int32_t dist;
|
---|
58 | int32_t lvl;
|
---|
59 | LLIST *badsids; // sids that have failed to decode (struct cc_srvid)
|
---|
60 | LLIST *goodsids; //sids that could be decoded (struct cc_srvid)
|
---|
61 | };
|
---|
62 |
|
---|
63 | struct gbox_peer {
|
---|
64 | uint16_t id;
|
---|
65 | uchar key[4];
|
---|
66 | uchar ver;
|
---|
67 | uchar type;
|
---|
68 | LLIST *cards;
|
---|
69 | LLIST *cards_send;
|
---|
70 | uchar checkcode[7];
|
---|
71 | uchar *hostname;
|
---|
72 | int32_t online;
|
---|
73 | int32_t fail_count;
|
---|
74 | int32_t hello_count;
|
---|
75 | int32_t hello_cont;
|
---|
76 | int32_t goodbye_cont;
|
---|
77 | uchar ecm_idx;
|
---|
78 | uchar gbox_count_ecm;
|
---|
79 | time_t t_ecm;
|
---|
80 | uint32_t cont_port;
|
---|
81 | uint16_t last_srvid;
|
---|
82 | uint16_t last_caid;
|
---|
83 | };
|
---|
84 |
|
---|
85 | struct gbox_data {
|
---|
86 | uint16_t id;
|
---|
87 | uchar checkcode[7];
|
---|
88 | uchar key[4];
|
---|
89 | uchar ver;
|
---|
90 | uchar type;
|
---|
91 | uint16_t exp_seq; // hello seq
|
---|
92 | int32_t hello_expired;
|
---|
93 | uchar cws[16];
|
---|
94 | struct gbox_peer peer;
|
---|
95 | CS_MUTEX_LOCK lock;
|
---|
96 | uchar buf[1024];
|
---|
97 | pthread_mutex_t peer_online_mutex;
|
---|
98 | pthread_cond_t peer_online_cond;
|
---|
99 | LLIST *local_cards;
|
---|
100 | };
|
---|
101 |
|
---|
102 | struct gbox_ecm_info {
|
---|
103 | uint16_t peer,
|
---|
104 | peer_cw,
|
---|
105 | caid,
|
---|
106 | extra;
|
---|
107 | uchar version,
|
---|
108 | type,
|
---|
109 | slot,
|
---|
110 | unknwn1,
|
---|
111 | unknwn2,
|
---|
112 | ncards;
|
---|
113 | uchar checksums[14];
|
---|
114 | uchar ecm[512];
|
---|
115 | int16_t l;
|
---|
116 | uint16_t id_cl_gbox;
|
---|
117 | };
|
---|
118 |
|
---|
119 | struct cwcache_data {
|
---|
120 | uint32_t ecmcrc;
|
---|
121 | uchar cws[16];
|
---|
122 | } cwcache[64];
|
---|
123 |
|
---|
124 | void gbox_decrypt(uchar *buffer, int bufsize, uchar *localkey);
|
---|
125 | static void gbox_send_boxinfo(struct s_client *cli);
|
---|
126 | static void gbox_decompress(struct gbox_data *UNUSED(gbox), uchar *buf, int32_t *unpacked_len);
|
---|
127 | static void gbox_send_hello(struct s_client *cli);
|
---|
128 | static void gbox_expire_hello(struct s_client *cli);
|
---|
129 | static int32_t gbox_client_init(struct s_client *cli);
|
---|
130 | static int32_t gbox_recv2(IN_ADDR_T recv_ip, in_port_t recv_port, uchar *b, int32_t l);
|
---|
131 | static int32_t gbox_recv_chk(struct s_client *cli, uchar *dcw, int32_t *rc, uchar *data, int32_t UNUSED(n));
|
---|
132 | static int32_t gbox_decode_cmd(uchar *buf);
|
---|
133 | uint32_t gbox_get_ecmchecksum(ECM_REQUEST *er);
|
---|
134 |
|
---|
135 | const uint8_t gbox_version_high_byte = 0x02;
|
---|
136 | const uint8_t gbox_version_low_byte = 0x1b;
|
---|
137 | const uint8_t gbox_type_dvb = 0x53;
|
---|
138 |
|
---|
139 | time_t login_gbox;
|
---|
140 | int8_t main_1 = 0;
|
---|
141 |
|
---|
142 | void gbox_write_version(void)
|
---|
143 | {
|
---|
144 | FILE *fhandle = fopen(FILE_GBOX_VERSION, "w");
|
---|
145 | if (!fhandle) {
|
---|
146 | cs_log("Couldn't open %s: %s\n", FILE_GBOX_VERSION, strerror(errno));
|
---|
147 | return;
|
---|
148 | }
|
---|
149 | fprintf(fhandle, "%02X.%02X\n", gbox_version_high_byte, gbox_version_low_byte);
|
---|
150 | fclose(fhandle);
|
---|
151 | }
|
---|
152 |
|
---|
153 | void gbox_write_shared_cards_info(void)
|
---|
154 | {
|
---|
155 | int32_t card_count=0;
|
---|
156 | int32_t i = 0;
|
---|
157 |
|
---|
158 | FILE *fhandle;
|
---|
159 | fhandle = fopen(FILE_SHARED_CARDS_INFO, "w");
|
---|
160 | if (!fhandle) {
|
---|
161 | cs_log("Couldn't open %s: %s\n", FILE_SHARED_CARDS_INFO, strerror(errno));
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | struct s_client *cl;
|
---|
166 | for (i = 0, cl = first_client; cl; cl = cl->next, i++) {
|
---|
167 | if (cl->gbox) {
|
---|
168 | struct s_reader *rdr = cl->reader;
|
---|
169 | struct gbox_data *gbox = cl->gbox;
|
---|
170 | struct gbox_card *card;
|
---|
171 |
|
---|
172 | if ((rdr->card_status == CARD_INSERTED) && (cl->typ == 'p')) {
|
---|
173 | LL_ITER it = ll_iter_create(gbox->peer.cards);
|
---|
174 | while ((card = ll_iter_next(&it))) {
|
---|
175 | fprintf(fhandle, "CardID %4d at %s Card %08X Sl:%2d Lev:%2d dist:%2d id:%04X\n",
|
---|
176 | card_count, cl->reader->label, card->provid_1,
|
---|
177 | card->slot, card->lvl, card->dist, card->peer_id);
|
---|
178 | card_count++;
|
---|
179 | } // end of while ll_iter_next
|
---|
180 | } // end of if INSERTED && 'p'
|
---|
181 | } // end of if cl->gbox
|
---|
182 | } // end of for cl->next
|
---|
183 | fclose(fhandle);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | void hostname2ip(char *hostname, IN_ADDR_T* ip)
|
---|
188 | {
|
---|
189 | cs_resolve(hostname, ip, NULL, NULL);
|
---|
190 | }
|
---|
191 |
|
---|
192 | void gbox_add_good_card(struct s_client *cl, uint16_t id_card, uint32_t prov,uint16_t sid_ok)
|
---|
193 | {
|
---|
194 | struct gbox_data *gbox = cl->gbox;
|
---|
195 | struct gbox_card *card = NULL;
|
---|
196 | struct gbox_srvid *srvid = NULL;
|
---|
197 | LL_ITER it = ll_iter_create(gbox->peer.cards);
|
---|
198 | while ((card = ll_iter_next(&it))) {
|
---|
199 | if (card->peer_id==id_card && card->provid == prov) {
|
---|
200 | cl->reader->currenthops = card->dist;
|
---|
201 | LL_ITER it2 = ll_iter_create(card->goodsids);
|
---|
202 | while ((srvid = ll_iter_next(&it2))) {
|
---|
203 | if (srvid->sid == sid_ok) {
|
---|
204 | return; // sid_ok is already in the list of goodsids
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | LL_ITER it3 = ll_iter_create(card->badsids);
|
---|
209 | while ((srvid = ll_iter_next(&it3))) {
|
---|
210 | if (srvid->sid == sid_ok){
|
---|
211 | ll_iter_remove_data(&it3); // remove sid_ok from badsids
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | if (!cs_malloc(&srvid, sizeof(struct gbox_srvid)))
|
---|
217 | return;
|
---|
218 | srvid->sid=sid_ok;
|
---|
219 | srvid->peer_idcard=id_card;
|
---|
220 | srvid->provid_id=card->provid;
|
---|
221 | ll_append(card->goodsids, srvid);
|
---|
222 | break;
|
---|
223 | }
|
---|
224 | }//end of ll_iter_next
|
---|
225 | //return dist_c;
|
---|
226 | }
|
---|
227 |
|
---|
228 | void gbox_free_card(struct gbox_card *card)
|
---|
229 | {
|
---|
230 | ll_destroy_data_NULL(card->badsids);
|
---|
231 | ll_destroy_data_NULL(card->goodsids);
|
---|
232 | add_garbage(card);
|
---|
233 | return;
|
---|
234 | }
|
---|
235 |
|
---|
236 | void gbox_remove_cards_without_goodsids(LLIST *card_list)
|
---|
237 | {
|
---|
238 | if (card_list) {
|
---|
239 | LL_ITER it = ll_iter_create(card_list);
|
---|
240 | struct gbox_card *card;
|
---|
241 | while ((card = ll_iter_next(&it))) {
|
---|
242 | if (ll_count(card->goodsids)==0){
|
---|
243 | ll_iter_remove(&it);
|
---|
244 | gbox_free_card(card);
|
---|
245 | } else {
|
---|
246 | ll_destroy_data_NULL(card->badsids);
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|
250 | return;
|
---|
251 | }
|
---|
252 |
|
---|
253 | void gbox_free_cardlist(LLIST *card_list)
|
---|
254 | {
|
---|
255 | if (card_list) {
|
---|
256 | LL_ITER it = ll_iter_create(card_list);
|
---|
257 | struct gbox_card *card;
|
---|
258 | while ((card = ll_iter_next_remove(&it))) {
|
---|
259 | gbox_free_card(card);
|
---|
260 | }
|
---|
261 | ll_destroy_NULL(card_list);
|
---|
262 | }
|
---|
263 | return;
|
---|
264 | }
|
---|
265 |
|
---|
266 | void gbox_reconnect_client(void)
|
---|
267 | {
|
---|
268 | struct s_client *cl;
|
---|
269 | for (cl = first_client; cl; cl = cl->next) {
|
---|
270 | if (cl->gbox) {
|
---|
271 | hostname2ip(cl->reader->device, &SIN_GET_ADDR(cl->udp_sa));
|
---|
272 | SIN_GET_FAMILY(cl->udp_sa) = AF_INET;
|
---|
273 | SIN_GET_PORT(cl->udp_sa) = htons((uint16_t)cl->reader->r_port);
|
---|
274 | hostname2ip(cl->reader->device, &(cl->ip));
|
---|
275 | cl->reader->tcp_connected = 0;
|
---|
276 | cl->reader->card_status = CARD_NEED_INIT;
|
---|
277 | struct gbox_data *gbox = cl->gbox;
|
---|
278 | gbox->peer.hello_cont = 0;
|
---|
279 | gbox->hello_expired = 0;
|
---|
280 | gbox->peer.online = 0;
|
---|
281 | gbox->peer.ecm_idx = 0;
|
---|
282 | gbox->peer.t_ecm=time((time_t*)0);
|
---|
283 | cl->reader->last_s = cl->reader->last_g = 0;
|
---|
284 | gbox_free_cardlist(gbox->peer.cards);
|
---|
285 | gbox->peer.cards = ll_create("peer.cards");
|
---|
286 | gbox_send_hello(cl);
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | static void * gbox_server(struct s_client *cli, uchar *b, int32_t l)
|
---|
292 | {
|
---|
293 | cs_log("gbox: gbox_server %s/%d",cli->reader->label, cli->port);
|
---|
294 | gbox_recv2(cli->ip, cli->reader->l_port, b, l);
|
---|
295 | return 0;
|
---|
296 | }
|
---|
297 |
|
---|
298 | static void gbox_server_init(struct s_client * client)
|
---|
299 | {
|
---|
300 | cs_log("gbox: gbox_server_init %s/%d",client->reader->label, client->port);
|
---|
301 | }
|
---|
302 |
|
---|
303 | static void gbox_decompress2(uchar *buf, int32_t *unpacked_len)
|
---|
304 | {
|
---|
305 | uint8_t *tmp;
|
---|
306 | if (!cs_malloc(&tmp,0x40000))
|
---|
307 | return;
|
---|
308 |
|
---|
309 | int err;
|
---|
310 | int len = *unpacked_len - 12;
|
---|
311 | *unpacked_len = 0x40000;
|
---|
312 |
|
---|
313 | lzo_init();
|
---|
314 | cs_debug_mask(D_READER, "decompressing %d bytes",len);
|
---|
315 | if ((err=lzo1x_decompress_safe(buf + 12, len, tmp, (lzo_uint *)unpacked_len, NULL)) != LZO_E_OK) {
|
---|
316 | cs_debug_mask(D_READER, "gbox: decompression failed! errno=%d", err);
|
---|
317 | }
|
---|
318 |
|
---|
319 | memcpy(buf + 12, tmp, *unpacked_len);
|
---|
320 | *unpacked_len += 12;
|
---|
321 | free(tmp);
|
---|
322 | }
|
---|
323 |
|
---|
324 | int32_t gbox_cmd_hello(struct s_client *cli, int32_t n)
|
---|
325 | {
|
---|
326 | struct gbox_data *gbox = cli->gbox;
|
---|
327 | uint8_t *data = gbox->buf;
|
---|
328 | int32_t i;
|
---|
329 | int32_t ncards_in_msg = 0;
|
---|
330 | int32_t payload_len = n;
|
---|
331 | int32_t checkcode_len = 0;
|
---|
332 | int32_t hostname_len = 0;
|
---|
333 | int32_t footer_len = 0;
|
---|
334 | uint8_t *ptr = 0;
|
---|
335 |
|
---|
336 | if (!(data[0] == 0x48 && data[1]==0x49)) { // if not MSG_HELLO1
|
---|
337 | gbox_decompress(gbox, data, &payload_len);
|
---|
338 | }
|
---|
339 | cs_ddump_mask(D_READER, data, payload_len, "gbox: decompressed data (%d bytes):", payload_len);
|
---|
340 |
|
---|
341 | if ((data[0x0B] == 0) | ((data[0x0A] == 1) && (data[0x0B] == 0x80))) {
|
---|
342 | if (gbox->peer.cards)
|
---|
343 | gbox_remove_cards_without_goodsids(gbox->peer.cards);
|
---|
344 | if (!gbox->peer.cards)
|
---|
345 | gbox->peer.cards = ll_create("peer.cards");
|
---|
346 | checkcode_len = 7;
|
---|
347 | hostname_len = data[payload_len - 1];
|
---|
348 | footer_len = hostname_len + 2;
|
---|
349 | }
|
---|
350 |
|
---|
351 | if (data[0] == 0x48 && data[1] == 0x49) // if MSG_HELLO1
|
---|
352 | ptr = data + 11;
|
---|
353 | else
|
---|
354 | ptr = data + 12;
|
---|
355 |
|
---|
356 | while (ptr < data + payload_len - footer_len - checkcode_len - 1) {
|
---|
357 | uint16_t caid;
|
---|
358 | uint32_t provid;
|
---|
359 | uint32_t provid1;
|
---|
360 |
|
---|
361 | if (ptr[0]==0x05) {
|
---|
362 | caid = ptr[0] << 8;
|
---|
363 | provid = ptr[1] << 16 | ptr[2] << 8 | ptr[3];
|
---|
364 | } else {
|
---|
365 | caid = ptr[0] << 8 | ptr[1];
|
---|
366 | provid = 0x00 << 16 | ptr[2] << 8 | ptr[3];
|
---|
367 | }
|
---|
368 | provid1 = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
|
---|
369 | int32_t ncards = ptr[4];
|
---|
370 |
|
---|
371 | ptr += 5;
|
---|
372 |
|
---|
373 | for (i = 0; i < ncards; i++) {
|
---|
374 | // for all n cards and current caid/provid,
|
---|
375 | // create card info from data and add card to peer.cards
|
---|
376 | struct gbox_card *card;
|
---|
377 | if (!cs_malloc(&card,sizeof(struct gbox_card)))
|
---|
378 | continue;
|
---|
379 |
|
---|
380 | card->caid = caid;
|
---|
381 | card->provid = provid;
|
---|
382 | card->provid_1 = provid1;
|
---|
383 | card->slot = ptr[0];
|
---|
384 | card->dist = ptr[1] & 0xf;
|
---|
385 | card->lvl = ptr[1] >> 4;
|
---|
386 | card->peer_id = ptr[2] << 8 | ptr[3];
|
---|
387 | ptr += 4;
|
---|
388 |
|
---|
389 | if ((cli->reader->gbox_maxdist >= card->dist) && (card->peer_id != gbox->id)) {
|
---|
390 |
|
---|
391 | LL_ITER it = ll_iter_create(gbox->peer.cards);
|
---|
392 | struct gbox_card *card_s;
|
---|
393 | uint8_t v_card=0;
|
---|
394 | while ((card_s = ll_iter_next(&it))) { // don't add card if already in peer.cards list
|
---|
395 | if (card_s->peer_id == card->peer_id && card_s->provid_1 == card->provid_1) {
|
---|
396 | gbox_free_card(card);
|
---|
397 | card=NULL;
|
---|
398 | v_card=1;
|
---|
399 | break;
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (v_card != 1) { // new card - not in list
|
---|
404 | card->badsids = ll_create("badsids");
|
---|
405 | card->goodsids = ll_create("goodsids");
|
---|
406 | ll_append(gbox->peer.cards, card);
|
---|
407 | ncards_in_msg++;
|
---|
408 | cs_debug_mask(D_READER," card: caid=%04x, provid=%06x, slot=%d, level=%d, dist=%d, peer=%04x",
|
---|
409 | card->caid, card->provid, card->slot, card->lvl, card->dist, card->peer_id);
|
---|
410 | }
|
---|
411 | } else { // don't add card
|
---|
412 | gbox_free_card(card);
|
---|
413 | card=NULL;
|
---|
414 | }
|
---|
415 | cli->reader->tcp_connected = 2; // we have card
|
---|
416 | } // end for ncards
|
---|
417 | } // end while caid/provid
|
---|
418 |
|
---|
419 | if ((data[0x0B] == 0) | (data[0x0B] == 0x80)) { // we've got peer hostname
|
---|
420 | NULLFREE(gbox->peer.hostname);
|
---|
421 |
|
---|
422 | if (!cs_malloc(&gbox->peer.hostname,hostname_len + 1)) {
|
---|
423 | cs_writeunlock(&gbox->lock);
|
---|
424 | return -1;
|
---|
425 | }
|
---|
426 |
|
---|
427 | memcpy(gbox->peer.hostname, data + payload_len - 1 - hostname_len, hostname_len);
|
---|
428 | gbox->peer.hostname[hostname_len] = '\0';
|
---|
429 |
|
---|
430 | memcpy(gbox->peer.checkcode, data + payload_len - footer_len - checkcode_len - 1, checkcode_len);
|
---|
431 | gbox->peer.ver = data[payload_len - footer_len - 1];
|
---|
432 | cli->gbox_ver = data[payload_len - footer_len - 1];
|
---|
433 | gbox->peer.type = data[payload_len - footer_len];
|
---|
434 |
|
---|
435 | if (gbox->hello_expired < 1) {
|
---|
436 | cs_log("<-Hello from %s (%s:%d)",cli->reader->label,cs_inet_ntoa(cli->ip), cli->reader->l_port);
|
---|
437 | gbox->hello_expired++;
|
---|
438 | gbox_send_hello(cli);
|
---|
439 | }
|
---|
440 | } // end if data[0x0B]
|
---|
441 |
|
---|
442 | if (gbox->hello_expired < 2 && data[0x0B] >= 0x80 && (cli->gbox_ver == 0x2C || cli->gbox_ver == 0x2D || cli->gbox_ver == 0x30)) {
|
---|
443 | gbox->hello_expired++;
|
---|
444 | gbox_send_hello(cli);
|
---|
445 | }
|
---|
446 |
|
---|
447 | if (data[0x0B]&0x80) {
|
---|
448 | gbox->hello_expired++;
|
---|
449 | gbox->peer.hello_cont=1;
|
---|
450 | gbox->peer.online = 1;
|
---|
451 |
|
---|
452 | cli->grp = cli->reader->gbox_grp;// cli->grp=0xFF;
|
---|
453 |
|
---|
454 | // create expire thread
|
---|
455 | pthread_t t;
|
---|
456 | pthread_attr_t attr;
|
---|
457 | pthread_attr_init(&attr);
|
---|
458 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
459 | int32_t ret = pthread_create(&t, &attr, (void *)gbox_expire_hello, cli);
|
---|
460 |
|
---|
461 | if (ret) {
|
---|
462 | cs_log("ERROR: can't create gbox expire thread (errno=%d %s)", ret, strerror(ret));
|
---|
463 | pthread_attr_destroy(&attr);
|
---|
464 | return -1;
|
---|
465 | } else
|
---|
466 | pthread_detach(t);
|
---|
467 |
|
---|
468 | pthread_attr_destroy(&attr);
|
---|
469 |
|
---|
470 | cli->reader->tcp_connected = 2; //we have card
|
---|
471 | cli->reader->card_status = CARD_INSERTED;
|
---|
472 | if (ll_count(gbox->peer.cards)==0)
|
---|
473 | cli->reader->card_status = NO_CARD;
|
---|
474 |
|
---|
475 | gbox_write_shared_cards_info();
|
---|
476 | gbox_write_version();
|
---|
477 | }
|
---|
478 | return 0;
|
---|
479 | }
|
---|
480 |
|
---|
481 | int32_t gbox_cmd_switch(struct s_client *cli, int32_t n)
|
---|
482 | {
|
---|
483 | struct gbox_data *gbox = cli->gbox;
|
---|
484 | uchar *data = gbox->buf;
|
---|
485 | int32_t n1=0, rc1=0, i1, idx;
|
---|
486 | uchar dcw[16];
|
---|
487 | char tmp[0x50];
|
---|
488 |
|
---|
489 | switch (gbox_decode_cmd(data)) {
|
---|
490 | case MSG_BOXINFO:
|
---|
491 | gbox->peer.hello_count=0;
|
---|
492 | gbox_send_hello(cli);
|
---|
493 | break;
|
---|
494 | case MSG_GOODBYE:
|
---|
495 | if (gbox->peer.goodbye_cont==5) {
|
---|
496 | gbox->peer.goodbye_cont=0;
|
---|
497 | gbox->peer.hello_count=0;
|
---|
498 | gbox_send_boxinfo(cli);
|
---|
499 | } else {
|
---|
500 | gbox->peer.goodbye_cont++;
|
---|
501 | }
|
---|
502 | break;
|
---|
503 | case MSG_HELLO1:
|
---|
504 | case MSG_HELLO:
|
---|
505 | if (gbox_cmd_hello(cli, n) < 0)
|
---|
506 | return -1;
|
---|
507 | break;
|
---|
508 | case MSG_CW:
|
---|
509 | memcpy(gbox->cws, data + 14, 16);
|
---|
510 | cli->last=time((time_t*)0);
|
---|
511 | idx=gbox_recv_chk(cli, dcw, &rc1, data, rc1);
|
---|
512 | if (idx<0) break; // no dcw received
|
---|
513 | if (!idx) idx=cli->last_idx;
|
---|
514 | cli->reader->last_g= time((time_t*)0); // for reconnect timeout
|
---|
515 | for (i1=0, n1=0; i1< cfg.max_pending && n1 == 0; i1++) {
|
---|
516 | if (cli->ecmtask[i1].idx==idx) {
|
---|
517 | cli->pending--;
|
---|
518 | casc_check_dcw(cli->reader, i1, rc1, dcw);
|
---|
519 | n1++;
|
---|
520 | }
|
---|
521 | }
|
---|
522 | break;
|
---|
523 | case MSG_CHECKCODE:
|
---|
524 | memcpy(gbox->peer.checkcode, data + 10, 7);
|
---|
525 | cs_debug_mask(D_READER, "gbox: received checkcode=%s", cs_hexdump(0, gbox->peer.checkcode, 7, tmp, sizeof(tmp)));
|
---|
526 | gbox->peer.hello_cont=0;
|
---|
527 | break;
|
---|
528 | case MSG_ECM: {
|
---|
529 | if (gbox->peer.hello_cont==0) break;
|
---|
530 |
|
---|
531 | gbox->peer.t_ecm = time((time_t*)0);
|
---|
532 |
|
---|
533 | ECM_REQUEST *er;
|
---|
534 | if (!(er=get_ecmtask())) break;
|
---|
535 |
|
---|
536 | struct gbox_ecm_info *ei;
|
---|
537 | if (!cs_malloc(&ei,sizeof(struct gbox_ecm_info))){
|
---|
538 | cs_writeunlock(&gbox->lock);
|
---|
539 | return -1;
|
---|
540 | }
|
---|
541 |
|
---|
542 | er->src_data = ei;
|
---|
543 | uchar *ecm = data + 18;
|
---|
544 |
|
---|
545 | gbox->peer.gbox_count_ecm++;
|
---|
546 | er->gbox_ecm_id = gbox->peer.id;
|
---|
547 |
|
---|
548 | if (gbox->peer.ecm_idx == 100)gbox->peer.ecm_idx=0;
|
---|
549 |
|
---|
550 | er->idx = gbox->peer.ecm_idx++;
|
---|
551 | er->ecmlen = ecm[2] + 3;
|
---|
552 |
|
---|
553 | er->pid = data[10] << 8 | data[11];
|
---|
554 | er->srvid = data[12] << 8 | data[13];
|
---|
555 |
|
---|
556 | int32_t adr_caid_1 = data[20] + 26;
|
---|
557 | if (data[adr_caid_1] == 0x05)
|
---|
558 | er->caid = (data[adr_caid_1]<<8);
|
---|
559 | else
|
---|
560 | er->caid = (data[adr_caid_1]<<8 | data[adr_caid_1+1]);
|
---|
561 |
|
---|
562 | ei->caid = (data[adr_caid_1]<<8 | data[adr_caid_1+1]);
|
---|
563 |
|
---|
564 | gbox->peer.last_caid=er->caid;
|
---|
565 | gbox->peer.last_srvid=er->srvid;
|
---|
566 | ei->extra = data[14] << 8 | data[15];
|
---|
567 | memcpy(er->ecm, data + 18, er->ecmlen);
|
---|
568 |
|
---|
569 | memcpy(ei->ecm, data, (-14 +n));
|
---|
570 | ei->l=(-14+n);
|
---|
571 | ei->id_cl_gbox=gbox->peer.id;
|
---|
572 |
|
---|
573 | ei->ncards = data[16];
|
---|
574 | ei->peer_cw = data[data[0x14]+0x1F] << 8 | data[data[0x14]+0x20];
|
---|
575 | ei->peer = ecm[er->ecmlen] << 8 | ecm[er->ecmlen + 1];
|
---|
576 | ei->version = ecm[er->ecmlen + 2];
|
---|
577 | ei->type = ecm[er->ecmlen + 4];
|
---|
578 | ei->slot = ecm[er->ecmlen + 12];
|
---|
579 | memcpy(ei->checksums, ecm + er->ecmlen + 14, 14);
|
---|
580 | er->gbox_crc = gbox_get_ecmchecksum(er);
|
---|
581 |
|
---|
582 | gbox->peer.t_ecm = time((time_t*)0);
|
---|
583 | cli->typ = 'p';
|
---|
584 | er->prid = chk_provid(er->ecm, er->caid);
|
---|
585 | cs_debug_mask(D_READER, "<- ECM (%d<-) from server (%s:%d) to cardserver (%04X) SID %02X%02X", data[-15 + n] + 1, gbox->peer.hostname,cli->port,ei->peer,data[0x0C],data[0x0D]);
|
---|
586 | get_cw(cli, er);
|
---|
587 | //TODO:gbox_cw_cache(cli,er);
|
---|
588 | break;
|
---|
589 | }
|
---|
590 | default:
|
---|
591 | cs_ddump_mask(D_READER, data, n, "gbox: unknown data received (%d bytes):", n);
|
---|
592 | } // end switch
|
---|
593 | return 0;
|
---|
594 | }
|
---|
595 |
|
---|
596 | int32_t gbox_peer_ip_unverified(IN_ADDR_T recv_ip, in_port_t recv_port, uchar *b, int32_t l)
|
---|
597 | {
|
---|
598 | uchar attc_key[4];
|
---|
599 | char no_hostname[64];
|
---|
600 | int32_t payload_len;
|
---|
601 | int32_t hostname_len;
|
---|
602 | uint32_t key = a2i("AF14E35C", 4);
|
---|
603 | int32_t i;
|
---|
604 |
|
---|
605 | for (i = 3; i >= 0; i--) {
|
---|
606 | attc_key[3 - i] = (key >> (8 * i)) & 0xff;
|
---|
607 | }
|
---|
608 | gbox_decrypt(b, l, attc_key);
|
---|
609 |
|
---|
610 | switch (gbox_decode_cmd(b)) {
|
---|
611 | case MSG_HELLO:
|
---|
612 | case MSG_HELLO1:
|
---|
613 | payload_len = l;
|
---|
614 | hostname_len = b[payload_len - 1];
|
---|
615 | gbox_decompress2(b, &payload_len);
|
---|
616 | if ((b[0x0B] == 0) | (b[0x0B] == 0x80)) { // we've got peer hostname
|
---|
617 | memcpy(no_hostname, b + payload_len - 1 - hostname_len, hostname_len);
|
---|
618 | no_hostname[hostname_len] = '\0';
|
---|
619 | }
|
---|
620 | break;
|
---|
621 |
|
---|
622 | case MSG_BOXINFO:
|
---|
623 | if (l>11) {
|
---|
624 | memcpy(no_hostname, b + 12, l-11); no_hostname[l-12] = '\0';
|
---|
625 | break;
|
---|
626 | }
|
---|
627 | memcpy(no_hostname, "No peer gbox 0", 14); no_hostname[14] = '\0';
|
---|
628 | break;
|
---|
629 |
|
---|
630 | default:
|
---|
631 | memcpy(no_hostname, "No peer gbox", 12); no_hostname[12] = '\0';
|
---|
632 | break;
|
---|
633 | }
|
---|
634 |
|
---|
635 | if (cs_add_violation_by_ip(recv_ip,recv_port,no_hostname)==0) {
|
---|
636 | FILE *fhandle = fopen(FILE_ATTACK_INFO, "a");
|
---|
637 | if (!fhandle) {
|
---|
638 | memcpy(no_hostname, "No peer gbox_3", 14); no_hostname[14] = '\0';
|
---|
639 | cs_add_violation_by_ip(recv_ip, recv_port, no_hostname);
|
---|
640 | return -1;
|
---|
641 | }
|
---|
642 | fprintf(fhandle,"gbox: ATACK ALERT: %s proxy %s:%d MSG_TYP %02X%02X my pass %02X%02X%02X%02X peer pass %02X%02X%02X%02X\n", no_hostname,cs_inet_ntoa(recv_ip), recv_port,b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8],b[9]);
|
---|
643 | fclose(fhandle);
|
---|
644 | }
|
---|
645 |
|
---|
646 | return 0;
|
---|
647 | }
|
---|
648 |
|
---|
649 | static int32_t gbox_recv2(IN_ADDR_T recv_ip, in_port_t recv_port, uchar *b, int32_t l)
|
---|
650 | {
|
---|
651 | int32_t verify_peer_ip = 0;
|
---|
652 | struct s_client *cli;
|
---|
653 | for (cli=first_client; cli; cli=cli->next) {
|
---|
654 |
|
---|
655 | if (IP_EQUAL(recv_ip, cli->ip)
|
---|
656 | && cli->gbox && recv_port == cli->reader->l_port) {
|
---|
657 |
|
---|
658 | verify_peer_ip = 1;
|
---|
659 |
|
---|
660 | struct gbox_data *gbox = cli->gbox;
|
---|
661 | uchar *data = gbox->buf;
|
---|
662 | char tmp[0x50];
|
---|
663 | if(cli->reader->gbox_maxdist == 0) {
|
---|
664 | cli->reader->gbox_maxdist=DEFAULT_GBOX_MAX_DIST;
|
---|
665 | }
|
---|
666 |
|
---|
667 | if (!gbox)
|
---|
668 | return -1;
|
---|
669 |
|
---|
670 | int32_t n=l;
|
---|
671 | memcpy(data ,b, l);
|
---|
672 | pthread_mutex_lock (&gbox->peer_online_mutex);
|
---|
673 | pthread_cond_signal(&gbox->peer_online_cond);
|
---|
674 | pthread_mutex_unlock (&gbox->peer_online_mutex);
|
---|
675 |
|
---|
676 | cs_writelock(&gbox->lock);
|
---|
677 |
|
---|
678 | cs_ddump_mask(D_READER, data, n, "gbox: encrypted data received (%d bytes):", n);
|
---|
679 | cs_ddump_mask(D_READER, gbox->key, 4, "gbox: key before decrypt:");
|
---|
680 |
|
---|
681 | if ((data[0]==0x48)&&(data[1]==0x49)) // if MSG_HELLO1
|
---|
682 | cs_log("test cs2gbox");
|
---|
683 | else
|
---|
684 | gbox_decrypt(data, n, gbox->key);
|
---|
685 |
|
---|
686 | // if my pass ok verify CW | pass to peer
|
---|
687 | if (data[0]==0x48 && data[1]==0x44){ // if MSG_CW
|
---|
688 | if (data[39] != cli->gbox_cw_id[0] && data[40] != cli->gbox_cw_id[1]){
|
---|
689 | cs_writeunlock(&gbox->lock);
|
---|
690 | continue; // next client
|
---|
691 | }
|
---|
692 | }
|
---|
693 |
|
---|
694 | cs_ddump_mask(D_READER, gbox->key, 4, "gbox: key after decrypt:");
|
---|
695 | cs_ddump_mask(D_READER, data, n, "gbox: decrypted received data (%d bytes):", n);
|
---|
696 |
|
---|
697 | //verify my pass received
|
---|
698 | if (data[2]==gbox->key[0] && data[3]==gbox->key[1] && data[4]==gbox->key[2] && data[5]==gbox->key[3]) {
|
---|
699 | cs_debug_mask(D_READER,"received data, peer : %04x data: %s",gbox->peer.id,cs_hexdump(0, data, l, tmp, sizeof(tmp)));
|
---|
700 |
|
---|
701 | if (data[0]!=0x48 && data[1]!=0x44) { // if MSG_CW
|
---|
702 | if (data[6]!=gbox->peer.key[0] && data[7]!=gbox->peer.key[1] && data[8]!=gbox->peer.key[2] && data[9]!=gbox->peer.key[3]) {
|
---|
703 | cs_writeunlock(&gbox->lock);
|
---|
704 | continue; // next client
|
---|
705 | }
|
---|
706 | }
|
---|
707 | } // error my pass
|
---|
708 | else
|
---|
709 | {
|
---|
710 | cs_log("gbox: ATACK ALERT: proxy %s:%d",cs_inet_ntoa(cli->ip), cli->reader->l_port);
|
---|
711 | cs_log("received data, peer : %04x data: %s",gbox->peer.id,cs_hexdump(0, data, n, tmp, sizeof(tmp)));
|
---|
712 | cs_writeunlock(&gbox->lock);
|
---|
713 | continue; // next client
|
---|
714 |
|
---|
715 | }
|
---|
716 |
|
---|
717 | if (gbox_cmd_switch(cli, n) < 0)
|
---|
718 | return -1;
|
---|
719 |
|
---|
720 | cs_writeunlock(&gbox->lock);
|
---|
721 | return 0;
|
---|
722 | } // end if ip ok
|
---|
723 |
|
---|
724 | }//end for client
|
---|
725 |
|
---|
726 | time_t now = time((time_t*)0);
|
---|
727 | int32_t lsec=0;
|
---|
728 | lsec = now - login_gbox;
|
---|
729 |
|
---|
730 | if((lsec > cfg.gbox_reconnect) || (main_1 == 1 && lsec > 30)) {
|
---|
731 | login_gbox=time((time_t*)0);
|
---|
732 | main_1=2;
|
---|
733 | gbox_reconnect_client();
|
---|
734 | }
|
---|
735 |
|
---|
736 | if(verify_peer_ip == 0) {
|
---|
737 | if (gbox_peer_ip_unverified(recv_ip, recv_port, b, l) < 0) return -1;
|
---|
738 | }
|
---|
739 |
|
---|
740 | return -1;
|
---|
741 | }
|
---|
742 |
|
---|
743 | ////////////////////////////////////////////////////////////////////////////////
|
---|
744 | // GBOX BUFFER ENCRYPTION/DECRYPTION (thanks to dvbcrypt@gmail.com)
|
---|
745 | ////////////////////////////////////////////////////////////////////////////////
|
---|
746 |
|
---|
747 | unsigned char Lookup_Table[0x40] = {
|
---|
748 | 0x25,0x38,0xD4,0xCD,0x17,0x7A,0x5E,0x6C,0x52,0x42,0xFE,0x68,0xAB,0x3F,0xF7,0xBE,
|
---|
749 | 0x47,0x57,0x71,0xB0,0x23,0xC1,0x26,0x6C,0x41,0xCE,0x94,0x37,0x45,0x04,0xA2,0xEA,
|
---|
750 | 0x07,0x58,0x35,0x55,0x08,0x2A,0x0F,0xE7,0xAC,0x76,0xF0,0xC1,0xE6,0x09,0x10,0xDD,
|
---|
751 | 0xC5,0x8D,0x2E,0xD9,0x03,0x9C,0x3D,0x2C,0x4D,0x41,0x0C,0x5E,0xDE,0xE4,0x90,0xAE
|
---|
752 | };
|
---|
753 |
|
---|
754 |
|
---|
755 | void gbox_encrypt8(unsigned char *buffer, unsigned char *pass)
|
---|
756 | {
|
---|
757 | int passcounter;
|
---|
758 | int bufcounter;
|
---|
759 | unsigned char temp;
|
---|
760 |
|
---|
761 | for(passcounter=0; passcounter<4; passcounter++)
|
---|
762 | for(bufcounter=7; bufcounter>=0; bufcounter--)
|
---|
763 | {
|
---|
764 | temp = ( buffer[bufcounter]>>2);
|
---|
765 | temp = pass[3];
|
---|
766 | pass[3] = (pass[3]/2)+(pass[2]&1)*0x80;
|
---|
767 | pass[2] = (pass[2]/2)+(pass[1]&1)*0x80;
|
---|
768 | pass[1] = (pass[1]/2)+(pass[0]&1)*0x80;
|
---|
769 | pass[0] = (pass[0]/2)+(temp &1)*0x80;
|
---|
770 | buffer[(bufcounter+1) & 7] = buffer[ (bufcounter+1) & 7 ] - Lookup_Table[ (buffer[bufcounter]>>2) & 0x3F ];
|
---|
771 | buffer[(bufcounter+1) & 7] = Lookup_Table[ ( buffer[bufcounter] - pass[(bufcounter+1) & 3] ) & 0x3F ] ^ buffer[ (bufcounter+1) & 7 ];
|
---|
772 | buffer[(bufcounter+1) & 7] = buffer[ (bufcounter+1) & 7 ] - pass[(bufcounter & 3)];
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | void gbox_decrypt8(unsigned char *buffer,unsigned char *pass)
|
---|
777 | {
|
---|
778 | unsigned char temp;
|
---|
779 | int bufcounter;
|
---|
780 | int passcounter;
|
---|
781 | for( passcounter=3; passcounter>=0; passcounter--)
|
---|
782 | for( bufcounter=0; bufcounter<=7; bufcounter++) {
|
---|
783 | buffer[(bufcounter+1)&7] = pass[bufcounter&3] + buffer[(bufcounter+1)&7];
|
---|
784 | temp = buffer[bufcounter] - pass[(bufcounter+1)&3];
|
---|
785 | buffer[(bufcounter+1)&7] = Lookup_Table[temp &0x3F] ^ buffer[(bufcounter+1)&7];
|
---|
786 | temp = buffer[bufcounter] >> 2;
|
---|
787 | buffer[(bufcounter+1)&7] = Lookup_Table[temp & 0x3F] + buffer[(bufcounter+1)&7];
|
---|
788 |
|
---|
789 | temp = pass[0] & 0x80;
|
---|
790 | pass[0] = ( (pass[1]&0x80)>>7 ) + (pass[0]<<1);
|
---|
791 | pass[1] = ( (pass[2]&0x80)>>7 ) + (pass[1]<<1);
|
---|
792 | pass[2] = ( (pass[3]&0x80)>>7 ) + (pass[2]<<1);
|
---|
793 | pass[3] = ( temp>>7 ) + (pass[3]<<1);
|
---|
794 | }
|
---|
795 |
|
---|
796 | }
|
---|
797 |
|
---|
798 | void gbox_decryptB(unsigned char *buffer, int bufsize, uchar *localkey)
|
---|
799 | {
|
---|
800 | int counter;
|
---|
801 | gbox_encrypt8(&buffer[bufsize-9], localkey);
|
---|
802 | gbox_decrypt8(buffer, localkey);
|
---|
803 | for (counter=bufsize-2; counter>=0; counter--)
|
---|
804 | buffer[counter] = buffer[counter+1] ^ buffer[counter];
|
---|
805 | }
|
---|
806 |
|
---|
807 | void gbox_encryptB(unsigned char *buffer, int bufsize, uchar *key)
|
---|
808 | {
|
---|
809 | int counter;
|
---|
810 | for (counter=0; counter<(bufsize-1); counter++)
|
---|
811 | buffer[counter] = buffer[counter+1] ^ buffer[counter];
|
---|
812 | gbox_encrypt8(buffer, key);
|
---|
813 | gbox_decrypt8(&buffer[bufsize-9], key);
|
---|
814 | }
|
---|
815 |
|
---|
816 | void gbox_encryptA(unsigned char *buffer, unsigned char *pass)
|
---|
817 | {
|
---|
818 | int counter;
|
---|
819 | unsigned char temp;
|
---|
820 | for (counter=0x1F; counter>=0; counter--) {
|
---|
821 | temp = pass[3]&1;
|
---|
822 | pass[3] = ((pass[2]&1)<<7) + (pass[3]>>1);
|
---|
823 | pass[2] = ((pass[1]&1)<<7) + (pass[2]>>1);
|
---|
824 | pass[1] = ((pass[0]&1)<<7) + (pass[1]>>1);
|
---|
825 | pass[0] = (temp<<7) + (pass[0]>>1);
|
---|
826 | temp = ( pass[(counter+1)&3] ^ buffer[counter&7] ) >> 2;
|
---|
827 | buffer[(counter+1)&7] = Lookup_Table[temp & 0x3F]*2 + buffer[ (counter+1) & 7 ];
|
---|
828 | temp = buffer[counter&7] - pass[(counter+1) & 3];
|
---|
829 | buffer[(counter+1)&7] = Lookup_Table[temp & 0x3F] ^ buffer[(counter+1)&7];
|
---|
830 | buffer[(counter+1)&7] = pass[counter&3] + buffer[(counter+1)&7];
|
---|
831 | }
|
---|
832 | }
|
---|
833 |
|
---|
834 | void gbox_decryptA(unsigned char *buffer, unsigned char *pass)
|
---|
835 | {
|
---|
836 | int counter;
|
---|
837 | unsigned char temp;
|
---|
838 | for (counter=0; counter<=0x1F; counter++) {
|
---|
839 | buffer[(counter+1)&7] = buffer[(counter+1)&7] - pass[counter&3];
|
---|
840 | temp = buffer[counter&7] - pass[(counter+1)&3];
|
---|
841 | buffer[(counter+1)&7] = Lookup_Table[temp&0x3F] ^ buffer[(counter+1)&7];
|
---|
842 | temp = ( pass[ (counter+1) & 3] ^ buffer[counter & 7] ) >> 2;
|
---|
843 | buffer[(counter+1) & 7] = buffer[(counter+1)&7] - Lookup_Table[temp & 0x3F]*2;
|
---|
844 | temp = pass[0]&0x80;
|
---|
845 | pass[0] = ((pass[1]&0x80)>>7) + (pass[0]<<1);
|
---|
846 | pass[1] = ((pass[2]&0x80)>>7) + (pass[1]<<1);
|
---|
847 | pass[2] = ((pass[3]&0x80)>>7) + (pass[2]<<1);
|
---|
848 | pass[3] = (temp>>7) + (pass[3]<<1);
|
---|
849 | }
|
---|
850 | }
|
---|
851 |
|
---|
852 | void gbox_encrypt(uchar *buffer, int bufsize, uchar *key)
|
---|
853 | {
|
---|
854 | gbox_encryptA(buffer, key);
|
---|
855 | gbox_encryptB(buffer, bufsize, key);
|
---|
856 | }
|
---|
857 |
|
---|
858 | void gbox_decrypt(uchar *buffer, int bufsize, uchar *localkey)
|
---|
859 | {
|
---|
860 | gbox_decryptB(buffer, bufsize, localkey);
|
---|
861 | gbox_decryptA(buffer, localkey);
|
---|
862 | }
|
---|
863 |
|
---|
864 | static void gbox_compress(struct gbox_data *UNUSED(gbox), uchar *buf, int32_t unpacked_len, int32_t *packed_len)
|
---|
865 | {
|
---|
866 | unsigned char *tmp, *tmp2;
|
---|
867 | lzo_voidp wrkmem;
|
---|
868 |
|
---|
869 | if (!cs_malloc(&tmp, 0x40000)) {
|
---|
870 | return;
|
---|
871 | }
|
---|
872 | if (!cs_malloc(&tmp2,0x40000)) {
|
---|
873 | free(tmp);
|
---|
874 | return;
|
---|
875 | }
|
---|
876 |
|
---|
877 | if (!cs_malloc(&wrkmem, unpacked_len * 0x1000)) {
|
---|
878 | free(tmp);
|
---|
879 | free(tmp2);
|
---|
880 | return;
|
---|
881 | }
|
---|
882 |
|
---|
883 | unpacked_len -= 12;
|
---|
884 | memcpy(tmp2, buf + 12, unpacked_len);
|
---|
885 |
|
---|
886 | lzo_init();
|
---|
887 |
|
---|
888 | lzo_uint pl = 0;
|
---|
889 | if (lzo1x_1_compress(tmp2, unpacked_len, tmp, &pl, wrkmem) != LZO_E_OK)
|
---|
890 | cs_log("gbox: compression failed!");
|
---|
891 |
|
---|
892 | memcpy(buf + 12, tmp, pl);
|
---|
893 | pl += 12;
|
---|
894 |
|
---|
895 | free(tmp);
|
---|
896 | free(tmp2);
|
---|
897 | free(wrkmem);
|
---|
898 |
|
---|
899 | *packed_len = pl;
|
---|
900 | }
|
---|
901 |
|
---|
902 | static void gbox_decompress(struct gbox_data *UNUSED(gbox), uchar *buf, int32_t *unpacked_len)
|
---|
903 | {
|
---|
904 | uchar *tmp;
|
---|
905 |
|
---|
906 | if (!cs_malloc(&tmp, 0x40000))
|
---|
907 | return;
|
---|
908 | int err;
|
---|
909 | int len = *unpacked_len - 12;
|
---|
910 | *unpacked_len = 0x40000;
|
---|
911 |
|
---|
912 | lzo_init();
|
---|
913 | cs_debug_mask(D_READER, "decompressing %d bytes",len);
|
---|
914 | if ((err=lzo1x_decompress_safe(buf + 12, len, tmp, (lzo_uint *)unpacked_len, NULL)) != LZO_E_OK)
|
---|
915 | cs_debug_mask(D_READER, "gbox: decompression failed! errno=%d", err);
|
---|
916 |
|
---|
917 | memcpy(buf + 12, tmp, *unpacked_len);
|
---|
918 | *unpacked_len += 12;
|
---|
919 | free(tmp);
|
---|
920 | }
|
---|
921 |
|
---|
922 | static int32_t gbox_decode_cmd(uchar *buf)
|
---|
923 | {
|
---|
924 | return buf[0] << 8 | buf[1];
|
---|
925 | }
|
---|
926 |
|
---|
927 | void gbox_code_cmd(uchar *buf, int16_t cmd)
|
---|
928 | {
|
---|
929 | buf[0] = cmd >> 8;
|
---|
930 | buf[1] = cmd & 0xff;
|
---|
931 | }
|
---|
932 |
|
---|
933 | static void gbox_calc_checkcode(struct gbox_data *gbox)
|
---|
934 | {
|
---|
935 | gbox->checkcode[0] = 0x00;
|
---|
936 | gbox->checkcode[1] = 0x03;
|
---|
937 | gbox->checkcode[2] = 0x04;
|
---|
938 | gbox->checkcode[3] = 0x30;
|
---|
939 | gbox->checkcode[4] = 0x01; // reader number
|
---|
940 | gbox->checkcode[5] = 0x25;
|
---|
941 | gbox->checkcode[6] = 0x88;
|
---|
942 | }
|
---|
943 |
|
---|
944 | uint32_t gbox_get_ecmchecksum(ECM_REQUEST *er)
|
---|
945 | {
|
---|
946 |
|
---|
947 | uint8_t checksum[4];
|
---|
948 | int32_t counter;
|
---|
949 |
|
---|
950 | uchar ecm[255];
|
---|
951 | memcpy(ecm, er->ecm, er->ecmlen);
|
---|
952 |
|
---|
953 | checksum[3] = ecm[0];
|
---|
954 | checksum[2] = ecm[1];
|
---|
955 | checksum[1] = ecm[2];
|
---|
956 | checksum[0] = ecm[3];
|
---|
957 |
|
---|
958 | for (counter=1; counter < (er->ecmlen/4) - 4; counter++) {
|
---|
959 | checksum[3] ^= ecm[counter*4];
|
---|
960 | checksum[2] ^= ecm[counter*4+1];
|
---|
961 | checksum[1] ^= ecm[counter*4+2];
|
---|
962 | checksum[0] ^= ecm[counter*4+3];
|
---|
963 | }
|
---|
964 |
|
---|
965 | return checksum[3] << 24 | checksum[2] << 16 | checksum[1] << 8 | checksum[0];
|
---|
966 | }
|
---|
967 |
|
---|
968 | /*
|
---|
969 | static void gbox_handle_gsms(uint16_t peerid, char *gsms)
|
---|
970 | {
|
---|
971 | cs_log("gbox: gsms received from peer %04x: %s", peerid, gsms);
|
---|
972 |
|
---|
973 | if (cfg.gbox_gsms_path) {
|
---|
974 | FILE *f = fopen(cfg.gbox_gsms_path, "a");
|
---|
975 | if (f) {
|
---|
976 | f//printf(f, "FROM %04X: %s\n", peerid, gsms);
|
---|
977 | fclose(f);
|
---|
978 | }
|
---|
979 | else
|
---|
980 | cs_log("gbox: error writing to file! (path=%s)", cfg.gbox_gsms_path);
|
---|
981 | }
|
---|
982 | }
|
---|
983 | */
|
---|
984 |
|
---|
985 | static void gbox_expire_hello(struct s_client *cli)
|
---|
986 | {
|
---|
987 | //printf("gbox: enter gbox_expire_hello()\n");
|
---|
988 | struct gbox_data *gbox = cli->gbox;
|
---|
989 |
|
---|
990 | set_thread_name(__func__);
|
---|
991 |
|
---|
992 | pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
|
---|
993 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
---|
994 |
|
---|
995 | struct timespec ts;
|
---|
996 | struct timeval tv;
|
---|
997 | gettimeofday(&tv, NULL);
|
---|
998 | ts.tv_sec = tv.tv_sec;
|
---|
999 | ts.tv_nsec = tv.tv_usec * 1000;
|
---|
1000 | ts.tv_sec += 5;
|
---|
1001 |
|
---|
1002 | pthread_mutex_lock (&mut);
|
---|
1003 | if (pthread_cond_timedwait(&cond, &mut, &ts) == ETIMEDOUT) {
|
---|
1004 | //printf("gbox: hello expired!\n");
|
---|
1005 | gbox->hello_expired = 0;
|
---|
1006 | }
|
---|
1007 | pthread_mutex_unlock (&mut);
|
---|
1008 | //printf("gbox: exit gbox_expire_hello()\n");
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | /* static void gbox_wait_for_response(struct s_client *cli)
|
---|
1012 | {
|
---|
1013 | //printf("gbox: enter gbox_wait_for_response()\n");
|
---|
1014 | //cs_debug_mask(D_READER, "gbox: enter gbox_wait_for_response()");
|
---|
1015 | struct gbox_data *gbox = cli->gbox;
|
---|
1016 | struct timespec ts;
|
---|
1017 | struct timeval tv;
|
---|
1018 | gettimeofday(&tv, NULL);
|
---|
1019 | ts.tv_sec = tv.tv_sec;
|
---|
1020 | ts.tv_nsec = tv.tv_usec * 1000;
|
---|
1021 | ts.tv_sec += 5;
|
---|
1022 |
|
---|
1023 | pthread_mutex_lock (&gbox->peer_online_mutex);
|
---|
1024 | if (pthread_cond_timedwait(&gbox->peer_online_cond, &gbox->peer_online_mutex, &ts) == ETIMEDOUT) {
|
---|
1025 | gbox->peer.fail_count++;
|
---|
1026 | //printf("gbox: wait timed-out, fail_count=%d\n", gbox->peer.fail_count);
|
---|
1027 | #define GBOX_FAIL_COUNT 1
|
---|
1028 | if (gbox->peer.fail_count >= GBOX_FAIL_COUNT) {
|
---|
1029 | gbox->peer.online = 0;
|
---|
1030 | //printf("gbox: fail_count >= %d, peer is offline\n", GBOX_FAIL_COUNT);
|
---|
1031 | }
|
---|
1032 | //cs_debug_mask(D_READER, "gbox: wait timed-out, fail_count=%d\n", gbox->peer.fail_count);
|
---|
1033 | } else {
|
---|
1034 | gbox->peer.fail_count = 0;
|
---|
1035 | //printf("gbox: cond posted, peer is online\n");
|
---|
1036 | }
|
---|
1037 | pthread_mutex_unlock (&gbox->peer_online_mutex);
|
---|
1038 |
|
---|
1039 | //cs_debug_mask(D_READER, "gbox: exit gbox_wait_for_response()");
|
---|
1040 | //printf("gbox: exit gbox_wait_for_response()\n");
|
---|
1041 | } */
|
---|
1042 |
|
---|
1043 | static void gbox_send(struct s_client *cli, uchar *buf, int32_t l)
|
---|
1044 | {
|
---|
1045 | struct gbox_data *gbox = cli->gbox;
|
---|
1046 | uchar key_v[4] = {buf[2], buf[3], buf[4], buf[5]};
|
---|
1047 | if (key_v[0]!=gbox->peer.key[0] && key_v[1]!=gbox->peer.key[1] && key_v[2]!=gbox->peer.key[2] && key_v[3]!=gbox->peer.key[3]) {
|
---|
1048 | cs_log("gbox: VERIFY send proxy ,gbox->peer.key: %02x%02x%02x%02x",gbox->peer.key[0],gbox->peer.key[1],gbox->peer.key[2],gbox->peer.key[3]);
|
---|
1049 | cs_log("gbox: VERIFY send proxy ,key_v : %02x%02x%02x%02x",key_v[0],key_v[1],key_v[2],key_v[3]);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | cs_ddump_mask(D_READER, buf, l, "gbox: decrypted data send (%d bytes):", l);
|
---|
1053 | // cs_ddump_mask(D_READER, gbox->key, 4, "gbox: key before encrypt:");
|
---|
1054 |
|
---|
1055 | hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
|
---|
1056 | SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
|
---|
1057 | SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)cli->reader->r_port);
|
---|
1058 |
|
---|
1059 | gbox_encrypt(buf, l, gbox->peer.key);
|
---|
1060 | sendto(cli->udp_fd, buf, l, 0, (struct sockaddr *)&cli->udp_sa, cli->udp_sa_len);
|
---|
1061 |
|
---|
1062 | // cs_ddump_mask(D_READER, gbox->key, 4, "gbox: key after encrypt:");
|
---|
1063 | cs_ddump_mask(D_READER, buf, l, "gbox: encrypted data send (%d bytes):", l);
|
---|
1064 |
|
---|
1065 | // pthread_t t;
|
---|
1066 | // pthread_create(&t, NULL, (void *)gbox_wait_for_response, cli);
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | static void gbox_send_boxinfo(struct s_client *cli)
|
---|
1070 | {
|
---|
1071 | struct gbox_data *gbox = cli->gbox;
|
---|
1072 |
|
---|
1073 | uchar buf[256];
|
---|
1074 |
|
---|
1075 | int32_t hostname_len = strlen(cfg.gbox_hostname);
|
---|
1076 |
|
---|
1077 | gbox_code_cmd(buf, MSG_BOXINFO);
|
---|
1078 | memcpy(buf + 2, gbox->peer.key, 4);
|
---|
1079 | memcpy(buf + 6, gbox->key, 4);
|
---|
1080 | buf[10] = gbox->peer.ver;
|
---|
1081 | buf[11] = 0x10;
|
---|
1082 | memcpy(buf + 12, cfg.gbox_hostname, hostname_len);
|
---|
1083 |
|
---|
1084 | gbox_send(cli, buf, 12+hostname_len);
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 |
|
---|
1088 | /* static void gbox_send_goodbye(struct s_client *cli)
|
---|
1089 | {
|
---|
1090 | struct gbox_data *gbox = cli->gbox;
|
---|
1091 |
|
---|
1092 | uchar buf[20];
|
---|
1093 |
|
---|
1094 | gbox_code_cmd(buf, MSG_GOODBYE);
|
---|
1095 | memcpy(buf + 2, gbox->peer.key, 4);
|
---|
1096 | memcpy(buf + 6, gbox->key, 4);
|
---|
1097 |
|
---|
1098 | gbox_send(cli, buf, 11);
|
---|
1099 | } */
|
---|
1100 |
|
---|
1101 | static void gbox_send_hello(struct s_client *cli)
|
---|
1102 | {
|
---|
1103 | struct gbox_data *gbox = cli->gbox;
|
---|
1104 |
|
---|
1105 | gbox_calc_checkcode(gbox);
|
---|
1106 | int32_t len;
|
---|
1107 | int32_t buf_index=12;
|
---|
1108 | int32_t nbcards=0;
|
---|
1109 | int32_t i;
|
---|
1110 | uchar buf[2048];
|
---|
1111 | char card_reshare;
|
---|
1112 | int32_t ok = 0;
|
---|
1113 | #ifdef WEBIF
|
---|
1114 | ok = check_ip(cfg.http_allowed, cli->ip) ? 1 : 0;
|
---|
1115 | #endif
|
---|
1116 | int32_t hostname_len = strlen(cfg.gbox_hostname);
|
---|
1117 |
|
---|
1118 | // TODO build local card list
|
---|
1119 | if (!gbox->local_cards) {
|
---|
1120 | gbox_free_cardlist(gbox->local_cards);
|
---|
1121 | }
|
---|
1122 | gbox->local_cards = ll_create("local_cards");
|
---|
1123 | int8_t slot = 0;
|
---|
1124 |
|
---|
1125 | // send local card
|
---|
1126 | for (i = 0; i < cfg.gbox_local_cards_num; i++) {
|
---|
1127 | struct gbox_card *c;
|
---|
1128 | if (!cs_malloc(&c,sizeof(struct gbox_card))) {
|
---|
1129 | continue;
|
---|
1130 | }
|
---|
1131 | c->provid = cfg.gbox_card[i];
|
---|
1132 | c->peer_id=gbox->id;c->slot = ++slot;
|
---|
1133 | if (!ok) {
|
---|
1134 | c->dist = 1;
|
---|
1135 | } else {
|
---|
1136 | c->dist = 0;
|
---|
1137 | }
|
---|
1138 | ll_append(gbox->local_cards, c);
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | // currently this will only work for initialised local cards or single remote cards
|
---|
1142 | struct s_client *cl;
|
---|
1143 | for (cl=first_client; cl; cl=cl->next) {
|
---|
1144 | struct s_reader *rdr = cl->reader;
|
---|
1145 | if (rdr) {
|
---|
1146 | if (rdr->card_status == CARD_INSERTED) {
|
---|
1147 | for (i = 0; i < rdr->nprov; i++) {
|
---|
1148 | struct gbox_card *c;
|
---|
1149 | if (!cs_malloc(&c, sizeof(struct gbox_card)))
|
---|
1150 | continue;
|
---|
1151 | c->provid = rdr->caid << 16 | rdr->prid[i][2] << 8 | rdr->prid[i][3];
|
---|
1152 | c->peer_id = gbox->id;
|
---|
1153 | c->slot = ++slot;
|
---|
1154 | c->dist = 1;
|
---|
1155 | ll_append(gbox->local_cards, c);
|
---|
1156 | }
|
---|
1157 | // if rdr = gbox, reshare card
|
---|
1158 | if (cl->gbox) {
|
---|
1159 | struct gbox_data *gbox_1 = cl->gbox;
|
---|
1160 | struct gbox_card *card_g;
|
---|
1161 | if (strcmp(cli->reader->label, cl->reader->label)) {
|
---|
1162 | LL_ITER it = ll_iter_create(gbox_1->peer.cards);
|
---|
1163 | while ((card_g = ll_iter_next(&it))) {
|
---|
1164 | struct gbox_card *c;
|
---|
1165 | if (!cs_malloc(&c, sizeof(struct gbox_card)))
|
---|
1166 | continue;
|
---|
1167 | c->peer_id = card_g->peer_id;
|
---|
1168 | c->slot = card_g->slot ;
|
---|
1169 | if ((!ok) && (card_g->dist == 1)){
|
---|
1170 | c->dist = card_g->dist+1;
|
---|
1171 | } else {
|
---|
1172 | c->dist = card_g->dist;
|
---|
1173 | }
|
---|
1174 | c->provid = card_g->provid_1;
|
---|
1175 | if (card_g->lvl != 0 && card_g->dist <= 3) {
|
---|
1176 | ll_append(gbox->local_cards, c);
|
---|
1177 | } else {
|
---|
1178 | gbox_free_card(c);c=NULL;
|
---|
1179 | }
|
---|
1180 | } //end of ll_iter_nextcli->reader->label
|
---|
1181 | }
|
---|
1182 | } //end typ
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 | gbox->peer.hello_count=0;
|
---|
1187 | if (ll_count(gbox->local_cards) != 0) {
|
---|
1188 | len = 22 + hostname_len + ll_count(gbox->local_cards) * 9;
|
---|
1189 | memset(buf, 0, sizeof(buf));
|
---|
1190 | gbox_code_cmd(buf, MSG_HELLO);
|
---|
1191 | memcpy(buf + 2, gbox->peer.key, 4);
|
---|
1192 | memcpy(buf + 6, gbox->key, 4);
|
---|
1193 | buf[10] = 0x00;//gbox->hello_initial ^ 1; // initial HELLO = 0, subsequent = 1
|
---|
1194 | buf[11] = gbox->peer.hello_count; // TODO this needs adjusting to allow for packet splitting
|
---|
1195 | uchar *ptr = buf + 11;
|
---|
1196 |
|
---|
1197 | if(cli->reader->gbox_reshare > 5)
|
---|
1198 | card_reshare = 5 * 0x10;
|
---|
1199 | else
|
---|
1200 | card_reshare = cli->reader->gbox_reshare * 0x10;
|
---|
1201 |
|
---|
1202 | LL_ITER it = ll_iter_create(gbox->local_cards);
|
---|
1203 | struct gbox_card *card;
|
---|
1204 | while ((card = ll_iter_next(&it))) {
|
---|
1205 | card->lvl=card_reshare + card->dist;
|
---|
1206 | *(++ptr) = card->provid >> 24;
|
---|
1207 | *(++ptr) = card->provid >> 16;
|
---|
1208 | *(++ptr) = card->provid >> 8;
|
---|
1209 | *(++ptr) = card->provid & 0xff;
|
---|
1210 | *(++ptr) = 1;
|
---|
1211 | *(++ptr) = card->slot;
|
---|
1212 | *(++ptr) = card->lvl;
|
---|
1213 | *(++ptr) = card->peer_id >> 8;
|
---|
1214 | *(++ptr) = card->peer_id & 0xff;
|
---|
1215 | nbcards++;
|
---|
1216 | if (nbcards == 100) {
|
---|
1217 | len = 22 + hostname_len + 900;
|
---|
1218 | break;
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | len = 22 + hostname_len + (nbcards * 9);
|
---|
1223 | gbox_calc_checkcode(gbox);
|
---|
1224 |
|
---|
1225 | if (gbox->peer.hello_count==0) {
|
---|
1226 | memcpy(++ptr, gbox->checkcode, 7);
|
---|
1227 | ptr += 7;
|
---|
1228 | *ptr = gbox_version_low_byte;
|
---|
1229 | *(++ptr) = gbox_type_dvb;
|
---|
1230 | memcpy(++ptr, cfg.gbox_hostname, hostname_len);
|
---|
1231 | *(ptr + hostname_len) = hostname_len;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | cs_log("gbox: send hello total cards %d to %s",ll_count(gbox->local_cards),cli->reader->label);
|
---|
1235 | gbox_compress(gbox, buf, len, &len);
|
---|
1236 |
|
---|
1237 | cs_ddump_mask(D_READER, buf, len, "send hello, compressed (len=%d):", len);
|
---|
1238 |
|
---|
1239 | gbox_send(cli, buf, len);
|
---|
1240 | gbox->peer.hello_count++;
|
---|
1241 | } // end if local card exists
|
---|
1242 |
|
---|
1243 | buf[0] = MSG_HELLO >> 8;
|
---|
1244 | buf[1] = MSG_HELLO & 0xff;
|
---|
1245 | memcpy(buf + 2, gbox->peer.key, 4);
|
---|
1246 | memcpy(buf + 6, gbox->key, 4);
|
---|
1247 | buf[10] = 0x00;
|
---|
1248 | buf[11] = 0x80 | gbox->peer.hello_count;
|
---|
1249 | len = 12;
|
---|
1250 |
|
---|
1251 | if (gbox->peer.hello_count==0) {
|
---|
1252 | buf_index=12;
|
---|
1253 | memcpy(buf + buf_index, gbox->checkcode, 7);
|
---|
1254 | buf_index=buf_index+7;
|
---|
1255 | buf[buf_index] = gbox_version_low_byte;
|
---|
1256 | buf_index++;
|
---|
1257 | buf[buf_index] = gbox_type_dvb;
|
---|
1258 | buf_index++;
|
---|
1259 | memcpy(buf + buf_index, cfg.gbox_hostname, hostname_len);
|
---|
1260 | buf[buf_index + hostname_len] = hostname_len;
|
---|
1261 | len=buf_index + hostname_len + 1;
|
---|
1262 | }
|
---|
1263 | gbox_compress(gbox, buf, len, &len); // TODO: remove _re
|
---|
1264 |
|
---|
1265 | gbox_send(cli, buf, len);
|
---|
1266 | gbox->peer.hello_count = 0;
|
---|
1267 | gbox_free_cardlist(gbox->local_cards);
|
---|
1268 |
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | static int32_t gbox_recv(struct s_client *cli, uchar *UNUSED(b), int32_t l)
|
---|
1272 | {
|
---|
1273 | struct gbox_data *gbox = cli->gbox;
|
---|
1274 | if (!gbox)
|
---|
1275 | return -1;
|
---|
1276 |
|
---|
1277 | uchar *data = gbox->buf;
|
---|
1278 | int32_t n=l;
|
---|
1279 |
|
---|
1280 | pthread_mutex_lock (&gbox->peer_online_mutex);
|
---|
1281 | pthread_cond_signal(&gbox->peer_online_cond);
|
---|
1282 | pthread_mutex_unlock (&gbox->peer_online_mutex);
|
---|
1283 |
|
---|
1284 | cs_writelock(&gbox->lock);
|
---|
1285 |
|
---|
1286 | struct sockaddr_in r_addr;
|
---|
1287 | socklen_t lenn = sizeof(struct sockaddr_in);
|
---|
1288 | if ((n = recvfrom(cli->udp_fd, data, sizeof(gbox->buf), 0, (struct sockaddr *)&r_addr, &lenn)) < 8) {
|
---|
1289 | cs_log("gbox: invalid recvfrom!!!");
|
---|
1290 | cs_writeunlock(&gbox->lock);
|
---|
1291 | return -1;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | cs_writeunlock(&gbox->lock);
|
---|
1295 |
|
---|
1296 | gbox_recv2(SIN_GET_ADDR(r_addr), cli->reader->l_port, data, n);
|
---|
1297 |
|
---|
1298 | return -1;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | static void gbox_send_dcw(struct s_client *cli, ECM_REQUEST *er)
|
---|
1302 | {
|
---|
1303 | struct gbox_data *gbox = cli->gbox;
|
---|
1304 |
|
---|
1305 | gbox->peer.gbox_count_ecm--;
|
---|
1306 | if( er->rc >= E_NOTFOUND ) {
|
---|
1307 | cs_debug_mask(D_READER,"gbox: unable to decode!");
|
---|
1308 | return;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | uchar buf[50];
|
---|
1312 | uint32_t crc = gbox_get_ecmchecksum(er);
|
---|
1313 | struct gbox_ecm_info *ei = er->src_data;
|
---|
1314 |
|
---|
1315 | memset(buf, 0, sizeof(buf));
|
---|
1316 |
|
---|
1317 | gbox_code_cmd(buf, MSG_CW);
|
---|
1318 | buf[2] = ei->ecm[6];
|
---|
1319 | buf[3] = ei->ecm[7];
|
---|
1320 | buf[4] = ei->ecm[8];
|
---|
1321 | buf[5] = ei->ecm[9];
|
---|
1322 | buf[6] = ei->ecm[10];
|
---|
1323 | buf[7] = ei->ecm[11];
|
---|
1324 | buf[8] = ei->ecm[12];
|
---|
1325 | buf[9] = ei->ecm[13];
|
---|
1326 | buf[10] = ei->peer_cw >> 8;
|
---|
1327 | buf[11] = ei->peer_cw & 0xff;
|
---|
1328 | buf[12] = (ei->ecm[18] & 0xF) | 0x10;
|
---|
1329 | buf[13] = er->prid;
|
---|
1330 | memcpy(buf + 14, er->cw, 16);
|
---|
1331 | buf[30] = crc >> 24;
|
---|
1332 | buf[31] = crc >> 16;
|
---|
1333 | buf[32] = crc >> 8;
|
---|
1334 | buf[33] = crc & 0xff;
|
---|
1335 | buf[34] = ei->ecm[ei->ecm[20] + 26];
|
---|
1336 | buf[35] = ei->ecm[ei->ecm[20] + 27];
|
---|
1337 | buf[36] = ei->ecm[16];
|
---|
1338 | buf[37] = 0;
|
---|
1339 | buf[38] = 0;
|
---|
1340 | buf[39] = ei->ecm[ei->ecm[20] + 21];
|
---|
1341 | buf[40] = ei->ecm[ei->ecm[20] + 22];
|
---|
1342 | buf[41] = 5;
|
---|
1343 | buf[42] = -128;
|
---|
1344 | buf[43] = ei->ecm[ei->ecm[20] + 24];
|
---|
1345 | memcpy(&buf[44], &ei->ecm[ei->l - ei->ecm[ei->l - 1] - 1], ei->ecm[ei->l - 1] + 1);
|
---|
1346 | gbox_send(cli, buf, ei->ecm[ei->l - 1] +45);
|
---|
1347 | cs_debug_mask(D_READER, "-> CW (->%d) from %s/%d (%04X) ",buf[ei->ecm[ei->l] + 34] + 1,cli->reader->label, cli->port, ei->peer);
|
---|
1348 | if (er->src_data) {
|
---|
1349 | free(er->src_data);
|
---|
1350 | er->src_data = NULL;
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | static int32_t gbox_client_init(struct s_client *cli)
|
---|
1355 | {
|
---|
1356 | if (!cfg.gbox_hostname || strlen(cfg.gbox_hostname) > 128) {
|
---|
1357 | cs_log("gbox: error, no/invalid hostname '%s' configured in oscam.conf!",
|
---|
1358 | cfg.gbox_hostname ? cfg.gbox_hostname : "");
|
---|
1359 | return -1;
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | if (!cli->reader->gbox_my_password || strlen(cli->reader->gbox_my_password) != 8) {
|
---|
1363 | cs_log("gbox: error, no/invalid password '%s' configured in oscam.conf!",
|
---|
1364 | cli->reader->gbox_my_password ? cli->reader->gbox_my_password : "");
|
---|
1365 | return -1;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | if (cli->reader->l_port < 1) {
|
---|
1369 | cs_log("gbox: no/invalid port configured in oscam.conf!");
|
---|
1370 | return -1;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | if (!cs_malloc(&cli->gbox, sizeof(struct gbox_data)))
|
---|
1374 | return -1;
|
---|
1375 |
|
---|
1376 | struct gbox_data *gbox = cli->gbox;
|
---|
1377 | struct s_reader *rdr = cli->reader;
|
---|
1378 |
|
---|
1379 | rdr->card_status = CARD_NEED_INIT;
|
---|
1380 | rdr->tcp_connected = 0;
|
---|
1381 |
|
---|
1382 | memset(gbox, 0, sizeof(struct gbox_data));
|
---|
1383 | memset(&gbox->peer, 0, sizeof(struct gbox_peer));
|
---|
1384 |
|
---|
1385 | pthread_mutex_init(&gbox->peer_online_mutex, NULL);
|
---|
1386 | pthread_cond_init(&gbox->peer_online_cond, NULL);
|
---|
1387 |
|
---|
1388 | uint32_t r_pwd = a2i(rdr->r_pwd, 4);
|
---|
1389 | uint32_t key = a2i(cli->reader->gbox_my_password, 4);
|
---|
1390 | int32_t i;
|
---|
1391 | for (i = 3; i >= 0; i--) {
|
---|
1392 | gbox->peer.key[3 - i] = (r_pwd >> (8 * i)) & 0xff;
|
---|
1393 | gbox->key[3 - i] = (key >> (8 * i)) & 0xff;
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | cs_ddump_mask(D_READER, gbox->peer.key, 4, "Peer password: %s:", rdr->r_pwd);
|
---|
1397 | cs_ddump_mask(D_READER, gbox->key, 4, " My password: %s:", cli->reader->gbox_my_password);
|
---|
1398 |
|
---|
1399 | gbox->peer.id = (gbox->peer.key[0] ^ gbox->peer.key[2]) << 8 | (gbox->peer.key[1] ^ gbox->peer.key[3]);
|
---|
1400 |
|
---|
1401 | cli->gbox_peer_id[0] = gbox->peer.id >> 8;
|
---|
1402 | cli->gbox_peer_id[1] = gbox->peer.id & 0xff;
|
---|
1403 |
|
---|
1404 | gbox->id = (gbox->key[0] ^ gbox->key[2]) << 8 | (gbox->key[1] ^ gbox->key[3]);
|
---|
1405 | gbox->ver = gbox_version_low_byte;
|
---|
1406 | gbox->type = gbox_type_dvb;
|
---|
1407 | rdr->gbox_peer_id= gbox->peer.id;
|
---|
1408 |
|
---|
1409 | cli->gbox_cw_id[0] = gbox->peer.id >> 8;
|
---|
1410 | cli->gbox_cw_id[1] = (gbox->id >> 8) + (gbox->peer.id & 0xff);
|
---|
1411 |
|
---|
1412 | struct sockaddr_in loc_sa;
|
---|
1413 | cli->pfd=0;
|
---|
1414 |
|
---|
1415 | set_null_ip(&cli->ip);
|
---|
1416 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
1417 | SIN_GET_FAMILY(loc_sa) = AF_INET;
|
---|
1418 | SIN_GET_ADDR(loc_sa) =
|
---|
1419 | #ifdef IPV6SUPPORT
|
---|
1420 | in6addr_any;
|
---|
1421 | #else
|
---|
1422 | INADDR_ANY;
|
---|
1423 | #endif
|
---|
1424 | SIN_GET_PORT(loc_sa) = htons(rdr->l_port);
|
---|
1425 |
|
---|
1426 | if ((cli->udp_fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))<0)
|
---|
1427 | {
|
---|
1428 | cs_log("socket creation failed (errno=%d %s)", errno, strerror(errno));
|
---|
1429 | cs_disconnect_client(cli);
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | int32_t opt = 1;
|
---|
1433 | setsockopt(cli->udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
|
---|
1434 |
|
---|
1435 | set_socket_priority(cli->udp_fd, cfg.netprio);
|
---|
1436 |
|
---|
1437 | if (rdr->l_port>0)
|
---|
1438 | {
|
---|
1439 | if (bind(cli->udp_fd, (struct sockaddr *)&loc_sa, sizeof (loc_sa))<0)
|
---|
1440 | {
|
---|
1441 | cs_log("bind failed (port=%d, errno=%d %s)", rdr->l_port, errno, strerror(errno));
|
---|
1442 | close(cli->udp_fd);
|
---|
1443 | return 1;
|
---|
1444 | }
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | memset((char *)&cli->udp_sa, 0, sizeof(cli->udp_sa));
|
---|
1448 |
|
---|
1449 | if (!hostResolve(rdr))
|
---|
1450 | return 0;
|
---|
1451 |
|
---|
1452 | cli->port=rdr->r_port;
|
---|
1453 | SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
|
---|
1454 | SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)rdr->r_port);
|
---|
1455 | hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
|
---|
1456 |
|
---|
1457 | cs_log("proxy %s:%d (fd=%d, peer id=%04x, my id=%04x, my hostname=%s, listen port=%d)",
|
---|
1458 | rdr->device, rdr->r_port, cli->udp_fd, gbox->peer.id, gbox->id, cfg.gbox_hostname, rdr->r_port);
|
---|
1459 |
|
---|
1460 | cli->pfd=cli->udp_fd;
|
---|
1461 |
|
---|
1462 | cs_lock_create(&gbox->lock, 5, "gbox_lock");
|
---|
1463 |
|
---|
1464 | gbox->peer.hello_cont = 0;
|
---|
1465 | gbox->hello_expired = 0;
|
---|
1466 | gbox->peer.online = 0;
|
---|
1467 | gbox->peer.ecm_idx = 0;
|
---|
1468 | gbox->peer.t_ecm = time(NULL);
|
---|
1469 |
|
---|
1470 | cli->reader->card_status = CARD_NEED_INIT;
|
---|
1471 | gbox_send_hello(cli);
|
---|
1472 |
|
---|
1473 | if (!cli->reader->gbox_maxecmsend)
|
---|
1474 | cli->reader->gbox_maxecmsend = DEFAULT_GBOX_MAX_ECM_SEND;
|
---|
1475 |
|
---|
1476 | if (!cli->reader->gbox_maxdist)
|
---|
1477 | cli->reader->gbox_maxdist=DEFAULT_GBOX_MAX_DIST;
|
---|
1478 |
|
---|
1479 | if (!main_1) {
|
---|
1480 | login_gbox = time(NULL);
|
---|
1481 | main_1 = 1;
|
---|
1482 | if (!cfg.gbox_reconnect)
|
---|
1483 | cfg.gbox_reconnect = DEFAULT_GBOX_RECONNECT;
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | return 0;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | static int32_t gbox_recv_chk(struct s_client *cli, uchar *dcw, int32_t *rc, uchar *data, int32_t UNUSED(n))
|
---|
1490 | {
|
---|
1491 | //struct gbox_data *gbox = cli->gbox;
|
---|
1492 | char tmp[512];
|
---|
1493 | if (gbox_decode_cmd(data) == MSG_CW) {
|
---|
1494 | int i, n;
|
---|
1495 | *rc = 1;
|
---|
1496 | memcpy(dcw, data + 14, 16);
|
---|
1497 | uint32_t crc = data[30] << 24 | data[31] << 16 | data[32] << 8 | data[33];
|
---|
1498 | //TODO:gbox_add_cwcache(crc, dcw);
|
---|
1499 | cs_debug_mask(D_READER, "gbox: received cws=%s, peer=%04x, ecm_pid=%04x, sid=%04x, crc=%08x",
|
---|
1500 | cs_hexdump(0, dcw, 16, tmp, sizeof(tmp)), data[10] << 8 | data[11], data[6] << 8 | data[7], data[8] << 8 | data[9], crc);
|
---|
1501 | for (i = 0, n = 0; i < cfg.max_pending && n == 0; i++) {
|
---|
1502 | if (cli->ecmtask[i].gbox_crc==crc) {
|
---|
1503 | uint16_t id_card=data[10]<<8 | data[11];
|
---|
1504 | uint16_t good_sid=cli->ecmtask[i].srvid;
|
---|
1505 | gbox_add_good_card(cli,id_card,cli->ecmtask[i].prid,good_sid);
|
---|
1506 | if(cli->ecmtask[i].gbox_ecm_ok==0 || cli->ecmtask[i].gbox_ecm_ok==2)
|
---|
1507 | return -1;
|
---|
1508 | struct s_ecm_answer ea;
|
---|
1509 | memset(&ea, 0, sizeof(struct s_ecm_answer));
|
---|
1510 | cli->ecmtask[i].gbox_ecm_ok=2;
|
---|
1511 | memcpy(ea.cw, dcw, 16);
|
---|
1512 | *rc = 1;
|
---|
1513 | return cli->ecmtask[i].idx;
|
---|
1514 | }
|
---|
1515 | }
|
---|
1516 | cs_debug_mask(D_READER, "gbox: no task found for crc=%08x",crc);
|
---|
1517 | }
|
---|
1518 | return -1;
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | static int32_t gbox_send_ecm(struct s_client *cli, ECM_REQUEST *er, uchar *UNUSED(buf))
|
---|
1522 | {
|
---|
1523 | struct gbox_data *gbox = cli->gbox;
|
---|
1524 | int32_t cont_1;
|
---|
1525 | uint32_t sid_verified=0;
|
---|
1526 |
|
---|
1527 | if (!gbox || !cli->reader->tcp_connected) {
|
---|
1528 | cs_debug_mask(D_READER, "gbox: %s server not init!", cli->reader->label);
|
---|
1529 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1530 |
|
---|
1531 | return 0;
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 | if (!ll_count(gbox->peer.cards)) {
|
---|
1535 | cs_debug_mask(D_READER, "gbox: %s NO CARDS!", cli->reader->label);
|
---|
1536 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1537 | return 0;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | if (!gbox->peer.online) {
|
---|
1541 | cs_debug_mask(D_READER, "gbox: peer is OFFLINE!");
|
---|
1542 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1543 | gbox_send_hello(cli);
|
---|
1544 | return 0;
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | if(er->gbox_ecm_ok==2) {
|
---|
1548 | cs_debug_mask(D_READER, "gbox: %s replied to this ecm already", cli->reader->label);
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | if(er->gbox_ecm_id == gbox->peer.id) {
|
---|
1552 | cs_debug_mask(D_READER, "gbox: %s provided ecm", cli->reader->label);
|
---|
1553 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1554 | return 0;
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | uint16_t ercaid = er->caid;
|
---|
1558 | uint32_t erprid = er->prid;
|
---|
1559 |
|
---|
1560 | if(cli->reader->gbox_maxecmsend == 0) {
|
---|
1561 | cli->reader->gbox_maxecmsend=DEFAULT_GBOX_MAX_ECM_SEND;
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | switch (ercaid >> 8) {
|
---|
1565 | // cryptoworks
|
---|
1566 | case 0x0d:
|
---|
1567 | erprid = erprid << 8;
|
---|
1568 | break;
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | uchar send_buf_1[0x1024];
|
---|
1572 | int32_t len2;
|
---|
1573 |
|
---|
1574 | if (!er->ecmlen) return 0;
|
---|
1575 |
|
---|
1576 | len2 = er->ecmlen + 18;
|
---|
1577 | er->gbox_crc = gbox_get_ecmchecksum(er);
|
---|
1578 |
|
---|
1579 | memset(send_buf_1, 0, sizeof(send_buf_1));
|
---|
1580 |
|
---|
1581 | LL_ITER it = ll_iter_create(gbox->peer.cards);
|
---|
1582 | struct gbox_card *card;
|
---|
1583 |
|
---|
1584 | int32_t cont_send = 0;
|
---|
1585 | int32_t cont_card_1 = 0;
|
---|
1586 |
|
---|
1587 | send_buf_1[0] = MSG_ECM >> 8;
|
---|
1588 | send_buf_1[1] = MSG_ECM & 0xff;
|
---|
1589 | memcpy(send_buf_1 + 2, gbox->peer.key, 4);
|
---|
1590 | memcpy(send_buf_1 + 6, gbox->key, 4);
|
---|
1591 |
|
---|
1592 | if(er->pid==0) {
|
---|
1593 | send_buf_1[10] = 0x00;//er->pid >> 8;
|
---|
1594 | send_buf_1[11] = 0x00;//er->pid;
|
---|
1595 | } else {
|
---|
1596 | send_buf_1[10] = er->pid >> 8;
|
---|
1597 | send_buf_1[11] = er->pid;
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | send_buf_1[12] = er->srvid >> 8;
|
---|
1601 | send_buf_1[13] = er->srvid;
|
---|
1602 | send_buf_1[14] = 0x00;
|
---|
1603 | send_buf_1[15] = 0x00;
|
---|
1604 |
|
---|
1605 | send_buf_1[16] = cont_card_1;
|
---|
1606 | send_buf_1[17] = 0x00;
|
---|
1607 |
|
---|
1608 | memcpy(send_buf_1 + 18, er->ecm, er->ecmlen);
|
---|
1609 |
|
---|
1610 | send_buf_1[len2] = cli->gbox_cw_id[0];
|
---|
1611 | send_buf_1[len2+1] = cli->gbox_cw_id[1];
|
---|
1612 | send_buf_1[len2+2] = gbox_version_low_byte;
|
---|
1613 | send_buf_1[len2+3] = 0x00;
|
---|
1614 | send_buf_1[len2+4] = gbox_type_dvb;
|
---|
1615 |
|
---|
1616 | send_buf_1[len2+5] = er->caid >> 8;
|
---|
1617 | if(send_buf_1[len2+5] == 0x05)
|
---|
1618 | send_buf_1[len2+6] = er->prid >> 16;
|
---|
1619 | else
|
---|
1620 | send_buf_1[len2+6] = er->caid & 0xFF;
|
---|
1621 |
|
---|
1622 | send_buf_1[len2+7] = 0x00;
|
---|
1623 | send_buf_1[len2+8] = 0x00;
|
---|
1624 | send_buf_1[len2+9] = 0x00;
|
---|
1625 | cont_1 =len2+10;
|
---|
1626 |
|
---|
1627 | struct gbox_srvid *srvid1=NULL;
|
---|
1628 | while ((card = ll_iter_next(&it))) {
|
---|
1629 | if (card->caid == er->caid && card->provid == er->prid) {
|
---|
1630 | sid_verified = 0;
|
---|
1631 |
|
---|
1632 | LL_ITER it2 = ll_iter_create(card->goodsids);
|
---|
1633 | while ((srvid1 = ll_iter_next(&it2))) {
|
---|
1634 | if (srvid1->provid_id == er->prid && srvid1->sid == er->srvid) {
|
---|
1635 | send_buf_1[cont_1] = card->peer_id >> 8;
|
---|
1636 | send_buf_1[cont_1+1] = card->peer_id;
|
---|
1637 | send_buf_1[cont_1+2] = card->slot;
|
---|
1638 | cont_1=cont_1+3;cont_card_1++;
|
---|
1639 | cont_send++;
|
---|
1640 | sid_verified=1;
|
---|
1641 | break;
|
---|
1642 | }
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | if (cont_send == cli->reader->gbox_maxecmsend)
|
---|
1646 | break;
|
---|
1647 |
|
---|
1648 | if (sid_verified == 0) {
|
---|
1649 | LL_ITER itt = ll_iter_create(card->badsids);
|
---|
1650 | while ((srvid1 = ll_iter_next(&itt))) {
|
---|
1651 | if (srvid1->provid_id == er->prid && srvid1->sid == er->srvid) {
|
---|
1652 | sid_verified = 1;
|
---|
1653 | break;
|
---|
1654 | }
|
---|
1655 | }
|
---|
1656 |
|
---|
1657 | if (sid_verified != 1) {
|
---|
1658 | send_buf_1[cont_1] = card->peer_id >> 8;
|
---|
1659 | send_buf_1[cont_1+1] = card->peer_id;
|
---|
1660 | send_buf_1[cont_1+2] = card->slot;
|
---|
1661 | cont_1=cont_1+3;
|
---|
1662 | cont_card_1++;
|
---|
1663 | cont_send++;
|
---|
1664 | sid_verified=0;
|
---|
1665 |
|
---|
1666 | if (!cs_malloc(&srvid1, sizeof(struct gbox_srvid)))
|
---|
1667 | return 0;
|
---|
1668 |
|
---|
1669 | srvid1->sid=er->srvid;
|
---|
1670 | srvid1->peer_idcard=card->peer_id;
|
---|
1671 | srvid1->provid_id=card->provid;
|
---|
1672 | ll_append(card->badsids, srvid1);
|
---|
1673 |
|
---|
1674 | if(cont_send == cli->reader->gbox_maxecmsend)
|
---|
1675 | break;
|
---|
1676 | }
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | if (cont_send == cli->reader->gbox_maxecmsend)
|
---|
1680 | break;
|
---|
1681 | }
|
---|
1682 | }
|
---|
1683 |
|
---|
1684 | if (!cont_card_1)
|
---|
1685 | return 0;
|
---|
1686 |
|
---|
1687 | send_buf_1[16] = cont_card_1;
|
---|
1688 |
|
---|
1689 | memcpy(&send_buf_1[cont_1], gbox->checkcode, 7);
|
---|
1690 | cont_1 = cont_1 + 7;
|
---|
1691 | memcpy(&send_buf_1[cont_1], gbox->peer.checkcode, 7);
|
---|
1692 | cont_1 = cont_1 + 7;
|
---|
1693 |
|
---|
1694 | cs_debug_mask(D_READER,"Gbox sending ecm for %06x : %s",er->prid ,cli->reader->label);
|
---|
1695 |
|
---|
1696 | cont_1++;
|
---|
1697 | er->gbox_ecm_ok = 1;
|
---|
1698 | gbox_send(cli, send_buf_1, cont_1);
|
---|
1699 | cli->pending++;
|
---|
1700 | memset(send_buf_1, 0, sizeof(send_buf_1));
|
---|
1701 |
|
---|
1702 | return 0;
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | static int32_t gbox_send_emm(EMM_PACKET *UNUSED(ep))
|
---|
1706 | {
|
---|
1707 | // emms not yet supported
|
---|
1708 |
|
---|
1709 | return 0;
|
---|
1710 | }
|
---|
1711 |
|
---|
1712 | // Parsing of mg-encrypted option in [reader]
|
---|
1713 | void mgencrypted_fn(const char *UNUSED(token), char *value, void *setting, FILE *UNUSED(f)) {
|
---|
1714 | struct s_reader *rdr = setting;
|
---|
1715 |
|
---|
1716 | if (value) {
|
---|
1717 | uchar key[16];
|
---|
1718 | uchar mac[6];
|
---|
1719 | char tmp_dbg[13];
|
---|
1720 | uchar *buf = NULL;
|
---|
1721 | int32_t i, len = 0;
|
---|
1722 | char *ptr, *saveptr1 = NULL;
|
---|
1723 |
|
---|
1724 | memset(&key, 0, 16);
|
---|
1725 | memset(&mac, 0, 6);
|
---|
1726 |
|
---|
1727 | for (i = 0, ptr = strtok_r(value, ",", &saveptr1); (i < 2) && (ptr); ptr = strtok_r(NULL, ",", &saveptr1), i++) {
|
---|
1728 | trim(ptr);
|
---|
1729 | switch(i) {
|
---|
1730 | case 0:
|
---|
1731 | len = strlen(ptr) / 2 + (16 - (strlen(ptr) / 2) % 16);
|
---|
1732 | if (!cs_malloc(&buf, len)) return;
|
---|
1733 | key_atob_l(ptr, buf, strlen(ptr));
|
---|
1734 | cs_log("enc %d: %s", len, ptr);
|
---|
1735 | break;
|
---|
1736 |
|
---|
1737 | case 1:
|
---|
1738 | key_atob_l(ptr, mac, 12);
|
---|
1739 | cs_log("mac: %s", ptr);
|
---|
1740 | break;
|
---|
1741 | }
|
---|
1742 | }
|
---|
1743 | if (!buf)
|
---|
1744 | return;
|
---|
1745 |
|
---|
1746 | if (!memcmp(mac, "\x00\x00\x00\x00\x00\x00", 6)) {
|
---|
1747 | #if defined(__APPLE__) || defined(__FreeBSD__)
|
---|
1748 | // no mac address specified so use mac of en0 on local box
|
---|
1749 | struct ifaddrs *ifs, *current;
|
---|
1750 |
|
---|
1751 | if (getifaddrs(&ifs) == 0)
|
---|
1752 | {
|
---|
1753 | for (current = ifs; current != 0; current = current->ifa_next)
|
---|
1754 | {
|
---|
1755 | if (current->ifa_addr->sa_family == AF_LINK && strcmp(current->ifa_name, "en0") == 0)
|
---|
1756 | {
|
---|
1757 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)current->ifa_addr;
|
---|
1758 | memcpy(mac, LLADDR(sdl), sdl->sdl_alen);
|
---|
1759 | break;
|
---|
1760 | }
|
---|
1761 | }
|
---|
1762 | freeifaddrs(ifs);
|
---|
1763 | }
|
---|
1764 | #elif defined(__SOLARIS__)
|
---|
1765 | // no mac address specified so use first filled mac
|
---|
1766 | int32_t j, sock, niccount;
|
---|
1767 | struct ifreq nicnumber[16];
|
---|
1768 | struct ifconf ifconf;
|
---|
1769 | struct arpreq arpreq;
|
---|
1770 |
|
---|
1771 | if ((sock=socket(AF_INET,SOCK_DGRAM,0)) > -1){
|
---|
1772 | ifconf.ifc_buf = (caddr_t)nicnumber;
|
---|
1773 | ifconf.ifc_len = sizeof(nicnumber);
|
---|
1774 | if (!ioctl(sock,SIOCGIFCONF,(char*)&ifconf)){
|
---|
1775 | niccount = ifconf.ifc_len/(sizeof(struct ifreq));
|
---|
1776 | for(i = 0; i < niccount, ++i){
|
---|
1777 | memset(&arpreq, 0, sizeof(arpreq));
|
---|
1778 | ((struct sockaddr_in*)&arpreq.arp_pa)->sin_addr.s_addr = ((struct sockaddr_in*)&nicnumber[i].ifr_addr)->sin_addr.s_addr;
|
---|
1779 | if (!(ioctl(sock,SIOCGARP,(char*)&arpreq))){
|
---|
1780 | for (j = 0; j < 6; ++j)
|
---|
1781 | mac[j] = (unsigned char)arpreq.arp_ha.sa_data[j];
|
---|
1782 | if(check_filled(mac, 6) > 0) break;
|
---|
1783 | }
|
---|
1784 | }
|
---|
1785 | }
|
---|
1786 | close(sock);
|
---|
1787 | }
|
---|
1788 | #else
|
---|
1789 | // no mac address specified so use mac of eth0 on local box
|
---|
1790 | int32_t fd = socket(PF_INET, SOCK_STREAM, 0);
|
---|
1791 |
|
---|
1792 | struct ifreq ifreq;
|
---|
1793 | memset(&ifreq, 0, sizeof(ifreq));
|
---|
1794 | snprintf(ifreq.ifr_name, sizeof(ifreq.ifr_name), "eth0");
|
---|
1795 |
|
---|
1796 | ioctl(fd, SIOCGIFHWADDR, &ifreq);
|
---|
1797 | memcpy(mac, ifreq.ifr_ifru.ifru_hwaddr.sa_data, 6);
|
---|
1798 |
|
---|
1799 | close(fd);
|
---|
1800 | #endif
|
---|
1801 | cs_debug_mask(D_TRACE, "Determined local mac address for mg-encrypted as %s", cs_hexdump(1, mac, 6, tmp_dbg, sizeof(tmp_dbg)));
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | // decrypt encrypted mgcamd gbox line
|
---|
1805 | for (i = 0; i < 6; i++)
|
---|
1806 | key[i * 2] = mac[i];
|
---|
1807 |
|
---|
1808 | AES_KEY aeskey;
|
---|
1809 | AES_set_decrypt_key(key, 128, &aeskey);
|
---|
1810 | for (i = 0; i < len; i+=16)
|
---|
1811 | AES_decrypt(buf + i,buf + i, &aeskey);
|
---|
1812 |
|
---|
1813 | // parse d-line
|
---|
1814 | for (i = 0, ptr = strtok_r((char *)buf, " {", &saveptr1); (i < 5) && (ptr); ptr = strtok_r(NULL, " {", &saveptr1), i++) {
|
---|
1815 | trim(ptr);
|
---|
1816 | switch (i) {
|
---|
1817 | case 1: // hostname
|
---|
1818 | cs_strncpy(rdr->device, ptr, sizeof(rdr->device));
|
---|
1819 | break;
|
---|
1820 | case 2: // local port
|
---|
1821 | rdr->l_port = atoi(ptr);
|
---|
1822 | break;
|
---|
1823 | case 3: // remote port
|
---|
1824 | rdr->r_port = atoi(ptr);
|
---|
1825 | break;
|
---|
1826 | case 4: // password
|
---|
1827 | cs_strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd));
|
---|
1828 | break;
|
---|
1829 | }
|
---|
1830 | }
|
---|
1831 |
|
---|
1832 | free(buf);
|
---|
1833 | return;
|
---|
1834 | }
|
---|
1835 | }
|
---|
1836 |
|
---|
1837 |
|
---|
1838 | void module_gbox(struct s_module *ph)
|
---|
1839 | {
|
---|
1840 | ph->desc="gbox";
|
---|
1841 | ph->num=R_GBOX;
|
---|
1842 | ph->type=MOD_CONN_UDP;
|
---|
1843 | ph->listenertype = LIS_GBOX;
|
---|
1844 |
|
---|
1845 | ph->s_handler=gbox_server;
|
---|
1846 | ph->s_init=gbox_server_init;
|
---|
1847 |
|
---|
1848 | ph->send_dcw=gbox_send_dcw;
|
---|
1849 |
|
---|
1850 | ph->recv=gbox_recv;
|
---|
1851 | ph->c_init=gbox_client_init;
|
---|
1852 | ph->c_recv_chk=gbox_recv_chk;
|
---|
1853 | ph->c_send_ecm=gbox_send_ecm;
|
---|
1854 | ph->c_send_emm=gbox_send_emm;
|
---|
1855 | }
|
---|
1856 | #endif
|
---|
1857 |
|
---|