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 "module-gbox.h"
|
---|
17 | #include "module-gbox-helper.h"
|
---|
18 | #include "module-cccam.h"
|
---|
19 | #include "module-cccam-data.h"
|
---|
20 | #include "oscam-failban.h"
|
---|
21 | #include "oscam-client.h"
|
---|
22 | #include "oscam-ecm.h"
|
---|
23 | #include "oscam-lock.h"
|
---|
24 | #include "oscam-net.h"
|
---|
25 | #include "oscam-chk.h"
|
---|
26 | #include "oscam-string.h"
|
---|
27 | #include "oscam-time.h"
|
---|
28 | #include "oscam-reader.h"
|
---|
29 | #include "oscam-garbage.h"
|
---|
30 |
|
---|
31 | #if defined(__CYGWIN__)
|
---|
32 | #define FILE_GBOX_VERSION "C:/tmp/gbx.ver"
|
---|
33 | #define FILE_SHARED_CARDS_INFO "C:/tmp/gbx_card.info"
|
---|
34 | #define FILE_ATTACK_INFO "C:/tmp/gbx_attack.txt"
|
---|
35 | #define FILE_GBOX_PEER_ONL "C:/tmp/gbx_peer.onl"
|
---|
36 | #define FILE_STATS "C:/tmp/gbx_stats.info"
|
---|
37 | #else
|
---|
38 | #define FILE_GBOX_VERSION "/tmp/gbx.ver"
|
---|
39 | #define FILE_SHARED_CARDS_INFO "/tmp/gbx_card.info"
|
---|
40 | #define FILE_ATTACK_INFO "/tmp/gbx_attack.txt"
|
---|
41 | #define FILE_GBOX_PEER_ONL "/tmp/gbx_peer.onl"
|
---|
42 | #define FILE_STATS "/tmp/gbx_stats.info"
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #define GBOX_STAT_HELLOL 0
|
---|
46 | #define GBOX_STAT_HELLOS 1
|
---|
47 | #define GBOX_STAT_HELLOR 2
|
---|
48 | #define GBOX_STAT_HELLO3 3
|
---|
49 | #define GBOX_STAT_HELLO4 4
|
---|
50 |
|
---|
51 | #define RECEIVE_BUFFER_SIZE 1024
|
---|
52 | #define MIN_GBOX_MESSAGE_LENGTH 10 //CMD + pw + pw. TODO: Check if is really min
|
---|
53 | #define MIN_ECM_LENGTH 8
|
---|
54 | #define HELLO_KEEPALIVE_TIME 120 //send hello to peer every 2 min in case no ecm received
|
---|
55 | #define ECM_BROADCAST_PAUSE 3600
|
---|
56 | #define STATS_WRITE_TIME 300 //write stats file every 5 min
|
---|
57 |
|
---|
58 | #define LOCAL_GBOX_MAJOR_VERSION 0x02
|
---|
59 | #define LOCAL_GBOX_MINOR_VERSION 0x25
|
---|
60 | #define LOCAL_GBOX_TYPE 0x40
|
---|
61 |
|
---|
62 | enum
|
---|
63 | {
|
---|
64 | MSG_ECM = 0x445c,
|
---|
65 | MSG_CW = 0x4844,
|
---|
66 | MSG_HELLO = 0xddab,
|
---|
67 | MSG_HELLO1 = 0x4849,
|
---|
68 | MSG_CHECKCODE = 0x41c0,
|
---|
69 | MSG_GOODBYE = 0x9091,
|
---|
70 | MSG_GSMS_ACK = 0x9098,
|
---|
71 | MSG_GSMS = 0xff0,
|
---|
72 | MSG_BOXINFO = 0xa0a1,
|
---|
73 | MSG_UNKNWN1 = 0x9099,
|
---|
74 | MSG_UNKNWN2 = 0x48F9,
|
---|
75 | };
|
---|
76 |
|
---|
77 | struct gbox_srvid
|
---|
78 | {
|
---|
79 | uint16_t sid;
|
---|
80 | uint32_t provid_id;
|
---|
81 | time_t last_cw_received;
|
---|
82 | };
|
---|
83 |
|
---|
84 | struct gbox_card
|
---|
85 | {
|
---|
86 | uint16_t peer_id;
|
---|
87 | uint16_t caid;
|
---|
88 | uint32_t provid;
|
---|
89 | uint32_t provid_1;
|
---|
90 | uint8_t slot;
|
---|
91 | uint8_t dist;
|
---|
92 | uint8_t lvl;
|
---|
93 | LLIST *badsids; // sids that have failed to decode (struct cc_srvid)
|
---|
94 | LLIST *goodsids; //sids that could be decoded (struct cc_srvid)
|
---|
95 | uint32_t no_cws_returned;
|
---|
96 | uint32_t average_cw_time;
|
---|
97 | };
|
---|
98 |
|
---|
99 | struct gbox_data
|
---|
100 | {
|
---|
101 | uint16_t id;
|
---|
102 | uchar password[4];
|
---|
103 | uchar checkcode[7];
|
---|
104 | uint8_t minor_version;
|
---|
105 | uint8_t type;
|
---|
106 | LLIST *cards;
|
---|
107 | };
|
---|
108 |
|
---|
109 | struct gbox_peer
|
---|
110 | {
|
---|
111 | struct gbox_data gbox;
|
---|
112 | uchar *hostname;
|
---|
113 | int32_t online;
|
---|
114 | int32_t hello_stat;
|
---|
115 | int32_t goodbye_cont;
|
---|
116 | uchar ecm_idx;
|
---|
117 | uchar gbox_count_ecm;
|
---|
118 | CS_MUTEX_LOCK lock;
|
---|
119 | struct s_client *my_user;
|
---|
120 | LL_ITER last_it;
|
---|
121 | };
|
---|
122 |
|
---|
123 | static struct gbox_data local_gbox;
|
---|
124 | static time_t last_stats_written;
|
---|
125 |
|
---|
126 | //static void gbox_send_boxinfo(struct s_client *cli);
|
---|
127 | static void gbox_send_hello(struct s_client *cli);
|
---|
128 | static void gbox_send_hello_packet(struct s_client *cli, int8_t number, uchar *outbuf, uchar *ptr, int32_t nbcards);
|
---|
129 | static void gbox_send_checkcode(struct s_client *cli);
|
---|
130 | static void gbox_local_cards(struct s_client *cli);
|
---|
131 | static void gbox_init_ecm_request_ext(struct gbox_ecm_request_ext *ere);
|
---|
132 | static int32_t gbox_client_init(struct s_client *cli);
|
---|
133 | static int8_t gbox_check_header(struct s_client *cli, uchar *data, int32_t l);
|
---|
134 | static int8_t gbox_incoming_ecm(struct s_client *cli, uchar *data, int32_t n);
|
---|
135 | static int32_t gbox_recv_chk(struct s_client *cli, uchar *dcw, int32_t *rc, uchar *data, int32_t n);
|
---|
136 | static int32_t gbox_checkcode_recv(struct s_client *cli, uchar *checkcode);
|
---|
137 | static int32_t gbox_decode_cmd(uchar *buf);
|
---|
138 | static uint8_t gbox_compare_pw(uchar *my_pw, uchar *rec_pw);
|
---|
139 | static uint16_t gbox_convert_password_to_id(uchar *password);
|
---|
140 | uint32_t gbox_get_ecmchecksum(ECM_REQUEST *er);
|
---|
141 | static void init_local_gbox(void);
|
---|
142 |
|
---|
143 | void gbox_write_peer_onl(void)
|
---|
144 | {
|
---|
145 | FILE *fhandle = fopen(FILE_GBOX_PEER_ONL, "w");
|
---|
146 | if(!fhandle)
|
---|
147 | {
|
---|
148 | cs_log("Couldn't open %s: %s\n", FILE_GBOX_PEER_ONL, strerror(errno));
|
---|
149 | return;
|
---|
150 | }
|
---|
151 | struct s_client *cl;
|
---|
152 | for(cl = first_client; cl; cl = cl->next)
|
---|
153 | {
|
---|
154 | if(cl->gbox && cl->typ == 'p')
|
---|
155 | {
|
---|
156 | struct gbox_peer *peer = cl->gbox;
|
---|
157 | if (peer->online)
|
---|
158 | { fprintf(fhandle, "1 %s %s %04X 2.%02X\n",cl->reader->device, cs_inet_ntoa(cl->ip),peer->gbox.id, peer->gbox.minor_version); }
|
---|
159 | else
|
---|
160 | { fprintf(fhandle, "0 %s %s %04X 0.00\n",cl->reader->device, cs_inet_ntoa(cl->ip),peer->gbox.id); }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | fclose(fhandle);
|
---|
164 | return;
|
---|
165 | }
|
---|
166 |
|
---|
167 | void gbox_write_version(void)
|
---|
168 | {
|
---|
169 | FILE *fhandle = fopen(FILE_GBOX_VERSION, "w");
|
---|
170 | if(!fhandle)
|
---|
171 | {
|
---|
172 | cs_log("Couldn't open %s: %s\n", FILE_GBOX_VERSION, strerror(errno));
|
---|
173 | return;
|
---|
174 | }
|
---|
175 | fprintf(fhandle, "%02X.%02X\n", LOCAL_GBOX_MAJOR_VERSION, LOCAL_GBOX_MINOR_VERSION);
|
---|
176 | fclose(fhandle);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void gbox_write_shared_cards_info(void)
|
---|
180 | {
|
---|
181 | int32_t card_count = 0;
|
---|
182 | int32_t i = 0;
|
---|
183 |
|
---|
184 | FILE *fhandle;
|
---|
185 | fhandle = fopen(FILE_SHARED_CARDS_INFO, "w");
|
---|
186 | if(!fhandle)
|
---|
187 | {
|
---|
188 | cs_log("Couldn't open %s: %s\n", FILE_SHARED_CARDS_INFO, strerror(errno));
|
---|
189 | return;
|
---|
190 | }
|
---|
191 |
|
---|
192 | LL_ITER it;
|
---|
193 | struct gbox_card *card;
|
---|
194 |
|
---|
195 | //write local cards
|
---|
196 | it = ll_iter_create(local_gbox.cards);
|
---|
197 | while((card = ll_iter_next(&it)))
|
---|
198 | {
|
---|
199 | fprintf(fhandle, "CardID %4d at oscam Card %08X Sl:%2d Lev:%2d dist:%2d id:%04X\n",
|
---|
200 | card_count, card->provid_1,
|
---|
201 | card->slot, card->lvl, card->dist, card->peer_id);
|
---|
202 | card_count++;
|
---|
203 | } // end of while ll_iter_next
|
---|
204 |
|
---|
205 | struct s_client *cl;
|
---|
206 | for(i = 0, cl = first_client; cl; cl = cl->next, i++)
|
---|
207 | {
|
---|
208 | if(cl->gbox && cl->reader->card_status == CARD_INSERTED && cl->typ == 'p')
|
---|
209 | {
|
---|
210 | struct gbox_peer *peer = cl->gbox;
|
---|
211 |
|
---|
212 | it = ll_iter_create(peer->gbox.cards);
|
---|
213 | while((card = ll_iter_next(&it)))
|
---|
214 | {
|
---|
215 | fprintf(fhandle, "CardID %4d at %s Card %08X Sl:%2d Lev:%2d dist:%2d id:%04X\n",
|
---|
216 | card_count, cl->reader->device, card->provid_1,
|
---|
217 | card->slot, card->lvl, card->dist, card->peer_id);
|
---|
218 | card_count++;
|
---|
219 | } // end of while ll_iter_next
|
---|
220 | } // end of if cl->gbox INSERTED && 'p'
|
---|
221 | } // end of for cl->next
|
---|
222 | fclose(fhandle);
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | void gbox_write_stats(void)
|
---|
227 | {
|
---|
228 | int32_t card_count = 0;
|
---|
229 | int32_t i = 0;
|
---|
230 | struct gbox_srvid *srvid = NULL;
|
---|
231 |
|
---|
232 | FILE *fhandle;
|
---|
233 | fhandle = fopen(FILE_STATS, "w");
|
---|
234 | if(!fhandle)
|
---|
235 | {
|
---|
236 | cs_log("Couldn't open %s: %s\n", FILE_STATS, strerror(errno));
|
---|
237 | return;
|
---|
238 | }
|
---|
239 |
|
---|
240 | LL_ITER it;
|
---|
241 | struct gbox_card *card;
|
---|
242 |
|
---|
243 | struct s_client *cl;
|
---|
244 | for(i = 0, cl = first_client; cl; cl = cl->next, i++)
|
---|
245 | {
|
---|
246 | if(cl->gbox && cl->reader->card_status == CARD_INSERTED && cl->typ == 'p')
|
---|
247 | {
|
---|
248 | struct gbox_peer *peer = cl->gbox;
|
---|
249 |
|
---|
250 | it = ll_iter_create(peer->gbox.cards);
|
---|
251 | while((card = ll_iter_next(&it)))
|
---|
252 | {
|
---|
253 | fprintf(fhandle, "CardID %4d Card %08X id:%04X #CWs:%d AVGtime:%d ms\n",
|
---|
254 | card_count, card->provid_1, card->peer_id, card->no_cws_returned, card->average_cw_time);
|
---|
255 | fprintf(fhandle, "Good SIDs:\n");
|
---|
256 | LL_ITER it2 = ll_iter_create(card->goodsids);
|
---|
257 | while((srvid = ll_iter_next(&it2)))
|
---|
258 | { fprintf(fhandle, "%04X\n", srvid->sid); }
|
---|
259 | fprintf(fhandle, "Bad SIDs:\n");
|
---|
260 | it2 = ll_iter_create(card->badsids);
|
---|
261 | while((srvid = ll_iter_next(&it2)))
|
---|
262 | { fprintf(fhandle, "%04X\n", srvid->sid); }
|
---|
263 | card_count++;
|
---|
264 | } // end of while ll_iter_next
|
---|
265 | } // end of if cl->gbox INSERTED && 'p'
|
---|
266 | } // end of for cl->next
|
---|
267 | fclose(fhandle);
|
---|
268 | return;
|
---|
269 | }
|
---|
270 |
|
---|
271 | void hostname2ip(char *hostname, IN_ADDR_T *ip)
|
---|
272 | {
|
---|
273 | cs_resolve(hostname, ip, NULL, NULL);
|
---|
274 | }
|
---|
275 |
|
---|
276 | static uint8_t gbox_compare_pw(uchar *my_pw, uchar *rec_pw)
|
---|
277 | {
|
---|
278 | return my_pw[0] == rec_pw[0] && my_pw[1] == rec_pw[1] && my_pw[2] == rec_pw[2] && my_pw[3] == rec_pw[3];
|
---|
279 | }
|
---|
280 |
|
---|
281 | static uint16_t gbox_convert_password_to_id(uchar *password)
|
---|
282 | {
|
---|
283 | return (password[0] ^ password[2]) << 8 | (password[1] ^ password[3]);
|
---|
284 | }
|
---|
285 |
|
---|
286 | void gbox_add_good_card(struct s_client *cl, uint16_t id_card, uint16_t caid, uint32_t prov, uint16_t sid_ok, uint32_t cw_time)
|
---|
287 | {
|
---|
288 | struct gbox_peer *peer = cl->gbox;
|
---|
289 | struct gbox_card *card = NULL;
|
---|
290 | struct gbox_srvid *srvid = NULL;
|
---|
291 | uint8_t factor = 0;
|
---|
292 | LL_ITER it = ll_iter_create(peer->gbox.cards);
|
---|
293 | while((card = ll_iter_next(&it)))
|
---|
294 | {
|
---|
295 | if(card->peer_id == id_card && card->caid == caid && card->provid == prov)
|
---|
296 | {
|
---|
297 | card->no_cws_returned++;
|
---|
298 | if (!card->no_cws_returned)
|
---|
299 | { card->no_cws_returned = 10; } //wrap around
|
---|
300 | if (card->no_cws_returned < 10)
|
---|
301 | { factor = card->no_cws_returned; }
|
---|
302 | else
|
---|
303 | { factor = 10; }
|
---|
304 | card->average_cw_time = ((card->average_cw_time * (factor-1)) + cw_time) / factor;
|
---|
305 | cl->reader->currenthops = card->dist;
|
---|
306 | LL_ITER it2 = ll_iter_create(card->goodsids);
|
---|
307 | while((srvid = ll_iter_next(&it2)))
|
---|
308 | {
|
---|
309 | if(srvid->sid == sid_ok)
|
---|
310 | {
|
---|
311 | srvid->last_cw_received = time(NULL);
|
---|
312 | return; // sid_ok is already in the list of goodsids
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | LL_ITER it3 = ll_iter_create(card->badsids);
|
---|
317 | while((srvid = ll_iter_next(&it3)))
|
---|
318 | {
|
---|
319 | if(srvid->sid == sid_ok)
|
---|
320 | {
|
---|
321 | ll_iter_remove_data(&it3); // remove sid_ok from badsids
|
---|
322 | break;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | if(!cs_malloc(&srvid, sizeof(struct gbox_srvid)))
|
---|
327 | { return; }
|
---|
328 | srvid->sid = sid_ok;
|
---|
329 | srvid->provid_id = card->provid;
|
---|
330 | srvid->last_cw_received = time(NULL);
|
---|
331 | cs_debug_mask(D_READER, "GBOX Adding good SID: %04X for CAID: %04X Provider: %04X on CardID: %04X\n", sid_ok, caid, card->provid, id_card);
|
---|
332 | ll_append(card->goodsids, srvid);
|
---|
333 | break;
|
---|
334 | }
|
---|
335 | }//end of ll_iter_next
|
---|
336 | //return dist_c;
|
---|
337 | }
|
---|
338 |
|
---|
339 | void gbox_free_card(struct gbox_card *card)
|
---|
340 | {
|
---|
341 | ll_destroy_data_NULL(card->badsids);
|
---|
342 | ll_destroy_data_NULL(card->goodsids);
|
---|
343 | add_garbage(card);
|
---|
344 | return;
|
---|
345 | }
|
---|
346 |
|
---|
347 | void gbox_free_cardlist(LLIST *card_list)
|
---|
348 | {
|
---|
349 | if(card_list)
|
---|
350 | {
|
---|
351 | LL_ITER it = ll_iter_create(card_list);
|
---|
352 | struct gbox_card *card;
|
---|
353 | while((card = ll_iter_next_remove(&it)))
|
---|
354 | {
|
---|
355 | gbox_free_card(card);
|
---|
356 | }
|
---|
357 | ll_destroy_NULL(card_list);
|
---|
358 | }
|
---|
359 | return;
|
---|
360 | }
|
---|
361 |
|
---|
362 | void gbox_init_ecm_request_ext(struct gbox_ecm_request_ext *ere)
|
---|
363 | {
|
---|
364 | /*
|
---|
365 | ere->gbox_crc = 0;
|
---|
366 | ere->gbox_ecm_id = 0;
|
---|
367 | ere->gbox_ecm_ok = 0;
|
---|
368 | */
|
---|
369 | ere->gbox_hops = 0;
|
---|
370 | ere->gbox_peer = 0;
|
---|
371 | ere->gbox_mypeer = 0;
|
---|
372 | ere->gbox_caid = 0;
|
---|
373 | ere->gbox_prid = 0;
|
---|
374 | ere->gbox_slot = 0;
|
---|
375 | ere->gbox_version = 0;
|
---|
376 | ere->gbox_unknown = 0;
|
---|
377 | ere->gbox_type = 0;
|
---|
378 | }
|
---|
379 |
|
---|
380 | struct s_client *get_gbox_proxy(uint16_t gbox_id)
|
---|
381 | {
|
---|
382 | struct s_client *cl;
|
---|
383 | for(cl = first_client; cl; cl = cl->next)
|
---|
384 | {
|
---|
385 | if(cl->typ == 'p' && cl->gbox && cl->gbox_peer_id == gbox_id)
|
---|
386 | {
|
---|
387 | return cl;
|
---|
388 | }
|
---|
389 | }
|
---|
390 | return NULL;
|
---|
391 | }
|
---|
392 |
|
---|
393 | // if input client is typ proxy get client and vice versa
|
---|
394 | struct s_client *switch_client_proxy(struct s_client *cli, uint16_t gbox_id)
|
---|
395 | {
|
---|
396 | struct s_client *cl;
|
---|
397 | int8_t typ;
|
---|
398 | if(cli->typ == 'c')
|
---|
399 | { typ = 'p'; }
|
---|
400 | else
|
---|
401 | { typ = 'c'; }
|
---|
402 | for(cl = first_client; cl; cl = cl->next)
|
---|
403 | {
|
---|
404 | if(cl->typ == typ && cl->gbox && cl->gbox_peer_id == gbox_id)
|
---|
405 | {
|
---|
406 | return cl;
|
---|
407 | }
|
---|
408 | }
|
---|
409 | return cli;
|
---|
410 | }
|
---|
411 |
|
---|
412 | void gbox_reconnect_client(uint16_t gbox_id)
|
---|
413 | {
|
---|
414 | struct s_client *cl;
|
---|
415 | for(cl = first_client; cl; cl = cl->next)
|
---|
416 | {
|
---|
417 | if(cl->gbox && cl->typ == 'p' && cl->gbox_peer_id == gbox_id)
|
---|
418 | {
|
---|
419 | hostname2ip(cl->reader->device, &SIN_GET_ADDR(cl->udp_sa));
|
---|
420 | SIN_GET_FAMILY(cl->udp_sa) = AF_INET;
|
---|
421 | SIN_GET_PORT(cl->udp_sa) = htons((uint16_t)cl->reader->r_port);
|
---|
422 | hostname2ip(cl->reader->device, &(cl->ip));
|
---|
423 | cl->reader->tcp_connected = 0;
|
---|
424 | cl->reader->card_status = NO_CARD;
|
---|
425 | struct gbox_peer *peer = cl->gbox;
|
---|
426 | peer->online = 0;
|
---|
427 | peer->ecm_idx = 0;
|
---|
428 | peer->hello_stat = GBOX_STAT_HELLOL;
|
---|
429 | cl->reader->last_s = cl->reader->last_g = 0;
|
---|
430 | gbox_free_cardlist(peer->gbox.cards);
|
---|
431 | peer->gbox.cards = ll_create("peer.cards");
|
---|
432 | gbox_send_hello(cl);
|
---|
433 | }
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | static void *gbox_server(struct s_client *cli, uchar *b, int32_t l)
|
---|
438 | {
|
---|
439 | if(l > 0)
|
---|
440 | {
|
---|
441 | cs_log("gbox: gbox_server %s/%d", cli->reader->label, cli->port);
|
---|
442 | gbox_check_header(cli, b, l);
|
---|
443 | }
|
---|
444 | return 0;
|
---|
445 | }
|
---|
446 |
|
---|
447 | char *gbox_username(struct s_client *client)
|
---|
448 | {
|
---|
449 | if(!client) { return "anonymous"; }
|
---|
450 | if(client->reader)
|
---|
451 | if(client->reader->r_usr[0])
|
---|
452 | {
|
---|
453 | return client->reader->r_usr;
|
---|
454 | }
|
---|
455 | return "anonymous";
|
---|
456 | }
|
---|
457 |
|
---|
458 | static int8_t gbox_disconnect_double_peers(struct s_client *cli)
|
---|
459 | {
|
---|
460 | struct s_client *cl;
|
---|
461 | for(cl = first_client; cl; cl = cl->next)
|
---|
462 | {
|
---|
463 | if (cl->typ == 'c' && cl->gbox_peer_id == cli->gbox_peer_id && cl != cli)
|
---|
464 | {
|
---|
465 | cs_debug_mask(D_READER, "gbox: disconnected double client %s",username(cl));
|
---|
466 | cs_disconnect_client(cl);
|
---|
467 | }
|
---|
468 | }
|
---|
469 | return 0;
|
---|
470 | }
|
---|
471 |
|
---|
472 | static int8_t gbox_auth_client(struct s_client *cli, uchar *gbox_password)
|
---|
473 | {
|
---|
474 | uint16_t gbox_id = gbox_convert_password_to_id(gbox_password);
|
---|
475 | struct s_client *cl = switch_client_proxy(cli, gbox_id);
|
---|
476 |
|
---|
477 | if(cl->typ == 'p' && cl->gbox && cl->reader)
|
---|
478 | {
|
---|
479 | struct gbox_peer *peer = cl->gbox;
|
---|
480 | struct s_auth *account = get_account_by_name(gbox_username(cl));
|
---|
481 |
|
---|
482 | if (gbox_compare_pw(&peer->gbox.password[0],gbox_password) && account)
|
---|
483 | {
|
---|
484 | cli->crypted = 1; //display as crypted
|
---|
485 | cli->gbox = cl->gbox; //point to the same gbox as proxy
|
---|
486 | cli->reader = cl->reader; //point to the same reader as proxy
|
---|
487 | cli->gbox_peer_id = cl->gbox_peer_id; //signal authenticated
|
---|
488 | gbox_disconnect_double_peers(cli);
|
---|
489 | cs_auth_client(cli, account, NULL);
|
---|
490 | cli->account = account;
|
---|
491 | cli->grp = account->grp;
|
---|
492 | cli->lastecm = time(NULL);
|
---|
493 | return 0;
|
---|
494 | }
|
---|
495 | }
|
---|
496 | return -1;
|
---|
497 | }
|
---|
498 |
|
---|
499 | static void gbox_server_init(struct s_client *cl)
|
---|
500 | {
|
---|
501 | if(!cl->init_done)
|
---|
502 | {
|
---|
503 | if(IP_ISSET(cl->ip))
|
---|
504 | { cs_log("gbox: new connection from %s", cs_inet_ntoa(cl->ip)); }
|
---|
505 | //We cannot authenticate here, because we don't know gbox pw
|
---|
506 | cl->gbox_peer_id = NO_GBOX_ID;
|
---|
507 | cl->init_done = 1;
|
---|
508 | }
|
---|
509 | return;
|
---|
510 | }
|
---|
511 |
|
---|
512 | int8_t get_card_action(struct gbox_card *card, uint32_t provid1, uint16_t peer_id, uint8_t slot, struct gbox_peer *peer)
|
---|
513 | {
|
---|
514 | LL_ITER it;
|
---|
515 | struct gbox_card *card_s;
|
---|
516 | if (!card) { return 1; } //insert
|
---|
517 | if (card->provid_1 < provid1) { return -1; } //remove
|
---|
518 | if (card->peer_id == peer_id && card->provid_1 == provid1 && card->slot == slot)
|
---|
519 | { return 0; } //keep
|
---|
520 | else
|
---|
521 | {
|
---|
522 | it = ll_iter_create(peer->gbox.cards);
|
---|
523 | while ((card_s = ll_iter_next(&it)))
|
---|
524 | {
|
---|
525 | //card is still somewhere else we need to remove current
|
---|
526 | if (card_s->peer_id == peer_id && card_s->provid_1 == provid1 && card_s->slot == slot)
|
---|
527 | { return -1; } //remove
|
---|
528 | }
|
---|
529 | return 1; //insert
|
---|
530 | }
|
---|
531 | }
|
---|
532 |
|
---|
533 | int32_t gbox_cmd_hello(struct s_client *cli, uchar *data, int32_t n)
|
---|
534 | {
|
---|
535 | struct gbox_peer *peer = cli->gbox;
|
---|
536 | int32_t ncards_in_msg = 0;
|
---|
537 | int32_t payload_len = n;
|
---|
538 | //TODO: checkcode_len can be made void
|
---|
539 | int32_t checkcode_len = 0;
|
---|
540 | int32_t hostname_len = 0;
|
---|
541 | int32_t footer_len = 0;
|
---|
542 | uint8_t *ptr = 0;
|
---|
543 | uint8_t *current_ptr = 0;
|
---|
544 | LL_ITER it,previous_it;
|
---|
545 | struct gbox_card *card_s;
|
---|
546 | struct gbox_card *card;
|
---|
547 |
|
---|
548 | if(!(gbox_decode_cmd(data) == MSG_HELLO1))
|
---|
549 | {
|
---|
550 | gbox_decompress(data, &payload_len);
|
---|
551 | }
|
---|
552 | cs_ddump_mask(D_READER, data, payload_len, "gbox: decompressed data (%d bytes):", payload_len);
|
---|
553 |
|
---|
554 | if((data[0xB] & 0xF) == 0) //is first packet
|
---|
555 | {
|
---|
556 | if(!peer->gbox.cards)
|
---|
557 | { peer->gbox.cards = ll_create("peer.cards"); }
|
---|
558 | it = ll_iter_create(peer->gbox.cards);
|
---|
559 | checkcode_len = 7;
|
---|
560 | hostname_len = data[payload_len - 1];
|
---|
561 | footer_len = hostname_len + 2;
|
---|
562 | }
|
---|
563 | else
|
---|
564 | { it = peer->last_it; }
|
---|
565 |
|
---|
566 | if(gbox_decode_cmd(data) == MSG_HELLO1)
|
---|
567 | { ptr = data + 11; }
|
---|
568 | else
|
---|
569 | { ptr = data + 12; }
|
---|
570 |
|
---|
571 | while(ptr < data + payload_len - footer_len - checkcode_len - 1)
|
---|
572 | {
|
---|
573 | uint16_t caid;
|
---|
574 | uint32_t provid;
|
---|
575 | uint32_t provid1;
|
---|
576 |
|
---|
577 | switch(ptr[0])
|
---|
578 | {
|
---|
579 | //Viaccess
|
---|
580 | case 0x05:
|
---|
581 | caid = ptr[0] << 8;
|
---|
582 | provid = ptr[1] << 16 | ptr[2] << 8 | ptr[3];
|
---|
583 | break;
|
---|
584 | //Cryptoworks
|
---|
585 | case 0x0D:
|
---|
586 | caid = ptr[0] << 8 | ptr[1];
|
---|
587 | provid = ptr[2];
|
---|
588 | break;
|
---|
589 | default:
|
---|
590 | caid = ptr[0] << 8 | ptr[1];
|
---|
591 | provid = ptr[2] << 8 | ptr[3];
|
---|
592 | break;
|
---|
593 | }
|
---|
594 |
|
---|
595 | ncards_in_msg += ptr[4];
|
---|
596 |
|
---|
597 | //caid check
|
---|
598 | if(chk_ctab(caid, &cli->reader->ctab))
|
---|
599 | {
|
---|
600 | provid1 = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
|
---|
601 |
|
---|
602 | current_ptr = ptr;
|
---|
603 | ptr += 5;
|
---|
604 |
|
---|
605 | // for all cards of current caid/provid,
|
---|
606 | while (ptr < current_ptr + 5 + current_ptr[4] * 4)
|
---|
607 | {
|
---|
608 | previous_it = it;
|
---|
609 | card_s = ll_iter_next(&it);
|
---|
610 | switch (get_card_action(card_s,provid1,ptr[2] << 8 | ptr[3],ptr[0],peer))
|
---|
611 | {
|
---|
612 | case -1:
|
---|
613 | //IDEA: Later put card to a list of temporary not available cards
|
---|
614 | //reason: not loose good/bad sids
|
---|
615 | //can be later removed by daily garbage collector for example
|
---|
616 | cs_debug_mask(D_READER, "delete card: caid=%04X, provid=%06X, slot=%d, level=%d, dist=%d, peer=%04X",
|
---|
617 | card_s->caid, card_s->provid, card_s->slot, card_s->lvl, card_s->dist, card_s->peer_id);
|
---|
618 | //delete card because not send anymore
|
---|
619 | ll_iter_remove(&it);
|
---|
620 | gbox_free_card(card_s);
|
---|
621 | break;
|
---|
622 | case 0:
|
---|
623 | ptr += 4;
|
---|
624 | break;
|
---|
625 | case 1:
|
---|
626 | // create card info from data and add card to peer.cards
|
---|
627 | if(!cs_malloc(&card, sizeof(struct gbox_card)))
|
---|
628 | { continue; }
|
---|
629 | card->caid = caid;
|
---|
630 | card->provid = provid;
|
---|
631 | card->provid_1 = provid1;
|
---|
632 | card->slot = ptr[0];
|
---|
633 | card->dist = ptr[1] & 0xf;
|
---|
634 | card->lvl = ptr[1] >> 4;
|
---|
635 | card->peer_id = ptr[2] << 8 | ptr[3];
|
---|
636 | card->badsids = ll_create("badsids");
|
---|
637 | card->goodsids = ll_create("goodsids");
|
---|
638 | card->no_cws_returned = 0;
|
---|
639 | card->average_cw_time = 0;
|
---|
640 |
|
---|
641 | if (!card_s)
|
---|
642 | { ll_append(peer->gbox.cards, card); }
|
---|
643 | else
|
---|
644 | {
|
---|
645 | ll_iter_insert(&previous_it, card);
|
---|
646 | it = previous_it;
|
---|
647 | }
|
---|
648 | ll_iter_next(&it);
|
---|
649 | cs_debug_mask(D_READER, "new card: caid=%04X, provid=%06X, slot=%d, level=%d, dist=%d, peer=%04X",
|
---|
650 | card->caid, card->provid, card->slot, card->lvl, card->dist, card->peer_id);
|
---|
651 | ptr += 4;
|
---|
652 | break;
|
---|
653 | default:
|
---|
654 | break;
|
---|
655 | } //switch
|
---|
656 | } // end while cards for provider
|
---|
657 | }
|
---|
658 | else
|
---|
659 | {
|
---|
660 | ptr += 5 + ptr[4] * 4; //skip cards because caid
|
---|
661 | }
|
---|
662 | } // end while caid/provid
|
---|
663 |
|
---|
664 | if(!(data[0x0B] & 0xF)) // first packet. We've got peer hostname
|
---|
665 | {
|
---|
666 | NULLFREE(peer->hostname);
|
---|
667 | if(!cs_malloc(&peer->hostname, hostname_len + 1))
|
---|
668 | {
|
---|
669 | cs_writeunlock(&peer->lock);
|
---|
670 | return -1;
|
---|
671 | }
|
---|
672 | memcpy(peer->hostname, data + payload_len - 1 - hostname_len, hostname_len);
|
---|
673 | peer->hostname[hostname_len] = '\0';
|
---|
674 |
|
---|
675 | gbox_checkcode_recv(cli, data + payload_len - footer_len - checkcode_len - 1);
|
---|
676 | peer->gbox.minor_version = data[payload_len - footer_len - 1];
|
---|
677 | peer->gbox.type = data[payload_len - footer_len];
|
---|
678 | } // end if first hello packet
|
---|
679 |
|
---|
680 | //This is a good night / reset packet (good night data[0xA] / reset !data[0xA]
|
---|
681 | if((data[0x0B] & 0x8F) == 0x80 && !ncards_in_msg) //first + last packet with no cards
|
---|
682 | {
|
---|
683 | gbox_free_cardlist(peer->gbox.cards);
|
---|
684 | peer->online = 0;
|
---|
685 | peer->hello_stat = GBOX_STAT_HELLOL;
|
---|
686 | cli->reader->tcp_connected = 0;
|
---|
687 | cli->reader->card_status = NO_CARD;
|
---|
688 | cli->reader->last_s = cli->reader->last_g = 0;
|
---|
689 | peer->gbox.cards = ll_create("peer.cards");
|
---|
690 | }
|
---|
691 |
|
---|
692 | if(data[0x0B] & 0x80) //last packet
|
---|
693 | {
|
---|
694 | //delete cards at the end of the list if there are some
|
---|
695 | while ((card_s = ll_iter_next(&it)))
|
---|
696 | {
|
---|
697 | cs_debug_mask(D_READER, "delete card: caid=%04X, provid=%06X, slot=%d, level=%d, dist=%d, peer=%04X",
|
---|
698 | card_s->caid, card_s->provid, card_s->slot, card_s->lvl, card_s->dist, card_s->peer_id);
|
---|
699 | //delete card because not send anymore
|
---|
700 | ll_iter_remove(&it);
|
---|
701 | gbox_free_card(card_s);
|
---|
702 | }
|
---|
703 | peer->online = 1;
|
---|
704 | if(!data[0xA])
|
---|
705 | {
|
---|
706 | cs_log("<-HelloS in %d packets from %s (%s:%d) V2.%02X with %d cards filtered to %d cards", (data[0x0B] & 0x0f)+1, cli->reader->label, cs_inet_ntoa(cli->ip), cli->reader->r_port, peer->gbox.minor_version, ncards_in_msg,ll_count(peer->gbox.cards));
|
---|
707 | peer->hello_stat = GBOX_STAT_HELLOR;
|
---|
708 | gbox_send_hello(cli);
|
---|
709 | }
|
---|
710 | else
|
---|
711 | {
|
---|
712 | cs_log("<-HelloR in %d packets from %s (%s:%d) V2.%02X with %d cards filtered to %d cards", (data[0x0B] & 0x0f)+1, cli->reader->label, cs_inet_ntoa(cli->ip), cli->reader->r_port, peer->gbox.minor_version, ncards_in_msg,ll_count(peer->gbox.cards));
|
---|
713 | gbox_send_checkcode(cli);
|
---|
714 | }
|
---|
715 | if(peer->hello_stat == GBOX_STAT_HELLOS)
|
---|
716 | {
|
---|
717 | gbox_send_hello(cli);
|
---|
718 | }
|
---|
719 | cli->reader->tcp_connected = 2; //we have card
|
---|
720 | cli->reader->card_status = CARD_INSERTED;
|
---|
721 | if(ll_count(peer->gbox.cards) == 0)
|
---|
722 | { cli->reader->card_status = NO_CARD; }
|
---|
723 |
|
---|
724 | gbox_write_shared_cards_info();
|
---|
725 | gbox_write_peer_onl();
|
---|
726 | }
|
---|
727 | peer->last_it = it; //save position for next hello
|
---|
728 | return 0;
|
---|
729 | }
|
---|
730 |
|
---|
731 | static int8_t is_blocked_peer(uint16_t peer)
|
---|
732 | {
|
---|
733 | if (peer == NO_GBOX_ID) { return 1; }
|
---|
734 | else { return 0; }
|
---|
735 | }
|
---|
736 |
|
---|
737 | static int8_t gbox_incoming_ecm(struct s_client *cli, uchar *data, int32_t n)
|
---|
738 | {
|
---|
739 | struct gbox_peer *peer;
|
---|
740 | struct s_client *cl;
|
---|
741 | int32_t diffcheck = 0;
|
---|
742 |
|
---|
743 | peer = cli->gbox;
|
---|
744 | if (!peer || !peer->my_user) { return -1; }
|
---|
745 | cl = peer->my_user;
|
---|
746 |
|
---|
747 | // No ECMs with length < MIN_LENGTH expected
|
---|
748 | if ((((data[19] & 0x0f) << 8) | data[20]) < MIN_ECM_LENGTH) { return -1; }
|
---|
749 |
|
---|
750 | // GBOX_MAX_HOPS not violated
|
---|
751 | if (data[n - 15] + 1 > GBOX_MAXHOPS) { return -1; }
|
---|
752 |
|
---|
753 | //Check for blocked peers
|
---|
754 | uint16_t requesting_peer = data[(((data[19] & 0x0f) << 8) | data[20]) + 21] << 8 |
|
---|
755 | data[(((data[19] & 0x0f) << 8) | data[20]) + 22];
|
---|
756 | if (is_blocked_peer(requesting_peer))
|
---|
757 | {
|
---|
758 | cs_debug_mask(D_READER, "ECM from peer %04X blocked", requesting_peer);
|
---|
759 | return -1;
|
---|
760 | }
|
---|
761 |
|
---|
762 | ECM_REQUEST *er;
|
---|
763 | if(!(er = get_ecmtask())) { return -1; }
|
---|
764 |
|
---|
765 | struct gbox_ecm_request_ext *ere;
|
---|
766 | if(!cs_malloc(&ere, sizeof(struct gbox_ecm_request_ext)))
|
---|
767 | {
|
---|
768 | cs_writeunlock(&peer->lock);
|
---|
769 | return -1;
|
---|
770 | }
|
---|
771 |
|
---|
772 | uchar *ecm = data + 18; //offset of ECM in gbx message
|
---|
773 |
|
---|
774 | er->src_data = ere;
|
---|
775 | gbox_init_ecm_request_ext(ere);
|
---|
776 |
|
---|
777 | peer->gbox_count_ecm++;
|
---|
778 | er->gbox_ecm_id = peer->gbox.id;
|
---|
779 |
|
---|
780 | if(peer->ecm_idx == 100) { peer->ecm_idx = 0; }
|
---|
781 |
|
---|
782 | er->idx = peer->ecm_idx++;
|
---|
783 | er->ecmlen = (((ecm[1] & 0x0f) << 8) | ecm[2]) + 3;
|
---|
784 |
|
---|
785 | er->pid = data[10] << 8 | data[11];
|
---|
786 | er->srvid = data[12] << 8 | data[13];
|
---|
787 |
|
---|
788 | if(ecm[er->ecmlen + 5] == 0x05)
|
---|
789 | { er->caid = (ecm[er->ecmlen + 5] << 8); }
|
---|
790 | else
|
---|
791 | { er->caid = (ecm[er->ecmlen + 5] << 8 | ecm[er->ecmlen + 6]); }
|
---|
792 |
|
---|
793 | // ei->extra = data[14] << 8 | data[15];
|
---|
794 | memcpy(er->ecm, data + 18, er->ecmlen);
|
---|
795 | ere->gbox_peer = ecm[er->ecmlen] << 8 | ecm[er->ecmlen + 1];
|
---|
796 | ere->gbox_version = ecm[er->ecmlen + 2];
|
---|
797 | ere->gbox_unknown = ecm[er->ecmlen + 3];
|
---|
798 | ere->gbox_type = ecm[er->ecmlen + 4];
|
---|
799 | ere->gbox_caid = ecm[er->ecmlen + 5] << 8 | ecm[er->ecmlen + 6];
|
---|
800 | ere->gbox_prid = ecm[er->ecmlen + 7] << 8 | ecm[er->ecmlen + 8];
|
---|
801 | ere->gbox_mypeer = ecm[er->ecmlen + 10] << 8 | ecm[er->ecmlen + 11];
|
---|
802 | ere->gbox_slot = ecm[er->ecmlen + 12];
|
---|
803 |
|
---|
804 | diffcheck = gbox_checkcode_recv(cl, data + n - 14);
|
---|
805 | //TODO: What do we do with our own checkcode @-7?
|
---|
806 | er->gbox_crc = gbox_get_ecmchecksum(er);
|
---|
807 | ere->gbox_hops = data[n - 15] + 1;
|
---|
808 | memcpy(&ere->gbox_routing_info[0], &data[n - 15 - ere->gbox_hops + 1], ere->gbox_hops - 1);
|
---|
809 |
|
---|
810 | er->prid = chk_provid(er->ecm, er->caid);
|
---|
811 | cs_debug_mask(D_READER, "<- ECM (%d<-) from server (%s:%d) to cardserver (%04X) SID %04X", ere->gbox_hops, peer->hostname, cli->port, ere->gbox_peer, er->srvid);
|
---|
812 | get_cw(cl, er);
|
---|
813 |
|
---|
814 | //checkcode did not match gbox->peer checkcode
|
---|
815 | if(diffcheck)
|
---|
816 | {
|
---|
817 | // TODO: Send HelloS here?
|
---|
818 | // gbox->peer.hello_stat = GBOX_STAT_HELLOS;
|
---|
819 | // gbox_send_hello(cli);
|
---|
820 | }
|
---|
821 | return 0;
|
---|
822 | }
|
---|
823 |
|
---|
824 | int32_t gbox_cmd_switch(struct s_client *cli, uchar *data, int32_t n)
|
---|
825 | {
|
---|
826 | int32_t n1 = 0, rc1 = 0, i1, idx;
|
---|
827 | uchar dcw[16];
|
---|
828 |
|
---|
829 | switch(gbox_decode_cmd(data))
|
---|
830 | {
|
---|
831 | case MSG_BOXINFO:
|
---|
832 | gbox_send_hello(cli);
|
---|
833 | break;
|
---|
834 | case MSG_GOODBYE:
|
---|
835 | //needfix what to do after Goodbye?
|
---|
836 | //suspect: we get goodbye as signal of SID not found
|
---|
837 | cs_debug_mask(D_READER, "gbox: received goodbye message from %s\n",username(cli));
|
---|
838 | break;
|
---|
839 | case MSG_HELLO1:
|
---|
840 | case MSG_HELLO:
|
---|
841 | if(gbox_cmd_hello(cli, data, n) < 0)
|
---|
842 | { return -1; }
|
---|
843 | break;
|
---|
844 | case MSG_CW:
|
---|
845 | cli->last = time((time_t *)0);
|
---|
846 | idx = gbox_recv_chk(cli, dcw, &rc1, data, n);
|
---|
847 | if(idx < 0) { break; } // no dcw received
|
---|
848 | if(!idx) { idx = cli->last_idx; }
|
---|
849 | cli->reader->last_g = time((time_t *)0); // for reconnect timeout
|
---|
850 | for(i1 = 0, n1 = 0; i1 < cfg.max_pending && n1 == 0; i1++)
|
---|
851 | {
|
---|
852 | if(cli->ecmtask[i1].idx == idx)
|
---|
853 | {
|
---|
854 | cli->pending--;
|
---|
855 | casc_check_dcw(cli->reader, i1, rc1, dcw);
|
---|
856 | n1++;
|
---|
857 | }
|
---|
858 | }
|
---|
859 | break;
|
---|
860 | case MSG_CHECKCODE:
|
---|
861 | gbox_checkcode_recv(cli, data + 10);
|
---|
862 | break;
|
---|
863 | case MSG_ECM:
|
---|
864 | {
|
---|
865 | gbox_incoming_ecm(cli, data, n);
|
---|
866 | break;
|
---|
867 | }
|
---|
868 | default:
|
---|
869 | cs_ddump_mask(D_READER, data, n, "gbox: unknown data received (%d bytes):", n);
|
---|
870 | } // end switch
|
---|
871 | if ((time(NULL) - last_stats_written) > STATS_WRITE_TIME)
|
---|
872 | {
|
---|
873 | gbox_write_stats();
|
---|
874 | last_stats_written = time(NULL);
|
---|
875 | }
|
---|
876 | return 0;
|
---|
877 | }
|
---|
878 |
|
---|
879 | static int8_t gbox_check_header(struct s_client *cli, uchar *data, int32_t l)
|
---|
880 | {
|
---|
881 | struct s_client *cl = switch_client_proxy(cli, cli->gbox_peer_id);
|
---|
882 |
|
---|
883 | //clients may timeout - attach to peer's gbox/reader
|
---|
884 | cli->gbox = cl->gbox; //point to the same gbox as proxy
|
---|
885 | cli->reader = cl->reader; //point to the same reader as proxy
|
---|
886 |
|
---|
887 | struct gbox_peer *peer = cl->gbox;
|
---|
888 |
|
---|
889 | char tmp[0x50];
|
---|
890 | int32_t n = l;
|
---|
891 | cs_ddump_mask(D_READER, data, n, "gbox: encrypted data received (%d bytes):", n);
|
---|
892 |
|
---|
893 | if(gbox_decode_cmd(data) == MSG_HELLO1)
|
---|
894 | { cs_log("test cs2gbox"); }
|
---|
895 | else
|
---|
896 | { gbox_decrypt(data, n, local_gbox.password); }
|
---|
897 |
|
---|
898 | cs_ddump_mask(D_READER, data, n, "gbox: decrypted received data (%d bytes):", n);
|
---|
899 |
|
---|
900 | //verify my pass received
|
---|
901 | if (gbox_compare_pw(&data[2],&local_gbox.password[0]))
|
---|
902 | {
|
---|
903 | cs_debug_mask(D_READER, "received data, peer : %04x data: %s", cli->gbox_peer_id, cs_hexdump(0, data, l, tmp, sizeof(tmp)));
|
---|
904 |
|
---|
905 | if (gbox_decode_cmd(data) != MSG_CW)
|
---|
906 | {
|
---|
907 | if (cli->gbox_peer_id == NO_GBOX_ID)
|
---|
908 | {
|
---|
909 | if (gbox_auth_client(cli, &data[6]) < 0)
|
---|
910 | {
|
---|
911 | cs_debug_mask(D_READER, "gbox: Authentication failed. Please check user in oscam.server and oscam.user");
|
---|
912 | return -1;
|
---|
913 | }
|
---|
914 | //NEEDFIX: Pretty sure this should not be done here
|
---|
915 | gbox_local_cards(cli);
|
---|
916 | cl = switch_client_proxy(cli, cli->gbox_peer_id);
|
---|
917 |
|
---|
918 | //clients may timeout - attach to peer's gbox/reader
|
---|
919 | cli->gbox = cl->gbox; //point to the same gbox as proxy
|
---|
920 | cli->reader = cl->reader; //point to the same reader as proxy
|
---|
921 |
|
---|
922 | peer = cl->gbox;
|
---|
923 | if (peer) { peer->my_user = cli; }
|
---|
924 | }
|
---|
925 | if (!peer) { return -1; }
|
---|
926 | if (!gbox_compare_pw(&data[6],&peer->gbox.password[0]))
|
---|
927 | {
|
---|
928 | cs_log("gbox peer: %04X sends wrong password", peer->gbox.id);
|
---|
929 | return -1;
|
---|
930 | //continue; // next client
|
---|
931 | }
|
---|
932 | } else
|
---|
933 | {
|
---|
934 | // if my pass ok verify CW | pass to peer
|
---|
935 | if(((data[39] != ((local_gbox.id >> 8) & 0xff)) || (data[40] != (local_gbox.id & 0xff))) &&
|
---|
936 | ((data[40] != ((local_gbox.id >> 8) & 0xff)) || (data[41] != (local_gbox.id & 0xff)))) //some mbox CW messages are corrupt
|
---|
937 | {
|
---|
938 | cs_log("gbox peer: %04X sends CW for other than my id: %04X", cli->gbox_peer_id, local_gbox.id);
|
---|
939 | return -1;
|
---|
940 | //continue; // next client
|
---|
941 | }
|
---|
942 | }
|
---|
943 | } // error my pass
|
---|
944 | else
|
---|
945 | {
|
---|
946 | cs_log("gbox: ATTACK ALERT from IP %s", cs_inet_ntoa(cli->ip));
|
---|
947 | cs_log("received data, data: %s", cs_hexdump(0, data, n, tmp, sizeof(tmp)));
|
---|
948 | return -1;
|
---|
949 | //continue; // next client
|
---|
950 | }
|
---|
951 |
|
---|
952 | if (!IP_EQUAL(cli->ip, cl->ip))
|
---|
953 | {
|
---|
954 | gbox_reconnect_client(cli->gbox_peer_id);
|
---|
955 | return -1;
|
---|
956 | }
|
---|
957 |
|
---|
958 | if(!peer) { return -1; }
|
---|
959 |
|
---|
960 | cli->last = time((time_t *)0);
|
---|
961 |
|
---|
962 | cs_writelock(&peer->lock);
|
---|
963 | if(gbox_cmd_switch(cl, data, n) < 0)
|
---|
964 | { return -1; }
|
---|
965 | cs_writeunlock(&peer->lock);
|
---|
966 |
|
---|
967 | return 0;
|
---|
968 | }
|
---|
969 |
|
---|
970 | static int32_t gbox_decode_cmd(uchar *buf)
|
---|
971 | {
|
---|
972 | return buf[0] << 8 | buf[1];
|
---|
973 | }
|
---|
974 |
|
---|
975 | void gbox_code_cmd(uchar *buf, int16_t cmd)
|
---|
976 | {
|
---|
977 | buf[0] = cmd >> 8;
|
---|
978 | buf[1] = cmd & 0xff;
|
---|
979 | }
|
---|
980 |
|
---|
981 | static void gbox_calc_checkcode(void)
|
---|
982 | {
|
---|
983 | int32_t i = 0;
|
---|
984 | struct s_client *cl;
|
---|
985 |
|
---|
986 | local_gbox.checkcode[0] = 0x15;
|
---|
987 | local_gbox.checkcode[1] = 0x30;
|
---|
988 | local_gbox.checkcode[2] = 0x02;
|
---|
989 | local_gbox.checkcode[3] = 0x04;
|
---|
990 | local_gbox.checkcode[4] = 0x19;
|
---|
991 | local_gbox.checkcode[5] = 0x19;
|
---|
992 | local_gbox.checkcode[6] = 0x66;
|
---|
993 |
|
---|
994 | LL_ITER it = ll_iter_create(local_gbox.cards);
|
---|
995 | struct gbox_card *card;
|
---|
996 | while((card = ll_iter_next(&it)))
|
---|
997 | {
|
---|
998 | local_gbox.checkcode[0] ^= (0xFF & (card->provid_1 >> 24));
|
---|
999 | local_gbox.checkcode[1] ^= (0xFF & (card->provid_1 >> 16));
|
---|
1000 | local_gbox.checkcode[2] ^= (0xFF & (card->provid_1 >> 8));
|
---|
1001 | local_gbox.checkcode[3] ^= (0xFF & (card->provid_1));
|
---|
1002 | local_gbox.checkcode[4] ^= (0xFF & (card->slot));
|
---|
1003 | local_gbox.checkcode[5] ^= (0xFF & (card->peer_id >> 8));
|
---|
1004 | local_gbox.checkcode[6] ^= (0xFF & (card->peer_id));
|
---|
1005 | }
|
---|
1006 | for(i = 0, cl = first_client; cl; cl = cl->next, i++)
|
---|
1007 | {
|
---|
1008 | if (cl->gbox && cl->typ == 'p')
|
---|
1009 | {
|
---|
1010 | struct gbox_peer *peer = cl->gbox;
|
---|
1011 | it = ll_iter_create(peer->gbox.cards);
|
---|
1012 | while((card = ll_iter_next(&it)))
|
---|
1013 | {
|
---|
1014 | local_gbox.checkcode[0] ^= (0xFF & (card->provid_1 >> 24));
|
---|
1015 | local_gbox.checkcode[1] ^= (0xFF & (card->provid_1 >> 16));
|
---|
1016 | local_gbox.checkcode[2] ^= (0xFF & (card->provid_1 >> 8));
|
---|
1017 | local_gbox.checkcode[3] ^= (0xFF & (card->provid_1));
|
---|
1018 | local_gbox.checkcode[4] ^= (0xFF & (card->slot));
|
---|
1019 | local_gbox.checkcode[5] ^= (0xFF & (card->peer_id >> 8));
|
---|
1020 | local_gbox.checkcode[6] ^= (0xFF & (card->peer_id));
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 | }
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | //returns 1 if checkcode changed / 0 if not
|
---|
1027 | static int32_t gbox_checkcode_recv(struct s_client *cli, uchar *checkcode)
|
---|
1028 | {
|
---|
1029 | struct gbox_peer *peer = cli->gbox;
|
---|
1030 | char tmp[14];
|
---|
1031 |
|
---|
1032 | if(memcmp(peer->gbox.checkcode, checkcode, 7))
|
---|
1033 | {
|
---|
1034 | memcpy(peer->gbox.checkcode, checkcode, 7);
|
---|
1035 | cs_debug_mask(D_READER, "gbox: received new checkcode=%s", cs_hexdump(0, peer->gbox.checkcode, 14, tmp, sizeof(tmp)));
|
---|
1036 | return 1;
|
---|
1037 | }
|
---|
1038 | return 0;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | uint32_t gbox_get_ecmchecksum(ECM_REQUEST *er)
|
---|
1042 | {
|
---|
1043 |
|
---|
1044 | uint8_t checksum[4];
|
---|
1045 | int32_t counter;
|
---|
1046 |
|
---|
1047 | checksum[3] = er->ecm[0];
|
---|
1048 | checksum[2] = er->ecm[1];
|
---|
1049 | checksum[1] = er->ecm[2];
|
---|
1050 | checksum[0] = er->ecm[3];
|
---|
1051 |
|
---|
1052 | for(counter = 1; counter < (er->ecmlen / 4) - 4; counter++)
|
---|
1053 | {
|
---|
1054 | checksum[3] ^= er->ecm[counter * 4];
|
---|
1055 | checksum[2] ^= er->ecm[counter * 4 + 1];
|
---|
1056 | checksum[1] ^= er->ecm[counter * 4 + 2];
|
---|
1057 | checksum[0] ^= er->ecm[counter * 4 + 3];
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | return checksum[3] << 24 | checksum[2] << 16 | checksum[1] << 8 | checksum[0];
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | static void gbox_send(struct s_client *cli, uchar *buf, int32_t l)
|
---|
1064 | {
|
---|
1065 | struct gbox_peer *peer = cli->gbox;
|
---|
1066 |
|
---|
1067 | cs_ddump_mask(D_READER, buf, l, "gbox: decrypted data send (%d bytes):", l);
|
---|
1068 |
|
---|
1069 | hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
|
---|
1070 | SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
|
---|
1071 | SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)cli->reader->r_port);
|
---|
1072 |
|
---|
1073 | gbox_encrypt(buf, l, peer->gbox.password);
|
---|
1074 | sendto(cli->udp_fd, buf, l, 0, (struct sockaddr *)&cli->udp_sa, cli->udp_sa_len);
|
---|
1075 | cs_ddump_mask(D_READER, buf, l, "gbox: encrypted data send (%d bytes):", l);
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | static void gbox_send_hello_packet(struct s_client *cli, int8_t number, uchar *outbuf, uchar *ptr, int32_t nbcards)
|
---|
1079 | {
|
---|
1080 | struct gbox_peer *peer = cli->gbox;
|
---|
1081 | int32_t hostname_len = strlen(cfg.gbox_hostname);
|
---|
1082 | int32_t len;
|
---|
1083 |
|
---|
1084 | gbox_code_cmd(outbuf, MSG_HELLO);
|
---|
1085 | memcpy(outbuf + 2, peer->gbox.password, 4);
|
---|
1086 | memcpy(outbuf + 6, local_gbox.password, 4);
|
---|
1087 | // initial HELLO = 0, subsequent = 1
|
---|
1088 | if(peer->hello_stat > GBOX_STAT_HELLOS)
|
---|
1089 | { outbuf[10] = 1; }
|
---|
1090 | else
|
---|
1091 | { outbuf[10] = 0; }
|
---|
1092 | outbuf[11] = number; // 0x80 (if last packet) else 0x00 | packet number
|
---|
1093 |
|
---|
1094 | if((number & 0x0F) == 0)
|
---|
1095 | {
|
---|
1096 | gbox_calc_checkcode();
|
---|
1097 | if(peer->hello_stat != GBOX_STAT_HELLOL)
|
---|
1098 | { memcpy(++ptr, local_gbox.checkcode, 7); }
|
---|
1099 | else
|
---|
1100 | { memset(++ptr, 0, 7); }
|
---|
1101 | ptr += 7;
|
---|
1102 | *ptr = local_gbox.minor_version;
|
---|
1103 | *(++ptr) = local_gbox.type;
|
---|
1104 | memcpy(++ptr, cfg.gbox_hostname, hostname_len);
|
---|
1105 | ptr += hostname_len;
|
---|
1106 | *ptr = hostname_len;
|
---|
1107 | }
|
---|
1108 | len = ptr - outbuf + 1;
|
---|
1109 | switch(peer->hello_stat)
|
---|
1110 | {
|
---|
1111 | case GBOX_STAT_HELLOL:
|
---|
1112 | cs_log("gbox: send HELLOL to %s", cli->reader->label);
|
---|
1113 | if((number & 0x80) == 0x80)
|
---|
1114 | { peer->hello_stat = GBOX_STAT_HELLOS; }
|
---|
1115 | break;
|
---|
1116 | case GBOX_STAT_HELLOS:
|
---|
1117 | cs_log("gbox: send HELLOS total cards %d to %s", nbcards, cli->reader->label);
|
---|
1118 | if((number & 0x80) == 0x80)
|
---|
1119 | { peer->hello_stat = GBOX_STAT_HELLO3; }
|
---|
1120 | break;
|
---|
1121 | case GBOX_STAT_HELLOR:
|
---|
1122 | cs_log("gbox: send HELLOR total cards %d to %s", nbcards, cli->reader->label);
|
---|
1123 | if((number & 0x80) == 0x80)
|
---|
1124 | { peer->hello_stat = GBOX_STAT_HELLO3; }
|
---|
1125 | break;
|
---|
1126 | default:
|
---|
1127 | cs_log("gbox: send hello total cards %d to %s", nbcards, cli->reader->label);
|
---|
1128 | break;
|
---|
1129 | }
|
---|
1130 | cs_ddump_mask(D_READER, outbuf, len, "send hello, (len=%d):", len);
|
---|
1131 |
|
---|
1132 | gbox_compress(outbuf, len, &len);
|
---|
1133 |
|
---|
1134 | gbox_send(cli, outbuf, len);
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | static void gbox_send_hello(struct s_client *cli)
|
---|
1138 | {
|
---|
1139 | struct gbox_peer *peer = cli->gbox;
|
---|
1140 |
|
---|
1141 | int32_t nbcards = 0;
|
---|
1142 | int32_t packet;
|
---|
1143 | uchar buf[2048];
|
---|
1144 | /*
|
---|
1145 | int32_t ok = 0;
|
---|
1146 | #ifdef WEBIF
|
---|
1147 | ok = check_ip(cfg.http_allowed, cli->ip) ? 1 : 0;
|
---|
1148 | #endif
|
---|
1149 | */
|
---|
1150 | packet = 0;
|
---|
1151 | uchar *ptr = buf + 11;
|
---|
1152 | if(ll_count(local_gbox.cards) != 0 && peer->hello_stat > GBOX_STAT_HELLOL)
|
---|
1153 | {
|
---|
1154 | memset(buf, 0, sizeof(buf));
|
---|
1155 |
|
---|
1156 | LL_ITER it = ll_iter_create(local_gbox.cards);
|
---|
1157 | struct gbox_card *card;
|
---|
1158 | while((card = ll_iter_next(&it)))
|
---|
1159 | {
|
---|
1160 | //send to user only cards which matching CAID from account and lvl > 0
|
---|
1161 | if(chk_ctab(card->caid, &peer->my_user->account->ctab) && card->lvl > 0)
|
---|
1162 | {
|
---|
1163 | *(++ptr) = card->provid_1 >> 24;
|
---|
1164 | *(++ptr) = card->provid_1 >> 16;
|
---|
1165 | *(++ptr) = card->provid_1 >> 8;
|
---|
1166 | *(++ptr) = card->provid_1 & 0xff;
|
---|
1167 | *(++ptr) = 1; //note: original gbx is more efficient and sends all cards of one caid as package
|
---|
1168 | *(++ptr) = card->slot;
|
---|
1169 | //If you modify the next line you are going to destroy the community
|
---|
1170 | //It will be recognized by original gbx and you will get banned
|
---|
1171 | *(++ptr) = ((card->lvl - 1) << 4) + card->dist + 1;
|
---|
1172 | *(++ptr) = card->peer_id >> 8;
|
---|
1173 | *(++ptr) = card->peer_id & 0xff;
|
---|
1174 | nbcards++;
|
---|
1175 | if(nbcards == 100) //check if 100 is good or we need more sophisticated algorithm
|
---|
1176 | {
|
---|
1177 | gbox_send_hello_packet(cli, packet, buf, ptr, nbcards);
|
---|
1178 | packet++;
|
---|
1179 | nbcards = 0;
|
---|
1180 | ptr = buf + 11;
|
---|
1181 | memset(buf, 0, sizeof(buf));
|
---|
1182 | }
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 | } // end if local card exists
|
---|
1186 |
|
---|
1187 | //last packet has bit 0x80 set
|
---|
1188 | gbox_send_hello_packet(cli, 0x80 | packet, buf, ptr, nbcards);
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | static void gbox_send_checkcode(struct s_client *cli)
|
---|
1192 | {
|
---|
1193 | struct gbox_peer *peer = cli->gbox;
|
---|
1194 | uchar outbuf[20];
|
---|
1195 |
|
---|
1196 | gbox_calc_checkcode();
|
---|
1197 | gbox_code_cmd(outbuf, MSG_CHECKCODE);
|
---|
1198 | memcpy(outbuf + 2, peer->gbox.password, 4);
|
---|
1199 | memcpy(outbuf + 6, local_gbox.password, 4);
|
---|
1200 | memcpy(outbuf + 10, local_gbox.checkcode, 7);
|
---|
1201 |
|
---|
1202 | gbox_send(cli, outbuf, 17);
|
---|
1203 | }
|
---|
1204 | /*
|
---|
1205 | static void gbox_send_boxinfo(struct s_client *cli)
|
---|
1206 | {
|
---|
1207 | struct gbox_peer *peer = cli->gbox;
|
---|
1208 | uchar outbuf[256];
|
---|
1209 | int32_t hostname_len = strlen(cfg.gbox_hostname);
|
---|
1210 |
|
---|
1211 | gbox_code_cmd(outbuf, MSG_BOXINFO);
|
---|
1212 | memcpy(outbuf + 2, peer->gbox.password, 4);
|
---|
1213 | memcpy(outbuf + 6, local_gbox.password, 4);
|
---|
1214 | outbuf[0xA] = local_gbox.minor_version;
|
---|
1215 | outbuf[0xB] = local_gbox.type;
|
---|
1216 | memcpy(&outbuf[0xC], cfg.gbox_hostname, hostname_len);
|
---|
1217 | gbox_send(cli, outbuf, hostname_len + 0xC);
|
---|
1218 | }
|
---|
1219 | */
|
---|
1220 | static int32_t gbox_recv(struct s_client *cli, uchar *buf, int32_t l)
|
---|
1221 | {
|
---|
1222 | uchar data[RECEIVE_BUFFER_SIZE];
|
---|
1223 | int32_t n = l;
|
---|
1224 |
|
---|
1225 | if(cli->udp_fd && cli->is_udp && cli->typ == 'c')
|
---|
1226 | {
|
---|
1227 | n = recv_from_udpipe(buf);
|
---|
1228 | if (n > MIN_GBOX_MESSAGE_LENGTH && n < RECEIVE_BUFFER_SIZE) //protect against too short or too long messages
|
---|
1229 | {
|
---|
1230 | memcpy(&data[0], buf, n);
|
---|
1231 | gbox_check_header(cli, &data[0], n);
|
---|
1232 |
|
---|
1233 | //clients may timeout - dettach from peer's gbox/reader
|
---|
1234 | cli->gbox = NULL;
|
---|
1235 | cli->reader = NULL;
|
---|
1236 | return 0;
|
---|
1237 | }
|
---|
1238 | }
|
---|
1239 | return -1;
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | static void gbox_send_dcw(struct s_client *cl, ECM_REQUEST *er)
|
---|
1243 | {
|
---|
1244 | struct s_client *cli = switch_client_proxy(cl, cl->gbox_peer_id);
|
---|
1245 | struct gbox_peer *peer = cli->gbox;
|
---|
1246 |
|
---|
1247 | peer->gbox_count_ecm--;
|
---|
1248 | if(er->rc >= E_NOTFOUND)
|
---|
1249 | {
|
---|
1250 | cs_debug_mask(D_READER, "gbox: unable to decode!");
|
---|
1251 | return;
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | uchar buf[60];
|
---|
1255 | memset(buf, 0, sizeof(buf));
|
---|
1256 |
|
---|
1257 | struct gbox_ecm_request_ext *ere = er->src_data;
|
---|
1258 |
|
---|
1259 | gbox_code_cmd(buf, MSG_CW);
|
---|
1260 | buf[2] = peer->gbox.password[0]; //Peer key
|
---|
1261 | buf[3] = peer->gbox.password[1]; //Peer key
|
---|
1262 | buf[4] = peer->gbox.password[2]; //Peer key
|
---|
1263 | buf[5] = peer->gbox.password[3]; //Peer key
|
---|
1264 | buf[6] = er->pid >> 8; //PID
|
---|
1265 | buf[7] = er->pid & 0xff; //PID
|
---|
1266 | buf[8] = er->srvid >> 8; //SrvID
|
---|
1267 | buf[9] = er->srvid & 0xff; //SrvID
|
---|
1268 | buf[10] = ere->gbox_mypeer >> 8; //From peer
|
---|
1269 | buf[11] = ere->gbox_mypeer & 0xff; //From peer
|
---|
1270 | buf[12] = (ere->gbox_slot << 4) | (er->ecm[0] & 0x0f); //slot << 4 | even/odd
|
---|
1271 | buf[13] = ere->gbox_caid >> 8; //CAID first byte
|
---|
1272 | memcpy(buf + 14, er->cw, 16); //CW
|
---|
1273 | buf[30] = er->gbox_crc >> 24; //CRC
|
---|
1274 | buf[31] = er->gbox_crc >> 16; //CRC
|
---|
1275 | buf[32] = er->gbox_crc >> 8; //CRC
|
---|
1276 | buf[33] = er->gbox_crc & 0xff; //CRC
|
---|
1277 | buf[34] = ere->gbox_caid >> 8; //CAID
|
---|
1278 | buf[35] = ere->gbox_caid & 0xff; //CAID
|
---|
1279 | buf[36] = ere->gbox_slot; //Slot
|
---|
1280 | if (buf[34] == 0x06) //if irdeto
|
---|
1281 | {
|
---|
1282 | buf[37] = er->chid >> 8; //CHID
|
---|
1283 | buf[38] = er->chid & 0xff; //CHID
|
---|
1284 | }
|
---|
1285 | else
|
---|
1286 | {
|
---|
1287 | if (local_gbox.minor_version == 0x2A)
|
---|
1288 | {
|
---|
1289 | buf[37] = 0xff; //gbox.net sends 0xff
|
---|
1290 | buf[38] = 0xff; //gbox.net sends 0xff
|
---|
1291 | }
|
---|
1292 | else
|
---|
1293 | {
|
---|
1294 | buf[37] = 0; //gbox sends 0
|
---|
1295 | buf[38] = 0; //gbox sends 0
|
---|
1296 | }
|
---|
1297 | }
|
---|
1298 | buf[39] = ere->gbox_peer >> 8; //Target peer
|
---|
1299 | buf[40] = ere->gbox_peer & 0xff; //Target peer
|
---|
1300 | if (er->rc == E_CACHE1 || er->rc == E_CACHE2 || er->rc == E_CACHEEX)
|
---|
1301 | { buf[41] = 0x03; } //cache
|
---|
1302 | else
|
---|
1303 | { buf[41] = 0x01; } //card, emu, needs probably further investigation
|
---|
1304 | buf[42] = 0x30; //1st nibble unknown / 2nd nibble distance
|
---|
1305 | buf[43] = ere->gbox_unknown; //meaning unknown, copied from ECM request
|
---|
1306 |
|
---|
1307 | //This copies the routing info from ECM to answer.
|
---|
1308 | //Each hop adds one byte and number of hops is in er->gbox_hops.
|
---|
1309 | memcpy(&buf[44], &ere->gbox_routing_info, ere->gbox_hops - 1);
|
---|
1310 | buf[44 + ere->gbox_hops - 1] = ere->gbox_hops - 1; //Hops
|
---|
1311 | /*
|
---|
1312 | char tmp[0x50];
|
---|
1313 | cs_log("sending dcw to peer : %04x data: %s", er->gbox_peer, cs_hexdump(0, buf, er->gbox_hops + 44, tmp, sizeof(tmp)));
|
---|
1314 | */
|
---|
1315 | gbox_send(cli, buf, ere->gbox_hops + 44);
|
---|
1316 |
|
---|
1317 | cs_debug_mask(D_READER, "-> CW (->%d) from %s/%d (%04X) ", ere->gbox_hops, cli->reader->label, cli->port, ere->gbox_peer);
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | static uint8_t gbox_next_free_slot(uint16_t id)
|
---|
1321 | {
|
---|
1322 | LL_ITER it = ll_iter_create(local_gbox.cards);
|
---|
1323 | struct gbox_card *c;
|
---|
1324 | uint8_t lastslot = 0;
|
---|
1325 |
|
---|
1326 | while((c = ll_iter_next(&it)))
|
---|
1327 | {
|
---|
1328 | if(id == c->peer_id && c->slot > lastslot)
|
---|
1329 | { lastslot = c->slot; }
|
---|
1330 | }
|
---|
1331 | return ++lastslot;
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | static void gbox_add_local_card(uint16_t id, uint16_t caid, uint32_t prid, uint8_t slot, uint8_t card_reshare, uint8_t dist)
|
---|
1335 | {
|
---|
1336 | struct gbox_card *c;
|
---|
1337 |
|
---|
1338 | //don't insert 0100:000000
|
---|
1339 | if((caid >> 8 == 0x01) && (!prid))
|
---|
1340 | {
|
---|
1341 | return;
|
---|
1342 | }
|
---|
1343 | //skip CAID 18XX providers
|
---|
1344 | if((caid >> 8 == 0x18) && (prid))
|
---|
1345 | {
|
---|
1346 | return;
|
---|
1347 | }
|
---|
1348 | if(!cs_malloc(&c, sizeof(struct gbox_card)))
|
---|
1349 | {
|
---|
1350 | return;
|
---|
1351 | }
|
---|
1352 | c->caid = caid;
|
---|
1353 | switch(caid >> 8)
|
---|
1354 | {
|
---|
1355 | // Viaccess
|
---|
1356 | case 0x05:
|
---|
1357 | c->provid_1 = (caid >> 8) << 24 | (prid & 0xFFFFFF);
|
---|
1358 | break;
|
---|
1359 | // Cryptoworks
|
---|
1360 | case 0x0D:
|
---|
1361 | c->provid_1 = (caid >> 8) << 24 | (caid & 0xFF) << 16 |
|
---|
1362 | ((prid << 8) & 0xFF00);
|
---|
1363 | break;
|
---|
1364 | default:
|
---|
1365 | c->provid_1 = (caid >> 8) << 24 | (caid & 0xFF) << 16 |
|
---|
1366 | (prid & 0xFFFF);
|
---|
1367 | break;
|
---|
1368 | }
|
---|
1369 | c->provid = prid;
|
---|
1370 | c->peer_id = id;
|
---|
1371 | c->slot = slot;
|
---|
1372 | c->lvl = card_reshare;
|
---|
1373 | c->dist = dist;
|
---|
1374 | ll_append(local_gbox.cards, c);
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | static void gbox_local_cards(struct s_client *cli)
|
---|
1378 | {
|
---|
1379 | int32_t i;
|
---|
1380 | uint32_t prid = 0;
|
---|
1381 | int8_t slot = 0;
|
---|
1382 | char card_reshare;
|
---|
1383 | #ifdef MODULE_CCCAM
|
---|
1384 | LL_ITER it, it2;
|
---|
1385 | struct cc_card *card = NULL;
|
---|
1386 | struct cc_data *cc;
|
---|
1387 | uint32_t checksum = 0;
|
---|
1388 | uint16_t cc_peer_id = 0;
|
---|
1389 | struct cc_provider *provider;
|
---|
1390 | uint8_t *node1 = NULL;
|
---|
1391 | #endif
|
---|
1392 |
|
---|
1393 | if(!local_gbox.cards)
|
---|
1394 | {
|
---|
1395 | gbox_free_cardlist(local_gbox.cards);
|
---|
1396 | }
|
---|
1397 | local_gbox.cards = ll_create("local_cards");
|
---|
1398 |
|
---|
1399 | //value >5 not allowed in gbox network
|
---|
1400 | if(cli->reader->gbox_reshare > 5)
|
---|
1401 | { card_reshare = 5; }
|
---|
1402 | else
|
---|
1403 | { card_reshare = cli->reader->gbox_reshare; }
|
---|
1404 |
|
---|
1405 | struct s_client *cl;
|
---|
1406 | for(cl = first_client; cl; cl = cl->next)
|
---|
1407 | {
|
---|
1408 | if(cl->typ == 'r' && cl->reader && cl->reader->card_status == 2)
|
---|
1409 | {
|
---|
1410 | slot = gbox_next_free_slot(local_gbox.id);
|
---|
1411 | //SECA, Viaccess and Cryptoworks have multiple providers
|
---|
1412 | if((cl->reader->caid >> 8 == 0x01) || (cl->reader->caid >> 8 == 0x05) ||
|
---|
1413 | (cl->reader->caid >> 8 == 0x0D))
|
---|
1414 | {
|
---|
1415 | for(i = 0; i < cl->reader->nprov; i++)
|
---|
1416 | {
|
---|
1417 | prid = cl->reader->prid[i][1] << 16 |
|
---|
1418 | cl->reader->prid[i][2] << 8 | cl->reader->prid[i][3];
|
---|
1419 | gbox_add_local_card(local_gbox.id, cl->reader->caid, prid, slot, card_reshare, 0);
|
---|
1420 | }
|
---|
1421 | }
|
---|
1422 | else
|
---|
1423 | {
|
---|
1424 | gbox_add_local_card(local_gbox.id, cl->reader->caid, 0, slot, card_reshare, 0);
|
---|
1425 |
|
---|
1426 | //Check for Betatunnel on gbox account in oscam.user
|
---|
1427 | if (chk_is_betatunnel_caid(cl->reader->caid) == 1 && cli->ttab.n && cl->reader->caid == cli->ttab.bt_caidto[0])
|
---|
1428 | {
|
---|
1429 | //For now only first entry in tunnel tab. No sense in iteration?
|
---|
1430 | //Add betatunnel card to transmitted list
|
---|
1431 | gbox_add_local_card(local_gbox.id, cli->ttab.bt_caidfrom[0], 0, slot, card_reshare, 0);
|
---|
1432 | cs_debug_mask(D_READER, "gbox created betatunnel card for caid: %04X->%04X",cli->ttab.bt_caidfrom[0],cl->reader->caid);
|
---|
1433 | }
|
---|
1434 | }
|
---|
1435 | } //end local readers
|
---|
1436 | #ifdef MODULE_CCCAM
|
---|
1437 | if(cl->typ == 'p' && cl->reader
|
---|
1438 | && cl->reader->typ == R_CCCAM && cl->cc)
|
---|
1439 | {
|
---|
1440 | cc = cl->cc;
|
---|
1441 | it = ll_iter_create(cc->cards);
|
---|
1442 | while((card = ll_iter_next(&it)))
|
---|
1443 | {
|
---|
1444 | //calculate gbox id from cc node
|
---|
1445 | //1st node is orgin, shorten to 32Bit by CRC, the GBX-ID like from PW
|
---|
1446 | node1 = ll_has_elements(card->remote_nodes);
|
---|
1447 | checksum = (uint32_t)crc32(0L, node1, 8);
|
---|
1448 | cc_peer_id = ((((checksum >> 24) & 0xFF) ^((checksum >> 8) & 0xFF)) << 8 |
|
---|
1449 | (((checksum >> 16) & 0xFF) ^(checksum & 0xFF)));
|
---|
1450 | slot = gbox_next_free_slot(cc_peer_id);
|
---|
1451 | if((card->caid >> 8 == 0x01) || (card->caid >> 8 == 0x05) ||
|
---|
1452 | (card->caid >> 8 == 0x0D))
|
---|
1453 | {
|
---|
1454 | it2 = ll_iter_create(card->providers);
|
---|
1455 | while((provider = ll_iter_next(&it2)))
|
---|
1456 | {
|
---|
1457 | gbox_add_local_card(cc_peer_id, card->caid, provider->prov, slot, card->reshare, card->hop);
|
---|
1458 | }
|
---|
1459 | }
|
---|
1460 | else
|
---|
1461 | { gbox_add_local_card(cc_peer_id, card->caid, 0, slot, card->reshare, card->hop); }
|
---|
1462 | }
|
---|
1463 | } //end cccam
|
---|
1464 | #endif
|
---|
1465 | } //end for clients
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | static int32_t gbox_client_init(struct s_client *cli)
|
---|
1469 | {
|
---|
1470 | if(!cfg.gbox_port || cfg.gbox_port > 65535)
|
---|
1471 | {
|
---|
1472 | cs_log("gbox: error, no/invalid port=%d configured in oscam.conf!",
|
---|
1473 | cfg.gbox_port ? cfg.gbox_port : 0);
|
---|
1474 | return -1;
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 | if(!cfg.gbox_hostname || strlen(cfg.gbox_hostname) > 128)
|
---|
1478 | {
|
---|
1479 | cs_log("gbox: error, no/invalid hostname '%s' configured in oscam.conf!",
|
---|
1480 | cfg.gbox_hostname ? cfg.gbox_hostname : "");
|
---|
1481 | return -1;
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | if(!local_gbox.id)
|
---|
1485 | {
|
---|
1486 | cs_log("gbox: error, no/invalid password '%s' configured in oscam.conf!",
|
---|
1487 | cfg.gbox_my_password ? cfg.gbox_my_password : "");
|
---|
1488 | return -1;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | if(!cs_malloc(&cli->gbox, sizeof(struct gbox_peer)))
|
---|
1492 | { return -1; }
|
---|
1493 |
|
---|
1494 | struct gbox_peer *peer = cli->gbox;
|
---|
1495 | struct s_reader *rdr = cli->reader;
|
---|
1496 |
|
---|
1497 | rdr->card_status = CARD_NEED_INIT;
|
---|
1498 | rdr->tcp_connected = 0;
|
---|
1499 |
|
---|
1500 | memset(peer, 0, sizeof(struct gbox_peer));
|
---|
1501 |
|
---|
1502 | uint32_t r_pwd = a2i(rdr->r_pwd, 4);
|
---|
1503 | int32_t i;
|
---|
1504 | for(i = 3; i >= 0; i--)
|
---|
1505 | {
|
---|
1506 | peer->gbox.password[3 - i] = (r_pwd >> (8 * i)) & 0xff;
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | cs_ddump_mask(D_READER, peer->gbox.password, 4, "Peer password: %s:", rdr->r_pwd);
|
---|
1510 |
|
---|
1511 | peer->gbox.id = gbox_convert_password_to_id(&peer->gbox.password[0]);
|
---|
1512 | if (get_gbox_proxy(peer->gbox.id) || peer->gbox.id == NO_GBOX_ID || peer->gbox.id == local_gbox.id)
|
---|
1513 | {
|
---|
1514 | cs_log("gbox: error, double/invalid gbox id: %04X", peer->gbox.id);
|
---|
1515 | return -1;
|
---|
1516 | }
|
---|
1517 | cli->gbox_peer_id = peer->gbox.id;
|
---|
1518 |
|
---|
1519 | cli->pfd = 0;
|
---|
1520 | cli->crypted = 1;
|
---|
1521 |
|
---|
1522 | set_null_ip(&cli->ip);
|
---|
1523 |
|
---|
1524 | if((cli->udp_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
|
---|
1525 | {
|
---|
1526 | cs_log("socket creation failed (errno=%d %s)", errno, strerror(errno));
|
---|
1527 | cs_disconnect_client(cli);
|
---|
1528 | }
|
---|
1529 |
|
---|
1530 | int32_t opt = 1;
|
---|
1531 | setsockopt(cli->udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
---|
1532 |
|
---|
1533 | #ifdef SO_REUSEPORT
|
---|
1534 | setsockopt(cli->udp_fd, SOL_SOCKET, SO_REUSEPORT, (void *)&opt, sizeof(opt));
|
---|
1535 | #endif
|
---|
1536 |
|
---|
1537 | set_socket_priority(cli->udp_fd, cfg.netprio);
|
---|
1538 |
|
---|
1539 | memset((char *)&cli->udp_sa, 0, sizeof(cli->udp_sa));
|
---|
1540 |
|
---|
1541 | if(!hostResolve(rdr))
|
---|
1542 | { return 0; }
|
---|
1543 |
|
---|
1544 | cli->port = rdr->r_port;
|
---|
1545 | SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
|
---|
1546 | SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)rdr->r_port);
|
---|
1547 | hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
|
---|
1548 |
|
---|
1549 | cs_log("proxy %s (fd=%d, peer id=%04X, my id=%04X, my hostname=%s, my listen port=%d, peer's listen port=%d)",
|
---|
1550 | rdr->device, cli->udp_fd, peer->gbox.id, local_gbox.id, cfg.gbox_hostname, cfg.gbox_port, rdr->r_port);
|
---|
1551 |
|
---|
1552 | cli->pfd = cli->udp_fd;
|
---|
1553 |
|
---|
1554 | cs_lock_create(&peer->lock, "gbox_lock", 5000);
|
---|
1555 |
|
---|
1556 | peer->online = 0;
|
---|
1557 | peer->ecm_idx = 0;
|
---|
1558 | peer->hello_stat = GBOX_STAT_HELLOL;
|
---|
1559 | peer->my_user = NULL;
|
---|
1560 |
|
---|
1561 | cli->reader->card_status = CARD_NEED_INIT;
|
---|
1562 | gbox_send_hello(cli);
|
---|
1563 |
|
---|
1564 | if(!cli->reader->gbox_maxecmsend)
|
---|
1565 | { cli->reader->gbox_maxecmsend = DEFAULT_GBOX_MAX_ECM_SEND; }
|
---|
1566 |
|
---|
1567 | if(!cli->reader->gbox_maxdist)
|
---|
1568 | { cli->reader->gbox_maxdist = DEFAULT_GBOX_MAX_DIST; }
|
---|
1569 |
|
---|
1570 | return 0;
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | static int32_t gbox_recv_chk(struct s_client *cli, uchar *dcw, int32_t *rc, uchar *data, int32_t n)
|
---|
1574 | {
|
---|
1575 | if(gbox_decode_cmd(data) == MSG_CW && n > 43)
|
---|
1576 | {
|
---|
1577 | int i, k;
|
---|
1578 | uint16_t id_card = 0;
|
---|
1579 | struct s_client *cl;
|
---|
1580 | if(cli->typ != 'p')
|
---|
1581 | {
|
---|
1582 | cl = switch_client_proxy(cli, cli->gbox_peer_id);
|
---|
1583 | }
|
---|
1584 | else
|
---|
1585 | {
|
---|
1586 | cl = cli;
|
---|
1587 | }
|
---|
1588 | *rc = 1;
|
---|
1589 | memcpy(dcw, data + 14, 16);
|
---|
1590 | uint32_t crc = data[30] << 24 | data[31] << 16 | data[32] << 8 | data[33];
|
---|
1591 | char tmp[32];
|
---|
1592 | cs_debug_mask(D_READER, "gbox: received cws=%s, peer=%04X, ecm_pid=%04X, sid=%04X, crc=%08X, type=%02X, dist=%01X, unkn1=%01X, unkn2=%02X, chid/0x0000/0xffff=%04X",
|
---|
1593 | cs_hexdump(0, dcw, 32, tmp, sizeof(tmp)),
|
---|
1594 | data[10] << 8 | data[11], data[6] << 8 | data[7], data[8] << 8 | data[9], crc, data[41], data[42] & 0x0f, data[42] >> 4, data[43], data[37] << 8 | data[38]);
|
---|
1595 |
|
---|
1596 | struct timeb t_now;
|
---|
1597 | for(i = 0, k = 0; i < cfg.max_pending && k == 0; i++)
|
---|
1598 | {
|
---|
1599 | if(cl->ecmtask[i].gbox_crc == crc)
|
---|
1600 | {
|
---|
1601 | id_card = data[10] << 8 | data[11];
|
---|
1602 | cs_ftime(&t_now);
|
---|
1603 | gbox_add_good_card(cl, id_card, cl->ecmtask[i].caid, cl->ecmtask[i].prid, cl->ecmtask[i].srvid, comp_timeb(&t_now, &cl->ecmtask[i].tps));
|
---|
1604 | if(cl->ecmtask[i].gbox_ecm_ok == 0 || cl->ecmtask[i].gbox_ecm_ok == 2)
|
---|
1605 | { return -1; }
|
---|
1606 | struct s_ecm_answer ea;
|
---|
1607 | memset(&ea, 0, sizeof(struct s_ecm_answer));
|
---|
1608 | cl->ecmtask[i].gbox_ecm_ok = 2;
|
---|
1609 | memcpy(ea.cw, dcw, 16);
|
---|
1610 | *rc = 1;
|
---|
1611 | return cl->ecmtask[i].idx;
|
---|
1612 | }
|
---|
1613 | }
|
---|
1614 | cs_debug_mask(D_READER, "gbox: no task found for crc=%08x", crc);
|
---|
1615 | }
|
---|
1616 | return -1;
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | static int32_t gbox_send_ecm(struct s_client *cli, ECM_REQUEST *er, uchar *UNUSED(buf))
|
---|
1620 | {
|
---|
1621 | struct gbox_peer *peer = cli->gbox;
|
---|
1622 | int32_t cont_1;
|
---|
1623 | uint32_t sid_verified = 0;
|
---|
1624 | // uint32_t time_since_lastcw = 0;
|
---|
1625 | /* struct gbox_ecm_request_ext *ere;
|
---|
1626 |
|
---|
1627 | if (!er->src_data) {
|
---|
1628 | if(!cs_malloc(&ere, sizeof(struct gbox_ecm_request_ext)))
|
---|
1629 | {
|
---|
1630 | cs_writeunlock(&gbox->lock);
|
---|
1631 | return -1;
|
---|
1632 | }
|
---|
1633 | er->src_data = ere;
|
---|
1634 | gbox_init_ecm_request_ext(ere);
|
---|
1635 | }
|
---|
1636 | else
|
---|
1637 | ere = er->src_data;
|
---|
1638 | */
|
---|
1639 | if(!peer || !cli->reader->tcp_connected)
|
---|
1640 | {
|
---|
1641 | cs_debug_mask(D_READER, "gbox: %s server not init!", cli->reader->label);
|
---|
1642 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1643 |
|
---|
1644 | return -1;
|
---|
1645 | }
|
---|
1646 |
|
---|
1647 | if(!ll_count(peer->gbox.cards))
|
---|
1648 | {
|
---|
1649 | cs_debug_mask(D_READER, "gbox: %s NO CARDS!", cli->reader->label);
|
---|
1650 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1651 | return -1;
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | if(!peer->online)
|
---|
1655 | {
|
---|
1656 | cs_debug_mask(D_READER, "gbox: peer is OFFLINE!");
|
---|
1657 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1658 | // gbox_send_hello(cli,0);
|
---|
1659 | return -1;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | if(er->gbox_ecm_ok == 2)
|
---|
1663 | {
|
---|
1664 | cs_debug_mask(D_READER, "gbox: %s replied to this ecm already", cli->reader->label);
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | if(er->gbox_ecm_id == peer->gbox.id)
|
---|
1668 | {
|
---|
1669 | cs_debug_mask(D_READER, "gbox: %s provided ecm", cli->reader->label);
|
---|
1670 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1671 | return 0;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | uint16_t ercaid = er->caid;
|
---|
1675 | uint32_t erprid = er->prid;
|
---|
1676 |
|
---|
1677 | if(cli->reader->gbox_maxecmsend == 0)
|
---|
1678 | {
|
---|
1679 | cli->reader->gbox_maxecmsend = DEFAULT_GBOX_MAX_ECM_SEND;
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | switch(ercaid >> 8)
|
---|
1683 | {
|
---|
1684 | //Viaccess
|
---|
1685 | case 0x05:
|
---|
1686 | ercaid = (ercaid & 0xFF00) | ((erprid >> 16) & 0xFF);
|
---|
1687 | erprid = erprid & 0xFFFF;
|
---|
1688 | break;
|
---|
1689 | //Cryptoworks
|
---|
1690 | case 0x0D:
|
---|
1691 | erprid = erprid << 8;
|
---|
1692 | break;
|
---|
1693 | //Nagra
|
---|
1694 | case 0x18:
|
---|
1695 | erprid = 0;
|
---|
1696 | break;
|
---|
1697 | }
|
---|
1698 |
|
---|
1699 | uchar send_buf_1[1024];
|
---|
1700 | int32_t len2;
|
---|
1701 |
|
---|
1702 | if(!er->ecmlen) { return 0; }
|
---|
1703 |
|
---|
1704 | len2 = er->ecmlen + 18;
|
---|
1705 | er->gbox_crc = gbox_get_ecmchecksum(er);
|
---|
1706 |
|
---|
1707 | memset(send_buf_1, 0, sizeof(send_buf_1));
|
---|
1708 |
|
---|
1709 | LL_ITER it = ll_iter_create(peer->gbox.cards);
|
---|
1710 | struct gbox_card *card;
|
---|
1711 |
|
---|
1712 | int32_t cont_send = 0;
|
---|
1713 | uint32_t cont_card_1 = 0;
|
---|
1714 |
|
---|
1715 | send_buf_1[0] = MSG_ECM >> 8;
|
---|
1716 | send_buf_1[1] = MSG_ECM & 0xff;
|
---|
1717 | memcpy(send_buf_1 + 2, peer->gbox.password, 4);
|
---|
1718 | memcpy(send_buf_1 + 6, local_gbox.password, 4);
|
---|
1719 |
|
---|
1720 | send_buf_1[10] = (er->pid >> 8) & 0xFF;
|
---|
1721 | send_buf_1[11] = er->pid & 0xFF;
|
---|
1722 |
|
---|
1723 | send_buf_1[12] = (er->srvid >> 8) & 0xFF;
|
---|
1724 | send_buf_1[13] = er->srvid & 0xFF;
|
---|
1725 | send_buf_1[14] = 0x00;
|
---|
1726 | send_buf_1[15] = 0x00;
|
---|
1727 |
|
---|
1728 | send_buf_1[16] = cont_card_1;
|
---|
1729 | send_buf_1[17] = 0x00;
|
---|
1730 |
|
---|
1731 | memcpy(send_buf_1 + 18, er->ecm, er->ecmlen);
|
---|
1732 |
|
---|
1733 | send_buf_1[len2] = (local_gbox.id >> 8) & 0xff;
|
---|
1734 | send_buf_1[len2 + 1] = local_gbox.id & 0xff;
|
---|
1735 | send_buf_1[len2 + 2] = LOCAL_GBOX_MINOR_VERSION;
|
---|
1736 | send_buf_1[len2 + 3] = 0x00;
|
---|
1737 | send_buf_1[len2 + 4] = LOCAL_GBOX_TYPE;
|
---|
1738 |
|
---|
1739 | send_buf_1[len2 + 5] = ercaid >> 8;
|
---|
1740 | send_buf_1[len2 + 6] = ercaid & 0xFF;
|
---|
1741 |
|
---|
1742 | send_buf_1[len2 + 7] = (erprid >> 8) & 0xFF;
|
---|
1743 | send_buf_1[len2 + 8] = erprid & 0xFF;
|
---|
1744 | send_buf_1[len2 + 9] = 0x00;
|
---|
1745 | cont_1 = len2 + 10;
|
---|
1746 |
|
---|
1747 | struct gbox_srvid *srvid1 = NULL;
|
---|
1748 | while((card = ll_iter_next(&it)))
|
---|
1749 | {
|
---|
1750 | if(card->caid == er->caid && card->provid == er->prid)
|
---|
1751 | {
|
---|
1752 | sid_verified = 0;
|
---|
1753 |
|
---|
1754 | LL_ITER it2 = ll_iter_create(card->goodsids);
|
---|
1755 | while((srvid1 = ll_iter_next(&it2)))
|
---|
1756 | {
|
---|
1757 | if(srvid1->provid_id == er->prid && srvid1->sid == er->srvid)
|
---|
1758 | {
|
---|
1759 | // time_since_lastcw = abs(srvid1->last_cw_received - time(NULL));
|
---|
1760 | // if (time_since_lastcw > ECM_BROADCAST_PAUSE || !cont_card_1)
|
---|
1761 | // {
|
---|
1762 | send_buf_1[cont_1] = card->peer_id >> 8;
|
---|
1763 | send_buf_1[cont_1 + 1] = card->peer_id;
|
---|
1764 | send_buf_1[cont_1 + 2] = card->slot;
|
---|
1765 | cont_1 = cont_1 + 3;
|
---|
1766 | cont_card_1++;
|
---|
1767 | cont_send++;
|
---|
1768 | sid_verified = 1;
|
---|
1769 | break;
|
---|
1770 | // }
|
---|
1771 | }
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 | if(cont_send == cli->reader->gbox_maxecmsend)
|
---|
1775 | { break; }
|
---|
1776 |
|
---|
1777 | if(sid_verified == 0)
|
---|
1778 | {
|
---|
1779 | LL_ITER itt = ll_iter_create(card->badsids);
|
---|
1780 | while((srvid1 = ll_iter_next(&itt)))
|
---|
1781 | {
|
---|
1782 | if(srvid1->provid_id == er->prid && srvid1->sid == er->srvid)
|
---|
1783 | {
|
---|
1784 | sid_verified = 1;
|
---|
1785 | break;
|
---|
1786 | }
|
---|
1787 | }
|
---|
1788 |
|
---|
1789 | if(sid_verified != 1)
|
---|
1790 | {
|
---|
1791 | send_buf_1[cont_1] = card->peer_id >> 8;
|
---|
1792 | send_buf_1[cont_1 + 1] = card->peer_id;
|
---|
1793 | send_buf_1[cont_1 + 2] = card->slot;
|
---|
1794 | cont_1 = cont_1 + 3;
|
---|
1795 | cont_card_1++;
|
---|
1796 | cont_send++;
|
---|
1797 |
|
---|
1798 | if(!cs_malloc(&srvid1, sizeof(struct gbox_srvid)))
|
---|
1799 | { return 0; }
|
---|
1800 |
|
---|
1801 | srvid1->sid = er->srvid;
|
---|
1802 | srvid1->provid_id = card->provid;
|
---|
1803 | ll_append(card->badsids, srvid1);
|
---|
1804 |
|
---|
1805 | if(cont_send == cli->reader->gbox_maxecmsend)
|
---|
1806 | { break; }
|
---|
1807 | }
|
---|
1808 | }
|
---|
1809 |
|
---|
1810 | if(cont_send == cli->reader->gbox_maxecmsend)
|
---|
1811 | { break; }
|
---|
1812 | }
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | if(!cont_card_1)
|
---|
1816 | {
|
---|
1817 | cs_debug_mask(D_READER, "GBOX: no valid card found for CAID: %04X PROVID: %04X", er->caid, er->prid);
|
---|
1818 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1819 | return -1;
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 | send_buf_1[16] = cont_card_1;
|
---|
1823 |
|
---|
1824 | //Hops
|
---|
1825 | send_buf_1[cont_1] = 0;
|
---|
1826 | cont_1++;
|
---|
1827 |
|
---|
1828 | memcpy(&send_buf_1[cont_1], local_gbox.checkcode, 7);
|
---|
1829 | cont_1 = cont_1 + 7;
|
---|
1830 | memcpy(&send_buf_1[cont_1], peer->gbox.checkcode, 7);
|
---|
1831 | cont_1 = cont_1 + 7;
|
---|
1832 |
|
---|
1833 | cs_debug_mask(D_READER, "gbox sending ecm for %04X:%06X:%04X to %d cards -> %s", er->caid, er->prid , er->srvid, cont_card_1, cli->reader->label);
|
---|
1834 | uint32_t i = 0;
|
---|
1835 | for (i = 0; i < cont_card_1; i++)
|
---|
1836 | { cs_debug_mask(D_READER, "gbox card %d: ID: %04X, Slot: %02X", i+1, (send_buf_1[len2+10+i*3] << 8) | send_buf_1[len2+11+i*3], send_buf_1[len2+12+i*3]); }
|
---|
1837 | er->gbox_ecm_ok = 1;
|
---|
1838 | gbox_send(cli, send_buf_1, cont_1);
|
---|
1839 | cli->pending++;
|
---|
1840 | cli->reader->last_s = time((time_t *) 0);
|
---|
1841 |
|
---|
1842 | return 0;
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 | static int32_t gbox_send_emm(EMM_PACKET *UNUSED(ep))
|
---|
1846 | {
|
---|
1847 | // emms not yet supported
|
---|
1848 |
|
---|
1849 | return 0;
|
---|
1850 | }
|
---|
1851 |
|
---|
1852 | //init my gbox with id, password and cards crc
|
---|
1853 | static void init_local_gbox(void)
|
---|
1854 | {
|
---|
1855 | remove(FILE_GBOX_VERSION);
|
---|
1856 | remove(FILE_SHARED_CARDS_INFO);
|
---|
1857 | remove(FILE_ATTACK_INFO);
|
---|
1858 | remove(FILE_GBOX_PEER_ONL);
|
---|
1859 | remove(FILE_STATS);
|
---|
1860 | local_gbox.id = 0;
|
---|
1861 | memset(&local_gbox.password[0], 0, 4);
|
---|
1862 | memset(&local_gbox.checkcode[0], 0, 7);
|
---|
1863 | local_gbox.minor_version = LOCAL_GBOX_MINOR_VERSION;
|
---|
1864 | local_gbox.type = LOCAL_GBOX_TYPE;
|
---|
1865 |
|
---|
1866 | if(!cfg.gbox_my_password || strlen(cfg.gbox_my_password) != 8) { return; }
|
---|
1867 |
|
---|
1868 | uint32_t key = a2i(cfg.gbox_my_password, 4);
|
---|
1869 | int32_t i;
|
---|
1870 | for(i = 3; i >= 0; i--)
|
---|
1871 | {
|
---|
1872 | local_gbox.password[3 - i] = (key >> (8 * i)) & 0xff;
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | cs_ddump_mask(D_READER, local_gbox.password, 4, " My password: %s:", cfg.gbox_my_password);
|
---|
1876 |
|
---|
1877 | local_gbox.id = gbox_convert_password_to_id(&local_gbox.password[0]);
|
---|
1878 | if (local_gbox.id == NO_GBOX_ID)
|
---|
1879 | {
|
---|
1880 | cs_log("gbox: invalid local gbox id: %04X", local_gbox.id);
|
---|
1881 | }
|
---|
1882 | last_stats_written = time(NULL);
|
---|
1883 | gbox_write_version();
|
---|
1884 |
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 | static void gbox_s_idle(struct s_client *cl)
|
---|
1888 | {
|
---|
1889 | uint32_t time_since_lastecm;
|
---|
1890 | struct s_client *proxy = get_gbox_proxy(cl->gbox_peer_id);
|
---|
1891 | struct gbox_peer *peer;
|
---|
1892 |
|
---|
1893 | time_since_lastecm = abs(cl->lastecm - time(NULL));
|
---|
1894 | if (time_since_lastecm > HELLO_KEEPALIVE_TIME && cl->gbox_peer_id != NO_GBOX_ID && proxy && proxy->gbox)
|
---|
1895 | {
|
---|
1896 | peer = proxy->gbox;
|
---|
1897 | cs_debug_mask(D_READER, "gbox time since last ecm in sec: %d => trigger keepalive hello",time_since_lastecm);
|
---|
1898 | if (!peer->online)
|
---|
1899 | { peer->hello_stat = GBOX_STAT_HELLOL; }
|
---|
1900 | else
|
---|
1901 | { peer->hello_stat = GBOX_STAT_HELLOS; }
|
---|
1902 | gbox_send_hello(proxy);
|
---|
1903 | peer->hello_stat = GBOX_STAT_HELLOR;
|
---|
1904 | }
|
---|
1905 | //prevent users from timing out
|
---|
1906 | cs_debug_mask(D_READER, "gbox client idle prevented: %s", username(cl));
|
---|
1907 | cl->last = time((time_t *)0);
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 | void module_gbox(struct s_module *ph)
|
---|
1911 | {
|
---|
1912 | init_local_gbox();
|
---|
1913 | ph->ptab.nports = 1;
|
---|
1914 | ph->ptab.ports[0].s_port = cfg.gbox_port;
|
---|
1915 |
|
---|
1916 | ph->desc = "gbox";
|
---|
1917 | ph->num = R_GBOX;
|
---|
1918 | ph->type = MOD_CONN_UDP;
|
---|
1919 | ph->large_ecm_support = 1;
|
---|
1920 | ph->listenertype = LIS_GBOX;
|
---|
1921 |
|
---|
1922 | ph->s_handler = gbox_server;
|
---|
1923 | ph->s_init = gbox_server_init;
|
---|
1924 |
|
---|
1925 | ph->send_dcw = gbox_send_dcw;
|
---|
1926 |
|
---|
1927 | ph->recv = gbox_recv;
|
---|
1928 | ph->c_init = gbox_client_init;
|
---|
1929 | ph->c_recv_chk = gbox_recv_chk;
|
---|
1930 | ph->c_send_ecm = gbox_send_ecm;
|
---|
1931 | ph->c_send_emm = gbox_send_emm;
|
---|
1932 |
|
---|
1933 | ph->s_idle = gbox_s_idle;
|
---|
1934 | }
|
---|
1935 | #endif
|
---|