1 | #include "globals.h"
|
---|
2 | #ifdef MODULE_GBOX
|
---|
3 | #include <pthread.h>
|
---|
4 | //#define _XOPEN_SOURCE 600
|
---|
5 | #include <semaphore.h>
|
---|
6 | #include <time.h>
|
---|
7 | #include <sys/time.h>
|
---|
8 |
|
---|
9 |
|
---|
10 | #include "module-datastruct-llist.h"
|
---|
11 | #include "algo/minilzo.h"
|
---|
12 |
|
---|
13 | enum {
|
---|
14 | MSG_ECM = 0x445c,
|
---|
15 | MSG_CW = 0x4844,
|
---|
16 | MSG_HELLO = 0xddab,
|
---|
17 | MSG_CHECKCODE = 0x41c0,
|
---|
18 | MSG_GOODBYE = 0x9091,
|
---|
19 | MSG_GSMS_ACK = 0x9098,
|
---|
20 | MSG_GSMS = 0xff0,
|
---|
21 | MSG_BOXINFO = 0xa0a1
|
---|
22 | };
|
---|
23 |
|
---|
24 | struct gbox_card {
|
---|
25 | uint16 peer_id;
|
---|
26 | uint16 caid;
|
---|
27 | uint32 provid;
|
---|
28 | int slot;
|
---|
29 | int dist;
|
---|
30 | int lvl;
|
---|
31 | };
|
---|
32 |
|
---|
33 | struct gbox_peer {
|
---|
34 | uint16 id;
|
---|
35 | uchar key[4];
|
---|
36 | uchar ver;
|
---|
37 | uchar type;
|
---|
38 | LLIST *cards;
|
---|
39 | uchar checkcode[7];
|
---|
40 | uchar *hostname;
|
---|
41 | int online;
|
---|
42 | int fail_count;
|
---|
43 | };
|
---|
44 |
|
---|
45 | struct gbox_data {
|
---|
46 | uint16 id;
|
---|
47 | uchar checkcode[7];
|
---|
48 | uchar key[4];
|
---|
49 | uchar ver;
|
---|
50 | uchar type;
|
---|
51 | int ecm_idx;
|
---|
52 | uchar cws[16];
|
---|
53 | struct gbox_peer peer;
|
---|
54 | pthread_mutex_t lock;
|
---|
55 | //char *hostname;
|
---|
56 | uchar buf[1024];
|
---|
57 | sem_t sem;
|
---|
58 | };
|
---|
59 |
|
---|
60 | static const uchar sbox[] = {
|
---|
61 | 0x25, 0x38, 0xd4, 0xcd, 0x17, 0x7a, 0x5e, 0x6c,
|
---|
62 | 0x52, 0x42, 0xfe, 0x68, 0xab, 0x3f, 0xf7, 0xbe,
|
---|
63 | 0x47, 0x57, 0x71, 0xb0, 0x23, 0xc1, 0x26, 0x6c,
|
---|
64 | 0x41, 0xce, 0x94, 0x37, 0x45, 0x04, 0xa2, 0xea,
|
---|
65 | 0x07, 0x58, 0x35, 0x55, 0x08, 0x2a, 0x0f, 0xe7,
|
---|
66 | 0xac, 0x76, 0xf0, 0xc1, 0xe6, 0x09, 0x10, 0xdd,
|
---|
67 | 0xc5, 0x8d, 0x2e, 0xd9, 0x03, 0x9c, 0x3d, 0x2c,
|
---|
68 | 0x4d, 0x41, 0x0c, 0x5e, 0xde, 0xe4, 0x90, 0xae
|
---|
69 | };
|
---|
70 |
|
---|
71 | static int gbox_decode_cmd(uchar *buf)
|
---|
72 | {
|
---|
73 | return buf[0] << 8 | buf[1];
|
---|
74 | }
|
---|
75 |
|
---|
76 | static void gbox_calc_checkcode(struct gbox_data *gbox)
|
---|
77 | {
|
---|
78 | memcpy(gbox->checkcode, "\x15\x30\x2\x4\x19\x19\x66", 7); /* no local cards */
|
---|
79 | // for all local cards do:
|
---|
80 | /*
|
---|
81 | gbox->checkcode[0] ^= caid << 8;
|
---|
82 | gbox->checkcode[1] ^= caid & 0xff;
|
---|
83 | gbox->checkcode[2] ^= provid << 8;
|
---|
84 | gbox->checkcode[3] ^= provid & 0xff;
|
---|
85 | gbox->checkcode[4] ^= slot; // reader number
|
---|
86 | gbox->checkcode[5] ^= gbox->id << 8;
|
---|
87 | gbox->checkcode[6] ^= gbox->id & 0xff;
|
---|
88 | */
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void gbox_encrypt_stage1(uchar *buf, int l, uchar *key)
|
---|
92 | {
|
---|
93 | int i;
|
---|
94 |
|
---|
95 | for (i = 31; i >= 0; i--) {
|
---|
96 | uchar tmp;
|
---|
97 |
|
---|
98 | tmp = key[3];
|
---|
99 | int j;
|
---|
100 | for (j = 3; j > 0; j--)
|
---|
101 | key[j] = (key[j - 1] << 7) + (key[j] >> 1);
|
---|
102 | key[0] = (tmp << 7) + (key[0] >> 1);
|
---|
103 |
|
---|
104 | buf[i + 1 - ((i + 1) & 0xf8)] += sbox[((key[i + 1 - ((i + 1) & 0xfc)] ^ buf[i - (i & 0xf8)]) >> 2) & 0x3f] * 2;
|
---|
105 | buf[i + 1 - ((i + 1) & 0xf8)] ^= sbox[(buf[i - (i & 0xf8)] - key[i + 1 - ((i + 1) & 0xfc)]) & 0x3f];
|
---|
106 | buf[i + 1 - ((i + 1) & 0xf8)] += key[i - (i & 0xfc)];
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void gbox_encrypt_stage2(uchar *buf, int l, uchar *key)
|
---|
111 | {
|
---|
112 | int i, j;
|
---|
113 |
|
---|
114 | for (i = 0; i < 4; i++)
|
---|
115 | for (j = 7; j >= 0; j--) {
|
---|
116 | uchar tmp;
|
---|
117 |
|
---|
118 | tmp = key[3];
|
---|
119 | int k;
|
---|
120 | for (k = 3; k > 0; k--)
|
---|
121 | key[k] = (key[k - 1] << 7) + (key[k] >> 1);
|
---|
122 | key[0] = (tmp << 7) + (key[0] >> 1);
|
---|
123 |
|
---|
124 | buf[(j + 1) & 7] -= sbox[(buf[j] >> 2) & 0x3f];
|
---|
125 | buf[(j + 1) & 7] ^= sbox[(buf[j] - key[(j + 1) & 3]) & 0x3f];
|
---|
126 | buf[(j + 1) & 7] -= key[j & 3];
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | static void gbox_decrypt_stage1(uchar *buf, int l, uchar *key)
|
---|
131 | {
|
---|
132 | int i;
|
---|
133 |
|
---|
134 | for (i = 0; i < 32; i++) {
|
---|
135 | uchar tmp;
|
---|
136 |
|
---|
137 | buf[i + 1 - ((i + 1) & 0xf8)] -= key[i - (i & 0xfc)];
|
---|
138 | buf[i + 1 - ((i + 1) & 0xf8)] ^= sbox[(buf[i - (i & 0xf8)] - key[i + 1 - ((i + 1) & 0xfc)]) & 0x3f];
|
---|
139 | buf[i + 1 - ((i + 1) & 0xf8)] -= sbox[((key[i + 1 - ((i + 1) & 0xfc)] ^ buf[i - (i & 0xf8)]) >> 2) & 0x3f] * 2;
|
---|
140 |
|
---|
141 | tmp = key[0];
|
---|
142 | int j;
|
---|
143 | for (j = 0; j < 3; j++)
|
---|
144 | key[j] = ((key[j + 1] & 0x80) >> 7) + (key[j] * 2);
|
---|
145 | key[3] = ((tmp & 0x80) >> 7) + (key[3] * 2);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | static void gbox_decrypt_stage2(uchar *buf, int l, uchar *key)
|
---|
150 | {
|
---|
151 | int i, j;
|
---|
152 |
|
---|
153 | for (i = 3; i >= 0; i--)
|
---|
154 | for (j = 0; j < 8; j++) {
|
---|
155 | uchar tmp;
|
---|
156 |
|
---|
157 | buf[(j + 1) & 7] += key[j & 3];
|
---|
158 | buf[(j + 1) & 7] ^= sbox[(buf[j] - key[(j + 1) & 3]) & 0x3f];
|
---|
159 | buf[(j + 1) & 7] += sbox[(buf[j] >> 2) & 0x3f];
|
---|
160 |
|
---|
161 | tmp = key[0];
|
---|
162 | int k;
|
---|
163 | for (k = 0; k < 3; k++)
|
---|
164 | key[k] = ((key[k + 1] & 0x80) >> 7) + (key[k] * 2);
|
---|
165 | key[3] = ((tmp & 0x80) >> 7) + (key[3] * 2);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void gbox_encrypt(uchar *buf, int l, uchar *key)
|
---|
170 | {
|
---|
171 | int i;
|
---|
172 | uchar tmp_key[4];
|
---|
173 |
|
---|
174 | memcpy(tmp_key, key, 4);
|
---|
175 |
|
---|
176 | gbox_encrypt_stage1(buf, l, tmp_key);
|
---|
177 |
|
---|
178 | for (i = 0; i < l - 2; i++)
|
---|
179 | buf[i] ^= buf[i + 1];
|
---|
180 |
|
---|
181 | gbox_encrypt_stage2(buf, l, tmp_key);
|
---|
182 | gbox_decrypt_stage2(buf + l - 9, 9, tmp_key);
|
---|
183 | }
|
---|
184 |
|
---|
185 | static void gbox_decrypt(uchar *buf, int l, uchar *key)
|
---|
186 | {
|
---|
187 | uchar tmp_key[4];
|
---|
188 |
|
---|
189 | memcpy(tmp_key, key, 4);
|
---|
190 |
|
---|
191 | gbox_encrypt_stage2(buf + l - 9, 9, tmp_key);
|
---|
192 | gbox_decrypt_stage2(buf, l, tmp_key);
|
---|
193 |
|
---|
194 | int i;
|
---|
195 | for (i = l - 2; i >= 0; i--) {
|
---|
196 | buf[i] ^= buf[i + 1];
|
---|
197 | }
|
---|
198 |
|
---|
199 | gbox_decrypt_stage1(buf, l, tmp_key);
|
---|
200 | }
|
---|
201 |
|
---|
202 | static void gbox_compress(struct gbox_data *gbox, uchar *buf, int unpacked_len, int *packed_len)
|
---|
203 | {
|
---|
204 | unsigned char *tmp = malloc(0x40000);
|
---|
205 | unsigned char *tmp2 = malloc(0x40000);
|
---|
206 |
|
---|
207 | unpacked_len -= 12;
|
---|
208 | memcpy(tmp2, buf + 12, unpacked_len);
|
---|
209 |
|
---|
210 | lzo_init();
|
---|
211 |
|
---|
212 | lzo_voidp wrkmem = malloc(unpacked_len * 0x1000);
|
---|
213 | cs_debug_mask(D_READER, "gbox: wrkmem = %p", wrkmem);
|
---|
214 | lzo_uint pl = 0;
|
---|
215 | if (lzo1x_1_compress(tmp2, unpacked_len, tmp, &pl, wrkmem) != LZO_E_OK)
|
---|
216 | cs_log("gbox: compression failed!");
|
---|
217 |
|
---|
218 | memcpy(buf + 12, tmp, pl);
|
---|
219 | pl += 12;
|
---|
220 |
|
---|
221 | free(tmp);
|
---|
222 | free(tmp2);
|
---|
223 | free(wrkmem);
|
---|
224 |
|
---|
225 | *packed_len = pl;
|
---|
226 | }
|
---|
227 |
|
---|
228 | static void gbox_decompress(struct gbox_data *gbox, uchar *buf, int *unpacked_len)
|
---|
229 | {
|
---|
230 | uchar tmp[1024];
|
---|
231 |
|
---|
232 | int len = buf[12] - 13;
|
---|
233 |
|
---|
234 | lzo_init();
|
---|
235 | if (lzo1x_decompress(buf + 12, len, tmp, (lzo_uint *)unpacked_len, NULL) != LZO_E_OK)
|
---|
236 | cs_log("gbox: decompression failed!");
|
---|
237 |
|
---|
238 | memcpy(buf + 12, tmp, *unpacked_len);
|
---|
239 | *unpacked_len += 12;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /*
|
---|
243 | static void gbox_handle_gsms(ushort peerid, char *gsms)
|
---|
244 | {
|
---|
245 | cs_log("gbox: gsms received from peer %04x: %s", peerid, gsms);
|
---|
246 |
|
---|
247 | if (strnlen(cfg.gbox_gsms_path, sizeof(cfg.gbox_gsms_path))) {
|
---|
248 | FILE *f = fopen(cfg.gbox_gsms_path, "a");
|
---|
249 | if (f) {
|
---|
250 | fprintf(f, "FROM %04X: %s\n", peerid, gsms);
|
---|
251 | fclose(f);
|
---|
252 | }
|
---|
253 | else
|
---|
254 | cs_log("gbox: error writing to file! (path=%s)", cfg.gbox_gsms_path);
|
---|
255 | }
|
---|
256 | }
|
---|
257 | */
|
---|
258 |
|
---|
259 | static void gbox_wait_for_response(struct s_client *cli)
|
---|
260 | {
|
---|
261 | printf("gbox: enter gbox_wait_for_response()\n");
|
---|
262 | //cs_debug_mask(D_READER, "gbox: enter gbox_wait_for_response()");
|
---|
263 | struct gbox_data *gbox = cli->gbox;
|
---|
264 | struct timespec ts;
|
---|
265 | struct timeval tv;
|
---|
266 | gettimeofday(&tv, NULL);
|
---|
267 | ts.tv_sec = tv.tv_sec;
|
---|
268 | ts.tv_nsec = tv.tv_usec * 1000;
|
---|
269 | //clock_gettime(CLOCK_REALTIME, &ts);
|
---|
270 | ts.tv_sec += 5;
|
---|
271 |
|
---|
272 | //sem_wait(&gbox->sem);
|
---|
273 | if (sem_timedwait(&gbox->sem, &ts) == -1) {
|
---|
274 | if (errno == ETIMEDOUT) {
|
---|
275 | gbox->peer.fail_count++;
|
---|
276 | printf("gbox: sem wait timed-out, fail_count=%d\n", gbox->peer.fail_count);
|
---|
277 | if (gbox->peer.fail_count >= 5) {
|
---|
278 | gbox->peer.online = 0;
|
---|
279 | printf("gbox: fail_count >=5, peer is offline\n");
|
---|
280 | }
|
---|
281 | //cs_debug_mask(D_READER, "gbox: sem wait timed-out, fail_count=%d\n", gbox->peer.fail_count);
|
---|
282 | }
|
---|
283 | } else {
|
---|
284 | gbox->peer.fail_count = 0;
|
---|
285 | printf("gbox: sem posted, peer is online\n");
|
---|
286 | }
|
---|
287 | //cs_debug_mask(D_READER, "gbox: sem posted, peer is online");
|
---|
288 |
|
---|
289 | //cs_debug_mask(D_READER, "gbox: exit gbox_wait_for_response()");
|
---|
290 | printf("gbox: exit gbox_wait_for_response()\n");
|
---|
291 | }
|
---|
292 |
|
---|
293 | static void gbox_send(struct s_client *cli, uchar *buf, int l)
|
---|
294 | {
|
---|
295 | struct gbox_data *gbox = cli->gbox;
|
---|
296 |
|
---|
297 | cs_ddump_mask(D_READER, buf, l, "gbox: decrypted data sent (%d bytes):", l);
|
---|
298 | cs_ddump_mask(D_READER, gbox->key, 4, "gbox: key before encrypt:");
|
---|
299 |
|
---|
300 | gbox_encrypt(buf, l, gbox->peer.key);
|
---|
301 | sendto(cli->udp_fd, buf, l, 0, (struct sockaddr *)&cli->udp_sa, sizeof(cli->udp_sa));
|
---|
302 |
|
---|
303 | cs_ddump_mask(D_READER, gbox->key, 4, "gbox: key after encrypt:");
|
---|
304 | cs_ddump_mask(D_READER, buf, l, "gbox: encrypted data sent (%d bytes):", l);
|
---|
305 |
|
---|
306 | pthread_t t;
|
---|
307 | pthread_create(&t, NULL, (void *)gbox_wait_for_response, cli);
|
---|
308 | }
|
---|
309 |
|
---|
310 | static void gbox_send_boxinfo(struct s_client *cli)
|
---|
311 | {
|
---|
312 | struct gbox_data *gbox = cli->gbox;
|
---|
313 |
|
---|
314 | int len;
|
---|
315 | uchar buf[4096];
|
---|
316 |
|
---|
317 | int hostname_len = strnlen(cfg.gbox_hostname, sizeof(cfg.gbox_hostname) - 1);
|
---|
318 |
|
---|
319 | buf[0] = 0xA0;
|
---|
320 | buf[1] = 0xA1;
|
---|
321 | memcpy(buf + 2, gbox->peer.key, 4);
|
---|
322 | memcpy(buf + 6, gbox->key, 4);
|
---|
323 | buf[10] = gbox->peer.ver;
|
---|
324 | buf[11] = 0x10;
|
---|
325 | memcpy(buf + 12, cfg.gbox_hostname, hostname_len);
|
---|
326 |
|
---|
327 | len = 12 + hostname_len;
|
---|
328 |
|
---|
329 | gbox_send(cli, buf, len);
|
---|
330 | }
|
---|
331 |
|
---|
332 | static void gbox_send_hello(struct s_client *cli)
|
---|
333 | {
|
---|
334 | struct gbox_data *gbox = cli->gbox;
|
---|
335 |
|
---|
336 | int len;
|
---|
337 | uchar buf[4096];
|
---|
338 |
|
---|
339 | int hostname_len = strnlen(cfg.gbox_hostname, sizeof(cfg.gbox_hostname) - 1);
|
---|
340 |
|
---|
341 | len = 22 + hostname_len;
|
---|
342 |
|
---|
343 | memset(buf, 0, sizeof(buf));
|
---|
344 |
|
---|
345 | gbox_calc_checkcode(gbox);
|
---|
346 |
|
---|
347 | buf[0] = MSG_HELLO >> 8;
|
---|
348 | buf[1] = MSG_HELLO & 0xff;
|
---|
349 | memcpy(buf + 2, gbox->peer.key, 4);
|
---|
350 | memcpy(buf + 6, gbox->key, 4);
|
---|
351 | buf[11] = 0x80; // todo this needs adjusting if local card support is added
|
---|
352 |
|
---|
353 | memcpy(buf + 12, gbox->checkcode, 7);
|
---|
354 | buf[19] = 0x73;
|
---|
355 | buf[20] = 0x10;
|
---|
356 | memcpy(buf + 21, cfg.gbox_hostname, hostname_len);
|
---|
357 | buf[21 + hostname_len] = hostname_len;
|
---|
358 |
|
---|
359 | cs_ddump_mask(D_READER, buf, len, "send hello (len=%d):", len);
|
---|
360 |
|
---|
361 | gbox_compress(gbox, buf, len, &len); // TODO: remove _re
|
---|
362 |
|
---|
363 | cs_ddump_mask(D_READER, buf, len, "send hello, compressed (len=%d):", len);
|
---|
364 |
|
---|
365 | gbox_send(cli, buf, len);
|
---|
366 | }
|
---|
367 |
|
---|
368 | static int gbox_recv(struct s_client *cli, uchar *b, int l)
|
---|
369 | {
|
---|
370 | struct gbox_data *gbox = cli->gbox;
|
---|
371 |
|
---|
372 | if (!gbox)
|
---|
373 | return -1;
|
---|
374 |
|
---|
375 | uchar *data = gbox->buf;
|
---|
376 | int n;
|
---|
377 |
|
---|
378 | sem_post(&gbox->sem);
|
---|
379 | gbox->peer.online = 1;
|
---|
380 |
|
---|
381 | pthread_mutex_lock(&gbox->lock);
|
---|
382 |
|
---|
383 | unsigned int r_addr_len = 0;
|
---|
384 | struct sockaddr_in r_addr;
|
---|
385 | if ((n = recvfrom(cli->udp_fd, data, sizeof(gbox->buf), 0, (struct sockaddr *)&r_addr, &r_addr_len)) < 8) {
|
---|
386 | cs_log("gbox: invalid recvfrom!!!");
|
---|
387 | pthread_mutex_unlock(&gbox->lock);
|
---|
388 | return -1;
|
---|
389 | }
|
---|
390 |
|
---|
391 | cs_ddump_mask(D_READER, data, n, "gbox: encrypted data recvd (%d bytes):", n);
|
---|
392 | cs_ddump_mask(D_READER, gbox->key, 4, "gbox: key before decrypt:");
|
---|
393 |
|
---|
394 | gbox_decrypt(data, n, gbox->key);
|
---|
395 |
|
---|
396 | cs_ddump_mask(D_READER, gbox->key, 4, "gbox: key after decrypt:");
|
---|
397 | cs_ddump_mask(D_READER, data, n, "gbox: decrypted data recvd (%d bytes):", n);
|
---|
398 |
|
---|
399 | memcpy(b, data, l);
|
---|
400 |
|
---|
401 | if (!memcmp(data + 2, gbox->key, 4)) {
|
---|
402 | if (memcmp(data + 6, gbox->peer.key, 4) && gbox_decode_cmd(data) != MSG_CW) {
|
---|
403 | cs_log("gbox: invalid password!");
|
---|
404 |
|
---|
405 | pthread_mutex_unlock(&gbox->lock);
|
---|
406 | return -1;
|
---|
407 | }
|
---|
408 | } else {
|
---|
409 | cs_log("gbox: invalid password!");
|
---|
410 | return -1;
|
---|
411 | }
|
---|
412 |
|
---|
413 | switch (gbox_decode_cmd(data)) {
|
---|
414 | case MSG_HELLO:
|
---|
415 | {
|
---|
416 | static int exp_seq = 0;
|
---|
417 |
|
---|
418 | int payload_len = n;
|
---|
419 |
|
---|
420 | gbox_decompress(gbox, data, &payload_len);
|
---|
421 | cs_ddump_mask(D_READER, data, payload_len, "gbox: decompressed data (%d bytes):", payload_len);
|
---|
422 |
|
---|
423 | int seqno = data[11] & 0x7f;
|
---|
424 | int final = data[11] & 0x80;
|
---|
425 |
|
---|
426 | int ncards_in_msg = 0;
|
---|
427 |
|
---|
428 | if (seqno != exp_seq) return -1;
|
---|
429 |
|
---|
430 | if (seqno == 0) {
|
---|
431 | exp_seq++;
|
---|
432 |
|
---|
433 | if (gbox->peer.cards) ll_destroy_data(gbox->peer.cards);
|
---|
434 | gbox->peer.cards = ll_create();
|
---|
435 |
|
---|
436 | int checkcode_len = 7;
|
---|
437 | int hostname_len = data[payload_len - 1];
|
---|
438 | int footer_len = hostname_len + 2;
|
---|
439 |
|
---|
440 | // add cards to card list
|
---|
441 | uchar *ptr = data + 12;
|
---|
442 | while (ptr < data + payload_len - footer_len - checkcode_len - 1) {
|
---|
443 | uint16 caid = ptr[0] << 8 | ptr[1];
|
---|
444 | uint32 provid = ptr[2] << 8 | ptr[3];
|
---|
445 | int ncards = ptr[4];
|
---|
446 |
|
---|
447 | ptr += 5;
|
---|
448 |
|
---|
449 | int i;
|
---|
450 | for (i = 0; i < ncards; i++) {
|
---|
451 | struct gbox_card *card = malloc(sizeof(struct gbox_card));
|
---|
452 |
|
---|
453 | card->caid = caid;
|
---|
454 | card->provid = provid;
|
---|
455 | card->slot = ptr[0];
|
---|
456 | card->dist = ptr[1] & 0xf;
|
---|
457 | card->lvl = ((ptr[1] & 0xf0) >> 4) - 1;
|
---|
458 | card->peer_id = ptr[2] << 8 | ptr[3];
|
---|
459 |
|
---|
460 | ptr += 4;
|
---|
461 |
|
---|
462 | ll_append(gbox->peer.cards, card);
|
---|
463 |
|
---|
464 | ncards_in_msg++;
|
---|
465 |
|
---|
466 | cs_debug_mask(D_READER, " card: caid=%04x, provid=%04x, slot=%d, level=%d, dist=%d, peer=%04x"
|
---|
467 | , card->caid, card->provid, card->slot, card->lvl, card->dist, card->peer_id);
|
---|
468 |
|
---|
469 | cli->reader->tcp_connected = 2; //we have card
|
---|
470 | cli->reader->card_status = CARD_INSERTED;
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | NULLFREE(gbox->peer.hostname);
|
---|
475 | gbox->peer.hostname = malloc(hostname_len + 1);
|
---|
476 | memcpy(gbox->peer.hostname, data + payload_len - 1 - hostname_len, hostname_len);
|
---|
477 | gbox->peer.hostname[hostname_len] = '\0';
|
---|
478 |
|
---|
479 | memcpy(gbox->peer.checkcode, data + payload_len - footer_len - checkcode_len - 1, checkcode_len);
|
---|
480 | gbox->peer.ver = data[payload_len - footer_len - 1];
|
---|
481 | gbox->peer.type = data[payload_len - footer_len];
|
---|
482 | } else {
|
---|
483 | // add cards to card list
|
---|
484 | uchar *ptr = data + 12;
|
---|
485 | while (ptr < data + payload_len - 1) {
|
---|
486 | uint16 caid = ptr[0] << 8 | ptr[1];
|
---|
487 | uint32 provid = ptr[2] << 8 | ptr[3];
|
---|
488 | int ncards = ptr[4];
|
---|
489 |
|
---|
490 | ptr += 5;
|
---|
491 |
|
---|
492 | int i;
|
---|
493 | for (i = 0; i < ncards; i++) {
|
---|
494 | struct gbox_card *card = malloc(sizeof(struct gbox_card));
|
---|
495 |
|
---|
496 | card->caid = caid;
|
---|
497 | card->provid = provid;
|
---|
498 | card->slot = ptr[0];
|
---|
499 | card->dist = ptr[1] & 0xf;
|
---|
500 | card->lvl = ptr[1] >> 4;
|
---|
501 | card->peer_id = ptr[2] << 8 | ptr[3];
|
---|
502 |
|
---|
503 | ptr += 4;
|
---|
504 |
|
---|
505 | ll_append(gbox->peer.cards, card);
|
---|
506 |
|
---|
507 | ncards_in_msg++;
|
---|
508 |
|
---|
509 | cs_debug_mask(D_READER, " card: caid=%04x, provid=%04x, slot=%d, level=%d, dist=%d, peer=%04x"
|
---|
510 | , card->caid, card->provid, card->slot, card->lvl, card->dist, card->peer_id);
|
---|
511 | }
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | if (final) exp_seq = 0;
|
---|
516 |
|
---|
517 | cs_log("gbox: received hello %d%s, %d providers from %s, version=2.%02X, checkcode=%s",
|
---|
518 | seqno, final ? " (final)" : "", ncards_in_msg, gbox->peer.hostname, gbox->peer.ver, cs_hexdump(0, gbox->peer.checkcode, 7));
|
---|
519 |
|
---|
520 | if (!ll_count(gbox->peer.cards))
|
---|
521 | cli->reader->tcp_connected = 1;
|
---|
522 |
|
---|
523 | gbox_send_hello(cli);
|
---|
524 | gbox_send_boxinfo(cli);
|
---|
525 | }
|
---|
526 | break;
|
---|
527 | case MSG_CW:
|
---|
528 | memcpy(gbox->cws, data + 14, 16);
|
---|
529 | /*
|
---|
530 | cs_log("gbox: received cws=%s, peer=%04x, ecm_pid=%d, sid=%d",
|
---|
531 | data[10] << 8 | data[11], data[6] << 8 | data[7], data[8] << 8 | data[9], cs_hexdump(0, gbox->cws, 16));*/
|
---|
532 | break;
|
---|
533 | case MSG_CHECKCODE:
|
---|
534 | memcpy(gbox->peer.checkcode, data + 10, 7);
|
---|
535 | cs_debug_mask(D_READER, "gbox: received checkcode=%s", cs_hexdump(0, gbox->peer.checkcode, 7));
|
---|
536 | break;
|
---|
537 | /*case MSG_GSMS:
|
---|
538 | //gbox_handle_gsms(peerid, gsms);
|
---|
539 | break;
|
---|
540 | */
|
---|
541 | default:
|
---|
542 | cs_ddump_mask(D_READER, data, n, "gbox: unknown data received (%d bytes):", n);
|
---|
543 | }
|
---|
544 | pthread_mutex_unlock(&gbox->lock);
|
---|
545 |
|
---|
546 | return 0;
|
---|
547 | }
|
---|
548 |
|
---|
549 | static void gbox_send_dcw(struct s_client *cli, ECM_REQUEST *er)
|
---|
550 | {
|
---|
551 | // server not yet supported
|
---|
552 | }
|
---|
553 |
|
---|
554 | static int gbox_client_init(struct s_client *cli)
|
---|
555 | {
|
---|
556 | if (!strnlen(cfg.gbox_hostname, sizeof(cfg.gbox_hostname) - 1)) {
|
---|
557 | cs_log("gbox: error, no hostname configured in oscam.conf!");
|
---|
558 | return -1;
|
---|
559 | }
|
---|
560 |
|
---|
561 | if (strnlen(cfg.gbox_key, sizeof(cfg.gbox_key) - 1) != 8) {
|
---|
562 | cs_log("gbox: error, no/invalid password configured in oscam.conf!");
|
---|
563 | return -1;
|
---|
564 | }
|
---|
565 |
|
---|
566 | if (cfg.gbox_port < 1) {
|
---|
567 | cs_log("gbox: no/invalid port configured in oscam.conf!");
|
---|
568 | return -1;
|
---|
569 | }
|
---|
570 |
|
---|
571 | cli->gbox = malloc(sizeof(struct gbox_data));
|
---|
572 |
|
---|
573 | struct gbox_data *gbox = cli->gbox;
|
---|
574 | struct s_reader *rdr = cli->reader;
|
---|
575 |
|
---|
576 | sem_init(&gbox->sem, 0 , 1);
|
---|
577 |
|
---|
578 | rdr->card_status = CARD_FAILURE;
|
---|
579 | rdr->tcp_connected = 0;
|
---|
580 |
|
---|
581 | memset(gbox, 0, sizeof(struct gbox_data));
|
---|
582 | memset(&gbox->peer, 0, sizeof(struct gbox_peer));
|
---|
583 |
|
---|
584 | ulong r_pwd = a2i(rdr->r_pwd, 4);
|
---|
585 | ulong key = a2i(cfg.gbox_key, 4);
|
---|
586 | int i;
|
---|
587 | for (i = 3; i >= 0; i--) {
|
---|
588 | gbox->peer.key[3 - i] = (r_pwd >> (8 * i)) & 0xff;
|
---|
589 | gbox->key[3 - i] = (key >> (8 * i)) & 0xff;
|
---|
590 | }
|
---|
591 |
|
---|
592 | cs_ddump_mask(D_READER, gbox->peer.key, 4, "r_pwd: %s:", rdr->r_pwd);
|
---|
593 | cs_ddump_mask(D_READER, gbox->key, 4, "cfg.gbox_key: %s:", cfg.gbox_key);
|
---|
594 |
|
---|
595 | gbox->peer.id = (gbox->peer.key[0] ^ gbox->peer.key[2]) << 8 | (gbox->peer.key[1] ^ gbox->peer.key[3]);
|
---|
596 | gbox->id = (gbox->key[0] ^ gbox->key[2]) << 8 | (gbox->key[1] ^ gbox->key[3]);
|
---|
597 | gbox->ver = 0x30;
|
---|
598 | gbox->type = 0x32;
|
---|
599 |
|
---|
600 | struct sockaddr_in loc_sa;
|
---|
601 | struct protoent *ptrp;
|
---|
602 | int p_proto;
|
---|
603 |
|
---|
604 | cli->pfd=0;
|
---|
605 |
|
---|
606 | cli->is_udp = 1;
|
---|
607 |
|
---|
608 | if ((ptrp=getprotobyname("udp")))
|
---|
609 | p_proto=ptrp->p_proto;
|
---|
610 | else
|
---|
611 | p_proto=(cli->is_udp) ? 17 : 6;
|
---|
612 |
|
---|
613 | cli->ip=0;
|
---|
614 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
615 | loc_sa.sin_family = AF_INET;
|
---|
616 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
617 | loc_sa.sin_port = htons(cfg.gbox_port);
|
---|
618 |
|
---|
619 | if ((cli->udp_fd=socket(PF_INET, SOCK_DGRAM, p_proto))<0)
|
---|
620 | {
|
---|
621 | cs_log("socket creation failed (errno=%d)", errno);
|
---|
622 | cs_exit(1);
|
---|
623 | }
|
---|
624 |
|
---|
625 | int opt = 1;
|
---|
626 | setsockopt(cli->udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
|
---|
627 |
|
---|
628 | #ifdef SO_PRIORITY
|
---|
629 | if (cfg.netprio)
|
---|
630 | setsockopt(cli->udp_fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg.netprio, sizeof(ulong));
|
---|
631 | #endif
|
---|
632 |
|
---|
633 | if (cfg.gbox_port>0)
|
---|
634 | {
|
---|
635 | if (bind(cli->udp_fd, (struct sockaddr *)&loc_sa, sizeof (loc_sa))<0)
|
---|
636 | {
|
---|
637 | cs_log("bind failed (port=%d, errno=%d)", rdr->l_port, errno);
|
---|
638 | close(cli->udp_fd);
|
---|
639 | return 1;
|
---|
640 | }
|
---|
641 | }
|
---|
642 |
|
---|
643 | memset((char *)&cli->udp_sa, 0, sizeof(cli->udp_sa));
|
---|
644 |
|
---|
645 | struct hostent *hp;
|
---|
646 | hp = gethostbyname(rdr->device);
|
---|
647 | memcpy((char *)&cli->udp_sa.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
648 |
|
---|
649 | cli->udp_sa.sin_family=AF_INET;
|
---|
650 | cli->udp_sa.sin_port=htons((u_short)rdr->r_port);
|
---|
651 |
|
---|
652 | cs_log("proxy %s:%d (fd=%d, peer id=%04x, my id=%04x, my hostname=%s, listen port=%d)",
|
---|
653 | rdr->device, rdr->r_port, cli->udp_fd, gbox->peer.id, gbox->id, cfg.gbox_hostname, cfg.gbox_port);
|
---|
654 |
|
---|
655 | cli->pfd=cli->udp_fd;
|
---|
656 |
|
---|
657 | pthread_mutex_init(&gbox->lock, NULL);
|
---|
658 |
|
---|
659 | gbox_send_hello(cli);
|
---|
660 |
|
---|
661 | return 0;
|
---|
662 | }
|
---|
663 |
|
---|
664 | static int gbox_recv_chk(struct s_client *cli, uchar *dcw, int *rc, uchar *buf, int UNUSED(n))
|
---|
665 | {
|
---|
666 | struct gbox_data *gbox = cli->gbox;
|
---|
667 |
|
---|
668 | if (gbox_decode_cmd(buf) == MSG_CW) {
|
---|
669 | *rc = 1;
|
---|
670 |
|
---|
671 | memcpy(dcw, gbox->cws, 16);
|
---|
672 |
|
---|
673 | return gbox->ecm_idx;
|
---|
674 | }
|
---|
675 |
|
---|
676 | return -1;
|
---|
677 | }
|
---|
678 |
|
---|
679 | static int gbox_send_ecm(struct s_client *cli, ECM_REQUEST *er, uchar *buf)
|
---|
680 | {
|
---|
681 | struct gbox_data *gbox = cli->gbox;
|
---|
682 |
|
---|
683 | if (!gbox || !cli->reader->tcp_connected) {
|
---|
684 | er->rc = E_RDR_NOTFOUND;
|
---|
685 | er->rcEx = 0x27;
|
---|
686 | cs_debug_mask(D_READER, "gbox: %s server not init!", cli->reader->label);
|
---|
687 | write_ecm_answer(cli->reader, er);
|
---|
688 |
|
---|
689 | return 0;
|
---|
690 | }
|
---|
691 |
|
---|
692 | if (!ll_count(gbox->peer.cards)) {
|
---|
693 | er->rc = E_RDR_NOTFOUND;
|
---|
694 | er->rcEx = 0x27;
|
---|
695 | cs_debug_mask(D_READER, "gbox: %s NO CARDS!", cli->reader->label);
|
---|
696 | write_ecm_answer(cli->reader, er);
|
---|
697 | return 0;
|
---|
698 | }
|
---|
699 |
|
---|
700 | if (!gbox->peer.online) {
|
---|
701 | er->rc = E_RDR_NOTFOUND;
|
---|
702 | er->rcEx = 0x27;
|
---|
703 | cs_debug_mask(D_READER, "gbox: peer is OFFLINE!");
|
---|
704 | write_ecm_answer(cli->reader, er);
|
---|
705 | return 0;
|
---|
706 | }
|
---|
707 |
|
---|
708 | uchar send_buf[0x1024], *ptr;
|
---|
709 | int len;
|
---|
710 |
|
---|
711 | if (!er->l) return -1;
|
---|
712 |
|
---|
713 | len = er->l + 18;
|
---|
714 |
|
---|
715 | gbox->ecm_idx = er->idx;
|
---|
716 |
|
---|
717 | memset(send_buf, 0, sizeof(send_buf));
|
---|
718 |
|
---|
719 | send_buf[0] = MSG_ECM >> 8;
|
---|
720 | send_buf[1] = MSG_ECM & 0xff;
|
---|
721 | memcpy(send_buf + 2, gbox->peer.key, 4);
|
---|
722 | memcpy(send_buf + 6, gbox->key, 4);
|
---|
723 | send_buf[10] = er->pid >> 8;
|
---|
724 | send_buf[11] = er->pid;
|
---|
725 | send_buf[12] = er->srvid >> 8;
|
---|
726 | send_buf[13] = er->srvid;
|
---|
727 | /* send_buf[14] = er->prid >> 16;
|
---|
728 | send_buf[15] = er->prid >> 8;
|
---|
729 | send_buf[17] = er->prid;*/
|
---|
730 | memcpy(send_buf + 18, er->ecm, er->l);
|
---|
731 | ptr = send_buf + len;
|
---|
732 | *(ptr) = gbox->id >> 8;
|
---|
733 | *(++ptr) = gbox->id;
|
---|
734 | *(++ptr) = gbox->ver;
|
---|
735 | ptr++;
|
---|
736 | *(++ptr) = gbox->type;
|
---|
737 | *(++ptr) = er->caid >> 8;
|
---|
738 | *(++ptr) = er->caid;
|
---|
739 | ptr += 3;
|
---|
740 |
|
---|
741 | int found = 0;
|
---|
742 | LL_ITER *it = ll_iter_create(gbox->peer.cards);
|
---|
743 | struct gbox_card *card;
|
---|
744 | while ((card = ll_iter_next(it))) {
|
---|
745 | if (card->caid == er->caid && card->provid == er->prid) {
|
---|
746 | *(++ptr) = card->peer_id >> 8;
|
---|
747 | *(++ptr) = card->peer_id;
|
---|
748 | *(++ptr) = card->slot;
|
---|
749 | send_buf[16]++;
|
---|
750 | found = 1;
|
---|
751 | }
|
---|
752 | break;
|
---|
753 | }
|
---|
754 | ll_iter_release(it);
|
---|
755 |
|
---|
756 | if (!found) {
|
---|
757 | er->rc = E_RDR_NOTFOUND;
|
---|
758 | er->rcEx = 0x27;
|
---|
759 | cs_debug_mask(D_READER, "gbox: %s no suitable card found, discarding ecm", cli->reader->label);
|
---|
760 | write_ecm_answer(cli->reader, er);
|
---|
761 |
|
---|
762 | return 0;
|
---|
763 | }
|
---|
764 |
|
---|
765 | memcpy(++ptr, gbox->checkcode, 7);
|
---|
766 | ptr += 7;
|
---|
767 | memcpy(ptr, gbox->peer.checkcode, 7);
|
---|
768 |
|
---|
769 | len = ptr + 7 - send_buf;
|
---|
770 |
|
---|
771 | gbox_send(cli, send_buf, len);
|
---|
772 |
|
---|
773 | return 0;
|
---|
774 | }
|
---|
775 |
|
---|
776 | static int gbox_send_emm(EMM_PACKET *ep)
|
---|
777 | {
|
---|
778 | // emms not yet supported
|
---|
779 |
|
---|
780 | return 0;
|
---|
781 | }
|
---|
782 |
|
---|
783 | void module_gbox(struct s_module *ph)
|
---|
784 | {
|
---|
785 | strcpy(ph->desc, "gbox");
|
---|
786 | ph->num=R_GBOX;
|
---|
787 | ph->type=MOD_CONN_UDP;
|
---|
788 | ph->logtxt = ", crypted";
|
---|
789 |
|
---|
790 | ph->multi=1;
|
---|
791 | ph->watchdog=1;
|
---|
792 | ph->send_dcw=gbox_send_dcw;
|
---|
793 |
|
---|
794 | ph->recv=gbox_recv;
|
---|
795 | ph->c_multi=1;
|
---|
796 | ph->c_init=gbox_client_init;
|
---|
797 | ph->c_recv_chk=gbox_recv_chk;
|
---|
798 | ph->c_send_ecm=gbox_send_ecm;
|
---|
799 | ph->c_send_emm=gbox_send_emm;
|
---|
800 | }
|
---|
801 | #endif
|
---|
802 |
|
---|