-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-radegast.c
368 lines (323 loc) · 8.9 KB
/
module-radegast.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "globals.h"
#ifdef MODULE_RADEGAST
#include "oscam-client.h"
#include "oscam-ecm.h"
#include "oscam-net.h"
#include "oscam-string.h"
#include "oscam-reader.h"
static int32_t radegast_connect(void);
static int32_t radegast_send(struct s_client *client, uchar *buf)
{
int32_t l = buf[1] + 2;
return (send(client->pfd, buf, l, 0));
}
static int32_t radegast_recv(struct s_client *client, uchar *buf, int32_t l)
{
int32_t n;
if(!client->pfd) { return (-1); }
if(client->typ == 'c') // server code
{
if((n = recv(client->pfd, buf, l, 0)) > 0)
{ client->last = time((time_t *) 0); }
}
else // client code
{
if((n = recv(client->pfd, buf, l, 0)) > 0)
{
cs_ddump_mask(D_CLIENT, buf, n, "radegast: received %d bytes from %s", n, remote_txt());
client->last = time((time_t *) 0);
if((buf[0] == 0x02) && (buf[1] == 0x12) && (buf[2] == 0x05) && (buf[3] == 0x10)) { return (n); } // dcw received
else if((buf[0] == 0x02) && (buf[1] == 0x02) && (buf[2] == 0x04) && (buf[3] == 0x00)) { return (n); } // dcw no found
else if((buf[0] == 0x81) && (buf[1] == 0x00)) { return (n); } // cmd unknown
else { n = -1; }// no cmd radegast disconnect
}
}
return (n);
}
static int32_t radegast_recv_chk(struct s_client *client, uchar *dcw, int32_t *rc, uchar *buf, int32_t UNUSED(n))
{
if((buf[0] == 2) && (buf[1] == 0x12))
{
char tmp_dbg[33];
memcpy(dcw, buf + 4, 16);
cs_debug_mask(D_CLIENT, "radegast: recv chk - %s", cs_hexdump(0, dcw, 16, tmp_dbg, sizeof(tmp_dbg)));
*rc = 1;
return (client->reader->msg_idx);
}
return (-1);
}
static void radegast_auth_client(IN_ADDR_T ip)
{
int32_t ok;
struct s_auth *account;
struct s_client *cl = cur_client();
ok = check_ip(cfg.rad_allowed, ip);
if(!ok)
{
cs_log("radegast: IP not allowed");
cs_auth_client(cl, (struct s_auth *)0, NULL);
cs_disconnect_client(cl);
}
for(ok = 0, account = cfg.account; cfg.rad_usr && account && !ok; account = account->next)
{
ok = streq(cfg.rad_usr, account->usr);
if(ok && cs_auth_client(cl, account, NULL))
{ cs_disconnect_client(cl); }
}
if(!ok)
{ cs_auth_client(cl, ok ? account : (struct s_auth *)(-1), "radegast"); }
}
static void radegast_send_dcw(struct s_client *client, ECM_REQUEST *er)
{
uchar mbuf[1024];
mbuf[0] = 0x02; // DCW
if(er->rc < E_NOTFOUND)
{
mbuf[1] = 0x12; // len (overall)
mbuf[2] = 0x05; // ACCESS
mbuf[3] = 0x10; // len
memcpy(mbuf + 4, er->cw, 16);
}
else
{
mbuf[1] = 0x02; // len (overall)
mbuf[2] = 0x04; // NO ACCESS
mbuf[3] = 0x00; // len
}
radegast_send(client, mbuf);
}
static void radegast_process_ecm(uchar *buf, int32_t l)
{
int32_t i, n, sl;
ECM_REQUEST *er;
struct s_client *cl = cur_client();
if(!(er = get_ecmtask()))
{ return; }
for(i = 0; i < l; i += (sl + 2))
{
sl = buf[i + 1];
switch(buf[i])
{
case 2: // CAID (upper byte only, oldstyle)
er->caid = buf[i + 2] << 8;
break;
case 10: // CAID
er->caid = b2i(2, buf + i + 2);
break;
case 3: // ECM DATA
//er->ecmlen = sl;
er->ecmlen = (((buf[i + 1 + 2] & 0x0F) << 8) | buf[i + 2 + 2]) + 3;
memcpy(er->ecm, buf + i + 2, er->ecmlen);
break;
case 6: // PROVID (ASCII)
n = (sl > 6) ? 3 : (sl >> 1);
er->prid = cs_atoi((char *) buf + i + 2 + sl - (n << 1), n, 0);
break;
case 7: // KEYNR (ASCII), not needed
break;
case 8: // ECM PROCESS PID ?? don't know, not needed
break;
}
}
if(l != i)
{ cs_log("WARNING: ECM-request corrupt"); }
else
{ get_cw(cl, er); }
}
static void radegast_process_unknown(uchar *buf)
{
uchar answer[2] = {0x81, 0x00};
radegast_send(cur_client(), answer);
cs_log("unknown request %02X, len=%d", buf[0], buf[1]);
}
static void *radegast_server(struct s_client *client, uchar *mbuf, int32_t n)
{
if(n < 3)
{ return NULL; }
if(!client->init_done)
{
radegast_auth_client(cur_client()->ip);
client->init_done = 1;
}
switch(mbuf[0])
{
case 1:
radegast_process_ecm(mbuf + 2, mbuf[1]);
break;
default:
radegast_process_unknown(mbuf);
}
return NULL;
}
static int32_t radegast_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *UNUSED(buf))
{
uchar provid_buf[8];
uchar header[22] = "\x02\x01\x00\x06\x08\x30\x30\x30\x30\x30\x30\x30\x30\x07\x04\x30\x30\x30\x38\x08\x01\x02";
uchar *ecmbuf;
uint8_t *SubECMp;
uint8_t *via_ecm_mod;
uint32_t n, k, Len, pos = 0;
if(!radegast_connect())
{ return (-1); }
if(!cs_malloc(&ecmbuf, er->ecmlen + 30))
{ return -1; }
// Quickfix to suppress SubECMs with CWsSwap set to 01
// Applied only on Viaccess (CAID: 0x0500)
// this reduce the size of the ECM from long to short
// 40 07 03 0B 00 08 07 01 00 ... -> to keep
// 40 07 03 0B 00 08 07 01 01 ... -> to delete
// Thanks to luffy for the tip and the code.
if(er->caid == 0x500)
{
cs_ddump_mask(D_ATR, er->ecm, er->ecmlen, "%s: ecm dump BEFORE suppressing SubECMs with CWsSwap set to 01", __func__);
Len = er->ecmlen;
if(cs_malloc (&via_ecm_mod, Len+4))
{
if( er->ecm[4]==0x80 )
{
memcpy(via_ecm_mod, er->ecm, 4);
via_ecm_mod[1] = 0x70;
via_ecm_mod[2] = 0x01;
pos = 0x04;
k = 4;
while(k<Len)
{
SubECMp = (uint8_t *)&er->ecm[k];
if( ((pos+SubECMp[1]+2)>0xE0)||(pos+SubECMp[1]+2)>Len )
{
break;
}
if (SubECMp[2]==0xD2)
{
if( SubECMp[0x0E] == 0x00 )
{
memcpy(via_ecm_mod+pos, SubECMp, SubECMp[1]+2);
via_ecm_mod[2] += SubECMp[1]+2;
pos += SubECMp[1]+2;
}
}
else if ( (SubECMp[2]==0x90 || SubECMp[2]==0x40) && SubECMp[3]==0x07 )
{
if( SubECMp[0x0A] == 0x00 )
{
memcpy(via_ecm_mod+pos, SubECMp, SubECMp[1]+2);
via_ecm_mod[2] += SubECMp[1]+2;
pos += SubECMp[1]+2;
}
}
k += SubECMp[1] + 2;
}
Len = via_ecm_mod[2]+3;
er->ecmlen = Len;
memcpy(er->ecm, via_ecm_mod, Len);
cs_ddump_mask(D_ATR, er->ecm, er->ecmlen, "%s: ecm dump AFTER suppressing SubECMs with CWsSwap set to 01", __func__);
}
NULLFREE(via_ecm_mod);
}
}
ecmbuf[0] = 1;
ecmbuf[1] = (er->ecmlen + 30 - 2) & 0xff;
memcpy(ecmbuf + 2, header, sizeof(header));
for(n = 0; n < 4; n++)
{
snprintf((char *)provid_buf + (n * 2), sizeof(provid_buf) - (n * 2), "%02X", ((uchar *)(&er->prid))[4 - 1 - n]);
}
ecmbuf[7] = provid_buf[0];
ecmbuf[8] = provid_buf[1];
ecmbuf[9] = provid_buf[2];
ecmbuf[10] = provid_buf[3];
ecmbuf[11] = provid_buf[4];
ecmbuf[12] = provid_buf[5];
ecmbuf[13] = provid_buf[6];
ecmbuf[14] = provid_buf[7];
ecmbuf[2 + sizeof(header)] = 0xa;
ecmbuf[3 + sizeof(header)] = 2;
ecmbuf[4 + sizeof(header)] = er->caid >> 8;
ecmbuf[5 + sizeof(header)] = er->caid & 0xff;
ecmbuf[6 + sizeof(header)] = 3;
ecmbuf[7 + sizeof(header)] = er->ecmlen & 0xff;
memcpy(ecmbuf + 8 + sizeof(header), er->ecm, er->ecmlen);
ecmbuf[4] = er->caid >> 8;
client->reader->msg_idx = er->idx;
n = send(client->pfd, ecmbuf, er->ecmlen + 30, 0);
cs_debug_mask(D_TRACE, "radegast: sending ecm");
cs_ddump_mask(D_CLIENT, ecmbuf, er->ecmlen + 30, "ecm:");
NULLFREE(ecmbuf);
return ((n < 1) ? (-1) : 0);
}
int32_t radegast_cli_init(struct s_client *cl)
{
int32_t handle;
handle = network_tcp_connection_open(cl->reader);
if(handle < 0) { return -1; }
cs_log("radegast: proxy %s:%d (fd=%d)",
cl->reader->device, cl->reader->r_port, cl->udp_fd);
cl->reader->tcp_connected = 2;
cl->reader->card_status = CARD_INSERTED;
cl->reader->last_g = cl->reader->last_s = time((time_t *)0);
cs_debug_mask(D_CLIENT, "radegast: last_s=%ld, last_g=%ld", cl->reader->last_s, cl->reader->last_g);
cl->pfd = cl->udp_fd;
return (0);
}
static void radegast_server_init(struct s_client *cl)
{
if(!cl->init_done)
{
if(IP_ISSET(cl->ip))
{ cs_log("radegast: new connection from %s", cs_inet_ntoa(cl->ip)); }
radegast_auth_client(cur_client()->ip);
cl->init_done = 1;
}
return;
}
static int32_t radegast_connect(void)
{
struct s_client *cl = cur_client();
if(cl->reader->tcp_connected < 2 && radegast_cli_init(cl) < 0)
{ return 0; }
if(!cl->udp_fd)
{ return 0; }
return 1;
}
void radegast_idle(void)
{
struct s_client *client = cur_client();
struct s_reader *rdr = client->reader;
time_t now = time(NULL);
if(!rdr) { return; }
if(rdr->tcp_ito > 0)
{
int32_t time_diff;
time_diff = abs(now - rdr->last_s);
if(time_diff > (rdr->tcp_ito))
{
network_tcp_connection_close(rdr, "inactivity");
return;
}
}
else if(rdr->tcp_ito == -1)
{
radegast_connect();
return;
}
}
void module_radegast(struct s_module *ph)
{
ph->ptab.nports = 1;
ph->ptab.ports[0].s_port = cfg.rad_port;
ph->desc = "radegast";
ph->type = MOD_CONN_TCP;
ph->large_ecm_support = 1;
ph->listenertype = LIS_RADEGAST;
IP_ASSIGN(ph->s_ip, cfg.rad_srvip);
ph->s_handler = radegast_server;
ph->s_init = radegast_server_init;
ph->c_idle = radegast_idle;
ph->recv = radegast_recv;
ph->send_dcw = radegast_send_dcw;
ph->c_init = radegast_cli_init;
ph->c_recv_chk = radegast_recv_chk;
ph->c_send_ecm = radegast_send_ecm;
ph->num = R_RADEGAST;
}
#endif