1 | #define MODULE_LOG_PREFIX "camd33"
|
---|
2 |
|
---|
3 | #include "globals.h"
|
---|
4 | #ifdef MODULE_CAMD33
|
---|
5 | #include "oscam-aes.h"
|
---|
6 | #include "oscam-client.h"
|
---|
7 | #include "oscam-ecm.h"
|
---|
8 | #include "oscam-emm.h"
|
---|
9 | #include "oscam-net.h"
|
---|
10 | #include "oscam-string.h"
|
---|
11 |
|
---|
12 | #define REQ_SIZE 4
|
---|
13 |
|
---|
14 | static int32_t camd33_send(uint8_t *buf, int32_t ml)
|
---|
15 | {
|
---|
16 | int32_t l;
|
---|
17 |
|
---|
18 | if(!cur_client()->pfd)
|
---|
19 | {
|
---|
20 | return (-1);
|
---|
21 | }
|
---|
22 |
|
---|
23 | l = boundary(4, ml);
|
---|
24 | memset(buf + ml, 0, l - ml);
|
---|
25 | cs_log_dump_dbg(D_CLIENT, buf, l, "send %d bytes to client", l);
|
---|
26 |
|
---|
27 | if(cur_client()->crypted)
|
---|
28 | {
|
---|
29 | aes_encrypt_idx(cur_client()->aes_keys, buf, l);
|
---|
30 | }
|
---|
31 |
|
---|
32 | return (send(cur_client()->pfd, buf, l, 0));
|
---|
33 | }
|
---|
34 |
|
---|
35 | static int32_t camd33_recv(struct s_client *client, uint8_t *buf, int32_t l)
|
---|
36 | {
|
---|
37 | int32_t n;
|
---|
38 |
|
---|
39 | if(!client->pfd)
|
---|
40 | {
|
---|
41 | return (-1);
|
---|
42 | }
|
---|
43 |
|
---|
44 | if((n = cs_recv(client->pfd, buf, l, 0)) > 0)
|
---|
45 | {
|
---|
46 | client->last = time((time_t *)0);
|
---|
47 | if(client->crypted)
|
---|
48 | {
|
---|
49 | aes_encrypt_idx(cur_client()->aes_keys, buf, n);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | cs_log_dump_dbg(D_CLIENT, buf, n, "received %d bytes from client", n);
|
---|
53 |
|
---|
54 | return (n);
|
---|
55 | }
|
---|
56 |
|
---|
57 | static void camd33_request_emm(void)
|
---|
58 | {
|
---|
59 | uint8_t mbuf[20];
|
---|
60 | struct s_reader *aureader = NULL, *rdr = NULL;
|
---|
61 |
|
---|
62 | // TODO: just take the first reader in list
|
---|
63 | LL_ITER itr = ll_iter_create(cur_client()->aureader_list);
|
---|
64 | while((rdr = ll_iter_next(&itr)))
|
---|
65 | {
|
---|
66 | aureader = rdr;
|
---|
67 | break;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if(!aureader)
|
---|
71 | {
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if(aureader->hexserial[0])
|
---|
76 | {
|
---|
77 | cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06X)",
|
---|
78 | username(cur_client()), aureader->label, aureader->caid,
|
---|
79 | aureader->auprovid ? aureader->auprovid : b2i(4, aureader->prid[0]));
|
---|
80 |
|
---|
81 | mbuf[0] = 0;
|
---|
82 | mbuf[1] = aureader->caid >> 8;
|
---|
83 | mbuf[2] = aureader->caid & 0xff;
|
---|
84 |
|
---|
85 | memcpy(mbuf + 3, aureader->hexserial, 4);
|
---|
86 | memcpy(mbuf + 7, &aureader->prid[0][1], 3);
|
---|
87 | memcpy(mbuf + 10, &aureader->prid[2][1], 3);
|
---|
88 | camd33_send(mbuf, 13);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | static void camd33_auth_client(uint8_t *camdbug)
|
---|
93 | {
|
---|
94 | int32_t i, rc;
|
---|
95 | uint8_t *usr = NULL, *pwd = NULL;
|
---|
96 | struct s_auth *account;
|
---|
97 | uint8_t mbuf[1024];
|
---|
98 | struct s_client *cl = cur_client();
|
---|
99 |
|
---|
100 | cl->crypted = cfg.c33_crypted;
|
---|
101 | if(cl->crypted)
|
---|
102 | {
|
---|
103 | cl->crypted = !check_ip(cfg.c33_plain, cl->ip);
|
---|
104 | }
|
---|
105 |
|
---|
106 | if(cl->crypted)
|
---|
107 | {
|
---|
108 | if (!aes_set_key_alloc(&cl->aes_keys, (char *)cfg.c33_key))
|
---|
109 | {
|
---|
110 | cs_disconnect_client(cl);
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | mbuf[0] = 0;
|
---|
116 | camd33_send(mbuf, 1); // send login-request
|
---|
117 |
|
---|
118 | for(rc = 0, camdbug[0] = 0, mbuf[0] = 1; (rc < 2) && (mbuf[0]); rc++)
|
---|
119 | {
|
---|
120 | i = process_input(mbuf, sizeof(mbuf), 1);
|
---|
121 | if((i > 0) && (!mbuf[0]))
|
---|
122 | {
|
---|
123 | usr = mbuf + 1;
|
---|
124 | pwd = usr + cs_strlen((char *)usr) + 2;
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | memcpy(camdbug + 1, mbuf, camdbug[0] = i);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | for(rc = -1, account = cfg.account; (usr) && (account) && (rc < 0); account = account->next)
|
---|
133 | {
|
---|
134 | if(streq((char *)usr, account->usr) && streq((char *)pwd, account->pwd))
|
---|
135 | {
|
---|
136 | rc = cs_auth_client(cl, account, NULL);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | if(!rc)
|
---|
141 | {
|
---|
142 | camd33_request_emm();
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | if(rc < 0)
|
---|
147 | {
|
---|
148 | cs_auth_client(cl, 0, usr ? "invalid account" : "no user given");
|
---|
149 | }
|
---|
150 | cs_disconnect_client(cl);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | static void camd33_send_dcw(struct s_client *UNUSED(client), ECM_REQUEST *er)
|
---|
155 | {
|
---|
156 | uint8_t mbuf[128];
|
---|
157 |
|
---|
158 | mbuf[0] = 2;
|
---|
159 | memcpy(mbuf + 1, &er->msgid, 4); // get pin
|
---|
160 | memcpy(mbuf + 5, er->cw, 16);
|
---|
161 | camd33_send(mbuf, 21);
|
---|
162 |
|
---|
163 | if(!cfg.c33_passive)
|
---|
164 | {
|
---|
165 | camd33_request_emm();
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void camd33_process_ecm(uint8_t *buf, int32_t l)
|
---|
170 | {
|
---|
171 | ECM_REQUEST *er;
|
---|
172 |
|
---|
173 | if(l < 7)
|
---|
174 | {
|
---|
175 | return;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if(!(er = get_ecmtask()))
|
---|
179 | {
|
---|
180 | return;
|
---|
181 | }
|
---|
182 |
|
---|
183 | memcpy(&er->msgid, buf + 3, 4); // save pin
|
---|
184 | er->ecmlen = l - 7;
|
---|
185 |
|
---|
186 | if(er->ecmlen < 0 || er->ecmlen > MAX_ECM_SIZE)
|
---|
187 | {
|
---|
188 | NULLFREE(er);
|
---|
189 | return;
|
---|
190 | }
|
---|
191 |
|
---|
192 | er->caid = b2i(2, buf + 1);
|
---|
193 | memcpy(er->ecm , buf + 7, er->ecmlen);
|
---|
194 | get_cw(cur_client(), er);
|
---|
195 | }
|
---|
196 |
|
---|
197 | static void camd33_process_emm(uint8_t *buf, int32_t l)
|
---|
198 | {
|
---|
199 | EMM_PACKET epg;
|
---|
200 |
|
---|
201 | if(l < 7)
|
---|
202 | {
|
---|
203 | return;
|
---|
204 | }
|
---|
205 |
|
---|
206 | memset(&epg, 0, sizeof(epg));
|
---|
207 | epg.emmlen = l - 7;
|
---|
208 |
|
---|
209 | if(epg.emmlen < 3 || epg.emmlen > MAX_EMM_SIZE)
|
---|
210 | {
|
---|
211 | return;
|
---|
212 | }
|
---|
213 |
|
---|
214 | memcpy(epg.caid, buf + 1, 2);
|
---|
215 | memcpy(epg.hexserial, buf + 3, 4);
|
---|
216 | memcpy(epg.emm, buf + 7, epg.emmlen);
|
---|
217 | do_emm(cur_client(), &epg);
|
---|
218 | }
|
---|
219 |
|
---|
220 | static void *camd33_server(struct s_client *UNUSED(client), uint8_t *mbuf, int32_t n)
|
---|
221 | {
|
---|
222 | switch(mbuf[0])
|
---|
223 | {
|
---|
224 | case 2:
|
---|
225 | camd33_process_ecm(mbuf, n);
|
---|
226 | break;
|
---|
227 |
|
---|
228 | case 3:
|
---|
229 | camd33_process_emm(mbuf, n);
|
---|
230 | break;
|
---|
231 |
|
---|
232 | default:
|
---|
233 | cs_log_dbg(D_CLIENT, "unknown command!");
|
---|
234 | }
|
---|
235 |
|
---|
236 | return NULL;
|
---|
237 | }
|
---|
238 |
|
---|
239 | static void camd33_server_init(struct s_client *UNUSED(client))
|
---|
240 | {
|
---|
241 | uint8_t camdbug[256];
|
---|
242 |
|
---|
243 | camd33_auth_client(camdbug);
|
---|
244 | }
|
---|
245 |
|
---|
246 | void module_camd33(struct s_module *ph)
|
---|
247 | {
|
---|
248 | cfg.c33_crypted = array_has_nonzero_byte(cfg.c33_key, sizeof(cfg.c33_key));
|
---|
249 | ph->ptab.nports = 1;
|
---|
250 | ph->ptab.ports[0].s_port = cfg.c33_port;
|
---|
251 | ph->desc = "camd33";
|
---|
252 | ph->type = MOD_CONN_TCP;
|
---|
253 | ph->large_ecm_support = 1;
|
---|
254 | ph->listenertype = LIS_CAMD33TCP;
|
---|
255 | IP_ASSIGN(ph->s_ip, cfg.c33_srvip);
|
---|
256 | ph->s_handler = camd33_server;
|
---|
257 | ph->s_init = camd33_server_init;
|
---|
258 | ph->recv = camd33_recv;
|
---|
259 | ph->send_dcw = camd33_send_dcw;
|
---|
260 | ph->num = R_CAMD33;
|
---|
261 | }
|
---|
262 | #endif
|
---|