1 | #include "globals.h"
|
---|
2 |
|
---|
3 | #define CS_VERSION_X CS_VERSION
|
---|
4 |
|
---|
5 | static void monitor_check_ip()
|
---|
6 | {
|
---|
7 | int ok=0;
|
---|
8 | struct s_client *cur_cl = cur_client();
|
---|
9 |
|
---|
10 | if (cur_cl->auth) return;
|
---|
11 | ok = check_ip(cfg.mon_allowed, cur_cl->ip);
|
---|
12 | if (!ok)
|
---|
13 | {
|
---|
14 | cs_auth_client(cur_cl, (struct s_auth *)0, "invalid ip");
|
---|
15 | cs_exit(0);
|
---|
16 | }
|
---|
17 | }
|
---|
18 |
|
---|
19 | static void monitor_auth_client(char *usr, char *pwd)
|
---|
20 | {
|
---|
21 | struct s_auth *account;
|
---|
22 | struct s_client *cur_cl = cur_client();
|
---|
23 |
|
---|
24 | if (cur_cl->auth) return;
|
---|
25 | if ((!usr) || (!pwd))
|
---|
26 | {
|
---|
27 | cs_auth_client(cur_cl, (struct s_auth *)0, NULL);
|
---|
28 | cs_exit(0);
|
---|
29 | }
|
---|
30 | for (account=cfg.account, cur_cl->auth=0; (account) && (!cur_cl->auth);)
|
---|
31 | {
|
---|
32 | if (account->monlvl)
|
---|
33 | cur_cl->auth=!(strcmp(usr, account->usr) | strcmp(pwd, account->pwd));
|
---|
34 | if (!cur_cl->auth)
|
---|
35 | account=account->next;
|
---|
36 | }
|
---|
37 | if (!cur_cl->auth)
|
---|
38 | {
|
---|
39 | cs_auth_client(cur_cl, (struct s_auth *)0, "invalid account");
|
---|
40 | cs_exit(0);
|
---|
41 | }
|
---|
42 | if (cs_auth_client(cur_cl, account, NULL))
|
---|
43 | cs_exit(0);
|
---|
44 | }
|
---|
45 |
|
---|
46 | static int secmon_auth_client(uchar *ucrc)
|
---|
47 | {
|
---|
48 | ulong crc;
|
---|
49 | struct s_auth *account;
|
---|
50 | struct s_client *cur_cl = cur_client();
|
---|
51 |
|
---|
52 | if (cur_cl->auth)
|
---|
53 | {
|
---|
54 | int s=memcmp(cur_cl->ucrc, ucrc, 4);
|
---|
55 | if (s)
|
---|
56 | cs_log("wrong user-crc or garbage !?");
|
---|
57 | return(!s);
|
---|
58 | }
|
---|
59 | cur_cl->crypted=1;
|
---|
60 | crc=(ucrc[0]<<24) | (ucrc[1]<<16) | (ucrc[2]<<8) | ucrc[3];
|
---|
61 | for (account=cfg.account; (account) && (!cur_cl->auth); account=account->next)
|
---|
62 | if ((account->monlvl) &&
|
---|
63 | (crc==crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), cur_cl->dump), 16)))
|
---|
64 | {
|
---|
65 | memcpy(cur_cl->ucrc, ucrc, 4);
|
---|
66 | aes_set_key((char *)MD5((unsigned char *)account->pwd, strlen(account->pwd), cur_cl->dump));
|
---|
67 | if (cs_auth_client(cur_cl, account, NULL))
|
---|
68 | cs_exit(0);
|
---|
69 | cur_cl->auth=1;
|
---|
70 | }
|
---|
71 | if (!cur_cl->auth)
|
---|
72 | {
|
---|
73 | cs_auth_client(cur_cl, (struct s_auth *)0, "invalid user");
|
---|
74 | cs_exit(0);
|
---|
75 | }
|
---|
76 | return(cur_cl->auth);
|
---|
77 | }
|
---|
78 |
|
---|
79 | int monitor_send_idx(struct s_client *cl, char *txt)
|
---|
80 | {
|
---|
81 | int l;
|
---|
82 | unsigned char buf[256+32];
|
---|
83 | if (!cl->udp_fd)
|
---|
84 | return(-1);
|
---|
85 | struct timespec req_ts;
|
---|
86 | req_ts.tv_sec = 0;
|
---|
87 | req_ts.tv_nsec = 500000;
|
---|
88 | nanosleep (&req_ts, NULL);//avoid lost udp-pakkets
|
---|
89 | if (!cl->crypted)
|
---|
90 | return(sendto(cl->udp_fd, txt, strlen(txt), 0,
|
---|
91 | (struct sockaddr *)&cl->udp_sa,
|
---|
92 | sizeof(cl->udp_sa)));
|
---|
93 | buf[0]='&';
|
---|
94 | buf[9]=l=strlen(txt);
|
---|
95 | l=boundary(4, l+5)+5;
|
---|
96 | memcpy(buf+1, cl->ucrc, 4);
|
---|
97 | cs_strncpy((char *)buf+10, txt, sizeof(buf)-10);
|
---|
98 | uchar tmp[10];
|
---|
99 | memcpy(buf+5, i2b_buf(4, crc32(0L, buf+10, l-10), tmp), 4);
|
---|
100 | aes_encrypt_idx(cl, buf+5, l-5);
|
---|
101 | return(sendto(cl->udp_fd, buf, l, 0,
|
---|
102 | (struct sockaddr *)&cl->udp_sa,
|
---|
103 | sizeof(cl->udp_sa)));
|
---|
104 | }
|
---|
105 |
|
---|
106 | #define monitor_send(t) monitor_send_idx(cur_client(), t)
|
---|
107 |
|
---|
108 | static int monitor_recv(struct s_client * client, uchar *buf, int l)
|
---|
109 | {
|
---|
110 | int n;
|
---|
111 | uchar nbuf[3] = { 'U', 0, 0 };
|
---|
112 | static int bpos=0;
|
---|
113 | static uchar *bbuf=NULL;
|
---|
114 | if (!bbuf)
|
---|
115 | {
|
---|
116 | bbuf=(uchar *)malloc(l);
|
---|
117 | if (!bbuf)
|
---|
118 | {
|
---|
119 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
120 | cs_exit(1);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | if (bpos)
|
---|
124 | memcpy(buf, bbuf, n=bpos);
|
---|
125 | else
|
---|
126 | n=recv_from_udpipe(buf);
|
---|
127 | bpos=0;
|
---|
128 | if (!n) return(buf[0]=0);
|
---|
129 | if (buf[0]=='&')
|
---|
130 | {
|
---|
131 | int bsize;
|
---|
132 | if (n<21) // 5+16 is minimum
|
---|
133 | {
|
---|
134 | cs_log("packet to short !");
|
---|
135 | return(buf[0]=0);
|
---|
136 | }
|
---|
137 | if (!secmon_auth_client(buf+1))
|
---|
138 | return(buf[0]=0);
|
---|
139 | aes_decrypt(buf+5, 16);
|
---|
140 | bsize=boundary(4, buf[9]+5)+5;
|
---|
141 | // cs_log("n=%d bsize=%d", n, bsize);
|
---|
142 | if (n>bsize)
|
---|
143 | {
|
---|
144 | // cs_log("DO >>>> copy-back");
|
---|
145 | memcpy(bbuf, buf+bsize, bpos=n-bsize);
|
---|
146 | n=bsize;
|
---|
147 | write_to_pipe(client->fd_m2c, PIP_ID_UDP, (uchar*)&nbuf, sizeof(nbuf));
|
---|
148 | }
|
---|
149 | else if (n<bsize)
|
---|
150 | {
|
---|
151 | cs_log("packet-size mismatch !");
|
---|
152 | return(buf[0]=0);
|
---|
153 | }
|
---|
154 | aes_decrypt(buf+21, n-21);
|
---|
155 | uchar tmp[10];
|
---|
156 | if (memcmp(buf+5, i2b_buf(4, crc32(0L, buf+10, n-10), tmp), 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 | write_to_pipe(client->fd_m2c, PIP_ID_UDP, (uchar*)&nbuf, sizeof(nbuf));
|
---|
174 | }
|
---|
175 | }
|
---|
176 | buf[n]='\0';
|
---|
177 | n=strlen(trim((char *)buf));
|
---|
178 | if (n) client->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 | snprintf(buf, sizeof(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 | cs_strncpy(btxt, txt, sizeof(btxt));
|
---|
208 | return;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (txt && btxt[0])
|
---|
212 | {
|
---|
213 | monitor_send(btxt);
|
---|
214 | txt[2]='E';
|
---|
215 | cs_strncpy(btxt, txt, sizeof(btxt));
|
---|
216 | }
|
---|
217 | else
|
---|
218 | {
|
---|
219 | if (txt)
|
---|
220 | cs_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 | static char *monitor_client_info(char id, struct s_client *cl){
|
---|
233 | static char sbuf[256];
|
---|
234 | sbuf[0] = '\0';
|
---|
235 |
|
---|
236 | if (cl){
|
---|
237 | char ldate[16], ltime[16], *usr;
|
---|
238 | int lsec, isec, con, cau, lrt =- 1;
|
---|
239 | time_t now;
|
---|
240 | struct tm lt;
|
---|
241 | now=time((time_t)0);
|
---|
242 |
|
---|
243 | if ((cfg.mon_hideclient_to <= 0) ||
|
---|
244 | (now-cl->lastecm < cfg.mon_hideclient_to) ||
|
---|
245 | (now-cl->lastemm < cfg.mon_hideclient_to) ||
|
---|
246 | (cl->typ != 'c'))
|
---|
247 | {
|
---|
248 | lsec=now-cl->login;
|
---|
249 | isec=now-cl->last;
|
---|
250 | usr=cl->account->usr;
|
---|
251 | if ((cl->typ == 'r') || (cl->typ == 'p'))
|
---|
252 | usr=cl->reader->label;
|
---|
253 | if (cl->dup)
|
---|
254 | con=2;
|
---|
255 | else
|
---|
256 | if ((cl->tosleep) && (now-cl->lastswitch>cl->tosleep))
|
---|
257 | con = 1;
|
---|
258 | else
|
---|
259 | con = 0;
|
---|
260 |
|
---|
261 | //if( (cau = get_ridx(cl->aureader) + 1) )
|
---|
262 | // if ((now-cl->lastemm) /60 > cfg.mon_aulow)
|
---|
263 | // cau=-cau;
|
---|
264 | // workaround: no AU reader == 0 / AU ok == 1 / Last EMM > aulow == -1
|
---|
265 | if (!cl->aureader_list) {
|
---|
266 | cau = 0;
|
---|
267 | } else {
|
---|
268 | if ((now-cl->lastemm)/60 > cfg.mon_aulow)
|
---|
269 | cau = -1;
|
---|
270 | else
|
---|
271 | cau = 1;
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | if( cl->typ == 'r')
|
---|
276 | {
|
---|
277 | int i;
|
---|
278 | struct s_reader *rdr;
|
---|
279 | for (i=0,rdr=first_active_reader; rdr ; rdr=rdr->next, i++)
|
---|
280 | if (cl->reader == rdr)
|
---|
281 | lrt=i;
|
---|
282 |
|
---|
283 | if( lrt >= 0 )
|
---|
284 | lrt = 10 + cl->reader->card_status;
|
---|
285 | }
|
---|
286 | else
|
---|
287 | lrt = cl->cwlastresptime;
|
---|
288 | localtime_r(&cl->login, <);
|
---|
289 | snprintf(ldate, sizeof(ldate), "%02d.%02d.%02d", lt.tm_mday, lt.tm_mon+1, lt.tm_year % 100);
|
---|
290 | int cnr=get_threadnum(cl);
|
---|
291 | snprintf(ltime, sizeof(ldate), "%02d:%02d:%02d", lt.tm_hour, lt.tm_min, lt.tm_sec);
|
---|
292 | snprintf(sbuf, sizeof(sbuf), "[%c--CCC]%8lX|%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",
|
---|
293 | id, (unsigned long)cl->thread, cl->typ, cnr, usr, cau, cl->crypted,
|
---|
294 | cs_inet_ntoa(cl->ip), cl->port, monitor_get_proto(cl),
|
---|
295 | ldate, ltime, lsec, cl->last_caid, cl->last_srvid,
|
---|
296 | get_servicename(cl->last_srvid, cl->last_caid), isec, con,
|
---|
297 | cl->cwfound, cl->cwnot, cl->cwcache, cl->cwignored,
|
---|
298 | cl->cwtout, cl->emmok, cl->emmnok, lrt);
|
---|
299 | }
|
---|
300 | }
|
---|
301 | return(sbuf);
|
---|
302 | }
|
---|
303 |
|
---|
304 | static void monitor_process_info(){
|
---|
305 | time_t now = time((time_t)0);
|
---|
306 |
|
---|
307 | struct s_client *cl, *cur_cl = cur_client();
|
---|
308 |
|
---|
309 | for (cl=first_client; cl ; cl=cl->next) {
|
---|
310 | if ((cfg.mon_hideclient_to <= 0) ||
|
---|
311 | ( now-cl->lastecm < cfg.mon_hideclient_to) ||
|
---|
312 | ( now-cl->lastemm < cfg.mon_hideclient_to) ||
|
---|
313 | ( cl->typ != 'c')){
|
---|
314 | if ((cur_cl->monlvl < 2) && (cl->typ != 's')) {
|
---|
315 | if ((cur_cl->account && cl->account && strcmp(cur_cl->account->usr, cl->account->usr)) ||
|
---|
316 | ((cl->typ != 'c') && (cl->typ != 'm')))
|
---|
317 | continue;
|
---|
318 | }
|
---|
319 | monitor_send_info(monitor_client_info('I', cl), 0);
|
---|
320 | }
|
---|
321 | }
|
---|
322 | monitor_send_info(NULL, 1);
|
---|
323 | }
|
---|
324 |
|
---|
325 | static void monitor_send_details(char *txt, unsigned int tid){
|
---|
326 | char buf[256];
|
---|
327 | snprintf(buf, 255, "[D-----]%8X|%s\n", tid, txt);
|
---|
328 | monitor_send_info(buf, 0);
|
---|
329 | }
|
---|
330 |
|
---|
331 | static void monitor_send_details_version(){
|
---|
332 | char buf[256];
|
---|
333 | snprintf(buf, sizeof(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);
|
---|
334 | monitor_send_info(buf, 1);
|
---|
335 | }
|
---|
336 |
|
---|
337 | static void monitor_send_keepalive_ack(){
|
---|
338 | char buf[32];
|
---|
339 | snprintf(buf, sizeof(buf), "[K-0000]keepalive_ack\n");
|
---|
340 | monitor_send_info(buf, 1);
|
---|
341 | }
|
---|
342 |
|
---|
343 | static void monitor_process_details_master(char *buf, unsigned long pid){
|
---|
344 | snprintf(buf, 256, "Version=%s#%s", CS_VERSION_X, CS_SVN_VERSION);
|
---|
345 | monitor_send_details(buf, pid);
|
---|
346 | snprintf(buf, 256, "System=%s-%s-%s", CS_OS_CPU, CS_OS_HW, CS_OS_SYS);
|
---|
347 | monitor_send_details(buf, pid);
|
---|
348 | snprintf(buf, 256, "DebugLevel=%d", cs_dblevel);
|
---|
349 | monitor_send_details(buf, pid);
|
---|
350 | snprintf(buf, 256, "MaxClients=UNLIMITED");
|
---|
351 | monitor_send_details(buf, pid);
|
---|
352 | snprintf(buf, 256, "ClientMaxIdle=%ld sec", cfg.cmaxidle);
|
---|
353 | monitor_send_details(buf, pid);
|
---|
354 | if( cfg.max_log_size )
|
---|
355 | snprintf(buf + 200, 56, "%d Kb", cfg.max_log_size);
|
---|
356 | else
|
---|
357 | cs_strncpy(buf + 200, "unlimited", 56);
|
---|
358 | snprintf(buf, 256, "MaxLogsize=%s", buf + 200);
|
---|
359 | monitor_send_details(buf, pid);
|
---|
360 | snprintf(buf, 256, "ClientTimeout=%lu ms", cfg.ctimeout);
|
---|
361 | monitor_send_details(buf, pid);
|
---|
362 | snprintf(buf, 256, "CacheDelay=%ld ms", cfg.delay);
|
---|
363 | monitor_send_details(buf, pid);
|
---|
364 | if( cfg.cwlogdir ) {
|
---|
365 | snprintf(buf, 256, "CwlogDir=%s", cfg.cwlogdir);
|
---|
366 | monitor_send_details(buf, pid);
|
---|
367 | }
|
---|
368 | if( cfg.preferlocalcards ) {
|
---|
369 | snprintf(buf, 256, "PreferlocalCards=%d", cfg.preferlocalcards);
|
---|
370 | monitor_send_details(buf, pid);
|
---|
371 | }
|
---|
372 | if( cfg.waitforcards ) {
|
---|
373 | snprintf(buf, 256, "WaitforCards=%d", cfg.waitforcards);
|
---|
374 | monitor_send_details(buf, pid);
|
---|
375 | }
|
---|
376 | snprintf(buf, 256, "LogFile=%s", cfg.logfile);
|
---|
377 | monitor_send_details(buf, pid);
|
---|
378 | if( cfg.usrfile ) {
|
---|
379 | snprintf(buf, 256, "UsrFile=%s", cfg.usrfile);
|
---|
380 | monitor_send_details(buf, pid);
|
---|
381 | }
|
---|
382 | monitor_send_details(buf, pid);
|
---|
383 | snprintf(buf, 256, "Sleep=%d", cfg.tosleep);
|
---|
384 | monitor_send_details(buf, pid);
|
---|
385 | snprintf(buf, 256, "Monitorport=%d", cfg.mon_port);
|
---|
386 | monitor_send_details(buf, pid);
|
---|
387 | snprintf(buf, 256, "Nice=%d", cfg.nice);
|
---|
388 | monitor_send_details(buf, pid);
|
---|
389 | #ifdef WEBIF
|
---|
390 | snprintf(buf, 256, "Restartmode=%d", cs_get_restartmode());
|
---|
391 | monitor_send_details(buf, pid);
|
---|
392 | #else
|
---|
393 | snprintf(buf, 256, "Restartmode=%s", "no");
|
---|
394 | monitor_send_details(buf, pid);
|
---|
395 | #endif
|
---|
396 |
|
---|
397 | // monitor_send_details(buf, pid);
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 | static void monitor_process_details_reader(struct s_client *cl) {
|
---|
402 |
|
---|
403 | if (cfg.saveinithistory) {
|
---|
404 | if (cl->reader->init_history) {
|
---|
405 | char *ptr,*ptr1 = NULL;
|
---|
406 | for (ptr=strtok_r(cl->reader->init_history, "\n", &ptr1); ptr; ptr=strtok_r(NULL, "\n", &ptr1)) {
|
---|
407 | monitor_send_details(ptr, (unsigned long)(cl->thread));
|
---|
408 | ptr1[-1]='\n';
|
---|
409 | }
|
---|
410 | }
|
---|
411 | } else {
|
---|
412 | monitor_send_details("Missing reader index or entitlement not saved!", (unsigned long)(cl->thread));
|
---|
413 | }
|
---|
414 |
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | static void monitor_process_details(char *arg){
|
---|
419 | unsigned long tid = 0; //using threadid 8 positions hex see oscam-log.c //FIXME untested but pid isnt working anyway with threading
|
---|
420 | struct s_client *cl;
|
---|
421 | char sbuf[256];
|
---|
422 |
|
---|
423 | if (!arg)
|
---|
424 | cl = first_client; // no arg - show master
|
---|
425 | else
|
---|
426 | if (sscanf(arg,"%lX",&tid) == 1)
|
---|
427 | cl = get_client_by_tid(tid);
|
---|
428 | else
|
---|
429 | cl = NULL;
|
---|
430 |
|
---|
431 | if (!cl)
|
---|
432 | monitor_send_details("Invalid TID", tid);
|
---|
433 | else
|
---|
434 | {
|
---|
435 | //monitor_send_info(monitor_client_info('D', idx), 0); //FIXME
|
---|
436 | switch(cl->typ)
|
---|
437 | {
|
---|
438 | case 's':
|
---|
439 | monitor_process_details_master(sbuf, (unsigned long)(cl->thread));
|
---|
440 | break;
|
---|
441 | case 'c': case 'm':
|
---|
442 | monitor_send_details(monitor_client_info(1, cl), (unsigned long)(cl->thread));
|
---|
443 | break;
|
---|
444 | case 'r':
|
---|
445 | monitor_process_details_reader(cl);//with client->typ='r' client->ridx is always filled and valid, so no need checking
|
---|
446 | break;
|
---|
447 | case 'p':
|
---|
448 | monitor_send_details(monitor_client_info(1, cl), (unsigned long)(cl->thread));
|
---|
449 | break;
|
---|
450 | }
|
---|
451 | }
|
---|
452 | monitor_send_info(NULL, 1);
|
---|
453 | }
|
---|
454 |
|
---|
455 | static void monitor_send_login(void){
|
---|
456 | char buf[64];
|
---|
457 | struct s_client *cur_cl = cur_client();
|
---|
458 | if (cur_cl->auth && cur_cl->account)
|
---|
459 | snprintf(buf, sizeof(buf), "[A-0000]1|%s logged in\n", cur_cl->account->usr);
|
---|
460 | else
|
---|
461 | cs_strncpy(buf, "[A-0000]0|not logged in\n", sizeof(buf));
|
---|
462 | monitor_send_info(buf, 1);
|
---|
463 | }
|
---|
464 |
|
---|
465 | static void monitor_login(char *usr){
|
---|
466 | char *pwd=NULL;
|
---|
467 | if ((usr) && (pwd=strchr(usr, ' ')))
|
---|
468 | *pwd++=0;
|
---|
469 | if (pwd)
|
---|
470 | monitor_auth_client(trim(usr), trim(pwd));
|
---|
471 | else
|
---|
472 | monitor_auth_client(NULL, NULL);
|
---|
473 | monitor_send_login();
|
---|
474 | }
|
---|
475 |
|
---|
476 | static void monitor_logsend(char *flag){
|
---|
477 | #ifdef CS_LOGHISTORY
|
---|
478 | int i;
|
---|
479 | #endif
|
---|
480 | if (!flag) return; //no arg
|
---|
481 |
|
---|
482 | struct s_client *cur_cl = cur_client();
|
---|
483 | if (strcmp(flag, "on")) {
|
---|
484 | if (strcmp(flag, "onwohist")) {
|
---|
485 | cur_cl->log=0;
|
---|
486 | return;
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | if (cur_cl->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 | char *p_usr, *p_txt;
|
---|
496 | p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
|
---|
497 | p_txt = p_usr + 32;
|
---|
498 | if ((p_txt[0]) && ((cur_cl->monlvl > 1) || (cur_cl->account && !strcmp(p_usr, cur_cl->account->usr)))) {
|
---|
499 | char sbuf[8];
|
---|
500 | snprintf(sbuf, sizeof(sbuf), "%03d", cur_cl->logcounter);
|
---|
501 | cur_cl->logcounter=(cur_cl->logcounter + 1) % 1000;
|
---|
502 | memcpy(p_txt + 4, sbuf, 3);
|
---|
503 | monitor_send(p_txt);
|
---|
504 | }
|
---|
505 | }
|
---|
506 | }
|
---|
507 | #endif
|
---|
508 | cur_cl->log=1;
|
---|
509 | }
|
---|
510 |
|
---|
511 | static void monitor_set_debuglevel(char *flag){
|
---|
512 | if (flag) {
|
---|
513 | cs_dblevel = atoi(flag);
|
---|
514 | #ifndef WITH_DEBUG
|
---|
515 | cs_log("*** Warning: Debug Support not compiled in ***");
|
---|
516 | #else
|
---|
517 | cs_log("%s debug_level=%d", "all", cs_dblevel);
|
---|
518 | #endif
|
---|
519 | }
|
---|
520 | }
|
---|
521 |
|
---|
522 | static void monitor_get_account(){
|
---|
523 | struct s_auth *account;
|
---|
524 | char buf[256];
|
---|
525 | int count = 0;
|
---|
526 |
|
---|
527 | for (account=cfg.account; (account); account=account->next){
|
---|
528 | count++;
|
---|
529 | snprintf(buf, sizeof(buf), "[U-----]%s\n", account->usr);
|
---|
530 | monitor_send_info(buf, 0);
|
---|
531 | }
|
---|
532 | snprintf(buf, sizeof(buf), "[U-----] %i User registered\n", count);
|
---|
533 | monitor_send_info(buf, 1);
|
---|
534 | return;
|
---|
535 | }
|
---|
536 |
|
---|
537 | static void monitor_set_account(char *args){
|
---|
538 | struct s_auth *account;
|
---|
539 | char delimiter[] = " =";
|
---|
540 | char *ptr;
|
---|
541 | int argidx, i, found;
|
---|
542 | char *argarray[3];
|
---|
543 | static const char *token[]={"au", "sleep", "uniq", "monlevel", "group", "services", "betatunnel", "ident", "caid", "chid", "class", "hostname", "expdate", "keepalive", "disabled"};
|
---|
544 | int tokencnt = sizeof(token)/sizeof(char *);
|
---|
545 | char buf[256], tmp[64];
|
---|
546 |
|
---|
547 | argidx = 0;
|
---|
548 | found = 0;
|
---|
549 |
|
---|
550 | snprintf(tmp, sizeof(tmp), "%s",args);
|
---|
551 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s check\n", tmp);
|
---|
552 | monitor_send_info(buf, 0);
|
---|
553 |
|
---|
554 | ptr = strtok(args, delimiter);
|
---|
555 |
|
---|
556 | // resolve arguments
|
---|
557 | while(ptr != NULL) {
|
---|
558 | argarray[argidx]=trim(ptr);
|
---|
559 | ptr = strtok(NULL, delimiter);
|
---|
560 | argidx++;
|
---|
561 | }
|
---|
562 |
|
---|
563 | if(argidx != 3) {
|
---|
564 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s failed - wrong number of parameters (%d)\n",tmp, argidx);
|
---|
565 | monitor_send_info(buf, 0);
|
---|
566 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
|
---|
567 | monitor_send_info(buf, 1);
|
---|
568 | return;
|
---|
569 | }
|
---|
570 |
|
---|
571 | //search account
|
---|
572 | for (account=cfg.account; (account) ; account=account->next){
|
---|
573 | if (!strcmp(argarray[0], account->usr)){
|
---|
574 | found = 1;
|
---|
575 | break;
|
---|
576 | }
|
---|
577 | }
|
---|
578 |
|
---|
579 | if (found != 1){
|
---|
580 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s failed - user %s not found\n",tmp , argarray[0]);
|
---|
581 | monitor_send_info(buf, 0);
|
---|
582 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
|
---|
583 | monitor_send_info(buf, 1);
|
---|
584 | return;
|
---|
585 | }
|
---|
586 |
|
---|
587 | found = -1;
|
---|
588 | for (i = 0; i < tokencnt; i++){
|
---|
589 | if (!strcmp(argarray[1], token[i])){
|
---|
590 | // preparing the parameters before re-load
|
---|
591 | switch(i) {
|
---|
592 |
|
---|
593 | case 6: clear_tuntab(&account->ttab); break; //betatunnel
|
---|
594 |
|
---|
595 | case 8: clear_caidtab(&account->ctab); break; //Caid
|
---|
596 | }
|
---|
597 | found = i;
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | if (found < 0){
|
---|
602 | snprintf(buf, sizeof(buf), "[S-0000]setuser: parameter %s not exist. possible values:\n", argarray[1]);
|
---|
603 | monitor_send_info(buf, 0);
|
---|
604 | for (i = 0; i < tokencnt; i++){
|
---|
605 | snprintf(buf, sizeof(buf), "[S-0000]%s\n", token[i]);
|
---|
606 | monitor_send_info(buf, 0);
|
---|
607 | }
|
---|
608 | snprintf(buf, sizeof(buf),"[S-0000]setuser: %s end\n", tmp);
|
---|
609 | monitor_send_info(buf, 1);
|
---|
610 | return;
|
---|
611 | } else {
|
---|
612 | chk_account(token[found], argarray[2], account);
|
---|
613 | }
|
---|
614 |
|
---|
615 | if (write_userdb(cfg.account)==0)
|
---|
616 | cs_reinit_clients(cfg.account);
|
---|
617 |
|
---|
618 | snprintf(buf, sizeof(buf),"[S-0000]setuser: %s done - param %s set to %s\n", tmp, argarray[1], argarray[2]);
|
---|
619 | monitor_send_info(buf, 1);
|
---|
620 | }
|
---|
621 |
|
---|
622 | static void monitor_set_server(char *args){
|
---|
623 | char delimiter[] = "=";
|
---|
624 | char *ptr;
|
---|
625 | int argidx, i, found;
|
---|
626 | char *argarray[3];
|
---|
627 | static const char *token[]={"clienttimeout", "fallbacktimeout", "clientmaxidle", "cachedelay", "bindwait", "netprio", "sleep", "unlockparental", "serialreadertimeout", "maxlogsize", "showecmdw", "waitforcards", "preferlocalcards"};
|
---|
628 | char buf[256];
|
---|
629 |
|
---|
630 | argidx=0; found=0;
|
---|
631 | ptr = strtok(args, delimiter);
|
---|
632 |
|
---|
633 | // resolve arguments
|
---|
634 | while(ptr != NULL) {
|
---|
635 | argarray[argidx]=trim(ptr);
|
---|
636 | ptr = strtok(NULL, delimiter);
|
---|
637 | argidx++;
|
---|
638 | }
|
---|
639 |
|
---|
640 | if(argidx != 2) {
|
---|
641 | snprintf(buf, sizeof(buf),"[S-0000]setserver failed - wrong number of parameters (%d)\n", argidx);
|
---|
642 | monitor_send_info(buf, 1);
|
---|
643 | return;
|
---|
644 | }
|
---|
645 |
|
---|
646 | trim(argarray[0]);
|
---|
647 | trim(argarray[1]);
|
---|
648 | strtolower(argarray[0]);
|
---|
649 |
|
---|
650 | for (i = 0; i < 13; i++)
|
---|
651 | if (!strcmp(argarray[0], token[i])) break;
|
---|
652 |
|
---|
653 | if (i < 13){
|
---|
654 | chk_t_global(token[i],argarray[1]);
|
---|
655 | snprintf(buf, sizeof(buf), "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
|
---|
656 | monitor_send_info(buf, 1);
|
---|
657 | } else {
|
---|
658 | snprintf(buf, sizeof(buf), "[S-0000]setserver failed - parameter %s not exist\n", argarray[0]);
|
---|
659 | monitor_send_info(buf, 1);
|
---|
660 | return;
|
---|
661 | }
|
---|
662 |
|
---|
663 | if (cfg.ftimeout>=cfg.ctimeout) {
|
---|
664 | cfg.ftimeout = cfg.ctimeout - 100;
|
---|
665 | snprintf(buf, sizeof(buf), "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms\n", cfg.ftimeout);
|
---|
666 | monitor_send_info(buf, 1);
|
---|
667 | }
|
---|
668 | if(cfg.ftimeout < cfg.srtimeout) {
|
---|
669 | cfg.ftimeout = cfg.srtimeout + 100;
|
---|
670 | snprintf(buf, sizeof(buf), "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms\n", cfg.ftimeout);
|
---|
671 | monitor_send_info(buf, 1);
|
---|
672 | }
|
---|
673 | if(cfg.ctimeout < cfg.srtimeout) {
|
---|
674 | cfg.ctimeout = cfg.srtimeout + 100;
|
---|
675 | snprintf(buf, sizeof(buf), "[S-0000]setserver WARNING: clienttimeout adjusted to %lu ms\n", cfg.ctimeout);
|
---|
676 | monitor_send_info(buf, 1);
|
---|
677 | }
|
---|
678 | //kill(first_client->pid, SIGUSR1);
|
---|
679 | }
|
---|
680 |
|
---|
681 | #ifdef WEBIF
|
---|
682 | static void monitor_restart_server(){
|
---|
683 | cs_restart_oscam();
|
---|
684 | }
|
---|
685 | #endif
|
---|
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 | snprintf(buf, sizeof(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",
|
---|
703 | "exit",
|
---|
704 | "log",
|
---|
705 | "status",
|
---|
706 | "shutdown",
|
---|
707 | "reload",
|
---|
708 | "details",
|
---|
709 | "version",
|
---|
710 | "debug",
|
---|
711 | "getuser",
|
---|
712 | "setuser",
|
---|
713 | "setserver",
|
---|
714 | "commands",
|
---|
715 | "keepalive",
|
---|
716 | "reread"
|
---|
717 | #ifdef WEBIF
|
---|
718 | ,"restart"
|
---|
719 | #endif
|
---|
720 | };
|
---|
721 |
|
---|
722 | int cmdcnt = sizeof(cmd)/sizeof(char *); // Calculate the amount of items in array
|
---|
723 | char *arg;
|
---|
724 | struct s_client *cur_cl = cur_client();
|
---|
725 |
|
---|
726 | if( (arg = strchr(req, ' ')) ) { *arg++ = 0; trim(arg); }
|
---|
727 | //trim(req);
|
---|
728 | if ((!cur_cl->auth) && (strcmp(req, cmd[0]))) monitor_login(NULL);
|
---|
729 |
|
---|
730 | for (rc=1, i = 0; i < cmdcnt; i++)
|
---|
731 | if (!strcmp(req, cmd[i])) {
|
---|
732 | switch(i) {
|
---|
733 | case 0: monitor_login(arg); break; // login
|
---|
734 | case 1: rc=0; break; // exit
|
---|
735 | case 2: monitor_logsend(arg); break; // log
|
---|
736 | case 3: monitor_process_info(); break; // status
|
---|
737 | case 4: if (cur_cl->monlvl > 3) cs_exit(SIGQUIT); break; // shutdown
|
---|
738 | case 5: if (cur_cl->monlvl > 2) cs_reinit_clients(cfg.account); break; // reload
|
---|
739 | case 6: monitor_process_details(arg); break; // details
|
---|
740 | case 7: monitor_send_details_version(); break; // version
|
---|
741 | case 8: if (cur_cl->monlvl > 3) monitor_set_debuglevel(arg); break; // debuglevel
|
---|
742 | case 9: if (cur_cl->monlvl > 3) monitor_get_account(); break; // getuser
|
---|
743 | case 10: if (cur_cl->monlvl > 3) monitor_set_account(arg); break; // setuser
|
---|
744 | case 11: if (cur_cl->monlvl > 3) monitor_set_server(arg); break; // setserver
|
---|
745 | case 12: if (cur_cl->monlvl > 3) monitor_list_commands(cmd, cmdcnt); break; // list commands
|
---|
746 | case 13: if (cur_cl->monlvl > 3) monitor_send_keepalive_ack(); break; // keepalive
|
---|
747 | case 14: { char buf[64];snprintf(buf, sizeof(buf), "[S-0000]reread\n");monitor_send_info(buf, 1); cs_card_info(); break; } // reread
|
---|
748 | #ifdef WEBIF
|
---|
749 | case 15: if (cur_cl->monlvl > 3) monitor_restart_server(); break; // keepalive
|
---|
750 | #endif
|
---|
751 | default: continue;
|
---|
752 | }
|
---|
753 | break;
|
---|
754 | }
|
---|
755 | return(rc);
|
---|
756 | }
|
---|
757 |
|
---|
758 | static void * monitor_server(void *cli){
|
---|
759 | int n;
|
---|
760 | uchar mbuf[1024];
|
---|
761 |
|
---|
762 | struct s_client * client = (struct s_client *) cli;
|
---|
763 | client->thread=pthread_self();
|
---|
764 | pthread_setspecific(getclient, cli);
|
---|
765 | client->typ='m';
|
---|
766 | while (((n = process_input(mbuf, sizeof(mbuf), cfg.cmaxidle)) >= 0) && monitor_process_request((char *)mbuf));
|
---|
767 | cs_disconnect_client(cli);
|
---|
768 | return NULL;
|
---|
769 | }
|
---|
770 |
|
---|
771 | void module_monitor(struct s_module *ph){
|
---|
772 | static PTAB ptab; //since there is always only 1 monitor running, this is threadsafe
|
---|
773 | ptab.ports[0].s_port = cfg.mon_port;
|
---|
774 | ph->ptab = &ptab;
|
---|
775 | ph->ptab->nports = 1;
|
---|
776 |
|
---|
777 | if (cfg.mon_aulow < 1)
|
---|
778 | cfg.mon_aulow = 30;
|
---|
779 | cs_strncpy(ph->desc, "monitor", sizeof(ph->desc));
|
---|
780 | ph->type=MOD_CONN_UDP;
|
---|
781 | ph->multi = 0;
|
---|
782 | ph->watchdog = 1;
|
---|
783 | ph->s_ip = cfg.mon_srvip;
|
---|
784 | ph->s_handler = monitor_server;
|
---|
785 | ph->recv = monitor_recv;
|
---|
786 | // ph->send_dcw=NULL;
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|