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