1 | #define MODULE_LOG_PREFIX "newcamd"
|
---|
2 |
|
---|
3 | #include "globals.h"
|
---|
4 | #ifdef MODULE_NEWCAMD
|
---|
5 | #include "cscrypt/des.h"
|
---|
6 | #include "cscrypt/md5.h"
|
---|
7 | #include "module-newcamd.h"
|
---|
8 | #include "oscam-conf-chk.h"
|
---|
9 | #include "oscam-chk.h"
|
---|
10 | #include "oscam-client.h"
|
---|
11 | #include "oscam-ecm.h"
|
---|
12 | #include "oscam-emm.h"
|
---|
13 | #include "oscam-net.h"
|
---|
14 | #include "oscam-reader.h"
|
---|
15 | #include "oscam-string.h"
|
---|
16 | #include "oscam-time.h"
|
---|
17 |
|
---|
18 | #define CWS_NETMSGSIZE 400 //csp 0.8.9 (default: 400). This is CWS_NETMSGSIZE. The old default was 240
|
---|
19 | #define NCD_CLIENT_ID 0x8888
|
---|
20 |
|
---|
21 | #define CWS_FIRSTCMDNO 0xe0
|
---|
22 | typedef enum
|
---|
23 | {
|
---|
24 | MSG_CLIENT_2_SERVER_LOGIN = CWS_FIRSTCMDNO,
|
---|
25 | MSG_CLIENT_2_SERVER_LOGIN_ACK,
|
---|
26 | MSG_CLIENT_2_SERVER_LOGIN_NAK,
|
---|
27 | MSG_CARD_DATA_REQ,
|
---|
28 | MSG_CARD_DATA,
|
---|
29 | MSG_SERVER_2_CLIENT_NAME,
|
---|
30 | MSG_SERVER_2_CLIENT_NAME_ACK,
|
---|
31 | MSG_SERVER_2_CLIENT_NAME_NAK,
|
---|
32 | MSG_SERVER_2_CLIENT_LOGIN,
|
---|
33 | MSG_SERVER_2_CLIENT_LOGIN_ACK,
|
---|
34 | MSG_SERVER_2_CLIENT_LOGIN_NAK,
|
---|
35 | MSG_ADMIN,
|
---|
36 | MSG_ADMIN_ACK,
|
---|
37 | MSG_ADMIN_LOGIN,
|
---|
38 | MSG_ADMIN_LOGIN_ACK,
|
---|
39 | MSG_ADMIN_LOGIN_NAK,
|
---|
40 | MSG_ADMIN_COMMAND,
|
---|
41 | MSG_ADMIN_COMMAND_ACK,
|
---|
42 | MSG_ADMIN_COMMAND_NAK,
|
---|
43 | MSG_KEEPALIVE = CWS_FIRSTCMDNO + 0x1d,
|
---|
44 | MSG_SERVER_2_CLIENT_OSD = 0xd1,
|
---|
45 | MSG_SERVER_2_CLIENT_ALLCARDS = 0xd2,
|
---|
46 | MSG_SERVER_2_CLIENT_ADDCARD = 0xd3,
|
---|
47 | MSG_SERVER_2_CLIENT_REMOVECARD = 0xd4,
|
---|
48 | MSG_SERVER_2_CLIENT_CHANGE_KEY = 0xd5,
|
---|
49 | MSG_SERVER_2_CLIENT_GET_VERSION = 0xd6,
|
---|
50 | MSG_SERVER_2_CLIENT_ADDSID = 0xd7,
|
---|
51 | MSG_CLIENT_2_SERVER_CARDDISCOVER = 0xd8
|
---|
52 | } net_msg_type_t;
|
---|
53 |
|
---|
54 | typedef enum
|
---|
55 | {
|
---|
56 | COMMTYPE_CLIENT,
|
---|
57 | COMMTYPE_SERVER
|
---|
58 | } comm_type_t;
|
---|
59 |
|
---|
60 |
|
---|
61 | typedef struct custom_data
|
---|
62 | {
|
---|
63 | uint16_t sid;
|
---|
64 | uint16_t caid;
|
---|
65 | int32_t provid;
|
---|
66 | uchar x;
|
---|
67 | } custom_data_t;
|
---|
68 |
|
---|
69 | #define REQ_SIZE 2
|
---|
70 |
|
---|
71 | static int32_t network_message_send(int32_t handle, uint16_t *netMsgId, uint8_t *buffer,
|
---|
72 | int32_t len, uint8_t *deskey, comm_type_t commType,
|
---|
73 | uint16_t sid, custom_data_t *cd)
|
---|
74 | {
|
---|
75 | uint8_t netbuf[CWS_NETMSGSIZE];
|
---|
76 | int32_t head_size;
|
---|
77 | struct s_client *cl = cur_client();
|
---|
78 |
|
---|
79 | head_size = (cl->ncd_proto == NCD_524) ? 8 : 12;
|
---|
80 |
|
---|
81 | if(len < 3 || len + head_size > CWS_NETMSGSIZE || handle < 0)
|
---|
82 | { return -1; }
|
---|
83 | buffer[1] = (buffer[1] & 0xf0) | (((len - 3) >> 8) & 0x0f);
|
---|
84 | buffer[2] = (len - 3) & 0xff;
|
---|
85 | memcpy(netbuf + head_size, buffer, len);
|
---|
86 | len += head_size;
|
---|
87 | if(netMsgId)
|
---|
88 | {
|
---|
89 | if(commType == COMMTYPE_CLIENT) { (*netMsgId)++; }
|
---|
90 | netbuf[2] = (*netMsgId) >> 8;
|
---|
91 | netbuf[3] = (*netMsgId) & 0xff;
|
---|
92 | }
|
---|
93 | else
|
---|
94 | { netbuf[2] = netbuf[3] = 0; }
|
---|
95 | memset(netbuf + 4, 0, (cl->ncd_proto == NCD_524) ? 4 : 8);
|
---|
96 | if(sid)
|
---|
97 | {
|
---|
98 | if(cl->reader && cl->reader->ncd_disable_server_filt &&
|
---|
99 | sid != NCD_CLIENT_ID && cl->ncd_proto != NCD_524) //mgclient send header
|
---|
100 | {
|
---|
101 | memcpy(netbuf + 4, cl->ncd_header + 4, 7);
|
---|
102 | }
|
---|
103 | netbuf[(cl->ncd_proto == NCD_524) ? 6 : 4] = (uchar)(sid >> 8); //sid
|
---|
104 | netbuf[(cl->ncd_proto == NCD_524) ? 7 : 5] = (uchar)(sid);
|
---|
105 | }
|
---|
106 | //if ((!ncd_proto==NCD_524) && (buffer[0] >= 0xd1) && (buffer[0]<= 0xd8)) { // extended proto for mg
|
---|
107 | //cs_log_dbg(D_CLIENT, "newcamd: extended: msg");
|
---|
108 | if(cd)
|
---|
109 | {
|
---|
110 | cs_log_dbg(D_CLIENT, "newcamd: has cd");
|
---|
111 | netbuf[4] = cd->sid >> 8;
|
---|
112 | netbuf[5] = cd->sid & 0xff;
|
---|
113 | netbuf[6] = cd->caid >> 8;
|
---|
114 | netbuf[7] = cd->caid & 0xff;
|
---|
115 | netbuf[8] = (cd->provid >> 16) & 0xFF;
|
---|
116 | netbuf[9] = (cd->provid >> 8) & 0xff;
|
---|
117 | netbuf[10] = cd->provid & 0xff;
|
---|
118 | }
|
---|
119 | //}
|
---|
120 | if(NCD_525 == cl->ncd_proto && cfg.ncd_mgclient &&
|
---|
121 | MSG_CLIENT_2_SERVER_LOGIN_ACK == buffer[0])
|
---|
122 | {
|
---|
123 | netbuf[4] = 0x6E; //From ExtNewcamSession.java CSP line 65-66 getLoginOkMsg()
|
---|
124 | netbuf[5] = 0x73;
|
---|
125 | netbuf[11] = 0x14;
|
---|
126 | }
|
---|
127 | if(NCD_525 == cl->ncd_proto &&
|
---|
128 | MSG_SERVER_2_CLIENT_ADDSID == buffer[0])
|
---|
129 | {
|
---|
130 | netbuf[11] = 0x14;
|
---|
131 | }
|
---|
132 | // if (buffer[0]==MSG_CLIENT_2_SERVER_LOGIN && cur_client()->reader->ncd_disable_server_filt) {
|
---|
133 | // netbuf[11] = 0x11; //From ChameleonCwsConnector.java CSP line 59-61 run()
|
---|
134 | // }
|
---|
135 |
|
---|
136 | netbuf[0] = (len - 2) >> 8;
|
---|
137 | netbuf[1] = (len - 2) & 0xff;
|
---|
138 | cs_log_dump_dbg(D_CLIENT, netbuf, len, "send %d bytes to %s", len, remote_txt());
|
---|
139 | if((len = des_encrypt(netbuf, len, deskey)) < 0)
|
---|
140 | { return -1; }
|
---|
141 | netbuf[0] = (len - 2) >> 8;
|
---|
142 | netbuf[1] = (len - 2) & 0xff;
|
---|
143 | return send(handle, netbuf, len, 0);
|
---|
144 | }
|
---|
145 |
|
---|
146 | static int32_t send_sid_list(void)
|
---|
147 | {
|
---|
148 | struct s_client *cl = cur_client();
|
---|
149 |
|
---|
150 | if(1 != cl->ftab.nfilts || !cl->sidtabs.no || !cfg.ncd_ptab.ports[cl->port_idx].ncd)
|
---|
151 | {
|
---|
152 | cs_log("SID list will not be send to mgcamd client.");
|
---|
153 | return 0;
|
---|
154 | }
|
---|
155 |
|
---|
156 | uchar mbuf[CWS_NETMSGSIZE];
|
---|
157 | int32_t n = 0, nr = 0, portion_sid_num = 0, i = 0, sid_num = 0,
|
---|
158 | portion_num = 0;
|
---|
159 | SIDTAB *sidtab = 0;
|
---|
160 | custom_data_t cd;
|
---|
161 |
|
---|
162 | cs_log_dbg(D_TRACE, "Send SID list to mgcamd client.");
|
---|
163 | memset(&cd, 0, sizeof(cd));
|
---|
164 | FILTER *pfilts = cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts;
|
---|
165 |
|
---|
166 | /*memset(mbuf, 0, sizeof(mbuf));*/ // not nessesery
|
---|
167 |
|
---|
168 | for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
|
---|
169 | if((cl->sidtabs.no & ((SIDTABBITS)1 << nr)) && (sidtab->num_caid | sidtab->num_provid | sidtab->num_srvid))
|
---|
170 | {
|
---|
171 | for(n = 0; n < pfilts[0].nprids; n++)
|
---|
172 | {
|
---|
173 | if(chk_srvid_match_by_caid_prov(pfilts[0].caid, pfilts[0].prids[n], sidtab))
|
---|
174 | {
|
---|
175 | for(i = 0; i < sidtab->num_srvid; i++)
|
---|
176 | {
|
---|
177 | // First SID goes to header
|
---|
178 | if(0 == portion_sid_num)
|
---|
179 | {
|
---|
180 | cd.sid = sidtab->srvid[i]; // first sid
|
---|
181 | cd.caid = cfg.ncd_ptab.ports[cl->port_idx].s_port; //assigned port
|
---|
182 | cd.provid = 0x1; // mark as deny
|
---|
183 | }
|
---|
184 | mbuf[portion_sid_num * 3] = (uchar)(sidtab->srvid[i] >> 8);
|
---|
185 | mbuf[portion_sid_num * 3 + 1] = (uchar)(sidtab->srvid[i] & 0xFF);
|
---|
186 | mbuf[portion_sid_num * 3 + 2] = 0x1; // mark as deny
|
---|
187 |
|
---|
188 | ++sid_num;
|
---|
189 | ++portion_sid_num;
|
---|
190 |
|
---|
191 | if(portion_sid_num >= 50)
|
---|
192 | {
|
---|
193 | ++portion_num;
|
---|
194 | cs_log_dump_dbg(0x0800, mbuf, (portion_sid_num) * 3, "Portion %d contains %d SIDs", portion_num, portion_sid_num);
|
---|
195 | mbuf[0] = MSG_SERVER_2_CLIENT_ADDSID;
|
---|
196 | mbuf[1] = 0x0;
|
---|
197 | mbuf[2] = 0x0;
|
---|
198 | network_message_send(cl->udp_fd, &cl->ncd_msgid,
|
---|
199 | mbuf, portion_sid_num * 3, cl->ncd_skey, COMMTYPE_SERVER, 0, &cd);
|
---|
200 | portion_sid_num = 0;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | break;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | if(portion_sid_num)
|
---|
210 | {
|
---|
211 | ++portion_num;
|
---|
212 | cs_log_dump_dbg(0x0800, mbuf, (portion_sid_num) * 3, "Portion %d contains %d SIDs", portion_num, portion_sid_num);
|
---|
213 | mbuf[0] = MSG_SERVER_2_CLIENT_ADDSID;
|
---|
214 | mbuf[1] = 0x0;
|
---|
215 | mbuf[2] = 0x0;
|
---|
216 | network_message_send(cl->udp_fd, &cl->ncd_msgid, mbuf, portion_sid_num * 3, cl->ncd_skey, COMMTYPE_SERVER, 0, &cd);
|
---|
217 | portion_sid_num = 0;
|
---|
218 | }
|
---|
219 |
|
---|
220 | cs_log("%d deny SIDs in the %d messages were sent to the client.", sid_num, portion_num);
|
---|
221 | return sid_num;
|
---|
222 | }
|
---|
223 |
|
---|
224 | static int32_t network_message_receive(int32_t handle, uint16_t *netMsgId, uint8_t *buffer,
|
---|
225 | uint8_t *deskey, comm_type_t commType)
|
---|
226 | {
|
---|
227 | int32_t len, ncd_off, msgid;
|
---|
228 | uint8_t netbuf[CWS_NETMSGSIZE];
|
---|
229 | int32_t returnLen;
|
---|
230 | struct s_client *cl = cur_client();
|
---|
231 |
|
---|
232 | if(!buffer || handle < 0)
|
---|
233 | { return -1; }
|
---|
234 | len = recv(handle, netbuf, 2, 0);
|
---|
235 | cs_log_dbg(D_CLIENT, "nmr(): len=%d, errno=%d", len, (len == -1) ? errno : 0);
|
---|
236 | if(!len)
|
---|
237 | {
|
---|
238 | cs_log_dbg(D_CLIENT, "nmr: 1 return 0");
|
---|
239 | if(commType == COMMTYPE_CLIENT)
|
---|
240 | { network_tcp_connection_close(cl->reader, "receive error1"); }
|
---|
241 | else
|
---|
242 | { cs_disconnect_client(cl); }
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 | if(len != 2)
|
---|
246 | {
|
---|
247 | cs_log_dbg(D_CLIENT, "nmr: len!=2");
|
---|
248 | if(commType == COMMTYPE_CLIENT)
|
---|
249 | { network_tcp_connection_close(cl->reader, "receive error2"); }
|
---|
250 | else
|
---|
251 | { cs_disconnect_client(cl); }
|
---|
252 | return -1;
|
---|
253 | }
|
---|
254 | if(((netbuf[0] << 8) | netbuf[1]) > CWS_NETMSGSIZE - 2)
|
---|
255 | {
|
---|
256 | cs_log_dbg(D_CLIENT, "nmr: received data len=%d lonage than CWS_NETMSGSIZE=%d", ((netbuf[0] << 8) | netbuf[1]), CWS_NETMSGSIZE);
|
---|
257 | cs_log_dbg(D_CLIENT, "nmr: 1 return -1");
|
---|
258 | return -1;
|
---|
259 | }
|
---|
260 |
|
---|
261 | len = recv(handle, netbuf + 2, (netbuf[0] << 8) | netbuf[1], 0);
|
---|
262 | if(!len)
|
---|
263 | {
|
---|
264 | cs_log_dbg(D_CLIENT, "nmr: 2 return 0");
|
---|
265 | return 0;
|
---|
266 | }
|
---|
267 | if(len != ((netbuf[0] << 8) | netbuf[1]))
|
---|
268 | {
|
---|
269 | cs_log_dbg(D_CLIENT, "nmr: 2 return -1");
|
---|
270 | return -1;
|
---|
271 | }
|
---|
272 | len += 2;
|
---|
273 | if((len = des_decrypt(netbuf, len, deskey)) < 11) // 15(newcamd525) or 11 ???
|
---|
274 | {
|
---|
275 | cs_log_dbg(D_CLIENT, "nmr: can't decrypt, invalid des key?");
|
---|
276 | cs_sleepms(2000);
|
---|
277 | return -1;
|
---|
278 | }
|
---|
279 | //cs_log_dump_dbg(D_CLIENT, netbuf, len, "nmr: decrypted data, len=%d", len);
|
---|
280 | msgid = (netbuf[2] << 8) | netbuf[3];
|
---|
281 |
|
---|
282 | if(cl->ncd_proto == NCD_AUTO)
|
---|
283 | {
|
---|
284 | // auto detect
|
---|
285 | int32_t l5 = (((netbuf[13] & 0x0f) << 8) | netbuf[14]) + 3;
|
---|
286 | int32_t l4 = (((netbuf[9] & 0x0f) << 8) | netbuf[10]) + 3;
|
---|
287 |
|
---|
288 | if((l5 <= len - 12) && ((netbuf[12] & 0xF0) == 0xE0 || (netbuf[12] & 0xF0) == 0x80))
|
---|
289 | { cl->ncd_proto = NCD_525; }
|
---|
290 | else if((l4 <= len - 8) && ((netbuf[8] & 0xF0) == 0xE0 || (netbuf[9] & 0xF0) == 0x80))
|
---|
291 | { cl->ncd_proto = NCD_524; }
|
---|
292 | else
|
---|
293 | {
|
---|
294 | cs_log_dbg(D_CLIENT, "nmr: 4 return -1");
|
---|
295 | return -1;
|
---|
296 | }
|
---|
297 |
|
---|
298 | cs_log_dbg(D_CLIENT, "nmr: autodetect: newcamd52%d used", (cl->ncd_proto == NCD_525) ? 5 : 4);
|
---|
299 | }
|
---|
300 |
|
---|
301 | ncd_off = (cl->ncd_proto == NCD_525) ? 4 : 0;
|
---|
302 |
|
---|
303 | returnLen = (((netbuf[9 + ncd_off] & 0x0f) << 8) | netbuf[10 + ncd_off]) + 3;
|
---|
304 | if(returnLen > (len - (8 + ncd_off)))
|
---|
305 | {
|
---|
306 | cs_log_dbg(D_CLIENT, "nmr: 4 return -1");
|
---|
307 | return -1;
|
---|
308 | }
|
---|
309 |
|
---|
310 | //cs_log_dump_dbg(D_CLIENT, netbuf, len, "nmr: decrypted data");
|
---|
311 | if(netMsgId)
|
---|
312 | {
|
---|
313 | switch(commType)
|
---|
314 | {
|
---|
315 | case COMMTYPE_SERVER:
|
---|
316 | *netMsgId = msgid;
|
---|
317 | break;
|
---|
318 |
|
---|
319 | case COMMTYPE_CLIENT:
|
---|
320 | //if (*netMsgId != ((netbuf[2] << 8) | netbuf[3])) {
|
---|
321 | cs_log_dbg(D_CLIENT, "nmr: netMsgId=%d, from server=%d, ", *netMsgId, msgid);
|
---|
322 | //return -2;
|
---|
323 | //}
|
---|
324 | break;
|
---|
325 |
|
---|
326 | default:
|
---|
327 | cs_log_dbg(D_CLIENT, "nmr: 5 return -1");
|
---|
328 | return -1;
|
---|
329 | break;
|
---|
330 | }
|
---|
331 | }
|
---|
332 | switch(commType)
|
---|
333 | {
|
---|
334 | case COMMTYPE_SERVER:
|
---|
335 | memcpy(cl->ncd_header, netbuf, (8 + ncd_off));
|
---|
336 | buffer[0] = (cl->ncd_proto == NCD_525) ? netbuf[4] : netbuf[6]; // sid
|
---|
337 | buffer[1] = (cl->ncd_proto == NCD_525) ? netbuf[5] : netbuf[7];
|
---|
338 | break;
|
---|
339 | case COMMTYPE_CLIENT:
|
---|
340 | memcpy(cl->ncd_header, netbuf, (8 + ncd_off));
|
---|
341 | buffer[0] = netbuf[2]; // msgid
|
---|
342 | buffer[1] = netbuf[3];
|
---|
343 | break;
|
---|
344 | }
|
---|
345 |
|
---|
346 | memcpy(buffer + 2, netbuf + (8 + ncd_off), returnLen);
|
---|
347 | return returnLen + 2;
|
---|
348 | }
|
---|
349 |
|
---|
350 | static void network_cmd_no_data_send(int32_t handle, uint16_t *netMsgId,
|
---|
351 | net_msg_type_t cmd, uint8_t *deskey,
|
---|
352 | comm_type_t commType)
|
---|
353 | {
|
---|
354 | uint8_t buffer[3];
|
---|
355 |
|
---|
356 | buffer[0] = cmd;
|
---|
357 | buffer[1] = 0;
|
---|
358 | buffer[2] = 0;
|
---|
359 | network_message_send(handle, netMsgId, buffer, 3, deskey, commType, 0, NULL);
|
---|
360 | }
|
---|
361 |
|
---|
362 | static int32_t network_cmd_no_data_receive(int32_t handle, uint16_t *netMsgId,
|
---|
363 | uint8_t *deskey, comm_type_t commType)
|
---|
364 | {
|
---|
365 | uint8_t buffer[CWS_NETMSGSIZE];
|
---|
366 |
|
---|
367 | if(network_message_receive(handle, netMsgId, buffer, deskey, commType) != 3 + 2)
|
---|
368 | { return -1; }
|
---|
369 | return buffer[2];
|
---|
370 | }
|
---|
371 |
|
---|
372 | void newcamd_reply_ka(void)
|
---|
373 | {
|
---|
374 | struct s_client *cl = cur_client();
|
---|
375 |
|
---|
376 | if(!cl) { return; }
|
---|
377 |
|
---|
378 | if(!cl->udp_fd)
|
---|
379 | {
|
---|
380 | cs_log_dbg(D_CLIENT, "invalid client fd=%d", cl->udp_fd);
|
---|
381 | return;
|
---|
382 | }
|
---|
383 |
|
---|
384 | cs_log_dbg(D_CLIENT, "send keepalive to client fd=%d", cl->udp_fd);
|
---|
385 |
|
---|
386 | if(cl->reader)
|
---|
387 | { cl->reader->last_s = time((time_t *)0); }
|
---|
388 |
|
---|
389 | network_cmd_no_data_send(cl->udp_fd, &cl->ncd_msgid, MSG_KEEPALIVE, cl->ncd_skey, COMMTYPE_SERVER);
|
---|
390 | }
|
---|
391 |
|
---|
392 | static int32_t connect_newcamd_server(void)
|
---|
393 | {
|
---|
394 | int32_t i;
|
---|
395 | uint8_t buf[CWS_NETMSGSIZE];
|
---|
396 | uint8_t keymod[14];
|
---|
397 | uint8_t key[16];
|
---|
398 | int32_t handle = 0;
|
---|
399 |
|
---|
400 | uint32_t idx;
|
---|
401 | uchar passwdcrypt[120];
|
---|
402 | uint8_t login_answer;
|
---|
403 | int32_t bytes_received;
|
---|
404 | struct s_client *cl = cur_client();
|
---|
405 |
|
---|
406 | if(cl->reader->device[0] == 0 || cl->reader->r_pwd[0] == 0 ||
|
---|
407 | cl->reader->r_usr[0] == 0 || cl->reader->r_port == 0)
|
---|
408 | { return -5; }
|
---|
409 |
|
---|
410 | // 1. Connect
|
---|
411 | handle = network_tcp_connection_open(cl->reader);
|
---|
412 | if(handle < 0) { return -1; }
|
---|
413 |
|
---|
414 | // 2. Get init sequence
|
---|
415 | cl->ncd_msgid = 0;
|
---|
416 | if(read(handle, keymod, sizeof(keymod)) != sizeof(keymod))
|
---|
417 | {
|
---|
418 | cs_log("server does not return 14 bytes");
|
---|
419 | network_tcp_connection_close(cl->reader, "connect error");
|
---|
420 | return -2;
|
---|
421 | }
|
---|
422 | cs_log_dump_dbg(D_CLIENT, keymod, sizeof(cl->reader->ncd_key), "server init sequence:");
|
---|
423 | des_login_key_get(keymod, cl->reader->ncd_key, sizeof(cl->reader->ncd_key), key);
|
---|
424 |
|
---|
425 | // 3. Send login info
|
---|
426 | idx = 3;
|
---|
427 | buf[0] = MSG_CLIENT_2_SERVER_LOGIN;
|
---|
428 | buf[1] = 0;
|
---|
429 | cs_strncpy((char *)buf + idx, cl->reader->r_usr, sizeof(buf) - idx);
|
---|
430 | __md5_crypt(cl->reader->r_pwd, "$1$abcdefgh$", (char *)passwdcrypt);
|
---|
431 | idx += strlen(cl->reader->r_usr) + 1;
|
---|
432 | cs_strncpy((char *)buf + idx, (const char *)passwdcrypt, sizeof(buf) - idx);
|
---|
433 |
|
---|
434 | network_message_send(handle, 0, buf, idx + strlen((char *)passwdcrypt) + 1, key,
|
---|
435 | COMMTYPE_CLIENT, NCD_CLIENT_ID, NULL);
|
---|
436 |
|
---|
437 | // 3.1 Get login answer
|
---|
438 | login_answer = network_cmd_no_data_receive(handle, &cl->ncd_msgid,
|
---|
439 | key, COMMTYPE_CLIENT);
|
---|
440 | if(login_answer == MSG_CLIENT_2_SERVER_LOGIN_NAK)
|
---|
441 | {
|
---|
442 | cs_log("login failed for user '%s'", cl->reader->r_usr);
|
---|
443 | network_tcp_connection_close(cl->reader, "login error1");
|
---|
444 | return -3;
|
---|
445 | }
|
---|
446 | if(login_answer != MSG_CLIENT_2_SERVER_LOGIN_ACK)
|
---|
447 | {
|
---|
448 | cs_log("expected MSG_CLIENT_2_SERVER_LOGIN_ACK (%02X), received %02X",
|
---|
449 | MSG_CLIENT_2_SERVER_LOGIN_ACK, login_answer);
|
---|
450 | network_tcp_connection_close(cl->reader, "login error2");
|
---|
451 | return -3;
|
---|
452 | }
|
---|
453 |
|
---|
454 | // 3.2 Set connection info
|
---|
455 | cl->reader->tcp_connected = 1;
|
---|
456 | cl->crypted = 1;
|
---|
457 |
|
---|
458 | // 4. Send MSG_CARD_DATE_REQ
|
---|
459 | des_login_key_get(cl->reader->ncd_key, passwdcrypt, strlen((char *)passwdcrypt), key);
|
---|
460 |
|
---|
461 | network_cmd_no_data_send(handle, &cl->ncd_msgid, MSG_CARD_DATA_REQ,
|
---|
462 | key, COMMTYPE_CLIENT);
|
---|
463 | bytes_received = network_message_receive(handle, &cl->ncd_msgid, buf,
|
---|
464 | key, COMMTYPE_CLIENT);
|
---|
465 | if(bytes_received < 16 || buf[2] != MSG_CARD_DATA)
|
---|
466 | {
|
---|
467 | cs_log("expected MSG_CARD_DATA (%02X), received %02X",
|
---|
468 | MSG_CARD_DATA, buf[2]);
|
---|
469 | network_tcp_connection_close(cl->reader, "receive error");
|
---|
470 | return -4;
|
---|
471 | }
|
---|
472 |
|
---|
473 | // 5. Parse CAID and PROVID(s)
|
---|
474 | cl->reader->caid = (uint16_t)((buf[6] << 8) | buf[7]);
|
---|
475 |
|
---|
476 | /* handle special serial format in newcamd. See newcamd_auth_client */
|
---|
477 | newcamd_to_hexserial(buf + 10, cl->reader->hexserial, cl->reader->caid);
|
---|
478 | cs_log("Newcamd Server: %s:%d - UserID: %i", cl->reader->device, cl->reader->r_port, buf[3 + 2]);
|
---|
479 | cs_log("CAID: %04X - UA: %02X%02X%02X%02X%02X%02X%02X%02X - Provider # %i", cl->reader->caid, cl->reader->hexserial[0], cl->reader->hexserial[1], cl->reader->hexserial[2], cl->reader->hexserial[3], cl->reader->hexserial[4], cl->reader->hexserial[5], cl->reader->hexserial[6], cl->reader->hexserial[7], buf[14 + 2]);
|
---|
480 | cl->reader->nprov = buf[14 + 2];
|
---|
481 | memset(cl->reader->prid, 0x00, sizeof(cl->reader->prid));
|
---|
482 | for(i = 0; i < cl->reader->nprov; i++)
|
---|
483 | {
|
---|
484 | if(((cl->reader->caid >> 8) == 0x17) ||
|
---|
485 | ((cl->reader->caid >> 8) == 0x06)) //Betacrypt or Irdeto
|
---|
486 | {
|
---|
487 | memcpy(&cl->reader->prid[i], buf + 22 + 2 + 11 * i, 4);
|
---|
488 | }
|
---|
489 | else
|
---|
490 | {
|
---|
491 | cl->reader->prid[i][1] = buf[15 + 2 + 11 * i];
|
---|
492 | cl->reader->prid[i][2] = buf[16 + 2 + 11 * i];
|
---|
493 | cl->reader->prid[i][3] = buf[17 + 2 + 11 * i];
|
---|
494 | }
|
---|
495 | memcpy(&cl->reader->sa[i], buf + 22 + 2 + 11 * i, 4); // the 4 first bytes are not read
|
---|
496 | cs_log("Provider ID: %02X%02X%02X - SA: %02X%02X%02X%02X", cl->reader->prid[i][1], cl->reader->prid[i][2], cl->reader->prid[i][3], cl->reader->sa[i][0], cl->reader->sa[i][1], cl->reader->sa[i][2], cl->reader->sa[i][3]);
|
---|
497 | }
|
---|
498 | memcpy(cl->reader->ncd_skey, key, 16);
|
---|
499 |
|
---|
500 | // 6. Set card inserted
|
---|
501 | cl->reader->tcp_connected = 2;
|
---|
502 | cl->reader->card_status = CARD_INSERTED;
|
---|
503 | cl->reader->last_g = cl->reader->last_s = time((time_t *)0);
|
---|
504 |
|
---|
505 | // Only after connect() on cl->udp_fd (Linux)
|
---|
506 | cl->pfd = cl->udp_fd;
|
---|
507 |
|
---|
508 | if(cl->reader->ncd_disable_server_filt) //act like mgclient
|
---|
509 | {
|
---|
510 | network_cmd_no_data_send(handle, &cl->ncd_msgid, MSG_SERVER_2_CLIENT_GET_VERSION,
|
---|
511 | key, COMMTYPE_CLIENT);
|
---|
512 | }
|
---|
513 |
|
---|
514 | return 0;
|
---|
515 | }
|
---|
516 |
|
---|
517 | static int32_t newcamd_connect(void)
|
---|
518 | {
|
---|
519 | struct s_client *cl = cur_client();
|
---|
520 |
|
---|
521 | if(cl->reader->tcp_connected < 2 && connect_newcamd_server() < 0)
|
---|
522 | { return 0; }
|
---|
523 |
|
---|
524 | if(!cl->udp_fd)
|
---|
525 | { return 0; }
|
---|
526 |
|
---|
527 | return 1;
|
---|
528 | }
|
---|
529 |
|
---|
530 |
|
---|
531 | static int32_t newcamd_send(uchar *buf, int32_t ml, uint16_t sid)
|
---|
532 | {
|
---|
533 | struct s_client *cl = cur_client();
|
---|
534 |
|
---|
535 | if(!newcamd_connect())
|
---|
536 | { return (-1); }
|
---|
537 |
|
---|
538 | return (network_message_send(cl->udp_fd, &cl->ncd_msgid,
|
---|
539 | buf, ml, cl->reader->ncd_skey, COMMTYPE_CLIENT, sid, NULL));
|
---|
540 | }
|
---|
541 |
|
---|
542 | static int32_t newcamd_recv(struct s_client *client, uchar *buf, int32_t UNUSED(l))
|
---|
543 | {
|
---|
544 | int32_t rc, rs;
|
---|
545 |
|
---|
546 | if(client->typ == 'c')
|
---|
547 | {
|
---|
548 | rs = network_message_receive(client->udp_fd,
|
---|
549 | &client->ncd_msgid, buf,
|
---|
550 | client->ncd_skey, COMMTYPE_SERVER);
|
---|
551 | }
|
---|
552 | else
|
---|
553 | {
|
---|
554 | if(!client->udp_fd) { return (-1); }
|
---|
555 | rs = network_message_receive(client->udp_fd,
|
---|
556 | &client->ncd_msgid, buf,
|
---|
557 | client->reader->ncd_skey, COMMTYPE_CLIENT);
|
---|
558 | }
|
---|
559 |
|
---|
560 | if(rs < 5) { rc = (-1); }
|
---|
561 | else { rc = rs; }
|
---|
562 |
|
---|
563 | cs_log_dump_dbg(D_CLIENT, buf, rs, "received %d bytes from %s", rs, remote_txt());
|
---|
564 | client->last = time((time_t *) 0);
|
---|
565 |
|
---|
566 | if(rc == -1)
|
---|
567 | {
|
---|
568 | if(rs > 0)
|
---|
569 | { cs_log("packet is too small (%d bytes)", rs); }
|
---|
570 | else
|
---|
571 | { cs_log("Connection closed to %s", remote_txt()); }
|
---|
572 | }
|
---|
573 | return (rc);
|
---|
574 | }
|
---|
575 |
|
---|
576 | static void mk_user_au_ftab(struct s_reader *aureader, FILTER *filt)
|
---|
577 | {
|
---|
578 | int32_t i, j, found;
|
---|
579 | struct s_client *cl = cur_client();
|
---|
580 | FILTER client_filter;
|
---|
581 | FILTER *pufilt = &client_filter;
|
---|
582 |
|
---|
583 | memset(filt, 0, sizeof(*filt));
|
---|
584 | memset(&client_filter, 0, sizeof(client_filter));
|
---|
585 |
|
---|
586 | if(cl->ftab.filts) client_filter = cl->ftab.filts[0];
|
---|
587 |
|
---|
588 | filt->caid = aureader->caid;
|
---|
589 | if(filt->caid == 0)
|
---|
590 | filt->caid = client_filter.caid;
|
---|
591 |
|
---|
592 | for(i = 0; i < aureader->nprov; i++)
|
---|
593 | { filt->prids[filt->nprids++] = b2i(3, &aureader->prid[i][1]); }
|
---|
594 |
|
---|
595 | for(i = 0; i < pufilt->nprids; i++)
|
---|
596 | {
|
---|
597 | for(j = found = 0; (!found) && (j < filt->nprids); j++)
|
---|
598 | if(pufilt->prids[i] == filt->prids[j]) { found = 1; }
|
---|
599 | if(!found)
|
---|
600 | { filt->prids[filt->nprids++] = pufilt->prids[i]; }
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | static void mk_user_ftab(FILTER *filt)
|
---|
605 | {
|
---|
606 | int32_t port_idx, i, j, k, c;
|
---|
607 | struct s_client *cl = cur_client();
|
---|
608 |
|
---|
609 | memset(filt, 0, sizeof(*filt));
|
---|
610 |
|
---|
611 | port_idx = cl->port_idx;
|
---|
612 | if(!cfg.ncd_ptab.ports[port_idx].ncd)
|
---|
613 | return;
|
---|
614 | FILTER *psfilt = &cfg.ncd_ptab.ports[port_idx].ncd->ncd_ftab.filts[0];
|
---|
615 |
|
---|
616 | // 1. CAID
|
---|
617 | // search server CAID in client CAID
|
---|
618 | for(c = i = 0; i < CS_MAXCAIDTAB; i++)
|
---|
619 | {
|
---|
620 | int32_t ctab_caid;
|
---|
621 | ctab_caid = cl->ctab.caid[i] & cl->ctab.mask[i];
|
---|
622 | if(ctab_caid) { c++; }
|
---|
623 |
|
---|
624 | if(psfilt->caid == ctab_caid)
|
---|
625 | {
|
---|
626 | filt->caid = ctab_caid;
|
---|
627 | break;
|
---|
628 | }
|
---|
629 | }
|
---|
630 | if(c && !filt->caid)
|
---|
631 | {
|
---|
632 | cs_log("no valid CAID found in CAID for user '%s'", cl->account->usr);
|
---|
633 | return;
|
---|
634 | }
|
---|
635 |
|
---|
636 | // search CAID in client IDENT
|
---|
637 | cs_log_dbg(D_CLIENT, "client[%8lX].%s nfilts=%d, filt.caid=%04X", (unsigned long)pthread_self(),
|
---|
638 | cl->account->usr, cl->ftab.nfilts, filt->caid);
|
---|
639 |
|
---|
640 | if(!filt->caid && cl->ftab.filts)
|
---|
641 | {
|
---|
642 | int32_t fcaids;
|
---|
643 | for(i = fcaids = 0; i < cl->ftab.nfilts; i++)
|
---|
644 | {
|
---|
645 | uint16_t ucaid = cl->ftab.filts[i].caid;
|
---|
646 | if(ucaid) { fcaids++; }
|
---|
647 | if(ucaid && psfilt->caid == ucaid)
|
---|
648 | {
|
---|
649 | filt->caid = ucaid;
|
---|
650 | break;
|
---|
651 | }
|
---|
652 | }
|
---|
653 | if(fcaids == cl->ftab.nfilts && !filt->caid)
|
---|
654 | {
|
---|
655 | cs_log("no valid CAID found in IDENT for user '%s'", cl->account->usr);
|
---|
656 | //cs_disconnect_client();
|
---|
657 | return;
|
---|
658 | }
|
---|
659 | }
|
---|
660 | // empty client CAID - use server CAID
|
---|
661 | if(!filt->caid) { filt->caid = psfilt->caid; }
|
---|
662 |
|
---|
663 | // 2. PROVID
|
---|
664 | if(!cl->ftab.nfilts)
|
---|
665 | {
|
---|
666 | int32_t add;
|
---|
667 | for(i = 0; i < psfilt->nprids; i++)
|
---|
668 | {
|
---|
669 | // use server PROVID(s) (and only those which are in user's groups)
|
---|
670 | add = 0;
|
---|
671 | struct s_reader *rdr;
|
---|
672 | for(rdr = first_active_reader; rdr ; rdr = rdr->next)
|
---|
673 | if(rdr->grp & cl->grp)
|
---|
674 | {
|
---|
675 | if(!rdr->ftab.nfilts)
|
---|
676 | {
|
---|
677 | if(is_network_reader(rdr)) { add = 1; }
|
---|
678 | for(j = 0; !add && j < rdr->nprov; j++)
|
---|
679 | if(b2i(3, &rdr->prid[j][1]) == psfilt->prids[i]) { add = 1; }
|
---|
680 | }
|
---|
681 | else
|
---|
682 | {
|
---|
683 | for(j = 0; !add && j < rdr->ftab.nfilts; j++)
|
---|
684 | {
|
---|
685 | uint32_t rcaid = rdr->ftab.filts[j].caid;
|
---|
686 | if(!rcaid || rcaid == filt->caid)
|
---|
687 | {
|
---|
688 | for(k = 0; !add && k < rdr->ftab.filts[j].nprids; k++)
|
---|
689 | if(rdr->ftab.filts[j].prids[k] == psfilt->prids[i]) { add = 1; }
|
---|
690 | }
|
---|
691 | }
|
---|
692 | }
|
---|
693 | }
|
---|
694 | if(add) { filt->prids[filt->nprids++] = psfilt->prids[i]; }
|
---|
695 | }
|
---|
696 | memcpy(filt, psfilt, sizeof(*filt));
|
---|
697 | return;
|
---|
698 | }
|
---|
699 |
|
---|
700 | // search in client IDENT
|
---|
701 | for(j = 0; j < cl->ftab.nfilts; j++)
|
---|
702 | {
|
---|
703 | uint32_t ucaid = cl->ftab.filts[j].caid;
|
---|
704 | cs_log_dbg(D_CLIENT, "client caid #%d: %04X", j, ucaid);
|
---|
705 | if(!ucaid || ucaid == filt->caid)
|
---|
706 | {
|
---|
707 | for(i = 0; i < psfilt->nprids; i++)
|
---|
708 | {
|
---|
709 | cs_log_dbg(D_CLIENT, "search server provid #%d: %06X", i, psfilt->prids[i]);
|
---|
710 | if(cl->ftab.filts[j].nprids)
|
---|
711 | {
|
---|
712 | for(k = 0; k < cl->ftab.filts[j].nprids; k++)
|
---|
713 | if(cl->ftab.filts[j].prids[k] == psfilt->prids[i])
|
---|
714 | { filt->prids[filt->nprids++] = cl->ftab.filts[j].prids[k]; }
|
---|
715 | }
|
---|
716 | else
|
---|
717 | {
|
---|
718 | filt->prids[filt->nprids++] = psfilt->prids[i];
|
---|
719 | // allow server PROVID(s) if no PROVID(s) specified in IDENT
|
---|
720 | }
|
---|
721 | }
|
---|
722 | }
|
---|
723 | }
|
---|
724 |
|
---|
725 | if(!filt->nprids)
|
---|
726 | {
|
---|
727 | cs_log("no valid PROVID(s) found in CAID for user '%s'", cl->account->usr);
|
---|
728 | //cs_disconnect_client();
|
---|
729 | }
|
---|
730 | }
|
---|
731 |
|
---|
732 | static int8_t newcamd_auth_client(IN_ADDR_T ip, uint8_t *deskey)
|
---|
733 | {
|
---|
734 | int32_t i, ok, rc, sid_list;
|
---|
735 | uchar *usr = NULL, *pwd = NULL;
|
---|
736 | struct s_auth *account;
|
---|
737 | uchar buf[14];
|
---|
738 | uchar key[16];
|
---|
739 | uchar passwdcrypt[120];
|
---|
740 | struct s_reader *aureader = NULL, *rdr = NULL;
|
---|
741 | struct s_client *cl = cur_client();
|
---|
742 | uchar mbuf[CWS_NETMSGSIZE];
|
---|
743 |
|
---|
744 | sid_list = 0;
|
---|
745 |
|
---|
746 | ok = cfg.ncd_allowed ? check_ip(cfg.ncd_allowed, ip) : 1;
|
---|
747 |
|
---|
748 | if(!ok)
|
---|
749 | {
|
---|
750 | cs_auth_client(cl, (struct s_auth *)0, NULL);
|
---|
751 | return -1;
|
---|
752 | }
|
---|
753 |
|
---|
754 | // make random 14 bytes
|
---|
755 | get_random_bytes(buf, 14);
|
---|
756 |
|
---|
757 | // send init sequence
|
---|
758 | send(cl->udp_fd, buf, 14, 0);
|
---|
759 | des_login_key_get(buf, deskey, 14, key);
|
---|
760 | memcpy(cl->ncd_skey, key, 16);
|
---|
761 | cl->ncd_msgid = 0;
|
---|
762 |
|
---|
763 | i = process_input(mbuf, sizeof(mbuf), cfg.cmaxidle);
|
---|
764 | if(i > 0)
|
---|
765 | {
|
---|
766 | if(mbuf[2] != MSG_CLIENT_2_SERVER_LOGIN)
|
---|
767 | {
|
---|
768 | cs_log_dbg(D_CLIENT, "expected MSG_CLIENT_2_SERVER_LOGIN (%02X), received %02X",
|
---|
769 | MSG_CLIENT_2_SERVER_LOGIN, mbuf[2]);
|
---|
770 | return -1;
|
---|
771 | }
|
---|
772 | usr = mbuf + 5;
|
---|
773 | pwd = usr + strlen((char *)usr) + 1;
|
---|
774 | }
|
---|
775 | else
|
---|
776 | {
|
---|
777 | cs_log_dbg(D_CLIENT, "bad client login request");
|
---|
778 | return -1;
|
---|
779 | }
|
---|
780 |
|
---|
781 | cl->ncd_client_id = (mbuf[0] << 8) | mbuf[1];
|
---|
782 | const char *client_name = newcamd_get_client_name(cl->ncd_client_id);
|
---|
783 | #if defined(TCP_KEEPIDLE)
|
---|
784 | if(cl->ncd_client_id == 0x4453) // DiabloWifi has problems with TCPKeepAlive
|
---|
785 | {
|
---|
786 | int32_t flag = 600;
|
---|
787 | if(setsockopt(cl->udp_fd, IPPROTO_TCP, TCP_KEEPIDLE, &flag, sizeof(flag)) && errno != EBADF) //send first keepalive packet after 600 seconds of last package received (keepalive packets included)
|
---|
788 | {
|
---|
789 | cs_log("Setting TCP_KEEPIDLE failed, errno=%d, %s", errno, strerror(errno));
|
---|
790 | }
|
---|
791 | else { cs_log("WARNING: Setting TCP_KEEPIDLE to 10 minutes for bugged DiabloWifi. Note that this might lead to not detected broken connections or multiple connections."); }
|
---|
792 | }
|
---|
793 | #endif
|
---|
794 |
|
---|
795 | if(cl->ncd_proto == NCD_525 && 0x6D == mbuf[0]
|
---|
796 | && 0x67 == mbuf[1] && 0x11 == cl->ncd_header[11])
|
---|
797 | {
|
---|
798 | sid_list = 1;
|
---|
799 | }
|
---|
800 |
|
---|
801 | for(ok = 0, account = cfg.account; (usr) && (account) && (!ok); account = account->next)
|
---|
802 | {
|
---|
803 | cs_log_dbg(D_CLIENT, "account->usr=%s", account->usr);
|
---|
804 | if(strcmp((char *)usr, account->usr) == 0)
|
---|
805 | {
|
---|
806 | __md5_crypt(ESTR(account->pwd), "$1$abcdefgh$", (char *)passwdcrypt);
|
---|
807 | cs_log_dbg(D_CLIENT, "account->pwd=%s", passwdcrypt);
|
---|
808 | if(strcmp((char *)pwd, (const char *)passwdcrypt) == 0)
|
---|
809 | {
|
---|
810 | cl->crypted = 1;
|
---|
811 | char e_txt[20];
|
---|
812 | snprintf(e_txt, 20, "%s:%d", "newcamd", cfg.ncd_ptab.ports[cl->port_idx].s_port);
|
---|
813 | if((rc = cs_auth_client(cl, account, e_txt)) == 2)
|
---|
814 | {
|
---|
815 | cs_log("hostname or ip mismatch for user %s (%s)", usr, client_name);
|
---|
816 | break;
|
---|
817 | }
|
---|
818 | else if(rc != 0)
|
---|
819 | {
|
---|
820 | cs_log("account is invalid for user %s (%s)", usr, client_name);
|
---|
821 | break;
|
---|
822 | }
|
---|
823 | else
|
---|
824 | {
|
---|
825 | cs_log("user %s authenticated successfully (%s)", usr, client_name);
|
---|
826 | ok = 1;
|
---|
827 | break;
|
---|
828 | }
|
---|
829 | }
|
---|
830 | else
|
---|
831 | { cs_log("user %s is providing a wrong password (%s)", usr, client_name); }
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | if(!ok && !account)
|
---|
836 | {
|
---|
837 | cs_log("user %s is trying to connect but doesnt exist ! (%s)", usr, client_name);
|
---|
838 | usr = 0;
|
---|
839 | }
|
---|
840 |
|
---|
841 | // check for non ready reader and reject client
|
---|
842 | for(rdr = first_active_reader; rdr ; rdr = rdr->next)
|
---|
843 | {
|
---|
844 | if(!cfg.ncd_ptab.ports[cl->port_idx].ncd)
|
---|
845 | { continue; }
|
---|
846 | if(rdr->caid == cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].caid)
|
---|
847 | {
|
---|
848 | if(rdr->card_status == CARD_NEED_INIT)
|
---|
849 | {
|
---|
850 | cs_log("init for reader %s not finished -> reject client", rdr->label);
|
---|
851 | ok = 0;
|
---|
852 | }
|
---|
853 | break;
|
---|
854 | }
|
---|
855 | }
|
---|
856 |
|
---|
857 | if(ok)
|
---|
858 | {
|
---|
859 | LL_ITER itr = ll_iter_create(cl->aureader_list);
|
---|
860 | while((rdr = ll_iter_next(&itr)))
|
---|
861 | {
|
---|
862 | int32_t n;
|
---|
863 | if(!cfg.ncd_ptab.ports[cl->port_idx].ncd)
|
---|
864 | { continue; }
|
---|
865 | for(n = 0; n < cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].nprids; n++)
|
---|
866 | {
|
---|
867 | if(emm_reader_match(rdr, cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].caid, cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].prids[n]))
|
---|
868 | {
|
---|
869 | aureader = rdr;
|
---|
870 | break;
|
---|
871 | }
|
---|
872 | }
|
---|
873 | if(aureader)
|
---|
874 | { break; }
|
---|
875 | }
|
---|
876 |
|
---|
877 | if(aureader)
|
---|
878 | {
|
---|
879 | cs_log("AU enabled for user %s on reader %s", usr, aureader->label);
|
---|
880 | }
|
---|
881 | else
|
---|
882 | {
|
---|
883 | cs_log("AU disabled for user %s", usr);
|
---|
884 | }
|
---|
885 | }
|
---|
886 |
|
---|
887 | network_cmd_no_data_send(cl->udp_fd, &cl->ncd_msgid,
|
---|
888 | (ok) ? MSG_CLIENT_2_SERVER_LOGIN_ACK : MSG_CLIENT_2_SERVER_LOGIN_NAK,
|
---|
889 | cl->ncd_skey, COMMTYPE_SERVER);
|
---|
890 |
|
---|
891 | if(ok)
|
---|
892 | {
|
---|
893 | FILTER usr_filter;
|
---|
894 | FILTER *pufilt = &usr_filter;
|
---|
895 |
|
---|
896 | des_login_key_get(deskey, passwdcrypt, strlen((char *)passwdcrypt), key);
|
---|
897 | memcpy(cl->ncd_skey, key, 16);
|
---|
898 |
|
---|
899 | i = process_input(mbuf, sizeof(mbuf), cfg.cmaxidle);
|
---|
900 | if(i > 0)
|
---|
901 | {
|
---|
902 | int32_t j, len = 15;
|
---|
903 | if(mbuf[2] != MSG_CARD_DATA_REQ)
|
---|
904 | {
|
---|
905 | cs_log_dbg(D_CLIENT, "expected MSG_CARD_DATA_REQ (%02X), received %02X",
|
---|
906 | MSG_CARD_DATA_REQ, mbuf[2]);
|
---|
907 | return -1;
|
---|
908 | }
|
---|
909 |
|
---|
910 | // set userfilter for au enabled clients
|
---|
911 | if(aureader)
|
---|
912 | mk_user_au_ftab(aureader, &usr_filter);
|
---|
913 | else
|
---|
914 | mk_user_ftab(&usr_filter);
|
---|
915 |
|
---|
916 | if(!cfg.ncd_mgclient)
|
---|
917 | ftab_add(&cl->ftab, &usr_filter); //We cannot filter all cards!
|
---|
918 |
|
---|
919 | mbuf[0] = MSG_CARD_DATA;
|
---|
920 | mbuf[1] = 0x00;
|
---|
921 | mbuf[2] = 0x00;
|
---|
922 |
|
---|
923 | if(aureader)
|
---|
924 | { mbuf[3] = 1; }
|
---|
925 | else
|
---|
926 | { mbuf[3] = get_threadnum(cl) + 10; } // Unique user number
|
---|
927 |
|
---|
928 | mbuf[4] = (uchar)(pufilt->caid >> 8);
|
---|
929 | mbuf[5] = (uchar)(pufilt->caid);
|
---|
930 | mbuf[6] = 0x00;
|
---|
931 | mbuf[7] = 0x00;
|
---|
932 |
|
---|
933 | if(aureader)
|
---|
934 | { hexserial_to_newcamd(aureader->hexserial, mbuf + 8, pufilt->caid); }
|
---|
935 | else
|
---|
936 | { memset(&mbuf[8], 0, 6); } //mbuf[8] - mbuf[13]
|
---|
937 |
|
---|
938 | mbuf[14] = pufilt->nprids;
|
---|
939 | for(j = 0; j < pufilt->nprids; j++)
|
---|
940 | {
|
---|
941 | if(((pufilt->caid >> 8) == 0x17) || ((pufilt->caid >> 8) == 0x06)) // Betacrypt or Irdeto
|
---|
942 | {
|
---|
943 | mbuf[15 + 11 * j] = 0;
|
---|
944 | mbuf[16 + 11 * j] = 0;
|
---|
945 | mbuf[17 + 11 * j] = j;
|
---|
946 | }
|
---|
947 | else
|
---|
948 | {
|
---|
949 | mbuf[15 + 11 * j] = (uchar)(pufilt->prids[j] >> 16);
|
---|
950 | mbuf[16 + 11 * j] = (uchar)(pufilt->prids[j] >> 8);
|
---|
951 | mbuf[17 + 11 * j] = (uchar)(pufilt->prids[j]);
|
---|
952 | }
|
---|
953 | mbuf[18 + 11 * j] = 0x00;
|
---|
954 | mbuf[19 + 11 * j] = 0x00;
|
---|
955 | mbuf[20 + 11 * j] = 0x00;
|
---|
956 | mbuf[21 + 11 * j] = 0x00;
|
---|
957 | if(aureader)
|
---|
958 | {
|
---|
959 | // check if user provid from IDENT exists on card
|
---|
960 | int32_t k, found;
|
---|
961 | uint32_t rprid;
|
---|
962 | found = 0;
|
---|
963 | if(pufilt->caid == aureader->caid)
|
---|
964 | {
|
---|
965 | for(k = 0; (k < aureader->nprov); k++)
|
---|
966 | {
|
---|
967 | rprid = b2i(3, &aureader->prid[k][1]);
|
---|
968 | if(rprid == pufilt->prids[j])
|
---|
969 | {
|
---|
970 | if(((pufilt->caid >> 8) == 0x17) || ((pufilt->caid >> 8) == 0x06)) // Betacrypt or Irdeto
|
---|
971 | {
|
---|
972 | mbuf[22 + 11 * j] = aureader->prid[k][0];
|
---|
973 | mbuf[23 + 11 * j] = aureader->prid[k][1];
|
---|
974 | mbuf[24 + 11 * j] = aureader->prid[k][2];
|
---|
975 | mbuf[25 + 11 * j] = aureader->prid[k][3];
|
---|
976 | }
|
---|
977 | else
|
---|
978 | {
|
---|
979 | mbuf[22 + 11 * j] = aureader->sa[k][0];
|
---|
980 | mbuf[23 + 11 * j] = aureader->sa[k][1];
|
---|
981 | mbuf[24 + 11 * j] = aureader->sa[k][2];
|
---|
982 | mbuf[25 + 11 * j] = aureader->sa[k][3];
|
---|
983 | }
|
---|
984 | found = 1;
|
---|
985 | break;
|
---|
986 | }
|
---|
987 | }
|
---|
988 | }
|
---|
989 | if(!found)
|
---|
990 | {
|
---|
991 | mbuf[22 + 11 * j] = 0x00;
|
---|
992 | mbuf[23 + 11 * j] = 0x00;
|
---|
993 | mbuf[24 + 11 * j] = 0x00;
|
---|
994 | mbuf[25 + 11 * j] = 0x00;
|
---|
995 | }
|
---|
996 | }
|
---|
997 | else
|
---|
998 | {
|
---|
999 | if(((pufilt->caid >> 8) == 0x17) || ((pufilt->caid >> 8) == 0x06)) // Betacrypt or Irdeto
|
---|
1000 | {
|
---|
1001 | mbuf[22 + 11 * j] = 0x00;
|
---|
1002 | mbuf[23 + 11 * j] = (uchar)(pufilt->prids[j] >> 16);
|
---|
1003 | mbuf[24 + 11 * j] = (uchar)(pufilt->prids[j] >> 8);
|
---|
1004 | mbuf[25 + 11 * j] = (uchar)(pufilt->prids[j]);
|
---|
1005 | }
|
---|
1006 | else
|
---|
1007 | {
|
---|
1008 | mbuf[22 + 11 * j] = 0x00;
|
---|
1009 | mbuf[23 + 11 * j] = 0x00;
|
---|
1010 | mbuf[24 + 11 * j] = 0x00;
|
---|
1011 | mbuf[25 + 11 * j] = 0x00;
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 | len += 11;
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | custom_data_t cd;
|
---|
1018 | memset(&cd, 0, sizeof(cd));
|
---|
1019 |
|
---|
1020 | if(aureader)
|
---|
1021 | {
|
---|
1022 | if(aureader->blockemm & EMM_GLOBAL && !(aureader->saveemm & EMM_GLOBAL))
|
---|
1023 | { cd.sid |= 4; }
|
---|
1024 | if(aureader->blockemm & EMM_SHARED && !(aureader->saveemm & EMM_SHARED))
|
---|
1025 | { cd.sid |= 2; }
|
---|
1026 | if(aureader->blockemm & EMM_UNIQUE && !(aureader->saveemm & EMM_UNIQUE))
|
---|
1027 | { cd.sid |= 1; }
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | if(network_message_send(cl->udp_fd, &cl->ncd_msgid,
|
---|
1031 | mbuf, len, key, COMMTYPE_SERVER, 0, &cd) < 0)
|
---|
1032 | {
|
---|
1033 | return -1;
|
---|
1034 | }
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | // send SID list
|
---|
1038 | if(sid_list)
|
---|
1039 | { send_sid_list(); }
|
---|
1040 | }
|
---|
1041 | else
|
---|
1042 | {
|
---|
1043 | cs_auth_client(cl, 0, usr ? "login failure" : "no such user");
|
---|
1044 | return -1;
|
---|
1045 | }
|
---|
1046 | return 0;
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | static void newcamd_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
1050 | {
|
---|
1051 | int32_t len;
|
---|
1052 | uint16_t cl_msgid;
|
---|
1053 | uchar mbuf[19];
|
---|
1054 |
|
---|
1055 | if(!client->udp_fd)
|
---|
1056 | {
|
---|
1057 | cs_log_dbg(D_CLIENT, "ncd_send_dcw: error: client->udp_fd=%d", client->udp_fd);
|
---|
1058 | return;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | cl_msgid = er->msgid;
|
---|
1062 | mbuf[0] = er->ecm[0];
|
---|
1063 | if(client->ftab.filts && (client->ftab.filts[0].nprids == 0 || er->rc >= E_NOTFOUND /*not found*/))
|
---|
1064 | {
|
---|
1065 | len = 3;
|
---|
1066 | mbuf[1] = mbuf[2] = 0x00;
|
---|
1067 | }
|
---|
1068 | else
|
---|
1069 | {
|
---|
1070 | len = 19;
|
---|
1071 | mbuf[1] = mbuf[2] = 0x10;
|
---|
1072 | memcpy(mbuf + 3, er->cw, 16);
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | cs_log_dbg(D_CLIENT, "ncd_send_dcw: er->msgid=%d, cl_msgid=%d, %02X", er->msgid, cl_msgid, mbuf[0]);
|
---|
1076 |
|
---|
1077 | network_message_send(client->udp_fd, &cl_msgid, mbuf, len,
|
---|
1078 | client->ncd_skey, COMMTYPE_SERVER, 0, NULL);
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | static void newcamd_process_ecm(struct s_client *cl, uchar *buf, int32_t len)
|
---|
1082 | {
|
---|
1083 | int32_t pi;
|
---|
1084 | ECM_REQUEST *er;
|
---|
1085 |
|
---|
1086 | if(!(er = get_ecmtask()))
|
---|
1087 | {
|
---|
1088 | return;
|
---|
1089 | }
|
---|
1090 | // save client ncd_msgid
|
---|
1091 | er->msgid = cl->ncd_msgid;
|
---|
1092 | er->ecmlen = (((buf[3] & 0x0F) << 8) | buf[4]) + 3;
|
---|
1093 | cs_log_dbg(D_CLIENT, "ncd_process_ecm: er->msgid=%d len=%d ecmlen=%d", er->msgid, len, er->ecmlen);
|
---|
1094 | er->srvid = cl->ncd_header[4] << 8 | cl->ncd_header[5];
|
---|
1095 | er->caid = cl->ncd_header[6] << 8 | cl->ncd_header[7];
|
---|
1096 | er->prid = cl->ncd_header[8] << 16 | cl->ncd_header[9] << 8 | cl->ncd_header[10];
|
---|
1097 | if(!er->caid)
|
---|
1098 | {
|
---|
1099 | pi = cl->port_idx;
|
---|
1100 | if(cfg.ncd_ptab.nports && cfg.ncd_ptab.nports >= pi && cfg.ncd_ptab.ports[pi].ncd)
|
---|
1101 | { er->caid = cfg.ncd_ptab.ports[pi].ncd->ncd_ftab.filts[0].caid; }
|
---|
1102 | }
|
---|
1103 | memcpy(er->ecm, buf + 2, er->ecmlen);
|
---|
1104 | get_cw(cl, er);
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | static void newcamd_process_emm(uchar *buf)
|
---|
1108 | {
|
---|
1109 | int32_t ok = 1;
|
---|
1110 | uint16_t caid = 0;
|
---|
1111 | struct s_client *cl = cur_client();
|
---|
1112 | EMM_PACKET epg;
|
---|
1113 |
|
---|
1114 | memset(&epg, 0, sizeof(epg));
|
---|
1115 |
|
---|
1116 | epg.emmlen = buf[2] + 3;
|
---|
1117 | if (cl->ftab.filts)
|
---|
1118 | caid = cl->ftab.filts[0].caid;
|
---|
1119 | epg.caid[0] = (uchar)(caid >> 8);
|
---|
1120 | epg.caid[1] = (uchar)(caid);
|
---|
1121 |
|
---|
1122 | /*
|
---|
1123 | epg.provid[0] = (uchar)(aureader->auprovid>>24);
|
---|
1124 | epg.provid[1] = (uchar)(aureader->auprovid>>16);
|
---|
1125 | epg.provid[2] = (uchar)(aureader->auprovid>>8);
|
---|
1126 | epg.provid[3] = (uchar)(aureader->auprovid);
|
---|
1127 | */
|
---|
1128 | /* if (caid == 0x0500)
|
---|
1129 | {
|
---|
1130 | uint16_t emm_head;
|
---|
1131 |
|
---|
1132 | emm_head = (buf[0]<<8) | buf[1];
|
---|
1133 | switch( emm_head )
|
---|
1134 | {
|
---|
1135 | case 0x8e70: // EMM-S
|
---|
1136 | memcpy(epg.hexserial+1, buf+3, 4);
|
---|
1137 | epg.hexserial[4]=aureader->hexserial[4];
|
---|
1138 | break;
|
---|
1139 | case 0x8870: // EMM-U
|
---|
1140 | case 0x8c70: // confidential ?
|
---|
1141 | default:
|
---|
1142 | cs_log("unsupported emm type: %04X", emm_head);
|
---|
1143 | ok=0;
|
---|
1144 | }
|
---|
1145 | if( !ok ) cs_log("only EMM-S supported");
|
---|
1146 | }
|
---|
1147 | else*/
|
---|
1148 |
|
---|
1149 | memcpy(epg.emm, buf, epg.emmlen);
|
---|
1150 | if(ok)
|
---|
1151 | { do_emm(cl, &epg); }
|
---|
1152 |
|
---|
1153 | // Should always send an answer to client (also if au is disabled),
|
---|
1154 | // some clients will disconnect if they get no answer
|
---|
1155 | buf[1] = 0x10;
|
---|
1156 | buf[2] = 0x00;
|
---|
1157 | network_message_send(cl->udp_fd, &cl->ncd_msgid, buf, 3,
|
---|
1158 | cl->ncd_skey, COMMTYPE_SERVER, 0, NULL);
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | static void newcamd_report_cards(struct s_client *client)
|
---|
1162 | {
|
---|
1163 | int32_t j, k, l;
|
---|
1164 | uint8_t buf[512];
|
---|
1165 | custom_data_t *cd;
|
---|
1166 | if(!cs_malloc(&cd, sizeof(struct custom_data)))
|
---|
1167 | { return; }
|
---|
1168 | memset(buf, 0, sizeof(buf));
|
---|
1169 |
|
---|
1170 | cd->sid = cfg.ncd_ptab.ports[client->port_idx].s_port;
|
---|
1171 |
|
---|
1172 | buf[0] = MSG_SERVER_2_CLIENT_ADDCARD;
|
---|
1173 | struct s_reader *rdr;
|
---|
1174 | for(rdr = first_active_reader; rdr ; rdr = rdr->next)
|
---|
1175 | {
|
---|
1176 | int32_t flt = 0;
|
---|
1177 | if(!(rdr->grp & client->grp)) { continue; } //test - skip unaccesible readers
|
---|
1178 | if(rdr->ftab.filts)
|
---|
1179 | {
|
---|
1180 | for(j = 0; j < rdr->ftab.nfilts; j++)
|
---|
1181 | {
|
---|
1182 | if(rdr->ftab.filts[j].caid)
|
---|
1183 | {
|
---|
1184 | cd->caid = rdr->ftab.filts[j].caid;
|
---|
1185 | if(!rdr->ftab.filts[j].nprids)
|
---|
1186 | {
|
---|
1187 | cd->provid = 0;
|
---|
1188 | cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X:%06X svc", cd->caid, cd->provid);
|
---|
1189 | network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
|
---|
1190 | }
|
---|
1191 | for(k = 0; k < rdr->ftab.filts[j].nprids; k++)
|
---|
1192 | {
|
---|
1193 | cd->provid = rdr->ftab.filts[j].prids[k];
|
---|
1194 | cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X:%06X svc", cd->caid, cd->provid);
|
---|
1195 | network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
|
---|
1196 | flt = 1;
|
---|
1197 | }
|
---|
1198 | }
|
---|
1199 | }
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | if(rdr->caid && !flt)
|
---|
1203 | {
|
---|
1204 | if((rdr->tcp_connected || rdr->card_status == CARD_INSERTED))
|
---|
1205 | {
|
---|
1206 | cd->caid = rdr->caid;
|
---|
1207 | if(!rdr->nprov)
|
---|
1208 | {
|
---|
1209 | cd->provid = 0;
|
---|
1210 | cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X:%06X caid", cd->caid, cd->provid);
|
---|
1211 | network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
|
---|
1212 | }
|
---|
1213 | for(j = 0; j < rdr->nprov; j++)
|
---|
1214 | {
|
---|
1215 | cd->provid = (rdr->prid[j][1]) << 16 | (rdr->prid[j][2] << 8) | rdr->prid[j][3];
|
---|
1216 | cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X:%06X caid", cd->caid, cd->provid);
|
---|
1217 | network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 | }
|
---|
1222 | if(cfg.sidtab && client->account)
|
---|
1223 | {
|
---|
1224 | struct s_sidtab *ptr;
|
---|
1225 | for(j = 0, ptr = cfg.sidtab; ptr; ptr = ptr->next, j++)
|
---|
1226 | {
|
---|
1227 | if(client->account->sidtabs.ok & ((SIDTABBITS)1 << j))
|
---|
1228 | for(k = 0; k < ptr->num_caid; k++)
|
---|
1229 | {
|
---|
1230 | cd->caid = ptr->caid[k];
|
---|
1231 | if(!ptr->num_provid)
|
---|
1232 | {
|
---|
1233 | cd->provid = 0;
|
---|
1234 | cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X:%06X acs", cd->caid, cd->provid);
|
---|
1235 | network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
|
---|
1236 | }
|
---|
1237 | for(l = 0; l < ptr->num_provid; l++)
|
---|
1238 | {
|
---|
1239 | cd->provid = ptr->provid[l];
|
---|
1240 | cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X:%06X acs", cd->caid, cd->provid);
|
---|
1241 | network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
|
---|
1242 | }
|
---|
1243 | }
|
---|
1244 | }
|
---|
1245 | }
|
---|
1246 | NULLFREE(cd);
|
---|
1247 |
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | static void newcamd_server_init(struct s_client *client)
|
---|
1251 | {
|
---|
1252 | int8_t res = 0;
|
---|
1253 |
|
---|
1254 | client->ncd_server = 1;
|
---|
1255 | cs_log("client connected to %d port", cfg.ncd_ptab.ports[client->port_idx].s_port);
|
---|
1256 |
|
---|
1257 | if(cfg.ncd_ptab.ports[client->port_idx].ncd && cfg.ncd_ptab.ports[client->port_idx].ncd->ncd_key_is_set)
|
---|
1258 | {
|
---|
1259 | //port has a des key specified
|
---|
1260 | res = newcamd_auth_client(client->ip, cfg.ncd_ptab.ports[client->port_idx].ncd->ncd_key);
|
---|
1261 | }
|
---|
1262 | else
|
---|
1263 | {
|
---|
1264 | //default global des key
|
---|
1265 | res = newcamd_auth_client(client->ip, cfg.ncd_key);
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | if(res == -1)
|
---|
1269 | {
|
---|
1270 | cs_disconnect_client(client);
|
---|
1271 | return;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | // report all cards if using extended mg proto
|
---|
1275 | if(cfg.ncd_mgclient)
|
---|
1276 | {
|
---|
1277 | cs_log_dbg(D_CLIENT, "newcamd: extended: report all available cards");
|
---|
1278 | newcamd_report_cards(client);
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | #define EXT_VERSION_STR "1.67"
|
---|
1284 | #define EXT_VERSION_LEN 4
|
---|
1285 |
|
---|
1286 | static void newcamd_send_version(struct s_client *client)
|
---|
1287 | {
|
---|
1288 | uchar buf[30];
|
---|
1289 | memset(buf, 0, sizeof(buf));
|
---|
1290 | buf[0] = MSG_SERVER_2_CLIENT_GET_VERSION;
|
---|
1291 | buf[1] = EXT_VERSION_LEN >> 8;
|
---|
1292 | buf[2] = EXT_VERSION_LEN & 0xFF;
|
---|
1293 | memcpy(buf + 3, EXT_VERSION_STR, EXT_VERSION_LEN);
|
---|
1294 | network_message_send(client->udp_fd, &client->ncd_msgid, buf, EXT_VERSION_LEN + 3, client->ncd_skey, COMMTYPE_SERVER, 0, NULL);
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | static void *newcamd_server(struct s_client *client, uchar *mbuf, int32_t len)
|
---|
1298 | {
|
---|
1299 | // check for clienttimeout, if timeout occurs try to send keepalive / wait for answer
|
---|
1300 | // befor client was disconnected. If keepalive was disabled, exit after clienttimeout
|
---|
1301 |
|
---|
1302 | if(len < 3)
|
---|
1303 | { return NULL; }
|
---|
1304 |
|
---|
1305 | cs_log_dbg(D_CLIENT, "newcamd: got cmd %d", mbuf[2]);
|
---|
1306 |
|
---|
1307 | switch(mbuf[2])
|
---|
1308 | {
|
---|
1309 | case 0x80:
|
---|
1310 | case 0x81:
|
---|
1311 | newcamd_process_ecm(client, mbuf, len);
|
---|
1312 | break;
|
---|
1313 |
|
---|
1314 | case MSG_SERVER_2_CLIENT_GET_VERSION:
|
---|
1315 | cs_log_dbg(D_CLIENT, "newcamd: extended: send Version 1.67");
|
---|
1316 | newcamd_send_version(client);
|
---|
1317 | break;
|
---|
1318 |
|
---|
1319 | case MSG_KEEPALIVE:
|
---|
1320 | newcamd_reply_ka();
|
---|
1321 | break;
|
---|
1322 |
|
---|
1323 | default:
|
---|
1324 | if(mbuf[2] > 0x81 && mbuf[2] < 0x90)
|
---|
1325 | { newcamd_process_emm(mbuf + 2); }
|
---|
1326 | else
|
---|
1327 | {
|
---|
1328 | cs_log_dbg(D_CLIENT, "unknown newcamd command! (%d)", mbuf[2]);
|
---|
1329 | }
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | return NULL;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | void newcamd_idle(void)
|
---|
1336 | {
|
---|
1337 | struct s_client *client = cur_client();
|
---|
1338 | struct s_reader *rdr = client->reader;
|
---|
1339 |
|
---|
1340 | if(!rdr) { return; }
|
---|
1341 |
|
---|
1342 | if(rdr->tcp_ito > 0)
|
---|
1343 | {
|
---|
1344 | // inactivitytimeout > 0 enables protocol keepalive packages
|
---|
1345 | time_t now;
|
---|
1346 | int32_t time_diff;
|
---|
1347 | time(&now);
|
---|
1348 | time_diff = abs(now - rdr->last_s);
|
---|
1349 | if(time_diff > (rdr->tcp_ito))
|
---|
1350 | {
|
---|
1351 | if(client->ncd_keepalive)
|
---|
1352 | { newcamd_reply_ka(); }
|
---|
1353 | else
|
---|
1354 | { network_tcp_connection_close(client->reader, "inactivity"); }
|
---|
1355 | }
|
---|
1356 | }
|
---|
1357 | else if(rdr->tcp_ito == -1)
|
---|
1358 | {
|
---|
1359 | // idle reconnect
|
---|
1360 | newcamd_connect();
|
---|
1361 | }
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | /*
|
---|
1365 | * client functions
|
---|
1366 | */
|
---|
1367 |
|
---|
1368 | int32_t newcamd_client_init(struct s_client *client)
|
---|
1369 | {
|
---|
1370 |
|
---|
1371 | char ptxt[1] = { "\0" };
|
---|
1372 |
|
---|
1373 | client->ncd_proto = client->reader->ncd_proto;
|
---|
1374 |
|
---|
1375 | cs_log("proxy %s:%d newcamd52%d (fd=%d%s)",
|
---|
1376 | client->reader->device, client->reader->r_port,
|
---|
1377 | (client->reader->ncd_proto == NCD_525) ? 5 : 4, client->udp_fd, ptxt);
|
---|
1378 |
|
---|
1379 | // try to connect. ignore possible failures
|
---|
1380 | // idle reconnect (tcp_ito = -1) will trigger an additional connect anyway
|
---|
1381 | if(client->reader->ncd_connect_on_init && client->reader->tcp_ito != -1)
|
---|
1382 | { newcamd_connect(); }
|
---|
1383 |
|
---|
1384 | return (0);
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | static int32_t newcamd_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *buf)
|
---|
1388 | {
|
---|
1389 | struct s_reader *rdr = client->reader;
|
---|
1390 |
|
---|
1391 | if(!newcamd_connect())
|
---|
1392 | { return (-1); }
|
---|
1393 |
|
---|
1394 | // check server filters
|
---|
1395 | if(!chk_rsfilter(rdr, er))
|
---|
1396 | { return (-1); }
|
---|
1397 |
|
---|
1398 | memcpy(buf, er->ecm, er->ecmlen);
|
---|
1399 |
|
---|
1400 | client->ncd_header[4] = er->srvid >> 8;
|
---|
1401 | client->ncd_header[5] = er->srvid & 0xFF;
|
---|
1402 | client->ncd_header[6] = er->caid >> 8;
|
---|
1403 | client->ncd_header[7] = er->caid & 0xFF;
|
---|
1404 | client->ncd_header[8] = er->prid >> 16;
|
---|
1405 | client->ncd_header[9] = er->prid >> 8;
|
---|
1406 | client->ncd_header[10] = er->prid & 0xFF;
|
---|
1407 |
|
---|
1408 | return ((newcamd_send(buf, er->ecmlen, er->srvid) < 1) ? (-1) : 0);
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 |
|
---|
1412 | static int32_t newcamd_send_emm(EMM_PACKET *ep)
|
---|
1413 | {
|
---|
1414 | uchar buf[ep->emmlen];
|
---|
1415 |
|
---|
1416 | if(!newcamd_connect())
|
---|
1417 | { return (-1); }
|
---|
1418 |
|
---|
1419 | memcpy(buf, ep->emm, ep->emmlen);
|
---|
1420 | return ((newcamd_send(buf, ep->emmlen, 0) < 1) ? 0 : 1);
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | static int32_t newcamd_recv_chk(struct s_client *client, uchar *dcw, int32_t *rc, uchar *buf, int32_t n)
|
---|
1424 | {
|
---|
1425 | uint16_t idx = -1;
|
---|
1426 |
|
---|
1427 | if(n < 5)
|
---|
1428 | { return -1; }
|
---|
1429 |
|
---|
1430 | switch(buf[2])
|
---|
1431 | {
|
---|
1432 | case 0x80:
|
---|
1433 | case 0x81:
|
---|
1434 | idx = (buf[0] << 8) | buf[1];
|
---|
1435 | if(n == 5) //not found on server
|
---|
1436 | {
|
---|
1437 | *rc = 0;
|
---|
1438 | memset(dcw, 0, 16);
|
---|
1439 | break;
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 | if(n < 21)
|
---|
1443 | {
|
---|
1444 | cs_log_dbg(D_CLIENT, "invalid newcamd answer");
|
---|
1445 | return (-1);
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | *rc = 1;
|
---|
1449 | memcpy(dcw, buf + 5, 16);
|
---|
1450 | break;
|
---|
1451 |
|
---|
1452 | case MSG_KEEPALIVE:
|
---|
1453 | return -1;
|
---|
1454 |
|
---|
1455 | case MSG_SERVER_2_CLIENT_ADDCARD:
|
---|
1456 | if(client->reader)
|
---|
1457 | {
|
---|
1458 | client->reader->ncd_disable_server_filt = 1;
|
---|
1459 | }
|
---|
1460 | return -1;
|
---|
1461 |
|
---|
1462 | default:
|
---|
1463 | if(buf[2] > 0x81 && buf[2] < 0x90) //answer to emm
|
---|
1464 | {
|
---|
1465 | return -1;
|
---|
1466 | }
|
---|
1467 | cs_log_dbg(D_CLIENT, "unknown newcamd command from server");
|
---|
1468 | return -1;
|
---|
1469 | }
|
---|
1470 | return (idx);
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 | /*
|
---|
1474 | * resolve client type for newcamd protocol
|
---|
1475 | */
|
---|
1476 | const char *newcamd_get_client_name(uint16_t client_id)
|
---|
1477 | {
|
---|
1478 | // When adding new entries keep the list sorted!
|
---|
1479 | static const struct
|
---|
1480 | {
|
---|
1481 | uint16_t id;
|
---|
1482 | const char *client;
|
---|
1483 | } ncd_service_ids[] =
|
---|
1484 | {
|
---|
1485 | { 0x0000, "generic" },
|
---|
1486 | { 0x02C2, "Opticum" },
|
---|
1487 | { 0x0665, "rq-sssp-client-CS" },
|
---|
1488 | { 0x0666, "rqcamd" },
|
---|
1489 | { 0x0667, "rq-echo-client" },
|
---|
1490 | { 0x0669, "rq-sssp-client-CW" },
|
---|
1491 | { 0x0769, "JlsRq" },
|
---|
1492 | { 0x414C, "AlexCS" },
|
---|
1493 | { 0x4333, "camd3" },
|
---|
1494 | { 0x4343, "CCcam" },
|
---|
1495 | { 0x434C, "Cardlink" },
|
---|
1496 | { 0x4453, "DiabloCam-UW" },
|
---|
1497 | { 0x4543, "eyetvCamd" },
|
---|
1498 | { 0x4765, "Octagon" },
|
---|
1499 | { 0x4C43, "LCE" },
|
---|
1500 | { 0x4E58, "NextYE2k" },
|
---|
1501 | { 0x5342, "SBCL" },
|
---|
1502 | { 0x5456, "Tecview" },
|
---|
1503 | { 0x5644, "vdr-sc" },
|
---|
1504 | { 0x5743, "WiCard" },
|
---|
1505 | { 0x6378, "cx" },
|
---|
1506 | { 0x6502, "Tvheadend" },
|
---|
1507 | { 0x6576, "evocamd" },
|
---|
1508 | { 0x6762, "gbox2CS" },
|
---|
1509 | { 0x6B61, "Kaffeine" },
|
---|
1510 | { 0x6B63, "kpcs" },
|
---|
1511 | { 0x6D63, "mpcs" },
|
---|
1512 | { 0x6D67, "mgcamd" },
|
---|
1513 | { 0x6E65, "NextYE2k" },
|
---|
1514 | { 0x6E73, "NewCS" },
|
---|
1515 | { 0x7264, "radegast" },
|
---|
1516 | { 0x7363, "Scam" },
|
---|
1517 | { 0x7763, "WinCSC" },
|
---|
1518 | { 0x7878, "tsdecrypt" },
|
---|
1519 | { 0x8888, "OSCam" },
|
---|
1520 | { 0x9911, "ACamd" },
|
---|
1521 | { 0x9922, "DVBplug" },
|
---|
1522 | { 0xFFFF, NULL }
|
---|
1523 | };
|
---|
1524 | int i = 0;
|
---|
1525 | while(1)
|
---|
1526 | {
|
---|
1527 | if(!ncd_service_ids[i].client)
|
---|
1528 | { break; }
|
---|
1529 | if(ncd_service_ids[i].id == client_id)
|
---|
1530 | { return ncd_service_ids[i].client; }
|
---|
1531 | i++;
|
---|
1532 | }
|
---|
1533 | return "unknown - please report";
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | void module_newcamd(struct s_module *ph)
|
---|
1537 | {
|
---|
1538 | ph->desc = "newcamd";
|
---|
1539 | ph->type = MOD_CONN_TCP;
|
---|
1540 | ph->large_ecm_support = 1;
|
---|
1541 | ph->listenertype = LIS_NEWCAMD;
|
---|
1542 | IP_ASSIGN(ph->s_ip, cfg.ncd_srvip);
|
---|
1543 | ph->s_handler = newcamd_server;
|
---|
1544 | ph->s_init = newcamd_server_init;
|
---|
1545 | ph->recv = newcamd_recv;
|
---|
1546 | ph->send_dcw = newcamd_send_dcw;
|
---|
1547 | ph->ptab = cfg.ncd_ptab;
|
---|
1548 | ph->c_init = newcamd_client_init;
|
---|
1549 | ph->c_recv_chk = newcamd_recv_chk;
|
---|
1550 | ph->c_send_ecm = newcamd_send_ecm;
|
---|
1551 | ph->c_send_emm = newcamd_send_emm;
|
---|
1552 | ph->c_idle = newcamd_idle;
|
---|
1553 | ph->num = R_NEWCAMD;
|
---|
1554 | }
|
---|
1555 | #endif
|
---|