revert breaks some stupid old compilers
[oscam.git] / module-monitor.c
blob5130cf1a2110869d415dd7c6dd778ecdd1f3303c
1 #define MODULE_LOG_PREFIX "monitor"
3 #include "globals.h"
4 #ifdef MODULE_MONITOR
5 #include "cscrypt/md5.h"
6 #include "module-monitor.h"
7 #include "oscam-aes.h"
8 #include "oscam-array.h"
9 #include "oscam-client.h"
10 #include "oscam-config.h"
11 #include "oscam-conf-chk.h"
12 #include "oscam-lock.h"
13 #include "oscam-net.h"
14 #include "oscam-reader.h"
15 #include "oscam-string.h"
16 #include "oscam-work.h"
18 extern char *entitlement_type[];
20 struct monitor_data
22 bool auth;
23 uint8_t ucrc[4];
24 struct aes_keys aes_keys;
25 int32_t seq;
26 int32_t counter;
27 char btxt[256];
30 static int8_t monitor_check_ip(void)
32 int32_t ok = 0;
33 struct s_client *cur_cl = cur_client();
34 struct monitor_data *module_data = cur_cl->module_data;
36 if(module_data->auth) { return 0; }
37 ok = check_ip(cfg.mon_allowed, cur_cl->ip);
38 if(!ok)
40 cs_auth_client(cur_cl, (struct s_auth *)0, "invalid ip");
41 return -1;
43 return 0;
46 static int8_t monitor_auth_client(char *usr, char *pwd)
48 struct s_auth *account;
49 struct s_client *cur_cl = cur_client();
50 struct monitor_data *module_data = cur_cl->module_data;
52 if(module_data->auth) { return 0; }
53 if((!usr) || (!pwd))
55 cs_auth_client(cur_cl, (struct s_auth *)0, NULL);
56 return -1;
58 for(account = cfg.account; account; account = account->next)
60 if(account->monlvl && streq(usr, account->usr) && streq(pwd, account->pwd))
62 module_data->auth = 1;
63 break;
66 if(!module_data->auth)
68 cs_auth_client(cur_cl, (struct s_auth *)0, "invalid account");
69 return -1;
71 if(cs_auth_client(cur_cl, account, NULL))
72 { return -1; }
73 return 0;
76 static int32_t secmon_auth_client(uchar *ucrc)
78 uint32_t crc;
79 struct s_auth *account;
80 struct s_client *cur_cl = cur_client();
81 struct monitor_data *module_data = cur_cl->module_data;
82 unsigned char md5tmp[MD5_DIGEST_LENGTH];
84 if(module_data->auth)
86 int32_t s = memcmp(module_data->ucrc, ucrc, 4);
87 if(s)
88 { cs_log("wrong user-crc or garbage !?"); }
89 return !s;
91 cur_cl->crypted = 1;
92 crc = (ucrc[0] << 24) | (ucrc[1] << 16) | (ucrc[2] << 8) | ucrc[3];
93 for(account = cfg.account; (account) && (!module_data->auth); account = account->next)
94 if((account->monlvl) &&
95 (crc == crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), md5tmp), MD5_DIGEST_LENGTH)))
97 memcpy(module_data->ucrc, ucrc, 4);
98 aes_set_key(&module_data->aes_keys, (char *)MD5((unsigned char *)ESTR(account->pwd), strlen(ESTR(account->pwd)), md5tmp));
99 if(cs_auth_client(cur_cl, account, NULL))
100 { return -1; }
101 module_data->auth = 1;
103 if(!module_data->auth)
105 cs_auth_client(cur_cl, (struct s_auth *)0, "invalid user");
106 return -1;
108 return module_data->auth;
111 int32_t monitor_send_idx(struct s_client *cl, char *txt)
113 struct monitor_data *module_data = cl->module_data;
114 int32_t l;
115 unsigned char buf[256 + 32];
116 if(!cl->udp_fd)
117 { return -1; }
118 struct timespec req_ts;
119 req_ts.tv_sec = 0;
120 req_ts.tv_nsec = 500000;
121 nanosleep(&req_ts, NULL); //avoid lost udp-pakkets
122 if(!cl->crypted)
123 { return sendto(cl->udp_fd, txt, strlen(txt), 0, (struct sockaddr *)&cl->udp_sa, cl->udp_sa_len); }
124 l = strlen(txt);
125 if(l > 255)
126 { l = 255; }
127 buf[0] = '&';
128 buf[9] = l;
129 l = boundary(4, l + 5) + 5;
130 memcpy(buf + 1, module_data->ucrc, 4);
131 cs_strncpy((char *)buf + 10, txt, sizeof(buf) - 10);
132 memset(buf+10+buf[9], 0, l-10-buf[9]);
133 uchar tmp[10];
134 memcpy(buf + 5, i2b_buf(4, crc32(0L, buf + 10, l - 10), tmp), 4);
135 aes_encrypt_idx(&module_data->aes_keys, buf + 5, l - 5);
136 return sendto(cl->udp_fd, buf, l, 0, (struct sockaddr *)&cl->udp_sa, cl->udp_sa_len);
139 #define monitor_send(t) monitor_send_idx(cur_client(), t)
141 static int32_t monitor_recv(struct s_client *client, uchar *buf, int32_t UNUSED(buflen))
143 int32_t n = recv_from_udpipe(buf);
144 if(!n)
145 { return buf[0] = 0; }
146 if(!client->module_data && !cs_malloc(&client->module_data, sizeof(struct monitor_data)))
147 { return 0; }
148 if(buf[0] == '&')
150 int32_t bsize;
151 if(n < 21) // 5+16 is minimum
153 cs_log("packet too small!");
154 return buf[0] = 0;
156 int32_t res = secmon_auth_client(buf + 1);
157 if(res == -1)
159 cs_disconnect_client(client);
160 return 0;
162 if(!res)
164 return buf[0] = 0;
166 struct monitor_data *module_data = client->module_data;
167 aes_decrypt(&module_data->aes_keys, buf + 5, 16);
168 bsize = boundary(4, buf[9] + 5) + 5;
169 if(n < bsize)
171 cs_log("packet-size mismatch !");
172 return buf[0] = 0;
174 aes_decrypt(&module_data->aes_keys, buf + 21, n - 21);
175 uchar tmp[10];
176 if(memcmp(buf + 5, i2b_buf(4, crc32(0L, buf + 10, n - 10), tmp), 4))
178 cs_log("CRC error ! wrong password ?");
179 return buf[0] = 0;
181 n = buf[9];
182 memmove(buf, buf + 10, n);
184 else
186 if(monitor_check_ip() == -1)
188 cs_disconnect_client(client);
189 return 0;
192 buf[n] = '\0';
193 n = strlen(trim((char *)buf));
194 if(n) { client->last = time((time_t *) 0); }
195 return n;
198 static void monitor_send_info(char *txt, int32_t last)
200 struct s_client *cur_cl = cur_client();
201 struct monitor_data *module_data = cur_cl->module_data;
202 char buf[8];
203 if(txt)
205 if(!module_data->btxt[0])
207 module_data->counter = 0;
208 txt[2] = 'B';
210 else
211 { module_data->counter++; }
212 snprintf(buf, sizeof(buf), "%03d", module_data->counter);
213 memcpy(txt + 4, buf, 3);
214 txt[3] = '0' + module_data->seq;
216 else if(!last)
217 { return; }
219 if(!last)
221 if(module_data->btxt[0]) { monitor_send(module_data->btxt); }
222 cs_strncpy(module_data->btxt, txt, sizeof(module_data->btxt));
223 return;
226 if(txt && module_data->btxt[0])
228 monitor_send(module_data->btxt);
229 txt[2] = 'E';
230 cs_strncpy(module_data->btxt, txt, sizeof(module_data->btxt));
232 else
234 if(txt)
235 { cs_strncpy(module_data->btxt, txt, sizeof(module_data->btxt)); }
236 module_data->btxt[2] = (module_data->btxt[2] == 'B') ? 'S' : 'E';
239 if(module_data->btxt[0])
241 monitor_send(module_data->btxt);
242 module_data->seq = (module_data->seq + 1) % 10;
244 module_data->btxt[0] = 0;
247 static char *monitor_client_info(char id, struct s_client *cl, char *sbuf)
249 char channame[CS_SERVICENAME_SIZE];
250 sbuf[0] = '\0';
252 if(cl)
254 char ldate[16], ltime[16];
255 const char *usr;
256 int32_t lsec, isec, con, cau, lrt = - 1;
257 time_t now;
258 struct tm lt;
259 now = time((time_t *)0);
261 if((cfg.hideclient_to <= 0) ||
262 (now - cl->lastecm < cfg.hideclient_to) ||
263 (now - cl->lastemm < cfg.hideclient_to) ||
264 (cl->typ != 'c'))
266 lsec = now - cl->login;
267 isec = now - cl->last;
268 usr = username(cl);
269 if(cl->dup)
270 { con = 2; }
271 else if((cl->tosleep) && (now - cl->lastswitch > cl->tosleep))
272 { con = 1; }
273 else
274 { con = 0; }
276 // no AU reader == 0 / AU ok == 1 / Last EMM > aulow == -1
277 if(cl->typ == 'c' || cl->typ == 'p' || cl->typ == 'r')
280 if((cl->typ == 'c' && ll_count(cl->aureader_list) == 0) ||
281 ((cl->typ == 'p' || cl->typ == 'r') && cl->reader->audisabled))
282 { cau = 0; }
284 else if((now - cl->lastemm) / 60 > cfg.aulow)
285 { cau = (-1); }
287 else
288 { cau = 1; }
291 else
293 cau = 0;
296 if(cl->typ == 'r')
298 int32_t i;
299 struct s_reader *rdr;
300 for(i = 0, rdr = first_active_reader; rdr ; rdr = rdr->next, i++)
301 if(cl->reader == rdr)
302 { lrt = i; }
304 if(lrt >= 0)
305 { lrt = 10 + cl->reader->card_status; }
307 else
308 { lrt = cl->cwlastresptime; }
309 localtime_r(&cl->login, &lt);
310 snprintf(ldate, sizeof(ldate), "%02d.%02d.%02d", lt.tm_mday, lt.tm_mon + 1, lt.tm_year % 100);
311 int32_t cnr = get_threadnum(cl);
312 snprintf(ltime, sizeof(ldate), "%02d:%02d:%02d", lt.tm_hour, lt.tm_min, lt.tm_sec);
313 snprintf(sbuf, 256, "[%c--CCC]%8X|%c|%d|%s|%d|%d|%s|%d|%s|%s|%s|%d|%04X@%06X:%04X|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d\n",
314 id, cl->tid, cl->typ, cnr, usr, cau, cl->crypted,
315 cs_inet_ntoa(cl->ip), cl->port, client_get_proto(cl),
316 ldate, ltime, lsec, cl->last_caid, cl->last_provid, cl->last_srvid,
317 get_servicename_or_null(cl, cl->last_srvid, cl->last_provid, cl->last_caid, channame, sizeof(channame)), isec, con,
318 cl->cwfound, cl->cwnot, cl->cwcache, cl->cwignored,
319 cl->cwtout, cl->emmok, cl->emmnok, lrt);
322 return sbuf;
325 static void monitor_process_info(void)
327 time_t now = time((time_t *)0);
328 char sbuf[256];
330 struct s_client *cl, *cur_cl = cur_client();
332 for(cl = first_client; cl ; cl = cl->next)
334 if((cfg.hideclient_to <= 0) ||
335 (now - cl->lastecm < cfg.hideclient_to) ||
336 (now - cl->lastemm < cfg.hideclient_to) ||
337 (cl->typ != 'c'))
339 if((cur_cl->monlvl < 2) && (cl->typ != 's'))
341 if((cur_cl->account && cl->account && strcmp(cur_cl->account->usr, cl->account->usr)) ||
342 ((cl->typ != 'c') && (cl->typ != 'm')))
343 { continue; }
345 monitor_send_info(monitor_client_info('I', cl, sbuf), 0);
348 monitor_send_info(NULL, 1);
351 static void monitor_send_details(char *txt, uint32_t tid)
353 char buf[256];
354 snprintf(buf, 255, "[D-----]%8X|%s\n", tid, txt);
355 monitor_send_info(buf, 0);
358 static void monitor_send_details_version(void)
360 char buf[256];
361 snprintf(buf, sizeof(buf), "[V-0000]version=%s, build=%s, system=%s\n", CS_VERSION, CS_SVN_VERSION, CS_TARGET);
362 monitor_send_info(buf, 1);
365 static void monitor_send_keepalive_ack(void)
367 char buf[32];
368 snprintf(buf, sizeof(buf), "[K-0000]keepalive_ack\n");
369 monitor_send_info(buf, 1);
372 static void monitor_process_details_master(char *buf, uint32_t pid)
374 snprintf(buf, 256, "Version=%sr%s", CS_VERSION, CS_SVN_VERSION);
375 monitor_send_details(buf, pid);
376 snprintf(buf, 256, "System=%s", CS_TARGET);
377 monitor_send_details(buf, pid);
378 snprintf(buf, 256, "DebugLevel=%d", cs_dblevel);
379 monitor_send_details(buf, pid);
380 snprintf(buf, 256, "MaxClients=UNLIMITED");
381 monitor_send_details(buf, pid);
382 snprintf(buf, 256, "ClientMaxIdle=%d sec", cfg.cmaxidle);
383 monitor_send_details(buf, pid);
384 if(cfg.max_log_size)
385 { snprintf(buf + 200, 56, "%d Kb", cfg.max_log_size); }
386 else
387 { cs_strncpy(buf + 200, "unlimited", 56); }
388 snprintf(buf, 256, "MaxLogsize=%s", buf + 200);
389 monitor_send_details(buf, pid);
390 snprintf(buf, 256, "ClientTimeout=%u ms", cfg.ctimeout);
391 monitor_send_details(buf, pid);
392 snprintf(buf, 256, "CacheDelay=%d ms", cfg.delay);
393 monitor_send_details(buf, pid);
394 if(cfg.cwlogdir)
396 snprintf(buf, 256, "CwlogDir=%s", cfg.cwlogdir);
397 monitor_send_details(buf, pid);
399 if(cfg.preferlocalcards)
401 snprintf(buf, 256, "PreferlocalCards=%d", cfg.preferlocalcards);
402 monitor_send_details(buf, pid);
404 if(cfg.waitforcards)
406 snprintf(buf, 256, "WaitforCards=%d", cfg.waitforcards);
407 monitor_send_details(buf, pid);
409 snprintf(buf, 256, "LogFile=%s", cfg.logfile);
410 monitor_send_details(buf, pid);
411 if(cfg.mailfile)
413 snprintf(buf, 256, "MailFile=%s", cfg.mailfile);
414 monitor_send_details(buf, pid);
416 if(cfg.usrfile)
418 snprintf(buf, 256, "UsrFile=%s", cfg.usrfile);
419 monitor_send_details(buf, pid);
421 monitor_send_details(buf, pid);
422 snprintf(buf, 256, "Sleep=%d", cfg.tosleep);
423 monitor_send_details(buf, pid);
424 snprintf(buf, 256, "Monitorport=%d", cfg.mon_port);
425 monitor_send_details(buf, pid);
426 snprintf(buf, 256, "Nice=%d", cfg.nice);
427 monitor_send_details(buf, pid);
428 #ifdef WEBIF
429 snprintf(buf, 256, "Restartmode=%d", cs_get_restartmode());
430 monitor_send_details(buf, pid);
431 #else
432 snprintf(buf, 256, "Restartmode=%s", "no");
433 monitor_send_details(buf, pid);
434 #endif
436 // monitor_send_details(buf, pid);
440 static void monitor_process_details_reader(struct s_client *cl)
442 char tbuffer1[64], tbuffer2[64], buf[256] = { 0 }, tmpbuf[256] = { 0 }, valid_to[32] = { 0 };
443 struct s_reader *rdr = cl->reader;
444 if(!rdr)
446 monitor_send_details("Reader do not exist or it is not started.", cl->tid);
447 return;
450 if(rdr->card_valid_to)
452 struct tm vto_t;
453 localtime_r(&rdr->card_valid_to, &vto_t);
454 strftime(valid_to, sizeof(valid_to) - 1, "%Y-%m-%d", &vto_t);
456 else
458 strncpy(valid_to, "n/a", 3);
461 snprintf(tmpbuf, sizeof(tmpbuf) - 1, "Cardsystem: %s Reader: %s ValidTo: %s HexSerial: %s ATR: %s",
462 rdr->csystem ? rdr->csystem->desc : "-",
463 rdr->label,
464 valid_to,
465 cs_hexdump(1, rdr->hexserial, 8, tbuffer2, sizeof(tbuffer2)),
466 rdr->card_atr_length
467 ? cs_hexdump(1, rdr->card_atr, rdr->card_atr_length, buf, sizeof(buf))
468 : ""
470 monitor_send_details(tmpbuf, cl->tid);
472 if(!rdr->ll_entitlements)
474 monitor_send_details("No entitlements for the reader.", cl->tid);
475 return;
478 S_ENTITLEMENT *item;
479 LL_ITER itr = ll_iter_create(rdr->ll_entitlements);
480 time_t now = (time(NULL) / 86400) * 86400;
482 while((item = ll_iter_next(&itr)))
484 struct tm start_t, end_t;
486 localtime_r(&item->start, &start_t);
487 localtime_r(&item->end , &end_t);
489 strftime(tbuffer1, sizeof(tbuffer1) - 1, "%Y-%m-%d %H:%M %z", &start_t);
490 strftime(tbuffer2, sizeof(tbuffer2) - 1, "%Y-%m-%d %H:%M %z", &end_t);
492 char *entresname = get_tiername(item->id & 0xFFFF, item->caid, buf);
493 if(!entresname[0])
494 { entresname = get_provider(item->provid, item->caid, buf, sizeof(buf)); }
496 snprintf(tmpbuf, sizeof(tmpbuf) - 1, "%s Type: %s CAID: %04X Provid: %06X ID: %08X%08X Class: %08X StartDate: %s ExpireDate: %s Name: %s",
497 item->end > now ? "active " : "expired",
498 entitlement_type[item->type],
499 item->caid,
500 item->provid,
501 (uint32_t)(item->id >> 32),
502 (uint32_t)(item->id),
503 item->class,
504 tbuffer1,
505 tbuffer2,
506 entresname
508 monitor_send_details(tmpbuf, cl->tid);
513 static void monitor_process_details(char *arg)
515 uint32_t tid = 0; //using threadid 8 positions hex see oscam-log.c //FIXME untested but pid isnt working anyway with threading
516 struct s_client *cl = NULL, *cl1;
517 char sbuf[256];
519 if(!arg)
520 { cl = first_client; } // no arg - show master
521 else
523 if(sscanf(arg, "%X", &tid) == 1)
525 for(cl1 = first_client; cl1 ; cl1 = cl1->next)
526 if(cl1->tid == tid)
528 cl = cl1;
529 break;
534 if(!cl)
535 { monitor_send_details("Invalid TID", tid); }
536 else
538 //monitor_send_info(monitor_client_info('D', idx), 0); //FIXME
539 switch(cl->typ)
541 case 's':
542 monitor_process_details_master(sbuf, cl->tid);
543 break;
544 case 'c':
545 case 'm':
546 monitor_send_details(monitor_client_info(1, cl, sbuf), cl->tid);
547 break;
548 case 'r':
549 monitor_process_details_reader(cl);//with client->typ='r' client->ridx is always filled and valid, so no need checking
550 break;
551 case 'p':
552 monitor_send_details(monitor_client_info(1, cl, sbuf), cl->tid);
553 break;
556 monitor_send_info(NULL, 1);
559 static void monitor_send_login(void)
561 char buf[64];
562 struct s_client *cur_cl = cur_client();
563 struct monitor_data *module_data = cur_cl->module_data;
564 if(module_data->auth && cur_cl->account)
565 { snprintf(buf, sizeof(buf), "[A-0000]1|%s logged in\n", cur_cl->account->usr); }
566 else
567 { cs_strncpy(buf, "[A-0000]0|not logged in\n", sizeof(buf)); }
568 monitor_send_info(buf, 1);
571 static void monitor_login(char *usr)
573 char *pwd = NULL;
574 int8_t res = 0;
575 if((usr) && (pwd = strchr(usr, ' ')))
576 { * pwd++ = 0; }
577 if(pwd)
578 { res = monitor_auth_client(trim(usr), trim(pwd)); }
579 else
580 { res = monitor_auth_client(NULL, NULL); }
582 if(res == -1)
584 cs_disconnect_client(cur_client());
585 return;
587 monitor_send_login();
590 static void monitor_logsend(char *flag)
592 if(!flag) { return; } //no arg
594 struct s_client *cur_cl = cur_client();
595 if(strcmp(flag, "on"))
597 if(strcmp(flag, "onwohist"))
599 cur_cl->log = 0;
600 return;
604 if(cur_cl->log) // already on
605 { return; }
607 if(!strcmp(flag, "on") && cfg.loghistorylines)
609 if(cfg.loghistorylines && log_history)
611 LL_ITER it = ll_iter_create(log_history);
612 struct s_log_history *hist;
614 while((hist = (struct s_log_history*)ll_iter_next(&it)))
616 char p_usr[32], p_txt[512];
617 size_t pos1 = strcspn(hist->txt, "\t") + 1;
619 cs_strncpy(p_usr, hist->txt , pos1 > sizeof(p_usr) ? sizeof(p_usr) : pos1);
621 if((p_usr[0]) && ((cur_cl->monlvl > 1) || (cur_cl->account && !strcmp(p_usr, cur_cl->account->usr))))
623 snprintf(p_txt, sizeof(p_txt), "[LOG%03d]%s", cur_cl->logcounter, hist->txt + pos1);
624 cur_cl->logcounter = (cur_cl->logcounter + 1) % 1000;
625 monitor_send(p_txt);
631 cur_cl->log = 1;
634 static void monitor_set_debuglevel(char *flag)
636 if(flag)
638 cs_dblevel = atoi(flag);
639 #ifndef WITH_DEBUG
640 cs_log("*** Warning: Debug Support not compiled in ***");
641 #else
642 cs_log("%s debug_level=%d", "all", cs_dblevel);
643 #endif
647 static void monitor_get_account(void)
649 struct s_auth *account;
650 char buf[256];
651 int32_t count = 0;
653 for(account = cfg.account; (account); account = account->next)
655 count++;
656 snprintf(buf, sizeof(buf), "[U-----]%s\n", account->usr);
657 monitor_send_info(buf, 0);
659 snprintf(buf, sizeof(buf), "[U-----] %i User registered\n", count);
660 monitor_send_info(buf, 1);
661 return;
664 static void monitor_set_account(char *args)
666 struct s_auth *account;
667 char delimiter[] = " =";
668 char *ptr, *saveptr1 = NULL;
669 int32_t argidx, i, found;
670 char *argarray[3];
671 static const char *token[] = {"au", "sleep", "uniq", "monlevel", "group", "services", "betatunnel", "ident", "caid", "chid", "class", "hostname", "expdate", "keepalive", "disabled"};
672 int32_t tokencnt = sizeof(token) / sizeof(char *);
673 char buf[256], tmp[64];
675 argidx = 0;
676 found = 0;
678 snprintf(tmp, sizeof(tmp), "%s", args);
679 snprintf(buf, sizeof(buf), "[S-0000]setuser: %s check\n", tmp);
680 monitor_send_info(buf, 0);
682 ptr = strtok_r(args, delimiter, &saveptr1);
684 // resolve arguments
685 while(ptr != NULL)
687 argarray[argidx] = trim(ptr);
688 ptr = strtok_r(NULL, delimiter, &saveptr1);
689 argidx++;
692 if(argidx != 3)
694 snprintf(buf, sizeof(buf), "[S-0000]setuser: %s failed - wrong number of parameters (%d)\n", tmp, argidx);
695 monitor_send_info(buf, 0);
696 snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
697 monitor_send_info(buf, 1);
698 return;
701 //search account
702 for(account = cfg.account; (account) ; account = account->next)
704 if(!strcmp(argarray[0], account->usr))
706 found = 1;
707 break;
711 if(found != 1)
713 snprintf(buf, sizeof(buf), "[S-0000]setuser: %s failed - user %s not found\n", tmp , argarray[0]);
714 monitor_send_info(buf, 0);
715 snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
716 monitor_send_info(buf, 1);
717 return;
720 found = -1;
721 for(i = 0; i < tokencnt; i++)
723 if(!strcmp(argarray[1], token[i]))
725 // preparing the parameters before re-load
726 switch(i)
729 case 6:
730 tuntab_clear(&account->ttab);
731 break; //betatunnel
733 case 8:
734 caidtab_clear(&account->ctab);
735 break; //Caid
737 found = i;
741 if(found < 0)
743 snprintf(buf, sizeof(buf), "[S-0000]setuser: parameter %s not exist. possible values:\n", argarray[1]);
744 monitor_send_info(buf, 0);
745 for(i = 0; i < tokencnt; i++)
747 snprintf(buf, sizeof(buf), "[S-0000]%s\n", token[i]);
748 monitor_send_info(buf, 0);
750 snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
751 monitor_send_info(buf, 1);
752 return;
754 else
756 chk_account(token[found], argarray[2], account);
759 if(write_userdb() == 0)
760 { cs_reinit_clients(cfg.account); }
762 snprintf(buf, sizeof(buf), "[S-0000]setuser: %s done - param %s set to %s\n", tmp, argarray[1], argarray[2]);
763 monitor_send_info(buf, 1);
766 static void monitor_set_server(char *args)
768 char delimiter[] = "=";
769 char *ptr, *saveptr1;
770 int32_t argidx, i;
771 char *argarray[3];
772 static const char *token[] = {"clienttimeout", "fallbacktimeout", "clientmaxidle", "cachedelay", "bindwait", "netprio", "sleep", "unlockparental", "serialreadertimeout", "maxlogsize", "showecmdw", "waitforcards", "preferlocalcards"};
773 char buf[256];
775 argidx = 0;
776 ptr = strtok_r(args, delimiter, &saveptr1);
778 // resolve arguments
779 while(ptr != NULL)
781 argarray[argidx] = trim(ptr);
782 ptr = strtok_r(NULL, delimiter, &saveptr1);
783 argidx++;
786 if(argidx != 2)
788 snprintf(buf, sizeof(buf), "[S-0000]setserver failed - wrong number of parameters (%d)\n", argidx);
789 monitor_send_info(buf, 1);
790 return;
793 trim(argarray[0]);
794 trim(argarray[1]);
795 strtolower(argarray[0]);
797 for(i = 0; i < 13; i++)
798 if(!strcmp(argarray[0], token[i])) { break; }
800 if(i < 13)
802 config_set("global", token[i], argarray[1]);
803 snprintf(buf, sizeof(buf), "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
804 monitor_send_info(buf, 1);
806 else
808 snprintf(buf, sizeof(buf), "[S-0000]setserver failed - parameter %s not exist\n", argarray[0]);
809 monitor_send_info(buf, 1);
810 return;
813 /*Hide by blueven. Introduce new fallbacktimeout_percaid.
815 * if (cfg.ftimeout>=cfg.ctimeout) {
816 cfg.ftimeout = cfg.ctimeout - 100;
817 snprintf(buf, sizeof(buf), "[S-0000]setserver WARNING: fallbacktimeout adjusted to %u ms\n", cfg.ftimeout);
818 monitor_send_info(buf, 1);
820 //kill(first_client->pid, SIGUSR1);
823 #ifdef WEBIF
824 static void monitor_restart_server(void)
826 cs_restart_oscam();
828 #endif
830 static void monitor_list_commands(const char *args[], int32_t cmdcnt)
832 int32_t i;
833 for(i = 0; i < cmdcnt; i++)
835 char buf[64];
836 snprintf(buf, sizeof(buf), "[S-0000]commands: %s\n", args[i]);
837 if(i < cmdcnt - 1)
838 { monitor_send_info(buf, 0); }
839 else
840 { monitor_send_info(buf, 1); }
844 static int32_t monitor_process_request(char *req)
846 int32_t i, rc;
847 static const char *cmd[] = {"login",
848 "exit",
849 "log",
850 "status",
851 "shutdown",
852 "reload",
853 "details",
854 "version",
855 "debug",
856 "getuser",
857 "setuser",
858 "setserver",
859 "commands",
860 "keepalive",
861 "reread"
862 #ifdef WEBIF
863 , "restart"
864 #endif
867 int32_t cmdcnt = sizeof(cmd) / sizeof(char *); // Calculate the amount of items in array
868 char *arg;
869 struct s_client *cur_cl = cur_client();
870 struct monitor_data *module_data = cur_cl->module_data;
872 if((arg = strchr(req, ' ')))
874 *arg++ = 0;
875 trim(arg);
877 //trim(req);
879 if(!module_data->auth && strcmp(req, cmd[0]) != 0)
880 { monitor_login(NULL); }
882 for(rc = 1, i = 0; i < cmdcnt; i++)
883 if(!strcmp(req, cmd[i]))
885 switch(i)
887 case 0:
888 monitor_login(arg);
889 break; // login
890 case 1:
891 cs_disconnect_client(cur_cl);
892 break; // exit
893 case 2:
894 monitor_logsend(arg);
895 break; // log
896 case 3:
897 monitor_process_info();
898 break; // status
899 case 4:
900 if(cur_cl->monlvl > 3) { cs_exit_oscam(); }
901 break; // shutdown
902 case 5:
903 if(cur_cl->monlvl > 2) { cs_accounts_chk(); }
904 break; // reload
905 case 6:
906 monitor_process_details(arg);
907 break; // details
908 case 7:
909 monitor_send_details_version();
910 break; // version
911 case 8:
912 if(cur_cl->monlvl > 3) { monitor_set_debuglevel(arg); }
913 break; // debuglevel
914 case 9:
915 if(cur_cl->monlvl > 3) { monitor_get_account(); }
916 break; // getuser
917 case 10:
918 if(cur_cl->monlvl > 3) { monitor_set_account(arg); }
919 break; // setuser
920 case 11:
921 if(cur_cl->monlvl > 3) { monitor_set_server(arg); }
922 break; // setserver
923 case 12:
924 if(cur_cl->monlvl > 3) { monitor_list_commands(cmd, cmdcnt); }
925 break; // list commands
926 case 13:
927 if(cur_cl->monlvl > 3) { monitor_send_keepalive_ack(); }
928 break; // keepalive
929 case 14:
931 char buf[64]; // reread
932 snprintf(buf, sizeof(buf), "[S-0000]reread\n");
933 monitor_send_info(buf, 1);
934 cs_card_info();
935 break;
937 #ifdef WEBIF
938 case 15:
939 if(cur_cl->monlvl > 3) { monitor_restart_server(); }
940 break; // keepalive
941 #endif
942 default:
943 continue;
945 break;
947 return rc;
950 static void *monitor_server(struct s_client *client, uchar *mbuf, int32_t UNUSED(n))
952 client->typ = 'm';
953 monitor_process_request((char *)mbuf);
955 return NULL;
958 static void monitor_cleanup(struct s_client *client)
960 NULLFREE(client->module_data);
963 void module_monitor(struct s_module *ph)
965 ph->ptab.nports = 1;
966 ph->ptab.ports[0].s_port = cfg.mon_port;
967 ph->desc = "monitor";
968 ph->type = MOD_CONN_UDP;
969 IP_ASSIGN(ph->s_ip, cfg.mon_srvip);
970 ph->s_handler = monitor_server;
971 ph->recv = monitor_recv;
972 ph->cleanup = monitor_cleanup;
973 // ph->send_dcw=NULL;
975 #endif