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

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

WebIf:

  • Debug log added: load oscam.srvid is detailed shown with -d63
File size: 60.2 KB
Line 
1#include "globals.h"
2#ifdef CS_WITH_BOXKEYS
3# include "oscam-boxkeys.np"
4#endif
5
6#define CONFVARWIDTH 16
7
8static char *cs_conf="oscam.conf";
9static char *cs_user="oscam.user";
10static char *cs_srvr="oscam.server";
11static char *cs_srid="oscam.srvid";
12static char *cs_l4ca="oscam.guess";
13static char *cs_cert="oscam.cert";
14static char *cs_sidt="oscam.services";
15//static char *cs_ird="oscam.ird";
16#ifdef CS_ANTICASC
17static char *cs_ac="oscam.ac";
18#endif
19
20static char token[4096];
21
22typedef enum cs_proto_type
23{
24 TAG_GLOBAL, // must be first !
25 TAG_MONITOR, // monitor
26 TAG_CAMD33, // camd 3.3x
27 TAG_CAMD35, // camd 3.5x UDP
28 TAG_NEWCAMD, // newcamd
29 TAG_RADEGAST, // radegast
30 TAG_SERIAL, // serial (static)
31 TAG_CS357X, // camd 3.5x UDP
32 TAG_CS378X, // camd 3.5x TCP
33#ifdef CS_WITH_GBOX
34 TAG_GBOX, // gbox
35#endif
36 TAG_CCCAM, // cccam
37 TAG_DVBAPI
38#ifdef CS_ANTICASC
39 ,TAG_ANTICASC // anti-cascading
40#endif
41} cs_proto_type_t;
42
43static char *cctag[]={"global", "monitor", "camd33", "camd35",
44 "newcamd", "radegast", "serial", "cs357x", "cs378x",
45#ifdef CS_WITH_GBOX
46 "gbox",
47#endif
48 "cccam", "dvbapi",
49#ifdef CS_ANTICASC
50 "anticasc",
51#endif
52 NULL};
53
54#ifdef DEBUG_SIDTAB
55static void show_sidtab(struct s_sidtab *sidtab)
56{
57 for (; sidtab; sidtab=sidtab->next)
58 {
59 int i;
60 char buf[1024];
61 cs_log("label=%s", sidtab->label);
62 sprintf(buf, "caid(%d)=", sidtab->num_caid);
63 for (i=0; i<sidtab->num_caid; i++)
64 sprintf(buf+strlen(buf), "%04X ", sidtab->caid[i]);
65 cs_log("%s", buf);
66 sprintf(buf, "provider(%d)=", sidtab->num_provid);
67 for (i=0; i<sidtab->num_provid; i++)
68 sprintf(buf+strlen(buf), "%08X ", sidtab->provid[i]);
69 cs_log("%s", buf);
70 sprintf(buf, "services(%d)=", sidtab->num_srvid);
71 for (i=0; i<sidtab->num_srvid; i++)
72 sprintf(buf+strlen(buf), "%04X ", sidtab->srvid[i]);
73 cs_log("%s", buf);
74 }
75}
76#endif
77
78void chk_iprange(char *value, struct s_ip **base)
79{
80 int i = 0;
81 char *ptr1, *ptr2;
82 struct s_ip *lip, *cip;
83
84 for (cip=lip=*base; cip; cip=cip->next)
85 lip=cip;
86 if (!(cip=malloc(sizeof(struct s_ip))))
87 {
88 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
89 exit(1);
90 }
91 if (*base)
92 lip->next=cip;
93 else
94 *base=cip;
95
96 memset(cip, 0, sizeof(struct s_ip));
97 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
98 {
99 if (i == 0) ++i;
100 else {
101 if (!(cip=malloc(sizeof(struct s_ip)))){
102 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
103 exit(1);
104 }
105 lip->next = cip;
106 memset(cip, 0, sizeof(struct s_ip));
107 }
108 if( (ptr2=strchr(trim(ptr1), '-')) )
109 {
110 *ptr2++='\0';
111 cip->ip[0]=cs_inet_addr(trim(ptr1));
112 cip->ip[1]=cs_inet_addr(trim(ptr2));
113 }
114 else
115 cip->ip[0]=cip->ip[1]=cs_inet_addr(ptr1);
116 lip = cip;
117 }
118}
119
120void chk_caidtab(char *caidasc, CAIDTAB *ctab)
121{
122 int i;
123 char *ptr1, *ptr2, *ptr3;
124
125 //reset caid tab needed for usage while runtime from webif
126 for (i=0;i<CS_MAXCAIDTAB;i++){
127 ctab->caid[i]=0;
128 ctab->mask[i]=0;
129 ctab->cmap[i++]=0;
130 }
131
132 for (i=0, ptr1=strtok(caidasc, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
133 {
134 ulong caid, mask, cmap;
135 if( (ptr3=strchr(trim(ptr1), ':')) )
136 *ptr3++='\0';
137 else
138 ptr3="";
139 if( (ptr2=strchr(trim(ptr1), '&')) )
140 *ptr2++='\0';
141 else
142 ptr2="";
143 if (((caid=a2i(ptr1, 2))|(mask=a2i(ptr2,-2))|(cmap=a2i(ptr3, 2))) < 0x10000)
144 {
145 ctab->caid[i]=caid;
146 ctab->mask[i]=mask;
147 ctab->cmap[i++]=cmap;
148 }
149// else
150// cs_log("WARNING: wrong CAID in %s -> ignored", cs_user);
151 }
152}
153
154void chk_tuntab(char *tunasc, TUNTAB *ttab)
155{
156 int i;
157 char *ptr1, *ptr2, *ptr3;
158
159 //reset tuntab for usage during runtime from WebIf
160 for (i=0;i<CS_MAXTUNTAB;i++) {
161 ttab->bt_caidfrom[i]=0;
162 ttab->bt_caidto[i]=0;
163 ttab->bt_srvid[i++]=0;
164 }
165
166 for (i=0, ptr1=strtok(tunasc, ","); (i<CS_MAXTUNTAB) && (ptr1); ptr1=strtok(NULL, ","))
167 {
168 ulong bt_caidfrom, bt_caidto, bt_srvid;
169 if( (ptr3=strchr(trim(ptr1), ':')) )
170 *ptr3++='\0';
171 else
172 ptr3="";
173 if( (ptr2=strchr(trim(ptr1), '.')) )
174 *ptr2++='\0';
175 else
176 ptr2="";
177 if ((bt_caidfrom=a2i(ptr1, 2))|(bt_srvid=a2i(ptr2,-2))|(bt_caidto=a2i(ptr3, 2)))
178 {
179 ttab->bt_caidfrom[i]=bt_caidfrom;
180 ttab->bt_caidto[i]=bt_caidto;
181 ttab->bt_srvid[i++]=bt_srvid;
182 }
183// else
184// cs_log("WARNING: wrong Betatunnel in %s -> ignored", cs_user);
185 }
186}
187
188void chk_services(char *labels, ulong *sidok, ulong *sidno)
189{
190 int i;
191 char *ptr;
192 SIDTAB *sidtab;
193 *sidok=*sidno=0;
194 for (ptr=strtok(labels, ","); ptr; ptr=strtok(NULL, ","))
195 for (trim(ptr), i=0, sidtab=cfg->sidtab; sidtab; sidtab=sidtab->next, i++)
196 {
197 if (!strcmp(sidtab->label, ptr)) *sidok|=(1<<i);
198 if ((ptr[0]=='!') && (!strcmp(sidtab->label, ptr+1))) *sidno|=(1<<i);
199 }
200}
201
202void chk_ftab(char *zFilterAsc, FTAB *ftab, const char *zType, const char *zName, const char *zFiltName)
203{
204 int i,j;
205 char *ptr1,*ptr2,*ptr3;
206 char *ptr[CS_MAXFILTERS] = {0};
207
208 memset(ftab, 0, sizeof(FTAB));
209 for( i=0, ptr1=strtok(zFilterAsc, ";"); (i<CS_MAXFILTERS) && (ptr1); ptr1=strtok(NULL, ";"), i++ )
210 {
211 //cs_log("ptr1=%s", ptr1);
212 ptr[i] = ptr1;
213 if( (ptr2=strchr(trim(ptr1), ':')) )
214 {
215 //cs_log("ptr2=%s", ptr2);
216 *ptr2++='\0';
217 //cs_log("ptr2=%s", ptr2);
218 ftab->filts[i].caid = (ushort)a2i(ptr1, 4);
219 //cs_log("caid=%04X", ftab->filts[i].caid);
220 ptr[i] = ptr2;
221 }
222 else if (zFiltName && zFiltName[0]=='c')
223 {
224 cs_log("PANIC: CAID field not found in CHID parameter!");
225 cs_exit(1);
226 }
227 ftab->nfilts++;
228 }
229
230 if( ftab->nfilts ) cs_debug("%s '%s' %s filter(s):", zType, zName, zFiltName);
231 for( i=0; i<ftab->nfilts; i++ )
232 {
233 cs_debug("CAID #%d: %04X", i, ftab->filts[i].caid);
234 for( j=0, ptr3=strtok(ptr[i], ","); (j<CS_MAXPROV) && (ptr3); ptr3=strtok(NULL, ","), j++ )
235 {
236 ftab->filts[i].prids[j] = a2i(ptr3,6);
237 ftab->filts[i].nprids++;
238 cs_debug("%s #%d: %06X", zFiltName, j, ftab->filts[i].prids[j]);
239 }
240 }
241 //cs_log("exit chk_ftab");
242}
243
244void chk_cltab(char *classasc, CLASSTAB *clstab)
245{
246 int i;
247 char *ptr1;
248 for( i=0, ptr1=strtok(classasc, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ",") )
249 {
250 ptr1=trim(ptr1);
251 if( ptr1[0] == '!' )
252 clstab->bclass[clstab->bn++] = (uchar)a2i(ptr1+1, 2);
253 else
254 clstab->aclass[clstab->an++] = (uchar)a2i(ptr1, 2);
255 }
256}
257
258void chk_port_tab(char *portasc, PTAB *ptab)
259{
260 int i,j,nfilts,ifilt,iport;
261 char *ptr1,*ptr2,*ptr3;
262 char *ptr[CS_MAXPORTS] = {0};
263 int port[CS_MAXPORTS] = {0};
264 int previous_nports = ptab->nports;
265
266 for (nfilts=i=previous_nports, ptr1=strtok(portasc, ";"); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ";"), i++)
267 {
268 ptr[i] = ptr1;
269 if( (ptr2=strchr(trim(ptr1), '@')) )
270 {
271 *ptr2++='\0';
272 ptab->ports[i].s_port = atoi(ptr1);
273 ptr[i] = ptr2;
274 port[i] = ptab->ports[i].s_port;
275 ptab->nports++;
276 }
277 nfilts++;
278 }
279
280 if( nfilts==1 && strlen(portasc)<6 && ptab->ports[0].s_port == 0 ) {
281 ptab->ports[0].s_port = atoi(portasc);
282 ptab->nports = 1;
283 }
284
285 iport=ifilt = previous_nports;
286 for (i=previous_nports; i<nfilts; i++)
287 {
288 if( port[i]!=0 ) iport = i;
289 for (j=0, ptr3=strtok(ptr[i], ","); (j<CS_MAXPROV) && (ptr3); ptr3=strtok(NULL, ","), j++)
290 {
291 if( (ptr2=strchr(trim(ptr3), ':')) )
292 {
293 *ptr2++='\0';
294 ptab->ports[iport].ftab.nfilts++;
295 ifilt = ptab->ports[iport].ftab.nfilts-1;
296 ptab->ports[iport].ftab.filts[ifilt].caid = (ushort)a2i(ptr3, 4);
297 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr2, 6);
298 } else {
299 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr3, 6);
300 }
301 ptab->ports[iport].ftab.filts[ifilt].nprids++;
302 }
303 }
304}
305
306#ifdef NOTUSED
307static void chk_srvip(char *value, in_addr_t *ip)
308{
309 int i;
310 char *ptr;
311 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
312 if (i<8) ip[i++]=inet_addr(ptr);
313}
314#endif
315
316void chk_t_global(char *token, char *value)
317{
318 if (!strcmp(token, "serverip")) { cfg->srvip=inet_addr(value); return; }
319 if (!strcmp(token, "logfile")) { 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 /*
786 * case insensitive
787 */
788 strtolower(value);
789 if (!strcmp(token, "au"))
790 {
791 //set default values for usage during runtime from Webif
792 account->au=-1;
793 account->autoau=0;
794
795 if(value && value[0]=='1') account->autoau=1;
796 for (i=0; i<CS_MAXREADER; i++)
797 if ((reader[i].label[0]) &&
798 (!strncmp(reader[i].label, value, strlen(reader[i].label))))
799 account->au=i;
800 return;
801 }
802 if (!strcmp(token, "group")) {
803 account->grp = 0;
804 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
805 {
806 int g;
807 g=atoi(ptr1);
808 if ((g>0) && (g<33)) account->grp|=(1<<(g-1));
809 }
810 return;
811 }
812 if(!strcmp(token, "services")) { chk_services(value, &account->sidtabok, &account->sidtabno); return; }
813 if(!strcmp(token, "ident")) { chk_ftab(value, &account->ftab, "user", account->usr, "provid"); return; }
814 if(!strcmp(token, "class")) { chk_cltab(value, &account->cltab); return; }
815 if(!strcmp(token, "chid")) { chk_ftab(value, &account->fchid, "user", account->usr, "chid"); return; }
816
817 if (!strcmp(token, "expdate"))
818 {
819 if (!value[0]) {
820 account->expirationdate=(time_t)NULL;
821 return;
822 }
823 struct tm cstime;
824 memset(&cstime,0,sizeof(cstime));
825 for (i=0, ptr1=strtok(value, "-/"); (i<3)&&(ptr1); ptr1=strtok(NULL, "-/"), i++)
826 {
827 switch(i)
828 {
829 case 0: cstime.tm_year=atoi(ptr1)-1900; break;
830 case 1: cstime.tm_mon =atoi(ptr1)-1; break;
831 case 2: cstime.tm_mday=atoi(ptr1); break;
832 }
833 }
834 account->expirationdate=mktime(&cstime);
835 return;
836 }
837
838#ifdef CS_ANTICASC
839 if( !strcmp(token, "numusers") )
840 {
841 account->ac_users = atoi(value);
842 return;
843 }
844 if( !strcmp(token, "penalty") )
845 {
846 account->ac_penalty = atoi(value);
847 return;
848 }
849#endif
850 if (token[0] != '#')
851 fprintf(stderr, "Warning: keyword '%s' in account section not recognized\n",token);
852
853// if (!strcmp(token, "caid"))
854// {
855// for (i=0, ptr1=strtok(value, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
856// {
857// ulong caid, mask;
858// if (ptr2=strchr(trim(ptr1), '&'))
859// *ptr2++='\0';
860// else
861// ptr2="";
862// if (((caid=a2i(ptr1, 2))|(mask=a2i(ptr2,-2))) < 0x10000)
863// {
864// account->caidtab[i][0]=caid;
865// account->caidtab[i++][1]=mask;
866// }
867// else
868// cs_log("WARNING: wrong CAID in %s -> ignored", cs_user);
869// }
870// }
871}
872
873int write_services()
874{
875 int i;
876 FILE *f;
877 struct s_sidtab *sidtab = cfg->sidtab;
878 char tmpfile[256];
879 char destfile[256];
880 char bakfile[256];
881
882 snprintf(destfile, 255,"%s%s", cs_confdir, cs_sidt);
883 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_sidt);
884 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_sidt);
885
886 if (!(f=fopen(tmpfile, "w"))){
887 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
888 return(1);
889 }
890 fprintf(f,"#oscam.services generated automatically\n\n");
891
892 while(sidtab != NULL){
893 fprintf(f,"[%s]\n", sidtab->label);
894 fprintf_conf(f, CONFVARWIDTH, "caid", "");
895 for (i=0; i<sidtab->num_caid; i++){
896 if (i==0) fprintf(f,"%04X", sidtab->caid[i]);
897 else fprintf(f,",%04X", sidtab->caid[i]);
898 }
899 fputc((int)'\n', f);
900 fprintf_conf(f, CONFVARWIDTH, "provid", "");
901 for (i=0; i<sidtab->num_provid; i++){
902 if (i==0) fprintf(f,"%08lX", sidtab->provid[i]);
903 else fprintf(f,",%08lX", sidtab->provid[i]);
904 }
905 fputc((int)'\n', f);
906 fprintf_conf(f, CONFVARWIDTH, "srvid", "");
907 for (i=0; i<sidtab->num_srvid; i++){
908 if (i==0) fprintf(f,"%04X", sidtab->srvid[i]);
909 else fprintf(f,",%04X", sidtab->srvid[i]);
910 }
911 fprintf(f,"\n\n");
912 sidtab=sidtab->next;
913 }
914
915 fclose(f);
916 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
917}
918
919int write_config()
920{
921 int i,j;
922 FILE *f;
923 char *dot = "", *dot1 = "", *dot2 = ""; //flags for delimiters
924 char tmpfile[256];
925 char destfile[256];
926 char bakfile[256];
927
928 snprintf(destfile, 255,"%s%s", cs_confdir, cs_conf);
929 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_conf);
930 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_conf);
931
932 if (!(f=fopen(tmpfile, "w"))){
933 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
934 return(1);
935 }
936 fprintf(f,"#oscam.config generated automatically\n\n");
937
938 /*global settings*/
939 fprintf(f,"[global]\n");
940 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->srvip));
941 fprintf_conf(f, CONFVARWIDTH, "logfile", "%s\n", logfile);
942 fprintf_conf(f, CONFVARWIDTH, "pidfile", "%s\n", cfg->pidfile);
943 fprintf_conf(f, CONFVARWIDTH, "usrfile", "%s\n", cfg->usrfile);
944 fprintf_conf(f, CONFVARWIDTH, "cwlogdir", "%s\n", cfg->cwlogdir);
945 fprintf_conf(f, CONFVARWIDTH, "clienttimeout", "%ld\n", cfg->ctimeout/1000);
946 fprintf_conf(f, CONFVARWIDTH, "fallbacktimeout", "%ld\n", cfg->ftimeout/1000);
947 fprintf_conf(f, CONFVARWIDTH, "clientmaxidle", "%d\n", cfg->cmaxidle);
948 fprintf_conf(f, CONFVARWIDTH, "cachedelay", "%ld\n", cfg->delay/1000);
949 fprintf_conf(f, CONFVARWIDTH, "bindwait", "%d\n", cfg->bindwait);
950 fprintf_conf(f, CONFVARWIDTH, "netprio", "%ld\n", cfg->netprio);
951 fprintf_conf(f, CONFVARWIDTH, "resolvedelay", "%d\n", cfg->resolvedelay);
952 fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", cfg->tosleep);
953 fprintf_conf(f, CONFVARWIDTH, "unlockparental", "%d\n", cfg->ulparent);
954 fprintf_conf(f, CONFVARWIDTH, "nice", "%d\n", cfg->nice);
955 fprintf_conf(f, CONFVARWIDTH, "serialreadertimeout", "%d\n", cfg->srtimeout);
956 fprintf_conf(f, CONFVARWIDTH, "maxlogsize", "%d\n", cfg->max_log_size);
957 fprintf_conf(f, CONFVARWIDTH, "showecmdw", "%d\n", cfg->show_ecm_dw);
958 fprintf_conf(f, CONFVARWIDTH, "waitforcards", "%d\n", cfg->waitforcards);
959 fprintf_conf(f, CONFVARWIDTH, "preferlocalcards", "%d\n", cfg->preferlocalcards);
960 fputc((int)'\n', f);
961
962 /*monitor settings*/
963 fprintf(f,"[monitor]\n");
964 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->mon_port);
965 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->mon_srvip));
966
967 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
968 struct s_ip *cip;
969 for (cip = cfg->mon_allowed; cip; cip = cip->next){
970 fprintf(f,"%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
971 if (cip->ip[0] == cip->ip[1]) fprintf(f,"-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
972 dot=",";
973 }
974 fputc((int)'\n', f);
975 fprintf_conf(f, CONFVARWIDTH, "aulow", "%d\n", cfg->mon_aulow);
976 fprintf_conf(f, CONFVARWIDTH, "hideclient_to", "%d\n", cfg->mon_hideclient_to);
977 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", cfg->mon_level);
978 fprintf_conf(f, CONFVARWIDTH, "httpport", "%d\n", cfg->http_port);
979 fprintf_conf(f, CONFVARWIDTH, "httpuser", "%s\n", cfg->http_user);
980 fprintf_conf(f, CONFVARWIDTH, "httppwd", "%s\n", cfg->http_pwd);
981 fprintf_conf(f, CONFVARWIDTH, "httpcss", "%s\n", cfg->http_css);
982 fprintf_conf(f, CONFVARWIDTH, "httprefresh", "%d\n", cfg->http_refresh);
983 fprintf_conf(f, CONFVARWIDTH, "httphideidleclients", "%d\n", cfg->http_hide_idle_clients);
984 fputc((int)'\n', f);
985
986 /*newcamd*/
987 if ((cfg->ncd_ptab.nports > 0) && (cfg->ncd_ptab.ports[0].s_port > 0)){
988 fprintf(f,"[newcamd]\n");
989 fprintf_conf(f, CONFVARWIDTH, "port", "");
990 dot1 = "";
991 for(i = 0; i < cfg->ncd_ptab.nports; ++i){
992 fprintf(f,"%s%d@%04X", dot1, cfg->ncd_ptab.ports[i].s_port, cfg->ncd_ptab.ports[i].ftab.filts[0].caid);
993 if (cfg->ncd_ptab.ports[i].ftab.filts[0].nprids > 0){
994 fprintf(f,":");
995 dot2 = "";
996 for (j = 0; j < cfg->ncd_ptab.ports[i].ftab.filts[0].nprids; ++j){
997 fprintf(f,"%s%lX", dot2, cfg->ncd_ptab.ports[i].ftab.filts[0].prids[j]);
998 dot2 = ",";
999 }
1000 }
1001 dot1=";";
1002 }
1003
1004 fputc((int)'\n', f);
1005 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->ncd_srvip));
1006 fprintf_conf(f, CONFVARWIDTH, "key", "");
1007 for (i=0;i<14;i++) fprintf(f,"%02X", cfg->ncd_key[i]);
1008 fprintf(f,"\n\n");
1009 }
1010
1011 /*camd3.3*/
1012 if ( cfg->c33_port > 0) {
1013 fprintf(f,"[camd33]\n");
1014 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c33_port);
1015 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c33_srvip));
1016 fprintf_conf(f, CONFVARWIDTH, "passive", "%d\n", cfg->c33_passive);
1017 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);
1018 fprintf_conf(f, CONFVARWIDTH, "nocrypt", "");
1019 dot="";
1020 for (cip = cfg->c33_plain; cip; cip = cip->next){
1021 fprintf(f,"%s%s", dot, cs_inet_ntoa(cip->ip[0]));
1022 if (cip->ip[0] != cip->ip[1]) fprintf(f,"-%s", cs_inet_ntoa(cip->ip[1]));
1023 dot=",";
1024 }
1025 fprintf(f,"\n\n");
1026 }
1027
1028 /*camd3.5*/
1029 if ( cfg->c35_port > 0) {
1030 fprintf(f,"[cs357x]\n");
1031 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->c35_port);
1032 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1033 fprintf(f,"\n\n");
1034 }
1035
1036 /*camd3.5 TCP*/
1037 if ((cfg->c35_tcp_ptab.nports > 0) && (cfg->c35_tcp_ptab.ports[0].s_port > 0)) {
1038 fprintf(f,"[cs378x]\n");
1039 fprintf_conf(f, CONFVARWIDTH, "port", "");
1040 dot1 = "";
1041 for(i = 0; i < cfg->c35_tcp_ptab.nports; ++i){
1042 fprintf(f,"%s%d@%04X", dot1, cfg->c35_tcp_ptab.ports[i].s_port, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].caid);
1043 if (cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids > 0){
1044 fprintf(f,":");
1045 dot2 = "";
1046 for (j = 0; j < cfg->c35_tcp_ptab.ports[i].ftab.filts[0].nprids; ++j){
1047 fprintf(f,"%s%lX", dot2, cfg->c35_tcp_ptab.ports[i].ftab.filts[0].prids[j]);
1048 dot2 = ",";
1049 }
1050 }
1051 dot1=";";
1052 }
1053
1054 fputc((int)'\n', f);
1055 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->c35_tcp_srvip));
1056 fputc((int)'\n', f);
1057 }
1058
1059 /*Radegast*/
1060 if ( cfg->rad_port > 0) {
1061 fprintf(f,"[radegast]\n");
1062 fprintf_conf(f, CONFVARWIDTH, "port", "%d\n", cfg->rad_port);
1063 fprintf_conf(f, CONFVARWIDTH, "serverip", "%s\n", inet_ntoa(*(struct in_addr *)&cfg->rad_srvip));
1064 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->rad_usr);
1065 fprintf_conf(f, CONFVARWIDTH, "allowed", "");
1066 struct s_ip *cip;
1067 for (cip = cfg->rad_allowed; cip; cip = cip->next){
1068 fprintf(f,"%s%s", dot, inet_ntoa(*(struct in_addr *)&cip->ip[0]));
1069 if (cip->ip[0] == cip->ip[1]) fprintf(f,"-%s", inet_ntoa(*(struct in_addr *)&cip->ip[1]));
1070 dot=",";
1071 }
1072 fprintf(f,"\n\n");
1073 }
1074
1075#ifdef CS_WITH_GBOX
1076 /*Gbox*/
1077 if ((cfg->gbox_pwd[0] > 0) || (cfg->gbox_pwd[1] > 0) || (cfg->gbox_pwd[2] > 0) || (cfg->gbox_pwd[3] > 0)){
1078 fprintf(f,"[gbox]\n");
1079 fprintf_conf(f, CONFVARWIDTH, "password", ""); for (i=0;i<4;i++) fprintf(f,"%02X", cfg->gbox_pwd[i]); fputc((int)'\n', f);;
1080 fprintf_conf(f, CONFVARWIDTH, "maxdist", "%d\n", cfg->maxdist);
1081 fprintf_conf(f, CONFVARWIDTH, "ignorelist", "%s\n", cfg->ignorefile);
1082 fprintf_conf(f, CONFVARWIDTH, "onlineinfos", "%s\n", cfg->gbxShareOnl);
1083 fprintf_conf(f, CONFVARWIDTH, "cardinfos", "%s\n", cfg->cardfile);
1084 fprintf_conf(f, CONFVARWIDTH, "locals", "");
1085 char *dot = "";
1086 for (i = 0; i < cfg->num_locals; i++){
1087 fprintf(f,"%s%06lX", dot, cfg->locals[i]);
1088 dot=";";
1089 }
1090 fprintf(f,"\n\n");
1091 }
1092#endif
1093
1094 /*serial*/
1095
1096
1097 /*cccam placeholder*/
1098
1099#ifdef HAVE_DVBAPI
1100 /*dvb-api*/
1101 if (cfg->dvbapi_enabled > 0) {
1102 fprintf(f,"[dvbapi]\n");
1103 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->dvbapi_enabled);
1104 fprintf_conf(f, CONFVARWIDTH, "au", "%d\n", cfg->dvbapi_au);
1105 fprintf_conf(f, CONFVARWIDTH, "boxtype", "%s\n", cfg->dvbapi_boxtype);
1106 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", cfg->dvbapi_usr);
1107 fputc((int)'\n', f);
1108 }
1109#endif
1110
1111#ifdef CS_ANTICASC
1112 if (cfg->ac_enabled > 0){
1113 fprintf_conf(f, CONFVARWIDTH, "enabled", "%d\n", cfg->ac_enabled);
1114 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", cfg->ac_users);
1115 fprintf_conf(f, CONFVARWIDTH, "sampletime", "%d\n", cfg->ac_stime);
1116 fprintf_conf(f, CONFVARWIDTH, "samples", "%d\n", cfg->ac_samples);
1117 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", cfg->ac_penalty);
1118 fprintf_conf(f, CONFVARWIDTH, "aclogfile", "%s\n", cfg->ac_logfile);
1119 fprintf_conf(f, CONFVARWIDTH, "denysamples", "%d\n", cfg->ac_denysamples);
1120 fprintf_conf(f, CONFVARWIDTH, "fakedelay", "%d\n", cfg->ac_fakedelay);
1121 fputc((int)'\n', f);
1122 }
1123#endif
1124
1125 fclose(f);
1126
1127 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1128}
1129
1130int write_userdb()
1131{
1132 int i;
1133 FILE *f;
1134 struct s_auth *account;
1135 char *dot = ""; //flag for comma
1136 char tmpfile[256];
1137 char destfile[256];
1138 char bakfile[256];
1139
1140 snprintf(destfile, 255,"%s%s", cs_confdir, cs_user);
1141 snprintf(tmpfile, 255, "%s%s.tmp", cs_confdir, cs_user);
1142 snprintf(bakfile, 255,"%s%s.bak", cs_confdir, cs_user);
1143
1144 if (!(f=fopen(tmpfile, "w"))){
1145 cs_log("Cannot open file \"%s\" (errno=%d)", tmpfile, errno);
1146 return(1);
1147 }
1148 fprintf(f,"#oscam.user generated automatically\n\n");
1149
1150 //each account
1151 for (account=cfg->account; (account) ; account=account->next){
1152 fprintf(f,"[account]\n");
1153 fprintf_conf(f, CONFVARWIDTH, "user", "%s\n", account->usr);
1154 fprintf_conf(f, CONFVARWIDTH, "pwd", "%s\n", account->pwd);
1155 struct tm * timeinfo = localtime (&account->expirationdate);
1156 char buf [80];
1157 strftime (buf,80,"%Y-%m-%d",timeinfo);
1158 if(strcmp(buf,"1970-01-01"))
1159 fprintf_conf(f, CONFVARWIDTH, "expdate", "%s\n", buf);
1160 else
1161 fprintf_conf(f, CONFVARWIDTH, "expdate", "\n");
1162
1163 //group
1164 char *value = mk_t_group((ulong*)account->grp);
1165 fprintf_conf(f, CONFVARWIDTH, "group", "%s\n", value);
1166 free(value);
1167
1168 fprintf_conf(f, CONFVARWIDTH, "hostname", "%s\n", account->dyndns);
1169 fprintf_conf(f, CONFVARWIDTH, "uniq", "%d\n", account->uniq);
1170 fprintf_conf(f, CONFVARWIDTH, "sleep", "%d\n", account->tosleep);
1171 fprintf_conf(f, CONFVARWIDTH, "monlevel", "%d\n", account->monlvl);
1172
1173 if (account->au > -1)
1174 if (account->au < CS_MAXREADER)
1175 fprintf_conf(f, CONFVARWIDTH, "au", "%s\n", reader[account->au].label);
1176 if (account->autoau == 1) fprintf_conf(f, CONFVARWIDTH, "au", "1\n");
1177
1178 fprintf_conf(f, CONFVARWIDTH, "services", "");
1179 char sidok[33]; long2bitchar(account->sidtabok,sidok);
1180 char sidno[33]; long2bitchar(account->sidtabno,sidno);
1181 struct s_sidtab *sidtab = cfg->sidtab;
1182 i=0; dot = "";
1183 for (; sidtab; sidtab=sidtab->next){
1184 if(sidok[i]=='1') {fprintf(f,"%s%s", dot, sidtab->label); dot = ",";}
1185 if(sidno[i]=='1') {fprintf(f,"%s!%s", dot, sidtab->label); dot = ",";}
1186 i++;
1187 }
1188 fputc((int)'\n', f);
1189
1190 //CAID
1191 value = mk_t_caidtab(&account->ctab);
1192 fprintf_conf(f, CONFVARWIDTH, "caid", "%s\n", value);
1193 free(value);
1194
1195 //betatunnel
1196 value = mk_t_tuntab(&account->ttab);
1197 fprintf_conf(f, CONFVARWIDTH, "betatunnel", "%s\n", value);
1198 free(value);
1199
1200 //ident
1201 value = mk_t_ftab(&account->ftab);
1202 fprintf_conf(f, CONFVARWIDTH, "ident", "%s\n", value);
1203 free(value);
1204
1205#ifdef CS_ANTICASC
1206 fprintf_conf(f, CONFVARWIDTH, "numusers", "%d\n", account->ac_users);
1207 fprintf_conf(f, CONFVARWIDTH, "penalty", "%d\n", account->ac_penalty);
1208#endif
1209 fputc((int)'\n', f);
1210 }
1211 fclose(f);
1212
1213 return(safe_overwrite_with_bak(destfile, tmpfile, bakfile, 0));
1214}
1215
1216int init_userdb()
1217{
1218 int tag=0, nr, nro, expired;
1219 //int first=1;
1220 FILE *fp;
1221 char *value;
1222 struct s_auth *ptr;
1223 /*static */struct s_auth *account=(struct s_auth *)0;
1224
1225 sprintf(token, "%s%s", cs_confdir, cs_user);
1226 if (!(fp=fopen(token, "r")))
1227 {
1228 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1229 return(1);
1230 }
1231 for (nro=0, ptr=cfg->account; ptr; nro++)
1232 {
1233 struct s_auth *ptr_next;
1234 ptr_next=ptr->next;
1235 free(ptr);
1236 ptr=ptr_next;
1237 }
1238 nr=0;
1239 while (fgets(token, sizeof(token), fp))
1240 {
1241 int i, l;
1242 void *ptr;
1243 if ((l=strlen(trim(token)))<3) continue;
1244 if ((token[0]=='[') && (token[l-1]==']'))
1245 {
1246 token[l-1]=0;
1247 tag=(!strcmp("account", strtolower(token+1)));
1248 if (!(ptr=malloc(sizeof(struct s_auth))))
1249 {
1250 cs_log("Error allocating memory (errno=%d)", errno);
1251 return(1);
1252 }
1253 if (account)
1254 account->next=ptr;
1255 else
1256 cfg->account=ptr;
1257 account=ptr;
1258 memset(account, 0, sizeof(struct s_auth));
1259 account->au=(-1);
1260 account->monlvl=cfg->mon_level;
1261 account->tosleep=cfg->tosleep;
1262 for (i=1; i<CS_MAXCAIDTAB; account->ctab.mask[i++]=0xffff);
1263 for (i=1; i<CS_MAXTUNTAB; account->ttab.bt_srvid[i++]=0x0000);
1264 nr++;
1265#ifdef CS_ANTICASC
1266 account->ac_users=cfg->ac_users;
1267 account->ac_penalty=cfg->ac_penalty;
1268 account->ac_idx = nr;
1269#endif
1270 continue;
1271 }
1272 if (!tag) continue;
1273 if (!(value=strchr(token, '='))) continue;
1274 *value++='\0';
1275 chk_account(trim(strtolower(token)), trim(value), account);
1276 }
1277 fclose(fp);
1278
1279 for (expired=0, ptr=cfg->account; ptr;)
1280 {
1281 if(ptr->expirationdate && ptr->expirationdate<time(NULL)) expired++;
1282 ptr=ptr->next;
1283 }
1284
1285 cs_log("userdb reloaded: %d accounts freed, %d accounts loaded, %d expired", nro, nr, expired);
1286 return(0);
1287}
1288
1289static void chk_entry4sidtab(char *value, struct s_sidtab *sidtab, int what)
1290{
1291 int i, b;
1292 char *ptr;
1293 ushort *slist=(ushort *) 0;
1294 ulong *llist=(ulong *) 0;
1295 ulong caid;
1296 char buf[strlen(value) + 1];
1297 cs_strncpy(buf, value, sizeof(buf));
1298 b=(what==1) ? sizeof(ulong) : sizeof(ushort);
1299 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1300 {
1301 caid=a2i(ptr, b);
1302 if (!errno) i++;
1303 }
1304 //if (!i) return(0);
1305 if (b==sizeof(ushort))
1306 slist=malloc(i*sizeof(ushort));
1307 else
1308 llist=malloc(i*sizeof(ulong));
1309 strcpy(value, buf);
1310 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1311 {
1312 caid=a2i(ptr, b);
1313 if (errno) continue;
1314 if (b==sizeof(ushort))
1315 slist[i++]=(ushort) caid;
1316 else
1317 llist[i++]=caid;
1318 }
1319 switch (what)
1320 {
1321 case 0: sidtab->caid=slist;
1322 sidtab->num_caid=i;
1323 break;
1324 case 1: sidtab->provid=llist;
1325 sidtab->num_provid=i;
1326 break;
1327 case 2: sidtab->srvid=slist;
1328 sidtab->num_srvid=i;
1329 break;
1330 }
1331}
1332
1333void chk_sidtab(char *token, char *value, struct s_sidtab *sidtab)
1334{
1335 if (!strcmp(token, "caid")) { chk_entry4sidtab(value, sidtab, 0); return; }
1336 if (!strcmp(token, "provid")) { chk_entry4sidtab(value, sidtab, 1); return; }
1337 if (!strcmp(token, "ident")) { chk_entry4sidtab(value, sidtab, 1); return; }
1338 if (!strcmp(token, "srvid")) { chk_entry4sidtab(value, sidtab, 2); return; }
1339 if (token[0] != '#')
1340 fprintf(stderr, "Warning: keyword '%s' in sidtab section not recognized\n",token);
1341}
1342
1343int init_sidtab()
1344{
1345 int nr, nro;
1346 FILE *fp;
1347 char *value;
1348 struct s_sidtab *ptr;
1349 struct s_sidtab *sidtab=(struct s_sidtab *)0;
1350
1351 sprintf(token, "%s%s", cs_confdir, cs_sidt);
1352 if (!(fp=fopen(token, "r")))
1353 {
1354 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
1355 return(1);
1356 }
1357 for (nro=0, ptr=cfg->sidtab; ptr; nro++)
1358 {
1359 struct s_sidtab *ptr_next;
1360 ptr_next=ptr->next;
1361 if (ptr->caid) free(ptr->caid);
1362 if (ptr->provid) free(ptr->provid);
1363 if (ptr->srvid) free(ptr->srvid);
1364 free(ptr);
1365 ptr=ptr_next;
1366 }
1367 nr=0;
1368 while (fgets(token, sizeof(token), fp))
1369 {
1370 int l;
1371 void *ptr;
1372 if ((l=strlen(trim(token)))<3) continue;
1373 if ((token[0]=='[') && (token[l-1]==']'))
1374 {
1375 token[l-1]=0;
1376 if (!(ptr=malloc(sizeof(struct s_sidtab))))
1377 {
1378 cs_log("Error allocating memory (errno=%d)", errno);
1379 return(1);
1380 }
1381 if (sidtab)
1382 sidtab->next=ptr;
1383 else
1384 cfg->sidtab=ptr;
1385 sidtab=ptr;
1386 nr++;
1387 memset(sidtab, 0, sizeof(struct s_sidtab));
1388 cs_strncpy(sidtab->label, strtolower(token+1), sizeof(sidtab->label));
1389 continue;
1390 }
1391 if (!sidtab) continue;
1392 if (!(value=strchr(token, '='))) continue;
1393 *value++='\0';
1394 chk_sidtab(trim(strtolower(token)), trim(strtolower(value)), sidtab);
1395 }
1396 fclose(fp);
1397
1398#ifdef DEBUG_SIDTAB
1399 show_sidtab(cfg->sidtab);
1400#endif
1401 cs_log("services reloaded: %d services freed, %d services loaded", nro, nr);
1402 return(0);
1403}
1404
1405int init_srvid()
1406{
1407 int nr;
1408 FILE *fp;
1409 char *payload;
1410 static struct s_srvid *srvid=(struct s_srvid *)0;
1411 sprintf(token, "%s%s", cs_confdir, cs_srid);
1412
1413 if (!(fp=fopen(token, "r"))) {
1414 cs_log("can't open file \"%s\" (err=%d), no service-id's loaded", token, errno);
1415 return(0);
1416 }
1417
1418 nr=0;
1419 while (fgets(token, sizeof(token), fp)) {
1420
1421 int l;
1422 void *ptr;
1423 char *tmp;
1424 tmp = trim(token);
1425
1426 if (tmp[0] == '#') continue;
1427 if ((l=strlen(tmp)) < 6) continue;
1428 if (!(payload=strchr(token, '|'))) continue;
1429 *payload++ = '\0';
1430
1431 if (!(ptr = malloc(sizeof(struct s_srvid)))) {
1432 cs_log("Error allocating memory (errno=%d)", errno);
1433 return(1);
1434 }
1435
1436 if (srvid)
1437 srvid->next = ptr;
1438 else
1439 cfg->srvid = ptr;
1440
1441 srvid = ptr;
1442 memset(srvid, 0, sizeof(struct s_srvid));
1443
1444 int i;
1445 char *ptr1;
1446 for (i = 0, ptr1 = strtok(payload, "|"); ptr1; ptr1 = strtok(NULL, "|"), i++){
1447 switch(i){
1448 case 0:
1449 cs_strncpy(srvid->prov, trim(ptr1), sizeof(srvid->prov));
1450 break;
1451 case 1:
1452 cs_strncpy(srvid->name, trim(ptr1), sizeof(srvid->name));
1453 break;
1454 case 2:
1455 cs_strncpy(srvid->type, trim(ptr1), sizeof(srvid->type));
1456 break;
1457 case 3:
1458 cs_strncpy(srvid->desc, trim(ptr1), sizeof(srvid->desc));
1459 break;
1460 }
1461 }
1462
1463 char *srvidasc = strchr(token, ':');
1464 *srvidasc++ = '\0';
1465 srvid->srvid = word_atob(srvidasc);
1466
1467 srvid->ncaid = 0;
1468 for (i = 0, ptr1 = strtok(token, ","); (ptr1) && (i < 10) ; ptr1 = strtok(NULL, ","), i++){
1469 srvid->caid[i] = word_atob(ptr1);
1470 srvid->ncaid = i+1;
1471 cs_debug("ld caid: %04X srvid: %04X Prov: %s Chan: %s",srvid->caid[i],srvid->srvid,srvid->prov,srvid->name);
1472 }
1473 nr++;
1474 }
1475
1476 fclose(fp);
1477 if (nr>0)
1478 cs_log("%d service-id's loaded", nr);
1479 else{
1480 cs_log("oscam.srvid loading failed, old format");
1481 }
1482 return(0);
1483}
1484
1485static void chk_reader(char *token, char *value, struct s_reader *rdr)
1486{
1487 int i;
1488 char *ptr;
1489 /*
1490 * case sensitive first
1491 */
1492 if (!strcmp(token, "device"))
1493 {
1494 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1495 {
1496 trim(ptr);
1497 switch(i)
1498 {
1499 case 0: cs_strncpy(rdr->device, ptr, sizeof(rdr->device)); break;
1500 case 1: rdr->r_port=atoi(ptr); break;
1501 case 2: rdr->l_port=atoi(ptr); break;
1502 }
1503 }
1504 return;
1505 }
1506 if (!strcmp(token, "key"))
1507 {
1508 if (key_atob14(value, rdr->ncd_key))
1509 {
1510 fprintf(stderr, "Configuration newcamd: Error in Key\n");
1511 exit(1);
1512 }
1513 return;
1514 }
1515#ifdef CS_WITH_GBOX
1516 if (!strcmp(token, "password")) { cs_strncpy((char *)rdr->gbox_pwd, (const char *)i2b(4, a2i(value, 4)), 4); return; }
1517 if (!strcmp(token, "premium")) { rdr->gbox_prem=1; return; }
1518#endif
1519 if (!strcmp(token, "account"))
1520 {
1521 for (i=0, ptr=strtok(value, ","); (i<2)&&(ptr); ptr=strtok(NULL, ","), i++)
1522 {
1523 trim(ptr);
1524 switch(i)
1525 {
1526 case 0: cs_strncpy(rdr->r_usr, ptr, sizeof(rdr->r_usr)); break;
1527 case 1: cs_strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd)); break;
1528 }
1529 }
1530 return;
1531 }
1532 if( !strcmp(token, "pincode")) { cs_strncpy(rdr->pincode, value, sizeof(rdr->pincode)); return; }
1533 if (!strcmp(token, "readnano")) { cs_strncpy((char *)rdr->emmfile, value, sizeof(rdr->emmfile)); return; }
1534 /*
1535 * case insensitive
1536 */
1537 strtolower(value);
1538
1539 if (!strcmp(token, "services")) { chk_services(value, &rdr->sidtabok, &rdr->sidtabno); return; }
1540 if (!strcmp(token, "inactivitytimeout")) { rdr->tcp_ito = atoi(value); return; }
1541 if (!strcmp(token, "reconnecttimeout")) { rdr->tcp_rto = atoi(value); return; }
1542 if (!strcmp(token, "disableserverfilter")) { rdr->ncd_disable_server_filt = atoi(value); return; }
1543
1544 if (!strcmp(token, "label")) { cs_strncpy(rdr->label, value, sizeof(rdr->label)); return; }
1545 if (!strcmp(token, "fallback")) { rdr->fallback=atoi(value) ? 1 : 0; return; }
1546 if (!strcmp(token, "logport")) { rdr->log_port=atoi(value); return; }
1547 if (!strcmp(token, "caid")) { chk_caidtab(value, &rdr->ctab); return; }
1548 if (!strcmp(token, "boxid")) { rdr->boxid=a2i(value,4); return; }
1549 if (!strcmp(token, "aeskey"))
1550 {
1551 if (key_atob(value, rdr->aes_key))
1552 {
1553 fprintf(stderr, "Configuration reader: Error in AES Key\n");
1554 exit(1);
1555 }
1556 return;
1557 }
1558 if ((!strcmp(token, "n3_rsakey")) || (!strcmp(token, "rsakey")) )
1559 {
1560 rdr->has_rsa=1;
1561 if (key_atob_l(value, rdr->rsa_mod, 128))
1562 {
1563 fprintf(stderr, "Configuration reader: Error in rsakey\n");
1564 exit(1);
1565 }
1566 return;
1567 }
1568 if (!strcmp(token, "tiger_rsakey"))
1569 {
1570 if (key_atob_l(value, rdr->rsa_mod, 240))
1571 {
1572 fprintf(stderr, "Configuration reader: Error in tiger_rsakey\n");
1573 exit(1);
1574 }
1575 return;
1576 }
1577 if ((!strcmp(token, "n3_boxkey")) || (!strcmp(token, "boxkey")))
1578 {
1579 if (key_atob_l(value, rdr->nagra_boxkey, 16))
1580 {
1581 fprintf(stderr, "Configuration reader: Error in boxkey\n");
1582 exit(1);
1583 }
1584 return;
1585 }
1586 if (!strcmp(token, "detect"))
1587 {
1588 for (i=0; RDR_CD_TXT[i]; i++)
1589 {
1590 if (!strcmp(value, RDR_CD_TXT[i]))
1591 rdr->detect=i;
1592 else
1593 if ((value[0]=='!') && (!strcmp(value+1, RDR_CD_TXT[i])))
1594 rdr->detect=i|0x80;
1595 }
1596 return;
1597 }
1598 if (!strcmp(token, "mhz")) { rdr->mhz=atoi(value); return; }
1599 if (!strcmp(token, "cardmhz")) { rdr->cardmhz=atoi(value); return; }
1600 if (!strcmp(token, "protocol"))
1601 {
1602 if (!strcmp(value, "mouse")) { rdr->typ=R_MOUSE; return; }
1603 if (!strcmp(value, "smartreader")) { rdr->typ=R_SMART; return; }
1604 if (!strcmp(value, "internal")) { rdr->typ=R_INTERNAL; return; }
1605#ifdef HAVE_PCSC
1606 if (!strcmp(value, "pcsc")) { rdr->typ=R_PCSC; return; }
1607#endif
1608 if (!strcmp(value, "serial")) { rdr->typ=R_SERIAL; return; }
1609 if (!strcmp(value, "camd35")) { rdr->typ=R_CAMD35; return; }
1610 if (!strcmp(value, "cs378x")) { rdr->typ=R_CS378X; return; }
1611 if (!strcmp(value, "cs357x")) { rdr->typ=R_CAMD35; return; }
1612 if (!strcmp(value, "gbox")) { rdr->typ=R_GBOX; return; }
1613 if (!strcmp(value, "cccam")) {
1614 rdr->typ=R_CCCAM;
1615 // strcpy(value, "1");
1616 // chk_caidtab(value, &rdr->ctab); // this is a MAJOR hack for auto multiple caid support (not currently working due to ncd table issue)
1617 return;
1618 }
1619 if (!strcmp(value, "radegast")) { rdr->typ=R_RADEGAST; return; }
1620 if (!strcmp(value, "newcamd") ||
1621 !strcmp(value, "newcamd525")) {rdr->typ=R_NEWCAMD;
1622 rdr->ncd_proto=NCD_525; return; }
1623 if (!strcmp(value, "newcamd524")) {rdr->typ=R_NEWCAMD;
1624 rdr->ncd_proto=NCD_524; return; }
1625 fprintf(stderr, "WARNING: value '%s' in protocol-line not recognized, assuming MOUSE\n",value);
1626 rdr->typ=R_MOUSE;
1627 return;
1628 }
1629 if (!strcmp(token, "ident")) { chk_ftab(value, &rdr->ftab,"reader",rdr->label,"provid"); return; }
1630 if (!strcmp(token, "class")) { chk_cltab(value, &rdr->cltab); return; }
1631 if (!strcmp(token, "chid")) { chk_ftab(value, &rdr->fchid,"reader",rdr->label,"chid"); return; }
1632 if (!strcmp(token, "showcls")) { rdr->show_cls = atoi(value); return; }
1633 if (!strcmp(token, "maxqlen"))
1634 {
1635 rdr->maxqlen = atoi(value);
1636 if( rdr->maxqlen<0 || rdr->maxqlen>CS_MAXQLEN )
1637 rdr->maxqlen=CS_MAXQLEN;
1638 return;
1639 }
1640 if (!strcmp(token, "group"))
1641 {
1642 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1643 {
1644 int g;
1645 g=atoi(ptr);
1646 if ((g>0) && (g<33)) rdr->grp|=(1<<(g-1));
1647 }
1648 return;
1649 }
1650 if (!strcmp(token, "emmcache"))
1651 {
1652 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1653 switch(i)
1654 {
1655 case 0: rdr->cachemm=atoi(ptr); break;
1656 case 1: rdr->rewritemm=atoi(ptr); break;
1657 case 2: rdr->logemm=atoi(ptr); break;
1658 }
1659 if (rdr->rewritemm <=0) {
1660 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);
1661 rdr->rewritemm = 1;
1662 }
1663 return;
1664 }
1665
1666 if (!strcmp(token, "blocknano"))
1667 {
1668 if (!strcmp(value,"all")) //wildcard is used
1669 for (i=0 ; i<256; i++)
1670 rdr->b_nano[i] |= 0x01; //set all lsb's to block all nanos
1671 else
1672 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1673 if ((i=byte_atob(ptr))>=0)
1674 rdr->b_nano[i]|= 0x01; //lsb is set when to block nano
1675 return;
1676 }
1677 if (!strcmp(token, "savenano"))
1678 {
1679 if (!strcmp(value,"all")) //wildcard is used
1680 for (i=0 ; i<256; i++)
1681 rdr->b_nano[i] |= 0x02; //set all lsb+1 to save all nanos to file
1682 else
1683 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1684 if ((i=byte_atob(ptr))>=0)
1685 rdr->b_nano[i]|= 0x02; //lsb+1 is set when to save nano to file
1686 return;
1687 }
1688 if (!strcmp(token, "cccversion")) { // cccam version
1689 if (strlen(value)>sizeof(rdr->cc_version)-1) {
1690 fprintf(stderr, "cccam config: version too long\n");
1691 exit(1);
1692 }
1693 bzero(rdr->cc_version, sizeof(rdr->cc_version));
1694 cs_strncpy(rdr->cc_version, value, sizeof(rdr->cc_version));
1695 return;
1696 }
1697 if (!strcmp(token, "cccbuild")) { // cccam build number
1698 if (strlen(value)>sizeof(rdr->cc_build)-1) {
1699 fprintf(stderr, "cccam config build number too long\n");
1700 exit(1);
1701 }
1702 bzero(rdr->cc_build, sizeof(rdr->cc_build));
1703 cs_strncpy(rdr->cc_build, value, sizeof(rdr->cc_build));
1704 return;
1705 }
1706 if (!strcmp(token, "cccmaxhop")) { // cccam max card distance
1707 rdr->cc_maxhop = atoi(value);
1708 return;
1709 }
1710 if (token[0] != '#')
1711 fprintf(stderr, "Warning: keyword '%s' in reader section not recognized\n",token);
1712}
1713
1714int init_readerdb()
1715{
1716 int tag=0, nr;
1717 FILE *fp;
1718 char *value;
1719
1720 sprintf(token, "%s%s", cs_confdir, cs_srvr);
1721 if (!(fp=fopen(token, "r")))
1722 {
1723 cs_log("can't open file \"%s\" (errno=%d)\n", token, errno);
1724 return(1);
1725 }
1726 nr=0;
1727 while (fgets(token, sizeof(token), fp))
1728 {
1729 int i, l;
1730 if ((l=strlen(trim(token)))<3) continue;
1731 if ((token[0]=='[') && (token[l-1]==']'))
1732 {
1733 token[l-1]=0;
1734 tag=(!strcmp("reader", strtolower(token+1)));
1735 if (reader[nr].label[0] && reader[nr].typ) nr++;
1736 memset(&reader[nr], 0, sizeof(struct s_reader));
1737 reader[nr].tcp_rto = 30;
1738 reader[nr].show_cls = 10;
1739 reader[nr].maxqlen = CS_MAXQLEN;
1740 reader[nr].mhz = 357;
1741 reader[nr].cardmhz = 357;
1742 strcpy(reader[nr].pincode, "none");
1743 for (i=1; i<CS_MAXCAIDTAB; reader[nr].ctab.mask[i++]=0xffff);
1744 continue;
1745 }
1746 if (!tag) continue;
1747 if (!(value=strchr(token, '='))) continue;
1748 *value++='\0';
1749 chk_reader(trim(strtolower(token)), trim(value), &reader[nr]);
1750 }
1751 fclose(fp);
1752 return(0);
1753}
1754
1755/*
1756int init_irdeto_guess_tab()
1757{
1758 int i, j, skip;
1759 int b47;
1760 FILE *fp;
1761 char token[128], *value, *ptr;
1762 char zSid[5];
1763 uchar b3;
1764 ushort caid, sid;
1765 struct s_irdeto_quess *ird_row, *head;
1766
1767 memset(cfg->itab, 0, sizeof(cfg->itab));
1768 sprintf(token, "%s%s", cs_confdir, cs_ird);
1769 if (!(fp=fopen(token, "r")))
1770 {
1771 cs_log("can't open file \"%s\" (errno=%d) irdeto guessing not loaded",
1772 token, errno);
1773 return(1);
1774 }
1775 while (fgets(token, sizeof(token), fp))
1776 {
1777 if( strlen(token)<20 ) continue;
1778 for( i=b3=b47=caid=sid=skip=0, ptr=strtok(token, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1779 {
1780 trim(ptr);
1781 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1782 skip=1;
1783 break;
1784 }
1785 switch(i)
1786 {
1787 case 0: b3 = a2i(ptr, 2); break;
1788 case 1: b47 = a2i(ptr, 8); break;
1789 case 2: caid = a2i(ptr, 4); break;
1790 case 3:
1791 for( j=0; j<4; j++ )
1792 zSid[j]=ptr[j];
1793 zSid[4]=0;
1794 sid = a2i(zSid, 4);
1795 break;
1796 }
1797 }
1798 if( !skip )
1799 {
1800 if (!(ird_row=(struct s_irdeto_quess*)malloc(sizeof(struct s_irdeto_quess))))
1801 {
1802 cs_log("Error allocating memory (errno=%d)", errno);
1803 return;
1804 }
1805 ird_row->b47 = b47;
1806 ird_row->caid = caid;
1807 ird_row->sid = sid;
1808 ird_row->next = 0;
1809
1810 head = cfg->itab[b3];
1811 if( head ) {
1812 while( head->next )
1813 head=head->next;
1814 head->next=ird_row;
1815 }
1816 else
1817 cfg->itab[b3]=ird_row;
1818
1819 //cs_debug("%02X:%08X:%04X:%04X", b3, b47, caid, sid);
1820 }
1821 }
1822 fclose(fp);
1823
1824 for( i=0; i<0xff; i++ )
1825 {
1826 head=cfg->itab[i];
1827 while(head)
1828 {
1829 cs_debug("itab[%02X]: b47=%08X, caid=%04X, sid=%04X",
1830 i, head->b47, head->caid, head->sid);
1831 head=head->next;
1832 }
1833 }
1834 return(0);
1835}
1836*/
1837
1838#ifdef CS_ANTICASC
1839void init_ac()
1840{
1841 int nr;
1842 FILE *fp;
1843 //char *value;
1844
1845 sprintf(token, "%s%s", cs_confdir, cs_ac);
1846 if (!(fp=fopen(token, "r")))
1847 {
1848 cs_log("can't open file \"%s\" (errno=%d) anti-cascading table not loaded",
1849 token, errno);
1850 return;
1851 }
1852
1853 for(nr=0; fgets(token, sizeof(token), fp);)
1854 {
1855 int i, skip;
1856 ushort caid, sid, chid, dwtime;
1857 ulong provid;
1858 char *ptr, *ptr1;
1859 struct s_cpmap *ptr_cpmap;
1860 static struct s_cpmap *cpmap=(struct s_cpmap *)0;
1861
1862 if( strlen(token)<4 ) continue;
1863
1864 caid=sid=chid=dwtime=0;
1865 provid=0;
1866 skip=0;
1867 ptr1=0;
1868 for( i=0, ptr=strtok(token, "="); (i<2)&&(ptr); ptr=strtok(NULL, "="), i++ )
1869 {
1870 trim(ptr);
1871 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1872 skip=1;
1873 break;
1874 }
1875 switch( i )
1876 {
1877 case 0:
1878 ptr1=ptr;
1879 break;
1880 case 1:
1881 dwtime = atoi(ptr);
1882 break;
1883 }
1884 }
1885
1886 if( !skip )
1887 {
1888 for( i=0, ptr=strtok(ptr1, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1889 {
1890 trim(ptr);
1891 switch( i )
1892 {
1893 case 0:
1894 if( *ptr=='*' ) caid = 0;
1895 else caid = a2i(ptr, 4);
1896 break;
1897 case 1:
1898 if( *ptr=='*' ) provid = 0;
1899 else provid = a2i(ptr, 6);
1900 break;
1901 case 2:
1902 if( *ptr=='*' ) sid = 0;
1903 else sid = a2i(ptr, 4);
1904 break;
1905 case 3:
1906 if( *ptr=='*' ) chid = 0;
1907 else chid = a2i(ptr, 4);
1908 break;
1909 }
1910 }
1911 if (!(ptr_cpmap=(struct s_cpmap*)malloc(sizeof(struct s_cpmap))))
1912 {
1913 cs_log("Error allocating memory (errno=%d)", errno);
1914 return;
1915 }
1916 if( cpmap )
1917 cpmap->next=ptr_cpmap;
1918 else
1919 cfg->cpmap=ptr_cpmap;
1920 cpmap=ptr_cpmap;
1921
1922 cpmap->caid = caid;
1923 cpmap->provid = provid;
1924 cpmap->sid = sid;
1925 cpmap->chid = chid;
1926 cpmap->dwtime = dwtime;
1927 cpmap->next = 0;
1928
1929 cs_debug("nr=%d, caid=%04X, provid=%06X, sid=%04X, chid=%04X, dwtime=%d",
1930 nr, caid, provid, sid, chid, dwtime);
1931 nr++;
1932 }
1933 }
1934 fclose(fp);
1935 //cs_log("%d lengths for caid guessing loaded", nr);
1936 return;
1937}
1938#endif
1939
1940/*
1941 * makes a char ready to write a token into config or webIf
1942 */
1943char *mk_t_caidtab(CAIDTAB *ctab){
1944 int i = 0, needed = 1, pos = 0;
1945 while(ctab->caid[i]){
1946 if(ctab->mask[i]) needed += 10;
1947 else needed += 5;
1948 if(ctab->cmap[i]) needed += 5;
1949 ++i;
1950 }
1951 char *value = (char *) malloc(needed * sizeof(char));
1952 i = 0;
1953 while(ctab->caid[i]) {
1954 if(i == 0) {
1955 sprintf(value + pos, "%04X", ctab->caid[i]);
1956 pos += 4;
1957 } else {
1958 sprintf(value + pos, ",%04X", ctab->caid[i]);
1959 pos += 5;
1960 }
1961 if(ctab->mask[i]){
1962 sprintf(value + pos, "&%04X", ctab->mask[i]);
1963 pos += 5;
1964 }
1965 if(ctab->cmap[i]){
1966 sprintf(value + pos, ":%04X", ctab->cmap[i]);
1967 pos += 5;
1968 }
1969 ++i;
1970 }
1971 value[pos] = '\0';
1972 return value;
1973}
1974
1975/*
1976 * makes a char ready to write a token into config or webIf
1977 */
1978char *mk_t_tuntab(TUNTAB *ttab){
1979 int i = 0, needed = 1, pos = 0;
1980 while(ttab->bt_caidfrom[i]){
1981 if(ttab->bt_srvid[i]) needed += 10;
1982 else needed += 5;
1983 if(ttab->bt_caidto[i]) needed += 5;
1984 ++i;
1985 }
1986 char *value = (char *) malloc(needed * sizeof(char));
1987 i = 0;
1988 while(ttab->bt_caidfrom[i]) {
1989 if(i == 0) {
1990 sprintf(value + pos, "%04X", ttab->bt_caidfrom[i]);
1991 pos += 4;
1992 } else {
1993 sprintf(value + pos, ",%04X", ttab->bt_caidfrom[i]);
1994 pos += 5;
1995 }
1996 if(ttab->bt_srvid[i]){
1997 sprintf(value + pos, ".%04X", ttab->bt_srvid[i]);
1998 pos += 5;
1999 }
2000 if(ttab->bt_caidto[i]){
2001 sprintf(value + pos, ":%04X", ttab->bt_caidto[i]);
2002 pos += 5;
2003 }
2004 ++i;
2005 }
2006 value[pos] = '\0';
2007 return value;
2008}
2009
2010/*
2011 * makes a char ready to write a token into config or webIf
2012 */
2013char *mk_t_group(ulong *grp){
2014 int i = 0, needed = 1, pos = 0, dot = 0;
2015 char grpbit[33];
2016 long2bitchar((long) grp, grpbit);
2017
2018 for(i = 0; i < 32; i++){
2019 if (grpbit[i] == '1'){
2020 needed += 2;
2021 if(i > 9) needed += 1;
2022 }
2023 }
2024 char *value = (char *) malloc(needed * sizeof(char));
2025
2026 for(i = 0; i < 32; i++){
2027 if (grpbit[i] == '1'){
2028 if (dot == 0){
2029 sprintf(value + pos, "%d", i+1);
2030 if (i > 9)pos += 2;
2031 else pos += 1;
2032 dot = 1;
2033 } else {
2034 sprintf(value + pos, ",%d", i+1);
2035 if (i > 9)pos += 3;
2036 else pos += 2;
2037 }
2038 }
2039 }
2040 value[pos] = '\0';
2041 return value;
2042}
2043
2044/*
2045 * makes a char ready to write a token into config or webIf
2046 */
2047char *mk_t_ftab(FTAB *ftab){
2048 int i = 0, j = 0, needed = 1, pos = 0;
2049
2050 needed = ftab->nfilts * 5;
2051 for (i = 0; i < ftab->nfilts; ++i)
2052 needed += ftab->filts[i].nprids * 7;
2053
2054 char *value = (char *) malloc(needed * sizeof(char));
2055
2056 char *dot="";
2057 for (i = 0; i < ftab->nfilts; ++i){
2058 sprintf(value + pos, "%s%04X", dot, ftab->filts[i].caid);
2059 pos += 4;
2060 if (i > 0) pos += 1;
2061 dot=":";
2062 for (j = 0; j < ftab->filts[i].nprids; ++j) {
2063 sprintf(value + pos, "%s%06lX", dot, ftab->filts[i].prids[j]);
2064 pos += 7;
2065 dot=",";
2066 }
2067 dot=";";
2068 }
2069
2070 value[pos] = '\0';
2071 return value;
2072}
Note: See TracBrowser for help on using the repository browser.