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