forked from gfto/oscam
/
oscam.c
1912 lines (1696 loc) · 49.8 KB
/
oscam.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
1000
#define MODULE_LOG_PREFIX "main"
#include "globals.h"
#include <getopt.h>
#include "csctapi/cardreaders.h"
#include "modules.h"
#include "readers.h"
#include "extapi/coolapi.h"
#include "module-anticasc.h"
#include "module-cacheex.h"
#include "module-cccam.h"
#include "module-dvbapi.h"
#include "module-dvbapi-azbox.h"
#include "module-dvbapi-mca.h"
#include "module-dvbapi-chancache.h"
#include "module-gbox-sms.h"
#include "module-ird-guess.h"
#include "module-lcd.h"
#include "module-led.h"
#include "module-stat.h"
#include "module-webif.h"
#include "module-webif-tpl.h"
#include "module-cw-cycle-check.h"
#include "oscam-chk.h"
#include "oscam-cache.h"
#include "oscam-client.h"
#include "oscam-config.h"
#include "oscam-ecm.h"
#include "oscam-emm.h"
#include "oscam-emm-cache.h"
#include "oscam-files.h"
#include "oscam-garbage.h"
#include "oscam-lock.h"
#include "oscam-net.h"
#include "oscam-reader.h"
#include "oscam-string.h"
#include "oscam-time.h"
#include "oscam-work.h"
#include "reader-common.h"
#include "module-gbox.h"
#ifdef WITH_SSL
#include <openssl/crypto.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
static void ssl_init(void)
{
SSL_load_error_strings();
ERR_load_BIO_strings();
ERR_load_SSL_strings();
SSL_library_init();
}
static void ssl_done(void)
{
ERR_remove_state(0);
ERR_free_strings();
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
}
#else
static void ssl_init(void) { }
static void ssl_done(void) { }
#endif
extern char *config_mak;
/*****************************************************************************
Globals
*****************************************************************************/
const char *syslog_ident = "oscam";
static char *oscam_pidfile;
static char default_pidfile[64];
int32_t exit_oscam = 0;
static struct s_module modules[CS_MAX_MOD];
struct s_client *first_client = NULL; //Pointer to clients list, first client is master
struct s_reader *first_active_reader = NULL; //list of active readers (enable=1 deleted = 0)
LLIST *configured_readers = NULL; //list of all (configured) readers
uint16_t len4caid[256]; // table for guessing caid (by len)
char cs_confdir[128] = CS_CONFDIR;
uint16_t cs_dblevel = 0; // Debug Level
int32_t thread_pipe[2] = {0, 0};
static int8_t cs_restart_mode = 1; //Restartmode: 0=off, no restart fork, 1=(default)restart fork, restart by webif, 2=like=1, but also restart on segfaults
#ifdef WITH_UTF8
uint8_t cs_http_use_utf8 = 1;
#else
uint8_t cs_http_use_utf8 = 0;
#endif
static int8_t cs_capture_SEGV;
static int8_t cs_dump_stack;
static uint16_t cs_waittime = 60;
char cs_tmpdir[200] = {0x00};
CS_MUTEX_LOCK system_lock;
CS_MUTEX_LOCK config_lock;
CS_MUTEX_LOCK gethostbyname_lock;
CS_MUTEX_LOCK clientlist_lock;
CS_MUTEX_LOCK readerlist_lock;
CS_MUTEX_LOCK fakeuser_lock;
CS_MUTEX_LOCK readdir_lock;
CS_MUTEX_LOCK cwcycle_lock;
pthread_key_t getclient;
static int32_t bg;
static int32_t gbdb;
static int32_t max_pending = 32;
//ecms list
CS_MUTEX_LOCK ecmcache_lock;
struct ecm_request_t *ecmcwcache = NULL;
uint32_t ecmcwcache_size = 0;
//pushout deleted list
CS_MUTEX_LOCK ecm_pushed_deleted_lock;
struct ecm_request_t *ecm_pushed_deleted = NULL;
struct s_config cfg;
int log_remove_sensitive = 1;
static char *prog_name;
static char *stb_boxtype;
static char *stb_boxname;
static uint32_t oscam_stacksize = 0;
/*****************************************************************************
Statics
*****************************************************************************/
/* Prints usage information and information about the built-in modules. */
static void show_usage(void)
{
printf("%s",
" ___ ____ ___\n"
" / _ \\/ ___| / __|__ _ _ __ ___\n"
"| | | \\___ \\| | / _` | '_ ` _ \\\n"
"| |_| |___) | |_| (_| | | | | | |\n"
" \\___/|____/ \\___\\__,_|_| |_| |_|\n\n");
printf("OSCam cardserver v%s, build r%s (%s)\n", CS_VERSION, CS_SVN_VERSION, CS_TARGET);
printf("Copyright (C) 2009-2015 OSCam developers.\n");
printf("This program is distributed under GPLv3.\n");
printf("OSCam is based on Streamboard mp-cardserver v0.9d written by dukat\n");
printf("Visit http://www.streamboard.tv/oscam/ for more details.\n\n");
printf(" ConfigDir : %s\n", CS_CONFDIR);
printf("\n");
printf(" Usage: oscam [parameters]\n");
printf("\n Directories:\n");
printf(" -c, --config-dir <dir> | Read configuration files from <dir>.\n");
printf(" . Default: %s\n", CS_CONFDIR);
printf(" -t, --temp-dir <dir> | Set temporary directory to <dir>.\n");
#if defined(__CYGWIN__)
printf(" . Default: (OS-TMP)\n");
#else
printf(" . Default: /tmp/.oscam\n");
#endif
printf("\n Startup:\n");
#if defined(WITH_STAPI) || defined(WITH_STAPI5)
printf(" -f, --foreground | Start in the foreground mode.\n");
#else
printf(" -b, --daemon | Start in the background as daemon.\n");
#endif
printf(" -B, --pidfile <pidfile> | Create pidfile when starting.\n");
if(config_enabled(WEBIF))
{
printf(" -r, --restart <level> | Set restart level:\n");
printf(" . 0 - Restart disabled (exit on restart request).\n");
printf(" . 1 - WebIf restart is active (default).\n");
printf(" . 2 - Like 1, but also restart on segfaults.\n");
}
printf(" -w, --wait <secs> | Set how much seconds to wait at startup for the\n");
printf(" . system clock to be set correctly. Default: 60\n");
printf("\n Logging:\n");
printf(" -I, --syslog-ident <ident> | Set syslog ident. Default: oscam\n");
printf(" -S, --show-sensitive | Do not filter sensitive info (card serials, boxids)\n");
printf(" . from the logs.\n");
printf(" -d, --debug <level> | Set debug level mask used for logging:\n");
printf(" . 0 - No extra debugging (default).\n");
printf(" . 1 - Detailed error messages.\n");
printf(" . 2 - ATR parsing info, ECM, EMM and CW dumps.\n");
printf(" . 4 - Traffic from/to the reader.\n");
printf(" . 8 - Traffic from/to the clients.\n");
printf(" . 16 - Traffic to the reader-device on IFD layer.\n");
printf(" . 32 - Traffic to the reader-device on I/O layer.\n");
printf(" . 64 - EMM logging.\n");
printf(" . 128 - DVBAPI logging.\n");
printf(" . 256 - Loadbalancer logging.\n");
printf(" . 512 - CACHEEX logging.\n");
printf(" . 1024 - Client ECM logging.\n");
printf(" . 2048 - CSP logging.\n");
printf(" . 4096 - CWC logging.\n");
printf(" . 65535 - Debug all.\n");
printf("\n Settings:\n");
printf(" -p, --pending-ecm <num> | Set the maximum number of pending ECM packets.\n");
printf(" . Default: 32 Max: 4096\n");
if(config_enabled(WEBIF))
{
printf(" -u, --utf8 | Enable WebIf support for UTF-8 charset.\n");
}
printf("\n Debug parameters:\n");
printf(" -a, --crash-dump | Write oscam.crash file on segfault. This option\n");
printf(" . needs GDB to be installed and OSCam executable to\n");
printf(" . contain the debug information (run oscam-XXXX.debug)\n");
printf(" -s, --capture-segfaults | Capture segmentation faults.\n");
printf(" -g, --gcollect <mode> | Garbage collector debug mode:\n");
printf(" . 1 - Immediate free.\n");
printf(" . 2 - Check for double frees.\n");
printf("\n Information:\n");
printf(" -h, --help | Show command line help text.\n");
printf(" -V, --build-info | Show OSCam binary configuration and version.\n");
}
/* Keep the options sorted */
#if defined(WITH_STAPI) || defined(WITH_STAPI5)
static const char short_options[] = "aB:fc:d:g:hI:p:r:Sst:uVw:";
#else
static const char short_options[] = "aB:bc:d:g:hI:p:r:Sst:uVw:";
#endif
/* Keep the options sorted by short option */
static const struct option long_options[] =
{
{ "crash-dump", no_argument, NULL, 'a' },
{ "pidfile", required_argument, NULL, 'B' },
#if defined(WITH_STAPI) || defined(WITH_STAPI5)
{ "foreground", no_argument, NULL, 'f' },
#else
{ "daemon", no_argument, NULL, 'b' },
#endif
{ "config-dir", required_argument, NULL, 'c' },
{ "debug", required_argument, NULL, 'd' },
{ "gcollect", required_argument, NULL, 'g' },
{ "help", no_argument, NULL, 'h' },
{ "syslog-ident", required_argument, NULL, 'I' },
{ "pending-ecm", required_argument, NULL, 'p' },
{ "restart", required_argument, NULL, 'r' },
{ "show-sensitive", no_argument, NULL, 'S' },
{ "capture-segfaults", no_argument, NULL, 's' },
{ "temp-dir", required_argument, NULL, 't' },
{ "utf8", no_argument, NULL, 'u' },
{ "build-info", no_argument, NULL, 'V' },
{ "wait", required_argument, NULL, 'w' },
{ 0, 0, 0, 0 }
};
static void write_versionfile(bool use_stdout);
static void parse_cmdline_params(int argc, char **argv)
{
#if defined(WITH_STAPI) || defined(WITH_STAPI5)
bg = 1;
#endif
int i;
while((i = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF)
{
if(i == '?')
{ fprintf(stderr, "ERROR: Unknown command line parameter: %s\n", argv[optind - 1]); }
switch(i)
{
case 'a': // --crash-dump
cs_dump_stack = 1;
break;
case 'B': // --pidfile
oscam_pidfile = optarg;
break;
#if defined(WITH_STAPI) || defined(WITH_STAPI5)
case 'f': // --foreground
bg = 0;
break;
#else
case 'b': // --daemon
bg = 1;
break;
#endif
case 'c': // --config-dir
cs_strncpy(cs_confdir, optarg, sizeof(cs_confdir));
break;
case 'd': // --debug
cs_dblevel = atoi(optarg);
break;
case 'g': // --gcollect
gbdb = atoi(optarg);
break;
case 'h': // --help
show_usage();
exit(EXIT_SUCCESS);
break;
case 'I': // --syslog-ident
syslog_ident = optarg;
break;
case 'p': // --pending-ecm
max_pending = atoi(optarg) <= 0 ? 32 : MIN(atoi(optarg), 4096);
break;
case 'r': // --restart
if(config_enabled(WEBIF))
{
cs_restart_mode = atoi(optarg);
}
break;
case 'S': // --show-sensitive
log_remove_sensitive = !log_remove_sensitive;
break;
case 's': // --capture-segfaults
cs_capture_SEGV = 1;
break;
case 't': // --temp-dir
{
mkdir(optarg, S_IRWXU);
int j = open(optarg, O_RDONLY);
if(j >= 0)
{
close(j);
cs_strncpy(cs_tmpdir, optarg, sizeof(cs_tmpdir));
}
else
{
printf("WARNING: Temp dir does not exist. Using default value.\n");
}
break;
}
case 'u': // --utf8
if(config_enabled(WEBIF))
{
cs_http_use_utf8 = 1;
printf("WARNING: Web interface UTF-8 mode enabled. Carefully read documentation as bugs may arise.\n");
}
break;
case 'V': // --build-info
write_versionfile(true);
exit(EXIT_SUCCESS);
break;
case 'w': // --wait
cs_waittime = strtoul(optarg, NULL, 10);
break;
}
}
}
#define write_conf(CONFIG_VAR, text) \
fprintf(fp, "%-40s %s\n", text ":", config_enabled(CONFIG_VAR) ? "yes" : "no")
#define write_readerconf(CONFIG_VAR, text) \
fprintf(fp, "%-40s %s\n", text ":", config_enabled(CONFIG_VAR) ? "yes" : "no - no EMM support!")
#define write_cardreaderconf(CONFIG_VAR, text) \
fprintf(fp, "%s%-29s %s\n", "cardreader_", text ":", config_enabled(CONFIG_VAR) ? "yes" : "no")
static void write_versionfile(bool use_stdout)
{
FILE *fp = stdout;
if(!use_stdout)
{
char targetfile[256];
fp = fopen(get_tmp_dir_filename(targetfile, sizeof(targetfile), "oscam.version"), "w");
if(!fp)
{
cs_log("Cannot open %s (errno=%d %s)", targetfile, errno, strerror(errno));
return;
}
struct tm st;
time_t walltime = cs_time();
localtime_r(&walltime, &st);
fprintf(fp, "Unix starttime: %ld\n", walltime);
fprintf(fp, "Starttime: %02d.%02d.%04d %02d:%02d:%02d\n",
st.tm_mday, st.tm_mon + 1, st.tm_year + 1900,
st.tm_hour, st.tm_min, st.tm_sec);
}
fprintf(fp, "Version: oscam-%s-r%s\n", CS_VERSION, CS_SVN_VERSION);
fprintf(fp, "Compiler: %s\n", CS_TARGET);
fprintf(fp, "Box type: %s (%s)\n", boxtype_get(), boxname_get());
fprintf(fp, "TempDir: %s\n", cs_tmpdir);
fprintf(fp, "ConfigDir: %s\n", cs_confdir);
#ifdef WEBIF
fprintf(fp, "WebifPort: %d\n", cfg.http_port);
#endif
fprintf(fp, "\n");
write_conf(WEBIF, "Web interface support");
write_conf(WEBIF_LIVELOG, "LiveLog support");
write_conf(WEBIF_JQUERY, "jQuery support intern");
write_conf(TOUCH, "Touch interface support");
write_conf(WITH_SSL, "SSL support");
write_conf(HAVE_DVBAPI, "DVB API support");
if(config_enabled(HAVE_DVBAPI))
{
write_conf(WITH_AZBOX, "DVB API with AZBOX support");
write_conf(WITH_MCA, "DVB API with MCA support");
write_conf(WITH_COOLAPI, "DVB API with COOLAPI support");
write_conf(WITH_STAPI, "DVB API with STAPI support");
write_conf(WITH_STAPI5, "DVB API with STAPI5 support");
write_conf(READ_SDT_CHARSETS, "DVB API read-sdt charsets");
}
write_conf(IRDETO_GUESSING, "Irdeto guessing");
write_conf(CS_ANTICASC, "Anti-cascading support");
write_conf(WITH_DEBUG, "Debug mode");
write_conf(MODULE_MONITOR, "Monitor");
write_conf(WITH_LB, "Loadbalancing support");
write_conf(CS_CACHEEX, "Cache exchange support");
write_conf(CW_CYCLE_CHECK, "CW Cycle Check support");
write_conf(LCDSUPPORT, "LCD support");
write_conf(LEDSUPPORT, "LED support");
switch (cs_getclocktype()) {
case CLOCK_TYPE_UNKNOWN : write_conf(CLOCKFIX, "Clockfix with UNKNOWN clock"); break;
case CLOCK_TYPE_REALTIME : write_conf(CLOCKFIX, "Clockfix with realtime clock"); break;
case CLOCK_TYPE_MONOTONIC: write_conf(CLOCKFIX, "Clockfix with monotonic clock"); break;
}
write_conf(IPV6SUPPORT, "IPv6 support");
fprintf(fp, "\n");
write_conf(MODULE_CAMD33, "camd 3.3x");
write_conf(MODULE_CAMD35, "camd 3.5 UDP");
write_conf(MODULE_CAMD35_TCP, "camd 3.5 TCP");
write_conf(MODULE_NEWCAMD, "newcamd");
write_conf(MODULE_CCCAM, "CCcam");
write_conf(MODULE_CCCSHARE, "CCcam share");
write_conf(MODULE_GBOX, "gbox");
write_conf(MODULE_RADEGAST, "radegast");
write_conf(MODULE_SCAM, "scam");
write_conf(MODULE_SERIAL, "serial");
write_conf(MODULE_CONSTCW, "constant CW");
write_conf(MODULE_PANDORA, "Pandora");
write_conf(MODULE_GHTTP, "ghttp");
fprintf(fp, "\n");
write_conf(WITH_CARDREADER, "Reader support");
if(config_enabled(WITH_CARDREADER))
{
fprintf(fp, "\n");
write_readerconf(READER_NAGRA, "Nagra");
write_readerconf(READER_IRDETO, "Irdeto");
write_readerconf(READER_CONAX, "Conax");
write_readerconf(READER_CRYPTOWORKS, "Cryptoworks");
write_readerconf(READER_SECA, "Seca");
write_readerconf(READER_VIACCESS, "Viaccess");
write_readerconf(READER_VIDEOGUARD, "NDS Videoguard");
write_readerconf(READER_DRE, "DRE Crypt");
write_readerconf(READER_TONGFANG, "TONGFANG");
write_readerconf(READER_BULCRYPT, "Bulcrypt");
write_readerconf(READER_GRIFFIN, "Griffin");
write_readerconf(READER_DGCRYPT, "DGCrypt");
fprintf(fp, "\n");
write_cardreaderconf(CARDREADER_PHOENIX, "phoenix");
write_cardreaderconf(CARDREADER_INTERNAL_AZBOX, "internal_azbox");
write_cardreaderconf(CARDREADER_INTERNAL_COOLAPI, "internal_coolapi");
write_cardreaderconf(CARDREADER_INTERNAL_SCI, "internal_sci");
write_cardreaderconf(CARDREADER_SC8IN1, "sc8in1");
write_cardreaderconf(CARDREADER_MP35, "mp35");
write_cardreaderconf(CARDREADER_SMARGO, "smargo");
write_cardreaderconf(CARDREADER_PCSC, "pcsc");
write_cardreaderconf(CARDREADER_SMART, "smartreader");
write_cardreaderconf(CARDREADER_DB2COM, "db2com");
write_cardreaderconf(CARDREADER_STAPI, "stapi");
write_cardreaderconf(CARDREADER_STAPI5, "stapi5");
write_cardreaderconf(CARDREADER_STINGER, "stinger");
}
else
{
write_readerconf(WITH_CARDREADER, "Reader Support");
}
if(!use_stdout)
{ fclose(fp); }
}
#undef write_conf
#undef write_readerconf
#undef write_cardreaderconf
static void remove_versionfile(void)
{
char targetfile[256];
unlink(get_tmp_dir_filename(targetfile, sizeof(targetfile), "oscam.version"));
}
#define report_emm_support(CONFIG_VAR, text) \
do { \
if (!config_enabled(CONFIG_VAR)) \
cs_log("Binary without %s module - no EMM processing for %s possible!", text, text); \
} while(0)
static void do_report_emm_support(void)
{
if(!config_enabled(WITH_CARDREADER))
{
cs_log("Binary without Cardreader Support! No EMM processing possible!");
}
else
{
report_emm_support(READER_NAGRA, "Nagra");
report_emm_support(READER_IRDETO, "Irdeto");
report_emm_support(READER_CONAX, "Conax");
report_emm_support(READER_CRYPTOWORKS, "Cryptoworks");
report_emm_support(READER_SECA, "Seca");
report_emm_support(READER_VIACCESS, "Viaccess");
report_emm_support(READER_VIDEOGUARD, "NDS Videoguard");
report_emm_support(READER_DRE, "DRE Crypt");
report_emm_support(READER_TONGFANG, "TONGFANG");
report_emm_support(READER_BULCRYPT, "Bulcrypt");
report_emm_support(READER_GRIFFIN, "Griffin");
report_emm_support(READER_DGCRYPT, "DGCrypt");
}
}
#undef report_emm_support
#ifdef NEED_DAEMON
// The compat function is not called daemon() because this may cause problems.
static int32_t do_daemon(int32_t nochdir, int32_t noclose)
{
int32_t fd;
switch(fork())
{
case -1:
return (-1);
case 0:
break;
default:
_exit(0);
}
if(setsid() == (-1))
{ return (-1); }
if(!nochdir)
{ (void)chdir("/"); }
if(!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1)
{
(void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO);
(void)dup2(fd, STDERR_FILENO);
if(fd > 2)
{ (void)close(fd); }
}
return (0);
}
#else
#define do_daemon daemon
#endif
/*
* flags: 1 = restart, 2 = don't modify if SIG_IGN, may be combined
*/
static void set_signal_handler(int32_t sig, int32_t flags, void (*sighandler))
{
struct sigaction sa;
sigaction(sig, (struct sigaction *) 0, &sa);
if(!((flags & 2) && (sa.sa_handler == SIG_IGN)))
{
sigemptyset(&sa.sa_mask);
sa.sa_flags = (flags & 1) ? SA_RESTART : 0;
sa.sa_handler = sighandler;
sigaction(sig, &sa, (struct sigaction *) 0);
}
}
static void cs_master_alarm(void)
{
cs_log("PANIC: master deadlock!");
fprintf(stderr, "PANIC: master deadlock!");
fflush(stderr);
}
static void cs_sigpipe(void)
{
if(cs_dblevel & D_ALL_DUMP)
{ cs_log("Got sigpipe signal -> captured"); }
}
static void cs_dummy(void)
{
return;
}
/* Switch debuglevel forward one step (called when receiving SIGUSR1). */
static void cs_debug_level(void)
{
switch(cs_dblevel)
{
case 0:
cs_dblevel = 1;
break;
case 128:
cs_dblevel = 255;
break;
case 255:
cs_dblevel = 0;
break;
default:
cs_dblevel <<= 1;
}
cs_log("debug_level=%d", cs_dblevel);
}
/**
* write stacktrace to oscam.crash. file is always appended
* Usage:
* 1. compile oscam with debug parameters (Makefile: DS_OPTS="-ggdb")
* 2. you need gdb installed and working on the local machine
* 3. start oscam with parameter: -a
*/
static void cs_dumpstack(int32_t sig)
{
FILE *fp = fopen("oscam.crash", "a+");
time_t timep;
char buf[200];
time(&timep);
cs_ctime_r(&timep, buf);
fprintf(stderr, "crashed with signal %d on %swriting oscam.crash\n", sig, buf);
fprintf(fp, "%sOSCam cardserver v%s, build r%s (%s)\n", buf, CS_VERSION, CS_SVN_VERSION, CS_TARGET);
fprintf(fp, "FATAL: Signal %d: %s Fault. Logged StackTrace:\n\n", sig, (sig == SIGSEGV) ? "Segmentation" : ((sig == SIGBUS) ? "Bus" : "Unknown"));
fclose(fp);
FILE *cmd = fopen("/tmp/gdbcmd", "w");
fputs("bt\n", cmd);
fputs("thread apply all bt\n", cmd);
fclose(cmd);
snprintf(buf, sizeof(buf) - 1, "gdb %s %d -batch -x /tmp/gdbcmd >> oscam.crash", prog_name, getpid());
if(system(buf) == -1)
{ fprintf(stderr, "Fatal error on trying to start gdb process."); }
exit(-1);
}
/**
* called by signal SIGHUP
*
* reloads configs:
* - useraccounts (oscam.user)
* - readers (oscam.server)
* - services ids (oscam.srvid)
* - tier ids (oscam.tiers)
* Also clears anticascading stats.
**/
static void cs_reload_config(void)
{
static pthread_mutex_t mutex;
static int8_t mutex_init = 0;
if(!mutex_init)
{
SAFE_MUTEX_INIT(&mutex, NULL);
mutex_init = 1;
}
if(pthread_mutex_trylock(&mutex))
{
return;
}
cs_accounts_chk();
reload_readerdb();
init_provid();
init_srvid();
init_tierid();
init_fakecws();
ac_init_stat();
cs_reopen_log(); // FIXME: aclog.log, emm logs, cw logs (?)
SAFE_MUTEX_UNLOCK(&mutex);
}
/* Sets signal handlers to ignore for early startup of OSCam because for example log
could cause SIGPIPE errors and the normal signal handlers can't be used at this point. */
static void init_signal_pre(void)
{
set_signal_handler(SIGPIPE , 1, SIG_IGN);
set_signal_handler(SIGWINCH, 1, SIG_IGN);
set_signal_handler(SIGALRM , 1, SIG_IGN);
set_signal_handler(SIGHUP , 1, SIG_IGN);
}
/* Sets the signal handlers.*/
static void init_signal(void)
{
set_signal_handler(SIGINT, 3, cs_exit);
#if defined(__APPLE__)
set_signal_handler(SIGEMT, 3, cs_exit);
#endif
set_signal_handler(SIGTERM, 3, cs_exit);
set_signal_handler(SIGWINCH, 1, SIG_IGN);
set_signal_handler(SIGPIPE , 0, cs_sigpipe);
set_signal_handler(SIGALRM , 0, cs_master_alarm);
set_signal_handler(SIGHUP , 1, cs_reload_config);
set_signal_handler(SIGUSR1, 1, cs_debug_level);
set_signal_handler(SIGUSR2, 1, cs_card_info);
set_signal_handler(OSCAM_SIGNAL_WAKEUP, 0, cs_dummy);
if(cs_capture_SEGV)
{
set_signal_handler(SIGSEGV, 1, cs_exit);
set_signal_handler(SIGBUS, 1, cs_exit);
}
else if(cs_dump_stack)
{
set_signal_handler(SIGSEGV, 1, cs_dumpstack);
set_signal_handler(SIGBUS, 1, cs_dumpstack);
}
cs_log("signal handling initialized");
return;
}
void cs_exit(int32_t sig)
{
if(cs_dump_stack && (sig == SIGSEGV || sig == SIGBUS || sig == SIGQUIT))
{ cs_dumpstack(sig); }
set_signal_handler(SIGHUP , 1, SIG_IGN);
set_signal_handler(SIGPIPE, 1, SIG_IGN);
struct s_client *cl = cur_client();
if(!cl)
{ return; }
// this is very important - do not remove
if(cl->typ != 's')
{
cs_log_dbg(D_TRACE, "thread %8lX ended!", (unsigned long)pthread_self());
free_client(cl);
//Restore signals before exiting thread
set_signal_handler(SIGPIPE , 0, cs_sigpipe);
set_signal_handler(SIGHUP , 1, cs_reload_config);
pthread_exit(NULL);
return;
}
if(!exit_oscam)
{ exit_oscam = sig ? sig : 1; }
}
static char *read_line_from_file(char *fname, char *buf, int bufsz)
{
memset(buf, 0, bufsz);
FILE *f = fopen(fname, "r");
if (!f)
return NULL;
if (fgets(buf, bufsz, f)) { // only the first line is needed
char *p = strchr(buf, '\n');
*p = '\0';
}
fclose(f);
if (buf[0])
return buf;
return NULL;
}
static void init_machine_info(void)
{
struct utsname buffer;
if (uname(&buffer) == 0)
{
cs_log("System name = %s", buffer.sysname);
cs_log("Host name = %s", buffer.nodename);
cs_log("Release = %s", buffer.release);
cs_log("Version = %s", buffer.version);
cs_log("Machine = %s", buffer.machine);
} else {
cs_log("ERROR: uname call failed: %s", strerror(errno));
}
#if !defined(__linux__)
return;
#endif
// Linux only functionality
char boxtype[128];
boxtype[0] = 0;
char model[128];
model[0] = 0;
char vumodel[128];
vumodel[0] = 0;
int8_t azmodel = 0;
FILE *f;
if ((f = fopen("/proc/stb/info/azmodel", "r"))){ azmodel = 1; fclose(f);}
read_line_from_file("/proc/stb/info/model", model, sizeof(model));
read_line_from_file("/proc/stb/info/boxtype", boxtype, sizeof(boxtype));
read_line_from_file("/proc/stb/info/vumodel", vumodel, sizeof(vumodel));
if (vumodel[0] && !boxtype[0] && !azmodel)
snprintf(boxtype, sizeof(boxtype), "vu%s", vumodel);
if (!boxtype[0] && azmodel)
snprintf(boxtype, sizeof(boxtype), "Azbox-%s", model);
// Detect dreambox type
if (strcasecmp(buffer.machine, "ppc") == 0 && !model[0] && !boxtype[0])
{
char line[128], *p;
int have_dreambox = 0;
if ((f = fopen("/proc/cpuinfo", "r")))
{
while (fgets(line, sizeof(line), f))
{
if (strstr(line, "STBx25xx")) have_dreambox++;
if (strstr(line, "pvr" )) have_dreambox++;
if (strstr(line, "Dreambox")) have_dreambox++;
if (strstr(line, "9.80" )) have_dreambox++;
if (strstr(line, "63MHz" )) have_dreambox++;
}
fclose(f);
have_dreambox = have_dreambox == 5 ? 1 : 0; // Need to find all 5 strings
}
if (have_dreambox)
{
if (read_line_from_file("/proc/meminfo", line, sizeof(line)) && (p = strchr(line, ' ')))
{
unsigned long memtotal = strtoul(p, NULL, 10);
if (memtotal > 40000)
snprintf(boxtype, sizeof(boxtype), "%s", "dm600pvr");
else
snprintf(boxtype, sizeof(boxtype), "%s", "dm500");
}
}
}
if (!boxtype[0] && !strcasecmp(model, "dm800") && !strcasecmp(buffer.machine, "armv7l"))
snprintf(boxtype, sizeof(boxtype), "%s", "su980");
if (!boxtype[0])
{
uchar *pos;
pos = (uchar*) memchr(buffer.release, 'd', sizeof(buffer.release));
if(pos)
{
if((!memcmp(pos, "dbox2", sizeof("dbox2"))) && !strcasecmp(buffer.machine, "ppc"))
{
snprintf(boxtype, sizeof(boxtype), "%s", "dbox2");
}
}
}
if (model[0])
cs_log("Stb model = %s", model);
if (vumodel[0])
cs_log("Stb vumodel = vu%s", vumodel);
if (boxtype[0])
{
char boxname[128];
if(!strcasecmp(boxtype,"ini-8000am")){snprintf(boxname, sizeof(boxname), "%s", "Atemio Nemesis");}
else if(!strcasecmp(boxtype,"ini-9000ru")){snprintf(boxname, sizeof(boxname), "%s", "Sezam Marvel");}
else if(!strcasecmp(boxtype,"ini-8000sv")){snprintf(boxname, sizeof(boxname), "%s", "Miraclebox Ultra");}
else if(!strcasecmp(boxtype,"ini-9000de")){snprintf(boxname, sizeof(boxname), "%s", "Xpeed LX3");}
else boxname[0] = 0;
if(boxname[0]){cs_log("Stb boxname = %s", boxname); stb_boxname = cs_strdup(boxname);}
cs_log("Stb boxtype = %s", boxtype);
}
if (boxtype[0])
stb_boxtype = cs_strdup(boxtype);
else if (model[0])
stb_boxtype = cs_strdup(model);
}
const char *boxtype_get(void)
{
return stb_boxtype ? stb_boxtype : "generic";
}
const char *boxname_get(void)
{
return stb_boxname ? stb_boxname : "generic";
}
bool boxtype_is(const char *boxtype)
{
return strcasecmp(boxtype_get(), boxtype) == 0;
}
bool boxname_is(const char *boxname)
{
return strcasecmp(boxname_get(), boxname) == 0;
}
/* Checks if the date of the system is correct and waits if necessary. */
static void init_check(void)
{
char *ptr = __DATE__;
int32_t month, year = atoi(ptr + strlen(ptr) - 4), day = atoi(ptr + 4);
if(day > 0 && day < 32 && year > 2010 && year < 9999)
{
struct tm timeinfo;
char months[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
for(month = 0; month < 12; ++month)
{
if(!strncmp(ptr, months[month], 3)) { break; }
}
if(month > 11) { month = 0; }
memset(&timeinfo, 0, sizeof(timeinfo));
timeinfo.tm_mday = day;
timeinfo.tm_mon = month;
timeinfo.tm_year = year - 1900;
time_t builddate = mktime(&timeinfo) - 86400;
int32_t i = 0;
while(time((time_t *)0) < builddate)
{
if(i == 0) { cs_log("The current system time is smaller than the build date (%s). Waiting up to %d seconds for time to correct", ptr, cs_waittime); }
cs_sleepms(1000);
++i;
if(i > cs_waittime)
{
cs_log("Waiting was not successful. OSCam will be started but is UNSUPPORTED this way. Do not report any errors with this version.");
break;
}
}
// adjust login time of first client
if(i > 0) { first_client->login = time((time_t *)0); }
}
}
#ifdef __linux__
#include <sys/prctl.h>
// PR_SET_NAME is introduced in 2.6.9 (which is ancient, released 18 Oct 2004)
// but apparantly we can't count on having at least that version :(
#ifndef PR_SET_NAME
#define PR_SET_NAME 15
#endif
// Set the thread name (comm) under linux (the limit is 16 chars)
void set_thread_name(const char *thread_name)
{
prctl(PR_SET_NAME, thread_name, NULL, NULL, NULL);
}
#else
void set_thread_name(const char *UNUSED(thread_name)) { }
#endif
static void fix_stacksize(void)
{
// Changing the default stack size is generally a bad idea.
// We are doing it anyway at the moment, because we are using several threads,
// and are running on machnies with little RAM.
// HOWEVER, as we do not know which minimal stack size is needed to run
// oscam without SEQFAULT (stack overflow), this is risky business.
// If after a code change SEQFAULTs related to stack overflow appear,
// increase OSCAM_STACK_MIN or remove the calls to SAFE_ATTR_SETSTACKSIZE.
#ifndef PTHREAD_STACK_MIN
#define PTHREAD_STACK_MIN 64000
#endif
#define OSCAM_STACK_MIN PTHREAD_STACK_MIN+32768
if(oscam_stacksize < OSCAM_STACK_MIN)
{
long pagesize = sysconf(_SC_PAGESIZE);
if(pagesize < 1)
{
oscam_stacksize = OSCAM_STACK_MIN;
return;
}
oscam_stacksize = (((OSCAM_STACK_MIN) / pagesize) + 1) * pagesize;
}
}
/* Starts a thread named nameroutine with the start function startroutine. */
int32_t start_thread(char *nameroutine, void *startroutine, void *arg, pthread_t *pthread, int8_t detach, int8_t modify_stacksize)
{
pthread_t temp;
pthread_attr_t attr;
cs_log_dbg(D_TRACE, "starting thread %s", nameroutine);
SAFE_ATTR_INIT(&attr);
if(modify_stacksize)
{ SAFE_ATTR_SETSTACKSIZE(&attr, oscam_stacksize); }
int32_t ret = pthread_create(pthread == NULL ? &temp : pthread, &attr, startroutine, arg);
if(ret)
{ cs_log("ERROR: can't create %s thread (errno=%d %s)", nameroutine, ret, strerror(ret)); }
else
{
cs_log_dbg(D_TRACE, "%s thread started", nameroutine);
if(detach)
{ pthread_detach(pthread == NULL ? temp : *pthread); }
}
pthread_attr_destroy(&attr);
return ret;
}