source: branches/monitor-improvement/oscam-config.c@ 1668

Last change on this file since 1668 was 1668, checked in by alno, 11 years ago

WebIf:

  • reject all empty token is a bad idea
File size: 64.4 KB
Line 
1#include "globals.h"
2#ifdef CS_WITH_BOXKEYS
3# include "oscam-boxkeys.np"
4#endif
5
6#define CONFVARWIDTH 20
7
8static char *cs_conf="oscam.conf";
9static char *cs_user="oscam.user";
10static char *cs_srvr="oscam.server";
11static char *cs_srid="oscam.srvid";
12static char *cs_l4ca="oscam.guess";
13static char *cs_cert="oscam.cert";
14static char *cs_sidt="oscam.services";
15//static char *cs_ird="oscam.ird";
16#ifdef CS_ANTICASC
17static char *cs_ac="oscam.ac";
18#endif
19
20static char token[4096];
21
22typedef enum cs_proto_type
23{
24 TAG_GLOBAL, // must be first !
25 TAG_MONITOR, // monitor
26 TAG_CAMD33, // camd 3.3x
27 TAG_CAMD35, // camd 3.5x UDP
28 TAG_NEWCAMD, // newcamd
29 TAG_RADEGAST, // radegast
30 TAG_SERIAL, // serial (static)
31 TAG_CS357X, // camd 3.5x UDP
32 TAG_CS378X, // camd 3.5x TCP
33#ifdef CS_WITH_GBOX
34 TAG_GBOX, // gbox
35#endif
36 TAG_CCCAM, // cccam
37 TAG_DVBAPI
38#ifdef CS_ANTICASC
39 ,TAG_ANTICASC // anti-cascading
40#endif
41} cs_proto_type_t;
42
43static char *cctag[]={"global", "monitor", "camd33", "camd35",
44 "newcamd", "radegast", "serial", "cs357x", "cs378x",
45#ifdef CS_WITH_GBOX
46 "gbox",
47#endif
48 "cccam", "dvbapi",
49#ifdef CS_ANTICASC
50 "anticasc",
51#endif
52 NULL};
53
54#ifdef DEBUG_SIDTAB
55static void show_sidtab(struct s_sidtab *sidtab)
56{
57 for (; sidtab; sidtab=sidtab->next)
58 {
59 int i;
60 char buf[1024];
61 cs_log("label=%s", sidtab->label);
62 sprintf(buf, "caid(%d)=", sidtab->num_caid);
63 for (i=0; i<sidtab->num_caid; i++)
64 sprintf(buf+strlen(buf), "%04X ", sidtab->caid[i]);
65 cs_log("%s", buf);
66 sprintf(buf, "provider(%d)=", sidtab->num_provid);
67 for (i=0; i<sidtab->num_provid; i++)
68 sprintf(buf+strlen(buf), "%08X ", sidtab->provid[i]);
69 cs_log("%s", buf);
70 sprintf(buf, "services(%d)=", sidtab->num_srvid);
71 for (i=0; i<sidtab->num_srvid; i++)
72 sprintf(buf+strlen(buf), "%04X ", sidtab->srvid[i]);
73 cs_log("%s", buf);
74 }
75}
76#endif
77
78void chk_iprange(char *value, struct s_ip **base)
79{
80 int i = 0;
81 char *ptr1, *ptr2;
82 struct s_ip *lip, *cip;
83
84 for (cip=lip=*base; cip; cip=cip->next)
85 lip=cip;
86 if (!(cip=malloc(sizeof(struct s_ip))))
87 {
88 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
89 exit(1);
90 }
91 if (*base)
92 lip->next=cip;
93 else
94 *base=cip;
95
96 memset(cip, 0, sizeof(struct s_ip));
97 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
98 {
99 if (i == 0) ++i;
100 else {
101 if (!(cip=malloc(sizeof(struct s_ip)))){
102 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
103 exit(1);
104 }
105 lip->next = cip;
106 memset(cip, 0, sizeof(struct s_ip));
107 }
108 if( (ptr2=strchr(trim(ptr1), '-')) )
109 {
110 *ptr2++='\0';
111 cip->ip[0]=cs_inet_addr(trim(ptr1));
112 cip->ip[1]=cs_inet_addr(trim(ptr2));
113 }
114 else
115 cip->ip[0]=cip->ip[1]=cs_inet_addr(ptr1);
116 lip = cip;
117 }
118}
119
120void chk_caidtab(char *caidasc, CAIDTAB *ctab)
121{
122 int i;
123 char *ptr1, *ptr2, *ptr3;
124
125 for (i = 0, ptr1 = strtok(caidasc, ","); (i < CS_MAXCAIDTAB) && (ptr1); ptr1 = strtok(NULL, ",")) {
126 ulong caid, mask, cmap;
127 if( (ptr3 = strchr(trim(ptr1), ':')) )
128 *ptr3++ = '\0';
129 else
130 ptr3 = "";
131
132 if( (ptr2 = strchr(trim(ptr1), '&')) )
133 *ptr2++ = '\0';
134 else
135 ptr2 = "";
136
137 if (((caid = a2i(ptr1, 2)) | (mask = a2i(ptr2,-2)) | (cmap = a2i(ptr3, 2))) < 0x10000) {
138 ctab->caid[i] = caid;
139 ctab->mask[i] = mask;
140 ctab->cmap[i++] = cmap;
141 }
142 }
143}
144
145void chk_tuntab(char *tunasc, TUNTAB *ttab)
146{
147 int i;
148 char *ptr1, *ptr2, *ptr3;
149
150 // ToDo: remove after using clear_tuntab() in Webif
151 for (i = 0; i < CS_MAXTUNTAB; i++) {
152 ttab->bt_caidfrom[i] = 0;
153 ttab->bt_caidto[i] = 0;
154 ttab->bt_srvid[i] = 0;
155 }
156
157 for (i = 0, ptr1 = strtok(tunasc, ","); (i < CS_MAXTUNTAB) && (ptr1); ptr1 = strtok(NULL, ",")) {
158 ulong bt_caidfrom, bt_caidto, bt_srvid;
159 if( (ptr3 = strchr(trim(ptr1), ':')) )
160 *ptr3++ = '\0';
161 else
162 ptr3 = "";
163
164 if( (ptr2 = strchr(trim(ptr1), '.')) )
165 *ptr2++ = '\0';
166 else
167 ptr2 = "";
168
169 if ((bt_caidfrom = a2i(ptr1, 2)) | (bt_srvid = a2i(ptr2,-2)) | (bt_caidto = a2i(ptr3, 2))) {
170 ttab->bt_caidfrom[i] = bt_caidfrom;
171 ttab->bt_caidto[i] = bt_caidto;
172 ttab->bt_srvid[i++] = bt_srvid;
173 }
174 }
175}
176
177void chk_services(char *labels, ulong *sidok, ulong *sidno)
178{
179 int i;
180 char *ptr;
181 SIDTAB *sidtab;
182 *sidok = *sidno = 0;
183 for (ptr=strtok(labels, ","); ptr; ptr=strtok(NULL, ",")) {
184 for (trim(ptr), i = 0, sidtab = cfg->sidtab; sidtab; sidtab = sidtab->next, i++) {
185 if (!strcmp(sidtab->label, ptr)) *sidok|=(1<<i);
186 if ((ptr[0]=='!') && (!strcmp(sidtab->label, ptr+1))) *sidno|=(1<<i);
187 }
188 }
189}
190
191void chk_ftab(char *zFilterAsc, FTAB *ftab, const char *zType, const char *zName, const char *zFiltName)
192{
193 int i,j;
194 char *ptr1,*ptr2,*ptr3;
195 char *ptr[CS_MAXFILTERS] = {0};
196
197 memset(ftab, 0, sizeof(FTAB));
198 for( i=0, ptr1=strtok(zFilterAsc, ";"); (i<CS_MAXFILTERS) && (ptr1); ptr1=strtok(NULL, ";"), i++ )
199 {
200 //cs_log("ptr1=%s", ptr1);
201 ptr[i] = ptr1;
202 if( (ptr2=strchr(trim(ptr1), ':')) )
203 {
204 //cs_log("ptr2=%s", ptr2);
205 *ptr2++='\0';
206 //cs_log("ptr2=%s", ptr2);
207 ftab->filts[i].caid = (ushort)a2i(ptr1, 4);
208 //cs_log("caid=%04X", ftab->filts[i].caid);
209 ptr[i] = ptr2;
210 }
211 else if (zFiltName && zFiltName[0]=='c')
212 {
213 cs_log("PANIC: CAID field not found in CHID parameter!");
214 cs_exit(1);
215 }
216 ftab->nfilts++;
217 }
218
219 if( ftab->nfilts ) cs_debug("%s '%s' %s filter(s):", zType, zName, zFiltName);
220 for( i=0; i<ftab->nfilts; i++ )
221 {
222 cs_debug("CAID #%d: %04X", i, ftab->filts[i].caid);
223 for( j=0, ptr3=strtok(ptr[i], ","); (j<CS_MAXPROV) && (ptr3); ptr3=strtok(NULL, ","), j++ )
224 {
225 ftab->filts[i].prids[j] = a2i(ptr3,6);
226 ftab->filts[i].nprids++;
227 cs_debug("%s #%d: %06X", zFiltName, j, ftab->filts[i].prids[j]);
228 }
229 }
230 //cs_log("exit chk_ftab");
231}
232
233void chk_cltab(char *classasc, CLASSTAB *clstab)
234{
235 int i;
236 char *ptr1;
237 for( i=0, ptr1=strtok(classasc, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ",") )
238 {
239 ptr1=trim(ptr1);
240 if( ptr1[0] == '!' )
241 clstab->bclass[clstab->bn++] = (uchar)a2i(ptr1+1, 2);
242 else
243 clstab->aclass[clstab->an++] = (uchar)a2i(ptr1, 2);
244 }
245}
246
247void chk_port_tab(char *portasc, PTAB *ptab)
248{
249 int i,j,nfilts,ifilt,iport;
250 char *ptr1,*ptr2,*ptr3;
251 char *ptr[CS_MAXPORTS] = {0};
252 int port[CS_MAXPORTS] = {0};
253 int previous_nports = ptab->nports;
254
255 for (nfilts=i=previous_nports, ptr1=strtok(portasc, ";"); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ";"), i++)
256 {
257 ptr[i] = ptr1;
258 if( (ptr2=strchr(trim(ptr1), '@')) )
259 {
260 *ptr2++='\0';
261 ptab->ports[i].s_port = atoi(ptr1);
262 ptr[i] = ptr2;
263 port[i] = ptab->ports[i].s_port;
264 ptab->nports++;
265 }
266 nfilts++;
267 }
268
269 if( nfilts==1 && strlen(portasc)<6 && ptab->ports[0].s_port == 0 ) {
270 ptab->ports[0].s_port = atoi(portasc);
271 ptab->nports = 1;
272 }
273
274 iport=ifilt = previous_nports;
275 for (i=previous_nports; i<nfilts; i++)
276 {
277 if( port[i]!=0 ) iport = i;
278 for (j=0, ptr3=strtok(ptr[i], ","); (j<CS_MAXPROV) && (ptr3); ptr3=strtok(NULL, ","), j++)
279 {
280 if( (ptr2=strchr(trim(ptr3), ':')) )
281 {
282 *ptr2++='\0';
283 ptab->ports[iport].ftab.nfilts++;
284 ifilt = ptab->ports[iport].ftab.nfilts-1;
285 ptab->ports[iport].ftab.filts[ifilt].caid = (ushort)a2i(ptr3, 4);
286 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr2, 6);
287 } else {
288 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr3, 6);
289 }
290 ptab->ports[iport].ftab.filts[ifilt].nprids++;
291 }
292 }
293}
294
295#ifdef NOTUSED
296static void chk_srvip(char *value, in_addr_t *ip)
297{
298 int i;
299 char *ptr;
300 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
301 if (i<8) ip[i++]=inet_addr(ptr);
302}
303#endif
304
305void chk_t_global(char *token, char *value)
306{
307 if (!strcmp(token, "disablelog")) { cfg->disablelog = atoi(value); return; }
308
309 if (!strcmp(token, "serverip")) {
310 if (strlen(value) == 0) {
311 return;
312 } else {
313 cfg->srvip=inet_addr(value);
314 return;
315 }
316 }
317
318 if (!strcmp(token, "logfile")) {
319 if (cfg->logfile != NULL) {
320 free(cfg->logfile);
321 cfg->logfile = NULL;
322 }
323 if (value[0])
324 asprintf(&(cfg->logfile), "%s", value);
325 return;
326 }
327 if (!strcmp(token, "pidfile")) {
328 if (cfg->pidfile != NULL) {
329 free(cfg->pidfile);
330 cfg->pidfile = NULL;
331 }
332 if (value[0])
333 asprintf(&(cfg->pidfile), "%s", value);
334 return;
335 }
336 if (!strcmp(token, "usrfile")) {
337 if (cfg->usrfile != NULL) {
338 free(cfg->usrfile);
339 cfg->usrfile = NULL;
340 }
341 if (value[0])
342 asprintf(&(cfg->usrfile), "%s", value);
343 return;
344 }
345 if (!strcmp(token, "cwlogdir")) {
346 if (cfg->cwlogdir != NULL) {
347 free(cfg->cwlogdir);
348 cfg->cwlogdir = NULL;
349 }
350 if (value[0])
351 asprintf(&(cfg->cwlogdir), "%s", value);
352 return;
353 }
354
355 if (!strcmp(token, "usrfileflag")) { cfg->usrfileflag=atoi(value); return; }
356
357 if (!strcmp(token, "clienttimeout"))
358 {
359 cfg->ctimeout = atoi(value);
360 if (cfg->ctimeout < 100)
361 cfg->ctimeout *= 1000;
362 return;
363 }
364 if (!strcmp(token, "fallbacktimeout"))
365 {
366 cfg->ftimeout = atoi(value);
367 if (cfg->ftimeout < 100)
368 cfg->ftimeout *= 1000;
369 return;
370 }
371
372 if (!strcmp(token, "clientmaxidle")) { cfg->cmaxidle=atoi(value); return; }
373 if (!strcmp(token, "cachedelay")) { cfg->delay=atoi(value); return; }
374 if (!strcmp(token, "bindwait")) { cfg->bindwait=atoi(value); return; }
375 if (!strcmp(token, "netprio")) { cfg->netprio=atoi(value); return; }
376 if (!strcmp(token, "resolvedelay")) { cfg->resolvedelay=atoi(value); return; }
377 if (!strcmp(token, "sleep")) { cfg->tosleep=atoi(value); return; }
378 if (!strcmp(token, "unlockparental")) { cfg->ulparent=atoi(value); return; }
379 if (!strcmp(token, "nice"))
380 {
381 cfg->nice=atoi(value);
382 if ((cfg->nice<-20) || (cfg->nice>20)) cfg->nice=99;
383 if (cfg->nice!=99) cs_setpriority(cfg->nice); // ignore errors
384 return;
385 }
386 if (!strcmp(token, "serialreadertimeout"))
387 {
388 if (cfg->srtimeout < 100)
389 cfg->srtimeout = atoi(value) * 1000;
390 else
391 cfg->srtimeout = atoi(value);
392 if( cfg->srtimeout <=0 )
393 cfg->srtimeout=1500;
394 return;
395 }
396 if (!strcmp(token, "maxlogsize"))
397 {
398 cfg->max_log_size=atoi(value);
399 if( cfg->max_log_size <=10 )
400 cfg->max_log_size=10;
401 return;
402 }
403 if( !strcmp(token, "waitforcards")) { cfg->waitforcards = atoi(value); return; }
404 if( !strcmp(token, "preferlocalcards")) { cfg->preferlocalcards = atoi(value); return; }
405 if (token[0] != '#')
406 fprintf(stderr, "Warning: keyword '%s' in global section not recognized\n",token);
407}
408
409#ifdef CS_ANTICASC
410void chk_t_ac(char *token, char *value)
411{
412 if (!strcmp(token, "enabled"))
413 {
414 cfg->ac_enabled=atoi(value);
415 if( cfg->ac_enabled<=0 ) cfg->ac_enabled=0;
416 else cfg->ac_enabled=1;
417 return;
418 }
419
420 if (!strcmp(token, "numusers"))
421 {
422 cfg->ac_users=atoi(value);
423 if( cfg->ac_users<0 ) cfg->ac_users=0;
424 return;
425 }
426 if (!strcmp(token, "sampletime"))
427 {
428 cfg->ac_stime=atoi(value);
429 if( cfg->ac_stime<0 ) cfg->ac_stime=2;
430 return;
431 }
432 if (!strcmp(token, "samples"))
433 {
434 cfg->ac_samples=atoi(value);
435 if( cfg->ac_samples<2 || cfg->ac_samples>10) cfg->ac_samples=10;
436 return;
437 }
438 if (!strcmp(token, "penalty"))
439 {
440 cfg->ac_penalty=atoi(value);
441 if( cfg->ac_penalty<0 ) cfg->ac_penalty=0;
442 return;
443 }
444 if (!strcmp(token, "aclogfile"))
445 {
446 cs_strncpy(cfg->ac_logfile, value, sizeof(cfg->ac_logfile));
447 return;
448 }
449 if( !strcmp(token, "fakedelay") )
450 {
451 cfg->ac_fakedelay=atoi(value);
452 if( cfg->ac_fakedelay<100 || cfg->ac_fakedelay>1000 )
453 cfg->ac_fakedelay=1000;
454 return;
455 }
456 if( !strcmp(token, "denysamples") )
457 {
458 cfg->ac_denysamples=atoi(value);
459 if( cfg->ac_denysamples<2 || cfg->ac_denysamples>cfg->ac_samples-1 )
460 cfg->ac_denysamples=cfg->ac_samples-1;
461 return;
462 }
463 if (token[0] != '#')
464 fprintf(stderr, "Warning: keyword '%s' in anticascading section not recognized\n",token);
465//#endif moved this endif up two lines, I think this was erroneous - dingo35
466}
467#endif
468
469void chk_t_monitor(char *token, char *value)
470{
471 if (!strcmp(token, "port")) { cfg->mon_port=atoi(value); return; }
472 if (!strcmp(token, "serverip")) { cfg->mon_srvip=inet_addr(value); return; }
473 if (!strcmp(token, "nocrypt")) { chk_iprange(value, &cfg->mon_allowed); return; }
474 if (!strcmp(token, "aulow")) { cfg->mon_aulow=atoi(value); return; }
475 if (!strcmp(token, "monlevel")) { cfg->mon_level=atoi(value); return; }
476 if (!strcmp(token, "httpport")) { cfg->http_port=atoi(value); return; }
477 if (!strcmp(token, "httpuser")) { cs_strncpy(cfg->http_user, value, sizeof(cfg->http_user)); return; }
478 if (!strcmp(token, "httppwd")) { cs_strncpy(cfg->http_pwd, value, sizeof(cfg->http_pwd)); return; }
479 if (!strcmp(token, "httpcss")) { cs_strncpy(cfg->http_css, value, sizeof(cfg->http_css)); return; }
480 if (!strcmp(token, "httpscript")) { cs_strncpy(cfg->http_script, value, sizeof(cfg->http_script)); return; }
481 if (!strcmp(token, "httptpl")) {
482 cs_strncpy(cfg->http_tpl, value, sizeof(cfg->http_tpl));
483 if(strlen(cfg->http_tpl) < (sizeof(cfg->http_tpl)-2) && cfg->http_tpl[strlen(cfg->http_tpl)-1] != '/'){
484 cfg->http_tpl[strlen(cfg->http_tpl)] = '/';
485 cfg->http_tpl[strlen(cfg->http_tpl)] = '\0';
486 }
487 return;
488 }
489 if (!strcmp(token, "httprefresh")) { cfg->http_refresh=atoi(value); return; }
490 if (!strcmp(token, "httphideidleclients")) { cfg->http_hide_idle_clients=atoi(value); return; }
491 if (!strcmp(token, "hideclient_to")) { cfg->mon_hideclient_to=atoi(value); return; }
492 if (token[0] != '#')
493 fprintf(stderr, "Warning: keyword '%s' in monitor section not recognized\n",token);
494}
495
496void chk_t_camd33(char *token, char *value)
497{
498 if (!strcmp(token, "port")) { cfg->c33_port=atoi(value); return; }
499 if (!strcmp(token, "serverip")) { cfg->c33_srvip=inet_addr(value); return; }
500 if (!strcmp(token, "nocrypt")) { chk_iprange(value, &cfg->c33_plain); return; }
501 if (!strcmp(token, "passive")) { cfg->c33_passive=(value[0]!='0'); return; }
502 if (!strcmp(token, "key"))
503 {
504 if (key_atob(value, cfg->c33_key))
505 {
506 fprintf(stderr, "Configuration camd3.3x: Error in Key\n");
507 exit(1);
508 }
509 cfg->c33_crypted=1;
510 return;
511 }
512 if (token[0] != '#')
513 fprintf(stderr, "Warning: keyword '%s' in camd33 section not recognized\n",token);
514}
515
516void chk_t_camd35(char *token, char *value)
517{
518 if (!strcmp(token, "port")) { cfg->c35_port=atoi(value); return; }
519 if (!strcmp(token, "serverip")) { cfg->c35_tcp_srvip=inet_addr(value); return; }
520 if (!strcmp(token, "suppresscmd08")) { cfg->c35_suppresscmd08=atoi(value); return; }
521 if (token[0] != '#')
522 fprintf(stderr, "Warning: keyword '%s' in camd35 section not recognized\n",token);
523}
524
525void chk_t_camd35_tcp(char *token, char *value)
526{
527 if (!strcmp(token, "port")) { chk_port_tab(value, &cfg->c35_tcp_ptab); return; }
528 if (!strcmp(token, "serverip")) { cfg->c35_tcp_srvip=inet_addr(value); return; }
529 if (!strcmp(token, "suppresscmd08")) { cfg->c35_suppresscmd08=atoi(value); return; }
530 if (token[0] != '#')
531 fprintf(stderr, "Warning: keyword '%s' in camd35 tcp section not recognized\n",token);
532}
533
534void chk_t_newcamd(char *token, char *value)
535{
536 if (!strcmp(token, "port")) { chk_port_tab(value, &cfg->ncd_ptab); return; }
537 if (!strcmp(token, "serverip")) { cfg->ncd_srvip=inet_addr(value); return; }
538 if (!strcmp(token, "allowed")) { chk_iprange(value, &cfg->ncd_allowed); return; }
539 if (!strcmp(token, "key"))
540 {
541 if (key_atob14(value, cfg->ncd_key))
542 {
543 fprintf(stderr, "Configuration newcamd: Error in Key\n");
544 exit(1);
545 }
546 return;
547 }
548 if (!strcmp(token, "keepalive"))
549 {
550 cfg->ncd_keepalive = atoi(value);
551 return;
552 }
553 if (token[0] != '#')
554 fprintf(stderr, "Warning: keyword '%s' in newcamd section not recognized\n",token);
555}
556
557void chk_t_cccam(char *token, char *value)
558{
559 if (!strcmp(token, "port")) { cfg->cc_port=atoi(value); return; }
560 //if (!strcmp(token, "serverip")) { cfg->cc_srvip=inet_addr(value); return; }
561 if (!strcmp(token, "reshare")) { cfg->cc_reshare=atoi(value); return; }
562 if (!strcmp(token, "version")) { // cccam version
563 if (strlen(value)>sizeof(cfg->cc_version)-1) {
564 fprintf(stderr, "cccam config: version too long\n");
565 exit(1);
566 }
567 bzero(cfg->cc_version, sizeof(cfg->cc_version));
568 strncpy((char*)cfg->cc_version, value, sizeof(cfg->cc_version)-1);
569 return;
570 }
571 if (!strcmp(token, "build")) { // cccam build number
572 if (strlen(value)>sizeof(cfg->cc_build)-1) {
573 fprintf(stderr, "cccam config build number too long\n");
574 exit(1);
575 }
576 bzero(cfg->cc_build, sizeof(cfg->cc_build));
577 strncpy((char*)cfg->cc_build, value, sizeof(cfg->cc_build)-1);
578 return;
579 }
580 if (token[0] != '#')
581 fprintf(stderr, "Warning: keyword '%s' in cccam section not recognized\n",token);
582}
583
584void chk_t_radegast(char *token, char *value)
585{
586 if (!strcmp(token, "port")) { cfg->rad_port=atoi(value); return; }
587 if (!strcmp(token, "serverip")) { cfg->rad_srvip=inet_addr(value); return; }
588 if (!strcmp(token, "allowed")) { chk_iprange(value, &cfg->rad_allowed); return; }
589 if (!strcmp(token, "user")) { cs_strncpy(cfg->rad_usr, value, sizeof(cfg->rad_usr)); return; }
590 if (token[0] != '#')
591 fprintf(stderr, "Warning: keyword '%s' in radegast section not recognized\n",token);
592}
593
594void chk_t_serial(char *token, char *value)
595{
596 if (!strcmp(token, "device"))
597 {
598 int l;
599 l=strlen(cfg->ser_device);
600 if (l) cfg->ser_device[l++]=1; // use ctrl-a as delimiter
601 cs_strncpy(cfg->ser_device+l, value, sizeof(cfg->ser_device)-l);
602 return;
603 }
604 if (token[0] != '#')
605 fprintf(stderr, "Warning: keyword '%s' in serial section not recognized\n",token);
606}
607
608#ifdef CS_WITH_GBOX
609static void chk_t_gbox(char *token, char *value)
610{
611// if (!strcmp(token, "password")) strncpy(cfg->gbox_pwd, i2b(4, a2i(value, 4)), 4);
612 if (!strcmp(token, "password")) { cs_atob(cfg->gbox_pwd, value, 4); return; }
613 if (!strcmp(token, "maxdist")) { cfg->maxdist=atoi(value); return; }
614 if (!strcmp(token, "ignorelist")) { cs_strncpy((char *)cfg->ignorefile, value, sizeof(cfg->ignorefile)); return; }
615 if (!strcmp(token, "onlineinfos")) { cs_strncpy((char *)cfg->gbxShareOnl, value, sizeof(cfg->gbxShareOnl)); return; }
616 if (!strcmp(token, "cardinfos")) { cs_strncpy((char *)cfg->cardfile, value, sizeof(cfg->cardfile)); return; }
617 if (!strcmp(token, "locals"))
618 {
619 char *ptr1;
620 int n=0, i;
621 for (i=0, ptr1=strtok(value, ","); (i<CS_MAXLOCALS) && (ptr1); ptr1=strtok(NULL, ","))
622 {
623 cfg->locals[n++]=a2i(ptr1, 8);
624 //printf("%i %08X",n,cfg->locals[n-1]);
625 }
626 cfg->num_locals=n;
627 return;
628 }
629 if (token[0] != '#')
630 fprintf(stderr, "Warning: keyword '%s' in gbox section not recognized\n",token);
631}
632#endif
633
634#ifdef HAVE_DVBAPI
635void chk_t_dvbapi(char *token, char *value)
636{
637 if (!strcmp(token, "enabled")) { cfg->dvbapi_enabled=atoi(value); return; }
638 if (!strcmp(token, "au")) { cfg->dvbapi_au=atoi(value); return; }
639 if (!strcmp(token, "boxtype")) { cs_strncpy(cfg->dvbapi_boxtype, value, sizeof(cfg->dvbapi_boxtype)); return; }
640 if (!strcmp(token, "user")) { cs_strncpy(cfg->dvbapi_usr, value, sizeof(cfg->dvbapi_usr)); return; }
641 if (!strcmp(token, "priority")) { cs_strncpy(cfg->dvbapi_priority, value, sizeof(cfg->dvbapi_priority)); return; }
642 if (!strcmp(token, "ignore")) { cs_strncpy(cfg->dvbapi_ignore, value, sizeof(cfg->dvbapi_ignore)); return; }
643
644 if (token[0] != '#')
645 fprintf(stderr, "Warning: keyword '%s' in dvbapi section not recognized\n",token);
646}
647#endif
648
649static void chk_token(char *token, char *value, int tag)
650{
651 switch(tag)
652 {
653 case TAG_GLOBAL : chk_t_global(token, value); break;
654 case TAG_MONITOR : chk_t_monitor(token, value); break;
655 case TAG_CAMD33 : chk_t_camd33(token, value); break;
656 case TAG_CAMD35 :
657 case TAG_CS357X : chk_t_camd35(token, value); break;
658 case TAG_NEWCAMD : chk_t_newcamd(token, value); break;
659 case TAG_RADEGAST: chk_t_radegast(token, value); break;
660 case TAG_SERIAL : chk_t_serial(token, value); break;
661 case TAG_CS378X : chk_t_camd35_tcp(token, value); break;
662#ifdef CS_WITH_GBOX
663 case TAG_GBOX : chk_t_gbox(token, value); break;
664#endif
665 case TAG_CCCAM : chk_t_cccam(token, value); break;
666#ifdef HAVE_DVBAPI
667 case TAG_DVBAPI : chk_t_dvbapi(token, value); break;
668#else
669 case TAG_DVBAPI : fprintf(stderr, "Warning: OSCam compiled without DVB API support.\n"); break;
670#endif
671#ifdef CS_ANTICASC
672 case TAG_ANTICASC: chk_t_ac(token, value); break;
673#endif
674 }
675}
676
677void init_len4caid()
678{
679 int nr;
680 FILE *fp;
681 char *value;
682
683 memset(len4caid, 0, sizeof(ushort)<<8);
684 sprintf(token, "%s%s", cs_confdir, cs_l4ca);
685 if (!(fp=fopen(token, "r")))
686 return;
687 for(nr=0; fgets(token, sizeof(token), fp);)
688 {
689 int i, c;
690 char *ptr;
691 if (!(value=strchr(token, ':'))) continue;
692 *value++='\0';
693 if( (ptr=strchr(value, '#')) )
694 *ptr='\0';
695 if (strlen(trim(token))!=2) continue;
696 if (strlen(trim(value))!=4) continue;
697 if ((i=byte_atob(token))<0) continue;
698 if ((c=word_atob(value))<0) continue;
699//printf("idx %02X = %04X\n", i, c); fflush(stdout);
700 len4caid[i]=c;
701 nr++;
702 }
703 fclose(fp);
704 cs_log("%d lengths for caid guessing loaded", nr);
705 return;
706}
707
708int search_boxkey(ushort caid, char *key)
709{
710 int i, rc=0;
711 FILE *fp;
712 char c_caid[512];
713
714 sprintf(c_caid, "%s%s", cs_confdir, cs_cert);
715 fp=fopen(c_caid, "r");
716 if (fp)
717 {
718 for (; (!rc) && fgets(c_caid, sizeof(c_caid), fp);)
719 {
720 char *c_provid, *c_key;
721
722 c_provid=strchr(c_caid, '#');
723 if (c_provid) *c_provid='\0';
724 if (!(c_provid=strchr(c_caid, ':'))) continue;
725 *c_provid++='\0';
726 if (!(c_key=strchr(c_provid, ':'))) continue;
727 *c_key++='\0';
728 if (word_atob(trim(c_caid))!=caid) continue;
729 if ((i=(strlen(trim(c_key))>>1))>256) continue;
730 if (cs_atob((uchar *)key, c_key, i)<0)
731 {
732 cs_log("wrong key in \"%s\"", cs_cert);
733 continue;
734 }
735 rc=1;
736 }
737 fclose(fp);
738 }
739#ifdef OSCAM_INBUILD_KEYS
740 for(i=0; (!rc) && (npkey[i].keylen); i++)
741 if (rc=((caid==npkey[i].caid) && (npkey[i].provid==0)))
742 memcpy(key, npkey[i].key, npkey[i].keylen);
743#endif
744 return(rc);
745}
746
747int init_config()
748{
749 int tag=TAG_GLOBAL;
750 FILE *fp;
751 char *value;
752
753#ifndef CS_EMBEDDED
754#ifdef PRIO_PROCESS
755 errno=0;
756 if ((cfg->nice=getpriority(PRIO_PROCESS, 0))==(-1))
757 if (errno)
758#endif
759#endif
760 cfg->nice=99;
761 cfg->ctimeout=CS_CLIENT_TIMEOUT;
762 cfg->ftimeout=CS_CLIENT_TIMEOUT / 2;
763 cfg->cmaxidle=CS_CLIENT_MAXIDLE;
764 cfg->delay=CS_DELAY;
765 cfg->bindwait=CS_BIND_TIMEOUT;
766 cfg->resolvedelay=CS_RESOLVE_DELAY;
767 cfg->mon_level=2;
768 cfg->mon_hideclient_to=0;
769 cfg->srtimeout=1500;
770 cfg->ulparent=0;
771 cfg->logfile=NULL;
772 cfg->pidfile=NULL;
773 cfg->usrfile=NULL;
774 cfg->cwlogdir=NULL;
775 strcpy(cfg->http_user, "");
776 strcpy(cfg->http_pwd, "");
777 strcpy(cfg->http_css, "");
778 cfg->http_refresh=0;
779 cfg->http_hide_idle_clients=0;
780 strcpy(cfg->http_tpl, "");
781#ifdef CS_ANTICASC
782 cfg->ac_enabled=0;
783 cfg->ac_users=0;
784 cfg->ac_stime=2;
785 cfg->ac_samples=10;
786 cfg->ac_denysamples=8;
787 cfg->ac_fakedelay=1000;
788 strcpy(cfg->ac_logfile, "./oscam_ac.log");
789#endif
790 cfg->ncd_keepalive=1;
791 sprintf(token, "%s%s", cs_confdir, cs_conf);
792 if (!(fp=fopen(token, "r")))
793 {
794 fprintf(stderr, "Cannot open config file '%s' (errno=%d)\n", token, errno);
795 exit(1);
796 }
797 while (fgets(token, sizeof(token), fp))
798 {
799 int i, l;
800 //void *ptr;
801 if ((l=strlen(trim(token)))<3) continue;
802 if ((token[0]=='[') && (token[l-1]==']'))
803 {
804 for (token[l-1]=0, tag=-1, i=TAG_GLOBAL; cctag[i]; i++)
805 if (!strcmp(cctag[i], strtolower(token+1)))
806 tag=i;
807 continue;
808 }
809 if (!(value=strchr(token, '='))) continue;
810 *value++='\0';
811 chk_token(trim(strtolower(token)), trim(value), tag);
812 }
813 fclose(fp);
814#ifdef CS_LOGFILE
815 if (cfg->logfile == NULL)
816 asprintf(&(cfg->logfile), "%s", CS_LOGFILE);
817#endif
818 cs_init_log(cfg->logfile);
819 if (cfg->ftimeout>=cfg->ctimeout)
820 {
821 cfg->ftimeout = cfg->ctimeout - 100;
822 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be smaller than clienttimeout (%lu ms))", cfg->ftimeout, cfg->ctimeout);
823 }
824 if(cfg->ftimeout < cfg->srtimeout)
825 {
826 cfg->ftimeout = cfg->srtimeout + 100;
827 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ftimeout, cfg->srtimeout);
828 }
829 if(cfg->ctimeout < cfg->srtimeout)
830 {
831 cfg->ctimeout = cfg->srtimeout + 100;
832 cs_log("WARNING: clienttimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ctimeout, cfg->srtimeout);
833 }
834#ifdef CS_ANTICASC
835 if( cfg->ac_denysamples+1>cfg->ac_samples )
836 {
837 cfg->ac_denysamples=cfg->ac_samples-1;
838 cs_log("WARNING: DenySamples adjusted to %d", cfg->ac_denysamples);
839 }
840#endif
841 return 0;
842}
843
844void chk_account(char *token, char *value, struct s_auth *account)
845{
846 int i;
847 char *ptr1;//, *ptr2;
848
849 if (!strcmp(token, "user")) { cs_strncpy(account->usr, value, sizeof(account->usr)); return; }
850 if (!strcmp(token, "pwd")) { cs_strncpy(account->pwd, value, sizeof(account->pwd)); return; }
851 if (!strcmp(token, "hostname")) { cs_strncpy((char *)account->dyndns, value, sizeof(account->dyndns));return; }
852 if (!strcmp(token, "betatunnel")) { chk_tuntab(value, &account->ttab); return; }
853 if (!strcmp(token, "uniq")) { account->uniq=atoi(value); return; }
854 if (!strcmp(token, "sleep")) { account->tosleep=atoi(value); return; }
855 if (!strcmp(token, "monlevel")) { account->monlvl=atoi(value); return; }
856 if (!strcmp(token, "caid")) { chk_caidtab(value, &account->ctab); return; }
857 if (!strcmp(token, "disabled")) { account->disabled=atoi(value); return; }
858 if (!strcmp(token, "suppresscmd08")) { account->c35_suppresscmd08=atoi(value); return; }
859 if (!strcmp(token, "keepalive"))
860 {
861 account->ncd_keepalive = atoi(value);
862 return;
863 }
864 /*
865 * case insensitive
866 */
867 strtolower(value);
868 if (!strcmp(token, "au"))
869 {
870 //set default values for usage during runtime from Webif
871 account->au=-1;
872 account->autoau=0;
873
874 if(value && value[0]=='1') account->autoau=1;
875 for (i=0; i<CS_MAXREADER; i++)
876 if ((reader[i].label[0]) &&
877 (!strncmp(reader[i].label, value, strlen(reader[i].label))))
878 account->au=i;
879 return;
880 }
881 if (!strcmp(token, "group")) {
882 account->grp = 0;
883 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
884 {
885 int g;
886 g=atoi(ptr1);
887 if ((g>0) && (g<33)) account->grp|=(1<<(g-1));
888 }
889 return;
890 }
891 if(!strcmp(token, "services")) { chk_services(value, &account->sidtabok, &account->sidtabno); return; }
892 if(!strcmp(token, "ident")) { chk_ftab(value, &account->ftab, "user", account->usr, "provid"); return; }
893 if(!strcmp(token, "class")) { chk_cltab(value, &account->cltab); return; }
894 if(!strcmp(token, "chid")) { chk_ftab(value, &account->fchid, "user", account->usr, "chid"); return; }
895
896 if (!strcmp(token, "expdate"))
897 {
898 if (!value[0]) {
899 account->expirationdate=(time_t)NULL;
900 return;
901 }
902 struct tm cstime;
903 memset(&cstime,0,sizeof(cstime));
904 for (i=0, ptr1=strtok(value, "-/"); (i<3)&&(ptr1); ptr1=strtok(NULL, "-/"), i++)
905 {
906 switch(i)
907 {
908 case 0: cstime.tm_year=atoi(ptr1)-1900; break;
909 case 1: cstime.tm_mon =atoi(ptr1)-1; break;
910 case 2: cstime.tm_mday=atoi(ptr1); break;
911 }
912 }
913 account->expirationdate=mktime(&cstime);
914 return;
915 }
916
917#ifdef CS_ANTICASC
918 if( !strcmp(token, "numusers") )
919 {
920 account->ac_users = atoi(value);
921 return;
922 }
923 if( !strcmp(token, "penalty") )
924 {
925 account->ac_penalty = atoi(value);
926 return;
927 }
928#endif
929 if (token[0] != '#')
930 fprintf(stderr, "Warning: keyword '%s' in account section not recognized\n",token);
931
932// if (!strcmp(token, "caid"))
933// {
934// for (i=0, ptr1=strtok(value, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
935// {
936// ulong caid, mask;
937// if (ptr2=strchr(trim(ptr1), '&'))
938// *ptr2++='\0';
939// else
940// ptr2="";
941// if (((caid=a2i(ptr1, 2))|(mask=a2i(ptr2,-2))) < 0x10000)
942// {
943// account->caidtab[i][0]=caid;
944// account->caidtab[i++][1]=mask;
945// }
946// else
947// cs_log("WARNING: wrong CAID in %s -> ignored", cs_user);
948// }
949// }
950}
951
952int write_services()
953{
954 int i;
955 FILE *f;
956 struct s_sidtab *sidtab = cfg->sidtab;
957 char tmpfile[256];
958 char destfile[256];
959 char bakfile[256];
960
961 snprintf(destfile, 255,"%s%s", cs_confdir, cs_sidt);
962 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_sidt);
963 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_sidt);
964
965 if (!(f=fopen(tmpfile, "w"))){
966 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
967 return(1);
968 }
969 fprintf(f,"#oscam.services generated automatically\n\n");
970
971 while(sidtab != NULL){
972 fprintf(f,"[%s]\n", sidtab->label);
973 fprintf_conf(f, CONFVARWIDTH, "caid", "");
974 for (i=0; i<sidtab->num_caid; i++){
975 if (i==0) fprintf(f,"%04X", sidtab->caid[i]);
976 else fprintf(f,",%04X", sidtab->caid[i]);
977 }
978 fputc((int)'\n', f);
979 fprintf_conf(f, CONFVARWIDTH, "provid", "");
980 for (i=0; i<sidtab->num_provid; i++){
981 if (i==0) fprintf(f,"%08lX", sidtab->provid[i]);
982 else fprintf(f,",%08lX", sidtab->provid[i]);
983 }
984 fputc((int)'\n', f);
985 fprintf_conf(f, CONFVARWIDTH, "srvid", "");
986 for (i=0; i<sidtab->num_srvid; i++){
987 if (i==0) fprintf(f,"%04X", sidtab->srvid[i]);
988 else fprintf(f,",%04X", sidtab->srvid[i]);
989 }
990 fprintf(f,"\n\n");
991 sidtab=sidtab->next;
992 }
993
994 fclose(f);
995 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
996}
997
998int write_config()
999{
1000 int i,j;
1001 FILE *f;
1002 char *dot = "", *dot1 = "", *dot2 = ""; //flags for delimiters
1003 char tmpfile[256];
1004 char destfile[256];
1005 char bakfile[256];
1006
1007 snprintf(destfile, 255,"%s%s", cs_confdir, cs_conf);
1008 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_conf);
1009 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_conf);
1010
1011 if (!(f=fopen(tmpfile, "w"))){
1012 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
1013 return(1);
1014 }
1015 fprintf(f,"#oscam.config generated automatically\n\n");
1016
1017 /*global settings*/
1018 fprintf(f,"[global]\n");
1019 if (cfg->srvip != 0)
1020 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->srvip));
1021 if (cfg->pidfile != NULL) fprintf_conf(f, CONFVARWIDTH, "pidfile", "%s\n", cfg->pidfile);
1022 if (cfg->usrfile != NULL) fprintf_conf(f, CONFVARWIDTH, "usrfile", "%s\n", cfg->usrfile);
1023 if (cfg->logfile != NULL) fprintf_conf(f, CONFVARWIDTH, "logfile", "%s\n", cfg->logfile);
1024 if (cfg->cwlogdir != NULL) fprintf_conf(f, CONFVARWIDTH, "cwlogdir", "%s\n", cfg->cwlogdir);
1025 fprintf_conf(f, CONFVARWIDTH, "usrfileflag", "%d\n", cfg->usrfileflag);
1026 fprintf_conf(f, CONFVARWIDTH, "clienttimeout", "%ld\n", cfg->ctimeout/1000);
1027 fprintf_conf(f, CONFVARWIDTH, "fallbacktimeout", "%ld\n", cfg->ftimeout/1000);
1028 fprintf_conf(f, CONFVARWIDTH, "clientmaxidle", "%d\n", cfg->cmaxidle);
1029 fprintf_conf(f, CONFVARWIDTH, "cachedelay", "%ld\n", cfg->delay);
1030 fprintf_conf(f, CONFVARWIDTH, "bindwait", "%d\n", cfg->bindwait);
1031 fprintf_conf(f, CONFVARWIDTH, "netprio", "%ld\n", cfg->netprio);
1032 fprintf_conf(f, CONFVARWIDTH, "resolvedelay", "%d\n", cfg->resolvedelay);
1033 if (cfg->tosleep) fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", cfg->tosleep);
1034 fprintf_conf(f, CONFVARWIDTH, "unlockparental", "%d\n", cfg->ulparent);
1035 fprintf_conf(f, CONFVARWIDTH, "nice", "%d\n", cfg->nice);
1036 fprintf_conf(f, CONFVARWIDTH, "serialreadertimeout", "%d\n", cfg->srtimeout);
1037 fprintf_conf(f, CONFVARWIDTH, "maxlogsize", "%d\n", cfg->max_log_size);
1038 fprintf_conf(f, CONFVARWIDTH, "waitforcards", "%d\n", cfg->waitforcards);
1039 fprintf_conf(f, CONFVARWIDTH, "preferlocalcards", "%d\n", cfg->preferlocalcards);
1040 fputc((int)'\n', f);
1041
1042 /*monitor settings*/
1043 fprintf(f,"[monitor]\n");
1044 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->mon_port);
1045 if (cfg->mon_srvip != 0)
1046 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
1047
1048 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
1049 struct s_ip *cip;
1050 for (cip = cfg->mon_allowed; cip; cip = cip->next){
1051 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1052 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1053 dot=",";
1054 }
1055 fputc((int)'\n', f);
1056 fprintf_conf(f, CONFVARWIDTH, "aulow", "%d\n", cfg->mon_aulow);
1057 fprintf_conf(f, CONFVARWIDTH, "hideclient_to", "%d\n", cfg->mon_hideclient_to);
1058 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", cfg->mon_level);
1059 fprintf_conf(f, CONFVARWIDTH, "httpport", "%d\n", cfg->http_port);
1060 fprintf_conf(f, CONFVARWIDTH, "httpuser", "%s\n", cfg->http_user);
1061 fprintf_conf(f, CONFVARWIDTH, "httppwd", "%s\n", cfg->http_pwd);
1062 fprintf_conf(f, CONFVARWIDTH, "httpcss", "%s\n", cfg->http_css);
1063 fprintf_conf(f, CONFVARWIDTH, "httpscript", "%s\n", cfg->http_script);
1064 fprintf_conf(f, CONFVARWIDTH, "httprefresh", "%d\n", cfg->http_refresh);
1065 fprintf_conf(f, CONFVARWIDTH, "httphideidleclients", "%d\n", cfg->http_hide_idle_clients);
1066 fputc((int)'\n', f);
1067
1068 /*newcamd*/
1069 if ((cfg->ncd_ptab.nports > 0) && (cfg->ncd_ptab.ports[0].s_port > 0)){
1070 fprintf(f,"[newcamd]\n");
1071 fprintf_conf(f, CONFVARWIDTH, "port", "");
1072 dot1 = "";
1073 for(i = 0; i < cfg->ncd_ptab.nports; ++i){
1074 fprintf(f,"%s%d@%04X", dot1, cfg->ncd_ptab.ports[i].s_port, cfg->ncd_ptab.ports[i].ftab.filts[0].caid);
1075 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0){
1076 fprintf(f,":");
1077 dot2 = "";
1078 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j){
1079 fprintf(f,"%s%06X", dot2, (int)cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
1080 dot2 = ",";
1081 }
1082 }
1083 dot1=";";
1084 }
1085
1086 fputc((int)'\n', f);
1087 if (cfg->ncd_srvip != 0)
1088 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
1089 fprintf_conf(f, CONFVARWIDTH, "key", "");
1090 for (i=0;i<14;i++) fprintf(f,"%02X", cfg->ncd_key[i]);
1091 fprintf(f,"\n");
1092 fprintf_conf(f, CONFVARWIDTH, "allowed", "");
1093 struct s_ip *cip;
1094 dot="";
1095 for (cip = cfg->ncd_allowed; cip; cip = cip->next){
1096 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1097 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1098 dot=",";
1099 }
1100 fprintf(f,"\n");
1101 fprintf_conf(f, CONFVARWIDTH, "keepalive", "%d\n", cfg->ncd_keepalive);
1102 fprintf(f,"\n");
1103 }
1104
1105 /*camd3.3*/
1106 if ( cfg->c33_port > 0) {
1107 fprintf(f,"[camd33]\n");
1108 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c33_port);
1109 if (cfg->c33_srvip != 0)
1110 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
1111 fprintf_conf(f, CONFVARWIDTH, "passive", "%d\n", cfg->c33_passive);
1112 fprintf_conf(f, CONFVARWIDTH, "key", ""); for (i = 0; i < (int) sizeof(cfg->c33_key); ++i) fprintf(f,"%02X", cfg->c33_key[i]); fputc((int)'\n', f);
1113 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
1114 dot="";
1115 for (cip = cfg->c33_plain; cip; cip = cip->next){
1116 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1117 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1118 dot=",";
1119 }
1120 fprintf(f,"\n\n");
1121 }
1122
1123 /*camd3.5*/
1124 if ( cfg->c35_port > 0) {
1125 fprintf(f,"[cs357x]\n");
1126 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c35_port);
1127 if (cfg->c35_tcp_srvip != 0)
1128 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1129 if (cfg->c35_suppresscmd08)
1130 fprintf_conf(f, CONFVARWIDTH, "suppresscmd08", "%d\n", cfg->c35_suppresscmd08);
1131 fprintf(f,"\n");
1132 }
1133
1134 /*camd3.5 TCP*/
1135 if ((cfg->c35_tcp_ptab.nports > 0) && (cfg->c35_tcp_ptab.ports[0].s_port > 0)) {
1136 fprintf(f,"[cs378x]\n");
1137 fprintf_conf(f, CONFVARWIDTH, "port", "");
1138 dot1 = "";
1139 for(i = 0; i < cfg->c35_tcp_ptab.nports; ++i){
1140 fprintf(f,"%s%d@%04X", dot1, cfg->c35_tcp_ptab.ports[i].s_port, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].caid);
1141 if (cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids > 0){
1142 fprintf(f,":");
1143 dot2 = "";
1144 for (j = 0; j < cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids; ++j){
1145 fprintf(f,"%s%lX", dot2, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].prids[j]);
1146 dot2 = ",";
1147 }
1148 }
1149 dot1=";";
1150 }
1151
1152 fputc((int)'\n', f);
1153 if (cfg->c35_tcp_srvip != 0)
1154 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1155 fputc((int)'\n', f);
1156 }
1157
1158 /*Radegast*/
1159 if ( cfg->rad_port > 0) {
1160 fprintf(f,"[radegast]\n");
1161 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->rad_port);
1162 if (cfg->rad_srvip != 0)
1163 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
1164 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->rad_usr);
1165 fprintf_conf(f, CONFVARWIDTH, "allowed", "");
1166 struct s_ip *cip;
1167 for (cip = cfg->rad_allowed; cip; cip = cip->next){
1168 fprintf(f,"%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
1169 if (cip->ip[0] == cip->ip[1]) fprintf(f,"-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
1170 dot=",";
1171 }
1172 fprintf(f,"\n\n");
1173 }
1174
1175#ifdef CS_WITH_GBOX
1176 /*Gbox*/
1177 if ((cfg->gbox_pwd[0] > 0) || (cfg->gbox_pwd[1] > 0) || (cfg->gbox_pwd[2] > 0) || (cfg->gbox_pwd[3] > 0)){
1178 fprintf(f,"[gbox]\n");
1179 fprintf_conf(f, CONFVARWIDTH, "password", ""); for (i=0;i<4;i++) fprintf(f,"%02X", cfg->gbox_pwd[i]); fputc((int)'\n', f);;
1180 fprintf_conf(f, CONFVARWIDTH, "maxdist", "%d\n", cfg->maxdist);
1181 fprintf_conf(f, CONFVARWIDTH, "ignorelist", "%s\n", cfg->ignorefile);
1182 fprintf_conf(f, CONFVARWIDTH, "onlineinfos", "%s\n", cfg->gbxShareOnl);
1183 fprintf_conf(f, CONFVARWIDTH, "cardinfos", "%s\n", cfg->cardfile);
1184 fprintf_conf(f, CONFVARWIDTH, "locals", "");
1185 char *dot = "";
1186 for (i = 0; i < cfg->num_locals; i++){
1187 fprintf(f,"%s%06lX", dot, cfg->locals[i]);
1188 dot=";";
1189 }
1190 fprintf(f,"\n\n");
1191 }
1192#endif
1193
1194 /*serial*/
1195
1196
1197 /*cccam*/
1198 if ( cfg->cc_port > 0) {
1199 fprintf(f,"[cccam]\n");
1200 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->cc_port);
1201 fprintf_conf(f, CONFVARWIDTH, "reshare", "%d\n", cfg->cc_reshare);
1202 fprintf_conf(f, CONFVARWIDTH, "version", "%s\n", cfg->cc_version);
1203 fprintf_conf(f, CONFVARWIDTH, "build", "%s\n", cfg->cc_build);
1204 fprintf(f,"\n");
1205 }
1206
1207#ifdef HAVE_DVBAPI
1208 /*dvb-api*/
1209 if (cfg->dvbapi_enabled > 0) {
1210 fprintf(f,"[dvbapi]\n");
1211 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->dvbapi_enabled);
1212 fprintf_conf(f, CONFVARWIDTH, "au", "%d\n", cfg->dvbapi_au);
1213 fprintf_conf(f, CONFVARWIDTH, "boxtype", "%s\n", cfg->dvbapi_boxtype);
1214 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->dvbapi_usr);
1215 fputc((int)'\n', f);
1216 }
1217#endif
1218
1219#ifdef CS_ANTICASC
1220 fprintf(f,"[anticasc]\n");
1221 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->ac_enabled);
1222 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", cfg->ac_users);
1223 fprintf_conf(f, CONFVARWIDTH, "sampletime", "%d\n", cfg->ac_stime);
1224 fprintf_conf(f, CONFVARWIDTH, "samples", "%d\n", cfg->ac_samples);
1225 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", cfg->ac_penalty);
1226 fprintf_conf(f, CONFVARWIDTH, "aclogfile", "%s\n", cfg->ac_logfile);
1227 fprintf_conf(f, CONFVARWIDTH, "denysamples", "%d\n", cfg->ac_denysamples);
1228 fprintf_conf(f, CONFVARWIDTH, "fakedelay", "%d\n", cfg->ac_fakedelay);
1229 fputc((int)'\n', f);
1230#endif
1231
1232 fclose(f);
1233
1234 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1235}
1236
1237int write_userdb()
1238{
1239 int i;
1240 FILE *f;
1241 struct s_auth *account;
1242 char *dot = ""; //flag for comma
1243 char tmpfile[256];
1244 char destfile[256];
1245 char bakfile[256];
1246
1247 snprintf(destfile, 255,"%s%s", cs_confdir, cs_user);
1248 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_user);
1249 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_user);
1250
1251 if (!(f=fopen(tmpfile, "w"))){
1252 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
1253 return(1);
1254 }
1255 fprintf(f,"#oscam.user generated automatically\n\n");
1256
1257 //each account
1258 for (account=cfg->account; (account) ; account=account->next){
1259 fprintf(f,"[account]\n");
1260 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", account->usr);
1261 fprintf_conf(f, CONFVARWIDTH, "pwd", "%s\n", account->pwd);
1262 fprintf_conf(f, CONFVARWIDTH, "disabled", "%d\n", account->disabled);
1263 struct tm * timeinfo = localtime (&account->expirationdate);
1264 char buf [80];
1265 strftime (buf,80,"%Y-%m-%d",timeinfo);
1266 if(strcmp(buf,"1970-01-01"))
1267 fprintf_conf(f, CONFVARWIDTH, "expdate", "%s\n", buf);
1268 else
1269 fprintf_conf(f, CONFVARWIDTH, "expdate", "\n");
1270
1271 //group
1272 char *value = mk_t_group((ulong*)account->grp);
1273 fprintf_conf(f, CONFVARWIDTH, "group", "%s\n", value);
1274 free(value);
1275
1276 fprintf_conf(f, CONFVARWIDTH, "hostname", "%s\n", account->dyndns);
1277 fprintf_conf(f, CONFVARWIDTH, "uniq", "%d\n", account->uniq);
1278 fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", account->tosleep);
1279 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", account->monlvl);
1280
1281 if (account->au > -1)
1282 if (account->au < CS_MAXREADER)
1283 fprintf_conf(f, CONFVARWIDTH, "au", "%s\n", reader[account->au].label);
1284 if (account->autoau == 1) fprintf_conf(f, CONFVARWIDTH, "au", "1\n");
1285
1286 fprintf_conf(f, CONFVARWIDTH, "services", "");
1287 char sidok[33]; long2bitchar(account->sidtabok,sidok);
1288 char sidno[33]; long2bitchar(account->sidtabno,sidno);
1289 struct s_sidtab *sidtab = cfg->sidtab;
1290 i=0; dot = "";
1291 for (; sidtab; sidtab=sidtab->next){
1292 if(sidok[i]=='1') {fprintf(f,"%s%s", dot, sidtab->label); dot = ",";}
1293 if(sidno[i]=='1') {fprintf(f,"%s!%s", dot, sidtab->label); dot = ",";}
1294 i++;
1295 }
1296 fputc((int)'\n', f);
1297
1298 //CAID
1299 value = mk_t_caidtab(&account->ctab);
1300 fprintf_conf(f, CONFVARWIDTH, "caid", "%s\n", value);
1301 free(value);
1302
1303 //betatunnel
1304 value = mk_t_tuntab(&account->ttab);
1305 fprintf_conf(f, CONFVARWIDTH, "betatunnel", "%s\n", value);
1306 free(value);
1307
1308 //ident
1309 value = mk_t_ftab(&account->ftab);
1310 fprintf_conf(f, CONFVARWIDTH, "ident", "%s\n", value);
1311 free(value);
1312
1313 if (account->c35_suppresscmd08)
1314 fprintf_conf(f, CONFVARWIDTH, "suppresscmd08", "%d\n", account->c35_suppresscmd08);
1315
1316 fprintf_conf(f, CONFVARWIDTH, "keepalive", "%d\n", account->ncd_keepalive);
1317
1318#ifdef CS_ANTICASC
1319 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", account->ac_users);
1320 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", account->ac_penalty);
1321#endif
1322 fputc((int)'\n', f);
1323 }
1324 fclose(f);
1325
1326 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1327}
1328
1329int init_userdb()
1330{
1331 int tag = 0, nr, nro, expired, disabled;
1332 //int first=1;
1333 FILE *fp;
1334 char *value;
1335 struct s_auth *ptr;
1336 /*static */struct s_auth *account=(struct s_auth *)0;
1337
1338 sprintf(token, "%s%s", cs_confdir, cs_user);
1339 if (!(fp = fopen(token, "r"))) {
1340 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1341 return(1);
1342 }
1343
1344 for (nro = 0, ptr = cfg->account; ptr; nro++) {
1345 struct s_auth *ptr_next;
1346 ptr_next = ptr->next;
1347 free(ptr);
1348 ptr = ptr_next;
1349 }
1350 nr = 0;
1351
1352 while (fgets(token, sizeof(token), fp)) {
1353 int i, l;
1354 void *ptr;
1355
1356 if ((l=strlen(trim(token))) < 3)
1357 continue;
1358
1359 if ((token[0] == '[') && (token[l-1] == ']')) {
1360 token[l - 1] = 0;
1361 tag = (!strcmp("account", strtolower(token + 1)));
1362
1363 if (!(ptr=malloc(sizeof(struct s_auth)))) {
1364 cs_log("Error allocating memory (errno=%d)", errno);
1365 return(1);
1366 }
1367
1368 if (account)
1369 account->next = ptr;
1370 else
1371 cfg->account = ptr;
1372
1373 account = ptr;
1374 memset(account, 0, sizeof(struct s_auth));
1375 account->au = (-1);
1376 account->monlvl = cfg->mon_level;
1377 account->tosleep = cfg->tosleep;
1378 account->c35_suppresscmd08 = cfg->c35_suppresscmd08;
1379 account->ncd_keepalive = cfg->ncd_keepalive;
1380 for (i = 1; i < CS_MAXCAIDTAB; account->ctab.mask[i++] = 0xffff);
1381 for (i = 1; i < CS_MAXTUNTAB; account->ttab.bt_srvid[i++] = 0x0000);
1382 nr++;
1383
1384#ifdef CS_ANTICASC
1385 account->ac_users = cfg->ac_users;
1386 account->ac_penalty = cfg->ac_penalty;
1387 account->ac_idx = nr;
1388#endif
1389 continue;
1390 }
1391
1392 if (!tag)
1393 continue;
1394
1395 if (!(value=strchr(token, '=')))
1396 continue;
1397
1398 *value++ = '\0';
1399 chk_account(trim(strtolower(token)), trim(value), account);
1400 }
1401
1402 fclose(fp);
1403
1404 for (expired = 0, disabled = 0, ptr = cfg->account; ptr;) {
1405
1406 if(ptr->expirationdate && ptr->expirationdate < time(NULL))
1407 expired++;
1408
1409 if(ptr->disabled != 0)
1410 disabled++;
1411
1412 ptr = ptr->next;
1413 }
1414
1415 cs_log("userdb reloaded: %d accounts freed, %d accounts loaded, %d expired, %d disabled", nro, nr, expired, disabled);
1416 return(0);
1417}
1418
1419static void chk_entry4sidtab(char *value, struct s_sidtab *sidtab, int what)
1420{
1421 int i, b;
1422 char *ptr;
1423 ushort *slist=(ushort *) 0;
1424 ulong *llist=(ulong *) 0;
1425 ulong caid;
1426 char buf[strlen(value) + 1];
1427 cs_strncpy(buf, value, sizeof(buf));
1428 b=(what==1) ? sizeof(ulong) : sizeof(ushort);
1429 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1430 {
1431 caid=a2i(ptr, b);
1432 if (!errno) i++;
1433 }
1434 //if (!i) return(0);
1435 if (b==sizeof(ushort))
1436 slist=malloc(i*sizeof(ushort));
1437 else
1438 llist=malloc(i*sizeof(ulong));
1439 strcpy(value, buf);
1440 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1441 {
1442 caid=a2i(ptr, b);
1443 if (errno) continue;
1444 if (b==sizeof(ushort))
1445 slist[i++]=(ushort) caid;
1446 else
1447 llist[i++]=caid;
1448 }
1449 switch (what)
1450 {
1451 case 0: sidtab->caid=slist;
1452 sidtab->num_caid=i;
1453 break;
1454 case 1: sidtab->provid=llist;
1455 sidtab->num_provid=i;
1456 break;
1457 case 2: sidtab->srvid=slist;
1458 sidtab->num_srvid=i;
1459 break;
1460 }
1461}
1462
1463void chk_sidtab(char *token, char *value, struct s_sidtab *sidtab)
1464{
1465 if (!strcmp(token, "caid")) { chk_entry4sidtab(value, sidtab, 0); return; }
1466 if (!strcmp(token, "provid")) { chk_entry4sidtab(value, sidtab, 1); return; }
1467 if (!strcmp(token, "ident")) { chk_entry4sidtab(value, sidtab, 1); return; }
1468 if (!strcmp(token, "srvid")) { chk_entry4sidtab(value, sidtab, 2); return; }
1469 if (token[0] != '#')
1470 fprintf(stderr, "Warning: keyword '%s' in sidtab section not recognized\n",token);
1471}
1472
1473int init_sidtab()
1474{
1475 int nr, nro;
1476 FILE *fp;
1477 char *value;
1478 struct s_sidtab *ptr;
1479 struct s_sidtab *sidtab=(struct s_sidtab *)0;
1480
1481 sprintf(token, "%s%s", cs_confdir, cs_sidt);
1482 if (!(fp=fopen(token, "r")))
1483 {
1484 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1485 return(1);
1486 }
1487 for (nro=0, ptr=cfg->sidtab; ptr; nro++)
1488 {
1489 struct s_sidtab *ptr_next;
1490 ptr_next=ptr->next;
1491 if (ptr->caid) free(ptr->caid);
1492 if (ptr->provid) free(ptr->provid);
1493 if (ptr->srvid) free(ptr->srvid);
1494 free(ptr);
1495 ptr=ptr_next;
1496 }
1497 nr=0;
1498 while (fgets(token, sizeof(token), fp))
1499 {
1500 int l;
1501 void *ptr;
1502 if ((l=strlen(trim(token)))<3) continue;
1503 if ((token[0]=='[') && (token[l-1]==']'))
1504 {
1505 token[l-1]=0;
1506 if (!(ptr=malloc(sizeof(struct s_sidtab))))
1507 {
1508 cs_log("Error allocating memory (errno=%d)", errno);
1509 return(1);
1510 }
1511 if (sidtab)
1512 sidtab->next=ptr;
1513 else
1514 cfg->sidtab=ptr;
1515 sidtab=ptr;
1516 nr++;
1517 memset(sidtab, 0, sizeof(struct s_sidtab));
1518 cs_strncpy(sidtab->label, strtolower(token+1), sizeof(sidtab->label));
1519 continue;
1520 }
1521 if (!sidtab) continue;
1522 if (!(value=strchr(token, '='))) continue;
1523 *value++='\0';
1524 chk_sidtab(trim(strtolower(token)), trim(strtolower(value)), sidtab);
1525 }
1526 fclose(fp);
1527
1528#ifdef DEBUG_SIDTAB
1529 show_sidtab(cfg->sidtab);
1530#endif
1531 cs_log("services reloaded: %d services freed, %d services loaded", nro, nr);
1532 return(0);
1533}
1534
1535int init_srvid()
1536{
1537 int nr;
1538 FILE *fp;
1539 char *payload;
1540 static struct s_srvid *srvid=(struct s_srvid *)0;
1541 sprintf(token, "%s%s", cs_confdir, cs_srid);
1542
1543 if (!(fp=fopen(token, "r"))) {
1544 cs_log("can't open file \"%s\" (err=%d), no service-id's loaded", token, errno);
1545 return(0);
1546 }
1547
1548 nr=0;
1549 while (fgets(token, sizeof(token), fp)) {
1550
1551 int l;
1552 void *ptr;
1553 char *tmp;
1554 tmp = trim(token);
1555
1556 if (tmp[0] == '#') continue;
1557 if ((l=strlen(tmp)) < 6) continue;
1558 if (!(payload=strchr(token, '|'))) continue;
1559 *payload++ = '\0';
1560
1561 if (!(ptr = malloc(sizeof(struct s_srvid)))) {
1562 cs_log("Error allocating memory (errno=%d)", errno);
1563 return(1);
1564 }
1565
1566 if (srvid)
1567 srvid->next = ptr;
1568 else
1569 cfg->srvid = ptr;
1570
1571 srvid = ptr;
1572 memset(srvid, 0, sizeof(struct s_srvid));
1573
1574 int i;
1575 char *ptr1;
1576 for (i = 0, ptr1 = strtok(payload, "|"); ptr1; ptr1 = strtok(NULL, "|"), i++){
1577 switch(i){
1578 case 0:
1579 cs_strncpy(srvid->prov, trim(ptr1), sizeof(srvid->prov));
1580 break;
1581 case 1:
1582 cs_strncpy(srvid->name, trim(ptr1), sizeof(srvid->name));
1583 break;
1584 case 2:
1585 cs_strncpy(srvid->type, trim(ptr1), sizeof(srvid->type));
1586 break;
1587 case 3:
1588 cs_strncpy(srvid->desc, trim(ptr1), sizeof(srvid->desc));
1589 break;
1590 }
1591 }
1592
1593 char *srvidasc = strchr(token, ':');
1594 *srvidasc++ = '\0';
1595 srvid->srvid = word_atob(srvidasc);
1596
1597 srvid->ncaid = 0;
1598 for (i = 0, ptr1 = strtok(token, ","); (ptr1) && (i < 10) ; ptr1 = strtok(NULL, ","), i++){
1599 srvid->caid[i] = word_atob(ptr1);
1600 srvid->ncaid = i+1;
1601 //cs_debug("ld caid: %04X srvid: %04X Prov: %s Chan: %s",srvid->caid[i],srvid->srvid,srvid->prov,srvid->name);
1602 }
1603 nr++;
1604 }
1605
1606 fclose(fp);
1607 if (nr>0)
1608 cs_log("%d service-id's loaded", nr);
1609 else{
1610 cs_log("oscam.srvid loading failed, old format");
1611 }
1612 return(0);
1613}
1614
1615static void chk_reader(char *token, char *value, struct s_reader *rdr)
1616{
1617 int i;
1618 char *ptr;
1619 /*
1620 * case sensitive first
1621 */
1622 if (!strcmp(token, "device"))
1623 {
1624 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1625 {
1626 trim(ptr);
1627 switch(i)
1628 {
1629 case 0: cs_strncpy(rdr->device, ptr, sizeof(rdr->device)); break;
1630 case 1: rdr->r_port=atoi(ptr); break;
1631 case 2: rdr->l_port=atoi(ptr); break;
1632 }
1633 }
1634 return;
1635 }
1636 if (!strcmp(token, "key"))
1637 {
1638 if (key_atob14(value, rdr->ncd_key))
1639 {
1640 fprintf(stderr, "Configuration newcamd: Error in Key\n");
1641 exit(1);
1642 }
1643 return;
1644 }
1645#ifdef CS_WITH_GBOX
1646 if (!strcmp(token, "password")) { cs_strncpy((char *)rdr->gbox_pwd, (const char *)i2b(4, a2i(value, 4)), 4); return; }
1647 if (!strcmp(token, "premium")) { rdr->gbox_prem=1; return; }
1648#endif
1649 if (!strcmp(token, "account"))
1650 {
1651 for (i=0, ptr=strtok(value, ","); (i<2)&&(ptr); ptr=strtok(NULL, ","), i++)
1652 {
1653 trim(ptr);
1654 switch(i)
1655 {
1656 case 0: cs_strncpy(rdr->r_usr, ptr, sizeof(rdr->r_usr)); break;
1657 case 1: cs_strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd)); break;
1658 }
1659 }
1660 return;
1661 }
1662 if( !strcmp(token, "pincode")) { strncpy(rdr->pincode, value, sizeof(rdr->pincode)-1); return; }
1663 if (!strcmp(token, "readnano") && value[0]) { asprintf(&(rdr->emmfile), "%s", value); return; }
1664 /*
1665 * case insensitive
1666 */
1667 strtolower(value);
1668
1669 if (!strcmp(token, "enable")) { rdr->enable=atoi(value) ? 1 : 0; return; }
1670 if (!strcmp(token, "services")) { chk_services(value, &rdr->sidtabok, &rdr->sidtabno); return; }
1671 if (!strcmp(token, "inactivitytimeout")) { rdr->tcp_ito = atoi(value); return; }
1672 if (!strcmp(token, "reconnecttimeout")) { rdr->tcp_rto = atoi(value); return; }
1673 if (!strcmp(token, "disableserverfilter")) { rdr->ncd_disable_server_filt = atoi(value); return; }
1674 //FIXME workaround for Smargo until native mode works
1675 if (!strcmp(token, "smargopatch")) { rdr->smargopatch = atoi(value); return; }
1676 if (!strcmp(token, "label")) { cs_strncpy(rdr->label, value, sizeof(rdr->label)); return; }
1677 if (!strcmp(token, "fallback")) { rdr->fallback=atoi(value) ? 1 : 0; return; }
1678 if (!strcmp(token, "logport")) { rdr->log_port=atoi(value); return; }
1679 if (!strcmp(token, "caid")) { chk_caidtab(value, &rdr->ctab); return; }
1680 if (!strcmp(token, "boxid")) { rdr->boxid=a2i(value,4); return; }
1681 if (!strcmp(token, "aeskey"))
1682 {
1683 if (key_atob(value, rdr->aes_key))
1684 {
1685 fprintf(stderr, "Configuration reader: Error in AES Key\n");
1686 exit(1);
1687 }
1688 return;
1689 }
1690 if ((!strcmp(token, "n3_rsakey")) || (!strcmp(token, "rsakey")) )
1691 {
1692 rdr->has_rsa=1;
1693 if (key_atob_l(value, rdr->rsa_mod, 128))
1694 {
1695 fprintf(stderr, "Configuration reader: Error in rsakey\n");
1696 exit(1);
1697 }
1698 return;
1699 }
1700 if (!strcmp(token, "tiger_rsakey"))
1701 {
1702 if (key_atob_l(value, rdr->rsa_mod, 240))
1703 {
1704 fprintf(stderr, "Configuration reader: Error in tiger_rsakey\n");
1705 exit(1);
1706 }
1707 return;
1708 }
1709 if ((!strcmp(token, "n3_boxkey")) || (!strcmp(token, "boxkey")))
1710 {
1711 if (key_atob_l(value, rdr->nagra_boxkey, 16))
1712 {
1713 fprintf(stderr, "Configuration reader: Error in boxkey\n");
1714 exit(1);
1715 }
1716 return;
1717 }
1718 if (!strcmp(token, "detect"))
1719 {
1720 for (i=0; RDR_CD_TXT[i]; i++)
1721 {
1722 if (!strcmp(value, RDR_CD_TXT[i]))
1723 rdr->detect=i;
1724 else
1725 if ((value[0]=='!') && (!strcmp(value+1, RDR_CD_TXT[i])))
1726 rdr->detect=i|0x80;
1727 }
1728 return;
1729 }
1730 if (!strcmp(token, "mhz")) { rdr->mhz=atoi(value); return; }
1731 if (!strcmp(token, "cardmhz")) { rdr->cardmhz=atoi(value); return; }
1732 if (!strcmp(token, "protocol"))
1733 {
1734 if (!strcmp(value, "mouse")) { rdr->typ=R_MOUSE; return; }
1735 if (!strcmp(value, "smartreader")) { rdr->typ=R_SMART; return; }
1736 if (!strcmp(value, "internal")) { rdr->typ=R_INTERNAL; return; }
1737#ifdef HAVE_PCSC
1738 if (!strcmp(value, "pcsc")) { rdr->typ=R_PCSC; return; }
1739#endif
1740 if (!strcmp(value, "serial")) { rdr->typ=R_SERIAL; return; }
1741 if (!strcmp(value, "camd35")) { rdr->typ=R_CAMD35; return; }
1742 if (!strcmp(value, "cs378x")) { rdr->typ=R_CS378X; return; }
1743 if (!strcmp(value, "cs357x")) { rdr->typ=R_CAMD35; return; }
1744 if (!strcmp(value, "gbox")) { rdr->typ=R_GBOX; return; }
1745 if (!strcmp(value, "cccam")) {
1746 rdr->typ=R_CCCAM;
1747 // strcpy(value, "1");
1748 // chk_caidtab(value, &rdr->ctab); // this is a MAJOR hack for auto multiple caid support (not currently working due to ncd table issue)
1749 return;
1750 }
1751 if (!strcmp(value, "radegast")) { rdr->typ=R_RADEGAST; return; }
1752 if (!strcmp(value, "newcamd") ||
1753 !strcmp(value, "newcamd525")) {rdr->typ=R_NEWCAMD;
1754 rdr->ncd_proto=NCD_525; return; }
1755 if (!strcmp(value, "newcamd524")) {rdr->typ=R_NEWCAMD;
1756 rdr->ncd_proto=NCD_524; return; }
1757 fprintf(stderr, "WARNING: value '%s' in protocol-line not recognized, assuming MOUSE\n",value);
1758 rdr->typ=R_MOUSE;
1759 return;
1760 }
1761 if (!strcmp(token, "loadbalanced")) { rdr->loadbalanced=atoi(value); return; }
1762 if (!strcmp(token, "ident")) { chk_ftab(value, &rdr->ftab,"reader",rdr->label,"provid"); return; }
1763 if (!strcmp(token, "class")) { chk_cltab(value, &rdr->cltab); return; }
1764 if (!strcmp(token, "chid")) { chk_ftab(value, &rdr->fchid,"reader",rdr->label,"chid"); return; }
1765 if (!strcmp(token, "showcls")) { rdr->show_cls = atoi(value); return; }
1766 if (!strcmp(token, "maxqlen"))
1767 {
1768 rdr->maxqlen = atoi(value);
1769 if( rdr->maxqlen<0 || rdr->maxqlen>CS_MAXQLEN )
1770 rdr->maxqlen=CS_MAXQLEN;
1771 return;
1772 }
1773 if (!strcmp(token, "group"))
1774 {
1775 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1776 {
1777 int g;
1778 g=atoi(ptr);
1779 if ((g>0) && (g<33)) rdr->grp|=(1<<(g-1));
1780 }
1781 return;
1782 }
1783 if (!strcmp(token, "emmcache"))
1784 {
1785 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1786 switch(i)
1787 {
1788 case 0: rdr->cachemm=atoi(ptr); break;
1789 case 1: rdr->rewritemm=atoi(ptr); break;
1790 case 2: rdr->logemm=atoi(ptr); break;
1791 }
1792 if (rdr->rewritemm <=0) {
1793 fprintf(stderr, "Notice: Setting EMMCACHE to %i,1,%i instead of %i,%i,%i. Zero or negative number of rewrites is silly\n", rdr->cachemm,rdr->logemm,rdr->cachemm,rdr->rewritemm,rdr->logemm);
1794 rdr->rewritemm = 1;
1795 }
1796 return;
1797 }
1798
1799 if (!strcmp(token, "blocknano"))
1800 {
1801 if (!strcmp(value,"all")) //wildcard is used
1802 for (i=0 ; i<256; i++)
1803 rdr->b_nano[i] |= 0x01; //set all lsb's to block all nanos
1804 else
1805 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1806 if ((i=byte_atob(ptr))>=0)
1807 rdr->b_nano[i]|= 0x01; //lsb is set when to block nano
1808 return;
1809 }
1810 if (!strcmp(token, "savenano"))
1811 {
1812 if (!strcmp(value,"all")) //wildcard is used
1813 for (i=0 ; i<256; i++)
1814 rdr->b_nano[i] |= 0x02; //set all lsb+1 to save all nanos to file
1815 else
1816 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1817 if ((i=byte_atob(ptr))>=0)
1818 rdr->b_nano[i]|= 0x02; //lsb+1 is set when to save nano to file
1819 return;
1820 }
1821 if (!strcmp(token, "cccversion")) { // cccam version
1822 if (strlen(value)>sizeof(rdr->cc_version)-1) {
1823 fprintf(stderr, "cccam config: version too long\n");
1824 exit(1);
1825 }
1826 bzero(rdr->cc_version, sizeof(rdr->cc_version));
1827 cs_strncpy(rdr->cc_version, value, sizeof(rdr->cc_version));
1828 return;
1829 }
1830 if (!strcmp(token, "cccbuild")) { // cccam build number
1831 if (strlen(value)>sizeof(rdr->cc_build)-1) {
1832 fprintf(stderr, "cccam config build number too long\n");
1833 exit(1);
1834 }
1835 bzero(rdr->cc_build, sizeof(rdr->cc_build));
1836 cs_strncpy(rdr->cc_build, value, sizeof(rdr->cc_build));
1837 return;
1838 }
1839 if (!strcmp(token, "cccmaxhop")) { // cccam max card distance
1840 rdr->cc_maxhop = atoi(value);
1841 return;
1842 }
1843 if (!strcmp(token, "deprecated")) { rdr->deprecated=atoi(value); return; }
1844 if (token[0] != '#')
1845 fprintf(stderr, "Warning: keyword '%s' in reader section not recognized\n",token);
1846}
1847
1848int init_readerdb()
1849{
1850 int tag=0, nr;
1851 FILE *fp;
1852 char *value;
1853
1854 sprintf(token, "%s%s", cs_confdir, cs_srvr);
1855 if (!(fp=fopen(token, "r")))
1856 {
1857 cs_log("can't open file \"%s\" (errno=%d)\n", token, errno);
1858 return(1);
1859 }
1860 nr=0;
1861 while (fgets(token, sizeof(token), fp))
1862 {
1863 int i, l;
1864 if ((l=strlen(trim(token)))<3) continue;
1865 if ((token[0]=='[') && (token[l-1]==']'))
1866 {
1867 token[l-1]=0;
1868 tag=(!strcmp("reader", strtolower(token+1)));
1869 if (reader[nr].label[0] && reader[nr].typ) nr++;
1870 memset(&reader[nr], 0, sizeof(struct s_reader));
1871 reader[nr].enable = 1;
1872 reader[nr].tcp_rto = 30;
1873 reader[nr].show_cls = 10;
1874 reader[nr].maxqlen = CS_MAXQLEN;
1875 reader[nr].mhz = 357;
1876 reader[nr].cardmhz = 357;
1877 reader[nr].deprecated = 0;
1878 strcpy(reader[nr].pincode, "none");
1879 for (i=1; i<CS_MAXCAIDTAB; reader[nr].ctab.mask[i++]=0xffff);
1880 continue;
1881 }
1882 if (!tag) continue;
1883 if (!(value=strchr(token, '='))) continue;
1884 *value++='\0';
1885 chk_reader(trim(strtolower(token)), trim(value), &reader[nr]);
1886 }
1887 fclose(fp);
1888 return(0);
1889}
1890
1891/*
1892int init_irdeto_guess_tab()
1893{
1894 int i, j, skip;
1895 int b47;
1896 FILE *fp;
1897 char token[128], *value, *ptr;
1898 char zSid[5];
1899 uchar b3;
1900 ushort caid, sid;
1901 struct s_irdeto_quess *ird_row, *head;
1902
1903 memset(cfg->itab, 0, sizeof(cfg->itab));
1904 sprintf(token, "%s%s", cs_confdir, cs_ird);
1905 if (!(fp=fopen(token, "r")))
1906 {
1907 cs_log("can't open file \"%s\" (errno=%d) irdeto guessing not loaded",
1908 token, errno);
1909 return(1);
1910 }
1911 while (fgets(token, sizeof(token), fp))
1912 {
1913 if( strlen(token)<20 ) continue;
1914 for( i=b3=b47=caid=sid=skip=0, ptr=strtok(token, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1915 {
1916 trim(ptr);
1917 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1918 skip=1;
1919 break;
1920 }
1921 switch(i)
1922 {
1923 case 0: b3 = a2i(ptr, 2); break;
1924 case 1: b47 = a2i(ptr, 8); break;
1925 case 2: caid = a2i(ptr, 4); break;
1926 case 3:
1927 for( j=0; j<4; j++ )
1928 zSid[j]=ptr[j];
1929 zSid[4]=0;
1930 sid = a2i(zSid, 4);
1931 break;
1932 }
1933 }
1934 if( !skip )
1935 {
1936 if (!(ird_row=(struct s_irdeto_quess*)malloc(sizeof(struct s_irdeto_quess))))
1937 {
1938 cs_log("Error allocating memory (errno=%d)", errno);
1939 return;
1940 }
1941 ird_row->b47 = b47;
1942 ird_row->caid = caid;
1943 ird_row->sid = sid;
1944 ird_row->next = 0;
1945
1946 head = cfg->itab[b3];
1947 if( head ) {
1948 while( head->next )
1949 head=head->next;
1950 head->next=ird_row;
1951 }
1952 else
1953 cfg->itab[b3]=ird_row;
1954
1955 //cs_debug("%02X:%08X:%04X:%04X", b3, b47, caid, sid);
1956 }
1957 }
1958 fclose(fp);
1959
1960 for( i=0; i<0xff; i++ )
1961 {
1962 head=cfg->itab[i];
1963 while(head)
1964 {
1965 cs_debug("itab[%02X]: b47=%08X, caid=%04X, sid=%04X",
1966 i, head->b47, head->caid, head->sid);
1967 head=head->next;
1968 }
1969 }
1970 return(0);
1971}
1972*/
1973
1974#ifdef CS_ANTICASC
1975void init_ac()
1976{
1977 int nr;
1978 FILE *fp;
1979 //char *value;
1980
1981 sprintf(token, "%s%s", cs_confdir, cs_ac);
1982 if (!(fp=fopen(token, "r")))
1983 {
1984 cs_log("can't open file \"%s\" (errno=%d) anti-cascading table not loaded",
1985 token, errno);
1986 return;
1987 }
1988
1989 for(nr=0; fgets(token, sizeof(token), fp);)
1990 {
1991 int i, skip;
1992 ushort caid, sid, chid, dwtime;
1993 ulong provid;
1994 char *ptr, *ptr1;
1995 struct s_cpmap *ptr_cpmap;
1996 static struct s_cpmap *cpmap=(struct s_cpmap *)0;
1997
1998 if( strlen(token)<4 ) continue;
1999
2000 caid=sid=chid=dwtime=0;
2001 provid=0;
2002 skip=0;
2003 ptr1=0;
2004 for( i=0, ptr=strtok(token, "="); (i<2)&&(ptr); ptr=strtok(NULL, "="), i++ )
2005 {
2006 trim(ptr);
2007 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
2008 skip=1;
2009 break;
2010 }
2011 switch( i )
2012 {
2013 case 0:
2014 ptr1=ptr;
2015 break;
2016 case 1:
2017 dwtime = atoi(ptr);
2018 break;
2019 }
2020 }
2021
2022 if( !skip )
2023 {
2024 for( i=0, ptr=strtok(ptr1, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
2025 {
2026 trim(ptr);
2027 switch( i )
2028 {
2029 case 0:
2030 if( *ptr=='*' ) caid = 0;
2031 else caid = a2i(ptr, 4);
2032 break;
2033 case 1:
2034 if( *ptr=='*' ) provid = 0;
2035 else provid = a2i(ptr, 6);
2036 break;
2037 case 2:
2038 if( *ptr=='*' ) sid = 0;
2039 else sid = a2i(ptr, 4);
2040 break;
2041 case 3:
2042 if( *ptr=='*' ) chid = 0;
2043 else chid = a2i(ptr, 4);
2044 break;
2045 }
2046 }
2047 if (!(ptr_cpmap=(struct s_cpmap*)malloc(sizeof(struct s_cpmap))))
2048 {
2049 cs_log("Error allocating memory (errno=%d)", errno);
2050 return;
2051 }
2052 if( cpmap )
2053 cpmap->next=ptr_cpmap;
2054 else
2055 cfg->cpmap=ptr_cpmap;
2056 cpmap=ptr_cpmap;
2057
2058 cpmap->caid = caid;
2059 cpmap->provid = provid;
2060 cpmap->sid = sid;
2061 cpmap->chid = chid;
2062 cpmap->dwtime = dwtime;
2063 cpmap->next = 0;
2064
2065 cs_debug("nr=%d, caid=%04X, provid=%06X, sid=%04X, chid=%04X, dwtime=%d",
2066 nr, caid, provid, sid, chid, dwtime);
2067 nr++;
2068 }
2069 }
2070 fclose(fp);
2071 //cs_log("%d lengths for caid guessing loaded", nr);
2072 return;
2073}
2074#endif
2075
2076/*
2077 * makes a char ready to write a token into config or webIf
2078 */
2079char *mk_t_caidtab(CAIDTAB *ctab){
2080 int i = 0, needed = 1, pos = 0;
2081 while(ctab->caid[i]){
2082 if(ctab->mask[i]) needed += 10;
2083 else needed += 5;
2084 if(ctab->cmap[i]) needed += 5;
2085 ++i;
2086 }
2087 char *value = (char *) malloc(needed * sizeof(char));
2088 i = 0;
2089 while(ctab->caid[i]) {
2090 if(i == 0) {
2091 sprintf(value + pos, "%04X", ctab->caid[i]);
2092 pos += 4;
2093 } else {
2094 sprintf(value + pos, ",%04X", ctab->caid[i]);
2095 pos += 5;
2096 }
2097 if(ctab->mask[i]){
2098 sprintf(value + pos, "&%04X", ctab->mask[i]);
2099 pos += 5;
2100 }
2101 if(ctab->cmap[i]){
2102 sprintf(value + pos, ":%04X", ctab->cmap[i]);
2103 pos += 5;
2104 }
2105 ++i;
2106 }
2107 value[pos] = '\0';
2108 return value;
2109}
2110
2111/*
2112 * makes a char ready to write a token into config or webIf
2113 */
2114char *mk_t_tuntab(TUNTAB *ttab){
2115 int i = 0, needed = 1, pos = 0;
2116 while(ttab->bt_caidfrom[i]){
2117 if(ttab->bt_srvid[i]) needed += 10;
2118 else needed += 5;
2119 if(ttab->bt_caidto[i]) needed += 5;
2120 ++i;
2121 }
2122 char *value = (char *) malloc(needed * sizeof(char));
2123 i = 0;
2124 while(ttab->bt_caidfrom[i]) {
2125 if(i == 0) {
2126 sprintf(value + pos, "%04X", ttab->bt_caidfrom[i]);
2127 pos += 4;
2128 } else {
2129 sprintf(value + pos, ",%04X", ttab->bt_caidfrom[i]);
2130 pos += 5;
2131 }
2132 if(ttab->bt_srvid[i]){
2133 sprintf(value + pos, ".%04X", ttab->bt_srvid[i]);
2134 pos += 5;
2135 }
2136 if(ttab->bt_caidto[i]){
2137 sprintf(value + pos, ":%04X", ttab->bt_caidto[i]);
2138 pos += 5;
2139 }
2140 ++i;
2141 }
2142 value[pos] = '\0';
2143 return value;
2144}
2145
2146/*
2147 * makes a char ready to write a token into config or webIf
2148 */
2149char *mk_t_group(ulong *grp){
2150 int i = 0, needed = 1, pos = 0, dot = 0;
2151 char grpbit[33];
2152 long2bitchar((long) grp, grpbit);
2153
2154 for(i = 0; i < 32; i++){
2155 if (grpbit[i] == '1'){
2156 needed += 2;
2157 if(i > 9) needed += 1;
2158 }
2159 }
2160 char *value = (char *) malloc(needed * sizeof(char));
2161
2162 for(i = 0; i < 32; i++){
2163 if (grpbit[i] == '1'){
2164 if (dot == 0){
2165 sprintf(value + pos, "%d", i+1);
2166 if (i > 9)pos += 2;
2167 else pos += 1;
2168 dot = 1;
2169 } else {
2170 sprintf(value + pos, ",%d", i+1);
2171 if (i > 9)pos += 3;
2172 else pos += 2;
2173 }
2174 }
2175 }
2176 value[pos] = '\0';
2177 return value;
2178}
2179
2180/*
2181 * makes a char ready to write a token into config or webIf
2182 */
2183char *mk_t_ftab(FTAB *ftab){
2184 int i = 0, j = 0, needed = 1, pos = 0;
2185
2186 needed = ftab->nfilts * 5;
2187 for (i = 0; i < ftab->nfilts; ++i)
2188 needed += ftab->filts[i].nprids * 7;
2189
2190 char *value = (char *) malloc(needed * sizeof(char));
2191
2192 char *dot="";
2193 for (i = 0; i < ftab->nfilts; ++i){
2194 sprintf(value + pos, "%s%04X", dot, ftab->filts[i].caid);
2195 pos += 4;
2196 if (i > 0) pos += 1;
2197 dot=":";
2198 for (j = 0; j < ftab->filts[i].nprids; ++j) {
2199 sprintf(value + pos, "%s%06lX", dot, ftab->filts[i].prids[j]);
2200 pos += 7;
2201 dot=",";
2202 }
2203 dot=";";
2204 }
2205
2206 value[pos] = '\0';
2207 return value;
2208}
Note: See TracBrowser for help on using the repository browser.