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

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

WebIf:

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