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