- fix for ticker #4787
[oscam.git] / module-newcamd.c
blobfe82c4f018396128a2161612f806b5789f1fdc76
1 #define MODULE_LOG_PREFIX "newcamd"
3 #include "globals.h"
5 #ifdef MODULE_NEWCAMD
7 #include "cscrypt/des.h"
8 #include "cscrypt/md5.h"
9 #include "module-newcamd.h"
10 #include "module-newcamd-des.h"
11 #include "oscam-array.h"
12 #include "oscam-conf-chk.h"
13 #include "oscam-chk.h"
14 #include "oscam-client.h"
15 #include "oscam-ecm.h"
16 #include "oscam-emm.h"
17 #include "oscam-net.h"
18 #include "oscam-reader.h"
19 #include "oscam-string.h"
20 #include "oscam-time.h"
22 const int32_t CWS_NETMSGSIZE = 1024; // csp 0.8.9 (default: 400). This is CWS_NETMSGSIZE. The old default was 240
24 #define NCD_CLIENT_ID 0x8888
25 #define CWS_FIRSTCMDNO 0xE0
27 typedef enum
29 MSG_CLIENT_2_SERVER_LOGIN = CWS_FIRSTCMDNO,
30 MSG_CLIENT_2_SERVER_LOGIN_ACK,
31 MSG_CLIENT_2_SERVER_LOGIN_NAK,
32 MSG_CARD_DATA_REQ,
33 MSG_CARD_DATA,
34 MSG_SERVER_2_CLIENT_NAME,
35 MSG_SERVER_2_CLIENT_NAME_ACK,
36 MSG_SERVER_2_CLIENT_NAME_NAK,
37 MSG_SERVER_2_CLIENT_LOGIN,
38 MSG_SERVER_2_CLIENT_LOGIN_ACK,
39 MSG_SERVER_2_CLIENT_LOGIN_NAK,
40 MSG_ADMIN,
41 MSG_ADMIN_ACK,
42 MSG_ADMIN_LOGIN,
43 MSG_ADMIN_LOGIN_ACK,
44 MSG_ADMIN_LOGIN_NAK,
45 MSG_ADMIN_COMMAND,
46 MSG_ADMIN_COMMAND_ACK,
47 MSG_ADMIN_COMMAND_NAK,
48 MSG_KEEPALIVE = CWS_FIRSTCMDNO + 0x1D,
49 MSG_SERVER_2_CLIENT_OSD = 0xD1,
50 MSG_SERVER_2_CLIENT_ALLCARDS = 0xD2,
51 MSG_SERVER_2_CLIENT_ADDCARD = 0xD3,
52 MSG_SERVER_2_CLIENT_REMOVECARD = 0xD4,
53 MSG_SERVER_2_CLIENT_CHANGE_KEY = 0xD5,
54 MSG_SERVER_2_CLIENT_GET_VERSION = 0xD6,
55 MSG_SERVER_2_CLIENT_ADDSID = 0xD7,
56 MSG_CLIENT_2_SERVER_CARDDISCOVER = 0xD8
57 } net_msg_type_t;
59 typedef enum
61 COMMTYPE_CLIENT,
62 COMMTYPE_SERVER
63 } comm_type_t;
65 typedef struct custom_data
67 uint16_t sid;
68 uint16_t caid;
69 int32_t provid;
70 uint8_t x;
71 } custom_data_t;
73 #define REQ_SIZE 2
75 static int32_t network_message_send(int32_t handle, uint16_t *netMsgId, uint8_t *buffer, int32_t len,
76 uint8_t *deskey, comm_type_t commType, uint16_t sid, custom_data_t *cd)
78 uint8_t netbuf[CWS_NETMSGSIZE];
79 int32_t head_size;
80 struct s_client *cl = cur_client();
82 head_size = (cl->ncd_proto == NCD_524) ? 8 : 12;
84 if(len < 3 || len + head_size > CWS_NETMSGSIZE || handle < 0)
86 return -1;
89 buffer[1] = (buffer[1] & 0xF0) | (((len - 3) >> 8) & 0x0F);
90 buffer[2] = (len - 3) & 0xFF;
92 memcpy(netbuf + head_size, buffer, len);
93 len += head_size;
95 if(netMsgId)
97 if(commType == COMMTYPE_CLIENT)
99 (*netMsgId)++;
102 netbuf[2] = (*netMsgId) >> 8;
103 netbuf[3] = (*netMsgId) & 0xFF;
105 else
107 netbuf[2] = 0;
108 netbuf[3] = 0;
111 memset(netbuf + 4, 0, (cl->ncd_proto == NCD_524) ? 4 : 8);
113 if(sid)
115 if(cl->reader && cl->reader->ncd_disable_server_filt
116 && sid != NCD_CLIENT_ID && cl->ncd_proto != NCD_524) // mgclient send header
118 memcpy(netbuf + 4, cl->ncd_header + 4, 7);
121 netbuf[(cl->ncd_proto == NCD_524) ? 6 : 4] = (uint8_t)(sid >> 8); // sid
122 netbuf[(cl->ncd_proto == NCD_524) ? 7 : 5] = (uint8_t)(sid);
125 //if((!ncd_proto == NCD_524) && (buffer[0] >= 0xD1) && (buffer[0] <= 0xD8)) // extended proto for mg
127 // cs_log_dbg(D_CLIENT, "newcamd: extended: msg");
129 if(cd)
131 cs_log_dbg(D_CLIENT, "newcamd: has cd");
133 netbuf[4] = cd->sid >> 8;
134 netbuf[5] = cd->sid & 0xFF;
135 netbuf[6] = cd->caid >> 8;
136 netbuf[7] = cd->caid & 0xFF;
137 netbuf[8] = (cd->provid >> 16) & 0xFF;
138 netbuf[9] = (cd->provid >> 8) & 0xFF;
139 netbuf[10] = cd->provid & 0xFF;
143 if(NCD_525 == cl->ncd_proto && cfg.ncd_mgclient && MSG_CLIENT_2_SERVER_LOGIN_ACK == buffer[0])
145 netbuf[4] = 0x6E; // From ExtNewcamSession.java CSP line 65-66 getLoginOkMsg()
146 netbuf[5] = 0x73;
147 netbuf[11] = 0x14;
150 if(NCD_525 == cl->ncd_proto && MSG_SERVER_2_CLIENT_ADDSID == buffer[0])
152 netbuf[11] = 0x14;
155 //if(buffer[0] == MSG_CLIENT_2_SERVER_LOGIN && cur_client()->reader->ncd_disable_server_filt)
157 // netbuf[11] = 0x11; // From ChameleonCwsConnector.java CSP line 59-61 run()
160 netbuf[0] = (len - 2) >> 8;
161 netbuf[1] = (len - 2) & 0xFF;
163 cs_log_dump_dbg(D_CLIENT, netbuf, len, "send %d bytes to %s", len, remote_txt());
165 if((len = nc_des_encrypt(netbuf, len, deskey)) < 0)
167 return -1;
170 netbuf[0] = (len - 2) >> 8;
171 netbuf[1] = (len - 2) & 0xFF;
173 return send(handle, netbuf, len, 0);
176 static int32_t send_sid_list(void)
178 struct s_client *cl = cur_client();
180 if(1 != cl->ftab.nfilts || !cl->sidtabs.no || !cfg.ncd_ptab.ports[cl->port_idx].ncd)
182 cs_log("SID list will not be send to mgcamd client.");
183 return 0;
186 uint8_t mbuf[CWS_NETMSGSIZE];
187 int32_t n = 0, nr = 0, portion_sid_num = 0, i = 0, sid_num = 0, portion_num = 0;
188 SIDTAB *sidtab = 0;
189 custom_data_t cd;
191 cs_log_dbg(D_TRACE, "Send SID list to mgcamd client.");
193 memset(&cd, 0, sizeof(cd));
194 FILTER *pfilts = cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts;
196 //memset(mbuf, 0, sizeof(mbuf)); // not nessesery
198 for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
200 if((cl->sidtabs.no & ((SIDTABBITS)1 << nr)) && (sidtab->num_caid | sidtab->num_provid | sidtab->num_srvid))
202 for(n = 0; n < pfilts[0].nprids; n++)
204 if(chk_srvid_match_by_caid_prov(pfilts[0].caid, pfilts[0].prids[n], sidtab))
206 for(i = 0; i < sidtab->num_srvid; i++)
208 // First SID goes to header
209 if(0 == portion_sid_num)
211 cd.sid = sidtab->srvid[i]; // first sid
212 cd.caid = cfg.ncd_ptab.ports[cl->port_idx].s_port; // assigned port
213 cd.provid = 0x1; // mark as deny
216 mbuf[portion_sid_num * 3] = (uint8_t)(sidtab->srvid[i] >> 8);
217 mbuf[portion_sid_num * 3 + 1] = (uint8_t)(sidtab->srvid[i] & 0xFF);
218 mbuf[portion_sid_num * 3 + 2] = 0x1; // mark as deny
220 ++sid_num;
221 ++portion_sid_num;
223 if(portion_sid_num >= 50)
225 ++portion_num;
227 cs_log_dump_dbg(0x0800, mbuf, (portion_sid_num) * 3, "Portion %d contains %d SIDs",
228 portion_num, portion_sid_num);
230 mbuf[0] = MSG_SERVER_2_CLIENT_ADDSID;
231 mbuf[1] = 0x00;
232 mbuf[2] = 0x00;
234 network_message_send(cl->udp_fd, &cl->ncd_msgid, mbuf, portion_sid_num * 3,
235 cl->ncd_skey, COMMTYPE_SERVER, 0, &cd);
236 portion_sid_num = 0;
240 break;
246 if(portion_sid_num)
248 ++portion_num;
250 cs_log_dump_dbg(0x0800, mbuf, (portion_sid_num) * 3, "Portion %d contains %d SIDs",
251 portion_num, portion_sid_num);
253 mbuf[0] = MSG_SERVER_2_CLIENT_ADDSID;
254 mbuf[1] = 0x0;
255 mbuf[2] = 0x0;
257 network_message_send(cl->udp_fd, &cl->ncd_msgid, mbuf, portion_sid_num * 3,
258 cl->ncd_skey, COMMTYPE_SERVER, 0, &cd);
259 portion_sid_num = 0;
262 cs_log("%d deny SIDs in the %d messages were sent to the client.", sid_num, portion_num);
264 return sid_num;
267 static int32_t network_message_receive(int32_t handle, uint16_t *netMsgId, uint8_t *buffer,
268 uint8_t *deskey, comm_type_t commType)
270 int32_t len, ncd_off, msgid;
271 uint8_t netbuf[CWS_NETMSGSIZE];
272 int32_t returnLen;
273 struct s_client *cl = cur_client();
275 if(!buffer || handle < 0)
277 return -1;
280 len = cs_recv(handle, netbuf, 2, 0);
282 cs_log_dbg(D_CLIENT, "nmr(): len=%d, errno=%d", len, (len == -1) ? errno : 0);
284 if(!len)
286 cs_log_dbg(D_CLIENT, "nmr: 1 return 0");
288 if(commType == COMMTYPE_CLIENT)
290 network_tcp_connection_close(cl->reader, "receive error1");
292 else
294 cs_disconnect_client(cl);
297 return 0;
300 if(len != 2)
302 cs_log_dbg(D_CLIENT, "nmr: len!=2");
304 if(commType == COMMTYPE_CLIENT)
306 network_tcp_connection_close(cl->reader, "receive error2");
308 else
310 cs_disconnect_client(cl);
313 return -1;
316 if(((netbuf[0] << 8) | netbuf[1]) > CWS_NETMSGSIZE - 2)
318 cs_log_dbg(D_CLIENT, "nmr: received data len=%d longer than CWS_NETMSGSIZE=%d",
319 ((netbuf[0] << 8) | netbuf[1]), CWS_NETMSGSIZE);
320 cs_log_dbg(D_CLIENT, "nmr: 1 return -1");
321 return -1;
324 len = cs_recv(handle, netbuf + 2, (netbuf[0] << 8) | netbuf[1], 0);
325 if(!len)
327 cs_log_dbg(D_CLIENT, "nmr: 2 return 0");
328 return 0;
331 if(len != ((netbuf[0] << 8) | netbuf[1]))
333 cs_log_dbg(D_CLIENT, "nmr: 2 return -1");
334 return -1;
337 len += 2;
339 if((len = nc_des_decrypt(netbuf, len, deskey)) < 11) // 15(newcamd525) or 11 ???
341 cs_log_dbg(D_CLIENT, "nmr: can't decrypt, invalid des key?");
342 cs_sleepms(2000);
343 return -1;
346 //cs_log_dump_dbg(D_CLIENT, netbuf, len, "nmr: decrypted data, len=%d", len);
348 msgid = (netbuf[2] << 8) | netbuf[3];
350 if(cl->ncd_proto == NCD_AUTO)
352 // auto detect
353 int32_t l5 = (((netbuf[13] & 0x0F) << 8) | netbuf[14]) + 3;
354 int32_t l4 = (((netbuf[9] & 0x0F) << 8) | netbuf[10]) + 3;
356 if((l5 <= len - 12) && ((netbuf[12] & 0xF0) == 0xE0 || (netbuf[12] & 0xF0) == 0x80))
358 cl->ncd_proto = NCD_525;
360 else if((l4 <= len - 8) && ((netbuf[8] & 0xF0) == 0xE0 || (netbuf[9] & 0xF0) == 0x80))
362 cl->ncd_proto = NCD_524;
364 else
366 cs_log_dbg(D_CLIENT, "nmr: 4 return -1");
367 return -1;
370 cs_log_dbg(D_CLIENT, "nmr: autodetect: newcamd52%d used", (cl->ncd_proto == NCD_525) ? 5 : 4);
373 ncd_off = (cl->ncd_proto == NCD_525) ? 4 : 0;
375 returnLen = (((netbuf[9 + ncd_off] & 0x0F) << 8) | netbuf[10 + ncd_off]) + 3;
376 if(returnLen > (len - (8 + ncd_off)))
378 cs_log_dbg(D_CLIENT, "nmr: 4 return -1");
379 return -1;
382 //cs_log_dump_dbg(D_CLIENT, netbuf, len, "nmr: decrypted data");
384 if(netMsgId)
386 switch(commType)
388 case COMMTYPE_SERVER:
389 *netMsgId = msgid;
390 break;
392 case COMMTYPE_CLIENT:
393 //if(*netMsgId != ((netbuf[2] << 8) | netbuf[3]))
395 cs_log_dbg(D_CLIENT, "nmr: netMsgId=%d, from server=%d, ", *netMsgId, msgid);
396 //return -2;
398 break;
400 default:
401 cs_log_dbg(D_CLIENT, "nmr: 5 return -1");
402 return -1;
403 break;
407 switch(commType)
409 case COMMTYPE_SERVER:
410 memcpy(cl->ncd_header, netbuf, (8 + ncd_off));
411 buffer[0] = (cl->ncd_proto == NCD_525) ? netbuf[4] : netbuf[6]; // sid
412 buffer[1] = (cl->ncd_proto == NCD_525) ? netbuf[5] : netbuf[7];
413 break;
415 case COMMTYPE_CLIENT:
416 memcpy(cl->ncd_header, netbuf, (8 + ncd_off));
417 buffer[0] = netbuf[2]; // msgid
418 buffer[1] = netbuf[3];
419 break;
422 memcpy(buffer + 2, netbuf + (8 + ncd_off), returnLen);
423 return returnLen + 2;
426 static void network_cmd_no_data_send(int32_t handle, uint16_t *netMsgId, net_msg_type_t cmd,
427 uint8_t *deskey, comm_type_t commType)
429 uint8_t buffer[3];
431 buffer[0] = cmd;
432 buffer[1] = 0;
433 buffer[2] = 0;
435 network_message_send(handle, netMsgId, buffer, 3, deskey, commType, 0, NULL);
438 static int32_t network_cmd_no_data_receive(int32_t handle, uint16_t *netMsgId, uint8_t *deskey,
439 comm_type_t commType)
441 uint8_t buffer[CWS_NETMSGSIZE];
443 if(network_message_receive(handle, netMsgId, buffer, deskey, commType) != 3 + 2)
445 return -1;
448 return buffer[2];
451 void newcamd_reply_ka(void)
453 struct s_client *cl = cur_client();
455 if(!cl)
457 return;
460 if(!cl->udp_fd)
462 cs_log_dbg(D_CLIENT, "invalid client fd=%d", cl->udp_fd);
463 return;
466 cs_log_dbg(D_CLIENT, "send keepalive to client fd=%d", cl->udp_fd);
468 if(cl->reader)
470 cl->reader->last_s = time((time_t *)0);
473 network_cmd_no_data_send(cl->udp_fd, &cl->ncd_msgid, MSG_KEEPALIVE, cl->ncd_skey, COMMTYPE_SERVER);
476 static int32_t connect_newcamd_server(void)
478 int32_t i;
479 uint8_t buf[CWS_NETMSGSIZE];
480 uint8_t keymod[14];
481 uint8_t key[16];
482 int32_t handle = 0;
484 uint32_t idx;
485 uint8_t passwdcrypt[120];
486 uint8_t login_answer;
487 int32_t bytes_received;
488 struct s_client *cl = cur_client();
490 if(cl->reader->device[0] == 0 || cl->reader->r_pwd[0] == 0
491 || cl->reader->r_usr[0] == 0 || cl->reader->r_port == 0)
493 return -5;
496 // 1. Connect
497 handle = network_tcp_connection_open(cl->reader);
498 if(handle < 0)
500 return -1;
503 // 2. Get init sequence
504 cl->ncd_msgid = 0;
505 if(read(handle, keymod, sizeof(keymod)) != sizeof(keymod))
507 cs_log("server does not return 14 bytes");
508 network_tcp_connection_close(cl->reader, "connect error");
509 return -2;
512 cs_log_dump_dbg(D_CLIENT, keymod, sizeof(cl->reader->ncd_key), "server init sequence:");
513 nc_des_login_key_get(keymod, cl->reader->ncd_key, sizeof(cl->reader->ncd_key), key);
515 // 3. Send login info
516 idx = 3;
517 buf[0] = MSG_CLIENT_2_SERVER_LOGIN;
518 buf[1] = 0;
520 cs_strncpy((char *)buf + idx, cl->reader->r_usr, sizeof(buf) - idx);
521 __md5_crypt(cl->reader->r_pwd, "$1$abcdefgh$", (char *)passwdcrypt);
522 idx += strlen(cl->reader->r_usr) + 1;
523 cs_strncpy((char *)buf + idx, (const char *)passwdcrypt, sizeof(buf) - idx);
525 network_message_send(handle, 0, buf, idx + strlen((char *)passwdcrypt) + 1, key,
526 COMMTYPE_CLIENT, NCD_CLIENT_ID, NULL);
528 // 3.1 Get login answer
529 login_answer = network_cmd_no_data_receive(handle, &cl->ncd_msgid, key, COMMTYPE_CLIENT);
530 if(login_answer == MSG_CLIENT_2_SERVER_LOGIN_NAK)
532 cs_log("login failed for user '%s'", cl->reader->r_usr);
533 network_tcp_connection_close(cl->reader, "login error1");
534 return -3;
537 if(login_answer != MSG_CLIENT_2_SERVER_LOGIN_ACK)
539 cs_log("expected MSG_CLIENT_2_SERVER_LOGIN_ACK (%02X), received %02X",
540 MSG_CLIENT_2_SERVER_LOGIN_ACK, login_answer);
542 network_tcp_connection_close(cl->reader, "login error2");
543 return -3;
546 // 3.2 Set connection info
547 cl->reader->tcp_connected = 1;
548 cl->crypted = 1;
550 // 4. Send MSG_CARD_DATE_REQ
551 nc_des_login_key_get(cl->reader->ncd_key, passwdcrypt, strlen((char *)passwdcrypt), key);
552 network_cmd_no_data_send(handle, &cl->ncd_msgid, MSG_CARD_DATA_REQ, key, COMMTYPE_CLIENT);
554 bytes_received = network_message_receive(handle, &cl->ncd_msgid, buf, key, COMMTYPE_CLIENT);
555 if(bytes_received < 16 || buf[2] != MSG_CARD_DATA)
557 cs_log("expected MSG_CARD_DATA (%02X), received %02X", MSG_CARD_DATA, buf[2]);
559 network_tcp_connection_close(cl->reader, "receive error");
560 return -4;
563 // 5. Parse CAID and PROVID(s)
564 cl->reader->caid = (uint16_t)((buf[6] << 8) | buf[7]);
566 // handle special serial format in newcamd. See newcamd_auth_client
567 newcamd_to_hexserial(buf + 10, cl->reader->hexserial, cl->reader->caid);
569 cs_log("Newcamd Server: %s:%d - UserID: %i", cl->reader->device, cl->reader->r_port, buf[3 + 2]);
571 cs_log("CAID: %04X - UA: %02X%02X%02X%02X%02X%02X%02X%02X - Provider # %i",
572 cl->reader->caid, cl->reader->hexserial[0], cl->reader->hexserial[1], cl->reader->hexserial[2],
573 cl->reader->hexserial[3], cl->reader->hexserial[4], cl->reader->hexserial[5], cl->reader->hexserial[6],
574 cl->reader->hexserial[7], buf[14 + 2]);
576 cl->reader->nprov = buf[14 + 2];
577 memset(cl->reader->prid, 0x00, sizeof(cl->reader->prid));
579 for(i = 0; i < cl->reader->nprov; i++)
581 if(caid_is_betacrypt(cl->reader->caid) || caid_is_irdeto(cl->reader->caid))
583 memcpy(&cl->reader->prid[i], buf + 22 + 2 + 11 * i, 4);
585 else
587 cl->reader->prid[i][1] = buf[15 + 2 + 11 * i];
588 cl->reader->prid[i][2] = buf[16 + 2 + 11 * i];
589 cl->reader->prid[i][3] = buf[17 + 2 + 11 * i];
592 memcpy(&cl->reader->sa[i], buf + 22 + 2 + 11 * i, 4); // the 4 first bytes are not read
594 cs_log("Provider ID: %02X%02X%02X - SA: %02X%02X%02X%02X",
595 cl->reader->prid[i][1], cl->reader->prid[i][2], cl->reader->prid[i][3], cl->reader->sa[i][0],
596 cl->reader->sa[i][1], cl->reader->sa[i][2], cl->reader->sa[i][3]);
599 memcpy(cl->reader->ncd_skey, key, 16);
601 // 6. Set card inserted
602 cl->reader->tcp_connected = 2;
603 cl->reader->card_status = CARD_INSERTED;
604 cl->reader->last_g = cl->reader->last_s = time((time_t *)0);
606 // Only after connect() on cl->udp_fd (Linux)
607 cl->pfd = cl->udp_fd;
609 if(cl->reader->ncd_disable_server_filt) // act like mgclient
611 network_cmd_no_data_send(handle, &cl->ncd_msgid, MSG_SERVER_2_CLIENT_GET_VERSION, key, COMMTYPE_CLIENT);
614 return 0;
617 static int32_t newcamd_connect(void)
619 struct s_client *cl = cur_client();
621 if(cl->reader->tcp_connected < 2 && connect_newcamd_server() < 0)
623 return 0;
626 if(!cl->udp_fd)
628 return 0;
631 return 1;
634 static int32_t newcamd_send(uint8_t *buf, int32_t ml, uint16_t sid)
636 struct s_client *cl = cur_client();
638 if(!newcamd_connect())
640 return -1;
643 return network_message_send(cl->udp_fd, &cl->ncd_msgid, buf, ml,
644 cl->reader->ncd_skey, COMMTYPE_CLIENT, sid, NULL);
647 static int32_t newcamd_recv(struct s_client *client, uint8_t *buf, int32_t UNUSED(l))
649 int32_t rc, rs;
651 if(client->typ == 'c')
653 rs = network_message_receive(client->udp_fd, &client->ncd_msgid, buf,
654 client->ncd_skey, COMMTYPE_SERVER);
656 else
658 if(!client->udp_fd)
660 return (-1);
663 rs = network_message_receive(client->udp_fd, &client->ncd_msgid, buf,
664 client->reader->ncd_skey, COMMTYPE_CLIENT);
667 if(rs < 5)
669 rc = -1;
671 else
673 rc = rs;
676 cs_log_dump_dbg(D_CLIENT, buf, rs, "received %d bytes from %s", rs, remote_txt());
678 client->last = time((time_t *) 0);
680 if(rc == -1)
682 if(rs > 0)
684 cs_log("packet is too small (%d bytes)", rs);
686 else
688 cs_log("Connection closed to %s", remote_txt());
692 return (rc);
695 static void mk_user_au_ftab(struct s_reader *aureader, FILTER *filt)
697 int32_t i, j, found;
698 FILTER old_filter;
700 memcpy(&old_filter, filt, sizeof(old_filter));
701 memset(filt, 0, sizeof(*filt));
703 filt->caid = aureader->caid;
704 if(filt->caid == 0)
706 filt->caid = old_filter.caid;
709 for(i = 0; i < aureader->nprov && filt->nprids < CS_MAXPROV; i++)
711 filt->prids[filt->nprids++] = b2i(3, &aureader->prid[i][1]);
714 for(i = 0; i < old_filter.nprids && filt->nprids < CS_MAXPROV; i++)
716 for(j = found = 0; (!found) && (j < filt->nprids); j++)
718 if(old_filter.prids[i] == filt->prids[j])
720 found = 1;
724 if(!found)
726 filt->prids[filt->nprids++] = old_filter.prids[i];
731 static void mk_user_ftab(FILTER *filt)
733 int32_t port_idx, i, j, k, c;
734 struct s_client *cl = cur_client();
736 memset(filt, 0, sizeof(*filt));
738 port_idx = cl->port_idx;
739 if(!cfg.ncd_ptab.ports[port_idx].ncd)
741 return;
744 FILTER *psfilt = &cfg.ncd_ptab.ports[port_idx].ncd->ncd_ftab.filts[0];
746 // 1. CAID
747 // search server CAID in client CAID
748 for(c = i = 0; i < cl->ctab.ctnum; i++)
750 CAIDTAB_DATA *d = &cl->ctab.ctdata[i];
752 int32_t ctab_caid = d->caid & d->mask;
753 if(ctab_caid)
755 c++;
758 if(psfilt->caid == ctab_caid)
760 filt->caid = ctab_caid;
761 break;
765 if(c && !filt->caid)
767 cs_log("no valid CAID found in CAID for user '%s'", cl->account->usr);
768 return;
771 // search CAID in client IDENT
772 cs_log_dbg(D_CLIENT, "client[%8lX].%s nfilts=%d, filt.caid=%04X", (unsigned long)pthread_self(),
773 cl->account->usr, cl->ftab.nfilts, filt->caid);
775 if(!filt->caid && cl->ftab.nfilts)
777 int32_t fcaids;
778 for(i = fcaids = 0; i < cl->ftab.nfilts; i++)
780 uint16_t ucaid = cl->ftab.filts[i].caid;
781 if(ucaid)
783 fcaids++;
786 if(ucaid && psfilt->caid == ucaid)
788 filt->caid = ucaid;
789 break;
793 if(fcaids == cl->ftab.nfilts && !filt->caid)
795 cs_log("no valid CAID found in IDENT for user '%s'", cl->account->usr);
796 //cs_disconnect_client();
797 return;
801 // empty client CAID - use server CAID
802 if(!filt->caid)
804 filt->caid = psfilt->caid;
807 // 2. PROVID
808 if(!cl->ftab.nfilts)
810 int32_t add;
811 for(i = 0; i < psfilt->nprids && filt->nprids < CS_MAXPROV; i++)
813 // use server PROVID(s) (and only those which are in user's groups)
814 add = 0;
815 struct s_reader *rdr;
817 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
819 if(rdr->grp & cl->grp)
821 if(!rdr->ftab.nfilts)
823 if(is_network_reader(rdr))
825 add = 1;
828 for(j = 0; !add && j < rdr->nprov; j++)
830 if(b2i(3, &rdr->prid[j][1]) == psfilt->prids[i])
832 add = 1;
836 else
838 for(j = 0; !add && j < rdr->ftab.nfilts; j++)
840 uint32_t rcaid = rdr->ftab.filts[j].caid;
841 if(!rcaid || rcaid == filt->caid)
843 for(k = 0; !add && k < rdr->ftab.filts[j].nprids; k++)
845 if(rdr->ftab.filts[j].prids[k] == psfilt->prids[i])
847 add = 1;
856 if(add)
858 filt->prids[filt->nprids++] = psfilt->prids[i];
862 memcpy(filt, psfilt, sizeof(*filt));
863 return;
866 // search in client IDENT
867 for(j = 0; j < cl->ftab.nfilts; j++)
869 uint32_t ucaid = cl->ftab.filts[j].caid;
871 cs_log_dbg(D_CLIENT, "client caid %d: %04X", j, ucaid);
873 if(!ucaid || ucaid == filt->caid)
875 for(i = 0; i < psfilt->nprids && filt->nprids < CS_MAXPROV; i++)
877 cs_log_dbg(D_CLIENT, "search server provid %d: %06X", i, psfilt->prids[i]);
879 if(cl->ftab.filts[j].nprids)
881 for(k = 0; k < cl->ftab.filts[j].nprids && filt->nprids < CS_MAXPROV; k++)
883 if(cl->ftab.filts[j].prids[k] == psfilt->prids[i])
885 filt->prids[filt->nprids++] = cl->ftab.filts[j].prids[k];
889 else
891 filt->prids[filt->nprids++] = psfilt->prids[i];
892 // allow server PROVID(s) if no PROVID(s) specified in IDENT
898 if(!filt->nprids)
900 cs_log("no valid PROVID(s) found in CAID for user '%s'", cl->account->usr);
901 //cs_disconnect_client();
905 static int8_t newcamd_auth_client(IN_ADDR_T ip, uint8_t *deskey)
907 int32_t i, ok, rc, sid_list;
908 uint8_t *usr = NULL, *pwd = NULL;
909 struct s_auth *account;
910 uint8_t buf[14];
911 uint8_t key[16];
912 uint8_t passwdcrypt[120];
913 struct s_reader *aureader = NULL, *rdr = NULL;
914 struct s_client *cl = cur_client();
915 uint8_t mbuf[CWS_NETMSGSIZE];
917 sid_list = 0;
919 ok = cfg.ncd_allowed ? check_ip(cfg.ncd_allowed, ip) : 1;
921 if(!ok)
923 cs_auth_client(cl, (struct s_auth *)0, NULL);
924 return -1;
927 // make random 14 bytes
928 get_random_bytes(buf, 14);
930 // send init sequence
931 send(cl->udp_fd, buf, 14, 0);
932 nc_des_login_key_get(buf, deskey, 14, key);
933 memcpy(cl->ncd_skey, key, 16);
934 cl->ncd_msgid = 0;
936 i = process_input(mbuf, sizeof(mbuf), cfg.cmaxidle);
937 if(i > 0)
939 if(mbuf[2] != MSG_CLIENT_2_SERVER_LOGIN)
941 cs_log_dbg(D_CLIENT, "expected MSG_CLIENT_2_SERVER_LOGIN (%02X), received %02X",
942 MSG_CLIENT_2_SERVER_LOGIN, mbuf[2]);
943 return -1;
946 usr = mbuf + 5;
947 pwd = usr + strlen((char *)usr) + 1;
949 else
951 cs_log_dbg(D_CLIENT, "bad client login request");
952 return -1;
955 cl->ncd_client_id = (mbuf[0] << 8) | mbuf[1];
956 const char *client_name = newcamd_get_client_name(cl->ncd_client_id);
958 #if defined(TCP_KEEPIDLE)
959 if(cl->ncd_client_id == 0x4453) // DiabloWifi has problems with TCPKeepAlive
961 int32_t flag = 600;
963 // send first keepalive packet after 600 seconds of
964 // last package received (keepalive packets included)
965 if(setsockopt(cl->udp_fd, IPPROTO_TCP, TCP_KEEPIDLE, &flag, sizeof(flag)) && errno != EBADF)
967 cs_log("Setting TCP_KEEPIDLE failed, errno=%d, %s", errno, strerror(errno));
969 else
971 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.");
974 #endif
976 if(cl->ncd_proto == NCD_525 && 0x6D == mbuf[0] && 0x67 == mbuf[1] && 0x11 == cl->ncd_header[11])
978 sid_list = 1;
981 for(ok = 0, account = cfg.account; usr && account && (!ok); account = account->next)
983 cs_log_dbg(D_CLIENT, "account->usr=%s", account->usr);
985 if(strcmp((char *)usr, account->usr) == 0)
987 __md5_crypt(ESTR(account->pwd), "$1$abcdefgh$", (char *)passwdcrypt);
988 cs_log_dbg(D_CLIENT, "account->pwd=%s", passwdcrypt);
990 if(strcmp((char *)pwd, (const char *)passwdcrypt) == 0)
992 cl->crypted = 1;
993 char e_txt[20];
995 snprintf(e_txt, 20, "%s:%d", "newcamd", cfg.ncd_ptab.ports[cl->port_idx].s_port);
997 if((rc = cs_auth_client(cl, account, e_txt)) == 2)
999 cs_log("hostname or ip mismatch for user %s (%s)", usr, client_name);
1000 break;
1002 else if(rc != 0)
1004 cs_log("account is invalid for user %s (%s)", usr, client_name);
1005 break;
1007 else
1009 cs_log("user %s authenticated successfully (%s)", usr, client_name);
1010 ok = 1;
1011 break;
1014 else
1016 cs_log("user %s is providing a wrong password (%s)", usr, client_name);
1021 if(!ok && !account)
1023 cs_log("user %s is trying to connect but doesnt exist ! (%s)", usr, client_name);
1024 usr = 0;
1027 // check for non ready reader and reject client
1028 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
1030 if(!cfg.ncd_ptab.ports[cl->port_idx].ncd)
1032 continue;
1035 if(rdr->caid == cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].caid)
1037 if(rdr->card_status == CARD_NEED_INIT)
1039 cs_log("init for reader %s not finished -> reject client", rdr->label);
1040 ok = 0;
1042 break;
1046 if(ok)
1048 LL_ITER itr = ll_iter_create(cl->aureader_list);
1049 while((rdr = ll_iter_next(&itr)))
1051 int32_t n;
1053 if(!cfg.ncd_ptab.ports[cl->port_idx].ncd)
1055 continue;
1058 if(cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].caid == 0
1059 && !rdr->audisabled && (is_network_reader(rdr) || rdr->card_status == CARD_INSERTED))
1061 aureader = rdr;
1062 break;
1065 for(n = 0; n < cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].nprids; n++)
1067 if(emm_reader_match(rdr, cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].caid,
1068 cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].prids[n]))
1070 aureader = rdr;
1071 break;
1075 if(aureader)
1077 break;
1081 if(aureader)
1083 cs_log("AU enabled for user %s on reader %s", usr, aureader->label);
1085 else
1087 cs_log("AU disabled for user %s", usr);
1091 network_cmd_no_data_send(cl->udp_fd, &cl->ncd_msgid,
1092 (ok) ? MSG_CLIENT_2_SERVER_LOGIN_ACK : MSG_CLIENT_2_SERVER_LOGIN_NAK,
1093 cl->ncd_skey, COMMTYPE_SERVER);
1095 if(ok)
1097 FILTER usr_filter;
1098 FILTER *pufilt = &usr_filter;
1100 nc_des_login_key_get(deskey, passwdcrypt, strlen((char *)passwdcrypt), key);
1101 memcpy(cl->ncd_skey, key, 16);
1103 i = process_input(mbuf, sizeof(mbuf), cfg.cmaxidle);
1104 if(i > 0)
1106 int32_t j, len = 15;
1108 if(mbuf[2] != MSG_CARD_DATA_REQ)
1110 cs_log_dbg(D_CLIENT, "expected MSG_CARD_DATA_REQ (%02X), received %02X",
1111 MSG_CARD_DATA_REQ, mbuf[2]);
1112 return -1;
1115 mk_user_ftab(&usr_filter);
1117 // set userfilter for au enabled clients
1118 if(aureader)
1120 #ifdef WITH_EMU
1121 if(aureader->typ == R_EMU)
1123 usr_filter = *get_emu_prids_for_caid(aureader, cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].caid);
1125 else
1126 #endif
1127 mk_user_au_ftab(aureader, &usr_filter);
1130 ftab_clear(&cl->ftab);
1132 if(!cfg.ncd_mgclient)
1134 ftab_add(&cl->ftab, &usr_filter);
1137 mbuf[0] = MSG_CARD_DATA;
1138 mbuf[1] = 0x00;
1139 mbuf[2] = 0x00;
1141 if(aureader)
1143 mbuf[3] = 1;
1145 else
1147 mbuf[3] = get_threadnum(cl) + 10; // Unique user number
1150 mbuf[4] = (uint8_t)(pufilt->caid >> 8);
1151 mbuf[5] = (uint8_t)(pufilt->caid);
1152 mbuf[6] = 0x00;
1153 mbuf[7] = 0x00;
1155 if(aureader)
1157 hexserial_to_newcamd(aureader->hexserial, mbuf + 8, pufilt->caid);
1159 else
1161 memset(&mbuf[8], 0, 6); // mbuf[8] - mbuf[13]
1164 mbuf[14] = pufilt->nprids;
1166 for(j = 0; j < pufilt->nprids; j++)
1168 if(caid_is_betacrypt(pufilt->caid) || caid_is_irdeto(pufilt->caid))
1170 mbuf[15 + 11 * j] = 0;
1171 mbuf[16 + 11 * j] = 0;
1172 mbuf[17 + 11 * j] = j;
1174 else
1176 mbuf[15 + 11 * j] = (uint8_t)(pufilt->prids[j] >> 16);
1177 mbuf[16 + 11 * j] = (uint8_t)(pufilt->prids[j] >> 8);
1178 mbuf[17 + 11 * j] = (uint8_t)(pufilt->prids[j]);
1181 mbuf[18 + 11 * j] = 0x00;
1182 mbuf[19 + 11 * j] = 0x00;
1183 mbuf[20 + 11 * j] = 0x00;
1184 mbuf[21 + 11 * j] = 0x00;
1186 if(aureader)
1188 // check if user provid from IDENT exists on card
1189 int32_t k, found;
1190 uint32_t rprid;
1191 found = 0;
1193 if(pufilt->caid == aureader->caid && aureader->typ != R_EMU)
1195 for(k = 0; k < aureader->nprov; k++)
1197 rprid = b2i(3, &aureader->prid[k][1]);
1198 if(rprid == pufilt->prids[j])
1200 if(caid_is_betacrypt(pufilt->caid) || caid_is_irdeto(pufilt->caid))
1202 mbuf[22 + 11 * j] = aureader->prid[k][0];
1203 mbuf[23 + 11 * j] = aureader->prid[k][1];
1204 mbuf[24 + 11 * j] = aureader->prid[k][2];
1205 mbuf[25 + 11 * j] = aureader->prid[k][3];
1207 else
1209 mbuf[22 + 11 * j] = aureader->sa[k][0];
1210 mbuf[23 + 11 * j] = aureader->sa[k][1];
1211 mbuf[24 + 11 * j] = aureader->sa[k][2];
1212 mbuf[25 + 11 * j] = aureader->sa[k][3];
1215 found = 1;
1216 break;
1221 if(!found)
1223 mbuf[22 + 11 * j] = 0x00;
1224 mbuf[23 + 11 * j] = 0x00;
1225 mbuf[24 + 11 * j] = 0x00;
1226 mbuf[25 + 11 * j] = 0x00;
1229 else
1231 if(caid_is_betacrypt(pufilt->caid) || caid_is_irdeto(pufilt->caid))
1233 mbuf[22 + 11 * j] = 0x00;
1234 mbuf[23 + 11 * j] = (uint8_t)(pufilt->prids[j] >> 16);
1235 mbuf[24 + 11 * j] = (uint8_t)(pufilt->prids[j] >> 8);
1236 mbuf[25 + 11 * j] = (uint8_t)(pufilt->prids[j]);
1238 else
1240 mbuf[22 + 11 * j] = 0x00;
1241 mbuf[23 + 11 * j] = 0x00;
1242 mbuf[24 + 11 * j] = 0x00;
1243 mbuf[25 + 11 * j] = 0x00;
1247 len += 11;
1250 custom_data_t cd;
1251 memset(&cd, 0, sizeof(cd));
1253 if(aureader)
1255 if((aureader->blockemm & EMM_GLOBAL) && !(aureader->saveemm & EMM_GLOBAL))
1257 cd.sid |= 4;
1260 if((aureader->blockemm & EMM_SHARED) && !(aureader->saveemm & EMM_SHARED))
1262 cd.sid |= 2;
1265 if((aureader->blockemm & EMM_UNIQUE) && !(aureader->saveemm & EMM_UNIQUE))
1267 cd.sid |= 1;
1271 if(network_message_send(cl->udp_fd, &cl->ncd_msgid, mbuf, len, key, COMMTYPE_SERVER, 0, &cd) < 0)
1273 return -1;
1277 // send SID list
1278 if(sid_list)
1280 send_sid_list();
1283 else
1285 cs_auth_client(cl, 0, usr ? "login failure" : "no such user");
1286 return -1;
1289 return 0;
1292 static void newcamd_send_dcw(struct s_client *client, ECM_REQUEST *er)
1294 int32_t len;
1295 uint16_t cl_msgid;
1296 uint8_t mbuf[19];
1298 if(!client->udp_fd)
1300 cs_log_dbg(D_CLIENT, "ncd_send_dcw: error: client->udp_fd=%d", client->udp_fd);
1301 return;
1304 cl_msgid = er->msgid;
1305 mbuf[0] = er->ecm[0];
1307 if(er->rc >= E_NOTFOUND) // not found
1309 len = 3;
1310 mbuf[1] = mbuf[2] = 0x00;
1312 else
1314 len = 19;
1315 mbuf[1] = mbuf[2] = 0x10;
1316 memcpy(mbuf + 3, er->cw, 16);
1319 cs_log_dbg(D_CLIENT, "ncd_send_dcw: er->msgid=%d, cl_msgid=%d, %02X", er->msgid, cl_msgid, mbuf[0]);
1321 network_message_send(client->udp_fd, &cl_msgid, mbuf, len, client->ncd_skey, COMMTYPE_SERVER, 0, NULL);
1324 static void newcamd_process_ecm(struct s_client *cl, uint8_t *buf, int32_t len)
1326 int32_t pi;
1327 ECM_REQUEST *er;
1328 uint16_t ecmlen;
1330 if(len < 5)
1332 return;
1335 ecmlen = SCT_LEN((&buf[2]));
1336 if(ecmlen < 4 || ecmlen > MAX_ECM_SIZE || ecmlen + 2 > len)
1338 return;
1341 if(!(er = get_ecmtask()))
1343 return;
1346 // save client ncd_msgid
1347 er->msgid = cl->ncd_msgid;
1348 er->ecmlen = ecmlen;
1350 cs_log_dbg(D_CLIENT, "ncd_process_ecm: er->msgid=%d len=%d ecmlen=%d", er->msgid, len, er->ecmlen);
1352 er->srvid = cl->ncd_header[4] << 8 | cl->ncd_header[5];
1353 er->caid = cl->ncd_header[6] << 8 | cl->ncd_header[7];
1354 er->prid = cl->ncd_header[8] << 16 | cl->ncd_header[9] << 8 | cl->ncd_header[10];
1356 if(!er->caid)
1358 pi = cl->port_idx;
1359 if(cfg.ncd_ptab.nports && cfg.ncd_ptab.nports >= pi && cfg.ncd_ptab.ports[pi].ncd)
1361 er->caid = cfg.ncd_ptab.ports[pi].ncd->ncd_ftab.filts[0].caid;
1365 memcpy(er->ecm, buf + 2, er->ecmlen);
1366 get_cw(cl, er);
1369 static void newcamd_process_emm(uint8_t *buf, int32_t len)
1371 int32_t ok = 1, provid;
1372 uint16_t caid = 0;
1373 struct s_client *cl = cur_client();
1374 EMM_PACKET epg;
1376 if(len < 3)
1378 return;
1381 memset(&epg, 0, sizeof(epg));
1383 epg.emmlen = SCT_LEN(buf);
1384 if(epg.emmlen > MAX_EMM_SIZE || epg.emmlen > len)
1386 return;
1389 caid = cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].caid;
1391 epg.caid[0] = (uint8_t)(caid >> 8);
1392 epg.caid[1] = (uint8_t)(caid);
1394 provid = cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].prids[0];
1396 epg.provid[0] = (uint8_t)(provid >> 24);
1397 epg.provid[1] = (uint8_t)(provid >> 16);
1398 epg.provid[2] = (uint8_t)(provid >> 8);
1399 epg.provid[3] = (uint8_t)(provid);
1401 /*if(caid == 0x0500)
1403 uint16_t emm_head;
1405 emm_head = (buf[0]<<8) | buf[1];
1406 switch( emm_head )
1408 case 0x8e70: // EMM-S
1409 memcpy(epg.hexserial+1, buf+3, 4);
1410 epg.hexserial[4]=aureader->hexserial[4];
1411 break;
1412 case 0x8870: // EMM-U
1413 case 0x8c70: // confidential?
1414 default:
1415 cs_log("unsupported emm type: %04X", emm_head);
1416 ok=0;
1419 if(!ok) cs_log("only EMM-S supported");
1421 else*/
1423 memcpy(epg.emm, buf, epg.emmlen);
1425 if(ok)
1427 do_emm(cl, &epg);
1430 // Should always send an answer to client (also if au is disabled),
1431 // some clients will disconnect if they get no answer
1432 buf[1] = 0x10;
1433 buf[2] = 0x00;
1435 network_message_send(cl->udp_fd, &cl->ncd_msgid, buf, 3, cl->ncd_skey, COMMTYPE_SERVER, 0, NULL);
1438 static void newcamd_report_cards(struct s_client *client)
1440 int32_t j, k, l;
1441 uint8_t buf[512];
1442 custom_data_t *cd;
1444 if(!cs_malloc(&cd, sizeof(struct custom_data)))
1446 return;
1449 memset(buf, 0, sizeof(buf));
1451 cd->sid = cfg.ncd_ptab.ports[client->port_idx].s_port;
1452 buf[0] = MSG_SERVER_2_CLIENT_ADDCARD;
1454 struct s_reader *rdr;
1455 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
1457 int32_t flt = 0;
1459 if(!(rdr->grp & client->grp))
1461 continue; // test - skip unaccesible readers
1464 if(rdr->ftab.filts)
1466 for(j = 0; j < rdr->ftab.nfilts; j++)
1468 if(rdr->ftab.filts[j].caid)
1470 cd->caid = rdr->ftab.filts[j].caid;
1472 if(!rdr->ftab.filts[j].nprids)
1474 cd->provid = 0;
1475 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X svc", cd->caid, cd->provid);
1477 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3,
1478 client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1481 for(k = 0; k < rdr->ftab.filts[j].nprids; k++)
1483 cd->provid = rdr->ftab.filts[j].prids[k];
1484 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X svc", cd->caid, cd->provid);
1486 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3,
1487 client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1488 flt = 1;
1494 if(rdr->caid && !flt)
1496 if((rdr->tcp_connected || rdr->card_status == CARD_INSERTED))
1498 cd->caid = rdr->caid;
1499 if(!rdr->nprov)
1501 cd->provid = 0;
1502 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X caid", cd->caid, cd->provid);
1504 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3,
1505 client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1508 for(j = 0; j < rdr->nprov; j++)
1510 cd->provid = (rdr->prid[j][1]) << 16 | (rdr->prid[j][2] << 8) | rdr->prid[j][3];
1511 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X caid", cd->caid, cd->provid);
1513 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3,
1514 client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1520 if(cfg.sidtab && client->account)
1522 struct s_sidtab *ptr;
1523 for(j = 0, ptr = cfg.sidtab; ptr; ptr = ptr->next, j++)
1525 if(client->account->sidtabs.ok & ((SIDTABBITS)1 << j))
1527 for(k = 0; k < ptr->num_caid; k++)
1529 cd->caid = ptr->caid[k];
1531 if(!ptr->num_provid)
1533 cd->provid = 0;
1534 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X acs", cd->caid, cd->provid);
1536 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3,
1537 client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1540 for(l = 0; l < ptr->num_provid; l++)
1542 cd->provid = ptr->provid[l];
1543 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X acs", cd->caid, cd->provid);
1545 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3,
1546 client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1553 NULLFREE(cd);
1556 static void newcamd_server_init(struct s_client *client)
1558 int8_t res = 0;
1560 client->ncd_server = 1;
1561 cs_log("client connected to %d port", cfg.ncd_ptab.ports[client->port_idx].s_port);
1563 if(cfg.ncd_ptab.ports[client->port_idx].ncd && cfg.ncd_ptab.ports[client->port_idx].ncd->ncd_key_is_set)
1565 // port has a des key specified
1566 res = newcamd_auth_client(client->ip, cfg.ncd_ptab.ports[client->port_idx].ncd->ncd_key);
1568 else
1570 // default global des key
1571 res = newcamd_auth_client(client->ip, cfg.ncd_key);
1574 if(res == -1)
1576 cs_disconnect_client(client);
1577 return;
1580 // report all cards if using extended mg proto
1581 if(cfg.ncd_mgclient)
1583 cs_log_dbg(D_CLIENT, "newcamd: extended: report all available cards");
1584 newcamd_report_cards(client);
1589 #define EXT_VERSION_STR "1.67"
1590 #define EXT_VERSION_LEN 4
1592 static void newcamd_send_version(struct s_client *client)
1594 uint8_t buf[30];
1595 memset(buf, 0, sizeof(buf));
1597 buf[0] = MSG_SERVER_2_CLIENT_GET_VERSION;
1598 buf[1] = EXT_VERSION_LEN >> 8;
1599 buf[2] = EXT_VERSION_LEN & 0xFF;
1600 memcpy(buf + 3, EXT_VERSION_STR, EXT_VERSION_LEN);
1602 network_message_send(client->udp_fd, &client->ncd_msgid, buf, EXT_VERSION_LEN + 3,
1603 client->ncd_skey, COMMTYPE_SERVER, 0, NULL);
1606 static void *newcamd_server(struct s_client *client, uint8_t *mbuf, int32_t len)
1608 // check for clienttimeout, if timeout occurs try to send keepalive / wait for answer
1609 // befor client was disconnected. If keepalive was disabled, exit after clienttimeout
1611 if(len < 3)
1613 return NULL;
1616 cs_log_dbg(D_CLIENT, "newcamd: got cmd %d", mbuf[2]);
1618 switch(mbuf[2])
1620 case 0x80:
1621 case 0x81:
1623 newcamd_process_ecm(client, mbuf, len);
1624 break;
1627 case MSG_SERVER_2_CLIENT_GET_VERSION:
1629 cs_log_dbg(D_CLIENT, "newcamd: extended: send Version 1.67");
1630 newcamd_send_version(client);
1631 break;
1634 case MSG_KEEPALIVE:
1636 newcamd_reply_ka();
1637 break;
1640 default:
1642 if(mbuf[2] > 0x81 && mbuf[2] < 0x92)
1644 newcamd_process_emm(mbuf + 2, len - 2);
1646 else
1648 cs_log_dbg(D_CLIENT, "unknown newcamd command! (%d)", mbuf[2]);
1653 return NULL;
1656 void newcamd_idle(void)
1658 struct s_client *client = cur_client();
1659 struct s_reader *rdr = client->reader;
1661 if(!rdr)
1663 return;
1666 if(rdr->tcp_ito > 0)
1668 // inactivitytimeout > 0 enables protocol keepalive packages
1669 time_t now;
1670 int32_t time_diff;
1672 time(&now);
1673 time_diff = llabs(now - rdr->last_s);
1675 if(time_diff > (rdr->tcp_ito))
1677 if(client->ncd_keepalive)
1679 newcamd_reply_ka();
1681 else
1683 network_tcp_connection_close(client->reader, "inactivity");
1687 else if(rdr->tcp_ito == -1)
1689 // idle reconnect
1690 newcamd_connect();
1695 * client functions
1698 int32_t newcamd_client_init(struct s_client *client)
1700 char ptxt[1] = { "\0" };
1702 client->ncd_proto = client->reader->ncd_proto;
1704 cs_log("proxy %s:%d newcamd52%d (fd=%d%s)", client->reader->device, client->reader->r_port,
1705 (client->reader->ncd_proto == NCD_525) ? 5 : 4, client->udp_fd, ptxt);
1707 // try to connect. ignore possible failures
1708 // idle reconnect (tcp_ito = -1) will trigger an additional connect anyway
1709 if(client->reader->ncd_connect_on_init && client->reader->tcp_ito != -1)
1711 newcamd_connect();
1714 return 0;
1717 static int32_t newcamd_send_ecm(struct s_client *client, ECM_REQUEST *er)
1719 struct s_reader *rdr = client->reader;
1721 if(!newcamd_connect())
1723 return -1;
1726 // check server filters
1727 if(!chk_rsfilter(rdr, er))
1729 return -1;
1732 uint8_t *buf;
1733 if(!cs_malloc(&buf, er->ecmlen))
1735 return -1;
1738 memcpy(buf, er->ecm, er->ecmlen);
1740 client->ncd_header[4] = er->srvid >> 8;
1741 client->ncd_header[5] = er->srvid & 0xFF;
1742 client->ncd_header[6] = er->caid >> 8;
1743 client->ncd_header[7] = er->caid & 0xFF;
1744 client->ncd_header[8] = er->prid >> 16;
1745 client->ncd_header[9] = er->prid >> 8;
1746 client->ncd_header[10] = er->prid & 0xFF;
1748 int32_t rc = (newcamd_send(buf, er->ecmlen, er->srvid) < 1) ? -1 : 0;
1750 NULLFREE(buf);
1751 return (rc);
1754 static int32_t newcamd_send_emm(EMM_PACKET *ep)
1756 uint8_t buf[ep->emmlen];
1758 if(!newcamd_connect())
1760 return -1;
1763 memcpy(buf, ep->emm, ep->emmlen);
1765 return (newcamd_send(buf, ep->emmlen, 0) < 1) ? 0 : 1;
1768 static int32_t newcamd_recv_chk(struct s_client *client, uint8_t *dcw, int32_t *rc, uint8_t *buf, int32_t n)
1770 uint16_t idx = -1;
1772 if(n < 5)
1774 return -1;
1777 switch(buf[2])
1779 case 0x80:
1780 case 0x81:
1782 idx = (buf[0] << 8) | buf[1];
1784 if(n == 5) // not found on server
1786 *rc = 0;
1787 memset(dcw, 0, 16);
1788 break;
1791 if(n < 21)
1793 cs_log_dbg(D_CLIENT, "invalid newcamd answer");
1794 return (-1);
1797 *rc = 1;
1798 memcpy(dcw, buf + 5, 16);
1799 break;
1802 case MSG_KEEPALIVE:
1804 return -1;
1807 case MSG_SERVER_2_CLIENT_ADDCARD:
1809 if(client->reader)
1811 client->reader->ncd_disable_server_filt = 1;
1814 return -1;
1817 default:
1819 if(buf[2] > 0x81 && buf[2] < 0x92) // answer to emm
1821 return -1;
1824 cs_log_dbg(D_CLIENT, "unknown newcamd command from server");
1825 return -1;
1829 return idx;
1833 * resolve client type for newcamd protocol
1835 const char *newcamd_get_client_name(uint16_t client_id)
1837 // When adding new entries keep the list sorted!
1838 static const struct
1840 uint16_t id;
1841 const char *client;
1842 } ncd_service_ids[] =
1844 { 0x0000, "generic" },
1845 { 0x02C2, "Opticum" },
1846 { 0x0665, "rq-sssp-client-CS" },
1847 { 0x0666, "rqcamd" },
1848 { 0x0667, "rq-echo-client" },
1849 { 0x0669, "rq-sssp-client-CW" },
1850 { 0x0769, "JlsRq" },
1851 { 0x414C, "AlexCS" },
1852 { 0x4333, "camd3" },
1853 { 0x4343, "CCcam" },
1854 { 0x434C, "Cardlink" },
1855 { 0x4453, "DiabloCam-UW" },
1856 { 0x4543, "eyetvCamd" },
1857 { 0x4765, "Octagon" },
1858 { 0x4C43, "LCE" },
1859 { 0x4E58, "NextYE2k" },
1860 { 0x5342, "SBCL" },
1861 { 0x5456, "Tecview" },
1862 { 0x5644, "vdr-sc" },
1863 { 0x5743, "WiCard" },
1864 { 0x6378, "cx" },
1865 { 0x6502, "Tvheadend" },
1866 { 0x6576, "evocamd" },
1867 { 0x6762, "gbox2CS" },
1868 { 0x6B61, "Kaffeine" },
1869 { 0x6B63, "kpcs" },
1870 { 0x6D63, "mpcs" },
1871 { 0x6D67, "mgcamd" },
1872 { 0x6E65, "NextYE2k" },
1873 { 0x6E73, "NewCS" },
1874 { 0x7264, "radegast" },
1875 { 0x7363, "Scam" },
1876 { 0x7763, "WinCSC" },
1877 { 0x7878, "tsdecrypt" },
1878 { 0x8888, "OSCam" },
1879 { 0x9911, "ACamd" },
1880 { 0x9922, "DVBplug" },
1881 { 0xFFFF, NULL }
1884 int i = 0;
1886 while(1)
1888 if(!ncd_service_ids[i].client)
1890 break;
1893 if(ncd_service_ids[i].id == client_id)
1895 return ncd_service_ids[i].client;
1898 i++;
1901 return "unknown - please report";
1904 void module_newcamd(struct s_module *ph)
1906 ph->desc = "newcamd";
1907 ph->type = MOD_CONN_TCP;
1908 ph->large_ecm_support = 1;
1909 ph->listenertype = LIS_NEWCAMD;
1910 IP_ASSIGN(ph->s_ip, cfg.ncd_srvip);
1911 ph->s_handler = newcamd_server;
1912 ph->s_init = newcamd_server_init;
1913 ph->recv = newcamd_recv;
1914 ph->send_dcw = newcamd_send_dcw;
1915 ph->ptab = cfg.ncd_ptab;
1916 ph->c_init = newcamd_client_init;
1917 ph->c_recv_chk = newcamd_recv_chk;
1918 ph->c_send_ecm = newcamd_send_ecm;
1919 ph->c_send_emm = newcamd_send_emm;
1920 ph->c_idle = newcamd_idle;
1921 ph->num = R_NEWCAMD;
1923 #endif