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

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

WebIf:

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