source: trunk/oscam-config.c@ 1766

Last change on this file since 1766 was 1766, checked in by landlord, 10 years ago

New EMM processing, so they can be filtered for DVB API, thanks to dingo35.

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