1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
2 | #define CS_CORE
|
---|
3 | #include "globals.h"
|
---|
4 | #if defined(AZBOX) && defined(HAVE_DVBAPI)
|
---|
5 | # include "openxcas/openxcas_api.h"
|
---|
6 | #endif
|
---|
7 | #define CS_VERSION_X CS_VERSION
|
---|
8 | #ifdef COOL
|
---|
9 | void coolapi_close_all();
|
---|
10 | void coolapi_open_all();
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | extern void cs_statistics(struct s_client * client);
|
---|
14 | extern int ICC_Async_Close (struct s_reader *reader);
|
---|
15 |
|
---|
16 | /*****************************************************************************
|
---|
17 | Globals
|
---|
18 | *****************************************************************************/
|
---|
19 | int exit_oscam=0;
|
---|
20 | struct s_module ph[CS_MAX_MOD]; // Protocols
|
---|
21 | struct s_cardsystem cardsystem[CS_MAX_MOD];
|
---|
22 | struct s_cardreader cardreader[CS_MAX_MOD];
|
---|
23 |
|
---|
24 | struct s_client * first_client = NULL; //Pointer to clients list, first client is master
|
---|
25 | struct s_reader * first_active_reader = NULL; //list of active readers (enable=1 deleted = 0)
|
---|
26 | LLIST * configured_readers = NULL; //list of all (configured) readers
|
---|
27 |
|
---|
28 | ushort len4caid[256]; // table for guessing caid (by len)
|
---|
29 | char cs_confdir[128]=CS_CONFDIR;
|
---|
30 | int cs_dblevel=0; // Debug Level (TODO !!)
|
---|
31 | #ifdef WEBIF
|
---|
32 | int cs_restart_mode=1; //Restartmode: 0=off, no restart fork, 1=(default)restart fork, restart by webif, 2=like=1, but also restart on segfaults
|
---|
33 | #endif
|
---|
34 | char cs_tmpdir[200]={0x00};
|
---|
35 | pthread_mutex_t gethostbyname_lock;
|
---|
36 | pthread_mutex_t get_cw_lock;
|
---|
37 | pthread_key_t getclient;
|
---|
38 |
|
---|
39 | //Cache for ecms, cws and rcs:
|
---|
40 | LLIST *ecmcache;
|
---|
41 |
|
---|
42 | struct s_config cfg;
|
---|
43 | #ifdef CS_LOGHISTORY
|
---|
44 | int loghistidx; // ptr to current entry
|
---|
45 | char loghist[CS_MAXLOGHIST*CS_LOGHISTSIZE]; // ptr of log-history
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | int get_threadnum(struct s_client *client) {
|
---|
49 | struct s_client *cl;
|
---|
50 | int count=0;
|
---|
51 |
|
---|
52 | for (cl=first_client->next; cl ; cl=cl->next) {
|
---|
53 | if (cl->typ==client->typ)
|
---|
54 | count++;
|
---|
55 | if(cl==client)
|
---|
56 | return count;
|
---|
57 | }
|
---|
58 | return 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | struct s_client * cur_client(void)
|
---|
62 | {
|
---|
63 | return (struct s_client *) pthread_getspecific(getclient);
|
---|
64 | }
|
---|
65 |
|
---|
66 | int cs_check_v(uint ip, int add) {
|
---|
67 | int result = 0;
|
---|
68 | if (cfg.failbantime) {
|
---|
69 |
|
---|
70 | if (!cfg.v_list)
|
---|
71 | cfg.v_list = ll_create();
|
---|
72 |
|
---|
73 | time_t now = time((time_t)0);
|
---|
74 | LL_ITER *itr = ll_iter_create(cfg.v_list);
|
---|
75 | V_BAN *v_ban_entry;
|
---|
76 | int ftime = cfg.failbantime*60;
|
---|
77 |
|
---|
78 | //run over all banned entries to do housekeeping:
|
---|
79 | while ((v_ban_entry=ll_iter_next(itr))) {
|
---|
80 |
|
---|
81 | // housekeeping:
|
---|
82 | if ((now - v_ban_entry->v_time) >= ftime) { // entry out of time->remove
|
---|
83 | ll_iter_remove_data(itr);
|
---|
84 | continue;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (ip == v_ban_entry->v_ip) {
|
---|
88 | result=1;
|
---|
89 | if (!add) {
|
---|
90 | if (v_ban_entry->v_count >= cfg.failbancount) {
|
---|
91 | cs_debug_mask(D_TRACE, "failban: banned ip %s - %ld seconds left",
|
---|
92 | cs_inet_ntoa(v_ban_entry->v_ip),ftime - (now - v_ban_entry->v_time));
|
---|
93 | } else {
|
---|
94 | cs_debug_mask(D_TRACE, "failban: ip %s chance %d of %d",
|
---|
95 | cs_inet_ntoa(v_ban_entry->v_ip), v_ban_entry->v_count, cfg.failbancount);
|
---|
96 | v_ban_entry->v_count++;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | cs_debug_mask(D_TRACE, "failban: banned ip %s - already exist in list", cs_inet_ntoa(v_ban_entry->v_ip));
|
---|
101 | }
|
---|
102 |
|
---|
103 | }
|
---|
104 | }
|
---|
105 | if (add && !result) {
|
---|
106 | v_ban_entry = malloc(sizeof(V_BAN));
|
---|
107 | memset(v_ban_entry, 0, sizeof(V_BAN));
|
---|
108 |
|
---|
109 | v_ban_entry->v_time = time((time_t *)0);
|
---|
110 | v_ban_entry->v_ip = ip;
|
---|
111 |
|
---|
112 | ll_iter_insert(itr, v_ban_entry);
|
---|
113 |
|
---|
114 | cs_debug_mask(D_TRACE, "failban: ban ip %s with timestamp %d", cs_inet_ntoa(v_ban_entry->v_ip), v_ban_entry->v_time);
|
---|
115 | }
|
---|
116 | ll_iter_release(itr);
|
---|
117 | }
|
---|
118 | return result;
|
---|
119 | }
|
---|
120 |
|
---|
121 | int cs_check_violation(uint ip) {
|
---|
122 | return cs_check_v(ip, 0);
|
---|
123 | }
|
---|
124 | void cs_add_violation(uint ip) {
|
---|
125 | cs_check_v(ip, 1);
|
---|
126 | }
|
---|
127 |
|
---|
128 | void cs_add_lastresponsetime(struct s_client *cl, int ltime){
|
---|
129 |
|
---|
130 | if(cl->cwlastresptimes_last == CS_ECM_RINGBUFFER_MAX - 1){
|
---|
131 | cl->cwlastresptimes_last = 0;
|
---|
132 | } else {
|
---|
133 | cl->cwlastresptimes_last++;
|
---|
134 | }
|
---|
135 | cl->cwlastresptimes[cl->cwlastresptimes_last] = ltime;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*****************************************************************************
|
---|
139 | Statics
|
---|
140 | *****************************************************************************/
|
---|
141 | static const char *logo = " ___ ____ ___ \n / _ \\/ ___| / __|__ _ _ __ ___ \n| | | \\___ \\| | / _` | '_ ` _ \\ \n| |_| |___) | |_| (_| | | | | | |\n \\___/|____/ \\___\\__,_|_| |_| |_|\n";
|
---|
142 |
|
---|
143 | static void usage()
|
---|
144 | {
|
---|
145 | fprintf(stderr, "%s\n\n", logo);
|
---|
146 | fprintf(stderr, "OSCam cardserver v%s, build #%s (%s) - (w) 2009-2011 Streamboard SVN\n", CS_VERSION_X, CS_SVN_VERSION, CS_OSTYPE);
|
---|
147 | fprintf(stderr, "\tsee http://streamboard.gmc.to/oscam/ for more details\n");
|
---|
148 | fprintf(stderr, "\tbased on Streamboard mp-cardserver v0.9d - (w) 2004-2007 by dukat\n");
|
---|
149 | fprintf(stderr, "\tThis program is distributed under GPL.\n");
|
---|
150 | fprintf(stderr, "\tinbuilt add-ons: ");
|
---|
151 | #ifdef WEBIF
|
---|
152 | fprintf(stderr, "webinterface ");
|
---|
153 | #endif
|
---|
154 | #ifdef MODULE_MONITOR
|
---|
155 | fprintf(stderr, "monitor ");
|
---|
156 | #endif
|
---|
157 | #ifdef WITH_SSL
|
---|
158 | fprintf(stderr, "openssl ");
|
---|
159 | #endif
|
---|
160 | #ifdef HAVE_DVBAPI
|
---|
161 | #ifdef WITH_STAPI
|
---|
162 | fprintf(stderr, "dvbapi-with-stapi ");
|
---|
163 | #else
|
---|
164 | fprintf(stderr, "dvbapi ");
|
---|
165 | #endif
|
---|
166 | #endif
|
---|
167 | #ifdef IRDETO_GUESSING
|
---|
168 | fprintf(stderr, "irdeto-guessing ");
|
---|
169 | #endif
|
---|
170 | #ifdef CS_ANTICASC
|
---|
171 | fprintf(stderr, "anticascading ");
|
---|
172 | #endif
|
---|
173 | #ifdef WITH_DEBUG
|
---|
174 | fprintf(stderr, "debug ");
|
---|
175 | #endif
|
---|
176 | #ifdef CS_LED
|
---|
177 | fprintf(stderr, "led-trigger ");
|
---|
178 | #endif
|
---|
179 | #ifdef CS_WITH_DOUBLECHECK
|
---|
180 | fprintf(stderr, "doublecheck ");
|
---|
181 | #endif
|
---|
182 | #ifdef QBOXHD_LED
|
---|
183 | fprintf(stderr, "qboxhd-led-trigger ");
|
---|
184 | #endif
|
---|
185 | #ifdef CS_LOGHISTORY
|
---|
186 | fprintf(stderr, "loghistory ");
|
---|
187 | #endif
|
---|
188 | #ifdef LIBUSB
|
---|
189 | fprintf(stderr, "smartreader ");
|
---|
190 | #endif
|
---|
191 | #ifdef HAVE_PCSC
|
---|
192 | fprintf(stderr, "pcsc ");
|
---|
193 | #endif
|
---|
194 | fprintf(stderr, "\n\tinbuilt protocols: ");
|
---|
195 | #ifdef MODULE_CAMD33
|
---|
196 | fprintf(stderr, "camd33 ");
|
---|
197 | #endif
|
---|
198 | #ifdef MODULE_CAMD35
|
---|
199 | fprintf(stderr, "camd35-udp ");
|
---|
200 | #endif
|
---|
201 | #ifdef MODULE_CAMD35_TCP
|
---|
202 | fprintf(stderr, "camd35-tcp ");
|
---|
203 | #endif
|
---|
204 | #ifdef MODULE_NEWCAMD
|
---|
205 | fprintf(stderr, "newcamd ");
|
---|
206 | #endif
|
---|
207 | #ifdef MODULE_CCCAM
|
---|
208 | fprintf(stderr, "cccam ");
|
---|
209 | #endif
|
---|
210 | #ifdef MODULE_GBOX
|
---|
211 | fprintf(stderr, "gbox ");
|
---|
212 | #endif
|
---|
213 | #ifdef MODULE_RADEGAST
|
---|
214 | fprintf(stderr, "radegast ");
|
---|
215 | #endif
|
---|
216 | #ifdef MODULE_SERIAL
|
---|
217 | fprintf(stderr, "serial ");
|
---|
218 | #endif
|
---|
219 | #ifdef MODULE_CONSTCW
|
---|
220 | fprintf(stderr, "constcw ");
|
---|
221 | #endif
|
---|
222 | fprintf(stderr, "\n\tinbuilt cardreaders: ");
|
---|
223 | #ifdef READER_NAGRA
|
---|
224 | fprintf(stderr, "nagra ");
|
---|
225 | #endif
|
---|
226 | #ifdef READER_IRDETO
|
---|
227 | fprintf(stderr, "irdeto ");
|
---|
228 | #endif
|
---|
229 | #ifdef READER_CONAX
|
---|
230 | fprintf(stderr, "conax ");
|
---|
231 | #endif
|
---|
232 | #ifdef READER_CRYPTOWORKS
|
---|
233 | fprintf(stderr, "cryptoworks ");
|
---|
234 | #endif
|
---|
235 | #ifdef READER_SECA
|
---|
236 | fprintf(stderr, "seca ");
|
---|
237 | #endif
|
---|
238 | #ifdef READER_VIACCESS
|
---|
239 | fprintf(stderr, "viaccess ");
|
---|
240 | #endif
|
---|
241 | #ifdef READER_VIDEOGUARD
|
---|
242 | fprintf(stderr, "videoguard ");
|
---|
243 | #endif
|
---|
244 | #ifdef READER_DRE
|
---|
245 | fprintf(stderr, "dre ");
|
---|
246 | #endif
|
---|
247 | #ifdef READER_TONGFANG
|
---|
248 | fprintf(stderr, "tongfang ");
|
---|
249 | #endif
|
---|
250 | fprintf(stderr, "\n\n");
|
---|
251 | fprintf(stderr, "oscam [-b] [-c <config dir>] [-t <tmp dir>] [-d <level>] [-r <level>] [-h]");
|
---|
252 | fprintf(stderr, "\n\n\t-b : start in background\n");
|
---|
253 | fprintf(stderr, "\t-c <dir> : read configuration from <dir>\n");
|
---|
254 | fprintf(stderr, "\t default = %s\n", CS_CONFDIR);
|
---|
255 | fprintf(stderr, "\t-t <dir> : tmp dir <dir>\n");
|
---|
256 | #ifdef CS_CYGWIN32
|
---|
257 | fprintf(stderr, "\t default = (OS-TMP)\n");
|
---|
258 | #else
|
---|
259 | fprintf(stderr, "\t default = /tmp/.oscam\n");
|
---|
260 | #endif
|
---|
261 | fprintf(stderr, "\t-d <level> : debug level mask\n");
|
---|
262 | fprintf(stderr, "\t 0 = no debugging (default)\n");
|
---|
263 | fprintf(stderr, "\t 1 = detailed error messages\n");
|
---|
264 | fprintf(stderr, "\t 2 = ATR parsing info, ECM, EMM and CW dumps\n");
|
---|
265 | fprintf(stderr, "\t 4 = traffic from/to the reader\n");
|
---|
266 | fprintf(stderr, "\t 8 = traffic from/to the clients\n");
|
---|
267 | fprintf(stderr, "\t 16 = traffic to the reader-device on IFD layer\n");
|
---|
268 | fprintf(stderr, "\t 32 = traffic to the reader-device on I/O layer\n");
|
---|
269 | fprintf(stderr, "\t 64 = EMM logging\n");
|
---|
270 | fprintf(stderr, "\t 128 = DVBAPI logging\n");
|
---|
271 | fprintf(stderr, "\t 255 = debug all\n");
|
---|
272 | #ifdef WEBIF
|
---|
273 | fprintf(stderr, "\t-r <level> : restart level\n");
|
---|
274 | fprintf(stderr, "\t 0 = disabled, restart request sets exit status 99\n");
|
---|
275 | fprintf(stderr, "\t 1 = restart activated, web interface can restart oscam (default)\n");
|
---|
276 | fprintf(stderr, "\t 2 = like 1, but also restart on SEGFAULTS\n");
|
---|
277 | #endif
|
---|
278 | fprintf(stderr, "\t-h : show this help\n");
|
---|
279 | fprintf(stderr, "\n");
|
---|
280 | exit(1);
|
---|
281 | }
|
---|
282 |
|
---|
283 | #ifdef NEED_DAEMON
|
---|
284 | #ifdef OS_MACOSX
|
---|
285 | // this is done because daemon is being deprecated starting with 10.5 and -Werror will always trigger an error
|
---|
286 | static int daemon_compat(int nochdir, int noclose)
|
---|
287 | #else
|
---|
288 | static int daemon(int nochdir, int noclose)
|
---|
289 | #endif
|
---|
290 | {
|
---|
291 | int fd;
|
---|
292 |
|
---|
293 | switch (fork())
|
---|
294 | {
|
---|
295 | case -1: return (-1);
|
---|
296 | case 0: break;
|
---|
297 | default: _exit(0);
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (setsid()==(-1))
|
---|
301 | return(-1);
|
---|
302 |
|
---|
303 | if (!nochdir)
|
---|
304 | (void)chdir("/");
|
---|
305 |
|
---|
306 | if (!noclose && (fd=open("/dev/null", O_RDWR, 0)) != -1)
|
---|
307 | {
|
---|
308 | (void)dup2(fd, STDIN_FILENO);
|
---|
309 | (void)dup2(fd, STDOUT_FILENO);
|
---|
310 | (void)dup2(fd, STDERR_FILENO);
|
---|
311 | if (fd>2)
|
---|
312 | (void)close(fd);
|
---|
313 | }
|
---|
314 | return(0);
|
---|
315 | }
|
---|
316 | #endif
|
---|
317 |
|
---|
318 | int recv_from_udpipe(uchar *buf)
|
---|
319 | {
|
---|
320 | unsigned short n;
|
---|
321 | if (buf[0]!='U')
|
---|
322 | {
|
---|
323 | cs_log("INTERNAL PIPE-ERROR");
|
---|
324 | cs_exit(1);
|
---|
325 | }
|
---|
326 | memcpy(&n, buf+1, 2);
|
---|
327 |
|
---|
328 | memmove(buf, buf+3, n);
|
---|
329 |
|
---|
330 | return n;
|
---|
331 | }
|
---|
332 |
|
---|
333 | char *username(struct s_client * client)
|
---|
334 | {
|
---|
335 | if (!client)
|
---|
336 | return "NULL";
|
---|
337 |
|
---|
338 | if (client->typ == 's' || client->typ == 'h')
|
---|
339 | {
|
---|
340 | // get username master running under
|
---|
341 | struct passwd *pwd;
|
---|
342 | if ((pwd = getpwuid(getuid())) != NULL)
|
---|
343 | return pwd->pw_name;
|
---|
344 | else
|
---|
345 | return "root";
|
---|
346 | }
|
---|
347 |
|
---|
348 | if(client->account)
|
---|
349 | {
|
---|
350 | if (client->account->usr[0])
|
---|
351 | return(client->account->usr);
|
---|
352 | else
|
---|
353 | return("anonymous");
|
---|
354 | }
|
---|
355 | else
|
---|
356 | {
|
---|
357 | return("NULL");
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | static struct s_client * idx_from_ip(in_addr_t ip, in_port_t port)
|
---|
362 | {
|
---|
363 | struct s_client *cl;
|
---|
364 | for (cl=first_client; cl ; cl=cl->next)
|
---|
365 | if ((cl->ip==ip) && (cl->port==port) && ((cl->typ=='c') || (cl->typ=='m')))
|
---|
366 | return cl;
|
---|
367 | return NULL;
|
---|
368 | }
|
---|
369 |
|
---|
370 | struct s_client * get_client_by_tid(unsigned long tid) //FIXME untested!! no longer pid in output...
|
---|
371 | {
|
---|
372 | struct s_client *cl;
|
---|
373 | for (cl=first_client; cl ; cl=cl->next)
|
---|
374 | if ((unsigned long)(cl->thread)==tid)
|
---|
375 | return cl;
|
---|
376 | return NULL;
|
---|
377 | }
|
---|
378 |
|
---|
379 | static long chk_caid(ushort caid, CAIDTAB *ctab)
|
---|
380 | {
|
---|
381 | int n;
|
---|
382 | long rc;
|
---|
383 | for (rc=(-1), n=0; (n<CS_MAXCAIDTAB) && (rc<0); n++)
|
---|
384 | if ((caid & ctab->mask[n]) == ctab->caid[n])
|
---|
385 | rc=ctab->cmap[n] ? ctab->cmap[n] : caid;
|
---|
386 | return(rc);
|
---|
387 | }
|
---|
388 |
|
---|
389 | int chk_bcaid(ECM_REQUEST *er, CAIDTAB *ctab)
|
---|
390 | {
|
---|
391 | long caid;
|
---|
392 | if ((caid=chk_caid(er->caid, ctab))<0)
|
---|
393 | return(0);
|
---|
394 | er->caid=caid;
|
---|
395 | return(1);
|
---|
396 | }
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * void set_signal_handler(int sig, int flags, void (*sighandler)(int))
|
---|
400 | * flags: 1 = restart, 2 = don't modify if SIG_IGN, may be combined
|
---|
401 | */
|
---|
402 | void set_signal_handler(int sig, int flags, void (*sighandler))
|
---|
403 | {
|
---|
404 | #ifdef CS_SIGBSD
|
---|
405 | if ((signal(sig, sighandler)==SIG_IGN) && (flags & 2))
|
---|
406 | {
|
---|
407 | signal(sig, SIG_IGN);
|
---|
408 | siginterrupt(sig, 0);
|
---|
409 | }
|
---|
410 | else
|
---|
411 | siginterrupt(sig, (flags & 1) ? 0 : 1);
|
---|
412 | #else
|
---|
413 | struct sigaction sa;
|
---|
414 | sigaction(sig, (struct sigaction *) 0, &sa);
|
---|
415 | if (!((flags & 2) && (sa.sa_handler==SIG_IGN)))
|
---|
416 | {
|
---|
417 | sigemptyset(&sa.sa_mask);
|
---|
418 | sa.sa_flags=(flags & 1) ? SA_RESTART : 0;
|
---|
419 | sa.sa_handler=sighandler;
|
---|
420 | sigaction(sig, &sa, (struct sigaction *) 0);
|
---|
421 | }
|
---|
422 | #endif
|
---|
423 | }
|
---|
424 |
|
---|
425 | static void cs_master_alarm()
|
---|
426 | {
|
---|
427 | cs_log("PANIC: master deadlock!");
|
---|
428 | fprintf(stderr, "PANIC: master deadlock!");
|
---|
429 | fflush(stderr);
|
---|
430 | }
|
---|
431 |
|
---|
432 | static void cs_sigpipe()
|
---|
433 | {
|
---|
434 | if (cs_dblevel & D_ALL_DUMP)
|
---|
435 | cs_log("Got sigpipe signal -> captured");
|
---|
436 | }
|
---|
437 |
|
---|
438 | #ifdef WEBIF
|
---|
439 | void clear_account_stats(struct s_auth *account)
|
---|
440 | {
|
---|
441 | account->cwfound = 0;
|
---|
442 | account->cwcache = 0;
|
---|
443 | account->cwnot = 0;
|
---|
444 | account->cwtun = 0;
|
---|
445 | account->cwignored = 0;
|
---|
446 | account->cwtout = 0;
|
---|
447 | account->emmok = 0;
|
---|
448 | account->emmnok = 0;
|
---|
449 | }
|
---|
450 |
|
---|
451 | void clear_all_account_stats()
|
---|
452 | {
|
---|
453 | struct s_auth *account = cfg.account;
|
---|
454 | while (account) {
|
---|
455 | clear_account_stats(account);
|
---|
456 | account = account->next;
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | void clear_system_stats()
|
---|
461 | {
|
---|
462 | first_client->cwfound = 0;
|
---|
463 | first_client->cwcache = 0;
|
---|
464 | first_client->cwnot = 0;
|
---|
465 | first_client->cwtun = 0;
|
---|
466 | first_client->cwignored = 0;
|
---|
467 | first_client->cwtout = 0;
|
---|
468 | first_client->emmok = 0;
|
---|
469 | first_client->emmnok = 0;
|
---|
470 | }
|
---|
471 | #endif
|
---|
472 |
|
---|
473 | void cs_accounts_chk()
|
---|
474 | {
|
---|
475 | struct s_auth *old_accounts = cfg.account;
|
---|
476 | struct s_auth *new_accounts = init_userdb();
|
---|
477 | struct s_auth *account1,*account2;
|
---|
478 | for (account1=cfg.account; account1; account1=account1->next) {
|
---|
479 | for (account2=new_accounts; account2; account2=account2->next) {
|
---|
480 | if (!strcmp(account1->usr, account2->usr)) {
|
---|
481 | account2->cwfound = account1->cwfound;
|
---|
482 | account2->cwcache = account1->cwcache;
|
---|
483 | account2->cwnot = account1->cwnot;
|
---|
484 | account2->cwtun = account1->cwtun;
|
---|
485 | account2->cwignored = account1->cwignored;
|
---|
486 | account2->cwtout = account1->cwtout;
|
---|
487 | account2->emmok = account1->emmok;
|
---|
488 | account2->emmnok = account1->emmnok;
|
---|
489 | account2->firstlogin = account1->firstlogin;
|
---|
490 | #ifdef CS_ANTICASC
|
---|
491 | account2->ac_users = account1->ac_users;
|
---|
492 | account2->ac_penalty = account1->ac_penalty;
|
---|
493 | account2->ac_stat = account1->ac_stat;
|
---|
494 | #endif
|
---|
495 | }
|
---|
496 | }
|
---|
497 | }
|
---|
498 | cs_reinit_clients(new_accounts);
|
---|
499 | cfg.account = new_accounts;
|
---|
500 | init_free_userdb(old_accounts);
|
---|
501 |
|
---|
502 | #ifdef CS_ANTICASC
|
---|
503 | ac_clear();
|
---|
504 | #endif
|
---|
505 | }
|
---|
506 |
|
---|
507 | void nullclose(int *fd)
|
---|
508 | {
|
---|
509 | //if closing an already closed pipe, we get a sigpipe signal, and this causes a cs_exit
|
---|
510 | //and this causes a close and this causes a sigpipe...and so on
|
---|
511 | int f = *fd;
|
---|
512 | *fd = 0; //so first null client-fd
|
---|
513 | close(f); //then close fd
|
---|
514 | }
|
---|
515 |
|
---|
516 | static void cleanup_ecmtasks(struct s_client *cl)
|
---|
517 | {
|
---|
518 | if (!cl->ecmtask)
|
---|
519 | return;
|
---|
520 |
|
---|
521 | int i, n=(ph[cl->ctyp].multi)?CS_MAXPENDING:1;
|
---|
522 | ECM_REQUEST *ecm;
|
---|
523 | for (i=0; i<n; i++) {
|
---|
524 | ecm = &cl->ecmtask[i];
|
---|
525 | ll_destroy(ecm->matching_rdr); //no need to garbage this
|
---|
526 | ecm->matching_rdr=NULL;
|
---|
527 | }
|
---|
528 | add_garbage(cl->ecmtask);
|
---|
529 | cl->ecmtask = NULL;
|
---|
530 | }
|
---|
531 |
|
---|
532 | void cleanup_thread(void *var)
|
---|
533 | {
|
---|
534 | struct s_client *cl = var;
|
---|
535 | if(cl && !cl->cleaned){
|
---|
536 | struct s_client *prev, *cl2;
|
---|
537 | for (prev=first_client, cl2=first_client->next; prev->next != NULL; prev=prev->next, cl2=cl2->next)
|
---|
538 | if (cl == cl2)
|
---|
539 | break;
|
---|
540 | if (cl != cl2)
|
---|
541 | cs_log("FATAL ERROR: could not find client to remove from list.");
|
---|
542 | else
|
---|
543 | prev->next = cl2->next; //remove client from list
|
---|
544 |
|
---|
545 | if(cl->typ == 'c' && ph[cl->ctyp].cleanup)
|
---|
546 | ph[cl->ctyp].cleanup(cl);
|
---|
547 | else if (cl->reader && cl->reader->ph.cleanup)
|
---|
548 | cl->reader->ph.cleanup(cl);
|
---|
549 | if(cl->typ == 'c'){
|
---|
550 | cs_statistics(cl);
|
---|
551 | cl->last_caid = 0xFFFF;
|
---|
552 | cl->last_srvid = 0xFFFF;
|
---|
553 | cs_statistics(cl);
|
---|
554 | }
|
---|
555 |
|
---|
556 | if(cl->pfd) nullclose(&cl->pfd); //Closing Network socket
|
---|
557 | if(cl->fd_m2c_c) nullclose(&cl->fd_m2c_c); //Closing client read fd
|
---|
558 | if(cl->fd_m2c) nullclose(&cl->fd_m2c); //Closing client read fd
|
---|
559 |
|
---|
560 | if(cl->typ == 'r' && cl->reader){
|
---|
561 | // Maybe we also need a "nullclose" mechanism here...
|
---|
562 | ICC_Async_Close(cl->reader);
|
---|
563 | }
|
---|
564 | if (cl->reader) {
|
---|
565 | cl->reader->client = NULL;
|
---|
566 | cl->reader = NULL;
|
---|
567 | }
|
---|
568 | cleanup_ecmtasks(cl);
|
---|
569 | add_garbage(cl->emmcache);
|
---|
570 | add_garbage(cl->req);
|
---|
571 | add_garbage(cl->cc);
|
---|
572 | add_garbage(cl->serialdata);
|
---|
573 | add_garbage(cl);
|
---|
574 | cl->cleaned++;
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | static void cs_cleanup()
|
---|
579 | {
|
---|
580 | if (cfg.lb_mode && cfg.lb_save) {
|
---|
581 | save_stat_to_file(0);
|
---|
582 | cfg.lb_save = 0; //this is for avoiding duplicate saves
|
---|
583 | }
|
---|
584 |
|
---|
585 | done_share();
|
---|
586 |
|
---|
587 | //cleanup clients:
|
---|
588 | struct s_client *cl;
|
---|
589 | for (cl=first_client->next; cl; cl=cl->next) {
|
---|
590 | if (cl->typ=='c')
|
---|
591 | kill_thread(cl);
|
---|
592 | }
|
---|
593 |
|
---|
594 | //cleanup readers:
|
---|
595 | struct s_reader *rdr;
|
---|
596 | for (rdr=first_active_reader; rdr ; rdr=rdr->next) {
|
---|
597 | cs_log("killing reader %s", rdr->label);
|
---|
598 | kill_thread(rdr->client);
|
---|
599 | }
|
---|
600 | first_active_reader = NULL;
|
---|
601 |
|
---|
602 | init_free_userdb(cfg.account);
|
---|
603 | cfg.account = NULL;
|
---|
604 | init_free_sidtab();
|
---|
605 | }
|
---|
606 |
|
---|
607 | void cs_exit(int sig)
|
---|
608 | {
|
---|
609 | char targetfile[256];
|
---|
610 |
|
---|
611 | set_signal_handler(SIGCHLD, 1, SIG_IGN);
|
---|
612 | set_signal_handler(SIGHUP , 1, SIG_IGN);
|
---|
613 | set_signal_handler(SIGPIPE, 1, SIG_IGN);
|
---|
614 |
|
---|
615 | if (sig==SIGALRM) {
|
---|
616 | cs_debug_mask(D_TRACE, "thread %8X: SIGALRM, skipping", pthread_self());
|
---|
617 | return;
|
---|
618 | }
|
---|
619 |
|
---|
620 | if (sig && (sig!=SIGQUIT))
|
---|
621 | cs_log("exit with signal %d", sig);
|
---|
622 |
|
---|
623 | struct s_client *cl = cur_client();
|
---|
624 |
|
---|
625 | switch(cl->typ)
|
---|
626 | {
|
---|
627 | case 'c':
|
---|
628 | cs_statistics(cl);
|
---|
629 | cl->last_caid = 0xFFFF;
|
---|
630 | cl->last_srvid = 0xFFFF;
|
---|
631 | cs_statistics(cl);
|
---|
632 | break;
|
---|
633 |
|
---|
634 | case 'm': break;
|
---|
635 | case 'r': break; //reader-cleanup now in cleanup_thread()
|
---|
636 |
|
---|
637 | case 'h':
|
---|
638 | case 's':
|
---|
639 | #ifdef CS_LED
|
---|
640 | cs_switch_led(LED1B, LED_OFF);
|
---|
641 | cs_switch_led(LED2, LED_OFF);
|
---|
642 | cs_switch_led(LED3, LED_OFF);
|
---|
643 | cs_switch_led(LED1A, LED_ON);
|
---|
644 | #endif
|
---|
645 | #ifdef QBOXHD_LED
|
---|
646 | qboxhd_led_blink(QBOXHD_LED_COLOR_YELLOW,QBOXHD_LED_BLINK_FAST);
|
---|
647 | qboxhd_led_blink(QBOXHD_LED_COLOR_RED,QBOXHD_LED_BLINK_FAST);
|
---|
648 | qboxhd_led_blink(QBOXHD_LED_COLOR_GREEN,QBOXHD_LED_BLINK_FAST);
|
---|
649 | qboxhd_led_blink(QBOXHD_LED_COLOR_BLUE,QBOXHD_LED_BLINK_FAST);
|
---|
650 | qboxhd_led_blink(QBOXHD_LED_COLOR_MAGENTA,QBOXHD_LED_BLINK_FAST);
|
---|
651 | #endif
|
---|
652 |
|
---|
653 | #ifndef OS_CYGWIN32
|
---|
654 | snprintf(targetfile, 255, "%s%s", get_tmp_dir(), "/oscam.version");
|
---|
655 | if (unlink(targetfile) < 0)
|
---|
656 | cs_log("cannot remove oscam version file %s (errno=%d %s)", targetfile, errno, strerror(errno));
|
---|
657 | #endif
|
---|
658 | #ifdef COOL
|
---|
659 | coolapi_close_all();
|
---|
660 | #endif
|
---|
661 | break;
|
---|
662 | }
|
---|
663 |
|
---|
664 | // this is very important - do not remove
|
---|
665 | if (cl->typ != 's') {
|
---|
666 | cs_log("thread %8X ended!", pthread_self());
|
---|
667 | #ifdef NO_PTHREAD_CLEANUP_PUSH
|
---|
668 | cleanup_thread(cl);
|
---|
669 | #endif
|
---|
670 | //Restore signals before exiting thread
|
---|
671 | set_signal_handler(SIGPIPE , 0, cs_sigpipe);
|
---|
672 | set_signal_handler(SIGHUP , 1, cs_reload_config);
|
---|
673 |
|
---|
674 | pthread_exit(NULL);
|
---|
675 | return;
|
---|
676 | }
|
---|
677 |
|
---|
678 | cs_log("cardserver down");
|
---|
679 | cs_close_log();
|
---|
680 |
|
---|
681 | cs_cleanup();
|
---|
682 |
|
---|
683 | if (!exit_oscam)
|
---|
684 | exit_oscam = sig?sig:1;
|
---|
685 | }
|
---|
686 |
|
---|
687 | void cs_reinit_clients(struct s_auth *new_accounts)
|
---|
688 | {
|
---|
689 | struct s_auth *account;
|
---|
690 |
|
---|
691 | struct s_client *cl;
|
---|
692 | for (cl=first_client->next; cl ; cl=cl->next)
|
---|
693 | if( cl->typ == 'c' && cl->account ) {
|
---|
694 | for (account = new_accounts; (account) ; account = account->next)
|
---|
695 | if (!strcmp(cl->account->usr, account->usr))
|
---|
696 | break;
|
---|
697 |
|
---|
698 | if (account && cl->pcrc == crc32(0L, MD5((uchar *)account->pwd, strlen(account->pwd), cur_client()->dump), 16)) {
|
---|
699 | cl->account = account;
|
---|
700 | cl->grp = account->grp;
|
---|
701 | cl->aureader_list = account->aureader_list;
|
---|
702 | cl->autoau = account->autoau;
|
---|
703 | cl->expirationdate = account->expirationdate;
|
---|
704 | cl->allowedtimeframe[0] = account->allowedtimeframe[0];
|
---|
705 | cl->allowedtimeframe[1] = account->allowedtimeframe[1];
|
---|
706 | cl->ncd_keepalive = account->ncd_keepalive;
|
---|
707 | cl->c35_suppresscmd08 = account->c35_suppresscmd08;
|
---|
708 | cl->tosleep = (60*account->tosleep);
|
---|
709 | cl->c35_sleepsend = account->c35_sleepsend;
|
---|
710 | cl->monlvl = account->monlvl;
|
---|
711 | cl->disabled = account->disabled;
|
---|
712 | cl->fchid = account->fchid; // CHID filters
|
---|
713 | cl->cltab = account->cltab; // Class
|
---|
714 | // newcamd module dosent like ident reloading
|
---|
715 | if(!cl->ncd_server)
|
---|
716 | cl->ftab = account->ftab; // Ident
|
---|
717 |
|
---|
718 | cl->sidtabok = account->sidtabok; // services
|
---|
719 | cl->sidtabno = account->sidtabno; // services
|
---|
720 | cl->failban = account->failban;
|
---|
721 |
|
---|
722 | memcpy(&cl->ctab, &account->ctab, sizeof(cl->ctab));
|
---|
723 | memcpy(&cl->ttab, &account->ttab, sizeof(cl->ttab));
|
---|
724 |
|
---|
725 | int i;
|
---|
726 | for(i = 0; i < CS_ECM_RINGBUFFER_MAX; i++)
|
---|
727 | cl->cwlastresptimes[i] = 0;
|
---|
728 | cl->cwlastresptimes_last = 0;
|
---|
729 |
|
---|
730 | #ifdef CS_ANTICASC
|
---|
731 | cl->ac_limit = (account->ac_users * 100 + 80) * cfg.ac_stime;
|
---|
732 | #endif
|
---|
733 | } else {
|
---|
734 | if (ph[cl->ctyp].type & MOD_CONN_NET) {
|
---|
735 | cs_debug_mask(D_TRACE, "client '%s', thread=%8X not found in db (or password changed)", cl->account->usr, cl->thread);
|
---|
736 | kill_thread(cl);
|
---|
737 | }
|
---|
738 | }
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | void cs_debug_level()
|
---|
743 | {
|
---|
744 | //switch debuglevel forward one step if not set from outside
|
---|
745 | switch (cs_dblevel) {
|
---|
746 | case 0:
|
---|
747 | cs_dblevel = 1;
|
---|
748 | break;
|
---|
749 | case 128:
|
---|
750 | cs_dblevel = 255;
|
---|
751 | break;
|
---|
752 | case 255:
|
---|
753 | cs_dblevel = 0;
|
---|
754 | break;
|
---|
755 | default:
|
---|
756 | cs_dblevel <<= 1;
|
---|
757 | }
|
---|
758 |
|
---|
759 | cs_log("%sdebug_level=%d", "all", cs_dblevel);
|
---|
760 | }
|
---|
761 |
|
---|
762 | void cs_card_info()
|
---|
763 | {
|
---|
764 | uchar dummy[1]={0x00};
|
---|
765 | struct s_client *cl;
|
---|
766 | for (cl=first_client->next; cl ; cl=cl->next)
|
---|
767 | if( cl->typ=='r' && cl->fd_m2c )
|
---|
768 | write_to_pipe(cl->fd_m2c, PIP_ID_CIN, dummy, 1);
|
---|
769 | //kill(client[i].pid, SIGUSR2);
|
---|
770 | }
|
---|
771 |
|
---|
772 | struct s_client * create_client(in_addr_t ip) {
|
---|
773 | struct s_client *cl;
|
---|
774 |
|
---|
775 | cl = malloc(sizeof(struct s_client));
|
---|
776 | if (cl) {
|
---|
777 | memset(cl, 0, sizeof(struct s_client));
|
---|
778 | int fdp[2];
|
---|
779 | if (pipe(fdp)) {
|
---|
780 | cs_log("Cannot create pipe (errno=%d: %s)", errno, strerror(errno));
|
---|
781 | free(cl);
|
---|
782 | return NULL;
|
---|
783 | }
|
---|
784 | //client part
|
---|
785 |
|
---|
786 | //make_non_blocking(fdp[0]);
|
---|
787 | //make_non_blocking(fdp[1]);
|
---|
788 | cl->fd_m2c_c = fdp[0]; //store client read fd
|
---|
789 | cl->fd_m2c = fdp[1]; //store client read fd
|
---|
790 | cl->ip=ip;
|
---|
791 | cl->account = first_client->account;
|
---|
792 |
|
---|
793 | //master part
|
---|
794 | cl->stat=1;
|
---|
795 |
|
---|
796 | cl->login=cl->last=time((time_t *)0);
|
---|
797 |
|
---|
798 | //Now add new client to the list:
|
---|
799 | struct s_client *last;
|
---|
800 | for (last=first_client; last->next != NULL; last=last->next); //ends with cl on last client
|
---|
801 | last->next = cl;
|
---|
802 | } else {
|
---|
803 | cs_log("max connections reached (out of memory) -> reject client %s", cs_inet_ntoa(ip));
|
---|
804 | return NULL;
|
---|
805 | }
|
---|
806 | return(cl);
|
---|
807 | }
|
---|
808 |
|
---|
809 | /**
|
---|
810 | * called by signal SIGHUB
|
---|
811 | *
|
---|
812 | * reloads configs:
|
---|
813 | * - useraccounts (oscom.user)
|
---|
814 | * - services ids (oscam.srvid)
|
---|
815 | * - tier ids (oscam.tiers)
|
---|
816 | **/
|
---|
817 | void cs_reload_config()
|
---|
818 | {
|
---|
819 | cs_accounts_chk();
|
---|
820 | init_srvid();
|
---|
821 | init_tierid();
|
---|
822 | #ifdef CS_ANTICASC
|
---|
823 | ac_init_stat();
|
---|
824 | #endif
|
---|
825 | }
|
---|
826 |
|
---|
827 | static void init_signal_pre()
|
---|
828 | {
|
---|
829 | set_signal_handler(SIGPIPE , 1, SIG_IGN);
|
---|
830 | set_signal_handler(SIGWINCH, 1, SIG_IGN);
|
---|
831 | set_signal_handler(SIGALRM , 1, SIG_IGN);
|
---|
832 | set_signal_handler(SIGHUP , 1, SIG_IGN);
|
---|
833 | }
|
---|
834 |
|
---|
835 | static void init_signal()
|
---|
836 | {
|
---|
837 | set_signal_handler(SIGINT, 3, cs_exit);
|
---|
838 | //set_signal_handler(SIGKILL, 3, cs_exit);
|
---|
839 | #ifdef OS_MACOSX
|
---|
840 | set_signal_handler(SIGEMT, 3, cs_exit);
|
---|
841 | #else
|
---|
842 | //set_signal_handler(SIGPOLL, 3, cs_exit);
|
---|
843 | #endif
|
---|
844 | //set_signal_handler(SIGPROF, 3, cs_exit);
|
---|
845 | set_signal_handler(SIGTERM, 3, cs_exit);
|
---|
846 | //set_signal_handler(SIGVTALRM, 3, cs_exit);
|
---|
847 |
|
---|
848 | set_signal_handler(SIGWINCH, 1, SIG_IGN);
|
---|
849 | // set_signal_handler(SIGPIPE , 0, SIG_IGN);
|
---|
850 | set_signal_handler(SIGPIPE , 0, cs_sigpipe);
|
---|
851 | // set_signal_handler(SIGALRM , 0, cs_alarm);
|
---|
852 | set_signal_handler(SIGALRM , 0, cs_master_alarm);
|
---|
853 | // set_signal_handler(SIGCHLD , 1, cs_child_chk);
|
---|
854 | set_signal_handler(SIGHUP , 1, cs_reload_config);
|
---|
855 | //set_signal_handler(SIGHUP , 1, cs_sighup);
|
---|
856 | set_signal_handler(SIGUSR1, 1, cs_debug_level);
|
---|
857 | set_signal_handler(SIGUSR2, 1, cs_card_info);
|
---|
858 | set_signal_handler(SIGCONT, 1, SIG_IGN);
|
---|
859 | cs_log("signal handling initialized (type=%s)",
|
---|
860 | #ifdef CS_SIGBSD
|
---|
861 | "bsd"
|
---|
862 | #else
|
---|
863 | "sysv"
|
---|
864 | #endif
|
---|
865 | );
|
---|
866 | return;
|
---|
867 | }
|
---|
868 |
|
---|
869 | static void init_first_client()
|
---|
870 | {
|
---|
871 | //Generate 5 ECM cache entries:
|
---|
872 | ecmcache = ll_create();
|
---|
873 |
|
---|
874 | first_client = malloc(sizeof(struct s_client));
|
---|
875 | if (!first_client) {
|
---|
876 | fprintf(stderr, "Could not allocate memory for master client, exiting...");
|
---|
877 | exit(1);
|
---|
878 | }
|
---|
879 | memset(first_client, 0, sizeof(struct s_auth));
|
---|
880 | first_client->next = NULL; //terminate clients list with NULL
|
---|
881 | first_client->login=time((time_t *)0);
|
---|
882 | first_client->ip=cs_inet_addr("127.0.0.1");
|
---|
883 | first_client->typ='s';
|
---|
884 | first_client->thread=pthread_self();
|
---|
885 | struct s_auth *null_account = malloc(sizeof(struct s_auth));
|
---|
886 | memset(null_account, 0, sizeof(struct s_auth));
|
---|
887 | first_client->account = null_account;
|
---|
888 | if (pthread_setspecific(getclient, first_client)) {
|
---|
889 | fprintf(stderr, "Could not setspecific getclient in master process, exiting...");
|
---|
890 | exit(1);
|
---|
891 | }
|
---|
892 |
|
---|
893 |
|
---|
894 | pthread_mutex_init(&gethostbyname_lock, NULL);
|
---|
895 | pthread_mutex_init(&get_cw_lock, NULL);
|
---|
896 |
|
---|
897 | #ifdef CS_LOGHISTORY
|
---|
898 | loghistidx=0;
|
---|
899 | memset(loghist, 0, CS_MAXLOGHIST*CS_LOGHISTSIZE);
|
---|
900 | #endif
|
---|
901 |
|
---|
902 | #ifdef COOL
|
---|
903 | coolapi_open_all();
|
---|
904 | #endif
|
---|
905 | }
|
---|
906 |
|
---|
907 | static void init_check(){
|
---|
908 | char *ptr = __DATE__;
|
---|
909 | int month, year = atoi(ptr + strlen(ptr) - 4), day = atoi(ptr + 4);
|
---|
910 | if(day > 0 && day < 32 && year > 2010 && year < 9999){
|
---|
911 | struct tm timeinfo;
|
---|
912 | char months[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
---|
913 | for(month = 0; month < 12; ++month){
|
---|
914 | if(!strncmp(ptr, months[month], 3)) break;
|
---|
915 | }
|
---|
916 | if(month > 11) month = 0;
|
---|
917 | memset(&timeinfo, 0, sizeof(timeinfo));
|
---|
918 | timeinfo.tm_mday = day;
|
---|
919 | timeinfo.tm_mon = month;
|
---|
920 | timeinfo.tm_year = year - 1900;
|
---|
921 | time_t builddate = mktime(&timeinfo) - 86400;
|
---|
922 | int i = 0;
|
---|
923 | while(time((time_t)0) < builddate){
|
---|
924 | cs_log("The current system time is smaller than the build date (%s). Waiting 5s for time to correct...", ptr);
|
---|
925 | cs_sleepms(5000);
|
---|
926 | ++i;
|
---|
927 | if(i > 12){
|
---|
928 | cs_log("Waiting was not successful. OSCam will be started but is UNSUPPORTED this way. Do not report any errors with this version.");
|
---|
929 | break;
|
---|
930 | }
|
---|
931 | }
|
---|
932 | }
|
---|
933 | }
|
---|
934 |
|
---|
935 | static int start_listener(struct s_module *ph, int port_idx)
|
---|
936 | {
|
---|
937 | int ov=1, timeout, is_udp, i;
|
---|
938 | char ptxt[2][32];
|
---|
939 | //struct hostent *ptrh; /* pointer to a host table entry */
|
---|
940 | struct protoent *ptrp; /* pointer to a protocol table entry */
|
---|
941 | struct sockaddr_in sad; /* structure to hold server's address */
|
---|
942 |
|
---|
943 | ptxt[0][0]=ptxt[1][0]='\0';
|
---|
944 | if (!ph->ptab->ports[port_idx].s_port)
|
---|
945 | {
|
---|
946 | cs_log("%s: disabled", ph->desc);
|
---|
947 | return(0);
|
---|
948 | }
|
---|
949 | is_udp=(ph->type==MOD_CONN_UDP);
|
---|
950 |
|
---|
951 | memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
|
---|
952 | sad.sin_family = AF_INET; /* set family to Internet */
|
---|
953 | if (!ph->s_ip)
|
---|
954 | ph->s_ip=cfg.srvip;
|
---|
955 | if (ph->s_ip)
|
---|
956 | {
|
---|
957 | sad.sin_addr.s_addr=ph->s_ip;
|
---|
958 | snprintf(ptxt[0], sizeof(ptxt[0]), ", ip=%s", inet_ntoa(sad.sin_addr));
|
---|
959 | }
|
---|
960 | else
|
---|
961 | sad.sin_addr.s_addr=INADDR_ANY;
|
---|
962 | timeout=cfg.bindwait;
|
---|
963 | //ph->fd=0;
|
---|
964 | ph->ptab->ports[port_idx].fd = 0;
|
---|
965 |
|
---|
966 | if (ph->ptab->ports[port_idx].s_port > 0) /* test for illegal value */
|
---|
967 | sad.sin_port = htons((u_short)ph->ptab->ports[port_idx].s_port);
|
---|
968 | else
|
---|
969 | {
|
---|
970 | cs_log("%s: Bad port %d", ph->desc, ph->ptab->ports[port_idx].s_port);
|
---|
971 | return(0);
|
---|
972 | }
|
---|
973 |
|
---|
974 | /* Map transport protocol name to protocol number */
|
---|
975 |
|
---|
976 | if( (ptrp=getprotobyname(is_udp ? "udp" : "tcp")) )
|
---|
977 | ov=ptrp->p_proto;
|
---|
978 | else
|
---|
979 | ov=(is_udp) ? 17 : 6; // use defaults on error
|
---|
980 |
|
---|
981 | if ((ph->ptab->ports[port_idx].fd=socket(PF_INET,is_udp ? SOCK_DGRAM : SOCK_STREAM, ov))<0)
|
---|
982 | {
|
---|
983 | cs_log("%s: Cannot create socket (errno=%d: %s)", ph->desc, errno, strerror(errno));
|
---|
984 | return(0);
|
---|
985 | }
|
---|
986 |
|
---|
987 | ov=1;
|
---|
988 | if (setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_REUSEADDR, (void *)&ov, sizeof(ov))<0)
|
---|
989 | {
|
---|
990 | cs_log("%s: setsockopt failed (errno=%d: %s)", ph->desc, errno, strerror(errno));
|
---|
991 | close(ph->ptab->ports[port_idx].fd);
|
---|
992 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
993 | }
|
---|
994 |
|
---|
995 | #ifdef SO_REUSEPORT
|
---|
996 | setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_REUSEPORT, (void *)&ov, sizeof(ov));
|
---|
997 | #endif
|
---|
998 |
|
---|
999 | #ifdef SO_PRIORITY
|
---|
1000 | if (cfg.netprio)
|
---|
1001 | if (!setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg.netprio, sizeof(ulong)))
|
---|
1002 | snprintf(ptxt[1], sizeof(ptxt[1]), ", prio=%ld", cfg.netprio);
|
---|
1003 | #endif
|
---|
1004 |
|
---|
1005 | if( !is_udp )
|
---|
1006 | {
|
---|
1007 | int keep_alive = 1;
|
---|
1008 | setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
1009 | (void *)&keep_alive, sizeof(keep_alive));
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | while (timeout--)
|
---|
1013 | {
|
---|
1014 | if (bind(ph->ptab->ports[port_idx].fd, (struct sockaddr *)&sad, sizeof (sad))<0)
|
---|
1015 | {
|
---|
1016 | if (timeout)
|
---|
1017 | {
|
---|
1018 | cs_log("%s: Bind request failed, waiting another %d seconds",
|
---|
1019 | ph->desc, timeout);
|
---|
1020 | cs_sleepms(1000);
|
---|
1021 | }
|
---|
1022 | else
|
---|
1023 | {
|
---|
1024 | cs_log("%s: Bind request failed, giving up", ph->desc);
|
---|
1025 | close(ph->ptab->ports[port_idx].fd);
|
---|
1026 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 | else timeout=0;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | if (!is_udp)
|
---|
1033 | if (listen(ph->ptab->ports[port_idx].fd, CS_QLEN)<0)
|
---|
1034 | {
|
---|
1035 | cs_log("%s: Cannot start listen mode (errno=%d: %s)", ph->desc, errno, strerror(errno));
|
---|
1036 | close(ph->ptab->ports[port_idx].fd);
|
---|
1037 | return(ph->ptab->ports[port_idx].fd=0);
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | cs_log("%s: initialized (fd=%d, port=%d%s%s%s)",
|
---|
1041 | ph->desc, ph->ptab->ports[port_idx].fd,
|
---|
1042 | ph->ptab->ports[port_idx].s_port,
|
---|
1043 | ptxt[0], ptxt[1], ph->logtxt ? ph->logtxt : "");
|
---|
1044 |
|
---|
1045 | for( i=0; i<ph->ptab->ports[port_idx].ftab.nfilts; i++ ) {
|
---|
1046 | int j;
|
---|
1047 | cs_log("CAID: %04X", ph->ptab->ports[port_idx].ftab.filts[i].caid );
|
---|
1048 | for( j=0; j<ph->ptab->ports[port_idx].ftab.filts[i].nprids; j++ )
|
---|
1049 | cs_log("provid #%d: %06X", j, ph->ptab->ports[port_idx].ftab.filts[i].prids[j]);
|
---|
1050 | }
|
---|
1051 | return(ph->ptab->ports[port_idx].fd);
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | int cs_user_resolve(struct s_auth *account)
|
---|
1055 | {
|
---|
1056 | struct hostent *rht;
|
---|
1057 | struct sockaddr_in udp_sa;
|
---|
1058 | int result=0;
|
---|
1059 | if (account->dyndns[0])
|
---|
1060 | {
|
---|
1061 | pthread_mutex_lock(&gethostbyname_lock);
|
---|
1062 | in_addr_t lastip = account->dynip;
|
---|
1063 | //Resolve with gethostbyname:
|
---|
1064 | if (cfg.resolve_gethostbyname) {
|
---|
1065 | rht = gethostbyname((char*)account->dyndns);
|
---|
1066 | if (!rht)
|
---|
1067 | cs_log("can't resolve %s", account->dyndns);
|
---|
1068 | else {
|
---|
1069 | memcpy(&udp_sa.sin_addr, rht->h_addr, sizeof(udp_sa.sin_addr));
|
---|
1070 | account->dynip=udp_sa.sin_addr.s_addr;
|
---|
1071 | result=1;
|
---|
1072 | }
|
---|
1073 | }
|
---|
1074 | else { //Resolve with getaddrinfo:
|
---|
1075 | struct addrinfo hints, *res = NULL;
|
---|
1076 | memset(&hints, 0, sizeof(hints));
|
---|
1077 | hints.ai_socktype = SOCK_STREAM;
|
---|
1078 | hints.ai_family = AF_INET;
|
---|
1079 | hints.ai_protocol = IPPROTO_TCP;
|
---|
1080 |
|
---|
1081 | int err = getaddrinfo((const char*)account->dyndns, NULL, &hints, &res);
|
---|
1082 | if (err != 0 || !res || !res->ai_addr) {
|
---|
1083 | cs_log("can't resolve %s, error: %s", account->dyndns, err ? gai_strerror(err) : "unknown");
|
---|
1084 | }
|
---|
1085 | else {
|
---|
1086 | account->dynip=((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
|
---|
1087 | result=1;
|
---|
1088 | }
|
---|
1089 | if (res) freeaddrinfo(res);
|
---|
1090 | }
|
---|
1091 | if (lastip != account->dynip) {
|
---|
1092 | cs_log("%s: resolved ip=%s", (char*)account->dyndns, cs_inet_ntoa(account->dynip));
|
---|
1093 | }
|
---|
1094 | pthread_mutex_unlock(&gethostbyname_lock);
|
---|
1095 | }
|
---|
1096 | if (!result)
|
---|
1097 | account->dynip=0;
|
---|
1098 | return result;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | #pragma GCC diagnostic ignored "-Wempty-body"
|
---|
1102 | void *clientthread_init(void * init){
|
---|
1103 | struct s_clientinit clientinit;
|
---|
1104 | memcpy(&clientinit, init, sizeof(struct s_clientinit)); //copy to stack to free init pointer
|
---|
1105 | free(init);
|
---|
1106 | #ifndef NO_PTHREAD_CLEANUP_PUSH
|
---|
1107 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
---|
1108 | pthread_setspecific(getclient, clientinit.client);
|
---|
1109 | pthread_cleanup_push(cleanup_thread, (void *) clientinit.client);
|
---|
1110 | clientinit.handler(clientinit.client);
|
---|
1111 | pthread_cleanup_pop(1);
|
---|
1112 | #else
|
---|
1113 | clientinit.handler(clientinit.client);
|
---|
1114 | cs_exit(0);
|
---|
1115 | #endif
|
---|
1116 | return NULL;
|
---|
1117 | }
|
---|
1118 | #pragma GCC diagnostic warning "-Wempty-body"
|
---|
1119 |
|
---|
1120 | void start_thread(void * startroutine, char * nameroutine) {
|
---|
1121 | pthread_t temp;
|
---|
1122 | pthread_attr_t attr;
|
---|
1123 | pthread_attr_init(&attr);
|
---|
1124 | #ifndef TUXBOX
|
---|
1125 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
1126 | #endif
|
---|
1127 | if (pthread_create(&temp, &attr, startroutine, NULL))
|
---|
1128 | cs_log("ERROR: can't create %s thread", nameroutine);
|
---|
1129 | else {
|
---|
1130 | cs_log("%s thread started", nameroutine);
|
---|
1131 | pthread_detach(temp);
|
---|
1132 | }
|
---|
1133 | pthread_attr_destroy(&attr);
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | void kill_thread(struct s_client *cl) { //cs_exit is used to let thread kill itself, this routine is for a thread to kill other thread
|
---|
1137 |
|
---|
1138 | if (!cl) return;
|
---|
1139 | pthread_t thread = cl->thread;
|
---|
1140 | if (pthread_equal(thread, pthread_self())) return; //cant kill yourself
|
---|
1141 |
|
---|
1142 | pthread_cancel(thread);
|
---|
1143 | pthread_join(thread, NULL);
|
---|
1144 | #ifndef NO_PTHREAD_CLEANUP_PUSH
|
---|
1145 | while(!cl->cleaned)
|
---|
1146 | cs_sleepms(50);
|
---|
1147 | #else
|
---|
1148 | cs_sleepms(50);
|
---|
1149 | cleanup_thread(cl);
|
---|
1150 | #endif
|
---|
1151 | cs_log("thread %8X killed!", thread);
|
---|
1152 | return;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | #ifdef CS_ANTICASC
|
---|
1156 | void start_anticascader()
|
---|
1157 | {
|
---|
1158 | struct s_client * cl = create_client(first_client->ip);
|
---|
1159 | if (cl == NULL) return;
|
---|
1160 | cl->thread = pthread_self();
|
---|
1161 | pthread_setspecific(getclient, cl);
|
---|
1162 | cl->typ = 'a';
|
---|
1163 |
|
---|
1164 | //set_signal_handler(SIGHUP, 1, ac_init_stat);
|
---|
1165 | ac_init_stat();
|
---|
1166 | while(1)
|
---|
1167 | {
|
---|
1168 | cs_sleepms(1000); //FIXME this is a cpu-killer!
|
---|
1169 | ac_do_stat();
|
---|
1170 | }
|
---|
1171 | }
|
---|
1172 | #endif
|
---|
1173 |
|
---|
1174 | static void remove_reader_from_active(struct s_reader *rdr) {
|
---|
1175 | struct s_reader *rdr2, *prv = NULL;
|
---|
1176 | for (rdr2=first_active_reader; rdr2 ; rdr2=rdr2->next) {
|
---|
1177 | if (rdr2==rdr) {
|
---|
1178 | if (prv) prv->next = rdr2->next;
|
---|
1179 | else first_active_reader = rdr2->next;
|
---|
1180 | break;
|
---|
1181 | }
|
---|
1182 | prv = rdr2;
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | static void add_reader_to_active(struct s_reader *rdr) {
|
---|
1187 | struct s_reader *rdr2;
|
---|
1188 | rdr->next = NULL;
|
---|
1189 | if (first_active_reader) {
|
---|
1190 | for (rdr2=first_active_reader; rdr2->next ; rdr2=rdr2->next) ; //search last element
|
---|
1191 | rdr2->next = rdr;
|
---|
1192 | } else first_active_reader = rdr;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | int restart_cardreader(struct s_reader *rdr, int restart) {
|
---|
1196 |
|
---|
1197 | if (restart) {
|
---|
1198 | //remove from list:
|
---|
1199 | remove_reader_from_active(rdr);
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | if (restart) //kill old thread
|
---|
1203 | if (rdr->client) {
|
---|
1204 | kill_thread(rdr->client);
|
---|
1205 | rdr->client = NULL;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | rdr->tcp_connected = 0;
|
---|
1209 | rdr->card_status = UNKNOWN;
|
---|
1210 | rdr->tcp_block_delay = 100;
|
---|
1211 | cs_ftime(&rdr->tcp_block_connect_till);
|
---|
1212 |
|
---|
1213 | if (rdr->device[0] && (rdr->typ & R_IS_CASCADING)) {
|
---|
1214 | if (!rdr->ph.num) {
|
---|
1215 | cs_log("Protocol Support missing. (typ=%d)", rdr->typ);
|
---|
1216 | return 0;
|
---|
1217 | }
|
---|
1218 | cs_debug_mask(D_TRACE, "reader %s protocol: %s", rdr->label, rdr->ph.desc);
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | if (rdr->enable == 0)
|
---|
1222 | return 0;
|
---|
1223 |
|
---|
1224 | if (rdr->device[0]) {
|
---|
1225 | if (restart) {
|
---|
1226 | cs_log("restarting reader %s", rdr->label);
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | struct s_client * cl = create_client(first_client->ip);
|
---|
1230 | if (cl == NULL) return 0;
|
---|
1231 |
|
---|
1232 |
|
---|
1233 | rdr->fd=cl->fd_m2c;
|
---|
1234 | cl->reader=rdr;
|
---|
1235 | cs_log("creating thread for device %s", rdr->device);
|
---|
1236 |
|
---|
1237 | cl->sidtabok=rdr->sidtabok;
|
---|
1238 | cl->sidtabno=rdr->sidtabno;
|
---|
1239 |
|
---|
1240 | rdr->client=cl;
|
---|
1241 |
|
---|
1242 | cl->typ='r';
|
---|
1243 | //client[i].ctyp=99;
|
---|
1244 | pthread_attr_t attr;
|
---|
1245 | pthread_attr_init(&attr);
|
---|
1246 | #if !defined(TUXBOX) && !defined(HAVE_PCSC)
|
---|
1247 | /* pcsc doesn't like this either; segfaults on x86, x86_64 */
|
---|
1248 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
1249 | #endif
|
---|
1250 |
|
---|
1251 | if (pthread_create(&cl->thread, &attr, start_cardreader, (void *)rdr)) {
|
---|
1252 | cs_log("ERROR: can't create thread for %s", rdr->label);
|
---|
1253 | cleanup_thread(cl);
|
---|
1254 | restart = 0;
|
---|
1255 | }
|
---|
1256 | else
|
---|
1257 | pthread_detach(cl->thread);
|
---|
1258 | pthread_attr_destroy(&attr);
|
---|
1259 |
|
---|
1260 | if (restart) {
|
---|
1261 | //add to list
|
---|
1262 | add_reader_to_active(rdr);
|
---|
1263 | }
|
---|
1264 | return 1;
|
---|
1265 | }
|
---|
1266 | return 0;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | static void init_cardreader() {
|
---|
1270 |
|
---|
1271 | struct s_reader *rdr;
|
---|
1272 | for (rdr=first_active_reader; rdr ; rdr=rdr->next) {
|
---|
1273 | if (!restart_cardreader(rdr, 0))
|
---|
1274 | remove_reader_from_active(rdr);
|
---|
1275 | }
|
---|
1276 | load_stat_from_file();
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | static void cs_fake_client(struct s_client *client, char *usr, int uniq, in_addr_t ip)
|
---|
1280 | {
|
---|
1281 | /* Uniq = 1: only one connection per user
|
---|
1282 | *
|
---|
1283 | * Uniq = 2: set (new connected) user only to fake if source
|
---|
1284 | * ip is different (e.g. for newcamd clients with
|
---|
1285 | * different CAID's -> Ports)
|
---|
1286 | *
|
---|
1287 | * Uniq = 3: only one connection per user, but only the last
|
---|
1288 | * login will survive (old mpcs behavior)
|
---|
1289 | *
|
---|
1290 | * Uniq = 4: set user only to fake if source ip is
|
---|
1291 | * different, but only the last login will survive
|
---|
1292 | */
|
---|
1293 |
|
---|
1294 | struct s_client *cl;
|
---|
1295 | for (cl=first_client->next; cl ; cl=cl->next)
|
---|
1296 | {
|
---|
1297 | if (cl != client && (cl->typ == 'c') && !cl->dup && !strcmp(cl->account->usr, usr)
|
---|
1298 | && (uniq < 5) && ((uniq % 2) || (cl->ip != ip)))
|
---|
1299 | {
|
---|
1300 | char buf[20];
|
---|
1301 | if (uniq == 3 || uniq == 4)
|
---|
1302 | {
|
---|
1303 | cl->dup = 1;
|
---|
1304 | cl->aureader_list = NULL;
|
---|
1305 | cs_strncpy(buf, cs_inet_ntoa(cl->ip), sizeof(buf));
|
---|
1306 | cs_log("client(%8X) duplicate user '%s' from %s (prev %s) set to fake (uniq=%d)",
|
---|
1307 | cl->thread, usr, cs_inet_ntoa(ip), buf, uniq);
|
---|
1308 | if (cl->failban & BAN_DUPLICATE) {
|
---|
1309 | cs_add_violation(cl->ip);
|
---|
1310 | }
|
---|
1311 | if (cfg.dropdups)
|
---|
1312 | kill_thread(cl);
|
---|
1313 | }
|
---|
1314 | else
|
---|
1315 | {
|
---|
1316 | client->dup = 1;
|
---|
1317 | client->aureader_list = NULL;
|
---|
1318 | cs_strncpy(buf, cs_inet_ntoa(ip), sizeof(buf));
|
---|
1319 | cs_log("client(%8X) duplicate user '%s' from %s (current %s) set to fake (uniq=%d)",
|
---|
1320 | pthread_self(), usr, cs_inet_ntoa(cl->ip), buf, uniq);
|
---|
1321 | if (client->failban & BAN_DUPLICATE) {
|
---|
1322 | cs_add_violation(ip);
|
---|
1323 | }
|
---|
1324 | if (cfg.dropdups)
|
---|
1325 | cs_disconnect_client(client);
|
---|
1326 | break;
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | }
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | int cs_auth_client(struct s_client * client, struct s_auth *account, const char *e_txt)
|
---|
1335 | {
|
---|
1336 | int rc=0;
|
---|
1337 | char buf[32];
|
---|
1338 | char *t_crypt="encrypted";
|
---|
1339 | char *t_plain="plain";
|
---|
1340 | char *t_grant=" granted";
|
---|
1341 | char *t_reject=" rejected";
|
---|
1342 | char *t_msg[]= { buf, "invalid access", "invalid ip", "unknown reason" };
|
---|
1343 | memset(&client->grp, 0xff, sizeof(uint64));
|
---|
1344 | //client->grp=0xffffffffffffff;
|
---|
1345 | client->account=first_client->account;
|
---|
1346 | switch((long)account)
|
---|
1347 | {
|
---|
1348 | case 0: // reject access
|
---|
1349 | rc=1;
|
---|
1350 | cs_add_violation((uint)client->ip);
|
---|
1351 | cs_log("%s %s-client %s%s (%s)",
|
---|
1352 | client->crypted ? t_crypt : t_plain,
|
---|
1353 | ph[client->ctyp].desc,
|
---|
1354 | client->ip ? cs_inet_ntoa(client->ip) : "",
|
---|
1355 | client->ip ? t_reject : t_reject+1,
|
---|
1356 | e_txt ? e_txt : t_msg[rc]);
|
---|
1357 | break;
|
---|
1358 | default: // grant/check access
|
---|
1359 | if (client->ip && account->dyndns[0]) {
|
---|
1360 | if (client->ip != account->dynip)
|
---|
1361 | cs_user_resolve(account);
|
---|
1362 | if (client->ip != account->dynip) {
|
---|
1363 | cs_add_violation((uint)client->ip);
|
---|
1364 | rc=2;
|
---|
1365 | }
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | if (!rc)
|
---|
1369 | {
|
---|
1370 | client->dup=0;
|
---|
1371 | if (client->typ=='c')
|
---|
1372 | {
|
---|
1373 | client->last_caid = 0xFFFE;
|
---|
1374 | client->last_srvid = 0xFFFE;
|
---|
1375 | client->expirationdate = account->expirationdate;
|
---|
1376 | client->disabled = account->disabled;
|
---|
1377 | client->allowedtimeframe[0] = account->allowedtimeframe[0];
|
---|
1378 | client->allowedtimeframe[1] = account->allowedtimeframe[1];
|
---|
1379 | if(account->firstlogin == 0) account->firstlogin = time((time_t)0);
|
---|
1380 | client->failban = account->failban;
|
---|
1381 | client->c35_suppresscmd08 = account->c35_suppresscmd08;
|
---|
1382 | client->ncd_keepalive = account->ncd_keepalive;
|
---|
1383 | client->grp = account->grp;
|
---|
1384 | client->aureader_list = account->aureader_list;
|
---|
1385 | client->autoau = account->autoau;
|
---|
1386 | client->tosleep = (60*account->tosleep);
|
---|
1387 | client->c35_sleepsend = account->c35_sleepsend;
|
---|
1388 | memcpy(&client->ctab, &account->ctab, sizeof(client->ctab));
|
---|
1389 | if (account->uniq)
|
---|
1390 | cs_fake_client(client, account->usr, account->uniq, client->ip);
|
---|
1391 | client->ftab = account->ftab; // IDENT filter
|
---|
1392 | client->cltab = account->cltab; // CLASS filter
|
---|
1393 | client->fchid = account->fchid; // CHID filter
|
---|
1394 | client->sidtabok= account->sidtabok; // services
|
---|
1395 | client->sidtabno= account->sidtabno; // services
|
---|
1396 | client->pcrc = crc32(0L, MD5((uchar *)account->pwd, strlen(account->pwd), client->dump), 16);
|
---|
1397 | memcpy(&client->ttab, &account->ttab, sizeof(client->ttab));
|
---|
1398 | #ifdef CS_ANTICASC
|
---|
1399 | ac_init_client(client, account);
|
---|
1400 | #endif
|
---|
1401 | }
|
---|
1402 | }
|
---|
1403 | client->monlvl=account->monlvl;
|
---|
1404 | client->account = account;
|
---|
1405 | case -1: // anonymous grant access
|
---|
1406 | if (rc)
|
---|
1407 | t_grant=t_reject;
|
---|
1408 | else {
|
---|
1409 | if (client->typ=='m')
|
---|
1410 | snprintf(t_msg[0], sizeof(buf), "lvl=%d", client->monlvl);
|
---|
1411 | else {
|
---|
1412 | int rcount = ll_count(client->aureader_list);
|
---|
1413 | snprintf(buf, sizeof(buf), "au=");
|
---|
1414 | if (!rcount)
|
---|
1415 | snprintf(buf+3, sizeof(buf)-3, "off");
|
---|
1416 | else {
|
---|
1417 | if (client->autoau)
|
---|
1418 | snprintf(buf+3, sizeof(buf)-3, "auto (%d reader)", rcount);
|
---|
1419 | else
|
---|
1420 | snprintf(buf+3, sizeof(buf)-3, "on (%d reader)", rcount);
|
---|
1421 | }
|
---|
1422 | }
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | cs_log("%s %s-client %s%s (%s, %s)",
|
---|
1426 | client->crypted ? t_crypt : t_plain,
|
---|
1427 | e_txt ? e_txt : ph[client->ctyp].desc,
|
---|
1428 | client->ip ? cs_inet_ntoa(client->ip) : "",
|
---|
1429 | client->ip ? t_grant : t_grant+1,
|
---|
1430 | username(client), t_msg[rc]);
|
---|
1431 |
|
---|
1432 | break;
|
---|
1433 | }
|
---|
1434 | return(rc);
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | void cs_disconnect_client(struct s_client * client)
|
---|
1438 | {
|
---|
1439 | char buf[32]={0};
|
---|
1440 | if (client->ip)
|
---|
1441 | snprintf(buf, sizeof(buf), " from %s", cs_inet_ntoa(client->ip));
|
---|
1442 | cs_log("%s disconnected %s", username(client), buf);
|
---|
1443 | cs_exit(0);
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | /**
|
---|
1447 | * ecm cache
|
---|
1448 | **/
|
---|
1449 | static int check_and_store_ecmcache(ECM_REQUEST *er, uint64 grp)
|
---|
1450 | {
|
---|
1451 | time_t now = time(NULL);
|
---|
1452 | time_t timeout = now-(time_t)(cfg.ctimeout/1000)-CS_CACHE_TIMEOUT;
|
---|
1453 | struct s_ecm *ecmc;
|
---|
1454 | LL_ITER *it = ll_iter_create(ecmcache);
|
---|
1455 | while ((ecmc=ll_iter_next(it))) {
|
---|
1456 | if (ecmc->time < timeout) {
|
---|
1457 | ll_iter_remove_data(it);
|
---|
1458 | continue;
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | if (grp && !(grp & ecmc->grp))
|
---|
1462 | continue;
|
---|
1463 |
|
---|
1464 | if (ecmc->caid!=er->caid)
|
---|
1465 | continue;
|
---|
1466 |
|
---|
1467 | if (memcmp(ecmc->ecmd5, er->ecmd5, CS_ECMSTORESIZE))
|
---|
1468 | continue;
|
---|
1469 |
|
---|
1470 | ll_iter_release(it);
|
---|
1471 | //cs_debug_mask(D_TRACE, "cachehit! (ecm)");
|
---|
1472 | memcpy(er->cw, ecmc->cw, 16);
|
---|
1473 | er->selected_reader = ecmc->reader;
|
---|
1474 | if (ecmc->rc == E_FOUND)
|
---|
1475 | return E_CACHE1;
|
---|
1476 | er->ecmcacheptr = ecmc;
|
---|
1477 | return ecmc->rc;
|
---|
1478 | }
|
---|
1479 | ll_iter_release(it);
|
---|
1480 |
|
---|
1481 | //Add cache entry:
|
---|
1482 | ecmc = cs_malloc(&ecmc, sizeof(struct s_ecm), 0);
|
---|
1483 | memcpy(ecmc->ecmd5, er->ecmd5, CS_ECMSTORESIZE);
|
---|
1484 | ecmc->caid = er->caid;
|
---|
1485 | ecmc->grp = grp;
|
---|
1486 | ecmc->rc = E_99;
|
---|
1487 | ecmc->time = now;
|
---|
1488 | er->ecmcacheptr = ecmc;
|
---|
1489 | ll_prepend(ecmcache, ecmc);
|
---|
1490 |
|
---|
1491 | return E_UNHANDLED;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | /**
|
---|
1495 | * cache 1: client-invoked
|
---|
1496 | * returns found ecm task index
|
---|
1497 | **/
|
---|
1498 | static int check_cwcache1(ECM_REQUEST *er, uint64 grp)
|
---|
1499 | {
|
---|
1500 | //cs_ddump(ecmd5, CS_ECMSTORESIZE, "ECM search");
|
---|
1501 | //cs_log("cache1 CHECK: grp=%lX", grp);
|
---|
1502 |
|
---|
1503 | //cs_debug_mask(D_TRACE, "cachesize %d", ll_count(ecmcache));
|
---|
1504 | time_t now = time(NULL);
|
---|
1505 | time_t timeout = now-(time_t)(cfg.ctimeout/1000)-CS_CACHE_TIMEOUT;
|
---|
1506 | struct s_ecm *ecmc;
|
---|
1507 |
|
---|
1508 | LL_ITER *it = ll_iter_create(ecmcache);
|
---|
1509 | while ((ecmc=ll_iter_next(it))) {
|
---|
1510 | if (ecmc->time < timeout) {
|
---|
1511 | ll_iter_remove_data(it);
|
---|
1512 | continue;
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 | if (ecmc->rc != E_FOUND)
|
---|
1516 | continue;
|
---|
1517 |
|
---|
1518 | if (ecmc->caid != er->caid)
|
---|
1519 | continue;
|
---|
1520 |
|
---|
1521 | if (grp && !(grp & ecmc->grp))
|
---|
1522 | continue;
|
---|
1523 |
|
---|
1524 | if (memcmp(ecmc->ecmd5, er->ecmd5, CS_ECMSTORESIZE))
|
---|
1525 | continue;
|
---|
1526 |
|
---|
1527 | memcpy(er->cw, ecmc->cw, 16);
|
---|
1528 | er->selected_reader = ecmc->reader;
|
---|
1529 | ll_iter_release(it);
|
---|
1530 | //cs_debug_mask(D_TRACE, "cachehit!");
|
---|
1531 | return 1;
|
---|
1532 | }
|
---|
1533 | ll_iter_release(it);
|
---|
1534 | return 0;
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | /**
|
---|
1538 | * cache 2: reader-invoked
|
---|
1539 | * returns 1 if found in cache. cw is copied to er
|
---|
1540 | **/
|
---|
1541 | int check_cwcache2(ECM_REQUEST *er, uint64 grp)
|
---|
1542 | {
|
---|
1543 | int rc = check_cwcache1(er, grp);
|
---|
1544 | return rc;
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 |
|
---|
1548 | static void store_cw_in_cache(ECM_REQUEST *er, uint64 grp, int rc)
|
---|
1549 | {
|
---|
1550 | #ifdef CS_WITH_DOUBLECHECK
|
---|
1551 | if (cfg.double_check && er->checked < 2)
|
---|
1552 | return;
|
---|
1553 | #endif
|
---|
1554 | struct s_ecm *ecm = er->ecmcacheptr;
|
---|
1555 | if (!ecm || rc >= ecm->rc) return;
|
---|
1556 |
|
---|
1557 | //cs_log("store ecm from reader %d", er->selected_reader);
|
---|
1558 | memcpy(ecm->ecmd5, er->ecmd5, CS_ECMSTORESIZE);
|
---|
1559 | memcpy(ecm->cw, er->cw, 16);
|
---|
1560 | ecm->caid = er->caid;
|
---|
1561 | ecm->grp = grp;
|
---|
1562 | ecm->reader = er->selected_reader;
|
---|
1563 | ecm->rc = rc;
|
---|
1564 | ecm->time = time(NULL);
|
---|
1565 |
|
---|
1566 | //cs_ddump(cwcache[*cwidx].ecmd5, CS_ECMSTORESIZE, "ECM stored (idx=%d)", *cwidx);
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 | /*
|
---|
1570 | // only for debug
|
---|
1571 | static struct s_client * get_thread_by_pipefd(int fd)
|
---|
1572 | {
|
---|
1573 | struct s_client *cl;
|
---|
1574 | for (cl=first_client->next; cl ; cl=cl->next)
|
---|
1575 | if (fd==cl->fd_m2c || fd==cl->fd_m2c_c)
|
---|
1576 | return cl;
|
---|
1577 | return first_client; //master process
|
---|
1578 | }
|
---|
1579 | */
|
---|
1580 |
|
---|
1581 | /*
|
---|
1582 | * write_to_pipe():
|
---|
1583 | * write all kind of data to pipe specified by fd
|
---|
1584 | */
|
---|
1585 | int write_to_pipe(int fd, int id, uchar *data, int n)
|
---|
1586 | {
|
---|
1587 | if (!fd) {
|
---|
1588 | cs_log("write_to_pipe: fd==0 id: %d", id);
|
---|
1589 | return -1;
|
---|
1590 | }
|
---|
1591 |
|
---|
1592 | //cs_debug_mask(D_TRACE, "write to pipe %d (%s) thread: %8X to %8X", fd, PIP_ID_TXT[id], pthread_self(), get_thread_by_pipefd(fd)->thread);
|
---|
1593 |
|
---|
1594 | uchar buf[3+sizeof(void*)];
|
---|
1595 |
|
---|
1596 | // fixme
|
---|
1597 | // copy data to allocated memory
|
---|
1598 | // needed for compatibility
|
---|
1599 | // need to be freed after read_from_pipe
|
---|
1600 | void *d = malloc(n);
|
---|
1601 | memcpy(d, data, n);
|
---|
1602 |
|
---|
1603 | if ((id<0) || (id>PIP_ID_MAX))
|
---|
1604 | return(PIP_ID_ERR);
|
---|
1605 |
|
---|
1606 | memcpy(buf, PIP_ID_TXT[id], 3);
|
---|
1607 | memcpy(buf+3, &d, sizeof(void*));
|
---|
1608 |
|
---|
1609 | n=3+sizeof(void*);
|
---|
1610 |
|
---|
1611 | return(write(fd, buf, n));
|
---|
1612 | }
|
---|
1613 |
|
---|
1614 |
|
---|
1615 | /*
|
---|
1616 | * read_from_pipe():
|
---|
1617 | * read all kind of data from pipe specified by fd
|
---|
1618 | * special-flag redir: if set AND data is ECM: this will redirected to appr. client
|
---|
1619 | */
|
---|
1620 | int read_from_pipe(int fd, uchar **data, int redir)
|
---|
1621 | {
|
---|
1622 | int rc;
|
---|
1623 | long hdr=0;
|
---|
1624 | uchar buf[3+sizeof(void*)];
|
---|
1625 | memset(buf, 0, sizeof(buf));
|
---|
1626 |
|
---|
1627 | redir=redir;
|
---|
1628 | *data=(uchar *)0;
|
---|
1629 | rc=PIP_ID_NUL;
|
---|
1630 |
|
---|
1631 | if (bytes_available(fd)) {
|
---|
1632 | if (read(fd, buf, sizeof(buf))==sizeof(buf)) {
|
---|
1633 | memcpy(&hdr, buf+3, sizeof(void*));
|
---|
1634 | } else {
|
---|
1635 | cs_log("WARNING: pipe header to small !");
|
---|
1636 | return PIP_ID_ERR;
|
---|
1637 | }
|
---|
1638 | } else {
|
---|
1639 | cs_log("!bytes_available(fd)");
|
---|
1640 | return PIP_ID_NUL;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | uchar id[4];
|
---|
1644 | memcpy(id, buf, 3);
|
---|
1645 | id[3]='\0';
|
---|
1646 |
|
---|
1647 | //cs_debug_mask(D_TRACE, "read from pipe %d (%s) thread: %8X", fd, id, (unsigned int)pthread_self());
|
---|
1648 |
|
---|
1649 | int l;
|
---|
1650 | for (l=0; (rc<0) && (PIP_ID_TXT[l]); l++)
|
---|
1651 | if (!memcmp(buf, PIP_ID_TXT[l], 3))
|
---|
1652 | rc=l;
|
---|
1653 |
|
---|
1654 | if (rc<0) {
|
---|
1655 | cs_log("WARNING: pipe garbage from pipe %i", fd);
|
---|
1656 | return PIP_ID_ERR;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | *data = (void*)hdr;
|
---|
1660 |
|
---|
1661 | return(rc);
|
---|
1662 | }
|
---|
1663 |
|
---|
1664 | /*
|
---|
1665 | * write_ecm_request():
|
---|
1666 | */
|
---|
1667 | static int write_ecm_request(int fd, ECM_REQUEST *er)
|
---|
1668 | {
|
---|
1669 | return(write_to_pipe(fd, PIP_ID_ECM, (uchar *) er, sizeof(ECM_REQUEST)));
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | /*
|
---|
1673 | * This function writes the current CW from ECM struct to a cwl file.
|
---|
1674 | * The filename is re-calculated and file re-opened every time.
|
---|
1675 | * This will consume a bit cpu time, but nothing has to be stored between
|
---|
1676 | * each call. If not file exists, a header is prepended
|
---|
1677 | */
|
---|
1678 | void logCWtoFile(ECM_REQUEST *er)
|
---|
1679 | {
|
---|
1680 | FILE *pfCWL;
|
---|
1681 | char srvname[128];
|
---|
1682 | /* %s / %s _I %04X _ %s .cwl */
|
---|
1683 | char buf[256 + sizeof(srvname)];
|
---|
1684 | char date[7];
|
---|
1685 | unsigned char i, parity, writeheader = 0;
|
---|
1686 | time_t t;
|
---|
1687 | struct tm timeinfo;
|
---|
1688 | struct s_srvid *this;
|
---|
1689 |
|
---|
1690 | /*
|
---|
1691 | * search service name for that id and change characters
|
---|
1692 | * causing problems in file name
|
---|
1693 | */
|
---|
1694 | srvname[0] = 0;
|
---|
1695 | for (this=cfg.srvid; this; this = this->next) {
|
---|
1696 | if (this->srvid == er->srvid) {
|
---|
1697 | cs_strncpy(srvname, this->name, sizeof(srvname));
|
---|
1698 | srvname[sizeof(srvname)-1] = 0;
|
---|
1699 | for (i = 0; srvname[i]; i++)
|
---|
1700 | if (srvname[i] == ' ') srvname[i] = '_';
|
---|
1701 | break;
|
---|
1702 | }
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | /* calc log file name */
|
---|
1706 | time(&t);
|
---|
1707 | localtime_r(&t, &timeinfo);
|
---|
1708 | strftime(date, sizeof(date), "%Y%m%d", &timeinfo);
|
---|
1709 | snprintf(buf, sizeof(buf), "%s/%s_I%04X_%s.cwl", cfg.cwlogdir, date, er->srvid, srvname);
|
---|
1710 |
|
---|
1711 | /* open failed, assuming file does not exist, yet */
|
---|
1712 | if((pfCWL = fopen(buf, "r")) == NULL) {
|
---|
1713 | writeheader = 1;
|
---|
1714 | } else {
|
---|
1715 | /* we need to close the file if it was opened correctly */
|
---|
1716 | fclose(pfCWL);
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | if ((pfCWL = fopen(buf, "a+")) == NULL) {
|
---|
1720 | /* maybe this fails because the subdir does not exist. Is there a common function to create it?
|
---|
1721 | for the moment do not print to log on every ecm
|
---|
1722 | cs_log(""error opening cw logfile for writing: %s (errno=%d %s)", buf, errno, strerror(errno)); */
|
---|
1723 | return;
|
---|
1724 | }
|
---|
1725 | if (writeheader) {
|
---|
1726 | /* no global macro for cardserver name :( */
|
---|
1727 | fprintf(pfCWL, "# OSCam cardserver v%s - http://streamboard.gmc.to/oscam/\n", CS_VERSION_X);
|
---|
1728 | fprintf(pfCWL, "# control word log file for use with tsdec offline decrypter\n");
|
---|
1729 | strftime(buf, sizeof(buf),"DATE %Y-%m-%d, TIME %H:%M:%S, TZ %Z\n", &timeinfo);
|
---|
1730 | fprintf(pfCWL, "# %s", buf);
|
---|
1731 | fprintf(pfCWL, "# CAID 0x%04X, SID 0x%04X, SERVICE \"%s\"\n", er->caid, er->srvid, srvname);
|
---|
1732 | }
|
---|
1733 |
|
---|
1734 | parity = er->ecm[0]&1;
|
---|
1735 | fprintf(pfCWL, "%d ", parity);
|
---|
1736 | for (i = parity * 8; i < 8 + parity * 8; i++)
|
---|
1737 | fprintf(pfCWL, "%02X ", er->cw[i]);
|
---|
1738 | /* better use incoming time er->tps rather than current time? */
|
---|
1739 | strftime(buf,sizeof(buf),"%H:%M:%S\n", &timeinfo);
|
---|
1740 | fprintf(pfCWL, "# %s", buf);
|
---|
1741 | fflush(pfCWL);
|
---|
1742 | fclose(pfCWL);
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | /**
|
---|
1746 | * distributes found ecm-request to all clients with rc=99
|
---|
1747 | **/
|
---|
1748 | void distribute_ecm(ECM_REQUEST *er, uint64 grp)
|
---|
1749 | {
|
---|
1750 | struct s_client *cl;
|
---|
1751 | ECM_REQUEST *ecm;
|
---|
1752 | int n, i;
|
---|
1753 |
|
---|
1754 | if (er->rc == E_RDR_FOUND) //found converted to cache...
|
---|
1755 | er->rc = E_CACHE2; //cache
|
---|
1756 |
|
---|
1757 | for (cl=first_client->next; cl ; cl=cl->next) {
|
---|
1758 | if (cl->fd_m2c && cl->typ=='c' && cl->ecmtask && (cl->grp&grp)) {
|
---|
1759 |
|
---|
1760 | n=(ph[cl->ctyp].multi)?CS_MAXPENDING:1;
|
---|
1761 | for (i=0; i<n; i++) {
|
---|
1762 | ecm = &cl->ecmtask[i];
|
---|
1763 | if (ecm->rc >= E_99 && (ecm->ecmcacheptr == er->ecmcacheptr)) { //
|
---|
1764 | //||
|
---|
1765 | //((ecm->caid==er->caid || ecm->ocaid==er->ocaid) && !memcmp(ecm->ecmd5, er->ecmd5, CS_ECMSTORESIZE)))) {
|
---|
1766 | er->cpti = ecm->cpti;
|
---|
1767 | //cs_log("distribute %04X:%06X:%04X cpti %d to client %s", ecm->caid, ecm->prid, ecm->srvid, ecm->cpti, username(cl));
|
---|
1768 | write_ecm_request(cl->fd_m2c, er);
|
---|
1769 | }
|
---|
1770 | //else if (ecm->rc == E_99)
|
---|
1771 | // cs_log("NO-distribute %04X:%06X:%04X cpti %d to client %s", ecm->caid, ecm->prid, ecm->srvid, ecm->cpti, username(cl));
|
---|
1772 | }
|
---|
1773 | }
|
---|
1774 | }
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 |
|
---|
1778 | int write_ecm_answer(struct s_reader * reader, ECM_REQUEST *er)
|
---|
1779 | {
|
---|
1780 | int i;
|
---|
1781 | uchar c;
|
---|
1782 | for (i=0; i<16; i+=4)
|
---|
1783 | {
|
---|
1784 | c=((er->cw[i]+er->cw[i+1]+er->cw[i+2]) & 0xff);
|
---|
1785 | if (er->cw[i+3]!=c)
|
---|
1786 | {
|
---|
1787 | cs_debug_mask(D_TRACE, "notice: changed dcw checksum byte cw[%i] from %02x to %02x", i+3, er->cw[i+3],c);
|
---|
1788 | er->cw[i+3]=c;
|
---|
1789 | }
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 | er->selected_reader=reader;
|
---|
1793 | //cs_log("answer from reader %d (rc=%d)", er->selected_reader, er->rc);
|
---|
1794 | er->caid=er->ocaid;
|
---|
1795 |
|
---|
1796 | if (er->rc==E_RDR_FOUND) {
|
---|
1797 |
|
---|
1798 | store_cw_in_cache(er, reader->grp, E_FOUND);
|
---|
1799 |
|
---|
1800 | /* CWL logging only if cwlogdir is set in config */
|
---|
1801 | if (cfg.cwlogdir != NULL)
|
---|
1802 | logCWtoFile(er);
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 | int res=0;
|
---|
1806 | if( er->client && er->client->fd_m2c ) {
|
---|
1807 | //Wie got an ECM (or nok). Now we should check for another clients waiting for it:
|
---|
1808 | res = write_ecm_request(er->client->fd_m2c, er);
|
---|
1809 | }
|
---|
1810 |
|
---|
1811 | return res;
|
---|
1812 | }
|
---|
1813 |
|
---|
1814 | /*
|
---|
1815 | static int cs_read_timer(int fd, uchar *buf, int l, int msec)
|
---|
1816 | {
|
---|
1817 | struct timeval tv;
|
---|
1818 | fd_set fds;
|
---|
1819 | int rc;
|
---|
1820 |
|
---|
1821 | if (!fd) return(-1);
|
---|
1822 | tv.tv_sec = msec / 1000;
|
---|
1823 | tv.tv_usec = (msec % 1000) * 1000;
|
---|
1824 | FD_ZERO(&fds);
|
---|
1825 | FD_SET(cur_client()->pfd, &fds);
|
---|
1826 |
|
---|
1827 | select(fd+1, &fds, 0, 0, &tv);
|
---|
1828 |
|
---|
1829 | rc=0;
|
---|
1830 | if (FD_ISSET(cur_client()->pfd, &fds))
|
---|
1831 | if (!(rc=read(fd, buf, l)))
|
---|
1832 | rc=-1;
|
---|
1833 |
|
---|
1834 | return(rc);
|
---|
1835 | }*/
|
---|
1836 |
|
---|
1837 | ECM_REQUEST *get_ecmtask()
|
---|
1838 | {
|
---|
1839 | int i, n;
|
---|
1840 | ECM_REQUEST *er=0;
|
---|
1841 | struct s_client *cl = cur_client();
|
---|
1842 | if(!cl) return NULL;
|
---|
1843 | if (!cl->ecmtask)
|
---|
1844 | {
|
---|
1845 | n=(ph[cl->ctyp].multi)?CS_MAXPENDING:1;
|
---|
1846 | if( (cl->ecmtask=(ECM_REQUEST *)malloc(n*sizeof(ECM_REQUEST))) )
|
---|
1847 | memset(cl->ecmtask, 0, n*sizeof(ECM_REQUEST));
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | n=(-1);
|
---|
1851 | if (!cl->ecmtask)
|
---|
1852 | {
|
---|
1853 | cs_log("Cannot allocate memory (errno=%d %s)", errno, strerror(errno));
|
---|
1854 | n=(-2);
|
---|
1855 | }
|
---|
1856 | else
|
---|
1857 | if (ph[cl->ctyp].multi)
|
---|
1858 | {
|
---|
1859 | for (i=0; (n<0) && (i<CS_MAXPENDING); i++)
|
---|
1860 | if (cl->ecmtask[i].rc<E_99)
|
---|
1861 | er=&cl->ecmtask[n=i];
|
---|
1862 | }
|
---|
1863 | else
|
---|
1864 | er=&cl->ecmtask[n=0];
|
---|
1865 |
|
---|
1866 | if (n<0)
|
---|
1867 | cs_log("WARNING: ecm pending table overflow !");
|
---|
1868 | else
|
---|
1869 | {
|
---|
1870 |
|
---|
1871 | LLIST *save = er->matching_rdr;
|
---|
1872 | memset(er, 0, sizeof(ECM_REQUEST));
|
---|
1873 | er->rc=E_UNHANDLED;
|
---|
1874 | er->cpti=n;
|
---|
1875 | er->client=cl;
|
---|
1876 | cs_ftime(&er->tps);
|
---|
1877 |
|
---|
1878 | if (cl->typ=='c') { //for clients only! Not for readers!
|
---|
1879 | if (save) {
|
---|
1880 | ll_clear(save);
|
---|
1881 | er->matching_rdr = save;
|
---|
1882 | }
|
---|
1883 | else
|
---|
1884 | er->matching_rdr = ll_create();
|
---|
1885 |
|
---|
1886 | //cs_log("client %s ECMTASK %d multi %d ctyp %d", username(cl), n, (ph[cl->ctyp].multi)?CS_MAXPENDING:1, cl->ctyp);
|
---|
1887 | }
|
---|
1888 | }
|
---|
1889 |
|
---|
1890 |
|
---|
1891 | return(er);
|
---|
1892 | }
|
---|
1893 |
|
---|
1894 | static void send_reader_stat(struct s_reader *rdr, ECM_REQUEST *er, int rc)
|
---|
1895 | {
|
---|
1896 | if (rc>=E_99)
|
---|
1897 | return;
|
---|
1898 | struct timeb tpe;
|
---|
1899 | cs_ftime(&tpe);
|
---|
1900 | int time = 1000*(tpe.time-er->tps.time)+tpe.millitm-er->tps.millitm;
|
---|
1901 | if (time < 1)
|
---|
1902 | time = 1;
|
---|
1903 |
|
---|
1904 | add_stat(rdr, er, time, rc);
|
---|
1905 | }
|
---|
1906 |
|
---|
1907 | int send_dcw(struct s_client * client, ECM_REQUEST *er)
|
---|
1908 | {
|
---|
1909 | static const char *stxt[]={"found", "cache1", "cache2", "emu",
|
---|
1910 | "not found", "timeout", "sleeping",
|
---|
1911 | "fake", "invalid", "corrupt", "no card", "expdate", "disabled", "stopped"};
|
---|
1912 | static const char *stxtEx[]={"", "group", "caid", "ident", "class", "chid", "queue", "peer"};
|
---|
1913 | static const char *stxtWh[]={"", "user ", "reader ", "server ", "lserver "};
|
---|
1914 | char sby[32]="", sreason[32]="", schaninfo[32]="";
|
---|
1915 | char erEx[32]="";
|
---|
1916 | char uname[38]="";
|
---|
1917 | struct timeb tpe;
|
---|
1918 | ushort lc, *lp;
|
---|
1919 | for (lp=(ushort *)er->ecm+(er->l>>2), lc=0; lp>=(ushort *)er->ecm; lp--)
|
---|
1920 | lc^=*lp;
|
---|
1921 |
|
---|
1922 | snprintf(uname,sizeof(uname)-1, "%s", username(client));
|
---|
1923 |
|
---|
1924 | struct s_reader *er_reader = er->selected_reader; //responding reader
|
---|
1925 | if (!er_reader) er_reader = ll_has_elements(er->matching_rdr); //no reader? use first reader
|
---|
1926 |
|
---|
1927 | if (er_reader)
|
---|
1928 | {
|
---|
1929 | // add marker to reader if ECM_REQUEST was betatunneled
|
---|
1930 | if(er->btun)
|
---|
1931 | snprintf(sby, sizeof(sby)-1, " by %s(btun)", er_reader->label);
|
---|
1932 | else
|
---|
1933 | snprintf(sby, sizeof(sby)-1, " by %s", er_reader->label);
|
---|
1934 | }
|
---|
1935 | if (er->rc < E_NOTFOUND) er->rcEx=0;
|
---|
1936 | if (er->rcEx)
|
---|
1937 | snprintf(erEx, sizeof(erEx)-1, "rejected %s%s", stxtWh[er->rcEx>>4],
|
---|
1938 | stxtEx[er->rcEx&0xf]);
|
---|
1939 |
|
---|
1940 | if(cfg.mon_appendchaninfo)
|
---|
1941 | snprintf(schaninfo, sizeof(schaninfo)-1, " - %s", get_servicename(er->srvid, er->caid));
|
---|
1942 |
|
---|
1943 | if(er->msglog[0])
|
---|
1944 | snprintf(sreason, sizeof(sreason)-1, " (%s)", er->msglog);
|
---|
1945 |
|
---|
1946 | cs_ftime(&tpe);
|
---|
1947 | client->cwlastresptime = 1000 * (tpe.time-er->tps.time) + tpe.millitm-er->tps.millitm;
|
---|
1948 | cs_add_lastresponsetime(client, client->cwlastresptime); // add to ringbuffer
|
---|
1949 |
|
---|
1950 | if (er_reader && er_reader->client){
|
---|
1951 | er_reader->client->cwlastresptime = client->cwlastresptime;
|
---|
1952 | cs_add_lastresponsetime(er_reader->client, client->cwlastresptime);
|
---|
1953 | }
|
---|
1954 |
|
---|
1955 | #ifdef CS_LED
|
---|
1956 | if(!er->rc) cs_switch_led(LED2, LED_BLINK_OFF);
|
---|
1957 | #endif
|
---|
1958 |
|
---|
1959 | send_reader_stat(er->selected_reader, er, er->rc);
|
---|
1960 |
|
---|
1961 | #ifdef WEBIF
|
---|
1962 | if (er_reader) {
|
---|
1963 | if(er->rc == E_FOUND)
|
---|
1964 | snprintf(client->lastreader, sizeof(client->lastreader)-1, "%s", er_reader->label);
|
---|
1965 | else if ((er->rc == E_CACHE1) || (er->rc == E_CACHE2))
|
---|
1966 | snprintf(client->lastreader, sizeof(client->lastreader)-1, "%s (cache)", er_reader->label);
|
---|
1967 | else
|
---|
1968 | snprintf(client->lastreader, sizeof(client->lastreader)-1, "%s", stxt[er->rc]);
|
---|
1969 | }
|
---|
1970 | #endif
|
---|
1971 |
|
---|
1972 | er->caid = er->ocaid;
|
---|
1973 | switch(er->rc) {
|
---|
1974 | case E_FOUND:
|
---|
1975 | case E_EMU: //FIXME obsolete ?
|
---|
1976 | client->cwfound++;
|
---|
1977 | client->account->cwfound++;
|
---|
1978 | first_client->cwfound++;
|
---|
1979 | break;
|
---|
1980 |
|
---|
1981 | case E_CACHE1:
|
---|
1982 | case E_CACHE2:
|
---|
1983 | client->cwcache++;
|
---|
1984 | client->account->cwcache++;
|
---|
1985 | first_client->cwcache++;
|
---|
1986 | break;
|
---|
1987 |
|
---|
1988 | case E_NOTFOUND:
|
---|
1989 | case E_CORRUPT:
|
---|
1990 | case E_NOCARD:
|
---|
1991 | if (er->rcEx) {
|
---|
1992 | client->cwignored++;
|
---|
1993 | client->account->cwignored++;
|
---|
1994 | first_client->cwignored++;
|
---|
1995 | } else {
|
---|
1996 | client->cwnot++;
|
---|
1997 | client->account->cwnot++;
|
---|
1998 | first_client->cwnot++;
|
---|
1999 | }
|
---|
2000 | break;
|
---|
2001 |
|
---|
2002 | case E_TIMEOUT:
|
---|
2003 | client->cwtout++;
|
---|
2004 | client->account->cwtout++;
|
---|
2005 | first_client->cwtout++;
|
---|
2006 | break;
|
---|
2007 |
|
---|
2008 | default:
|
---|
2009 | client->cwignored++;
|
---|
2010 | client->account->cwignored++;
|
---|
2011 | first_client->cwignored++;
|
---|
2012 | }
|
---|
2013 |
|
---|
2014 | #ifdef CS_ANTICASC
|
---|
2015 | ac_chk(client, er, 1);
|
---|
2016 | #endif
|
---|
2017 |
|
---|
2018 | int is_fake = 0;
|
---|
2019 | if (er->rc==E_FAKE) {
|
---|
2020 | is_fake = 1;
|
---|
2021 | er->rc=E_FOUND;
|
---|
2022 | }
|
---|
2023 |
|
---|
2024 | #ifdef CS_WITH_DOUBLECHECK
|
---|
2025 | if (cfg.double_check && er->rc < E_NOTFOUND) {
|
---|
2026 | if (er->checked == 0) {//First CW, save it and wait for next one
|
---|
2027 | er->checked = 1;
|
---|
2028 | er->origin_reader = er->selected_reader;
|
---|
2029 | memcpy(er->cw_checked, er->cw, sizeof(er->cw));
|
---|
2030 | cs_log("DOUBLE CHECK FIRST CW by %s idx %d cpti %d", er->origin_reader->label, er->idx, er->cpti);
|
---|
2031 | }
|
---|
2032 | else if (er->origin_reader != er->selected_reader) { //Second (or third and so on) cw. We have to compare
|
---|
2033 | if (memcmp(er->cw_checked, er->cw, sizeof(er->cw)) == 0) {
|
---|
2034 | er->checked++;
|
---|
2035 | cs_log("DOUBLE CHECKED! %d. CW by %s idx %d cpti %d", er->checked, er->selected_reader->label, er->idx, er->cpti);
|
---|
2036 | }
|
---|
2037 | else {
|
---|
2038 | cs_log("DOUBLE CHECKED NONMATCHING! %d. CW by %s idx %d cpti %d", er->checked, er->selected_reader->label, er->idx, er->cpti);
|
---|
2039 | }
|
---|
2040 | }
|
---|
2041 |
|
---|
2042 | if (er->checked < 2) { //less as two same cw? mark as pending!
|
---|
2043 | er->rc = E_UNHANDLED;
|
---|
2044 | return 0;
|
---|
2045 | }
|
---|
2046 |
|
---|
2047 | store_cw_in_cache(er, er->selected_reader->grp, E_FOUND); //Store in cache!
|
---|
2048 |
|
---|
2049 | }
|
---|
2050 | #endif
|
---|
2051 |
|
---|
2052 | ph[client->ctyp].send_dcw(client, er);
|
---|
2053 |
|
---|
2054 | if (is_fake)
|
---|
2055 | er->rc = E_FAKE;
|
---|
2056 |
|
---|
2057 | cs_log("%s (%04X&%06X/%04X/%02X:%04X): %s (%d ms)%s (of %d avail %d)%s%s",
|
---|
2058 | uname, er->caid, er->prid, er->srvid, er->l, lc,
|
---|
2059 | er->rcEx?erEx:stxt[er->rc], client->cwlastresptime, sby, er->reader_count, er->reader_avail, schaninfo, sreason);
|
---|
2060 |
|
---|
2061 | cs_ddump_mask (D_ATR, er->cw, 16, "cw:");
|
---|
2062 |
|
---|
2063 | #ifdef QBOXHD_LED
|
---|
2064 | if (er->rc < E_NOTFOUND) {
|
---|
2065 | qboxhd_led_blink(QBOXHD_LED_COLOR_GREEN, QBOXHD_LED_BLINK_MEDIUM);
|
---|
2066 | } else if (er->rc <= E_STOPPED) {
|
---|
2067 | qboxhd_led_blink(QBOXHD_LED_COLOR_RED, QBOXHD_LED_BLINK_MEDIUM);
|
---|
2068 | }
|
---|
2069 | #endif
|
---|
2070 |
|
---|
2071 | return 0;
|
---|
2072 | }
|
---|
2073 |
|
---|
2074 | void chk_dcw(struct s_client *cl, ECM_REQUEST *er)
|
---|
2075 | {
|
---|
2076 | if (!cl || !cl->ecmtask)
|
---|
2077 | return;
|
---|
2078 |
|
---|
2079 | ECM_REQUEST *ert;
|
---|
2080 |
|
---|
2081 | //cs_log("dcw check from reader %s for idx %d (rc=%d)", er->selected_reader->label, er->cpti, er->rc);
|
---|
2082 | ert=&cl->ecmtask[er->cpti];
|
---|
2083 | if (ert->rc<E_99) {
|
---|
2084 | //cs_debug_mask(D_TRACE, "chk_dcw: already done rc=%d %s", er->rc, er->selected_reader->label);
|
---|
2085 | send_reader_stat(er->selected_reader, er, (er->rc <= E_RDR_NOTFOUND)?E_NOTFOUND:E_FOUND);
|
---|
2086 | return; // already done
|
---|
2087 | }
|
---|
2088 | if( (er->caid!=ert->caid && er->ocaid!=ert->ocaid) || memcmp(er->ecmd5, ert->ecmd5, sizeof(er->ecmd5)) ) {
|
---|
2089 | //cs_debug_mask(D_TRACE, "OBSOLETE? %04X / %04X", er->caid, ert->caid);
|
---|
2090 | return; // obsolete
|
---|
2091 | }
|
---|
2092 |
|
---|
2093 | ert->selected_reader=er->selected_reader;
|
---|
2094 |
|
---|
2095 | if (er->rc>=E_RDR_FOUND)
|
---|
2096 | {
|
---|
2097 | if (er->rc == E_CACHE2 || er->rc == E_EMU)
|
---|
2098 | ert->rc=er->rc;
|
---|
2099 | else
|
---|
2100 | ert->rc=E_FOUND;
|
---|
2101 |
|
---|
2102 | ert->rcEx=0;
|
---|
2103 | memcpy(ert->cw , er->cw , sizeof(er->cw));
|
---|
2104 | } else { // not found (from ONE of the readers !) er->rc==E_RDR_NOTFOUND
|
---|
2105 | ert->rcEx=er->rcEx;
|
---|
2106 | cs_strncpy(ert->msglog, er->msglog, sizeof(ert->msglog));
|
---|
2107 |
|
---|
2108 | ll_remove(ert->matching_rdr, er->selected_reader);
|
---|
2109 |
|
---|
2110 | if (ll_has_elements(ert->matching_rdr)) {//we have still another chance
|
---|
2111 | ert->selected_reader=NULL;
|
---|
2112 | ert=NULL;
|
---|
2113 | }
|
---|
2114 | //at this point ert is only not NULL when it has no matching readers...
|
---|
2115 | if (ert) {
|
---|
2116 | ert->rc=E_NOTFOUND; //so we set the return code
|
---|
2117 | store_cw_in_cache(ert, er->selected_reader->grp, E_NOTFOUND);
|
---|
2118 | }
|
---|
2119 | else send_reader_stat(er->selected_reader, er, E_NOTFOUND);
|
---|
2120 | }
|
---|
2121 | if (ert) {
|
---|
2122 | send_dcw(cl, ert);
|
---|
2123 |
|
---|
2124 | distribute_ecm(er, ert->selected_reader->grp);
|
---|
2125 | }
|
---|
2126 | return;
|
---|
2127 | }
|
---|
2128 |
|
---|
2129 | ulong chk_provid(uchar *ecm, ushort caid) {
|
---|
2130 | int i, len, descriptor_length = 0;
|
---|
2131 | ulong provid = 0;
|
---|
2132 |
|
---|
2133 | switch(caid >> 8) {
|
---|
2134 | case 0x01:
|
---|
2135 | // seca
|
---|
2136 | provid = b2i(2, ecm+3);
|
---|
2137 | break;
|
---|
2138 |
|
---|
2139 | case 0x05:
|
---|
2140 | // viaccess
|
---|
2141 | i = (ecm[4] == 0xD2) ? ecm[5]+2 : 0; // skip d2 nano
|
---|
2142 | if((ecm[5+i] == 3) && ((ecm[4+i] == 0x90) || (ecm[4+i] == 0x40)))
|
---|
2143 | provid = (b2i(3, ecm+6+i) & 0xFFFFF0);
|
---|
2144 |
|
---|
2145 | i = (ecm[6] == 0xD2) ? ecm[7]+2 : 0; // skip d2 nano long ecm
|
---|
2146 | if((ecm[7+i] == 7) && ((ecm[6+i] == 0x90) || (ecm[6+i] == 0x40)))
|
---|
2147 | provid = (b2i(3, ecm+8+i) & 0xFFFFF0);
|
---|
2148 |
|
---|
2149 | break;
|
---|
2150 |
|
---|
2151 | case 0x0D:
|
---|
2152 | // cryptoworks
|
---|
2153 | len = (((ecm[1] & 0xf) << 8) | ecm[2])+3;
|
---|
2154 | for(i=8; i<len; i+=descriptor_length+2) {
|
---|
2155 | descriptor_length = ecm[i+1];
|
---|
2156 | if (ecm[i] == 0x83) {
|
---|
2157 | provid = (ulong)ecm[i+2] & 0xFE;
|
---|
2158 | break;
|
---|
2159 | }
|
---|
2160 | }
|
---|
2161 | break;
|
---|
2162 | }
|
---|
2163 | return(provid);
|
---|
2164 | }
|
---|
2165 |
|
---|
2166 | #ifdef IRDETO_GUESSING
|
---|
2167 | void guess_irdeto(ECM_REQUEST *er)
|
---|
2168 | {
|
---|
2169 | uchar b3;
|
---|
2170 | int b47;
|
---|
2171 | //ushort chid;
|
---|
2172 | struct s_irdeto_quess *ptr;
|
---|
2173 |
|
---|
2174 | b3 = er->ecm[3];
|
---|
2175 | ptr = cfg.itab[b3];
|
---|
2176 | if( !ptr ) {
|
---|
2177 | cs_debug_mask(D_TRACE, "unknown irdeto byte 3: %02X", b3);
|
---|
2178 | return;
|
---|
2179 | }
|
---|
2180 | b47 = b2i(4, er->ecm+4);
|
---|
2181 | //chid = b2i(2, er->ecm+6);
|
---|
2182 | //cs_debug_mask(D_TRACE, "ecm: b47=%08X, ptr->b47=%08X, ptr->caid=%04X", b47, ptr->b47, ptr->caid);
|
---|
2183 | while( ptr )
|
---|
2184 | {
|
---|
2185 | if( b47==ptr->b47 )
|
---|
2186 | {
|
---|
2187 | if( er->srvid && (er->srvid!=ptr->sid) )
|
---|
2188 | {
|
---|
2189 | cs_debug_mask(D_TRACE, "sid mismatched (ecm: %04X, guess: %04X), wrong oscam.ird file?",
|
---|
2190 | er->srvid, ptr->sid);
|
---|
2191 | return;
|
---|
2192 | }
|
---|
2193 | er->caid=ptr->caid;
|
---|
2194 | er->srvid=ptr->sid;
|
---|
2195 | er->chid=(ushort)ptr->b47;
|
---|
2196 | // cs_debug_mask(D_TRACE, "quess_irdeto() found caid=%04X, sid=%04X, chid=%04X",
|
---|
2197 | // er->caid, er->srvid, er->chid);
|
---|
2198 | return;
|
---|
2199 | }
|
---|
2200 | ptr=ptr->next;
|
---|
2201 | }
|
---|
2202 | }
|
---|
2203 | #endif
|
---|
2204 |
|
---|
2205 | void cs_betatunnel(ECM_REQUEST *er)
|
---|
2206 | {
|
---|
2207 | int n;
|
---|
2208 | struct s_client *cl = cur_client();
|
---|
2209 | ulong mask_all = 0xFFFF;
|
---|
2210 | uchar headerN3[10] = {0xc7, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x87, 0x12};
|
---|
2211 | uchar headerN2[10] = {0xc9, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x48, 0x12};
|
---|
2212 | TUNTAB *ttab;
|
---|
2213 | ttab = &cl->ttab;
|
---|
2214 |
|
---|
2215 | for (n = 0; (n < CS_MAXTUNTAB); n++) {
|
---|
2216 | if ((er->caid==ttab->bt_caidfrom[n]) && ((er->srvid==ttab->bt_srvid[n]) || (ttab->bt_srvid[n])==mask_all)) {
|
---|
2217 |
|
---|
2218 | er->caid = ttab->bt_caidto[n];
|
---|
2219 | er->prid = 0;
|
---|
2220 | er->l = er->ecm[2] + 3;
|
---|
2221 |
|
---|
2222 | memmove(er->ecm + 13, er->ecm + 3, er->l - 3);
|
---|
2223 |
|
---|
2224 | if (er->l > 0x88) {
|
---|
2225 | memcpy(er->ecm + 3, headerN3, 10);
|
---|
2226 |
|
---|
2227 | if (er->ecm[0] == 0x81)
|
---|
2228 | er->ecm[12] += 1;
|
---|
2229 |
|
---|
2230 | er->ecm[1]=0x70;
|
---|
2231 | }
|
---|
2232 | else
|
---|
2233 | memcpy(er->ecm + 3, headerN2, 10);
|
---|
2234 |
|
---|
2235 | er->l += 10;
|
---|
2236 | er->ecm[2] = er->l - 3;
|
---|
2237 | er->btun = 1;
|
---|
2238 |
|
---|
2239 | cl->cwtun++;
|
---|
2240 | cl->account->cwtun++;
|
---|
2241 | first_client->cwtun++;
|
---|
2242 |
|
---|
2243 | cs_debug_mask(D_TRACE, "ECM converted from: 0x%X to BetaCrypt: 0x%X for service id:0x%X",
|
---|
2244 | ttab->bt_caidfrom[n], ttab->bt_caidto[n], ttab->bt_srvid[n]);
|
---|
2245 | }
|
---|
2246 | }
|
---|
2247 | }
|
---|
2248 |
|
---|
2249 | static void guess_cardsystem(ECM_REQUEST *er)
|
---|
2250 | {
|
---|
2251 | ushort last_hope=0;
|
---|
2252 |
|
---|
2253 | // viaccess - check by provid-search
|
---|
2254 | if( (er->prid=chk_provid(er->ecm, 0x500)) )
|
---|
2255 | er->caid=0x500;
|
---|
2256 |
|
---|
2257 | // nagra
|
---|
2258 | // is ecm[1] always 0x30 ?
|
---|
2259 | // is ecm[3] always 0x07 ?
|
---|
2260 | if ((er->ecm[6]==1) && (er->ecm[4]==er->ecm[2]-2))
|
---|
2261 | er->caid=0x1801;
|
---|
2262 |
|
---|
2263 | // seca2 - very poor
|
---|
2264 | if ((er->ecm[8]==0x10) && ((er->ecm[9]&0xF1)==1))
|
---|
2265 | last_hope=0x100;
|
---|
2266 |
|
---|
2267 | // is cryptoworks, but which caid ?
|
---|
2268 | if ((er->ecm[3]==0x81) && (er->ecm[4]==0xFF) &&
|
---|
2269 | (!er->ecm[5]) && (!er->ecm[6]) && (er->ecm[7]==er->ecm[2]-5))
|
---|
2270 | last_hope=0xd00;
|
---|
2271 |
|
---|
2272 | #ifdef IRDETO_GUESSING
|
---|
2273 | if (!er->caid && er->ecm[2]==0x31 && er->ecm[0x0b]==0x28)
|
---|
2274 | guess_irdeto(er);
|
---|
2275 | #endif
|
---|
2276 |
|
---|
2277 | if (!er->caid) // guess by len ..
|
---|
2278 | er->caid=len4caid[er->ecm[2]+3];
|
---|
2279 |
|
---|
2280 | if (!er->caid)
|
---|
2281 | er->caid=last_hope;
|
---|
2282 | }
|
---|
2283 |
|
---|
2284 | /**
|
---|
2285 | * sends the ecm request to the readers
|
---|
2286 | * ECM_REQUEST er : the ecm
|
---|
2287 | * int flag : 0=primary readers (no fallback)
|
---|
2288 | * 1=all readers (primary+fallback)
|
---|
2289 | * int reader_types : 0=all readsers
|
---|
2290 | * 1=Hardware/local readers
|
---|
2291 | * 2=Proxy/network readers
|
---|
2292 | **/
|
---|
2293 | void request_cw(ECM_REQUEST *er, int flag, int reader_types)
|
---|
2294 | {
|
---|
2295 | if ((reader_types == 0) || (reader_types == 2))
|
---|
2296 | er->level=flag;
|
---|
2297 | struct s_reader *rdr;
|
---|
2298 |
|
---|
2299 | ushort lc=0, *lp;
|
---|
2300 | if (cs_dblevel) {
|
---|
2301 | for (lp=(ushort *)er->ecm+(er->l>>2), lc=0; lp>=(ushort *)er->ecm; lp--)
|
---|
2302 | lc^=*lp;
|
---|
2303 | }
|
---|
2304 |
|
---|
2305 | LL_NODE *ptr;
|
---|
2306 | for (ptr = er->matching_rdr->initial; ptr; ptr = ptr->nxt) {
|
---|
2307 | if (!flag && ptr == er->fallback)
|
---|
2308 | break;
|
---|
2309 |
|
---|
2310 | rdr = (struct s_reader*)ptr->obj;
|
---|
2311 |
|
---|
2312 | int status = 0;
|
---|
2313 | //reader_types:
|
---|
2314 | //0 = network and local cards
|
---|
2315 | //1 = only local cards
|
---|
2316 | //2 = only network
|
---|
2317 | if ((reader_types == 0) || ((reader_types == 1) && (!(rdr->typ & R_IS_NETWORK))) || ((reader_types == 2) && (rdr->typ & R_IS_NETWORK))) {
|
---|
2318 | cs_debug_mask(D_TRACE, "request_cw%i to reader %s fd=%d ecm=%04X", reader_types+1, rdr->label, rdr->fd, lc);
|
---|
2319 | status = write_ecm_request(rdr->fd, er);
|
---|
2320 | }
|
---|
2321 | if (status == -1) {
|
---|
2322 | cs_log("request_cw() failed on reader %s errno=%d, %s", rdr->label, errno, strerror(errno));
|
---|
2323 | if (rdr->fd) {
|
---|
2324 | rdr->fd_error++;
|
---|
2325 | if (rdr->fd_error > 5) {
|
---|
2326 | rdr->fd_error = 0;
|
---|
2327 | cs_log("Restarting reader %s", rdr->label);
|
---|
2328 | restart_cardreader(rdr, 1); //Schlocke: This restarts the reader!
|
---|
2329 | }
|
---|
2330 | }
|
---|
2331 | }
|
---|
2332 | else
|
---|
2333 | rdr->fd_error = 0;
|
---|
2334 | }
|
---|
2335 | }
|
---|
2336 |
|
---|
2337 | void get_cw(struct s_client * client, ECM_REQUEST *er)
|
---|
2338 | {
|
---|
2339 | int i, j, m;
|
---|
2340 | time_t now = time((time_t)0);
|
---|
2341 |
|
---|
2342 | client->lastecm = now;
|
---|
2343 |
|
---|
2344 | if (!er->caid)
|
---|
2345 | guess_cardsystem(er);
|
---|
2346 |
|
---|
2347 | /* Quickfix Area */
|
---|
2348 |
|
---|
2349 | if( (er->caid & 0xFF00) == 0x600 && !er->chid )
|
---|
2350 | er->chid = (er->ecm[6]<<8)|er->ecm[7];
|
---|
2351 |
|
---|
2352 | // quickfix for 0100:000065
|
---|
2353 | if (er->caid == 0x100 && er->prid == 0x65 && er->srvid == 0)
|
---|
2354 | er->srvid = 0x0642;
|
---|
2355 |
|
---|
2356 | // Quickfixes for Opticum/Globo HD9500
|
---|
2357 | // Quickfix for 0500:030300
|
---|
2358 | if (er->caid == 0x500 && er->prid == 0x030300)
|
---|
2359 | er->prid = 0x030600;
|
---|
2360 |
|
---|
2361 | // Quickfix for 0500:D20200
|
---|
2362 | if (er->caid == 0x500 && er->prid == 0xD20200)
|
---|
2363 | er->prid = 0x030600;
|
---|
2364 |
|
---|
2365 | //betacrypt ecm with nagra header
|
---|
2366 | if (er->caid == 0x1702 && er->l == 0x89 && er->ecm[3] == 0x07 && er->ecm[4] == 0x84)
|
---|
2367 | er->caid = 0x1833;
|
---|
2368 |
|
---|
2369 | /* END quickfixes */
|
---|
2370 |
|
---|
2371 | if (!er->prid)
|
---|
2372 | er->prid = chk_provid(er->ecm, er->caid);
|
---|
2373 |
|
---|
2374 | // Set providerid for newcamd clients if none is given
|
---|
2375 | if( (!er->prid) && client->ncd_server ) {
|
---|
2376 | int pi = client->port_idx;
|
---|
2377 | if( pi >= 0 && cfg.ncd_ptab.nports && cfg.ncd_ptab.nports >= pi )
|
---|
2378 | er->prid = cfg.ncd_ptab.ports[pi].ftab.filts[0].prids[0];
|
---|
2379 | }
|
---|
2380 |
|
---|
2381 | // CAID not supported or found
|
---|
2382 | if (!er->caid) {
|
---|
2383 | er->rc = E_INVALID;
|
---|
2384 | er->rcEx = E2_CAID;
|
---|
2385 | snprintf( er->msglog, MSGLOGSIZE, "CAID not supported or found" );
|
---|
2386 | }
|
---|
2387 |
|
---|
2388 | // user expired
|
---|
2389 | if(client->expirationdate && client->expirationdate < client->lastecm)
|
---|
2390 | er->rc = E_EXPDATE;
|
---|
2391 |
|
---|
2392 | // out of timeframe
|
---|
2393 | if(client->allowedtimeframe[0] && client->allowedtimeframe[1]) {
|
---|
2394 | struct tm acttm;
|
---|
2395 | localtime_r(&now, &acttm);
|
---|
2396 | int curtime = (acttm.tm_hour * 60) + acttm.tm_min;
|
---|
2397 | int mintime = client->allowedtimeframe[0];
|
---|
2398 | int maxtime = client->allowedtimeframe[1];
|
---|
2399 | if(!((mintime <= maxtime && curtime > mintime && curtime < maxtime) || (mintime > maxtime && (curtime > mintime || curtime < maxtime)))) {
|
---|
2400 | er->rc = E_EXPDATE;
|
---|
2401 | }
|
---|
2402 | cs_debug_mask(D_TRACE, "Check Timeframe - result: %d, start: %d, current: %d, end: %d\n",er->rc, mintime, curtime, maxtime);
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | // user disabled
|
---|
2406 | if(client->disabled != 0) {
|
---|
2407 | if (client->failban & BAN_DISABLED){
|
---|
2408 | cs_add_violation(client->ip);
|
---|
2409 | cs_exit(SIGQUIT); // don't know whether this is best way to kill the thread
|
---|
2410 | }
|
---|
2411 | er->rc = E_DISABLED;
|
---|
2412 | }
|
---|
2413 |
|
---|
2414 |
|
---|
2415 | // rc<100 -> ecm error
|
---|
2416 | if (er->rc >= E_UNHANDLED) {
|
---|
2417 |
|
---|
2418 | m = er->caid;
|
---|
2419 | er->ocaid = er->caid;
|
---|
2420 | i = er->srvid;
|
---|
2421 |
|
---|
2422 | if ((i != client->last_srvid) || (!client->lastswitch)) {
|
---|
2423 | if(cfg.usrfileflag)
|
---|
2424 | cs_statistics(client);
|
---|
2425 | client->lastswitch = now;
|
---|
2426 | }
|
---|
2427 |
|
---|
2428 | // user sleeping
|
---|
2429 | if ((client->tosleep) && (now - client->lastswitch > client->tosleep)) {
|
---|
2430 |
|
---|
2431 | if (client->failban & BAN_SLEEPING) {
|
---|
2432 | cs_add_violation(client->ip);
|
---|
2433 | cs_exit(SIGQUIT); // todo don't know whether this is best way to kill the thread
|
---|
2434 | }
|
---|
2435 |
|
---|
2436 | if (client->c35_sleepsend != 0) {
|
---|
2437 | er->rc = E_STOPPED; // send stop command CMD08 {00 xx}
|
---|
2438 | } else {
|
---|
2439 | er->rc = E_SLEEPING;
|
---|
2440 | }
|
---|
2441 | }
|
---|
2442 |
|
---|
2443 | client->last_srvid = i;
|
---|
2444 | client->last_caid = m;
|
---|
2445 |
|
---|
2446 | int ecm_len = (((er->ecm[1] & 0x0F) << 8) | er->ecm[2]) + 3;
|
---|
2447 |
|
---|
2448 | for (j = 0; (j < 6) && (er->rc >= E_UNHANDLED); j++)
|
---|
2449 | {
|
---|
2450 | switch(j) {
|
---|
2451 |
|
---|
2452 | case 0:
|
---|
2453 | // fake (uniq)
|
---|
2454 | if (client->dup)
|
---|
2455 | er->rc = E_FAKE;
|
---|
2456 | break;
|
---|
2457 |
|
---|
2458 | case 1:
|
---|
2459 | // invalid (caid)
|
---|
2460 | if (!chk_bcaid(er, &client->ctab)) {
|
---|
2461 | er->rc = E_INVALID;
|
---|
2462 | er->rcEx = E2_CAID;
|
---|
2463 | snprintf( er->msglog, MSGLOGSIZE, "invalid caid %x",er->caid );
|
---|
2464 | }
|
---|
2465 | break;
|
---|
2466 |
|
---|
2467 | case 2:
|
---|
2468 | // invalid (srvid)
|
---|
2469 | if (!chk_srvid(client, er))
|
---|
2470 | {
|
---|
2471 | er->rc = E_INVALID;
|
---|
2472 | snprintf( er->msglog, MSGLOGSIZE, "invalid SID" );
|
---|
2473 | }
|
---|
2474 |
|
---|
2475 | break;
|
---|
2476 |
|
---|
2477 | case 3:
|
---|
2478 | // invalid (ufilters)
|
---|
2479 | if (!chk_ufilters(er))
|
---|
2480 | er->rc = E_INVALID;
|
---|
2481 | break;
|
---|
2482 |
|
---|
2483 | case 4:
|
---|
2484 | // invalid (sfilter)
|
---|
2485 | if (!chk_sfilter(er, ph[client->ctyp].ptab))
|
---|
2486 | er->rc = E_INVALID;
|
---|
2487 | break;
|
---|
2488 |
|
---|
2489 | case 5:
|
---|
2490 | // corrupt
|
---|
2491 | if( (i = er->l - ecm_len) ) {
|
---|
2492 | if (i > 0) {
|
---|
2493 | cs_debug_mask(D_TRACE, "warning: ecm size adjusted from 0x%X to 0x%X", er->l, ecm_len);
|
---|
2494 | er->l = ecm_len;
|
---|
2495 | }
|
---|
2496 | else
|
---|
2497 | er->rc = E_CORRUPT;
|
---|
2498 | }
|
---|
2499 | break;
|
---|
2500 | }
|
---|
2501 | }
|
---|
2502 | }
|
---|
2503 |
|
---|
2504 | //Use locking - now default=FALSE, activate on problems!
|
---|
2505 | int locked;
|
---|
2506 | if (cfg.lb_mode && cfg.lb_use_locking) {
|
---|
2507 | pthread_mutex_lock(&get_cw_lock);
|
---|
2508 | locked=1;
|
---|
2509 | }
|
---|
2510 | else
|
---|
2511 | locked=0;
|
---|
2512 |
|
---|
2513 |
|
---|
2514 | //Schlocke: above checks could change er->rc so
|
---|
2515 | if (er->rc >= E_UNHANDLED) {
|
---|
2516 | /*BetaCrypt tunneling
|
---|
2517 | *moved behind the check routines,
|
---|
2518 | *because newcamd ECM will fail
|
---|
2519 | *if ECM is converted before
|
---|
2520 | */
|
---|
2521 | if (&client->ttab)
|
---|
2522 | cs_betatunnel(er);
|
---|
2523 |
|
---|
2524 | // ignore ecm ...
|
---|
2525 | int offset = 3;
|
---|
2526 | // ... and betacrypt header for cache md5 calculation
|
---|
2527 | if ((er->caid >> 8) == 0x17)
|
---|
2528 | offset = 13;
|
---|
2529 |
|
---|
2530 | // store ECM in cache
|
---|
2531 | memcpy(er->ecmd5, MD5(er->ecm+offset, er->l-offset, client->dump), CS_ECMSTORESIZE);
|
---|
2532 |
|
---|
2533 | // cache1
|
---|
2534 | //cache check now done by check_and_store_ecmcache() !!
|
---|
2535 | //if (check_cwcache1(er, client->grp))
|
---|
2536 | // er->rc = E_CACHE1;
|
---|
2537 |
|
---|
2538 | #ifdef CS_ANTICASC
|
---|
2539 | ac_chk(client, er, 0);
|
---|
2540 | #endif
|
---|
2541 | }
|
---|
2542 |
|
---|
2543 | if(er->rc >= E_99) {
|
---|
2544 | er->reader_avail=0;
|
---|
2545 | struct s_reader *rdr;
|
---|
2546 | for (rdr=first_active_reader; rdr ; rdr=rdr->next) {
|
---|
2547 | if (matching_reader(er, rdr)) {
|
---|
2548 | if (rdr->fallback) {
|
---|
2549 | if (er->fallback == NULL) //first fallbackreader to be added
|
---|
2550 | er->fallback=ll_append(er->matching_rdr, rdr);
|
---|
2551 | else
|
---|
2552 | ll_append(er->matching_rdr, rdr);
|
---|
2553 |
|
---|
2554 | }
|
---|
2555 | else {
|
---|
2556 | ll_prepend(er->matching_rdr, rdr);
|
---|
2557 | }
|
---|
2558 | if (cfg.lb_mode || !rdr->fallback)
|
---|
2559 | er->reader_avail++;
|
---|
2560 | }
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 | if (cfg.lb_mode && er->reader_avail) {
|
---|
2564 | cs_debug_mask(D_TRACE, "requesting client %s best reader for %04X/%06X/%04X",
|
---|
2565 | username(client), er->caid, er->prid, er->srvid);
|
---|
2566 | get_best_reader(er);
|
---|
2567 | }
|
---|
2568 |
|
---|
2569 | LL_NODE *ptr;
|
---|
2570 | for (ptr = er->matching_rdr->initial; ptr && ptr != er->fallback; ptr = ptr->nxt)
|
---|
2571 | er->reader_count++;
|
---|
2572 |
|
---|
2573 | if (!ll_has_elements(er->matching_rdr)) { //no reader -> not found
|
---|
2574 | er->rc = E_NOTFOUND;
|
---|
2575 | if (!er->rcEx)
|
---|
2576 | er->rcEx = E2_GROUP;
|
---|
2577 | snprintf(er->msglog, MSGLOGSIZE, "no matching reader");
|
---|
2578 | }
|
---|
2579 | else
|
---|
2580 | if (er->matching_rdr->initial == er->fallback) { //fallbacks only
|
---|
2581 | er->fallback = NULL; //switch them
|
---|
2582 | }
|
---|
2583 |
|
---|
2584 | //we have to go through matching_reader() to check services!
|
---|
2585 | if (er->rc == E_UNHANDLED)
|
---|
2586 | er->rc = check_and_store_ecmcache(er, client->grp);
|
---|
2587 | }
|
---|
2588 |
|
---|
2589 | if (locked)
|
---|
2590 | pthread_mutex_unlock(&get_cw_lock);
|
---|
2591 |
|
---|
2592 | if (er->rc == E_99)
|
---|
2593 | return; //ECM already requested / found in ECM cache
|
---|
2594 |
|
---|
2595 | if (er->rc < E_UNHANDLED) {
|
---|
2596 | if (cfg.delay)
|
---|
2597 | cs_sleepms(cfg.delay);
|
---|
2598 |
|
---|
2599 | send_dcw(client, er);
|
---|
2600 | return;
|
---|
2601 | }
|
---|
2602 |
|
---|
2603 | er->rcEx = 0;
|
---|
2604 | request_cw(er, 0, cfg.preferlocalcards ? 1 : 0);
|
---|
2605 | }
|
---|
2606 |
|
---|
2607 | void log_emm_request(struct s_reader *rdr)
|
---|
2608 | {
|
---|
2609 | cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06lX)",
|
---|
2610 | username(cur_client()), rdr->label, rdr->caid,
|
---|
2611 | rdr->auprovid ? rdr->auprovid : b2i(4, rdr->prid[0]));
|
---|
2612 | }
|
---|
2613 |
|
---|
2614 | void do_emm(struct s_client * client, EMM_PACKET *ep)
|
---|
2615 | {
|
---|
2616 | char *typtext[]={"unknown", "unique", "shared", "global"};
|
---|
2617 |
|
---|
2618 | struct s_reader *aureader = NULL;
|
---|
2619 | cs_ddump_mask(D_EMM, ep->emm, ep->l, "emm:");
|
---|
2620 |
|
---|
2621 | LL_ITER *itr = ll_iter_create(client->aureader_list);
|
---|
2622 | while ((aureader = ll_iter_next(itr))) {
|
---|
2623 | if (!aureader->enable)
|
---|
2624 | continue;
|
---|
2625 |
|
---|
2626 | ushort caid = b2i(2, ep->caid);
|
---|
2627 | ulong provid = b2i(4, ep->provid);
|
---|
2628 |
|
---|
2629 | if (aureader->audisabled) {
|
---|
2630 | cs_debug_mask(D_EMM, "AU is disabled for reader %s", aureader->label);
|
---|
2631 | continue;
|
---|
2632 | }
|
---|
2633 |
|
---|
2634 | if (!(aureader->grp & client->grp)) {
|
---|
2635 | cs_debug_mask(D_EMM, "skip emm reader %s group mismatch", aureader->label);
|
---|
2636 | continue;
|
---|
2637 | }
|
---|
2638 |
|
---|
2639 | //TODO: provider possibly not set yet, this is done in get_emm_type()
|
---|
2640 | if (!emm_reader_match(aureader, caid, provid))
|
---|
2641 | continue;
|
---|
2642 |
|
---|
2643 | struct s_cardsystem *cs = NULL;
|
---|
2644 |
|
---|
2645 | if (aureader->typ & R_IS_CASCADING) { // network reader (R_CAMD35 R_NEWCAMD R_CS378X R_CCCAM)
|
---|
2646 | if (!aureader->ph.c_send_emm) // no emm support
|
---|
2647 | continue;
|
---|
2648 |
|
---|
2649 | cs = get_cardsystem_by_caid(caid);
|
---|
2650 | if (!cs) {
|
---|
2651 | cs_debug_mask(D_EMM, "unable to find cardsystem for caid %04X, reader %s", caid, aureader->label);
|
---|
2652 | continue;
|
---|
2653 | }
|
---|
2654 | } else { // local reader
|
---|
2655 | if (aureader->csystem.active)
|
---|
2656 | cs=&aureader->csystem;
|
---|
2657 | }
|
---|
2658 |
|
---|
2659 | if (cs && cs->get_emm_type) {
|
---|
2660 | if(!cs->get_emm_type(ep, aureader)) {
|
---|
2661 | cs_debug_mask(D_EMM, "emm skipped, get_emm_type() returns error, reader %s", aureader->label);
|
---|
2662 | client->emmnok++;
|
---|
2663 | if (client->account)
|
---|
2664 | client->account->emmnok++;
|
---|
2665 | first_client->emmnok++;
|
---|
2666 | continue;
|
---|
2667 | }
|
---|
2668 | }
|
---|
2669 |
|
---|
2670 | cs_debug_mask(D_EMM, "emmtype %s. Reader %s has serial %s.", typtext[ep->type], aureader->label, cs_hexdump(0, aureader->hexserial, 8));
|
---|
2671 | cs_ddump_mask(D_EMM, ep->hexserial, 8, "emm UA/SA:");
|
---|
2672 |
|
---|
2673 | client->last=time((time_t)0);
|
---|
2674 | if (aureader->b_nano[ep->emm[0]] & 0x02) { //should this nano be saved?
|
---|
2675 | char token[256];
|
---|
2676 | FILE *fp;
|
---|
2677 | time_t rawtime;
|
---|
2678 | time (&rawtime);
|
---|
2679 | struct tm timeinfo;
|
---|
2680 | localtime_r (&rawtime, &timeinfo); /* to access LOCAL date/time info */
|
---|
2681 | char buf[80];
|
---|
2682 | strftime (buf, 80, "%Y/%m/%d %H:%M:%S", &timeinfo);
|
---|
2683 | snprintf (token, sizeof(token), "%s%s_emm.log", cs_confdir, aureader->label);
|
---|
2684 | int emm_length = ((ep->emm[1] & 0x0f) << 8) | ep->emm[2];
|
---|
2685 |
|
---|
2686 | if (!(fp = fopen (token, "a"))) {
|
---|
2687 | cs_log ("ERROR: Cannot open file '%s' (errno=%d: %s)\n", token, errno, strerror(errno));
|
---|
2688 | } else {
|
---|
2689 | fprintf (fp, "%s %s ", buf, cs_hexdump(0, ep->hexserial, 8));
|
---|
2690 | fprintf (fp, "%s\n", cs_hexdump(0, ep->emm, emm_length + 3));
|
---|
2691 | fclose (fp);
|
---|
2692 | cs_log ("Succesfully added EMM to %s.", token);
|
---|
2693 | }
|
---|
2694 |
|
---|
2695 | snprintf (token, sizeof(token), "%s%s_emm.bin", cs_confdir, aureader->label);
|
---|
2696 | if (!(fp = fopen (token, "ab"))) {
|
---|
2697 | cs_log ("ERROR: Cannot open file '%s' (errno=%d: %s)\n", token, errno, strerror(errno));
|
---|
2698 | } else {
|
---|
2699 | if ((int)fwrite(ep->emm, 1, emm_length+3, fp) == emm_length+3) {
|
---|
2700 | cs_log ("Succesfully added binary EMM to %s.", token);
|
---|
2701 | } else {
|
---|
2702 | cs_log ("ERROR: Cannot write binary EMM to %s (errno=%d: %s)\n", token, errno, strerror(errno));
|
---|
2703 | }
|
---|
2704 | fclose (fp);
|
---|
2705 | }
|
---|
2706 | }
|
---|
2707 |
|
---|
2708 | int is_blocked = 0;
|
---|
2709 | switch (ep->type) {
|
---|
2710 | case UNKNOWN: is_blocked = (aureader->blockemm & EMM_UNKNOWN) ? 1 : 0;
|
---|
2711 | break;
|
---|
2712 | case UNIQUE: is_blocked = (aureader->blockemm & EMM_UNIQUE) ? 1 : 0;
|
---|
2713 | break;
|
---|
2714 | case SHARED: is_blocked = (aureader->blockemm & EMM_SHARED) ? 1 : 0;
|
---|
2715 | break;
|
---|
2716 | case GLOBAL: is_blocked = (aureader->blockemm & EMM_GLOBAL) ? 1 : 0;
|
---|
2717 | break;
|
---|
2718 | }
|
---|
2719 |
|
---|
2720 | if (is_blocked != 0) {
|
---|
2721 | #ifdef WEBIF
|
---|
2722 | aureader->emmblocked[ep->type]++;
|
---|
2723 | is_blocked = aureader->emmblocked[ep->type];
|
---|
2724 | #endif
|
---|
2725 | /* we have to write the log for blocked EMM here because
|
---|
2726 | this EMM never reach the reader module where the rest
|
---|
2727 | of EMM log is done. */
|
---|
2728 | if (aureader->logemm & 0x08) {
|
---|
2729 | cs_log("%s emmtype=%s, len=%d, idx=0, cnt=%d: blocked (0 ms) by %s",
|
---|
2730 | client->account->usr,
|
---|
2731 | typtext[ep->type],
|
---|
2732 | ep->emm[2],
|
---|
2733 | is_blocked,
|
---|
2734 | aureader->label);
|
---|
2735 | }
|
---|
2736 | continue;
|
---|
2737 | }
|
---|
2738 |
|
---|
2739 | client->lastemm = time((time_t)0);
|
---|
2740 |
|
---|
2741 | client->emmok++;
|
---|
2742 | if (client->account)
|
---|
2743 | client->account->emmok++;
|
---|
2744 | first_client->emmok++;
|
---|
2745 |
|
---|
2746 | ep->client = cur_client();
|
---|
2747 | cs_debug_mask(D_EMM, "emm is being sent to reader %s.", aureader->label);
|
---|
2748 | write_to_pipe(aureader->fd, PIP_ID_EMM, (uchar *) ep, sizeof(EMM_PACKET));
|
---|
2749 | }
|
---|
2750 | ll_iter_release(itr);
|
---|
2751 | }
|
---|
2752 |
|
---|
2753 | int comp_timeb(struct timeb *tpa, struct timeb *tpb)
|
---|
2754 | {
|
---|
2755 | if (tpa->time>tpb->time) return(1);
|
---|
2756 | if (tpa->time<tpb->time) return(-1);
|
---|
2757 | if (tpa->millitm>tpb->millitm) return(1);
|
---|
2758 | if (tpa->millitm<tpb->millitm) return(-1);
|
---|
2759 | return(0);
|
---|
2760 | }
|
---|
2761 |
|
---|
2762 | struct timeval *chk_pending(struct timeb tp_ctimeout)
|
---|
2763 | {
|
---|
2764 | int i;
|
---|
2765 | ulong td;
|
---|
2766 | struct timeb tpn, tpe, tpc; // <n>ow, <e>nd, <c>heck
|
---|
2767 |
|
---|
2768 | ECM_REQUEST *er;
|
---|
2769 | cs_ftime(&tpn);
|
---|
2770 | tpe=tp_ctimeout; // latest delay -> disconnect
|
---|
2771 |
|
---|
2772 | struct s_client *cl = cur_client();
|
---|
2773 |
|
---|
2774 | if (cl->ecmtask)
|
---|
2775 | i=(ph[cl->ctyp].multi)?CS_MAXPENDING:1;
|
---|
2776 | else
|
---|
2777 | i=0;
|
---|
2778 |
|
---|
2779 | //cs_log("num pend=%d", i);
|
---|
2780 |
|
---|
2781 | for (--i; i>=0; i--) {
|
---|
2782 |
|
---|
2783 | if (cl->ecmtask[i].rc>=E_99) { // check all pending ecm-requests
|
---|
2784 | int act=1;
|
---|
2785 | er=&cl->ecmtask[i];
|
---|
2786 |
|
---|
2787 | //additional cache check:
|
---|
2788 | if (check_cwcache2(er, cl->grp)) {
|
---|
2789 | //cs_log("found lost entry in cache! %s %04X&%06X/%04X", username(cl), er->caid, er->prid, er->srvid);
|
---|
2790 | er->rc = E_CACHE2;
|
---|
2791 | send_dcw(cl, er);
|
---|
2792 | continue;
|
---|
2793 | }
|
---|
2794 |
|
---|
2795 |
|
---|
2796 | tpc=er->tps;
|
---|
2797 | unsigned int tt;
|
---|
2798 | tt = (er->stage) ? cfg.ctimeout : cfg.ftimeout;
|
---|
2799 | tpc.time +=tt / 1000;
|
---|
2800 | tpc.millitm += tt % 1000;
|
---|
2801 | if (!er->stage && er->rc >= E_UNHANDLED) {
|
---|
2802 |
|
---|
2803 | LL_NODE *ptr;
|
---|
2804 | for (ptr = er->matching_rdr->initial; ptr && ptr != er->fallback; ptr = ptr->nxt)
|
---|
2805 | if (!cfg.preferlocalcards ||
|
---|
2806 | (cfg.preferlocalcards && !er->locals_done && (!(((struct s_reader*)ptr->obj)->typ & R_IS_NETWORK))) ||
|
---|
2807 | (cfg.preferlocalcards && er->locals_done && (((struct s_reader*)ptr->obj)->typ & R_IS_NETWORK)))
|
---|
2808 | act=0;
|
---|
2809 |
|
---|
2810 | //cs_log("stage 0, act=%d r0=%d, r1=%d, r2=%d, r3=%d, r4=%d r5=%d", act,
|
---|
2811 | // er->matching_rdr[0], er->matching_rdr[1], er->matching_rdr[2],
|
---|
2812 | // er->matching_rdr[3], er->matching_rdr[4], er->matching_rdr[5]);
|
---|
2813 |
|
---|
2814 | if (act) {
|
---|
2815 | int inc_stage = 1;
|
---|
2816 | if (cfg.preferlocalcards && !er->locals_done) {
|
---|
2817 | er->locals_done = 1;
|
---|
2818 | struct s_reader *rdr;
|
---|
2819 | for (rdr=first_active_reader; rdr ; rdr=rdr->next)
|
---|
2820 | if (rdr->typ & R_IS_NETWORK)
|
---|
2821 | inc_stage = 0;
|
---|
2822 | }
|
---|
2823 | unsigned int tt;
|
---|
2824 | if (!inc_stage) {
|
---|
2825 | request_cw(er, er->stage, 2);
|
---|
2826 | tt = 1000 * (tpn.time - er->tps.time) + tpn.millitm - er->tps.millitm;
|
---|
2827 | } else {
|
---|
2828 | er->locals_done = 0;
|
---|
2829 | er->stage++;
|
---|
2830 | request_cw(er, er->stage, cfg.preferlocalcards ? 1 : 0);
|
---|
2831 |
|
---|
2832 | tt = (cfg.ctimeout-cfg.ftimeout);
|
---|
2833 | }
|
---|
2834 | tpc.time += tt / 1000;
|
---|
2835 | tpc.millitm += tt % 1000;
|
---|
2836 | }
|
---|
2837 | }
|
---|
2838 | if (comp_timeb(&tpn, &tpc)>0) { // action needed
|
---|
2839 | //cs_log("Action now %d.%03d", tpn.time, tpn.millitm);
|
---|
2840 | //cs_log(" %d.%03d", tpc.time, tpc.millitm);
|
---|
2841 | if (er->stage) {
|
---|
2842 | er->rc = E_TIMEOUT;
|
---|
2843 | if (cfg.lb_mode) {
|
---|
2844 | LL_NODE *ptr;
|
---|
2845 | for (ptr = er->matching_rdr->initial; ptr ; ptr = ptr->nxt)
|
---|
2846 | send_reader_stat((struct s_reader *)ptr->obj, er, E_TIMEOUT);
|
---|
2847 | }
|
---|
2848 | store_cw_in_cache(er, cl->grp, E_TIMEOUT);
|
---|
2849 | send_dcw(cl, er);
|
---|
2850 | continue;
|
---|
2851 | } else {
|
---|
2852 | er->stage++;
|
---|
2853 | cs_debug_mask(D_TRACE, "fallback for %s %04X&%06X/%04X", username(cl), er->caid, er->prid, er->srvid);
|
---|
2854 | if (er->rc >= E_UNHANDLED) //do not request rc=99
|
---|
2855 | request_cw(er, er->stage, 0);
|
---|
2856 | unsigned int tt;
|
---|
2857 | tt = (cfg.ctimeout-cfg.ftimeout);
|
---|
2858 | tpc.time += tt / 1000;
|
---|
2859 | tpc.millitm += tt % 1000;
|
---|
2860 | }
|
---|
2861 | }
|
---|
2862 |
|
---|
2863 | //build_delay(&tpe, &tpc);
|
---|
2864 | if (comp_timeb(&tpe, &tpc)>0) {
|
---|
2865 | tpe.time=tpc.time;
|
---|
2866 | tpe.millitm=tpc.millitm;
|
---|
2867 | }
|
---|
2868 | }
|
---|
2869 | }
|
---|
2870 |
|
---|
2871 | td=(tpe.time-tpn.time)*1000+(tpe.millitm-tpn.millitm)+5;
|
---|
2872 | cl->tv.tv_sec = td/1000;
|
---|
2873 | cl->tv.tv_usec = (td%1000)*1000;
|
---|
2874 | //cs_log("delay %d.%06d", tv.tv_sec, tv.tv_usec);
|
---|
2875 |
|
---|
2876 | return(&cl->tv);
|
---|
2877 | }
|
---|
2878 |
|
---|
2879 | int process_input(uchar *buf, int l, int timeout)
|
---|
2880 | {
|
---|
2881 | int rc;
|
---|
2882 | fd_set fds;
|
---|
2883 | struct timeb tp;
|
---|
2884 |
|
---|
2885 | struct s_client *cl = cur_client();
|
---|
2886 |
|
---|
2887 | cs_ftime(&tp);
|
---|
2888 | tp.time+=timeout;
|
---|
2889 |
|
---|
2890 | while (1) {
|
---|
2891 | FD_ZERO(&fds);
|
---|
2892 |
|
---|
2893 | if (cl->pfd)
|
---|
2894 | FD_SET(cl->pfd, &fds);
|
---|
2895 |
|
---|
2896 | FD_SET(cl->fd_m2c_c, &fds);
|
---|
2897 |
|
---|
2898 | rc=select(((cl->pfd > cl->fd_m2c_c) ? cl->pfd : cl->fd_m2c_c)+1, &fds, 0, 0, chk_pending(tp));
|
---|
2899 | if (rc<0) {
|
---|
2900 | if (errno==EINTR) continue;
|
---|
2901 | else return(0);
|
---|
2902 | }
|
---|
2903 |
|
---|
2904 | if (FD_ISSET(cl->fd_m2c_c, &fds)) { // read from pipe
|
---|
2905 | if (process_client_pipe(cl, buf, l)==PIP_ID_UDP) {
|
---|
2906 | rc=ph[cl->ctyp].recv(cl, buf, l);
|
---|
2907 | break;
|
---|
2908 | }
|
---|
2909 | }
|
---|
2910 |
|
---|
2911 | if (cl->pfd && FD_ISSET(cl->pfd, &fds)) { // read from client
|
---|
2912 | rc=ph[cl->ctyp].recv(cl, buf, l);
|
---|
2913 | break;
|
---|
2914 | }
|
---|
2915 |
|
---|
2916 | if (tp.time<=time((time_t *)0)) { // client maxidle reached
|
---|
2917 | rc=(-9);
|
---|
2918 | break;
|
---|
2919 | }
|
---|
2920 | }
|
---|
2921 | return(rc);
|
---|
2922 | }
|
---|
2923 |
|
---|
2924 | static void restart_clients()
|
---|
2925 | {
|
---|
2926 | cs_log("restarting clients");
|
---|
2927 | struct s_client *cl;
|
---|
2928 | for (cl=first_client->next; cl ; cl=cl->next)
|
---|
2929 | if (cl->typ=='c' && ph[cl->ctyp].type & MOD_CONN_NET) {
|
---|
2930 | kill_thread(cl);
|
---|
2931 | cs_log("killing client c %8X", (unsigned long)(cl->thread));
|
---|
2932 | }
|
---|
2933 | }
|
---|
2934 |
|
---|
2935 |
|
---|
2936 | static void process_master_pipe(int mfdr)
|
---|
2937 | {
|
---|
2938 | int n;
|
---|
2939 | uchar *ptr;
|
---|
2940 |
|
---|
2941 | switch(n=read_from_pipe(mfdr, &ptr, 1))
|
---|
2942 | {
|
---|
2943 | case PIP_ID_KCL: //Kill all clients
|
---|
2944 | restart_clients();
|
---|
2945 | break;
|
---|
2946 | case PIP_ID_ERR:
|
---|
2947 | cs_exit(1); //better than reading from dead pipe!
|
---|
2948 | break;
|
---|
2949 | default:
|
---|
2950 | cs_log("unhandled pipe message %d (master pipe)", n);
|
---|
2951 | break;
|
---|
2952 | }
|
---|
2953 | if (ptr) free(ptr);
|
---|
2954 | }
|
---|
2955 |
|
---|
2956 |
|
---|
2957 | int process_client_pipe(struct s_client *cl, uchar *buf, int l) {
|
---|
2958 | uchar *ptr;
|
---|
2959 | unsigned short n;
|
---|
2960 | int pipeCmd = read_from_pipe(cl->fd_m2c_c, &ptr, 0);
|
---|
2961 |
|
---|
2962 | switch(pipeCmd) {
|
---|
2963 | case PIP_ID_ECM:
|
---|
2964 | chk_dcw(cl, (ECM_REQUEST *)ptr);
|
---|
2965 | break;
|
---|
2966 | case PIP_ID_UDP:
|
---|
2967 | if (ptr[0]!='U') {
|
---|
2968 | cs_log("INTERNAL PIPE-ERROR");
|
---|
2969 | }
|
---|
2970 | memcpy(&n, ptr+1, 2);
|
---|
2971 | if (n+3<=l) {
|
---|
2972 | memcpy(buf, ptr, n+3);
|
---|
2973 | }
|
---|
2974 | break;
|
---|
2975 | case PIP_ID_ERR:
|
---|
2976 | cs_exit(1);
|
---|
2977 | break;
|
---|
2978 | default:
|
---|
2979 | cs_log("unhandled pipe message %d (client %s)", pipeCmd, cl->account->usr);
|
---|
2980 | break;
|
---|
2981 | }
|
---|
2982 | if (ptr) free(ptr);
|
---|
2983 | return pipeCmd;
|
---|
2984 | }
|
---|
2985 |
|
---|
2986 | void cs_log_config()
|
---|
2987 | {
|
---|
2988 | uchar buf[20];
|
---|
2989 |
|
---|
2990 | if (cfg.nice!=99)
|
---|
2991 | snprintf((char *)buf, sizeof(buf), ", nice=%d", cfg.nice);
|
---|
2992 | else
|
---|
2993 | buf[0]='\0';
|
---|
2994 | 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);
|
---|
2995 | cs_log("client max. idle=%d sec, debug level=%d", cfg.cmaxidle, cs_dblevel);
|
---|
2996 |
|
---|
2997 | if( cfg.max_log_size )
|
---|
2998 | snprintf((char *)buf, sizeof(buf), "%d Kb", cfg.max_log_size);
|
---|
2999 | else
|
---|
3000 | cs_strncpy((char *)buf, "unlimited", sizeof(buf));
|
---|
3001 | cs_log("max. logsize=%s", buf);
|
---|
3002 | cs_log("client timeout=%lu ms, fallback timeout=%lu ms, cache delay=%d ms",
|
---|
3003 | cfg.ctimeout, cfg.ftimeout, cfg.delay);
|
---|
3004 | }
|
---|
3005 |
|
---|
3006 | void cs_waitforcardinit()
|
---|
3007 | {
|
---|
3008 | if (cfg.waitforcards)
|
---|
3009 | {
|
---|
3010 | cs_log("waiting for local card init");
|
---|
3011 | int card_init_done;
|
---|
3012 | do {
|
---|
3013 | card_init_done = 1;
|
---|
3014 | struct s_reader *rdr;
|
---|
3015 | for (rdr=first_active_reader; rdr ; rdr=rdr->next)
|
---|
3016 | if (((rdr->typ & R_IS_CASCADING) == 0) && (rdr->card_status == CARD_NEED_INIT || rdr->card_status == UNKNOWN)) {
|
---|
3017 | card_init_done = 0;
|
---|
3018 | break;
|
---|
3019 | }
|
---|
3020 | if (!card_init_done)
|
---|
3021 | cs_sleepms(300); // wait a little bit
|
---|
3022 | //alarm(cfg.cmaxidle + cfg.ctimeout / 1000 + 1);
|
---|
3023 | } while (!card_init_done);
|
---|
3024 | cs_log("init for all local cards done");
|
---|
3025 | }
|
---|
3026 | }
|
---|
3027 |
|
---|
3028 | int accept_connection(int i, int j) {
|
---|
3029 | struct sockaddr_in cad;
|
---|
3030 | int scad,n;
|
---|
3031 | scad = sizeof(cad);
|
---|
3032 | uchar buf[2048];
|
---|
3033 |
|
---|
3034 | if (ph[i].type==MOD_CONN_UDP) {
|
---|
3035 |
|
---|
3036 | if ((n=recvfrom(ph[i].ptab->ports[j].fd, buf+3, sizeof(buf)-3, 0, (struct sockaddr *)&cad, (socklen_t *)&scad))>0) {
|
---|
3037 | struct s_client *cl;
|
---|
3038 | cl=idx_from_ip(cad.sin_addr.s_addr, ntohs(cad.sin_port));
|
---|
3039 |
|
---|
3040 | unsigned short rl;
|
---|
3041 | rl=n;
|
---|
3042 | buf[0]='U';
|
---|
3043 | memcpy(buf+1, &rl, 2);
|
---|
3044 |
|
---|
3045 | if (!cl) {
|
---|
3046 | if (cs_check_violation((uint)cad.sin_addr.s_addr))
|
---|
3047 | return 0;
|
---|
3048 | //printf("IP: %s - %d\n", inet_ntoa(*(struct in_addr *)&cad.sin_addr.s_addr), cad.sin_addr.s_addr);
|
---|
3049 |
|
---|
3050 | cl = create_client(cad.sin_addr.s_addr);
|
---|
3051 | if (!cl) return 0;
|
---|
3052 |
|
---|
3053 | cl->ctyp=i;
|
---|
3054 | cl->port_idx=j;
|
---|
3055 | cl->udp_fd=ph[i].ptab->ports[j].fd;
|
---|
3056 | cl->udp_sa=cad;
|
---|
3057 |
|
---|
3058 | cl->port=ntohs(cad.sin_port);
|
---|
3059 | cl->typ='c';
|
---|
3060 |
|
---|
3061 | write_to_pipe(cl->fd_m2c, PIP_ID_UDP, (uchar*)&buf, n+3);
|
---|
3062 |
|
---|
3063 | pthread_attr_t attr;
|
---|
3064 | pthread_attr_init(&attr);
|
---|
3065 | #ifndef TUXBOX
|
---|
3066 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
3067 | #endif
|
---|
3068 | //We need memory here, because when on stack and we leave the function, stack is overwritten,
|
---|
3069 | //but assigned to the thread
|
---|
3070 | //So alloc memory for the init data and free them in clientthread_init:
|
---|
3071 | struct s_clientinit *init = cs_malloc(&init, sizeof(struct s_clientinit), 0);
|
---|
3072 | init->handler = ph[i].s_handler;
|
---|
3073 | init->client = cl;
|
---|
3074 | if (pthread_create(&cl->thread, &attr, clientthread_init, (void*) init)) {
|
---|
3075 | cs_log("ERROR: can't create thread for UDP client from %s", inet_ntoa(*(struct in_addr *)&cad.sin_addr.s_addr));
|
---|
3076 | cleanup_thread(cl);
|
---|
3077 | free(init);
|
---|
3078 | }
|
---|
3079 | else
|
---|
3080 | pthread_detach(cl->thread);
|
---|
3081 | pthread_attr_destroy(&attr);
|
---|
3082 | } else {
|
---|
3083 | write_to_pipe(cl->fd_m2c, PIP_ID_UDP, (uchar*)&buf, n+3);
|
---|
3084 | }
|
---|
3085 | }
|
---|
3086 | } else { //TCP
|
---|
3087 |
|
---|
3088 | int pfd3;
|
---|
3089 | if ((pfd3=accept(ph[i].ptab->ports[j].fd, (struct sockaddr *)&cad, (socklen_t *)&scad))>0) {
|
---|
3090 |
|
---|
3091 | if (cs_check_violation((uint)cad.sin_addr.s_addr)) {
|
---|
3092 | close(pfd3);
|
---|
3093 | return 0;
|
---|
3094 | }
|
---|
3095 |
|
---|
3096 | struct s_client * cl = create_client(cad.sin_addr.s_addr);
|
---|
3097 | if (cl == NULL) {
|
---|
3098 | close(pfd3);
|
---|
3099 | return 0;
|
---|
3100 | }
|
---|
3101 |
|
---|
3102 | int flag = 1;
|
---|
3103 | setsockopt(pfd3, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
|
---|
3104 |
|
---|
3105 | cl->ctyp=i;
|
---|
3106 | cl->udp_fd=pfd3;
|
---|
3107 | cl->port_idx=j;
|
---|
3108 |
|
---|
3109 | cl->pfd=pfd3;
|
---|
3110 | cl->port=ntohs(cad.sin_port);
|
---|
3111 | cl->typ='c';
|
---|
3112 |
|
---|
3113 | pthread_attr_t attr;
|
---|
3114 | pthread_attr_init(&attr);
|
---|
3115 | #ifndef TUXBOX
|
---|
3116 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
3117 | #endif
|
---|
3118 |
|
---|
3119 | //We need memory here, because when on stack and we leave the function, stack is overwritten,
|
---|
3120 | //but assigned to the thread
|
---|
3121 | //So alloc memory for the init data and free them in clientthread_init:
|
---|
3122 | struct s_clientinit *init = cs_malloc(&init, sizeof(struct s_clientinit), 0);
|
---|
3123 | init->handler = ph[i].s_handler;
|
---|
3124 | init->client = cl;
|
---|
3125 | if (pthread_create(&cl->thread, &attr, clientthread_init, (void*) init)) {
|
---|
3126 | cs_log("ERROR: can't create thread for TCP client from %s", inet_ntoa(*(struct in_addr *)&cad.sin_addr.s_addr));
|
---|
3127 | cleanup_thread(cl);
|
---|
3128 | free(init);
|
---|
3129 | }
|
---|
3130 | else
|
---|
3131 | pthread_detach(cl->thread);
|
---|
3132 | pthread_attr_destroy(&attr);
|
---|
3133 | }
|
---|
3134 | }
|
---|
3135 | return 0;
|
---|
3136 | }
|
---|
3137 |
|
---|
3138 | /**
|
---|
3139 | * get tmp dir
|
---|
3140 | **/
|
---|
3141 | char * get_tmp_dir()
|
---|
3142 | {
|
---|
3143 | if (cs_tmpdir[0])
|
---|
3144 | return cs_tmpdir;
|
---|
3145 |
|
---|
3146 | #ifdef OS_CYGWIN32
|
---|
3147 | char *d = getenv("TMPDIR");
|
---|
3148 | if (!d || !d[0])
|
---|
3149 | d = getenv("TMP");
|
---|
3150 | if (!d || !d[0])
|
---|
3151 | d = getenv("TEMP");
|
---|
3152 | if (!d || !d[0])
|
---|
3153 | getcwd(cs_tmpdir, sizeof(cs_tmpdir)-1);
|
---|
3154 |
|
---|
3155 | cs_strncpy(cs_tmpdir, d, sizeof(cs_tmpdir));
|
---|
3156 | char *p = cs_tmpdir;
|
---|
3157 | while(*p) p++;
|
---|
3158 | p--;
|
---|
3159 | if (*p != '/' && *p != '\\')
|
---|
3160 | strcat(cs_tmpdir, "/");
|
---|
3161 | strcat(cs_tmpdir, "_oscam");
|
---|
3162 | #else
|
---|
3163 | cs_strncpy(cs_tmpdir, "/tmp/.oscam", sizeof(cs_tmpdir));
|
---|
3164 | #endif
|
---|
3165 | mkdir(cs_tmpdir, S_IRWXU);
|
---|
3166 | return cs_tmpdir;
|
---|
3167 | }
|
---|
3168 |
|
---|
3169 | #ifdef WEBIF
|
---|
3170 | static void restart_daemon()
|
---|
3171 | {
|
---|
3172 | while (1) {
|
---|
3173 |
|
---|
3174 | //start client process:
|
---|
3175 | pid_t pid = fork();
|
---|
3176 | if (!pid)
|
---|
3177 | return; //client process=oscam process
|
---|
3178 | if (pid < 0)
|
---|
3179 | exit(1);
|
---|
3180 |
|
---|
3181 | //restart control process:
|
---|
3182 | int res=0;
|
---|
3183 | int status=0;
|
---|
3184 | do {
|
---|
3185 | res = waitpid(pid, &status, 0);
|
---|
3186 | if (res==-1) {
|
---|
3187 | if (errno!=EINTR)
|
---|
3188 | exit(1);
|
---|
3189 | }
|
---|
3190 | } while (res!=pid);
|
---|
3191 |
|
---|
3192 | if (cs_restart_mode==2 && WIFSIGNALED(status) && WTERMSIG(status)==SIGSEGV)
|
---|
3193 | status=99; //restart on segfault!
|
---|
3194 | else
|
---|
3195 | status = WEXITSTATUS(status);
|
---|
3196 |
|
---|
3197 | //status=99 restart oscam, all other->terminate
|
---|
3198 | if (status!=99) {
|
---|
3199 | exit(status);
|
---|
3200 | }
|
---|
3201 | }
|
---|
3202 | }
|
---|
3203 | #endif
|
---|
3204 |
|
---|
3205 | int main (int argc, char *argv[])
|
---|
3206 | {
|
---|
3207 |
|
---|
3208 | /* init random number generator with seed. */
|
---|
3209 | srand((unsigned int)time((time_t *)NULL));
|
---|
3210 |
|
---|
3211 | if (pthread_key_create(&getclient, NULL)) {
|
---|
3212 | fprintf(stderr, "Could not create getclient, exiting...");
|
---|
3213 | exit(1);
|
---|
3214 | }
|
---|
3215 | #ifdef CS_LED
|
---|
3216 | cs_switch_led(LED1A, LED_DEFAULT);
|
---|
3217 | cs_switch_led(LED1A, LED_ON);
|
---|
3218 | #endif
|
---|
3219 |
|
---|
3220 | //struct sockaddr_in cad; /* structure to hold client's address */
|
---|
3221 | //int scad; /* length of address */
|
---|
3222 | //int fd; /* socket descriptors */
|
---|
3223 | int i, j;
|
---|
3224 | int bg=0;
|
---|
3225 | int gbdb=0;
|
---|
3226 | int gfd; //nph,
|
---|
3227 | int fdp[2];
|
---|
3228 | int mfdr=0; // Master FD (read)
|
---|
3229 | int fd_c2m=0;
|
---|
3230 |
|
---|
3231 | void (*mod_def[])(struct s_module *)=
|
---|
3232 | {
|
---|
3233 | #ifdef MODULE_MONITOR
|
---|
3234 | module_monitor,
|
---|
3235 | #endif
|
---|
3236 | #ifdef MODULE_CAMD33
|
---|
3237 | module_camd33,
|
---|
3238 | #endif
|
---|
3239 | #ifdef MODULE_CAMD35
|
---|
3240 | module_camd35,
|
---|
3241 | #endif
|
---|
3242 | #ifdef MODULE_CAMD35_TCP
|
---|
3243 | module_camd35_tcp,
|
---|
3244 | #endif
|
---|
3245 | #ifdef MODULE_NEWCAMD
|
---|
3246 | module_newcamd,
|
---|
3247 | #endif
|
---|
3248 | #ifdef MODULE_CCCAM
|
---|
3249 | module_cccam,
|
---|
3250 | #endif
|
---|
3251 | #ifdef MODULE_GBOX
|
---|
3252 | module_gbox,
|
---|
3253 | #endif
|
---|
3254 | #ifdef MODULE_CONSTCW
|
---|
3255 | module_constcw,
|
---|
3256 | #endif
|
---|
3257 | #ifdef MODULE_RADEGAST
|
---|
3258 | module_radegast,
|
---|
3259 | #endif
|
---|
3260 | #ifdef MODULE_SERIAL
|
---|
3261 | module_oscam_ser,
|
---|
3262 | #endif
|
---|
3263 | #ifdef HAVE_DVBAPI
|
---|
3264 | module_dvbapi,
|
---|
3265 | #endif
|
---|
3266 | 0
|
---|
3267 | };
|
---|
3268 |
|
---|
3269 | void (*cardsystem_def[])(struct s_cardsystem *)=
|
---|
3270 | {
|
---|
3271 | #ifdef READER_NAGRA
|
---|
3272 | reader_nagra,
|
---|
3273 | #endif
|
---|
3274 | #ifdef READER_IRDETO
|
---|
3275 | reader_irdeto,
|
---|
3276 | #endif
|
---|
3277 | #ifdef READER_CONAX
|
---|
3278 | reader_conax,
|
---|
3279 | #endif
|
---|
3280 | #ifdef READER_CRYPTOWORKS
|
---|
3281 | reader_cryptoworks,
|
---|
3282 | #endif
|
---|
3283 | #ifdef READER_SECA
|
---|
3284 | reader_seca,
|
---|
3285 | #endif
|
---|
3286 | #ifdef READER_VIACCESS
|
---|
3287 | reader_viaccess,
|
---|
3288 | #endif
|
---|
3289 | #ifdef READER_VIDEOGUARD
|
---|
3290 | reader_videoguard1,
|
---|
3291 | reader_videoguard2,
|
---|
3292 | reader_videoguard12,
|
---|
3293 | #endif
|
---|
3294 | #ifdef READER_DRE
|
---|
3295 | reader_dre,
|
---|
3296 | #endif
|
---|
3297 | #ifdef READER_TONGFANG
|
---|
3298 | reader_tongfang,
|
---|
3299 | #endif
|
---|
3300 | 0
|
---|
3301 | };
|
---|
3302 |
|
---|
3303 | void (*cardreader_def[])(struct s_cardreader *)=
|
---|
3304 | {
|
---|
3305 | cardreader_mouse,
|
---|
3306 | cardreader_smargo,
|
---|
3307 | #ifdef WITH_STAPI
|
---|
3308 | cardreader_stapi,
|
---|
3309 | #endif
|
---|
3310 | 0
|
---|
3311 | };
|
---|
3312 |
|
---|
3313 | while ((i=getopt(argc, argv, "gbc:t:d:r:hm:x"))!=EOF)
|
---|
3314 | {
|
---|
3315 | switch(i) {
|
---|
3316 | case 'g':
|
---|
3317 | gbdb=1;
|
---|
3318 | break;
|
---|
3319 | case 'b':
|
---|
3320 | bg=1;
|
---|
3321 | break;
|
---|
3322 | case 'c':
|
---|
3323 | cs_strncpy(cs_confdir, optarg, sizeof(cs_confdir));
|
---|
3324 | break;
|
---|
3325 | case 'd':
|
---|
3326 | cs_dblevel=atoi(optarg);
|
---|
3327 | break;
|
---|
3328 | #ifdef WEBIF
|
---|
3329 | case 'r':
|
---|
3330 | cs_restart_mode=atoi(optarg);
|
---|
3331 | break;
|
---|
3332 | #endif
|
---|
3333 | case 't':
|
---|
3334 | mkdir(optarg, S_IRWXU);
|
---|
3335 | j = open(optarg, O_RDONLY);
|
---|
3336 | if (j >= 0) {
|
---|
3337 | close(j);
|
---|
3338 | cs_strncpy(cs_tmpdir, optarg, sizeof(cs_tmpdir));
|
---|
3339 | } else {
|
---|
3340 | printf("WARNING: tmpdir does not exist. using default value.\n");
|
---|
3341 | }
|
---|
3342 | break;
|
---|
3343 | case 'm':
|
---|
3344 | printf("WARNING: -m parameter is deprecated, ignoring it.\n");
|
---|
3345 | break;
|
---|
3346 | case 'h':
|
---|
3347 | default :
|
---|
3348 | usage();
|
---|
3349 | }
|
---|
3350 | }
|
---|
3351 |
|
---|
3352 |
|
---|
3353 | #ifdef OS_MACOSX
|
---|
3354 | if (bg && daemon_compat(1,0))
|
---|
3355 | #else
|
---|
3356 | if (bg && daemon(1,0))
|
---|
3357 | #endif
|
---|
3358 | {
|
---|
3359 | printf("Error starting in background (errno=%d: %s)", errno, strerror(errno));
|
---|
3360 | cs_exit(1);
|
---|
3361 | }
|
---|
3362 |
|
---|
3363 | #ifdef WEBIF
|
---|
3364 | if (cs_restart_mode)
|
---|
3365 | restart_daemon();
|
---|
3366 | #endif
|
---|
3367 |
|
---|
3368 | memset(&cfg, 0, sizeof(struct s_config));
|
---|
3369 |
|
---|
3370 | if (cs_confdir[strlen(cs_confdir)]!='/') strcat(cs_confdir, "/");
|
---|
3371 | init_signal_pre(); // because log could cause SIGPIPE errors, init a signal handler first
|
---|
3372 | init_first_client();
|
---|
3373 | init_config();
|
---|
3374 | init_check();
|
---|
3375 | init_stat();
|
---|
3376 |
|
---|
3377 | for (i=0; mod_def[i]; i++) // must be later BEFORE init_config()
|
---|
3378 | {
|
---|
3379 | memset(&ph[i], 0, sizeof(struct s_module));
|
---|
3380 | mod_def[i](&ph[i]);
|
---|
3381 | }
|
---|
3382 | for (i=0; cardsystem_def[i]; i++) // must be later BEFORE init_config()
|
---|
3383 | {
|
---|
3384 | memset(&cardsystem[i], 0, sizeof(struct s_cardsystem));
|
---|
3385 | cardsystem_def[i](&cardsystem[i]);
|
---|
3386 | }
|
---|
3387 |
|
---|
3388 | memset(&cardreader, 0, sizeof(struct s_cardsystem) * CS_MAX_MOD);
|
---|
3389 | for (i=0; cardreader_def[i]; i++) // must be later BEFORE init_config()
|
---|
3390 | {
|
---|
3391 | cardreader_def[i](&cardreader[i]);
|
---|
3392 | }
|
---|
3393 |
|
---|
3394 |
|
---|
3395 | cs_log("auth size=%d", sizeof(struct s_auth));
|
---|
3396 |
|
---|
3397 | init_rnd();
|
---|
3398 | init_sidtab();
|
---|
3399 | init_readerdb();
|
---|
3400 | cfg.account = init_userdb();
|
---|
3401 | init_signal();
|
---|
3402 | init_srvid();
|
---|
3403 | init_tierid();
|
---|
3404 | //Todo #ifdef CCCAM
|
---|
3405 | init_provid();
|
---|
3406 |
|
---|
3407 | start_garbage_collector(gbdb);
|
---|
3408 |
|
---|
3409 | init_len4caid();
|
---|
3410 | #ifdef IRDETO_GUESSING
|
---|
3411 | init_irdeto_guess_tab();
|
---|
3412 | #endif
|
---|
3413 |
|
---|
3414 |
|
---|
3415 | if (pipe(fdp))
|
---|
3416 | {
|
---|
3417 | cs_log("Cannot create pipe (errno=%d: %s)", errno, strerror(errno));
|
---|
3418 | cs_exit(1);
|
---|
3419 | }
|
---|
3420 | mfdr=fdp[0];
|
---|
3421 | fd_c2m=fdp[1];
|
---|
3422 | gfd=mfdr+1;
|
---|
3423 |
|
---|
3424 | first_client->fd_m2c=fd_c2m;
|
---|
3425 | first_client->fd_m2c_c=mfdr;
|
---|
3426 |
|
---|
3427 | write_versionfile();
|
---|
3428 | server_pid = getpid();
|
---|
3429 |
|
---|
3430 | #if defined(AZBOX) && defined(HAVE_DVBAPI)
|
---|
3431 | openxcas_debug_message_onoff(1); // debug
|
---|
3432 |
|
---|
3433 | #ifdef WITH_CARDREADER
|
---|
3434 | if (openxcas_open_with_smartcard("oscamCAS") < 0) {
|
---|
3435 | #else
|
---|
3436 | if (openxcas_open("oscamCAS") < 0) {
|
---|
3437 | #endif
|
---|
3438 | cs_log("openxcas: could not init");
|
---|
3439 | }
|
---|
3440 | #endif
|
---|
3441 |
|
---|
3442 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
3443 | if( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
|
---|
3444 | for(j=0; j<ph[i].ptab->nports; j++)
|
---|
3445 | {
|
---|
3446 | start_listener(&ph[i], j);
|
---|
3447 | if( ph[i].ptab->ports[j].fd+1>gfd )
|
---|
3448 | gfd=ph[i].ptab->ports[j].fd+1;
|
---|
3449 | }
|
---|
3450 |
|
---|
3451 | //set time for server to now to avoid 0 in monitor/webif
|
---|
3452 | first_client->last=time((time_t *)0);
|
---|
3453 |
|
---|
3454 | #ifdef WEBIF
|
---|
3455 | if(cfg.http_port == 0)
|
---|
3456 | cs_log("http disabled");
|
---|
3457 | else
|
---|
3458 | start_thread((void *) &http_srv, "http");
|
---|
3459 | #endif
|
---|
3460 |
|
---|
3461 | init_cardreader();
|
---|
3462 |
|
---|
3463 | cs_waitforcardinit();
|
---|
3464 |
|
---|
3465 | #ifdef CS_LED
|
---|
3466 | cs_switch_led(LED1A, LED_OFF);
|
---|
3467 | cs_switch_led(LED1B, LED_ON);
|
---|
3468 | #endif
|
---|
3469 |
|
---|
3470 | #ifdef QBOXHD_LED
|
---|
3471 | if(!cfg.disableqboxhdled)
|
---|
3472 | cs_log("QboxHD LED enabled");
|
---|
3473 | qboxhd_led_blink(QBOXHD_LED_COLOR_YELLOW,QBOXHD_LED_BLINK_FAST);
|
---|
3474 | qboxhd_led_blink(QBOXHD_LED_COLOR_RED,QBOXHD_LED_BLINK_FAST);
|
---|
3475 | qboxhd_led_blink(QBOXHD_LED_COLOR_GREEN,QBOXHD_LED_BLINK_FAST);
|
---|
3476 | qboxhd_led_blink(QBOXHD_LED_COLOR_BLUE,QBOXHD_LED_BLINK_FAST);
|
---|
3477 | qboxhd_led_blink(QBOXHD_LED_COLOR_MAGENTA,QBOXHD_LED_BLINK_FAST);
|
---|
3478 | #endif
|
---|
3479 |
|
---|
3480 | #ifdef CS_ANTICASC
|
---|
3481 | if( !cfg.ac_enabled )
|
---|
3482 | cs_log("anti cascading disabled");
|
---|
3483 | else {
|
---|
3484 | init_ac();
|
---|
3485 | start_thread((void *) &start_anticascader, "anticascader"); // 96
|
---|
3486 |
|
---|
3487 | }
|
---|
3488 | #endif
|
---|
3489 |
|
---|
3490 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
3491 | if (ph[i].type & MOD_CONN_SERIAL) // for now: oscam_ser only
|
---|
3492 | if (ph[i].s_handler)
|
---|
3493 | ph[i].s_handler(i);
|
---|
3494 |
|
---|
3495 | //cs_close_log();
|
---|
3496 | while (!exit_oscam) {
|
---|
3497 | fd_set fds;
|
---|
3498 |
|
---|
3499 | do {
|
---|
3500 | //timeout value for checking exit_oscam:
|
---|
3501 | struct timeval timeout;
|
---|
3502 | timeout.tv_sec = 1;
|
---|
3503 | timeout.tv_usec = 0;
|
---|
3504 |
|
---|
3505 | //Wait for incoming data
|
---|
3506 | FD_ZERO(&fds);
|
---|
3507 | FD_SET(mfdr, &fds);
|
---|
3508 | for (i=0; i<CS_MAX_MOD; i++)
|
---|
3509 | if ( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
|
---|
3510 | for (j=0; j<ph[i].ptab->nports; j++)
|
---|
3511 | if (ph[i].ptab->ports[j].fd)
|
---|
3512 | FD_SET(ph[i].ptab->ports[j].fd, &fds);
|
---|
3513 | errno=0;
|
---|
3514 | select(gfd, &fds, 0, 0, &timeout);
|
---|
3515 | } while (errno==EINTR && !exit_oscam); //if timeout accurs and exit_oscam is set, we break the loop
|
---|
3516 |
|
---|
3517 | if (exit_oscam)
|
---|
3518 | break;
|
---|
3519 |
|
---|
3520 | first_client->last=time((time_t *)0);
|
---|
3521 |
|
---|
3522 | if (FD_ISSET(mfdr, &fds)) {
|
---|
3523 | process_master_pipe(mfdr);
|
---|
3524 | }
|
---|
3525 | for (i=0; i<CS_MAX_MOD; i++) {
|
---|
3526 | if( (ph[i].type & MOD_CONN_NET) && ph[i].ptab ) {
|
---|
3527 | for( j=0; j<ph[i].ptab->nports; j++ ) {
|
---|
3528 | if( ph[i].ptab->ports[j].fd && FD_ISSET(ph[i].ptab->ports[j].fd, &fds) ) {
|
---|
3529 | accept_connection(i,j);
|
---|
3530 | }
|
---|
3531 | }
|
---|
3532 | } // if (ph[i].type & MOD_CONN_NET)
|
---|
3533 | }
|
---|
3534 | }
|
---|
3535 |
|
---|
3536 | #if defined(AZBOX) && defined(HAVE_DVBAPI)
|
---|
3537 | if (openxcas_close() < 0) {
|
---|
3538 | cs_log("openxcas: could not close");
|
---|
3539 | }
|
---|
3540 | #endif
|
---|
3541 |
|
---|
3542 | cs_cleanup();
|
---|
3543 |
|
---|
3544 | stop_garbage_collector();
|
---|
3545 |
|
---|
3546 | return exit_oscam;
|
---|
3547 | }
|
---|
3548 |
|
---|
3549 | #ifdef WEBIF
|
---|
3550 | void cs_exit_oscam()
|
---|
3551 | {
|
---|
3552 | exit_oscam=1;
|
---|
3553 | cs_log("exit oscam requested");
|
---|
3554 | }
|
---|
3555 |
|
---|
3556 | void cs_restart_oscam()
|
---|
3557 | {
|
---|
3558 | exit_oscam=99;
|
---|
3559 | cs_log("restart oscam requested");
|
---|
3560 | }
|
---|
3561 |
|
---|
3562 | int cs_get_restartmode() {
|
---|
3563 | return cs_restart_mode;
|
---|
3564 | }
|
---|
3565 |
|
---|
3566 | #endif
|
---|
3567 |
|
---|
3568 | #ifdef CS_LED
|
---|
3569 | void cs_switch_led(int led, int action) {
|
---|
3570 |
|
---|
3571 | if(action < 2) { // only LED_ON and LED_OFF
|
---|
3572 | char ledfile[256];
|
---|
3573 | FILE *f;
|
---|
3574 |
|
---|
3575 | #ifdef DOCKSTAR
|
---|
3576 | switch(led){
|
---|
3577 | case LED1A:snprintf(ledfile, 255, "/sys/class/leds/dockstar:orange:misc/brightness");
|
---|
3578 | break;
|
---|
3579 | case LED1B:snprintf(ledfile, 255, "/sys/class/leds/dockstar:green:health/brightness");
|
---|
3580 | break;
|
---|
3581 | case LED2:snprintf(ledfile, 255, "/sys/class/leds/dockstar:green:health/brightness");
|
---|
3582 | break;
|
---|
3583 | case LED3:snprintf(ledfile, 255, "/sys/class/leds/dockstar:orange:misc/brightness");
|
---|
3584 | break;
|
---|
3585 | }
|
---|
3586 | #elif WRT350NV2
|
---|
3587 | switch(led){
|
---|
3588 | case LED1A:snprintf(ledfile, 255, "/sys/class/leds/wrt350nv2:orange:power/brightness");
|
---|
3589 | break;
|
---|
3590 | case LED1B:snprintf(ledfile, 255, "/sys/class/leds/wrt350nv2:green:power/brightness");
|
---|
3591 | break;
|
---|
3592 | case LED2:snprintf(ledfile, 255, "/sys/class/leds/wrt350nv2:green:wireless/brightness");
|
---|
3593 | break;
|
---|
3594 | case LED3:snprintf(ledfile, 255, "/sys/class/leds/wrt350nv2:green:security/brightness");
|
---|
3595 | break;
|
---|
3596 | }
|
---|
3597 | #else
|
---|
3598 | switch(led){
|
---|
3599 | case LED1A:snprintf(ledfile, 255, "/sys/class/leds/nslu2:red:status/brightness");
|
---|
3600 | break;
|
---|
3601 | case LED1B:snprintf(ledfile, 255, "/sys/class/leds/nslu2:green:ready/brightness");
|
---|
3602 | break;
|
---|
3603 | case LED2:snprintf(ledfile, 255, "/sys/class/leds/nslu2:green:disk-1/brightness");
|
---|
3604 | break;
|
---|
3605 | case LED3:snprintf(ledfile, 255, "/sys/class/leds/nslu2:green:disk-2/brightness");
|
---|
3606 | break;
|
---|
3607 | }
|
---|
3608 | #endif
|
---|
3609 |
|
---|
3610 | if (!(f=fopen(ledfile, "w"))){
|
---|
3611 | // FIXME: sometimes cs_log was not available when calling cs_switch_led -> signal 11
|
---|
3612 | //cs_log("Cannot open file \"%s\" (errno=%d %s)", ledfile, errno, strerror(errno));
|
---|
3613 | return;
|
---|
3614 | }
|
---|
3615 | fprintf(f,"%d", action);
|
---|
3616 | fclose(f);
|
---|
3617 | } else { // LED Macros
|
---|
3618 | switch(action){
|
---|
3619 | case LED_DEFAULT:
|
---|
3620 | cs_switch_led(LED1A, LED_OFF);
|
---|
3621 | cs_switch_led(LED1B, LED_OFF);
|
---|
3622 | cs_switch_led(LED2, LED_ON);
|
---|
3623 | cs_switch_led(LED3, LED_OFF);
|
---|
3624 | break;
|
---|
3625 | case LED_BLINK_OFF:
|
---|
3626 | cs_switch_led(led, LED_OFF);
|
---|
3627 | cs_sleepms(100);
|
---|
3628 | cs_switch_led(led, LED_ON);
|
---|
3629 | break;
|
---|
3630 | case LED_BLINK_ON:
|
---|
3631 | cs_switch_led(led, LED_ON);
|
---|
3632 | cs_sleepms(300);
|
---|
3633 | cs_switch_led(led, LED_OFF);
|
---|
3634 | break;
|
---|
3635 | }
|
---|
3636 | }
|
---|
3637 | }
|
---|
3638 | #endif
|
---|
3639 |
|
---|
3640 | #ifdef QBOXHD_LED
|
---|
3641 | void qboxhd_led_blink(int color, int duration) {
|
---|
3642 | int f;
|
---|
3643 |
|
---|
3644 | if (cfg.disableqboxhdled) {
|
---|
3645 | return;
|
---|
3646 | }
|
---|
3647 |
|
---|
3648 | // try QboxHD-MINI first
|
---|
3649 | if ( (f = open ( QBOXHDMINI_LED_DEVICE, O_RDWR |O_NONBLOCK )) > -1 ) {
|
---|
3650 | qboxhdmini_led_color_struct qbminiled;
|
---|
3651 | ulong qboxhdmini_color = 0x000000;
|
---|
3652 |
|
---|
3653 | if (color != QBOXHD_LED_COLOR_OFF) {
|
---|
3654 | switch(color) {
|
---|
3655 | case QBOXHD_LED_COLOR_RED:
|
---|
3656 | qboxhdmini_color = QBOXHDMINI_LED_COLOR_RED;
|
---|
3657 | break;
|
---|
3658 | case QBOXHD_LED_COLOR_GREEN:
|
---|
3659 | qboxhdmini_color = QBOXHDMINI_LED_COLOR_GREEN;
|
---|
3660 | break;
|
---|
3661 | case QBOXHD_LED_COLOR_BLUE:
|
---|
3662 | qboxhdmini_color = QBOXHDMINI_LED_COLOR_BLUE;
|
---|
3663 | break;
|
---|
3664 | case QBOXHD_LED_COLOR_YELLOW:
|
---|
3665 | qboxhdmini_color = QBOXHDMINI_LED_COLOR_YELLOW;
|
---|
3666 | break;
|
---|
3667 | case QBOXHD_LED_COLOR_MAGENTA:
|
---|
3668 | qboxhdmini_color = QBOXHDMINI_LED_COLOR_MAGENTA;
|
---|
3669 | break;
|
---|
3670 | }
|
---|
3671 |
|
---|
3672 | // set LED on with color
|
---|
3673 | qbminiled.red = (uchar)((qboxhdmini_color&0xFF0000)>>16); // R
|
---|
3674 | qbminiled.green = (uchar)((qboxhdmini_color&0x00FF00)>>8); // G
|
---|
3675 | qbminiled.blue = (uchar)(qboxhdmini_color&0x0000FF); // B
|
---|
3676 |
|
---|
3677 | ioctl(f,QBOXHDMINI_IOSET_RGB,&qbminiled);
|
---|
3678 | cs_sleepms(duration);
|
---|
3679 | }
|
---|
3680 |
|
---|
3681 | // set LED off
|
---|
3682 | qbminiled.red = 0;
|
---|
3683 | qbminiled.green = 0;
|
---|
3684 | qbminiled.blue = 0;
|
---|
3685 |
|
---|
3686 | ioctl(f,QBOXHDMINI_IOSET_RGB,&qbminiled);
|
---|
3687 | close(f);
|
---|
3688 |
|
---|
3689 | } else if ( (f = open ( QBOXHD_LED_DEVICE, O_RDWR |O_NONBLOCK )) > -1 ) {
|
---|
3690 |
|
---|
3691 | qboxhd_led_color_struct qbled;
|
---|
3692 |
|
---|
3693 | if (color != QBOXHD_LED_COLOR_OFF) {
|
---|
3694 | // set LED on with color
|
---|
3695 | qbled.H = color;
|
---|
3696 | qbled.S = 99;
|
---|
3697 | qbled.V = 99;
|
---|
3698 | ioctl(f,QBOXHD_SET_LED_ALL_PANEL_COLOR, &qbled);
|
---|
3699 | cs_sleepms(duration);
|
---|
3700 | }
|
---|
3701 |
|
---|
3702 | // set LED off
|
---|
3703 | qbled.H = 0;
|
---|
3704 | qbled.S = 0;
|
---|
3705 | qbled.V = 0;
|
---|
3706 | ioctl(f,QBOXHD_SET_LED_ALL_PANEL_COLOR, &qbled);
|
---|
3707 | close(f);
|
---|
3708 | }
|
---|
3709 |
|
---|
3710 | return;
|
---|
3711 | }
|
---|
3712 | #endif
|
---|