1 | #include "globals.h"
|
---|
2 | #ifdef MODULE_RADEGAST
|
---|
3 | #include "oscam-client.h"
|
---|
4 | #include "oscam-ecm.h"
|
---|
5 | #include "oscam-net.h"
|
---|
6 | #include "oscam-string.h"
|
---|
7 | #include "oscam-reader.h"
|
---|
8 |
|
---|
9 | static int32_t radegast_send(struct s_client * client, uchar *buf)
|
---|
10 | {
|
---|
11 | int32_t l=buf[1]+2;
|
---|
12 | return(send(client->pfd, buf, l, 0));
|
---|
13 | }
|
---|
14 |
|
---|
15 | static int32_t radegast_recv(struct s_client *client, uchar *buf, int32_t l)
|
---|
16 | {
|
---|
17 | int32_t n;
|
---|
18 | if (!client->pfd) return(-1);
|
---|
19 | if (client->typ == 'c') { // server code
|
---|
20 | if ((n=recv(client->pfd, buf, l, 0))>0)
|
---|
21 | client->last=time((time_t *) 0);
|
---|
22 | } else { // client code
|
---|
23 | if ((n=recv(client->pfd, buf, l, 0))>0) {
|
---|
24 | cs_ddump_mask(D_CLIENT, buf, n, "radegast: received %d bytes from %s", n, remote_txt());
|
---|
25 | client->last = time((time_t *) 0);
|
---|
26 |
|
---|
27 | if (buf[0] == 2) { // dcw received
|
---|
28 | if (buf[3] != 0x10) { // dcw ok
|
---|
29 | cs_log("radegast: no dcw");
|
---|
30 | n = -1;
|
---|
31 | }
|
---|
32 | }
|
---|
33 | }
|
---|
34 | }
|
---|
35 | return(n);
|
---|
36 | }
|
---|
37 |
|
---|
38 | static int32_t radegast_recv_chk(struct s_client *client, uchar *dcw, int32_t *rc, uchar *buf, int32_t UNUSED(n))
|
---|
39 | {
|
---|
40 | if ((buf[0] == 2) && (buf[1] == 0x12)) {
|
---|
41 | char tmp_dbg[33];
|
---|
42 | memcpy(dcw, buf+4, 16);
|
---|
43 | cs_debug_mask(D_CLIENT, "radegast: recv chk - %s", cs_hexdump(0, dcw, 16, tmp_dbg, sizeof(tmp_dbg)));
|
---|
44 | *rc = 1;
|
---|
45 | return(client->reader->msg_idx);
|
---|
46 | }
|
---|
47 |
|
---|
48 | return (-1);
|
---|
49 | }
|
---|
50 |
|
---|
51 | static void radegast_auth_client(IN_ADDR_T ip)
|
---|
52 | {
|
---|
53 | int32_t ok;
|
---|
54 | struct s_auth *account;
|
---|
55 | struct s_client *cl = cur_client();
|
---|
56 |
|
---|
57 | ok = check_ip(cfg.rad_allowed, ip);
|
---|
58 |
|
---|
59 | if (!ok)
|
---|
60 | {
|
---|
61 | cs_log ("radegast: IP not allowed");
|
---|
62 | cs_auth_client(cl, (struct s_auth *)0, NULL);
|
---|
63 | cs_disconnect_client(cl);
|
---|
64 | }
|
---|
65 |
|
---|
66 | for (ok = 0, account = cfg.account; cfg.rad_usr && account && !ok; account = account->next)
|
---|
67 | {
|
---|
68 | ok = streq(cfg.rad_usr, account->usr);
|
---|
69 | if (ok && cs_auth_client(cl, account, NULL))
|
---|
70 | cs_disconnect_client(cl);
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (!ok)
|
---|
74 | cs_auth_client(cl, ok ? account : (struct s_auth *)(-1), "radegast");
|
---|
75 | }
|
---|
76 |
|
---|
77 | static void radegast_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
78 | {
|
---|
79 | uchar mbuf[1024];
|
---|
80 | mbuf[0]=0x02; // DCW
|
---|
81 | if (er->rc < E_NOTFOUND)
|
---|
82 | {
|
---|
83 | mbuf[1]=0x12; // len (overall)
|
---|
84 | mbuf[2]=0x05; // ACCESS
|
---|
85 | mbuf[3]=0x10; // len
|
---|
86 | memcpy(mbuf+4, er->cw, 16);
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | mbuf[1]=0x02; // len (overall)
|
---|
91 | mbuf[2]=0x04; // NO ACCESS
|
---|
92 | mbuf[3]=0x00; // len
|
---|
93 | }
|
---|
94 | radegast_send(client, mbuf);
|
---|
95 | }
|
---|
96 |
|
---|
97 | static void radegast_process_ecm(uchar *buf, int32_t l)
|
---|
98 | {
|
---|
99 | int32_t i, n, sl;
|
---|
100 | ECM_REQUEST *er;
|
---|
101 | struct s_client *cl = cur_client();
|
---|
102 |
|
---|
103 | if (!(er=get_ecmtask()))
|
---|
104 | return;
|
---|
105 | for (i=0; i<l; i+=(sl+2))
|
---|
106 | {
|
---|
107 | sl=buf[i+1];
|
---|
108 | switch(buf[i])
|
---|
109 | {
|
---|
110 | case 2: // CAID (upper byte only, oldstyle)
|
---|
111 | er->caid=buf[i+2]<<8;
|
---|
112 | break;
|
---|
113 | case 10: // CAID
|
---|
114 | er->caid=b2i(2, buf+i+2);
|
---|
115 | break;
|
---|
116 | case 3: // ECM DATA
|
---|
117 | er->ecmlen = sl;
|
---|
118 | memcpy(er->ecm, buf+i+2, er->ecmlen);
|
---|
119 | break;
|
---|
120 | case 6: // PROVID (ASCII)
|
---|
121 | n=(sl>6) ? 3 : (sl>>1);
|
---|
122 | er->prid=cs_atoi((char *) buf+i+2+sl-(n<<1), n, 0);
|
---|
123 | break;
|
---|
124 | case 7: // KEYNR (ASCII), not needed
|
---|
125 | break;
|
---|
126 | case 8: // ECM PROCESS PID ?? don't know, not needed
|
---|
127 | break;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | if (l!=i)
|
---|
131 | cs_log("WARNING: ECM-request corrupt");
|
---|
132 | else
|
---|
133 | get_cw(cl, er);
|
---|
134 | }
|
---|
135 |
|
---|
136 | static void radegast_process_unknown(uchar *buf)
|
---|
137 | {
|
---|
138 | uchar answer[2]={0x81, 0x00};
|
---|
139 | radegast_send(cur_client(), answer);
|
---|
140 | cs_log("unknown request %02X, len=%d", buf[0], buf[1]);
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void * radegast_server(struct s_client * client, uchar *mbuf, int32_t n)
|
---|
144 | {
|
---|
145 | if (n<3)
|
---|
146 | return NULL;
|
---|
147 |
|
---|
148 | if (!client->init_done) {
|
---|
149 | radegast_auth_client(cur_client()->ip);
|
---|
150 | client->init_done=1;
|
---|
151 | }
|
---|
152 |
|
---|
153 | switch(mbuf[0]) {
|
---|
154 | case 1:
|
---|
155 | radegast_process_ecm(mbuf+2, mbuf[1]);
|
---|
156 | break;
|
---|
157 | default:
|
---|
158 | radegast_process_unknown(mbuf);
|
---|
159 | }
|
---|
160 |
|
---|
161 | return NULL;
|
---|
162 | }
|
---|
163 |
|
---|
164 | static int32_t radegast_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *UNUSED(buf))
|
---|
165 | {
|
---|
166 | int32_t n;
|
---|
167 | uchar provid_buf[8];
|
---|
168 | uchar header[22] = "\x02\x01\x00\x06\x08\x30\x30\x30\x30\x30\x30\x30\x30\x07\x04\x30\x30\x30\x38\x08\x01\x02";
|
---|
169 | uchar *ecmbuf;
|
---|
170 | if (!cs_malloc(&ecmbuf, er->ecmlen + 30))
|
---|
171 | return -1;
|
---|
172 |
|
---|
173 | ecmbuf[0] = 1;
|
---|
174 | ecmbuf[1] = er->ecmlen + 30 - 2;
|
---|
175 | memcpy(ecmbuf + 2, header, sizeof(header));
|
---|
176 | for(n = 0; n < 4; n++) {
|
---|
177 | snprintf((char*)provid_buf+(n*2), sizeof(provid_buf)-(n*2), "%02X", ((uchar *)(&er->prid))[4 - 1 - n]);
|
---|
178 | }
|
---|
179 | ecmbuf[7] = provid_buf[0];
|
---|
180 | ecmbuf[8] = provid_buf[1];
|
---|
181 | ecmbuf[9] = provid_buf[2];
|
---|
182 | ecmbuf[10] = provid_buf[3];
|
---|
183 | ecmbuf[11] = provid_buf[4];
|
---|
184 | ecmbuf[12] = provid_buf[5];
|
---|
185 | ecmbuf[13] = provid_buf[6];
|
---|
186 | ecmbuf[14] = provid_buf[7];
|
---|
187 | ecmbuf[2 + sizeof(header)] = 0xa;
|
---|
188 | ecmbuf[3 + sizeof(header)] = 2;
|
---|
189 | ecmbuf[4 + sizeof(header)] = er->caid >> 8;
|
---|
190 | ecmbuf[5 + sizeof(header)] = er->caid & 0xff;
|
---|
191 | ecmbuf[6 + sizeof(header)] = 3;
|
---|
192 | ecmbuf[7 + sizeof(header)] = er->ecmlen;
|
---|
193 | memcpy(ecmbuf + 8 + sizeof(header), er->ecm, er->ecmlen);
|
---|
194 | ecmbuf[4] = er->caid >> 8;
|
---|
195 |
|
---|
196 | client->reader->msg_idx = er->idx;
|
---|
197 | n = send(client->pfd, ecmbuf, er->ecmlen + 30, 0);
|
---|
198 |
|
---|
199 | cs_debug_mask(D_TRACE,"radegast: sending ecm");
|
---|
200 | cs_ddump_mask(D_CLIENT, ecmbuf, er->ecmlen + 30, "ecm:");
|
---|
201 |
|
---|
202 | free(ecmbuf);
|
---|
203 |
|
---|
204 | return 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 | int32_t radegast_cli_init(struct s_client *cl)
|
---|
208 | {
|
---|
209 | int32_t handle;
|
---|
210 |
|
---|
211 | cs_log("radegast: proxy %s:%d (fd=%d)",
|
---|
212 | cl->reader->device, cl->reader->r_port, cl->udp_fd);
|
---|
213 |
|
---|
214 | handle = network_tcp_connection_open(cl->reader);
|
---|
215 | if(handle < 0) return -1;
|
---|
216 |
|
---|
217 | cl->reader->tcp_connected = 2;
|
---|
218 | cl->reader->card_status = CARD_INSERTED;
|
---|
219 | cl->reader->last_g = cl->reader->last_s = time((time_t *)0);
|
---|
220 |
|
---|
221 | cs_debug_mask(D_CLIENT, "radegast: last_s=%ld, last_g=%ld", cl->reader->last_s, cl->reader->last_g);
|
---|
222 |
|
---|
223 | cl->pfd=cl->udp_fd;
|
---|
224 |
|
---|
225 | return(0);
|
---|
226 | }
|
---|
227 |
|
---|
228 | static void radegast_server_init(struct s_client *cl) {
|
---|
229 | if (!cl->init_done) {
|
---|
230 | if (IP_ISSET(cl->ip))
|
---|
231 | cs_log("radegast: new connection from %s", cs_inet_ntoa(cl->ip));
|
---|
232 | radegast_auth_client(cur_client()->ip);
|
---|
233 | cl->init_done=1;
|
---|
234 | }
|
---|
235 | return;
|
---|
236 | }
|
---|
237 |
|
---|
238 | void module_radegast(struct s_module *ph)
|
---|
239 | {
|
---|
240 | ph->ptab.nports = 1;
|
---|
241 | ph->ptab.ports[0].s_port = cfg.rad_port;
|
---|
242 |
|
---|
243 | ph->desc="radegast";
|
---|
244 | ph->type=MOD_CONN_TCP;
|
---|
245 | ph->listenertype = LIS_RADEGAST;
|
---|
246 | IP_ASSIGN(ph->s_ip, cfg.rad_srvip);
|
---|
247 | ph->s_handler=radegast_server;
|
---|
248 | ph->s_init=radegast_server_init;
|
---|
249 | ph->recv=radegast_recv;
|
---|
250 | ph->send_dcw=radegast_send_dcw;
|
---|
251 | ph->c_init=radegast_cli_init;
|
---|
252 | ph->c_recv_chk=radegast_recv_chk;
|
---|
253 | ph->c_send_ecm=radegast_send_ecm;
|
---|
254 | ph->num=R_RADEGAST;
|
---|
255 | }
|
---|
256 | #endif
|
---|