source: trunk/module-newcamd.c@ 11518

Last change on this file since 11518 was 11518, checked in by Gorgone Impertinence, 4 years ago

DVBAPI:
Fixed bug in dvbapi not releasing used descramblers in stapi, coolapi and gxapi boxes
Increased MAX_STREAM_INDICES back to 32 (it must be equal to MAX_STREAM_PIDS)
Modified the get_emm_filter_adv() function to accept more arguments

OTHER:
Cosmetics to module-cccam.c and module-cccam-cacheex.x
Removed some emu specific code not needed anymore

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