source: trunk/oscam-config.c@ 1691

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

Merge Webinterface into Trunk - Use -DWEBIF=1 with cmake to build with Webinterface - For make add -DWEBIF to target in Makefile to build with Webinterface for, thanks to Alno and Streamboard community

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