source: trunk/module-monitor.c@ 1237

Last change on this file since 1237 was 1237, checked in by landlord, 11 years ago

New 'oscam.srvid' format introduced with example and documentation, backport of 'monitor-improvement' branch

File size: 14.4 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
9static int auth=0;
10
11static void monitor_check_ip()
12{
13 int ok=0;
14 struct s_ip *p_ip;
15
16 if (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 (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, auth=0; (account) && (!auth);)
37 {
38 if (account->monlvl)
39 auth=!(strcmp(usr, account->usr) | strcmp(pwd, account->pwd));
40 if (!auth)
41 account=account->next;
42 }
43 if (!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 (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) && (!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 auth=1;
75 }
76 if (!auth)
77 {
78 cs_auth_client((struct s_auth *)0, "invalid user");
79 cs_exit(0);
80 }
81 return(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 usleep(500L); // avoid lost udp-pakets ..
91 if (!client[idx].crypted)
92 return(sendto(client[idx].udp_fd, txt, strlen(txt), 0,
93 (struct sockaddr *)&client[idx].udp_sa,
94 sizeof(client[idx].udp_sa)));
95 buf[0]='&';
96 buf[9]=l=strlen(txt);
97 l=boundary(4, l+5)+5;
98 memcpy(buf+1, client[idx].ucrc, 4);
99 strcpy((char *)buf+10, txt);
100 memcpy(buf+5, i2b(4, crc32(0L, buf+10, l-10)), 4);
101 aes_encrypt_idx(idx, buf+5, l-5);
102 return(sendto(client[idx].udp_fd, buf, l, 0,
103 (struct sockaddr *)&client[idx].udp_sa,
104 sizeof(client[idx].udp_sa)));
105}
106
107#define monitor_send(t) monitor_send_idx(cs_idx, t)
108
109static int monitor_recv(uchar *buf, int l)
110{
111 int n;
112 uchar nbuf[3] = { 'U', 0, 0 };
113 static int bpos=0;
114 static uchar *bbuf=NULL;
115 if (!bbuf)
116 {
117 bbuf=(uchar *)malloc(l);
118 if (!bbuf)
119 {
120 cs_log("Cannot allocate memory (errno=%d)", errno);
121 cs_exit(1);
122 }
123 }
124 if (bpos)
125 memcpy(buf, bbuf, n=bpos);
126 else
127 n=recv_from_udpipe(buf);
128 bpos=0;
129 if (!n) return(buf[0]=0);
130 if (buf[0]=='&')
131 {
132 int bsize;
133 if (n<21) // 5+16 is minimum
134 {
135 cs_log("packet to short !");
136 return(buf[0]=0);
137 }
138 if (!secmon_auth_client(buf+1))
139 return(buf[0]=0);
140 aes_decrypt(buf+5, 16);
141 bsize=boundary(4, buf[9]+5)+5;
142// cs_log("n=%d bsize=%d", n, bsize);
143 if (n>bsize)
144 {
145// cs_log("DO >>>> copy-back");
146 memcpy(bbuf, buf+bsize, bpos=n-bsize);
147 n=bsize;
148 if (!write(client[cs_idx].ufd, nbuf, sizeof(nbuf))) cs_exit(1); // trigger new event
149 }
150 else if (n<bsize)
151 {
152 cs_log("packet-size mismatch !");
153 return(buf[0]=0);
154 }
155 aes_decrypt(buf+21, n-21);
156 if (memcmp(buf+5, i2b(4, crc32(0L, buf+10, n-10)), 4))
157 {
158 cs_log("CRC error ! wrong password ?");
159 return(buf[0]=0);
160 }
161 n=buf[9];
162 memmove(buf, buf+10, n);
163 }
164 else
165 {
166 uchar *p;
167 monitor_check_ip();
168 buf[n]='\0';
169 if ((p=(uchar *)strchr((char *)buf, 10)) && (bpos=n-(p-buf)-1))
170 {
171 memcpy(bbuf, p+1, bpos);
172 n=p-buf;
173 if (!write(client[cs_idx].ufd, nbuf, sizeof(nbuf))) cs_exit(1); // trigger new event
174 }
175 }
176 buf[n]='\0';
177 n=strlen(trim((char *)buf));
178 if (n) client[cs_idx].last=time((time_t *) 0);
179 return(n);
180}
181
182static void monitor_send_info(char *txt, int last)
183{
184 static int seq=0, counter=0;
185 static char btxt[256] = {0};
186 char buf[8];
187 if (txt)
188 {
189 if (!btxt[0])
190 {
191 counter=0;
192 txt[2]='B';
193 }
194 else
195 counter++;
196 sprintf(buf, "%03d", counter);
197 memcpy(txt+4, buf, 3);
198 txt[3]='0'+seq;
199 }
200 else
201 if (!last)
202 return;
203
204 if (!last)
205 {
206 if (btxt[0]) monitor_send(btxt);
207 strncpy(btxt, txt, sizeof(btxt));
208 return;
209 }
210
211 if (txt && btxt[0])
212 {
213 monitor_send(btxt);
214 txt[2]='E';
215 strncpy(btxt, txt, sizeof(btxt));
216 }
217 else
218 {
219 if (txt)
220 strncpy(btxt, txt, sizeof(btxt));
221 btxt[2]=(btxt[2]=='B') ? 'S' : 'E';
222 }
223
224 if (btxt[0])
225 {
226 monitor_send(btxt);
227 seq=(seq+1)%10;
228 }
229 btxt[0]=0;
230}
231
232static int cs_idx2ridx(int idx)
233{
234 int i;
235 for (i=0; i<CS_MAXREADER; i++)
236 if (reader[i].cs_idx==idx)
237 return(i);
238 return(-1);
239}
240
241char *monitor_get_srvname(int srvid, int caid){
242 int i;
243 struct s_srvid *this = cfg->srvid;
244 static char name[83];
245
246 for (name[0] = 0; this && (!name[0]); this = this->next)
247 if (this->srvid == srvid)
248 for (i=0; i<this->ncaid; i++)
249 if (this->caid[i] == caid)
250 strncpy(name, this->name, 32);
251
252 if (!name[0]) sprintf(name, "[%04X:%04X]", caid, srvid);
253 if (!srvid) name[0] = '\0';
254 return(name);
255}
256
257static char *monitor_get_proto(int idx)
258{
259 int i;
260 char *ctyp;
261 switch(client[idx].typ)
262 {
263 case 's': ctyp="server" ; break;
264 case 'n': ctyp="resolver" ; break;
265 case 'l': ctyp="logger" ; break;
266 case 'p':
267 case 'r': if ((i=cs_idx2ridx(idx))<0) // should never happen
268 ctyp=(client[idx].typ=='p') ? "proxy" : "reader";
269 else
270 {
271 switch(reader[i].typ) // TODO like ph
272 {
273 case R_MOUSE : ctyp="mouse"; break;
274 case R_INTERNAL: ctyp="intern"; break;
275 case R_SMART : ctyp="smartreader"; break;
276 case R_CAMD35 : ctyp="camd 3.5x";break;
277 case R_CAMD33 : ctyp="camd 3.3x";break;
278 case R_NEWCAMD : ctyp="newcamd"; break;
279 case R_RADEGAST: ctyp="radegast"; break;
280 case R_SERIAL : ctyp="serial"; break;
281 case R_GBOX : ctyp="gbox"; break;
282 default : ctyp="unknown"; break;
283 }
284 }
285 break;
286 default : ctyp=ph[client[idx].ctyp].desc;
287 }
288 return(ctyp);
289}
290
291static char *monitor_client_info(char id, int i)
292{
293 static char sbuf[256];
294
295 sbuf[0]='\0';
296 if (client[i].pid)
297 {
298 char ldate[16], ltime[16], *usr;
299 int lsec, isec, cnr, con, cau;
300 time_t now;
301 struct tm *lt;
302 now=time((time_t)0);
303
304 if ((cfg->mon_hideclient_to <= 0) ||
305 (((now-client[i].lastecm)/60)<cfg->mon_hideclient_to) ||
306 (((now-client[i].lastemm)/60)<cfg->mon_hideclient_to) ||
307 (client[i].typ!='c'))
308 {
309 lsec=now-client[i].login;
310 isec=now-client[i].last;
311 usr=client[i].usr;
312 if (((client[i].typ=='r') || (client[i].typ=='p')) &&
313 (con=cs_idx2ridx(i))>=0)
314 usr=reader[con].label;
315 if (client[i].dup)
316 con=2;
317 else
318 if ((client[i].tosleep) &&
319 (now-client[i].lastswitch>client[i].tosleep))
320 con=1;
321 else
322 con=0;
323 if (i-cdiff>0)
324 cnr=i-cdiff;
325 else
326 cnr=(i>1) ? i-1 : 0;
327 if( (cau=client[i].au+1) )
328 if ((now-client[i].lastemm)/60>cfg->mon_aulow)
329 cau=-cau;
330 lt=localtime(&client[i].login);
331 sprintf(ldate, "%2d.%02d.%02d",
332 lt->tm_mday, lt->tm_mon+1, lt->tm_year % 100);
333 sprintf(ltime, "%2d:%02d:%02d",
334 lt->tm_hour, lt->tm_min, lt->tm_sec);
335 sprintf(sbuf, "[%c--CCC]%d|%c|%d|%s|%d|%d|%s|%d|%s|%s|%s|%d|%04X:%04X|%s|%d|%d\n",
336 id, client[i].pid, client[i].typ, cnr, usr, cau, client[i].crypted,
337 cs_inet_ntoa(client[i].ip), client[i].port, monitor_get_proto(i),
338 ldate, ltime, lsec, client[i].last_caid, client[i].last_srvid,
339 monitor_get_srvname(client[i].last_srvid, client[i].last_caid), isec, con);
340 }
341 }
342 return(sbuf);
343}
344
345static void monitor_process_info(){
346 int i;
347 time_t now = time((time_t)0);
348
349 for (i = 0; i < CS_MAXPID; i++){
350 if ((cfg->mon_hideclient_to <= 0) ||
351 ( now-client[i].lastecm < cfg->mon_hideclient_to) ||
352 ( now-client[i].lastemm < cfg->mon_hideclient_to) ||
353 ( client[i].typ != 'c')){
354 if (client[i].pid) {
355 if ((client[cs_idx].monlvl < 2) && (client[i].typ != 's')) {
356 if ((strcmp(client[cs_idx].usr, client[i].usr)) ||
357 ((client[i].typ != 'c') && (client[i].typ != 'm')))
358 continue;
359 }
360 monitor_send_info(monitor_client_info('I', i), 0);
361 }
362 }
363 }
364 monitor_send_info(NULL, 1);
365}
366
367static void monitor_send_details(char *txt, int pid)
368{
369 char buf[256];
370 snprintf(buf, 255, "[D-----]%d|%s\n", pid, txt);
371 monitor_send_info(buf, 0);
372}
373
374static void monitor_send_details_version()
375{
376 char buf[256];
377 sprintf(buf, "[A-0000]version=%s, build=%s, system=%s%s", CS_VERSION_X, CS_SVN_VERSION, cs_platform(buf+100), buf+200);
378 monitor_send_info(buf, 1);
379}
380
381static void monitor_process_details_master(char *buf, int pid)
382{
383 if (cfg->nice!=99)
384 sprintf(buf+200, ", nice=%d", cfg->nice);
385 else
386 buf[200]='\0';
387 sprintf(buf, "version=%s#%s, system=%s%s", CS_VERSION_X, CS_SVN_VERSION, cs_platform(buf+100), buf+200);
388 monitor_send_details(buf, pid);
389
390 sprintf(buf, "max. clients=%d, client max. idle=%ld sec", CS_MAXPID-2, cfg->cmaxidle);
391 monitor_send_details(buf, pid);
392
393 if( cfg->max_log_size )
394 sprintf(buf+200, "%d Kb", cfg->max_log_size);
395 else
396 strcpy(buf+200, "unlimited");
397 sprintf(buf, "max. logsize=%s", buf+200);
398 monitor_send_details(buf, pid);
399
400 sprintf(buf, "client timeout=%lu ms, cache delay=%ld ms", cfg->ctimeout, cfg->delay);
401 monitor_send_details(buf, pid);
402
403//#ifdef CS_NOSHM
404// sprintf(buf, "shared memory initialized (size=%d, fd=%d)", shmsize, shmid);
405//#else
406// sprintf(buf, "shared memory initialized (size=%d, id=%d)", shmsize, shmid);
407//#endif
408// monitor_send_details(buf, pid);
409}
410
411#ifdef CS_RDR_INIT_HIST
412static void monitor_process_details_reader(int pid, int idx)
413{
414 int r_idx;
415 char *p;
416 if ((r_idx=cs_idx2ridx(idx))>=0)
417 for (p=(char *)reader[r_idx].init_history; *p; p+=strlen(p)+1)
418 monitor_send_details(p, pid);
419 else
420 monitor_send_details("Missing reader index !", pid);
421}
422#endif
423
424static void monitor_process_details(char *arg)
425{
426 int pid, idx;
427 char sbuf[256];
428 if (!arg) return;
429 if ((idx=idx_from_pid(pid=atoi(arg)))<0)
430 monitor_send_details("Invalid PID", pid);
431 else
432 {
433 monitor_send_info(monitor_client_info('D', idx), 0);
434 switch(client[idx].typ)
435 {
436 case 's':
437 monitor_process_details_master(sbuf, pid);
438 break;
439 case 'c': case 'm':
440 break;
441 case 'r':
442#ifdef CS_RDR_INIT_HIST
443 monitor_process_details_reader(pid, idx);
444#endif
445 break;
446 case 'p':
447 break;
448 }
449 }
450 monitor_send_info(NULL, 1);
451}
452
453static void monitor_send_login(void)
454{
455 char buf[64];
456 if (auth)
457 sprintf(buf, "[A-0000]1|%s logged in\n", client[cs_idx].usr);
458 else
459 strcpy(buf, "[A-0000]0|not logged in\n");
460 monitor_send_info(buf, 1);
461}
462
463static void monitor_login(char *usr)
464{
465 char *pwd=NULL;
466 if ((usr) && (pwd=strchr(usr, ' ')))
467 *pwd++=0;
468 if (pwd)
469 monitor_auth_client(trim(usr), trim(pwd));
470 else
471 monitor_auth_client(NULL, NULL);
472 monitor_send_login();
473}
474
475static void monitor_logsend(char *flag)
476{
477#ifdef CS_LOGHISTORY
478 int i;
479#endif
480 if (strcmp(flag, "on")) {
481 if (strcmp(flag, "onwohist")) {
482 client[cs_idx].log=0;
483 return;
484 }
485 }
486
487 if (client[cs_idx].log) // already on
488 return;
489#ifdef CS_LOGHISTORY
490 if (!strcmp(flag, "on")){
491 for (i=(*loghistidx+3) % CS_MAXLOGHIST; i!=*loghistidx; i=(i+1) % CS_MAXLOGHIST)
492 {
493 char *p_usr, *p_txt;
494 p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
495 p_txt=p_usr+32;
496 if ((p_txt[0]) &&
497 ((client[cs_idx].monlvl>1) || (!strcmp(p_usr, client[cs_idx].usr))))
498 {
499 char sbuf[8];
500 sprintf(sbuf, "%03d", client[cs_idx].logcounter);
501 client[cs_idx].logcounter=(client[cs_idx].logcounter+1) % 1000;
502 memcpy(p_txt+4, sbuf, 3);
503 monitor_send(p_txt);
504 }
505 }
506 }
507#endif
508 client[cs_idx].log=1;
509}
510static void monitor_set_debuglevel(char *flag)
511{
512 cs_dblevel^=atoi(flag);
513 kill(client[0].pid, SIGUSR1);
514}
515
516static int monitor_process_request(char *req)
517{
518 int i, rc;
519 char *cmd[]={"login", "exit", "log", "status", "shutdown", "reload", "details", "version", "debug"};
520 char *arg;
521 if( (arg=strchr(req, ' ')) )
522 {
523 *arg++=0;
524 trim(arg);
525 }
526 trim(req);
527 if ((!auth) && (strcmp(req, cmd[0])))
528 monitor_login(NULL);
529 for (rc=1, i=0; i<9; i++)
530 if (!strcmp(req, cmd[i]))
531 {
532 switch(i)
533 {
534 case 0: monitor_login(arg); break; // login
535 case 1: rc=0; break; // exit
536 case 2: monitor_logsend(arg); break; // log
537 case 3: monitor_process_info(); break; // status
538 case 4: if (client[cs_idx].monlvl>3)
539 kill(client[0].pid, SIGQUIT); // shutdown
540 break;
541 case 5: if (client[cs_idx].monlvl>2)
542 kill(client[0].pid, SIGHUP); // reload
543 break;
544 case 6: monitor_process_details(arg); break; // details
545 case 7: monitor_send_details_version(); break;
546 case 8: if (client[cs_idx].monlvl>3)
547 monitor_set_debuglevel(arg); // debuglevel
548 break;
549 default: continue;
550 }
551 break;
552 }
553 return(rc);
554}
555
556static void monitor_server()
557{
558 int n;
559 client[cs_idx].typ='m';
560 while (((n=process_input(mbuf, sizeof(mbuf), cfg->cmaxidle))>=0) &&
561 monitor_process_request((char *)mbuf));
562 cs_disconnect_client();
563}
564
565void module_monitor(struct s_module *ph)
566{
567 static PTAB ptab;
568 ptab.ports[0].s_port = cfg->mon_port;
569 ph->ptab = &ptab;
570 ph->ptab->nports = 1;
571
572 if (cfg->mon_aulow<1)
573 cfg->mon_aulow=30;
574 strcpy(ph->desc, "monitor");
575 ph->type=MOD_CONN_UDP;
576 ph->multi=0;
577 ph->watchdog=1;
578 ph->s_ip=cfg->mon_srvip;
579 ph->s_handler=monitor_server;
580 ph->recv=monitor_recv;
581// ph->send_dcw=NULL;
582}
Note: See TracBrowser for help on using the repository browser.