1 | #define MODULE_LOG_PREFIX "pandora"
|
---|
2 |
|
---|
3 | #include "globals.h"
|
---|
4 |
|
---|
5 | #ifdef MODULE_PANDORA
|
---|
6 |
|
---|
7 | #include "cscrypt/md5.h"
|
---|
8 | #include "oscam-client.h"
|
---|
9 | #include "oscam-ecm.h"
|
---|
10 | #include "oscam-net.h"
|
---|
11 | #include "oscam-string.h"
|
---|
12 |
|
---|
13 | #define CWS_NETMSGSIZE 320
|
---|
14 | #define START_TIME 150000
|
---|
15 | #define MAX_TIME 500000
|
---|
16 |
|
---|
17 | static void simple_crypt(uchar *buf, int len, uchar *key, int key_len)
|
---|
18 | {
|
---|
19 | int i, x;
|
---|
20 | for(i = 0, x = 0; i < len; i++)
|
---|
21 | {
|
---|
22 | buf[i] ^= key[x++];
|
---|
23 | if(x >= key_len)
|
---|
24 | { x = 0; }
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | static void pandora_process_request(struct s_client *cl, uchar *buf, int32_t l)
|
---|
29 | {
|
---|
30 | int ecmlen;
|
---|
31 | ECM_REQUEST *er;
|
---|
32 | uchar md5tmp[MD5_DIGEST_LENGTH];
|
---|
33 |
|
---|
34 | if(!(er = get_ecmtask()))
|
---|
35 | { return; }
|
---|
36 | er->caid = b2i(2, buf + 1);
|
---|
37 | er->srvid = b2i(2, buf + 3);
|
---|
38 | er->prid = b2i(4, buf + 5);
|
---|
39 | //er->ecmcrc32 = crc32(0L, buf+10, CS_ECMSTORESIZE);
|
---|
40 | er->chid = b2i(2, buf + 10 + CS_ECMSTORESIZE);
|
---|
41 |
|
---|
42 | if(l > 12 + CS_ECMSTORESIZE + 16)
|
---|
43 | {
|
---|
44 | ecmlen = b2i(2, buf + 12 + CS_ECMSTORESIZE);
|
---|
45 | if((ecmlen > 320) || cl->pand_ignore_ecm)
|
---|
46 | { er->ecmlen = 0; }
|
---|
47 | else
|
---|
48 | {
|
---|
49 | if(!memcmp(buf + 10,
|
---|
50 | MD5(buf + 14 + CS_ECMSTORESIZE, ecmlen, md5tmp),
|
---|
51 | CS_ECMSTORESIZE))
|
---|
52 | {
|
---|
53 | er->ecmlen = ecmlen;
|
---|
54 | memcpy(er->ecm, buf + 14 + CS_ECMSTORESIZE, ecmlen);
|
---|
55 | //set_ecmhash(cl, er);
|
---|
56 | }
|
---|
57 | else
|
---|
58 | { er->ecmlen = 0; }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | else
|
---|
62 | { er->ecmlen = 0; }
|
---|
63 |
|
---|
64 | if(!er->ecmlen)
|
---|
65 | { usleep(cl->pand_autodelay); }
|
---|
66 | get_cw(cl, er);
|
---|
67 | }
|
---|
68 |
|
---|
69 | static int pandora_recv(struct s_client *cl, uchar *buf, int32_t l)
|
---|
70 | {
|
---|
71 | int ret;
|
---|
72 |
|
---|
73 | if(!cl->udp_fd)
|
---|
74 | { return (-9); }
|
---|
75 | if(cl->typ != 'c')
|
---|
76 | { ret = recv_from_udpipe(buf); }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | ret = recvfrom(cl->udp_fd, buf, l, 0, (struct sockaddr *)&cl->udp_sa, &cl->udp_sa_len);
|
---|
80 | }
|
---|
81 | if(ret < 1)
|
---|
82 | { return (-1); }
|
---|
83 |
|
---|
84 | simple_crypt(buf, ret, cl->pand_md5_key, 16);
|
---|
85 | cl->last = time((time_t *) 0);
|
---|
86 |
|
---|
87 | if(cl->typ != 'c')
|
---|
88 | { pandora_process_request(cl, buf, ret); }
|
---|
89 | return (ret);
|
---|
90 | }
|
---|
91 |
|
---|
92 | static void pandora_send_dcw(struct s_client *cl, ECM_REQUEST *er)
|
---|
93 | {
|
---|
94 | uchar msgbuf[CWS_NETMSGSIZE], len;
|
---|
95 | if(cfg.pand_skip_send_dw)
|
---|
96 | { return; }
|
---|
97 | if(er->rc < E_NOTFOUND)
|
---|
98 | {
|
---|
99 | msgbuf[0] = 2; //DW_FOUND
|
---|
100 | memcpy(&msgbuf[1], er->cw, 16);
|
---|
101 | len = 1 + 16;
|
---|
102 | cl->pand_autodelay = START_TIME;
|
---|
103 | }
|
---|
104 | else
|
---|
105 | {
|
---|
106 | msgbuf[0] = 0xFF; //DW_NOT_FOUND
|
---|
107 | len = 1;
|
---|
108 | if(cl->pand_autodelay < MAX_TIME)
|
---|
109 | { cl->pand_autodelay += 100000; }
|
---|
110 | }
|
---|
111 | simple_crypt(msgbuf, len, cl->pand_md5_key, 16);
|
---|
112 | sendto(cl->udp_fd, msgbuf, len, 0, (struct sockaddr *) &cl->udp_sa, cl->udp_sa_len);
|
---|
113 | }
|
---|
114 |
|
---|
115 | int pandora_auth_client(struct s_client *cl, IN_ADDR_T ip)
|
---|
116 | {
|
---|
117 | int ok;
|
---|
118 | struct s_auth *account;
|
---|
119 |
|
---|
120 | #ifdef IPV6SUPPORT
|
---|
121 | // FIXME: Add IPv6 support
|
---|
122 | (void)ip; // Prevent warning about unused var "ip"
|
---|
123 | #else
|
---|
124 | if(!cl->pand_ignore_ecm && cfg.pand_allowed)
|
---|
125 | {
|
---|
126 | struct s_ip *p_ip;
|
---|
127 | for(ok = 0, p_ip = cfg.pand_allowed; (p_ip) && (!ok); p_ip
|
---|
128 | = p_ip->next)
|
---|
129 | { ok = ((ip >= p_ip->ip[0]) && (ip <= p_ip->ip[1])); }
|
---|
130 |
|
---|
131 | if(!ok)
|
---|
132 | {
|
---|
133 | cs_auth_client(cl, (struct s_auth *) 0, "IP not allowed");
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | for(ok = 0, account = cfg.account; cfg.pand_usr && account && !ok; account = account->next)
|
---|
140 | {
|
---|
141 | ok = streq(cfg.pand_usr, account->usr);
|
---|
142 | if(ok && cs_auth_client(cl, account, NULL))
|
---|
143 | { cs_disconnect_client(cl); }
|
---|
144 | }
|
---|
145 | if(!ok)
|
---|
146 | { cs_auth_client(cl, (struct s_auth *)(-1), NULL); }
|
---|
147 | return ok;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void *pandora_server(struct s_client *cl, uchar *UNUSED(mbuf),
|
---|
151 | int32_t UNUSED(len))
|
---|
152 | {
|
---|
153 | uchar md5tmp[MD5_DIGEST_LENGTH];
|
---|
154 | if(!cl->init_done)
|
---|
155 | {
|
---|
156 | if(cfg.pand_pass)
|
---|
157 | {
|
---|
158 | cl->pand_autodelay = 150000;
|
---|
159 | memcpy(cl->pand_md5_key,
|
---|
160 | MD5((uchar *)cfg.pand_pass, strlen(cfg.pand_pass), md5tmp), 16);
|
---|
161 | cl->pand_ignore_ecm = (cfg.pand_ecm) ? 0 : 1;
|
---|
162 | cl->crypted = 1;
|
---|
163 | pandora_auth_client(cl, cl->ip);
|
---|
164 | cl->init_done = 1;
|
---|
165 | }
|
---|
166 | else
|
---|
167 | {
|
---|
168 | cs_log("Password for Pandora share MUST be set !!!");
|
---|
169 | }
|
---|
170 | }
|
---|
171 | return NULL;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /************************************************************************************************************************
|
---|
175 | * client functions
|
---|
176 | *************************************************************************************************************************/
|
---|
177 | int pandora_client_init(struct s_client *cl)
|
---|
178 | {
|
---|
179 | static struct sockaddr_in loc_sa;
|
---|
180 | int16_t p_proto;
|
---|
181 | char ptxt[16];
|
---|
182 | struct s_reader *rdr = cl->reader;
|
---|
183 | uchar md5tmp[MD5_DIGEST_LENGTH];
|
---|
184 |
|
---|
185 | cl->pfd = 0;
|
---|
186 | if(rdr->r_port <= 0)
|
---|
187 | {
|
---|
188 | cs_log("invalid port %d for server %s", rdr->r_port, rdr->device);
|
---|
189 | return (1);
|
---|
190 | }
|
---|
191 | p_proto = IPPROTO_UDP;
|
---|
192 |
|
---|
193 | set_null_ip(&cl->ip);
|
---|
194 | memset((char *) &loc_sa, 0, sizeof(loc_sa));
|
---|
195 | loc_sa.sin_family = AF_INET;
|
---|
196 |
|
---|
197 | if(IP_ISSET(cfg.srvip))
|
---|
198 | { IP_ASSIGN(SIN_GET_ADDR(loc_sa), cfg.srvip); }
|
---|
199 | else
|
---|
200 | { loc_sa.sin_addr.s_addr = INADDR_ANY; }
|
---|
201 | loc_sa.sin_port = htons(rdr->l_port);
|
---|
202 |
|
---|
203 | if((cl->udp_fd = socket(PF_INET, SOCK_DGRAM, p_proto)) < 0)
|
---|
204 | {
|
---|
205 | cs_log("Socket creation failed (errno=%d)", errno);
|
---|
206 | return 1;
|
---|
207 | }
|
---|
208 |
|
---|
209 | int32_t opt = 1;
|
---|
210 | setsockopt(cl->udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
---|
211 |
|
---|
212 | set_so_reuseport(cl->udp_fd);
|
---|
213 |
|
---|
214 | set_socket_priority(cl->udp_fd, cfg.netprio);
|
---|
215 |
|
---|
216 | if(rdr->l_port > 0)
|
---|
217 | {
|
---|
218 | if(bind(cl->udp_fd, (struct sockaddr *) &loc_sa, sizeof(loc_sa)) < 0)
|
---|
219 | {
|
---|
220 | cs_log("bind failed (errno=%d)", errno);
|
---|
221 | close(cl->udp_fd);
|
---|
222 | return (1);
|
---|
223 | }
|
---|
224 | snprintf(ptxt, sizeof(ptxt), ", port=%d", rdr->l_port);
|
---|
225 | }
|
---|
226 | else
|
---|
227 | { ptxt[0] = '\0'; }
|
---|
228 |
|
---|
229 | memcpy(cl->pand_md5_key, MD5((uchar *)rdr->r_pwd, strlen(rdr->r_pwd), md5tmp), 16);
|
---|
230 | cl->crypted = 1;
|
---|
231 |
|
---|
232 | //cl->grp = 0xFFFFFFFF;
|
---|
233 | //rdr->caid[0] = rdr->ctab.caid[0];
|
---|
234 |
|
---|
235 | cl->pand_send_ecm = rdr->pand_send_ecm;
|
---|
236 | memset((char *) &cl->udp_sa, 0, sizeof(cl->udp_sa));
|
---|
237 | #ifdef IPV6SUPPORT
|
---|
238 | ((struct sockaddr_in *)(&cl->udp_sa))->sin_family = AF_INET;
|
---|
239 | ((struct sockaddr_in *)(&cl->udp_sa))->sin_port = htons((u_short) rdr->r_port);
|
---|
240 | #else
|
---|
241 | cl->udp_sa.sin_family = AF_INET;
|
---|
242 | cl->udp_sa.sin_port = htons((u_short) rdr->r_port);
|
---|
243 | #endif
|
---|
244 |
|
---|
245 | cs_log("proxy %s:%d pandora %s (%s)", rdr->device, rdr->r_port, rdr->pand_send_ecm ? "with ECM support" : "", ptxt);
|
---|
246 |
|
---|
247 | cl->pfd = cl->udp_fd;
|
---|
248 | //set_nonblock(cl->udp_fd, true); //!!!!!
|
---|
249 | return (0);
|
---|
250 | }
|
---|
251 |
|
---|
252 | static int pandora_send_ecm(struct s_client *cl, ECM_REQUEST *er, uchar *UNUSED(buf))
|
---|
253 | {
|
---|
254 | uchar md5tmp[MD5_DIGEST_LENGTH];
|
---|
255 | uchar msgbuf[CWS_NETMSGSIZE];
|
---|
256 | int ret, len;
|
---|
257 | uchar adel;
|
---|
258 | adel = (cfg.ctimeout > 7) ? 7 : cfg.ctimeout;
|
---|
259 |
|
---|
260 | msgbuf[0] = 1;
|
---|
261 | msgbuf[1] = er->caid >> 8;
|
---|
262 | msgbuf[2] = er->caid & 0xFF;
|
---|
263 | msgbuf[3] = er->srvid >> 8;
|
---|
264 | msgbuf[4] = er->srvid & 0xFF;
|
---|
265 | msgbuf[5] = er->prid >> 24;
|
---|
266 | msgbuf[6] = er->prid >> 16;
|
---|
267 | msgbuf[7] = er->prid >> 8;
|
---|
268 | msgbuf[8] = er->prid & 0xFF;
|
---|
269 | msgbuf[9] = adel;
|
---|
270 | memcpy(&msgbuf[10], MD5(er->ecm, er->ecmlen, md5tmp), CS_ECMSTORESIZE);
|
---|
271 | msgbuf[10 + CS_ECMSTORESIZE] = er->chid >> 8;
|
---|
272 | msgbuf[11 + CS_ECMSTORESIZE] = er->chid & 0xFF;
|
---|
273 | len = 12 + CS_ECMSTORESIZE;
|
---|
274 | if(cl->pand_send_ecm)
|
---|
275 | {
|
---|
276 | msgbuf[12 + CS_ECMSTORESIZE] = er->ecmlen >> 8;
|
---|
277 | msgbuf[13 + CS_ECMSTORESIZE] = er->ecmlen & 0xFF;
|
---|
278 | memcpy(&msgbuf[14 + CS_ECMSTORESIZE], er->ecm, er->ecmlen);
|
---|
279 | len += er->ecmlen + 2;
|
---|
280 | }
|
---|
281 | simple_crypt(msgbuf, len, cl->pand_md5_key, 16);
|
---|
282 | ret = sendto(cl->pfd, msgbuf, len, 0, (struct sockaddr *) &cl->udp_sa, cl->udp_sa_len);
|
---|
283 | return ((ret < len) ? (-1) : 0);
|
---|
284 | }
|
---|
285 |
|
---|
286 | static int pandora_recv_chk(struct s_client *UNUSED(cl), uchar *dcw, int *rc,
|
---|
287 | uchar *buf, int UNUSED(n))
|
---|
288 | {
|
---|
289 | if(buf[0] != 0x2)
|
---|
290 | { return (-1); }
|
---|
291 | *rc = 1;
|
---|
292 | memcpy(dcw, buf + 1, 16);
|
---|
293 | return (0);
|
---|
294 | }
|
---|
295 |
|
---|
296 | void module_pandora(struct s_module *ph)
|
---|
297 | {
|
---|
298 | ph->ptab.nports = 1;
|
---|
299 | ph->ptab.ports[0].s_port = cfg.pand_port;
|
---|
300 | ph->num = R_PANDORA;
|
---|
301 |
|
---|
302 | ph->desc = "pandora";
|
---|
303 | ph->type = MOD_CONN_UDP;
|
---|
304 | ph->large_ecm_support = 1;
|
---|
305 | //ph->watchdog = 1;
|
---|
306 | IP_ASSIGN(ph->s_ip, cfg.pand_srvip);
|
---|
307 | ph->s_handler = pandora_server;
|
---|
308 | ph->recv = pandora_recv;
|
---|
309 | ph->send_dcw = pandora_send_dcw;
|
---|
310 |
|
---|
311 | ph->c_init = pandora_client_init;
|
---|
312 | ph->c_recv_chk = pandora_recv_chk;
|
---|
313 | ph->c_send_ecm = pandora_send_ecm;
|
---|
314 | }
|
---|
315 |
|
---|
316 | #endif
|
---|