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