source: trunk/globals.h@ 8

Last change on this file since 8 was 8, checked in by smurzch2, 12 years ago

Start repository for OSCam (Open Source Cam)

The start is from the MpCS source code. Thanks a lot to the authors for this
great sources.

File size: 17.8 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <assert.h>
4#include <fcntl.h>
5#include <sys/ioctl.h>
6#include <sys/poll.h>
7#include <ctype.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/shm.h>
11#include <sys/wait.h>
12#include <unistd.h>
13#include <sys/mman.h>
14#include <stdarg.h>
15#include <time.h>
16#include <sys/timeb.h>
17#include <limits.h>
18
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <netdb.h>
22#include <string.h>
23#include <signal.h>
24#include <errno.h>
25
26#include <pthread.h>
27
28#ifndef CS_GLOBALS
29#define CS_GLOBALS
30#define CS_VERSION "0.9d"
31
32#if defined(__GNUC__)
33# define GCC_PACK __attribute__((packed))
34#else
35# define GCC_PACK
36#endif
37
38#include "mpcs-config.h"
39#include "mpcs-ostype.h"
40#include "mpcs-types.h"
41#include "cscrypt/cscrypt.h"
42
43#ifndef CS_CONFDIR
44#define CS_CONFDIR "/usr/local/etc"
45#endif
46#ifndef CS_MMAPFILE
47#define CS_MMAPFILE "/tmp/mcps.mem"
48#endif
49#ifndef CS_LOGFILE
50#define CS_LOGFILE "/var/log/mcps.log"
51#endif
52#define CS_QLEN 128 // size of request queue
53#define CS_MAXQLEN 128 // size of request queue for cardreader
54#define CS_MAXCAIDTAB 32 // max. caid-defs/user
55#define CS_MAXPROV 32
56#define CS_MAXPORTS 32 // max server ports
57#define CS_MAXFILTERS 16
58
59#define CS_MAXCARDS 4096
60#define CS_MAXIGNORE 1024
61#define CS_MAXLOCALS 16
62#define CS_ECMSTORESIZE 16 // use MD5()
63#define CS_EMMSTORESIZE 270
64#define CS_CLIENT_TIMEOUT 5
65#define CS_CLIENT_MAXIDLE 120
66#define CS_BIND_TIMEOUT 120
67#define CS_DELAY 0
68#define CS_RESOLVE_DELAY 30
69#define CS_MAXLOGHIST 30
70#define CS_LOGHISTSIZE 160 // 32+128: username + logline
71
72#ifdef OLD_DEFS
73#ifdef CS_EMBEDDED
74#define CS_MAXPENDING 32
75#define CS_ECMCACHESIZE 32
76#define CS_EMMCACHESIZE 64
77#define CS_MAXPID 32
78#define CS_MAXREADER 8
79#else
80#define CS_MAXPENDING 128
81#define CS_ECMCACHESIZE 128
82#define CS_EMMCACHESIZE 256
83#define CS_MAXPID 128
84#define CS_MAXREADER 64
85#endif
86#endif
87
88#ifdef CS_EMBEDDED
89#define CS_MAXPID 32
90#define CS_MAXREADER (CS_MAXPID>>1)
91#define CS_MAXPENDING CS_MAXPID
92#define CS_ECMCACHESIZE CS_MAXPID
93#define CS_EMMCACHESIZE (CS_MAXPID<<1)
94#else
95#define CS_MAXPID 512
96#define CS_MAXREADER (CS_MAXPID>>2)
97#define CS_MAXPENDING (CS_MAXPID<<1)
98#define CS_ECMCACHESIZE CS_MAXPID
99#define CS_EMMCACHESIZE (CS_MAXPID<<1)
100#define CS_RDR_INIT_HIST
101#endif
102
103#define D_DUMP 1 // Debug Dumps
104#define D_MASTER 2 // Debug Master Process
105#define D_READER 4 // Debug Reader/Proxy Process
106#define D_CLIENT 8 // Debug Client Process
107#define D_DEVICE 16 // Debug Reader I/O
108#define D_WATCHDOG 32 // Debug Watchdog
109#define D_ALL_DUMP 63
110
111#define R_MOUSE 0x1 // Reader smartcard mouse
112#define R_INTERN 0x2 // Reader smartcard intern
113#define R_CAMD35 0x10 // Reader cascading camd 3.5x
114#define R_CAMD33 0x11 // Reader cascading camd 3.3x
115#define R_NEWCAMD 0x12 // Reader cascading newcamd
116#define R_RADEGAST 0x13 // Reader cascading radegast
117#define R_CS378X 0x14 // Reader cascading camd 3.5x TCP
118#define R_GBOX 0x20 // Reader cascading gbox
119#define R_SERIAL 0x80 // Reader serial
120#define R_IS_NETWORK 0x70
121#define R_IS_CASCADING 0xF0
122
123#define CS_MAX_MOD 8
124#define MOD_CONN_TCP 1
125#define MOD_CONN_UDP 2
126#define MOD_CONN_NET 3
127#define MOD_CONN_SERIAL 4
128
129#ifdef CS_CORE
130char *PIP_ID_TXT[] = { "ECM", "EMM", "LOG", "CIN", "HUP", NULL };
131char *RDR_CD_TXT[] = { "cd", "dsr", "cts", "ring", "none",
132#ifdef USE_GPIO
133 "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7",
134#endif
135 NULL };
136#else
137extern char *PIP_ID_TXT[];
138extern char *RDR_CD_TXT[];
139#endif
140
141#define PIP_ID_ECM 0
142#define PIP_ID_EMM 1
143#define PIP_ID_LOG 2
144#define PIP_ID_CIN 3 // CARD_INFO
145#define PIP_ID_HUP 4
146#define PIP_ID_MAX PIP_ID_HUP
147#define PIP_ID_DCW 5
148
149#define PIP_ID_ERR (-1)
150#define PIP_ID_DIR (-2)
151#define PIP_ID_NUL (-3)
152
153#define cdiff *c_start
154
155#define NCD_AUTO 0
156#define NCD_524 1
157#define NCD_525 2
158
159#define CS_ANTICASC
160
161enum {E1_GLOBAL=0, E1_USER, E1_READER, E1_SERVER, E1_LSERVER};
162enum {E2_GLOBAL=0, E2_GROUP, E2_CAID, E2_IDENT, E2_CLASS, E2_CHID, E2_QUEUE,
163 E2_EA_LEN, E2_F0_LEN, E2_OFFLINE, E2_SID};
164
165//typedef unsigned char uchar;
166//typedef unsigned long ulong;
167
168typedef struct s_classtab
169{
170 uchar an;
171 uchar bn;
172 uchar aclass[31];
173 uchar bclass[31];
174} GCC_PACK CLASSTAB;
175
176typedef struct s_caidtab
177{
178 ushort caid[CS_MAXCAIDTAB];
179 ushort mask[CS_MAXCAIDTAB];
180 ushort cmap[CS_MAXCAIDTAB];
181} GCC_PACK CAIDTAB;
182
183typedef struct s_sidtab
184{
185 char label[33];
186 ushort num_caid;
187 ushort num_provid;
188 ushort num_srvid;
189 ushort *caid;
190 ulong *provid;
191 ushort *srvid;
192 struct s_sidtab *next;
193} GCC_PACK SIDTAB;
194
195
196typedef struct s_filter
197{
198 ushort caid;
199 uchar nprids;
200 ulong prids[CS_MAXPROV];
201} GCC_PACK FILTER;
202
203typedef struct s_ftab
204{
205 int nfilts;
206 FILTER filts[CS_MAXFILTERS];
207} GCC_PACK FTAB;
208
209typedef struct s_port
210{
211 int fd;
212 int s_port;
213 FTAB ftab;
214} GCC_PACK PORT;
215
216typedef struct s_ptab
217{
218 int nports;
219 PORT ports[CS_MAXPORTS];
220} GCC_PACK PTAB;
221
222struct s_ecm
223{
224 uchar ecmd5[CS_ECMSTORESIZE];
225 uchar cw[16];
226 ushort caid;
227 ulong prid;
228 ulong grp;
229// int level;
230};
231
232struct s_emm
233{
234 uchar emm[CS_EMMSTORESIZE];
235 uchar type;
236 int count;
237};
238
239struct s_module
240{
241 //int fd;
242 int multi;
243 int type;
244 int watchdog;
245 char desc[16];
246 char *logtxt;
247 //int s_port;
248 in_addr_t s_ip;
249 void (*s_handler)();
250 int (*recv)();
251 void (*send_dcw)();
252 int c_multi;
253 int (*c_recv_chk)();
254 int (*c_init)();
255 int (*c_send_ecm)();
256 int (*c_init_log)();
257 int (*c_recv_log)();
258 int c_port;
259 PTAB *ptab;
260};
261
262struct s_irdeto_quess
263{
264 int b47;
265 ushort caid;
266 ushort sid;
267 struct s_irdeto_quess *next;
268};
269
270struct s_client
271{
272 pid_t pid;
273 in_addr_t ip;
274 in_port_t port;
275 time_t login;
276 time_t last;
277 time_t lastswitch;
278 time_t lastemm;
279 time_t lastecm;
280 ulong grp;
281 int crypted;
282 int dup;
283 int au;
284 int monlvl;
285 int dbglvl;
286 CAIDTAB ctab;
287 ulong sidtabok; // positiv services
288 ulong sidtabno; // negative services
289 int typ;
290 int ctyp;
291 int stat;
292 int ufd;
293 int last_srvid;
294 int last_caid;
295 int tosleep;
296 char usr[32];
297 int udp_fd;
298 int fd_m2c;
299 struct sockaddr_in udp_sa;
300 int log;
301 int logcounter;
302 int cwfound;
303 int cwcache;
304 int cwnot;
305 uchar ucrc[4]; // needed by monitor and used by camd35
306 ulong pcrc; // pwd crc
307 AES_KEY aeskey; // needed by monitor and used by camd33, camd35
308 ushort ncd_msgid;
309 uchar ncd_skey[16];
310 int port_idx; // index in server ptab
311 int ncd_server; // newcamd server?
312#ifdef CS_ANTICASC
313 ushort ac_idx;
314 ushort ac_limit;
315 uchar ac_penalty;
316#endif
317 FTAB fchid;
318 FTAB ftab; // user [caid] and ident filter
319 CLASSTAB cltab;
320};
321
322struct s_reader
323{
324 int cs_idx;
325 int fd;
326 ulong grp;
327 int fallback;
328 int typ;
329 int card_system;
330 char label[32];
331 char device[128];
332 int detect;
333 int r_port;
334 char r_usr[64];
335 char r_pwd[64];
336 int r_crypted;
337 int l_port;
338 int log_port;
339 CAIDTAB ctab;
340 ulong sidtabok; // positiv services
341 ulong sidtabno; // negative services
342 uchar hexserial[8];
343 int nprov;
344 uchar prid[CS_MAXPROV][8];
345 uchar availkeys[CS_MAXPROV][16]; // viaccess; misused in seca, if availkeys[PROV][0]=0 then expired, 1 then valid.
346 uchar sa[CS_MAXPROV][4]; // viaccess & seca
347 ushort acs; // irdeto
348 ushort caid[16];
349 uchar b_nano[256];
350 int logemm;
351 int cachemm;
352 int rewritemm;
353 int online;
354 struct s_module ph;
355 uchar ncd_key[16];
356 uchar ncd_skey[16];
357 int ncd_disable_server_filt;
358 ushort ncd_msgid;
359 int ncd_proto;
360 uchar tcp_connected;
361 int tcp_ito; // inactivity timeout
362 int tcp_rto; // reconnect timeout
363 time_t last_g; // get (if last_s-last_g>tcp_rto - reconnect )
364 time_t last_s; // send
365 uchar show_cls; // number of classes subscription showed on kill -31
366 int maxqlen; // max queue length
367 int qlen; // current queue length
368 FTAB fchid;
369 FTAB ftab;
370 CLASSTAB cltab;
371 uchar gbox_pwd[4];
372 uchar gbox_timecode[7];
373 int gbox_online;
374 uchar gbox_vers;
375 uchar gbox_prem;
376 int gbox_fd;
377 struct timeb gbox_lasthello; // incoming time stamp
378#ifdef CS_RDR_INIT_HIST
379 uchar init_history[1024];
380 int init_history_pos;
381#endif
382};
383
384#ifdef CS_ANTICASC
385
386struct s_acasc_shm {
387 ushort count : 15;
388 ushort deny : 1;
389};
390
391struct s_acasc {
392 ushort stat[10];
393 uchar idx; // current active index in stat[]
394};
395
396struct s_cpmap
397{
398 ushort caid;
399 ulong provid;
400 ushort sid;
401 ushort chid;
402 ushort dwtime;
403 struct s_cpmap *next;
404};
405#endif
406
407struct s_auth
408{
409 char usr[33];
410 char pwd[33];
411 int uniq;
412 int au;
413 int monlvl;
414 ulong grp;
415 int tosleep;
416 CAIDTAB ctab;
417 ulong sidtabok; // positiv services
418 ulong sidtabno; // negative services
419 FTAB fchid;
420 FTAB ftab; // user [caid] and ident filter
421 CLASSTAB cltab;
422#ifdef CS_ANTICASC
423 int ac_idx;
424 int ac_users; // 0 - unlimited
425 uchar ac_penalty; // 0 - log, >0 - fake dw
426#endif
427 in_addr_t dynip;
428 uchar dyndns[64];
429 int premhack;
430 struct s_auth *next;
431};
432
433struct s_srvid
434{
435 int srvid;
436 char name[33];
437 struct s_srvid *next;
438};
439
440struct s_ip
441{
442 in_addr_t ip[2];
443 struct s_ip *next;
444};
445
446struct s_config
447{
448 int nice;
449 ulong netprio;
450 int ctimeout;
451 int ftimeout;
452 int cmaxidle;
453 ulong delay;
454 int bindwait;
455 int resolvedelay;
456 int tosleep;
457 in_addr_t srvip;
458 char pidfile[128];
459 char usrfile[128];
460 struct s_auth *account;
461 struct s_srvid *srvid;
462 struct s_sidtab *sidtab;
463 int mon_port;
464 in_addr_t mon_srvip;
465 struct s_ip *mon_allowed;
466 int mon_aulow;
467 int mon_hideclient_to;
468 int mon_level;
469 int c33_port;
470 in_addr_t c33_srvip;
471 uchar c33_key[16];
472 int c33_crypted;
473 int c33_passive;
474 struct s_ip *c33_plain;
475 int c35_port;
476 in_addr_t c35_srvip;
477 PTAB c35_tcp_ptab;
478 in_addr_t c35_tcp_srvip;
479 PTAB ncd_ptab;
480 in_addr_t ncd_srvip;
481 uchar ncd_key[16];
482 int rad_port;
483 in_addr_t rad_srvip;
484 struct s_ip *rad_allowed;
485 char rad_usr[32];
486 char ser_device[512];
487 int srtimeout; // SerialReaderTimeount in millisec
488 int max_log_size;
489 int show_ecm_dw;
490 uchar gbox_pwd[8];
491 uchar ignorefile[512];
492 uchar cardfile[512];
493 uchar gbxShareOnl[512];
494 int maxdist;
495 int num_locals;
496 unsigned long locals[CS_MAXLOCALS];
497 //struct s_irdeto_quess *itab[0xff];
498#ifdef CS_ANTICASC
499 char ac_enabled;
500 int ac_users; // num of users for account (0 - default)
501 int ac_stime; // time to collect AC statistics (3 min - default)
502 int ac_samples; // qty of samples
503 int ac_penalty; // 0 - write to log
504 int ac_fakedelay; // 100-1000 ms
505 int ac_denysamples;
506 char ac_logfile[128];
507 struct s_cpmap *cpmap;
508#endif
509// struct s_reader reader[];
510};
511
512typedef struct ecm_request_t
513{
514
515 uchar ecm[256];
516 uchar cw[16];
517 uchar ecmd5[CS_ECMSTORESIZE];
518// uchar l;
519 short l;
520 ushort caid;
521 ushort ocaid;
522 ushort srvid;
523 ushort chid;
524 ushort pid;
525 ushort idx;
526 ulong prid;
527 int reader[CS_MAXREADER];
528 int cidx; // client index
529 int cpti; // client pending table index
530 int stage; // processing stage in server module
531 int level; // send-level in client module
532 int rc;
533 uchar rcEx;
534 struct timeb tps; // incoming time stamp
535 ushort gbxCWFrom;
536 ushort gbxFrom;
537 ushort gbxTo;
538
539 uchar gbxForward[16];
540 int gbxRidx;
541} GCC_PACK ECM_REQUEST;
542
543typedef struct emm_packet_t
544{
545 uchar emm[258];
546 uchar l;
547 uchar caid[2];
548 uchar provid[4];
549 uchar hexserial[8];
550 uchar type;
551 int cidx;
552} GCC_PACK EMM_PACKET;
553
554// mpcs-simples
555extern char *remote_txt(void);
556extern char *trim(char *);
557extern char *strtolower(char *);
558extern int gethexval(char);
559extern int cs_atob(uchar *, char *, int);
560extern ulong cs_atoi(char *, int, int);
561extern int byte_atob(char *);
562extern long word_atob(char *);
563extern int key_atob(char *, uchar *);
564extern int key_atob4(char *, uchar *);
565extern char *key_btoa(char *, uchar *);
566extern char *cs_hexdump(int, uchar *, int);
567extern in_addr_t cs_inet_order(in_addr_t);
568extern char *cs_inet_ntoa(in_addr_t);
569extern in_addr_t cs_inet_addr(char *txt);
570extern ulong b2i(int, uchar *);
571extern ullong b2ll(int, uchar *);
572extern uchar *i2b(int, ulong);
573extern ulong a2i(char *, int);
574extern int boundary(int, int);
575extern void cs_ftime(struct timeb *);
576extern void cs_sleepms(int);
577extern int bytes_available(int);
578extern void cs_setpriority(int);
579extern struct s_auth *find_user(char *);
580
581// mpcs variables
582extern int pfd, rfd, fd_c2m, fd_m2c, cs_idx, *c_start, cs_ptyp, cs_dblevel, cs_hw;
583extern int *logidx, *loghistidx, *log_fd;
584extern int is_server, *mcl;
585extern uchar mbuf[1024];
586extern ushort len4caid[256];
587extern pid_t master_pid;
588extern struct s_ecm *ecmcache;
589extern struct s_client *client;
590extern struct s_reader *reader;
591
592extern struct card_struct *Cards;
593extern struct idstore_struct *idstore;
594extern unsigned long *IgnoreList;
595
596extern struct s_config *cfg;
597extern char cs_confdir[], *loghist;
598extern EMM_PACKET epg;
599extern struct s_module ph[CS_MAX_MOD];
600extern ECM_REQUEST *ecmtask;
601extern char logfile[256];
602#ifdef CS_ANTICASC
603extern struct s_acasc_shm *acasc;
604extern FILE *fpa;
605extern int use_ac_log;
606#endif
607
608
609// mpcs
610extern char *cs_platform(char *);
611extern int recv_from_udpipe(uchar *, int);
612extern char* username(int);
613extern int idx_from_pid(pid_t);
614extern int chk_bcaid(ECM_REQUEST *, CAIDTAB *);
615extern void cs_exit(int sig);
616extern int cs_fork(in_addr_t, in_port_t);
617extern void wait4master(void);
618extern int cs_auth_client(struct s_auth *, char*);
619extern void cs_disconnect_client(void);
620extern int check_ecmcache(ECM_REQUEST *, ulong);
621extern int write_to_pipe(int, int, uchar *, int);
622extern int read_from_pipe(int, uchar **, int);
623extern int write_ecm_request(int, ECM_REQUEST *);
624extern int write_ecm_answer(int, ECM_REQUEST *);
625extern void log_emm_request(int);
626extern ulong chk_provid(uchar *, ushort);
627extern void guess_cardsystem(ECM_REQUEST *);
628extern void guess_irdeto(ECM_REQUEST *);
629extern void get_cw(ECM_REQUEST *);
630extern void do_emm(EMM_PACKET *);
631extern ECM_REQUEST *get_ecmtask(void);
632extern void request_cw(ECM_REQUEST *, int);
633extern int send_dcw(ECM_REQUEST *);
634extern int process_input(uchar *, int, int);
635extern int chk_srvid(ECM_REQUEST *, int);
636extern int chk_sfilter(ECM_REQUEST *, PTAB*);
637extern int chk_ufilters(ECM_REQUEST *);
638extern int chk_rfilter(ECM_REQUEST *, struct s_reader *);
639extern int chk_rsfilter(ECM_REQUEST *, int);
640extern int chk_avail_reader(ECM_REQUEST *, struct s_reader *);
641extern void set_signal_handler(int , int , void (*)(int));
642extern void cs_log_config(void);
643
644#ifdef CS_ANTICASC
645//extern void start_anticascader(void);
646extern void init_ac(void);
647extern void ac_init_stat(int);
648extern int ac_init_log(char*);
649extern void ac_do_stat(void);
650extern void ac_init_client(struct s_auth *);
651extern void ac_chk(ECM_REQUEST*, int);
652#endif
653
654// mpcs-nano
655extern int chk_class(ECM_REQUEST *, CLASSTAB*, const char*, const char*);
656
657// mpcs-config
658extern int init_config(void);
659extern int init_userdb(void);
660extern int init_readerdb(void);
661extern int init_sidtab(void);
662extern int init_srvid(void);
663extern int search_boxkey(ushort, ulong, char *);
664extern void init_len4caid(void);
665extern int init_irdeto_guess_tab(void);
666
667// mpcs-reader
668extern int ridx, logfd;
669extern void cs_ri_brk(int);
670extern void cs_ri_log(char *,...);
671extern void start_cardreader(void);
672extern void reader_card_info(void);
673
674// mpcs-log
675extern int cs_init_log(char *);
676extern void cs_log(char *,...);
677extern void cs_debug(char *,...);
678extern void cs_ddump(uchar *, int, char *, ...);
679extern void cs_close_log(void);
680extern int cs_init_statistics(char *);
681extern void cs_statistics(int);
682extern void cs_dump(uchar *, int, char *, ...);
683
684// mpcs-aes
685extern void aes_set_key(char *);
686extern void aes_encrypt_idx(int, uchar *, int);
687extern void aes_decrypt(uchar *, int);
688#define aes_encrypt(b, n) aes_encrypt_idx(cs_idx, b, n)
689
690// reader-common
691extern int reader_device_init(char *, int);
692extern int reader_checkhealth(void);
693extern int reader_ecm(ECM_REQUEST *);
694extern int reader_emm(EMM_PACKET *);
695
696// reader-irdeto
697extern int irdeto_card_init(uchar *, int);
698extern int irdeto_do_ecm(ECM_REQUEST *);
699extern int irdeto_do_emm(EMM_PACKET *);
700extern int irdeto_card_info(void);
701
702// reader-viaccess
703extern int viaccess_card_init(uchar *, int);
704extern int viaccess_do_ecm(ECM_REQUEST *);
705extern int viaccess_do_emm(EMM_PACKET *);
706extern int viaccess_card_info(void);
707
708// reader-cryptoworks
709extern int cryptoworks_card_init(uchar *, int);
710extern int cryptoworks_do_ecm(ECM_REQUEST *);
711extern int cryptoworks_do_emm(EMM_PACKET *);
712extern int cryptoworks_card_info(void);
713
714// reader-seca
715extern int seca_card_init(uchar *, int);
716extern int seca_do_ecm(ECM_REQUEST *);
717extern int seca_do_emm(EMM_PACKET *);
718extern int seca_card_info(void);
719
720// protocol modules
721extern int monitor_send_idx(int, char *);
722extern void module_monitor(struct s_module *);
723extern void module_camd35(struct s_module *);
724extern void module_camd35_tcp(struct s_module *);
725extern void module_camd33(struct s_module *);
726extern void module_newcamd(struct s_module *);
727extern void module_radegast(struct s_module *);
728extern void module_mpcser(struct s_module *);
729extern void module_gbox(struct s_module *);
730extern struct timeval *chk_pending(struct timeb tp_ctimeout);
731#endif // CS_GLOBALS
Note: See TracBrowser for help on using the repository browser.