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