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