source: trunk/module-monitor.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

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