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