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