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=D_MASTER; // process-type
|
---|
20 | struct s_module ph[CS_MAX_MOD]; // Protocols
|
---|
21 | int maxph=0; // Protocols used
|
---|
22 | int cs_hw=0; // hardware autodetect
|
---|
23 | int is_server=0; // used in modules to specify function
|
---|
24 | int premhack=0; // used to activate premiere hack 1801 -> 1702
|
---|
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 | EMM_PACKET epg;
|
---|
31 | #ifdef CS_ANTICASC
|
---|
32 | struct s_acasc ac_stat[CS_MAXPID];
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | /*****************************************************************************
|
---|
36 | Shared Memory
|
---|
37 | *****************************************************************************/
|
---|
38 | int *ecmidx; // Shared Memory
|
---|
39 | int *logidx; // Shared Memory
|
---|
40 | int *mpcs_sem; // sem (multicam.o)
|
---|
41 | int *c_start; // idx of 1st client
|
---|
42 | int *log_fd; // log-process is running
|
---|
43 | struct s_ecm *ecmcache; // Shared Memory
|
---|
44 | struct s_client *client; // Shared Memory
|
---|
45 | struct s_reader *reader; // Shared Memory
|
---|
46 |
|
---|
47 | struct card_struct *Cards; // Shared Memory
|
---|
48 | struct idstore_struct *idstore; // Shared Memory
|
---|
49 | unsigned long *IgnoreList; // Shared Memory
|
---|
50 |
|
---|
51 | struct s_config *cfg; // Shared Memory
|
---|
52 | #ifdef CS_ANTICASC
|
---|
53 | struct s_acasc_shm *acasc; // anti-cascading table indexed by account.ac_idx
|
---|
54 | #endif
|
---|
55 | #ifdef CS_LOGHISTORY
|
---|
56 | int *loghistidx; // ptr to current entry
|
---|
57 | char *loghist; // ptr of log-history
|
---|
58 | #endif
|
---|
59 | int *mcl=0; // Master close log?
|
---|
60 |
|
---|
61 | static int shmsize = CS_ECMCACHESIZE*(sizeof(struct s_ecm)) +
|
---|
62 | CS_MAXPID*(sizeof(struct s_client)) +
|
---|
63 | CS_MAXREADER*(sizeof(struct s_reader)) +
|
---|
64 | #ifdef CS_WITH_GBOX
|
---|
65 | CS_MAXCARDS*(sizeof(struct card_struct))+
|
---|
66 | CS_MAXIGNORE*(sizeof(long))+
|
---|
67 | CS_MAXPID*(sizeof(struct idstore_struct))+
|
---|
68 | #endif
|
---|
69 | #ifdef CS_ANTICASC
|
---|
70 | CS_MAXPID*(sizeof(struct s_acasc_shm)) +
|
---|
71 | #endif
|
---|
72 | #ifdef CS_LOGHISTORY
|
---|
73 | CS_MAXLOGHIST*CS_LOGHISTSIZE + sizeof(int) +
|
---|
74 | #endif
|
---|
75 | sizeof(struct s_config)+(6*sizeof(int));
|
---|
76 |
|
---|
77 | #ifdef CS_NOSHM
|
---|
78 | char cs_memfile[128]=CS_MMAPFILE;
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | /*****************************************************************************
|
---|
82 | Statics
|
---|
83 | *****************************************************************************/
|
---|
84 | static char mloc[128]={0};
|
---|
85 | static int shmid=0; // Shared Memory ID
|
---|
86 | static int cs_last_idx=0; // client index of last fork (master only)
|
---|
87 | static char* credit[] = {
|
---|
88 | "all members of streamboard.de.vu for testing",
|
---|
89 | "scotty and aroureos for the first softcam (no longer used)",
|
---|
90 | "John Moore for the hsic-client (humax 5400) and the arm-support",
|
---|
91 | "doz21 for the sio-routines and his support on camd3-protocol",
|
---|
92 | "kindzadza for his support on radegast-protocol",
|
---|
93 | "DS and ago for several modules in mpcs development",
|
---|
94 | "dingo35 for seca reader-support",
|
---|
95 | "dingo35 and okmikel for newcamd-support",
|
---|
96 | "hellmaster1024 for gb*x-support",
|
---|
97 | "the vdr-sc team for several good ideas :-)",
|
---|
98 | NULL };
|
---|
99 |
|
---|
100 | static void cs_set_mloc(int ato, char *txt)
|
---|
101 | {
|
---|
102 | if (ato>=0)
|
---|
103 | alarm(ato);
|
---|
104 | if (txt)
|
---|
105 | strcpy(mloc, txt);
|
---|
106 | }
|
---|
107 |
|
---|
108 | char *cs_platform(char *buf)
|
---|
109 | {
|
---|
110 | static char *hw=NULL;
|
---|
111 | if (!hw)
|
---|
112 | {
|
---|
113 | #ifdef TUXBOX
|
---|
114 | struct stat st;
|
---|
115 | cs_hw=CS_HW_DBOX2; // dbox2, default for now
|
---|
116 | if (!stat("/dev/sci0", &st)) cs_hw=CS_HW_DREAM; // dreambox
|
---|
117 | switch(cs_hw)
|
---|
118 | {
|
---|
119 | #ifdef PPC
|
---|
120 | case CS_HW_DBOX2: hw="dbox2" ; break;
|
---|
121 | #endif
|
---|
122 | case CS_HW_DREAM: hw="dreambox"; break;
|
---|
123 | }
|
---|
124 | #endif
|
---|
125 | if (!hw) hw=CS_OS_HW;
|
---|
126 | }
|
---|
127 | sprintf(buf, "%s-%s-%s", CS_OS_CPU, hw, CS_OS_SYS);
|
---|
128 | return(buf);
|
---|
129 | }
|
---|
130 |
|
---|
131 | static void usage()
|
---|
132 | {
|
---|
133 | int i;
|
---|
134 | fprintf(stderr, "\nstreamboard mp-cardserver v%s (%s) - (w) 2004-2007 by dukat\n\n", CS_VERSION_X, CS_OSTYPE);
|
---|
135 | fprintf(stderr, "cardserver [-b] [-c config-dir]");
|
---|
136 | #ifdef CS_NOSHM
|
---|
137 | fprintf(stderr, " [-m memory-file]");
|
---|
138 | #endif
|
---|
139 | fprintf(stderr, "\n\n\t-b : start in background\n");
|
---|
140 | fprintf(stderr, "\t-c <dir> : read configuration from <dir>\n");
|
---|
141 | fprintf(stderr, "\t default=%s\n", CS_CONFDIR);
|
---|
142 | #ifdef CS_NOSHM
|
---|
143 | fprintf(stderr, "\t-m <file>: use <file> as mmaped memory file\n");
|
---|
144 | fprintf(stderr, "\t default=%s\n", CS_MMAPFILE);
|
---|
145 | #endif
|
---|
146 | fprintf(stderr, "\nthanks to ...\n");
|
---|
147 | for (i=0; credit[i]; i++)
|
---|
148 | fprintf(stderr, "\t%s\n", credit[i]);
|
---|
149 | fprintf(stderr, "\n");
|
---|
150 | exit(1);
|
---|
151 | }
|
---|
152 |
|
---|
153 | #ifdef NEED_DAEMON
|
---|
154 | static int daemon(int nochdir, int noclose)
|
---|
155 | {
|
---|
156 | int fd;
|
---|
157 |
|
---|
158 | switch (fork())
|
---|
159 | {
|
---|
160 | case -1: return (-1);
|
---|
161 | case 0: break;
|
---|
162 | default: _exit(0);
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (setsid()==(-1))
|
---|
166 | return(-1);
|
---|
167 |
|
---|
168 | if (!nochdir)
|
---|
169 | (void)chdir("/");
|
---|
170 |
|
---|
171 | if (!noclose && (fd=open("/dev/null", O_RDWR, 0)) != -1)
|
---|
172 | {
|
---|
173 | (void)dup2(fd, STDIN_FILENO);
|
---|
174 | (void)dup2(fd, STDOUT_FILENO);
|
---|
175 | (void)dup2(fd, STDERR_FILENO);
|
---|
176 | if (fd>2)
|
---|
177 | (void)close(fd);
|
---|
178 | }
|
---|
179 | return(0);
|
---|
180 | }
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | int recv_from_udpipe(uchar *buf, int l)
|
---|
184 | {
|
---|
185 | unsigned short n;
|
---|
186 | if (!pfd) return(-9);
|
---|
187 | read(pfd, buf, 3);
|
---|
188 | if (buf[0]!='U')
|
---|
189 | {
|
---|
190 | cs_log("INTERNAL PIPE-ERROR");
|
---|
191 | cs_exit(1);
|
---|
192 | }
|
---|
193 | memcpy(&n, buf+1, 2);
|
---|
194 | return(read(pfd, buf, n));
|
---|
195 | }
|
---|
196 |
|
---|
197 | char *username(int idx)
|
---|
198 | {
|
---|
199 | if (client[idx].usr[0])
|
---|
200 | return(client[idx].usr);
|
---|
201 | else
|
---|
202 | return("anonymous");
|
---|
203 | }
|
---|
204 |
|
---|
205 | static int idx_from_ip(in_addr_t ip, in_port_t port)
|
---|
206 | {
|
---|
207 | int i, idx;
|
---|
208 | for (i=idx=0; (i<CS_MAXPID) && (!idx); i++)
|
---|
209 | if ((client[i].ip==ip) && (client[i].port==port) &&
|
---|
210 | ((client[i].typ=='c') || (client[i].typ=='m')))
|
---|
211 | idx=i;
|
---|
212 | return(idx);
|
---|
213 | }
|
---|
214 |
|
---|
215 | int idx_from_pid(pid_t pid)
|
---|
216 | {
|
---|
217 | int i, idx;
|
---|
218 | for (i=0, idx=(-1); (i<CS_MAXPID) && (idx<0); i++)
|
---|
219 | if (client[i].pid==pid)
|
---|
220 | idx=i;
|
---|
221 | return(idx);
|
---|
222 | }
|
---|
223 |
|
---|
224 | static long chk_caid(ushort caid, CAIDTAB *ctab)
|
---|
225 | {
|
---|
226 | int n;
|
---|
227 | long rc;
|
---|
228 | for (rc=(-1), n=0; (n<CS_MAXCAIDTAB) && (rc<0); n++)
|
---|
229 | if ((caid & ctab->mask[n]) == ctab->caid[n])
|
---|
230 | rc=ctab->cmap[n] ? ctab->cmap[n] : caid;
|
---|
231 | return(rc);
|
---|
232 | }
|
---|
233 |
|
---|
234 | int chk_bcaid(ECM_REQUEST *er, CAIDTAB *ctab)
|
---|
235 | {
|
---|
236 | long caid;
|
---|
237 | if ((caid=chk_caid(er->caid, ctab))<0)
|
---|
238 | return(0);
|
---|
239 | er->caid=caid;
|
---|
240 | return(1);
|
---|
241 | }
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * void set_signal_handler(int sig, int flags, void (*sighandler)(int))
|
---|
245 | * flags: 1 = restart, 2 = don't modify if SIG_IGN, may be combined
|
---|
246 | */
|
---|
247 | void set_signal_handler(int sig, int flags, void (*sighandler)(int))
|
---|
248 | {
|
---|
249 | #ifdef CS_SIGBSD
|
---|
250 | if ((signal(sig, sighandler)==SIG_IGN) && (flags & 2))
|
---|
251 | {
|
---|
252 | signal(sig, SIG_IGN);
|
---|
253 | siginterrupt(sig, 0);
|
---|
254 | }
|
---|
255 | else
|
---|
256 | siginterrupt(sig, (flags & 1) ? 0 : 1);
|
---|
257 | #else
|
---|
258 | struct sigaction sa;
|
---|
259 | sigaction(sig, (struct sigaction *) 0, &sa);
|
---|
260 | if (!((flags & 2) && (sa.sa_handler==SIG_IGN)))
|
---|
261 | {
|
---|
262 | sigemptyset(&sa.sa_mask);
|
---|
263 | sa.sa_flags=(flags & 1) ? SA_RESTART : 0;
|
---|
264 | sa.sa_handler=sighandler;
|
---|
265 | sigaction(sig, &sa, (struct sigaction *) 0);
|
---|
266 | }
|
---|
267 | #endif
|
---|
268 | }
|
---|
269 |
|
---|
270 | static void cs_alarm(int sig)
|
---|
271 | {
|
---|
272 | cs_debug("Got alarm signal");
|
---|
273 | cs_log("disconnect from %s (deadlock!)", cs_inet_ntoa(client[cs_idx].ip));
|
---|
274 | cs_exit(0);
|
---|
275 | }
|
---|
276 |
|
---|
277 | static void cs_master_alarm(int sig)
|
---|
278 | {
|
---|
279 | cs_log("PANIC: master deadlock! last location: %s", mloc);
|
---|
280 | fprintf(stderr, "PANIC: master deadlock! last location: %s", mloc);
|
---|
281 | fflush(stderr);
|
---|
282 | cs_exit(0);
|
---|
283 | }
|
---|
284 |
|
---|
285 | static void cs_sigpipe(int sig)
|
---|
286 | {
|
---|
287 | if ((cs_idx) && (master_pid!=getppid()))
|
---|
288 | cs_exit(0);
|
---|
289 | cs_log("Got sigpipe signal -> captured");
|
---|
290 | }
|
---|
291 |
|
---|
292 | void cs_exit(int sig)
|
---|
293 | {
|
---|
294 | int i;
|
---|
295 |
|
---|
296 | set_signal_handler(SIGCHLD, 1, SIG_IGN);
|
---|
297 | set_signal_handler(SIGHUP , 1, SIG_IGN);
|
---|
298 | if (sig && (sig!=SIGQUIT))
|
---|
299 | cs_log("exit with signal %d", sig);
|
---|
300 | switch(client[cs_idx].typ)
|
---|
301 | {
|
---|
302 | case 'c': cs_statistics(cs_idx);
|
---|
303 | case 'm': break;
|
---|
304 | case 'n': *log_fd=0;
|
---|
305 | break;
|
---|
306 | case 's': *log_fd=0;
|
---|
307 | for (i=1; i<CS_MAXPID; i++)
|
---|
308 | if (client[i].pid)
|
---|
309 | kill(client[i].pid, SIGQUIT);
|
---|
310 | cs_log("cardserver down");
|
---|
311 | #ifndef CS_NOSHM
|
---|
312 | if (ecmcache) shmdt((void *)ecmcache);
|
---|
313 | #endif
|
---|
314 | break;
|
---|
315 | }
|
---|
316 | if (pfd) close(pfd);
|
---|
317 | #ifdef CS_NOSHM
|
---|
318 | munmap((void *)ecmcache, (size_t)shmsize);
|
---|
319 | if (shmid) close(shmid);
|
---|
320 | unlink(CS_MMAPFILE); // ignore errors, last process must succeed
|
---|
321 | #endif
|
---|
322 | exit(sig);
|
---|
323 | }
|
---|
324 |
|
---|
325 | static void cs_reinit_clients()
|
---|
326 | {
|
---|
327 | int i;
|
---|
328 | struct s_auth *account;
|
---|
329 |
|
---|
330 | for( i=1; i<CS_MAXPID; i++ )
|
---|
331 | if( client[i].pid && client[i].typ=='c' && client[i].usr[0] )
|
---|
332 | {
|
---|
333 | for (account=cfg->account; (account) ; account=account->next)
|
---|
334 | if (!strcmp(client[i].usr, account->usr))
|
---|
335 | break;
|
---|
336 |
|
---|
337 | if (account &&
|
---|
338 | client[i].pcrc==crc32(0L, MD5(account->pwd, strlen(account->pwd), NULL), 16))
|
---|
339 | {
|
---|
340 | client[i].grp = account->grp;
|
---|
341 | client[i].au = account->au;
|
---|
342 | client[i].tosleep = (60*account->tosleep);
|
---|
343 | client[i].monlvl = account->monlvl;
|
---|
344 | client[i].fchid = account->fchid; // CHID filters
|
---|
345 | client[i].cltab = account->cltab; // Class
|
---|
346 | client[i].ftab = account->ftab; // Ident
|
---|
347 | client[i].sidtabok= account->sidtabok; // services
|
---|
348 | client[i].sidtabno= account->sidtabno; // services
|
---|
349 | memcpy(&client[i].ctab, &account->ctab, sizeof(client[i].ctab));
|
---|
350 | #ifdef CS_ANTICASC
|
---|
351 | client[i].ac_idx = account->ac_idx;
|
---|
352 | client[i].ac_penalty = account->ac_penalty;
|
---|
353 | client[i].ac_limit = (account->ac_users*100+80)*cfg->ac_stime;
|
---|
354 | #endif
|
---|
355 | }
|
---|
356 | else
|
---|
357 | {
|
---|
358 | if (ph[client[i].ctyp].type & MOD_CONN_NET)
|
---|
359 | {
|
---|
360 | cs_debug("client '%s', pid=%d not found in db (or password changed)",
|
---|
361 | client[i].usr, client[i].pid);
|
---|
362 | kill(client[i].pid, SIGQUIT);
|
---|
363 | }
|
---|
364 | }
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | static void cs_sighup()
|
---|
369 | {
|
---|
370 | uchar dummy[1]={0x00};
|
---|
371 | write_to_pipe(fd_c2m, PIP_ID_HUP, dummy, 1);
|
---|
372 | }
|
---|
373 |
|
---|
374 | static void cs_accounts_chk()
|
---|
375 | {
|
---|
376 | int i;
|
---|
377 |
|
---|
378 | init_userdb();
|
---|
379 | cs_reinit_clients();
|
---|
380 | #ifdef CS_ANTICASC
|
---|
381 | for (i=0; i<CS_MAXPID; i++)
|
---|
382 | if (client[i].typ=='a')
|
---|
383 | {
|
---|
384 | kill(client[i].pid, SIGHUP);
|
---|
385 | break;
|
---|
386 | }
|
---|
387 | #endif
|
---|
388 | }
|
---|
389 |
|
---|
390 | static void cs_debug_level()
|
---|
391 | {
|
---|
392 | int i;
|
---|
393 |
|
---|
394 | cs_dblevel ^= D_ALL_DUMP;
|
---|
395 | if (master_pid==getpid())
|
---|
396 | for (i=0; i<CS_MAXPID && client[i].pid; i++)
|
---|
397 | client[i].dbglvl=cs_dblevel;
|
---|
398 | else
|
---|
399 | client[cs_idx].dbglvl=cs_dblevel;
|
---|
400 | cs_log("%sdebug_level=%d", (master_pid==getpid())?"all ":"",cs_dblevel);
|
---|
401 | }
|
---|
402 |
|
---|
403 | static void cs_card_info(int i)
|
---|
404 | {
|
---|
405 | uchar dummy[1]={0x00};
|
---|
406 | for( i=1; i<CS_MAXPID; i++ )
|
---|
407 | if( client[i].pid && client[i].typ=='r' && client[i].fd_m2c )
|
---|
408 | write_to_pipe(client[i].fd_m2c, PIP_ID_CIN, dummy, 1);
|
---|
409 |
|
---|
410 | //kill(client[i].pid, SIGUSR2);
|
---|
411 | }
|
---|
412 |
|
---|
413 | static void cs_child_chk(int i)
|
---|
414 | {
|
---|
415 | while (waitpid(0, NULL, WNOHANG)>0);
|
---|
416 | for (i=1; i<CS_MAXPID; i++)
|
---|
417 | if (client[i].pid)
|
---|
418 | if (kill(client[i].pid, 0)) {
|
---|
419 | if ((client[i].typ!='c') && (client[i].typ!='m'))
|
---|
420 | {
|
---|
421 | char *txt="";
|
---|
422 | *log_fd=0;
|
---|
423 | switch(client[i].typ)
|
---|
424 | {
|
---|
425 | #ifdef CS_ANTICASC
|
---|
426 | case 'a': txt="anticascader"; break;
|
---|
427 | #endif
|
---|
428 | case 'l': txt="logger"; break;
|
---|
429 | case 'p': txt="proxy"; break;
|
---|
430 | case 'r': txt="reader"; break;
|
---|
431 | case 'n': txt="resolver"; break;
|
---|
432 | }
|
---|
433 | cs_log("PANIC: %s lost !! (pid=%d)", txt, client[i].pid);
|
---|
434 | cs_exit(1);
|
---|
435 | }
|
---|
436 | else
|
---|
437 | {
|
---|
438 | #ifdef CS_ANTICASC
|
---|
439 | char usr[32];
|
---|
440 | ushort ac_idx;
|
---|
441 | ushort ac_limit;
|
---|
442 | uchar ac_penalty;
|
---|
443 | if( cfg->ac_enabled )
|
---|
444 | {
|
---|
445 | strncpy(usr, client[i].usr, sizeof(usr)-1);
|
---|
446 | ac_idx = client[i].ac_idx;
|
---|
447 | ac_limit = client[i].ac_limit;
|
---|
448 | ac_penalty = client[i].ac_penalty;
|
---|
449 | }
|
---|
450 | #endif
|
---|
451 | if (client[i].fd_m2c) close(client[i].fd_m2c);
|
---|
452 | if (client[i].ufd) close(client[i].ufd);
|
---|
453 | memset(&client[i], 0, sizeof(struct s_client));
|
---|
454 | #ifdef CS_ANTICASC
|
---|
455 | if( cfg->ac_enabled )
|
---|
456 | {
|
---|
457 | client[i].ac_idx = ac_idx;
|
---|
458 | client[i].ac_limit = ac_limit;
|
---|
459 | client[i].ac_penalty = ac_penalty;
|
---|
460 | strcpy(client[i].usr, usr);
|
---|
461 | }
|
---|
462 | #endif
|
---|
463 | client[i].au=(-1);
|
---|
464 | }
|
---|
465 | }
|
---|
466 | return;
|
---|
467 | }
|
---|
468 |
|
---|
469 | int cs_fork(in_addr_t ip, in_port_t port)
|
---|
470 | {
|
---|
471 | int i;
|
---|
472 | pid_t pid;
|
---|
473 | for (i=1; (i<CS_MAXPID) && (client[i].pid); i++);
|
---|
474 | if (i<CS_MAXPID)
|
---|
475 | {
|
---|
476 | int fdp[2];
|
---|
477 | memset(&client[i], 0, sizeof(struct s_client));
|
---|
478 | client[i].au=(-1);
|
---|
479 | if (pipe(fdp))
|
---|
480 | {
|
---|
481 | cs_log("Cannot create pipe (errno=%d)", errno);
|
---|
482 | cs_exit(1);
|
---|
483 | }
|
---|
484 | switch(pid=fork())
|
---|
485 | {
|
---|
486 | case -1:
|
---|
487 | cs_log("PANIC: Cannot fork() (errno=%d)", errno);
|
---|
488 | cs_exit(1);
|
---|
489 | case 0: // HERE is client
|
---|
490 | alarm(0);
|
---|
491 | set_signal_handler(SIGALRM, 0, cs_alarm);
|
---|
492 | set_signal_handler(SIGCHLD, 1, SIG_IGN);
|
---|
493 | set_signal_handler(SIGHUP , 1, SIG_IGN);
|
---|
494 | set_signal_handler(SIGINT , 1, SIG_IGN);
|
---|
495 | set_signal_handler(SIGUSR1, 1, cs_debug_level);
|
---|
496 | is_server=((ip) || (port<90)) ? 1 : 0;
|
---|
497 | fd_m2c=fdp[0];
|
---|
498 | close(fdp[1]);
|
---|
499 | close(mfdr);
|
---|
500 | if( port!=97 ) cs_close_log();
|
---|
501 | mfdr=0;
|
---|
502 | cs_ptyp=D_CLIENT;
|
---|
503 | cs_idx=i;
|
---|
504 | #ifndef CS_NOSHM
|
---|
505 | shmid=0;
|
---|
506 | #endif
|
---|
507 | break;
|
---|
508 | default: // HERE is master
|
---|
509 | client[i].fd_m2c=fdp[1];
|
---|
510 | client[i].dbglvl=cs_dblevel;
|
---|
511 | close(fdp[0]);
|
---|
512 | if (ip)
|
---|
513 | {
|
---|
514 | client[i].typ='c'; // dynamic client
|
---|
515 | client[i].ip=ip;
|
---|
516 | client[i].port=port;
|
---|
517 | cs_log("client(%d) connect from %s (pid=%d, pipfd=%d)",
|
---|
518 | i-cdiff, cs_inet_ntoa(ip), pid, client[i].fd_m2c);
|
---|
519 | }
|
---|
520 | else
|
---|
521 | {
|
---|
522 | client[i].stat=1;
|
---|
523 | switch(port)
|
---|
524 | {
|
---|
525 | case 99: client[i].typ='r'; // reader
|
---|
526 | client[i].sidtabok=reader[ridx].sidtabok;
|
---|
527 | client[i].sidtabno=reader[ridx].sidtabno;
|
---|
528 | reader[ridx].fd=client[i].fd_m2c;
|
---|
529 | reader[ridx].cs_idx=i;
|
---|
530 | if (reader[ridx].r_port)
|
---|
531 | cs_log("proxy started (pid=%d, server=%s)",
|
---|
532 | pid, reader[ridx].device);
|
---|
533 | else
|
---|
534 | {
|
---|
535 | if (reader[ridx].typ==R_MOUSE)
|
---|
536 | cs_log("reader started (pid=%d, device=%s, detect=%s%s)",
|
---|
537 | pid, reader[ridx].device,
|
---|
538 | reader[ridx].detect&0x80 ? "!" : "",
|
---|
539 | RDR_CD_TXT[reader[ridx].detect&0x7f]);
|
---|
540 | else
|
---|
541 | cs_log("reader started (pid=%d, device=%s)",
|
---|
542 | pid, reader[ridx].device);
|
---|
543 | client[i].ip=client[0].ip;
|
---|
544 | strcpy(client[i].usr, client[0].usr);
|
---|
545 | }
|
---|
546 | cdiff=i;
|
---|
547 | break;
|
---|
548 | case 98: client[i].typ='n'; // resolver
|
---|
549 | client[i].ip=client[0].ip;
|
---|
550 | strcpy(client[i].usr, client[0].usr);
|
---|
551 | cs_log("resolver started (pid=%d, delay=%d sec)",
|
---|
552 | pid, cfg->resolvedelay);
|
---|
553 | cdiff=i;
|
---|
554 | break;
|
---|
555 | case 97: client[i].typ='l'; // logger
|
---|
556 | client[i].ip=client[0].ip;
|
---|
557 | strcpy(client[i].usr, client[0].usr);
|
---|
558 | cs_log("logger started (pid=%d)", pid);
|
---|
559 | cdiff=i;
|
---|
560 | break;
|
---|
561 | #ifdef CS_ANTICASC
|
---|
562 | case 96: client[i].typ='a';
|
---|
563 | client[i].ip=client[0].ip;
|
---|
564 | strcpy(client[i].usr, client[0].usr);
|
---|
565 | cs_log("anticascader started (pid=%d, delay=%d min)",
|
---|
566 | pid, cfg->ac_stime);
|
---|
567 | cdiff=i;
|
---|
568 | break;
|
---|
569 | #endif
|
---|
570 | default: client[i].typ='c'; // static client
|
---|
571 | client[i].ip=client[0].ip;
|
---|
572 | client[i].ctyp=port;
|
---|
573 | cs_log("%s: initialized (pid=%d%s)", ph[port].desc,
|
---|
574 | pid, ph[port].logtxt ? ph[port].logtxt : "");
|
---|
575 | break;
|
---|
576 | }
|
---|
577 | }
|
---|
578 | client[i].login=client[i].last=time((time_t *)0);
|
---|
579 | client[i].pid=pid; // MUST be last -> wait4master()
|
---|
580 | cs_last_idx=i;
|
---|
581 | i=0;
|
---|
582 | }
|
---|
583 | }
|
---|
584 | else
|
---|
585 | {
|
---|
586 | cs_log("max connections reached -> reject client %s", cs_inet_ntoa(ip));
|
---|
587 | i=(-1);
|
---|
588 | }
|
---|
589 | return(i);
|
---|
590 | }
|
---|
591 |
|
---|
592 | static void init_signal()
|
---|
593 | {
|
---|
594 | int i;
|
---|
595 | for (i=1; i<NSIG; i++)
|
---|
596 | set_signal_handler(i, 3, cs_exit);
|
---|
597 | set_signal_handler(SIGWINCH, 1, SIG_IGN);
|
---|
598 | // set_signal_handler(SIGPIPE , 0, SIG_IGN);
|
---|
599 | set_signal_handler(SIGPIPE , 0, cs_sigpipe);
|
---|
600 | // set_signal_handler(SIGALRM , 0, cs_alarm);
|
---|
601 | set_signal_handler(SIGALRM , 0, cs_master_alarm);
|
---|
602 | set_signal_handler(SIGCHLD , 1, cs_child_chk);
|
---|
603 | // set_signal_handler(SIGHUP , 1, cs_accounts_chk);
|
---|
604 | set_signal_handler(SIGHUP , 1, cs_sighup);
|
---|
605 | set_signal_handler(SIGUSR1, 1, cs_debug_level);
|
---|
606 | set_signal_handler(SIGUSR2, 1, cs_card_info);
|
---|
607 | set_signal_handler(SIGCONT, 1, SIG_IGN);
|
---|
608 | cs_log("signal handling initialized (type=%s)",
|
---|
609 | #ifdef CS_SIGBSD
|
---|
610 | "bsd"
|
---|
611 | #else
|
---|
612 | "sysv"
|
---|
613 | #endif
|
---|
614 | );
|
---|
615 | return;
|
---|
616 | }
|
---|
617 |
|
---|
618 | static void init_shm()
|
---|
619 | {
|
---|
620 | #ifdef CS_NOSHM
|
---|
621 | //int i, fd;
|
---|
622 | char *buf;
|
---|
623 | if ((shmid=open(cs_memfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))<0)
|
---|
624 | {
|
---|
625 | fprintf(stderr, "Cannot create mmaped file (errno=%d)", errno);
|
---|
626 | cs_exit(1);
|
---|
627 | }
|
---|
628 |
|
---|
629 | buf=(char *)malloc(shmsize);
|
---|
630 | memset(buf, 0, shmsize);
|
---|
631 | write(shmid, buf, shmsize);
|
---|
632 | free(buf);
|
---|
633 |
|
---|
634 | ecmcache=(struct s_ecm *)mmap((void *)0, (size_t) shmsize,
|
---|
635 | PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0);
|
---|
636 | #else
|
---|
637 | struct shmid_ds sd;
|
---|
638 | char *shmerr_txt="Cannot %s shared memory (errno=%d)\n";
|
---|
639 | if ((shmid=shmget(IPC_PRIVATE, shmsize, IPC_CREAT | 0600))<0)
|
---|
640 | {
|
---|
641 | fprintf(stderr, shmerr_txt, "create", errno);
|
---|
642 | shmid=0;
|
---|
643 | cs_exit(1);
|
---|
644 | }
|
---|
645 | if ((ecmcache=(struct s_ecm *)shmat(shmid, 0, 0))==(void *)(-1))
|
---|
646 | {
|
---|
647 | fprintf(stderr, shmerr_txt, "attach", errno);
|
---|
648 | cs_exit(1);
|
---|
649 | }
|
---|
650 | memset(ecmcache, 0, shmsize);
|
---|
651 | shmctl(shmid, IPC_RMID, &sd);
|
---|
652 | #endif
|
---|
653 | #ifdef CS_ANTICASC
|
---|
654 | acasc=(struct s_acasc_shm *)&ecmcache[CS_ECMCACHESIZE];
|
---|
655 | ecmidx=(int *)&acasc[CS_MAXPID];
|
---|
656 | #else
|
---|
657 | ecmidx=(int *)&ecmcache[CS_ECMCACHESIZE];
|
---|
658 | #endif
|
---|
659 | mcl=(int *)((void *)ecmidx+sizeof(int));
|
---|
660 | logidx=(int *)((void *)mcl+sizeof(int));
|
---|
661 | c_start=(int *)((void *)logidx+sizeof(int));
|
---|
662 | log_fd=(int *)((void *)c_start+sizeof(int));
|
---|
663 | mpcs_sem=(int *)((void *)log_fd+sizeof(int));
|
---|
664 | client=(struct s_client *)((void *)mpcs_sem+sizeof(int));
|
---|
665 | reader=(struct s_reader *)&client[CS_MAXPID];
|
---|
666 | #ifdef CS_WITH_GBOX
|
---|
667 | Cards=(struct card_struct*)&reader[CS_MAXREADER];
|
---|
668 | IgnoreList=(unsigned long*)&Cards[CS_MAXCARDS];
|
---|
669 | idstore=(struct idstore_struct*)&IgnoreList[CS_MAXIGNORE];
|
---|
670 | cfg=(struct s_config *)&idstore[CS_MAXPID];
|
---|
671 | #else
|
---|
672 | cfg=(struct s_config *)&reader[CS_MAXREADER];
|
---|
673 | #endif
|
---|
674 | #ifdef CS_LOGHISTORY
|
---|
675 | loghistidx=(int *)((void *)cfg+sizeof(struct s_config));
|
---|
676 | loghist=(char *)((void *)loghistidx+sizeof(int));
|
---|
677 | #endif
|
---|
678 |
|
---|
679 | #ifdef DEBUG_SHM_POINTER
|
---|
680 | printf("SHM ALLOC: %x\n", shmsize);
|
---|
681 | printf("SHM START: %p\n", (void *) ecmcache);
|
---|
682 | printf("SHM ST1: %p %x (%x)\n", (void *) ecmidx, ((void *) ecmidx) - ((void *) ecmcache), CS_ECMCACHESIZE*(sizeof(struct s_ecm)));
|
---|
683 | printf("SHM ST2: %p %x (%x)\n", (void *) mpcs_sem, ((void *) mpcs_sem) - ((void *) ecmidx), sizeof(int));
|
---|
684 | printf("SHM ST3: %p %x (%x)\n", (void *) client, ((void *) client) - ((void *) mpcs_sem), sizeof(int));
|
---|
685 | printf("SHM ST4: %p %x (%x)\n", (void *) reader, ((void *) reader) - ((void *) client), CS_MAXPID*(sizeof(struct s_client)));
|
---|
686 | printf("SHM ST5: %p %x (%x)\n", (void *) cfg, ((void *) cfg) - ((void *) reader), CS_MAXREADER*(sizeof(struct s_reader)));
|
---|
687 | printf("SHM ST6: %p %x (%x)\n", ((void *) cfg)+sizeof(struct s_config), sizeof(struct s_config), sizeof(struct s_config));
|
---|
688 | printf("SHM ENDE: %p\n", ((void *) cfg)+sizeof(struct s_config));
|
---|
689 | printf("SHM SIZE: %x\n", ((void *) cfg)-((void *) ecmcache) + sizeof(struct s_config));
|
---|
690 | fflush(stdout);
|
---|
691 | #endif
|
---|
692 |
|
---|
693 | *ecmidx=0;
|
---|
694 | *logidx=0;
|
---|
695 | *mpcs_sem=0;
|
---|
696 | client[0].pid=getpid();
|
---|
697 | client[0].login=time((time_t *)0);
|
---|
698 | client[0].ip=cs_inet_addr("127.0.0.1");
|
---|
699 | client[0].typ='s';
|
---|
700 | client[0].au=(-1);
|
---|
701 | client[0].dbglvl=cs_dblevel;
|
---|
702 | strcpy(client[0].usr, "root");
|
---|
703 | #ifdef CS_LOGHISTORY
|
---|
704 | *loghistidx=0;
|
---|
705 | memset(loghist, 0, CS_MAXLOGHIST*CS_LOGHISTSIZE);
|
---|
706 | #endif
|
---|
707 | }
|
---|
708 |
|
---|
709 | static int start_listener(struct s_module *ph, int port_idx)
|
---|
710 | {
|
---|
711 | int ov=1, timeout, is_udp, i;
|
---|
712 | char ptxt[2][32];
|
---|
713 | //struct hostent *ptrh; /* pointer to a host table entry */
|
---|
714 | struct protoent *ptrp; /* pointer to a protocol table entry */
|
---|
715 | struct sockaddr_in sad; /* structure to hold server's address */
|
---|
716 |
|
---|
717 | ptxt[0][0]=ptxt[1][0]='\0';
|
---|
718 | if (!ph->ptab->ports[port_idx].s_port)
|
---|
719 | {
|
---|
720 | cs_log("%s: disabled", ph->desc);
|
---|
721 | return(0);
|
---|
722 | }
|
---|
723 | is_udp=(ph->type==MOD_CONN_UDP);
|
---|
724 |
|
---|
725 | memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
|
---|
726 | sad.sin_family = AF_INET; /* set family to Internet */
|
---|
727 | if (!ph->s_ip)
|
---|
728 | ph->s_ip=cfg->srvip;
|
---|
729 | if (ph->s_ip)
|
---|
730 | {
|
---|
731 | sad.sin_addr.s_addr=ph->s_ip;
|
---|
732 | sprintf(ptxt[0], ", ip=%s", inet_ntoa(ph->s_ip));
|
---|
733 | }
|
---|
734 | else
|
---|
735 | sad.sin_addr.s_addr=INADDR_ANY;
|
---|
736 | timeout=cfg->bindwait;
|
---|
737 | //ph->fd=0;
|
---|
738 | ph->ptab->ports[port_idx].fd = 0;
|
---|
739 |
|
---|
740 | if (ph->ptab->ports[port_idx].s_port > 0) /* test for illegal value */
|
---|
741 | sad.sin_port = htons((u_short)ph->ptab->ports[port_idx].s_port);
|
---|
742 | else
|
---|
743 | {
|
---|
744 | cs_log("%s: Bad port %d", ph->desc, ph->ptab->ports[port_idx].s_port);
|
---|
745 | return(0);
|
---|
746 | }
|
---|
747 |
|
---|
748 | /* Map transport protocol name to protocol number */
|
---|
749 |
|
---|
750 | if( (ptrp=getprotobyname(is_udp ? "udp" : "tcp")) )
|
---|
751 | ov=ptrp->p_proto;
|
---|
752 | else
|
---|
753 | ov=(is_udp) ? 17 : 6; // use defaults on error
|
---|
754 |
|
---|
755 | if ((ph->ptab->ports[port_idx].fd=socket(PF_INET,is_udp ? SOCK_DGRAM : SOCK_STREAM, ov))<0)
|
---|
756 | {
|
---|
757 | cs_log("%s: Cannot create socket (errno=%d)", ph->desc, errno);
|
---|
758 | return(0);
|
---|
759 | }
|
---|
760 |
|
---|
761 | ov=1;
|
---|
762 | if (setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_REUSEADDR, (void *)&ov, sizeof(ov))<0)
|
---|
763 | {
|
---|
764 | cs_log("%s: setsockopt failed (errno=%d)", ph->desc, errno);
|
---|
765 | close(ph->ptab->ports[port_idx].fd);
|
---|
766 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
767 | }
|
---|
768 |
|
---|
769 | #ifdef SO_REUSEPORT
|
---|
770 | setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_REUSEPORT, (void *)&ov, sizeof(ov));
|
---|
771 | #endif
|
---|
772 |
|
---|
773 | #ifdef SO_PRIORITY
|
---|
774 | if (cfg->netprio)
|
---|
775 | if (!setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg->netprio, sizeof(ulong)))
|
---|
776 | sprintf(ptxt[1], ", prio=%d", cfg->netprio);
|
---|
777 | #endif
|
---|
778 |
|
---|
779 | if( !is_udp )
|
---|
780 | {
|
---|
781 | ulong keep_alive = 1;
|
---|
782 | setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
783 | (void *)&keep_alive, sizeof(ulong));
|
---|
784 | }
|
---|
785 |
|
---|
786 | while (timeout--)
|
---|
787 | {
|
---|
788 | if (bind(ph->ptab->ports[port_idx].fd, (struct sockaddr *)&sad, sizeof (sad))<0)
|
---|
789 | {
|
---|
790 | if (timeout)
|
---|
791 | {
|
---|
792 | cs_log("%s: Bind request failed, waiting another %d seconds",
|
---|
793 | ph->desc, timeout);
|
---|
794 | sleep(1);
|
---|
795 | }
|
---|
796 | else
|
---|
797 | {
|
---|
798 | cs_log("%s: Bind request failed, giving up", ph->desc);
|
---|
799 | close(ph->ptab->ports[port_idx].fd);
|
---|
800 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
801 | }
|
---|
802 | }
|
---|
803 | else timeout=0;
|
---|
804 | }
|
---|
805 |
|
---|
806 | if (!is_udp)
|
---|
807 | if (listen(ph->ptab->ports[port_idx].fd, CS_QLEN)<0)
|
---|
808 | {
|
---|
809 | cs_log("%s: Cannot start listen mode (errno=%d)", ph->desc, errno);
|
---|
810 | close(ph->ptab->ports[port_idx].fd);
|
---|
811 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
812 | }
|
---|
813 |
|
---|
814 | cs_log("%s: initialized (fd=%d, port=%d%s%s%s)",
|
---|
815 | ph->desc, ph->ptab->ports[port_idx].fd,
|
---|
816 | ph->ptab->ports[port_idx].s_port,
|
---|
817 | ptxt[0], ptxt[1], ph->logtxt ? ph->logtxt : "");
|
---|
818 |
|
---|
819 | for( i=0; i<ph->ptab->ports[port_idx].ftab.nfilts; i++ ) {
|
---|
820 | int j;
|
---|
821 | cs_log("CAID: %04X", ph->ptab->ports[port_idx].ftab.filts[i].caid );
|
---|
822 | for( j=0; j<ph->ptab->ports[port_idx].ftab.filts[i].nprids; j++ )
|
---|
823 | cs_log("provid #%d: %06X", j, ph->ptab->ports[port_idx].ftab.filts[i].prids[j]);
|
---|
824 | }
|
---|
825 | return(ph->ptab->ports[port_idx].fd);
|
---|
826 | }
|
---|
827 |
|
---|
828 | static void *cs_client_resolve(void *dummy)
|
---|
829 | {
|
---|
830 | while (1)
|
---|
831 | {
|
---|
832 | struct hostent *rht;
|
---|
833 | struct s_auth *account;
|
---|
834 | struct sockaddr_in udp_sa;
|
---|
835 |
|
---|
836 | for (account=cfg->account; account; account=account->next)
|
---|
837 | if (account->dyndns[0])
|
---|
838 | {
|
---|
839 | if (rht=gethostbyname(account->dyndns))
|
---|
840 | {
|
---|
841 | memcpy(&udp_sa.sin_addr, rht->h_addr, sizeof(udp_sa.sin_addr));
|
---|
842 | account->dynip=cs_inet_order(udp_sa.sin_addr.s_addr);
|
---|
843 | }
|
---|
844 | else
|
---|
845 | cs_log("can't resolve hostname %s (user: %s)", account->dyndns, account->usr);
|
---|
846 | client[cs_idx].last=time((time_t)0);
|
---|
847 | }
|
---|
848 | sleep(cfg->resolvedelay);
|
---|
849 | }
|
---|
850 | }
|
---|
851 |
|
---|
852 | static void start_client_resolver()
|
---|
853 | {
|
---|
854 | int i;
|
---|
855 | pthread_t tid;
|
---|
856 |
|
---|
857 | if (i=pthread_create(&tid, (pthread_attr_t *)0, cs_client_resolve, (void *) 0))
|
---|
858 | cs_log("ERROR: can't create resolver-thread (err=%d)", i);
|
---|
859 | else
|
---|
860 | {
|
---|
861 | cs_log("resolver thread started");
|
---|
862 | pthread_detach(tid);
|
---|
863 | }
|
---|
864 | }
|
---|
865 |
|
---|
866 | void cs_resolve()
|
---|
867 | {
|
---|
868 | int i, idx;
|
---|
869 | struct hostent *rht;
|
---|
870 | struct s_auth *account;
|
---|
871 | for (i=0; i<CS_MAXREADER; i++)
|
---|
872 | if ((idx=reader[i].cs_idx) && (reader[i].typ & R_IS_NETWORK))
|
---|
873 | {
|
---|
874 | client[cs_idx].last=time((time_t)0);
|
---|
875 | if (rht=gethostbyname(reader[i].device))
|
---|
876 | {
|
---|
877 | memcpy(&client[idx].udp_sa.sin_addr, rht->h_addr,
|
---|
878 | sizeof(client[idx].udp_sa.sin_addr));
|
---|
879 | client[idx].ip=cs_inet_order(client[idx].udp_sa.sin_addr.s_addr);
|
---|
880 | }
|
---|
881 | else
|
---|
882 | cs_log("can't resolve %s", reader[i].device);
|
---|
883 | client[cs_idx].last=time((time_t)0);
|
---|
884 | }
|
---|
885 | }
|
---|
886 |
|
---|
887 | #ifdef USE_PTHREAD
|
---|
888 | static void *cs_logger(void *dummy)
|
---|
889 | #else
|
---|
890 | static void cs_logger(void)
|
---|
891 | #endif
|
---|
892 | {
|
---|
893 | *log_fd=client[cs_idx].fd_m2c;
|
---|
894 | while(1)
|
---|
895 | {
|
---|
896 | uchar *ptr;
|
---|
897 | //struct timeval tv;
|
---|
898 | fd_set fds;
|
---|
899 |
|
---|
900 | FD_ZERO(&fds);
|
---|
901 | FD_SET(fd_m2c, &fds);
|
---|
902 | select(fd_m2c+1, &fds, 0, 0, 0);
|
---|
903 | #ifndef USE_PTHREAD
|
---|
904 | if (master_pid!=getppid())
|
---|
905 | cs_exit(0);
|
---|
906 | #endif
|
---|
907 | if (FD_ISSET(fd_m2c, &fds))
|
---|
908 | {
|
---|
909 | int n;
|
---|
910 | // switch(n=read_from_pipe(fd_m2c, &ptr, 1))
|
---|
911 | n=read_from_pipe(fd_m2c, &ptr, 1);
|
---|
912 | //if (n!=PIP_ID_NUL) printf("received %d bytes\n", n); fflush(stdout);
|
---|
913 | switch(n)
|
---|
914 | {
|
---|
915 | case PIP_ID_LOG:
|
---|
916 | cs_write_log(ptr);
|
---|
917 | break;
|
---|
918 | }
|
---|
919 | }
|
---|
920 | }
|
---|
921 | }
|
---|
922 |
|
---|
923 | static void start_resolver()
|
---|
924 | {
|
---|
925 | int i;
|
---|
926 | #ifdef USE_PTHREAD
|
---|
927 | pthread_t tid;
|
---|
928 | if (i=pthread_create(&tid, (pthread_attr_t *)0, cs_logger, (void *) 0))
|
---|
929 | cs_log("ERROR: can't create logging-thread (err=%d)", i);
|
---|
930 | else
|
---|
931 | {
|
---|
932 | cs_log("logging thread started");
|
---|
933 | pthread_detach(tid);
|
---|
934 | }
|
---|
935 | #endif
|
---|
936 | sleep(1); // wait for reader
|
---|
937 | while(1)
|
---|
938 | {
|
---|
939 | if (master_pid!=getppid())
|
---|
940 | cs_exit(0);
|
---|
941 | cs_resolve();
|
---|
942 | for (i=0; i<cfg->resolvedelay; i++)
|
---|
943 | if (master_pid!=getppid())
|
---|
944 | cs_exit(0);
|
---|
945 | else
|
---|
946 | sleep(1);
|
---|
947 | // sleep(cfg->resolvedelay);
|
---|
948 | }
|
---|
949 | }
|
---|
950 |
|
---|
951 | #ifdef CS_ANTICASC
|
---|
952 | static void start_anticascader()
|
---|
953 | {
|
---|
954 | int i;
|
---|
955 |
|
---|
956 | use_ac_log=1;
|
---|
957 | set_signal_handler(SIGHUP, 1, ac_init_stat);
|
---|
958 |
|
---|
959 | ac_init_stat(0);
|
---|
960 | while(1)
|
---|
961 | {
|
---|
962 | for( i=0; i<cfg->ac_stime*60; i++ )
|
---|
963 | if( master_pid!=getppid() )
|
---|
964 | cs_exit(0);
|
---|
965 | else
|
---|
966 | sleep(1);
|
---|
967 |
|
---|
968 | if (master_pid!=getppid())
|
---|
969 | cs_exit(0);
|
---|
970 |
|
---|
971 | ac_do_stat();
|
---|
972 | }
|
---|
973 | }
|
---|
974 | #endif
|
---|
975 |
|
---|
976 | static void init_cardreader()
|
---|
977 | {
|
---|
978 | for (ridx=0; ridx<CS_MAXREADER; ridx++)
|
---|
979 | if (reader[ridx].device[0])
|
---|
980 | switch(cs_fork(0, 99))
|
---|
981 | {
|
---|
982 | case -1:
|
---|
983 | cs_exit(1);
|
---|
984 | case 0:
|
---|
985 | break;
|
---|
986 | default:
|
---|
987 | wait4master();
|
---|
988 | start_cardreader();
|
---|
989 | }
|
---|
990 | }
|
---|
991 |
|
---|
992 | static void init_service(int srv)
|
---|
993 | {
|
---|
994 | switch(cs_fork(0, srv))
|
---|
995 | {
|
---|
996 | case -1:
|
---|
997 | cs_exit(1);
|
---|
998 | case 0:
|
---|
999 | break;
|
---|
1000 | default:
|
---|
1001 | wait4master();
|
---|
1002 | switch(srv)
|
---|
1003 | {
|
---|
1004 | #ifdef CS_ANTICASC
|
---|
1005 | case 96: start_anticascader();
|
---|
1006 | #endif
|
---|
1007 | case 97: cs_logger();
|
---|
1008 | case 98: start_resolver();
|
---|
1009 | }
|
---|
1010 | }
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | void wait4master()
|
---|
1014 | {
|
---|
1015 | int i;
|
---|
1016 | for (i=0; (i<1000) && (client[cs_idx].pid!=getpid()); i++)
|
---|
1017 | usleep(1000L);
|
---|
1018 | if (client[cs_idx].pid!=getpid())
|
---|
1019 | {
|
---|
1020 | cs_log("PANIC: client not found in shared memory");
|
---|
1021 | cs_exit(1);
|
---|
1022 | }
|
---|
1023 | cs_debug("starting client %d with ip %s",
|
---|
1024 | cs_idx-cdiff, cs_inet_ntoa(client[cs_idx].ip));
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | static void cs_fake_client(char *usr)
|
---|
1028 | {
|
---|
1029 | int i;
|
---|
1030 | for (i=cdiff+1; i<CS_MAXPID; i++)
|
---|
1031 | if ((client[i].pid) && (client[i].typ=='c') &&
|
---|
1032 | (!client[i].dup) && (!strcmp(client[i].usr, usr)))
|
---|
1033 | {
|
---|
1034 | client[i].dup=1;
|
---|
1035 | client[i].au=(-1);
|
---|
1036 | cs_log("client %d duplicate user '%s', set to fake", i-cdiff, usr);
|
---|
1037 | }
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | int cs_auth_client(struct s_auth *account, char *e_txt)
|
---|
1041 | {
|
---|
1042 | int rc=0;
|
---|
1043 | char buf[16];
|
---|
1044 | char *t_crypt="encrypted";
|
---|
1045 | char *t_plain="plain";
|
---|
1046 | char *t_grant=" granted";
|
---|
1047 | char *t_reject=" rejected";
|
---|
1048 | char *t_msg[]= { buf, "invalid access", "invalid ip", "unknown reason" };
|
---|
1049 | client[cs_idx].grp=0xffffffff;
|
---|
1050 | client[cs_idx].au=(-1);
|
---|
1051 | switch((long)account)
|
---|
1052 | {
|
---|
1053 | case -2: // gbx-dummy
|
---|
1054 | client[cs_idx].dup=0;
|
---|
1055 | break;
|
---|
1056 | case 0: // reject access
|
---|
1057 | rc=1;
|
---|
1058 | cs_log("%s %s-client %s%s (%s)",
|
---|
1059 | client[cs_idx].crypted ? t_crypt : t_plain,
|
---|
1060 | ph[client[cs_idx].ctyp].desc,
|
---|
1061 | client[cs_idx].ip ? cs_inet_ntoa(client[cs_idx].ip) : "",
|
---|
1062 | client[cs_idx].ip ? t_reject : t_reject+1,
|
---|
1063 | e_txt ? e_txt : t_msg[rc]);
|
---|
1064 | break;
|
---|
1065 | default: // grant/check access
|
---|
1066 | if (client[cs_idx].ip && account->dyndns[0])
|
---|
1067 | if (client[cs_idx].ip != account->dynip)
|
---|
1068 | rc=2;
|
---|
1069 | if (!rc)
|
---|
1070 | {
|
---|
1071 | client[cs_idx].dup=0;
|
---|
1072 | if (client[cs_idx].typ=='c')
|
---|
1073 | {
|
---|
1074 | client[cs_idx].grp=account->grp;
|
---|
1075 | client[cs_idx].au=account->au;
|
---|
1076 | client[cs_idx].tosleep=(60*account->tosleep);
|
---|
1077 | memcpy(&client[cs_idx].ctab, &account->ctab, sizeof(client[cs_idx].ctab));
|
---|
1078 | if (account->uniq)
|
---|
1079 | cs_fake_client(account->usr);
|
---|
1080 | client[cs_idx].ftab = account->ftab; // IDENT filter
|
---|
1081 | client[cs_idx].cltab = account->cltab; // CLASS filter
|
---|
1082 | client[cs_idx].fchid = account->fchid; // CHID filter
|
---|
1083 | client[cs_idx].sidtabok= account->sidtabok; // services
|
---|
1084 | client[cs_idx].sidtabno= account->sidtabno; // services
|
---|
1085 | client[cs_idx].pcrc = crc32(0L, MD5(account->pwd, strlen(account->pwd), NULL), 16);
|
---|
1086 | premhack=account->premhack;
|
---|
1087 | #ifdef CS_ANTICASC
|
---|
1088 | ac_init_client(account);
|
---|
1089 | #endif
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 | client[cs_idx].monlvl=account->monlvl;
|
---|
1093 | strcpy(client[cs_idx].usr, account->usr);
|
---|
1094 | case -1: // anonymous grant access
|
---|
1095 | if (rc)
|
---|
1096 | t_grant=t_reject;
|
---|
1097 | else
|
---|
1098 | {
|
---|
1099 | if (client[cs_idx].typ=='m')
|
---|
1100 | sprintf(t_msg[0], "lvl=%d", client[cs_idx].monlvl);
|
---|
1101 | else
|
---|
1102 | sprintf(t_msg[0], "au=%d", client[cs_idx].au+1);
|
---|
1103 | }
|
---|
1104 | cs_log("%s %s-client %s%s (%s, %s)",
|
---|
1105 | client[cs_idx].crypted ? t_crypt : t_plain,
|
---|
1106 | e_txt ? e_txt : ph[client[cs_idx].ctyp].desc,
|
---|
1107 | client[cs_idx].ip ? cs_inet_ntoa(client[cs_idx].ip) : "",
|
---|
1108 | client[cs_idx].ip ? t_grant : t_grant+1,
|
---|
1109 | username(cs_idx), t_msg[rc]);
|
---|
1110 | break;
|
---|
1111 | }
|
---|
1112 | return(rc);
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | void cs_disconnect_client(void)
|
---|
1116 | {
|
---|
1117 | char buf[32]={0};
|
---|
1118 | if (client[cs_idx].ip)
|
---|
1119 | sprintf(buf, " from %s", cs_inet_ntoa(client[cs_idx].ip));
|
---|
1120 | cs_log("%s disconnected%s", username(cs_idx), buf);
|
---|
1121 | cs_exit(0);
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | int check_ecmcache(ECM_REQUEST *er, ulong grp)
|
---|
1125 | {
|
---|
1126 | int i;
|
---|
1127 | // cs_ddump(ecmd5, CS_ECMSTORESIZE, "ECM search");
|
---|
1128 | //cs_log("cache CHECK: grp=%lX", grp);
|
---|
1129 | for(i=0; i<CS_ECMCACHESIZE; i++)
|
---|
1130 | if ((grp & ecmcache[i].grp) &&
|
---|
1131 | (!memcmp(ecmcache[i].ecmd5, er->ecmd5, CS_ECMSTORESIZE)))
|
---|
1132 | {
|
---|
1133 | //cs_log("cache found: grp=%lX cgrp=%lX", grp, ecmcache[i].grp);
|
---|
1134 | memcpy(er->cw, ecmcache[i].cw, 16);
|
---|
1135 | return(1);
|
---|
1136 | }
|
---|
1137 | return(0);
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | static void store_ecm(ECM_REQUEST *er)
|
---|
1141 | {
|
---|
1142 | //cs_log("store ecm from reader %d", er->reader[0]);
|
---|
1143 | memcpy(ecmcache[*ecmidx].ecmd5, er->ecmd5, CS_ECMSTORESIZE);
|
---|
1144 | memcpy(ecmcache[*ecmidx].cw, er->cw, 16);
|
---|
1145 | ecmcache[*ecmidx].caid=er->caid;
|
---|
1146 | ecmcache[*ecmidx].prid=er->prid;
|
---|
1147 | ecmcache[*ecmidx].grp =reader[er->reader[0]].grp;
|
---|
1148 | // cs_ddump(ecmcache[*ecmidx].ecmd5, CS_ECMSTORESIZE, "ECM stored (idx=%d)", *ecmidx);
|
---|
1149 | *ecmidx=(*ecmidx+1) % CS_ECMCACHESIZE;
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | void store_logentry(char *txt)
|
---|
1153 | {
|
---|
1154 | #ifdef CS_LOGHISTORY
|
---|
1155 | char *ptr;
|
---|
1156 | ptr=(char *)(loghist+(*loghistidx*CS_LOGHISTSIZE));
|
---|
1157 | ptr[0]='\1'; // make username unusable
|
---|
1158 | ptr[1]='\0';
|
---|
1159 | if ((client[cs_idx].typ=='c') || (client[cs_idx].typ=='m'))
|
---|
1160 | strncpy(ptr, client[cs_idx].usr, 31);
|
---|
1161 | strncpy(ptr+32, txt, CS_LOGHISTSIZE-33);
|
---|
1162 | *loghistidx=(*loghistidx+1) % CS_MAXLOGHIST;
|
---|
1163 | #endif
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | /*
|
---|
1167 | * write_to_pipe():
|
---|
1168 | * write all kind of data to pipe specified by fd
|
---|
1169 | */
|
---|
1170 | int write_to_pipe(int fd, int id, uchar *data, int n)
|
---|
1171 | {
|
---|
1172 | uchar buf[1024+3+sizeof(int)];
|
---|
1173 |
|
---|
1174 | //printf("WRITE_START pid=%d", getpid()); fflush(stdout);
|
---|
1175 | if ((id<0) || (id>PIP_ID_MAX))
|
---|
1176 | return(PIP_ID_ERR);
|
---|
1177 | memcpy(buf, PIP_ID_TXT[id], 3);
|
---|
1178 | memcpy(buf+3, &n, sizeof(int));
|
---|
1179 | memcpy(buf+3+sizeof(int), data, n);
|
---|
1180 | n+=3+sizeof(int);
|
---|
1181 | //n=write(fd, buf, n);
|
---|
1182 | //printf("WRITE_END pid=%d", getpid()); fflush(stdout);
|
---|
1183 | //return(n);
|
---|
1184 | if( !fd )
|
---|
1185 | cs_log("write_to_pipe: fd==0");
|
---|
1186 | return(write(fd, buf, n));
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | /*
|
---|
1190 | * read_from_pipe():
|
---|
1191 | * read all kind of data from pipe specified by fd
|
---|
1192 | * special-flag redir: if set AND data is ECM: this will redirected to appr. client
|
---|
1193 | */
|
---|
1194 | int read_from_pipe(int fd, uchar **data, int redir)
|
---|
1195 | {
|
---|
1196 | int rc;
|
---|
1197 | static int hdr=0;
|
---|
1198 | static uchar buf[1024+1+3+sizeof(int)];
|
---|
1199 |
|
---|
1200 | *data=(uchar *)0;
|
---|
1201 | rc=PIP_ID_NUL;
|
---|
1202 |
|
---|
1203 | if (!hdr)
|
---|
1204 | {
|
---|
1205 | if (bytes_available(fd))
|
---|
1206 | {
|
---|
1207 | if (read(fd, buf, 3+sizeof(int))==3+sizeof(int))
|
---|
1208 | memcpy(&hdr, buf+3, sizeof(int));
|
---|
1209 | else
|
---|
1210 | cs_log("WARNING: pipe header to small !");
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 | if (hdr)
|
---|
1214 | {
|
---|
1215 | int l;
|
---|
1216 | for (l=0; (rc<0) && (PIP_ID_TXT[l]); l++)
|
---|
1217 | if (!memcmp(buf, PIP_ID_TXT[l], 3))
|
---|
1218 | rc=l;
|
---|
1219 |
|
---|
1220 | if (rc<0)
|
---|
1221 | {
|
---|
1222 | fprintf(stderr, "WARNING: pipe garbage");
|
---|
1223 | fflush(stderr);
|
---|
1224 | cs_log("WARNING: pipe garbage");
|
---|
1225 | rc=PIP_ID_ERR;
|
---|
1226 | }
|
---|
1227 | else
|
---|
1228 | {
|
---|
1229 | l=hdr;
|
---|
1230 | if ((l+3-1+sizeof(int))>sizeof(buf))
|
---|
1231 | {
|
---|
1232 | cs_log("WARNING: packet size (%d) to large", l);
|
---|
1233 | l=sizeof(buf)+3-1+sizeof(int);
|
---|
1234 | }
|
---|
1235 | if (!bytes_available(fd))
|
---|
1236 | return(PIP_ID_NUL);
|
---|
1237 | hdr=0;
|
---|
1238 | if (read(fd, buf+3+sizeof(int), l)==l)
|
---|
1239 | *data=buf+3+sizeof(int);
|
---|
1240 | else
|
---|
1241 | {
|
---|
1242 | cs_log("WARNING: pipe data to small !");
|
---|
1243 | return(PIP_ID_ERR);
|
---|
1244 | }
|
---|
1245 | buf[l+3+sizeof(int)]=0;
|
---|
1246 | if ((redir) && (rc==PIP_ID_ECM))
|
---|
1247 | {
|
---|
1248 | //int idx;
|
---|
1249 | ECM_REQUEST *er;
|
---|
1250 | er=(ECM_REQUEST *)(buf+3+sizeof(int));
|
---|
1251 | if( er->cidx && client[er->cidx].fd_m2c )
|
---|
1252 | write(client[er->cidx].fd_m2c, buf, l+3+sizeof(int));
|
---|
1253 | rc=PIP_ID_DIR;
|
---|
1254 | }
|
---|
1255 | }
|
---|
1256 | }
|
---|
1257 | return(rc);
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | /*
|
---|
1261 | * write_ecm_request():
|
---|
1262 | */
|
---|
1263 | int write_ecm_request(int fd, ECM_REQUEST *er)
|
---|
1264 | {
|
---|
1265 | return(write_to_pipe(fd, PIP_ID_ECM, (uchar *) er, sizeof(ECM_REQUEST)));
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | int write_ecm_DCW(int fd, ECM_REQUEST *er)
|
---|
1269 | {
|
---|
1270 | return(write_to_pipe(fd, PIP_ID_DCW, (uchar *) er, sizeof(ECM_REQUEST)));
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | int write_ecm_answer(int fd, ECM_REQUEST *er)
|
---|
1274 | {
|
---|
1275 | int i, f;
|
---|
1276 | uchar c;
|
---|
1277 | for (i=f=0; i<16; i+=4)
|
---|
1278 | {
|
---|
1279 | c=((er->cw[i]+er->cw[i+1]+er->cw[i+2]) & 0xff);
|
---|
1280 | if (er->cw[i+3]!=c)
|
---|
1281 | {
|
---|
1282 | f=1;
|
---|
1283 | er->cw[i+3]=c;
|
---|
1284 | }
|
---|
1285 | }
|
---|
1286 | if (f)
|
---|
1287 | cs_debug("notice: changed dcw checksum bytes");
|
---|
1288 |
|
---|
1289 | er->reader[0]=ridx;
|
---|
1290 | //cs_log("answer from reader %d (rc=%d)", er->reader[0], er->rc);
|
---|
1291 | er->caid=er->ocaid;
|
---|
1292 | if (er->rc==1||(er->gbxRidx&&er->rc==0)){
|
---|
1293 | store_ecm(er);
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | return(write_ecm_request(fd, er));
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | static int cs_read_timer(int fd, uchar *buf, int l, int msec)
|
---|
1300 | {
|
---|
1301 | struct timeval tv;
|
---|
1302 | fd_set fds;
|
---|
1303 | int rc;
|
---|
1304 |
|
---|
1305 | if (!fd) return(-1);
|
---|
1306 | tv.tv_sec = msec / 1000;
|
---|
1307 | tv.tv_usec = (msec % 1000) * 1000;
|
---|
1308 | FD_ZERO(&fds);
|
---|
1309 | FD_SET(pfd, &fds);
|
---|
1310 |
|
---|
1311 | select(fd+1, &fds, 0, 0, &tv);
|
---|
1312 |
|
---|
1313 | rc=0;
|
---|
1314 | if (FD_ISSET(pfd, &fds))
|
---|
1315 | if (!(rc=read(fd, buf, l)))
|
---|
1316 | rc=-1;
|
---|
1317 |
|
---|
1318 | return(rc);
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | ECM_REQUEST *get_ecmtask()
|
---|
1322 | {
|
---|
1323 | int i, n;
|
---|
1324 | ECM_REQUEST *er=0;
|
---|
1325 |
|
---|
1326 | if (!ecmtask)
|
---|
1327 | {
|
---|
1328 | n=(ph[client[cs_idx].ctyp].multi)?CS_MAXPENDING:1;
|
---|
1329 | if( (ecmtask=(ECM_REQUEST *)malloc(n*sizeof(ECM_REQUEST))) )
|
---|
1330 | memset(ecmtask, 0, n*sizeof(ECM_REQUEST));
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 | n=(-1);
|
---|
1334 | if (!ecmtask)
|
---|
1335 | {
|
---|
1336 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
1337 | n=(-2);
|
---|
1338 | }
|
---|
1339 | else
|
---|
1340 | if (ph[client[cs_idx].ctyp].multi)
|
---|
1341 | {
|
---|
1342 | for (i=0; (n<0) && (i<CS_MAXPENDING); i++)
|
---|
1343 | if (ecmtask[i].rc<100)
|
---|
1344 | er=&ecmtask[n=i];
|
---|
1345 | }
|
---|
1346 | else
|
---|
1347 | er=&ecmtask[n=0];
|
---|
1348 |
|
---|
1349 | if (n<0)
|
---|
1350 | cs_log("WARNING: ecm pending table overflow !");
|
---|
1351 | else
|
---|
1352 | {
|
---|
1353 | memset(er, 0, sizeof(ECM_REQUEST));
|
---|
1354 | er->rc=100;
|
---|
1355 | er->cpti=n;
|
---|
1356 | er->cidx=cs_idx;
|
---|
1357 | cs_ftime(&er->tps);
|
---|
1358 | }
|
---|
1359 | return(er);
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | int send_dcw(ECM_REQUEST *er)
|
---|
1363 | {
|
---|
1364 | static char *stxt[]={"found", "cache1", "cache2", "emu",
|
---|
1365 | "not found", "timeout", "sleeping",
|
---|
1366 | "fake", "invalid", "corrupt"};
|
---|
1367 | static char *stxtEx[]={"", "group", "caid", "ident", "class", "chid", "queue"};
|
---|
1368 | static char *stxtWh[]={"", "user ", "reader ", "server ", "lserver "};
|
---|
1369 | char sby[32]="";
|
---|
1370 | char erEx[32]="";
|
---|
1371 | char uname[38]="";
|
---|
1372 | struct timeb tpe;
|
---|
1373 | ushort lc, *lp;
|
---|
1374 | for (lp=(ushort *)er->ecm+(er->l>>2), lc=0; lp>=(ushort *)er->ecm; lp--)
|
---|
1375 | lc^=*lp;
|
---|
1376 | cs_ftime(&tpe);
|
---|
1377 | if(er->gbxFrom)
|
---|
1378 | snprintf(uname,sizeof(uname)-1, "%s(%04X)", username(cs_idx), er->gbxFrom);
|
---|
1379 | else
|
---|
1380 | snprintf(uname,sizeof(uname)-1, "%s", username(cs_idx));
|
---|
1381 | if (er->rc==0)
|
---|
1382 | {
|
---|
1383 | if(reader[er->reader[0]].typ==R_GBOX)
|
---|
1384 | snprintf(sby, sizeof(sby)-1, " by %s(%04X)", reader[er->reader[0]].label,er->gbxCWFrom);
|
---|
1385 | else
|
---|
1386 | snprintf(sby, sizeof(sby)-1, " by %s", reader[er->reader[0]].label);
|
---|
1387 | }
|
---|
1388 | if (er->rc<4) er->rcEx=0;
|
---|
1389 | if (er->rcEx)
|
---|
1390 | snprintf(erEx, sizeof(erEx)-1, "rejected %s%s", stxtWh[er->rcEx>>4],
|
---|
1391 | stxtEx[er->rcEx&0xf]);
|
---|
1392 | cs_log("%s (%04X&%06X/%04X/%02X:%04X): %s (%d ms)%s",
|
---|
1393 | uname, er->caid, er->prid, er->srvid, er->l, lc,
|
---|
1394 | er->rcEx?erEx:stxt[er->rc],
|
---|
1395 | 1000*(tpe.time-er->tps.time)+tpe.millitm-er->tps.millitm, sby);
|
---|
1396 | er->caid=er->ocaid;
|
---|
1397 | switch(er->rc)
|
---|
1398 | {
|
---|
1399 | case 2:
|
---|
1400 | case 1: client[cs_idx].cwcache++;
|
---|
1401 | case 3:
|
---|
1402 | case 0: client[cs_idx].cwfound++; break;
|
---|
1403 | default: client[cs_idx].cwnot++;
|
---|
1404 | if (er->rc>5)
|
---|
1405 | client[cs_idx].cwcache++;
|
---|
1406 | }
|
---|
1407 | #ifdef CS_ANTICASC
|
---|
1408 | ac_chk(er, 1);
|
---|
1409 | #endif
|
---|
1410 |
|
---|
1411 | if( cfg->show_ecm_dw && !client[cs_idx].dbglvl )
|
---|
1412 | cs_dump(er->cw, 16, 0);
|
---|
1413 | if (er->rc==7) er->rc=0;
|
---|
1414 | ph[client[cs_idx].ctyp].send_dcw(er);
|
---|
1415 | return 0;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | static void chk_dcw(int fd)
|
---|
1419 | {
|
---|
1420 | ECM_REQUEST *er, *ert;
|
---|
1421 | if (read_from_pipe(fd, (uchar **)&er, 0)!=PIP_ID_ECM)
|
---|
1422 | return;
|
---|
1423 | //cs_log("dcw check from reader %d for idx %d (rc=%d)", er->reader[0], er->cpti, er->rc);
|
---|
1424 | ert=&ecmtask[er->cpti];
|
---|
1425 | if (ert->rc<100)
|
---|
1426 | return; // already done
|
---|
1427 | if( (er->caid!=ert->caid) || memcmp(er->ecm , ert->ecm , sizeof(er->ecm)) )
|
---|
1428 | return; // obsolete
|
---|
1429 | ert->rcEx=er->rcEx;
|
---|
1430 | if (er->rc>0) // found
|
---|
1431 | {
|
---|
1432 | ert->rc=(er->rc==2)?2:0;
|
---|
1433 | ert->rcEx=0;
|
---|
1434 | ert->reader[0]=er->reader[0];
|
---|
1435 | memcpy(ert->cw , er->cw , sizeof(er->cw));
|
---|
1436 | ert->gbxCWFrom=er->gbxCWFrom;
|
---|
1437 | }
|
---|
1438 | else // not found (from ONE of the readers !)
|
---|
1439 | {
|
---|
1440 | int i;
|
---|
1441 | ert->reader[er->reader[0]]=0;
|
---|
1442 | for (i=0; (ert) && (i<CS_MAXREADER); i++)
|
---|
1443 | if (ert->reader[i]) // we have still another chance
|
---|
1444 | ert=(ECM_REQUEST *)0;
|
---|
1445 | if (ert) ert->rc=4;
|
---|
1446 | }
|
---|
1447 | if (ert) send_dcw(ert);
|
---|
1448 | return;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | ulong chk_provid(uchar *ecm, ushort caid)
|
---|
1452 | {
|
---|
1453 | int i;
|
---|
1454 | ulong provid=0;
|
---|
1455 | switch(caid)
|
---|
1456 | {
|
---|
1457 | case 0x100: // seca
|
---|
1458 | provid=b2i(2, ecm+3);
|
---|
1459 | break;
|
---|
1460 | case 0x500: // viaccess
|
---|
1461 | i=(ecm[4]==0xD2) ? 3 : 0; // tpsflag -> offset+3
|
---|
1462 | if ((ecm[5+i]==3) && ((ecm[4+i]==0x90) || (ecm[4+i]==0x40)))
|
---|
1463 | provid=(b2i(3, ecm+6+i) & 0xFFFFF0);
|
---|
1464 | default:
|
---|
1465 | // cryptoworks ?
|
---|
1466 | if( caid&0x0d00 && ecm[8]==0x83 && ecm[9]==1 )
|
---|
1467 | provid=(ulong)ecm[10];
|
---|
1468 | }
|
---|
1469 | return(provid);
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | /*
|
---|
1473 | void guess_irdeto(ECM_REQUEST *er)
|
---|
1474 | {
|
---|
1475 | uchar b3;
|
---|
1476 | int b47;
|
---|
1477 | //ushort chid;
|
---|
1478 | struct s_irdeto_quess *ptr;
|
---|
1479 |
|
---|
1480 | b3 = er->ecm[3];
|
---|
1481 | ptr = cfg->itab[b3];
|
---|
1482 | if( !ptr ) {
|
---|
1483 | cs_debug("unknown irdeto byte 3: %02X", b3);
|
---|
1484 | return;
|
---|
1485 | }
|
---|
1486 | b47 = b2i(4, er->ecm+4);
|
---|
1487 | //chid = b2i(2, er->ecm+6);
|
---|
1488 | //cs_debug("ecm: b47=%08X, ptr->b47=%08X, ptr->caid=%04X", b47, ptr->b47, ptr->caid);
|
---|
1489 | while( ptr )
|
---|
1490 | {
|
---|
1491 | if( b47==ptr->b47 )
|
---|
1492 | {
|
---|
1493 | if( er->srvid && (er->srvid!=ptr->sid) )
|
---|
1494 | {
|
---|
1495 | cs_debug("sid mismatched (ecm: %04X, guess: %04X), wrong mpcs.ird file?",
|
---|
1496 | er->srvid, ptr->sid);
|
---|
1497 | return;
|
---|
1498 | }
|
---|
1499 | er->caid=ptr->caid;
|
---|
1500 | er->srvid=ptr->sid;
|
---|
1501 | er->chid=(ushort)ptr->b47;
|
---|
1502 | // cs_debug("quess_irdeto() found caid=%04X, sid=%04X, chid=%04X",
|
---|
1503 | // er->caid, er->srvid, er->chid);
|
---|
1504 | return;
|
---|
1505 | }
|
---|
1506 | ptr=ptr->next;
|
---|
1507 | }
|
---|
1508 | }
|
---|
1509 | */
|
---|
1510 |
|
---|
1511 | void guess_cardsystem(ECM_REQUEST *er)
|
---|
1512 | {
|
---|
1513 | ushort last_hope=0;
|
---|
1514 |
|
---|
1515 | // viaccess - check by provid-search
|
---|
1516 | if( (er->prid=chk_provid(er->ecm, 0x500)) )
|
---|
1517 | er->caid=0x500;
|
---|
1518 |
|
---|
1519 | // nagra
|
---|
1520 | // is ecm[1] always 0x30 ?
|
---|
1521 | // is ecm[3] always 0x07 ?
|
---|
1522 | if ((er->ecm[6]==1) && (er->ecm[4]==er->ecm[2]-2))
|
---|
1523 | er->caid=0x1801;
|
---|
1524 |
|
---|
1525 | // seca2 - very poor
|
---|
1526 | if ((er->ecm[8]==0x10) && ((er->ecm[9]&0xF1)==1))
|
---|
1527 | last_hope=0x100;
|
---|
1528 |
|
---|
1529 | // is cryptoworks, but which caid ?
|
---|
1530 | if ((er->ecm[3]==0x81) && (er->ecm[4]==0xFF) &&
|
---|
1531 | (!er->ecm[5]) && (!er->ecm[6]) && (er->ecm[7]==er->ecm[2]-5))
|
---|
1532 | last_hope=0xd00;
|
---|
1533 |
|
---|
1534 | /*
|
---|
1535 | if (!er->caid && er->ecm[2]==0x31 && er->ecm[0x0b]==0x28)
|
---|
1536 | guess_irdeto(er);
|
---|
1537 | */
|
---|
1538 |
|
---|
1539 | if (!er->caid) // guess by len ..
|
---|
1540 | er->caid=len4caid[er->ecm[2]+3];
|
---|
1541 |
|
---|
1542 | if (!er->caid)
|
---|
1543 | er->caid=last_hope;
|
---|
1544 | }
|
---|
1545 |
|
---|
1546 | void request_cw(ECM_REQUEST *er, int flag)
|
---|
1547 | {
|
---|
1548 | int i;
|
---|
1549 | er->level=flag;
|
---|
1550 | flag=(flag)?3:1; // flag specifies with/without fallback-readers
|
---|
1551 | for (i=0; i<CS_MAXREADER; i++)
|
---|
1552 | if (er->reader[i]&flag)
|
---|
1553 | write_ecm_request(reader[i].fd, er);
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 | void get_cw(ECM_REQUEST *er)
|
---|
1557 | {
|
---|
1558 | int i, j, m, rejected;
|
---|
1559 | //uchar orig_caid[sizeof(er->caid)];
|
---|
1560 | time_t now;
|
---|
1561 | //test the guessing ...
|
---|
1562 | //cs_log("caid should be %04X, provid %06X", er->caid, er->prid);
|
---|
1563 | //er->caid=0;
|
---|
1564 |
|
---|
1565 | client[cs_idx].lastecm=time((time_t)0);
|
---|
1566 |
|
---|
1567 | if (!er->caid)
|
---|
1568 | guess_cardsystem(er);
|
---|
1569 |
|
---|
1570 | if( (er->caid & 0xFF00)==0x600 && !er->chid )
|
---|
1571 | er->chid = (er->ecm[6]<<8)|er->ecm[7];
|
---|
1572 |
|
---|
1573 | if (!er->prid)
|
---|
1574 | er->prid=chk_provid(er->ecm, er->caid);
|
---|
1575 |
|
---|
1576 | // quickfix for 0100:000065
|
---|
1577 | if (er->caid == 0x100 && er->prid == 0x65 && er->srvid == 0)
|
---|
1578 | er->srvid = 0x0642;
|
---|
1579 |
|
---|
1580 | if( (!er->prid) && client[cs_idx].ncd_server )
|
---|
1581 | {
|
---|
1582 | int pi = client[cs_idx].port_idx;
|
---|
1583 | if( pi>=0 && cfg->ncd_ptab.nports && cfg->ncd_ptab.nports >= pi )
|
---|
1584 | er->prid = cfg->ncd_ptab.ports[pi].ftab.filts[0].prids[0];
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 | //cs_log("caid IS NOW .. %04X, provid %06X", er->caid, er->prid);
|
---|
1588 |
|
---|
1589 | rejected=0;
|
---|
1590 | if (er->rc>99) // rc<100 -> ecm error
|
---|
1591 | {
|
---|
1592 | now=time((time_t *) 0);
|
---|
1593 | m=er->caid;
|
---|
1594 | er->ocaid=er->caid;
|
---|
1595 |
|
---|
1596 | i=er->srvid;
|
---|
1597 | if ((i!=client[cs_idx].last_srvid) || (!client[cs_idx].lastswitch))
|
---|
1598 | client[cs_idx].lastswitch=now;
|
---|
1599 | if ((client[cs_idx].tosleep) &&
|
---|
1600 | (now-client[cs_idx].lastswitch>client[cs_idx].tosleep))
|
---|
1601 | er->rc=6; // sleeping
|
---|
1602 | client[cs_idx].last_srvid=i;
|
---|
1603 | client[cs_idx].last_caid=m;
|
---|
1604 |
|
---|
1605 | for (j=0; (j<6) && (er->rc>99); j++)
|
---|
1606 | switch(j)
|
---|
1607 | {
|
---|
1608 | case 0: if (client[cs_idx].dup)
|
---|
1609 | er->rc=7; // fake
|
---|
1610 | break;
|
---|
1611 | case 1: if (!chk_bcaid(er, &client[cs_idx].ctab))
|
---|
1612 | {
|
---|
1613 | // cs_log("chk_bcaid failed");
|
---|
1614 | er->rc=8; // invalid
|
---|
1615 | er->rcEx=E2_CAID;
|
---|
1616 | }
|
---|
1617 | break;
|
---|
1618 | case 2: if (!chk_srvid(er, cs_idx))
|
---|
1619 | er->rc=8;
|
---|
1620 | break;
|
---|
1621 | case 3: if (!chk_ufilters(er))
|
---|
1622 | er->rc=8;
|
---|
1623 | break;
|
---|
1624 | case 4: if (!chk_sfilter(er, ph[client[cs_idx].ctyp].ptab))
|
---|
1625 | er->rc=8;
|
---|
1626 | break;
|
---|
1627 | case 5: if( (i=er->l-(er->ecm[2]+3)) )
|
---|
1628 | {
|
---|
1629 | if (i>0)
|
---|
1630 | {
|
---|
1631 | cs_debug("warning: ecm size adjusted from 0x%X to 0x%X",
|
---|
1632 | er->l, er->ecm[2]+3);
|
---|
1633 | er->l=(er->ecm[2]+3);
|
---|
1634 | }
|
---|
1635 | else
|
---|
1636 | er->rc=9; // corrupt
|
---|
1637 | }
|
---|
1638 | break;
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | if (premhack) // quickhack for 1801:000501
|
---|
1642 | // moved behind the check routines, because newcamd-ECM will fail if ecm is converted before
|
---|
1643 | if (er->caid==0x1801 && er->ecm[3]==7 && er->ecm[5]==5 && er->ecm[6]==1)
|
---|
1644 | {
|
---|
1645 | int l;
|
---|
1646 | char hack[13]={0x70, 0x51, 0xc9, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x48, 0x12, 0x07};
|
---|
1647 | er->caid=0x1702;
|
---|
1648 | er->prid=0;
|
---|
1649 | er->l=(er->ecm[2]+3);
|
---|
1650 | memmove(er->ecm+14, er->ecm+4, er->l-1);
|
---|
1651 | memcpy(er->ecm+1, hack, 13);
|
---|
1652 | er->l+=10;
|
---|
1653 | er->ecm[2]=er->l-3;
|
---|
1654 | cs_debug("ecm converted 1801:000501 -> 1702:000000");
|
---|
1655 | }
|
---|
1656 |
|
---|
1657 | memcpy(er->ecmd5, MD5(er->ecm, er->l, NULL), CS_ECMSTORESIZE);
|
---|
1658 |
|
---|
1659 | if (check_ecmcache(er, client[cs_idx].grp))
|
---|
1660 | er->rc=1; // cache1
|
---|
1661 |
|
---|
1662 | #ifdef CS_ANTICASC
|
---|
1663 | ac_chk(er, 0);
|
---|
1664 | #endif
|
---|
1665 | if( er->rc<100 && er->rc!=1 )
|
---|
1666 | rejected=1;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | if( !rejected && er->rc!=1 )
|
---|
1670 | {
|
---|
1671 | for (i=m=0; i<CS_MAXREADER; i++)
|
---|
1672 | if (matching_reader(er, &reader[i])&&(i!=ridx))
|
---|
1673 | m|=er->reader[i]=(reader[i].fallback)?2:1;
|
---|
1674 |
|
---|
1675 | switch(m)
|
---|
1676 | {
|
---|
1677 | case 0: er->rc=4; // no reader -> not found
|
---|
1678 | if (!er->rcEx) er->rcEx=E2_GROUP;
|
---|
1679 | break;
|
---|
1680 | case 2: for (i=0; i<CS_MAXREADER; i++) // fallbacks only, switch them.
|
---|
1681 | er->reader[i]>>=1;
|
---|
1682 | }
|
---|
1683 | }
|
---|
1684 | if (er->rc<100)
|
---|
1685 | {
|
---|
1686 | if (cfg->delay) usleep(cfg->delay);
|
---|
1687 | send_dcw(er);
|
---|
1688 | return;
|
---|
1689 | }
|
---|
1690 |
|
---|
1691 | er->rcEx=0;
|
---|
1692 | request_cw(er, 0);
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | void log_emm_request(int auidx)
|
---|
1696 | {
|
---|
1697 | // cs_log("%s send emm-request (reader=%s, caid=%04X)",
|
---|
1698 | // cs_inet_ntoa(client[cs_idx].ip), reader[auidx].label, reader[auidx].caid[0]);
|
---|
1699 | cs_log("%s emm-request sent (reader=%s, caid=%04X)",
|
---|
1700 | username(cs_idx), reader[auidx].label, reader[auidx].caid[0]);
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | void do_emm(EMM_PACKET *ep)
|
---|
1704 | {
|
---|
1705 | int au;//, ephs;
|
---|
1706 | au=client[cs_idx].au;
|
---|
1707 |
|
---|
1708 | if ((au<0) || (au>=CS_MAXREADER))
|
---|
1709 | return;
|
---|
1710 | client[cs_idx].lastemm=time((time_t)0);
|
---|
1711 | cs_ddump(reader[au].hexserial, 8, "reader serial:");
|
---|
1712 | cs_ddump(ep->hexserial, 8, "emm SA:");
|
---|
1713 | // if ((!reader[au].fd) || (reader[au].b_nano[ep->emm[3]])) // blocknano is obsolete
|
---|
1714 | if ((!reader[au].fd) || // reader has no fd
|
---|
1715 | (reader[au].caid[0]!=b2i(2,ep->caid)) || // wrong caid
|
---|
1716 | (memcmp(reader[au].hexserial, ep->hexserial, 8))) // wrong serial
|
---|
1717 | return;
|
---|
1718 |
|
---|
1719 | ep->cidx=cs_idx;
|
---|
1720 | write_to_pipe(reader[au].fd, PIP_ID_EMM, (uchar *) ep, sizeof(EMM_PACKET));
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | static int comp_timeb(struct timeb *tpa, struct timeb *tpb)
|
---|
1724 | {
|
---|
1725 | if (tpa->time>tpb->time) return(1);
|
---|
1726 | if (tpa->time<tpb->time) return(-1);
|
---|
1727 | if (tpa->millitm>tpb->millitm) return(1);
|
---|
1728 | if (tpa->millitm<tpb->millitm) return(-1);
|
---|
1729 | return(0);
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | static void build_delay(struct timeb *tpe, struct timeb *tpc)
|
---|
1733 | {
|
---|
1734 | if (comp_timeb(tpe, tpc)>0)
|
---|
1735 | {
|
---|
1736 | tpe->time=tpc->time;
|
---|
1737 | tpe->millitm=tpc->millitm;
|
---|
1738 | }
|
---|
1739 | }
|
---|
1740 |
|
---|
1741 | struct timeval *chk_pending(struct timeb tp_ctimeout)
|
---|
1742 | {
|
---|
1743 | int i;
|
---|
1744 | ulong td;
|
---|
1745 | struct timeb tpn, tpe, tpc; // <n>ow, <e>nd, <c>heck
|
---|
1746 | static struct timeval tv;
|
---|
1747 |
|
---|
1748 | ECM_REQUEST *er;
|
---|
1749 | cs_ftime(&tpn);
|
---|
1750 | tpe=tp_ctimeout; // latest delay -> disconnect
|
---|
1751 |
|
---|
1752 | if (ecmtask)
|
---|
1753 | i=(ph[client[cs_idx].ctyp].multi)?CS_MAXPENDING:1;
|
---|
1754 | else
|
---|
1755 | i=0;
|
---|
1756 | //cs_log("num pend=%d", i);
|
---|
1757 | for (--i; i>=0; i--)
|
---|
1758 | if (ecmtask[i].rc>=100) // check all pending ecm-requests
|
---|
1759 | {
|
---|
1760 | int act, j;
|
---|
1761 | er=&ecmtask[i];
|
---|
1762 | tpc=er->tps;
|
---|
1763 | tpc.time+=(er->stage) ? cfg->ctimeout : cfg->ftimeout;
|
---|
1764 | if (!er->stage)
|
---|
1765 | {
|
---|
1766 | for (j=0, act=1; (act) && (j<CS_MAXREADER); j++)
|
---|
1767 | if (er->reader[j]&1)
|
---|
1768 | act=0;
|
---|
1769 | //cs_log("stage 0, act=%d r0=%d, r1=%d, r2=%d, r3=%d, r4=%d r5=%d", act,
|
---|
1770 | // er->reader[0], er->reader[1], er->reader[2],
|
---|
1771 | // er->reader[3], er->reader[4], er->reader[5]);
|
---|
1772 | if (act)
|
---|
1773 | {
|
---|
1774 | er->stage++;
|
---|
1775 | request_cw(er, er->stage);
|
---|
1776 | tpc.time+=cfg->ctimeout-cfg->ftimeout;
|
---|
1777 | }
|
---|
1778 | }
|
---|
1779 | if (comp_timeb(&tpn, &tpc)>0) // action needed
|
---|
1780 | {
|
---|
1781 | //cs_log("Action now %d.%03d", tpn.time, tpn.millitm);
|
---|
1782 | //cs_log(" %d.%03d", tpc.time, tpc.millitm);
|
---|
1783 | if (er->stage)
|
---|
1784 | {
|
---|
1785 | er->rc=5; // timeout
|
---|
1786 | send_dcw(er);
|
---|
1787 | continue;
|
---|
1788 | }
|
---|
1789 | else
|
---|
1790 | {
|
---|
1791 | er->stage++;
|
---|
1792 | request_cw(er, er->stage);
|
---|
1793 | tpc.time+=cfg->ctimeout-cfg->ftimeout;
|
---|
1794 | }
|
---|
1795 | }
|
---|
1796 | build_delay(&tpe, &tpc);
|
---|
1797 | }
|
---|
1798 | td=(tpe.time-tpn.time)*1000+(tpe.millitm-tpn.millitm)+5;
|
---|
1799 | tv.tv_sec = td/1000;
|
---|
1800 | tv.tv_usec = (td%1000)*1000;
|
---|
1801 | //cs_log("delay %d.%06d", tv.tv_sec, tv.tv_usec);
|
---|
1802 | return(&tv);
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 | int process_input(uchar *buf, int l, int timeout)
|
---|
1806 | {
|
---|
1807 | int rc;
|
---|
1808 | fd_set fds;
|
---|
1809 | struct timeb tp;
|
---|
1810 |
|
---|
1811 | if (master_pid!=getppid()) cs_exit(0);
|
---|
1812 | if (!pfd) return(-1);
|
---|
1813 | cs_ftime(&tp);
|
---|
1814 | tp.time+=timeout;
|
---|
1815 | if (ph[client[cs_idx].ctyp].watchdog)
|
---|
1816 | alarm(cfg->cmaxidle+2);
|
---|
1817 | while (1)
|
---|
1818 | {
|
---|
1819 | FD_ZERO(&fds);
|
---|
1820 | FD_SET(pfd, &fds);
|
---|
1821 | FD_SET(fd_m2c, &fds);
|
---|
1822 |
|
---|
1823 | rc=select(((pfd>fd_m2c)?pfd:fd_m2c)+1, &fds, 0, 0, chk_pending(tp));
|
---|
1824 | if (master_pid!=getppid()) cs_exit(0);
|
---|
1825 | if (rc<0)
|
---|
1826 | {
|
---|
1827 | if (errno==EINTR) continue;
|
---|
1828 | else return(0);
|
---|
1829 | }
|
---|
1830 |
|
---|
1831 | if (FD_ISSET(fd_m2c, &fds)) // read from pipe
|
---|
1832 | chk_dcw(fd_m2c);
|
---|
1833 |
|
---|
1834 | if (FD_ISSET(pfd, &fds)) // read from client
|
---|
1835 | {
|
---|
1836 | rc=ph[client[cs_idx].ctyp].recv(buf, l);
|
---|
1837 | break;
|
---|
1838 | }
|
---|
1839 | if (tp.time<=time((time_t *)0)) // client maxidle reached
|
---|
1840 | {
|
---|
1841 | rc=(-9);
|
---|
1842 | break;
|
---|
1843 | }
|
---|
1844 | }
|
---|
1845 | if (ph[client[cs_idx].ctyp].watchdog)
|
---|
1846 | alarm(cfg->cmaxidle+2);
|
---|
1847 | return(rc);
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | static void process_master_pipe()
|
---|
1851 | {
|
---|
1852 | int n;
|
---|
1853 | uchar *ptr;
|
---|
1854 |
|
---|
1855 | switch(n=read_from_pipe(mfdr, &ptr, 1))
|
---|
1856 | {
|
---|
1857 | case PIP_ID_LOG:
|
---|
1858 | cs_write_log(ptr);
|
---|
1859 | break;
|
---|
1860 | case PIP_ID_HUP:
|
---|
1861 | cs_accounts_chk();
|
---|
1862 | break;
|
---|
1863 | }
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | void cs_log_config()
|
---|
1867 | {
|
---|
1868 | uchar buf[2048];
|
---|
1869 |
|
---|
1870 | if (cfg->nice!=99)
|
---|
1871 | sprintf(buf, ", nice=%d", cfg->nice);
|
---|
1872 | else
|
---|
1873 | buf[0]='\0';
|
---|
1874 | cs_log("version=%s, system=%s%s", CS_VERSION_X, cs_platform(buf+64), buf);
|
---|
1875 | cs_log("max. clients=%d, client max. idle=%d sec",
|
---|
1876 | #ifdef CS_ANTICASC
|
---|
1877 | CS_MAXPID-3, cfg->cmaxidle);
|
---|
1878 | #else
|
---|
1879 | CS_MAXPID-2, cfg->cmaxidle);
|
---|
1880 | #endif
|
---|
1881 | if( cfg->max_log_size )
|
---|
1882 | sprintf(buf, "%d Kb", cfg->max_log_size);
|
---|
1883 | else
|
---|
1884 | strcpy(buf, "unlimited");
|
---|
1885 | cs_log("max. logsize=%s", buf);
|
---|
1886 | cs_log("client timeout=%d sec, cache delay=%d msec",
|
---|
1887 | cfg->ctimeout, cfg->delay);
|
---|
1888 | #ifdef CS_NOSHM
|
---|
1889 | cs_log("shared memory initialized (size=%d, fd=%d)", shmsize, shmid);
|
---|
1890 | #else
|
---|
1891 | cs_log("shared memory initialized (size=%d, id=%d)", shmsize, shmid);
|
---|
1892 | #endif
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | int main (int argc, char *argv[])
|
---|
1896 | {
|
---|
1897 | struct sockaddr_in cad; /* structure to hold client's address */
|
---|
1898 | int scad; /* length of address */
|
---|
1899 | //int fd; /* socket descriptors */
|
---|
1900 | int i, j, n;
|
---|
1901 | int bg=0;
|
---|
1902 | int gfd; //nph,
|
---|
1903 | int fdp[2];
|
---|
1904 | uchar buf[2048];
|
---|
1905 | void (*mod_def[])(struct s_module *)=
|
---|
1906 | {
|
---|
1907 | module_monitor,
|
---|
1908 | module_camd33,
|
---|
1909 | module_camd35,
|
---|
1910 | module_camd35_tcp,
|
---|
1911 | module_newcamd,
|
---|
1912 | #ifdef CS_WITH_GBOX
|
---|
1913 | module_gbox,
|
---|
1914 | #endif
|
---|
1915 | module_radegast,
|
---|
1916 | module_mpcser,
|
---|
1917 | 0
|
---|
1918 | };
|
---|
1919 |
|
---|
1920 | while ((i=getopt(argc, argv, "bc:d:hm:"))!=EOF)
|
---|
1921 | {
|
---|
1922 | switch(i)
|
---|
1923 | {
|
---|
1924 | case 'b': bg=1;
|
---|
1925 | break;
|
---|
1926 | case 'c': strncpy(cs_confdir, optarg, sizeof(cs_confdir)-1);
|
---|
1927 | break;
|
---|
1928 | case 'd': cs_dblevel=atoi(optarg);
|
---|
1929 | break;
|
---|
1930 | case 'm':
|
---|
1931 | #ifdef CS_NOSHM
|
---|
1932 | strncpy(cs_memfile, optarg, sizeof(cs_memfile)-1);
|
---|
1933 | break;
|
---|
1934 | #endif
|
---|
1935 | case 'h':
|
---|
1936 | default : usage();
|
---|
1937 | }
|
---|
1938 | }
|
---|
1939 | if (cs_confdir[strlen(cs_confdir)]!='/') strcat(cs_confdir, "/");
|
---|
1940 | init_shm();
|
---|
1941 | init_config();
|
---|
1942 | for (i=0; mod_def[i]; i++) // must be later BEFORE init_config()
|
---|
1943 | {
|
---|
1944 | memset(&ph[i], 0, sizeof(struct s_module));
|
---|
1945 | mod_def[i](&ph[i]);
|
---|
1946 | }
|
---|
1947 |
|
---|
1948 | cs_log("auth size=%d", sizeof(struct s_auth));
|
---|
1949 | //cs_log_config();
|
---|
1950 | cfg->delay*=1000;
|
---|
1951 | init_sidtab();
|
---|
1952 | init_readerdb();
|
---|
1953 | init_userdb();
|
---|
1954 | init_signal();
|
---|
1955 | cs_set_mloc(30, "init");
|
---|
1956 | init_srvid();
|
---|
1957 | init_len4caid();
|
---|
1958 | //init_irdeto_guess_tab();
|
---|
1959 | cs_init_statistics(cfg->usrfile);
|
---|
1960 |
|
---|
1961 | if (pipe(fdp))
|
---|
1962 | {
|
---|
1963 | cs_log("Cannot create pipe (errno=%d)", errno);
|
---|
1964 | cs_exit(1);
|
---|
1965 | }
|
---|
1966 | mfdr=fdp[0];
|
---|
1967 | fd_c2m=fdp[1];
|
---|
1968 | gfd=mfdr+1;
|
---|
1969 |
|
---|
1970 | if (bg && daemon(1,0))
|
---|
1971 | {
|
---|
1972 | cs_log("Error starting in background (errno=%d)", errno);
|
---|
1973 | cs_exit(1);
|
---|
1974 | }
|
---|
1975 | master_pid=client[0].pid=getpid();
|
---|
1976 | if (cfg->pidfile[0])
|
---|
1977 | {
|
---|
1978 | FILE *fp;
|
---|
1979 | if (!(fp=fopen(cfg->pidfile, "w")))
|
---|
1980 | {
|
---|
1981 | cs_log("Cannot open pid-file (errno=%d)", errno);
|
---|
1982 | cs_exit(1);
|
---|
1983 | }
|
---|
1984 | fprintf(fp, "%d\n", getpid());
|
---|
1985 | fclose(fp);
|
---|
1986 | }
|
---|
1987 |
|
---|
1988 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
1989 | if( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
|
---|
1990 | for(j=0; j<ph[i].ptab->nports; j++)
|
---|
1991 | {
|
---|
1992 | start_listener(&ph[i], j);
|
---|
1993 | if( ph[i].ptab->ports[j].fd+1>gfd )
|
---|
1994 | gfd=ph[i].ptab->ports[j].fd+1;
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | start_client_resolver();
|
---|
1998 | init_service(97); // logger
|
---|
1999 | init_service(98); // resolver
|
---|
2000 | init_cardreader();
|
---|
2001 | #ifdef CS_ANTICASC
|
---|
2002 | if( !cfg->ac_enabled )
|
---|
2003 | cs_log("anti cascading disabled");
|
---|
2004 | else
|
---|
2005 | {
|
---|
2006 | init_ac();
|
---|
2007 | init_service(96);
|
---|
2008 | }
|
---|
2009 | #endif
|
---|
2010 |
|
---|
2011 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
2012 | if (ph[i].type & MOD_CONN_SERIAL) // for now: mpcser only
|
---|
2013 | if (ph[i].s_handler)
|
---|
2014 | ph[i].s_handler(i);
|
---|
2015 |
|
---|
2016 | cs_close_log();
|
---|
2017 | *mcl=1;
|
---|
2018 | while (1)
|
---|
2019 | {
|
---|
2020 | fd_set fds;
|
---|
2021 |
|
---|
2022 | do
|
---|
2023 | {
|
---|
2024 | FD_ZERO(&fds);
|
---|
2025 | FD_SET(mfdr, &fds);
|
---|
2026 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
2027 | if ( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
|
---|
2028 | for (j=0; j<ph[i].ptab->nports; j++)
|
---|
2029 | if (ph[i].ptab->ports[j].fd)
|
---|
2030 | FD_SET(ph[i].ptab->ports[j].fd, &fds);
|
---|
2031 | errno=0;
|
---|
2032 | cs_set_mloc(0, "before select");
|
---|
2033 | select(gfd, &fds, 0, 0, 0);
|
---|
2034 | cs_set_mloc(60, "after select");
|
---|
2035 | } while (errno==EINTR);
|
---|
2036 | cs_set_mloc(-1, "event (global)");
|
---|
2037 |
|
---|
2038 | client[0].last=time((time_t *)0);
|
---|
2039 | scad = sizeof(cad);
|
---|
2040 | if (FD_ISSET(mfdr, &fds))
|
---|
2041 | {
|
---|
2042 | cs_set_mloc(-1, "event: master-pipe");
|
---|
2043 | process_master_pipe();
|
---|
2044 | }
|
---|
2045 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
2046 | {
|
---|
2047 | if( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
|
---|
2048 | {
|
---|
2049 | for( j=0; j<ph[i].ptab->nports; j++ )
|
---|
2050 | {
|
---|
2051 | if( ph[i].ptab->ports[j].fd && FD_ISSET(ph[i].ptab->ports[j].fd, &fds) )
|
---|
2052 | {
|
---|
2053 | if (ph[i].type==MOD_CONN_UDP)
|
---|
2054 | {
|
---|
2055 | cs_set_mloc(-1, "event: udp-socket");
|
---|
2056 | if ((n=recvfrom(ph[i].ptab->ports[j].fd, buf+3, sizeof(buf)-3, 0, (struct sockaddr *)&cad, &scad))>0)
|
---|
2057 | {
|
---|
2058 | int idx;
|
---|
2059 | idx=idx_from_ip(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port));
|
---|
2060 | if (!idx)
|
---|
2061 | {
|
---|
2062 | if (pipe(fdp))
|
---|
2063 | {
|
---|
2064 | cs_log("Cannot create pipe (errno=%d)", errno);
|
---|
2065 | cs_exit(1);
|
---|
2066 | }
|
---|
2067 | switch(cs_fork(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port)))
|
---|
2068 | {
|
---|
2069 | case -1:
|
---|
2070 | close(fdp[0]);
|
---|
2071 | close(fdp[1]);
|
---|
2072 | break;
|
---|
2073 | case 0:
|
---|
2074 | client[idx=cs_last_idx].ufd=fdp[1];
|
---|
2075 | close(fdp[0]);
|
---|
2076 | break;
|
---|
2077 | default:
|
---|
2078 | // close(fdp[1]); // now used to simulate event
|
---|
2079 | pfd=fdp[0];
|
---|
2080 | wait4master();
|
---|
2081 | client[cs_idx].ctyp=i;
|
---|
2082 | client[cs_idx].port_idx=j;
|
---|
2083 | client[cs_idx].udp_fd=ph[i].ptab->ports[j].fd;
|
---|
2084 | client[cs_idx].udp_sa=cad;
|
---|
2085 | if (ph[client[cs_idx].ctyp].watchdog)
|
---|
2086 | alarm(cfg->cmaxidle<<2);
|
---|
2087 | ph[i].s_handler(cad); // never return
|
---|
2088 | }
|
---|
2089 | }
|
---|
2090 | if (idx)
|
---|
2091 | {
|
---|
2092 | unsigned short rl;
|
---|
2093 | rl=n;
|
---|
2094 | buf[0]='U';
|
---|
2095 | memcpy(buf+1, &rl, 2);
|
---|
2096 | write(client[idx].ufd, buf, n+3);
|
---|
2097 | }
|
---|
2098 | }
|
---|
2099 | }
|
---|
2100 | else
|
---|
2101 | {
|
---|
2102 | cs_set_mloc(-1, "event: tcp-socket");
|
---|
2103 | if ((pfd=accept(ph[i].ptab->ports[j].fd, (struct sockaddr *)&cad, &scad))>0)
|
---|
2104 | {
|
---|
2105 | switch(cs_fork(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port)))
|
---|
2106 | {
|
---|
2107 | case -1:
|
---|
2108 | case 0:
|
---|
2109 | close(pfd);
|
---|
2110 | break;
|
---|
2111 | default:
|
---|
2112 | wait4master();
|
---|
2113 | client[cs_idx].ctyp=i;
|
---|
2114 | client[cs_idx].udp_fd=pfd;
|
---|
2115 | client[cs_idx].port_idx=j;
|
---|
2116 | if (ph[client[cs_idx].ctyp].watchdog)
|
---|
2117 | alarm(cfg->cmaxidle<<2);
|
---|
2118 | ph[i].s_handler();
|
---|
2119 | }
|
---|
2120 | }
|
---|
2121 | }
|
---|
2122 | }
|
---|
2123 | }
|
---|
2124 | } // if (ph[i].type & MOD_CONN_NET)
|
---|
2125 | }
|
---|
2126 | }
|
---|
2127 | cs_exit(1);
|
---|
2128 | }
|
---|