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 | static int auth=0;
|
---|
10 |
|
---|
11 | static 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 |
|
---|
26 | static 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 |
|
---|
52 | static 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 |
|
---|
84 | int 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 |
|
---|
109 | static 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, l);
|
---|
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 |
|
---|
182 | static 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 |
|
---|
232 | 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 |
|
---|
241 | char *monitor_get_srvname(int id)
|
---|
242 | {
|
---|
243 | struct s_srvid *this=cfg->srvid;
|
---|
244 | static char name[83];
|
---|
245 | for (name[0]=0; this && (!name[0]); this=this->next)
|
---|
246 | if (this->srvid==id)
|
---|
247 | strncpy(name, this->name, 32);
|
---|
248 | if (!name[0]) sprintf(name, "[%04X]", id);
|
---|
249 | if (!id) name[0]='\0';
|
---|
250 | return(name);
|
---|
251 | }
|
---|
252 |
|
---|
253 | char *monitor_get_proto(int idx)
|
---|
254 | {
|
---|
255 | int i;
|
---|
256 | char *ctyp;
|
---|
257 | switch(client[idx].typ)
|
---|
258 | {
|
---|
259 | case 's': ctyp="server" ; break;
|
---|
260 | case 'n': ctyp="resolver" ; break;
|
---|
261 | case 'l': ctyp="logger" ; break;
|
---|
262 | case 'p':
|
---|
263 | case 'r': if ((i=cs_idx2ridx(idx))<0) // should never happen
|
---|
264 | ctyp=(client[idx].typ=='p') ? "proxy" : "reader";
|
---|
265 | else
|
---|
266 | {
|
---|
267 | switch(reader[i].typ) // TODO like ph
|
---|
268 | {
|
---|
269 | case R_MOUSE : ctyp="mouse"; break;
|
---|
270 | case R_INTERNAL: ctyp="intern"; break;
|
---|
271 | case R_SMART : ctyp="smartreader"; break;
|
---|
272 | case R_CAMD35 : ctyp="camd 3.5x";break;
|
---|
273 | case R_CAMD33 : ctyp="camd 3.3x";break;
|
---|
274 | case R_NEWCAMD : ctyp="newcamd"; break;
|
---|
275 | case R_RADEGAST: ctyp="radegast"; break;
|
---|
276 | case R_SERIAL : ctyp="serial"; break;
|
---|
277 | case R_GBOX : ctyp="gbox"; break;
|
---|
278 | #ifdef HAVE_PCSC
|
---|
279 | case R_PCSC : ctyp="pcsc"; break;
|
---|
280 | #endif
|
---|
281 | case R_CCCAM : ctyp="cccam"; break;
|
---|
282 | case R_CS378X : ctyp="cs378x"; break;
|
---|
283 | default : ctyp="unknown"; break;
|
---|
284 | }
|
---|
285 | }
|
---|
286 | break;
|
---|
287 | default : ctyp=ph[client[idx].ctyp].desc;
|
---|
288 | }
|
---|
289 | return(ctyp);
|
---|
290 | }
|
---|
291 |
|
---|
292 | static char *monitor_client_info(char id, int i)
|
---|
293 | {
|
---|
294 | static char sbuf[256];
|
---|
295 |
|
---|
296 | sbuf[0]='\0';
|
---|
297 | if (client[i].pid)
|
---|
298 | {
|
---|
299 | char ldate[16], ltime[16], *usr;
|
---|
300 | int lsec, isec, cnr, con, cau;
|
---|
301 | time_t now;
|
---|
302 | struct tm *lt;
|
---|
303 | now=time((time_t)0);
|
---|
304 |
|
---|
305 | if ((cfg->mon_hideclient_to <= 0) ||
|
---|
306 | (((now-client[i].lastecm)/60)<cfg->mon_hideclient_to) ||
|
---|
307 | (((now-client[i].lastemm)/60)<cfg->mon_hideclient_to) ||
|
---|
308 | (client[i].typ!='c'))
|
---|
309 | {
|
---|
310 | lsec=now-client[i].login;
|
---|
311 | isec=now-client[i].last;
|
---|
312 | usr=client[i].usr;
|
---|
313 | if (((client[i].typ=='r') || (client[i].typ=='p')) &&
|
---|
314 | (con=cs_idx2ridx(i))>=0)
|
---|
315 | usr=reader[con].label;
|
---|
316 | if (client[i].dup)
|
---|
317 | con=2;
|
---|
318 | else
|
---|
319 | if ((client[i].tosleep) &&
|
---|
320 | (now-client[i].lastswitch>client[i].tosleep))
|
---|
321 | con=1;
|
---|
322 | else
|
---|
323 | con=0;
|
---|
324 | if (i-cdiff>0)
|
---|
325 | cnr=i-cdiff;
|
---|
326 | else
|
---|
327 | cnr=(i>1) ? i-1 : 0;
|
---|
328 | if( (cau=client[i].au+1) )
|
---|
329 | if ((now-client[i].lastemm)/60>cfg->mon_aulow)
|
---|
330 | cau=-cau;
|
---|
331 | lt=localtime(&client[i].login);
|
---|
332 | sprintf(ldate, "%2d.%02d.%02d",
|
---|
333 | lt->tm_mday, lt->tm_mon+1, lt->tm_year % 100);
|
---|
334 | sprintf(ltime, "%2d:%02d:%02d",
|
---|
335 | lt->tm_hour, lt->tm_min, lt->tm_sec);
|
---|
336 | sprintf(sbuf, "[%c--CCC]%d|%c|%d|%s|%d|%d|%s|%d|%s|%s|%s|%d|%04X:%04X|%s|%d|%d\n",
|
---|
337 | id, client[i].pid, client[i].typ, cnr, usr, cau, client[i].crypted,
|
---|
338 | cs_inet_ntoa(client[i].ip), client[i].port, monitor_get_proto(i),
|
---|
339 | ldate, ltime, lsec, client[i].last_caid, client[i].last_srvid,
|
---|
340 | monitor_get_srvname(client[i].last_srvid), isec, con);
|
---|
341 | }
|
---|
342 | }
|
---|
343 | return(sbuf);
|
---|
344 | }
|
---|
345 |
|
---|
346 | static void monitor_process_info()
|
---|
347 | {
|
---|
348 | int i;
|
---|
349 | time_t now;
|
---|
350 |
|
---|
351 | now=time((time_t)0);
|
---|
352 | for (i=0; i<CS_MAXPID; i++)
|
---|
353 | if ((cfg->mon_hideclient_to <= 0) ||
|
---|
354 | (((now-client[i].lastecm)/60)<cfg->mon_hideclient_to) ||
|
---|
355 | (((now-client[i].lastemm)/60)<cfg->mon_hideclient_to) ||
|
---|
356 | (client[i].typ!='c'))
|
---|
357 | if (client[i].pid)
|
---|
358 | {
|
---|
359 | if ((client[cs_idx].monlvl<2) && (client[i].typ!='s'))
|
---|
360 | {
|
---|
361 | if ((strcmp(client[cs_idx].usr, client[i].usr)) ||
|
---|
362 | ((client[i].typ!='c') && (client[i].typ!='m')))
|
---|
363 | continue;
|
---|
364 | }
|
---|
365 | monitor_send_info(monitor_client_info('I', i), 0);
|
---|
366 | }
|
---|
367 | monitor_send_info(NULL, 1);
|
---|
368 | }
|
---|
369 |
|
---|
370 | static void monitor_send_details(char *txt, int pid)
|
---|
371 | {
|
---|
372 | char buf[256];
|
---|
373 | snprintf(buf, 255, "[D-----]%d|%s\n", pid, txt);
|
---|
374 | monitor_send_info(buf, 0);
|
---|
375 | }
|
---|
376 |
|
---|
377 | static void monitor_send_details_version()
|
---|
378 | {
|
---|
379 | char buf[256];
|
---|
380 | sprintf(buf, "[V-0000]version=%s, build=%s, system=%s%s\n", CS_VERSION_X, CS_SVN_VERSION, cs_platform(buf+100), buf+200);
|
---|
381 | monitor_send_info(buf, 1);
|
---|
382 | }
|
---|
383 |
|
---|
384 | static void monitor_process_details_master(char *buf, int pid)
|
---|
385 | {
|
---|
386 | if (cfg->nice!=99)
|
---|
387 | sprintf(buf+200, ", nice=%d", cfg->nice);
|
---|
388 | else
|
---|
389 | buf[200]='\0';
|
---|
390 | sprintf(buf, "version=%s#%s, system=%s%s", CS_VERSION_X, CS_SVN_VERSION, cs_platform(buf+100), buf+200);
|
---|
391 | monitor_send_details(buf, pid);
|
---|
392 |
|
---|
393 | sprintf(buf, "max. clients=%d, client max. idle=%d sec", CS_MAXPID-2, cfg->cmaxidle);
|
---|
394 | monitor_send_details(buf, pid);
|
---|
395 |
|
---|
396 | if( cfg->max_log_size )
|
---|
397 | sprintf(buf+200, "%d Kb", cfg->max_log_size);
|
---|
398 | else
|
---|
399 | strcpy(buf+200, "unlimited");
|
---|
400 | sprintf(buf, "max. logsize=%s", buf+200);
|
---|
401 | monitor_send_details(buf, pid);
|
---|
402 |
|
---|
403 | sprintf(buf, "client timeout=%lu ms, cache delay=%ld ms", cfg->ctimeout, cfg->delay);
|
---|
404 | monitor_send_details(buf, pid);
|
---|
405 |
|
---|
406 | //#ifdef CS_NOSHM
|
---|
407 | // sprintf(buf, "shared memory initialized (size=%d, fd=%d)", shmsize, shmid);
|
---|
408 | //#else
|
---|
409 | // sprintf(buf, "shared memory initialized (size=%d, id=%d)", shmsize, shmid);
|
---|
410 | //#endif
|
---|
411 | // monitor_send_details(buf, pid);
|
---|
412 | }
|
---|
413 |
|
---|
414 | #ifdef CS_RDR_INIT_HIST
|
---|
415 | static void monitor_process_details_reader(int pid, int idx)
|
---|
416 | {
|
---|
417 | int r_idx;
|
---|
418 | char *p;
|
---|
419 | if ((r_idx=cs_idx2ridx(idx))>=0)
|
---|
420 | for (p=(char *)reader[r_idx].init_history; *p; p+=strlen(p)+1)
|
---|
421 | monitor_send_details(p, pid);
|
---|
422 | else
|
---|
423 | monitor_send_details("Missing reader index !", pid);
|
---|
424 | }
|
---|
425 | #endif
|
---|
426 |
|
---|
427 | static void monitor_process_details(char *arg)
|
---|
428 | {
|
---|
429 | int pid, idx;
|
---|
430 | char sbuf[256];
|
---|
431 | if (!arg) return;
|
---|
432 | if ((idx=idx_from_pid(pid=atoi(arg)))<0)
|
---|
433 | monitor_send_details("Invalid PID", pid);
|
---|
434 | else
|
---|
435 | {
|
---|
436 | monitor_send_info(monitor_client_info('D', idx), 0);
|
---|
437 | switch(client[idx].typ)
|
---|
438 | {
|
---|
439 | case 's':
|
---|
440 | monitor_process_details_master(sbuf, pid);
|
---|
441 | break;
|
---|
442 | case 'c': case 'm':
|
---|
443 | break;
|
---|
444 | case 'r':
|
---|
445 | #ifdef CS_RDR_INIT_HIST
|
---|
446 | monitor_process_details_reader(pid, idx);
|
---|
447 | #endif
|
---|
448 | break;
|
---|
449 | case 'p':
|
---|
450 | break;
|
---|
451 | }
|
---|
452 | }
|
---|
453 | monitor_send_info(NULL, 1);
|
---|
454 | }
|
---|
455 |
|
---|
456 | static void monitor_send_login(void)
|
---|
457 | {
|
---|
458 | char buf[64];
|
---|
459 | if (auth)
|
---|
460 | sprintf(buf, "[A-0000]1|%s logged in\n", client[cs_idx].usr);
|
---|
461 | else
|
---|
462 | strcpy(buf, "[A-0000]0|not logged in\n");
|
---|
463 | monitor_send_info(buf, 1);
|
---|
464 | }
|
---|
465 |
|
---|
466 | static void monitor_login(char *usr)
|
---|
467 | {
|
---|
468 | char *pwd=NULL;
|
---|
469 | if ((usr) && (pwd=strchr(usr, ' ')))
|
---|
470 | *pwd++=0;
|
---|
471 | if (pwd)
|
---|
472 | monitor_auth_client(trim(usr), trim(pwd));
|
---|
473 | else
|
---|
474 | monitor_auth_client(NULL, NULL);
|
---|
475 | monitor_send_login();
|
---|
476 | }
|
---|
477 |
|
---|
478 | static void monitor_logsend(char *flag)
|
---|
479 | {
|
---|
480 | #ifdef CS_LOGHISTORY
|
---|
481 | int i;
|
---|
482 | #endif
|
---|
483 | if (strcmp(flag, "on")) {
|
---|
484 | if (strcmp(flag, "onwohist")) {
|
---|
485 | client[cs_idx].log=0;
|
---|
486 | return;
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | if (client[cs_idx].log) // already on
|
---|
491 | return;
|
---|
492 | #ifdef CS_LOGHISTORY
|
---|
493 | if (!strcmp(flag, "on")){
|
---|
494 | for (i=(*loghistidx+3) % CS_MAXLOGHIST; i!=*loghistidx; i=(i+1) % CS_MAXLOGHIST)
|
---|
495 | {
|
---|
496 | char *p_usr, *p_txt;
|
---|
497 | p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
|
---|
498 | p_txt=p_usr+32;
|
---|
499 | if ((p_txt[0]) &&
|
---|
500 | ((client[cs_idx].monlvl>1) || (!strcmp(p_usr, client[cs_idx].usr))))
|
---|
501 | {
|
---|
502 | char sbuf[8];
|
---|
503 | sprintf(sbuf, "%03d", client[cs_idx].logcounter);
|
---|
504 | client[cs_idx].logcounter=(client[cs_idx].logcounter+1) % 1000;
|
---|
505 | memcpy(p_txt+4, sbuf, 3);
|
---|
506 | monitor_send(p_txt);
|
---|
507 | }
|
---|
508 | }
|
---|
509 | }
|
---|
510 | #endif
|
---|
511 | client[cs_idx].log=1;
|
---|
512 | }
|
---|
513 | static void monitor_set_debuglevel(char *flag)
|
---|
514 | {
|
---|
515 | cs_dblevel^=atoi(flag);
|
---|
516 | kill(client[0].pid, SIGUSR1);
|
---|
517 | }
|
---|
518 |
|
---|
519 | static void monitor_set_account(char *args)
|
---|
520 | {
|
---|
521 | struct s_auth *account;
|
---|
522 | char delimiter[] = " =";
|
---|
523 | char *ptr, *ptr1;
|
---|
524 | int argidx, i, found;
|
---|
525 | char *argarray[3];
|
---|
526 | char *token[]={"au", "sleep", "uniq", "monlevel", "group", "services", "betatunnel", "ident", "caid", "chid", "class", "hostname"};
|
---|
527 | char buf[256];
|
---|
528 |
|
---|
529 | argidx=0;
|
---|
530 | found=0;
|
---|
531 |
|
---|
532 | ptr = strtok(args, delimiter);
|
---|
533 |
|
---|
534 | // resolve arguments
|
---|
535 | while(ptr != NULL) {
|
---|
536 | argarray[argidx]=trim(ptr);
|
---|
537 | ptr = strtok(NULL, delimiter);
|
---|
538 | argidx++;
|
---|
539 | }
|
---|
540 |
|
---|
541 | if(argidx != 3) {
|
---|
542 | sprintf(buf, "[S-0000]setuser failed - wrong number of parameters (%d)\n", argidx);
|
---|
543 | monitor_send_info(buf, 1);
|
---|
544 | //cs_log("setuser failed - wrong number of parameters (%d)", argidx);
|
---|
545 | return;
|
---|
546 | }
|
---|
547 |
|
---|
548 | //search account
|
---|
549 | for (account=cfg->account; (account) ; account=account->next){
|
---|
550 | if (!strcmp(argarray[0], account->usr)){
|
---|
551 | found=1;
|
---|
552 | break;
|
---|
553 | }
|
---|
554 | }
|
---|
555 |
|
---|
556 | if (found != 1){
|
---|
557 | sprintf(buf, "[S-0000]setuser failed - user %s not found\n", argarray[0]);
|
---|
558 | monitor_send_info(buf, 1);
|
---|
559 | //cs_log("setuser failed - user %s not found", argarray[0]);
|
---|
560 | return;
|
---|
561 | }
|
---|
562 |
|
---|
563 | for (i=0; i<12; i++){
|
---|
564 | if (!strcmp(argarray[1], token[i])){
|
---|
565 | switch(i){
|
---|
566 | case 0: strtolower(argarray[2]);
|
---|
567 | for (i=0; i<CS_MAXREADER; i++)
|
---|
568 | if ((reader[i].label[0]) && (!strncmp(reader[i].label, argarray[2], strlen(reader[i].label))))
|
---|
569 | account->au=i;
|
---|
570 | break; //au
|
---|
571 | case 1: account->tosleep=atoi(argarray[2]);
|
---|
572 | break; //sleep
|
---|
573 | case 2: account->uniq=atoi(argarray[2]);
|
---|
574 | break; //unique
|
---|
575 | case 3: account->monlvl=atoi(argarray[2]);
|
---|
576 | break; //monlevel
|
---|
577 | case 4: for (ptr1=strtok(argarray[2], ","); ptr1; ptr1=strtok(NULL, ",")) {
|
---|
578 | int g;
|
---|
579 | g=atoi(ptr1);
|
---|
580 | if ((g>0) && (g<33)) account->grp|=(1<<(g-1));
|
---|
581 | }
|
---|
582 | break; //group
|
---|
583 | case 5: chk_services(argarray[2], &account->sidtabok, &account->sidtabno);
|
---|
584 | break; //services
|
---|
585 | case 6: chk_tuntab(argarray[2], &account->ttab);
|
---|
586 | break; //betatunnel
|
---|
587 | case 7: chk_ftab(argarray[2], &account->ftab, "user", account->usr, "provid");
|
---|
588 | break; //ident
|
---|
589 | case 8: chk_caidtab(argarray[2], &account->ctab);
|
---|
590 | break; //caid
|
---|
591 | case 9: chk_ftab(argarray[2], &account->fchid, "user", account->usr, "chid");
|
---|
592 | break; //chid
|
---|
593 | case 10:chk_cltab(argarray[2], &account->cltab);
|
---|
594 | break; //class
|
---|
595 | case 11:strncpy((char *)account->dyndns, argarray[2], sizeof(account->dyndns)-1);
|
---|
596 | break;
|
---|
597 | //hostname
|
---|
598 | default: sprintf(buf, "[S-0000]setuser failed - parameter %s not exist", argarray[1]);
|
---|
599 | monitor_send_info(buf, 1);
|
---|
600 | //cs_log("setuser failed - parameter %s not exist", argarray[1]);
|
---|
601 | }
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | cs_reinit_clients();
|
---|
606 |
|
---|
607 | sprintf(buf, "[S-0000]setuser %s done - param %s set to %s\n", argarray[0], argarray[1], argarray[2]);
|
---|
608 | monitor_send_info(buf, 1);
|
---|
609 | //cs_log("setuser %s done - param %s set to %s", argarray[0], argarray[1], argarray[2]);
|
---|
610 | }
|
---|
611 |
|
---|
612 | static void monitor_set_server(char *args)
|
---|
613 | {
|
---|
614 | char delimiter[] = "=";
|
---|
615 | char *ptr;
|
---|
616 | int argidx, i, found;
|
---|
617 | char *argarray[3];
|
---|
618 | char *token[]={"clienttimeout", "fallbacktimeout", "clientmaxidle", "cachedelay", "bindwait", "netprio", "resolvedelay", "sleep", "unlockparental", "serialreadertimeout", "maxlogsize", "showecmdw", "waitforcards", "preferlocalcards"};
|
---|
619 | char buf[256];
|
---|
620 |
|
---|
621 | argidx=0;
|
---|
622 | found=0;
|
---|
623 |
|
---|
624 | ptr = strtok(args, delimiter);
|
---|
625 |
|
---|
626 | // resolve arguments
|
---|
627 | while(ptr != NULL) {
|
---|
628 | argarray[argidx]=trim(ptr);
|
---|
629 | ptr = strtok(NULL, delimiter);
|
---|
630 | argidx++;
|
---|
631 | }
|
---|
632 |
|
---|
633 | if(argidx != 2) {
|
---|
634 | sprintf(buf, "[S-0000]setserver failed - wrong number of parameters (%d)\n", argidx);
|
---|
635 | monitor_send_info(buf, 1);
|
---|
636 | return;
|
---|
637 | }
|
---|
638 |
|
---|
639 | trim(argarray[0]);
|
---|
640 | trim(argarray[1]);
|
---|
641 | strtolower(argarray[0]);
|
---|
642 |
|
---|
643 | for (i=0; i<14; i++){
|
---|
644 | if (!strcmp(argarray[0], token[i])){
|
---|
645 | switch(i){
|
---|
646 | case 0: cfg->ctimeout = atoi(argarray[1]); //clienttimeout
|
---|
647 | if (cfg->ctimeout < 100)
|
---|
648 | cfg->ctimeout *= 1000;
|
---|
649 | break;
|
---|
650 | case 1: cfg->ftimeout = atoi(argarray[1]); //fallbacktimeout
|
---|
651 | if (cfg->ftimeout < 100)
|
---|
652 | cfg->ftimeout *= 1000;
|
---|
653 | break;
|
---|
654 | case 2: cfg->cmaxidle=atoi(argarray[1]); //clientmaxidle
|
---|
655 | break;
|
---|
656 | case 3: cfg->delay=atoi(argarray[1]); //cachedelay
|
---|
657 | break;
|
---|
658 | case 4: cfg->bindwait=atoi(argarray[1]); //bindwait
|
---|
659 | break;
|
---|
660 | case 5: cfg->netprio=atoi(argarray[1]); //netprio
|
---|
661 | break;
|
---|
662 | case 6: cfg->resolvedelay=atoi(argarray[1]);//resolvedelay
|
---|
663 | break;
|
---|
664 | case 7: cfg->tosleep=atoi(argarray[1]); //sleep
|
---|
665 | break;
|
---|
666 | case 8: cfg->ulparent=atoi(argarray[1]); //unlockparental
|
---|
667 | break;
|
---|
668 | case 9: if (cfg->srtimeout < 100)
|
---|
669 | cfg->srtimeout = atoi(argarray[1]) * 1000; //serialreadertimeout
|
---|
670 | else
|
---|
671 | cfg->srtimeout = atoi(argarray[1]);
|
---|
672 | if( cfg->srtimeout <=0 )
|
---|
673 | cfg->srtimeout=1500;
|
---|
674 | break;
|
---|
675 | case 10:cfg->max_log_size=atoi(argarray[1]); //maxlogsize
|
---|
676 | if( cfg->max_log_size <=10 )
|
---|
677 | cfg->max_log_size=10;
|
---|
678 | break;
|
---|
679 | case 11:cfg->show_ecm_dw = atoi(argarray[1]); //showecmdw
|
---|
680 | break;
|
---|
681 | case 12:cfg->waitforcards = atoi(argarray[1]); //waitforcards
|
---|
682 | break;
|
---|
683 | case 13:cfg->preferlocalcards = atoi(argarray[1]); //preferlocalcards
|
---|
684 | break;
|
---|
685 |
|
---|
686 | default: sprintf(buf, "[S-0000]setserver failed - parameter %s not exist", argarray[0]);
|
---|
687 | monitor_send_info(buf, 1);
|
---|
688 | return;
|
---|
689 | }
|
---|
690 | }
|
---|
691 | }
|
---|
692 |
|
---|
693 | sprintf(buf, "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
|
---|
694 | monitor_send_info(buf, 1);
|
---|
695 |
|
---|
696 | if (cfg->ftimeout>=cfg->ctimeout) {
|
---|
697 | cfg->ftimeout = cfg->ctimeout - 100;
|
---|
698 | sprintf(buf, "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms", cfg->ftimeout);
|
---|
699 | monitor_send_info(buf, 1);
|
---|
700 | }
|
---|
701 | if(cfg->ftimeout < cfg->srtimeout) {
|
---|
702 | cfg->ftimeout = cfg->srtimeout + 100;
|
---|
703 | sprintf(buf, "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms", cfg->ftimeout);
|
---|
704 | monitor_send_info(buf, 1);
|
---|
705 | }
|
---|
706 | if(cfg->ctimeout < cfg->srtimeout) {
|
---|
707 | cfg->ctimeout = cfg->srtimeout + 100;
|
---|
708 | sprintf(buf, "[S-0000]setserver WARNING: clienttimeout adjusted to %lu ms", cfg->ctimeout);
|
---|
709 | monitor_send_info(buf, 1);
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | //kill(client[0].pid, SIGUSR1);
|
---|
714 |
|
---|
715 | }
|
---|
716 |
|
---|
717 | static int monitor_process_request(char *req)
|
---|
718 | {
|
---|
719 | int i, rc;
|
---|
720 | char *cmd[]={"login", "exit", "log", "status", "shutdown", "reload", "details", "version", "debug", "setuser", "setserver"};
|
---|
721 | char *arg;
|
---|
722 | if( (arg=strchr(req, ' ')) )
|
---|
723 | {
|
---|
724 | *arg++=0;
|
---|
725 | trim(arg);
|
---|
726 | }
|
---|
727 | trim(req);
|
---|
728 | if ((!auth) && (strcmp(req, cmd[0])))
|
---|
729 | monitor_login(NULL);
|
---|
730 | for (rc=1, i=0; i<11; i++)
|
---|
731 | if (!strcmp(req, cmd[i]))
|
---|
732 | {
|
---|
733 | switch(i)
|
---|
734 | {
|
---|
735 | case 0: monitor_login(arg);
|
---|
736 | break; // login
|
---|
737 | case 1: rc=0;
|
---|
738 | break; // exit
|
---|
739 | case 2: monitor_logsend(arg);
|
---|
740 | break; // log
|
---|
741 | case 3: monitor_process_info();
|
---|
742 | break; // status
|
---|
743 | case 4: if (client[cs_idx].monlvl>3)
|
---|
744 | kill(client[0].pid, SIGQUIT); // shutdown
|
---|
745 | break;
|
---|
746 | case 5: if (client[cs_idx].monlvl>2)
|
---|
747 | kill(client[0].pid, SIGHUP); // reload
|
---|
748 | break;
|
---|
749 | case 6: monitor_process_details(arg);
|
---|
750 | break; // details
|
---|
751 | case 7: monitor_send_details_version();
|
---|
752 | break; // version
|
---|
753 | case 8: if (client[cs_idx].monlvl>3)
|
---|
754 | monitor_set_debuglevel(arg); // debuglevel
|
---|
755 | break;
|
---|
756 | case 9: if (client[cs_idx].monlvl>3)
|
---|
757 | monitor_set_account(arg); // setuser
|
---|
758 | break;
|
---|
759 | case 10: if (client[cs_idx].monlvl>3)
|
---|
760 | monitor_set_server(arg); // setserver
|
---|
761 | break;
|
---|
762 |
|
---|
763 | default: continue;
|
---|
764 | }
|
---|
765 | break;
|
---|
766 | }
|
---|
767 | return(rc);
|
---|
768 | }
|
---|
769 |
|
---|
770 | static void monitor_server()
|
---|
771 | {
|
---|
772 | int n;
|
---|
773 | client[cs_idx].typ='m';
|
---|
774 | while (((n=process_input(mbuf, sizeof(mbuf), cfg->cmaxidle))>=0) &&
|
---|
775 | monitor_process_request((char *)mbuf));
|
---|
776 | cs_disconnect_client();
|
---|
777 | }
|
---|
778 |
|
---|
779 | void module_monitor(struct s_module *ph)
|
---|
780 | {
|
---|
781 | static PTAB ptab;
|
---|
782 | ptab.ports[0].s_port = cfg->mon_port;
|
---|
783 | ph->ptab = &ptab;
|
---|
784 | ph->ptab->nports = 1;
|
---|
785 |
|
---|
786 | if (cfg->mon_aulow<1)
|
---|
787 | cfg->mon_aulow=30;
|
---|
788 | strcpy(ph->desc, "monitor");
|
---|
789 | ph->type=MOD_CONN_UDP;
|
---|
790 | ph->multi=0;
|
---|
791 | ph->watchdog=1;
|
---|
792 | ph->s_ip=cfg->mon_srvip;
|
---|
793 | ph->s_handler=monitor_server;
|
---|
794 | ph->recv=monitor_recv;
|
---|
795 | // ph->send_dcw=NULL;
|
---|
796 | }
|
---|
797 |
|
---|
798 |
|
---|