source: trunk/oscam-config.c@ 1237

Last change on this file since 1237 was 1237, checked in by landlord, 11 years ago

New 'oscam.srvid' format introduced with example and documentation, backport of 'monitor-improvement' branch

File size: 43.7 KB
Line 
1#include "globals.h"
2#ifdef CS_WITH_BOXKEYS
3# include "oscam-boxkeys.np"
4#endif
5
6static char *cs_conf="oscam.conf";
7static char *cs_user="oscam.user";
8static char *cs_srvr="oscam.server";
9static char *cs_srid="oscam.srvid";
10static char *cs_l4ca="oscam.guess";
11static char *cs_cert="oscam.cert";
12static char *cs_sidt="oscam.services";
13//static char *cs_ird="oscam.ird";
14#ifdef CS_ANTICASC
15static char *cs_ac="oscam.ac";
16#endif
17
18static char token[4096];
19
20typedef enum cs_proto_type
21{
22 TAG_GLOBAL, // must be first !
23 TAG_MONITOR, // monitor
24 TAG_CAMD33, // camd 3.3x
25 TAG_CAMD35, // camd 3.5x UDP
26 TAG_NEWCAMD, // newcamd
27 TAG_RADEGAST, // radegast
28 TAG_SERIAL, // serial (static)
29 TAG_CS357X, // camd 3.5x UDP
30 TAG_CS378X, // camd 3.5x TCP
31#ifdef CS_WITH_GBOX
32 TAG_GBOX, // gbox
33#endif
34 TAG_CCCAM, // cccam
35 TAG_DVBAPI
36#ifdef CS_ANTICASC
37 ,TAG_ANTICASC // anti-cascading
38#endif
39} cs_proto_type_t;
40
41static char *cctag[]={"global", "monitor", "camd33", "camd35",
42 "newcamd", "radegast", "serial", "cs357x", "cs378x",
43#ifdef CS_WITH_GBOX
44 "gbox",
45#endif
46 "cccam", "dvbapi",
47#ifdef CS_ANTICASC
48 "anticasc",
49#endif
50 NULL};
51
52#ifdef DEBUG_SIDTAB
53static void show_sidtab(struct s_sidtab *sidtab)
54{
55 for (; sidtab; sidtab=sidtab->next)
56 {
57 int i;
58 char buf[1024];
59 cs_log("label=%s", sidtab->label);
60 sprintf(buf, "caid(%d)=", sidtab->num_caid);
61 for (i=0; i<sidtab->num_caid; i++)
62 sprintf(buf+strlen(buf), "%04X ", sidtab->caid[i]);
63 cs_log("%s", buf);
64 sprintf(buf, "provider(%d)=", sidtab->num_provid);
65 for (i=0; i<sidtab->num_provid; i++)
66 sprintf(buf+strlen(buf), "%08X ", sidtab->provid[i]);
67 cs_log("%s", buf);
68 sprintf(buf, "services(%d)=", sidtab->num_srvid);
69 for (i=0; i<sidtab->num_srvid; i++)
70 sprintf(buf+strlen(buf), "%04X ", sidtab->srvid[i]);
71 cs_log("%s", buf);
72 }
73}
74#endif
75
76void chk_iprange(char *value, struct s_ip **base)
77{
78 int i = 0;
79 char *ptr1, *ptr2;
80 struct s_ip *lip, *cip;
81
82 for (cip=lip=*base; cip; cip=cip->next)
83 lip=cip;
84 if (!(cip=malloc(sizeof(struct s_ip))))
85 {
86 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
87 exit(1);
88 }
89 if (*base)
90 lip->next=cip;
91 else
92 *base=cip;
93
94 memset(cip, 0, sizeof(struct s_ip));
95 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
96 {
97 if (i == 0) ++i;
98 else {
99 if (!(cip=malloc(sizeof(struct s_ip)))){
100 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno);
101 exit(1);
102 }
103 lip->next = cip;
104 memset(cip, 0, sizeof(struct s_ip));
105 }
106 if( (ptr2=strchr(trim(ptr1), '-')) )
107 {
108 *ptr2++='\0';
109 cip->ip[0]=cs_inet_addr(trim(ptr1));
110 cip->ip[1]=cs_inet_addr(trim(ptr2));
111 }
112 else
113 cip->ip[0]=cip->ip[1]=cs_inet_addr(ptr1);
114 lip = cip;
115 }
116}
117
118static void chk_caidtab(char *caidasc, CAIDTAB *ctab)
119{
120 int i;
121 char *ptr1, *ptr2, *ptr3;
122 for (i=0, ptr1=strtok(caidasc, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
123 {
124 ulong caid, mask, cmap;
125 if( (ptr3=strchr(trim(ptr1), ':')) )
126 *ptr3++='\0';
127 else
128 ptr3="";
129 if( (ptr2=strchr(trim(ptr1), '&')) )
130 *ptr2++='\0';
131 else
132 ptr2="";
133 if (((caid=a2i(ptr1, 2))|(mask=a2i(ptr2,-2))|(cmap=a2i(ptr3, 2))) < 0x10000)
134 {
135 ctab->caid[i]=caid;
136 ctab->mask[i]=mask;
137 ctab->cmap[i++]=cmap;
138 }
139// else
140// cs_log("WARNING: wrong CAID in %s -> ignored", cs_user);
141 }
142}
143
144static void chk_tuntab(char *tunasc, TUNTAB *ttab)
145{
146 int i;
147 char *ptr1, *ptr2, *ptr3;
148 for (i=0, ptr1=strtok(tunasc, ","); (i<CS_MAXTUNTAB) && (ptr1); ptr1=strtok(NULL, ","))
149 {
150 ulong bt_caidfrom, bt_caidto, bt_srvid;
151 if( (ptr3=strchr(trim(ptr1), ':')) )
152 *ptr3++='\0';
153 else
154 ptr3="";
155 if( (ptr2=strchr(trim(ptr1), '.')) )
156 *ptr2++='\0';
157 else
158 ptr2="";
159 if ((bt_caidfrom=a2i(ptr1, 2))|(bt_srvid=a2i(ptr2,-2))|(bt_caidto=a2i(ptr3, 2)))
160 {
161 ttab->bt_caidfrom[i]=bt_caidfrom;
162 ttab->bt_caidto[i]=bt_caidto;
163 ttab->bt_srvid[i++]=bt_srvid;
164 }
165// else
166// cs_log("WARNING: wrong Betatunnel in %s -> ignored", cs_user);
167 }
168}
169
170static void chk_services(char *labels, ulong *sidok, ulong *sidno)
171{
172 int i;
173 char *ptr;
174 SIDTAB *sidtab;
175 *sidok=*sidno=0;
176 for (ptr=strtok(labels, ","); ptr; ptr=strtok(NULL, ","))
177 for (trim(ptr), i=0, sidtab=cfg->sidtab; sidtab; sidtab=sidtab->next, i++)
178 {
179 if (!strcmp(sidtab->label, ptr)) *sidok|=(1<<i);
180 if ((ptr[0]=='!') && (!strcmp(sidtab->label, ptr+1))) *sidno|=(1<<i);
181 }
182}
183
184static
185void chk_ftab(char *zFilterAsc, FTAB *ftab, const char *zType, const char *zName,
186 const char *zFiltName)
187{
188 int i,j;
189 char *ptr1,*ptr2,*ptr3;
190 char *ptr[CS_MAXFILTERS] = {0};
191
192 memset(ftab, 0, sizeof(FTAB));
193 for( i=0, ptr1=strtok(zFilterAsc, ";"); (i<CS_MAXFILTERS) && (ptr1); ptr1=strtok(NULL, ";"), i++ )
194 {
195 //cs_log("ptr1=%s", ptr1);
196 ptr[i] = ptr1;
197 if( (ptr2=strchr(trim(ptr1), ':')) )
198 {
199 //cs_log("ptr2=%s", ptr2);
200 *ptr2++='\0';
201 //cs_log("ptr2=%s", ptr2);
202 ftab->filts[i].caid = (ushort)a2i(ptr1, 4);
203 //cs_log("caid=%04X", ftab->filts[i].caid);
204 ptr[i] = ptr2;
205 }
206 else if (zFiltName && zFiltName[0]=='c')
207 {
208 cs_log("PANIC: CAID field not found in CHID parameter!");
209 cs_exit(1);
210 }
211 ftab->nfilts++;
212 }
213
214 if( ftab->nfilts ) cs_debug("%s '%s' %s filter(s):", zType, zName, zFiltName);
215 for( i=0; i<ftab->nfilts; i++ )
216 {
217 cs_debug("CAID #%d: %04X", i, ftab->filts[i].caid);
218 for( j=0, ptr3=strtok(ptr[i], ","); (j<CS_MAXPROV) && (ptr3); ptr3=strtok(NULL, ","), j++ )
219 {
220 ftab->filts[i].prids[j] = a2i(ptr3,6);
221 ftab->filts[i].nprids++;
222 cs_debug("%s #%d: %06X", zFiltName, j, ftab->filts[i].prids[j]);
223 }
224 }
225 //cs_log("exit chk_ftab");
226}
227
228static void chk_cltab(char *classasc, CLASSTAB *clstab)
229{
230 int i;
231 char *ptr1;
232 for( i=0, ptr1=strtok(classasc, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ",") )
233 {
234 ptr1=trim(ptr1);
235 if( ptr1[0] == '!' )
236 clstab->bclass[clstab->bn++] = (uchar)a2i(ptr1+1, 2);
237 else
238 clstab->aclass[clstab->an++] = (uchar)a2i(ptr1, 2);
239 }
240}
241
242static void chk_port_tab(char *portasc, PTAB *ptab)
243{
244 int i,j,nfilts,ifilt,iport;
245 char *ptr1,*ptr2,*ptr3;
246 char *ptr[CS_MAXPORTS] = {0};
247 int port[CS_MAXPORTS] = {0};
248 int previous_nports = ptab->nports;
249
250 for (nfilts=i=previous_nports, ptr1=strtok(portasc, ";"); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ";"), i++)
251 {
252 ptr[i] = ptr1;
253 if( (ptr2=strchr(trim(ptr1), '@')) )
254 {
255 *ptr2++='\0';
256 ptab->ports[i].s_port = atoi(ptr1);
257 ptr[i] = ptr2;
258 port[i] = ptab->ports[i].s_port;
259 ptab->nports++;
260 }
261 nfilts++;
262 }
263
264 if( nfilts==1 && strlen(portasc)<6 && ptab->ports[0].s_port == 0 ) {
265 ptab->ports[0].s_port = atoi(portasc);
266 ptab->nports = 1;
267 }
268
269 iport=ifilt = previous_nports;
270 for (i=previous_nports; i<nfilts; i++)
271 {
272 if( port[i]!=0 ) iport = i;
273 for (j=0, ptr3=strtok(ptr[i], ","); (j<CS_MAXPROV) && (ptr3); ptr3=strtok(NULL, ","), j++)
274 {
275 if( (ptr2=strchr(trim(ptr3), ':')) )
276 {
277 *ptr2++='\0';
278 ptab->ports[iport].ftab.nfilts++;
279 ifilt = ptab->ports[iport].ftab.nfilts-1;
280 ptab->ports[iport].ftab.filts[ifilt].caid = (ushort)a2i(ptr3, 4);
281 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr2, 6);
282 } else {
283 ptab->ports[iport].ftab.filts[ifilt].prids[j] = a2i(ptr3, 6);
284 }
285 ptab->ports[iport].ftab.filts[ifilt].nprids++;
286 }
287 }
288}
289
290#ifdef NOTUSED
291static void chk_srvip(char *value, in_addr_t *ip)
292{
293 int i;
294 char *ptr;
295 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
296 if (i<8) ip[i++]=inet_addr(ptr);
297}
298#endif
299
300static void chk_t_global(char *token, char *value)
301{
302 if (!strcmp(token, "serverip")) { cfg->srvip=inet_addr(value); return; }
303 if (!strcmp(token, "logfile")) { strncpy(logfile, value, sizeof(logfile)-1); return; }
304 if (!strcmp(token, "pidfile")) { strncpy(cfg->pidfile, value, sizeof(cfg->pidfile)-1); return; }
305 if (!strcmp(token, "usrfile")) { strncpy(cfg->usrfile, value, sizeof(cfg->usrfile)-1); return; }
306 if (!strcmp(token, "cwlogdir")) { strncpy(cfg->cwlogdir, value, sizeof(cfg->cwlogdir)-1); return; }
307 if (!strcmp(token, "clienttimeout"))
308 {
309 cfg->ctimeout = atoi(value);
310 if (cfg->ctimeout < 100)
311 cfg->ctimeout *= 1000;
312 return;
313 }
314 if (!strcmp(token, "fallbacktimeout"))
315 {
316 cfg->ftimeout = atoi(value);
317 if (cfg->ftimeout < 100)
318 cfg->ftimeout *= 1000;
319 return;
320 }
321
322 if (!strcmp(token, "clientmaxidle")) { cfg->cmaxidle=atoi(value); return; }
323 if (!strcmp(token, "cachedelay")) { cfg->delay=atoi(value); return; }
324 if (!strcmp(token, "bindwait")) { cfg->bindwait=atoi(value); return; }
325 if (!strcmp(token, "netprio")) { cfg->netprio=atoi(value); return; }
326 if (!strcmp(token, "resolvedelay")) { cfg->resolvedelay=atoi(value); return; }
327 if (!strcmp(token, "sleep")) { cfg->tosleep=atoi(value); return; }
328 if (!strcmp(token, "unlockparental")) { cfg->ulparent=atoi(value); return; }
329 if (!strcmp(token, "nice"))
330 {
331 cfg->nice=atoi(value);
332 if ((cfg->nice<-20) || (cfg->nice>20)) cfg->nice=99;
333 if (cfg->nice!=99) cs_setpriority(cfg->nice); // ignore errors
334 return;
335 }
336 if (!strcmp(token, "serialreadertimeout"))
337 {
338 if (cfg->srtimeout < 100)
339 cfg->srtimeout = atoi(value) * 1000;
340 else
341 cfg->srtimeout = atoi(value);
342 if( cfg->srtimeout <=0 )
343 cfg->srtimeout=1500;
344 return;
345 }
346 if (!strcmp(token, "maxlogsize"))
347 {
348 cfg->max_log_size=atoi(value);
349 if( cfg->max_log_size <=10 )
350 cfg->max_log_size=10;
351 return;
352 }
353 if( !strcmp(token, "showecmdw")) { cfg->show_ecm_dw = atoi(value); return; }
354 if( !strcmp(token, "waitforcards")) { cfg->waitforcards = atoi(value); return; }
355 if( !strcmp(token, "preferlocalcards")) { cfg->preferlocalcards = atoi(value); return; }
356 if (token[0] != '#')
357 fprintf(stderr, "Warning: keyword '%s' in global section not recognized\n",token);
358}
359
360#ifdef CS_ANTICASC
361static void chk_t_ac(char *token, char *value)
362{
363 if (!strcmp(token, "enabled"))
364 {
365 cfg->ac_enabled=atoi(value);
366 if( cfg->ac_enabled<=0 ) cfg->ac_enabled=0;
367 else cfg->ac_enabled=1;
368 return;
369 }
370
371 if (!strcmp(token, "numusers"))
372 {
373 cfg->ac_users=atoi(value);
374 if( cfg->ac_users<0 ) cfg->ac_users=0;
375 return;
376 }
377 if (!strcmp(token, "sampletime"))
378 {
379 cfg->ac_stime=atoi(value);
380 if( cfg->ac_stime<0 ) cfg->ac_stime=2;
381 return;
382 }
383 if (!strcmp(token, "samples"))
384 {
385 cfg->ac_samples=atoi(value);
386 if( cfg->ac_samples<2 || cfg->ac_samples>10) cfg->ac_samples=10;
387 return;
388 }
389 if (!strcmp(token, "penalty"))
390 {
391 cfg->ac_penalty=atoi(value);
392 if( cfg->ac_penalty<0 ) cfg->ac_penalty=0;
393 return;
394 }
395 if (!strcmp(token, "aclogfile"))
396 {
397 strncpy(cfg->ac_logfile, value, sizeof(cfg->ac_logfile)-1);
398 return;
399 }
400 if( !strcmp(token, "fakedelay") )
401 {
402 cfg->ac_fakedelay=atoi(value);
403 if( cfg->ac_fakedelay<100 || cfg->ac_fakedelay>1000 )
404 cfg->ac_fakedelay=1000;
405 return;
406 }
407 if( !strcmp(token, "denysamples") )
408 {
409 cfg->ac_denysamples=atoi(value);
410 if( cfg->ac_denysamples<2 || cfg->ac_denysamples>cfg->ac_samples-1 )
411 cfg->ac_denysamples=cfg->ac_samples-1;
412 return;
413 }
414 if (token[0] != '#')
415 fprintf(stderr, "Warning: keyword '%s' in anticascading section not recognized\n",token);
416//#endif moved this endif up two lines, I think this was erroneous - dingo35
417}
418#endif
419
420static void chk_t_monitor(char *token, char *value)
421{
422 if (!strcmp(token, "port")) { cfg->mon_port=atoi(value); return; }
423 if (!strcmp(token, "serverip")) { cfg->mon_srvip=inet_addr(value); return; }
424 if (!strcmp(token, "nocrypt")) { chk_iprange(value, &cfg->mon_allowed); return; }
425 if (!strcmp(token, "aulow")) { cfg->mon_aulow=atoi(value); return; }
426 if (!strcmp(token, "monlevel")) { cfg->mon_level=atoi(value); return; }
427 if (!strcmp(token, "hideclient_to")) { cfg->mon_hideclient_to=atoi(value); return; }
428 if (token[0] != '#')
429 fprintf(stderr, "Warning: keyword '%s' in monitor section not recognized\n",token);
430}
431
432static void chk_t_camd33(char *token, char *value)
433{
434 if (!strcmp(token, "port")) { cfg->c33_port=atoi(value); return; }
435 if (!strcmp(token, "serverip")) { cfg->c33_srvip=inet_addr(value); return; }
436 if (!strcmp(token, "nocrypt")) { chk_iprange(value, &cfg->c33_plain); return; }
437 if (!strcmp(token, "passive")) { cfg->c33_passive=(value[0]!='0'); return; }
438 if (!strcmp(token, "key"))
439 {
440 if (key_atob(value, cfg->c33_key))
441 {
442 fprintf(stderr, "Configuration camd3.3x: Error in Key\n");
443 exit(1);
444 }
445 cfg->c33_crypted=1;
446 return;
447 }
448 if (token[0] != '#')
449 fprintf(stderr, "Warning: keyword '%s' in camd33 section not recognized\n",token);
450}
451
452static void chk_t_camd35(char *token, char *value)
453{
454 if (!strcmp(token, "port")) { cfg->c35_port=atoi(value); return; }
455 if (!strcmp(token, "serverip")) { cfg->c35_tcp_srvip=inet_addr(value); return; }
456 if (token[0] != '#')
457 fprintf(stderr, "Warning: keyword '%s' in camd35 section not recognized\n",token);
458}
459
460static void chk_t_camd35_tcp(char *token, char *value)
461{
462 if (!strcmp(token, "port")) { chk_port_tab(value, &cfg->c35_tcp_ptab); return; }
463 if (!strcmp(token, "serverip")) { cfg->c35_tcp_srvip=inet_addr(value); return; }
464 if (token[0] != '#')
465 fprintf(stderr, "Warning: keyword '%s' in camd35 tcp section not recognized\n",token);
466}
467
468static void chk_t_newcamd(char *token, char *value)
469{
470 if (!strcmp(token, "port")) { chk_port_tab(value, &cfg->ncd_ptab); return; }
471 if (!strcmp(token, "serverip")) { cfg->ncd_srvip=inet_addr(value); return; }
472 if (!strcmp(token, "key"))
473 {
474 if (key_atob14(value, cfg->ncd_key))
475 {
476 fprintf(stderr, "Configuration newcamd: Error in Key\n");
477 exit(1);
478 }
479 return;
480 }
481 if (token[0] != '#')
482 fprintf(stderr, "Warning: keyword '%s' in newcamd section not recognized\n",token);
483}
484
485static void chk_t_radegast(char *token, char *value)
486{
487 if (!strcmp(token, "port")) { cfg->rad_port=atoi(value); return; }
488 if (!strcmp(token, "serverip")) { cfg->rad_srvip=inet_addr(value); return; }
489 if (!strcmp(token, "allowed")) { chk_iprange(value, &cfg->rad_allowed); return; }
490 if (!strcmp(token, "user")) { strncpy(cfg->rad_usr, value, sizeof(cfg->rad_usr)-1); return; }
491 if (token[0] != '#')
492 fprintf(stderr, "Warning: keyword '%s' in radegast section not recognized\n",token);
493}
494
495static void chk_t_serial(char *token, char *value)
496{
497 if (!strcmp(token, "device"))
498 {
499 int l;
500 l=strlen(cfg->ser_device);
501 if (l) cfg->ser_device[l++]=1; // use ctrl-a as delimiter
502 strncpy(cfg->ser_device+l, value, sizeof(cfg->ser_device)-1-l);
503 return;
504 }
505 if (token[0] != '#')
506 fprintf(stderr, "Warning: keyword '%s' in serial section not recognized\n",token);
507}
508
509#ifdef CS_WITH_GBOX
510static void chk_t_gbox(char *token, char *value)
511{
512// if (!strcmp(token, "password")) strncpy(cfg->gbox_pwd, i2b(4, a2i(value, 4)), 4);
513 if (!strcmp(token, "password")) { cs_atob(cfg->gbox_pwd, value, 4); return; }
514 if (!strcmp(token, "maxdist")) { cfg->maxdist=atoi(value); return; }
515 if (!strcmp(token, "ignorelist")) { strncpy((char *)cfg->ignorefile, value, sizeof(cfg->ignorefile)-1); return; }
516 if (!strcmp(token, "onlineinfos")) { strncpy((char *)cfg->gbxShareOnl, value, sizeof(cfg->gbxShareOnl)-1); return; }
517 if (!strcmp(token, "cardinfos")) { strncpy((char *)cfg->cardfile, value, sizeof(cfg->cardfile)-1); return; }
518 if (!strcmp(token, "locals"))
519 {
520 char *ptr1;
521 int n=0, i;
522 for (i=0, ptr1=strtok(value, ","); (i<CS_MAXLOCALS) && (ptr1); ptr1=strtok(NULL, ","))
523 {
524 cfg->locals[n++]=a2i(ptr1, 8);
525 //printf("%i %08X",n,cfg->locals[n-1]);
526 }
527 cfg->num_locals=n;
528 return;
529 }
530 if (token[0] != '#')
531 fprintf(stderr, "Warning: keyword '%s' in gbox section not recognized\n",token);
532}
533#endif
534
535static void chk_t_cccam(char *token, char *value)
536{
537 // placeholder for ccam server support
538 fprintf(stderr, "Warning: OSCam have no cccam server support yet. Parametr %s = %s\n", token, value);
539}
540
541#ifdef HAVE_DVBAPI
542static void chk_t_dvbapi(char *token, char *value)
543{
544 if (!strcmp(token, "enabled")) { cfg->dvbapi_enabled=atoi(value); return; }
545 if (!strcmp(token, "au")) { cfg->dvbapi_au=atoi(value); return; }
546 if (!strcmp(token, "boxtype")) { strncpy(cfg->dvbapi_boxtype, value, sizeof(cfg->dvbapi_boxtype)-1); return; }
547 if (!strcmp(token, "user")) { strncpy(cfg->dvbapi_usr, value, sizeof(cfg->dvbapi_usr)-1); return; }
548 if (!strcmp(token, "priority")) { strncpy(cfg->dvbapi_priority, value, sizeof(cfg->dvbapi_priority)-1); return; }
549 if (!strcmp(token, "ignore")) { strncpy(cfg->dvbapi_ignore, value, sizeof(cfg->dvbapi_ignore)-1); return; }
550
551 if (token[0] != '#')
552 fprintf(stderr, "Warning: keyword '%s' in dvbapi section not recognized\n",token);
553}
554#endif
555
556static void chk_token(char *token, char *value, int tag)
557{
558 switch(tag)
559 {
560 case TAG_GLOBAL : chk_t_global(token, value); break;
561 case TAG_MONITOR : chk_t_monitor(token, value); break;
562 case TAG_CAMD33 : chk_t_camd33(token, value); break;
563 case TAG_CAMD35 :
564 case TAG_CS357X : chk_t_camd35(token, value); break;
565 case TAG_NEWCAMD : chk_t_newcamd(token, value); break;
566 case TAG_RADEGAST: chk_t_radegast(token, value); break;
567 case TAG_SERIAL : chk_t_serial(token, value); break;
568 case TAG_CS378X : chk_t_camd35_tcp(token, value); break;
569#ifdef CS_WITH_GBOX
570 case TAG_GBOX : chk_t_gbox(token, value); break;
571#endif
572 case TAG_CCCAM : chk_t_cccam(token, value); break;
573#ifdef HAVE_DVBAPI
574 case TAG_DVBAPI : chk_t_dvbapi(token, value); break;
575#else
576 case TAG_DVBAPI : fprintf(stderr, "Warning: OSCam compiled without DVB API support.\n"); break;
577#endif
578#ifdef CS_ANTICASC
579 case TAG_ANTICASC: chk_t_ac(token, value); break;
580#endif
581 }
582}
583
584void init_len4caid()
585{
586 int nr;
587 FILE *fp;
588 char *value;
589
590 memset(len4caid, 0, sizeof(ushort)<<8);
591 sprintf(token, "%s%s", cs_confdir, cs_l4ca);
592 if (!(fp=fopen(token, "r")))
593 return;
594 for(nr=0; fgets(token, sizeof(token), fp);)
595 {
596 int i, c;
597 char *ptr;
598 if (!(value=strchr(token, ':'))) continue;
599 *value++='\0';
600 if( (ptr=strchr(value, '#')) )
601 *ptr='\0';
602 if (strlen(trim(token))!=2) continue;
603 if (strlen(trim(value))!=4) continue;
604 if ((i=byte_atob(token))<0) continue;
605 if ((c=word_atob(value))<0) continue;
606//printf("idx %02X = %04X\n", i, c); fflush(stdout);
607 len4caid[i]=c;
608 nr++;
609 }
610 fclose(fp);
611 cs_log("%d lengths for caid guessing loaded", nr);
612 return;
613}
614
615int search_boxkey(ushort caid, char *key)
616{
617 int i, rc=0;
618 FILE *fp;
619 char c_caid[512];
620
621 sprintf(c_caid, "%s%s", cs_confdir, cs_cert);
622 fp=fopen(c_caid, "r");
623 if (fp)
624 {
625 for (; (!rc) && fgets(c_caid, sizeof(c_caid), fp);)
626 {
627 char *c_provid, *c_key;
628
629 c_provid=strchr(c_caid, '#');
630 if (c_provid) *c_provid='\0';
631 if (!(c_provid=strchr(c_caid, ':'))) continue;
632 *c_provid++='\0';
633 if (!(c_key=strchr(c_provid, ':'))) continue;
634 *c_key++='\0';
635 if (word_atob(trim(c_caid))!=caid) continue;
636 if ((i=(strlen(trim(c_key))>>1))>256) continue;
637 if (cs_atob((uchar *)key, c_key, i)<0)
638 {
639 cs_log("wrong key in \"%s\"", cs_cert);
640 continue;
641 }
642 rc=1;
643 }
644 fclose(fp);
645 }
646#ifdef OSCAM_INBUILD_KEYS
647 for(i=0; (!rc) && (npkey[i].keylen); i++)
648 if (rc=((caid==npkey[i].caid) && (npkey[i].provid==0)))
649 memcpy(key, npkey[i].key, npkey[i].keylen);
650#endif
651 return(rc);
652}
653
654int init_config()
655{
656 int tag=TAG_GLOBAL;
657 FILE *fp;
658 char *value;
659
660#ifndef CS_EMBEDDED
661#ifdef PRIO_PROCESS
662 errno=0;
663 if ((cfg->nice=getpriority(PRIO_PROCESS, 0))==(-1))
664 if (errno)
665#endif
666#endif
667 cfg->nice=99;
668 cfg->ctimeout=CS_CLIENT_TIMEOUT;
669 cfg->ftimeout=CS_CLIENT_TIMEOUT / 2;
670 cfg->cmaxidle=CS_CLIENT_MAXIDLE;
671 cfg->delay=CS_DELAY;
672 cfg->bindwait=CS_BIND_TIMEOUT;
673 cfg->resolvedelay=CS_RESOLVE_DELAY;
674 cfg->mon_level=2;
675 cfg->mon_hideclient_to=0;
676 cfg->srtimeout=1500;
677 cfg->ulparent=0;
678#ifdef CS_ANTICASC
679 cfg->ac_enabled=0;
680 cfg->ac_users=0;
681 cfg->ac_stime=2;
682 cfg->ac_samples=10;
683 cfg->ac_denysamples=8;
684 cfg->ac_fakedelay=1000;
685 strcpy(cfg->ac_logfile, "./oscam_ac.log");
686#endif
687 sprintf(token, "%s%s", cs_confdir, cs_conf);
688 if (!(fp=fopen(token, "r")))
689 {
690 fprintf(stderr, "Cannot open config file '%s' (errno=%d)\n", token, errno);
691 exit(1);
692 }
693 while (fgets(token, sizeof(token), fp))
694 {
695 int i, l;
696 //void *ptr;
697 if ((l=strlen(trim(token)))<3) continue;
698 if ((token[0]=='[') && (token[l-1]==']'))
699 {
700 for (token[l-1]=0, tag=-1, i=TAG_GLOBAL; cctag[i]; i++)
701 if (!strcmp(cctag[i], strtolower(token+1)))
702 tag=i;
703 continue;
704 }
705 if (!(value=strchr(token, '='))) continue;
706 *value++='\0';
707 chk_token(trim(strtolower(token)), trim(value), tag);
708 }
709 fclose(fp);
710 cs_init_log(logfile);
711 if (cfg->ftimeout>=cfg->ctimeout)
712 {
713 cfg->ftimeout = cfg->ctimeout - 100;
714 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be smaller than clienttimeout (%lu ms))", cfg->ftimeout, cfg->ctimeout);
715 }
716 if(cfg->ftimeout < cfg->srtimeout)
717 {
718 cfg->ftimeout = cfg->srtimeout + 100;
719 cs_log("WARNING: fallbacktimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ftimeout, cfg->srtimeout);
720 }
721 if(cfg->ctimeout < cfg->srtimeout)
722 {
723 cfg->ctimeout = cfg->srtimeout + 100;
724 cs_log("WARNING: clienttimeout adjusted to %lu ms (must be greater than serialreadertimeout (%lu ms))", cfg->ctimeout, cfg->srtimeout);
725 }
726#ifdef CS_ANTICASC
727 if( cfg->ac_denysamples+1>cfg->ac_samples )
728 {
729 cfg->ac_denysamples=cfg->ac_samples-1;
730 cs_log("WARNING: DenySamples adjusted to %d", cfg->ac_denysamples);
731 }
732#endif
733 return 0;
734}
735
736static void chk_account(char *token, char *value, struct s_auth *account)
737{
738 int i;
739 char *ptr1;//, *ptr2;
740 if (!strcmp(token, "user")) { strncpy(account->usr, value, sizeof(account->usr)-1); return; }
741 if (!strcmp(token, "pwd")) { strncpy(account->pwd, value, sizeof(account->pwd)-1); return; }
742 if (!strcmp(token, "hostname")) { strncpy((char *)account->dyndns, value, sizeof(account->dyndns)-1); return; }
743 if (!strcmp(token, "betatunnel")) { chk_tuntab(value, &account->ttab); return; }
744 if (!strcmp(token, "uniq")) { account->uniq=atoi(value); return; }
745 if (!strcmp(token, "sleep")) { account->tosleep=atoi(value); return; }
746 if (!strcmp(token, "monlevel")) { account->monlvl=atoi(value); return; }
747 if (!strcmp(token, "caid")) { chk_caidtab(value, &account->ctab); return; }
748 /*
749 * case insensitive
750 */
751 strtolower(value);
752 if (!strcmp(token, "au"))
753 {
754 if(value && value[0]=='1') account->autoau=1;
755 for (i=0; i<CS_MAXREADER; i++)
756 if ((reader[i].label[0]) &&
757 (!strncmp(reader[i].label, value, strlen(reader[i].label))))
758 account->au=i;
759 return;
760 }
761 if (!strcmp(token, "group"))\
762 {
763 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ","))
764 {
765 int g;
766 g=atoi(ptr1);
767 if ((g>0) && (g<33)) account->grp|=(1<<(g-1));
768 }
769 return;
770 }
771 if(!strcmp(token, "services")) { chk_services(value, &account->sidtabok, &account->sidtabno); return; }
772 if(!strcmp(token, "ident")) { chk_ftab(value, &account->ftab, "user", account->usr, "provid"); return; }
773 if(!strcmp(token, "class")) { chk_cltab(value, &account->cltab); return; }
774 if(!strcmp(token, "chid")) { chk_ftab(value, &account->fchid, "user", account->usr, "chid"); return; }
775
776 if (!strcmp(token, "expdate"))
777 {
778 struct tm cstime;
779 memset(&cstime,0,sizeof(cstime));
780 for (i=0, ptr1=strtok(value, "-/"); (i<3)&&(ptr1); ptr1=strtok(NULL, "-/"), i++)
781 {
782 switch(i)
783 {
784 case 0: cstime.tm_year=atoi(ptr1)-1900; break;
785 case 1: cstime.tm_mon =atoi(ptr1)-1; break;
786 case 2: cstime.tm_mday=atoi(ptr1); break;
787 }
788 }
789 account->expirationdate=mktime(&cstime);
790 return;
791 }
792
793#ifdef CS_ANTICASC
794 if( !strcmp(token, "numusers") )
795 {
796 account->ac_users = atoi(value);
797 return;
798 }
799 if( !strcmp(token, "penalty") )
800 {
801 account->ac_penalty = atoi(value);
802 return;
803 }
804#endif
805 if (token[0] != '#')
806 fprintf(stderr, "Warning: keyword '%s' in account section not recognized\n",token);
807
808// if (!strcmp(token, "caid"))
809// {
810// for (i=0, ptr1=strtok(value, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
811// {
812// ulong caid, mask;
813// if (ptr2=strchr(trim(ptr1), '&'))
814// *ptr2++='\0';
815// else
816// ptr2="";
817// if (((caid=a2i(ptr1, 2))|(mask=a2i(ptr2,-2))) < 0x10000)
818// {
819// account->caidtab[i][0]=caid;
820// account->caidtab[i++][1]=mask;
821// }
822// else
823// cs_log("WARNING: wrong CAID in %s -> ignored", cs_user);
824// }
825// }
826}
827
828int init_userdb()
829{
830 int tag=0, nr, nro, expired;
831 //int first=1;
832 FILE *fp;
833 char *value;
834 struct s_auth *ptr;
835 /*static */struct s_auth *account=(struct s_auth *)0;
836
837 sprintf(token, "%s%s", cs_confdir, cs_user);
838 if (!(fp=fopen(token, "r")))
839 {
840 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
841 return(1);
842 }
843 for (nro=0, ptr=cfg->account; ptr; nro++)
844 {
845 struct s_auth *ptr_next;
846 ptr_next=ptr->next;
847 free(ptr);
848 ptr=ptr_next;
849 }
850 nr=0;
851 while (fgets(token, sizeof(token), fp))
852 {
853 int i, l;
854 void *ptr;
855 if ((l=strlen(trim(token)))<3) continue;
856 if ((token[0]=='[') && (token[l-1]==']'))
857 {
858 token[l-1]=0;
859 tag=(!strcmp("account", strtolower(token+1)));
860 if (!(ptr=malloc(sizeof(struct s_auth))))
861 {
862 cs_log("Error allocating memory (errno=%d)", errno);
863 return(1);
864 }
865 if (account)
866 account->next=ptr;
867 else
868 cfg->account=ptr;
869 account=ptr;
870 memset(account, 0, sizeof(struct s_auth));
871 account->au=(-1);
872 account->monlvl=cfg->mon_level;
873 account->tosleep=cfg->tosleep;
874 for (i=1; i<CS_MAXCAIDTAB; account->ctab.mask[i++]=0xffff);
875 for (i=1; i<CS_MAXTUNTAB; account->ttab.bt_srvid[i++]=0x0000);
876 nr++;
877#ifdef CS_ANTICASC
878 account->ac_users=cfg->ac_users;
879 account->ac_penalty=cfg->ac_penalty;
880 account->ac_idx = nr;
881#endif
882 continue;
883 }
884 if (!tag) continue;
885 if (!(value=strchr(token, '='))) continue;
886 *value++='\0';
887 chk_account(trim(strtolower(token)), trim(value), account);
888 }
889 fclose(fp);
890
891 for (expired=0, ptr=cfg->account; ptr;)
892 {
893 if(ptr->expirationdate && ptr->expirationdate<time(NULL)) expired++;
894 ptr=ptr->next;
895 }
896
897 cs_log("userdb reloaded: %d accounts freed, %d accounts loaded, %d expired", nro, nr, expired);
898 return(0);
899}
900
901static void chk_entry4sidtab(char *value, struct s_sidtab *sidtab, int what)
902{
903 int i, b;
904 char *ptr;
905 ushort *slist=(ushort *) 0;
906 ulong *llist=(ulong *) 0;
907 ulong caid;
908 char buf[strlen(value) + 1];
909 strncpy(buf, value, sizeof(buf));
910 b=(what==1) ? sizeof(ulong) : sizeof(ushort);
911 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
912 {
913 caid=a2i(ptr, b);
914 if (!errno) i++;
915 }
916 //if (!i) return(0);
917 if (b==sizeof(ushort))
918 slist=malloc(i*sizeof(ushort));
919 else
920 llist=malloc(i*sizeof(ulong));
921 strcpy(value, buf);
922 for (i=0, ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
923 {
924 caid=a2i(ptr, b);
925 if (errno) continue;
926 if (b==sizeof(ushort))
927 slist[i++]=(ushort) caid;
928 else
929 llist[i++]=caid;
930 }
931 switch (what)
932 {
933 case 0: sidtab->caid=slist;
934 sidtab->num_caid=i;
935 break;
936 case 1: sidtab->provid=llist;
937 sidtab->num_provid=i;
938 break;
939 case 2: sidtab->srvid=slist;
940 sidtab->num_srvid=i;
941 break;
942 }
943}
944
945static void chk_sidtab(char *token, char *value, struct s_sidtab *sidtab)
946{
947 if (!strcmp(token, "caid")) { chk_entry4sidtab(value, sidtab, 0); return; }
948 if (!strcmp(token, "provid")) { chk_entry4sidtab(value, sidtab, 1); return; }
949 if (!strcmp(token, "ident")) { chk_entry4sidtab(value, sidtab, 1); return; }
950 if (!strcmp(token, "srvid")) { chk_entry4sidtab(value, sidtab, 2); return; }
951 if (token[0] != '#')
952 fprintf(stderr, "Warning: keyword '%s' in sidtab section not recognized\n",token);
953}
954
955int init_sidtab()
956{
957 int nr, nro;
958 FILE *fp;
959 char *value;
960 struct s_sidtab *ptr;
961 struct s_sidtab *sidtab=(struct s_sidtab *)0;
962
963 sprintf(token, "%s%s", cs_confdir, cs_sidt);
964 if (!(fp=fopen(token, "r")))
965 {
966 cs_log("Cannot open file \"%s\" (errno=%d)", token, errno);
967 return(1);
968 }
969 for (nro=0, ptr=cfg->sidtab; ptr; nro++)
970 {
971 struct s_sidtab *ptr_next;
972 ptr_next=ptr->next;
973 if (ptr->caid) free(ptr->caid);
974 if (ptr->provid) free(ptr->provid);
975 if (ptr->srvid) free(ptr->srvid);
976 free(ptr);
977 ptr=ptr_next;
978 }
979 nr=0;
980 while (fgets(token, sizeof(token), fp))
981 {
982 int l;
983 void *ptr;
984 if ((l=strlen(trim(token)))<3) continue;
985 if ((token[0]=='[') && (token[l-1]==']'))
986 {
987 token[l-1]=0;
988 if (!(ptr=malloc(sizeof(struct s_sidtab))))
989 {
990 cs_log("Error allocating memory (errno=%d)", errno);
991 return(1);
992 }
993 if (sidtab)
994 sidtab->next=ptr;
995 else
996 cfg->sidtab=ptr;
997 sidtab=ptr;
998 nr++;
999 memset(sidtab, 0, sizeof(struct s_sidtab));
1000 strncpy(sidtab->label, strtolower(token+1), sizeof(sidtab->label));
1001 continue;
1002 }
1003 if (!sidtab) continue;
1004 if (!(value=strchr(token, '='))) continue;
1005 *value++='\0';
1006 chk_sidtab(trim(strtolower(token)), trim(strtolower(value)), sidtab);
1007 }
1008 fclose(fp);
1009
1010#ifdef DEBUG_SIDTAB
1011 show_sidtab(cfg->sidtab);
1012#endif
1013 cs_log("services reloaded: %d services freed, %d services loaded", nro, nr);
1014 return(0);
1015}
1016
1017int init_srvid()
1018{
1019 int nr;
1020 FILE *fp;
1021 char *payload;
1022 static struct s_srvid *srvid=(struct s_srvid *)0;
1023 sprintf(token, "%s%s", cs_confdir, cs_srid);
1024
1025 if (!(fp=fopen(token, "r"))) {
1026 cs_log("can't open file \"%s\" (err=%d), no service-id's loaded", token, errno);
1027 return(0);
1028 }
1029
1030 nr=0;
1031 while (fgets(token, sizeof(token), fp)) {
1032
1033 int l;
1034 void *ptr;
1035 if ((l=strlen(trim(token))) < 6) continue;
1036 if (!(payload=strchr(token, '|'))) continue;
1037 *payload++ = '\0';
1038 //if (strlen(token)!=4) continue;
1039
1040 if (!(ptr = malloc(sizeof(struct s_srvid)))) {
1041 cs_log("Error allocating memory (errno=%d)", errno);
1042 return(1);
1043 }
1044
1045 if (srvid)
1046 srvid->next = ptr;
1047 else
1048 cfg->srvid = ptr;
1049
1050 srvid = ptr;
1051 memset(srvid, 0, sizeof(struct s_srvid));
1052
1053 int i;
1054 char *ptr1;
1055 for (i = 0, ptr1 = strtok(payload, "|"); ptr1; ptr1 = strtok(NULL, "|"), i++){
1056 switch(i){
1057 case 0:
1058 strncpy(srvid->prov, trim(ptr1), sizeof(srvid->prov)-1);
1059 break;
1060 case 1:
1061 strncpy(srvid->name, trim(ptr1), sizeof(srvid->name)-1);
1062 break;
1063 case 2:
1064 strncpy(srvid->type, trim(ptr1), sizeof(srvid->type)-1);
1065 break;
1066 case 3:
1067 strncpy(srvid->desc, trim(ptr1), sizeof(srvid->desc)-1);
1068 break;
1069 }
1070 }
1071
1072 char *srvidasc = strchr(token, ':');
1073 *srvidasc++ = '\0';
1074 srvid->srvid = word_atob(srvidasc);
1075
1076 srvid->ncaid = 0;
1077 for (i = 0, ptr1 = strtok(token, ","); (ptr1) && (i < 10) ; ptr1 = strtok(NULL, ","), i++){
1078 srvid->caid[i] = word_atob(ptr1);
1079 srvid->ncaid = i+1;
1080 }
1081 nr++;
1082 }
1083
1084 fclose(fp);
1085 if (nr>0)
1086 cs_log("%d service-id's loaded", nr);
1087 else{
1088 cs_log("oscam.srvid loading failed, old format");
1089 }
1090 return(0);
1091}
1092
1093static void chk_reader(char *token, char *value, struct s_reader *rdr)
1094{
1095 int i;
1096 char *ptr;
1097 /*
1098 * case sensitive first
1099 */
1100 if (!strcmp(token, "device"))
1101 {
1102 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1103 {
1104 trim(ptr);
1105 switch(i)
1106 {
1107 case 0: strncpy(rdr->device, ptr, sizeof(rdr->device)-1); break;
1108 case 1: rdr->r_port=atoi(ptr); break;
1109 case 2: rdr->l_port=atoi(ptr); break;
1110 }
1111 }
1112 return;
1113 }
1114 if (!strcmp(token, "key"))
1115 {
1116 if (key_atob14(value, rdr->ncd_key))
1117 {
1118 fprintf(stderr, "Configuration newcamd: Error in Key\n");
1119 exit(1);
1120 }
1121 return;
1122 }
1123#ifdef CS_WITH_GBOX
1124 if (!strcmp(token, "password")) { strncpy((char *)rdr->gbox_pwd, (const char *)i2b(4, a2i(value, 4)), 4); return; }
1125 if (!strcmp(token, "premium")) { rdr->gbox_prem=1; return; }
1126#endif
1127 if (!strcmp(token, "account"))
1128 {
1129 for (i=0, ptr=strtok(value, ","); (i<2)&&(ptr); ptr=strtok(NULL, ","), i++)
1130 {
1131 trim(ptr);
1132 switch(i)
1133 {
1134 case 0: strncpy(rdr->r_usr, ptr, sizeof(rdr->r_usr)-1); break;
1135 case 1: strncpy(rdr->r_pwd, ptr, sizeof(rdr->r_pwd)-1); break;
1136 }
1137 }
1138 return;
1139 }
1140 if( !strcmp(token, "pincode")) { strncpy(rdr->pincode, value, sizeof(rdr->pincode)-1); return; }
1141 if (!strcmp(token, "readnano")) { strncpy((char *)rdr->emmfile, value, sizeof(rdr->emmfile)-1); return; }
1142 /*
1143 * case insensitive
1144 */
1145 strtolower(value);
1146
1147 if (!strcmp(token, "services")) { chk_services(value, &rdr->sidtabok, &rdr->sidtabno); return; }
1148 if (!strcmp(token, "inactivitytimeout")) { rdr->tcp_ito = atoi(value); return; }
1149 if (!strcmp(token, "reconnecttimeout")) { rdr->tcp_rto = atoi(value); return; }
1150 if (!strcmp(token, "disableserverfilter")) { rdr->ncd_disable_server_filt = atoi(value); return; }
1151
1152 if (!strcmp(token, "label")) { strncpy(rdr->label, value, sizeof(rdr->label)-1); return; }
1153 if (!strcmp(token, "fallback")) { rdr->fallback=atoi(value) ? 1 : 0; return; }
1154 if (!strcmp(token, "logport")) { rdr->log_port=atoi(value); return; }
1155 if (!strcmp(token, "caid")) { chk_caidtab(value, &rdr->ctab); return; }
1156 if (!strcmp(token, "boxid")) { rdr->boxid=a2i(value,4); return; }
1157 if (!strcmp(token, "aeskey"))
1158 {
1159 if (key_atob(value, rdr->aes_key))
1160 {
1161 fprintf(stderr, "Configuration reader: Error in AES Key\n");
1162 exit(1);
1163 }
1164 return;
1165 }
1166 if ((!strcmp(token, "n3_rsakey")) || (!strcmp(token, "rsakey")) )
1167 {
1168 rdr->has_rsa=1;
1169 if (key_atob_l(value, rdr->rsa_mod, 128))
1170 {
1171 fprintf(stderr, "Configuration reader: Error in rsakey\n");
1172 exit(1);
1173 }
1174 return;
1175 }
1176 if (!strcmp(token, "tiger_rsakey"))
1177 {
1178 if (key_atob_l(value, rdr->rsa_mod, 240))
1179 {
1180 fprintf(stderr, "Configuration reader: Error in tiger_rsakey\n");
1181 exit(1);
1182 }
1183 return;
1184 }
1185 if ((!strcmp(token, "n3_boxkey")) || (!strcmp(token, "boxkey")))
1186 {
1187 if (key_atob_l(value, rdr->nagra_boxkey, 16))
1188 {
1189 fprintf(stderr, "Configuration reader: Error in boxkey\n");
1190 exit(1);
1191 }
1192 return;
1193 }
1194 if (!strcmp(token, "detect"))
1195 {
1196 for (i=0; RDR_CD_TXT[i]; i++)
1197 {
1198 if (!strcmp(value, RDR_CD_TXT[i]))
1199 rdr->detect=i;
1200 else
1201 if ((value[0]=='!') && (!strcmp(value+1, RDR_CD_TXT[i])))
1202 rdr->detect=i|0x80;
1203 }
1204 return;
1205 }
1206 if (!strcmp(token, "mhz")) { rdr->mhz=atoi(value); return; }
1207 if (!strcmp(token, "cardmhz")) { rdr->cardmhz=atoi(value); return; }
1208 if (!strcmp(token, "protocol"))
1209 {
1210 if (!strcmp(value, "mouse")) { rdr->typ=R_MOUSE; return; }
1211 if (!strcmp(value, "smartreader")) { rdr->typ=R_SMART; return; }
1212 if (!strcmp(value, "internal")) { rdr->typ=R_INTERNAL; return; }
1213#ifdef HAVE_PCSC
1214 if (!strcmp(value, "pcsc")) { rdr->typ=R_PCSC; return; }
1215#endif
1216 if (!strcmp(value, "serial")) { rdr->typ=R_SERIAL; return; }
1217 if (!strcmp(value, "camd35")) { rdr->typ=R_CAMD35; return; }
1218 if (!strcmp(value, "cs378x")) { rdr->typ=R_CS378X; return; }
1219 if (!strcmp(value, "cs357x")) { rdr->typ=R_CAMD35; return; }
1220 if (!strcmp(value, "gbox")) { rdr->typ=R_GBOX; return; }
1221 if (!strcmp(value, "cccam")) {
1222 rdr->typ=R_CCCAM;
1223 // strcpy(value, "1");
1224 // chk_caidtab(value, &rdr->ctab); // this is a MAJOR hack for auto multiple caid support (not currently working due to ncd table issue)
1225 return;
1226 }
1227 if (!strcmp(value, "radegast")) { rdr->typ=R_RADEGAST; return; }
1228 if (!strcmp(value, "newcamd") ||
1229 !strcmp(value, "newcamd525")) {rdr->typ=R_NEWCAMD;
1230 rdr->ncd_proto=NCD_525; return; }
1231 if (!strcmp(value, "newcamd524")) {rdr->typ=R_NEWCAMD;
1232 rdr->ncd_proto=NCD_524; return; }
1233 fprintf(stderr, "WARNING: value '%s' in protocol-line not recognized, assuming MOUSE\n",value);
1234 rdr->typ=R_MOUSE;
1235 return;
1236 }
1237 if (!strcmp(token, "ident")) { chk_ftab(value, &rdr->ftab,"reader",rdr->label,"provid"); return; }
1238 if (!strcmp(token, "class")) { chk_cltab(value, &rdr->cltab); return; }
1239 if (!strcmp(token, "chid")) { chk_ftab(value, &rdr->fchid,"reader",rdr->label,"chid"); return; }
1240 if (!strcmp(token, "showcls")) { rdr->show_cls = atoi(value); return; }
1241 if (!strcmp(token, "maxqlen"))
1242 {
1243 rdr->maxqlen = atoi(value);
1244 if( rdr->maxqlen<0 || rdr->maxqlen>CS_MAXQLEN )
1245 rdr->maxqlen=CS_MAXQLEN;
1246 return;
1247 }
1248 if (!strcmp(token, "group"))
1249 {
1250 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1251 {
1252 int g;
1253 g=atoi(ptr);
1254 if ((g>0) && (g<33)) rdr->grp|=(1<<(g-1));
1255 }
1256 return;
1257 }
1258 if (!strcmp(token, "emmcache"))
1259 {
1260 for (i=0, ptr=strtok(value, ","); (i<3)&&(ptr); ptr=strtok(NULL, ","), i++)
1261 switch(i)
1262 {
1263 case 0: rdr->cachemm=atoi(ptr); break;
1264 case 1: rdr->rewritemm=atoi(ptr); break;
1265 case 2: rdr->logemm=atoi(ptr); break;
1266 }
1267 if (rdr->rewritemm <=0) {
1268 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);
1269 rdr->rewritemm = 1;
1270 }
1271 return;
1272 }
1273
1274 if (!strcmp(token, "blocknano"))
1275 {
1276 if (!strcmp(value,"all")) //wildcard is used
1277 for (i=0 ; i<256; i++)
1278 rdr->b_nano[i] |= 0x01; //set all lsb's to block all nanos
1279 else
1280 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1281 if ((i=byte_atob(ptr))>=0)
1282 rdr->b_nano[i]|= 0x01; //lsb is set when to block nano
1283 return;
1284 }
1285 if (!strcmp(token, "savenano"))
1286 {
1287 if (!strcmp(value,"all")) //wildcard is used
1288 for (i=0 ; i<256; i++)
1289 rdr->b_nano[i] |= 0x02; //set all lsb+1 to save all nanos to file
1290 else
1291 for (ptr=strtok(value, ","); ptr; ptr=strtok(NULL, ","))
1292 if ((i=byte_atob(ptr))>=0)
1293 rdr->b_nano[i]|= 0x02; //lsb+1 is set when to save nano to file
1294 return;
1295 }
1296 if (!strcmp(token, "cccversion")) { // cccam version
1297 if (strlen(value)>sizeof(rdr->cc_version)-1) {
1298 fprintf(stderr, "cccam config: version too long\n");
1299 exit(1);
1300 }
1301 bzero(rdr->cc_version, sizeof(rdr->cc_version));
1302 strncpy(rdr->cc_version, value, sizeof(rdr->cc_version)-1);
1303 return;
1304 }
1305 if (!strcmp(token, "cccbuild")) { // cccam build number
1306 if (strlen(value)>sizeof(rdr->cc_build)-1) {
1307 fprintf(stderr, "cccam config build number too long\n");
1308 exit(1);
1309 }
1310 bzero(rdr->cc_build, sizeof(rdr->cc_build));
1311 strncpy(rdr->cc_build, value, sizeof(rdr->cc_build)-1);
1312 return;
1313 }
1314 if (!strcmp(token, "cccmaxhop")) { // cccam max card distance
1315 rdr->cc_maxhop = atoi(value);
1316 return;
1317 }
1318 if (token[0] != '#')
1319 fprintf(stderr, "Warning: keyword '%s' in reader section not recognized\n",token);
1320}
1321
1322int init_readerdb()
1323{
1324 int tag=0, nr;
1325 FILE *fp;
1326 char *value;
1327
1328 sprintf(token, "%s%s", cs_confdir, cs_srvr);
1329 if (!(fp=fopen(token, "r")))
1330 {
1331 cs_log("can't open file \"%s\" (errno=%d)\n", token, errno);
1332 return(1);
1333 }
1334 nr=0;
1335 while (fgets(token, sizeof(token), fp))
1336 {
1337 int i, l;
1338 if ((l=strlen(trim(token)))<3) continue;
1339 if ((token[0]=='[') && (token[l-1]==']'))
1340 {
1341 token[l-1]=0;
1342 tag=(!strcmp("reader", strtolower(token+1)));
1343 if (reader[nr].label[0] && reader[nr].typ) nr++;
1344 memset(&reader[nr], 0, sizeof(struct s_reader));
1345 reader[nr].tcp_rto = 30;
1346 reader[nr].show_cls = 10;
1347 reader[nr].maxqlen = CS_MAXQLEN;
1348 reader[nr].mhz = 357;
1349 reader[nr].cardmhz = 357;
1350 strcpy(reader[nr].pincode, "none");
1351 for (i=1; i<CS_MAXCAIDTAB; reader[nr].ctab.mask[i++]=0xffff);
1352 continue;
1353 }
1354 if (!tag) continue;
1355 if (!(value=strchr(token, '='))) continue;
1356 *value++='\0';
1357 chk_reader(trim(strtolower(token)), trim(value), &reader[nr]);
1358 }
1359 fclose(fp);
1360 return(0);
1361}
1362
1363/*
1364int init_irdeto_guess_tab()
1365{
1366 int i, j, skip;
1367 int b47;
1368 FILE *fp;
1369 char token[128], *value, *ptr;
1370 char zSid[5];
1371 uchar b3;
1372 ushort caid, sid;
1373 struct s_irdeto_quess *ird_row, *head;
1374
1375 memset(cfg->itab, 0, sizeof(cfg->itab));
1376 sprintf(token, "%s%s", cs_confdir, cs_ird);
1377 if (!(fp=fopen(token, "r")))
1378 {
1379 cs_log("can't open file \"%s\" (errno=%d) irdeto guessing not loaded",
1380 token, errno);
1381 return(1);
1382 }
1383 while (fgets(token, sizeof(token), fp))
1384 {
1385 if( strlen(token)<20 ) continue;
1386 for( i=b3=b47=caid=sid=skip=0, ptr=strtok(token, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1387 {
1388 trim(ptr);
1389 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1390 skip=1;
1391 break;
1392 }
1393 switch(i)
1394 {
1395 case 0: b3 = a2i(ptr, 2); break;
1396 case 1: b47 = a2i(ptr, 8); break;
1397 case 2: caid = a2i(ptr, 4); break;
1398 case 3:
1399 for( j=0; j<4; j++ )
1400 zSid[j]=ptr[j];
1401 zSid[4]=0;
1402 sid = a2i(zSid, 4);
1403 break;
1404 }
1405 }
1406 if( !skip )
1407 {
1408 if (!(ird_row=(struct s_irdeto_quess*)malloc(sizeof(struct s_irdeto_quess))))
1409 {
1410 cs_log("Error allocating memory (errno=%d)", errno);
1411 return;
1412 }
1413 ird_row->b47 = b47;
1414 ird_row->caid = caid;
1415 ird_row->sid = sid;
1416 ird_row->next = 0;
1417
1418 head = cfg->itab[b3];
1419 if( head ) {
1420 while( head->next )
1421 head=head->next;
1422 head->next=ird_row;
1423 }
1424 else
1425 cfg->itab[b3]=ird_row;
1426
1427 //cs_debug("%02X:%08X:%04X:%04X", b3, b47, caid, sid);
1428 }
1429 }
1430 fclose(fp);
1431
1432 for( i=0; i<0xff; i++ )
1433 {
1434 head=cfg->itab[i];
1435 while(head)
1436 {
1437 cs_debug("itab[%02X]: b47=%08X, caid=%04X, sid=%04X",
1438 i, head->b47, head->caid, head->sid);
1439 head=head->next;
1440 }
1441 }
1442 return(0);
1443}
1444*/
1445
1446#ifdef CS_ANTICASC
1447void init_ac()
1448{
1449 int nr;
1450 FILE *fp;
1451 //char *value;
1452
1453 sprintf(token, "%s%s", cs_confdir, cs_ac);
1454 if (!(fp=fopen(token, "r")))
1455 {
1456 cs_log("can't open file \"%s\" (errno=%d) anti-cascading table not loaded",
1457 token, errno);
1458 return;
1459 }
1460
1461 for(nr=0; fgets(token, sizeof(token), fp);)
1462 {
1463 int i, skip;
1464 ushort caid, sid, chid, dwtime;
1465 ulong provid;
1466 char *ptr, *ptr1;
1467 struct s_cpmap *ptr_cpmap;
1468 static struct s_cpmap *cpmap=(struct s_cpmap *)0;
1469
1470 if( strlen(token)<4 ) continue;
1471
1472 caid=sid=chid=dwtime=0;
1473 provid=0;
1474 skip=0;
1475 ptr1=0;
1476 for( i=0, ptr=strtok(token, "="); (i<2)&&(ptr); ptr=strtok(NULL, "="), i++ )
1477 {
1478 trim(ptr);
1479 if( *ptr==';' || *ptr=='#' || *ptr=='-' ) {
1480 skip=1;
1481 break;
1482 }
1483 switch( i )
1484 {
1485 case 0:
1486 ptr1=ptr;
1487 break;
1488 case 1:
1489 dwtime = atoi(ptr);
1490 break;
1491 }
1492 }
1493
1494 if( !skip )
1495 {
1496 for( i=0, ptr=strtok(ptr1, ":"); (i<4)&&(ptr); ptr=strtok(NULL, ":"), i++ )
1497 {
1498 trim(ptr);
1499 switch( i )
1500 {
1501 case 0:
1502 if( *ptr=='*' ) caid = 0;
1503 else caid = a2i(ptr, 4);
1504 break;
1505 case 1:
1506 if( *ptr=='*' ) provid = 0;
1507 else provid = a2i(ptr, 6);
1508 break;
1509 case 2:
1510 if( *ptr=='*' ) sid = 0;
1511 else sid = a2i(ptr, 4);
1512 break;
1513 case 3:
1514 if( *ptr=='*' ) chid = 0;
1515 else chid = a2i(ptr, 4);
1516 break;
1517 }
1518 }
1519 if (!(ptr_cpmap=(struct s_cpmap*)malloc(sizeof(struct s_cpmap))))
1520 {
1521 cs_log("Error allocating memory (errno=%d)", errno);
1522 return;
1523 }
1524 if( cpmap )
1525 cpmap->next=ptr_cpmap;
1526 else
1527 cfg->cpmap=ptr_cpmap;
1528 cpmap=ptr_cpmap;
1529
1530 cpmap->caid = caid;
1531 cpmap->provid = provid;
1532 cpmap->sid = sid;
1533 cpmap->chid = chid;
1534 cpmap->dwtime = dwtime;
1535 cpmap->next = 0;
1536
1537 cs_debug("nr=%d, caid=%04X, provid=%06X, sid=%04X, chid=%04X, dwtime=%d",
1538 nr, caid, provid, sid, chid, dwtime);
1539 nr++;
1540 }
1541 }
1542 fclose(fp);
1543 //cs_log("%d lengths for caid guessing loaded", nr);
1544 return;
1545}
1546#endif
Note: See TracBrowser for help on using the repository browser.