source: trunk/oscam-config.c@ 1708

Last change on this file since 1708 was 1708, checked in by merek, 11 years ago
  • Remove deprecated irdeto_guessing code
  • Show config warnings also for Anticascading, Gbox when using config directives without support in binary
File size: 67.2 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#ifdef CS_ANTICASC
16static char *cs_ac="oscam.ac";
17#endif
18
19static char token[4096];
20
21typedef enum cs_proto_type
22{
23 TAG_GLOBAL, // must be first !
24 TAG_MONITOR, // monitor
25 TAG_CAMD33, // camd 3.3x
26 TAG_CAMD35, // camd 3.5x UDP
27 TAG_NEWCAMD, // newcamd
28 TAG_RADEGAST, // radegast
29 TAG_SERIAL, // serial (static)
30 TAG_CS357X, // camd 3.5x UDP
31 TAG_CS378X, // camd 3.5x TCP
32 TAG_GBOX, // gbox
33 TAG_CCCAM, // cccam
34 TAG_DVBAPI, // dvbapi
35 TAG_WEBIF, // webif
36 TAG_ANTICASC // anti-cascading
37} cs_proto_type_t;
38
39static char *cctag[]={"global", "monitor", "camd33", "camd35", "newcamd", "radegast", "serial",
40 "cs357x", "cs378x", "gbox", "cccam", "dvbapi", "webif", "anticasc", NULL};
41
42#ifdef DEBUG_SIDTAB
43static void show_sidtab(struct s_sidtab *sidtab)
44{
45 for (; sidtab; sidtab=sidtab->next)
46 {
47 int i;
48 char buf[1024];
49 cs_log("label=%s", sidtab->label);
50 sprintf(buf, "caid(%d)=", sidtab->num_caid);
51 for (i=0; i<sidtab->num_caid; i++)
52 sprintf(buf+strlen(buf), "%04X ", sidtab->caid[i]);
53 cs_log("%s", buf);
54 sprintf(buf, "provider(%d)=", sidtab->num_provid);
55 for (i=0; i<sidtab->num_provid; i++)
56 sprintf(buf+strlen(buf), "%08X ", sidtab->provid[i]);
57 cs_log("%s", buf);
58 sprintf(buf, "services(%d)=", sidtab->num_srvid);
59 for (i=0; i<sidtab->num_srvid; i++)
60 sprintf(buf+strlen(buf), "%04X ", sidtab->srvid[i]);
61 cs_log("%s", buf);
62 }
63}
64#endif
65
66void chk_iprange(char *value, struct s_ip **base)
67{
68 int i = 0;
69 char *ptr1, *ptr2;
70 struct s_ip *lip, *cip;
71
72 for (cip=lip=*base; cip; cip=cip->next)
73 lip = cip;
74 if (!(cip=malloc(sizeof(struct s_ip)))) {
75 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
76 exit(1);
77 }
78 if (*base)
79 lip->next = cip;
80 else
81 *base = cip;
82
83 memset(cip, 0, sizeof(struct s_ip));
84 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ",")) {
85 if (i == 0)
86 ++i;
87 else {
88 if (!(cip=malloc(sizeof(struct s_ip)))) {
89 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
90 exit(1);
91 }
92 lip->next = cip;
93 memset(cip, 0, sizeof(struct s_ip));
94 }
95
96 if( (ptr2=strchr(trim(ptr1), '-')) ) {
97 *ptr2++ ='\0';
98 cip->ip[0]=cs_inet_addr(trim(ptr1));
99 cip->ip[1]=cs_inet_addr(trim(ptr2));
100 } else {
101 cip->ip[0]=cip->ip[1]=cs_inet_addr(ptr1);
102 }
103 lip = cip;
104 }
105}
106
107void chk_caidtab(char *caidasc, CAIDTAB *ctab)
108{
109 int i;
110 char *ptr1, *ptr2, *ptr3;
111
112 for (i = 0, ptr1 = strtok(caidasc, ","); (i < CS_MAXCAIDTAB) && (ptr1); ptr1 = strtok(NULL, ",")) {
113 ulong caid, mask, cmap;
114 if( (ptr3 = strchr(trim(ptr1), ':')) )
115 *ptr3++ = '\0';
116 else
117 ptr3 = "";
118
119 if( (ptr2 = strchr(trim(ptr1), '&')) )
120 *ptr2++ = '\0';
121 else
122 ptr2 = "";
123
124 if (((caid = a2i(ptr1, 2)) | (mask = a2i(ptr2,-2)) | (cmap = a2i(ptr3, 2))) < 0x10000) {
125 ctab->caid[i] = caid;
126 ctab->mask[i] = mask;
127 ctab->cmap[i++] = cmap;
128 }
129 }
130}
131
132void chk_tuntab(char *tunasc, TUNTAB *ttab)
133{
134 int i;
135 char *ptr1, *ptr2, *ptr3;
136
137 for (i = 0, ptr1 = strtok(tunasc, ","); (i < CS_MAXTUNTAB) && (ptr1); ptr1 = strtok(NULL, ",")) {
138 ulong bt_caidfrom, bt_caidto, bt_srvid;
139 if( (ptr3 = strchr(trim(ptr1), ':')) )
140 *ptr3++ = '\0';
141 else
142 ptr3 = "";
143
144 if( (ptr2 = strchr(trim(ptr1), '.')) )
145 *ptr2++ = '\0';
146 else
147 ptr2 = "";
148
149 if ((bt_caidfrom = a2i(ptr1, 2)) | (bt_srvid = a2i(ptr2,-2)) | (bt_caidto = a2i(ptr3, 2))) {
150 ttab->bt_caidfrom[i] = bt_caidfrom;
151 ttab->bt_caidto[i] = bt_caidto;
152 ttab->bt_srvid[i++] = bt_srvid;
153 }
154 }
155}
156
157void chk_services(char *labels, ulong *sidok, ulong *sidno)
158{
159 int i;
160 char *ptr;
161 SIDTAB *sidtab;
162 *sidok = *sidno = 0;
163 for (ptr=strtok(labels, ","); ptr; ptr=strtok(NULL, ",")) {
164 for (trim(ptr), i = 0, sidtab = cfg->sidtab; sidtab; sidtab = sidtab->next, i++) {
165 if (!strcmp(sidtab->label, ptr)) *sidok|=(1<<i);
166 if ((ptr[0]=='!') && (!strcmp(sidtab->label, ptr+1))) *sidno|=(1<<i);
167 }
168 }
169}
170
171void chk_ftab(char *zFilterAsc, FTAB *ftab, const char *zType, const char *zName, const char *zFiltName)
172{
173 int i, j;
174 char *ptr1, *ptr2, *ptr3;
175 char *ptr[CS_MAXFILTERS] = {0};
176
177 memset(ftab, 0, sizeof(FTAB));
178 for( i = 0, ptr1 = strtok(zFilterAsc, ";"); (i < CS_MAXFILTERS) && (ptr1); ptr1 = strtok(NULL, ";"), i++ ) {
179 ptr[i] = ptr1;
180 if( (ptr2 = strchr(trim(ptr1), ':')) ) {
181 *ptr2++ ='\0';
182 ftab->filts[i].caid = (ushort)a2i(ptr1, 4);
183 ptr[i] = ptr2;
184 }
185 else if (zFiltName && zFiltName[0] == 'c') {
186 cs_log("PANIC: CAID field not found in CHID parameter!");
187 cs_exit(1);
188 }
189 ftab->nfilts++;
190 }
191
192 if( ftab->nfilts ) cs_debug("%s '%s' %s filter(s):", zType, zName, zFiltName);
193 for( i = 0; i < ftab->nfilts; i++ ) {
194 cs_debug("CAID #%d: %04X", i, ftab->filts[i].caid);
195 for( j = 0, ptr3 = strtok(ptr[i], ","); (j < CS_MAXPROV) && (ptr3); ptr3 = strtok(NULL, ","), j++ ) {
196 ftab->filts[i].prids[j] = a2i(ptr3,6);
197 ftab->filts[i].nprids++;
198 cs_debug("%s #%d: %06X", zFiltName, j, ftab->filts[i].prids[j]);
199 }
200 }
201}
202
203void chk_cltab(char *classasc, CLASSTAB *clstab)
204{
205 int i;
206 char *ptr1;
207 for( i = 0, ptr1 = strtok(classasc, ","); (i < CS_MAXCAIDTAB) && (ptr1); ptr1 = strtok(NULL, ",") ) {
208 ptr1 = trim(ptr1);
209 if( ptr1[0] == '!' )
210 clstab->bclass[clstab->bn++] = (uchar)a2i(ptr1+1, 2);
211 else
212 clstab->aclass[clstab->an++] = (uchar)a2i(ptr1, 2);
213 }
214}
215
216void chk_port_tab(char *portasc, PTAB *ptab)
217{
218 int i, j, nfilts, ifilt, iport;
219 char *ptr1, *ptr2, *ptr3;
220 char *ptr[CS_MAXPORTS] = {0};
221 int port[CS_MAXPORTS] = {0};
222 int previous_nports = ptab->nports;
223
224 for (nfilts = i = previous_nports, ptr1 = strtok(portasc, ";"); (i < CS_MAXCAIDTAB) && (ptr1); ptr1 = strtok(NULL, ";"), i++) {
225 ptr[i] = ptr1;
226 if( (ptr2=strchr(trim(ptr1), '@')) ) {
227 *ptr2++ ='\0';
228 ptab->ports[i].s_port = atoi(ptr1);
229 ptr[i] = ptr2;
230 port[i] = ptab->ports[i].s_port;
231 ptab->nports++;
232 }
233 nfilts++;
234 }
235
236 if( nfilts == 1 && strlen(portasc) < 6 && ptab->ports[0].s_port == 0 ) {
237 ptab->ports[0].s_port = atoi(portasc);
238 ptab->nports = 1;
239 }
240
241 iport = ifilt = previous_nports;
242 for (i=previous_nports; i<nfilts; i++) {
243 if( port[i] != 0 )
244 iport = i;
245 for (j = 0, ptr3 = strtok(ptr[i], ","); (j < CS_MAXPROV) && (ptr3); ptr3 = strtok(NULL, ","), j++) {
246 if( (ptr2=strchr(trim(ptr3), ':')) ) {
247 *ptr2++='\0';
248 ptab->ports[iport].ftab.nfilts++;
249 ifilt = ptab->ports[iport].ftab.nfilts-1;
250 ptab->ports[iport].ftab.filts[ifilt].caid = (ushort)a2i(ptr3, 4);
251 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr2, 6);
252 } else {
253 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr3, 6);
254 }
255 ptab->ports[iport].ftab.filts[ifilt].nprids++;
256 }
257 }
258}
259
260#ifdef NOTUSED
261static void chk_srvip(char *value, in_addr_t *ip)
262{
263 int i;
264 char *ptr;
265 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
266 if (i<8) ip[i++] = inet_addr(ptr);
267}
268#endif
269
270void chk_t_global(char *token, char *value)
271{
272 if (!strcmp(token, "disablelog")) {
273 if (strlen(value) == 0) {
274 cfg->disablelog = 0;
275 return;
276 } else {
277 cfg->disablelog = atoi(value);
278 return;
279 }
280 }
281
282 if (!strcmp(token, "serverip")) {
283 if (strlen(value) == 0) {
284 cfg->srvip = 0;
285 return;
286 } else {
287 cfg->srvip=inet_addr(value);
288 return;
289 }
290 }
291
292 if (!strcmp(token, "logfile")) {
293 if (cfg->logfile != NULL) {
294 free(cfg->logfile);
295 cfg->logfile = NULL;
296 }
297 if (strlen(value) > 0)
298 asprintf(&(cfg->logfile), "%s", value);
299 return;
300 }
301
302 if (!strcmp(token, "pidfile")) {
303 if (cfg->pidfile != NULL) {
304 free(cfg->pidfile);
305 cfg->pidfile = NULL;
306 }
307 if (strlen(value) > 0)
308 asprintf(&(cfg->pidfile), "%s", value);
309 return;
310 }
311
312 if (!strcmp(token, "usrfile")) {
313 if (cfg->usrfile != NULL) {
314 free(cfg->usrfile);
315 cfg->usrfile = NULL;
316 }
317 if (strlen(value) > 0)
318 asprintf(&(cfg->usrfile), "%s", value);
319 return;
320 }
321
322 if (!strcmp(token, "cwlogdir")) {
323 if (cfg->cwlogdir != NULL) {
324 free(cfg->cwlogdir);
325 cfg->cwlogdir = NULL;
326 }
327 if (strlen(value) > 0)
328 asprintf(&(cfg->cwlogdir), "%s", value);
329 return;
330 }
331
332 if (!strcmp(token, "usrfileflag")) {
333 if (strlen(value) == 0) {
334 cfg->usrfileflag = 0;
335 return;
336 } else {
337 cfg->usrfileflag = atoi(value);
338 return;
339 }
340 }
341
342 if (!strcmp(token, "clienttimeout")) {
343 if (strlen(value) == 0) {
344 cfg->ctimeout = CS_CLIENT_TIMEOUT;
345 return;
346 } else {
347 cfg->ctimeout = atoi(value);
348 if (cfg->ctimeout < 100)
349 cfg->ctimeout *= 1000;
350 return;
351 }
352 }
353
354 if (!strcmp(token, "fallbacktimeout")) {
355 if (strlen(value) == 0) {
356 cfg->ftimeout = CS_CLIENT_TIMEOUT;
357 return;
358 } else {
359 cfg->ftimeout = atoi(value);
360 if (cfg->ftimeout < 100)
361 cfg->ftimeout *= 1000;
362 return;
363 }
364 }
365
366 if (!strcmp(token, "clientmaxidle")) {
367 if (strlen(value) == 0) {
368 cfg->cmaxidle = CS_CLIENT_MAXIDLE;
369 return;
370 } else {
371 cfg->cmaxidle = atoi(value);
372 return;
373 }
374 }
375
376 if (!strcmp(token, "cachedelay")) {
377 if (strlen(value) == 0) {
378 cfg->delay = CS_DELAY;
379 return;
380 } else {
381 cfg->delay = atoi(value);
382 return;
383 }
384 }
385
386 if (!strcmp(token, "bindwait")) {
387 if (strlen(value) == 0) {
388 cfg->bindwait = CS_BIND_TIMEOUT;
389 return;
390 } else {
391 cfg->bindwait = atoi(value);
392 return;
393 }
394 }
395
396 if (!strcmp(token, "netprio")) {
397 if (strlen(value) == 0) {
398 cfg->netprio = 0;
399 return;
400 } else {
401 cfg->netprio = atoi(value);
402 return;
403 }
404 }
405
406 if (!strcmp(token, "resolvedelay")) {
407 if (strlen(value) == 0) {
408 cfg->resolvedelay = CS_RESOLVE_DELAY;
409 return;
410 } else {
411 cfg->resolvedelay = atoi(value);
412 return;
413 }
414 }
415
416 if (!strcmp(token, "sleep")) {
417 if (strlen(value) == 0) {
418 cfg->tosleep = 0;
419 return;
420 } else {
421 cfg->tosleep = atoi(value);
422 return;
423 }
424 }
425
426 if (!strcmp(token, "unlockparental")) {
427 if (strlen(value) == 0) {
428 cfg->ulparent = 0;
429 return;
430 } else {
431 cfg->ulparent = atoi(value);
432 return;
433 }
434 }
435
436 if (!strcmp(token, "nice")) {
437 if (strlen(value) == 0) {
438 cfg->nice = 99;
439 return;
440 } else {
441 cfg->nice = atoi(value);
442 if ((cfg->nice<-20) || (cfg->nice>20)) cfg->nice = 99;
443 if (cfg->nice != 99) cs_setpriority(cfg->nice); // ignore errors
444 return;
445 }
446 }
447
448 if (!strcmp(token, "serialreadertimeout")) {
449 if (cfg->srtimeout < 100)
450 cfg->srtimeout = atoi(value) * 1000;
451 else
452 cfg->srtimeout = atoi(value);
453 if (cfg->srtimeout <= 0)
454 cfg->srtimeout = 1500;
455 return;
456 }
457
458 if (!strcmp(token, "maxlogsize")) {
459 if (strlen(value) == 0) {
460 cfg->max_log_size = 10;
461 return;
462 } else {
463 cfg->max_log_size = atoi(value);
464 if( cfg->max_log_size <= 10 )
465 cfg->max_log_size = 10;
466 return;
467 }
468 }
469
470 if( !strcmp(token, "waitforcards")) {
471 if (strlen(value) == 0) {
472 cfg->waitforcards = 0;
473 return;
474 } else {
475 cfg->waitforcards = atoi(value);
476 return;
477 }
478 }
479
480 if( !strcmp(token, "preferlocalcards")) {
481 if (strlen(value) == 0) {
482 cfg->preferlocalcards = 0;
483 return;
484 } else {
485 cfg->preferlocalcards = atoi(value);
486 return;
487 }
488 }
489
490 if (token[0] != '#')
491 cs_log("Warning: keyword '%s' in global section not recognized", token);
492}
493
494#ifdef CS_ANTICASC
495void chk_t_ac(char *token, char *value)
496{
497 if (!strcmp(token, "enabled")) {
498 cfg->ac_enabled = atoi(value);
499 if( cfg->ac_enabled <= 0 )
500 cfg->ac_enabled = 0;
501 else
502 cfg->ac_enabled = 1;
503 return;
504 }
505
506 if (!strcmp(token, "numusers")) {
507 cfg->ac_users = atoi(value);
508 if( cfg->ac_users < 0 )
509 cfg->ac_users = 0;
510 return;
511 }
512
513 if (!strcmp(token, "sampletime")) {
514 cfg->ac_stime = atoi(value);
515 if( cfg->ac_stime < 0 )
516 cfg->ac_stime = 2;
517 return;
518 }
519
520 if (!strcmp(token, "samples")) {
521 cfg->ac_samples = atoi(value);
522 if( cfg->ac_samples < 2 || cfg->ac_samples > 10)
523 cfg->ac_samples = 10;
524 return;
525 }
526
527 if (!strcmp(token, "penalty")) {
528 cfg->ac_penalty = atoi(value);
529 if( cfg->ac_penalty < 0 )
530 cfg->ac_penalty = 0;
531 return;
532 }
533
534 if (!strcmp(token, "aclogfile")) {
535 cs_strncpy(cfg->ac_logfile, value, sizeof(cfg->ac_logfile));
536 return;
537 }
538
539 if( !strcmp(token, "fakedelay") ) {
540 cfg->ac_fakedelay = atoi(value);
541 if( cfg->ac_fakedelay < 100 || cfg->ac_fakedelay > 1000 )
542 cfg->ac_fakedelay = 1000;
543 return;
544 }
545
546 if( !strcmp(token, "denysamples") ) {
547 cfg->ac_denysamples = atoi(value);
548 if( cfg->ac_denysamples < 2 || cfg->ac_denysamples > cfg->ac_samples - 1 )
549 cfg->ac_denysamples=cfg->ac_samples-1;
550 return;
551 }
552
553 if (token[0] != '#')
554 cs_log( "Warning: keyword '%s' in anticascading section not recognized",token);
555}
556#endif
557
558void chk_t_monitor(char *token, char *value)
559{
560 if (!strcmp(token, "port")) {
561 if(strlen(value) == 0) {
562 cfg->mon_port = 0;
563 return;
564 } else {
565 cfg->mon_port=atoi(value);
566 return;
567 }
568 }
569
570 if (!strcmp(token, "serverip")) {
571 if(strlen(value) == 0) {
572 cfg->mon_srvip = 0;
573 return;
574 } else {
575 cfg->mon_srvip=inet_addr(value);
576 return;
577 }
578 }
579
580 if (!strcmp(token, "nocrypt")) {
581 if(strlen(value) == 0) {
582 clear_sip(&cfg->mon_allowed);
583 return;
584 } else {
585 chk_iprange(value, &cfg->mon_allowed);
586 return;
587 }
588 }
589
590 if (!strcmp(token, "aulow")) {
591 if(strlen(value) == 0) {
592 cfg->mon_aulow = 0;
593 return;
594 } else {
595 cfg->mon_aulow = atoi(value);
596 return;
597 }
598 }
599
600 if (!strcmp(token, "monlevel")) {
601 if(strlen(value) == 0) {
602 cfg->mon_level = 0;
603 return;
604 } else {
605 cfg->mon_level = atoi(value);
606 return;
607 }
608 }
609
610 if (!strcmp(token, "hideclient_to")) {
611 if(strlen(value) == 0) {
612 cfg->mon_hideclient_to = 0;
613 return;
614 } else {
615 cfg->mon_hideclient_to = atoi(value);
616 return;
617 }
618 }
619
620 if (token[0] != '#')
621 cs_log("Warning: keyword '%s' in monitor section not recognized",token);
622}
623
624#ifdef WEBIF
625void chk_t_webif(char *token, char *value)
626{
627 if (!strcmp(token, "httpport")) {
628 if(strlen(value) == 0) {
629 cfg->http_port = 0;
630 return;
631 } else {
632 cfg->http_port = atoi(value);
633 return;
634 }
635 }
636
637 if (!strcmp(token, "httpuser")) {
638 cs_strncpy(cfg->http_user, value, sizeof(cfg->http_user));
639 return;
640 }
641
642 if (!strcmp(token, "httppwd")) {
643 cs_strncpy(cfg->http_pwd, value, sizeof(cfg->http_pwd));
644 return;
645 }
646
647 if (!strcmp(token, "httpcss")) {
648 cs_strncpy(cfg->http_css, value, sizeof(cfg->http_css));
649 return;
650 }
651
652 if (!strcmp(token, "httpscript")) {
653 cs_strncpy(cfg->http_script, value, sizeof(cfg->http_script));
654 return;
655 }
656
657 if (!strcmp(token, "httptpl")) {
658 cs_strncpy(cfg->http_tpl, value, sizeof(cfg->http_tpl));
659 if(strlen(cfg->http_tpl) < (sizeof(cfg->http_tpl)-2) && cfg->http_tpl[strlen(cfg->http_tpl)-1] != '/') {
660 cfg->http_tpl[strlen(cfg->http_tpl)] = '/';
661 cfg->http_tpl[strlen(cfg->http_tpl)] = '\0';
662 }
663 return;
664 }
665
666 if (!strcmp(token, "httprefresh")) {
667 if(strlen(value) == 0) {
668 cfg->http_refresh = 0;
669 return;
670 } else {
671 cfg->http_refresh = atoi(value);
672 return;
673 }
674 }
675
676 if (!strcmp(token, "httphideidleclients")) {
677 if(strlen(value) == 0) {
678 cfg->http_hide_idle_clients = 0;
679 return;
680 } else {
681 cfg->http_hide_idle_clients=atoi(value);
682 return;
683 }
684 }
685
686 if (!strcmp(token, "httpallowed")) {
687 if(strlen(value) == 0) {
688 clear_sip(&cfg->http_allowed);
689 return;
690 } else {
691 chk_iprange(value, &cfg->http_allowed);
692 return;
693 }
694 }
695
696 if (token[0] != '#')
697 cs_log("Warning: keyword '%s' in webif section not recognized",token);
698}
699#endif
700
701
702void chk_t_camd33(char *token, char *value)
703{
704 if (!strcmp(token, "port")) {
705 if(strlen(value) == 0) {
706 cfg->c33_port = 0;
707 return;
708 } else {
709 cfg->c33_port = atoi(value);
710 return;
711 }
712 }
713
714 if (!strcmp(token, "serverip")) {
715 if(strlen(value) == 0) {
716 cfg->c33_srvip = 0;
717 return;
718 } else {
719 cfg->c33_srvip = inet_addr(value);
720 return;
721 }
722 }
723
724 if (!strcmp(token, "nocrypt")) {
725 if(strlen(value) == 0) {
726 return;
727 } else {
728 chk_iprange(value, &cfg->c33_plain);
729 return;
730 }
731 }
732
733 if (!strcmp(token, "passive")) {
734 cfg->c33_passive = (value[0]!='0');
735 return;
736 }
737
738 if (!strcmp(token, "key")) {
739 if (key_atob(value, cfg->c33_key)) {
740 fprintf(stderr, "Configuration camd3.3x: Error in Key\n");
741 exit(1);
742 }
743 cfg->c33_crypted=1;
744 return;
745 }
746
747 if (token[0] != '#')
748 cs_log( "Warning: keyword '%s' in camd33 section not recognized",token);
749}
750
751void chk_t_camd35(char *token, char *value)
752{
753 if (!strcmp(token, "port")) {
754 if(strlen(value) == 0) {
755 cfg->c35_port = 0;
756 return;
757 } else {
758 cfg->c35_port = atoi(value);
759 return;
760 }
761 }
762
763 if (!strcmp(token, "serverip")) {
764 if(strlen(value) == 0) {
765 cfg->c35_tcp_srvip = 0;
766 return;
767 } else {
768 cfg->c35_tcp_srvip = inet_addr(value);
769 return;
770 }
771 }
772
773 if (!strcmp(token, "suppresscmd08")) {
774 if(strlen(value) == 0) {
775 cfg->c35_suppresscmd08 = 0;
776 return;
777 } else {
778 cfg->c35_suppresscmd08=atoi(value);
779 return;
780 }
781 }
782
783 if (token[0] != '#')
784 cs_log( "Warning: keyword '%s' in camd35 section not recognized", token);
785}
786
787void chk_t_camd35_tcp(char *token, char *value)
788{
789 if (!strcmp(token, "port")) {
790 if(strlen(value) == 0) {
791 clear_ptab(&cfg->c35_tcp_ptab);
792 return;
793 } else {
794 chk_port_tab(value, &cfg->c35_tcp_ptab);
795 return;
796 }
797 }
798
799 if (!strcmp(token, "serverip")) {
800 if(strlen(value) == 0) {
801 cfg->c35_tcp_srvip = 0;
802 return;
803 } else {
804 cfg->c35_tcp_srvip = inet_addr(value);
805 return;
806 }
807 }
808
809 if (!strcmp(token, "suppresscmd08")) {
810 if(strlen(value) == 0) {
811 cfg->c35_suppresscmd08 = 0;
812 return;
813 } else {
814 cfg->c35_suppresscmd08 = atoi(value);
815 return;
816 }
817 }
818
819 if (token[0] != '#')
820 cs_log( "Warning: keyword '%s' in camd35 tcp section not recognized", token);
821}
822
823void chk_t_newcamd(char *token, char *value)
824{
825 if (!strcmp(token, "port")) {
826 if(strlen(value) == 0) {
827 clear_ptab(&cfg->ncd_ptab);
828 return;
829 } else {
830 chk_port_tab(value, &cfg->ncd_ptab);
831 return;
832 }
833 }
834
835 if (!strcmp(token, "serverip")) {
836 if(strlen(value) == 0) {
837 cfg->ncd_srvip = 0;
838 return;
839 } else {
840 cfg->ncd_srvip = inet_addr(value);
841 return;
842 }
843 }
844
845 if (!strcmp(token, "allowed")) {
846 if(strlen(value) == 0) {
847 clear_sip(&cfg->ncd_allowed);
848 return;
849 } else {
850 chk_iprange(value, &cfg->ncd_allowed);
851 return;
852 }
853 }
854
855 if (!strcmp(token, "key")) {
856 if (key_atob14(value, cfg->ncd_key)) {
857 fprintf(stderr, "Configuration newcamd: Error in Key\n");
858 exit(1);
859 }
860 return;
861 }
862
863 if (!strcmp(token, "keepalive")) {
864 if(strlen(value) == 0) {
865 cfg->ncd_keepalive = 1;
866 return;
867 } else {
868 cfg->ncd_keepalive = atoi(value);
869 return;
870 }
871 }
872
873 if (token[0] != '#')
874 cs_log( "Warning: keyword '%s' in newcamd section not recognized", token);
875}
876
877void chk_t_cccam(char *token, char *value)
878{
879 if (!strcmp(token, "port")) {
880 if(strlen(value) == 0) {
881 cfg->cc_port = 0;
882 return;
883 } else {
884 cfg->cc_port = atoi(value);
885 return;
886 }
887 }
888 //if (!strcmp(token, "serverip")) { cfg->cc_srvip=inet_addr(value); return; }
889
890 if (!strcmp(token, "reshare")) {
891 if(strlen(value) == 0) {
892 cfg->cc_reshare = 0;
893 return;
894 } else {
895 cfg->cc_reshare=atoi(value);
896 return;
897 }
898 }
899 // cccam version
900 if (!strcmp(token, "version")) {
901 if (strlen(value) > sizeof(cfg->cc_version) - 1) {
902 fprintf(stderr, "cccam config: version too long\n");
903 exit(1);
904 }
905 memset(cfg->cc_version, 0, sizeof(cfg->cc_version));
906 strncpy((char*)cfg->cc_version, value, sizeof(cfg->cc_version) - 1);
907 return;
908 }
909 // cccam build number
910 if (!strcmp(token, "build")) {
911 if (strlen(value) > sizeof(cfg->cc_build) - 1) {
912 fprintf(stderr, "cccam config build number too long\n");
913 exit(1);
914 }
915 memset(cfg->cc_build, 0, sizeof(cfg->cc_build));
916 strncpy((char*)cfg->cc_build, value, sizeof(cfg->cc_build)-1);
917 return;
918 }
919
920 if (token[0] != '#')
921 cs_log( "Warning: keyword '%s' in cccam section not recognized",token);
922}
923
924void chk_t_radegast(char *token, char *value)
925{
926 if (!strcmp(token, "port")) {
927 if(strlen(value) == 0) {
928 cfg->rad_port = 0;
929 return;
930 } else {
931 cfg->rad_port = atoi(value);
932 return;
933 }
934 }
935
936 if (!strcmp(token, "serverip")) {
937 if(strlen(value) == 0) {
938 cfg->rad_srvip = 0;
939 return;
940 } else {
941 cfg->rad_srvip = inet_addr(value);
942 return;
943 }
944 }
945
946 if (!strcmp(token, "allowed")) {
947 if(strlen(value) == 0) {
948 clear_sip(&cfg->rad_allowed);
949 return;
950 } else {
951 chk_iprange(value, &cfg->rad_allowed);
952 return;
953 }
954 }
955
956 if (!strcmp(token, "user")) {
957 cs_strncpy(cfg->rad_usr, value, sizeof(cfg->rad_usr));
958 return;
959 }
960
961 if (token[0] != '#')
962 cs_log( "Warning: keyword '%s' in radegast section not recognized", token);
963}
964
965void chk_t_serial(char *token, char *value)
966{
967 if (!strcmp(token, "device")) {
968 int l;
969 l = strlen(cfg->ser_device);
970 if (l)
971 cfg->ser_device[l++]=1; // use ctrl-a as delimiter
972 cs_strncpy(cfg->ser_device+l, value, sizeof(cfg->ser_device)-l);
973 return;
974 }
975 if (token[0] != '#')
976 cs_log( "Warning: keyword '%s' in serial section not recognized", token);
977}
978
979#ifdef CS_WITH_GBOX
980static void chk_t_gbox(char *token, char *value)
981{
982// if (!strcmp(token, "password")) strncpy(cfg->gbox_pwd, i2b(4, a2i(value, 4)), 4);
983 if (!strcmp(token, "password")) { cs_atob(cfg->gbox_pwd, value, 4); return; }
984 if (!strcmp(token, "maxdist")) { cfg->maxdist=atoi(value); return; }
985 if (!strcmp(token, "ignorelist")) { cs_strncpy((char *)cfg->ignorefile, value, sizeof(cfg->ignorefile)); return; }
986 if (!strcmp(token, "onlineinfos")) { cs_strncpy((char *)cfg->gbxShareOnl, value, sizeof(cfg->gbxShareOnl)); return; }
987 if (!strcmp(token, "cardinfos")) { cs_strncpy((char *)cfg->cardfile, value, sizeof(cfg->cardfile)); return; }
988 if (!strcmp(token, "locals"))
989 {
990 char *ptr1;
991 int n=0, i;
992 for (i=0, ptr1=strtok(value, ","); (i<CS_MAXLOCALS) && (ptr1); ptr1=strtok(NULL, ","))
993 {
994 cfg->locals[n++]=a2i(ptr1, 8);
995 //printf("%i %08X",n,cfg->locals[n-1]);
996 }
997 cfg->num_locals=n;
998 return;
999 }
1000 if (token[0] != '#')
1001 fprintf(stderr, "Warning: keyword '%s' in gbox section not recognized\n",token);
1002}
1003#endif
1004
1005#ifdef HAVE_DVBAPI
1006void chk_t_dvbapi(char *token, char *value)
1007{
1008 if (!strcmp(token, "enabled")) { cfg->dvbapi_enabled=atoi(value); return; }
1009 if (!strcmp(token, "au")) { cfg->dvbapi_au=atoi(value); return; }
1010 if (!strcmp(token, "boxtype")) { cs_strncpy(cfg->dvbapi_boxtype, value, sizeof(cfg->dvbapi_boxtype)); return; }
1011 if (!strcmp(token, "user")) { cs_strncpy(cfg->dvbapi_usr, value, sizeof(cfg->dvbapi_usr)); return; }
1012 if (!strcmp(token, "priority")) { cs_strncpy(cfg->dvbapi_priority, value, sizeof(cfg->dvbapi_priority)); return; }
1013 if (!strcmp(token, "ignore")) { cs_strncpy(cfg->dvbapi_ignore, value, sizeof(cfg->dvbapi_ignore)); return; }
1014
1015 if (token[0] != '#')
1016 fprintf(stderr, "Warning: keyword '%s' in dvbapi section not recognized\n",token);
1017}
1018#endif
1019
1020static void chk_token(char *token, char *value, int tag)
1021{
1022 switch(tag) {
1023 case TAG_GLOBAL : chk_t_global(token, value); break;
1024 case TAG_MONITOR : chk_t_monitor(token, value); break;
1025 case TAG_CAMD33 : chk_t_camd33(token, value); break;
1026 case TAG_CAMD35 :
1027 case TAG_CS357X : chk_t_camd35(token, value); break;
1028 case TAG_NEWCAMD : chk_t_newcamd(token, value); break;
1029 case TAG_RADEGAST: chk_t_radegast(token, value); break;
1030 case TAG_SERIAL : chk_t_serial(token, value); break;
1031 case TAG_CS378X : chk_t_camd35_tcp(token, value); break;
1032
1033#ifdef CS_WITH_GBOX
1034 case TAG_GBOX : chk_t_gbox(token, value); break;
1035#else
1036 case TAG_GBOX : fprintf(stderr, "Warning: OSCam compiled without gbox support.\n"); break;
1037#endif
1038
1039 case TAG_CCCAM : chk_t_cccam(token, value); break;
1040#ifdef HAVE_DVBAPI
1041 case TAG_DVBAPI : chk_t_dvbapi(token, value); break;
1042#else
1043 case TAG_DVBAPI : fprintf(stderr, "Warning: OSCam compiled without DVB API support.\n"); break;
1044#endif
1045
1046
1047#ifdef WEBIF
1048 case TAG_WEBIF : chk_t_webif(token, value); break;
1049#else
1050 case TAG_WEBIF : fprintf(stderr, "Warning: OSCam compiled without Webinterface support.\n"); break;
1051#endif
1052
1053
1054#ifdef CS_ANTICASC
1055 case TAG_ANTICASC: chk_t_ac(token, value); break;
1056#else
1057 case TAG_ANTICASC: fprintf(stderr, "Warning: OSCam compiled without anticascading support.\n"); break;
1058#endif
1059
1060 }
1061}
1062
1063void init_len4caid()
1064{
1065 int nr;
1066 FILE *fp;
1067 char *value;
1068
1069 memset(len4caid, 0, sizeof(ushort)<<8);
1070 sprintf(token, "%s%s", cs_confdir, cs_l4ca);
1071 if (!(fp = fopen(token, "r")))
1072 return;
1073 for(nr = 0; fgets(token, sizeof(token), fp);) {
1074 int i, c;
1075 char *ptr;
1076 if (!(value=strchr(token, ':')))
1077 continue;
1078 *value++ ='\0';
1079 if( (ptr = strchr(value, '#')) )
1080 *ptr = '\0';
1081 if (strlen(trim(token)) != 2)
1082 continue;
1083 if (strlen(trim(value)) != 4)
1084 continue;
1085 if ((i = byte_atob(token)) < 0)
1086 continue;
1087 if ((c = word_atob(value)) < 0)
1088 continue;
1089 len4caid[i] = c;
1090 nr++;
1091 }
1092 fclose(fp);
1093 cs_log("%d lengths for caid guessing loaded", nr);
1094 return;
1095}
1096
1097int search_boxkey(ushort caid, char *key)
1098{
1099 int i, rc = 0;
1100 FILE *fp;
1101 char c_caid[512];
1102
1103 sprintf(c_caid, "%s%s", cs_confdir, cs_cert);
1104 fp = fopen(c_caid, "r");
1105 if (fp) {
1106 for (; (!rc) && fgets(c_caid, sizeof(c_caid), fp);) {
1107 char *c_provid, *c_key;
1108
1109 c_provid = strchr(c_caid, '#');
1110 if (c_provid)
1111 *c_provid = '\0';
1112 if (!(c_provid = strchr(c_caid, ':')))
1113 continue;
1114 *c_provid++ ='\0';
1115 if (!(c_key = strchr(c_provid, ':')))
1116 continue;
1117 *c_key++ ='\0';
1118 if (word_atob(trim(c_caid))!=caid)
1119 continue;
1120 if ((i=(strlen(trim(c_key))>>1)) > 256)
1121 continue;
1122 if (cs_atob((uchar *)key, c_key, i) < 0) {
1123 cs_log("wrong key in \"%s\"", cs_cert);
1124 continue;
1125 }
1126 rc = 1;
1127 }
1128 fclose(fp);
1129 }
1130#ifdef OSCAM_INBUILD_KEYS
1131 for(i=0; (!rc) && (npkey[i].keylen); i++)
1132 if (rc=((caid==npkey[i].caid) && (npkey[i].provid==0)))
1133 memcpy(key, npkey[i].key, npkey[i].keylen);
1134#endif
1135 return(rc);
1136}
1137
1138int init_config()
1139{
1140 int tag=TAG_GLOBAL;
1141 FILE *fp;
1142 char *value;
1143
1144#ifndef CS_EMBEDDED
1145#ifdef PRIO_PROCESS
1146 errno=0;
1147 if ((cfg->nice = getpriority(PRIO_PROCESS, 0)) == (-1))
1148 if (errno)
1149#endif
1150#endif
1151 cfg->nice = 99;
1152 cfg->ctimeout = CS_CLIENT_TIMEOUT;
1153 cfg->ftimeout = CS_CLIENT_TIMEOUT / 2;
1154 cfg->cmaxidle = CS_CLIENT_MAXIDLE;
1155 cfg->delay = CS_DELAY;
1156 cfg->bindwait = CS_BIND_TIMEOUT;
1157 cfg->resolvedelay = CS_RESOLVE_DELAY;
1158 cfg->mon_level = 2;
1159 cfg->mon_hideclient_to = 0;
1160 cfg->srtimeout = 1500;
1161 cfg->ulparent = 0;
1162 cfg->logfile = NULL;
1163 cfg->pidfile = NULL;
1164 cfg->usrfile = NULL;
1165 cfg->cwlogdir = NULL;
1166#ifdef WEBIF
1167 strcpy(cfg->http_user, "");
1168 strcpy(cfg->http_pwd, "");
1169 strcpy(cfg->http_css, "");
1170 cfg->http_refresh = 0;
1171 cfg->http_hide_idle_clients = 0;
1172 strcpy(cfg->http_tpl, "");
1173#endif
1174 cfg->ncd_keepalive = 1;
1175#ifdef CS_ANTICASC
1176 cfg->ac_enabled = 0;
1177 cfg->ac_users = 0;
1178 cfg->ac_stime = 2;
1179 cfg->ac_samples = 10;
1180 cfg->ac_denysamples = 8;
1181 cfg->ac_fakedelay = 1000;
1182 strcpy(cfg->ac_logfile, "./oscam_ac.log");
1183#endif
1184 sprintf(token, "%s%s", cs_confdir, cs_conf);
1185 if (!(fp = fopen(token, "r"))) {
1186 fprintf(stderr, "Cannot open config file '%s' (errno=%d)\n", token, errno);
1187 exit(1);
1188 }
1189 while (fgets(token, sizeof(token), fp)) {
1190 int i, l;
1191 //void *ptr;
1192 if ((l = strlen(trim(token))) < 3)
1193 continue;
1194 if ((token[0] == '[') && (token[l-1] == ']')) {
1195 for (token[l-1] = 0, tag = -1, i = TAG_GLOBAL; cctag[i]; i++)
1196 if (!strcmp(cctag[i], strtolower(token+1)))
1197 tag = i;
1198 continue;
1199 }
1200 if (!(value=strchr(token, '=')))
1201 continue;
1202 *value++ ='\0';
1203 chk_token(trim(strtolower(token)), trim(value), tag);
1204 }
1205 fclose(fp);
1206#ifdef CS_LOGFILE
1207 if (cfg->logfile == NULL)
1208 asprintf(&(cfg->logfile), "%s", CS_LOGFILE);
1209#endif
1210 cs_init_log(cfg->logfile);
1211 if (cfg->ftimeout >= cfg->ctimeout) {
1212 cfg->ftimeout = cfg->ctimeout - 100;
1213 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be smaller than clienttimeout (%lu ms))", cfg->ftimeout, cfg->ctimeout);
1214 }
1215 if(cfg->ftimeout < cfg->srtimeout) {
1216 cfg->ftimeout = cfg->srtimeout + 100;
1217 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ftimeout, cfg->srtimeout);
1218 }
1219 if(cfg->ctimeout < cfg->srtimeout) {
1220 cfg->ctimeout = cfg->srtimeout + 100;
1221 cs_log("WARNING: clienttimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ctimeout, cfg->srtimeout);
1222 }
1223#ifdef CS_ANTICASC
1224 if( cfg->ac_denysamples+1 > cfg->ac_samples ) {
1225 cfg->ac_denysamples = cfg->ac_samples - 1;
1226 cs_log("WARNING: DenySamples adjusted to %d", cfg->ac_denysamples);
1227 }
1228#endif
1229 return 0;
1230}
1231
1232void chk_account(char *token, char *value, struct s_auth *account)
1233{
1234 int i;
1235 char *ptr1;
1236
1237 if (!strcmp(token, "user")) {
1238 cs_strncpy(account->usr, value, sizeof(account->usr));
1239 return;
1240 }
1241
1242 if (!strcmp(token, "pwd")) {
1243 cs_strncpy(account->pwd, value, sizeof(account->pwd));
1244 return;
1245 }
1246
1247 if (!strcmp(token, "hostname")) {
1248 cs_strncpy((char *)account->dyndns, value, sizeof(account->dyndns));
1249 return;
1250 }
1251
1252 if (!strcmp(token, "betatunnel")) {
1253 if(strlen(value) == 0) {
1254 clear_tuntab(&account->ttab);
1255 return;
1256 } else {
1257 chk_tuntab(value, &account->ttab);
1258 return;
1259 }
1260 }
1261
1262 if (!strcmp(token, "uniq")) {
1263 if(strlen(value) == 0) {
1264 account->uniq = 0;
1265 return;
1266 } else {
1267 account->uniq = atoi(value);
1268 return;
1269 }
1270 }
1271
1272 if (!strcmp(token, "sleep")) {
1273 if(strlen(value) == 0) {
1274 account->tosleep = 0;
1275 return;
1276 } else {
1277 account->tosleep=atoi(value);
1278 return;
1279 }
1280 }
1281
1282 if (!strcmp(token, "monlevel")) {
1283 if(strlen(value) == 0) {
1284 account->monlvl = 0;
1285 return;
1286 } else {
1287 account->monlvl = atoi(value);
1288 return;
1289 }
1290 }
1291
1292 if (!strcmp(token, "caid")) {
1293 if(strlen(value) == 0) {
1294 clear_caidtab(&account->ctab);
1295 return;
1296 } else {
1297 chk_caidtab(value, &account->ctab);
1298 return;
1299 }
1300 }
1301
1302 if (!strcmp(token, "disabled")) {
1303 if(strlen(value) == 0) {
1304 account->disabled = 0;
1305 return;
1306 } else {
1307 account->disabled = atoi(value);
1308 return;
1309 }
1310 }
1311
1312 if (!strcmp(token, "suppresscmd08")) {
1313 if(strlen(value) == 0) {
1314 account->c35_suppresscmd08 = 0;
1315 return;
1316 } else {
1317 account->c35_suppresscmd08=atoi(value);
1318 return;
1319 }
1320 }
1321
1322 if (!strcmp(token, "keepalive")) {
1323 if(strlen(value) == 0) {
1324 account->ncd_keepalive = 1;
1325 return;
1326 } else {
1327 account->ncd_keepalive = atoi(value);
1328 return;
1329 }
1330 }
1331 /*
1332 * case insensitive
1333 */
1334 strtolower(value);
1335
1336 if (!strcmp(token, "au")) {
1337 //set default values for usage during runtime from Webif
1338 account->au=-1;
1339 account->autoau=0;
1340
1341 if(value && value[0]=='1') account->autoau=1;
1342 for (i=0; i<CS_MAXREADER; i++)
1343 if ((reader[i].label[0]) && (!strncmp(reader[i].label, value, strlen(reader[i].label))))
1344 account->au=i;
1345 return;
1346 }
1347
1348 if (!strcmp(token, "group")) {
1349 account->grp = 0;
1350 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ",")) {
1351 int g;
1352 g = atoi(ptr1);
1353 if ((g>0) && (g < 33)) account->grp|=(1<<(g-1));
1354 }
1355 return;
1356 }
1357
1358 if(!strcmp(token, "services")) {
1359 chk_services(value, &account->sidtabok, &account->sidtabno);
1360 return;
1361 }
1362
1363 if(!strcmp(token, "ident")) { /*ToDo ftab clear*/
1364 chk_ftab(value, &account->ftab, "user", account->usr, "provid");
1365 return;
1366 }
1367
1368 if(!strcmp(token, "class")) {
1369 chk_cltab(value, &account->cltab);
1370 return;
1371 }
1372
1373 if(!strcmp(token, "chid")) {
1374 chk_ftab(value, &account->fchid, "user", account->usr, "chid");
1375 return;
1376 }
1377
1378 if (!strcmp(token, "expdate")) {
1379 if (!value[0]) {
1380 account->expirationdate=(time_t)NULL;
1381 return;
1382 }
1383 struct tm cstime;
1384 memset(&cstime,0,sizeof(cstime));
1385 for (i=0, ptr1=strtok(value, "-/"); (i<3)&&(ptr1); ptr1=strtok(NULL, "-/"), i++) {
1386 switch(i) {
1387 case 0: cstime.tm_year=atoi(ptr1)-1900; break;
1388 case 1: cstime.tm_mon =atoi(ptr1)-1; break;
1389 case 2: cstime.tm_mday=atoi(ptr1); break;
1390 }
1391 }
1392 account->expirationdate=mktime(&cstime);
1393 return;
1394 }
1395
1396#ifdef CS_ANTICASC
1397 if( !strcmp(token, "numusers") ) {
1398 account->ac_users = atoi(value);
1399 return;
1400 }
1401
1402 if( !strcmp(token, "penalty") ) {
1403 account->ac_penalty = atoi(value);
1404 return;
1405 }
1406#endif
1407
1408 if (token[0] != '#')
1409 cs_log( "Warning: keyword '%s' in account section not recognized",token);
1410}
1411
1412int write_services()
1413{
1414 int i;
1415 FILE *f;
1416 struct s_sidtab *sidtab = cfg->sidtab;
1417 char tmpfile[256];
1418 char destfile[256];
1419 char bakfile[256];
1420
1421 snprintf(destfile, 255,"%s%s", cs_confdir, cs_sidt);
1422 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_sidt);
1423 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_sidt);
1424
1425 if (!(f=fopen(tmpfile, "w"))){
1426 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
1427 return(1);
1428 }
1429 fprintf(f,"#oscam.services generated automatically\n\n");
1430
1431 while(sidtab != NULL){
1432 fprintf(f,"[%s]\n", sidtab->label);
1433 fprintf_conf(f, CONFVARWIDTH, "caid", "");
1434 for (i=0; i<sidtab->num_caid; i++){
1435 if (i==0) fprintf(f,"%04X", sidtab->caid[i]);
1436 else fprintf(f,",%04X", sidtab->caid[i]);
1437 }
1438 fputc((int)'\n', f);
1439 fprintf_conf(f, CONFVARWIDTH, "provid", "");
1440 for (i=0; i<sidtab->num_provid; i++){
1441 if (i==0) fprintf(f,"%06lX", sidtab->provid[i]);
1442 else fprintf(f,",%06lX", sidtab->provid[i]);
1443 }
1444 fputc((int)'\n', f);
1445 fprintf_conf(f, CONFVARWIDTH, "srvid", "");
1446 for (i=0; i<sidtab->num_srvid; i++){
1447 if (i==0) fprintf(f,"%04X", sidtab->srvid[i]);
1448 else fprintf(f,",%04X", sidtab->srvid[i]);
1449 }
1450 fprintf(f,"\n\n");
1451 sidtab=sidtab->next;
1452 }
1453
1454 fclose(f);
1455 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1456}
1457
1458int write_config()
1459{
1460 int i,j;
1461 FILE *f;
1462 char *dot = "", *dot1 = "", *dot2 = ""; //flags for delimiters
1463 char tmpfile[256];
1464 char destfile[256];
1465 char bakfile[256];
1466
1467 snprintf(destfile, 255,"%s%s", cs_confdir, cs_conf);
1468 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_conf);
1469 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_conf);
1470
1471 if (!(f=fopen(tmpfile, "w"))){
1472 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
1473 return(1);
1474 }
1475 fprintf(f,"#oscam.config generated automatically\n\n");
1476
1477 /*global settings*/
1478 fprintf(f,"[global]\n");
1479 if (cfg->srvip != 0)
1480 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->srvip));
1481 if (cfg->pidfile != NULL) fprintf_conf(f, CONFVARWIDTH, "pidfile", "%s\n", cfg->pidfile);
1482 if (cfg->usrfile != NULL) fprintf_conf(f, CONFVARWIDTH, "usrfile", "%s\n", cfg->usrfile);
1483 if (cfg->logfile != NULL) fprintf_conf(f, CONFVARWIDTH, "logfile", "%s\n", cfg->logfile);
1484 if (cfg->cwlogdir != NULL) fprintf_conf(f, CONFVARWIDTH, "cwlogdir", "%s\n", cfg->cwlogdir);
1485 fprintf_conf(f, CONFVARWIDTH, "disablelog", "%d\n", cfg->disablelog);
1486 fprintf_conf(f, CONFVARWIDTH, "usrfileflag", "%d\n", cfg->usrfileflag);
1487 fprintf_conf(f, CONFVARWIDTH, "clienttimeout", "%ld\n", cfg->ctimeout/1000);
1488 fprintf_conf(f, CONFVARWIDTH, "fallbacktimeout", "%ld\n", cfg->ftimeout/1000);
1489 fprintf_conf(f, CONFVARWIDTH, "clientmaxidle", "%d\n", cfg->cmaxidle);
1490 fprintf_conf(f, CONFVARWIDTH, "cachedelay", "%ld\n", cfg->delay);
1491 fprintf_conf(f, CONFVARWIDTH, "bindwait", "%d\n", cfg->bindwait);
1492 fprintf_conf(f, CONFVARWIDTH, "netprio", "%ld\n", cfg->netprio);
1493 fprintf_conf(f, CONFVARWIDTH, "resolvedelay", "%d\n", cfg->resolvedelay);
1494 if (cfg->tosleep) fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", cfg->tosleep);
1495 fprintf_conf(f, CONFVARWIDTH, "unlockparental", "%d\n", cfg->ulparent);
1496 fprintf_conf(f, CONFVARWIDTH, "nice", "%d\n", cfg->nice);
1497 fprintf_conf(f, CONFVARWIDTH, "serialreadertimeout", "%d\n", cfg->srtimeout);
1498 fprintf_conf(f, CONFVARWIDTH, "maxlogsize", "%d\n", cfg->max_log_size);
1499 fprintf_conf(f, CONFVARWIDTH, "waitforcards", "%d\n", cfg->waitforcards);
1500 fprintf_conf(f, CONFVARWIDTH, "preferlocalcards", "%d\n", cfg->preferlocalcards);
1501 fputc((int)'\n', f);
1502
1503 /*monitor settings*/
1504 fprintf(f,"[monitor]\n");
1505 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->mon_port);
1506 if (cfg->mon_srvip != 0)
1507 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
1508
1509 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
1510 struct s_ip *cip;
1511 for (cip = cfg->mon_allowed; cip; cip = cip->next){
1512 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1513 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1514 dot=",";
1515 }
1516 fputc((int)'\n', f);
1517 fprintf_conf(f, CONFVARWIDTH, "aulow", "%d\n", cfg->mon_aulow);
1518 fprintf_conf(f, CONFVARWIDTH, "hideclient_to", "%d\n", cfg->mon_hideclient_to);
1519 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", cfg->mon_level);
1520 fputc((int)'\n', f);
1521
1522 /*newcamd*/
1523 if ((cfg->ncd_ptab.nports > 0) && (cfg->ncd_ptab.ports[0].s_port > 0)){
1524 fprintf(f,"[newcamd]\n");
1525 fprintf_conf(f, CONFVARWIDTH, "port", "");
1526 dot1 = "";
1527 for(i = 0; i < cfg->ncd_ptab.nports; ++i){
1528 fprintf(f,"%s%d@%04X", dot1, cfg->ncd_ptab.ports[i].s_port, cfg->ncd_ptab.ports[i].ftab.filts[0].caid);
1529 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0){
1530 fprintf(f,":");
1531 dot2 = "";
1532 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j){
1533 fprintf(f,"%s%06X", dot2, (int)cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
1534 dot2 = ",";
1535 }
1536 }
1537 dot1=";";
1538 }
1539
1540 fputc((int)'\n', f);
1541 if (cfg->ncd_srvip != 0)
1542 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
1543 fprintf_conf(f, CONFVARWIDTH, "key", "");
1544 for (i=0;i<14;i++) fprintf(f,"%02X", cfg->ncd_key[i]);
1545 fprintf(f,"\n");
1546 fprintf_conf(f, CONFVARWIDTH, "allowed", "");
1547 struct s_ip *cip;
1548 dot="";
1549 for (cip = cfg->ncd_allowed; cip; cip = cip->next){
1550 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1551 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1552 dot=",";
1553 }
1554 fprintf(f,"\n");
1555 fprintf_conf(f, CONFVARWIDTH, "keepalive", "%d\n", cfg->ncd_keepalive);
1556 fprintf(f,"\n");
1557 }
1558
1559 /*camd3.3*/
1560 if ( cfg->c33_port > 0) {
1561 fprintf(f,"[camd33]\n");
1562 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c33_port);
1563 if (cfg->c33_srvip != 0)
1564 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
1565 fprintf_conf(f, CONFVARWIDTH, "passive", "%d\n", cfg->c33_passive);
1566 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);
1567 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
1568 dot="";
1569 for (cip = cfg->c33_plain; cip; cip = cip->next){
1570 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1571 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1572 dot=",";
1573 }
1574 fprintf(f,"\n\n");
1575 }
1576
1577 /*camd3.5*/
1578 if ( cfg->c35_port > 0) {
1579 fprintf(f,"[cs357x]\n");
1580 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c35_port);
1581 if (cfg->c35_tcp_srvip != 0)
1582 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1583 if (cfg->c35_suppresscmd08)
1584 fprintf_conf(f, CONFVARWIDTH, "suppresscmd08", "%d\n", cfg->c35_suppresscmd08);
1585 fprintf(f,"\n");
1586 }
1587
1588 /*camd3.5 TCP*/
1589 if ((cfg->c35_tcp_ptab.nports > 0) && (cfg->c35_tcp_ptab.ports[0].s_port > 0)) {
1590 fprintf(f,"[cs378x]\n");
1591 fprintf_conf(f, CONFVARWIDTH, "port", "");
1592 dot1 = "";
1593 for(i = 0; i < cfg->c35_tcp_ptab.nports; ++i){
1594 fprintf(f,"%s%d@%04X", dot1, cfg->c35_tcp_ptab.ports[i].s_port, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].caid);
1595 if (cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids > 0){
1596 fprintf(f,":");
1597 dot2 = "";
1598 for (j = 0; j < cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids; ++j){
1599 fprintf(f,"%s%lX", dot2, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].prids[j]);
1600 dot2 = ",";
1601 }
1602 }
1603 dot1=";";
1604 }
1605
1606 fputc((int)'\n', f);
1607 if (cfg->c35_tcp_srvip != 0)
1608 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1609 fputc((int)'\n', f);
1610 }
1611
1612 /*Radegast*/
1613 if ( cfg->rad_port > 0) {
1614 fprintf(f,"[radegast]\n");
1615 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->rad_port);
1616 if (cfg->rad_srvip != 0)
1617 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
1618 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->rad_usr);
1619 fprintf_conf(f, CONFVARWIDTH, "allowed", "");
1620 struct s_ip *cip;
1621 for (cip = cfg->rad_allowed; cip; cip = cip->next){
1622 fprintf(f,"%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
1623 if (cip->ip[0] == cip->ip[1]) fprintf(f,"-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
1624 dot=",";
1625 }
1626 fprintf(f,"\n\n");
1627 }
1628
1629#ifdef CS_WITH_GBOX
1630 /*Gbox*/
1631 if ((cfg->gbox_pwd[0] > 0) || (cfg->gbox_pwd[1] > 0) || (cfg->gbox_pwd[2] > 0) || (cfg->gbox_pwd[3] > 0)){
1632 fprintf(f,"[gbox]\n");
1633 fprintf_conf(f, CONFVARWIDTH, "password", ""); for (i=0;i<4;i++) fprintf(f,"%02X", cfg->gbox_pwd[i]); fputc((int)'\n', f);;
1634 fprintf_conf(f, CONFVARWIDTH, "maxdist", "%d\n", cfg->maxdist);
1635 fprintf_conf(f, CONFVARWIDTH, "ignorelist", "%s\n", cfg->ignorefile);
1636 fprintf_conf(f, CONFVARWIDTH, "onlineinfos", "%s\n", cfg->gbxShareOnl);
1637 fprintf_conf(f, CONFVARWIDTH, "cardinfos", "%s\n", cfg->cardfile);
1638 fprintf_conf(f, CONFVARWIDTH, "locals", "");
1639 char *dot = "";
1640 for (i = 0; i < cfg->num_locals; i++){
1641 fprintf(f,"%s%06lX", dot, cfg->locals[i]);
1642 dot=";";
1643 }
1644 fprintf(f,"\n\n");
1645 }
1646#endif
1647
1648 /*serial*/
1649
1650
1651 /*cccam*/
1652 if ( cfg->cc_port > 0) {
1653 fprintf(f,"[cccam]\n");
1654 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->cc_port);
1655 fprintf_conf(f, CONFVARWIDTH, "reshare", "%d\n", cfg->cc_reshare);
1656 fprintf_conf(f, CONFVARWIDTH, "version", "%s\n", cfg->cc_version);
1657 fprintf_conf(f, CONFVARWIDTH, "build", "%s\n", cfg->cc_build);
1658 fprintf(f,"\n");
1659 }
1660
1661#ifdef HAVE_DVBAPI
1662 /*dvb-api*/
1663 if (cfg->dvbapi_enabled > 0) {
1664 fprintf(f,"[dvbapi]\n");
1665 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->dvbapi_enabled);
1666 fprintf_conf(f, CONFVARWIDTH, "au", "%d\n", cfg->dvbapi_au);
1667 fprintf_conf(f, CONFVARWIDTH, "boxtype", "%s\n", cfg->dvbapi_boxtype);
1668 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->dvbapi_usr);
1669 fputc((int)'\n', f);
1670 }
1671#endif
1672
1673#ifdef WEBIF
1674 /*webinterface*/
1675 if (cfg->http_port > 0) {
1676 fprintf(f,"[webif]\n");
1677 fprintf_conf(f, CONFVARWIDTH, "httpport", "%d\n", cfg->http_port);
1678 fprintf_conf(f, CONFVARWIDTH, "httpuser", "%s\n", cfg->http_user);
1679 fprintf_conf(f, CONFVARWIDTH, "httppwd", "%s\n", cfg->http_pwd);
1680 fprintf_conf(f, CONFVARWIDTH, "httpcss", "%s\n", cfg->http_css);
1681 fprintf_conf(f, CONFVARWIDTH, "httpscript", "%s\n", cfg->http_script);
1682 fprintf_conf(f, CONFVARWIDTH, "httprefresh", "%d\n", cfg->http_refresh);
1683 fprintf_conf(f, CONFVARWIDTH, "httphideidleclients", "%d\n", cfg->http_hide_idle_clients);
1684 fputc((int)'\n', f);
1685 }
1686#endif
1687
1688#ifdef CS_ANTICASC
1689 fprintf(f,"[anticasc]\n");
1690 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->ac_enabled);
1691 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", cfg->ac_users);
1692 fprintf_conf(f, CONFVARWIDTH, "sampletime", "%d\n", cfg->ac_stime);
1693 fprintf_conf(f, CONFVARWIDTH, "samples", "%d\n", cfg->ac_samples);
1694 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", cfg->ac_penalty);
1695 fprintf_conf(f, CONFVARWIDTH, "aclogfile", "%s\n", cfg->ac_logfile);
1696 fprintf_conf(f, CONFVARWIDTH, "denysamples", "%d\n", cfg->ac_denysamples);
1697 fprintf_conf(f, CONFVARWIDTH, "fakedelay", "%d\n", cfg->ac_fakedelay);
1698 fputc((int)'\n', f);
1699#endif
1700
1701 fclose(f);
1702
1703 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1704}
1705
1706int write_userdb()
1707{
1708 int i;
1709 FILE *f;
1710 struct s_auth *account;
1711 char *dot = ""; //flag for comma
1712 char tmpfile[256];
1713 char destfile[256];
1714 char bakfile[256];
1715
1716 snprintf(destfile, 255,"%s%s", cs_confdir, cs_user);
1717 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_user);
1718 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_user);
1719
1720 if (!(f=fopen(tmpfile, "w"))){
1721 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
1722 return(1);
1723 }
1724 fprintf(f,"#oscam.user generated automatically\n\n");
1725
1726 //each account
1727 for (account=cfg->account; (account) ; account=account->next){
1728 fprintf(f,"[account]\n");
1729 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", account->usr);
1730 fprintf_conf(f, CONFVARWIDTH, "pwd", "%s\n", account->pwd);
1731 fprintf_conf(f, CONFVARWIDTH, "disabled", "%d\n", account->disabled);
1732 struct tm * timeinfo = localtime (&account->expirationdate);
1733 char buf [80];
1734 strftime (buf,80,"%Y-%m-%d",timeinfo);
1735 if(strcmp(buf,"1970-01-01"))
1736 fprintf_conf(f, CONFVARWIDTH, "expdate", "%s\n", buf);
1737 else
1738 fprintf_conf(f, CONFVARWIDTH, "expdate", "\n");
1739
1740 //group
1741 char *value = mk_t_group((ulong*)account->grp);
1742 fprintf_conf(f, CONFVARWIDTH, "group", "%s\n", value);
1743 free(value);
1744
1745 fprintf_conf(f, CONFVARWIDTH, "hostname", "%s\n", account->dyndns);
1746 fprintf_conf(f, CONFVARWIDTH, "uniq", "%d\n", account->uniq);
1747 fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", account->tosleep);
1748 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", account->monlvl);
1749
1750 if (account->au > -1)
1751 if (account->au < CS_MAXREADER)
1752 fprintf_conf(f, CONFVARWIDTH, "au", "%s\n", reader[account->au].label);
1753 if (account->autoau == 1) fprintf_conf(f, CONFVARWIDTH, "au", "1\n");
1754
1755 fprintf_conf(f, CONFVARWIDTH, "services", "");
1756 char sidok[33]; long2bitchar(account->sidtabok,sidok);
1757 char sidno[33]; long2bitchar(account->sidtabno,sidno);
1758 struct s_sidtab *sidtab = cfg->sidtab;
1759 i=0; dot = "";
1760 for (; sidtab; sidtab=sidtab->next){
1761 if(sidok[i]=='1') {fprintf(f,"%s%s", dot, sidtab->label); dot = ",";}
1762 if(sidno[i]=='1') {fprintf(f,"%s!%s", dot, sidtab->label); dot = ",";}
1763 i++;
1764 }
1765 fputc((int)'\n', f);
1766
1767 //CAID
1768 value = mk_t_caidtab(&account->ctab);
1769 fprintf_conf(f, CONFVARWIDTH, "caid", "%s\n", value);
1770 free(value);
1771
1772 //betatunnel
1773 value = mk_t_tuntab(&account->ttab);
1774 fprintf_conf(f, CONFVARWIDTH, "betatunnel", "%s\n", value);
1775 free(value);
1776
1777 //ident
1778 value = mk_t_ftab(&account->ftab);
1779 fprintf_conf(f, CONFVARWIDTH, "ident", "%s\n", value);
1780 free(value);
1781
1782 if (account->c35_suppresscmd08)
1783 fprintf_conf(f, CONFVARWIDTH, "suppresscmd08", "%d\n", account->c35_suppresscmd08);
1784
1785 fprintf_conf(f, CONFVARWIDTH, "keepalive", "%d\n", account->ncd_keepalive);
1786
1787#ifdef CS_ANTICASC
1788 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", account->ac_users);
1789 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", account->ac_penalty);
1790#endif
1791 fputc((int)'\n', f);
1792 }
1793 fclose(f);
1794
1795 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1796}
1797
1798int init_userdb()
1799{
1800 int tag = 0, nr, nro, expired, disabled;
1801 //int first=1;
1802 FILE *fp;
1803 char *value;
1804 struct s_auth *ptr;
1805 /*static */struct s_auth *account=(struct s_auth *)0;
1806
1807 sprintf(token, "%s%s", cs_confdir, cs_user);
1808 if (!(fp = fopen(token, "r"))) {
1809 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1810 return(1);
1811 }
1812
1813 for (nro = 0, ptr = cfg->account; ptr; nro++) {
1814 struct s_auth *ptr_next;
1815 ptr_next = ptr->next;
1816 free(ptr);
1817 ptr = ptr_next;
1818 }
1819 nr = 0;
1820
1821 while (fgets(token, sizeof(token), fp)) {
1822 int i, l;
1823 void *ptr;
1824
1825 if ((l=strlen(trim(token))) < 3)
1826 continue;
1827
1828 if ((token[0] == '[') && (token[l-1] == ']')) {
1829 token[l - 1] = 0;
1830 tag = (!strcmp("account", strtolower(token + 1)));
1831
1832 if (!(ptr=malloc(sizeof(struct s_auth)))) {
1833 cs_log("Error allocating memory (errno=%d)", errno);
1834 return(1);
1835 }
1836
1837 if (account)
1838 account->next = ptr;
1839 else
1840 cfg->account = ptr;
1841
1842 account = ptr;
1843 memset(account, 0, sizeof(struct s_auth));
1844 account->au = (-1);
1845 account->monlvl = cfg->mon_level;
1846 account->tosleep = cfg->tosleep;
1847 account->c35_suppresscmd08 = cfg->c35_suppresscmd08;
1848 account->ncd_keepalive = cfg->ncd_keepalive;
1849 for (i = 1; i < CS_MAXCAIDTAB; account->ctab.mask[i++] = 0xffff);
1850 for (i = 1; i < CS_MAXTUNTAB; account->ttab.bt_srvid[i++] = 0x0000);
1851 nr++;
1852
1853#ifdef CS_ANTICASC
1854 account->ac_users = cfg->ac_users;
1855 account->ac_penalty = cfg->ac_penalty;
1856 account->ac_idx = nr;
1857#endif
1858 continue;
1859 }
1860
1861 if (!tag)
1862 continue;
1863
1864 if (!(value=strchr(token, '=')))
1865 continue;
1866
1867 *value++ = '\0';
1868 chk_account(trim(strtolower(token)), trim(value), account);
1869 }
1870
1871 fclose(fp);
1872
1873 for (expired = 0, disabled = 0, ptr = cfg->account; ptr;) {
1874
1875 if(ptr->expirationdate && ptr->expirationdate < time(NULL))
1876 expired++;
1877
1878 if(ptr->disabled != 0)
1879 disabled++;
1880
1881 ptr = ptr->next;
1882 }
1883
1884 cs_log("userdb reloaded: %d accounts freed, %d accounts loaded, %d expired, %d disabled", nro, nr, expired, disabled);
1885 return(0);
1886}
1887
1888static void chk_entry4sidtab(char *value, struct s_sidtab *sidtab, int what)
1889{
1890 int i, b;
1891 char *ptr;
1892 ushort *slist=(ushort *) 0;
1893 ulong *llist=(ulong *) 0;
1894 ulong caid;
1895 char buf[strlen(value) + 1];
1896 cs_strncpy(buf, value, sizeof(buf));
1897 b=(what==1) ? sizeof(ulong) : sizeof(ushort);
1898 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1899 {
1900 caid=a2i(ptr, b);
1901 if (!errno) i++;
1902 }
1903 //if (!i) return(0);
1904 if (b==sizeof(ushort))
1905 slist=malloc(i*sizeof(ushort));
1906 else
1907 llist=malloc(i*sizeof(ulong));
1908 strcpy(value, buf);
1909 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1910 {
1911 caid=a2i(ptr, b);
1912 if (errno) continue;
1913 if (b==sizeof(ushort))
1914 slist[i++]=(ushort) caid;
1915 else
1916 llist[i++]=caid;
1917 }
1918 switch (what)
1919 {
1920 case 0: sidtab->caid=slist;
1921 sidtab->num_caid=i;
1922 break;
1923 case 1: sidtab->provid=llist;
1924 sidtab->num_provid=i;
1925 break;
1926 case 2: sidtab->srvid=slist;
1927 sidtab->num_srvid=i;
1928 break;
1929 }
1930}
1931
1932void chk_sidtab(char *token, char *value, struct s_sidtab *sidtab)
1933{
1934 if (!strcmp(token, "caid")) { chk_entry4sidtab(value, sidtab, 0); return; }
1935 if (!strcmp(token, "provid")) { chk_entry4sidtab(value, sidtab, 1); return; }
1936 if (!strcmp(token, "ident")) { chk_entry4sidtab(value, sidtab, 1); return; }
1937 if (!strcmp(token, "srvid")) { chk_entry4sidtab(value, sidtab, 2); return; }
1938 if (token[0] != '#')
1939 fprintf(stderr, "Warning: keyword '%s' in sidtab section not recognized\n",token);
1940}
1941
1942int init_sidtab()
1943{
1944 int nr, nro;
1945 FILE *fp;
1946 char *value;
1947 struct s_sidtab *ptr;
1948 struct s_sidtab *sidtab=(struct s_sidtab *)0;
1949
1950 sprintf(token, "%s%s", cs_confdir, cs_sidt);
1951 if (!(fp=fopen(token, "r")))
1952 {
1953 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1954 return(1);
1955 }
1956 for (nro=0, ptr=cfg->sidtab; ptr; nro++)
1957 {
1958 struct s_sidtab *ptr_next;
1959 ptr_next=ptr->next;
1960 if (ptr->caid) free(ptr->caid);
1961 if (ptr->provid) free(ptr->provid);
1962 if (ptr->srvid) free(ptr->srvid);
1963 free(ptr);
1964 ptr=ptr_next;
1965 }
1966 nr=0;
1967 while (fgets(token, sizeof(token), fp))
1968 {
1969 int l;
1970 void *ptr;
1971 if ((l=strlen(trim(token)))<3) continue;
1972 if ((token[0]=='[') && (token[l-1]==']'))
1973 {
1974 token[l-1]=0;
1975 if (!(ptr=malloc(sizeof(struct s_sidtab))))
1976 {
1977 cs_log("Error allocating memory (errno=%d)", errno);
1978 return(1);
1979 }
1980 if (sidtab)
1981 sidtab->next=ptr;
1982 else
1983 cfg->sidtab=ptr;
1984 sidtab=ptr;
1985 nr++;
1986 memset(sidtab, 0, sizeof(struct s_sidtab));
1987 cs_strncpy(sidtab->label, strtolower(token+1), sizeof(sidtab->label));
1988 continue;
1989 }
1990 if (!sidtab) continue;
1991 if (!(value=strchr(token, '='))) continue;
1992 *value++='\0';
1993 chk_sidtab(trim(strtolower(token)), trim(strtolower(value)), sidtab);
1994 }
1995 fclose(fp);
1996
1997#ifdef DEBUG_SIDTAB
1998 show_sidtab(cfg->sidtab);
1999#endif
2000 cs_log("services reloaded: %d services freed, %d services loaded", nro, nr);
2001 return(0);
2002}
2003
2004int init_srvid()
2005{
2006 int nr;
2007 FILE *fp;
2008 char *payload;
2009 static struct s_srvid *srvid=(struct s_srvid *)0;
2010 sprintf(token, "%s%s", cs_confdir, cs_srid);
2011
2012 if (!(fp=fopen(token, "r"))) {
2013 cs_log("can't open file \"%s\" (err=%d), no service-id's loaded", token, errno);
2014 return(0);
2015 }
2016
2017 nr=0;
2018 while (fgets(token, sizeof(token), fp)) {
2019
2020 int l;
2021 void *ptr;
2022 char *tmp;
2023 tmp = trim(token);
2024
2025 if (tmp[0] == '#') continue;
2026 if ((l=strlen(tmp)) < 6) continue;
2027 if (!(payload=strchr(token, '|'))) continue;
2028 *payload++ = '\0';
2029
2030 if (!(ptr = malloc(sizeof(struct s_srvid)))) {
2031 cs_log("Error allocating memory (errno=%d)", errno);
2032 return(1);
2033 }
2034
2035 if (srvid)
2036 srvid->next = ptr;
2037 else
2038 cfg->srvid = ptr;
2039
2040 srvid = ptr;
2041 memset(srvid, 0, sizeof(struct s_srvid));
2042
2043 int i;
2044 char *ptr1;
2045 for (i = 0, ptr1 = strtok(payload, "|"); ptr1; ptr1 = strtok(NULL, "|"), i++){
2046 switch(i){
2047 case 0:
2048 cs_strncpy(srvid->prov, trim(ptr1), sizeof(srvid->prov));
2049 break;
2050 case 1:
2051 cs_strncpy(srvid->name, trim(ptr1), sizeof(srvid->name));
2052 break;
2053 case 2:
2054 cs_strncpy(srvid->type, trim(ptr1), sizeof(srvid->type));
2055 break;
2056 case 3:
2057 cs_strncpy(srvid->desc, trim(ptr1), sizeof(srvid->desc));
2058 break;
2059 }
2060 }
2061
2062 char *srvidasc = strchr(token, ':');
2063 *srvidasc++ = '\0';
2064 srvid->srvid = word_atob(srvidasc);
2065
2066 srvid->ncaid = 0;
2067 for (i = 0, ptr1 = strtok(token, ","); (ptr1) && (i < 10) ; ptr1 = strtok(NULL, ","), i++){
2068 srvid->caid[i] = word_atob(ptr1);
2069 srvid->ncaid = i+1;
2070 //cs_debug("ld caid: %04X srvid: %04X Prov: %s Chan: %s",srvid->caid[i],srvid->srvid,srvid->prov,srvid->name);
2071 }
2072 nr++;
2073 }
2074
2075 fclose(fp);
2076 if (nr>0)
2077 cs_log("%d service-id's loaded", nr);
2078 else{
2079 cs_log("oscam.srvid loading failed, old format");
2080 }
2081 return(0);
2082}
2083
2084static void chk_reader(char *token, char *value, struct s_reader *rdr)
2085{
2086 int i;
2087 char *ptr;
2088 /*
2089 * case sensitive first
2090 */
2091 if (!strcmp(token, "device")) {
2092 for (i = 0, ptr = strtok(value, ","); (i < 3) && (ptr); ptr = strtok(NULL, ","), i++) {
2093 trim(ptr);
2094 switch(i) {
2095 case 0:
2096 cs_strncpy(rdr->device, ptr, sizeof(rdr->device));
2097 break;
2098
2099 case 1:
2100 rdr->r_port = atoi(ptr);
2101 break;
2102
2103 case 2:
2104 rdr->l_port = atoi(ptr);
2105 break;
2106 }
2107 }
2108 return;
2109 }
2110
2111 if (!strcmp(token, "key")) {
2112 if (key_atob14(value, rdr->ncd_key)) {
2113 fprintf(stderr, "Configuration newcamd: Error in Key\n");
2114 exit(1);
2115 }
2116 return;
2117 }
2118
2119#ifdef CS_WITH_GBOX
2120 if (!strcmp(token, "password")) {
2121 cs_strncpy((char *)rdr->gbox_pwd, (const char *)i2b(4, a2i(value, 4)), 4);
2122 return;
2123 }
2124
2125 if (!strcmp(token, "premium")) {
2126 rdr->gbox_prem = 1;
2127 return;
2128 }
2129#endif
2130 if (!strcmp(token, "account")) {
2131 for (i = 0, ptr = strtok(value, ","); (i < 2) && (ptr); ptr = strtok(NULL, ","), i++) {
2132 trim(ptr);
2133 switch(i) {
2134 case 0:
2135 cs_strncpy(rdr->r_usr, ptr, sizeof(rdr->r_usr));
2136 break;
2137
2138 case 1:
2139 cs_strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd));
2140 break;
2141 }
2142 }
2143 return;
2144 }
2145
2146 if (!strcmp(token, "pincode")) {
2147 strncpy(rdr->pincode, value, sizeof(rdr->pincode) - 1);
2148 return;
2149 }
2150
2151 if (!strcmp(token, "readnano") && value[0]) {
2152 if (rdr->emmfile != NULL) {
2153 free(rdr->emmfile);
2154 rdr->emmfile = NULL;
2155 }
2156 if (strlen(value) > 0)
2157 asprintf(&(rdr->emmfile), "%s", value);
2158 return;
2159 }
2160
2161 /*
2162 * case insensitive
2163 */
2164 strtolower(value);
2165
2166 if (!strcmp(token, "enable")) {
2167 if(strlen(value) == 0) {
2168 rdr->enable = 0;
2169 return;
2170 } else {
2171 rdr->enable = atoi(value) ? 1 : 0;
2172 return;
2173 }
2174 }
2175
2176 if (!strcmp(token, "services")) {
2177 if(strlen(value) == 0) {
2178 rdr->sidtabok = 0;
2179 rdr->sidtabno = 0;
2180 return;
2181 } else {
2182 chk_services(value, &rdr->sidtabok, &rdr->sidtabno);
2183 return;
2184 }
2185 }
2186
2187 if (!strcmp(token, "inactivitytimeout")) {
2188 if(strlen(value) == 0) {
2189 rdr->tcp_ito = 0;
2190 return;
2191 } else {
2192 rdr->tcp_ito = atoi(value);
2193 return;
2194 }
2195 }
2196
2197 if (!strcmp(token, "reconnecttimeout")) {
2198 if(strlen(value) == 0) {
2199 rdr->tcp_rto = 0;
2200 return;
2201 } else {
2202 rdr->tcp_rto = atoi(value);
2203 return;
2204 }
2205 }
2206
2207 if (!strcmp(token, "disableserverfilter")) {
2208 if(strlen(value) == 0) {
2209 rdr->ncd_disable_server_filt = 0;
2210 return;
2211 } else {
2212 rdr->ncd_disable_server_filt = atoi(value);
2213 return;
2214 }
2215 }
2216
2217 //FIXME workaround for Smargo until native mode works
2218 if (!strcmp(token, "smargopatch")) {
2219 if(strlen(value) == 0) {
2220 rdr->smargopatch = 0;
2221 return;
2222 } else {
2223 rdr->smargopatch = atoi(value);
2224 return;
2225 }
2226 }
2227
2228 if (!strcmp(token, "label")) {
2229 cs_strncpy(rdr->label, value, sizeof(rdr->label));
2230 return;
2231 }
2232
2233 if (!strcmp(token, "fallback")) {
2234 if(strlen(value) == 0) {
2235 rdr->fallback = 0;
2236 return;
2237 } else {
2238 rdr->fallback = atoi(value) ? 1 : 0;
2239 return;
2240 }
2241 }
2242
2243 if (!strcmp(token, "logport")) {
2244 if(strlen(value) == 0) {
2245 rdr->log_port = 0;
2246 return;
2247 } else {
2248 rdr->log_port = atoi(value);
2249 return;
2250 }
2251 }
2252
2253 if (!strcmp(token, "caid")) {
2254 if(strlen(value) == 0) {
2255 clear_caidtab(&rdr->ctab);
2256 return;
2257 } else {
2258 chk_caidtab(value, &rdr->ctab);
2259 return;
2260 }
2261 }
2262
2263 if (!strcmp(token, "boxid")) {
2264 if(strlen(value) == 0) {
2265 rdr->boxid = 0;
2266 return;
2267 } else {
2268 rdr->boxid = a2i(value, 4);
2269 return;
2270 }
2271 }
2272
2273 if (!strcmp(token, "aeskey")) {
2274 if (key_atob(value, rdr->aes_key)) {
2275 fprintf(stderr, "Configuration reader: Error in AES Key\n");
2276 exit(1);
2277 }
2278 return;
2279 }
2280
2281 if ((!strcmp(token, "n3_rsakey")) || (!strcmp(token, "rsakey"))) {
2282 rdr->has_rsa = 1;
2283 if (key_atob_l(value, rdr->rsa_mod, 128)) {
2284 fprintf(stderr, "Configuration reader: Error in rsakey\n");
2285 exit(1);
2286 }
2287 return;
2288 }
2289
2290 if (!strcmp(token, "tiger_rsakey")) {
2291 if (key_atob_l(value, rdr->rsa_mod, 240)) {
2292 fprintf(stderr, "Configuration reader: Error in tiger_rsakey\n");
2293 exit(1);
2294 }
2295 return;
2296 }
2297
2298 if ((!strcmp(token, "n3_boxkey")) || (!strcmp(token, "boxkey"))) {
2299 if (key_atob_l(value, rdr->nagra_boxkey, 16)) {
2300 fprintf(stderr, "Configuration reader: Error in boxkey\n");
2301 exit(1);
2302 }
2303 return;
2304 }
2305
2306 if (!strcmp(token, "detect")) {
2307 for (i = 0; RDR_CD_TXT[i]; i++) {
2308 if (!strcmp(value, RDR_CD_TXT[i])) {
2309 rdr->detect = i;
2310 }
2311 else {
2312 if ((value[0] == '!') && (!strcmp(value+1, RDR_CD_TXT[i])))
2313 rdr->detect = i|0x80;
2314 }
2315 }
2316 return;
2317 }
2318
2319 if (!strcmp(token, "mhz")) {
2320 if(strlen(value) == 0) {
2321 rdr->mhz = 0;
2322 return;
2323 } else {
2324 rdr->mhz = atoi(value);
2325 return;
2326 }
2327 }
2328
2329 if (!strcmp(token, "cardmhz")) {
2330 if(strlen(value) == 0) {
2331 rdr->cardmhz = 0;
2332 return;
2333 } else {
2334 rdr->cardmhz = atoi(value);
2335 return;
2336 }
2337 }
2338
2339 if (!strcmp(token, "protocol")) {
2340
2341 if (!strcmp(value, "mouse")) {
2342 rdr->typ = R_MOUSE;
2343 return;
2344 }
2345
2346 if (!strcmp(value, "smartreader")) {
2347 rdr->typ = R_SMART;
2348 return;
2349 }
2350
2351 if (!strcmp(value, "internal")) {
2352 rdr->typ = R_INTERNAL;
2353 return;
2354 }
2355
2356#ifdef HAVE_PCSC
2357 if (!strcmp(value, "pcsc")) {
2358 rdr->typ = R_PCSC;
2359 return;
2360 }
2361#endif
2362
2363 if (!strcmp(value, "serial")) {
2364 rdr->typ = R_SERIAL;
2365 return;
2366 }
2367
2368 if (!strcmp(value, "camd35")) {
2369 rdr->typ = R_CAMD35;
2370 return;
2371 }
2372
2373 if (!strcmp(value, "cs378x")) {
2374 rdr->typ = R_CS378X;
2375 return;
2376 }
2377
2378 if (!strcmp(value, "cs357x")) {
2379 rdr->typ = R_CAMD35;
2380 return;
2381 }
2382
2383#ifdef CS_WITH_GBOX
2384 if (!strcmp(value, "gbox")) {
2385 rdr->typ = R_GBOX;
2386 return;
2387 }
2388#endif
2389
2390 if (!strcmp(value, "cccam")) {
2391 rdr->typ = R_CCCAM;
2392 //strcpy(value, "1");
2393 //chk_caidtab(value, &rdr->ctab);
2394 //this is a MAJOR hack for auto multiple caid support (not currently working due to ncd table issue)
2395 return;
2396 }
2397
2398 if (!strcmp(value, "radegast")) {
2399 rdr->typ = R_RADEGAST;
2400 return;
2401 }
2402
2403 if (!strcmp(value, "newcamd") || !strcmp(value, "newcamd525")) {
2404 rdr->typ = R_NEWCAMD;
2405 rdr->ncd_proto = NCD_525;
2406 return;
2407 }
2408
2409 if (!strcmp(value, "newcamd524")) {
2410 rdr->typ = R_NEWCAMD;
2411 rdr->ncd_proto = NCD_524;
2412 return;
2413 }
2414
2415 cs_log( "WARNING: value '%s' in protocol-line not recognized, assuming MOUSE",value);
2416 rdr->typ = R_MOUSE;
2417 return;
2418 }
2419
2420 if (!strcmp(token, "loadbalanced")) {
2421 if(strlen(value) == 0) {
2422 rdr->loadbalanced = 0;
2423 return;
2424 } else {
2425 rdr->loadbalanced = atoi(value);
2426 return;
2427 }
2428 }
2429
2430 if (!strcmp(token, "ident")) {
2431 if(strlen(value) == 0) {
2432 clear_ftab(&rdr->ftab);
2433 return;
2434 } else {
2435 chk_ftab(value, &rdr->ftab,"reader",rdr->label,"provid");
2436 return;
2437 }
2438 }
2439
2440 if (!strcmp(token, "class")) {
2441 chk_cltab(value, &rdr->cltab);
2442 return;
2443 }
2444
2445 if (!strcmp(token, "chid")) {
2446 chk_ftab(value, &rdr->fchid,"reader",rdr->label,"chid");
2447 return;
2448 }
2449
2450 if (!strcmp(token, "showcls")) {
2451 rdr->show_cls = atoi(value);
2452 return;
2453 }
2454
2455 if (!strcmp(token, "maxqlen")) {
2456 rdr->maxqlen = atoi(value);
2457 if( rdr->maxqlen < 0 || rdr->maxqlen > CS_MAXQLEN) {
2458 rdr->maxqlen = CS_MAXQLEN;
2459 }
2460 return;
2461 }
2462
2463 if (!strcmp(token, "group")) {
2464 if(strlen(value) == 0) {
2465 rdr->grp = 0;
2466 return;
2467 } else {
2468 for (ptr = strtok(value, ","); ptr; ptr = strtok(NULL, ",")) {
2469 int g;
2470 g = atoi(ptr);
2471 if ((g>0) && (g<33)) {
2472 rdr->grp |= (1<<(g-1));
2473 }
2474 }
2475 return;
2476 }
2477 }
2478
2479 if (!strcmp(token, "emmcache")) {
2480 if(strlen(value) == 0) {
2481 rdr->cachemm = 0;
2482 rdr->rewritemm = 0;
2483 rdr->logemm = 0;
2484 return;
2485 } else {
2486 for (i = 0, ptr = strtok(value, ","); (i < 3) && (ptr); ptr = strtok(NULL, ","), i++) {
2487 switch(i)
2488 {
2489 case 0:
2490 rdr->cachemm = atoi(ptr);
2491 break;
2492
2493 case 1:
2494 rdr->rewritemm = atoi(ptr);
2495 break;
2496
2497 case 2: rdr->logemm = atoi(ptr);
2498 break;
2499 }
2500 }
2501
2502 if (rdr->rewritemm <= 0) {
2503 fprintf(stderr, "Notice: Setting EMMCACHE to %i,1,%i instead of %i,%i,%i. ",
2504 rdr->cachemm, rdr->logemm,
2505 rdr->cachemm, rdr->rewritemm,
2506 rdr->logemm);
2507
2508 fprintf(stderr, "Zero or negative number of rewrites is silly\n");
2509 rdr->rewritemm = 1;
2510 }
2511 return;
2512 }
2513 }
2514
2515 if (!strcmp(token, "blocknano")) {
2516 //wildcard is used
2517 if (!strcmp(value,"all")) {
2518 for (i = 0 ; i < 256; i++) {
2519 rdr->b_nano[i] |= 0x01; //set all lsb's to block all nanos
2520 }
2521 }
2522 else {
2523 for (ptr = strtok(value, ","); ptr; ptr = strtok(NULL, ",")) {
2524 if ((i = byte_atob(ptr)) >= 0) {
2525 rdr->b_nano[i] |= 0x01; //lsb is set when to block nano
2526 }
2527 }
2528 }
2529 return;
2530 }
2531
2532 if (!strcmp(token, "savenano")) {
2533 //wildcard is used
2534 if (!strcmp(value,"all")) {
2535 for (i = 0 ; i < 256; i++) {
2536 rdr->b_nano[i] |= 0x02; //set all lsb+1 to save all nanos to file
2537 }
2538 }
2539 else {
2540 for (ptr = strtok(value, ","); ptr; ptr = strtok(NULL, ",")) {
2541 if ((i = byte_atob(ptr)) >= 0) {
2542 rdr->b_nano[i] |= 0x02; //lsb+1 is set when to save nano to file
2543 }
2544 }
2545 }
2546 return;
2547 }
2548
2549 if (!strcmp(token, "cccversion")) {
2550 // cccam version
2551 if (strlen(value) > sizeof(rdr->cc_version) - 1) {
2552 fprintf(stderr, "cccam config: version too long\n");
2553 exit(1);
2554 }
2555 memset(rdr->cc_version, 0, sizeof(rdr->cc_version));
2556 cs_strncpy(rdr->cc_version, value, sizeof(rdr->cc_version));
2557 return;
2558 }
2559
2560 if (!strcmp(token, "cccbuild")) {
2561 // cccam build number
2562 if (strlen(value) > sizeof(rdr->cc_build) - 1) {
2563 fprintf(stderr, "cccam config build number too long\n");
2564 exit(1);
2565 }
2566
2567 memset(rdr->cc_build, 0, sizeof(rdr->cc_build));
2568 cs_strncpy(rdr->cc_build, value, sizeof(rdr->cc_build));
2569 return;
2570 }
2571
2572 if (!strcmp(token, "cccmaxhop")) {
2573 // cccam max card distance
2574 rdr->cc_maxhop = atoi(value);
2575 return;
2576 }
2577
2578 if (!strcmp(token, "deprecated")) {
2579 rdr->deprecated = atoi(value);
2580 return;
2581 }
2582
2583 if (token[0] != '#')
2584 cs_log("Warning: keyword '%s' in reader section not recognized",token);
2585}
2586
2587int init_readerdb()
2588{
2589 int tag = 0, nr;
2590 FILE *fp;
2591 char *value;
2592
2593 sprintf(token, "%s%s", cs_confdir, cs_srvr);
2594 if (!(fp=fopen(token, "r"))) {
2595 cs_log("can't open file \"%s\" (errno=%d)\n", token, errno);
2596 return(1);
2597 }
2598 nr = 0;
2599 while (fgets(token, sizeof(token), fp)) {
2600 int i, l;
2601 if ((l = strlen(trim(token))) < 3)
2602 continue;
2603 if ((token[0] == '[') && (token[l-1] == ']')) {
2604 token[l-1] = 0;
2605 tag = (!strcmp("reader", strtolower(token+1)));
2606 if (reader[nr].label[0] && reader[nr].typ) nr++;
2607 memset(&reader[nr], 0, sizeof(struct s_reader));
2608 reader[nr].enable = 1;
2609 reader[nr].tcp_rto = 30;
2610 reader[nr].show_cls = 10;
2611 reader[nr].maxqlen = CS_MAXQLEN;
2612 reader[nr].mhz = 357;
2613 reader[nr].cardmhz = 357;
2614 reader[nr].deprecated = 0;
2615 strcpy(reader[nr].pincode, "none");
2616 for (i=1; i<CS_MAXCAIDTAB; reader[nr].ctab.mask[i++]=0xffff);
2617 continue;
2618 }
2619
2620 if (!tag)
2621 continue;
2622 if (!(value=strchr(token, '=')))
2623 continue;
2624 *value++ ='\0';
2625 chk_reader(trim(strtolower(token)), trim(value), &reader[nr]);
2626 }
2627 fclose(fp);
2628 return(0);
2629}
2630
2631#ifdef CS_ANTICASC
2632void init_ac()
2633{
2634 int nr;
2635 FILE *fp;
2636 //char *value;
2637
2638 sprintf(token, "%s%s", cs_confdir, cs_ac);
2639 if (!(fp=fopen(token, "r")))
2640 {
2641 cs_log("can't open file \"%s\" (errno=%d) anti-cascading table not loaded",
2642 token, errno);
2643 return;
2644 }
2645
2646 for(nr=0; fgets(token, sizeof(token), fp);)
2647 {
2648 int i, skip;
2649 ushort caid, sid, chid, dwtime;
2650 ulong provid;
2651 char *ptr, *ptr1;
2652 struct s_cpmap *ptr_cpmap;
2653 static struct s_cpmap *cpmap=(struct s_cpmap *)0;
2654
2655 if( strlen(token)<4 ) continue;
2656
2657 caid=sid=chid=dwtime=0;
2658 provid=0;
2659 skip=0;
2660 ptr1=0;
2661 for( i=0, ptr=strtok(token, "="); (i<2)&&(ptr); ptr=strtok(NULL, "="), i++ )
2662 {
2663 trim(ptr);
2664 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
2665 skip=1;
2666 break;
2667 }
2668 switch( i )
2669 {
2670 case 0:
2671 ptr1=ptr;
2672 break;
2673 case 1:
2674 dwtime = atoi(ptr);
2675 break;
2676 }
2677 }
2678
2679 if( !skip )
2680 {
2681 for( i=0, ptr=strtok(ptr1, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
2682 {
2683 trim(ptr);
2684 switch( i )
2685 {
2686 case 0:
2687 if( *ptr=='*' ) caid = 0;
2688 else caid = a2i(ptr, 4);
2689 break;
2690 case 1:
2691 if( *ptr=='*' ) provid = 0;
2692 else provid = a2i(ptr, 6);
2693 break;
2694 case 2:
2695 if( *ptr=='*' ) sid = 0;
2696 else sid = a2i(ptr, 4);
2697 break;
2698 case 3:
2699 if( *ptr=='*' ) chid = 0;
2700 else chid = a2i(ptr, 4);
2701 break;
2702 }
2703 }
2704 if (!(ptr_cpmap=(struct s_cpmap*)malloc(sizeof(struct s_cpmap))))
2705 {
2706 cs_log("Error allocating memory (errno=%d)", errno);
2707 return;
2708 }
2709 if( cpmap )
2710 cpmap->next=ptr_cpmap;
2711 else
2712 cfg->cpmap=ptr_cpmap;
2713 cpmap=ptr_cpmap;
2714
2715 cpmap->caid = caid;
2716 cpmap->provid = provid;
2717 cpmap->sid = sid;
2718 cpmap->chid = chid;
2719 cpmap->dwtime = dwtime;
2720 cpmap->next = 0;
2721
2722 cs_debug("nr=%d, caid=%04X, provid=%06X, sid=%04X, chid=%04X, dwtime=%d",
2723 nr, caid, provid, sid, chid, dwtime);
2724 nr++;
2725 }
2726 }
2727 fclose(fp);
2728 //cs_log("%d lengths for caid guessing loaded", nr);
2729 return;
2730}
2731#endif
2732
2733/*
2734 * makes a char ready to write a token into config or webIf
2735 */
2736char *mk_t_caidtab(CAIDTAB *ctab){
2737 int i = 0, needed = 1, pos = 0;
2738 while(ctab->caid[i]){
2739 if(ctab->mask[i]) needed += 10;
2740 else needed += 5;
2741 if(ctab->cmap[i]) needed += 5;
2742 ++i;
2743 }
2744 char *value = (char *) malloc(needed * sizeof(char));
2745 i = 0;
2746 while(ctab->caid[i]) {
2747 if(i == 0) {
2748 sprintf(value + pos, "%04X", ctab->caid[i]);
2749 pos += 4;
2750 } else {
2751 sprintf(value + pos, ",%04X", ctab->caid[i]);
2752 pos += 5;
2753 }
2754 if(ctab->mask[i]){
2755 sprintf(value + pos, "&%04X", ctab->mask[i]);
2756 pos += 5;
2757 }
2758 if(ctab->cmap[i]){
2759 sprintf(value + pos, ":%04X", ctab->cmap[i]);
2760 pos += 5;
2761 }
2762 ++i;
2763 }
2764 value[pos] = '\0';
2765 return value;
2766}
2767
2768/*
2769 * makes a char ready to write a token into config or webIf
2770 */
2771char *mk_t_tuntab(TUNTAB *ttab){
2772 int i = 0, needed = 1, pos = 0;
2773 while(ttab->bt_caidfrom[i]){
2774 if(ttab->bt_srvid[i]) needed += 10;
2775 else needed += 5;
2776 if(ttab->bt_caidto[i]) needed += 5;
2777 ++i;
2778 }
2779 char *value = (char *) malloc(needed * sizeof(char));
2780 i = 0;
2781 while(ttab->bt_caidfrom[i]) {
2782 if(i == 0) {
2783 sprintf(value + pos, "%04X", ttab->bt_caidfrom[i]);
2784 pos += 4;
2785 } else {
2786 sprintf(value + pos, ",%04X", ttab->bt_caidfrom[i]);
2787 pos += 5;
2788 }
2789 if(ttab->bt_srvid[i]){
2790 sprintf(value + pos, ".%04X", ttab->bt_srvid[i]);
2791 pos += 5;
2792 }
2793 if(ttab->bt_caidto[i]){
2794 sprintf(value + pos, ":%04X", ttab->bt_caidto[i]);
2795 pos += 5;
2796 }
2797 ++i;
2798 }
2799 value[pos] = '\0';
2800 return value;
2801}
2802
2803/*
2804 * makes a char ready to write a token into config or webIf
2805 */
2806char *mk_t_group(ulong *grp){
2807 int i = 0, needed = 1, pos = 0, dot = 0;
2808 char grpbit[33];
2809 long2bitchar((long) grp, grpbit);
2810
2811 for(i = 0; i < 32; i++){
2812 if (grpbit[i] == '1'){
2813 needed += 2;
2814 if(i > 9) needed += 1;
2815 }
2816 }
2817 char *value = (char *) malloc(needed * sizeof(char));
2818
2819 for(i = 0; i < 32; i++){
2820 if (grpbit[i] == '1'){
2821 if (dot == 0){
2822 sprintf(value + pos, "%d", i+1);
2823 if (i > 9)pos += 2;
2824 else pos += 1;
2825 dot = 1;
2826 } else {
2827 sprintf(value + pos, ",%d", i+1);
2828 if (i > 9)pos += 3;
2829 else pos += 2;
2830 }
2831 }
2832 }
2833 value[pos] = '\0';
2834 return value;
2835}
2836
2837/*
2838 * makes a char ready to write a token into config or webIf
2839 */
2840char *mk_t_ftab(FTAB *ftab){
2841 int i = 0, j = 0, needed = 1, pos = 0;
2842
2843 needed = ftab->nfilts * 5;
2844 for (i = 0; i < ftab->nfilts; ++i)
2845 needed += ftab->filts[i].nprids * 7;
2846
2847 char *value = (char *) malloc(needed * sizeof(char));
2848
2849 char *dot="";
2850 for (i = 0; i < ftab->nfilts; ++i){
2851 sprintf(value + pos, "%s%04X", dot, ftab->filts[i].caid);
2852 pos += 4;
2853 if (i > 0) pos += 1;
2854 dot=":";
2855 for (j = 0; j < ftab->filts[i].nprids; ++j) {
2856 sprintf(value + pos, "%s%06lX", dot, ftab->filts[i].prids[j]);
2857 pos += 7;
2858 dot=",";
2859 }
2860 dot=";";
2861 }
2862
2863 value[pos] = '\0';
2864 return value;
2865}
Note: See TracBrowser for help on using the repository browser.