source: branches/monitor-improvement/oscam-config.c@ 1180

Last change on this file since 1180 was 1180, checked in by alno, 12 years ago

WebIf:

  • Merging revisions 1177-1179 of trunk
File size: 59.3 KB
Line 
1#include "globals.h"
2#ifdef CS_WITH_BOXKEYS
3# include "oscam-boxkeys.np"
4#endif
5
6#define CONFVARWIDTH 16
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 {
88 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
89 exit(1);
90 }
91 if (*base)
92 lip->next=cip;
93 else
94 *base=cip;
95
96 memset(cip, 0, sizeof(struct s_ip));
97 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
98 {
99 if (i == 0) ++i;
100 else {
101 if (!(cip=malloc(sizeof(struct s_ip)))){
102 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
103 exit(1);
104 }
105 lip->next = cip;
106 memset(cip, 0, sizeof(struct s_ip));
107 }
108 if( (ptr2=strchr(trim(ptr1), '-')) )
109 {
110 *ptr2++='\0';
111 cip->ip[0]=cs_inet_addr(trim(ptr1));
112 cip->ip[1]=cs_inet_addr(trim(ptr2));
113 }
114 else
115 cip->ip[0]=cip->ip[1]=cs_inet_addr(ptr1);
116 lip = cip;
117 }
118}
119
120void chk_caidtab(char *caidasc, CAIDTAB *ctab)
121{
122 int i;
123 char *ptr1, *ptr2, *ptr3;
124
125 //reset caid tab needed for usage while runtime from webif
126 for (i=0;i<CS_MAXCAIDTAB;i++){
127 ctab->caid[i]=0;
128 ctab->mask[i]=0;
129 ctab->cmap[i++]=0;
130 }
131
132 for (i=0, ptr1=strtok(caidasc, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
133 {
134 ulong caid, mask, cmap;
135 if( (ptr3=strchr(trim(ptr1), ':')) )
136 *ptr3++='\0';
137 else
138 ptr3="";
139 if( (ptr2=strchr(trim(ptr1), '&')) )
140 *ptr2++='\0';
141 else
142 ptr2="";
143 if (((caid=a2i(ptr1, 2))|(mask=a2i(ptr2,-2))|(cmap=a2i(ptr3, 2))) < 0x10000)
144 {
145 ctab->caid[i]=caid;
146 ctab->mask[i]=mask;
147 ctab->cmap[i++]=cmap;
148 }
149// else
150// cs_log("WARNING: wrong CAID in %s -> ignored", cs_user);
151 }
152}
153
154void chk_tuntab(char *tunasc, TUNTAB *ttab)
155{
156 int i;
157 char *ptr1, *ptr2, *ptr3;
158
159 //reset tuntab for usage during runtime from WebIf
160 for (i=0;i<CS_MAXTUNTAB;i++) {
161 ttab->bt_caidfrom[i]=0;
162 ttab->bt_caidto[i]=0;
163 ttab->bt_srvid[i++]=0;
164 }
165
166 for (i=0, ptr1=strtok(tunasc, ","); (i<CS_MAXTUNTAB) && (ptr1); ptr1=strtok(NULL, ","))
167 {
168 ulong bt_caidfrom, bt_caidto, bt_srvid;
169 if( (ptr3=strchr(trim(ptr1), ':')) )
170 *ptr3++='\0';
171 else
172 ptr3="";
173 if( (ptr2=strchr(trim(ptr1), '.')) )
174 *ptr2++='\0';
175 else
176 ptr2="";
177 if ((bt_caidfrom=a2i(ptr1, 2))|(bt_srvid=a2i(ptr2,-2))|(bt_caidto=a2i(ptr3, 2)))
178 {
179 ttab->bt_caidfrom[i]=bt_caidfrom;
180 ttab->bt_caidto[i]=bt_caidto;
181 ttab->bt_srvid[i++]=bt_srvid;
182 }
183// else
184// cs_log("WARNING: wrong Betatunnel in %s -> ignored", cs_user);
185 }
186}
187
188void chk_services(char *labels, ulong *sidok, ulong *sidno)
189{
190 int i;
191 char *ptr;
192 SIDTAB *sidtab;
193 *sidok=*sidno=0;
194 for (ptr=strtok(labels, ","); ptr; ptr=strtok(NULL, ","))
195 for (trim(ptr), i=0, sidtab=cfg->sidtab; sidtab; sidtab=sidtab->next, i++)
196 {
197 if (!strcmp(sidtab->label, ptr)) *sidok|=(1<<i);
198 if ((ptr[0]=='!') && (!strcmp(sidtab->label, ptr+1))) *sidno|=(1<<i);
199 }
200}
201
202void chk_ftab(char *zFilterAsc, FTAB *ftab, const char *zType, const char *zName, const char *zFiltName)
203{
204 int i,j;
205 char *ptr1,*ptr2,*ptr3;
206 char *ptr[CS_MAXFILTERS] = {0};
207
208 memset(ftab, 0, sizeof(FTAB));
209 for( i=0, ptr1=strtok(zFilterAsc, ";"); (i<CS_MAXFILTERS) && (ptr1); ptr1=strtok(NULL, ";"), i++ )
210 {
211 //cs_log("ptr1=%s", ptr1);
212 ptr[i] = ptr1;
213 if( (ptr2=strchr(trim(ptr1), ':')) )
214 {
215 //cs_log("ptr2=%s", ptr2);
216 *ptr2++='\0';
217 //cs_log("ptr2=%s", ptr2);
218 ftab->filts[i].caid = (ushort)a2i(ptr1, 4);
219 //cs_log("caid=%04X", ftab->filts[i].caid);
220 ptr[i] = ptr2;
221 }
222 else if (zFiltName && zFiltName[0]=='c')
223 {
224 cs_log("PANIC: CAID field not found in CHID parameter!");
225 cs_exit(1);
226 }
227 ftab->nfilts++;
228 }
229
230 if( ftab->nfilts ) cs_debug("%s '%s' %s filter(s):", zType, zName, zFiltName);
231 for( i=0; i<ftab->nfilts; i++ )
232 {
233 cs_debug("CAID #%d: %04X", i, ftab->filts[i].caid);
234 for( j=0, ptr3=strtok(ptr[i], ","); (j<CS_MAXPROV) && (ptr3); ptr3=strtok(NULL, ","), j++ )
235 {
236 ftab->filts[i].prids[j] = a2i(ptr3,6);
237 ftab->filts[i].nprids++;
238 cs_debug("%s #%d: %06X", zFiltName, j, ftab->filts[i].prids[j]);
239 }
240 }
241 //cs_log("exit chk_ftab");
242}
243
244void chk_cltab(char *classasc, CLASSTAB *clstab)
245{
246 int i;
247 char *ptr1;
248 for( i=0, ptr1=strtok(classasc, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ",") )
249 {
250 ptr1=trim(ptr1);
251 if( ptr1[0] == '!' )
252 clstab->bclass[clstab->bn++] = (uchar)a2i(ptr1+1, 2);
253 else
254 clstab->aclass[clstab->an++] = (uchar)a2i(ptr1, 2);
255 }
256}
257
258void chk_port_tab(char *portasc, PTAB *ptab)
259{
260 int i,j,nfilts,ifilt,iport;
261 char *ptr1,*ptr2,*ptr3;
262 char *ptr[CS_MAXPORTS] = {0};
263 int port[CS_MAXPORTS] = {0};
264 int previous_nports = ptab->nports;
265
266 for (nfilts=i=previous_nports, ptr1=strtok(portasc, ";"); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ";"), i++)
267 {
268 ptr[i] = ptr1;
269 if( (ptr2=strchr(trim(ptr1), '@')) )
270 {
271 *ptr2++='\0';
272 ptab->ports[i].s_port = atoi(ptr1);
273 ptr[i] = ptr2;
274 port[i] = ptab->ports[i].s_port;
275 ptab->nports++;
276 }
277 nfilts++;
278 }
279
280 if( nfilts==1 && strlen(portasc)<6 && ptab->ports[0].s_port == 0 ) {
281 ptab->ports[0].s_port = atoi(portasc);
282 ptab->nports = 1;
283 }
284
285 iport=ifilt = previous_nports;
286 for (i=previous_nports; i<nfilts; i++)
287 {
288 if( port[i]!=0 ) iport = i;
289 for (j=0, ptr3=strtok(ptr[i], ","); (j<CS_MAXPROV) && (ptr3); ptr3=strtok(NULL, ","), j++)
290 {
291 if( (ptr2=strchr(trim(ptr3), ':')) )
292 {
293 *ptr2++='\0';
294 ptab->ports[iport].ftab.nfilts++;
295 ifilt = ptab->ports[iport].ftab.nfilts-1;
296 ptab->ports[iport].ftab.filts[ifilt].caid = (ushort)a2i(ptr3, 4);
297 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr2, 6);
298 } else {
299 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr3, 6);
300 }
301 ptab->ports[iport].ftab.filts[ifilt].nprids++;
302 }
303 }
304}
305
306#ifdef NOTUSED
307static void chk_srvip(char *value, in_addr_t *ip)
308{
309 int i;
310 char *ptr;
311 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
312 if (i<8) ip[i++]=inet_addr(ptr);
313}
314#endif
315
316void chk_t_global(char *token, char *value)
317{
318 if (!strcmp(token, "serverip")) { cfg->srvip=inet_addr(value); return; }
319 if (!strcmp(token, "logfile")) { strncpy(logfile, value, sizeof(logfile)-1); return; }
320 if (!strcmp(token, "pidfile")) { strncpy(cfg->pidfile, value, sizeof(cfg->pidfile)-1); return; }
321 if (!strcmp(token, "usrfile")) { strncpy(cfg->usrfile, value, sizeof(cfg->usrfile)-1); return; }
322 if (!strcmp(token, "cwlogdir")) { strncpy(cfg->cwlogdir, value, sizeof(cfg->cwlogdir)-1); return; }
323 if (!strcmp(token, "clienttimeout"))
324 {
325 cfg->ctimeout = atoi(value);
326 if (cfg->ctimeout < 100)
327 cfg->ctimeout *= 1000;
328 return;
329 }
330 if (!strcmp(token, "fallbacktimeout"))
331 {
332 cfg->ftimeout = atoi(value);
333 if (cfg->ftimeout < 100)
334 cfg->ftimeout *= 1000;
335 return;
336 }
337
338 if (!strcmp(token, "clientmaxidle")) { cfg->cmaxidle=atoi(value); return; }
339 if (!strcmp(token, "cachedelay")) { cfg->delay=atoi(value); return; }
340 if (!strcmp(token, "bindwait")) { cfg->bindwait=atoi(value); return; }
341 if (!strcmp(token, "netprio")) { cfg->netprio=atoi(value); return; }
342 if (!strcmp(token, "resolvedelay")) { cfg->resolvedelay=atoi(value); return; }
343 if (!strcmp(token, "sleep")) { cfg->tosleep=atoi(value); return; }
344 if (!strcmp(token, "unlockparental")) { cfg->ulparent=atoi(value); return; }
345 if (!strcmp(token, "nice"))
346 {
347 cfg->nice=atoi(value);
348 if ((cfg->nice<-20) || (cfg->nice>20)) cfg->nice=99;
349 if (cfg->nice!=99) cs_setpriority(cfg->nice); // ignore errors
350 return;
351 }
352 if (!strcmp(token, "serialreadertimeout"))
353 {
354 if (cfg->srtimeout < 100)
355 cfg->srtimeout = atoi(value) * 1000;
356 else
357 cfg->srtimeout = atoi(value);
358 if( cfg->srtimeout <=0 )
359 cfg->srtimeout=1500;
360 return;
361 }
362 if (!strcmp(token, "maxlogsize"))
363 {
364 cfg->max_log_size=atoi(value);
365 if( cfg->max_log_size <=10 )
366 cfg->max_log_size=10;
367 return;
368 }
369 if( !strcmp(token, "showecmdw")) { cfg->show_ecm_dw = atoi(value); return; }
370 if( !strcmp(token, "waitforcards")) { cfg->waitforcards = atoi(value); return; }
371 if( !strcmp(token, "preferlocalcards")) { cfg->preferlocalcards = atoi(value); return; }
372 if (token[0] != '#')
373 fprintf(stderr, "Warning: keyword '%s' in global section not recognized\n",token);
374}
375
376#ifdef CS_ANTICASC
377void chk_t_ac(char *token, char *value)
378{
379 if (!strcmp(token, "enabled"))
380 {
381 cfg->ac_enabled=atoi(value);
382 if( cfg->ac_enabled<=0 ) cfg->ac_enabled=0;
383 else cfg->ac_enabled=1;
384 return;
385 }
386
387 if (!strcmp(token, "numusers"))
388 {
389 cfg->ac_users=atoi(value);
390 if( cfg->ac_users<0 ) cfg->ac_users=0;
391 return;
392 }
393 if (!strcmp(token, "sampletime"))
394 {
395 cfg->ac_stime=atoi(value);
396 if( cfg->ac_stime<0 ) cfg->ac_stime=2;
397 return;
398 }
399 if (!strcmp(token, "samples"))
400 {
401 cfg->ac_samples=atoi(value);
402 if( cfg->ac_samples<2 || cfg->ac_samples>10) cfg->ac_samples=10;
403 return;
404 }
405 if (!strcmp(token, "penalty"))
406 {
407 cfg->ac_penalty=atoi(value);
408 if( cfg->ac_penalty<0 ) cfg->ac_penalty=0;
409 return;
410 }
411 if (!strcmp(token, "aclogfile"))
412 {
413 strncpy(cfg->ac_logfile, value, sizeof(cfg->ac_logfile)-1);
414 return;
415 }
416 if( !strcmp(token, "fakedelay") )
417 {
418 cfg->ac_fakedelay=atoi(value);
419 if( cfg->ac_fakedelay<100 || cfg->ac_fakedelay>1000 )
420 cfg->ac_fakedelay=1000;
421 return;
422 }
423 if( !strcmp(token, "denysamples") )
424 {
425 cfg->ac_denysamples=atoi(value);
426 if( cfg->ac_denysamples<2 || cfg->ac_denysamples>cfg->ac_samples-1 )
427 cfg->ac_denysamples=cfg->ac_samples-1;
428 return;
429 }
430 if (token[0] != '#')
431 fprintf(stderr, "Warning: keyword '%s' in anticascading section not recognized\n",token);
432//#endif moved this endif up two lines, I think this was erroneous - dingo35
433}
434#endif
435
436void chk_t_monitor(char *token, char *value)
437{
438 if (!strcmp(token, "port")) { cfg->mon_port=atoi(value); return; }
439 if (!strcmp(token, "serverip")) { cfg->mon_srvip=inet_addr(value); return; }
440 if (!strcmp(token, "nocrypt")) { chk_iprange(value, &cfg->mon_allowed); return; }
441 if (!strcmp(token, "aulow")) { cfg->mon_aulow=atoi(value); return; }
442 if (!strcmp(token, "monlevel")) { cfg->mon_level=atoi(value); return; }
443 if (!strcmp(token, "httpport")) { cfg->http_port=atoi(value); return; }
444 if (!strcmp(token, "httpuser")) { strncpy(cfg->http_user, value, sizeof(cfg->http_user)-1); cfg->http_user[sizeof(cfg->http_user)-1] = '\0'; return; }
445 if (!strcmp(token, "httppwd")) { strncpy(cfg->http_pwd, value, sizeof(cfg->http_pwd)-1); cfg->http_pwd[sizeof(cfg->http_pwd)-1] = '\0'; return; }
446 if (!strcmp(token, "httpcss")) { strncpy(cfg->http_css, value, sizeof(cfg->http_css)-1); cfg->http_css[sizeof(cfg->http_css)-1] = '\0'; return; }
447 if (!strcmp(token, "httptpl")) { strncpy(cfg->http_tpl, value, sizeof(cfg->http_tpl)-1); cfg->http_css[sizeof(cfg->http_tpl)-1] = '\0'; return; }
448 if (!strcmp(token, "httprefresh")) { cfg->http_refresh=atoi(value); return; }
449 if (!strcmp(token, "httphideidleclients")) { cfg->http_hide_idle_clients=atoi(value); return; }
450 if (!strcmp(token, "hideclient_to")) { cfg->mon_hideclient_to=atoi(value); return; }
451 if (token[0] != '#')
452 fprintf(stderr, "Warning: keyword '%s' in monitor section not recognized\n",token);
453}
454
455void chk_t_camd33(char *token, char *value)
456{
457 if (!strcmp(token, "port")) { cfg->c33_port=atoi(value); return; }
458 if (!strcmp(token, "serverip")) { cfg->c33_srvip=inet_addr(value); return; }
459 if (!strcmp(token, "nocrypt")) { chk_iprange(value, &cfg->c33_plain); return; }
460 if (!strcmp(token, "passive")) { cfg->c33_passive=(value[0]!='0'); return; }
461 if (!strcmp(token, "key"))
462 {
463 if (key_atob(value, cfg->c33_key))
464 {
465 fprintf(stderr, "Configuration camd3.3x: Error in Key\n");
466 exit(1);
467 }
468 cfg->c33_crypted=1;
469 return;
470 }
471 if (token[0] != '#')
472 fprintf(stderr, "Warning: keyword '%s' in camd33 section not recognized\n",token);
473}
474
475void chk_t_camd35(char *token, char *value)
476{
477 if (!strcmp(token, "port")) { cfg->c35_port=atoi(value); return; }
478 if (!strcmp(token, "serverip")) { cfg->c35_tcp_srvip=inet_addr(value); return; }
479 if (token[0] != '#')
480 fprintf(stderr, "Warning: keyword '%s' in camd35 section not recognized\n",token);
481}
482
483void chk_t_camd35_tcp(char *token, char *value)
484{
485 if (!strcmp(token, "port")) { chk_port_tab(value, &cfg->c35_tcp_ptab); return; }
486 if (!strcmp(token, "serverip")) { cfg->c35_tcp_srvip=inet_addr(value); return; }
487 if (token[0] != '#')
488 fprintf(stderr, "Warning: keyword '%s' in camd35 tcp section not recognized\n",token);
489}
490
491void chk_t_newcamd(char *token, char *value)
492{
493 if (!strcmp(token, "port")) { chk_port_tab(value, &cfg->ncd_ptab); return; }
494 if (!strcmp(token, "serverip")) { cfg->ncd_srvip=inet_addr(value); return; }
495 if (!strcmp(token, "key"))
496 {
497 if (key_atob14(value, cfg->ncd_key))
498 {
499 fprintf(stderr, "Configuration newcamd: Error in Key\n");
500 exit(1);
501 }
502 return;
503 }
504 if (token[0] != '#')
505 fprintf(stderr, "Warning: keyword '%s' in newcamd section not recognized\n",token);
506}
507
508void chk_t_radegast(char *token, char *value)
509{
510 if (!strcmp(token, "port")) { cfg->rad_port=atoi(value); return; }
511 if (!strcmp(token, "serverip")) { cfg->rad_srvip=inet_addr(value); return; }
512 if (!strcmp(token, "allowed")) { chk_iprange(value, &cfg->rad_allowed); return; }
513 if (!strcmp(token, "user")) { strncpy(cfg->rad_usr, value, sizeof(cfg->rad_usr)-1); return; }
514 if (token[0] != '#')
515 fprintf(stderr, "Warning: keyword '%s' in radegast section not recognized\n",token);
516}
517
518void chk_t_serial(char *token, char *value)
519{
520 if (!strcmp(token, "device"))
521 {
522 int l;
523 l=strlen(cfg->ser_device);
524 if (l) cfg->ser_device[l++]=1; // use ctrl-a as delimiter
525 strncpy(cfg->ser_device+l, value, sizeof(cfg->ser_device)-1-l);
526 return;
527 }
528 if (token[0] != '#')
529 fprintf(stderr, "Warning: keyword '%s' in serial section not recognized\n",token);
530}
531
532#ifdef CS_WITH_GBOX
533static void chk_t_gbox(char *token, char *value)
534{
535// if (!strcmp(token, "password")) strncpy(cfg->gbox_pwd, i2b(4, a2i(value, 4)), 4);
536 if (!strcmp(token, "password")) { cs_atob(cfg->gbox_pwd, value, 4); return; }
537 if (!strcmp(token, "maxdist")) { cfg->maxdist=atoi(value); return; }
538 if (!strcmp(token, "ignorelist")) { strncpy((char *)cfg->ignorefile, value, sizeof(cfg->ignorefile)-1); return; }
539 if (!strcmp(token, "onlineinfos")) { strncpy((char *)cfg->gbxShareOnl, value, sizeof(cfg->gbxShareOnl)-1); return; }
540 if (!strcmp(token, "cardinfos")) { strncpy((char *)cfg->cardfile, value, sizeof(cfg->cardfile)-1); return; }
541 if (!strcmp(token, "locals"))
542 {
543 char *ptr1;
544 int n=0, i;
545 for (i=0, ptr1=strtok(value, ","); (i<CS_MAXLOCALS) && (ptr1); ptr1=strtok(NULL, ","))
546 {
547 cfg->locals[n++]=a2i(ptr1, 8);
548 //printf("%i %08X",n,cfg->locals[n-1]);
549 }
550 cfg->num_locals=n;
551 return;
552 }
553 if (token[0] != '#')
554 fprintf(stderr, "Warning: keyword '%s' in gbox section not recognized\n",token);
555}
556#endif
557
558void chk_t_cccam(char *token, char *value)
559{
560 // placeholder for ccam server support
561 fprintf(stderr, "Warning: OSCam have no cccam server support yet. Parametr %s = %s\n", token, value);
562}
563
564#ifdef HAVE_DVBAPI
565void chk_t_dvbapi(char *token, char *value)
566{
567 if (!strcmp(token, "enabled")) { cfg->dvbapi_enabled=atoi(value); return; }
568 if (!strcmp(token, "au")) { cfg->dvbapi_au=atoi(value); return; }
569 if (!strcmp(token, "boxtype")) { strncpy(cfg->dvbapi_boxtype, value, sizeof(cfg->dvbapi_boxtype)-1); return; }
570 if (!strcmp(token, "user")) { strncpy(cfg->dvbapi_usr, value, sizeof(cfg->dvbapi_usr)-1); return; }
571
572 if (token[0] != '#')
573 fprintf(stderr, "Warning: keyword '%s' in dvbapi section not recognized\n",token);
574}
575#endif
576
577static void chk_token(char *token, char *value, int tag)
578{
579 switch(tag)
580 {
581 case TAG_GLOBAL : chk_t_global(token, value); break;
582 case TAG_MONITOR : chk_t_monitor(token, value); break;
583 case TAG_CAMD33 : chk_t_camd33(token, value); break;
584 case TAG_CAMD35 :
585 case TAG_CS357X : chk_t_camd35(token, value); break;
586 case TAG_NEWCAMD : chk_t_newcamd(token, value); break;
587 case TAG_RADEGAST: chk_t_radegast(token, value); break;
588 case TAG_SERIAL : chk_t_serial(token, value); break;
589 case TAG_CS378X : chk_t_camd35_tcp(token, value); break;
590#ifdef CS_WITH_GBOX
591 case TAG_GBOX : chk_t_gbox(token, value); break;
592#endif
593 case TAG_CCCAM : chk_t_cccam(token, value); break;
594#ifdef HAVE_DVBAPI
595 case TAG_DVBAPI : chk_t_dvbapi(token, value); break;
596#else
597 case TAG_DVBAPI : fprintf(stderr, "Warning: OSCam compiled without DVB API support.\n"); break;
598#endif
599#ifdef CS_ANTICASC
600 case TAG_ANTICASC: chk_t_ac(token, value); break;
601#endif
602 }
603}
604
605void init_len4caid()
606{
607 int nr;
608 FILE *fp;
609 char *value;
610
611 memset(len4caid, 0, sizeof(ushort)<<8);
612 sprintf(token, "%s%s", cs_confdir, cs_l4ca);
613 if (!(fp=fopen(token, "r")))
614 return;
615 for(nr=0; fgets(token, sizeof(token), fp);)
616 {
617 int i, c;
618 char *ptr;
619 if (!(value=strchr(token, ':'))) continue;
620 *value++='\0';
621 if( (ptr=strchr(value, '#')) )
622 *ptr='\0';
623 if (strlen(trim(token))!=2) continue;
624 if (strlen(trim(value))!=4) continue;
625 if ((i=byte_atob(token))<0) continue;
626 if ((c=word_atob(value))<0) continue;
627//printf("idx %02X = %04X\n", i, c); fflush(stdout);
628 len4caid[i]=c;
629 nr++;
630 }
631 fclose(fp);
632 cs_log("%d lengths for caid guessing loaded", nr);
633 return;
634}
635
636int search_boxkey(ushort caid, char *key)
637{
638 int i, rc=0;
639 FILE *fp;
640 char c_caid[512];
641
642 sprintf(c_caid, "%s%s", cs_confdir, cs_cert);
643 fp=fopen(c_caid, "r");
644 if (fp)
645 {
646 for (; (!rc) && fgets(c_caid, sizeof(c_caid), fp);)
647 {
648 char *c_provid, *c_key;
649
650 c_provid=strchr(c_caid, '#');
651 if (c_provid) *c_provid='\0';
652 if (!(c_provid=strchr(c_caid, ':'))) continue;
653 *c_provid++='\0';
654 if (!(c_key=strchr(c_provid, ':'))) continue;
655 *c_key++='\0';
656 if (word_atob(trim(c_caid))!=caid) continue;
657 if ((i=(strlen(trim(c_key))>>1))>256) continue;
658 if (cs_atob((uchar *)key, c_key, i)<0)
659 {
660 cs_log("wrong key in \"%s\"", cs_cert);
661 continue;
662 }
663 rc=1;
664 }
665 fclose(fp);
666 }
667#ifdef OSCAM_INBUILD_KEYS
668 for(i=0; (!rc) && (npkey[i].keylen); i++)
669 if (rc=((caid==npkey[i].caid) && (npkey[i].provid==0)))
670 memcpy(key, npkey[i].key, npkey[i].keylen);
671#endif
672 return(rc);
673}
674
675int init_config()
676{
677 int tag=TAG_GLOBAL;
678 FILE *fp;
679 char *value;
680
681#ifndef CS_EMBEDDED
682#ifdef PRIO_PROCESS
683 errno=0;
684 if ((cfg->nice=getpriority(PRIO_PROCESS, 0))==(-1))
685 if (errno)
686#endif
687#endif
688 cfg->nice=99;
689 cfg->ctimeout=CS_CLIENT_TIMEOUT;
690 cfg->ftimeout=CS_CLIENT_TIMEOUT / 2;
691 cfg->cmaxidle=CS_CLIENT_MAXIDLE;
692 cfg->delay=CS_DELAY;
693 cfg->bindwait=CS_BIND_TIMEOUT;
694 cfg->resolvedelay=CS_RESOLVE_DELAY;
695 cfg->mon_level=2;
696 cfg->mon_hideclient_to=0;
697 cfg->srtimeout=1500;
698 cfg->ulparent=0;
699 strcpy(cfg->http_user, "");
700 strcpy(cfg->http_pwd, "");
701 strcpy(cfg->http_css, "");
702 cfg->http_refresh=0;
703 cfg->http_hide_idle_clients=0;
704 strcpy(cfg->http_tpl, "");
705#ifdef CS_ANTICASC
706 cfg->ac_enabled=0;
707 cfg->ac_users=0;
708 cfg->ac_stime=2;
709 cfg->ac_samples=10;
710 cfg->ac_denysamples=8;
711 cfg->ac_fakedelay=1000;
712 strcpy(cfg->ac_logfile, "./oscam_ac.log");
713#endif
714 sprintf(token, "%s%s", cs_confdir, cs_conf);
715 if (!(fp=fopen(token, "r")))
716 {
717 fprintf(stderr, "Cannot open config file '%s' (errno=%d)\n", token, errno);
718 exit(1);
719 }
720 while (fgets(token, sizeof(token), fp))
721 {
722 int i, l;
723 //void *ptr;
724 if ((l=strlen(trim(token)))<3) continue;
725 if ((token[0]=='[') && (token[l-1]==']'))
726 {
727 for (token[l-1]=0, tag=-1, i=TAG_GLOBAL; cctag[i]; i++)
728 if (!strcmp(cctag[i], strtolower(token+1)))
729 tag=i;
730 continue;
731 }
732 if (!(value=strchr(token, '='))) continue;
733 *value++='\0';
734 chk_token(trim(strtolower(token)), trim(value), tag);
735 }
736 fclose(fp);
737 cs_init_log(logfile);
738 if (cfg->ftimeout>=cfg->ctimeout)
739 {
740 cfg->ftimeout = cfg->ctimeout - 100;
741 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be smaller than clienttimeout (%lu ms))", cfg->ftimeout, cfg->ctimeout);
742 }
743 if(cfg->ftimeout < cfg->srtimeout)
744 {
745 cfg->ftimeout = cfg->srtimeout + 100;
746 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ftimeout, cfg->srtimeout);
747 }
748 if(cfg->ctimeout < cfg->srtimeout)
749 {
750 cfg->ctimeout = cfg->srtimeout + 100;
751 cs_log("WARNING: clienttimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ctimeout, cfg->srtimeout);
752 }
753#ifdef CS_ANTICASC
754 if( cfg->ac_denysamples+1>cfg->ac_samples )
755 {
756 cfg->ac_denysamples=cfg->ac_samples-1;
757 cs_log("WARNING: DenySamples adjusted to %d", cfg->ac_denysamples);
758 }
759#endif
760 return 0;
761}
762
763void chk_account(char *token, char *value, struct s_auth *account)
764{
765 int i;
766 char *ptr1;//, *ptr2;
767
768 if (!strcmp(token, "user")) { strncpy(account->usr, value, sizeof(account->usr)-1); return; }
769 if (!strcmp(token, "pwd")) { strncpy(account->pwd, value, sizeof(account->pwd)-1); return; }
770 if (!strcmp(token, "hostname")) { strncpy((char *)account->dyndns, value, sizeof(account->dyndns)-1);return; }
771 if (!strcmp(token, "betatunnel")) { chk_tuntab(value, &account->ttab); return; }
772 if (!strcmp(token, "uniq")) { account->uniq=atoi(value); return; }
773 if (!strcmp(token, "sleep")) { account->tosleep=atoi(value); return; }
774 if (!strcmp(token, "monlevel")) { account->monlvl=atoi(value); return; }
775 if (!strcmp(token, "caid")) { chk_caidtab(value, &account->ctab); return; }
776 /*
777 * case insensitive
778 */
779 strtolower(value);
780 if (!strcmp(token, "au"))
781 {
782 //set default values for usage during runtime from Webif
783 account->au=-1;
784 account->autoau=0;
785
786 if(value && value[0]=='1') account->autoau=1;
787 for (i=0; i<CS_MAXREADER; i++)
788 if ((reader[i].label[0]) &&
789 (!strncmp(reader[i].label, value, strlen(reader[i].label))))
790 account->au=i;
791 return;
792 }
793 if (!strcmp(token, "group"))\
794 {
795 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
796 {
797 int g;
798 g=atoi(ptr1);
799 if ((g>0) && (g<33)) account->grp|=(1<<(g-1));
800 }
801 return;
802 }
803 if(!strcmp(token, "services")) { chk_services(value, &account->sidtabok, &account->sidtabno); return; }
804 if(!strcmp(token, "ident")) { chk_ftab(value, &account->ftab, "user", account->usr, "provid"); return; }
805 if(!strcmp(token, "class")) { chk_cltab(value, &account->cltab); return; }
806 if(!strcmp(token, "chid")) { chk_ftab(value, &account->fchid, "user", account->usr, "chid"); return; }
807
808 if (!strcmp(token, "expdate"))
809 {
810 if (!value[0]) {
811 account->expirationdate=(time_t)NULL;
812 return;
813 }
814 struct tm cstime;
815 memset(&cstime,0,sizeof(cstime));
816 for (i=0, ptr1=strtok(value, "-/"); (i<3)&&(ptr1); ptr1=strtok(NULL, "-/"), i++)
817 {
818 switch(i)
819 {
820 case 0: cstime.tm_year=atoi(ptr1)-1900; break;
821 case 1: cstime.tm_mon =atoi(ptr1)-1; break;
822 case 2: cstime.tm_mday=atoi(ptr1); break;
823 }
824 }
825 account->expirationdate=mktime(&cstime);
826 return;
827 }
828
829#ifdef CS_ANTICASC
830 if( !strcmp(token, "numusers") )
831 {
832 account->ac_users = atoi(value);
833 return;
834 }
835 if( !strcmp(token, "penalty") )
836 {
837 account->ac_penalty = atoi(value);
838 return;
839 }
840#endif
841 if (token[0] != '#')
842 fprintf(stderr, "Warning: keyword '%s' in account section not recognized\n",token);
843
844// if (!strcmp(token, "caid"))
845// {
846// for (i=0, ptr1=strtok(value, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
847// {
848// ulong caid, mask;
849// if (ptr2=strchr(trim(ptr1), '&'))
850// *ptr2++='\0';
851// else
852// ptr2="";
853// if (((caid=a2i(ptr1, 2))|(mask=a2i(ptr2,-2))) < 0x10000)
854// {
855// account->caidtab[i][0]=caid;
856// account->caidtab[i++][1]=mask;
857// }
858// else
859// cs_log("WARNING: wrong CAID in %s -> ignored", cs_user);
860// }
861// }
862}
863
864int write_services()
865{
866 int i;
867 FILE *f;
868 struct s_sidtab *sidtab = cfg->sidtab;
869 char tmpfile[256];
870 char destfile[256];
871 char bakfile[256];
872
873 snprintf(destfile, 255,"%s%s", cs_confdir, cs_sidt);
874 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_sidt);
875 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_sidt);
876
877 if (!(f=fopen(tmpfile, "w"))){
878 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
879 return(1);
880 }
881 fprintf(f,"#oscam.services generated automatically\n\n");
882
883 while(sidtab != NULL){
884 fprintf(f,"[%s]\n", sidtab->label);
885 fprintf_conf(f, CONFVARWIDTH, "caid", "");
886 for (i=0; i<sidtab->num_caid; i++){
887 if (i==0) fprintf(f,"%04X", sidtab->caid[i]);
888 else fprintf(f,",%04X", sidtab->caid[i]);
889 }
890 fputc((int)'\n', f);
891 fprintf_conf(f, CONFVARWIDTH, "provid", "");
892 for (i=0; i<sidtab->num_provid; i++){
893 if (i==0) fprintf(f,"%08lX", sidtab->provid[i]);
894 else fprintf(f,",%08lX", sidtab->provid[i]);
895 }
896 fputc((int)'\n', f);
897 fprintf_conf(f, CONFVARWIDTH, "srvid", "");
898 for (i=0; i<sidtab->num_srvid; i++){
899 if (i==0) fprintf(f,"%04X", sidtab->srvid[i]);
900 else fprintf(f,",%04X", sidtab->srvid[i]);
901 }
902 fprintf(f,"\n\n");
903 sidtab=sidtab->next;
904 }
905
906 fclose(f);
907 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
908}
909
910int write_config()
911{
912 int i,j;
913 FILE *f;
914 char *dot = "", *dot1 = "", *dot2 = ""; //flags for delimiters
915 char tmpfile[256];
916 char destfile[256];
917 char bakfile[256];
918
919 snprintf(destfile, 255,"%s%s", cs_confdir, cs_conf);
920 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_conf);
921 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_conf);
922
923 if (!(f=fopen(tmpfile, "w"))){
924 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
925 return(1);
926 }
927 fprintf(f,"#oscam.config generated automatically\n\n");
928
929 /*global settings*/
930 fprintf(f,"[global]\n");
931 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->srvip));
932 fprintf_conf(f, CONFVARWIDTH, "logfile", "%s\n", logfile);
933 fprintf_conf(f, CONFVARWIDTH, "pidfile", "%s\n", cfg->pidfile);
934 fprintf_conf(f, CONFVARWIDTH, "usrfile", "%s\n", cfg->usrfile);
935 fprintf_conf(f, CONFVARWIDTH, "cwlogdir", "%s\n", cfg->cwlogdir);
936 fprintf_conf(f, CONFVARWIDTH, "clienttimeout", "%ld\n", cfg->ctimeout/1000);
937 fprintf_conf(f, CONFVARWIDTH, "fallbacktimeout", "%ld\n", cfg->ftimeout/1000);
938 fprintf_conf(f, CONFVARWIDTH, "clientmaxidle", "%d\n", cfg->cmaxidle);
939 fprintf_conf(f, CONFVARWIDTH, "cachedelay", "%ld\n", cfg->delay/1000);
940 fprintf_conf(f, CONFVARWIDTH, "bindwait", "%d\n", cfg->bindwait);
941 fprintf_conf(f, CONFVARWIDTH, "netprio", "%ld\n", cfg->netprio);
942 fprintf_conf(f, CONFVARWIDTH, "resolvedelay", "%d\n", cfg->resolvedelay);
943 fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", cfg->tosleep);
944 fprintf_conf(f, CONFVARWIDTH, "unlockparental", "%d\n", cfg->ulparent);
945 fprintf_conf(f, CONFVARWIDTH, "nice", "%d\n", cfg->nice);
946 fprintf_conf(f, CONFVARWIDTH, "serialreadertimeout", "%d\n", cfg->srtimeout);
947 fprintf_conf(f, CONFVARWIDTH, "maxlogsize", "%d\n", cfg->max_log_size);
948 fprintf_conf(f, CONFVARWIDTH, "showecmdw", "%d\n", cfg->show_ecm_dw);
949 fprintf_conf(f, CONFVARWIDTH, "waitforcards", "%d\n", cfg->waitforcards);
950 fprintf_conf(f, CONFVARWIDTH, "preferlocalcards", "%d\n", cfg->preferlocalcards);
951 fputc((int)'\n', f);
952
953 /*monitor settings*/
954 fprintf(f,"[monitor]\n");
955 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->mon_port);
956 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
957
958 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
959 struct s_ip *cip;
960 for (cip = cfg->mon_allowed; cip; cip = cip->next){
961 fprintf(f,"%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
962 if (cip->ip[0] == cip->ip[1]) fprintf(f,"-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
963 dot=",";
964 }
965 fputc((int)'\n', f);
966 fprintf_conf(f, CONFVARWIDTH, "aulow", "%d\n", cfg->mon_aulow);
967 fprintf_conf(f, CONFVARWIDTH, "hideclient_to", "%d\n", cfg->mon_hideclient_to);
968 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", cfg->mon_level);
969 fprintf_conf(f, CONFVARWIDTH, "httpport", "%d\n", cfg->http_port);
970 fprintf_conf(f, CONFVARWIDTH, "httpuser", "%s\n", cfg->http_user);
971 fprintf_conf(f, CONFVARWIDTH, "httppwd", "%s\n", cfg->http_pwd);
972 fprintf_conf(f, CONFVARWIDTH, "httpcss", "%s\n", cfg->http_css);
973 fprintf_conf(f, CONFVARWIDTH, "httprefresh", "%d\n", cfg->http_refresh);
974 fprintf_conf(f, CONFVARWIDTH, "httphideidleclients", "%d\n", cfg->http_hide_idle_clients);
975 fputc((int)'\n', f);
976
977 /*newcamd*/
978 if ((cfg->ncd_ptab.nports > 0) && (cfg->ncd_ptab.ports[0].s_port > 0)){
979 fprintf(f,"[newcamd]\n");
980 fprintf_conf(f, CONFVARWIDTH, "port", "");
981 dot1 = "";
982 for(i = 0; i < cfg->ncd_ptab.nports; ++i){
983 fprintf(f,"%s%d@%04X", dot1, cfg->ncd_ptab.ports[i].s_port, cfg->ncd_ptab.ports[i].ftab.filts[0].caid);
984 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0){
985 fprintf(f,":");
986 dot2 = "";
987 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j){
988 fprintf(f,"%s%lX", dot2, cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
989 dot2 = ",";
990 }
991 }
992 dot1=";";
993 }
994
995 fputc((int)'\n', f);
996 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
997 fprintf_conf(f, CONFVARWIDTH, "key", "");
998 for (i=0;i<14;i++) fprintf(f,"%02X", cfg->ncd_key[i]);
999 fprintf(f,"\n\n");
1000 }
1001
1002 /*camd3.3*/
1003 if ( cfg->c33_port > 0) {
1004 fprintf(f,"[camd33]\n");
1005 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c33_port);
1006 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
1007 fprintf_conf(f, CONFVARWIDTH, "passive", "%d\n", cfg->c33_passive);
1008 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);
1009 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
1010 dot="";
1011 for (cip = cfg->c33_plain; cip; cip = cip->next){
1012 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1013 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1014 dot=",";
1015 }
1016 fprintf(f,"\n\n");
1017 }
1018
1019 /*camd3.5*/
1020 if ( cfg->c35_port > 0) {
1021 fprintf(f,"[cs357x]\n");
1022 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c35_port);
1023 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1024 fprintf(f,"\n\n");
1025 }
1026
1027 /*camd3.5 TCP*/
1028 if ((cfg->c35_tcp_ptab.nports > 0) && (cfg->c35_tcp_ptab.ports[0].s_port > 0)) {
1029 fprintf(f,"[cs378x]\n");
1030 fprintf_conf(f, CONFVARWIDTH, "port", "");
1031 dot1 = "";
1032 for(i = 0; i < cfg->c35_tcp_ptab.nports; ++i){
1033 fprintf(f,"%s%d@%04X", dot1, cfg->c35_tcp_ptab.ports[i].s_port, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].caid);
1034 if (cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids > 0){
1035 fprintf(f,":");
1036 dot2 = "";
1037 for (j = 0; j < cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids; ++j){
1038 fprintf(f,"%s%lX", dot2, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].prids[j]);
1039 dot2 = ",";
1040 }
1041 }
1042 dot1=";";
1043 }
1044
1045 fputc((int)'\n', f);
1046 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1047 fputc((int)'\n', f);
1048 }
1049
1050 /*Radegast*/
1051 if ( cfg->rad_port > 0) {
1052 fprintf(f,"[radegast]\n");
1053 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->rad_port);
1054 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
1055 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->rad_usr);
1056 fprintf_conf(f, CONFVARWIDTH, "allowed", "");
1057 struct s_ip *cip;
1058 for (cip = cfg->rad_allowed; cip; cip = cip->next){
1059 fprintf(f,"%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
1060 if (cip->ip[0] == cip->ip[1]) fprintf(f,"-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
1061 dot=",";
1062 }
1063 fprintf(f,"\n\n");
1064 }
1065
1066#ifdef CS_WITH_GBOX
1067 /*Gbox*/
1068 if ((cfg->gbox_pwd[0] > 0) || (cfg->gbox_pwd[1] > 0) || (cfg->gbox_pwd[2] > 0) || (cfg->gbox_pwd[3] > 0)){
1069 fprintf(f,"[gbox]\n");
1070 fprintf_conf(f, CONFVARWIDTH, "password", ""); for (i=0;i<4;i++) fprintf(f,"%02X", cfg->gbox_pwd[i]); fputc((int)'\n', f);;
1071 fprintf_conf(f, CONFVARWIDTH, "maxdist", "%d\n", cfg->maxdist);
1072 fprintf_conf(f, CONFVARWIDTH, "ignorelist", "%s\n", cfg->ignorefile);
1073 fprintf_conf(f, CONFVARWIDTH, "onlineinfos", "%s\n", cfg->gbxShareOnl);
1074 fprintf_conf(f, CONFVARWIDTH, "cardinfos", "%s\n", cfg->cardfile);
1075 fprintf_conf(f, CONFVARWIDTH, "locals", "");
1076 char *dot = "";
1077 for (i = 0; i < cfg->num_locals; i++){
1078 fprintf(f,"%s%06lX", dot, cfg->locals[i]);
1079 dot=";";
1080 }
1081 fprintf(f,"\n\n");
1082 }
1083#endif
1084
1085 /*serial*/
1086
1087
1088 /*cccam placeholder*/
1089
1090#ifdef HAVE_DVBAPI
1091 /*dvb-api*/
1092 if (cfg->dvbapi_enabled > 0) {
1093 fprintf(f,"[dvbapi]\n");
1094 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->dvbapi_enabled);
1095 fprintf_conf(f, CONFVARWIDTH, "au", "%d\n", cfg->dvbapi_au);
1096 fprintf_conf(f, CONFVARWIDTH, "boxtype", "%s\n", cfg->dvbapi_boxtype);
1097 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->dvbapi_usr);
1098 fputc((int)'\n', f);
1099 }
1100#endif
1101
1102#ifdef CS_ANTICASC
1103 if (cfg->ac_enabled > 0){
1104 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->ac_enabled);
1105 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", cfg->ac_users);
1106 fprintf_conf(f, CONFVARWIDTH, "sampletime", "%d\n", cfg->ac_stime);
1107 fprintf_conf(f, CONFVARWIDTH, "samples", "%d\n", cfg->ac_samples);
1108 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", cfg->ac_penalty);
1109 fprintf_conf(f, CONFVARWIDTH, "aclogfile", "%s\n", cfg->ac_logfile);
1110 fprintf_conf(f, CONFVARWIDTH, "denysamples", "%d\n", cfg->ac_denysamples);
1111 fprintf_conf(f, CONFVARWIDTH, "fakedelay", "%d\n", cfg->ac_fakedelay);
1112 fputc((int)'\n', f);
1113 }
1114#endif
1115
1116 fclose(f);
1117
1118 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1119}
1120
1121int write_userdb()
1122{
1123 int i;
1124 FILE *f;
1125 struct s_auth *account;
1126 char *dot = ""; //flag for comma
1127 char tmpfile[256];
1128 char destfile[256];
1129 char bakfile[256];
1130
1131 snprintf(destfile, 255,"%s%s", cs_confdir, cs_user);
1132 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_user);
1133 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_user);
1134
1135 if (!(f=fopen(tmpfile, "w"))){
1136 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
1137 return(1);
1138 }
1139 fprintf(f,"#oscam.user generated automatically\n\n");
1140
1141 //each account
1142 for (account=cfg->account; (account) ; account=account->next){
1143 fprintf(f,"[account]\n");
1144 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", account->usr);
1145 fprintf_conf(f, CONFVARWIDTH, "pwd", "%s\n", account->pwd);
1146 struct tm * timeinfo = localtime (&account->expirationdate);
1147 char buf [80];
1148 strftime (buf,80,"%Y-%m-%d",timeinfo);
1149 if(strcmp(buf,"1970-01-01"))
1150 fprintf_conf(f, CONFVARWIDTH, "expdate", "%s\n", buf);
1151 else
1152 fprintf_conf(f, CONFVARWIDTH, "expdate", "\n");
1153
1154 //group
1155 char *value = mk_t_group((long*)account->grp);
1156 fprintf_conf(f, CONFVARWIDTH, "group", "%s\n", value);
1157 free(value);
1158
1159 fprintf_conf(f, CONFVARWIDTH, "hostname", "%s\n", account->dyndns);
1160 fprintf_conf(f, CONFVARWIDTH, "uniq", "%d\n", account->uniq);
1161 fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", account->tosleep);
1162 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", account->monlvl);
1163
1164 if (account->au > -1)
1165 if (account->au < CS_MAXREADER)
1166 fprintf_conf(f, CONFVARWIDTH, "au", "%s\n", reader[account->au].label);
1167 if (account->autoau == 1) fprintf_conf(f, CONFVARWIDTH, "au", "1\n");
1168
1169 fprintf_conf(f, CONFVARWIDTH, "services", "");
1170 char sidok[33]; long2bitchar(account->sidtabok,sidok);
1171 char sidno[33]; long2bitchar(account->sidtabno,sidno);
1172 struct s_sidtab *sidtab = cfg->sidtab;
1173 i=0; dot = "";
1174 for (; sidtab; sidtab=sidtab->next){
1175 if(sidok[i]=='1') {fprintf(f,"%s%s", dot, sidtab->label); dot = ",";}
1176 if(sidno[i]=='1') {fprintf(f,"%s!%s", dot, sidtab->label); dot = ",";}
1177 i++;
1178 }
1179 fputc((int)'\n', f);
1180
1181 //CAID
1182 value = mk_t_caidtab(&account->ctab);
1183 fprintf_conf(f, CONFVARWIDTH, "caid", "%s\n", value);
1184 free(value);
1185
1186 //betatunnel
1187 value = mk_t_tuntab(&account->ttab);
1188 fprintf_conf(f, CONFVARWIDTH, "betatunnel", "%s\n", value);
1189 free(value);
1190
1191 //ident
1192 value = mk_t_ftab(&account->ftab);
1193 fprintf_conf(f, CONFVARWIDTH, "ident", "%s\n", value);
1194 free(value);
1195
1196#ifdef CS_ANTICASC
1197 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", account->ac_users);
1198 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", account->ac_penalty);
1199#endif
1200 fputc((int)'\n', f);
1201 }
1202 fclose(f);
1203
1204 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1205}
1206
1207int init_userdb()
1208{
1209 int tag=0, nr, nro, expired;
1210 //int first=1;
1211 FILE *fp;
1212 char *value;
1213 struct s_auth *ptr;
1214 /*static */struct s_auth *account=(struct s_auth *)0;
1215
1216 sprintf(token, "%s%s", cs_confdir, cs_user);
1217 if (!(fp=fopen(token, "r")))
1218 {
1219 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1220 return(1);
1221 }
1222 for (nro=0, ptr=cfg->account; ptr; nro++)
1223 {
1224 struct s_auth *ptr_next;
1225 ptr_next=ptr->next;
1226 free(ptr);
1227 ptr=ptr_next;
1228 }
1229 nr=0;
1230 while (fgets(token, sizeof(token), fp))
1231 {
1232 int i, l;
1233 void *ptr;
1234 if ((l=strlen(trim(token)))<3) continue;
1235 if ((token[0]=='[') && (token[l-1]==']'))
1236 {
1237 token[l-1]=0;
1238 tag=(!strcmp("account", strtolower(token+1)));
1239 if (!(ptr=malloc(sizeof(struct s_auth))))
1240 {
1241 cs_log("Error allocating memory (errno=%d)", errno);
1242 return(1);
1243 }
1244 if (account)
1245 account->next=ptr;
1246 else
1247 cfg->account=ptr;
1248 account=ptr;
1249 memset(account, 0, sizeof(struct s_auth));
1250 account->au=(-1);
1251 account->monlvl=cfg->mon_level;
1252 account->tosleep=cfg->tosleep;
1253 for (i=1; i<CS_MAXCAIDTAB; account->ctab.mask[i++]=0xffff);
1254 for (i=1; i<CS_MAXTUNTAB; account->ttab.bt_srvid[i++]=0x0000);
1255 nr++;
1256#ifdef CS_ANTICASC
1257 account->ac_users=cfg->ac_users;
1258 account->ac_penalty=cfg->ac_penalty;
1259 account->ac_idx = nr;
1260#endif
1261 continue;
1262 }
1263 if (!tag) continue;
1264 if (!(value=strchr(token, '='))) continue;
1265 *value++='\0';
1266 chk_account(trim(strtolower(token)), trim(value), account);
1267 }
1268 fclose(fp);
1269
1270 for (expired=0, ptr=cfg->account; ptr;)
1271 {
1272 if(ptr->expirationdate && ptr->expirationdate<time(NULL)) expired++;
1273 ptr=ptr->next;
1274 }
1275
1276 cs_log("userdb reloaded: %d accounts freed, %d accounts loaded, %d expired", nro, nr, expired);
1277 return(0);
1278}
1279
1280static void chk_entry4sidtab(char *value, struct s_sidtab *sidtab, int what)
1281{
1282 int i, b;
1283 char *ptr;
1284 ushort *slist=(ushort *) 0;
1285 ulong *llist=(ulong *) 0;
1286 ulong caid;
1287 char buf[strlen(value) + 1];
1288 strncpy(buf, value, sizeof(buf));
1289 b=(what==1) ? sizeof(ulong) : sizeof(ushort);
1290 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1291 {
1292 caid=a2i(ptr, b);
1293 if (!errno) i++;
1294 }
1295 //if (!i) return(0);
1296 if (b==sizeof(ushort))
1297 slist=malloc(i*sizeof(ushort));
1298 else
1299 llist=malloc(i*sizeof(ulong));
1300 strcpy(value, buf);
1301 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1302 {
1303 caid=a2i(ptr, b);
1304 if (errno) continue;
1305 if (b==sizeof(ushort))
1306 slist[i++]=(ushort) caid;
1307 else
1308 llist[i++]=caid;
1309 }
1310 switch (what)
1311 {
1312 case 0: sidtab->caid=slist;
1313 sidtab->num_caid=i;
1314 break;
1315 case 1: sidtab->provid=llist;
1316 sidtab->num_provid=i;
1317 break;
1318 case 2: sidtab->srvid=slist;
1319 sidtab->num_srvid=i;
1320 break;
1321 }
1322}
1323
1324void chk_sidtab(char *token, char *value, struct s_sidtab *sidtab)
1325{
1326 if (!strcmp(token, "caid")) { chk_entry4sidtab(value, sidtab, 0); return; }
1327 if (!strcmp(token, "provid")) { chk_entry4sidtab(value, sidtab, 1); return; }
1328 if (!strcmp(token, "ident")) { chk_entry4sidtab(value, sidtab, 1); return; }
1329 if (!strcmp(token, "srvid")) { chk_entry4sidtab(value, sidtab, 2); return; }
1330 if (token[0] != '#')
1331 fprintf(stderr, "Warning: keyword '%s' in sidtab section not recognized\n",token);
1332}
1333
1334int init_sidtab()
1335{
1336 int nr, nro;
1337 FILE *fp;
1338 char *value;
1339 struct s_sidtab *ptr;
1340 struct s_sidtab *sidtab=(struct s_sidtab *)0;
1341
1342 sprintf(token, "%s%s", cs_confdir, cs_sidt);
1343 if (!(fp=fopen(token, "r")))
1344 {
1345 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1346 return(1);
1347 }
1348 for (nro=0, ptr=cfg->sidtab; ptr; nro++)
1349 {
1350 struct s_sidtab *ptr_next;
1351 ptr_next=ptr->next;
1352 if (ptr->caid) free(ptr->caid);
1353 if (ptr->provid) free(ptr->provid);
1354 if (ptr->srvid) free(ptr->srvid);
1355 free(ptr);
1356 ptr=ptr_next;
1357 }
1358 nr=0;
1359 while (fgets(token, sizeof(token), fp))
1360 {
1361 int l;
1362 void *ptr;
1363 if ((l=strlen(trim(token)))<3) continue;
1364 if ((token[0]=='[') && (token[l-1]==']'))
1365 {
1366 token[l-1]=0;
1367 if (!(ptr=malloc(sizeof(struct s_sidtab))))
1368 {
1369 cs_log("Error allocating memory (errno=%d)", errno);
1370 return(1);
1371 }
1372 if (sidtab)
1373 sidtab->next=ptr;
1374 else
1375 cfg->sidtab=ptr;
1376 sidtab=ptr;
1377 nr++;
1378 memset(sidtab, 0, sizeof(struct s_sidtab));
1379 strncpy(sidtab->label, strtolower(token+1), sizeof(sidtab->label));
1380 continue;
1381 }
1382 if (!sidtab) continue;
1383 if (!(value=strchr(token, '='))) continue;
1384 *value++='\0';
1385 chk_sidtab(trim(strtolower(token)), trim(strtolower(value)), sidtab);
1386 }
1387 fclose(fp);
1388
1389#ifdef DEBUG_SIDTAB
1390 show_sidtab(cfg->sidtab);
1391#endif
1392 cs_log("services reloaded: %d services freed, %d services loaded", nro, nr);
1393 return(0);
1394}
1395
1396int init_srvid()
1397{
1398 int nr;
1399 FILE *fp;
1400 char *value;
1401 static struct s_srvid *srvid=(struct s_srvid *)0;
1402
1403 sprintf(token, "%s%s", cs_confdir, cs_srid);
1404 if (!(fp=fopen(token, "r")))
1405 {
1406 cs_log("can't open file \"%s\" (err=%d), no service-id's loaded",
1407 token, errno);
1408 return(0);
1409 }
1410 nr=0;
1411 while (fgets(token, sizeof(token), fp))
1412 {
1413 int l;
1414 void *ptr;
1415 if ((l=strlen(trim(token)))<6) continue;
1416 if (!(value=strchr(token, ':'))) continue;
1417 *value++='\0';
1418 if (strlen(token)!=4) continue;
1419 if (!(ptr=malloc(sizeof(struct s_srvid))))
1420 {
1421 cs_log("Error allocating memory (errno=%d)", errno);
1422 return(1);
1423 }
1424 if (srvid)
1425 srvid->next=ptr;
1426 else
1427 cfg->srvid=ptr;
1428 srvid=ptr;
1429 memset(srvid, 0, sizeof(struct s_srvid));
1430 srvid->srvid=word_atob(token);
1431 strncpy(srvid->name, value, sizeof(srvid->name)-1);
1432 nr++;
1433 }
1434 fclose(fp);
1435 cs_log("%d service-id's loaded", nr);
1436 return(0);
1437}
1438
1439static void chk_reader(char *token, char *value, struct s_reader *rdr)
1440{
1441 int i;
1442 char *ptr;
1443 /*
1444 * case sensitive first
1445 */
1446 if (!strcmp(token, "device"))
1447 {
1448 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1449 {
1450 trim(ptr);
1451 switch(i)
1452 {
1453 case 0: strncpy(rdr->device, ptr, sizeof(rdr->device)-1); break;
1454 case 1: rdr->r_port=atoi(ptr); break;
1455 case 2: rdr->l_port=atoi(ptr); break;
1456 }
1457 }
1458 return;
1459 }
1460 if (!strcmp(token, "key"))
1461 {
1462 if (key_atob14(value, rdr->ncd_key))
1463 {
1464 fprintf(stderr, "Configuration newcamd: Error in Key\n");
1465 exit(1);
1466 }
1467 return;
1468 }
1469#ifdef CS_WITH_GBOX
1470 if (!strcmp(token, "password")) { strncpy((char *)rdr->gbox_pwd, (const char *)i2b(4, a2i(value, 4)), 4); return; }
1471 if (!strcmp(token, "premium")) { rdr->gbox_prem=1; return; }
1472#endif
1473 if (!strcmp(token, "account"))
1474 {
1475 for (i=0, ptr=strtok(value, ","); (i<2)&&(ptr); ptr=strtok(NULL, ","), i++)
1476 {
1477 trim(ptr);
1478 switch(i)
1479 {
1480 case 0: strncpy(rdr->r_usr, ptr, sizeof(rdr->r_usr)-1); break;
1481 case 1: strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd)-1); break;
1482 }
1483 }
1484 return;
1485 }
1486 if( !strcmp(token, "pincode")) { strncpy(rdr->pincode, value, sizeof(rdr->pincode)-1); return; }
1487 if (!strcmp(token, "readnano")) { strncpy((char *)rdr->emmfile, value, sizeof(rdr->emmfile)-1); return; }
1488 /*
1489 * case insensitive
1490 */
1491 strtolower(value);
1492
1493 if (!strcmp(token, "services")) { chk_services(value, &rdr->sidtabok, &rdr->sidtabno); return; }
1494 if (!strcmp(token, "inactivitytimeout")) { rdr->tcp_ito = atoi(value); return; }
1495 if (!strcmp(token, "reconnecttimeout")) { rdr->tcp_rto = atoi(value); return; }
1496 if (!strcmp(token, "disableserverfilter")) { rdr->ncd_disable_server_filt = atoi(value); return; }
1497
1498 if (!strcmp(token, "label")) { strncpy(rdr->label, value, sizeof(rdr->label)-1); return; }
1499 if (!strcmp(token, "fallback")) { rdr->fallback=atoi(value) ? 1 : 0; return; }
1500 if (!strcmp(token, "logport")) { rdr->log_port=atoi(value); return; }
1501 if (!strcmp(token, "caid")) { chk_caidtab(value, &rdr->ctab); return; }
1502 if (!strcmp(token, "boxid")) { rdr->boxid=a2i(value,4); return; }
1503 if (!strcmp(token, "aeskey"))
1504 {
1505 if (key_atob(value, rdr->aes_key))
1506 {
1507 fprintf(stderr, "Configuration reader: Error in AES Key\n");
1508 exit(1);
1509 }
1510 return;
1511 }
1512 if (!strcmp(token, "n3_rsakey"))
1513 {
1514 rdr->nagra_native=1;
1515 if (key_atob_l(value, rdr->rsa_mod, 128))
1516 {
1517 fprintf(stderr, "Configuration reader: Error in n3_rsakey\n");
1518 exit(1);
1519 }
1520 return;
1521 }
1522 if (!strcmp(token, "tiger_rsakey"))
1523 {
1524 rdr->nagra_native=1;
1525 if (key_atob_l(value, rdr->rsa_mod, 240))
1526 {
1527 fprintf(stderr, "Configuration reader: Error in tiger_rsakey\n");
1528 exit(1);
1529 }
1530 return;
1531 }
1532 if (!strcmp(token, "n3_boxkey"))
1533 {
1534 if (key_atob_l(value, rdr->nagra_boxkey, 16))
1535 {
1536 fprintf(stderr, "Configuration reader: Error in Nagra Boxkey\n");
1537 exit(1);
1538 }
1539 return;
1540 }
1541 if (!strcmp(token, "tiger_ideakey"))
1542 {
1543 if (key_atob_l(value, rdr->nagra_boxkey, 32))
1544 {
1545 fprintf(stderr, "Configuration reader: Error in Nagra Boxkey\n");
1546 exit(1);
1547 }
1548 return;
1549 }
1550 if (!strcmp(token, "detect"))
1551 {
1552 for (i=0; RDR_CD_TXT[i]; i++)
1553 {
1554 if (!strcmp(value, RDR_CD_TXT[i]))
1555 rdr->detect=i;
1556 else
1557 if ((value[0]=='!') && (!strcmp(value+1, RDR_CD_TXT[i])))
1558 rdr->detect=i|0x80;
1559 }
1560 return;
1561 }
1562 if (!strcmp(token, "mhz")) { rdr->mhz=atoi(value); return; }
1563 if (!strcmp(token, "cardmhz")) { rdr->cardmhz=atoi(value); return; }
1564 if (!strcmp(token, "protocol"))
1565 {
1566 if (!strcmp(value, "mouse")) { rdr->typ=R_MOUSE; return; }
1567 if (!strcmp(value, "smartreader")) { rdr->typ=R_SMART; return; }
1568 if (!strcmp(value, "internal")) { rdr->typ=R_INTERNAL; return; }
1569#ifdef HAVE_PCSC
1570 if (!strcmp(value, "pcsc")) { rdr->typ=R_PCSC; return; }
1571#endif
1572 if (!strcmp(value, "serial")) { rdr->typ=R_SERIAL; return; }
1573 if (!strcmp(value, "camd35")) { rdr->typ=R_CAMD35; return; }
1574 if (!strcmp(value, "cs378x")) { rdr->typ=R_CS378X; return; }
1575 if (!strcmp(value, "cs357x")) { rdr->typ=R_CAMD35; return; }
1576 if (!strcmp(value, "gbox")) { rdr->typ=R_GBOX; return; }
1577 if (!strcmp(value, "cccam")) {
1578 rdr->typ=R_CCCAM;
1579 // strcpy(value, "1");
1580 // chk_caidtab(value, &rdr->ctab); // this is a MAJOR hack for auto multiple caid support (not currently working due to ncd table issue)
1581 return;
1582 }
1583 if (!strcmp(value, "radegast")) { rdr->typ=R_RADEGAST; return; }
1584 if (!strcmp(value, "newcamd") ||
1585 !strcmp(value, "newcamd525")) {rdr->typ=R_NEWCAMD;
1586 rdr->ncd_proto=NCD_525; return; }
1587 if (!strcmp(value, "newcamd524")) {rdr->typ=R_NEWCAMD;
1588 rdr->ncd_proto=NCD_524; return; }
1589 fprintf(stderr, "WARNING: value '%s' in protocol-line not recognized, assuming MOUSE\n",value);
1590 rdr->typ=R_MOUSE;
1591 return;
1592 }
1593 if (!strcmp(token, "ident")) { chk_ftab(value, &rdr->ftab,"reader",rdr->label,"provid"); return; }
1594 if (!strcmp(token, "class")) { chk_cltab(value, &rdr->cltab); return; }
1595 if (!strcmp(token, "chid")) { chk_ftab(value, &rdr->fchid,"reader",rdr->label,"chid"); return; }
1596 if (!strcmp(token, "showcls")) { rdr->show_cls = atoi(value); return; }
1597 if (!strcmp(token, "maxqlen"))
1598 {
1599 rdr->maxqlen = atoi(value);
1600 if( rdr->maxqlen<0 || rdr->maxqlen>CS_MAXQLEN )
1601 rdr->maxqlen=CS_MAXQLEN;
1602 return;
1603 }
1604 if (!strcmp(token, "group"))
1605 {
1606 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1607 {
1608 int g;
1609 g=atoi(ptr);
1610 if ((g>0) && (g<33)) rdr->grp|=(1<<(g-1));
1611 }
1612 return;
1613 }
1614 if (!strcmp(token, "emmcache"))
1615 {
1616 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1617 switch(i)
1618 {
1619 case 0: rdr->cachemm=atoi(ptr); break;
1620 case 1: rdr->rewritemm=atoi(ptr); break;
1621 case 2: rdr->logemm=atoi(ptr); break;
1622 }
1623 if (rdr->rewritemm <=0) {
1624 fprintf(stderr, "Notice: Setting EMMCACHE to %i,1,%i instead of %i,%i,%i. Zero or negative number of rewrites is silly\n", rdr->cachemm,rdr->logemm,rdr->cachemm,rdr->rewritemm,rdr->logemm);
1625 rdr->rewritemm = 1;
1626 }
1627 return;
1628 }
1629
1630 if (!strcmp(token, "blocknano"))
1631 {
1632 if (!strcmp(value,"all")) //wildcard is used
1633 for (i=0 ; i<256; i++)
1634 rdr->b_nano[i] |= 0x01; //set all lsb's to block all nanos
1635 else
1636 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1637 if ((i=byte_atob(ptr))>=0)
1638 rdr->b_nano[i]|= 0x01; //lsb is set when to block nano
1639 return;
1640 }
1641 if (!strcmp(token, "savenano"))
1642 {
1643 if (!strcmp(value,"all")) //wildcard is used
1644 for (i=0 ; i<256; i++)
1645 rdr->b_nano[i] |= 0x02; //set all lsb+1 to save all nanos to file
1646 else
1647 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1648 if ((i=byte_atob(ptr))>=0)
1649 rdr->b_nano[i]|= 0x02; //lsb+1 is set when to save nano to file
1650 return;
1651 }
1652 if (!strcmp(token, "cccversion")) { // cccam version
1653 if (strlen(value)>sizeof(rdr->cc_version)-1) {
1654 fprintf(stderr, "cccam config: version too long\n");
1655 exit(1);
1656 }
1657 bzero(rdr->cc_version, sizeof(rdr->cc_version));
1658 strncpy(rdr->cc_version, value, sizeof(rdr->cc_version)-1);
1659 return;
1660 }
1661 if (!strcmp(token, "cccbuild")) { // cccam build number
1662 if (strlen(value)>sizeof(rdr->cc_build)-1) {
1663 fprintf(stderr, "cccam config build number too long\n");
1664 exit(1);
1665 }
1666 bzero(rdr->cc_build, sizeof(rdr->cc_build));
1667 strncpy(rdr->cc_build, value, sizeof(rdr->cc_build)-1);
1668 return;
1669 }
1670 if (!strcmp(token, "cccmaxhop")) { // cccam max card distance
1671 rdr->cc_maxhop = atoi(value);
1672 return;
1673 }
1674 if (token[0] != '#')
1675 fprintf(stderr, "Warning: keyword '%s' in reader section not recognized\n",token);
1676}
1677
1678int init_readerdb()
1679{
1680 int tag=0, nr;
1681 FILE *fp;
1682 char *value;
1683
1684 sprintf(token, "%s%s", cs_confdir, cs_srvr);
1685 if (!(fp=fopen(token, "r")))
1686 {
1687 cs_log("can't open file \"%s\" (errno=%d)\n", token, errno);
1688 return(1);
1689 }
1690 nr=0;
1691 while (fgets(token, sizeof(token), fp))
1692 {
1693 int i, l;
1694 if ((l=strlen(trim(token)))<3) continue;
1695 if ((token[0]=='[') && (token[l-1]==']'))
1696 {
1697 token[l-1]=0;
1698 tag=(!strcmp("reader", strtolower(token+1)));
1699 if (reader[nr].label[0] && reader[nr].typ) nr++;
1700 memset(&reader[nr], 0, sizeof(struct s_reader));
1701 reader[nr].tcp_rto = 30;
1702 reader[nr].show_cls = 10;
1703 reader[nr].maxqlen = CS_MAXQLEN;
1704 reader[nr].mhz = 357;
1705 reader[nr].cardmhz = 357;
1706 strcpy(reader[nr].pincode, "none");
1707 for (i=1; i<CS_MAXCAIDTAB; reader[nr].ctab.mask[i++]=0xffff);
1708 continue;
1709 }
1710 if (!tag) continue;
1711 if (!(value=strchr(token, '='))) continue;
1712 *value++='\0';
1713 chk_reader(trim(strtolower(token)), trim(value), &reader[nr]);
1714 }
1715 fclose(fp);
1716 return(0);
1717}
1718
1719/*
1720int init_irdeto_guess_tab()
1721{
1722 int i, j, skip;
1723 int b47;
1724 FILE *fp;
1725 char token[128], *value, *ptr;
1726 char zSid[5];
1727 uchar b3;
1728 ushort caid, sid;
1729 struct s_irdeto_quess *ird_row, *head;
1730
1731 memset(cfg->itab, 0, sizeof(cfg->itab));
1732 sprintf(token, "%s%s", cs_confdir, cs_ird);
1733 if (!(fp=fopen(token, "r")))
1734 {
1735 cs_log("can't open file \"%s\" (errno=%d) irdeto guessing not loaded",
1736 token, errno);
1737 return(1);
1738 }
1739 while (fgets(token, sizeof(token), fp))
1740 {
1741 if( strlen(token)<20 ) continue;
1742 for( i=b3=b47=caid=sid=skip=0, ptr=strtok(token, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1743 {
1744 trim(ptr);
1745 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1746 skip=1;
1747 break;
1748 }
1749 switch(i)
1750 {
1751 case 0: b3 = a2i(ptr, 2); break;
1752 case 1: b47 = a2i(ptr, 8); break;
1753 case 2: caid = a2i(ptr, 4); break;
1754 case 3:
1755 for( j=0; j<4; j++ )
1756 zSid[j]=ptr[j];
1757 zSid[4]=0;
1758 sid = a2i(zSid, 4);
1759 break;
1760 }
1761 }
1762 if( !skip )
1763 {
1764 if (!(ird_row=(struct s_irdeto_quess*)malloc(sizeof(struct s_irdeto_quess))))
1765 {
1766 cs_log("Error allocating memory (errno=%d)", errno);
1767 return;
1768 }
1769 ird_row->b47 = b47;
1770 ird_row->caid = caid;
1771 ird_row->sid = sid;
1772 ird_row->next = 0;
1773
1774 head = cfg->itab[b3];
1775 if( head ) {
1776 while( head->next )
1777 head=head->next;
1778 head->next=ird_row;
1779 }
1780 else
1781 cfg->itab[b3]=ird_row;
1782
1783 //cs_debug("%02X:%08X:%04X:%04X", b3, b47, caid, sid);
1784 }
1785 }
1786 fclose(fp);
1787
1788 for( i=0; i<0xff; i++ )
1789 {
1790 head=cfg->itab[i];
1791 while(head)
1792 {
1793 cs_debug("itab[%02X]: b47=%08X, caid=%04X, sid=%04X",
1794 i, head->b47, head->caid, head->sid);
1795 head=head->next;
1796 }
1797 }
1798 return(0);
1799}
1800*/
1801
1802#ifdef CS_ANTICASC
1803void init_ac()
1804{
1805 int nr;
1806 FILE *fp;
1807 //char *value;
1808
1809 sprintf(token, "%s%s", cs_confdir, cs_ac);
1810 if (!(fp=fopen(token, "r")))
1811 {
1812 cs_log("can't open file \"%s\" (errno=%d) anti-cascading table not loaded",
1813 token, errno);
1814 return;
1815 }
1816
1817 for(nr=0; fgets(token, sizeof(token), fp);)
1818 {
1819 int i, skip;
1820 ushort caid, sid, chid, dwtime;
1821 ulong provid;
1822 char *ptr, *ptr1;
1823 struct s_cpmap *ptr_cpmap;
1824 static struct s_cpmap *cpmap=(struct s_cpmap *)0;
1825
1826 if( strlen(token)<4 ) continue;
1827
1828 caid=sid=chid=dwtime=0;
1829 provid=0;
1830 skip=0;
1831 ptr1=0;
1832 for( i=0, ptr=strtok(token, "="); (i<2)&&(ptr); ptr=strtok(NULL, "="), i++ )
1833 {
1834 trim(ptr);
1835 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1836 skip=1;
1837 break;
1838 }
1839 switch( i )
1840 {
1841 case 0:
1842 ptr1=ptr;
1843 break;
1844 case 1:
1845 dwtime = atoi(ptr);
1846 break;
1847 }
1848 }
1849
1850 if( !skip )
1851 {
1852 for( i=0, ptr=strtok(ptr1, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1853 {
1854 trim(ptr);
1855 switch( i )
1856 {
1857 case 0:
1858 if( *ptr=='*' ) caid = 0;
1859 else caid = a2i(ptr, 4);
1860 break;
1861 case 1:
1862 if( *ptr=='*' ) provid = 0;
1863 else provid = a2i(ptr, 6);
1864 break;
1865 case 2:
1866 if( *ptr=='*' ) sid = 0;
1867 else sid = a2i(ptr, 4);
1868 break;
1869 case 3:
1870 if( *ptr=='*' ) chid = 0;
1871 else chid = a2i(ptr, 4);
1872 break;
1873 }
1874 }
1875 if (!(ptr_cpmap=(struct s_cpmap*)malloc(sizeof(struct s_cpmap))))
1876 {
1877 cs_log("Error allocating memory (errno=%d)", errno);
1878 return;
1879 }
1880 if( cpmap )
1881 cpmap->next=ptr_cpmap;
1882 else
1883 cfg->cpmap=ptr_cpmap;
1884 cpmap=ptr_cpmap;
1885
1886 cpmap->caid = caid;
1887 cpmap->provid = provid;
1888 cpmap->sid = sid;
1889 cpmap->chid = chid;
1890 cpmap->dwtime = dwtime;
1891 cpmap->next = 0;
1892
1893 cs_debug("nr=%d, caid=%04X, provid=%06X, sid=%04X, chid=%04X, dwtime=%d",
1894 nr, caid, provid, sid, chid, dwtime);
1895 nr++;
1896 }
1897 }
1898 fclose(fp);
1899 //cs_log("%d lengths for caid guessing loaded", nr);
1900 return;
1901}
1902#endif
1903
1904/*
1905 * makes a char ready to write a token into config or webIf
1906 */
1907char *mk_t_caidtab(CAIDTAB *ctab){
1908 int i = 0, needed = 1, pos = 0;
1909 while(ctab->caid[i]){
1910 if(ctab->mask[i]) needed += 10;
1911 else needed += 5;
1912 if(ctab->cmap[i]) needed += 5;
1913 ++i;
1914 }
1915 char *value = (char *) malloc(needed * sizeof(char));
1916 i = 0;
1917 while(ctab->caid[i]) {
1918 if(i == 0) {
1919 sprintf(value + pos, "%04X", ctab->caid[i]);
1920 pos += 4;
1921 } else {
1922 sprintf(value + pos, ",%04X", ctab->caid[i]);
1923 pos += 5;
1924 }
1925 if(ctab->mask[i]){
1926 sprintf(value + pos, "&%04X", ctab->mask[i]);
1927 pos += 5;
1928 }
1929 if(ctab->cmap[i]){
1930 sprintf(value + pos, ":%04X", ctab->cmap[i]);
1931 pos += 5;
1932 }
1933 ++i;
1934 }
1935 value[pos] = '\0';
1936 return value;
1937}
1938
1939/*
1940 * makes a char ready to write a token into config or webIf
1941 */
1942char *mk_t_tuntab(TUNTAB *ttab){
1943 int i = 0, needed = 1, pos = 0;
1944 while(ttab->bt_caidfrom[i]){
1945 if(ttab->bt_srvid[i]) needed += 10;
1946 else needed += 5;
1947 if(ttab->bt_caidto[i]) needed += 5;
1948 ++i;
1949 }
1950 char *value = (char *) malloc(needed * sizeof(char));
1951 i = 0;
1952 while(ttab->bt_caidfrom[i]) {
1953 if(i == 0) {
1954 sprintf(value + pos, "%04X", ttab->bt_caidfrom[i]);
1955 pos += 4;
1956 } else {
1957 sprintf(value + pos, ",%04X", ttab->bt_caidfrom[i]);
1958 pos += 5;
1959 }
1960 if(ttab->bt_srvid[i]){
1961 sprintf(value + pos, ".%04X", ttab->bt_srvid[i]);
1962 pos += 5;
1963 }
1964 if(ttab->bt_caidto[i]){
1965 sprintf(value + pos, ":%04X", ttab->bt_caidto[i]);
1966 pos += 5;
1967 }
1968 ++i;
1969 }
1970 value[pos] = '\0';
1971 return value;
1972}
1973
1974/*
1975 * makes a char ready to write a token into config or webIf
1976 */
1977char *mk_t_group(ulong *grp){
1978 int i = 0, needed = 1, pos = 0, dot = 0;
1979 char grpbit[33];
1980 long2bitchar((long) grp, grpbit);
1981
1982 for(i = 0; i < 32; i++){
1983 if (grpbit[i] == '1'){
1984 needed += 2;
1985 if(i > 9) needed += 1;
1986 }
1987 }
1988 char *value = (char *) malloc(needed * sizeof(char));
1989
1990 for(i = 0; i < 32; i++){
1991 if (grpbit[i] == '1'){
1992 if (dot == 0){
1993 sprintf(value + pos, "%d", i+1);
1994 if (i > 9)pos += 2;
1995 else pos += 1;
1996 dot = 1;
1997 } else {
1998 sprintf(value + pos, ",%d", i+1);
1999 if (i > 9)pos += 3;
2000 else pos += 2;
2001 }
2002 }
2003 }
2004 value[pos] = '\0';
2005 return value;
2006}
2007
2008/*
2009 * makes a char ready to write a token into config or webIf
2010 */
2011char *mk_t_ftab(FTAB *ftab){
2012 int i = 0, j = 0, needed = 1, pos = 0;
2013
2014 needed = ftab->nfilts * 5;
2015 for (i = 0; i < ftab->nfilts; ++i)
2016 needed += ftab->filts[i].nprids * 7;
2017
2018 char *value = (char *) malloc(needed * sizeof(char));
2019
2020 char *dot="";
2021 for (i = 0; i < ftab->nfilts; ++i){
2022 sprintf(value + pos, "%s%04X", dot, ftab->filts[i].caid);
2023 pos += 4;
2024 if (i > 0) pos += 1;
2025 dot=":";
2026 for (j = 0; j < ftab->filts[i].nprids; ++j) {
2027 sprintf(value + pos, "%s%06lX", dot, ftab->filts[i].prids[j]);
2028 pos += 7;
2029 dot=",";
2030 }
2031 dot=";";
2032 }
2033
2034 value[pos] = '\0';
2035 return value;
2036}
Note: See TracBrowser for help on using the repository browser.