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

Last change on this file since 1250 was 1250, checked in by alno, 11 years ago

WebIf:

  • Merging revisions 1240-1249 of trunk
File size: 60.2 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 if (!strcmp(token, "priority")) { strncpy(cfg->dvbapi_priority, value, sizeof(cfg->dvbapi_priority)-1); return; }
572 if (!strcmp(token, "ignore")) { strncpy(cfg->dvbapi_ignore, value, sizeof(cfg->dvbapi_ignore)-1); return; }
573
574 if (token[0] != '#')
575 fprintf(stderr, "Warning: keyword '%s' in dvbapi section not recognized\n",token);
576}
577#endif
578
579static void chk_token(char *token, char *value, int tag)
580{
581 switch(tag)
582 {
583 case TAG_GLOBAL : chk_t_global(token, value); break;
584 case TAG_MONITOR : chk_t_monitor(token, value); break;
585 case TAG_CAMD33 : chk_t_camd33(token, value); break;
586 case TAG_CAMD35 :
587 case TAG_CS357X : chk_t_camd35(token, value); break;
588 case TAG_NEWCAMD : chk_t_newcamd(token, value); break;
589 case TAG_RADEGAST: chk_t_radegast(token, value); break;
590 case TAG_SERIAL : chk_t_serial(token, value); break;
591 case TAG_CS378X : chk_t_camd35_tcp(token, value); break;
592#ifdef CS_WITH_GBOX
593 case TAG_GBOX : chk_t_gbox(token, value); break;
594#endif
595 case TAG_CCCAM : chk_t_cccam(token, value); break;
596#ifdef HAVE_DVBAPI
597 case TAG_DVBAPI : chk_t_dvbapi(token, value); break;
598#else
599 case TAG_DVBAPI : fprintf(stderr, "Warning: OSCam compiled without DVB API support.\n"); break;
600#endif
601#ifdef CS_ANTICASC
602 case TAG_ANTICASC: chk_t_ac(token, value); break;
603#endif
604 }
605}
606
607void init_len4caid()
608{
609 int nr;
610 FILE *fp;
611 char *value;
612
613 memset(len4caid, 0, sizeof(ushort)<<8);
614 sprintf(token, "%s%s", cs_confdir, cs_l4ca);
615 if (!(fp=fopen(token, "r")))
616 return;
617 for(nr=0; fgets(token, sizeof(token), fp);)
618 {
619 int i, c;
620 char *ptr;
621 if (!(value=strchr(token, ':'))) continue;
622 *value++='\0';
623 if( (ptr=strchr(value, '#')) )
624 *ptr='\0';
625 if (strlen(trim(token))!=2) continue;
626 if (strlen(trim(value))!=4) continue;
627 if ((i=byte_atob(token))<0) continue;
628 if ((c=word_atob(value))<0) continue;
629//printf("idx %02X = %04X\n", i, c); fflush(stdout);
630 len4caid[i]=c;
631 nr++;
632 }
633 fclose(fp);
634 cs_log("%d lengths for caid guessing loaded", nr);
635 return;
636}
637
638int search_boxkey(ushort caid, char *key)
639{
640 int i, rc=0;
641 FILE *fp;
642 char c_caid[512];
643
644 sprintf(c_caid, "%s%s", cs_confdir, cs_cert);
645 fp=fopen(c_caid, "r");
646 if (fp)
647 {
648 for (; (!rc) && fgets(c_caid, sizeof(c_caid), fp);)
649 {
650 char *c_provid, *c_key;
651
652 c_provid=strchr(c_caid, '#');
653 if (c_provid) *c_provid='\0';
654 if (!(c_provid=strchr(c_caid, ':'))) continue;
655 *c_provid++='\0';
656 if (!(c_key=strchr(c_provid, ':'))) continue;
657 *c_key++='\0';
658 if (word_atob(trim(c_caid))!=caid) continue;
659 if ((i=(strlen(trim(c_key))>>1))>256) continue;
660 if (cs_atob((uchar *)key, c_key, i)<0)
661 {
662 cs_log("wrong key in \"%s\"", cs_cert);
663 continue;
664 }
665 rc=1;
666 }
667 fclose(fp);
668 }
669#ifdef OSCAM_INBUILD_KEYS
670 for(i=0; (!rc) && (npkey[i].keylen); i++)
671 if (rc=((caid==npkey[i].caid) && (npkey[i].provid==0)))
672 memcpy(key, npkey[i].key, npkey[i].keylen);
673#endif
674 return(rc);
675}
676
677int init_config()
678{
679 int tag=TAG_GLOBAL;
680 FILE *fp;
681 char *value;
682
683#ifndef CS_EMBEDDED
684#ifdef PRIO_PROCESS
685 errno=0;
686 if ((cfg->nice=getpriority(PRIO_PROCESS, 0))==(-1))
687 if (errno)
688#endif
689#endif
690 cfg->nice=99;
691 cfg->ctimeout=CS_CLIENT_TIMEOUT;
692 cfg->ftimeout=CS_CLIENT_TIMEOUT / 2;
693 cfg->cmaxidle=CS_CLIENT_MAXIDLE;
694 cfg->delay=CS_DELAY;
695 cfg->bindwait=CS_BIND_TIMEOUT;
696 cfg->resolvedelay=CS_RESOLVE_DELAY;
697 cfg->mon_level=2;
698 cfg->mon_hideclient_to=0;
699 cfg->srtimeout=1500;
700 cfg->ulparent=0;
701 strcpy(cfg->http_user, "");
702 strcpy(cfg->http_pwd, "");
703 strcpy(cfg->http_css, "");
704 cfg->http_refresh=0;
705 cfg->http_hide_idle_clients=0;
706 strcpy(cfg->http_tpl, "");
707#ifdef CS_ANTICASC
708 cfg->ac_enabled=0;
709 cfg->ac_users=0;
710 cfg->ac_stime=2;
711 cfg->ac_samples=10;
712 cfg->ac_denysamples=8;
713 cfg->ac_fakedelay=1000;
714 strcpy(cfg->ac_logfile, "./oscam_ac.log");
715#endif
716 sprintf(token, "%s%s", cs_confdir, cs_conf);
717 if (!(fp=fopen(token, "r")))
718 {
719 fprintf(stderr, "Cannot open config file '%s' (errno=%d)\n", token, errno);
720 exit(1);
721 }
722 while (fgets(token, sizeof(token), fp))
723 {
724 int i, l;
725 //void *ptr;
726 if ((l=strlen(trim(token)))<3) continue;
727 if ((token[0]=='[') && (token[l-1]==']'))
728 {
729 for (token[l-1]=0, tag=-1, i=TAG_GLOBAL; cctag[i]; i++)
730 if (!strcmp(cctag[i], strtolower(token+1)))
731 tag=i;
732 continue;
733 }
734 if (!(value=strchr(token, '='))) continue;
735 *value++='\0';
736 chk_token(trim(strtolower(token)), trim(value), tag);
737 }
738 fclose(fp);
739 cs_init_log(logfile);
740 if (cfg->ftimeout>=cfg->ctimeout)
741 {
742 cfg->ftimeout = cfg->ctimeout - 100;
743 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be smaller than clienttimeout (%lu ms))", cfg->ftimeout, cfg->ctimeout);
744 }
745 if(cfg->ftimeout < cfg->srtimeout)
746 {
747 cfg->ftimeout = cfg->srtimeout + 100;
748 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ftimeout, cfg->srtimeout);
749 }
750 if(cfg->ctimeout < cfg->srtimeout)
751 {
752 cfg->ctimeout = cfg->srtimeout + 100;
753 cs_log("WARNING: clienttimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ctimeout, cfg->srtimeout);
754 }
755#ifdef CS_ANTICASC
756 if( cfg->ac_denysamples+1>cfg->ac_samples )
757 {
758 cfg->ac_denysamples=cfg->ac_samples-1;
759 cs_log("WARNING: DenySamples adjusted to %d", cfg->ac_denysamples);
760 }
761#endif
762 return 0;
763}
764
765void chk_account(char *token, char *value, struct s_auth *account)
766{
767 int i;
768 char *ptr1;//, *ptr2;
769
770 if (!strcmp(token, "user")) { strncpy(account->usr, value, sizeof(account->usr)-1); return; }
771 if (!strcmp(token, "pwd")) { strncpy(account->pwd, value, sizeof(account->pwd)-1); return; }
772 if (!strcmp(token, "hostname")) { strncpy((char *)account->dyndns, value, sizeof(account->dyndns)-1);return; }
773 if (!strcmp(token, "betatunnel")) { chk_tuntab(value, &account->ttab); return; }
774 if (!strcmp(token, "uniq")) { account->uniq=atoi(value); return; }
775 if (!strcmp(token, "sleep")) { account->tosleep=atoi(value); return; }
776 if (!strcmp(token, "monlevel")) { account->monlvl=atoi(value); return; }
777 if (!strcmp(token, "caid")) { chk_caidtab(value, &account->ctab); return; }
778 /*
779 * case insensitive
780 */
781 strtolower(value);
782 if (!strcmp(token, "au"))
783 {
784 //set default values for usage during runtime from Webif
785 account->au=-1;
786 account->autoau=0;
787
788 if(value && value[0]=='1') account->autoau=1;
789 for (i=0; i<CS_MAXREADER; i++)
790 if ((reader[i].label[0]) &&
791 (!strncmp(reader[i].label, value, strlen(reader[i].label))))
792 account->au=i;
793 return;
794 }
795 if (!strcmp(token, "group")) {
796 account->grp = 0;
797 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
798 {
799 int g;
800 g=atoi(ptr1);
801 if ((g>0) && (g<33)) account->grp|=(1<<(g-1));
802 }
803 return;
804 }
805 if(!strcmp(token, "services")) { chk_services(value, &account->sidtabok, &account->sidtabno); return; }
806 if(!strcmp(token, "ident")) { chk_ftab(value, &account->ftab, "user", account->usr, "provid"); return; }
807 if(!strcmp(token, "class")) { chk_cltab(value, &account->cltab); return; }
808 if(!strcmp(token, "chid")) { chk_ftab(value, &account->fchid, "user", account->usr, "chid"); return; }
809
810 if (!strcmp(token, "expdate"))
811 {
812 if (!value[0]) {
813 account->expirationdate=(time_t)NULL;
814 return;
815 }
816 struct tm cstime;
817 memset(&cstime,0,sizeof(cstime));
818 for (i=0, ptr1=strtok(value, "-/"); (i<3)&&(ptr1); ptr1=strtok(NULL, "-/"), i++)
819 {
820 switch(i)
821 {
822 case 0: cstime.tm_year=atoi(ptr1)-1900; break;
823 case 1: cstime.tm_mon =atoi(ptr1)-1; break;
824 case 2: cstime.tm_mday=atoi(ptr1); break;
825 }
826 }
827 account->expirationdate=mktime(&cstime);
828 return;
829 }
830
831#ifdef CS_ANTICASC
832 if( !strcmp(token, "numusers") )
833 {
834 account->ac_users = atoi(value);
835 return;
836 }
837 if( !strcmp(token, "penalty") )
838 {
839 account->ac_penalty = atoi(value);
840 return;
841 }
842#endif
843 if (token[0] != '#')
844 fprintf(stderr, "Warning: keyword '%s' in account section not recognized\n",token);
845
846// if (!strcmp(token, "caid"))
847// {
848// for (i=0, ptr1=strtok(value, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
849// {
850// ulong caid, mask;
851// if (ptr2=strchr(trim(ptr1), '&'))
852// *ptr2++='\0';
853// else
854// ptr2="";
855// if (((caid=a2i(ptr1, 2))|(mask=a2i(ptr2,-2))) < 0x10000)
856// {
857// account->caidtab[i][0]=caid;
858// account->caidtab[i++][1]=mask;
859// }
860// else
861// cs_log("WARNING: wrong CAID in %s -> ignored", cs_user);
862// }
863// }
864}
865
866int write_services()
867{
868 int i;
869 FILE *f;
870 struct s_sidtab *sidtab = cfg->sidtab;
871 char tmpfile[256];
872 char destfile[256];
873 char bakfile[256];
874
875 snprintf(destfile, 255,"%s%s", cs_confdir, cs_sidt);
876 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_sidt);
877 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_sidt);
878
879 if (!(f=fopen(tmpfile, "w"))){
880 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
881 return(1);
882 }
883 fprintf(f,"#oscam.services generated automatically\n\n");
884
885 while(sidtab != NULL){
886 fprintf(f,"[%s]\n", sidtab->label);
887 fprintf_conf(f, CONFVARWIDTH, "caid", "");
888 for (i=0; i<sidtab->num_caid; i++){
889 if (i==0) fprintf(f,"%04X", sidtab->caid[i]);
890 else fprintf(f,",%04X", sidtab->caid[i]);
891 }
892 fputc((int)'\n', f);
893 fprintf_conf(f, CONFVARWIDTH, "provid", "");
894 for (i=0; i<sidtab->num_provid; i++){
895 if (i==0) fprintf(f,"%08lX", sidtab->provid[i]);
896 else fprintf(f,",%08lX", sidtab->provid[i]);
897 }
898 fputc((int)'\n', f);
899 fprintf_conf(f, CONFVARWIDTH, "srvid", "");
900 for (i=0; i<sidtab->num_srvid; i++){
901 if (i==0) fprintf(f,"%04X", sidtab->srvid[i]);
902 else fprintf(f,",%04X", sidtab->srvid[i]);
903 }
904 fprintf(f,"\n\n");
905 sidtab=sidtab->next;
906 }
907
908 fclose(f);
909 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
910}
911
912int write_config()
913{
914 int i,j;
915 FILE *f;
916 char *dot = "", *dot1 = "", *dot2 = ""; //flags for delimiters
917 char tmpfile[256];
918 char destfile[256];
919 char bakfile[256];
920
921 snprintf(destfile, 255,"%s%s", cs_confdir, cs_conf);
922 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_conf);
923 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_conf);
924
925 if (!(f=fopen(tmpfile, "w"))){
926 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
927 return(1);
928 }
929 fprintf(f,"#oscam.config generated automatically\n\n");
930
931 /*global settings*/
932 fprintf(f,"[global]\n");
933 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->srvip));
934 fprintf_conf(f, CONFVARWIDTH, "logfile", "%s\n", logfile);
935 fprintf_conf(f, CONFVARWIDTH, "pidfile", "%s\n", cfg->pidfile);
936 fprintf_conf(f, CONFVARWIDTH, "usrfile", "%s\n", cfg->usrfile);
937 fprintf_conf(f, CONFVARWIDTH, "cwlogdir", "%s\n", cfg->cwlogdir);
938 fprintf_conf(f, CONFVARWIDTH, "clienttimeout", "%ld\n", cfg->ctimeout/1000);
939 fprintf_conf(f, CONFVARWIDTH, "fallbacktimeout", "%ld\n", cfg->ftimeout/1000);
940 fprintf_conf(f, CONFVARWIDTH, "clientmaxidle", "%d\n", cfg->cmaxidle);
941 fprintf_conf(f, CONFVARWIDTH, "cachedelay", "%ld\n", cfg->delay/1000);
942 fprintf_conf(f, CONFVARWIDTH, "bindwait", "%d\n", cfg->bindwait);
943 fprintf_conf(f, CONFVARWIDTH, "netprio", "%ld\n", cfg->netprio);
944 fprintf_conf(f, CONFVARWIDTH, "resolvedelay", "%d\n", cfg->resolvedelay);
945 fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", cfg->tosleep);
946 fprintf_conf(f, CONFVARWIDTH, "unlockparental", "%d\n", cfg->ulparent);
947 fprintf_conf(f, CONFVARWIDTH, "nice", "%d\n", cfg->nice);
948 fprintf_conf(f, CONFVARWIDTH, "serialreadertimeout", "%d\n", cfg->srtimeout);
949 fprintf_conf(f, CONFVARWIDTH, "maxlogsize", "%d\n", cfg->max_log_size);
950 fprintf_conf(f, CONFVARWIDTH, "showecmdw", "%d\n", cfg->show_ecm_dw);
951 fprintf_conf(f, CONFVARWIDTH, "waitforcards", "%d\n", cfg->waitforcards);
952 fprintf_conf(f, CONFVARWIDTH, "preferlocalcards", "%d\n", cfg->preferlocalcards);
953 fputc((int)'\n', f);
954
955 /*monitor settings*/
956 fprintf(f,"[monitor]\n");
957 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->mon_port);
958 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
959
960 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
961 struct s_ip *cip;
962 for (cip = cfg->mon_allowed; cip; cip = cip->next){
963 fprintf(f,"%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
964 if (cip->ip[0] == cip->ip[1]) fprintf(f,"-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
965 dot=",";
966 }
967 fputc((int)'\n', f);
968 fprintf_conf(f, CONFVARWIDTH, "aulow", "%d\n", cfg->mon_aulow);
969 fprintf_conf(f, CONFVARWIDTH, "hideclient_to", "%d\n", cfg->mon_hideclient_to);
970 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", cfg->mon_level);
971 fprintf_conf(f, CONFVARWIDTH, "httpport", "%d\n", cfg->http_port);
972 fprintf_conf(f, CONFVARWIDTH, "httpuser", "%s\n", cfg->http_user);
973 fprintf_conf(f, CONFVARWIDTH, "httppwd", "%s\n", cfg->http_pwd);
974 fprintf_conf(f, CONFVARWIDTH, "httpcss", "%s\n", cfg->http_css);
975 fprintf_conf(f, CONFVARWIDTH, "httprefresh", "%d\n", cfg->http_refresh);
976 fprintf_conf(f, CONFVARWIDTH, "httphideidleclients", "%d\n", cfg->http_hide_idle_clients);
977 fputc((int)'\n', f);
978
979 /*newcamd*/
980 if ((cfg->ncd_ptab.nports > 0) && (cfg->ncd_ptab.ports[0].s_port > 0)){
981 fprintf(f,"[newcamd]\n");
982 fprintf_conf(f, CONFVARWIDTH, "port", "");
983 dot1 = "";
984 for(i = 0; i < cfg->ncd_ptab.nports; ++i){
985 fprintf(f,"%s%d@%04X", dot1, cfg->ncd_ptab.ports[i].s_port, cfg->ncd_ptab.ports[i].ftab.filts[0].caid);
986 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0){
987 fprintf(f,":");
988 dot2 = "";
989 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j){
990 fprintf(f,"%s%lX", dot2, cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
991 dot2 = ",";
992 }
993 }
994 dot1=";";
995 }
996
997 fputc((int)'\n', f);
998 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
999 fprintf_conf(f, CONFVARWIDTH, "key", "");
1000 for (i=0;i<14;i++) fprintf(f,"%02X", cfg->ncd_key[i]);
1001 fprintf(f,"\n\n");
1002 }
1003
1004 /*camd3.3*/
1005 if ( cfg->c33_port > 0) {
1006 fprintf(f,"[camd33]\n");
1007 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c33_port);
1008 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
1009 fprintf_conf(f, CONFVARWIDTH, "passive", "%d\n", cfg->c33_passive);
1010 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);
1011 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
1012 dot="";
1013 for (cip = cfg->c33_plain; cip; cip = cip->next){
1014 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1015 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1016 dot=",";
1017 }
1018 fprintf(f,"\n\n");
1019 }
1020
1021 /*camd3.5*/
1022 if ( cfg->c35_port > 0) {
1023 fprintf(f,"[cs357x]\n");
1024 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c35_port);
1025 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1026 fprintf(f,"\n\n");
1027 }
1028
1029 /*camd3.5 TCP*/
1030 if ((cfg->c35_tcp_ptab.nports > 0) && (cfg->c35_tcp_ptab.ports[0].s_port > 0)) {
1031 fprintf(f,"[cs378x]\n");
1032 fprintf_conf(f, CONFVARWIDTH, "port", "");
1033 dot1 = "";
1034 for(i = 0; i < cfg->c35_tcp_ptab.nports; ++i){
1035 fprintf(f,"%s%d@%04X", dot1, cfg->c35_tcp_ptab.ports[i].s_port, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].caid);
1036 if (cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids > 0){
1037 fprintf(f,":");
1038 dot2 = "";
1039 for (j = 0; j < cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids; ++j){
1040 fprintf(f,"%s%lX", dot2, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].prids[j]);
1041 dot2 = ",";
1042 }
1043 }
1044 dot1=";";
1045 }
1046
1047 fputc((int)'\n', f);
1048 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1049 fputc((int)'\n', f);
1050 }
1051
1052 /*Radegast*/
1053 if ( cfg->rad_port > 0) {
1054 fprintf(f,"[radegast]\n");
1055 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->rad_port);
1056 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
1057 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->rad_usr);
1058 fprintf_conf(f, CONFVARWIDTH, "allowed", "");
1059 struct s_ip *cip;
1060 for (cip = cfg->rad_allowed; cip; cip = cip->next){
1061 fprintf(f,"%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
1062 if (cip->ip[0] == cip->ip[1]) fprintf(f,"-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
1063 dot=",";
1064 }
1065 fprintf(f,"\n\n");
1066 }
1067
1068#ifdef CS_WITH_GBOX
1069 /*Gbox*/
1070 if ((cfg->gbox_pwd[0] > 0) || (cfg->gbox_pwd[1] > 0) || (cfg->gbox_pwd[2] > 0) || (cfg->gbox_pwd[3] > 0)){
1071 fprintf(f,"[gbox]\n");
1072 fprintf_conf(f, CONFVARWIDTH, "password", ""); for (i=0;i<4;i++) fprintf(f,"%02X", cfg->gbox_pwd[i]); fputc((int)'\n', f);;
1073 fprintf_conf(f, CONFVARWIDTH, "maxdist", "%d\n", cfg->maxdist);
1074 fprintf_conf(f, CONFVARWIDTH, "ignorelist", "%s\n", cfg->ignorefile);
1075 fprintf_conf(f, CONFVARWIDTH, "onlineinfos", "%s\n", cfg->gbxShareOnl);
1076 fprintf_conf(f, CONFVARWIDTH, "cardinfos", "%s\n", cfg->cardfile);
1077 fprintf_conf(f, CONFVARWIDTH, "locals", "");
1078 char *dot = "";
1079 for (i = 0; i < cfg->num_locals; i++){
1080 fprintf(f,"%s%06lX", dot, cfg->locals[i]);
1081 dot=";";
1082 }
1083 fprintf(f,"\n\n");
1084 }
1085#endif
1086
1087 /*serial*/
1088
1089
1090 /*cccam placeholder*/
1091
1092#ifdef HAVE_DVBAPI
1093 /*dvb-api*/
1094 if (cfg->dvbapi_enabled > 0) {
1095 fprintf(f,"[dvbapi]\n");
1096 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->dvbapi_enabled);
1097 fprintf_conf(f, CONFVARWIDTH, "au", "%d\n", cfg->dvbapi_au);
1098 fprintf_conf(f, CONFVARWIDTH, "boxtype", "%s\n", cfg->dvbapi_boxtype);
1099 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->dvbapi_usr);
1100 fputc((int)'\n', f);
1101 }
1102#endif
1103
1104#ifdef CS_ANTICASC
1105 if (cfg->ac_enabled > 0){
1106 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->ac_enabled);
1107 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", cfg->ac_users);
1108 fprintf_conf(f, CONFVARWIDTH, "sampletime", "%d\n", cfg->ac_stime);
1109 fprintf_conf(f, CONFVARWIDTH, "samples", "%d\n", cfg->ac_samples);
1110 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", cfg->ac_penalty);
1111 fprintf_conf(f, CONFVARWIDTH, "aclogfile", "%s\n", cfg->ac_logfile);
1112 fprintf_conf(f, CONFVARWIDTH, "denysamples", "%d\n", cfg->ac_denysamples);
1113 fprintf_conf(f, CONFVARWIDTH, "fakedelay", "%d\n", cfg->ac_fakedelay);
1114 fputc((int)'\n', f);
1115 }
1116#endif
1117
1118 fclose(f);
1119
1120 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1121}
1122
1123int write_userdb()
1124{
1125 int i;
1126 FILE *f;
1127 struct s_auth *account;
1128 char *dot = ""; //flag for comma
1129 char tmpfile[256];
1130 char destfile[256];
1131 char bakfile[256];
1132
1133 snprintf(destfile, 255,"%s%s", cs_confdir, cs_user);
1134 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_user);
1135 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_user);
1136
1137 if (!(f=fopen(tmpfile, "w"))){
1138 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
1139 return(1);
1140 }
1141 fprintf(f,"#oscam.user generated automatically\n\n");
1142
1143 //each account
1144 for (account=cfg->account; (account) ; account=account->next){
1145 fprintf(f,"[account]\n");
1146 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", account->usr);
1147 fprintf_conf(f, CONFVARWIDTH, "pwd", "%s\n", account->pwd);
1148 struct tm * timeinfo = localtime (&account->expirationdate);
1149 char buf [80];
1150 strftime (buf,80,"%Y-%m-%d",timeinfo);
1151 if(strcmp(buf,"1970-01-01"))
1152 fprintf_conf(f, CONFVARWIDTH, "expdate", "%s\n", buf);
1153 else
1154 fprintf_conf(f, CONFVARWIDTH, "expdate", "\n");
1155
1156 //group
1157 char *value = mk_t_group((ulong*)account->grp);
1158 fprintf_conf(f, CONFVARWIDTH, "group", "%s\n", value);
1159 free(value);
1160
1161 fprintf_conf(f, CONFVARWIDTH, "hostname", "%s\n", account->dyndns);
1162 fprintf_conf(f, CONFVARWIDTH, "uniq", "%d\n", account->uniq);
1163 fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", account->tosleep);
1164 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", account->monlvl);
1165
1166 if (account->au > -1)
1167 if (account->au < CS_MAXREADER)
1168 fprintf_conf(f, CONFVARWIDTH, "au", "%s\n", reader[account->au].label);
1169 if (account->autoau == 1) fprintf_conf(f, CONFVARWIDTH, "au", "1\n");
1170
1171 fprintf_conf(f, CONFVARWIDTH, "services", "");
1172 char sidok[33]; long2bitchar(account->sidtabok,sidok);
1173 char sidno[33]; long2bitchar(account->sidtabno,sidno);
1174 struct s_sidtab *sidtab = cfg->sidtab;
1175 i=0; dot = "";
1176 for (; sidtab; sidtab=sidtab->next){
1177 if(sidok[i]=='1') {fprintf(f,"%s%s", dot, sidtab->label); dot = ",";}
1178 if(sidno[i]=='1') {fprintf(f,"%s!%s", dot, sidtab->label); dot = ",";}
1179 i++;
1180 }
1181 fputc((int)'\n', f);
1182
1183 //CAID
1184 value = mk_t_caidtab(&account->ctab);
1185 fprintf_conf(f, CONFVARWIDTH, "caid", "%s\n", value);
1186 free(value);
1187
1188 //betatunnel
1189 value = mk_t_tuntab(&account->ttab);
1190 fprintf_conf(f, CONFVARWIDTH, "betatunnel", "%s\n", value);
1191 free(value);
1192
1193 //ident
1194 value = mk_t_ftab(&account->ftab);
1195 fprintf_conf(f, CONFVARWIDTH, "ident", "%s\n", value);
1196 free(value);
1197
1198#ifdef CS_ANTICASC
1199 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", account->ac_users);
1200 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", account->ac_penalty);
1201#endif
1202 fputc((int)'\n', f);
1203 }
1204 fclose(f);
1205
1206 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1207}
1208
1209int init_userdb()
1210{
1211 int tag=0, nr, nro, expired;
1212 //int first=1;
1213 FILE *fp;
1214 char *value;
1215 struct s_auth *ptr;
1216 /*static */struct s_auth *account=(struct s_auth *)0;
1217
1218 sprintf(token, "%s%s", cs_confdir, cs_user);
1219 if (!(fp=fopen(token, "r")))
1220 {
1221 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1222 return(1);
1223 }
1224 for (nro=0, ptr=cfg->account; ptr; nro++)
1225 {
1226 struct s_auth *ptr_next;
1227 ptr_next=ptr->next;
1228 free(ptr);
1229 ptr=ptr_next;
1230 }
1231 nr=0;
1232 while (fgets(token, sizeof(token), fp))
1233 {
1234 int i, l;
1235 void *ptr;
1236 if ((l=strlen(trim(token)))<3) continue;
1237 if ((token[0]=='[') && (token[l-1]==']'))
1238 {
1239 token[l-1]=0;
1240 tag=(!strcmp("account", strtolower(token+1)));
1241 if (!(ptr=malloc(sizeof(struct s_auth))))
1242 {
1243 cs_log("Error allocating memory (errno=%d)", errno);
1244 return(1);
1245 }
1246 if (account)
1247 account->next=ptr;
1248 else
1249 cfg->account=ptr;
1250 account=ptr;
1251 memset(account, 0, sizeof(struct s_auth));
1252 account->au=(-1);
1253 account->monlvl=cfg->mon_level;
1254 account->tosleep=cfg->tosleep;
1255 for (i=1; i<CS_MAXCAIDTAB; account->ctab.mask[i++]=0xffff);
1256 for (i=1; i<CS_MAXTUNTAB; account->ttab.bt_srvid[i++]=0x0000);
1257 nr++;
1258#ifdef CS_ANTICASC
1259 account->ac_users=cfg->ac_users;
1260 account->ac_penalty=cfg->ac_penalty;
1261 account->ac_idx = nr;
1262#endif
1263 continue;
1264 }
1265 if (!tag) continue;
1266 if (!(value=strchr(token, '='))) continue;
1267 *value++='\0';
1268 chk_account(trim(strtolower(token)), trim(value), account);
1269 }
1270 fclose(fp);
1271
1272 for (expired=0, ptr=cfg->account; ptr;)
1273 {
1274 if(ptr->expirationdate && ptr->expirationdate<time(NULL)) expired++;
1275 ptr=ptr->next;
1276 }
1277
1278 cs_log("userdb reloaded: %d accounts freed, %d accounts loaded, %d expired", nro, nr, expired);
1279 return(0);
1280}
1281
1282static void chk_entry4sidtab(char *value, struct s_sidtab *sidtab, int what)
1283{
1284 int i, b;
1285 char *ptr;
1286 ushort *slist=(ushort *) 0;
1287 ulong *llist=(ulong *) 0;
1288 ulong caid;
1289 char buf[strlen(value) + 1];
1290 strncpy(buf, value, sizeof(buf));
1291 b=(what==1) ? sizeof(ulong) : sizeof(ushort);
1292 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1293 {
1294 caid=a2i(ptr, b);
1295 if (!errno) i++;
1296 }
1297 //if (!i) return(0);
1298 if (b==sizeof(ushort))
1299 slist=malloc(i*sizeof(ushort));
1300 else
1301 llist=malloc(i*sizeof(ulong));
1302 strcpy(value, buf);
1303 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1304 {
1305 caid=a2i(ptr, b);
1306 if (errno) continue;
1307 if (b==sizeof(ushort))
1308 slist[i++]=(ushort) caid;
1309 else
1310 llist[i++]=caid;
1311 }
1312 switch (what)
1313 {
1314 case 0: sidtab->caid=slist;
1315 sidtab->num_caid=i;
1316 break;
1317 case 1: sidtab->provid=llist;
1318 sidtab->num_provid=i;
1319 break;
1320 case 2: sidtab->srvid=slist;
1321 sidtab->num_srvid=i;
1322 break;
1323 }
1324}
1325
1326void chk_sidtab(char *token, char *value, struct s_sidtab *sidtab)
1327{
1328 if (!strcmp(token, "caid")) { chk_entry4sidtab(value, sidtab, 0); return; }
1329 if (!strcmp(token, "provid")) { chk_entry4sidtab(value, sidtab, 1); return; }
1330 if (!strcmp(token, "ident")) { chk_entry4sidtab(value, sidtab, 1); return; }
1331 if (!strcmp(token, "srvid")) { chk_entry4sidtab(value, sidtab, 2); return; }
1332 if (token[0] != '#')
1333 fprintf(stderr, "Warning: keyword '%s' in sidtab section not recognized\n",token);
1334}
1335
1336int init_sidtab()
1337{
1338 int nr, nro;
1339 FILE *fp;
1340 char *value;
1341 struct s_sidtab *ptr;
1342 struct s_sidtab *sidtab=(struct s_sidtab *)0;
1343
1344 sprintf(token, "%s%s", cs_confdir, cs_sidt);
1345 if (!(fp=fopen(token, "r")))
1346 {
1347 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1348 return(1);
1349 }
1350 for (nro=0, ptr=cfg->sidtab; ptr; nro++)
1351 {
1352 struct s_sidtab *ptr_next;
1353 ptr_next=ptr->next;
1354 if (ptr->caid) free(ptr->caid);
1355 if (ptr->provid) free(ptr->provid);
1356 if (ptr->srvid) free(ptr->srvid);
1357 free(ptr);
1358 ptr=ptr_next;
1359 }
1360 nr=0;
1361 while (fgets(token, sizeof(token), fp))
1362 {
1363 int l;
1364 void *ptr;
1365 if ((l=strlen(trim(token)))<3) continue;
1366 if ((token[0]=='[') && (token[l-1]==']'))
1367 {
1368 token[l-1]=0;
1369 if (!(ptr=malloc(sizeof(struct s_sidtab))))
1370 {
1371 cs_log("Error allocating memory (errno=%d)", errno);
1372 return(1);
1373 }
1374 if (sidtab)
1375 sidtab->next=ptr;
1376 else
1377 cfg->sidtab=ptr;
1378 sidtab=ptr;
1379 nr++;
1380 memset(sidtab, 0, sizeof(struct s_sidtab));
1381 strncpy(sidtab->label, strtolower(token+1), sizeof(sidtab->label));
1382 continue;
1383 }
1384 if (!sidtab) continue;
1385 if (!(value=strchr(token, '='))) continue;
1386 *value++='\0';
1387 chk_sidtab(trim(strtolower(token)), trim(strtolower(value)), sidtab);
1388 }
1389 fclose(fp);
1390
1391#ifdef DEBUG_SIDTAB
1392 show_sidtab(cfg->sidtab);
1393#endif
1394 cs_log("services reloaded: %d services freed, %d services loaded", nro, nr);
1395 return(0);
1396}
1397
1398int init_srvid()
1399{
1400 int nr;
1401 FILE *fp;
1402 char *payload;
1403 static struct s_srvid *srvid=(struct s_srvid *)0;
1404 sprintf(token, "%s%s", cs_confdir, cs_srid);
1405
1406 if (!(fp=fopen(token, "r"))) {
1407 cs_log("can't open file \"%s\" (err=%d), no service-id's loaded", token, errno);
1408 return(0);
1409 }
1410
1411 nr=0;
1412 while (fgets(token, sizeof(token), fp)) {
1413
1414 int l;
1415 void *ptr;
1416 char *tmp;
1417 tmp = trim(token);
1418
1419 if (tmp[0] == '#') continue;
1420 if ((l=strlen(tmp)) < 6) continue;
1421 if (!(payload=strchr(token, '|'))) continue;
1422 *payload++ = '\0';
1423
1424 if (!(ptr = malloc(sizeof(struct s_srvid)))) {
1425 cs_log("Error allocating memory (errno=%d)", errno);
1426 return(1);
1427 }
1428
1429 if (srvid)
1430 srvid->next = ptr;
1431 else
1432 cfg->srvid = ptr;
1433
1434 srvid = ptr;
1435 memset(srvid, 0, sizeof(struct s_srvid));
1436
1437 int i;
1438 char *ptr1;
1439 for (i = 0, ptr1 = strtok(payload, "|"); ptr1; ptr1 = strtok(NULL, "|"), i++){
1440 switch(i){
1441 case 0:
1442 strncpy(srvid->prov, trim(ptr1), sizeof(srvid->prov)-1);
1443 break;
1444 case 1:
1445 strncpy(srvid->name, trim(ptr1), sizeof(srvid->name)-1);
1446 break;
1447 case 2:
1448 strncpy(srvid->type, trim(ptr1), sizeof(srvid->type)-1);
1449 break;
1450 case 3:
1451 strncpy(srvid->desc, trim(ptr1), sizeof(srvid->desc)-1);
1452 break;
1453 }
1454 }
1455
1456 char *srvidasc = strchr(token, ':');
1457 *srvidasc++ = '\0';
1458 srvid->srvid = word_atob(srvidasc);
1459
1460 srvid->ncaid = 0;
1461 for (i = 0, ptr1 = strtok(token, ","); (ptr1) && (i < 10) ; ptr1 = strtok(NULL, ","), i++){
1462 srvid->caid[i] = word_atob(ptr1);
1463 srvid->ncaid = i+1;
1464 }
1465 nr++;
1466 }
1467
1468 fclose(fp);
1469 if (nr>0)
1470 cs_log("%d service-id's loaded", nr);
1471 else{
1472 cs_log("oscam.srvid loading failed, old format");
1473 }
1474 return(0);
1475}
1476
1477static void chk_reader(char *token, char *value, struct s_reader *rdr)
1478{
1479 int i;
1480 char *ptr;
1481 /*
1482 * case sensitive first
1483 */
1484 if (!strcmp(token, "device"))
1485 {
1486 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1487 {
1488 trim(ptr);
1489 switch(i)
1490 {
1491 case 0: strncpy(rdr->device, ptr, sizeof(rdr->device)-1); break;
1492 case 1: rdr->r_port=atoi(ptr); break;
1493 case 2: rdr->l_port=atoi(ptr); break;
1494 }
1495 }
1496 return;
1497 }
1498 if (!strcmp(token, "key"))
1499 {
1500 if (key_atob14(value, rdr->ncd_key))
1501 {
1502 fprintf(stderr, "Configuration newcamd: Error in Key\n");
1503 exit(1);
1504 }
1505 return;
1506 }
1507#ifdef CS_WITH_GBOX
1508 if (!strcmp(token, "password")) { strncpy((char *)rdr->gbox_pwd, (const char *)i2b(4, a2i(value, 4)), 4); return; }
1509 if (!strcmp(token, "premium")) { rdr->gbox_prem=1; return; }
1510#endif
1511 if (!strcmp(token, "account"))
1512 {
1513 for (i=0, ptr=strtok(value, ","); (i<2)&&(ptr); ptr=strtok(NULL, ","), i++)
1514 {
1515 trim(ptr);
1516 switch(i)
1517 {
1518 case 0: strncpy(rdr->r_usr, ptr, sizeof(rdr->r_usr)-1); break;
1519 case 1: strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd)-1); break;
1520 }
1521 }
1522 return;
1523 }
1524 if( !strcmp(token, "pincode")) { strncpy(rdr->pincode, value, sizeof(rdr->pincode)-1); return; }
1525 if (!strcmp(token, "readnano")) { strncpy((char *)rdr->emmfile, value, sizeof(rdr->emmfile)-1); return; }
1526 /*
1527 * case insensitive
1528 */
1529 strtolower(value);
1530
1531 if (!strcmp(token, "services")) { chk_services(value, &rdr->sidtabok, &rdr->sidtabno); return; }
1532 if (!strcmp(token, "inactivitytimeout")) { rdr->tcp_ito = atoi(value); return; }
1533 if (!strcmp(token, "reconnecttimeout")) { rdr->tcp_rto = atoi(value); return; }
1534 if (!strcmp(token, "disableserverfilter")) { rdr->ncd_disable_server_filt = atoi(value); return; }
1535
1536 if (!strcmp(token, "label")) { strncpy(rdr->label, value, sizeof(rdr->label)-1); return; }
1537 if (!strcmp(token, "fallback")) { rdr->fallback=atoi(value) ? 1 : 0; return; }
1538 if (!strcmp(token, "logport")) { rdr->log_port=atoi(value); return; }
1539 if (!strcmp(token, "caid")) { chk_caidtab(value, &rdr->ctab); return; }
1540 if (!strcmp(token, "boxid")) { rdr->boxid=a2i(value,4); return; }
1541 if (!strcmp(token, "aeskey"))
1542 {
1543 if (key_atob(value, rdr->aes_key))
1544 {
1545 fprintf(stderr, "Configuration reader: Error in AES Key\n");
1546 exit(1);
1547 }
1548 return;
1549 }
1550 if ((!strcmp(token, "n3_rsakey")) || (!strcmp(token, "rsakey")) )
1551 {
1552 rdr->has_rsa=1;
1553 if (key_atob_l(value, rdr->rsa_mod, 128))
1554 {
1555 fprintf(stderr, "Configuration reader: Error in rsakey\n");
1556 exit(1);
1557 }
1558 return;
1559 }
1560 if (!strcmp(token, "tiger_rsakey"))
1561 {
1562 if (key_atob_l(value, rdr->rsa_mod, 240))
1563 {
1564 fprintf(stderr, "Configuration reader: Error in tiger_rsakey\n");
1565 exit(1);
1566 }
1567 return;
1568 }
1569 if ((!strcmp(token, "n3_boxkey")) || (!strcmp(token, "boxkey")))
1570 {
1571 if (key_atob_l(value, rdr->nagra_boxkey, 16))
1572 {
1573 fprintf(stderr, "Configuration reader: Error in boxkey\n");
1574 exit(1);
1575 }
1576 return;
1577 }
1578 if (!strcmp(token, "detect"))
1579 {
1580 for (i=0; RDR_CD_TXT[i]; i++)
1581 {
1582 if (!strcmp(value, RDR_CD_TXT[i]))
1583 rdr->detect=i;
1584 else
1585 if ((value[0]=='!') && (!strcmp(value+1, RDR_CD_TXT[i])))
1586 rdr->detect=i|0x80;
1587 }
1588 return;
1589 }
1590 if (!strcmp(token, "mhz")) { rdr->mhz=atoi(value); return; }
1591 if (!strcmp(token, "cardmhz")) { rdr->cardmhz=atoi(value); return; }
1592 if (!strcmp(token, "protocol"))
1593 {
1594 if (!strcmp(value, "mouse")) { rdr->typ=R_MOUSE; return; }
1595 if (!strcmp(value, "smartreader")) { rdr->typ=R_SMART; return; }
1596 if (!strcmp(value, "internal")) { rdr->typ=R_INTERNAL; return; }
1597#ifdef HAVE_PCSC
1598 if (!strcmp(value, "pcsc")) { rdr->typ=R_PCSC; return; }
1599#endif
1600 if (!strcmp(value, "serial")) { rdr->typ=R_SERIAL; return; }
1601 if (!strcmp(value, "camd35")) { rdr->typ=R_CAMD35; return; }
1602 if (!strcmp(value, "cs378x")) { rdr->typ=R_CS378X; return; }
1603 if (!strcmp(value, "cs357x")) { rdr->typ=R_CAMD35; return; }
1604 if (!strcmp(value, "gbox")) { rdr->typ=R_GBOX; return; }
1605 if (!strcmp(value, "cccam")) {
1606 rdr->typ=R_CCCAM;
1607 // strcpy(value, "1");
1608 // chk_caidtab(value, &rdr->ctab); // this is a MAJOR hack for auto multiple caid support (not currently working due to ncd table issue)
1609 return;
1610 }
1611 if (!strcmp(value, "radegast")) { rdr->typ=R_RADEGAST; return; }
1612 if (!strcmp(value, "newcamd") ||
1613 !strcmp(value, "newcamd525")) {rdr->typ=R_NEWCAMD;
1614 rdr->ncd_proto=NCD_525; return; }
1615 if (!strcmp(value, "newcamd524")) {rdr->typ=R_NEWCAMD;
1616 rdr->ncd_proto=NCD_524; return; }
1617 fprintf(stderr, "WARNING: value '%s' in protocol-line not recognized, assuming MOUSE\n",value);
1618 rdr->typ=R_MOUSE;
1619 return;
1620 }
1621 if (!strcmp(token, "ident")) { chk_ftab(value, &rdr->ftab,"reader",rdr->label,"provid"); return; }
1622 if (!strcmp(token, "class")) { chk_cltab(value, &rdr->cltab); return; }
1623 if (!strcmp(token, "chid")) { chk_ftab(value, &rdr->fchid,"reader",rdr->label,"chid"); return; }
1624 if (!strcmp(token, "showcls")) { rdr->show_cls = atoi(value); return; }
1625 if (!strcmp(token, "maxqlen"))
1626 {
1627 rdr->maxqlen = atoi(value);
1628 if( rdr->maxqlen<0 || rdr->maxqlen>CS_MAXQLEN )
1629 rdr->maxqlen=CS_MAXQLEN;
1630 return;
1631 }
1632 if (!strcmp(token, "group"))
1633 {
1634 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1635 {
1636 int g;
1637 g=atoi(ptr);
1638 if ((g>0) && (g<33)) rdr->grp|=(1<<(g-1));
1639 }
1640 return;
1641 }
1642 if (!strcmp(token, "emmcache"))
1643 {
1644 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1645 switch(i)
1646 {
1647 case 0: rdr->cachemm=atoi(ptr); break;
1648 case 1: rdr->rewritemm=atoi(ptr); break;
1649 case 2: rdr->logemm=atoi(ptr); break;
1650 }
1651 if (rdr->rewritemm <=0) {
1652 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);
1653 rdr->rewritemm = 1;
1654 }
1655 return;
1656 }
1657
1658 if (!strcmp(token, "blocknano"))
1659 {
1660 if (!strcmp(value,"all")) //wildcard is used
1661 for (i=0 ; i<256; i++)
1662 rdr->b_nano[i] |= 0x01; //set all lsb's to block all nanos
1663 else
1664 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1665 if ((i=byte_atob(ptr))>=0)
1666 rdr->b_nano[i]|= 0x01; //lsb is set when to block nano
1667 return;
1668 }
1669 if (!strcmp(token, "savenano"))
1670 {
1671 if (!strcmp(value,"all")) //wildcard is used
1672 for (i=0 ; i<256; i++)
1673 rdr->b_nano[i] |= 0x02; //set all lsb+1 to save all nanos to file
1674 else
1675 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1676 if ((i=byte_atob(ptr))>=0)
1677 rdr->b_nano[i]|= 0x02; //lsb+1 is set when to save nano to file
1678 return;
1679 }
1680 if (!strcmp(token, "cccversion")) { // cccam version
1681 if (strlen(value)>sizeof(rdr->cc_version)-1) {
1682 fprintf(stderr, "cccam config: version too long\n");
1683 exit(1);
1684 }
1685 bzero(rdr->cc_version, sizeof(rdr->cc_version));
1686 strncpy(rdr->cc_version, value, sizeof(rdr->cc_version)-1);
1687 return;
1688 }
1689 if (!strcmp(token, "cccbuild")) { // cccam build number
1690 if (strlen(value)>sizeof(rdr->cc_build)-1) {
1691 fprintf(stderr, "cccam config build number too long\n");
1692 exit(1);
1693 }
1694 bzero(rdr->cc_build, sizeof(rdr->cc_build));
1695 strncpy(rdr->cc_build, value, sizeof(rdr->cc_build)-1);
1696 return;
1697 }
1698 if (!strcmp(token, "cccmaxhop")) { // cccam max card distance
1699 rdr->cc_maxhop = atoi(value);
1700 return;
1701 }
1702 if (token[0] != '#')
1703 fprintf(stderr, "Warning: keyword '%s' in reader section not recognized\n",token);
1704}
1705
1706int init_readerdb()
1707{
1708 int tag=0, nr;
1709 FILE *fp;
1710 char *value;
1711
1712 sprintf(token, "%s%s", cs_confdir, cs_srvr);
1713 if (!(fp=fopen(token, "r")))
1714 {
1715 cs_log("can't open file \"%s\" (errno=%d)\n", token, errno);
1716 return(1);
1717 }
1718 nr=0;
1719 while (fgets(token, sizeof(token), fp))
1720 {
1721 int i, l;
1722 if ((l=strlen(trim(token)))<3) continue;
1723 if ((token[0]=='[') && (token[l-1]==']'))
1724 {
1725 token[l-1]=0;
1726 tag=(!strcmp("reader", strtolower(token+1)));
1727 if (reader[nr].label[0] && reader[nr].typ) nr++;
1728 memset(&reader[nr], 0, sizeof(struct s_reader));
1729 reader[nr].tcp_rto = 30;
1730 reader[nr].show_cls = 10;
1731 reader[nr].maxqlen = CS_MAXQLEN;
1732 reader[nr].mhz = 357;
1733 reader[nr].cardmhz = 357;
1734 strcpy(reader[nr].pincode, "none");
1735 for (i=1; i<CS_MAXCAIDTAB; reader[nr].ctab.mask[i++]=0xffff);
1736 continue;
1737 }
1738 if (!tag) continue;
1739 if (!(value=strchr(token, '='))) continue;
1740 *value++='\0';
1741 chk_reader(trim(strtolower(token)), trim(value), &reader[nr]);
1742 }
1743 fclose(fp);
1744 return(0);
1745}
1746
1747/*
1748int init_irdeto_guess_tab()
1749{
1750 int i, j, skip;
1751 int b47;
1752 FILE *fp;
1753 char token[128], *value, *ptr;
1754 char zSid[5];
1755 uchar b3;
1756 ushort caid, sid;
1757 struct s_irdeto_quess *ird_row, *head;
1758
1759 memset(cfg->itab, 0, sizeof(cfg->itab));
1760 sprintf(token, "%s%s", cs_confdir, cs_ird);
1761 if (!(fp=fopen(token, "r")))
1762 {
1763 cs_log("can't open file \"%s\" (errno=%d) irdeto guessing not loaded",
1764 token, errno);
1765 return(1);
1766 }
1767 while (fgets(token, sizeof(token), fp))
1768 {
1769 if( strlen(token)<20 ) continue;
1770 for( i=b3=b47=caid=sid=skip=0, ptr=strtok(token, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1771 {
1772 trim(ptr);
1773 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1774 skip=1;
1775 break;
1776 }
1777 switch(i)
1778 {
1779 case 0: b3 = a2i(ptr, 2); break;
1780 case 1: b47 = a2i(ptr, 8); break;
1781 case 2: caid = a2i(ptr, 4); break;
1782 case 3:
1783 for( j=0; j<4; j++ )
1784 zSid[j]=ptr[j];
1785 zSid[4]=0;
1786 sid = a2i(zSid, 4);
1787 break;
1788 }
1789 }
1790 if( !skip )
1791 {
1792 if (!(ird_row=(struct s_irdeto_quess*)malloc(sizeof(struct s_irdeto_quess))))
1793 {
1794 cs_log("Error allocating memory (errno=%d)", errno);
1795 return;
1796 }
1797 ird_row->b47 = b47;
1798 ird_row->caid = caid;
1799 ird_row->sid = sid;
1800 ird_row->next = 0;
1801
1802 head = cfg->itab[b3];
1803 if( head ) {
1804 while( head->next )
1805 head=head->next;
1806 head->next=ird_row;
1807 }
1808 else
1809 cfg->itab[b3]=ird_row;
1810
1811 //cs_debug("%02X:%08X:%04X:%04X", b3, b47, caid, sid);
1812 }
1813 }
1814 fclose(fp);
1815
1816 for( i=0; i<0xff; i++ )
1817 {
1818 head=cfg->itab[i];
1819 while(head)
1820 {
1821 cs_debug("itab[%02X]: b47=%08X, caid=%04X, sid=%04X",
1822 i, head->b47, head->caid, head->sid);
1823 head=head->next;
1824 }
1825 }
1826 return(0);
1827}
1828*/
1829
1830#ifdef CS_ANTICASC
1831void init_ac()
1832{
1833 int nr;
1834 FILE *fp;
1835 //char *value;
1836
1837 sprintf(token, "%s%s", cs_confdir, cs_ac);
1838 if (!(fp=fopen(token, "r")))
1839 {
1840 cs_log("can't open file \"%s\" (errno=%d) anti-cascading table not loaded",
1841 token, errno);
1842 return;
1843 }
1844
1845 for(nr=0; fgets(token, sizeof(token), fp);)
1846 {
1847 int i, skip;
1848 ushort caid, sid, chid, dwtime;
1849 ulong provid;
1850 char *ptr, *ptr1;
1851 struct s_cpmap *ptr_cpmap;
1852 static struct s_cpmap *cpmap=(struct s_cpmap *)0;
1853
1854 if( strlen(token)<4 ) continue;
1855
1856 caid=sid=chid=dwtime=0;
1857 provid=0;
1858 skip=0;
1859 ptr1=0;
1860 for( i=0, ptr=strtok(token, "="); (i<2)&&(ptr); ptr=strtok(NULL, "="), i++ )
1861 {
1862 trim(ptr);
1863 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1864 skip=1;
1865 break;
1866 }
1867 switch( i )
1868 {
1869 case 0:
1870 ptr1=ptr;
1871 break;
1872 case 1:
1873 dwtime = atoi(ptr);
1874 break;
1875 }
1876 }
1877
1878 if( !skip )
1879 {
1880 for( i=0, ptr=strtok(ptr1, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1881 {
1882 trim(ptr);
1883 switch( i )
1884 {
1885 case 0:
1886 if( *ptr=='*' ) caid = 0;
1887 else caid = a2i(ptr, 4);
1888 break;
1889 case 1:
1890 if( *ptr=='*' ) provid = 0;
1891 else provid = a2i(ptr, 6);
1892 break;
1893 case 2:
1894 if( *ptr=='*' ) sid = 0;
1895 else sid = a2i(ptr, 4);
1896 break;
1897 case 3:
1898 if( *ptr=='*' ) chid = 0;
1899 else chid = a2i(ptr, 4);
1900 break;
1901 }
1902 }
1903 if (!(ptr_cpmap=(struct s_cpmap*)malloc(sizeof(struct s_cpmap))))
1904 {
1905 cs_log("Error allocating memory (errno=%d)", errno);
1906 return;
1907 }
1908 if( cpmap )
1909 cpmap->next=ptr_cpmap;
1910 else
1911 cfg->cpmap=ptr_cpmap;
1912 cpmap=ptr_cpmap;
1913
1914 cpmap->caid = caid;
1915 cpmap->provid = provid;
1916 cpmap->sid = sid;
1917 cpmap->chid = chid;
1918 cpmap->dwtime = dwtime;
1919 cpmap->next = 0;
1920
1921 cs_debug("nr=%d, caid=%04X, provid=%06X, sid=%04X, chid=%04X, dwtime=%d",
1922 nr, caid, provid, sid, chid, dwtime);
1923 nr++;
1924 }
1925 }
1926 fclose(fp);
1927 //cs_log("%d lengths for caid guessing loaded", nr);
1928 return;
1929}
1930#endif
1931
1932/*
1933 * makes a char ready to write a token into config or webIf
1934 */
1935char *mk_t_caidtab(CAIDTAB *ctab){
1936 int i = 0, needed = 1, pos = 0;
1937 while(ctab->caid[i]){
1938 if(ctab->mask[i]) needed += 10;
1939 else needed += 5;
1940 if(ctab->cmap[i]) needed += 5;
1941 ++i;
1942 }
1943 char *value = (char *) malloc(needed * sizeof(char));
1944 i = 0;
1945 while(ctab->caid[i]) {
1946 if(i == 0) {
1947 sprintf(value + pos, "%04X", ctab->caid[i]);
1948 pos += 4;
1949 } else {
1950 sprintf(value + pos, ",%04X", ctab->caid[i]);
1951 pos += 5;
1952 }
1953 if(ctab->mask[i]){
1954 sprintf(value + pos, "&%04X", ctab->mask[i]);
1955 pos += 5;
1956 }
1957 if(ctab->cmap[i]){
1958 sprintf(value + pos, ":%04X", ctab->cmap[i]);
1959 pos += 5;
1960 }
1961 ++i;
1962 }
1963 value[pos] = '\0';
1964 return value;
1965}
1966
1967/*
1968 * makes a char ready to write a token into config or webIf
1969 */
1970char *mk_t_tuntab(TUNTAB *ttab){
1971 int i = 0, needed = 1, pos = 0;
1972 while(ttab->bt_caidfrom[i]){
1973 if(ttab->bt_srvid[i]) needed += 10;
1974 else needed += 5;
1975 if(ttab->bt_caidto[i]) needed += 5;
1976 ++i;
1977 }
1978 char *value = (char *) malloc(needed * sizeof(char));
1979 i = 0;
1980 while(ttab->bt_caidfrom[i]) {
1981 if(i == 0) {
1982 sprintf(value + pos, "%04X", ttab->bt_caidfrom[i]);
1983 pos += 4;
1984 } else {
1985 sprintf(value + pos, ",%04X", ttab->bt_caidfrom[i]);
1986 pos += 5;
1987 }
1988 if(ttab->bt_srvid[i]){
1989 sprintf(value + pos, ".%04X", ttab->bt_srvid[i]);
1990 pos += 5;
1991 }
1992 if(ttab->bt_caidto[i]){
1993 sprintf(value + pos, ":%04X", ttab->bt_caidto[i]);
1994 pos += 5;
1995 }
1996 ++i;
1997 }
1998 value[pos] = '\0';
1999 return value;
2000}
2001
2002/*
2003 * makes a char ready to write a token into config or webIf
2004 */
2005char *mk_t_group(ulong *grp){
2006 int i = 0, needed = 1, pos = 0, dot = 0;
2007 char grpbit[33];
2008 long2bitchar((long) grp, grpbit);
2009
2010 for(i = 0; i < 32; i++){
2011 if (grpbit[i] == '1'){
2012 needed += 2;
2013 if(i > 9) needed += 1;
2014 }
2015 }
2016 char *value = (char *) malloc(needed * sizeof(char));
2017
2018 for(i = 0; i < 32; i++){
2019 if (grpbit[i] == '1'){
2020 if (dot == 0){
2021 sprintf(value + pos, "%d", i+1);
2022 if (i > 9)pos += 2;
2023 else pos += 1;
2024 dot = 1;
2025 } else {
2026 sprintf(value + pos, ",%d", i+1);
2027 if (i > 9)pos += 3;
2028 else pos += 2;
2029 }
2030 }
2031 }
2032 value[pos] = '\0';
2033 return value;
2034}
2035
2036/*
2037 * makes a char ready to write a token into config or webIf
2038 */
2039char *mk_t_ftab(FTAB *ftab){
2040 int i = 0, j = 0, needed = 1, pos = 0;
2041
2042 needed = ftab->nfilts * 5;
2043 for (i = 0; i < ftab->nfilts; ++i)
2044 needed += ftab->filts[i].nprids * 7;
2045
2046 char *value = (char *) malloc(needed * sizeof(char));
2047
2048 char *dot="";
2049 for (i = 0; i < ftab->nfilts; ++i){
2050 sprintf(value + pos, "%s%04X", dot, ftab->filts[i].caid);
2051 pos += 4;
2052 if (i > 0) pos += 1;
2053 dot=":";
2054 for (j = 0; j < ftab->filts[i].nprids; ++j) {
2055 sprintf(value + pos, "%s%06lX", dot, ftab->filts[i].prids[j]);
2056 pos += 7;
2057 dot=",";
2058 }
2059 dot=";";
2060 }
2061
2062 value[pos] = '\0';
2063 return value;
2064}
Note: See TracBrowser for help on using the repository browser.