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