source: trunk/oscam-config.c@ 1704

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

Add disablelog to write_config()

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