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 | unsigned char md5tmp[MD5_DIGEST_LENGTH];
|
---|
52 |
|
---|
53 | if (cur_cl->auth)
|
---|
54 | {
|
---|
55 | int32_t s=memcmp(cur_cl->ucrc, ucrc, 4);
|
---|
56 | if (s)
|
---|
57 | cs_log("wrong user-crc or garbage !?");
|
---|
58 | return(!s);
|
---|
59 | }
|
---|
60 | cur_cl->crypted=1;
|
---|
61 | crc=(ucrc[0]<<24) | (ucrc[1]<<16) | (ucrc[2]<<8) | ucrc[3];
|
---|
62 | for (account=cfg.account; (account) && (!cur_cl->auth); account=account->next)
|
---|
63 | if ((account->monlvl) &&
|
---|
64 | (crc==crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), md5tmp), MD5_DIGEST_LENGTH)))
|
---|
65 | {
|
---|
66 | memcpy(cur_cl->ucrc, ucrc, 4);
|
---|
67 | aes_set_key((char *)MD5((unsigned char *)account->pwd, strlen(account->pwd), md5tmp));
|
---|
68 | if (cs_auth_client(cur_cl, account, NULL))
|
---|
69 | cs_exit(0);
|
---|
70 | cur_cl->auth=1;
|
---|
71 | }
|
---|
72 | if (!cur_cl->auth)
|
---|
73 | {
|
---|
74 | cs_auth_client(cur_cl, (struct s_auth *)0, "invalid user");
|
---|
75 | cs_exit(0);
|
---|
76 | }
|
---|
77 | return(cur_cl->auth);
|
---|
78 | }
|
---|
79 |
|
---|
80 | int32_t monitor_send_idx(struct s_client *cl, char *txt)
|
---|
81 | {
|
---|
82 | int32_t l;
|
---|
83 | unsigned char buf[256+32];
|
---|
84 | if (!cl->udp_fd)
|
---|
85 | return(-1);
|
---|
86 | struct timespec req_ts;
|
---|
87 | req_ts.tv_sec = 0;
|
---|
88 | req_ts.tv_nsec = 500000;
|
---|
89 | nanosleep (&req_ts, NULL);//avoid lost udp-pakkets
|
---|
90 | if (!cl->crypted)
|
---|
91 | return(sendto(cl->udp_fd, txt, strlen(txt), 0,
|
---|
92 | (struct sockaddr *)&cl->udp_sa,
|
---|
93 | sizeof(cl->udp_sa)));
|
---|
94 | buf[0]='&';
|
---|
95 | buf[9]=l=strlen(txt);
|
---|
96 | l=boundary(4, l+5)+5;
|
---|
97 | memcpy(buf+1, cl->ucrc, 4);
|
---|
98 | cs_strncpy((char *)buf+10, txt, sizeof(buf)-10);
|
---|
99 | uchar tmp[10];
|
---|
100 | memcpy(buf+5, i2b_buf(4, crc32(0L, buf+10, l-10), tmp), 4);
|
---|
101 | aes_encrypt_idx(cl, buf+5, l-5);
|
---|
102 | return(sendto(cl->udp_fd, buf, l, 0,
|
---|
103 | (struct sockaddr *)&cl->udp_sa,
|
---|
104 | sizeof(cl->udp_sa)));
|
---|
105 | }
|
---|
106 |
|
---|
107 | #define monitor_send(t) monitor_send_idx(cur_client(), t)
|
---|
108 |
|
---|
109 | static int32_t monitor_recv(struct s_client * client, uchar *buf, int32_t l)
|
---|
110 | {
|
---|
111 | int32_t n;
|
---|
112 | uchar nbuf[3] = { 'U', 0, 0 };
|
---|
113 | static int32_t bpos=0;
|
---|
114 | static uchar *bbuf=NULL;
|
---|
115 | if (!bbuf)
|
---|
116 | {
|
---|
117 | bbuf = cs_malloc(&bbuf, l, 1);
|
---|
118 | }
|
---|
119 | if (bpos)
|
---|
120 | memcpy(buf, bbuf, n=bpos);
|
---|
121 | else
|
---|
122 | n=recv_from_udpipe(buf);
|
---|
123 | bpos=0;
|
---|
124 | if (!n) return(buf[0]=0);
|
---|
125 | if (buf[0]=='&')
|
---|
126 | {
|
---|
127 | int32_t bsize;
|
---|
128 | if (n<21) // 5+16 is minimum
|
---|
129 | {
|
---|
130 | cs_log("packet to int16_t !");
|
---|
131 | return(buf[0]=0);
|
---|
132 | }
|
---|
133 | if (!secmon_auth_client(buf+1))
|
---|
134 | return(buf[0]=0);
|
---|
135 | aes_decrypt(buf+5, 16);
|
---|
136 | bsize=boundary(4, buf[9]+5)+5;
|
---|
137 | // cs_log("n=%d bsize=%d", n, bsize);
|
---|
138 | if (n>bsize)
|
---|
139 | {
|
---|
140 | // cs_log("DO >>>> copy-back");
|
---|
141 | memcpy(bbuf, buf+bsize, bpos=n-bsize);
|
---|
142 | n=bsize;
|
---|
143 | write_to_pipe(client->fd_m2c, PIP_ID_UDP, (uchar*)&nbuf, sizeof(nbuf));
|
---|
144 | }
|
---|
145 | else if (n<bsize)
|
---|
146 | {
|
---|
147 | cs_log("packet-size mismatch !");
|
---|
148 | return(buf[0]=0);
|
---|
149 | }
|
---|
150 | aes_decrypt(buf+21, n-21);
|
---|
151 | uchar tmp[10];
|
---|
152 | if (memcmp(buf+5, i2b_buf(4, crc32(0L, buf+10, n-10), tmp), 4))
|
---|
153 | {
|
---|
154 | cs_log("CRC error ! wrong password ?");
|
---|
155 | return(buf[0]=0);
|
---|
156 | }
|
---|
157 | n=buf[9];
|
---|
158 | memmove(buf, buf+10, n);
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | uchar *p;
|
---|
163 | monitor_check_ip();
|
---|
164 | buf[n]='\0';
|
---|
165 | if ((p=(uchar *)strchr((char *)buf, 10)) && (bpos=n-(p-buf)-1))
|
---|
166 | {
|
---|
167 | memcpy(bbuf, p+1, bpos);
|
---|
168 | n=p-buf;
|
---|
169 | write_to_pipe(client->fd_m2c, PIP_ID_UDP, (uchar*)&nbuf, sizeof(nbuf));
|
---|
170 | }
|
---|
171 | }
|
---|
172 | buf[n]='\0';
|
---|
173 | n=strlen(trim((char *)buf));
|
---|
174 | if (n) client->last=time((time_t *) 0);
|
---|
175 | return(n);
|
---|
176 | }
|
---|
177 |
|
---|
178 | static void monitor_send_info(char *txt, int32_t last)
|
---|
179 | {
|
---|
180 | static int32_t seq=0, counter=0;
|
---|
181 | static char btxt[256] = {0};
|
---|
182 | char buf[8];
|
---|
183 | if (txt)
|
---|
184 | {
|
---|
185 | if (!btxt[0])
|
---|
186 | {
|
---|
187 | counter=0;
|
---|
188 | txt[2]='B';
|
---|
189 | }
|
---|
190 | else
|
---|
191 | counter++;
|
---|
192 | snprintf(buf, sizeof(buf), "%03d", counter);
|
---|
193 | memcpy(txt+4, buf, 3);
|
---|
194 | txt[3]='0'+seq;
|
---|
195 | }
|
---|
196 | else
|
---|
197 | if (!last)
|
---|
198 | return;
|
---|
199 |
|
---|
200 | if (!last)
|
---|
201 | {
|
---|
202 | if (btxt[0]) monitor_send(btxt);
|
---|
203 | cs_strncpy(btxt, txt, sizeof(btxt));
|
---|
204 | return;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (txt && btxt[0])
|
---|
208 | {
|
---|
209 | monitor_send(btxt);
|
---|
210 | txt[2]='E';
|
---|
211 | cs_strncpy(btxt, txt, sizeof(btxt));
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | if (txt)
|
---|
216 | cs_strncpy(btxt, txt, sizeof(btxt));
|
---|
217 | btxt[2]=(btxt[2]=='B') ? 'S' : 'E';
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (btxt[0])
|
---|
221 | {
|
---|
222 | monitor_send(btxt);
|
---|
223 | seq=(seq+1)%10;
|
---|
224 | }
|
---|
225 | btxt[0]=0;
|
---|
226 | }
|
---|
227 |
|
---|
228 | static char *monitor_client_info(char id, struct s_client *cl, char *sbuf){
|
---|
229 | char channame[32];
|
---|
230 | sbuf[0] = '\0';
|
---|
231 |
|
---|
232 | if (cl){
|
---|
233 | char ldate[16], ltime[16], *usr;
|
---|
234 | int32_t lsec, isec, con, cau, lrt =- 1;
|
---|
235 | time_t now;
|
---|
236 | struct tm lt;
|
---|
237 | now=time((time_t)0);
|
---|
238 |
|
---|
239 | if ((cfg.mon_hideclient_to <= 0) ||
|
---|
240 | (now-cl->lastecm < cfg.mon_hideclient_to) ||
|
---|
241 | (now-cl->lastemm < cfg.mon_hideclient_to) ||
|
---|
242 | (cl->typ != 'c'))
|
---|
243 | {
|
---|
244 | lsec = now - cl->login;
|
---|
245 | isec = now - cl->last;
|
---|
246 | usr = cl->account->usr;
|
---|
247 | if ((cl->typ == 'r') || (cl->typ == 'p'))
|
---|
248 | usr = cl->reader->label;
|
---|
249 | if (cl->dup)
|
---|
250 | con = 2;
|
---|
251 | else
|
---|
252 | if ((cl->tosleep) && (now-cl->lastswitch>cl->tosleep))
|
---|
253 | con = 1;
|
---|
254 | else
|
---|
255 | con = 0;
|
---|
256 |
|
---|
257 | // no AU reader == 0 / AU ok == 1 / Last EMM > aulow == -1
|
---|
258 | if(cl->typ == 'c' || cl->typ == 'p' || cl->typ == 'r') {
|
---|
259 |
|
---|
260 | if ((cl->typ == 'c' && !cl->aureader_list) ||
|
---|
261 | ((cl->typ == 'p' || cl->typ == 'r') && cl->reader->audisabled))
|
---|
262 | cau = 0;
|
---|
263 |
|
---|
264 | else if ((now-cl->lastemm) / 60 > cfg.mon_aulow)
|
---|
265 | cau = (-1);
|
---|
266 |
|
---|
267 | else
|
---|
268 | cau = 1;
|
---|
269 |
|
---|
270 | } else {
|
---|
271 | cau = 0;
|
---|
272 | }
|
---|
273 |
|
---|
274 | if( cl->typ == 'r')
|
---|
275 | {
|
---|
276 | int32_t i;
|
---|
277 | struct s_reader *rdr;
|
---|
278 | for (i=0,rdr=first_active_reader; rdr ; rdr=rdr->next, i++)
|
---|
279 | if (cl->reader == rdr)
|
---|
280 | lrt=i;
|
---|
281 |
|
---|
282 | if( lrt >= 0 )
|
---|
283 | lrt = 10 + cl->reader->card_status;
|
---|
284 | }
|
---|
285 | else
|
---|
286 | lrt = cl->cwlastresptime;
|
---|
287 | localtime_r(&cl->login, <);
|
---|
288 | snprintf(ldate, sizeof(ldate), "%02d.%02d.%02d", lt.tm_mday, lt.tm_mon+1, lt.tm_year % 100);
|
---|
289 | int32_t cnr=get_threadnum(cl);
|
---|
290 | snprintf(ltime, sizeof(ldate), "%02d:%02d:%02d", lt.tm_hour, lt.tm_min, lt.tm_sec);
|
---|
291 | snprintf(sbuf, 256, "[%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",
|
---|
292 | id, (uint32_t)cl->thread, cl->typ, cnr, usr, cau, cl->crypted,
|
---|
293 | cs_inet_ntoa(cl->ip), cl->port, monitor_get_proto(cl),
|
---|
294 | ldate, ltime, lsec, cl->last_caid, cl->last_srvid,
|
---|
295 | get_servicename(cl, cl->last_srvid, cl->last_caid, channame), isec, con,
|
---|
296 | cl->cwfound, cl->cwnot, cl->cwcache, cl->cwignored,
|
---|
297 | cl->cwtout, cl->emmok, cl->emmnok, lrt);
|
---|
298 | }
|
---|
299 | }
|
---|
300 | return(sbuf);
|
---|
301 | }
|
---|
302 |
|
---|
303 | static void monitor_process_info(){
|
---|
304 | time_t now = time((time_t)0);
|
---|
305 | char sbuf[256];
|
---|
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, sbuf), 0);
|
---|
320 | }
|
---|
321 | }
|
---|
322 | monitor_send_info(NULL, 1);
|
---|
323 | }
|
---|
324 |
|
---|
325 | static void monitor_send_details(char *txt, uint32_t 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, uint32_t 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=%d 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=%u ms", cfg.ctimeout);
|
---|
361 | monitor_send_details(buf, pid);
|
---|
362 | snprintf(buf, 256, "CacheDelay=%d 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, (uint32_t)(cl->thread));
|
---|
408 | ptr1[-1]='\n';
|
---|
409 | }
|
---|
410 | }
|
---|
411 | } else {
|
---|
412 | monitor_send_details("Missing reader index or entitlement not saved!", (uint32_t)(cl->thread));
|
---|
413 | }
|
---|
414 |
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | static void monitor_process_details(char *arg){
|
---|
419 | uint32_t 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,"%X",&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, (uint32_t)(cl->thread));
|
---|
440 | break;
|
---|
441 | case 'c': case 'm':
|
---|
442 | monitor_send_details(monitor_client_info(1, cl, sbuf), (uint32_t)(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, sbuf), (uint32_t)(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 | if (!flag) return; //no arg
|
---|
478 |
|
---|
479 | struct s_client *cur_cl = cur_client();
|
---|
480 | if (strcmp(flag, "on")) {
|
---|
481 | if (strcmp(flag, "onwohist")) {
|
---|
482 | cur_cl->log=0;
|
---|
483 | return;
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | if (cur_cl->log) // already on
|
---|
488 | return;
|
---|
489 |
|
---|
490 | int32_t i, d = 0;
|
---|
491 | if (!strcmp(flag, "on") && loghist){
|
---|
492 | char *t_loghistptr = loghistptr, *ptr1 = NULL;
|
---|
493 | int32_t l1 = strlen(t_loghistptr+1) + 2;
|
---|
494 | char *lastpos = loghist + (cfg.loghistorysize)-1;
|
---|
495 |
|
---|
496 | for (ptr1 = t_loghistptr + l1, i=0; i<200; i++, ptr1 = ptr1+l1) {
|
---|
497 | l1 = strlen(ptr1)+1;
|
---|
498 | if (!d && ((ptr1 >= lastpos) || (l1 < 2))) {
|
---|
499 | ptr1 = loghist;
|
---|
500 | l1 = strlen(ptr1)+1;
|
---|
501 | d++;
|
---|
502 | }
|
---|
503 |
|
---|
504 | if (d && ((ptr1 >= t_loghistptr) || (l1 < 2)))
|
---|
505 | break;
|
---|
506 |
|
---|
507 | char p_usr[32], p_txt[512];
|
---|
508 | size_t pos1 = strcspn(ptr1, "\t") + 1;
|
---|
509 |
|
---|
510 | cs_strncpy(p_usr, ptr1 , pos1 > sizeof(p_usr) ? sizeof(p_usr) : pos1);
|
---|
511 |
|
---|
512 | if ((p_usr[0]) && ((cur_cl->monlvl > 1) || (cur_cl->account && !strcmp(p_usr, cur_cl->account->usr)))) {
|
---|
513 | snprintf(p_txt, sizeof(p_txt), "[LOG%03d]%s", cur_cl->logcounter, ptr1+pos1);
|
---|
514 | cur_cl->logcounter=(cur_cl->logcounter + 1) % 1000;
|
---|
515 | monitor_send(p_txt);
|
---|
516 | }
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | cur_cl->log=1;
|
---|
521 | }
|
---|
522 |
|
---|
523 | static void monitor_set_debuglevel(char *flag){
|
---|
524 | if (flag) {
|
---|
525 | cs_dblevel = atoi(flag);
|
---|
526 | #ifndef WITH_DEBUG
|
---|
527 | cs_log("*** Warning: Debug Support not compiled in ***");
|
---|
528 | #else
|
---|
529 | cs_log("%s debug_level=%d", "all", cs_dblevel);
|
---|
530 | #endif
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | static void monitor_get_account(){
|
---|
535 | struct s_auth *account;
|
---|
536 | char buf[256];
|
---|
537 | int32_t count = 0;
|
---|
538 |
|
---|
539 | for (account=cfg.account; (account); account=account->next){
|
---|
540 | count++;
|
---|
541 | snprintf(buf, sizeof(buf), "[U-----]%s\n", account->usr);
|
---|
542 | monitor_send_info(buf, 0);
|
---|
543 | }
|
---|
544 | snprintf(buf, sizeof(buf), "[U-----] %i User registered\n", count);
|
---|
545 | monitor_send_info(buf, 1);
|
---|
546 | return;
|
---|
547 | }
|
---|
548 |
|
---|
549 | static void monitor_set_account(char *args){
|
---|
550 | struct s_auth *account;
|
---|
551 | char delimiter[] = " =";
|
---|
552 | char *ptr, *saveptr1 = NULL;
|
---|
553 | int32_t argidx, i, found;
|
---|
554 | char *argarray[3];
|
---|
555 | static const char *token[]={"au", "sleep", "uniq", "monlevel", "group", "services", "betatunnel", "ident", "caid", "chid", "class", "hostname", "expdate", "keepalive", "disabled"};
|
---|
556 | int32_t tokencnt = sizeof(token)/sizeof(char *);
|
---|
557 | char buf[256], tmp[64];
|
---|
558 |
|
---|
559 | argidx = 0;
|
---|
560 | found = 0;
|
---|
561 |
|
---|
562 | snprintf(tmp, sizeof(tmp), "%s",args);
|
---|
563 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s check\n", tmp);
|
---|
564 | monitor_send_info(buf, 0);
|
---|
565 |
|
---|
566 | ptr = strtok_r(args, delimiter, &saveptr1);
|
---|
567 |
|
---|
568 | // resolve arguments
|
---|
569 | while(ptr != NULL) {
|
---|
570 | argarray[argidx]=trim(ptr);
|
---|
571 | ptr = strtok_r(NULL, delimiter, &saveptr1);
|
---|
572 | argidx++;
|
---|
573 | }
|
---|
574 |
|
---|
575 | if(argidx != 3) {
|
---|
576 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s failed - wrong number of parameters (%d)\n",tmp, argidx);
|
---|
577 | monitor_send_info(buf, 0);
|
---|
578 | snprintf(buf, sizeof(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 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s failed - user %s not found\n",tmp , argarray[0]);
|
---|
593 | monitor_send_info(buf, 0);
|
---|
594 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
|
---|
595 | monitor_send_info(buf, 1);
|
---|
596 | return;
|
---|
597 | }
|
---|
598 |
|
---|
599 | found = -1;
|
---|
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 = i;
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | if (found < 0){
|
---|
614 | snprintf(buf, sizeof(buf), "[S-0000]setuser: parameter %s not exist. possible values:\n", argarray[1]);
|
---|
615 | monitor_send_info(buf, 0);
|
---|
616 | for (i = 0; i < tokencnt; i++){
|
---|
617 | snprintf(buf, sizeof(buf), "[S-0000]%s\n", token[i]);
|
---|
618 | monitor_send_info(buf, 0);
|
---|
619 | }
|
---|
620 | snprintf(buf, sizeof(buf),"[S-0000]setuser: %s end\n", tmp);
|
---|
621 | monitor_send_info(buf, 1);
|
---|
622 | return;
|
---|
623 | } else {
|
---|
624 | chk_account(token[found], argarray[2], account);
|
---|
625 | }
|
---|
626 |
|
---|
627 | if (write_userdb(cfg.account)==0)
|
---|
628 | cs_reinit_clients(cfg.account);
|
---|
629 |
|
---|
630 | snprintf(buf, sizeof(buf),"[S-0000]setuser: %s done - param %s set to %s\n", tmp, argarray[1], argarray[2]);
|
---|
631 | monitor_send_info(buf, 1);
|
---|
632 | }
|
---|
633 |
|
---|
634 | static void monitor_set_server(char *args){
|
---|
635 | char delimiter[] = "=";
|
---|
636 | char *ptr, *saveptr1;
|
---|
637 | int32_t argidx, i, found;
|
---|
638 | char *argarray[3];
|
---|
639 | static const char *token[]={"clienttimeout", "fallbacktimeout", "clientmaxidle", "cachedelay", "bindwait", "netprio", "sleep", "unlockparental", "serialreadertimeout", "maxlogsize", "showecmdw", "waitforcards", "preferlocalcards"};
|
---|
640 | char buf[256];
|
---|
641 |
|
---|
642 | argidx=0; found=0;
|
---|
643 | ptr = strtok_r(args, delimiter, &saveptr1);
|
---|
644 |
|
---|
645 | // resolve arguments
|
---|
646 | while(ptr != NULL) {
|
---|
647 | argarray[argidx]=trim(ptr);
|
---|
648 | ptr = strtok_r(NULL, delimiter, &saveptr1);
|
---|
649 | argidx++;
|
---|
650 | }
|
---|
651 |
|
---|
652 | if(argidx != 2) {
|
---|
653 | snprintf(buf, sizeof(buf),"[S-0000]setserver failed - wrong number of parameters (%d)\n", argidx);
|
---|
654 | monitor_send_info(buf, 1);
|
---|
655 | return;
|
---|
656 | }
|
---|
657 |
|
---|
658 | trim(argarray[0]);
|
---|
659 | trim(argarray[1]);
|
---|
660 | strtolower(argarray[0]);
|
---|
661 |
|
---|
662 | for (i = 0; i < 13; i++)
|
---|
663 | if (!strcmp(argarray[0], token[i])) break;
|
---|
664 |
|
---|
665 | if (i < 13){
|
---|
666 | chk_t_global(token[i],argarray[1]);
|
---|
667 | snprintf(buf, sizeof(buf), "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
|
---|
668 | monitor_send_info(buf, 1);
|
---|
669 | } else {
|
---|
670 | snprintf(buf, sizeof(buf), "[S-0000]setserver failed - parameter %s not exist\n", argarray[0]);
|
---|
671 | monitor_send_info(buf, 1);
|
---|
672 | return;
|
---|
673 | }
|
---|
674 |
|
---|
675 | if (cfg.ftimeout>=cfg.ctimeout) {
|
---|
676 | cfg.ftimeout = cfg.ctimeout - 100;
|
---|
677 | snprintf(buf, sizeof(buf), "[S-0000]setserver WARNING: fallbacktimeout adjusted to %u ms\n", cfg.ftimeout);
|
---|
678 | monitor_send_info(buf, 1);
|
---|
679 | }
|
---|
680 | if(cfg.ftimeout < cfg.srtimeout) {
|
---|
681 | cfg.ftimeout = cfg.srtimeout + 100;
|
---|
682 | snprintf(buf, sizeof(buf), "[S-0000]setserver WARNING: fallbacktimeout adjusted to %u ms\n", cfg.ftimeout);
|
---|
683 | monitor_send_info(buf, 1);
|
---|
684 | }
|
---|
685 | if(cfg.ctimeout < cfg.srtimeout) {
|
---|
686 | cfg.ctimeout = cfg.srtimeout + 100;
|
---|
687 | snprintf(buf, sizeof(buf), "[S-0000]setserver WARNING: clienttimeout adjusted to %u ms\n", cfg.ctimeout);
|
---|
688 | monitor_send_info(buf, 1);
|
---|
689 | }
|
---|
690 | //kill(first_client->pid, SIGUSR1);
|
---|
691 | }
|
---|
692 |
|
---|
693 | #ifdef WEBIF
|
---|
694 | static void monitor_restart_server(){
|
---|
695 | cs_restart_oscam();
|
---|
696 | }
|
---|
697 | #endif
|
---|
698 |
|
---|
699 | static void monitor_list_commands(const char *args[], int32_t cmdcnt){
|
---|
700 | int32_t i;
|
---|
701 | for (i = 0; i < cmdcnt; i++) {
|
---|
702 | char buf[64];
|
---|
703 | snprintf(buf, sizeof(buf), "[S-0000]commands: %s\n", args[i]);
|
---|
704 | if(i < cmdcnt-1)
|
---|
705 | monitor_send_info(buf, 0);
|
---|
706 | else
|
---|
707 | monitor_send_info(buf, 1);
|
---|
708 | }
|
---|
709 | }
|
---|
710 |
|
---|
711 | static int32_t monitor_process_request(char *req)
|
---|
712 | {
|
---|
713 | int32_t i, rc;
|
---|
714 | static const char *cmd[] = {"login",
|
---|
715 | "exit",
|
---|
716 | "log",
|
---|
717 | "status",
|
---|
718 | "shutdown",
|
---|
719 | "reload",
|
---|
720 | "details",
|
---|
721 | "version",
|
---|
722 | "debug",
|
---|
723 | "getuser",
|
---|
724 | "setuser",
|
---|
725 | "setserver",
|
---|
726 | "commands",
|
---|
727 | "keepalive",
|
---|
728 | "reread"
|
---|
729 | #ifdef WEBIF
|
---|
730 | ,"restart"
|
---|
731 | #endif
|
---|
732 | };
|
---|
733 |
|
---|
734 | int32_t cmdcnt = sizeof(cmd)/sizeof(char *); // Calculate the amount of items in array
|
---|
735 | char *arg;
|
---|
736 | struct s_client *cur_cl = cur_client();
|
---|
737 |
|
---|
738 | if( (arg = strchr(req, ' ')) ) { *arg++ = 0; trim(arg); }
|
---|
739 | //trim(req);
|
---|
740 | if ((!cur_cl->auth) && (strcmp(req, cmd[0]))) monitor_login(NULL);
|
---|
741 |
|
---|
742 | for (rc=1, i = 0; i < cmdcnt; i++)
|
---|
743 | if (!strcmp(req, cmd[i])) {
|
---|
744 | switch(i) {
|
---|
745 | case 0: monitor_login(arg); break; // login
|
---|
746 | case 1: rc=0; break; // exit
|
---|
747 | case 2: monitor_logsend(arg); break; // log
|
---|
748 | case 3: monitor_process_info(); break; // status
|
---|
749 | case 4: if (cur_cl->monlvl > 3) cs_exit(SIGQUIT); break; // shutdown
|
---|
750 | case 5: if (cur_cl->monlvl > 2) cs_accounts_chk(); break; // reload
|
---|
751 | case 6: monitor_process_details(arg); break; // details
|
---|
752 | case 7: monitor_send_details_version(); break; // version
|
---|
753 | case 8: if (cur_cl->monlvl > 3) monitor_set_debuglevel(arg); break; // debuglevel
|
---|
754 | case 9: if (cur_cl->monlvl > 3) monitor_get_account(); break; // getuser
|
---|
755 | case 10: if (cur_cl->monlvl > 3) monitor_set_account(arg); break; // setuser
|
---|
756 | case 11: if (cur_cl->monlvl > 3) monitor_set_server(arg); break; // setserver
|
---|
757 | case 12: if (cur_cl->monlvl > 3) monitor_list_commands(cmd, cmdcnt); break; // list commands
|
---|
758 | case 13: if (cur_cl->monlvl > 3) monitor_send_keepalive_ack(); break; // keepalive
|
---|
759 | case 14: { char buf[64];snprintf(buf, sizeof(buf), "[S-0000]reread\n");monitor_send_info(buf, 1); cs_card_info(); break; } // reread
|
---|
760 | #ifdef WEBIF
|
---|
761 | case 15: if (cur_cl->monlvl > 3) monitor_restart_server(); break; // keepalive
|
---|
762 | #endif
|
---|
763 | default: continue;
|
---|
764 | }
|
---|
765 | break;
|
---|
766 | }
|
---|
767 | return(rc);
|
---|
768 | }
|
---|
769 |
|
---|
770 | static void * monitor_server(void *cli){
|
---|
771 | int32_t n;
|
---|
772 | uchar mbuf[1024];
|
---|
773 |
|
---|
774 | struct s_client * client = (struct s_client *) cli;
|
---|
775 | client->thread=pthread_self();
|
---|
776 | pthread_setspecific(getclient, cli);
|
---|
777 | client->typ='m';
|
---|
778 | while (((n = process_input(mbuf, sizeof(mbuf), cfg.cmaxidle)) >= 0) && monitor_process_request((char *)mbuf));
|
---|
779 | cs_disconnect_client(cli);
|
---|
780 | return NULL;
|
---|
781 | }
|
---|
782 |
|
---|
783 | void module_monitor(struct s_module *ph){
|
---|
784 | static PTAB ptab; //since there is always only 1 monitor running, this is threadsafe
|
---|
785 | ptab.ports[0].s_port = cfg.mon_port;
|
---|
786 | ph->ptab = &ptab;
|
---|
787 | ph->ptab->nports = 1;
|
---|
788 |
|
---|
789 | if (cfg.mon_aulow < 1)
|
---|
790 | cfg.mon_aulow = 30;
|
---|
791 | cs_strncpy(ph->desc, "monitor", sizeof(ph->desc));
|
---|
792 | ph->type=MOD_CONN_UDP;
|
---|
793 | ph->multi = 0;
|
---|
794 | ph->watchdog = 1;
|
---|
795 | ph->s_ip = cfg.mon_srvip;
|
---|
796 | ph->s_handler = monitor_server;
|
---|
797 | ph->recv = monitor_recv;
|
---|
798 | // ph->send_dcw=NULL;
|
---|
799 | }
|
---|
800 |
|
---|
801 |
|
---|