1 | #define CS_CORE
|
---|
2 | #include "globals.h"
|
---|
3 | #ifdef CS_WITH_GBOX
|
---|
4 | # include "csgbox/gbox.h"
|
---|
5 | # define CS_VERSION_X CS_VERSION "-gbx-" GBXVERSION
|
---|
6 | #else
|
---|
7 | # define CS_VERSION_X CS_VERSION
|
---|
8 | #endif
|
---|
9 | /*****************************************************************************
|
---|
10 | Globals
|
---|
11 | *****************************************************************************/
|
---|
12 | int pfd=0; // Primary FD, must be closed on exit
|
---|
13 | int mfdr=0; // Master FD (read)
|
---|
14 | int fd_m2c=0; // FD Master -> Client (for clients / read )
|
---|
15 | int fd_c2m=0; // FD Client -> Master (for clients / write )
|
---|
16 | int fd_c2l=0; // FD Client -> Logger (for clients / write )
|
---|
17 | int cs_dblevel=0; // Debug Level (TODO !!)
|
---|
18 | int cs_idx=0; // client index (0=master, ...)
|
---|
19 | int cs_ptyp=0; // process-type
|
---|
20 | struct s_module ph[CS_MAX_MOD]; // Protocols
|
---|
21 | struct s_cardsystem cardsystem[CS_MAX_MOD]; // Protocols
|
---|
22 | int maxph=0; // Protocols used
|
---|
23 | int cs_hw=0; // hardware autodetect
|
---|
24 | int is_server=0; // used in modules to specify function
|
---|
25 | pid_t master_pid=0; // master pid OUTSIDE shm
|
---|
26 | ushort len4caid[256]; // table for guessing caid (by len)
|
---|
27 | char cs_confdir[128]=CS_CONFDIR;
|
---|
28 | uchar mbuf[1024]; // global buffer
|
---|
29 | ECM_REQUEST *ecmtask;
|
---|
30 | #ifdef CS_ANTICASC
|
---|
31 | struct s_acasc ac_stat[CS_MAXPID];
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | /*****************************************************************************
|
---|
35 | Shared Memory
|
---|
36 | *****************************************************************************/
|
---|
37 | int *ecmidx; // Shared Memory
|
---|
38 | int *logidx; // Shared Memory
|
---|
39 | int *oscam_sem; // sem (multicam.o)
|
---|
40 | int *c_start; // idx of 1st client
|
---|
41 | int *log_fd; // log-process is running
|
---|
42 | struct s_ecm *ecmcache; // Shared Memory
|
---|
43 | struct s_client *client; // Shared Memory
|
---|
44 | struct s_reader *reader; // Shared Memory
|
---|
45 |
|
---|
46 | #ifdef CS_WITH_GBOX
|
---|
47 | struct card_struct *Cards; // Shared Memory
|
---|
48 | struct idstore_struct *idstore; // Shared Memory
|
---|
49 | unsigned long *IgnoreList; // Shared Memory
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | struct s_config *cfg; // Shared Memory
|
---|
53 | #ifdef CS_ANTICASC
|
---|
54 | struct s_acasc_shm *acasc; // anti-cascading table indexed by account.ac_idx
|
---|
55 | #endif
|
---|
56 | #ifdef CS_LOGHISTORY
|
---|
57 | int *loghistidx; // ptr to current entry
|
---|
58 | char *loghist; // ptr of log-history
|
---|
59 | #endif
|
---|
60 | int *mcl=0; // Master close log?
|
---|
61 |
|
---|
62 | static int shmsize = CS_ECMCACHESIZE*(sizeof(struct s_ecm)) +
|
---|
63 | CS_MAXPID*(sizeof(struct s_client)) +
|
---|
64 | CS_MAXREADER*(sizeof(struct s_reader)) +
|
---|
65 | #ifdef CS_WITH_GBOX
|
---|
66 | CS_MAXCARDS*(sizeof(struct card_struct))+
|
---|
67 | CS_MAXIGNORE*(sizeof(long))+
|
---|
68 | CS_MAXPID*(sizeof(struct idstore_struct))+
|
---|
69 | #endif
|
---|
70 | #ifdef CS_ANTICASC
|
---|
71 | CS_MAXPID*(sizeof(struct s_acasc_shm)) +
|
---|
72 | #endif
|
---|
73 | #ifdef CS_LOGHISTORY
|
---|
74 | CS_MAXLOGHIST*CS_LOGHISTSIZE + sizeof(int) +
|
---|
75 | #endif
|
---|
76 | sizeof(struct s_config)+(6*sizeof(int));
|
---|
77 |
|
---|
78 | #ifdef CS_NOSHM
|
---|
79 | char cs_memfile[128]=CS_MMAPFILE;
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | /*****************************************************************************
|
---|
83 | Statics
|
---|
84 | *****************************************************************************/
|
---|
85 | static char mloc[128]={0};
|
---|
86 | static int shmid=0; // Shared Memory ID
|
---|
87 | static int cs_last_idx=0; // client index of last fork (master only)
|
---|
88 | static char *logo = " ___ ____ ___ \n / _ \\/ ___| / __|__ _ _ __ ___ \n| | | \\___ \\| | / _` | '_ ` _ \\ \n| |_| |___) | |_| (_| | | | | | |\n \\___/|____/ \\___\\__,_|_| |_| |_|\n";
|
---|
89 |
|
---|
90 | static void cs_set_mloc(int ato, char *txt)
|
---|
91 | {
|
---|
92 | if (ato>=0)
|
---|
93 | alarm(ato);
|
---|
94 | if (txt)
|
---|
95 | strcpy(mloc, txt);
|
---|
96 | }
|
---|
97 |
|
---|
98 | char *cs_platform(char *buf)
|
---|
99 | {
|
---|
100 | static char *hw=NULL;
|
---|
101 | if (!hw)
|
---|
102 | {
|
---|
103 | #ifdef TUXBOX
|
---|
104 | struct stat st;
|
---|
105 | cs_hw=CS_HW_DBOX2; // dbox2, default for now
|
---|
106 | if (!stat("/dev/sci0", &st)) cs_hw=CS_HW_DREAM; // dreambox
|
---|
107 | #ifdef TRIPLEDRAGON
|
---|
108 | if (!stat("/dev/stb/tdsc0", &st)) cs_hw=CS_HW_DRAGON; // tripledragon
|
---|
109 | #endif
|
---|
110 | switch(cs_hw)
|
---|
111 | {
|
---|
112 | #ifdef PPC
|
---|
113 | case CS_HW_DBOX2: hw="dbox2" ; break;
|
---|
114 | #endif
|
---|
115 | case CS_HW_DREAM: hw="dreambox"; break;
|
---|
116 | case CS_HW_DRAGON: hw="tripledragon"; break;
|
---|
117 | }
|
---|
118 | #endif
|
---|
119 | if (!hw) hw=CS_OS_HW;
|
---|
120 | }
|
---|
121 | sprintf(buf, "%s-%s-%s", CS_OS_CPU, hw, CS_OS_SYS);
|
---|
122 | return(buf);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static void usage()
|
---|
126 | {
|
---|
127 | fprintf(stderr, "%s\n\n", logo);
|
---|
128 | fprintf(stderr, "OSCam cardserver v%s, build #%s (%s) - (w) 2009-2010 streamboard SVN\n", CS_VERSION_X, CS_SVN_VERSION, CS_OSTYPE);
|
---|
129 | fprintf(stderr, "\tsee http://streamboard.gmc.to:8001/wiki/ for more details\n");
|
---|
130 | fprintf(stderr, "\tbased on OSCam 0.99.x, (w) 2010 streamboard SVN\n");
|
---|
131 | fprintf(stderr, "\tbased on streamboard mp-cardserver v0.9d - (w) 2004-2007 by dukat\n");
|
---|
132 | fprintf(stderr, "\tinbuilt modules: ");
|
---|
133 | #ifdef HAVE_DVBAPI
|
---|
134 | fprintf(stderr, "dvbapi ");
|
---|
135 | #endif
|
---|
136 | #ifdef WEBIF
|
---|
137 | fprintf(stderr, "webinterface ");
|
---|
138 | #endif
|
---|
139 | #ifdef CS_ANTICASC
|
---|
140 | fprintf(stderr, "anticascading ");
|
---|
141 | #endif
|
---|
142 | #ifdef LIBUSB
|
---|
143 | fprintf(stderr, "smartreader ");
|
---|
144 | #endif
|
---|
145 | #ifdef HAVE_PCSC
|
---|
146 | fprintf(stderr, "pcsc ");
|
---|
147 | #endif
|
---|
148 | #ifdef CS_WITH_GBOX
|
---|
149 | fprintf(stderr, "gbox ");
|
---|
150 | #endif
|
---|
151 | #ifdef IRDETO_GUESSING
|
---|
152 | fprintf(stderr, "irdeto-guessing ");
|
---|
153 | #endif
|
---|
154 | fprintf(stderr, "\n\n");
|
---|
155 | fprintf(stderr, "oscam [-b] [-c config-dir] [-d]");
|
---|
156 | #ifdef CS_NOSHM
|
---|
157 | fprintf(stderr, " [-m memory-file]");
|
---|
158 | #endif
|
---|
159 | fprintf(stderr, "\n\n\t-b : start in background\n");
|
---|
160 | fprintf(stderr, "\t-c <dir> : read configuration from <dir>\n");
|
---|
161 | fprintf(stderr, "\t default = %s\n", CS_CONFDIR);
|
---|
162 | fprintf(stderr, "\t-d <level> : debug level mask\n");
|
---|
163 | fprintf(stderr, "\t 0 = no debugging (default)\n");
|
---|
164 | fprintf(stderr, "\t 1 = detailed error messages\n");
|
---|
165 | fprintf(stderr, "\t 2 = ATR parsing info, ECM, EMM and CW dumps\n");
|
---|
166 | fprintf(stderr, "\t 4 = traffic from/to the reader\n");
|
---|
167 | fprintf(stderr, "\t 8 = traffic from/to the clients\n");
|
---|
168 | fprintf(stderr, "\t 16 = traffic to the reader-device on IFD layer\n");
|
---|
169 | fprintf(stderr, "\t 32 = traffic to the reader-device on I/O layer\n");
|
---|
170 | fprintf(stderr, "\t 64 = EMM logging\n");
|
---|
171 | fprintf(stderr, "\t 255 = debug all\n");
|
---|
172 | #ifdef CS_NOSHM
|
---|
173 | fprintf(stderr, "\t-m <file> : use <file> as mmaped memory file\n");
|
---|
174 | fprintf(stderr, "\t default = %s\n", CS_MMAPFILE);
|
---|
175 | #endif
|
---|
176 | fprintf(stderr, "\n");
|
---|
177 | exit(1);
|
---|
178 | }
|
---|
179 |
|
---|
180 | #ifdef NEED_DAEMON
|
---|
181 | #ifdef OS_MACOSX
|
---|
182 | // this is done because daemon is being deprecated starting with 10.5 and -Werror will always trigger an error
|
---|
183 | static int daemon_compat(int nochdir, int noclose)
|
---|
184 | #else
|
---|
185 | static int daemon(int nochdir, int noclose)
|
---|
186 | #endif
|
---|
187 | {
|
---|
188 | int fd;
|
---|
189 |
|
---|
190 | switch (fork())
|
---|
191 | {
|
---|
192 | case -1: return (-1);
|
---|
193 | case 0: break;
|
---|
194 | default: _exit(0);
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (setsid()==(-1))
|
---|
198 | return(-1);
|
---|
199 |
|
---|
200 | if (!nochdir)
|
---|
201 | (void)chdir("/");
|
---|
202 |
|
---|
203 | if (!noclose && (fd=open("/dev/null", O_RDWR, 0)) != -1)
|
---|
204 | {
|
---|
205 | (void)dup2(fd, STDIN_FILENO);
|
---|
206 | (void)dup2(fd, STDOUT_FILENO);
|
---|
207 | (void)dup2(fd, STDERR_FILENO);
|
---|
208 | if (fd>2)
|
---|
209 | (void)close(fd);
|
---|
210 | }
|
---|
211 | return(0);
|
---|
212 | }
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | int recv_from_udpipe(uchar *buf)
|
---|
216 | {
|
---|
217 | unsigned short n;
|
---|
218 | if (!pfd) return(-9);
|
---|
219 | if (!read(pfd, buf, 3)) cs_exit(1);
|
---|
220 | if (buf[0]!='U')
|
---|
221 | {
|
---|
222 | cs_log("INTERNAL PIPE-ERROR");
|
---|
223 | cs_exit(1);
|
---|
224 | }
|
---|
225 | memcpy(&n, buf+1, 2);
|
---|
226 | return(read(pfd, buf, n));
|
---|
227 | }
|
---|
228 |
|
---|
229 | char *username(int idx)
|
---|
230 | {
|
---|
231 | if (client[idx].usr[0])
|
---|
232 | return(client[idx].usr);
|
---|
233 | else
|
---|
234 | return("anonymous");
|
---|
235 | }
|
---|
236 |
|
---|
237 | static int idx_from_ip(in_addr_t ip, in_port_t port)
|
---|
238 | {
|
---|
239 | int i, idx;
|
---|
240 | for (i=idx=0; (i<CS_MAXPID) && (!idx); i++)
|
---|
241 | if ((client[i].ip==ip) && (client[i].port==port) &&
|
---|
242 | ((client[i].typ=='c') || (client[i].typ=='m')))
|
---|
243 | idx=i;
|
---|
244 | return(idx);
|
---|
245 | }
|
---|
246 |
|
---|
247 | int idx_from_pid(pid_t pid)
|
---|
248 | {
|
---|
249 | int i, idx;
|
---|
250 | for (i=0, idx=(-1); (i<CS_MAXPID) && (idx<0); i++)
|
---|
251 | if (client[i].pid==pid)
|
---|
252 | idx=i;
|
---|
253 | return(idx);
|
---|
254 | }
|
---|
255 |
|
---|
256 | static long chk_caid(ushort caid, CAIDTAB *ctab)
|
---|
257 | {
|
---|
258 | int n;
|
---|
259 | long rc;
|
---|
260 | for (rc=(-1), n=0; (n<CS_MAXCAIDTAB) && (rc<0); n++)
|
---|
261 | if ((caid & ctab->mask[n]) == ctab->caid[n])
|
---|
262 | rc=ctab->cmap[n] ? ctab->cmap[n] : caid;
|
---|
263 | return(rc);
|
---|
264 | }
|
---|
265 |
|
---|
266 | int chk_bcaid(ECM_REQUEST *er, CAIDTAB *ctab)
|
---|
267 | {
|
---|
268 | long caid;
|
---|
269 | if ((caid=chk_caid(er->caid, ctab))<0)
|
---|
270 | return(0);
|
---|
271 | er->caid=caid;
|
---|
272 | return(1);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * void set_signal_handler(int sig, int flags, void (*sighandler)(int))
|
---|
277 | * flags: 1 = restart, 2 = don't modify if SIG_IGN, may be combined
|
---|
278 | */
|
---|
279 | void set_signal_handler(int sig, int flags, void (*sighandler)(int))
|
---|
280 | {
|
---|
281 | #ifdef CS_SIGBSD
|
---|
282 | if ((signal(sig, sighandler)==SIG_IGN) && (flags & 2))
|
---|
283 | {
|
---|
284 | signal(sig, SIG_IGN);
|
---|
285 | siginterrupt(sig, 0);
|
---|
286 | }
|
---|
287 | else
|
---|
288 | siginterrupt(sig, (flags & 1) ? 0 : 1);
|
---|
289 | #else
|
---|
290 | struct sigaction sa;
|
---|
291 | sigaction(sig, (struct sigaction *) 0, &sa);
|
---|
292 | if (!((flags & 2) && (sa.sa_handler==SIG_IGN)))
|
---|
293 | {
|
---|
294 | sigemptyset(&sa.sa_mask);
|
---|
295 | sa.sa_flags=(flags & 1) ? SA_RESTART : 0;
|
---|
296 | sa.sa_handler=sighandler;
|
---|
297 | sigaction(sig, &sa, (struct sigaction *) 0);
|
---|
298 | }
|
---|
299 | #endif
|
---|
300 | }
|
---|
301 |
|
---|
302 | static void cs_alarm()
|
---|
303 | {
|
---|
304 | cs_debug("Got alarm signal");
|
---|
305 | cs_log("disconnect from %s (deadlock!)", cs_inet_ntoa(client[cs_idx].ip));
|
---|
306 | cs_exit(0);
|
---|
307 | }
|
---|
308 |
|
---|
309 | static void cs_master_alarm()
|
---|
310 | {
|
---|
311 | cs_log("PANIC: master deadlock! last location: %s", mloc);
|
---|
312 | fprintf(stderr, "PANIC: master deadlock! last location: %s", mloc);
|
---|
313 | fflush(stderr);
|
---|
314 | cs_exit(0);
|
---|
315 | }
|
---|
316 |
|
---|
317 | static void cs_sigpipe()
|
---|
318 | {
|
---|
319 | if ((cs_idx) && (master_pid!=getppid()))
|
---|
320 | cs_exit(0);
|
---|
321 | cs_log("Got sigpipe signal -> captured");
|
---|
322 | }
|
---|
323 |
|
---|
324 | void cs_exit(int sig)
|
---|
325 | {
|
---|
326 | int i;
|
---|
327 |
|
---|
328 | set_signal_handler(SIGCHLD, 1, SIG_IGN);
|
---|
329 | set_signal_handler(SIGHUP , 1, SIG_IGN);
|
---|
330 | if (sig && (sig!=SIGQUIT))
|
---|
331 | cs_log("exit with signal %d", sig);
|
---|
332 | switch(client[cs_idx].typ)
|
---|
333 | {
|
---|
334 | case 'c': cs_statistics(cs_idx);
|
---|
335 | case 'm': break;
|
---|
336 | case 'n': *log_fd=0;
|
---|
337 | break;
|
---|
338 | case 's': *log_fd=0;
|
---|
339 | for (i=1; i<CS_MAXPID; i++)
|
---|
340 | if (client[i].pid)
|
---|
341 | kill(client[i].pid, SIGQUIT);
|
---|
342 | cs_log("cardserver down");
|
---|
343 | #ifndef CS_NOSHM
|
---|
344 | if (ecmcache) shmdt((void *)ecmcache);
|
---|
345 | #endif
|
---|
346 | break;
|
---|
347 | }
|
---|
348 | if (pfd) close(pfd);
|
---|
349 | #ifdef CS_NOSHM
|
---|
350 | munmap((void *)ecmcache, (size_t)shmsize);
|
---|
351 | if (shmid) close(shmid);
|
---|
352 | unlink(CS_MMAPFILE); // ignore errors, last process must succeed
|
---|
353 | #endif
|
---|
354 | exit(sig);
|
---|
355 | }
|
---|
356 |
|
---|
357 | void cs_reinit_clients()
|
---|
358 | {
|
---|
359 | int i;
|
---|
360 | struct s_auth *account;
|
---|
361 |
|
---|
362 | for( i = 1; i < CS_MAXPID; i++ )
|
---|
363 | if( client[i].pid && client[i].typ == 'c' && client[i].usr[0] ) {
|
---|
364 | for (account = cfg->account; (account) ; account = account->next)
|
---|
365 | if (!strcmp(client[i].usr, account->usr))
|
---|
366 | break;
|
---|
367 |
|
---|
368 | if (account && client[i].pcrc == crc32(0L, MD5((uchar *)account->pwd, strlen(account->pwd), NULL), 16)) {
|
---|
369 | client[i].grp = account->grp;
|
---|
370 | client[i].au = account->au;
|
---|
371 | client[i].autoau = account->autoau;
|
---|
372 | client[i].expirationdate = account->expirationdate;
|
---|
373 | client[i].ncd_keepalive = account->ncd_keepalive;
|
---|
374 | client[i].c35_suppresscmd08 = account->c35_suppresscmd08;
|
---|
375 | client[i].tosleep = (60*account->tosleep);
|
---|
376 | client[i].monlvl = account->monlvl;
|
---|
377 | client[i].disabled = account->disabled;
|
---|
378 | client[i].fchid = account->fchid; // CHID filters
|
---|
379 | client[i].cltab = account->cltab; // Class
|
---|
380 |
|
---|
381 | // newcamd module dosent like ident reloading
|
---|
382 | if(!client[i].ncd_server)
|
---|
383 | client[i].ftab = account->ftab; // Ident
|
---|
384 |
|
---|
385 | client[i].sidtabok = account->sidtabok; // services
|
---|
386 | client[i].sidtabno = account->sidtabno; // services
|
---|
387 |
|
---|
388 | memcpy(&client[i].ctab, &account->ctab, sizeof(client[i].ctab));
|
---|
389 | memcpy(&client[i].ttab, &account->ttab, sizeof(client[i].ttab));
|
---|
390 |
|
---|
391 | #ifdef CS_ANTICASC
|
---|
392 | client[i].ac_idx = account->ac_idx;
|
---|
393 | client[i].ac_penalty= account->ac_penalty;
|
---|
394 | client[i].ac_limit = (account->ac_users * 100 + 80) * cfg->ac_stime;
|
---|
395 | #endif
|
---|
396 | } else {
|
---|
397 | if (ph[client[i].ctyp].type & MOD_CONN_NET) {
|
---|
398 | cs_debug("client '%s', pid=%d not found in db (or password changed)", client[i].usr, client[i].pid);
|
---|
399 | kill(client[i].pid, SIGQUIT);
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | static void cs_sighup()
|
---|
406 | {
|
---|
407 | uchar dummy[1]={0x00};
|
---|
408 | write_to_pipe(fd_c2m, PIP_ID_HUP, dummy, 1);
|
---|
409 | }
|
---|
410 |
|
---|
411 | static void cs_accounts_chk()
|
---|
412 | {
|
---|
413 | int i;
|
---|
414 | init_userdb();
|
---|
415 | cs_reinit_clients();
|
---|
416 | #ifdef CS_ANTICASC
|
---|
417 | for (i=0; i<CS_MAXPID; i++)
|
---|
418 | if (client[i].typ=='a')
|
---|
419 | {
|
---|
420 | kill(client[i].pid, SIGHUP);
|
---|
421 | break;
|
---|
422 | }
|
---|
423 | #endif
|
---|
424 | }
|
---|
425 |
|
---|
426 | static void cs_debug_level()
|
---|
427 | {
|
---|
428 | int i;
|
---|
429 |
|
---|
430 | //switch debuglevel forward one step if not set from outside
|
---|
431 | if(cfg->debuglvl == cs_dblevel) {
|
---|
432 | switch (cs_dblevel) {
|
---|
433 | case 0:
|
---|
434 | cs_dblevel = 1;
|
---|
435 | break;
|
---|
436 | case 64:
|
---|
437 | cs_dblevel = 255;
|
---|
438 | break;
|
---|
439 | case 255:
|
---|
440 | cs_dblevel = 0;
|
---|
441 | break;
|
---|
442 | default:
|
---|
443 | cs_dblevel <<= 1;
|
---|
444 | }
|
---|
445 | } else {
|
---|
446 | cs_dblevel = cfg->debuglvl;
|
---|
447 | }
|
---|
448 |
|
---|
449 | cfg->debuglvl = cs_dblevel;
|
---|
450 |
|
---|
451 | if (master_pid == getpid())
|
---|
452 | for (i=0; i<CS_MAXPID && client[i].pid; i++)
|
---|
453 | client[i].dbglvl = cs_dblevel;
|
---|
454 | else
|
---|
455 | client[cs_idx].dbglvl = cs_dblevel;
|
---|
456 | cs_log("%sdebug_level=%d", (master_pid == getpid())?"all ":"", cs_dblevel);
|
---|
457 | }
|
---|
458 |
|
---|
459 | static void cs_card_info(int i)
|
---|
460 | {
|
---|
461 | uchar dummy[1]={0x00};
|
---|
462 |
|
---|
463 | for( i=1; i<CS_MAXPID; i++ )
|
---|
464 | if( client[i].pid && client[i].typ=='r' && client[i].fd_m2c ){
|
---|
465 | write_to_pipe(client[i].fd_m2c, PIP_ID_CIN, dummy, 1);
|
---|
466 | }
|
---|
467 |
|
---|
468 | //kill(client[i].pid, SIGUSR2);
|
---|
469 | }
|
---|
470 |
|
---|
471 | static void cs_child_chk(int i)
|
---|
472 | {
|
---|
473 | while (waitpid(0, NULL, WNOHANG)>0);
|
---|
474 | for (i=1; i<CS_MAXPID; i++)
|
---|
475 | if (client[i].pid)
|
---|
476 | if (kill(client[i].pid, 0)) {
|
---|
477 | if ((client[i].typ!='c') && (client[i].typ!='m'))
|
---|
478 | {
|
---|
479 | char *txt="";
|
---|
480 | *log_fd=0;
|
---|
481 | switch(client[i].typ)
|
---|
482 | {
|
---|
483 | #ifdef CS_ANTICASC
|
---|
484 | case 'a': txt="anticascader"; break;
|
---|
485 | #endif
|
---|
486 | case 'l': txt="logger"; break;
|
---|
487 | case 'p': txt="proxy"; break;
|
---|
488 | case 'r': txt="reader"; break;
|
---|
489 | case 'n': txt="resolver"; break;
|
---|
490 | #ifdef WEBIF
|
---|
491 | case 'h': txt="http"; break;
|
---|
492 | #endif
|
---|
493 | }
|
---|
494 | cs_log("PANIC: %s lost !! (pid=%d)", txt, client[i].pid);
|
---|
495 | cs_exit(1);
|
---|
496 | }
|
---|
497 | else
|
---|
498 | {
|
---|
499 | #ifdef CS_ANTICASC
|
---|
500 | char usr[32];
|
---|
501 | ushort ac_idx=0;
|
---|
502 | ushort ac_limit=0;
|
---|
503 | uchar ac_penalty=0;
|
---|
504 | if( cfg->ac_enabled )
|
---|
505 | {
|
---|
506 | cs_strncpy(usr, client[i].usr, sizeof(usr));
|
---|
507 | ac_idx = client[i].ac_idx;
|
---|
508 | ac_limit = client[i].ac_limit;
|
---|
509 | ac_penalty = client[i].ac_penalty;
|
---|
510 | }
|
---|
511 | #endif
|
---|
512 | if (client[i].fd_m2c) close(client[i].fd_m2c);
|
---|
513 | if (client[i].ufd) close(client[i].ufd);
|
---|
514 | memset(&client[i], 0, sizeof(struct s_client));
|
---|
515 | #ifdef CS_ANTICASC
|
---|
516 | if( cfg->ac_enabled )
|
---|
517 | {
|
---|
518 | client[i].ac_idx = ac_idx;
|
---|
519 | client[i].ac_limit = ac_limit;
|
---|
520 | client[i].ac_penalty = ac_penalty;
|
---|
521 | strcpy(client[i].usr, usr);
|
---|
522 | }
|
---|
523 | #endif
|
---|
524 | client[i].au=(-1);
|
---|
525 | }
|
---|
526 | }
|
---|
527 | return;
|
---|
528 | }
|
---|
529 |
|
---|
530 | int cs_fork(in_addr_t ip, in_port_t port)
|
---|
531 | {
|
---|
532 | int i;
|
---|
533 | pid_t pid;
|
---|
534 | for (i=1; (i<CS_MAXPID) && (client[i].pid); i++);
|
---|
535 | if (i<CS_MAXPID)
|
---|
536 | {
|
---|
537 | int fdp[2];
|
---|
538 | memset(&client[i], 0, sizeof(struct s_client));
|
---|
539 | client[i].au=(-1);
|
---|
540 | if (pipe(fdp))
|
---|
541 | {
|
---|
542 | cs_log("Cannot create pipe (errno=%d)", errno);
|
---|
543 | cs_exit(1);
|
---|
544 | }
|
---|
545 | switch(pid=fork())
|
---|
546 | {
|
---|
547 | case -1:
|
---|
548 | cs_log("PANIC: Cannot fork() (errno=%d)", errno);
|
---|
549 | cs_exit(1);
|
---|
550 | case 0: // HERE is client
|
---|
551 | alarm(0);
|
---|
552 | set_signal_handler(SIGALRM, 0, cs_alarm);
|
---|
553 | set_signal_handler(SIGCHLD, 1, SIG_IGN);
|
---|
554 | set_signal_handler(SIGHUP , 1, SIG_IGN);
|
---|
555 | set_signal_handler(SIGINT , 1, SIG_IGN);
|
---|
556 | set_signal_handler(SIGUSR1, 1, cs_debug_level);
|
---|
557 | is_server=((ip) || (port<90)) ? 1 : 0;
|
---|
558 | fd_m2c=fdp[0];
|
---|
559 | close(fdp[1]);
|
---|
560 | close(mfdr);
|
---|
561 | if( port!=97 ) cs_close_log();
|
---|
562 | mfdr=0;
|
---|
563 | cs_ptyp=D_CLIENT;
|
---|
564 | cs_idx=i;
|
---|
565 | #ifndef CS_NOSHM
|
---|
566 | shmid=0;
|
---|
567 | #endif
|
---|
568 | break;
|
---|
569 | default: // HERE is master
|
---|
570 | client[i].fd_m2c=fdp[1];
|
---|
571 | client[i].dbglvl=cs_dblevel;
|
---|
572 | close(fdp[0]);
|
---|
573 | if (ip)
|
---|
574 | {
|
---|
575 | client[i].typ='c'; // dynamic client
|
---|
576 | client[i].ip=ip;
|
---|
577 | client[i].port=port;
|
---|
578 | cs_log("client(%d) connect from %s (pid=%d, pipfd=%d)",
|
---|
579 | i-cdiff, cs_inet_ntoa(ip), pid, client[i].fd_m2c);
|
---|
580 | }
|
---|
581 | else
|
---|
582 | {
|
---|
583 | client[i].stat=1;
|
---|
584 | switch(port)
|
---|
585 | {
|
---|
586 | case 99: client[i].typ='r'; // reader
|
---|
587 | client[i].sidtabok=reader[ridx].sidtabok;
|
---|
588 | client[i].sidtabno=reader[ridx].sidtabno;
|
---|
589 | reader[ridx].fd=client[i].fd_m2c;
|
---|
590 | reader[ridx].cs_idx=i;
|
---|
591 | reader[ridx].pid=pid;
|
---|
592 | if (reader[ridx].r_port)
|
---|
593 | cs_log("proxy started (pid=%d, server=%s)",
|
---|
594 | pid, reader[ridx].device);
|
---|
595 | else
|
---|
596 | {
|
---|
597 | if (reader[ridx].typ==R_MOUSE || reader[ridx].typ==R_SMART)
|
---|
598 | cs_log("reader started (pid=%d, device=%s, detect=%s%s, mhz=%d, cardmhz=%d)",
|
---|
599 | pid, reader[ridx].device,
|
---|
600 | reader[ridx].detect&0x80 ? "!" : "",
|
---|
601 | RDR_CD_TXT[reader[ridx].detect&0x7f],
|
---|
602 | reader[ridx].mhz,
|
---|
603 | reader[ridx].cardmhz);
|
---|
604 | else
|
---|
605 | cs_log("reader started (pid=%d, device=%s)",
|
---|
606 | pid, reader[ridx].device);
|
---|
607 | client[i].ip=client[0].ip;
|
---|
608 | strcpy(client[i].usr, client[0].usr);
|
---|
609 | }
|
---|
610 | cdiff=i;
|
---|
611 | break;
|
---|
612 | case 98: client[i].typ='n'; // resolver
|
---|
613 | client[i].ip=client[0].ip;
|
---|
614 | strcpy(client[i].usr, client[0].usr);
|
---|
615 | cs_log("resolver started (pid=%d, delay=%d sec)",
|
---|
616 | pid, cfg->resolvedelay);
|
---|
617 | cdiff=i;
|
---|
618 | break;
|
---|
619 | case 97: client[i].typ='l'; // logger
|
---|
620 | client[i].ip=client[0].ip;
|
---|
621 | strcpy(client[i].usr, client[0].usr);
|
---|
622 | cs_log("logger started (pid=%d)", pid);
|
---|
623 | cdiff=i;
|
---|
624 | break;
|
---|
625 | #ifdef CS_ANTICASC
|
---|
626 | case 96: client[i].typ='a';
|
---|
627 | client[i].ip=client[0].ip;
|
---|
628 | strcpy(client[i].usr, client[0].usr);
|
---|
629 | cs_log("anticascader started (pid=%d, delay=%d min)",
|
---|
630 | pid, cfg->ac_stime);
|
---|
631 | cdiff=i;
|
---|
632 | break;
|
---|
633 | #endif
|
---|
634 |
|
---|
635 | #ifdef WEBIF
|
---|
636 | case 95: client[i].typ='h'; // http
|
---|
637 | client[i].ip=client[0].ip;
|
---|
638 | strcpy(client[i].usr, client[0].usr);
|
---|
639 | cs_log("http started (pid=%d)",pid);
|
---|
640 | cdiff=i;
|
---|
641 | break;
|
---|
642 | #endif
|
---|
643 |
|
---|
644 | default: client[i].typ='c'; // static client
|
---|
645 | client[i].ip=client[0].ip;
|
---|
646 | client[i].ctyp=port;
|
---|
647 | cs_log("%s: initialized (pid=%d%s)", ph[port].desc,
|
---|
648 | pid, ph[port].logtxt ? ph[port].logtxt : "");
|
---|
649 | break;
|
---|
650 | }
|
---|
651 | }
|
---|
652 | client[i].login=client[i].last=time((time_t *)0);
|
---|
653 | client[i].pid=pid; // MUST be last -> wait4master()
|
---|
654 | cs_last_idx=i;
|
---|
655 | i=0;
|
---|
656 | }
|
---|
657 | }
|
---|
658 | else
|
---|
659 | {
|
---|
660 | cs_log("max connections reached -> reject client %s", cs_inet_ntoa(ip));
|
---|
661 | i=(-1);
|
---|
662 | }
|
---|
663 | return(i);
|
---|
664 | }
|
---|
665 |
|
---|
666 | static void init_signal()
|
---|
667 | {
|
---|
668 | int i;
|
---|
669 | for (i=1; i<NSIG; i++)
|
---|
670 | set_signal_handler(i, 3, cs_exit);
|
---|
671 | set_signal_handler(SIGWINCH, 1, SIG_IGN);
|
---|
672 | // set_signal_handler(SIGPIPE , 0, SIG_IGN);
|
---|
673 | set_signal_handler(SIGPIPE , 0, cs_sigpipe);
|
---|
674 | // set_signal_handler(SIGALRM , 0, cs_alarm);
|
---|
675 | set_signal_handler(SIGALRM , 0, cs_master_alarm);
|
---|
676 | set_signal_handler(SIGCHLD , 1, cs_child_chk);
|
---|
677 | // set_signal_handler(SIGHUP , 1, cs_accounts_chk);
|
---|
678 | set_signal_handler(SIGHUP , 1, cs_sighup);
|
---|
679 | set_signal_handler(SIGUSR1, 1, cs_debug_level);
|
---|
680 | set_signal_handler(SIGUSR2, 1, cs_card_info);
|
---|
681 | set_signal_handler(SIGCONT, 1, SIG_IGN);
|
---|
682 | cs_log("signal handling initialized (type=%s)",
|
---|
683 | #ifdef CS_SIGBSD
|
---|
684 | "bsd"
|
---|
685 | #else
|
---|
686 | "sysv"
|
---|
687 | #endif
|
---|
688 | );
|
---|
689 | return;
|
---|
690 | }
|
---|
691 |
|
---|
692 | static void init_shm()
|
---|
693 | {
|
---|
694 | #ifdef CS_NOSHM
|
---|
695 | //int i, fd;
|
---|
696 | char *buf;
|
---|
697 | if ((shmid=open(cs_memfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))<0)
|
---|
698 | {
|
---|
699 | fprintf(stderr, "Cannot create mmaped file (errno=%d)", errno);
|
---|
700 | cs_exit(1);
|
---|
701 | }
|
---|
702 |
|
---|
703 | buf=(char *)malloc(shmsize);
|
---|
704 | memset(buf, 0, shmsize);
|
---|
705 | if (!write(shmid, buf, shmsize)) cs_exit(1);
|
---|
706 | free(buf);
|
---|
707 |
|
---|
708 | ecmcache=(struct s_ecm *)mmap((void *)0, (size_t) shmsize,
|
---|
709 | PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0);
|
---|
710 | #else
|
---|
711 | struct shmid_ds sd;
|
---|
712 | char *shmerr_txt="Cannot %s shared memory (errno=%d)\n";
|
---|
713 | if ((shmid=shmget(IPC_PRIVATE, shmsize, IPC_CREAT | 0600))<0)
|
---|
714 | {
|
---|
715 | fprintf(stderr, shmerr_txt, "create", errno);
|
---|
716 | shmid=0;
|
---|
717 | cs_exit(1);
|
---|
718 | }
|
---|
719 | if ((ecmcache=(struct s_ecm *)shmat(shmid, 0, 0))==(void *)(-1))
|
---|
720 | {
|
---|
721 | fprintf(stderr, shmerr_txt, "attach", errno);
|
---|
722 | cs_exit(1);
|
---|
723 | }
|
---|
724 | memset(ecmcache, 0, shmsize);
|
---|
725 | shmctl(shmid, IPC_RMID, &sd);
|
---|
726 | #endif
|
---|
727 | #ifdef CS_ANTICASC
|
---|
728 | acasc=(struct s_acasc_shm *)&ecmcache[CS_ECMCACHESIZE];
|
---|
729 | ecmidx=(int *)&acasc[CS_MAXPID];
|
---|
730 | #else
|
---|
731 | ecmidx=(int *)&ecmcache[CS_ECMCACHESIZE];
|
---|
732 | #endif
|
---|
733 | mcl=(int *)((void *)ecmidx+sizeof(int));
|
---|
734 | logidx=(int *)((void *)mcl+sizeof(int));
|
---|
735 | c_start=(int *)((void *)logidx+sizeof(int));
|
---|
736 | log_fd=(int *)((void *)c_start+sizeof(int));
|
---|
737 | oscam_sem=(int *)((void *)log_fd+sizeof(int));
|
---|
738 | client=(struct s_client *)((void *)oscam_sem+sizeof(int));
|
---|
739 | reader=(struct s_reader *)&client[CS_MAXPID];
|
---|
740 | #ifdef CS_WITH_GBOX
|
---|
741 | Cards=(struct card_struct*)&reader[CS_MAXREADER];
|
---|
742 | IgnoreList=(unsigned long*)&Cards[CS_MAXCARDS];
|
---|
743 | idstore=(struct idstore_struct*)&IgnoreList[CS_MAXIGNORE];
|
---|
744 | cfg=(struct s_config *)&idstore[CS_MAXPID];
|
---|
745 | #else
|
---|
746 | cfg=(struct s_config *)&reader[CS_MAXREADER];
|
---|
747 | #endif
|
---|
748 | #ifdef CS_LOGHISTORY
|
---|
749 | loghistidx=(int *)((void *)cfg+sizeof(struct s_config));
|
---|
750 | loghist=(char *)((void *)loghistidx+sizeof(int));
|
---|
751 | #endif
|
---|
752 |
|
---|
753 | #ifdef DEBUG_SHM_POINTER
|
---|
754 | printf("SHM ALLOC: %x\n", shmsize);
|
---|
755 | printf("SHM START: %p\n", (void *) ecmcache);
|
---|
756 | printf("SHM ST1: %p %x (%x)\n", (void *) ecmidx, ((void *) ecmidx) - ((void *) ecmcache), CS_ECMCACHESIZE*(sizeof(struct s_ecm)));
|
---|
757 | printf("SHM ST2: %p %x (%x)\n", (void *) oscam_sem, ((void *) oscam_sem) - ((void *) ecmidx), sizeof(int));
|
---|
758 | printf("SHM ST3: %p %x (%x)\n", (void *) client, ((void *) client) - ((void *) oscam_sem), sizeof(int));
|
---|
759 | printf("SHM ST4: %p %x (%x)\n", (void *) reader, ((void *) reader) - ((void *) client), CS_MAXPID*(sizeof(struct s_client)));
|
---|
760 | printf("SHM ST5: %p %x (%x)\n", (void *) cfg, ((void *) cfg) - ((void *) reader), CS_MAXREADER*(sizeof(struct s_reader)));
|
---|
761 | printf("SHM ST6: %p %x (%x)\n", ((void *) cfg)+sizeof(struct s_config), sizeof(struct s_config), sizeof(struct s_config));
|
---|
762 | printf("SHM ENDE: %p\n", ((void *) cfg)+sizeof(struct s_config));
|
---|
763 | printf("SHM SIZE: %x\n", ((void *) cfg)-((void *) ecmcache) + sizeof(struct s_config));
|
---|
764 | fflush(stdout);
|
---|
765 | #endif
|
---|
766 |
|
---|
767 | *ecmidx=0;
|
---|
768 | *logidx=0;
|
---|
769 | *oscam_sem=0;
|
---|
770 | client[0].pid=getpid();
|
---|
771 | client[0].login=time((time_t *)0);
|
---|
772 | client[0].ip=cs_inet_addr("127.0.0.1");
|
---|
773 | client[0].typ='s';
|
---|
774 | client[0].au=(-1);
|
---|
775 | client[0].dbglvl=cs_dblevel;
|
---|
776 | strcpy(client[0].usr, "root");
|
---|
777 | #ifdef CS_LOGHISTORY
|
---|
778 | *loghistidx=0;
|
---|
779 | memset(loghist, 0, CS_MAXLOGHIST*CS_LOGHISTSIZE);
|
---|
780 | #endif
|
---|
781 | }
|
---|
782 |
|
---|
783 | static int start_listener(struct s_module *ph, int port_idx)
|
---|
784 | {
|
---|
785 | int ov=1, timeout, is_udp, i;
|
---|
786 | char ptxt[2][32];
|
---|
787 | //struct hostent *ptrh; /* pointer to a host table entry */
|
---|
788 | struct protoent *ptrp; /* pointer to a protocol table entry */
|
---|
789 | struct sockaddr_in sad; /* structure to hold server's address */
|
---|
790 |
|
---|
791 | ptxt[0][0]=ptxt[1][0]='\0';
|
---|
792 | if (!ph->ptab->ports[port_idx].s_port)
|
---|
793 | {
|
---|
794 | cs_log("%s: disabled", ph->desc);
|
---|
795 | return(0);
|
---|
796 | }
|
---|
797 | is_udp=(ph->type==MOD_CONN_UDP);
|
---|
798 |
|
---|
799 | memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
|
---|
800 | sad.sin_family = AF_INET; /* set family to Internet */
|
---|
801 | if (!ph->s_ip)
|
---|
802 | ph->s_ip=cfg->srvip;
|
---|
803 | if (ph->s_ip)
|
---|
804 | {
|
---|
805 | sad.sin_addr.s_addr=ph->s_ip;
|
---|
806 | sprintf(ptxt[0], ", ip=%s", inet_ntoa(sad.sin_addr));
|
---|
807 | }
|
---|
808 | else
|
---|
809 | sad.sin_addr.s_addr=INADDR_ANY;
|
---|
810 | timeout=cfg->bindwait;
|
---|
811 | //ph->fd=0;
|
---|
812 | ph->ptab->ports[port_idx].fd = 0;
|
---|
813 |
|
---|
814 | if (ph->ptab->ports[port_idx].s_port > 0) /* test for illegal value */
|
---|
815 | sad.sin_port = htons((u_short)ph->ptab->ports[port_idx].s_port);
|
---|
816 | else
|
---|
817 | {
|
---|
818 | cs_log("%s: Bad port %d", ph->desc, ph->ptab->ports[port_idx].s_port);
|
---|
819 | return(0);
|
---|
820 | }
|
---|
821 |
|
---|
822 | /* Map transport protocol name to protocol number */
|
---|
823 |
|
---|
824 | if( (ptrp=getprotobyname(is_udp ? "udp" : "tcp")) )
|
---|
825 | ov=ptrp->p_proto;
|
---|
826 | else
|
---|
827 | ov=(is_udp) ? 17 : 6; // use defaults on error
|
---|
828 |
|
---|
829 | if ((ph->ptab->ports[port_idx].fd=socket(PF_INET,is_udp ? SOCK_DGRAM : SOCK_STREAM, ov))<0)
|
---|
830 | {
|
---|
831 | cs_log("%s: Cannot create socket (errno=%d)", ph->desc, errno);
|
---|
832 | return(0);
|
---|
833 | }
|
---|
834 |
|
---|
835 | ov=1;
|
---|
836 | if (setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_REUSEADDR, (void *)&ov, sizeof(ov))<0)
|
---|
837 | {
|
---|
838 | cs_log("%s: setsockopt failed (errno=%d)", ph->desc, errno);
|
---|
839 | close(ph->ptab->ports[port_idx].fd);
|
---|
840 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
841 | }
|
---|
842 |
|
---|
843 | #ifdef SO_REUSEPORT
|
---|
844 | setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_REUSEPORT, (void *)&ov, sizeof(ov));
|
---|
845 | #endif
|
---|
846 |
|
---|
847 | #ifdef SO_PRIORITY
|
---|
848 | if (cfg->netprio)
|
---|
849 | if (!setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg->netprio, sizeof(ulong)))
|
---|
850 | sprintf(ptxt[1], ", prio=%ld", cfg->netprio);
|
---|
851 | #endif
|
---|
852 |
|
---|
853 | if( !is_udp )
|
---|
854 | {
|
---|
855 | ulong keep_alive = 1;
|
---|
856 | setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
857 | (void *)&keep_alive, sizeof(ulong));
|
---|
858 | }
|
---|
859 |
|
---|
860 | while (timeout--)
|
---|
861 | {
|
---|
862 | if (bind(ph->ptab->ports[port_idx].fd, (struct sockaddr *)&sad, sizeof (sad))<0)
|
---|
863 | {
|
---|
864 | if (timeout)
|
---|
865 | {
|
---|
866 | cs_log("%s: Bind request failed, waiting another %d seconds",
|
---|
867 | ph->desc, timeout);
|
---|
868 | cs_sleepms(1000);
|
---|
869 | }
|
---|
870 | else
|
---|
871 | {
|
---|
872 | cs_log("%s: Bind request failed, giving up", ph->desc);
|
---|
873 | close(ph->ptab->ports[port_idx].fd);
|
---|
874 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
875 | }
|
---|
876 | }
|
---|
877 | else timeout=0;
|
---|
878 | }
|
---|
879 |
|
---|
880 | if (!is_udp)
|
---|
881 | if (listen(ph->ptab->ports[port_idx].fd, CS_QLEN)<0)
|
---|
882 | {
|
---|
883 | cs_log("%s: Cannot start listen mode (errno=%d)", ph->desc, errno);
|
---|
884 | close(ph->ptab->ports[port_idx].fd);
|
---|
885 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
886 | }
|
---|
887 |
|
---|
888 | cs_log("%s: initialized (fd=%d, port=%d%s%s%s)",
|
---|
889 | ph->desc, ph->ptab->ports[port_idx].fd,
|
---|
890 | ph->ptab->ports[port_idx].s_port,
|
---|
891 | ptxt[0], ptxt[1], ph->logtxt ? ph->logtxt : "");
|
---|
892 |
|
---|
893 | for( i=0; i<ph->ptab->ports[port_idx].ftab.nfilts; i++ ) {
|
---|
894 | int j;
|
---|
895 | cs_log("CAID: %04X", ph->ptab->ports[port_idx].ftab.filts[i].caid );
|
---|
896 | for( j=0; j<ph->ptab->ports[port_idx].ftab.filts[i].nprids; j++ )
|
---|
897 | cs_log("provid #%d: %06X", j, ph->ptab->ports[port_idx].ftab.filts[i].prids[j]);
|
---|
898 | }
|
---|
899 | return(ph->ptab->ports[port_idx].fd);
|
---|
900 | }
|
---|
901 |
|
---|
902 | static void cs_client_resolve()
|
---|
903 | {
|
---|
904 | while (1)
|
---|
905 | {
|
---|
906 | struct hostent *rht;
|
---|
907 | struct s_auth *account;
|
---|
908 | struct sockaddr_in udp_sa;
|
---|
909 |
|
---|
910 | for (account=cfg->account; account; account=account->next)
|
---|
911 | if (account->dyndns[0])
|
---|
912 | {
|
---|
913 | rht=gethostbyname((const char *)account->dyndns);
|
---|
914 | if (rht)
|
---|
915 | {
|
---|
916 | memcpy(&udp_sa.sin_addr, rht->h_addr, sizeof(udp_sa.sin_addr));
|
---|
917 | account->dynip=cs_inet_order(udp_sa.sin_addr.s_addr);
|
---|
918 | }
|
---|
919 | else
|
---|
920 | cs_log("can't resolve hostname %s (user: %s)", account->dyndns, account->usr);
|
---|
921 | client[cs_idx].last=time((time_t)0);
|
---|
922 | }
|
---|
923 | sleep(cfg->resolvedelay);
|
---|
924 | }
|
---|
925 | }
|
---|
926 |
|
---|
927 | static void start_client_resolver()
|
---|
928 | {
|
---|
929 | int i;
|
---|
930 | pthread_t tid;
|
---|
931 |
|
---|
932 | i=pthread_create(&tid, (pthread_attr_t *)0, (void *)&cs_client_resolve, (void *) 0);
|
---|
933 | if (i)
|
---|
934 | cs_log("ERROR: can't create resolver-thread (err=%d)", i);
|
---|
935 | else
|
---|
936 | {
|
---|
937 | cs_log("resolver thread started");
|
---|
938 | pthread_detach(tid);
|
---|
939 | }
|
---|
940 | }
|
---|
941 |
|
---|
942 | void cs_resolve()
|
---|
943 | {
|
---|
944 | int i, idx;
|
---|
945 | struct hostent *rht;
|
---|
946 | struct s_auth;
|
---|
947 | for (i=0; i<CS_MAXREADER; i++)
|
---|
948 | if ((idx=reader[i].cs_idx) && (reader[i].typ & R_IS_NETWORK))
|
---|
949 | {
|
---|
950 | client[cs_idx].last=time((time_t)0);
|
---|
951 | rht=gethostbyname(reader[i].device);
|
---|
952 | if (rht)
|
---|
953 | {
|
---|
954 | memcpy(&client[idx].udp_sa.sin_addr, rht->h_addr,
|
---|
955 | sizeof(client[idx].udp_sa.sin_addr));
|
---|
956 | client[idx].ip=cs_inet_order(client[idx].udp_sa.sin_addr.s_addr);
|
---|
957 | }
|
---|
958 | else
|
---|
959 | cs_log("can't resolve %s", reader[i].device);
|
---|
960 | client[cs_idx].last=time((time_t)0);
|
---|
961 | }
|
---|
962 | }
|
---|
963 |
|
---|
964 | static void cs_logger(void)
|
---|
965 | {
|
---|
966 | *log_fd=client[cs_idx].fd_m2c;
|
---|
967 | while(1)
|
---|
968 | {
|
---|
969 | uchar *ptr;
|
---|
970 | //struct timeval tv;
|
---|
971 | fd_set fds;
|
---|
972 |
|
---|
973 | FD_ZERO(&fds);
|
---|
974 | FD_SET(fd_m2c, &fds);
|
---|
975 | select(fd_m2c+1, &fds, 0, 0, 0);
|
---|
976 |
|
---|
977 | if (master_pid!=getppid())
|
---|
978 | cs_exit(0);
|
---|
979 |
|
---|
980 | if (FD_ISSET(fd_m2c, &fds))
|
---|
981 | {
|
---|
982 | int n;
|
---|
983 | // switch(n=read_from_pipe(fd_m2c, &ptr, 1))
|
---|
984 | n=read_from_pipe(fd_m2c, &ptr, 1);
|
---|
985 | //if (n!=PIP_ID_NUL) printf("received %d bytes\n", n); fflush(stdout);
|
---|
986 | switch(n)
|
---|
987 | {
|
---|
988 | case PIP_ID_LOG:
|
---|
989 | cs_write_log((char *)ptr);
|
---|
990 | break;
|
---|
991 | }
|
---|
992 | }
|
---|
993 | }
|
---|
994 | }
|
---|
995 |
|
---|
996 | static void start_resolver()
|
---|
997 | {
|
---|
998 | int i;
|
---|
999 |
|
---|
1000 | cs_sleepms(1000); // wait for reader
|
---|
1001 | while(1)
|
---|
1002 | {
|
---|
1003 | if (master_pid!=getppid())
|
---|
1004 | cs_exit(0);
|
---|
1005 | cs_resolve();
|
---|
1006 | for (i=0; i<cfg->resolvedelay; i++)
|
---|
1007 | if (master_pid!=getppid())
|
---|
1008 | cs_exit(0);
|
---|
1009 | else
|
---|
1010 | cs_sleepms(1000);
|
---|
1011 | // sleep(cfg->resolvedelay);
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | #ifdef CS_ANTICASC
|
---|
1016 | static void start_anticascader()
|
---|
1017 | {
|
---|
1018 | int i;
|
---|
1019 |
|
---|
1020 | use_ac_log=1;
|
---|
1021 | set_signal_handler(SIGHUP, 1, ac_init_stat);
|
---|
1022 |
|
---|
1023 | ac_init_stat();
|
---|
1024 | while(1)
|
---|
1025 | {
|
---|
1026 | for( i=0; i<cfg->ac_stime*60; i++ )
|
---|
1027 | if( master_pid!=getppid() )
|
---|
1028 | cs_exit(0);
|
---|
1029 | else
|
---|
1030 | cs_sleepms(1000);
|
---|
1031 |
|
---|
1032 | if (master_pid!=getppid())
|
---|
1033 | cs_exit(0);
|
---|
1034 |
|
---|
1035 | ac_do_stat();
|
---|
1036 | }
|
---|
1037 | }
|
---|
1038 | #endif
|
---|
1039 |
|
---|
1040 | #ifdef WEBIF
|
---|
1041 | static void cs_http()
|
---|
1042 | {
|
---|
1043 | http_srv();
|
---|
1044 | }
|
---|
1045 | #endif
|
---|
1046 |
|
---|
1047 | static void init_cardreader()
|
---|
1048 | {
|
---|
1049 | for (ridx=0; ridx<CS_MAXREADER; ridx++)
|
---|
1050 | if ((reader[ridx].device[0]) && (reader[ridx].enable == 1))
|
---|
1051 | switch(cs_fork(0, 99)) {
|
---|
1052 | case -1:
|
---|
1053 | cs_exit(1);
|
---|
1054 | case 0:
|
---|
1055 | break;
|
---|
1056 | default:
|
---|
1057 |
|
---|
1058 | wait4master();
|
---|
1059 | start_cardreader();
|
---|
1060 | }
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | static void init_service(int srv)
|
---|
1064 | {
|
---|
1065 | switch(cs_fork(0, srv))
|
---|
1066 | {
|
---|
1067 | case -1:
|
---|
1068 | cs_exit(1);
|
---|
1069 | case 0:
|
---|
1070 | break;
|
---|
1071 | default:
|
---|
1072 | wait4master();
|
---|
1073 | switch(srv)
|
---|
1074 | {
|
---|
1075 | #ifdef CS_ANTICASC
|
---|
1076 | case 96: start_anticascader();
|
---|
1077 | #endif
|
---|
1078 | case 97: cs_logger();
|
---|
1079 | case 98: start_resolver();
|
---|
1080 | #ifdef WEBIF
|
---|
1081 | case 95: cs_http();
|
---|
1082 | #endif
|
---|
1083 | }
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | void wait4master()
|
---|
1088 | {
|
---|
1089 | int i;
|
---|
1090 | for (i=0; (i<1000) && (client[cs_idx].pid!=getpid()); i++)
|
---|
1091 | cs_sleepms(1);
|
---|
1092 | if (client[cs_idx].pid!=getpid())
|
---|
1093 | {
|
---|
1094 | cs_log("PANIC: client not found in shared memory");
|
---|
1095 | cs_exit(1);
|
---|
1096 | }
|
---|
1097 | cs_debug("starting client %d with ip %s",
|
---|
1098 | cs_idx-cdiff, cs_inet_ntoa(client[cs_idx].ip));
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | static void cs_fake_client(char *usr, int uniq, in_addr_t ip)
|
---|
1102 | {
|
---|
1103 | /* Uniq = 1: only one connection per user
|
---|
1104 | *
|
---|
1105 | * Uniq = 2: set (new connected) user only to fake if source
|
---|
1106 | * ip is different (e.g. for newcamd clients with
|
---|
1107 | * different CAID's -> Ports)
|
---|
1108 | *
|
---|
1109 | * Uniq = 3: only one connection per user, but only the last
|
---|
1110 | * login will survive (old mpcs behavior)
|
---|
1111 | *
|
---|
1112 | * Uniq = 4: set user only to fake if source ip is
|
---|
1113 | * different, but only the last login will survive
|
---|
1114 | */
|
---|
1115 |
|
---|
1116 | int i;
|
---|
1117 | for (i=cdiff+1; i<CS_MAXPID; i++)
|
---|
1118 | {
|
---|
1119 | if (client[i].pid && (client[i].typ == 'c') && !client[i].dup && !strcmp(client[i].usr, usr)
|
---|
1120 | && (uniq < 5) && ((uniq % 2) || (client[i].ip != ip)))
|
---|
1121 | {
|
---|
1122 | if (uniq == 3 || uniq == 4)
|
---|
1123 | {
|
---|
1124 | client[i].dup = 1;
|
---|
1125 | client[i].au = -1;
|
---|
1126 | cs_log("client(%d) duplicate user '%s' from %s set to fake (uniq=%d)", i-cdiff, usr, cs_inet_ntoa(ip), uniq);
|
---|
1127 | }
|
---|
1128 | else
|
---|
1129 | {
|
---|
1130 | client[cs_idx].dup = 1;
|
---|
1131 | client[cs_idx].au = -1;
|
---|
1132 | cs_log("client(%d) duplicate user '%s' from %s set to fake (uniq=%d)", cs_idx-cdiff, usr, cs_inet_ntoa(ip), uniq);
|
---|
1133 | break;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | }
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | int cs_auth_client(struct s_auth *account, char *e_txt)
|
---|
1142 | {
|
---|
1143 | int rc=0;
|
---|
1144 | char buf[32];
|
---|
1145 | char *t_crypt="encrypted";
|
---|
1146 | char *t_plain="plain";
|
---|
1147 | char *t_grant=" granted";
|
---|
1148 | char *t_reject=" rejected";
|
---|
1149 | char *t_msg[]= { buf, "invalid access", "invalid ip", "unknown reason" };
|
---|
1150 | client[cs_idx].grp=0xffffffff;
|
---|
1151 | client[cs_idx].au=(-1);
|
---|
1152 | switch((long)account)
|
---|
1153 | {
|
---|
1154 | case -2: // gbx-dummy
|
---|
1155 | client[cs_idx].dup=0;
|
---|
1156 | break;
|
---|
1157 | case 0: // reject access
|
---|
1158 | rc=1;
|
---|
1159 | cs_log("%s %s-client %s%s (%s)",
|
---|
1160 | client[cs_idx].crypted ? t_crypt : t_plain,
|
---|
1161 | ph[client[cs_idx].ctyp].desc,
|
---|
1162 | client[cs_idx].ip ? cs_inet_ntoa(client[cs_idx].ip) : "",
|
---|
1163 | client[cs_idx].ip ? t_reject : t_reject+1,
|
---|
1164 | e_txt ? e_txt : t_msg[rc]);
|
---|
1165 | break;
|
---|
1166 | default: // grant/check access
|
---|
1167 | if (client[cs_idx].ip && account->dyndns[0])
|
---|
1168 | if (client[cs_idx].ip != account->dynip)
|
---|
1169 | rc=2;
|
---|
1170 | if (!rc)
|
---|
1171 | {
|
---|
1172 | client[cs_idx].dup=0;
|
---|
1173 | if (client[cs_idx].typ=='c')
|
---|
1174 | {
|
---|
1175 | client[cs_idx].expirationdate=account->expirationdate;
|
---|
1176 | client[cs_idx].disabled=account->disabled;
|
---|
1177 | client[cs_idx].c35_suppresscmd08 = account->c35_suppresscmd08;
|
---|
1178 | client[cs_idx].ncd_keepalive = account->ncd_keepalive;
|
---|
1179 | client[cs_idx].grp=account->grp;
|
---|
1180 | client[cs_idx].au=account->au;
|
---|
1181 | client[cs_idx].autoau=account->autoau;
|
---|
1182 | client[cs_idx].tosleep=(60*account->tosleep);
|
---|
1183 | memcpy(&client[cs_idx].ctab, &account->ctab, sizeof(client[cs_idx].ctab));
|
---|
1184 | if (account->uniq)
|
---|
1185 | cs_fake_client(account->usr, account->uniq, client[cs_idx].ip);
|
---|
1186 | client[cs_idx].ftab = account->ftab; // IDENT filter
|
---|
1187 | client[cs_idx].cltab = account->cltab; // CLASS filter
|
---|
1188 | client[cs_idx].fchid = account->fchid; // CHID filter
|
---|
1189 | client[cs_idx].sidtabok= account->sidtabok; // services
|
---|
1190 | client[cs_idx].sidtabno= account->sidtabno; // services
|
---|
1191 | client[cs_idx].pcrc = crc32(0L, MD5((uchar *)account->pwd, strlen(account->pwd), NULL), 16);
|
---|
1192 | memcpy(&client[cs_idx].ttab, &account->ttab, sizeof(client[cs_idx].ttab));
|
---|
1193 | #ifdef CS_ANTICASC
|
---|
1194 | ac_init_client(account);
|
---|
1195 | #endif
|
---|
1196 | }
|
---|
1197 | }
|
---|
1198 | client[cs_idx].monlvl=account->monlvl;
|
---|
1199 | strcpy(client[cs_idx].usr, account->usr);
|
---|
1200 | case -1: // anonymous grant access
|
---|
1201 | if (rc)
|
---|
1202 | t_grant=t_reject;
|
---|
1203 | else
|
---|
1204 | {
|
---|
1205 | if (client[cs_idx].typ=='m')
|
---|
1206 | sprintf(t_msg[0], "lvl=%d", client[cs_idx].monlvl);
|
---|
1207 | else
|
---|
1208 | {
|
---|
1209 | if(client[cs_idx].autoau)
|
---|
1210 | {
|
---|
1211 | if(client[cs_idx].ncd_server)
|
---|
1212 | {
|
---|
1213 | int r=0;
|
---|
1214 | for(r=0;r<CS_MAXREADER;r++)
|
---|
1215 | {
|
---|
1216 | if(reader[r].caid[0]==cfg->ncd_ptab.ports[client[cs_idx].port_idx].ftab.filts[0].caid)
|
---|
1217 | {
|
---|
1218 | client[cs_idx].au=r;
|
---|
1219 | break;
|
---|
1220 | }
|
---|
1221 | }
|
---|
1222 | if(client[cs_idx].au<0) sprintf(t_msg[0], "au(auto)=%d", client[cs_idx].au+1);
|
---|
1223 | else sprintf(t_msg[0], "au(auto)=%s", reader[client[cs_idx].au].label);
|
---|
1224 | }
|
---|
1225 | else
|
---|
1226 | {
|
---|
1227 | sprintf(t_msg[0], "au=auto");
|
---|
1228 | }
|
---|
1229 | }
|
---|
1230 | else
|
---|
1231 | {
|
---|
1232 | if(client[cs_idx].au<0) sprintf(t_msg[0], "au=%d", client[cs_idx].au+1);
|
---|
1233 | else sprintf(t_msg[0], "au=%s", reader[client[cs_idx].au].label);
|
---|
1234 | }
|
---|
1235 | }
|
---|
1236 | }
|
---|
1237 | if(client[cs_idx].ncd_server)
|
---|
1238 | {
|
---|
1239 | cs_log("%s %s:%d-client %s%s (%s, %s)",
|
---|
1240 | client[cs_idx].crypted ? t_crypt : t_plain,
|
---|
1241 | e_txt ? e_txt : ph[client[cs_idx].ctyp].desc,
|
---|
1242 | cfg->ncd_ptab.ports[client[cs_idx].port_idx].s_port,
|
---|
1243 | client[cs_idx].ip ? cs_inet_ntoa(client[cs_idx].ip) : "",
|
---|
1244 | client[cs_idx].ip ? t_grant : t_grant+1,
|
---|
1245 | username(cs_idx), t_msg[rc]);
|
---|
1246 | }
|
---|
1247 | else
|
---|
1248 | {
|
---|
1249 | cs_log("%s %s-client %s%s (%s, %s)",
|
---|
1250 | client[cs_idx].crypted ? t_crypt : t_plain,
|
---|
1251 | e_txt ? e_txt : ph[client[cs_idx].ctyp].desc,
|
---|
1252 | client[cs_idx].ip ? cs_inet_ntoa(client[cs_idx].ip) : "",
|
---|
1253 | client[cs_idx].ip ? t_grant : t_grant+1,
|
---|
1254 | username(cs_idx), t_msg[rc]);
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | break;
|
---|
1258 | }
|
---|
1259 | return(rc);
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | void cs_disconnect_client(void)
|
---|
1263 | {
|
---|
1264 | char buf[32]={0};
|
---|
1265 | if (client[cs_idx].ip)
|
---|
1266 | sprintf(buf, " from %s", cs_inet_ntoa(client[cs_idx].ip));
|
---|
1267 | cs_log("%s disconnected%s", username(cs_idx), buf);
|
---|
1268 | cs_exit(0);
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | int check_ecmcache(ECM_REQUEST *er, ulong grp)
|
---|
1272 | {
|
---|
1273 | int i;
|
---|
1274 | // cs_ddump(ecmd5, CS_ECMSTORESIZE, "ECM search");
|
---|
1275 | //cs_log("cache CHECK: grp=%lX", grp);
|
---|
1276 | for(i=0; i<CS_ECMCACHESIZE; i++)
|
---|
1277 | if ((grp & ecmcache[i].grp) &&
|
---|
1278 | ecmcache[i].caid==er->caid &&
|
---|
1279 | (!memcmp(ecmcache[i].ecmd5, er->ecmd5, CS_ECMSTORESIZE)))
|
---|
1280 | {
|
---|
1281 | //cs_log("cache found: grp=%lX cgrp=%lX", grp, ecmcache[i].grp);
|
---|
1282 | memcpy(er->cw, ecmcache[i].cw, 16);
|
---|
1283 | return(1);
|
---|
1284 | }
|
---|
1285 | return(0);
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | static void store_ecm(ECM_REQUEST *er)
|
---|
1289 | {
|
---|
1290 | int rc;
|
---|
1291 | rc=*ecmidx;
|
---|
1292 | *ecmidx=(*ecmidx+1) % CS_ECMCACHESIZE;
|
---|
1293 | //cs_log("store ecm from reader %d", er->reader[0]);
|
---|
1294 | memcpy(ecmcache[rc].ecmd5, er->ecmd5, CS_ECMSTORESIZE);
|
---|
1295 | memcpy(ecmcache[rc].cw, er->cw, 16);
|
---|
1296 | ecmcache[rc].caid = er->caid;
|
---|
1297 | ecmcache[rc].grp = reader[er->reader[0]].grp;
|
---|
1298 | //cs_ddump(ecmcache[*ecmidx].ecmd5, CS_ECMSTORESIZE, "ECM stored (idx=%d)", *ecmidx);
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | void store_logentry(char *txt)
|
---|
1302 | {
|
---|
1303 | #ifdef CS_LOGHISTORY
|
---|
1304 | char *ptr;
|
---|
1305 | ptr=(char *)(loghist+(*loghistidx*CS_LOGHISTSIZE));
|
---|
1306 | ptr[0]='\1'; // make username unusable
|
---|
1307 | ptr[1]='\0';
|
---|
1308 | if ((client[cs_idx].typ=='c') || (client[cs_idx].typ=='m'))
|
---|
1309 | cs_strncpy(ptr, client[cs_idx].usr, 31);
|
---|
1310 | cs_strncpy(ptr+32, txt, CS_LOGHISTSIZE-33);
|
---|
1311 | *loghistidx=(*loghistidx+1) % CS_MAXLOGHIST;
|
---|
1312 | #endif
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | /*
|
---|
1316 | * write_to_pipe():
|
---|
1317 | * write all kind of data to pipe specified by fd
|
---|
1318 | */
|
---|
1319 | int write_to_pipe(int fd, int id, uchar *data, int n)
|
---|
1320 | {
|
---|
1321 | uchar buf[1024+3+sizeof(int)];
|
---|
1322 |
|
---|
1323 | //printf("WRITE_START pid=%d", getpid()); fflush(stdout);
|
---|
1324 | if ((id<0) || (id>PIP_ID_MAX))
|
---|
1325 | return(PIP_ID_ERR);
|
---|
1326 | memcpy(buf, PIP_ID_TXT[id], 3);
|
---|
1327 | memcpy(buf+3, &n, sizeof(int));
|
---|
1328 | memcpy(buf+3+sizeof(int), data, n);
|
---|
1329 | n+=3+sizeof(int);
|
---|
1330 | //n=write(fd, buf, n);
|
---|
1331 | //printf("WRITE_END pid=%d", getpid()); fflush(stdout);
|
---|
1332 | //return(n);
|
---|
1333 | if( !fd )
|
---|
1334 | cs_log("write_to_pipe: fd==0");
|
---|
1335 | return(write(fd, buf, n));
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | /*
|
---|
1339 | * read_from_pipe():
|
---|
1340 | * read all kind of data from pipe specified by fd
|
---|
1341 | * special-flag redir: if set AND data is ECM: this will redirected to appr. client
|
---|
1342 | */
|
---|
1343 | int read_from_pipe(int fd, uchar **data, int redir)
|
---|
1344 | {
|
---|
1345 | int rc;
|
---|
1346 | static int hdr=0;
|
---|
1347 | static uchar buf[1024+1+3+sizeof(int)];
|
---|
1348 |
|
---|
1349 | *data=(uchar *)0;
|
---|
1350 | rc=PIP_ID_NUL;
|
---|
1351 |
|
---|
1352 | if (!hdr)
|
---|
1353 | {
|
---|
1354 | if (bytes_available(fd))
|
---|
1355 | {
|
---|
1356 | if (read(fd, buf, 3+sizeof(int))==3+sizeof(int))
|
---|
1357 | memcpy(&hdr, buf+3, sizeof(int));
|
---|
1358 | else
|
---|
1359 | cs_log("WARNING: pipe header to small !");
|
---|
1360 | }
|
---|
1361 | }
|
---|
1362 | if (hdr)
|
---|
1363 | {
|
---|
1364 | int l;
|
---|
1365 | for (l=0; (rc<0) && (PIP_ID_TXT[l]); l++)
|
---|
1366 | if (!memcmp(buf, PIP_ID_TXT[l], 3))
|
---|
1367 | rc=l;
|
---|
1368 |
|
---|
1369 | if (rc<0)
|
---|
1370 | {
|
---|
1371 | fprintf(stderr, "WARNING: pipe garbage");
|
---|
1372 | fflush(stderr);
|
---|
1373 | cs_log("WARNING: pipe garbage");
|
---|
1374 | rc=PIP_ID_ERR;
|
---|
1375 | }
|
---|
1376 | else
|
---|
1377 | {
|
---|
1378 | l=hdr;
|
---|
1379 | if ((l+3-1+sizeof(int))>sizeof(buf))
|
---|
1380 | {
|
---|
1381 | cs_log("WARNING: packet size (%d) to large", l);
|
---|
1382 | l=sizeof(buf)+3-1+sizeof(int);
|
---|
1383 | }
|
---|
1384 | if (!bytes_available(fd))
|
---|
1385 | return(PIP_ID_NUL);
|
---|
1386 | hdr=0;
|
---|
1387 | if (read(fd, buf+3+sizeof(int), l)==l)
|
---|
1388 | *data=buf+3+sizeof(int);
|
---|
1389 | else
|
---|
1390 | {
|
---|
1391 | cs_log("WARNING: pipe data to small !");
|
---|
1392 | return(PIP_ID_ERR);
|
---|
1393 | }
|
---|
1394 | buf[l+3+sizeof(int)]=0;
|
---|
1395 | if ((redir) && (rc==PIP_ID_ECM))
|
---|
1396 | {
|
---|
1397 | //int idx;
|
---|
1398 | ECM_REQUEST *er;
|
---|
1399 | er=(ECM_REQUEST *)(buf+3+sizeof(int));
|
---|
1400 | if( er->cidx && client[er->cidx].fd_m2c )
|
---|
1401 | if (!write(client[er->cidx].fd_m2c, buf, l+3+sizeof(int))) cs_exit(1);
|
---|
1402 | rc=PIP_ID_DIR;
|
---|
1403 | }
|
---|
1404 | }
|
---|
1405 | }
|
---|
1406 | return(rc);
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | /*
|
---|
1410 | * write_ecm_request():
|
---|
1411 | */
|
---|
1412 | int write_ecm_request(int fd, ECM_REQUEST *er)
|
---|
1413 | {
|
---|
1414 | return(write_to_pipe(fd, PIP_ID_ECM, (uchar *) er, sizeof(ECM_REQUEST)));
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | int write_ecm_DCW(int fd, ECM_REQUEST *er)
|
---|
1418 | {
|
---|
1419 | return(write_to_pipe(fd, PIP_ID_DCW, (uchar *) er, sizeof(ECM_REQUEST)));
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | /*
|
---|
1423 | * This function writes the current CW from ECM struct to a cwl file.
|
---|
1424 | * The filename is re-calculated and file re-opened every time.
|
---|
1425 | * This will consume a bit cpu time, but nothing has to be stored between
|
---|
1426 | * each call. If not file exists, a header is prepended
|
---|
1427 | */
|
---|
1428 | void logCWtoFile(ECM_REQUEST *er)
|
---|
1429 | {
|
---|
1430 | FILE *pfCWL;
|
---|
1431 | char srvname[128];
|
---|
1432 | /* %s / %s _I %04X _ %s .cwl */
|
---|
1433 | char buf[256 + sizeof(srvname)];
|
---|
1434 | char date[7];
|
---|
1435 | unsigned char i, parity, writeheader = 0;
|
---|
1436 | time_t t;
|
---|
1437 | struct tm *timeinfo;
|
---|
1438 | struct s_srvid *this;
|
---|
1439 |
|
---|
1440 | /*
|
---|
1441 | * search service name for that id and change characters
|
---|
1442 | * causing problems in file name
|
---|
1443 | */
|
---|
1444 | srvname[0] = 0;
|
---|
1445 | for (this=cfg->srvid; this; this = this->next) {
|
---|
1446 | if (this->srvid == er->srvid) {
|
---|
1447 | cs_strncpy(srvname, this->name, sizeof(srvname));
|
---|
1448 | srvname[sizeof(srvname)-1] = 0;
|
---|
1449 | for (i = 0; srvname[i]; i++)
|
---|
1450 | if (srvname[i] == ' ') srvname[i] = '_';
|
---|
1451 | break;
|
---|
1452 | }
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | /* calc log file name */
|
---|
1456 | time(&t);
|
---|
1457 | timeinfo = localtime(&t);
|
---|
1458 | strftime(date, sizeof(date), "%y%m%d", timeinfo);
|
---|
1459 | sprintf(buf, "%s/%s_I%04X_%s.cwl", cfg->cwlogdir, date, er->srvid, srvname);
|
---|
1460 |
|
---|
1461 | /* open failed, assuming file does not exist, yet */
|
---|
1462 | if((pfCWL = fopen(buf, "r")) == NULL) {
|
---|
1463 | writeheader = 1;
|
---|
1464 | } else {
|
---|
1465 | /* we need to close the file if it was opened correctly */
|
---|
1466 | fclose(pfCWL);
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | if ((pfCWL = fopen(buf, "a+")) == NULL) {
|
---|
1470 | /* maybe this fails because the subdir does not exist. Is there a common function to create it?
|
---|
1471 | for the moment do not print to log on every ecm
|
---|
1472 | cs_log(""error opening cw logfile for writing: %s (errno %d)", buf, errno); */
|
---|
1473 | return;
|
---|
1474 | }
|
---|
1475 | if (writeheader) {
|
---|
1476 | /* no global macro for cardserver name :( */
|
---|
1477 | fprintf(pfCWL, "# OSCam cardserver v%s - http://streamboard.gmc.to:8001/oscam/wiki\n", CS_VERSION_X);
|
---|
1478 | fprintf(pfCWL, "# control word log file for use with tsdec offline decrypter\n");
|
---|
1479 | strftime(buf, sizeof(buf),"DATE %Y-%m-%d, TIME %H:%M:%S, TZ %Z\n", timeinfo);
|
---|
1480 | fprintf(pfCWL, "# %s", buf);
|
---|
1481 | fprintf(pfCWL, "# CAID 0x%04X, SID 0x%04X, SERVICE \"%s\"\n", er->caid, er->srvid, srvname);
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | parity = er->ecm[0]&1;
|
---|
1485 | fprintf(pfCWL, "%d ", parity);
|
---|
1486 | for (i = parity * 8; i < 8 + parity * 8; i++)
|
---|
1487 | fprintf(pfCWL, "%02X ", er->cw[i]);
|
---|
1488 | /* better use incoming time er->tps rather than current time? */
|
---|
1489 | strftime(buf,sizeof(buf),"%H:%M:%S\n", timeinfo);
|
---|
1490 | fprintf(pfCWL, "# %s", buf);
|
---|
1491 | fflush(pfCWL);
|
---|
1492 | fclose(pfCWL);
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | int write_ecm_answer(int fd, ECM_REQUEST *er)
|
---|
1496 | {
|
---|
1497 | int i;
|
---|
1498 | uchar c;
|
---|
1499 | for (i=0; i<16; i+=4)
|
---|
1500 | {
|
---|
1501 | c=((er->cw[i]+er->cw[i+1]+er->cw[i+2]) & 0xff);
|
---|
1502 | if (er->cw[i+3]!=c)
|
---|
1503 | {
|
---|
1504 | cs_debug("notice: changed dcw checksum byte cw[%i] from %02x to %02x", i+3, er->cw[i+3],c);
|
---|
1505 | er->cw[i+3]=c;
|
---|
1506 | }
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | er->reader[0]=ridx;
|
---|
1510 | //cs_log("answer from reader %d (rc=%d)", er->reader[0], er->rc);
|
---|
1511 | er->caid=er->ocaid;
|
---|
1512 |
|
---|
1513 | #ifdef CS_WITH_GBOX
|
---|
1514 | if (er->rc==1||(er->gbxRidx&&er->rc==0)) {
|
---|
1515 | #else
|
---|
1516 | if (er->rc==1) {
|
---|
1517 | #endif
|
---|
1518 | store_ecm(er);
|
---|
1519 |
|
---|
1520 | /* CWL logging only if cwlogdir is set in config */
|
---|
1521 | if (cfg->cwlogdir != NULL)
|
---|
1522 | logCWtoFile(er);
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | return(write_ecm_request(fd, er));
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | /*
|
---|
1529 | static int cs_read_timer(int fd, uchar *buf, int l, int msec)
|
---|
1530 | {
|
---|
1531 | struct timeval tv;
|
---|
1532 | fd_set fds;
|
---|
1533 | int rc;
|
---|
1534 |
|
---|
1535 | if (!fd) return(-1);
|
---|
1536 | tv.tv_sec = msec / 1000;
|
---|
1537 | tv.tv_usec = (msec % 1000) * 1000;
|
---|
1538 | FD_ZERO(&fds);
|
---|
1539 | FD_SET(pfd, &fds);
|
---|
1540 |
|
---|
1541 | select(fd+1, &fds, 0, 0, &tv);
|
---|
1542 |
|
---|
1543 | rc=0;
|
---|
1544 | if (FD_ISSET(pfd, &fds))
|
---|
1545 | if (!(rc=read(fd, buf, l)))
|
---|
1546 | rc=-1;
|
---|
1547 |
|
---|
1548 | return(rc);
|
---|
1549 | }*/
|
---|
1550 |
|
---|
1551 | ECM_REQUEST *get_ecmtask()
|
---|
1552 | {
|
---|
1553 | int i, n;
|
---|
1554 | ECM_REQUEST *er=0;
|
---|
1555 |
|
---|
1556 | if (!ecmtask)
|
---|
1557 | {
|
---|
1558 | n=(ph[client[cs_idx].ctyp].multi)?CS_MAXPENDING:1;
|
---|
1559 | if( (ecmtask=(ECM_REQUEST *)malloc(n*sizeof(ECM_REQUEST))) )
|
---|
1560 | memset(ecmtask, 0, n*sizeof(ECM_REQUEST));
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | n=(-1);
|
---|
1564 | if (!ecmtask)
|
---|
1565 | {
|
---|
1566 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
1567 | n=(-2);
|
---|
1568 | }
|
---|
1569 | else
|
---|
1570 | if (ph[client[cs_idx].ctyp].multi)
|
---|
1571 | {
|
---|
1572 | for (i=0; (n<0) && (i<CS_MAXPENDING); i++)
|
---|
1573 | if (ecmtask[i].rc<100)
|
---|
1574 | er=&ecmtask[n=i];
|
---|
1575 | }
|
---|
1576 | else
|
---|
1577 | er=&ecmtask[n=0];
|
---|
1578 |
|
---|
1579 | if (n<0)
|
---|
1580 | cs_log("WARNING: ecm pending table overflow !");
|
---|
1581 | else
|
---|
1582 | {
|
---|
1583 | memset(er, 0, sizeof(ECM_REQUEST));
|
---|
1584 | er->rc=100;
|
---|
1585 | er->cpti=n;
|
---|
1586 | er->cidx=cs_idx;
|
---|
1587 | cs_ftime(&er->tps);
|
---|
1588 | }
|
---|
1589 | return(er);
|
---|
1590 | }
|
---|
1591 |
|
---|
1592 | int send_dcw(ECM_REQUEST *er)
|
---|
1593 | {
|
---|
1594 | static char *stxt[]={"found", "cache1", "cache2", "emu",
|
---|
1595 | "not found", "timeout", "sleeping",
|
---|
1596 | "fake", "invalid", "corrupt", "no card", "expdate", "disabled"};
|
---|
1597 | static char *stxtEx[]={"", "group", "caid", "ident", "class", "chid", "queue", "peer"};
|
---|
1598 | static char *stxtWh[]={"", "user ", "reader ", "server ", "lserver "};
|
---|
1599 | char sby[32]="";
|
---|
1600 | char erEx[32]="";
|
---|
1601 | char uname[38]="";
|
---|
1602 | struct timeb tpe;
|
---|
1603 | ushort lc, *lp;
|
---|
1604 | for (lp=(ushort *)er->ecm+(er->l>>2), lc=0; lp>=(ushort *)er->ecm; lp--)
|
---|
1605 | lc^=*lp;
|
---|
1606 | cs_ftime(&tpe);
|
---|
1607 |
|
---|
1608 | #ifdef CS_WITH_GBOX
|
---|
1609 | if(er->gbxFrom)
|
---|
1610 | snprintf(uname,sizeof(uname)-1, "%s(%04X)", username(cs_idx), er->gbxFrom);
|
---|
1611 | else
|
---|
1612 | #endif
|
---|
1613 | snprintf(uname,sizeof(uname)-1, "%s", username(cs_idx));
|
---|
1614 | if (er->rc==0)
|
---|
1615 | {
|
---|
1616 | #ifdef CS_WITH_GBOX
|
---|
1617 | if(reader[er->reader[0]].typ==R_GBOX)
|
---|
1618 | snprintf(sby, sizeof(sby)-1, " by %s(%04X)", reader[er->reader[0]].label,er->gbxCWFrom);
|
---|
1619 | else
|
---|
1620 | #endif
|
---|
1621 | // add marker to reader if ECM_REQUEST was betatunneled
|
---|
1622 | if(er->btun)
|
---|
1623 | snprintf(sby, sizeof(sby)-1, " by %s(btun)", reader[er->reader[0]].label);
|
---|
1624 | else
|
---|
1625 | snprintf(sby, sizeof(sby)-1, " by %s", reader[er->reader[0]].label);
|
---|
1626 | }
|
---|
1627 | if (er->rc<4) er->rcEx=0;
|
---|
1628 | if (er->rcEx)
|
---|
1629 | snprintf(erEx, sizeof(erEx)-1, "rejected %s%s", stxtWh[er->rcEx>>4],
|
---|
1630 | stxtEx[er->rcEx&0xf]);
|
---|
1631 |
|
---|
1632 | client[cs_idx].cwlastresptime = 1000*(tpe.time-er->tps.time)+tpe.millitm-er->tps.millitm;
|
---|
1633 |
|
---|
1634 | if(cfg->mon_appendchaninfo)
|
---|
1635 | cs_log("%s (%04X&%06X/%04X/%02X:%04X): %s (%d ms)%s - %s",
|
---|
1636 | uname, er->caid, er->prid, er->srvid, er->l, lc,
|
---|
1637 | er->rcEx?erEx:stxt[er->rc], client[cs_idx].cwlastresptime, sby, monitor_get_srvname(er->srvid, er->caid));
|
---|
1638 | else
|
---|
1639 | cs_log("%s (%04X&%06X/%04X/%02X:%04X): %s (%d ms)%s",
|
---|
1640 | uname, er->caid, er->prid, er->srvid, er->l, lc,
|
---|
1641 | er->rcEx?erEx:stxt[er->rc], client[cs_idx].cwlastresptime, sby);
|
---|
1642 |
|
---|
1643 | if(!client[cs_idx].ncd_server && client[cs_idx].autoau && er->rcEx==0)
|
---|
1644 | {
|
---|
1645 | if(client[cs_idx].au>=0 && er->caid!=reader[client[cs_idx].au].caid[0])
|
---|
1646 | {
|
---|
1647 | client[cs_idx].au=(-1);
|
---|
1648 | }
|
---|
1649 |
|
---|
1650 | client[cs_idx].au=er->reader[0];
|
---|
1651 | if(client[cs_idx].au<0)
|
---|
1652 | {
|
---|
1653 | int r=0;
|
---|
1654 | for(r=0;r<CS_MAXREADER;r++)
|
---|
1655 | {
|
---|
1656 | if(er->caid==reader[r].caid[0])
|
---|
1657 | {
|
---|
1658 | client[cs_idx].au=r;
|
---|
1659 | break;
|
---|
1660 | }
|
---|
1661 | }
|
---|
1662 | if(r==CS_MAXREADER)
|
---|
1663 | {
|
---|
1664 | client[cs_idx].au=(-1);
|
---|
1665 | }
|
---|
1666 | }
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | er->caid = er->ocaid;
|
---|
1670 | switch(er->rc) {
|
---|
1671 | case 0:
|
---|
1672 | case 3:
|
---|
1673 | // 0 - found
|
---|
1674 | // 3 - emu FIXME: obsolete ?
|
---|
1675 | client[cs_idx].cwfound++;
|
---|
1676 | break;
|
---|
1677 |
|
---|
1678 | case 1:
|
---|
1679 | case 2:
|
---|
1680 | // 1 - cache1
|
---|
1681 | // 2 - cache2
|
---|
1682 | client[cs_idx].cwcache++;
|
---|
1683 | break;
|
---|
1684 |
|
---|
1685 | case 4:
|
---|
1686 | case 9:
|
---|
1687 | case 10:
|
---|
1688 | // 4 - not found
|
---|
1689 | // 9 - corrupt
|
---|
1690 | // 10 - no card
|
---|
1691 | if (er->rcEx)
|
---|
1692 | client[cs_idx].cwignored++;
|
---|
1693 | else
|
---|
1694 | client[cs_idx].cwnot++;
|
---|
1695 | break;
|
---|
1696 |
|
---|
1697 | case 5:
|
---|
1698 | // 5 - timeout
|
---|
1699 | client[cs_idx].cwtout++;
|
---|
1700 | break;
|
---|
1701 |
|
---|
1702 | default:
|
---|
1703 | client[cs_idx].cwignored++;
|
---|
1704 | }
|
---|
1705 |
|
---|
1706 | #ifdef CS_ANTICASC
|
---|
1707 | ac_chk(er, 1);
|
---|
1708 | #endif
|
---|
1709 |
|
---|
1710 | cs_ddump_mask (D_ATR, er->cw, 16, "cw:");
|
---|
1711 | if (er->rc==7) er->rc=0;
|
---|
1712 | ph[client[cs_idx].ctyp].send_dcw(er);
|
---|
1713 | return 0;
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 | void chk_dcw(int fd)
|
---|
1717 | {
|
---|
1718 | ECM_REQUEST *er, *ert;
|
---|
1719 | if (read_from_pipe(fd, (uchar **)&er, 0)!=PIP_ID_ECM)
|
---|
1720 | return;
|
---|
1721 | //cs_log("dcw check from reader %d for idx %d (rc=%d)", er->reader[0], er->cpti, er->rc);
|
---|
1722 | ert=&ecmtask[er->cpti];
|
---|
1723 | if (ert->rc<100)
|
---|
1724 | return; // already done
|
---|
1725 | if( (er->caid!=ert->caid) || memcmp(er->ecm , ert->ecm , sizeof(er->ecm)) )
|
---|
1726 | return; // obsolete
|
---|
1727 | ert->rcEx=er->rcEx;
|
---|
1728 | if (er->rc>0) // found
|
---|
1729 | {
|
---|
1730 | switch(er->rc)
|
---|
1731 | {
|
---|
1732 | case 2:
|
---|
1733 | ert->rc=2;
|
---|
1734 | break;
|
---|
1735 | case 3:
|
---|
1736 | ert->rc=3;
|
---|
1737 | break;
|
---|
1738 | default:
|
---|
1739 | ert->rc=0;
|
---|
1740 | }
|
---|
1741 | ert->rcEx=0;
|
---|
1742 | ert->reader[0]=er->reader[0];
|
---|
1743 | memcpy(ert->cw , er->cw , sizeof(er->cw));
|
---|
1744 | #ifdef CS_WITH_GBOX
|
---|
1745 | ert->gbxCWFrom=er->gbxCWFrom;
|
---|
1746 | #endif
|
---|
1747 | }
|
---|
1748 | else // not found (from ONE of the readers !)
|
---|
1749 | {
|
---|
1750 | int i;
|
---|
1751 | ert->reader[er->reader[0]]=0;
|
---|
1752 | for (i=0; (ert) && (i<CS_MAXREADER); i++)
|
---|
1753 | if (ert->reader[i]) // we have still another chance
|
---|
1754 | ert=(ECM_REQUEST *)0;
|
---|
1755 | if (ert) ert->rc=4;
|
---|
1756 | }
|
---|
1757 | if (ert) send_dcw(ert);
|
---|
1758 | return;
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | ulong chk_provid(uchar *ecm, ushort caid)
|
---|
1762 | {
|
---|
1763 | int i;
|
---|
1764 | ulong provid=0;
|
---|
1765 | switch(caid)
|
---|
1766 | {
|
---|
1767 | case 0x100: // seca
|
---|
1768 | provid=b2i(2, ecm+3);
|
---|
1769 | break;
|
---|
1770 | case 0x500: // viaccess
|
---|
1771 | i=(ecm[4]==0xD2) ? ecm[5] + 2 : 0; // skip d2 nano
|
---|
1772 | if ((ecm[5+i]==3) && ((ecm[4+i]==0x90) || (ecm[4+i]==0x40)))
|
---|
1773 | provid=(b2i(3, ecm+6+i) & 0xFFFFF0);
|
---|
1774 | default:
|
---|
1775 | // cryptoworks ?
|
---|
1776 | if( caid&0x0d00 && ecm[8]==0x83 && ecm[9]==1 )
|
---|
1777 | provid=(ulong)ecm[10];
|
---|
1778 | }
|
---|
1779 | return(provid);
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 | #ifdef IRDETO_GUESSING
|
---|
1783 | void guess_irdeto(ECM_REQUEST *er)
|
---|
1784 | {
|
---|
1785 | uchar b3;
|
---|
1786 | int b47;
|
---|
1787 | //ushort chid;
|
---|
1788 | struct s_irdeto_quess *ptr;
|
---|
1789 |
|
---|
1790 | b3 = er->ecm[3];
|
---|
1791 | ptr = cfg->itab[b3];
|
---|
1792 | if( !ptr ) {
|
---|
1793 | cs_debug("unknown irdeto byte 3: %02X", b3);
|
---|
1794 | return;
|
---|
1795 | }
|
---|
1796 | b47 = b2i(4, er->ecm+4);
|
---|
1797 | //chid = b2i(2, er->ecm+6);
|
---|
1798 | //cs_debug("ecm: b47=%08X, ptr->b47=%08X, ptr->caid=%04X", b47, ptr->b47, ptr->caid);
|
---|
1799 | while( ptr )
|
---|
1800 | {
|
---|
1801 | if( b47==ptr->b47 )
|
---|
1802 | {
|
---|
1803 | if( er->srvid && (er->srvid!=ptr->sid) )
|
---|
1804 | {
|
---|
1805 | cs_debug("sid mismatched (ecm: %04X, guess: %04X), wrong oscam.ird file?",
|
---|
1806 | er->srvid, ptr->sid);
|
---|
1807 | return;
|
---|
1808 | }
|
---|
1809 | er->caid=ptr->caid;
|
---|
1810 | er->srvid=ptr->sid;
|
---|
1811 | er->chid=(ushort)ptr->b47;
|
---|
1812 | // cs_debug("quess_irdeto() found caid=%04X, sid=%04X, chid=%04X",
|
---|
1813 | // er->caid, er->srvid, er->chid);
|
---|
1814 | return;
|
---|
1815 | }
|
---|
1816 | ptr=ptr->next;
|
---|
1817 | }
|
---|
1818 | }
|
---|
1819 | #endif
|
---|
1820 |
|
---|
1821 | void cs_betatunnel(ECM_REQUEST *er)
|
---|
1822 | {
|
---|
1823 | int n;
|
---|
1824 | ulong mask_all = 0xFFFF;
|
---|
1825 | TUNTAB *ttab;
|
---|
1826 | ttab = &client[cs_idx].ttab;
|
---|
1827 | for (n = 0; (n < CS_MAXTUNTAB); n++) {
|
---|
1828 | if ((er->caid==ttab->bt_caidfrom[n]) && ((er->srvid==ttab->bt_srvid[n]) || (ttab->bt_srvid[n])==mask_all)) {
|
---|
1829 | uchar hack_n3[13] = {0x70, 0x51, 0xc7, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x87, 0x12, 0x07};
|
---|
1830 | uchar hack_n2[13] = {0x70, 0x51, 0xc9, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x48, 0x12, 0x07};
|
---|
1831 | er->caid = ttab->bt_caidto[n];
|
---|
1832 | er->prid = 0;
|
---|
1833 | er->l = (er->ecm[2]+3);
|
---|
1834 | memmove(er->ecm+14, er->ecm+4, er->l-1);
|
---|
1835 | if (er->l > 0x88) {
|
---|
1836 | memcpy(er->ecm+1, hack_n3, 13);
|
---|
1837 | if (er->ecm[0] == 0x81)
|
---|
1838 | er->ecm[12] += 1;
|
---|
1839 | }
|
---|
1840 | else {
|
---|
1841 | memcpy(er->ecm+1, hack_n2, 13);
|
---|
1842 | }
|
---|
1843 | er->l += 10;
|
---|
1844 | er->ecm[2] = er->l-3;
|
---|
1845 | er->btun = 1;
|
---|
1846 | client[cs_idx].cwtun++;
|
---|
1847 | cs_debug("ECM converted from: 0x%X to BetaCrypt: 0x%X for service id:0x%X",
|
---|
1848 | ttab->bt_caidfrom[n], ttab->bt_caidto[n], ttab->bt_srvid[n]);
|
---|
1849 | }
|
---|
1850 | }
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | void guess_cardsystem(ECM_REQUEST *er)
|
---|
1854 | {
|
---|
1855 | ushort last_hope=0;
|
---|
1856 |
|
---|
1857 | // viaccess - check by provid-search
|
---|
1858 | if( (er->prid=chk_provid(er->ecm, 0x500)) )
|
---|
1859 | er->caid=0x500;
|
---|
1860 |
|
---|
1861 | // nagra
|
---|
1862 | // is ecm[1] always 0x30 ?
|
---|
1863 | // is ecm[3] always 0x07 ?
|
---|
1864 | if ((er->ecm[6]==1) && (er->ecm[4]==er->ecm[2]-2))
|
---|
1865 | er->caid=0x1801;
|
---|
1866 |
|
---|
1867 | // seca2 - very poor
|
---|
1868 | if ((er->ecm[8]==0x10) && ((er->ecm[9]&0xF1)==1))
|
---|
1869 | last_hope=0x100;
|
---|
1870 |
|
---|
1871 | // is cryptoworks, but which caid ?
|
---|
1872 | if ((er->ecm[3]==0x81) && (er->ecm[4]==0xFF) &&
|
---|
1873 | (!er->ecm[5]) && (!er->ecm[6]) && (er->ecm[7]==er->ecm[2]-5))
|
---|
1874 | last_hope=0xd00;
|
---|
1875 |
|
---|
1876 | #ifdef IRDETO_GUESSING
|
---|
1877 | if (!er->caid && er->ecm[2]==0x31 && er->ecm[0x0b]==0x28)
|
---|
1878 | guess_irdeto(er);
|
---|
1879 | #endif
|
---|
1880 |
|
---|
1881 | if (!er->caid) // guess by len ..
|
---|
1882 | er->caid=len4caid[er->ecm[2]+3];
|
---|
1883 |
|
---|
1884 | if (!er->caid)
|
---|
1885 | er->caid=last_hope;
|
---|
1886 | }
|
---|
1887 |
|
---|
1888 | void request_cw(ECM_REQUEST *er, int flag, int reader_types)
|
---|
1889 | {
|
---|
1890 | int i;
|
---|
1891 | if ((reader_types == 0) || (reader_types == 2))
|
---|
1892 | er->level=flag;
|
---|
1893 | flag=(flag)?3:1; // flag specifies with/without fallback-readers
|
---|
1894 | for (i=0; i<CS_MAXREADER; i++)
|
---|
1895 | {
|
---|
1896 | switch (reader_types)
|
---|
1897 | {
|
---|
1898 | // network and local cards
|
---|
1899 | default:
|
---|
1900 | case 0:
|
---|
1901 | if (er->reader[i]&flag){
|
---|
1902 | write_ecm_request(reader[i].fd, er);
|
---|
1903 | }
|
---|
1904 | break;
|
---|
1905 | // only local cards
|
---|
1906 | case 1:
|
---|
1907 | if (!(reader[i].typ & R_IS_NETWORK))
|
---|
1908 | if (er->reader[i]&flag)
|
---|
1909 | write_ecm_request(reader[i].fd, er);
|
---|
1910 | break;
|
---|
1911 | // only network
|
---|
1912 | case 2:
|
---|
1913 | if ((reader[i].typ & R_IS_NETWORK))
|
---|
1914 | if (er->reader[i]&flag)
|
---|
1915 | write_ecm_request(reader[i].fd, er);
|
---|
1916 | break;
|
---|
1917 | }
|
---|
1918 | }
|
---|
1919 | }
|
---|
1920 |
|
---|
1921 | void get_cw(ECM_REQUEST *er)
|
---|
1922 | {
|
---|
1923 | int i, j, m;
|
---|
1924 | time_t now = time((time_t)0);
|
---|
1925 |
|
---|
1926 | client[cs_idx].lastecm = now;
|
---|
1927 |
|
---|
1928 | if (!er->caid)
|
---|
1929 | guess_cardsystem(er);
|
---|
1930 |
|
---|
1931 | /* Quickfix Area */
|
---|
1932 |
|
---|
1933 | if( (er->caid & 0xFF00) == 0x600 && !er->chid )
|
---|
1934 | er->chid = (er->ecm[6]<<8)|er->ecm[7];
|
---|
1935 |
|
---|
1936 | // quickfix for 0100:000065
|
---|
1937 | if (er->caid == 0x100 && er->prid == 0x65 && er->srvid == 0)
|
---|
1938 | er->srvid = 0x0642;
|
---|
1939 |
|
---|
1940 | // Quickfixes for Opticum/Globo HD9500
|
---|
1941 | // Quickfix for 0500:030300
|
---|
1942 | if (er->caid == 0x500 && er->prid == 0x030300)
|
---|
1943 | er->prid = 0x030600;
|
---|
1944 |
|
---|
1945 | // Quickfix for 0500:D20200
|
---|
1946 | if (er->caid == 0x500 && er->prid == 0xD20200)
|
---|
1947 | er->prid = 0x030600;
|
---|
1948 |
|
---|
1949 | /* END quickfixes */
|
---|
1950 |
|
---|
1951 | if (!er->prid)
|
---|
1952 | er->prid = chk_provid(er->ecm, er->caid);
|
---|
1953 |
|
---|
1954 | // Set providerid for newcamd clients if none is given
|
---|
1955 | if( (!er->prid) && client[cs_idx].ncd_server ) {
|
---|
1956 | int pi = client[cs_idx].port_idx;
|
---|
1957 | if( pi >= 0 && cfg->ncd_ptab.nports && cfg->ncd_ptab.nports >= pi )
|
---|
1958 | er->prid = cfg->ncd_ptab.ports[pi].ftab.filts[0].prids[0];
|
---|
1959 | }
|
---|
1960 |
|
---|
1961 | // CAID not supported or found
|
---|
1962 | if (!er->caid) {
|
---|
1963 | er->rc = 8;
|
---|
1964 | er->rcEx = E2_CAID;
|
---|
1965 | }
|
---|
1966 |
|
---|
1967 | // user expired
|
---|
1968 | if(client[cs_idx].expirationdate && client[cs_idx].expirationdate < client[cs_idx].lastecm)
|
---|
1969 | er->rc = 11;
|
---|
1970 |
|
---|
1971 | // user disabled
|
---|
1972 | if(client[cs_idx].disabled != 0)
|
---|
1973 | er->rc = 12;
|
---|
1974 |
|
---|
1975 | // rc<100 -> ecm error
|
---|
1976 | if (er->rc > 99) {
|
---|
1977 |
|
---|
1978 | m = er->caid;
|
---|
1979 | er->ocaid = er->caid;
|
---|
1980 | i = er->srvid;
|
---|
1981 |
|
---|
1982 | if ((i != client[cs_idx].last_srvid) || (!client[cs_idx].lastswitch)) {
|
---|
1983 | client[cs_idx].lastswitch = now;
|
---|
1984 | if(cfg->usrfileflag)
|
---|
1985 | cs_statistics(cs_idx);
|
---|
1986 | }
|
---|
1987 |
|
---|
1988 | // user sleeping
|
---|
1989 | if ((client[cs_idx].tosleep) && (now - client[cs_idx].lastswitch > client[cs_idx].tosleep))
|
---|
1990 | er->rc = 6;
|
---|
1991 |
|
---|
1992 | client[cs_idx].last_srvid = i;
|
---|
1993 | client[cs_idx].last_caid = m;
|
---|
1994 |
|
---|
1995 | for (j = 0; (j < 6) && (er->rc > 99); j++)
|
---|
1996 | {
|
---|
1997 | switch(j) {
|
---|
1998 |
|
---|
1999 | case 0:
|
---|
2000 | // fake (uniq)
|
---|
2001 | if (client[cs_idx].dup)
|
---|
2002 | er->rc = 7;
|
---|
2003 | break;
|
---|
2004 |
|
---|
2005 | case 1:
|
---|
2006 | // invalid (caid)
|
---|
2007 | if (!chk_bcaid(er, &client[cs_idx].ctab)) {
|
---|
2008 | er->rc = 8;
|
---|
2009 | er->rcEx = E2_CAID;
|
---|
2010 | }
|
---|
2011 | break;
|
---|
2012 |
|
---|
2013 | case 2:
|
---|
2014 | // invalid (srvid)
|
---|
2015 | if (!chk_srvid(er, cs_idx))
|
---|
2016 | er->rc = 8;
|
---|
2017 | break;
|
---|
2018 |
|
---|
2019 | case 3:
|
---|
2020 | // invalid (ufilters)
|
---|
2021 | if (!chk_ufilters(er))
|
---|
2022 | er->rc = 8;
|
---|
2023 | break;
|
---|
2024 |
|
---|
2025 | case 4:
|
---|
2026 | // invalid (sfilter)
|
---|
2027 | if (!chk_sfilter(er, ph[client[cs_idx].ctyp].ptab))
|
---|
2028 | er->rc = 8;
|
---|
2029 | break;
|
---|
2030 |
|
---|
2031 | case 5:
|
---|
2032 | // corrupt
|
---|
2033 | if( (i = er->l - (er->ecm[2] + 3)) ) {
|
---|
2034 | if (i > 0) {
|
---|
2035 | cs_debug("warning: ecm size adjusted from 0x%X to 0x%X",
|
---|
2036 | er->l, er->ecm[2] + 3);
|
---|
2037 | er->l = (er->ecm[2] + 3);
|
---|
2038 | }
|
---|
2039 | else
|
---|
2040 | er->rc = 9;
|
---|
2041 | }
|
---|
2042 | break;
|
---|
2043 | }
|
---|
2044 | }
|
---|
2045 |
|
---|
2046 | /*BetaCrypt tunneling
|
---|
2047 | *moved behind the check routines,
|
---|
2048 | *because newcamd ECM will fail
|
---|
2049 | *if ECM is converted before
|
---|
2050 | */
|
---|
2051 | if (&client[cs_idx].ttab)
|
---|
2052 | cs_betatunnel(er);
|
---|
2053 |
|
---|
2054 | // store ECM in cache
|
---|
2055 | memcpy(er->ecmd5, MD5(er->ecm, er->l, NULL), CS_ECMSTORESIZE);
|
---|
2056 |
|
---|
2057 | // cache1
|
---|
2058 | if (check_ecmcache(er, client[cs_idx].grp))
|
---|
2059 | er->rc = 1;
|
---|
2060 |
|
---|
2061 | #ifdef CS_ANTICASC
|
---|
2062 | ac_chk(er, 0);
|
---|
2063 | #endif
|
---|
2064 | }
|
---|
2065 |
|
---|
2066 | if(er->rc > 99 && er->rc != 1) {
|
---|
2067 |
|
---|
2068 | for (i = m = 0; i < CS_MAXREADER; i++)
|
---|
2069 | if (matching_reader(er, &reader[i]) && (i != ridx))
|
---|
2070 | m|=er->reader[i] = (reader[i].fallback)? 2: 1;
|
---|
2071 |
|
---|
2072 | switch(m) {
|
---|
2073 |
|
---|
2074 | // no reader -> not found
|
---|
2075 | case 0:
|
---|
2076 | er->rc = 4;
|
---|
2077 | if (!er->rcEx)
|
---|
2078 | er->rcEx = E2_GROUP;
|
---|
2079 | break;
|
---|
2080 |
|
---|
2081 | // fallbacks only, switch them
|
---|
2082 | case 2:
|
---|
2083 | for (i = 0; i < CS_MAXREADER; i++)
|
---|
2084 | er->reader[i]>>=1;
|
---|
2085 | }
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | if (er->rc < 100) {
|
---|
2089 | if (cfg->delay)
|
---|
2090 | cs_sleepms(cfg->delay);
|
---|
2091 |
|
---|
2092 | send_dcw(er);
|
---|
2093 | return;
|
---|
2094 | }
|
---|
2095 |
|
---|
2096 | er->rcEx = 0;
|
---|
2097 | request_cw(er, 0, cfg->preferlocalcards ? 1 : 0);
|
---|
2098 | }
|
---|
2099 |
|
---|
2100 | void log_emm_request(int auidx)
|
---|
2101 | {
|
---|
2102 | // cs_log("%s send emm-request (reader=%s, caid=%04X)",
|
---|
2103 | // cs_inet_ntoa(client[cs_idx].ip), reader[auidx].label, reader[auidx].caid[0]);
|
---|
2104 | cs_log("%s emm-request sent (reader=%s, caid=%04X)",
|
---|
2105 | username(cs_idx), reader[auidx].label, reader[auidx].caid[0]);
|
---|
2106 | }
|
---|
2107 |
|
---|
2108 | void do_emm(EMM_PACKET *ep)
|
---|
2109 | {
|
---|
2110 | int au;
|
---|
2111 | au = client[cs_idx].au;
|
---|
2112 |
|
---|
2113 | cs_ddump_mask(D_ATR, ep->emm, ep->l, "emm:");
|
---|
2114 |
|
---|
2115 | if ((au < 0) || (au >= CS_MAXREADER))
|
---|
2116 | return;
|
---|
2117 |
|
---|
2118 | if (!reader_get_emm_type(ep, &reader[au])) //decodes ep->type and ep->hexserial from the EMM
|
---|
2119 | return;
|
---|
2120 |
|
---|
2121 | cs_ddump_mask(D_EMM, ep->hexserial, 8, "emm UA/SA:");
|
---|
2122 |
|
---|
2123 | switch (ep->type) {
|
---|
2124 | case UNKNOWN:
|
---|
2125 | cs_debug_mask(D_EMM, "emmtype UNKNOWN. Reader %s has serial %s.", reader[au].label, cs_hexdump(0, reader[au].hexserial, 8));
|
---|
2126 | if (reader[au].blockemm_unknown) return;
|
---|
2127 | break;
|
---|
2128 |
|
---|
2129 | case UNIQUE:
|
---|
2130 | cs_debug_mask(D_EMM, "emmtype UNIQUE. Reader %s has serial %s.", reader[au].label, cs_hexdump(0, reader[au].hexserial, 8));
|
---|
2131 | if (reader[au].blockemm_u) return;
|
---|
2132 | break;
|
---|
2133 |
|
---|
2134 | case SHARED:
|
---|
2135 | cs_debug_mask(D_EMM, "emmtype SHARED. Reader %s has serial %s.", reader[au].label, cs_hexdump(0, reader[au].hexserial, 8));
|
---|
2136 | if (reader[au].blockemm_s) return;
|
---|
2137 | break;
|
---|
2138 |
|
---|
2139 | // FIXME only camd33 delivers hexserial from the net, newcamd, camd35 copy
|
---|
2140 | // cardreader hexserial in; reader_get_emm_type overwrites this with real SA value if known!
|
---|
2141 | case GLOBAL:
|
---|
2142 | cs_debug_mask(D_EMM, "emmtype GLOBAL. Reader %s has serial %s.", reader[au].label, cs_hexdump(0, reader[au].hexserial, 8));
|
---|
2143 | if (reader[au].blockemm_g) return;
|
---|
2144 | break;
|
---|
2145 | }
|
---|
2146 |
|
---|
2147 | client[cs_idx].lastemm = time((time_t)0);
|
---|
2148 | cs_ddump_mask(D_EMM, ep->emm, ep->l, "emm:");
|
---|
2149 |
|
---|
2150 | if ((!reader[au].fd) || // reader has no fd
|
---|
2151 | (reader[au].caid[0] != b2i(2,ep->caid))) { // wrong caid
|
---|
2152 | #ifdef WEBIF
|
---|
2153 | client[cs_idx].emmnok++;
|
---|
2154 | #endif
|
---|
2155 | return;
|
---|
2156 | }
|
---|
2157 | #ifdef WEBIF
|
---|
2158 | client[cs_idx].emmok++;
|
---|
2159 | #endif
|
---|
2160 | ep->cidx = cs_idx;
|
---|
2161 | cs_debug_mask(D_EMM, "emm is being sent to reader %s.", reader[au].label);
|
---|
2162 | write_to_pipe(reader[au].fd, PIP_ID_EMM, (uchar *) ep, sizeof(EMM_PACKET));
|
---|
2163 | }
|
---|
2164 |
|
---|
2165 | static int comp_timeb(struct timeb *tpa, struct timeb *tpb)
|
---|
2166 | {
|
---|
2167 | if (tpa->time>tpb->time) return(1);
|
---|
2168 | if (tpa->time<tpb->time) return(-1);
|
---|
2169 | if (tpa->millitm>tpb->millitm) return(1);
|
---|
2170 | if (tpa->millitm<tpb->millitm) return(-1);
|
---|
2171 | return(0);
|
---|
2172 | }
|
---|
2173 |
|
---|
2174 | static void build_delay(struct timeb *tpe, struct timeb *tpc)
|
---|
2175 | {
|
---|
2176 | if (comp_timeb(tpe, tpc)>0)
|
---|
2177 | {
|
---|
2178 | tpe->time=tpc->time;
|
---|
2179 | tpe->millitm=tpc->millitm;
|
---|
2180 | }
|
---|
2181 | }
|
---|
2182 |
|
---|
2183 | struct timeval *chk_pending(struct timeb tp_ctimeout)
|
---|
2184 | {
|
---|
2185 | int i;
|
---|
2186 | ulong td;
|
---|
2187 | struct timeb tpn, tpe, tpc; // <n>ow, <e>nd, <c>heck
|
---|
2188 | static struct timeval tv;
|
---|
2189 |
|
---|
2190 | ECM_REQUEST *er;
|
---|
2191 | cs_ftime(&tpn);
|
---|
2192 | tpe=tp_ctimeout; // latest delay -> disconnect
|
---|
2193 |
|
---|
2194 | if (ecmtask)
|
---|
2195 | i=(ph[client[cs_idx].ctyp].multi)?CS_MAXPENDING:1;
|
---|
2196 | else
|
---|
2197 | i=0;
|
---|
2198 | //cs_log("num pend=%d", i);
|
---|
2199 | for (--i; i>=0; i--)
|
---|
2200 | if (ecmtask[i].rc>=100) // check all pending ecm-requests
|
---|
2201 | {
|
---|
2202 | int act, j;
|
---|
2203 | er=&ecmtask[i];
|
---|
2204 | tpc=er->tps;
|
---|
2205 | tpc.millitm += (er->stage) ? cfg->ctimeout : cfg->ftimeout;
|
---|
2206 | tpc.time += tpc.millitm / 1000;
|
---|
2207 | tpc.millitm = tpc.millitm % 1000;
|
---|
2208 | if (!er->stage)
|
---|
2209 | {
|
---|
2210 | for (j=0, act=1; (act) && (j<CS_MAXREADER); j++)
|
---|
2211 | {
|
---|
2212 | if (cfg->preferlocalcards && !er->locals_done)
|
---|
2213 | {
|
---|
2214 | if ((er->reader[j]&1) && !(reader[j].typ & R_IS_NETWORK))
|
---|
2215 | act=0;
|
---|
2216 | }
|
---|
2217 | else if (cfg->preferlocalcards && er->locals_done)
|
---|
2218 | {
|
---|
2219 | if ((er->reader[j]&1) && (reader[j].typ & R_IS_NETWORK))
|
---|
2220 | act=0;
|
---|
2221 | }
|
---|
2222 | else
|
---|
2223 | {
|
---|
2224 | if (er->reader[j]&1)
|
---|
2225 | act=0;
|
---|
2226 | }
|
---|
2227 | }
|
---|
2228 | //cs_log("stage 0, act=%d r0=%d, r1=%d, r2=%d, r3=%d, r4=%d r5=%d", act,
|
---|
2229 | // er->reader[0], er->reader[1], er->reader[2],
|
---|
2230 | // er->reader[3], er->reader[4], er->reader[5]);
|
---|
2231 | if (act)
|
---|
2232 | {
|
---|
2233 | int inc_stage = 1;
|
---|
2234 |
|
---|
2235 | if (cfg->preferlocalcards && !er->locals_done)
|
---|
2236 | {
|
---|
2237 | int i;
|
---|
2238 |
|
---|
2239 | er->locals_done = 1;
|
---|
2240 | for (i = 0; i < CS_MAXREADER; i++)
|
---|
2241 | {
|
---|
2242 | if (reader[i].typ & R_IS_NETWORK)
|
---|
2243 | {
|
---|
2244 | inc_stage = 0;
|
---|
2245 | }
|
---|
2246 | }
|
---|
2247 | }
|
---|
2248 | if (!inc_stage)
|
---|
2249 | {
|
---|
2250 | request_cw(er, er->stage, 2);
|
---|
2251 | tpc.millitm += 1000 * (tpn.time - er->tps.time) + tpn.millitm - er->tps.millitm;
|
---|
2252 | tpc.time += tpc.millitm / 1000;
|
---|
2253 | tpc.millitm = tpc.millitm % 1000;
|
---|
2254 | }
|
---|
2255 | else
|
---|
2256 | {
|
---|
2257 | er->locals_done = 0;
|
---|
2258 | er->stage++;
|
---|
2259 | request_cw(er, er->stage, cfg->preferlocalcards ? 1 : 0);
|
---|
2260 |
|
---|
2261 | tpc.millitm += (cfg->ctimeout-cfg->ftimeout);
|
---|
2262 | tpc.time += tpc.millitm / 1000;
|
---|
2263 | tpc.millitm = tpc.millitm % 1000;
|
---|
2264 | }
|
---|
2265 | }
|
---|
2266 | }
|
---|
2267 | if (comp_timeb(&tpn, &tpc)>0) // action needed
|
---|
2268 | {
|
---|
2269 | //cs_log("Action now %d.%03d", tpn.time, tpn.millitm);
|
---|
2270 | //cs_log(" %d.%03d", tpc.time, tpc.millitm);
|
---|
2271 | if (er->stage)
|
---|
2272 | {
|
---|
2273 | er->rc=5; // timeout
|
---|
2274 | send_dcw(er);
|
---|
2275 | continue;
|
---|
2276 | }
|
---|
2277 | else
|
---|
2278 | {
|
---|
2279 | er->stage++;
|
---|
2280 | request_cw(er, er->stage, 0);
|
---|
2281 | tpc.millitm += (cfg->ctimeout-cfg->ftimeout);
|
---|
2282 | tpc.time += tpc.millitm / 1000;
|
---|
2283 | tpc.millitm = tpc.millitm % 1000;
|
---|
2284 | }
|
---|
2285 | }
|
---|
2286 | build_delay(&tpe, &tpc);
|
---|
2287 | }
|
---|
2288 | td=(tpe.time-tpn.time)*1000+(tpe.millitm-tpn.millitm)+5;
|
---|
2289 | tv.tv_sec = td/1000;
|
---|
2290 | tv.tv_usec = (td%1000)*1000;
|
---|
2291 | //cs_log("delay %d.%06d", tv.tv_sec, tv.tv_usec);
|
---|
2292 | return(&tv);
|
---|
2293 | }
|
---|
2294 |
|
---|
2295 | int process_input(uchar *buf, int l, int timeout)
|
---|
2296 | {
|
---|
2297 | int rc;
|
---|
2298 | fd_set fds;
|
---|
2299 | struct timeb tp;
|
---|
2300 |
|
---|
2301 | if (master_pid!=getppid()) cs_exit(0);
|
---|
2302 | if (!pfd) return(-1);
|
---|
2303 | cs_ftime(&tp);
|
---|
2304 | tp.time+=timeout;
|
---|
2305 | if (ph[client[cs_idx].ctyp].watchdog)
|
---|
2306 | alarm(cfg->cmaxidle + (cfg->ctimeout + 500) / 1000 + 1);
|
---|
2307 | while (1)
|
---|
2308 | {
|
---|
2309 | FD_ZERO(&fds);
|
---|
2310 | FD_SET(pfd, &fds);
|
---|
2311 | FD_SET(fd_m2c, &fds);
|
---|
2312 |
|
---|
2313 | rc=select(((pfd>fd_m2c)?pfd:fd_m2c)+1, &fds, 0, 0, chk_pending(tp));
|
---|
2314 | if (master_pid!=getppid()) cs_exit(0);
|
---|
2315 | if (rc<0)
|
---|
2316 | {
|
---|
2317 | if (errno==EINTR) continue;
|
---|
2318 | else return(0);
|
---|
2319 | }
|
---|
2320 |
|
---|
2321 | if (FD_ISSET(fd_m2c, &fds)) // read from pipe
|
---|
2322 | chk_dcw(fd_m2c);
|
---|
2323 |
|
---|
2324 | if (FD_ISSET(pfd, &fds)) // read from client
|
---|
2325 | {
|
---|
2326 | rc=ph[client[cs_idx].ctyp].recv(buf, l);
|
---|
2327 | break;
|
---|
2328 | }
|
---|
2329 | if (tp.time<=time((time_t *)0)) // client maxidle reached
|
---|
2330 | {
|
---|
2331 | rc=(-9);
|
---|
2332 | break;
|
---|
2333 | }
|
---|
2334 | }
|
---|
2335 | if (ph[client[cs_idx].ctyp].watchdog)
|
---|
2336 | alarm(cfg->cmaxidle + (cfg->ctimeout + 500) / 1000 + 1);
|
---|
2337 | return(rc);
|
---|
2338 | }
|
---|
2339 |
|
---|
2340 | static void process_master_pipe()
|
---|
2341 | {
|
---|
2342 | int n;
|
---|
2343 | uchar *ptr;
|
---|
2344 |
|
---|
2345 | switch(n=read_from_pipe(mfdr, &ptr, 1))
|
---|
2346 | {
|
---|
2347 | case PIP_ID_LOG:
|
---|
2348 | cs_write_log((char *)ptr);
|
---|
2349 | break;
|
---|
2350 | case PIP_ID_HUP:
|
---|
2351 | cs_accounts_chk();
|
---|
2352 | break;
|
---|
2353 | }
|
---|
2354 | }
|
---|
2355 |
|
---|
2356 | void cs_log_config()
|
---|
2357 | {
|
---|
2358 | uchar buf[2048];
|
---|
2359 |
|
---|
2360 | if (cfg->nice!=99)
|
---|
2361 | sprintf((char *)buf, ", nice=%d", cfg->nice);
|
---|
2362 | else
|
---|
2363 | buf[0]='\0';
|
---|
2364 | cs_log("version=%s, build #%s, system=%s%s", CS_VERSION_X, CS_SVN_VERSION, cs_platform((char *)buf+64), buf);
|
---|
2365 | cs_log("max. clients=%d, client max. idle=%d sec",
|
---|
2366 | #ifdef CS_ANTICASC
|
---|
2367 | CS_MAXPID-3, cfg->cmaxidle);
|
---|
2368 | #else
|
---|
2369 | CS_MAXPID-2, cfg->cmaxidle);
|
---|
2370 | #endif
|
---|
2371 | if( cfg->max_log_size )
|
---|
2372 | sprintf((char *)buf, "%d Kb", cfg->max_log_size);
|
---|
2373 | else
|
---|
2374 | strcpy((char *)buf, "unlimited");
|
---|
2375 | cs_log("max. logsize=%s", buf);
|
---|
2376 | cs_log("client timeout=%lu ms, fallback timeout=%lu ms, cache delay=%d ms",
|
---|
2377 | cfg->ctimeout, cfg->ftimeout, cfg->delay);
|
---|
2378 | #ifdef CS_NOSHM
|
---|
2379 | cs_log("shared memory initialized (size=%d, fd=%d)", shmsize, shmid);
|
---|
2380 | #else
|
---|
2381 | cs_log("shared memory initialized (size=%d, id=%d)", shmsize, shmid);
|
---|
2382 | #endif
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | void cs_waitforcardinit()
|
---|
2386 | {
|
---|
2387 | if (cfg->waitforcards)
|
---|
2388 | {
|
---|
2389 | int card_init_done, i;
|
---|
2390 | cs_sleepms(3000); // short sleep for card detect to work proberly
|
---|
2391 | do {
|
---|
2392 | card_init_done = 1;
|
---|
2393 | for (i = 0; i < CS_MAXREADER; i++) {
|
---|
2394 | if (reader[i].card_status == CARD_NEED_INIT) {
|
---|
2395 | card_init_done = 0;
|
---|
2396 | break;
|
---|
2397 | }
|
---|
2398 | }
|
---|
2399 | cs_sleepms(300); // wait a little bit
|
---|
2400 | alarm(cfg->cmaxidle + cfg->ctimeout / 1000 + 1);
|
---|
2401 | } while (!card_init_done);
|
---|
2402 | }
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | int main (int argc, char *argv[])
|
---|
2406 | {
|
---|
2407 | struct sockaddr_in cad; /* structure to hold client's address */
|
---|
2408 | int scad; /* length of address */
|
---|
2409 | //int fd; /* socket descriptors */
|
---|
2410 | int i, j, n;
|
---|
2411 | int bg=0;
|
---|
2412 | int gfd; //nph,
|
---|
2413 | int fdp[2];
|
---|
2414 | uchar buf[2048];
|
---|
2415 | void (*mod_def[])(struct s_module *)=
|
---|
2416 | {
|
---|
2417 | #ifdef MODULE_MONITOR
|
---|
2418 | module_monitor,
|
---|
2419 | #endif
|
---|
2420 | #ifdef MODULE_CAMD33
|
---|
2421 | module_camd33,
|
---|
2422 | #endif
|
---|
2423 | #ifdef MODULE_CAMD35
|
---|
2424 | module_camd35,
|
---|
2425 | #endif
|
---|
2426 | #ifdef MODULE_CAMD35_TCP
|
---|
2427 | module_camd35_tcp,
|
---|
2428 | #endif
|
---|
2429 | #ifdef MODULE_NEWCAMD
|
---|
2430 | module_newcamd,
|
---|
2431 | #endif
|
---|
2432 | #ifdef MODULE_CCCAM
|
---|
2433 | module_cccam,
|
---|
2434 | #endif
|
---|
2435 | #ifdef CS_WITH_GBOX
|
---|
2436 | module_gbox,
|
---|
2437 | #endif
|
---|
2438 | #ifdef MODULE_RADEGAST
|
---|
2439 | module_radegast,
|
---|
2440 | #endif
|
---|
2441 | #ifdef MODULE_SERIAL
|
---|
2442 | module_oscam_ser,
|
---|
2443 | #endif
|
---|
2444 | #ifdef HAVE_DVBAPI
|
---|
2445 | module_dvbapi,
|
---|
2446 | #endif
|
---|
2447 | 0
|
---|
2448 | };
|
---|
2449 |
|
---|
2450 | void (*cardsystem_def[])(struct s_cardsystem *)=
|
---|
2451 | {
|
---|
2452 | #ifdef READER_NAGRA
|
---|
2453 | reader_nagra,
|
---|
2454 | #endif
|
---|
2455 | #ifdef READER_IRDETO
|
---|
2456 | reader_irdeto,
|
---|
2457 | #endif
|
---|
2458 | #ifdef READER_CONAX
|
---|
2459 | reader_conax,
|
---|
2460 | #endif
|
---|
2461 | #ifdef READER_CRYPTOWORKS
|
---|
2462 | reader_cryptoworks,
|
---|
2463 | #endif
|
---|
2464 | #ifdef READER_SECA
|
---|
2465 | reader_seca,
|
---|
2466 | #endif
|
---|
2467 | #ifdef READER_VIACCESS
|
---|
2468 | reader_viaccess,
|
---|
2469 | #endif
|
---|
2470 | #ifdef READER_VIDEOGUARD
|
---|
2471 | reader_videoguard,
|
---|
2472 | #endif
|
---|
2473 | #ifdef READER_DRE
|
---|
2474 | reader_dre,
|
---|
2475 | #endif
|
---|
2476 | 0
|
---|
2477 | };
|
---|
2478 |
|
---|
2479 | while ((i=getopt(argc, argv, "bc:d:hm:"))!=EOF)
|
---|
2480 | {
|
---|
2481 | switch(i)
|
---|
2482 | {
|
---|
2483 | case 'b': bg=1;
|
---|
2484 | break;
|
---|
2485 | case 'c': cs_strncpy(cs_confdir, optarg, sizeof(cs_confdir));
|
---|
2486 | break;
|
---|
2487 | case 'd': cs_dblevel=atoi(optarg);
|
---|
2488 | break;
|
---|
2489 | case 'm':
|
---|
2490 | #ifdef CS_NOSHM
|
---|
2491 | cs_strncpy(cs_memfile, optarg, sizeof(cs_memfile));
|
---|
2492 | break;
|
---|
2493 | #endif
|
---|
2494 | case 'h':
|
---|
2495 | default : usage();
|
---|
2496 | }
|
---|
2497 | }
|
---|
2498 | if (cs_confdir[strlen(cs_confdir)]!='/') strcat(cs_confdir, "/");
|
---|
2499 | init_shm();
|
---|
2500 | init_config();
|
---|
2501 | cfg->debuglvl = cs_dblevel; // give static debuglevel to outer world
|
---|
2502 | for (i=0; mod_def[i]; i++) // must be later BEFORE init_config()
|
---|
2503 | {
|
---|
2504 | memset(&ph[i], 0, sizeof(struct s_module));
|
---|
2505 | mod_def[i](&ph[i]);
|
---|
2506 | }
|
---|
2507 | for (i=0; cardsystem_def[i]; i++) // must be later BEFORE init_config()
|
---|
2508 | {
|
---|
2509 | memset(&cardsystem[i], 0, sizeof(struct s_cardsystem));
|
---|
2510 | cardsystem_def[i](&cardsystem[i]);
|
---|
2511 | }
|
---|
2512 |
|
---|
2513 |
|
---|
2514 | cs_log("auth size=%d", sizeof(struct s_auth));
|
---|
2515 |
|
---|
2516 | init_sidtab();
|
---|
2517 | init_readerdb();
|
---|
2518 | init_userdb();
|
---|
2519 | init_signal();
|
---|
2520 | cs_set_mloc(30, "init");
|
---|
2521 | init_srvid();
|
---|
2522 | init_len4caid();
|
---|
2523 | #ifdef IRDETO_GUESSING
|
---|
2524 | init_irdeto_guess_tab();
|
---|
2525 | #endif
|
---|
2526 | cs_init_statistics(cfg->usrfile);
|
---|
2527 |
|
---|
2528 | if (pipe(fdp))
|
---|
2529 | {
|
---|
2530 | cs_log("Cannot create pipe (errno=%d)", errno);
|
---|
2531 | cs_exit(1);
|
---|
2532 | }
|
---|
2533 | mfdr=fdp[0];
|
---|
2534 | fd_c2m=fdp[1];
|
---|
2535 | gfd=mfdr+1;
|
---|
2536 |
|
---|
2537 | #ifdef OS_MACOSX
|
---|
2538 | if (bg && daemon_compat(1,0))
|
---|
2539 | #else
|
---|
2540 | if (bg && daemon(1,0))
|
---|
2541 | #endif
|
---|
2542 | {
|
---|
2543 | cs_log("Error starting in background (errno=%d)", errno);
|
---|
2544 | cs_exit(1);
|
---|
2545 | }
|
---|
2546 | master_pid=client[0].pid=getpid();
|
---|
2547 | if (cfg->pidfile != NULL)
|
---|
2548 | {
|
---|
2549 | FILE *fp;
|
---|
2550 | if (!(fp=fopen(cfg->pidfile, "w")))
|
---|
2551 | {
|
---|
2552 | cs_log("Cannot open pid-file (errno=%d)", errno);
|
---|
2553 | cs_exit(1);
|
---|
2554 | }
|
---|
2555 | fprintf(fp, "%d\n", getpid());
|
---|
2556 | fclose(fp);
|
---|
2557 | }
|
---|
2558 |
|
---|
2559 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
2560 | if( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
|
---|
2561 | for(j=0; j<ph[i].ptab->nports; j++)
|
---|
2562 | {
|
---|
2563 | start_listener(&ph[i], j);
|
---|
2564 | if( ph[i].ptab->ports[j].fd+1>gfd )
|
---|
2565 | gfd=ph[i].ptab->ports[j].fd+1;
|
---|
2566 | }
|
---|
2567 |
|
---|
2568 | //set time for server to now to avoid 0 in monitor/webif
|
---|
2569 | client[0].last=time((time_t *)0);
|
---|
2570 |
|
---|
2571 | start_client_resolver();
|
---|
2572 | init_service(97); // logger
|
---|
2573 | init_service(98); // resolver
|
---|
2574 | #ifdef WEBIF
|
---|
2575 | init_service(95); // http
|
---|
2576 | #endif
|
---|
2577 | init_cardreader();
|
---|
2578 |
|
---|
2579 | cs_log("waiting for local card init");
|
---|
2580 | cs_waitforcardinit();
|
---|
2581 | cs_log("init for all local cards done");
|
---|
2582 |
|
---|
2583 | #ifdef CS_ANTICASC
|
---|
2584 | if( !cfg->ac_enabled )
|
---|
2585 | cs_log("anti cascading disabled");
|
---|
2586 | else
|
---|
2587 | {
|
---|
2588 | init_ac();
|
---|
2589 | init_service(96);
|
---|
2590 | }
|
---|
2591 | #endif
|
---|
2592 |
|
---|
2593 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
2594 | if (ph[i].type & MOD_CONN_SERIAL) // for now: oscam_ser only
|
---|
2595 | if (ph[i].s_handler)
|
---|
2596 | ph[i].s_handler(i);
|
---|
2597 |
|
---|
2598 | cs_close_log();
|
---|
2599 | *mcl=1;
|
---|
2600 | while (1)
|
---|
2601 | {
|
---|
2602 | fd_set fds;
|
---|
2603 |
|
---|
2604 | do
|
---|
2605 | {
|
---|
2606 | FD_ZERO(&fds);
|
---|
2607 | FD_SET(mfdr, &fds);
|
---|
2608 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
2609 | if ( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
|
---|
2610 | for (j=0; j<ph[i].ptab->nports; j++)
|
---|
2611 | if (ph[i].ptab->ports[j].fd)
|
---|
2612 | FD_SET(ph[i].ptab->ports[j].fd, &fds);
|
---|
2613 | errno=0;
|
---|
2614 | cs_set_mloc(0, "before select");
|
---|
2615 | select(gfd, &fds, 0, 0, 0);
|
---|
2616 | cs_set_mloc(60, "after select");
|
---|
2617 | } while (errno==EINTR);
|
---|
2618 | cs_set_mloc(-1, "event (global)");
|
---|
2619 |
|
---|
2620 | client[0].last=time((time_t *)0);
|
---|
2621 | scad = sizeof(cad);
|
---|
2622 | if (FD_ISSET(mfdr, &fds))
|
---|
2623 | {
|
---|
2624 | cs_set_mloc(-1, "event: master-pipe");
|
---|
2625 | process_master_pipe();
|
---|
2626 | }
|
---|
2627 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
2628 | {
|
---|
2629 | if( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
|
---|
2630 | {
|
---|
2631 | for( j=0; j<ph[i].ptab->nports; j++ )
|
---|
2632 | {
|
---|
2633 | if( ph[i].ptab->ports[j].fd && FD_ISSET(ph[i].ptab->ports[j].fd, &fds) )
|
---|
2634 | {
|
---|
2635 | if (ph[i].type==MOD_CONN_UDP)
|
---|
2636 | {
|
---|
2637 | cs_set_mloc(-1, "event: udp-socket");
|
---|
2638 | if ((n=recvfrom(ph[i].ptab->ports[j].fd, buf+3, sizeof(buf)-3, 0, (struct sockaddr *)&cad, (socklen_t *)&scad))>0)
|
---|
2639 | {
|
---|
2640 | int idx;
|
---|
2641 | idx=idx_from_ip(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port));
|
---|
2642 | if (!idx)
|
---|
2643 | {
|
---|
2644 | if (pipe(fdp))
|
---|
2645 | {
|
---|
2646 | cs_log("Cannot create pipe (errno=%d)", errno);
|
---|
2647 | cs_exit(1);
|
---|
2648 | }
|
---|
2649 | switch(cs_fork(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port)))
|
---|
2650 | {
|
---|
2651 | case -1:
|
---|
2652 | close(fdp[0]);
|
---|
2653 | close(fdp[1]);
|
---|
2654 | break;
|
---|
2655 | case 0:
|
---|
2656 | client[idx=cs_last_idx].ufd=fdp[1];
|
---|
2657 | close(fdp[0]);
|
---|
2658 | break;
|
---|
2659 | default:
|
---|
2660 | // close(fdp[1]); // now used to simulate event
|
---|
2661 | pfd=fdp[0];
|
---|
2662 | wait4master();
|
---|
2663 | client[cs_idx].ctyp=i;
|
---|
2664 | client[cs_idx].port_idx=j;
|
---|
2665 | client[cs_idx].udp_fd=ph[i].ptab->ports[j].fd;
|
---|
2666 | client[cs_idx].udp_sa=cad;
|
---|
2667 | if (ph[client[cs_idx].ctyp].watchdog)
|
---|
2668 | alarm(cfg->cmaxidle + cfg->ctimeout / 1000 + 1);
|
---|
2669 | ph[i].s_handler(cad); // never return
|
---|
2670 | }
|
---|
2671 | }
|
---|
2672 | if (idx)
|
---|
2673 | {
|
---|
2674 | unsigned short rl;
|
---|
2675 | rl=n;
|
---|
2676 | buf[0]='U';
|
---|
2677 | memcpy(buf+1, &rl, 2);
|
---|
2678 | if (!write(client[idx].ufd, buf, n+3)) cs_exit(1);
|
---|
2679 | }
|
---|
2680 | }
|
---|
2681 | }
|
---|
2682 | else
|
---|
2683 | {
|
---|
2684 | cs_set_mloc(-1, "event: tcp-socket");
|
---|
2685 | if ((pfd=accept(ph[i].ptab->ports[j].fd, (struct sockaddr *)&cad, (socklen_t *)&scad))>0)
|
---|
2686 | {
|
---|
2687 | switch(cs_fork(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port)))
|
---|
2688 | {
|
---|
2689 | case -1:
|
---|
2690 | case 0:
|
---|
2691 | close(pfd);
|
---|
2692 | break;
|
---|
2693 | default:
|
---|
2694 | wait4master();
|
---|
2695 | client[cs_idx].ctyp=i;
|
---|
2696 | client[cs_idx].udp_fd=pfd;
|
---|
2697 | client[cs_idx].port_idx=j;
|
---|
2698 | if (ph[client[cs_idx].ctyp].watchdog)
|
---|
2699 | alarm(cfg->cmaxidle + cfg->ctimeout / 1000 + 1);
|
---|
2700 | ph[i].s_handler();
|
---|
2701 | }
|
---|
2702 | }
|
---|
2703 | }
|
---|
2704 | }
|
---|
2705 | }
|
---|
2706 | } // if (ph[i].type & MOD_CONN_NET)
|
---|
2707 | }
|
---|
2708 | }
|
---|
2709 | cs_exit(1);
|
---|
2710 | }
|
---|