source: trunk/oscam-config.c@ 1713

Last change on this file since 1713 was 1713, checked in by merek, 11 years ago

Add httpallowed to write_config()

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