source: trunk/module-monitor.c@ 4149

Last change on this file since 4149 was 3870, checked in by rorothetroll, 13 years ago

reader-viaccess.c : improvment on long ECM parsing and making sure we only send the right ECM to the card. NOT TESTED !!!!. I tested on a card with normal ECM and it still works. I need someone to test this on a card with long ECM in debug mode.
csctapi/ifd_pcsc.c : improve debug messages.
globals.h : added number_ecm for long ecm processing in viaccess.

File size: 19.5 KB
Line 
1#include "globals.h"
2#ifdef CS_WITH_GBOX
3# include "csgbox/gbox.h"
4# define CS_VERSION_X CS_VERSION "-gbx-" GBXVERSION
5#else
6# define CS_VERSION_X CS_VERSION
7#endif
8
9
10static void monitor_check_ip()
11{
12 int ok=0;
13 struct s_ip *p_ip;
14
15 if (cur_client()->auth) return;
16 for (p_ip=cfg->mon_allowed; (p_ip) && (!ok); p_ip=p_ip->next)
17 ok=((cur_client()->ip>=p_ip->ip[0]) && (cur_client()->ip<=p_ip->ip[1]));
18 if (!ok)
19 {
20 cs_auth_client(cur_client(), (struct s_auth *)0, "invalid ip");
21 cs_exit(0);
22 }
23}
24
25static void monitor_auth_client(char *usr, char *pwd)
26{
27 struct s_auth *account;
28
29 if (cur_client()->auth) return;
30 if ((!usr) || (!pwd))
31 {
32 cs_auth_client(cur_client(), (struct s_auth *)0, NULL);
33 cs_exit(0);
34 }
35 for (account=cfg->account, cur_client()->auth=0; (account) && (!cur_client()->auth);)
36 {
37 if (account->monlvl)
38 cur_client()->auth=!(strcmp(usr, account->usr) | strcmp(pwd, account->pwd));
39 if (!cur_client()->auth)
40 account=account->next;
41 }
42 if (!cur_client()->auth)
43 {
44 cs_auth_client(cur_client(), (struct s_auth *)0, "invalid account");
45 cs_exit(0);
46 }
47 if (cs_auth_client(cur_client(), account, NULL))
48 cs_exit(0);
49}
50
51static int secmon_auth_client(uchar *ucrc)
52{
53 ulong crc;
54 struct s_auth *account;
55
56 if (cur_client()->auth)
57 {
58 int s=memcmp(cur_client()->ucrc, ucrc, 4);
59 if (s)
60 cs_log("wrong user-crc or garbage !?");
61 return(!s);
62 }
63 cur_client()->crypted=1;
64 crc=(ucrc[0]<<24) | (ucrc[1]<<16) | (ucrc[2]<<8) | ucrc[3];
65 for (account=cfg->account; (account) && (!cur_client()->auth); account=account->next)
66 if ((account->monlvl) &&
67 (crc==crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), cur_client()->dump), 16)))
68 {
69 memcpy(cur_client()->ucrc, ucrc, 4);
70 aes_set_key((char *)MD5((unsigned char *)account->pwd, strlen(account->pwd), cur_client()->dump));
71 if (cs_auth_client(cur_client(), account, NULL))
72 cs_exit(0);
73 cur_client()->auth=1;
74 }
75 if (!cur_client()->auth)
76 {
77 cs_auth_client(cur_client(), (struct s_auth *)0, "invalid user");
78 cs_exit(0);
79 }
80 return(cur_client()->auth);
81}
82
83int monitor_send_idx(struct s_client *cl, char *txt)
84{
85 int l;
86 unsigned char buf[256+32];
87 if (!cl->udp_fd)
88 return(-1);
89 struct timespec req_ts;
90 req_ts.tv_sec = 0;
91 req_ts.tv_nsec = 500000;
92 nanosleep (&req_ts, NULL);//avoid lost udp-pakkets
93 if (!cl->crypted)
94 return(sendto(cl->udp_fd, txt, strlen(txt), 0,
95 (struct sockaddr *)&cl->udp_sa,
96 sizeof(cl->udp_sa)));
97 buf[0]='&';
98 buf[9]=l=strlen(txt);
99 l=boundary(4, l+5)+5;
100 memcpy(buf+1, cl->ucrc, 4);
101 strcpy((char *)buf+10, txt);
102 memcpy(buf+5, i2b(4, crc32(0L, buf+10, l-10)), 4);
103 aes_encrypt_idx(cl, buf+5, l-5);
104 return(sendto(cl->udp_fd, buf, l, 0,
105 (struct sockaddr *)&cl->udp_sa,
106 sizeof(cl->udp_sa)));
107}
108
109#define monitor_send(t) monitor_send_idx(cur_client(), t)
110
111static int monitor_recv(struct s_client * client, uchar *buf, int l)
112{
113 int n;
114 uchar nbuf[3] = { 'U', 0, 0 };
115 static int bpos=0;
116 static uchar *bbuf=NULL;
117 if (!bbuf)
118 {
119 bbuf=(uchar *)malloc(l);
120 if (!bbuf)
121 {
122 cs_log("Cannot allocate memory (errno=%d)", errno);
123 cs_exit(1);
124 }
125 }
126 if (bpos)
127 memcpy(buf, bbuf, n=bpos);
128 else
129 n=recv_from_udpipe(buf);
130 bpos=0;
131 if (!n) return(buf[0]=0);
132 if (buf[0]=='&')
133 {
134 int bsize;
135 if (n<21) // 5+16 is minimum
136 {
137 cs_log("packet to short !");
138 return(buf[0]=0);
139 }
140 if (!secmon_auth_client(buf+1))
141 return(buf[0]=0);
142 aes_decrypt(buf+5, 16);
143 bsize=boundary(4, buf[9]+5)+5;
144 // cs_log("n=%d bsize=%d", n, bsize);
145 if (n>bsize)
146 {
147 // cs_log("DO >>>> copy-back");
148 memcpy(bbuf, buf+bsize, bpos=n-bsize);
149 n=bsize;
150 write_to_pipe(client->fd_m2c, PIP_ID_UDP, (uchar*)&nbuf, sizeof(nbuf));
151 }
152 else if (n<bsize)
153 {
154 cs_log("packet-size mismatch !");
155 return(buf[0]=0);
156 }
157 aes_decrypt(buf+21, n-21);
158 if (memcmp(buf+5, i2b(4, crc32(0L, buf+10, n-10)), 4))
159 {
160 cs_log("CRC error ! wrong password ?");
161 return(buf[0]=0);
162 }
163 n=buf[9];
164 memmove(buf, buf+10, n);
165 }
166 else
167 {
168 uchar *p;
169 monitor_check_ip();
170 buf[n]='\0';
171 if ((p=(uchar *)strchr((char *)buf, 10)) && (bpos=n-(p-buf)-1))
172 {
173 memcpy(bbuf, p+1, bpos);
174 n=p-buf;
175 write_to_pipe(client->fd_m2c, PIP_ID_UDP, (uchar*)&nbuf, sizeof(nbuf));
176 }
177 }
178 buf[n]='\0';
179 n=strlen(trim((char *)buf));
180 if (n) client->last=time((time_t *) 0);
181 return(n);
182}
183
184static void monitor_send_info(char *txt, int last)
185{
186 static int seq=0, counter=0;
187 static char btxt[256] = {0};
188 char buf[8];
189 if (txt)
190 {
191 if (!btxt[0])
192 {
193 counter=0;
194 txt[2]='B';
195 }
196 else
197 counter++;
198 sprintf(buf, "%03d", counter);
199 memcpy(txt+4, buf, 3);
200 txt[3]='0'+seq;
201 }
202 else
203 if (!last)
204 return;
205
206 if (!last)
207 {
208 if (btxt[0]) monitor_send(btxt);
209 cs_strncpy(btxt, txt, sizeof(btxt));
210 return;
211 }
212
213 if (txt && btxt[0])
214 {
215 monitor_send(btxt);
216 txt[2]='E';
217 cs_strncpy(btxt, txt, sizeof(btxt));
218 }
219 else
220 {
221 if (txt)
222 cs_strncpy(btxt, txt, sizeof(btxt));
223 btxt[2]=(btxt[2]=='B') ? 'S' : 'E';
224 }
225
226 if (btxt[0])
227 {
228 monitor_send(btxt);
229 seq=(seq+1)%10;
230 }
231 btxt[0]=0;
232}
233
234static char *monitor_client_info(char id, struct s_client *cl){
235 static char sbuf[256];
236 sbuf[0] = '\0';
237
238 if (cl){
239 char ldate[16], ltime[16], *usr;
240 int lsec, isec, con, cau, lrt;
241 time_t now;
242 struct tm *lt;
243 now=time((time_t)0);
244
245 if ((cfg->mon_hideclient_to <= 0) ||
246 (now-cl->lastecm < cfg->mon_hideclient_to) ||
247 (now-cl->lastemm < cfg->mon_hideclient_to) ||
248 (cl->typ != 'c'))
249 {
250 lsec=now-cl->login;
251 isec=now-cl->last;
252 usr=cl->usr;
253 if (((cl->typ == 'r') || (cl->typ == 'p')) && (con=get_ridx(cl->reader)) >= 0)
254 usr=cl->reader->label;
255 if (cl->dup)
256 con=2;
257 else
258 if ((cl->tosleep) && (now-cl->lastswitch>cl->tosleep))
259 con = 1;
260 else
261 con = 0;
262
263 if( (cau = get_ridx(cl->aureader) + 1) )
264 if ((now-cl->lastemm) /60 > cfg->mon_aulow)
265 cau=-cau;
266 if( cl->typ == 'r')
267 {
268 lrt = get_ridx(cl->reader);
269 if( lrt >= 0 )
270 lrt = 10 + cl->reader->card_status;
271 }
272 else
273 lrt = cl->cwlastresptime;
274 lt = localtime(&cl->login);
275 sprintf(ldate, "%02d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year % 100);
276 int cnr=get_threadnum(cl);
277 sprintf(ltime, "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
278 sprintf(sbuf, "[%c--CCC]%8lX|%c|%d|%s|%d|%d|%s|%d|%s|%s|%s|%d|%04X:%04X|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d\n",
279 id, (unsigned long)cl->thread, cl->typ, cnr, usr, cau, cl->crypted,
280 cs_inet_ntoa(cl->ip), cl->port, monitor_get_proto(cl),
281 ldate, ltime, lsec, cl->last_caid, cl->last_srvid,
282 get_servicename(cl->last_srvid, cl->last_caid), isec, con,
283 cl->cwfound, cl->cwnot, cl->cwcache, cl->cwignored,
284 cl->cwtout, cl->emmok, cl->emmnok, lrt);
285 }
286 }
287 return(sbuf);
288}
289
290static void monitor_process_info(){
291 time_t now = time((time_t)0);
292
293 struct s_client *cl;
294 for (cl=first_client; cl ; cl=cl->next) {
295 if ((cfg->mon_hideclient_to <= 0) ||
296 ( now-cl->lastecm < cfg->mon_hideclient_to) ||
297 ( now-cl->lastemm < cfg->mon_hideclient_to) ||
298 ( cl->typ != 'c')){
299 if ((cur_client()->monlvl < 2) && (cl->typ != 's')) {
300 if ((strcmp(cur_client()->usr, cl->usr)) ||
301 ((cl->typ != 'c') && (cl->typ != 'm')))
302 continue;
303 }
304 monitor_send_info(monitor_client_info('I', cl), 0);
305 }
306 }
307 monitor_send_info(NULL, 1);
308}
309
310static void monitor_send_details(char *txt, unsigned int tid){
311 char buf[256];
312 snprintf(buf, 255, "[D-----]%8X|%s\n", tid, txt);
313 monitor_send_info(buf, 0);
314}
315
316static void monitor_send_details_version(){
317 char buf[256];
318 sprintf(buf, "[V-0000]version=%s, build=%s, system=%s-%s-%s\n", CS_VERSION_X, CS_SVN_VERSION, CS_OS_CPU, CS_OS_HW, CS_OS_SYS);
319 monitor_send_info(buf, 1);
320}
321
322static void monitor_send_keepalive_ack(){
323 char buf[32];
324 sprintf(buf, "[K-0000]keepalive_ack\n");
325 monitor_send_info(buf, 1);
326}
327
328static void monitor_process_details_master(char *buf, unsigned long pid){
329 sprintf(buf, "Version=%s#%s", CS_VERSION_X, CS_SVN_VERSION);
330 monitor_send_details(buf, pid);
331 sprintf(buf, "System=%s-%s-%s", CS_OS_CPU, CS_OS_HW, CS_OS_SYS);
332 monitor_send_details(buf, pid);
333 sprintf(buf, "DebugLevel=%d", cfg->debuglvl);
334 monitor_send_details(buf, pid);
335 sprintf(buf, "MaxClients=UNLIMITED");
336 monitor_send_details(buf, pid);
337 sprintf(buf, "ClientMaxIdle=%ld sec", cfg->cmaxidle);
338 monitor_send_details(buf, pid);
339 if( cfg->max_log_size )
340 sprintf(buf + 200, "%d Kb", cfg->max_log_size);
341 else
342 strcpy(buf + 200, "unlimited");
343 sprintf(buf, "MaxLogsize=%s", buf + 200);
344 monitor_send_details(buf, pid);
345 sprintf(buf, "ClientTimeout=%lu ms", cfg->ctimeout);
346 monitor_send_details(buf, pid);
347 sprintf(buf, "CacheDelay=%ld ms", cfg->delay);
348 monitor_send_details(buf, pid);
349 if( cfg->cwlogdir ) {
350 sprintf(buf, "CwlogDir=%s", cfg->cwlogdir);
351 monitor_send_details(buf, pid);
352 }
353 if( cfg->preferlocalcards ) {
354 sprintf(buf, "PreferlocalCards=%d", cfg->preferlocalcards);
355 monitor_send_details(buf, pid);
356 }
357 if( cfg->waitforcards ) {
358 sprintf(buf, "WaitforCards=%d", cfg->waitforcards);
359 monitor_send_details(buf, pid);
360 }
361 sprintf(buf, "LogFile=%s", cfg->logfile);
362 monitor_send_details(buf, pid);
363 if( cfg->usrfile ) {
364 sprintf(buf, "UsrFile=%s", cfg->usrfile);
365 monitor_send_details(buf, pid);
366 }
367 sprintf(buf, "ResolveDelay=%d", cfg->resolvedelay);
368 monitor_send_details(buf, pid);
369 sprintf(buf, "Sleep=%d", cfg->tosleep);
370 monitor_send_details(buf, pid);
371 sprintf(buf, "Monitorport=%d", cfg->mon_port);
372 monitor_send_details(buf, pid);
373 sprintf(buf, "Nice=%d", cfg->nice);
374 monitor_send_details(buf, pid);
375
376 // monitor_send_details(buf, pid);
377}
378
379
380static void monitor_process_details_reader(struct s_client *cl) {
381
382 if (cfg->saveinithistory) {
383 FILE *fp;
384 char filename[32];
385 char buffer[128];
386 sprintf(filename, "%s/reader%d", get_tmp_dir(), get_ridx(cl->reader));
387 fp = fopen(filename, "r");
388
389 if (fp) {
390 while(fgets(buffer, 128, fp) != NULL) {
391 monitor_send_details(buffer, (unsigned long)(cl->thread));
392 }
393 fclose(fp);
394 }
395 } else {
396 monitor_send_details("Missing reader index or entitlement not saved!", (unsigned long)(cl->thread));
397 }
398
399}
400
401
402static void monitor_process_details(char *arg){
403 unsigned long tid = 0; //using threadid 8 positions hex see oscam-log.c //FIXME untested but pid isnt working anyway with threading
404 struct s_client *cl;
405 char sbuf[256];
406
407 if (!arg)
408 cl = first_client; // no arg - show master
409 else
410 if (sscanf(arg,"%lX",&tid) == 1)
411 cl = get_client_by_tid(tid);
412 else
413 cl = NULL;
414
415 if (!cl)
416 monitor_send_details("Invalid TID", tid);
417 else
418 {
419 //monitor_send_info(monitor_client_info('D', idx), 0); //FIXME
420 switch(cl->typ)
421 {
422 case 's':
423 monitor_process_details_master(sbuf, (unsigned long)(cl->thread));
424 break;
425 case 'c': case 'm':
426 monitor_send_details(monitor_client_info(1, cl), (unsigned long)(cl->thread));
427 break;
428 case 'r':
429 monitor_process_details_reader(cl);//with client->typ='r' client->ridx is always filled and valid, so no need checking
430 break;
431 case 'p':
432 monitor_send_details(monitor_client_info(1, cl), (unsigned long)(cl->thread));
433 break;
434 }
435 }
436 monitor_send_info(NULL, 1);
437}
438
439static void monitor_send_login(void){
440 char buf[64];
441 if (cur_client()->auth)
442 sprintf(buf, "[A-0000]1|%s logged in\n", cur_client()->usr);
443 else
444 strcpy(buf, "[A-0000]0|not logged in\n");
445 monitor_send_info(buf, 1);
446}
447
448static void monitor_login(char *usr){
449 char *pwd=NULL;
450 if ((usr) && (pwd=strchr(usr, ' ')))
451 *pwd++=0;
452 if (pwd)
453 monitor_auth_client(trim(usr), trim(pwd));
454 else
455 monitor_auth_client(NULL, NULL);
456 monitor_send_login();
457}
458
459static void monitor_logsend(char *flag){
460#ifdef CS_LOGHISTORY
461 int i;
462#endif
463 if (!flag) return; //no arg
464
465 if (strcmp(flag, "on")) {
466 if (strcmp(flag, "onwohist")) {
467 cur_client()->log=0;
468 return;
469 }
470 }
471
472 if (cur_client()->log) // already on
473 return;
474#ifdef CS_LOGHISTORY
475 if (!strcmp(flag, "on")){
476 for (i = (loghistidx + 3) % CS_MAXLOGHIST; i != loghistidx; i = (i + 1) % CS_MAXLOGHIST){
477 char *p_usr, *p_txt;
478 p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
479 p_txt = p_usr + 32;
480 if ((p_txt[0]) && ((cur_client()->monlvl > 1) || (!strcmp(p_usr, cur_client()->usr)))) {
481 char sbuf[8];
482 sprintf(sbuf, "%03d", cur_client()->logcounter);
483 cur_client()->logcounter=(cur_client()->logcounter + 1) % 1000;
484 memcpy(p_txt + 4, sbuf, 3);
485 monitor_send(p_txt);
486 }
487 }
488 }
489#endif
490 cur_client()->log=1;
491}
492
493static void monitor_set_debuglevel(char *flag){
494 cfg->debuglvl = atoi(flag);
495 cs_debug_level();
496}
497
498static void monitor_get_account(){
499 struct s_auth *account;
500 char buf[256];
501 int count = 0;
502
503 for (account=cfg->account; (account); account=account->next){
504 count++;
505 snprintf(buf, 255, "[U-----]%s\n", account->usr);
506 monitor_send_info(buf, 0);
507 }
508 sprintf(buf, "[U-----] %i User registered\n", count);
509 monitor_send_info(buf, 1);
510 return;
511}
512
513static void monitor_set_account(char *args){
514 struct s_auth *account;
515 char delimiter[] = " =";
516 char *ptr;
517 int argidx, i, found;
518 char *argarray[3];
519 static const char *token[]={"au", "sleep", "uniq", "monlevel", "group", "services", "betatunnel", "ident", "caid", "chid", "class", "hostname", "expdate", "keepalive", "disabled"};
520 int tokencnt = sizeof(token)/sizeof(char *);
521 char buf[256], tmp[64];
522
523 argidx = 0;
524 found = 0;
525
526 sprintf(tmp, "%s",args);
527 sprintf(buf, "[S-0000]setuser: %s check\n", tmp);
528 monitor_send_info(buf, 0);
529
530 ptr = strtok(args, delimiter);
531
532 // resolve arguments
533 while(ptr != NULL) {
534 argarray[argidx]=trim(ptr);
535 ptr = strtok(NULL, delimiter);
536 argidx++;
537 }
538
539 if(argidx != 3) {
540 sprintf(buf, "[S-0000]setuser: %s failed - wrong number of parameters (%d)\n",tmp, argidx);
541 monitor_send_info(buf, 0);
542 sprintf(buf, "[S-0000]setuser: %s end\n", tmp);
543 monitor_send_info(buf, 1);
544 return;
545 }
546
547 //search account
548 for (account=cfg->account; (account) ; account=account->next){
549 if (!strcmp(argarray[0], account->usr)){
550 found = 1;
551 break;
552 }
553 }
554
555 if (found != 1){
556 sprintf(buf, "[S-0000]setuser: %s failed - user %s not found\n",tmp , argarray[0]);
557 monitor_send_info(buf, 0);
558 sprintf(buf, "[S-0000]setuser: %s end\n", tmp);
559 monitor_send_info(buf, 1);
560 return;
561 }
562
563 found = -1;
564 for (i = 0; i < tokencnt; i++){
565 if (!strcmp(argarray[1], token[i])){
566 // preparing the parameters before re-load
567 switch(i) {
568
569 case 6: clear_tuntab(&account->ttab); break; //betatunnel
570
571 case 8: clear_caidtab(&account->ctab); break; //Caid
572 }
573 found = i;
574 }
575 }
576
577 if (found < 0){
578 sprintf(buf, "[S-0000]setuser: parameter %s not exist. possible values:\n", argarray[1]);
579 monitor_send_info(buf, 0);
580 for (i = 0; i < tokencnt; i++){
581 sprintf(buf, "[S-0000]%s\n", token[i]);
582 monitor_send_info(buf, 0);
583 }
584 sprintf(buf, "[S-0000]setuser: %s end\n", tmp);
585 monitor_send_info(buf, 1);
586 return;
587 } else {
588 chk_account(token[found], argarray[2], account);
589 }
590
591 if (write_userdb(cfg->account)==0)
592 cs_reinit_clients();
593
594 sprintf(buf, "[S-0000]setuser: %s done - param %s set to %s\n", tmp, argarray[1], argarray[2]);
595 monitor_send_info(buf, 1);
596}
597
598static void monitor_set_server(char *args){
599 char delimiter[] = "=";
600 char *ptr;
601 int argidx, i, found;
602 char *argarray[3];
603 static const char *token[]={"clienttimeout", "fallbacktimeout", "clientmaxidle", "cachedelay", "bindwait", "netprio", "resolvedelay", "sleep", "unlockparental", "serialreadertimeout", "maxlogsize", "showecmdw", "waitforcards", "preferlocalcards"};
604 char buf[256];
605
606 argidx=0; found=0;
607 ptr = strtok(args, delimiter);
608
609 // resolve arguments
610 while(ptr != NULL) {
611 argarray[argidx]=trim(ptr);
612 ptr = strtok(NULL, delimiter);
613 argidx++;
614 }
615
616 if(argidx != 2) {
617 sprintf(buf, "[S-0000]setserver failed - wrong number of parameters (%d)\n", argidx);
618 monitor_send_info(buf, 1);
619 return;
620 }
621
622 trim(argarray[0]);
623 trim(argarray[1]);
624 strtolower(argarray[0]);
625
626 for (i = 0; i < 14; i++)
627 if (!strcmp(argarray[0], token[i])) break;
628
629 if (i < 14){
630 chk_t_global(token[i],argarray[1]);
631 sprintf(buf, "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
632 monitor_send_info(buf, 1);
633 } else {
634 sprintf(buf, "[S-0000]setserver failed - parameter %s not exist\n", argarray[0]);
635 monitor_send_info(buf, 1);
636 return;
637 }
638
639 if (cfg->ftimeout>=cfg->ctimeout) {
640 cfg->ftimeout = cfg->ctimeout - 100;
641 sprintf(buf, "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms\n", cfg->ftimeout);
642 monitor_send_info(buf, 1);
643 }
644 if(cfg->ftimeout < cfg->srtimeout) {
645 cfg->ftimeout = cfg->srtimeout + 100;
646 sprintf(buf, "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms\n", cfg->ftimeout);
647 monitor_send_info(buf, 1);
648 }
649 if(cfg->ctimeout < cfg->srtimeout) {
650 cfg->ctimeout = cfg->srtimeout + 100;
651 sprintf(buf, "[S-0000]setserver WARNING: clienttimeout adjusted to %lu ms\n", cfg->ctimeout);
652 monitor_send_info(buf, 1);
653 }
654 //kill(first_client->pid, SIGUSR1);
655}
656
657static void monitor_list_commands(const char *args[], int cmdcnt){
658 int i;
659 for (i = 0; i < cmdcnt; i++) {
660 char buf[64];
661 sprintf(buf, "[S-0000]commands: %s\n", args[i]);
662 if(i < cmdcnt-1)
663 monitor_send_info(buf, 0);
664 else
665 monitor_send_info(buf, 1);
666 }
667}
668
669static int monitor_process_request(char *req)
670{
671 int i, rc;
672 static const char *cmd[] = {"login", "exit", "log", "status", "shutdown", "reload", "details", "version", "debug", "getuser", "setuser", "setserver", "commands", "keepalive", "reread"};
673 int cmdcnt = sizeof(cmd)/sizeof(char *); // Calculate the amount of items in array
674 char *arg;
675
676 if( (arg = strchr(req, ' ')) ) { *arg++ = 0; trim(arg); }
677 //trim(req);
678 if ((!cur_client()->auth) && (strcmp(req, cmd[0]))) monitor_login(NULL);
679
680 for (rc=1, i = 0; i < cmdcnt; i++)
681 if (!strcmp(req, cmd[i])) {
682 switch(i) {
683 case 0: monitor_login(arg); break; // login
684 case 1: rc=0; break; // exit
685 case 2: monitor_logsend(arg); break; // log
686 case 3: monitor_process_info(); break; // status
687 case 4: if (cur_client()->monlvl > 3) cs_exit(SIGQUIT); break; // shutdown
688 case 5: if (cur_client()->monlvl > 2) cs_reinit_clients(); break; // reload
689 case 6: monitor_process_details(arg); break; // details
690 case 7: monitor_send_details_version(); break; // version
691 case 8: if (cur_client()->monlvl > 3) monitor_set_debuglevel(arg); break; // debuglevel
692 case 9: if (cur_client()->monlvl > 3) monitor_get_account(); break; // getuser
693 case 10: if (cur_client()->monlvl > 3) monitor_set_account(arg); break; // setuser
694 case 11: if (cur_client()->monlvl > 3) monitor_set_server(arg); break; // setserver
695 case 12: if (cur_client()->monlvl > 3) monitor_list_commands(cmd, cmdcnt); break; // list commands
696 case 13: if (cur_client()->monlvl > 3) monitor_send_keepalive_ack(); break; // keepalive
697 case 14: { char buf[64];sprintf(buf, "[S-0000]reread\n");monitor_send_info(buf, 1); cs_card_info(); break; } // reread
698 default: continue;
699 }
700 break;
701 }
702 return(rc);
703}
704
705static void * monitor_server(void *cli){
706 int n;
707 uchar mbuf[1024];
708
709 struct s_client * client = (struct s_client *) cli;
710 client->thread=pthread_self();
711 pthread_setspecific(getclient, cli);
712 client->typ='m';
713 while (((n = process_input(mbuf, sizeof(mbuf), cfg->cmaxidle)) >= 0) && monitor_process_request((char *)mbuf));
714 cs_disconnect_client(cli);
715 return NULL;
716}
717
718void module_monitor(struct s_module *ph){
719 static PTAB ptab; //since there is always only 1 monitor running, this is threadsafe
720 ptab.ports[0].s_port = cfg->mon_port;
721 ph->ptab = &ptab;
722 ph->ptab->nports = 1;
723
724 if (cfg->mon_aulow < 1)
725 cfg->mon_aulow = 30;
726 strcpy(ph->desc, "monitor");
727 ph->type=MOD_CONN_UDP;
728 ph->multi = 0;
729 ph->watchdog = 1;
730 ph->s_ip = cfg->mon_srvip;
731 ph->s_handler = monitor_server;
732 ph->recv = monitor_recv;
733 // ph->send_dcw=NULL;
734}
735
736
Note: See TracBrowser for help on using the repository browser.