1 | #include "globals.h"
|
---|
2 | #ifdef MODULE_MONITOR
|
---|
3 | #include "cscrypt/md5.h"
|
---|
4 | #include "module-monitor.h"
|
---|
5 | #include "oscam-aes.h"
|
---|
6 | #include "oscam-client.h"
|
---|
7 | #include "oscam-config.h"
|
---|
8 | #include "oscam-conf-chk.h"
|
---|
9 | #include "oscam-net.h"
|
---|
10 | #include "oscam-reader.h"
|
---|
11 | #include "oscam-string.h"
|
---|
12 | #include "oscam-work.h"
|
---|
13 |
|
---|
14 | extern char *entitlement_type[];
|
---|
15 | extern char *loghist;
|
---|
16 | extern char *loghistptr;
|
---|
17 |
|
---|
18 | struct monitor_data
|
---|
19 | {
|
---|
20 | bool auth;
|
---|
21 | uint8_t ucrc[4];
|
---|
22 | struct aes_keys aes_keys;
|
---|
23 | int32_t seq;
|
---|
24 | int32_t counter;
|
---|
25 | char btxt[256];
|
---|
26 | };
|
---|
27 |
|
---|
28 | static int8_t monitor_check_ip(void)
|
---|
29 | {
|
---|
30 | int32_t ok = 0;
|
---|
31 | struct s_client *cur_cl = cur_client();
|
---|
32 | struct monitor_data *module_data = cur_cl->module_data;
|
---|
33 |
|
---|
34 | if(module_data->auth) { return 0; }
|
---|
35 | ok = check_ip(cfg.mon_allowed, cur_cl->ip);
|
---|
36 | if(!ok)
|
---|
37 | {
|
---|
38 | cs_auth_client(cur_cl, (struct s_auth *)0, "invalid ip");
|
---|
39 | return -1;
|
---|
40 | }
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 |
|
---|
44 | static int8_t monitor_auth_client(char *usr, char *pwd)
|
---|
45 | {
|
---|
46 | struct s_auth *account;
|
---|
47 | struct s_client *cur_cl = cur_client();
|
---|
48 | struct monitor_data *module_data = cur_cl->module_data;
|
---|
49 |
|
---|
50 | if(module_data->auth) { return 0; }
|
---|
51 | if((!usr) || (!pwd))
|
---|
52 | {
|
---|
53 | cs_auth_client(cur_cl, (struct s_auth *)0, NULL);
|
---|
54 | return -1;
|
---|
55 | }
|
---|
56 | for(account = cfg.account; account; account = account->next)
|
---|
57 | {
|
---|
58 | if(account->monlvl && streq(usr, account->usr) && streq(pwd, account->pwd))
|
---|
59 | {
|
---|
60 | module_data->auth = 1;
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | if(!module_data->auth)
|
---|
65 | {
|
---|
66 | cs_auth_client(cur_cl, (struct s_auth *)0, "invalid account");
|
---|
67 | return -1;
|
---|
68 | }
|
---|
69 | if(cs_auth_client(cur_cl, account, NULL))
|
---|
70 | { return -1; }
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | static int32_t secmon_auth_client(uchar *ucrc)
|
---|
75 | {
|
---|
76 | uint32_t crc;
|
---|
77 | struct s_auth *account;
|
---|
78 | struct s_client *cur_cl = cur_client();
|
---|
79 | struct monitor_data *module_data = cur_cl->module_data;
|
---|
80 | unsigned char md5tmp[MD5_DIGEST_LENGTH];
|
---|
81 |
|
---|
82 | if(module_data->auth)
|
---|
83 | {
|
---|
84 | int32_t s = memcmp(module_data->ucrc, ucrc, 4);
|
---|
85 | if(s)
|
---|
86 | { cs_log("wrong user-crc or garbage !?"); }
|
---|
87 | return !s;
|
---|
88 | }
|
---|
89 | cur_cl->crypted = 1;
|
---|
90 | crc = (ucrc[0] << 24) | (ucrc[1] << 16) | (ucrc[2] << 8) | ucrc[3];
|
---|
91 | for(account = cfg.account; (account) && (!module_data->auth); account = account->next)
|
---|
92 | if((account->monlvl) &&
|
---|
93 | (crc == crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), md5tmp), MD5_DIGEST_LENGTH)))
|
---|
94 | {
|
---|
95 | memcpy(module_data->ucrc, ucrc, 4);
|
---|
96 | aes_set_key(&module_data->aes_keys, (char *)MD5((unsigned char *)ESTR(account->pwd), strlen(ESTR(account->pwd)), md5tmp));
|
---|
97 | if(cs_auth_client(cur_cl, account, NULL))
|
---|
98 | { return -1; }
|
---|
99 | module_data->auth = 1;
|
---|
100 | }
|
---|
101 | if(!module_data->auth)
|
---|
102 | {
|
---|
103 | cs_auth_client(cur_cl, (struct s_auth *)0, "invalid user");
|
---|
104 | return -1;
|
---|
105 | }
|
---|
106 | return module_data->auth;
|
---|
107 | }
|
---|
108 |
|
---|
109 | int32_t monitor_send_idx(struct s_client *cl, char *txt)
|
---|
110 | {
|
---|
111 | struct monitor_data *module_data = cl->module_data;
|
---|
112 | int32_t l;
|
---|
113 | unsigned char buf[256 + 32];
|
---|
114 | if(!cl->udp_fd)
|
---|
115 | { return -1; }
|
---|
116 | struct timespec req_ts;
|
---|
117 | req_ts.tv_sec = 0;
|
---|
118 | req_ts.tv_nsec = 500000;
|
---|
119 | nanosleep(&req_ts, NULL); //avoid lost udp-pakkets
|
---|
120 | if(!cl->crypted)
|
---|
121 | { return sendto(cl->udp_fd, txt, strlen(txt), 0, (struct sockaddr *)&cl->udp_sa, cl->udp_sa_len); }
|
---|
122 | buf[0] = '&';
|
---|
123 | buf[9] = l = strlen(txt);
|
---|
124 | l = boundary(4, l + 5) + 5;
|
---|
125 | memcpy(buf + 1, module_data->ucrc, 4);
|
---|
126 | cs_strncpy((char *)buf + 10, txt, sizeof(buf) - 10);
|
---|
127 | uchar tmp[10];
|
---|
128 | memcpy(buf + 5, i2b_buf(4, crc32(0L, buf + 10, l - 10), tmp), 4);
|
---|
129 | aes_encrypt_idx(&module_data->aes_keys, buf + 5, l - 5);
|
---|
130 | return sendto(cl->udp_fd, buf, l, 0, (struct sockaddr *)&cl->udp_sa, cl->udp_sa_len);
|
---|
131 | }
|
---|
132 |
|
---|
133 | #define monitor_send(t) monitor_send_idx(cur_client(), t)
|
---|
134 |
|
---|
135 | static int32_t monitor_recv(struct s_client *client, uchar *buf, int32_t UNUSED(buflen))
|
---|
136 | {
|
---|
137 | int32_t n = recv_from_udpipe(buf);
|
---|
138 | if(!n)
|
---|
139 | { return buf[0] = 0; }
|
---|
140 | if(!client->module_data && !cs_malloc(&client->module_data, sizeof(struct monitor_data)))
|
---|
141 | { return 0; }
|
---|
142 | if(buf[0] == '&')
|
---|
143 | {
|
---|
144 | int32_t bsize;
|
---|
145 | if(n < 21) // 5+16 is minimum
|
---|
146 | {
|
---|
147 | cs_log("packet too small!");
|
---|
148 | return buf[0] = 0;
|
---|
149 | }
|
---|
150 | int32_t res = secmon_auth_client(buf + 1);
|
---|
151 | if(res == -1)
|
---|
152 | {
|
---|
153 | cs_disconnect_client(client);
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 | if(!res)
|
---|
157 | {
|
---|
158 | return buf[0] = 0;
|
---|
159 | }
|
---|
160 | struct monitor_data *module_data = client->module_data;
|
---|
161 | aes_decrypt(&module_data->aes_keys, buf + 5, 16);
|
---|
162 | bsize = boundary(4, buf[9] + 5) + 5;
|
---|
163 | if(n < bsize)
|
---|
164 | {
|
---|
165 | cs_log("packet-size mismatch !");
|
---|
166 | return buf[0] = 0;
|
---|
167 | }
|
---|
168 | aes_decrypt(&module_data->aes_keys, buf + 21, n - 21);
|
---|
169 | uchar tmp[10];
|
---|
170 | if(memcmp(buf + 5, i2b_buf(4, crc32(0L, buf + 10, n - 10), tmp), 4))
|
---|
171 | {
|
---|
172 | cs_log("CRC error ! wrong password ?");
|
---|
173 | return buf[0] = 0;
|
---|
174 | }
|
---|
175 | n = buf[9];
|
---|
176 | memmove(buf, buf + 10, n);
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | if(monitor_check_ip() == -1)
|
---|
181 | {
|
---|
182 | cs_disconnect_client(client);
|
---|
183 | return 0;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | buf[n] = '\0';
|
---|
187 | n = strlen(trim((char *)buf));
|
---|
188 | if(n) { client->last = time((time_t *) 0); }
|
---|
189 | return n;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static void monitor_send_info(char *txt, int32_t last)
|
---|
193 | {
|
---|
194 | struct s_client *cur_cl = cur_client();
|
---|
195 | struct monitor_data *module_data = cur_cl->module_data;
|
---|
196 | char buf[8];
|
---|
197 | if(txt)
|
---|
198 | {
|
---|
199 | if(!module_data->btxt[0])
|
---|
200 | {
|
---|
201 | module_data->counter = 0;
|
---|
202 | txt[2] = 'B';
|
---|
203 | }
|
---|
204 | else
|
---|
205 | { module_data->counter++; }
|
---|
206 | snprintf(buf, sizeof(buf), "%03d", module_data->counter);
|
---|
207 | memcpy(txt + 4, buf, 3);
|
---|
208 | txt[3] = '0' + module_data->seq;
|
---|
209 | }
|
---|
210 | else if(!last)
|
---|
211 | { return; }
|
---|
212 |
|
---|
213 | if(!last)
|
---|
214 | {
|
---|
215 | if(module_data->btxt[0]) { monitor_send(module_data->btxt); }
|
---|
216 | cs_strncpy(module_data->btxt, txt, sizeof(module_data->btxt));
|
---|
217 | return;
|
---|
218 | }
|
---|
219 |
|
---|
220 | if(txt && module_data->btxt[0])
|
---|
221 | {
|
---|
222 | monitor_send(module_data->btxt);
|
---|
223 | txt[2] = 'E';
|
---|
224 | cs_strncpy(module_data->btxt, txt, sizeof(module_data->btxt));
|
---|
225 | }
|
---|
226 | else
|
---|
227 | {
|
---|
228 | if(txt)
|
---|
229 | { cs_strncpy(module_data->btxt, txt, sizeof(module_data->btxt)); }
|
---|
230 | module_data->btxt[2] = (module_data->btxt[2] == 'B') ? 'S' : 'E';
|
---|
231 | }
|
---|
232 |
|
---|
233 | if(module_data->btxt[0])
|
---|
234 | {
|
---|
235 | monitor_send(module_data->btxt);
|
---|
236 | module_data->seq = (module_data->seq + 1) % 10;
|
---|
237 | }
|
---|
238 | module_data->btxt[0] = 0;
|
---|
239 | }
|
---|
240 |
|
---|
241 | static char *monitor_client_info(char id, struct s_client *cl, char *sbuf)
|
---|
242 | {
|
---|
243 | char channame[32];
|
---|
244 | sbuf[0] = '\0';
|
---|
245 |
|
---|
246 | if(cl)
|
---|
247 | {
|
---|
248 | char ldate[16], ltime[16], *usr;
|
---|
249 | int32_t lsec, isec, con, cau, lrt = - 1;
|
---|
250 | time_t now;
|
---|
251 | struct tm lt;
|
---|
252 | now = time((time_t *)0);
|
---|
253 |
|
---|
254 | if((cfg.hideclient_to <= 0) ||
|
---|
255 | (now - cl->lastecm < cfg.hideclient_to) ||
|
---|
256 | (now - cl->lastemm < cfg.hideclient_to) ||
|
---|
257 | (cl->typ != 'c'))
|
---|
258 | {
|
---|
259 | lsec = now - cl->login;
|
---|
260 | isec = now - cl->last;
|
---|
261 | usr = username(cl);
|
---|
262 | if(cl->dup)
|
---|
263 | { con = 2; }
|
---|
264 | else if((cl->tosleep) && (now - cl->lastswitch > cl->tosleep))
|
---|
265 | { con = 1; }
|
---|
266 | else
|
---|
267 | { con = 0; }
|
---|
268 |
|
---|
269 | // no AU reader == 0 / AU ok == 1 / Last EMM > aulow == -1
|
---|
270 | if(cl->typ == 'c' || cl->typ == 'p' || cl->typ == 'r')
|
---|
271 | {
|
---|
272 |
|
---|
273 | if((cl->typ == 'c' && ll_count(cl->aureader_list) == 0) ||
|
---|
274 | ((cl->typ == 'p' || cl->typ == 'r') && cl->reader->audisabled))
|
---|
275 | { cau = 0; }
|
---|
276 |
|
---|
277 | else if((now - cl->lastemm) / 60 > cfg.aulow)
|
---|
278 | { cau = (-1); }
|
---|
279 |
|
---|
280 | else
|
---|
281 | { cau = 1; }
|
---|
282 |
|
---|
283 | }
|
---|
284 | else
|
---|
285 | {
|
---|
286 | cau = 0;
|
---|
287 | }
|
---|
288 |
|
---|
289 | if(cl->typ == 'r')
|
---|
290 | {
|
---|
291 | int32_t i;
|
---|
292 | struct s_reader *rdr;
|
---|
293 | for(i = 0, rdr = first_active_reader; rdr ; rdr = rdr->next, i++)
|
---|
294 | if(cl->reader == rdr)
|
---|
295 | { lrt = i; }
|
---|
296 |
|
---|
297 | if(lrt >= 0)
|
---|
298 | { lrt = 10 + cl->reader->card_status; }
|
---|
299 | }
|
---|
300 | else
|
---|
301 | { lrt = cl->cwlastresptime; }
|
---|
302 | localtime_r(&cl->login, <);
|
---|
303 | snprintf(ldate, sizeof(ldate), "%02d.%02d.%02d", lt.tm_mday, lt.tm_mon + 1, lt.tm_year % 100);
|
---|
304 | int32_t cnr = get_threadnum(cl);
|
---|
305 | snprintf(ltime, sizeof(ldate), "%02d:%02d:%02d", lt.tm_hour, lt.tm_min, lt.tm_sec);
|
---|
306 | 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",
|
---|
307 | id, cl->tid, cl->typ, cnr, usr, cau, cl->crypted,
|
---|
308 | cs_inet_ntoa(cl->ip), cl->port, client_get_proto(cl),
|
---|
309 | ldate, ltime, lsec, cl->last_caid, cl->last_srvid,
|
---|
310 | get_servicename_or_null(cl, cl->last_srvid, cl->last_caid, channame), 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(void)
|
---|
319 | {
|
---|
320 | time_t now = time((time_t *)0);
|
---|
321 | char sbuf[256];
|
---|
322 |
|
---|
323 | struct s_client *cl, *cur_cl = cur_client();
|
---|
324 |
|
---|
325 | for(cl = first_client; cl ; cl = cl->next)
|
---|
326 | {
|
---|
327 | if((cfg.hideclient_to <= 0) ||
|
---|
328 | (now - cl->lastecm < cfg.hideclient_to) ||
|
---|
329 | (now - cl->lastemm < cfg.hideclient_to) ||
|
---|
330 | (cl->typ != 'c'))
|
---|
331 | {
|
---|
332 | if((cur_cl->monlvl < 2) && (cl->typ != 's'))
|
---|
333 | {
|
---|
334 | if((cur_cl->account && cl->account && strcmp(cur_cl->account->usr, cl->account->usr)) ||
|
---|
335 | ((cl->typ != 'c') && (cl->typ != 'm')))
|
---|
336 | { continue; }
|
---|
337 | }
|
---|
338 | monitor_send_info(monitor_client_info('I', cl, sbuf), 0);
|
---|
339 | }
|
---|
340 | }
|
---|
341 | monitor_send_info(NULL, 1);
|
---|
342 | }
|
---|
343 |
|
---|
344 | static void monitor_send_details(char *txt, uint32_t tid)
|
---|
345 | {
|
---|
346 | char buf[256];
|
---|
347 | snprintf(buf, 255, "[D-----]%8X|%s\n", tid, txt);
|
---|
348 | monitor_send_info(buf, 0);
|
---|
349 | }
|
---|
350 |
|
---|
351 | static void monitor_send_details_version(void)
|
---|
352 | {
|
---|
353 | char buf[256];
|
---|
354 | snprintf(buf, sizeof(buf), "[V-0000]version=%s, build=%s, system=%s\n", CS_VERSION, CS_SVN_VERSION, CS_TARGET);
|
---|
355 | monitor_send_info(buf, 1);
|
---|
356 | }
|
---|
357 |
|
---|
358 | static void monitor_send_keepalive_ack(void)
|
---|
359 | {
|
---|
360 | char buf[32];
|
---|
361 | snprintf(buf, sizeof(buf), "[K-0000]keepalive_ack\n");
|
---|
362 | monitor_send_info(buf, 1);
|
---|
363 | }
|
---|
364 |
|
---|
365 | static void monitor_process_details_master(char *buf, uint32_t pid)
|
---|
366 | {
|
---|
367 | snprintf(buf, 256, "Version=%sr%s", CS_VERSION, CS_SVN_VERSION);
|
---|
368 | monitor_send_details(buf, pid);
|
---|
369 | snprintf(buf, 256, "System=%s", CS_TARGET);
|
---|
370 | monitor_send_details(buf, pid);
|
---|
371 | snprintf(buf, 256, "DebugLevel=%d", cs_dblevel);
|
---|
372 | monitor_send_details(buf, pid);
|
---|
373 | snprintf(buf, 256, "MaxClients=UNLIMITED");
|
---|
374 | monitor_send_details(buf, pid);
|
---|
375 | snprintf(buf, 256, "ClientMaxIdle=%d sec", cfg.cmaxidle);
|
---|
376 | monitor_send_details(buf, pid);
|
---|
377 | if(cfg.max_log_size)
|
---|
378 | { snprintf(buf + 200, 56, "%d Kb", cfg.max_log_size); }
|
---|
379 | else
|
---|
380 | { cs_strncpy(buf + 200, "unlimited", 56); }
|
---|
381 | snprintf(buf, 256, "MaxLogsize=%s", buf + 200);
|
---|
382 | monitor_send_details(buf, pid);
|
---|
383 | snprintf(buf, 256, "ClientTimeout=%u ms", cfg.ctimeout);
|
---|
384 | monitor_send_details(buf, pid);
|
---|
385 | snprintf(buf, 256, "CacheDelay=%d ms", cfg.delay);
|
---|
386 | monitor_send_details(buf, pid);
|
---|
387 | if(cfg.cwlogdir)
|
---|
388 | {
|
---|
389 | snprintf(buf, 256, "CwlogDir=%s", cfg.cwlogdir);
|
---|
390 | monitor_send_details(buf, pid);
|
---|
391 | }
|
---|
392 | if(cfg.preferlocalcards)
|
---|
393 | {
|
---|
394 | snprintf(buf, 256, "PreferlocalCards=%d", cfg.preferlocalcards);
|
---|
395 | monitor_send_details(buf, pid);
|
---|
396 | }
|
---|
397 | if(cfg.waitforcards)
|
---|
398 | {
|
---|
399 | snprintf(buf, 256, "WaitforCards=%d", cfg.waitforcards);
|
---|
400 | monitor_send_details(buf, pid);
|
---|
401 | }
|
---|
402 | snprintf(buf, 256, "LogFile=%s", cfg.logfile);
|
---|
403 | monitor_send_details(buf, pid);
|
---|
404 | if(cfg.mailfile)
|
---|
405 | {
|
---|
406 | snprintf(buf, 256, "MailFile=%s", cfg.mailfile);
|
---|
407 | monitor_send_details(buf, pid);
|
---|
408 | }
|
---|
409 | if(cfg.usrfile)
|
---|
410 | {
|
---|
411 | snprintf(buf, 256, "UsrFile=%s", cfg.usrfile);
|
---|
412 | monitor_send_details(buf, pid);
|
---|
413 | }
|
---|
414 | monitor_send_details(buf, pid);
|
---|
415 | snprintf(buf, 256, "Sleep=%d", cfg.tosleep);
|
---|
416 | monitor_send_details(buf, pid);
|
---|
417 | snprintf(buf, 256, "Monitorport=%d", cfg.mon_port);
|
---|
418 | monitor_send_details(buf, pid);
|
---|
419 | snprintf(buf, 256, "Nice=%d", cfg.nice);
|
---|
420 | monitor_send_details(buf, pid);
|
---|
421 | #ifdef WEBIF
|
---|
422 | snprintf(buf, 256, "Restartmode=%d", cs_get_restartmode());
|
---|
423 | monitor_send_details(buf, pid);
|
---|
424 | #else
|
---|
425 | snprintf(buf, 256, "Restartmode=%s", "no");
|
---|
426 | monitor_send_details(buf, pid);
|
---|
427 | #endif
|
---|
428 |
|
---|
429 | // monitor_send_details(buf, pid);
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | static void monitor_process_details_reader(struct s_client *cl)
|
---|
434 | {
|
---|
435 | char tbuffer1[64], tbuffer2[64], buf[256] = { 0 }, tmpbuf[256] = { 0 }, valid_to[32] = { 0 };
|
---|
436 | struct s_reader *rdr = cl->reader;
|
---|
437 | if(!rdr)
|
---|
438 | {
|
---|
439 | monitor_send_details("Reader do not exist or it is not started.", cl->tid);
|
---|
440 | return;
|
---|
441 | }
|
---|
442 |
|
---|
443 | if(rdr->card_valid_to)
|
---|
444 | {
|
---|
445 | struct tm vto_t;
|
---|
446 | localtime_r(&rdr->card_valid_to, &vto_t);
|
---|
447 | strftime(valid_to, sizeof(valid_to) - 1, "%Y-%m-%d", &vto_t);
|
---|
448 | }
|
---|
449 | else
|
---|
450 | {
|
---|
451 | strncpy(valid_to, "n/a", 3);
|
---|
452 | }
|
---|
453 |
|
---|
454 | snprintf(tmpbuf, sizeof(tmpbuf) - 1, "Cardsystem: %s Reader: %s ValidTo: %s HexSerial: %s ATR: %s",
|
---|
455 | rdr->csystem.desc,
|
---|
456 | rdr->label,
|
---|
457 | valid_to,
|
---|
458 | cs_hexdump(1, rdr->hexserial, 8, tbuffer2, sizeof(tbuffer2)),
|
---|
459 | rdr->card_atr_length
|
---|
460 | ? cs_hexdump(1, rdr->card_atr, rdr->card_atr_length, buf, sizeof(buf))
|
---|
461 | : ""
|
---|
462 | );
|
---|
463 | monitor_send_details(tmpbuf, cl->tid);
|
---|
464 |
|
---|
465 | if(!rdr->ll_entitlements)
|
---|
466 | {
|
---|
467 | monitor_send_details("No entitlements for the reader.", cl->tid);
|
---|
468 | return;
|
---|
469 | }
|
---|
470 |
|
---|
471 | S_ENTITLEMENT *item;
|
---|
472 | LL_ITER itr = ll_iter_create(rdr->ll_entitlements);
|
---|
473 | time_t now = (time(NULL) / 86400) * 86400;
|
---|
474 |
|
---|
475 | while((item = ll_iter_next(&itr)))
|
---|
476 | {
|
---|
477 | struct tm start_t, end_t;
|
---|
478 |
|
---|
479 | localtime_r(&item->start, &start_t);
|
---|
480 | localtime_r(&item->end , &end_t);
|
---|
481 |
|
---|
482 | strftime(tbuffer1, sizeof(tbuffer1) - 1, "%Y-%m-%d %H:%M %z", &start_t);
|
---|
483 | strftime(tbuffer2, sizeof(tbuffer2) - 1, "%Y-%m-%d %H:%M %z", &end_t);
|
---|
484 |
|
---|
485 | char *entresname = get_tiername(item->id & 0xFFFF, item->caid, buf);
|
---|
486 | if(!entresname[0])
|
---|
487 | { entresname = get_provider(item->caid, item->provid, buf, sizeof(buf)); }
|
---|
488 |
|
---|
489 | snprintf(tmpbuf, sizeof(tmpbuf) - 1, "%s Type: %s CAID: %04X Provid: %06X ID: %08X%08X Class: %08X StartDate: %s ExpireDate: %s Name: %s",
|
---|
490 | item->end > now ? "active " : "expired",
|
---|
491 | entitlement_type[item->type],
|
---|
492 | item->caid,
|
---|
493 | item->provid,
|
---|
494 | (uint32_t)(item->id >> 32),
|
---|
495 | (uint32_t)(item->id),
|
---|
496 | item->class,
|
---|
497 | tbuffer1,
|
---|
498 | tbuffer2,
|
---|
499 | entresname
|
---|
500 | );
|
---|
501 | monitor_send_details(tmpbuf, cl->tid);
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | static void monitor_process_details(char *arg)
|
---|
507 | {
|
---|
508 | uint32_t tid = 0; //using threadid 8 positions hex see oscam-log.c //FIXME untested but pid isnt working anyway with threading
|
---|
509 | struct s_client *cl = NULL, *cl1;
|
---|
510 | char sbuf[256];
|
---|
511 |
|
---|
512 | if(!arg)
|
---|
513 | { cl = first_client; } // no arg - show master
|
---|
514 | else
|
---|
515 | {
|
---|
516 | if(sscanf(arg, "%X", &tid) == 1)
|
---|
517 | {
|
---|
518 | for(cl1 = first_client; cl1 ; cl1 = cl1->next)
|
---|
519 | if(cl1->tid == tid)
|
---|
520 | {
|
---|
521 | cl = cl1;
|
---|
522 | break;
|
---|
523 | }
|
---|
524 | }
|
---|
525 | }
|
---|
526 |
|
---|
527 | if(!cl)
|
---|
528 | { monitor_send_details("Invalid TID", tid); }
|
---|
529 | else
|
---|
530 | {
|
---|
531 | //monitor_send_info(monitor_client_info('D', idx), 0); //FIXME
|
---|
532 | switch(cl->typ)
|
---|
533 | {
|
---|
534 | case 's':
|
---|
535 | monitor_process_details_master(sbuf, cl->tid);
|
---|
536 | break;
|
---|
537 | case 'c':
|
---|
538 | case 'm':
|
---|
539 | monitor_send_details(monitor_client_info(1, cl, sbuf), cl->tid);
|
---|
540 | break;
|
---|
541 | case 'r':
|
---|
542 | monitor_process_details_reader(cl);//with client->typ='r' client->ridx is always filled and valid, so no need checking
|
---|
543 | break;
|
---|
544 | case 'p':
|
---|
545 | monitor_send_details(monitor_client_info(1, cl, sbuf), cl->tid);
|
---|
546 | break;
|
---|
547 | }
|
---|
548 | }
|
---|
549 | monitor_send_info(NULL, 1);
|
---|
550 | }
|
---|
551 |
|
---|
552 | static void monitor_send_login(void)
|
---|
553 | {
|
---|
554 | char buf[64];
|
---|
555 | struct s_client *cur_cl = cur_client();
|
---|
556 | struct monitor_data *module_data = cur_cl->module_data;
|
---|
557 | if(module_data->auth && cur_cl->account)
|
---|
558 | { snprintf(buf, sizeof(buf), "[A-0000]1|%s logged in\n", cur_cl->account->usr); }
|
---|
559 | else
|
---|
560 | { cs_strncpy(buf, "[A-0000]0|not logged in\n", sizeof(buf)); }
|
---|
561 | monitor_send_info(buf, 1);
|
---|
562 | }
|
---|
563 |
|
---|
564 | static void monitor_login(char *usr)
|
---|
565 | {
|
---|
566 | char *pwd = NULL;
|
---|
567 | int8_t res = 0;
|
---|
568 | if((usr) && (pwd = strchr(usr, ' ')))
|
---|
569 | { * pwd++ = 0; }
|
---|
570 | if(pwd)
|
---|
571 | { res = monitor_auth_client(trim(usr), trim(pwd)); }
|
---|
572 | else
|
---|
573 | { res = monitor_auth_client(NULL, NULL); }
|
---|
574 |
|
---|
575 | if(res == -1)
|
---|
576 | {
|
---|
577 | cs_disconnect_client(cur_client());
|
---|
578 | return;
|
---|
579 | }
|
---|
580 | monitor_send_login();
|
---|
581 | }
|
---|
582 |
|
---|
583 | static void monitor_logsend(char *flag)
|
---|
584 | {
|
---|
585 | if(!flag) { return; } //no arg
|
---|
586 |
|
---|
587 | struct s_client *cur_cl = cur_client();
|
---|
588 | if(strcmp(flag, "on"))
|
---|
589 | {
|
---|
590 | if(strcmp(flag, "onwohist"))
|
---|
591 | {
|
---|
592 | cur_cl->log = 0;
|
---|
593 | return;
|
---|
594 | }
|
---|
595 | }
|
---|
596 |
|
---|
597 | if(cur_cl->log) // already on
|
---|
598 | { return; }
|
---|
599 |
|
---|
600 | int32_t i, d = 0;
|
---|
601 | if(!strcmp(flag, "on") && cfg.loghistorysize)
|
---|
602 | {
|
---|
603 | char *t_loghistptr = loghistptr, *ptr1 = NULL;
|
---|
604 | if(loghistptr >= loghist + (cfg.loghistorysize) - 1)
|
---|
605 | { t_loghistptr = loghist; }
|
---|
606 | int32_t l1 = strlen(t_loghistptr + 1) + 2;
|
---|
607 | char *lastpos = loghist + (cfg.loghistorysize) - 1;
|
---|
608 |
|
---|
609 | for(ptr1 = t_loghistptr + l1, i = 0; i < 200; i++, ptr1 = ptr1 + l1)
|
---|
610 | {
|
---|
611 | l1 = strlen(ptr1) + 1;
|
---|
612 | if(!d && ((ptr1 >= lastpos) || (l1 < 2)))
|
---|
613 | {
|
---|
614 | ptr1 = loghist;
|
---|
615 | l1 = strlen(ptr1) + 1;
|
---|
616 | d++;
|
---|
617 | }
|
---|
618 |
|
---|
619 | if(d && ((ptr1 >= t_loghistptr) || (l1 < 2)))
|
---|
620 | { break; }
|
---|
621 |
|
---|
622 | char p_usr[32], p_txt[512];
|
---|
623 | size_t pos1 = strcspn(ptr1, "\t") + 1;
|
---|
624 |
|
---|
625 | cs_strncpy(p_usr, ptr1 , pos1 > sizeof(p_usr) ? sizeof(p_usr) : pos1);
|
---|
626 |
|
---|
627 | if((p_usr[0]) && ((cur_cl->monlvl > 1) || (cur_cl->account && !strcmp(p_usr, cur_cl->account->usr))))
|
---|
628 | {
|
---|
629 | snprintf(p_txt, sizeof(p_txt), "[LOG%03d]%s", cur_cl->logcounter, ptr1 + pos1);
|
---|
630 | cur_cl->logcounter = (cur_cl->logcounter + 1) % 1000;
|
---|
631 | monitor_send(p_txt);
|
---|
632 | }
|
---|
633 | }
|
---|
634 | }
|
---|
635 |
|
---|
636 | cur_cl->log = 1;
|
---|
637 | }
|
---|
638 |
|
---|
639 | static void monitor_set_debuglevel(char *flag)
|
---|
640 | {
|
---|
641 | if(flag)
|
---|
642 | {
|
---|
643 | cs_dblevel = atoi(flag);
|
---|
644 | #ifndef WITH_DEBUG
|
---|
645 | cs_log("*** Warning: Debug Support not compiled in ***");
|
---|
646 | #else
|
---|
647 | cs_log("%s debug_level=%d", "all", cs_dblevel);
|
---|
648 | #endif
|
---|
649 | }
|
---|
650 | }
|
---|
651 |
|
---|
652 | static void monitor_get_account(void)
|
---|
653 | {
|
---|
654 | struct s_auth *account;
|
---|
655 | char buf[256];
|
---|
656 | int32_t count = 0;
|
---|
657 |
|
---|
658 | for(account = cfg.account; (account); account = account->next)
|
---|
659 | {
|
---|
660 | count++;
|
---|
661 | snprintf(buf, sizeof(buf), "[U-----]%s\n", account->usr);
|
---|
662 | monitor_send_info(buf, 0);
|
---|
663 | }
|
---|
664 | snprintf(buf, sizeof(buf), "[U-----] %i User registered\n", count);
|
---|
665 | monitor_send_info(buf, 1);
|
---|
666 | return;
|
---|
667 | }
|
---|
668 |
|
---|
669 | static void monitor_set_account(char *args)
|
---|
670 | {
|
---|
671 | struct s_auth *account;
|
---|
672 | char delimiter[] = " =";
|
---|
673 | char *ptr, *saveptr1 = NULL;
|
---|
674 | int32_t argidx, i, found;
|
---|
675 | char *argarray[3];
|
---|
676 | static const char *token[] = {"au", "sleep", "uniq", "monlevel", "group", "services", "betatunnel", "ident", "caid", "chid", "class", "hostname", "expdate", "keepalive", "disabled"};
|
---|
677 | int32_t tokencnt = sizeof(token) / sizeof(char *);
|
---|
678 | char buf[256], tmp[64];
|
---|
679 |
|
---|
680 | argidx = 0;
|
---|
681 | found = 0;
|
---|
682 |
|
---|
683 | snprintf(tmp, sizeof(tmp), "%s", args);
|
---|
684 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s check\n", tmp);
|
---|
685 | monitor_send_info(buf, 0);
|
---|
686 |
|
---|
687 | ptr = strtok_r(args, delimiter, &saveptr1);
|
---|
688 |
|
---|
689 | // resolve arguments
|
---|
690 | while(ptr != NULL)
|
---|
691 | {
|
---|
692 | argarray[argidx] = trim(ptr);
|
---|
693 | ptr = strtok_r(NULL, delimiter, &saveptr1);
|
---|
694 | argidx++;
|
---|
695 | }
|
---|
696 |
|
---|
697 | if(argidx != 3)
|
---|
698 | {
|
---|
699 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s failed - wrong number of parameters (%d)\n", tmp, argidx);
|
---|
700 | monitor_send_info(buf, 0);
|
---|
701 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
|
---|
702 | monitor_send_info(buf, 1);
|
---|
703 | return;
|
---|
704 | }
|
---|
705 |
|
---|
706 | //search account
|
---|
707 | for(account = cfg.account; (account) ; account = account->next)
|
---|
708 | {
|
---|
709 | if(!strcmp(argarray[0], account->usr))
|
---|
710 | {
|
---|
711 | found = 1;
|
---|
712 | break;
|
---|
713 | }
|
---|
714 | }
|
---|
715 |
|
---|
716 | if(found != 1)
|
---|
717 | {
|
---|
718 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s failed - user %s not found\n", tmp , argarray[0]);
|
---|
719 | monitor_send_info(buf, 0);
|
---|
720 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
|
---|
721 | monitor_send_info(buf, 1);
|
---|
722 | return;
|
---|
723 | }
|
---|
724 |
|
---|
725 | found = -1;
|
---|
726 | for(i = 0; i < tokencnt; i++)
|
---|
727 | {
|
---|
728 | if(!strcmp(argarray[1], token[i]))
|
---|
729 | {
|
---|
730 | // preparing the parameters before re-load
|
---|
731 | switch(i)
|
---|
732 | {
|
---|
733 |
|
---|
734 | case 6:
|
---|
735 | clear_tuntab(&account->ttab);
|
---|
736 | break; //betatunnel
|
---|
737 |
|
---|
738 | case 8:
|
---|
739 | clear_caidtab(&account->ctab);
|
---|
740 | break; //Caid
|
---|
741 | }
|
---|
742 | found = i;
|
---|
743 | }
|
---|
744 | }
|
---|
745 |
|
---|
746 | if(found < 0)
|
---|
747 | {
|
---|
748 | snprintf(buf, sizeof(buf), "[S-0000]setuser: parameter %s not exist. possible values:\n", argarray[1]);
|
---|
749 | monitor_send_info(buf, 0);
|
---|
750 | for(i = 0; i < tokencnt; i++)
|
---|
751 | {
|
---|
752 | snprintf(buf, sizeof(buf), "[S-0000]%s\n", token[i]);
|
---|
753 | monitor_send_info(buf, 0);
|
---|
754 | }
|
---|
755 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s end\n", tmp);
|
---|
756 | monitor_send_info(buf, 1);
|
---|
757 | return;
|
---|
758 | }
|
---|
759 | else
|
---|
760 | {
|
---|
761 | chk_account(token[found], argarray[2], account);
|
---|
762 | }
|
---|
763 |
|
---|
764 | if(write_userdb() == 0)
|
---|
765 | { cs_reinit_clients(cfg.account); }
|
---|
766 |
|
---|
767 | snprintf(buf, sizeof(buf), "[S-0000]setuser: %s done - param %s set to %s\n", tmp, argarray[1], argarray[2]);
|
---|
768 | monitor_send_info(buf, 1);
|
---|
769 | }
|
---|
770 |
|
---|
771 | static void monitor_set_server(char *args)
|
---|
772 | {
|
---|
773 | char delimiter[] = "=";
|
---|
774 | char *ptr, *saveptr1;
|
---|
775 | int32_t argidx, i;
|
---|
776 | char *argarray[3];
|
---|
777 | static const char *token[] = {"clienttimeout", "fallbacktimeout", "clientmaxidle", "cachedelay", "bindwait", "netprio", "sleep", "unlockparental", "serialreadertimeout", "maxlogsize", "showecmdw", "waitforcards", "preferlocalcards"};
|
---|
778 | char buf[256];
|
---|
779 |
|
---|
780 | argidx = 0;
|
---|
781 | ptr = strtok_r(args, delimiter, &saveptr1);
|
---|
782 |
|
---|
783 | // resolve arguments
|
---|
784 | while(ptr != NULL)
|
---|
785 | {
|
---|
786 | argarray[argidx] = trim(ptr);
|
---|
787 | ptr = strtok_r(NULL, delimiter, &saveptr1);
|
---|
788 | argidx++;
|
---|
789 | }
|
---|
790 |
|
---|
791 | if(argidx != 2)
|
---|
792 | {
|
---|
793 | snprintf(buf, sizeof(buf), "[S-0000]setserver failed - wrong number of parameters (%d)\n", argidx);
|
---|
794 | monitor_send_info(buf, 1);
|
---|
795 | return;
|
---|
796 | }
|
---|
797 |
|
---|
798 | trim(argarray[0]);
|
---|
799 | trim(argarray[1]);
|
---|
800 | strtolower(argarray[0]);
|
---|
801 |
|
---|
802 | for(i = 0; i < 13; i++)
|
---|
803 | if(!strcmp(argarray[0], token[i])) { break; }
|
---|
804 |
|
---|
805 | if(i < 13)
|
---|
806 | {
|
---|
807 | config_set("global", token[i], argarray[1]);
|
---|
808 | snprintf(buf, sizeof(buf), "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
|
---|
809 | monitor_send_info(buf, 1);
|
---|
810 | }
|
---|
811 | else
|
---|
812 | {
|
---|
813 | snprintf(buf, sizeof(buf), "[S-0000]setserver failed - parameter %s not exist\n", argarray[0]);
|
---|
814 | monitor_send_info(buf, 1);
|
---|
815 | return;
|
---|
816 | }
|
---|
817 |
|
---|
818 | /*Hide by blueven. Introduce new fallbacktimeout_percaid.
|
---|
819 | *
|
---|
820 | * if (cfg.ftimeout>=cfg.ctimeout) {
|
---|
821 | cfg.ftimeout = cfg.ctimeout - 100;
|
---|
822 | snprintf(buf, sizeof(buf), "[S-0000]setserver WARNING: fallbacktimeout adjusted to %u ms\n", cfg.ftimeout);
|
---|
823 | monitor_send_info(buf, 1);
|
---|
824 | }*/
|
---|
825 | //kill(first_client->pid, SIGUSR1);
|
---|
826 | }
|
---|
827 |
|
---|
828 | #ifdef WEBIF
|
---|
829 | static void monitor_restart_server(void)
|
---|
830 | {
|
---|
831 | cs_restart_oscam();
|
---|
832 | }
|
---|
833 | #endif
|
---|
834 |
|
---|
835 | static void monitor_list_commands(const char *args[], int32_t cmdcnt)
|
---|
836 | {
|
---|
837 | int32_t i;
|
---|
838 | for(i = 0; i < cmdcnt; i++)
|
---|
839 | {
|
---|
840 | char buf[64];
|
---|
841 | snprintf(buf, sizeof(buf), "[S-0000]commands: %s\n", args[i]);
|
---|
842 | if(i < cmdcnt - 1)
|
---|
843 | { monitor_send_info(buf, 0); }
|
---|
844 | else
|
---|
845 | { monitor_send_info(buf, 1); }
|
---|
846 | }
|
---|
847 | }
|
---|
848 |
|
---|
849 | static int32_t monitor_process_request(char *req)
|
---|
850 | {
|
---|
851 | int32_t i, rc;
|
---|
852 | static const char *cmd[] = {"login",
|
---|
853 | "exit",
|
---|
854 | "log",
|
---|
855 | "status",
|
---|
856 | "shutdown",
|
---|
857 | "reload",
|
---|
858 | "details",
|
---|
859 | "version",
|
---|
860 | "debug",
|
---|
861 | "getuser",
|
---|
862 | "setuser",
|
---|
863 | "setserver",
|
---|
864 | "commands",
|
---|
865 | "keepalive",
|
---|
866 | "reread"
|
---|
867 | #ifdef WEBIF
|
---|
868 | , "restart"
|
---|
869 | #endif
|
---|
870 | };
|
---|
871 |
|
---|
872 | int32_t cmdcnt = sizeof(cmd) / sizeof(char *); // Calculate the amount of items in array
|
---|
873 | char *arg;
|
---|
874 | struct s_client *cur_cl = cur_client();
|
---|
875 | struct monitor_data *module_data = cur_cl->module_data;
|
---|
876 |
|
---|
877 | if((arg = strchr(req, ' ')))
|
---|
878 | {
|
---|
879 | *arg++ = 0;
|
---|
880 | trim(arg);
|
---|
881 | }
|
---|
882 | //trim(req);
|
---|
883 |
|
---|
884 | if(!module_data->auth && strcmp(req, cmd[0]) != 0)
|
---|
885 | { monitor_login(NULL); }
|
---|
886 |
|
---|
887 | for(rc = 1, i = 0; i < cmdcnt; i++)
|
---|
888 | if(!strcmp(req, cmd[i]))
|
---|
889 | {
|
---|
890 | switch(i)
|
---|
891 | {
|
---|
892 | case 0:
|
---|
893 | monitor_login(arg);
|
---|
894 | break; // login
|
---|
895 | case 1:
|
---|
896 | cs_disconnect_client(cur_cl);
|
---|
897 | break; // exit
|
---|
898 | case 2:
|
---|
899 | monitor_logsend(arg);
|
---|
900 | break; // log
|
---|
901 | case 3:
|
---|
902 | monitor_process_info();
|
---|
903 | break; // status
|
---|
904 | case 4:
|
---|
905 | if(cur_cl->monlvl > 3) { cs_exit_oscam(); }
|
---|
906 | break; // shutdown
|
---|
907 | case 5:
|
---|
908 | if(cur_cl->monlvl > 2) { cs_accounts_chk(); }
|
---|
909 | break; // reload
|
---|
910 | case 6:
|
---|
911 | monitor_process_details(arg);
|
---|
912 | break; // details
|
---|
913 | case 7:
|
---|
914 | monitor_send_details_version();
|
---|
915 | break; // version
|
---|
916 | case 8:
|
---|
917 | if(cur_cl->monlvl > 3) { monitor_set_debuglevel(arg); }
|
---|
918 | break; // debuglevel
|
---|
919 | case 9:
|
---|
920 | if(cur_cl->monlvl > 3) { monitor_get_account(); }
|
---|
921 | break; // getuser
|
---|
922 | case 10:
|
---|
923 | if(cur_cl->monlvl > 3) { monitor_set_account(arg); }
|
---|
924 | break; // setuser
|
---|
925 | case 11:
|
---|
926 | if(cur_cl->monlvl > 3) { monitor_set_server(arg); }
|
---|
927 | break; // setserver
|
---|
928 | case 12:
|
---|
929 | if(cur_cl->monlvl > 3) { monitor_list_commands(cmd, cmdcnt); }
|
---|
930 | break; // list commands
|
---|
931 | case 13:
|
---|
932 | if(cur_cl->monlvl > 3) { monitor_send_keepalive_ack(); }
|
---|
933 | break; // keepalive
|
---|
934 | case 14:
|
---|
935 | {
|
---|
936 | char buf[64]; // reread
|
---|
937 | snprintf(buf, sizeof(buf), "[S-0000]reread\n");
|
---|
938 | monitor_send_info(buf, 1);
|
---|
939 | cs_card_info();
|
---|
940 | break;
|
---|
941 | }
|
---|
942 | #ifdef WEBIF
|
---|
943 | case 15:
|
---|
944 | if(cur_cl->monlvl > 3) { monitor_restart_server(); }
|
---|
945 | break; // keepalive
|
---|
946 | #endif
|
---|
947 | default:
|
---|
948 | continue;
|
---|
949 | }
|
---|
950 | break;
|
---|
951 | }
|
---|
952 | return rc;
|
---|
953 | }
|
---|
954 |
|
---|
955 | static void *monitor_server(struct s_client *client, uchar *mbuf, int32_t UNUSED(n))
|
---|
956 | {
|
---|
957 | client->typ = 'm';
|
---|
958 | monitor_process_request((char *)mbuf);
|
---|
959 |
|
---|
960 | return NULL;
|
---|
961 | }
|
---|
962 |
|
---|
963 | static void monitor_cleanup(struct s_client *client)
|
---|
964 | {
|
---|
965 | NULLFREE(client->module_data);
|
---|
966 | }
|
---|
967 |
|
---|
968 | void module_monitor(struct s_module *ph)
|
---|
969 | {
|
---|
970 | ph->ptab.nports = 1;
|
---|
971 | ph->ptab.ports[0].s_port = cfg.mon_port;
|
---|
972 | ph->desc = "monitor";
|
---|
973 | ph->type = MOD_CONN_UDP;
|
---|
974 | IP_ASSIGN(ph->s_ip, cfg.mon_srvip);
|
---|
975 | ph->s_handler = monitor_server;
|
---|
976 | ph->recv = monitor_recv;
|
---|
977 | ph->cleanup = monitor_cleanup;
|
---|
978 | // ph->send_dcw=NULL;
|
---|
979 | }
|
---|
980 | #endif
|
---|