-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-pandora.c
321 lines (279 loc) · 7.76 KB
/
module-pandora.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
#define MODULE_LOG_PREFIX "pandora"
#include "globals.h"
#ifdef MODULE_PANDORA
#include "cscrypt/md5.h"
#include "oscam-client.h"
#include "oscam-ecm.h"
#include "oscam-net.h"
#include "oscam-string.h"
#define CWS_NETMSGSIZE 320
#define START_TIME 150000
#define MAX_TIME 500000
static void simple_crypt(uint8_t *buf, int len, uint8_t *key, int key_len)
{
int i, x;
for(i = 0, x = 0; i < len; i++)
{
buf[i] ^= key[x++];
if(x >= key_len)
{ x = 0; }
}
}
static void pandora_process_request(struct s_client *cl, uint8_t *buf, int32_t l)
{
int ecmlen;
ECM_REQUEST *er;
uint8_t md5tmp[MD5_DIGEST_LENGTH];
if(l < 10 + CS_ECMSTORESIZE + 2)
{ return; }
if(!(er = get_ecmtask()))
{ return; }
er->caid = b2i(2, buf + 1);
er->srvid = b2i(2, buf + 3);
er->prid = b2i(4, buf + 5);
//er->ecmcrc32 = crc32(0L, buf+10, CS_ECMSTORESIZE);
er->chid = b2i(2, buf + 10 + CS_ECMSTORESIZE);
if(!cl->pand_ignore_ecm && (l >= 10 + CS_ECMSTORESIZE + 2 + 2))
{
ecmlen = b2i(2, buf + 10 + CS_ECMSTORESIZE + 2);
if(ecmlen < 0 || ecmlen > MAX_ECM_SIZE
|| ((10 + CS_ECMSTORESIZE + 2 + 2 + ecmlen) > CWS_NETMSGSIZE)
|| ((10 + CS_ECMSTORESIZE + 2 + 2 + ecmlen) > l))
{
er->ecmlen = 0;
}
else
{
if(!memcmp(buf + 10, MD5(buf + 14 + CS_ECMSTORESIZE, ecmlen, md5tmp), CS_ECMSTORESIZE))
{
er->ecmlen = ecmlen;
memcpy(er->ecm, buf + 14 + CS_ECMSTORESIZE, ecmlen);
//set_ecmhash(cl, er);
}
else
{ er->ecmlen = 0; }
}
}
else
{ er->ecmlen = 0; }
if(!er->ecmlen)
{ usleep(cl->pand_autodelay); }
get_cw(cl, er);
}
static int pandora_recv(struct s_client *cl, uint8_t *buf, int32_t l)
{
int ret;
if(!cl->udp_fd)
{ return (-9); }
if(cl->typ != 'c')
{ ret = recv_from_udpipe(buf); }
else
{
ret = recvfrom(cl->udp_fd, buf, l, 0, (struct sockaddr *)&cl->udp_sa, &cl->udp_sa_len);
}
if(ret < 1)
{ return (-1); }
simple_crypt(buf, ret, cl->pand_md5_key, 16);
cl->last = time((time_t *) 0);
if(cl->typ != 'c')
{ pandora_process_request(cl, buf, ret); }
return (ret);
}
static void pandora_send_dcw(struct s_client *cl, ECM_REQUEST *er)
{
uint8_t msgbuf[CWS_NETMSGSIZE], len;
if(cfg.pand_skip_send_dw)
{ return; }
if(er->rc < E_NOTFOUND)
{
msgbuf[0] = 2; // DW_FOUND
memcpy(&msgbuf[1], er->cw, 16);
len = 1 + 16;
cl->pand_autodelay = START_TIME;
}
else
{
msgbuf[0] = 0xFF; // DW_NOT_FOUND
len = 1;
if(cl->pand_autodelay < MAX_TIME)
{ cl->pand_autodelay += 100000; }
}
simple_crypt(msgbuf, len, cl->pand_md5_key, 16);
sendto(cl->udp_fd, msgbuf, len, 0, (struct sockaddr *) &cl->udp_sa, cl->udp_sa_len);
}
int pandora_auth_client(struct s_client *cl, IN_ADDR_T ip)
{
int ok;
struct s_auth *account;
#ifdef IPV6SUPPORT
// FIXME: Add IPv6 support
(void)ip; // Prevent warning about unused var "ip"
#else
if(!cl->pand_ignore_ecm && cfg.pand_allowed)
{
struct s_ip *p_ip;
for(ok = 0, p_ip = cfg.pand_allowed; (p_ip) && (!ok); p_ip
= p_ip->next)
{ ok = ((ip >= p_ip->ip[0]) && (ip <= p_ip->ip[1])); }
if(!ok)
{
cs_auth_client(cl, (struct s_auth *) 0, "IP not allowed");
return 0;
}
}
#endif
for(ok = 0, account = cfg.account; cfg.pand_usr && account && !ok; account = account->next)
{
ok = streq(cfg.pand_usr, account->usr);
if(ok && cs_auth_client(cl, account, NULL))
{ cs_disconnect_client(cl); }
}
if(!ok)
{ cs_auth_client(cl, (struct s_auth *)(-1), NULL); }
return ok;
}
static void *pandora_server(struct s_client *cl, uint8_t *UNUSED(mbuf), int32_t UNUSED(len))
{
uint8_t md5tmp[MD5_DIGEST_LENGTH];
if(!cl->init_done)
{
if(cfg.pand_pass)
{
cl->pand_autodelay = 150000;
memcpy(cl->pand_md5_key, MD5((uint8_t *)cfg.pand_pass, cs_strlen(cfg.pand_pass), md5tmp), 16);
cl->pand_ignore_ecm = (cfg.pand_ecm) ? 0 : 1;
cl->crypted = 1;
pandora_auth_client(cl, cl->ip);
cl->init_done = 1;
}
else
{
cs_log("Password for Pandora share MUST be set !!!");
}
}
return NULL;
}
/************************************************************************************************************************
* client functions
*************************************************************************************************************************/
int pandora_client_init(struct s_client *cl)
{
static struct sockaddr_in loc_sa;
int16_t p_proto;
char ptxt[16];
struct s_reader *rdr = cl->reader;
uint8_t md5tmp[MD5_DIGEST_LENGTH];
cl->pfd = 0;
if(rdr->r_port <= 0)
{
cs_log("invalid port %d for server %s", rdr->r_port, rdr->device);
return (1);
}
p_proto = IPPROTO_UDP;
set_null_ip(&cl->ip);
memset((char *) &loc_sa, 0, sizeof(loc_sa));
loc_sa.sin_family = AF_INET;
if(IP_ISSET(cfg.srvip))
{ IP_ASSIGN(SIN_GET_ADDR(loc_sa), cfg.srvip); }
else
{ loc_sa.sin_addr.s_addr = INADDR_ANY; }
loc_sa.sin_port = htons(rdr->l_port);
if((cl->udp_fd = socket(PF_INET, SOCK_DGRAM, p_proto)) < 0)
{
cs_log("Socket creation failed (errno=%d)", errno);
return 1;
}
int32_t opt = 1;
setsockopt(cl->udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
set_so_reuseport(cl->udp_fd);
set_socket_priority(cl->udp_fd, cfg.netprio);
if(rdr->l_port > 0)
{
if(bind(cl->udp_fd, (struct sockaddr *) &loc_sa, sizeof(loc_sa)) < 0)
{
cs_log("bind failed (errno=%d)", errno);
close(cl->udp_fd);
return (1);
}
snprintf(ptxt, sizeof(ptxt), ", port=%d", rdr->l_port);
}
else
{ ptxt[0] = '\0'; }
memcpy(cl->pand_md5_key, MD5((uint8_t *)rdr->r_pwd, cs_strlen(rdr->r_pwd), md5tmp), 16);
cl->crypted = 1;
//cl->grp = 0xFFFFFFFF;
//rdr->caid[0] = rdr->ctab.caid[0];
cl->pand_send_ecm = rdr->pand_send_ecm;
memset((char *) &cl->udp_sa, 0, sizeof(cl->udp_sa));
#ifdef IPV6SUPPORT
((struct sockaddr_in *)(&cl->udp_sa))->sin_family = AF_INET;
((struct sockaddr_in *)(&cl->udp_sa))->sin_port = htons((u_short) rdr->r_port);
#else
cl->udp_sa.sin_family = AF_INET;
cl->udp_sa.sin_port = htons((u_short) rdr->r_port);
#endif
cs_log("proxy %s:%d pandora %s (%s)", rdr->device, rdr->r_port, rdr->pand_send_ecm ? "with ECM support" : "", ptxt);
cl->pfd = cl->udp_fd;
//set_nonblock(cl->udp_fd, true); //!!!!!
return (0);
}
static int pandora_send_ecm(struct s_client *cl, ECM_REQUEST *er)
{
uint8_t md5tmp[MD5_DIGEST_LENGTH];
uint8_t msgbuf[CWS_NETMSGSIZE];
int ret, len;
uint8_t adel;
adel = (cfg.ctimeout > 7) ? 7 : cfg.ctimeout;
msgbuf[0] = 1;
msgbuf[1] = er->caid >> 8;
msgbuf[2] = er->caid & 0xFF;
msgbuf[3] = er->srvid >> 8;
msgbuf[4] = er->srvid & 0xFF;
msgbuf[5] = er->prid >> 24;
msgbuf[6] = er->prid >> 16;
msgbuf[7] = er->prid >> 8;
msgbuf[8] = er->prid & 0xFF;
msgbuf[9] = adel;
memcpy(&msgbuf[10], MD5(er->ecm, er->ecmlen, md5tmp), CS_ECMSTORESIZE);
msgbuf[10 + CS_ECMSTORESIZE] = er->chid >> 8;
msgbuf[11 + CS_ECMSTORESIZE] = er->chid & 0xFF;
len = 12 + CS_ECMSTORESIZE;
if(cl->pand_send_ecm)
{
if(len+2+er->ecmlen > CWS_NETMSGSIZE)
{ return -1; }
msgbuf[12 + CS_ECMSTORESIZE] = er->ecmlen >> 8;
msgbuf[13 + CS_ECMSTORESIZE] = er->ecmlen & 0xFF;
memcpy(&msgbuf[14 + CS_ECMSTORESIZE], er->ecm, er->ecmlen);
len += er->ecmlen + 2;
}
simple_crypt(msgbuf, len, cl->pand_md5_key, 16);
ret = sendto(cl->pfd, msgbuf, len, 0, (struct sockaddr *) &cl->udp_sa, cl->udp_sa_len);
return ((ret < len) ? (-1) : 0);
}
static int pandora_recv_chk(struct s_client *UNUSED(cl), uint8_t *dcw, int *rc, uint8_t *buf, int UNUSED(n))
{
if(buf[0] != 0x2)
{ return (-1); }
*rc = 1;
memcpy(dcw, buf + 1, 16);
return (0);
}
void module_pandora(struct s_module *ph)
{
ph->ptab.nports = 1;
ph->ptab.ports[0].s_port = cfg.pand_port;
ph->num = R_PANDORA;
ph->desc = "pandora";
ph->type = MOD_CONN_UDP;
ph->large_ecm_support = 1;
//ph->watchdog = 1;
IP_ASSIGN(ph->s_ip, cfg.pand_srvip);
ph->s_handler = pandora_server;
ph->recv = pandora_recv;
ph->send_dcw = pandora_send_dcw;
ph->c_init = pandora_client_init;
ph->c_recv_chk = pandora_recv_chk;
ph->c_send_ecm = pandora_send_ecm;
}
#endif