1 | #include "globals.h"
|
---|
2 |
|
---|
3 | static int radegast_send(uchar *buf)
|
---|
4 | {
|
---|
5 | int l=buf[1]+2;
|
---|
6 | return(send(pfd, buf, l, 0));
|
---|
7 | }
|
---|
8 |
|
---|
9 | static int radegast_recv(uchar *buf, int l)
|
---|
10 | {
|
---|
11 | int n;
|
---|
12 | if (!pfd) return(-1);
|
---|
13 | if ((n=recv(pfd, buf, l, 0))>0)
|
---|
14 | client[cs_idx].last=time((time_t *) 0);
|
---|
15 | return(n);
|
---|
16 | }
|
---|
17 |
|
---|
18 | static void radegast_auth_client(in_addr_t ip)
|
---|
19 | {
|
---|
20 | int ok;
|
---|
21 | struct s_auth *account;
|
---|
22 | struct s_ip *p_ip;
|
---|
23 | for (ok=0, p_ip=cfg->rad_allowed; (p_ip) && (!ok); p_ip=p_ip->next)
|
---|
24 | ok=((ip>=p_ip->ip[0]) && (ip<=p_ip->ip[1]));
|
---|
25 | if (!ok)
|
---|
26 | {
|
---|
27 | cs_auth_client((struct s_auth *)0, NULL);
|
---|
28 | cs_exit(0);
|
---|
29 | }
|
---|
30 | for (ok=0, account=cfg->account; (cfg->rad_usr[0]) && (account) && (!ok); account=account->next)
|
---|
31 | if (ok=(!strcmp(cfg->rad_usr, account->usr)))
|
---|
32 | if (cs_auth_client(account, NULL))
|
---|
33 | cs_exit(0);
|
---|
34 | if (!ok)
|
---|
35 | cs_auth_client((struct s_auth *)(-1), NULL);
|
---|
36 | }
|
---|
37 |
|
---|
38 | static int get_request(uchar *buf)
|
---|
39 | {
|
---|
40 | int n, rc=0;
|
---|
41 | if ((n=process_input(buf, 2, cfg->cmaxidle))==2)
|
---|
42 | {
|
---|
43 | if ((n=process_input(buf+2, buf[1], 0))>=0)
|
---|
44 | n+=2;
|
---|
45 | if (n-2==buf[1])
|
---|
46 | rc=n;
|
---|
47 | else
|
---|
48 | cs_log("WARNING: protocol error (garbage)");
|
---|
49 | }
|
---|
50 | if (n>0)
|
---|
51 | cs_ddump(buf, n, "received %d bytes from client", n);
|
---|
52 | return(rc);
|
---|
53 | }
|
---|
54 |
|
---|
55 | static void radegast_send_dcw(ECM_REQUEST *er)
|
---|
56 | {
|
---|
57 | mbuf[0]=0x02; // DCW
|
---|
58 | if (er->rc<4)
|
---|
59 | {
|
---|
60 | mbuf[1]=0x12; // len (overall)
|
---|
61 | mbuf[2]=0x05; // ACCESS
|
---|
62 | mbuf[3]=0x10; // len
|
---|
63 | memcpy(mbuf+4, er->cw, 16);
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | mbuf[1]=0x02; // len (overall)
|
---|
68 | mbuf[2]=0x04; // NO ACCESS
|
---|
69 | mbuf[3]=0x00; // len
|
---|
70 | }
|
---|
71 | radegast_send(mbuf);
|
---|
72 | }
|
---|
73 |
|
---|
74 | static void radegast_process_ecm(uchar *buf, int l)
|
---|
75 | {
|
---|
76 | int i, n, sl;
|
---|
77 | ECM_REQUEST *er;
|
---|
78 |
|
---|
79 | if (!(er=get_ecmtask()))
|
---|
80 | return;
|
---|
81 | for (i=0; i<l; i+=(sl+2))
|
---|
82 | {
|
---|
83 | sl=buf[i+1];
|
---|
84 | switch(buf[i])
|
---|
85 | {
|
---|
86 | case 2: // CAID (upper byte only, oldstyle)
|
---|
87 | er->caid=buf[i+2]<<8;
|
---|
88 | break;
|
---|
89 | case 10: // CAID
|
---|
90 | er->caid=b2i(2, buf+i+2);
|
---|
91 | break;
|
---|
92 | case 3: // ECM DATA
|
---|
93 | er->l=sl;
|
---|
94 | memcpy(er->ecm, buf+i+2, er->l);
|
---|
95 | break;
|
---|
96 | case 6: // PROVID (ASCII)
|
---|
97 | n=(sl>6) ? 3 : (sl>>1);
|
---|
98 | er->prid=cs_atoi((char *) buf+i+2+sl-(n<<1), n, 0);
|
---|
99 | break;
|
---|
100 | case 7: // KEYNR (ASCII), not needed
|
---|
101 | break;
|
---|
102 | case 8: // ECM PROCESS PID ?? don't know, not needed
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | if (l!=i)
|
---|
107 | cs_log("WARNING: ECM-request corrupt");
|
---|
108 | else
|
---|
109 | get_cw(er);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void radegast_process_unknown(uchar *buf)
|
---|
113 | {
|
---|
114 | uchar answer[2]={0x81, 0x00};
|
---|
115 | radegast_send(answer);
|
---|
116 | cs_log("unknown request %02X, len=%d", buf[0], buf[1]);
|
---|
117 | }
|
---|
118 |
|
---|
119 | static void radegast_server()
|
---|
120 | {
|
---|
121 | int n;
|
---|
122 |
|
---|
123 | radegast_auth_client(client[cs_idx].ip);
|
---|
124 | while ((n=get_request(mbuf))>0)
|
---|
125 | {
|
---|
126 | switch(mbuf[0])
|
---|
127 | {
|
---|
128 | case 1:
|
---|
129 | radegast_process_ecm(mbuf+2, mbuf[1]);
|
---|
130 | break;
|
---|
131 | default:
|
---|
132 | radegast_process_unknown(mbuf);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | cs_disconnect_client();
|
---|
136 | }
|
---|
137 |
|
---|
138 | void module_radegast(struct s_module *ph)
|
---|
139 | {
|
---|
140 | static PTAB ptab;
|
---|
141 | ptab.ports[0].s_port = cfg->rad_port;
|
---|
142 | ph->ptab = &ptab;
|
---|
143 | ph->ptab->nports = 1;
|
---|
144 |
|
---|
145 | strcpy(ph->desc, "radegast");
|
---|
146 | ph->type=MOD_CONN_TCP;
|
---|
147 | ph->multi=0;
|
---|
148 | ph->watchdog=1;
|
---|
149 | ph->s_ip=cfg->rad_srvip;
|
---|
150 | ph->s_handler=radegast_server;
|
---|
151 | ph->recv=radegast_recv;
|
---|
152 | ph->send_dcw=radegast_send_dcw;
|
---|
153 | }
|
---|