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