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