revert breaks some stupid old compilers
[oscam.git] / module-newcamd.c
blobf4cff8b37d2ead087d626bdbb5f5e172a6398d79
1 #define MODULE_LOG_PREFIX "newcamd"
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"
20 const int32_t CWS_NETMSGSIZE = 1024; //csp 0.8.9 (default: 400). This is CWS_NETMSGSIZE. The old default was 240
22 #define NCD_CLIENT_ID 0x8888
24 #define CWS_FIRSTCMDNO 0xe0
25 typedef enum
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;
57 typedef enum
59 COMMTYPE_CLIENT,
60 COMMTYPE_SERVER
61 } comm_type_t;
64 typedef struct custom_data
66 uint16_t sid;
67 uint16_t caid;
68 int32_t provid;
69 uchar x;
70 } custom_data_t;
72 #define REQ_SIZE 2
75 static 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)
79 uint8_t netbuf[CWS_NETMSGSIZE];
80 int32_t head_size;
81 struct s_client *cl = cur_client();
83 head_size = (cl->ncd_proto == NCD_524) ? 8 : 12;
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)
93 if(commType == COMMTYPE_CLIENT) { (*netMsgId)++; }
94 netbuf[2] = (*netMsgId) >> 8;
95 netbuf[3] = (*netMsgId) & 0xff;
97 else
98 { netbuf[2] = netbuf[3] = 0; }
99 memset(netbuf + 4, 0, (cl->ncd_proto == NCD_524) ? 4 : 8);
100 if(sid)
102 if(cl->reader && cl->reader->ncd_disable_server_filt &&
103 sid != NCD_CLIENT_ID && cl->ncd_proto != NCD_524) //mgclient send header
105 memcpy(netbuf + 4, cl->ncd_header + 4, 7);
107 netbuf[(cl->ncd_proto == NCD_524) ? 6 : 4] = (uchar)(sid >> 8); //sid
108 netbuf[(cl->ncd_proto == NCD_524) ? 7 : 5] = (uchar)(sid);
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)
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;
124 if(NCD_525 == cl->ncd_proto && cfg.ncd_mgclient &&
125 MSG_CLIENT_2_SERVER_LOGIN_ACK == buffer[0])
127 netbuf[4] = 0x6E; //From ExtNewcamSession.java CSP line 65-66 getLoginOkMsg()
128 netbuf[5] = 0x73;
129 netbuf[11] = 0x14;
131 if(NCD_525 == cl->ncd_proto &&
132 MSG_SERVER_2_CLIENT_ADDSID == buffer[0])
134 netbuf[11] = 0x14;
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 // }
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);
150 static int32_t send_sid_list(void)
152 struct s_client *cl = cur_client();
154 if(1 != cl->ftab.nfilts || !cl->sidtabs.no || !cfg.ncd_ptab.ports[cl->port_idx].ncd)
156 cs_log("SID list will not be send to mgcamd client.");
157 return 0;
160 uchar 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;
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;
170 /*memset(mbuf, 0, sizeof(mbuf));*/ // not nessesery
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))
175 for(n = 0; n < pfilts[0].nprids; n++)
177 if(chk_srvid_match_by_caid_prov(pfilts[0].caid, pfilts[0].prids[n], sidtab))
179 for(i = 0; i < sidtab->num_srvid; i++)
181 // First SID goes to header
182 if(0 == portion_sid_num)
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
188 mbuf[portion_sid_num * 3] = (uchar)(sidtab->srvid[i] >> 8);
189 mbuf[portion_sid_num * 3 + 1] = (uchar)(sidtab->srvid[i] & 0xFF);
190 mbuf[portion_sid_num * 3 + 2] = 0x1; // mark as deny
192 ++sid_num;
193 ++portion_sid_num;
195 if(portion_sid_num >= 50)
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;
208 break;
213 if(portion_sid_num)
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;
224 cs_log("%d deny SIDs in the %d messages were sent to the client.", sid_num, portion_num);
225 return sid_num;
228 static int32_t network_message_receive(int32_t handle, uint16_t *netMsgId, uint8_t *buffer,
229 uint8_t *deskey, comm_type_t commType)
231 int32_t len, ncd_off, msgid;
232 uint8_t netbuf[CWS_NETMSGSIZE];
233 int32_t returnLen;
234 struct s_client *cl = cur_client();
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)
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;
249 if(len != 2)
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;
258 if(((netbuf[0] << 8) | netbuf[1]) > CWS_NETMSGSIZE - 2)
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;
265 len = cs_recv(handle, netbuf + 2, (netbuf[0] << 8) | netbuf[1], 0);
266 if(!len)
268 cs_log_dbg(D_CLIENT, "nmr: 2 return 0");
269 return 0;
271 if(len != ((netbuf[0] << 8) | netbuf[1]))
273 cs_log_dbg(D_CLIENT, "nmr: 2 return -1");
274 return -1;
276 len += 2;
277 if((len = nc_des_decrypt(netbuf, len, deskey)) < 11) // 15(newcamd525) or 11 ???
279 cs_log_dbg(D_CLIENT, "nmr: can't decrypt, invalid des key?");
280 cs_sleepms(2000);
281 return -1;
283 //cs_log_dump_dbg(D_CLIENT, netbuf, len, "nmr: decrypted data, len=%d", len);
284 msgid = (netbuf[2] << 8) | netbuf[3];
286 if(cl->ncd_proto == NCD_AUTO)
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;
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
298 cs_log_dbg(D_CLIENT, "nmr: 4 return -1");
299 return -1;
302 cs_log_dbg(D_CLIENT, "nmr: autodetect: newcamd52%d used", (cl->ncd_proto == NCD_525) ? 5 : 4);
305 ncd_off = (cl->ncd_proto == NCD_525) ? 4 : 0;
307 returnLen = (((netbuf[9 + ncd_off] & 0x0f) << 8) | netbuf[10 + ncd_off]) + 3;
308 if(returnLen > (len - (8 + ncd_off)))
310 cs_log_dbg(D_CLIENT, "nmr: 4 return -1");
311 return -1;
314 //cs_log_dump_dbg(D_CLIENT, netbuf, len, "nmr: decrypted data");
315 if(netMsgId)
317 switch(commType)
319 case COMMTYPE_SERVER:
320 *netMsgId = msgid;
321 break;
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;
328 break;
330 default:
331 cs_log_dbg(D_CLIENT, "nmr: 5 return -1");
332 return -1;
333 break;
336 switch(commType)
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;
350 memcpy(buffer + 2, netbuf + (8 + ncd_off), returnLen);
351 return returnLen + 2;
354 static 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)
358 uint8_t buffer[3];
360 buffer[0] = cmd;
361 buffer[1] = 0;
362 buffer[2] = 0;
363 network_message_send(handle, netMsgId, buffer, 3, deskey, commType, 0, NULL);
366 static int32_t network_cmd_no_data_receive(int32_t handle, uint16_t *netMsgId,
367 uint8_t *deskey, comm_type_t commType)
369 uint8_t buffer[CWS_NETMSGSIZE];
371 if(network_message_receive(handle, netMsgId, buffer, deskey, commType) != 3 + 2)
372 { return -1; }
373 return buffer[2];
376 void newcamd_reply_ka(void)
378 struct s_client *cl = cur_client();
380 if(!cl) { return; }
382 if(!cl->udp_fd)
384 cs_log_dbg(D_CLIENT, "invalid client fd=%d", cl->udp_fd);
385 return;
388 cs_log_dbg(D_CLIENT, "send keepalive to client fd=%d", cl->udp_fd);
390 if(cl->reader)
391 { cl->reader->last_s = time((time_t *)0); }
393 network_cmd_no_data_send(cl->udp_fd, &cl->ncd_msgid, MSG_KEEPALIVE, cl->ncd_skey, COMMTYPE_SERVER);
396 static int32_t connect_newcamd_server(void)
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;
404 uint32_t idx;
405 uchar passwdcrypt[120];
406 uint8_t login_answer;
407 int32_t bytes_received;
408 struct s_client *cl = cur_client();
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; }
414 // 1. Connect
415 handle = network_tcp_connection_open(cl->reader);
416 if(handle < 0) { return -1; }
418 // 2. Get init sequence
419 cl->ncd_msgid = 0;
420 if(read(handle, keymod, sizeof(keymod)) != sizeof(keymod))
422 cs_log("server does not return 14 bytes");
423 network_tcp_connection_close(cl->reader, "connect error");
424 return -2;
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);
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);
438 network_message_send(handle, 0, buf, idx + strlen((char *)passwdcrypt) + 1, key,
439 COMMTYPE_CLIENT, NCD_CLIENT_ID, NULL);
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)
446 cs_log("login failed for user '%s'", cl->reader->r_usr);
447 network_tcp_connection_close(cl->reader, "login error1");
448 return -3;
450 if(login_answer != MSG_CLIENT_2_SERVER_LOGIN_ACK)
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;
458 // 3.2 Set connection info
459 cl->reader->tcp_connected = 1;
460 cl->crypted = 1;
462 // 4. Send MSG_CARD_DATE_REQ
463 nc_des_login_key_get(cl->reader->ncd_key, passwdcrypt, strlen((char *)passwdcrypt), key);
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)
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;
477 // 5. Parse CAID and PROVID(s)
478 cl->reader->caid = (uint16_t)((buf[6] << 8) | buf[7]);
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++)
488 if(caid_is_betacrypt(cl->reader->caid) || caid_is_irdeto(cl->reader->caid))
490 memcpy(&cl->reader->prid[i], buf + 22 + 2 + 11 * i, 4);
492 else
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];
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]);
501 memcpy(cl->reader->ncd_skey, key, 16);
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);
508 // Only after connect() on cl->udp_fd (Linux)
509 cl->pfd = cl->udp_fd;
511 if(cl->reader->ncd_disable_server_filt) //act like mgclient
513 network_cmd_no_data_send(handle, &cl->ncd_msgid, MSG_SERVER_2_CLIENT_GET_VERSION,
514 key, COMMTYPE_CLIENT);
517 return 0;
520 static int32_t newcamd_connect(void)
522 struct s_client *cl = cur_client();
524 if(cl->reader->tcp_connected < 2 && connect_newcamd_server() < 0)
525 { return 0; }
527 if(!cl->udp_fd)
528 { return 0; }
530 return 1;
534 static int32_t newcamd_send(uchar *buf, int32_t ml, uint16_t sid)
536 struct s_client *cl = cur_client();
538 if(!newcamd_connect())
539 { return (-1); }
541 return (network_message_send(cl->udp_fd, &cl->ncd_msgid,
542 buf, ml, cl->reader->ncd_skey, COMMTYPE_CLIENT, sid, NULL));
545 static int32_t newcamd_recv(struct s_client *client, uchar *buf, int32_t UNUSED(l))
547 int32_t rc, rs;
549 if(client->typ == 'c')
551 rs = network_message_receive(client->udp_fd,
552 &client->ncd_msgid, buf,
553 client->ncd_skey, COMMTYPE_SERVER);
555 else
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);
563 if(rs < 5) { rc = (-1); }
564 else { rc = rs; }
566 cs_log_dump_dbg(D_CLIENT, buf, rs, "received %d bytes from %s", rs, remote_txt());
567 client->last = time((time_t *) 0);
569 if(rc == -1)
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()); }
576 return (rc);
579 static void mk_user_au_ftab(struct s_reader *aureader, FILTER *filt)
581 int32_t i, j, found;
582 FILTER old_filter;
584 memcpy(&old_filter, filt, sizeof(old_filter));
585 memset(filt, 0, sizeof(*filt));
587 filt->caid = aureader->caid;
588 if(filt->caid == 0)
589 filt->caid = old_filter.caid;
591 for(i = 0; i < aureader->nprov && filt->nprids < CS_MAXPROV; i++)
592 { filt->prids[filt->nprids++] = b2i(3, &aureader->prid[i][1]); }
594 for(i = 0; i < old_filter.nprids && filt->nprids < CS_MAXPROV; i++)
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]; }
603 static void mk_user_ftab(FILTER *filt)
605 int32_t port_idx, i, j, k, c;
606 struct s_client *cl = cur_client();
608 memset(filt, 0, sizeof(*filt));
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];
615 // 1. CAID
616 // search server CAID in client CAID
617 for(c = i = 0; i < cl->ctab.ctnum; i++)
619 CAIDTAB_DATA *d = &cl->ctab.ctdata[i];
620 int32_t ctab_caid = d->caid & d->mask;
621 if(ctab_caid) { c++; }
623 if(psfilt->caid == ctab_caid)
625 filt->caid = ctab_caid;
626 break;
629 if(c && !filt->caid)
631 cs_log("no valid CAID found in CAID for user '%s'", cl->account->usr);
632 return;
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);
639 if(!filt->caid && cl->ftab.nfilts)
641 int32_t fcaids;
642 for(i = fcaids = 0; i < cl->ftab.nfilts; i++)
644 uint16_t ucaid = cl->ftab.filts[i].caid;
645 if(ucaid) { fcaids++; }
646 if(ucaid && psfilt->caid == ucaid)
648 filt->caid = ucaid;
649 break;
652 if(fcaids == cl->ftab.nfilts && !filt->caid)
654 cs_log("no valid CAID found in IDENT for user '%s'", cl->account->usr);
655 //cs_disconnect_client();
656 return;
659 // empty client CAID - use server CAID
660 if(!filt->caid) { filt->caid = psfilt->caid; }
662 // 2. PROVID
663 if(!cl->ftab.nfilts)
665 int32_t add;
666 for(i = 0; i < psfilt->nprids && filt->nprids < CS_MAXPROV; i++)
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)
674 if(!rdr->ftab.nfilts)
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; }
680 else
682 for(j = 0; !add && j < rdr->ftab.nfilts; j++)
684 uint32_t rcaid = rdr->ftab.filts[j].caid;
685 if(!rcaid || rcaid == filt->caid)
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; }
693 if(add) { filt->prids[filt->nprids++] = psfilt->prids[i]; }
695 memcpy(filt, psfilt, sizeof(*filt));
696 return;
699 // search in client IDENT
700 for(j = 0; j < cl->ftab.nfilts; j++)
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)
706 for(i = 0; i < psfilt->nprids && filt->nprids < CS_MAXPROV; i++)
708 cs_log_dbg(D_CLIENT, "search server provid %d: %06X", i, psfilt->prids[i]);
709 if(cl->ftab.filts[j].nprids)
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]; }
715 else
717 filt->prids[filt->nprids++] = psfilt->prids[i];
718 // allow server PROVID(s) if no PROVID(s) specified in IDENT
724 if(!filt->nprids)
726 cs_log("no valid PROVID(s) found in CAID for user '%s'", cl->account->usr);
727 //cs_disconnect_client();
731 static int8_t newcamd_auth_client(IN_ADDR_T ip, uint8_t *deskey)
733 int32_t i, ok, rc, sid_list;
734 uchar *usr = NULL, *pwd = NULL;
735 struct s_auth *account;
736 uchar buf[14];
737 uchar key[16];
738 uchar passwdcrypt[120];
739 struct s_reader *aureader = NULL, *rdr = NULL;
740 struct s_client *cl = cur_client();
741 uchar mbuf[CWS_NETMSGSIZE];
743 sid_list = 0;
745 ok = cfg.ncd_allowed ? check_ip(cfg.ncd_allowed, ip) : 1;
747 if(!ok)
749 cs_auth_client(cl, (struct s_auth *)0, NULL);
750 return -1;
753 // make random 14 bytes
754 get_random_bytes(buf, 14);
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;
762 i = process_input(mbuf, sizeof(mbuf), cfg.cmaxidle);
763 if(i > 0)
765 if(mbuf[2] != MSG_CLIENT_2_SERVER_LOGIN)
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;
771 usr = mbuf + 5;
772 pwd = usr + strlen((char *)usr) + 1;
774 else
776 cs_log_dbg(D_CLIENT, "bad client login request");
777 return -1;
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
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)
788 cs_log("Setting TCP_KEEPIDLE failed, errno=%d, %s", errno, strerror(errno));
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."); }
792 #endif
794 if(cl->ncd_proto == NCD_525 && 0x6D == mbuf[0]
795 && 0x67 == mbuf[1] && 0x11 == cl->ncd_header[11])
797 sid_list = 1;
800 for(ok = 0, account = cfg.account; (usr) && (account) && (!ok); account = account->next)
802 cs_log_dbg(D_CLIENT, "account->usr=%s", account->usr);
803 if(strcmp((char *)usr, account->usr) == 0)
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)
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)
814 cs_log("hostname or ip mismatch for user %s (%s)", usr, client_name);
815 break;
817 else if(rc != 0)
819 cs_log("account is invalid for user %s (%s)", usr, client_name);
820 break;
822 else
824 cs_log("user %s authenticated successfully (%s)", usr, client_name);
825 ok = 1;
826 break;
829 else
830 { cs_log("user %s is providing a wrong password (%s)", usr, client_name); }
834 if(!ok && !account)
836 cs_log("user %s is trying to connect but doesnt exist ! (%s)", usr, client_name);
837 usr = 0;
840 // check for non ready reader and reject client
841 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
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)
847 if(rdr->card_status == CARD_NEED_INIT)
849 cs_log("init for reader %s not finished -> reject client", rdr->label);
850 ok = 0;
852 break;
856 if(ok)
858 LL_ITER itr = ll_iter_create(cl->aureader_list);
859 while((rdr = ll_iter_next(&itr)))
861 int32_t n;
862 if(!cfg.ncd_ptab.ports[cl->port_idx].ncd)
863 { continue; }
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) )
868 aureader = rdr;
869 break;
872 for(n = 0; n < cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].nprids; n++)
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]))
876 aureader = rdr;
877 break;
880 if(aureader)
881 { break; }
884 if(aureader)
886 cs_log("AU enabled for user %s on reader %s", usr, aureader->label);
888 else
890 cs_log("AU disabled for user %s", usr);
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);
898 if(ok)
900 FILTER usr_filter;
901 FILTER *pufilt = &usr_filter;
903 nc_des_login_key_get(deskey, passwdcrypt, strlen((char *)passwdcrypt), key);
904 memcpy(cl->ncd_skey, key, 16);
906 i = process_input(mbuf, sizeof(mbuf), cfg.cmaxidle);
907 if(i > 0)
909 int32_t j, len = 15;
910 if(mbuf[2] != MSG_CARD_DATA_REQ)
912 cs_log_dbg(D_CLIENT, "expected MSG_CARD_DATA_REQ (%02X), received %02X",
913 MSG_CARD_DATA_REQ, mbuf[2]);
914 return -1;
917 mk_user_ftab(&usr_filter);
919 // set userfilter for au enabled clients
920 if(aureader)
922 mk_user_au_ftab(aureader, &usr_filter);
925 ftab_clear(&cl->ftab);
927 if(!cfg.ncd_mgclient)
929 ftab_add(&cl->ftab, &usr_filter);
932 mbuf[0] = MSG_CARD_DATA;
933 mbuf[1] = 0x00;
934 mbuf[2] = 0x00;
936 if(aureader)
937 { mbuf[3] = 1; }
938 else
939 { mbuf[3] = get_threadnum(cl) + 10; } // Unique user number
941 mbuf[4] = (uchar)(pufilt->caid >> 8);
942 mbuf[5] = (uchar)(pufilt->caid);
943 mbuf[6] = 0x00;
944 mbuf[7] = 0x00;
946 if(aureader)
947 { hexserial_to_newcamd(aureader->hexserial, mbuf + 8, pufilt->caid); }
948 else
949 { memset(&mbuf[8], 0, 6); } //mbuf[8] - mbuf[13]
951 mbuf[14] = pufilt->nprids;
952 for(j = 0; j < pufilt->nprids; j++)
954 if(caid_is_betacrypt(pufilt->caid) || caid_is_irdeto(pufilt->caid))
956 mbuf[15 + 11 * j] = 0;
957 mbuf[16 + 11 * j] = 0;
958 mbuf[17 + 11 * j] = j;
960 else
962 mbuf[15 + 11 * j] = (uchar)(pufilt->prids[j] >> 16);
963 mbuf[16 + 11 * j] = (uchar)(pufilt->prids[j] >> 8);
964 mbuf[17 + 11 * j] = (uchar)(pufilt->prids[j]);
966 mbuf[18 + 11 * j] = 0x00;
967 mbuf[19 + 11 * j] = 0x00;
968 mbuf[20 + 11 * j] = 0x00;
969 mbuf[21 + 11 * j] = 0x00;
970 if(aureader)
972 // check if user provid from IDENT exists on card
973 int32_t k, found;
974 uint32_t rprid;
975 found = 0;
976 if(pufilt->caid == aureader->caid)
978 for(k = 0; (k < aureader->nprov); k++)
980 rprid = b2i(3, &aureader->prid[k][1]);
981 if(rprid == pufilt->prids[j])
983 if(caid_is_betacrypt(pufilt->caid) || caid_is_irdeto(pufilt->caid))
985 mbuf[22 + 11 * j] = aureader->prid[k][0];
986 mbuf[23 + 11 * j] = aureader->prid[k][1];
987 mbuf[24 + 11 * j] = aureader->prid[k][2];
988 mbuf[25 + 11 * j] = aureader->prid[k][3];
990 else
992 mbuf[22 + 11 * j] = aureader->sa[k][0];
993 mbuf[23 + 11 * j] = aureader->sa[k][1];
994 mbuf[24 + 11 * j] = aureader->sa[k][2];
995 mbuf[25 + 11 * j] = aureader->sa[k][3];
997 found = 1;
998 break;
1002 if(!found)
1004 mbuf[22 + 11 * j] = 0x00;
1005 mbuf[23 + 11 * j] = 0x00;
1006 mbuf[24 + 11 * j] = 0x00;
1007 mbuf[25 + 11 * j] = 0x00;
1010 else
1012 if(caid_is_betacrypt(pufilt->caid) || caid_is_irdeto(pufilt->caid))
1014 mbuf[22 + 11 * j] = 0x00;
1015 mbuf[23 + 11 * j] = (uchar)(pufilt->prids[j] >> 16);
1016 mbuf[24 + 11 * j] = (uchar)(pufilt->prids[j] >> 8);
1017 mbuf[25 + 11 * j] = (uchar)(pufilt->prids[j]);
1019 else
1021 mbuf[22 + 11 * j] = 0x00;
1022 mbuf[23 + 11 * j] = 0x00;
1023 mbuf[24 + 11 * j] = 0x00;
1024 mbuf[25 + 11 * j] = 0x00;
1027 len += 11;
1030 custom_data_t cd;
1031 memset(&cd, 0, sizeof(cd));
1033 if(aureader)
1035 if((aureader->blockemm & EMM_GLOBAL) && !(aureader->saveemm & EMM_GLOBAL))
1036 { cd.sid |= 4; }
1037 if((aureader->blockemm & EMM_SHARED) && !(aureader->saveemm & EMM_SHARED))
1038 { cd.sid |= 2; }
1039 if((aureader->blockemm & EMM_UNIQUE) && !(aureader->saveemm & EMM_UNIQUE))
1040 { cd.sid |= 1; }
1043 if(network_message_send(cl->udp_fd, &cl->ncd_msgid,
1044 mbuf, len, key, COMMTYPE_SERVER, 0, &cd) < 0)
1046 return -1;
1050 // send SID list
1051 if(sid_list)
1052 { send_sid_list(); }
1054 else
1056 cs_auth_client(cl, 0, usr ? "login failure" : "no such user");
1057 return -1;
1059 return 0;
1062 static void newcamd_send_dcw(struct s_client *client, ECM_REQUEST *er)
1064 int32_t len;
1065 uint16_t cl_msgid;
1066 uchar mbuf[19];
1068 if(!client->udp_fd)
1070 cs_log_dbg(D_CLIENT, "ncd_send_dcw: error: client->udp_fd=%d", client->udp_fd);
1071 return;
1074 cl_msgid = er->msgid;
1075 mbuf[0] = er->ecm[0];
1076 if(er->rc >= E_NOTFOUND /*not found*/)
1078 len = 3;
1079 mbuf[1] = mbuf[2] = 0x00;
1081 else
1083 len = 19;
1084 mbuf[1] = mbuf[2] = 0x10;
1085 memcpy(mbuf + 3, er->cw, 16);
1088 cs_log_dbg(D_CLIENT, "ncd_send_dcw: er->msgid=%d, cl_msgid=%d, %02X", er->msgid, cl_msgid, mbuf[0]);
1090 network_message_send(client->udp_fd, &cl_msgid, mbuf, len,
1091 client->ncd_skey, COMMTYPE_SERVER, 0, NULL);
1094 static void newcamd_process_ecm(struct s_client *cl, uchar *buf, int32_t len)
1096 int32_t pi;
1097 ECM_REQUEST *er;
1098 uint16_t ecmlen;
1100 if(len < 5)
1101 { return; }
1103 ecmlen = SCT_LEN((&buf[2]));
1104 if(ecmlen < 3 || ecmlen > MAX_ECM_SIZE || ecmlen+2 > len)
1106 return;
1109 if(!(er = get_ecmtask()))
1111 return;
1114 // save client ncd_msgid
1115 er->msgid = cl->ncd_msgid;
1116 er->ecmlen = ecmlen;
1117 cs_log_dbg(D_CLIENT, "ncd_process_ecm: er->msgid=%d len=%d ecmlen=%d", er->msgid, len, er->ecmlen);
1118 er->srvid = cl->ncd_header[4] << 8 | cl->ncd_header[5];
1119 er->caid = cl->ncd_header[6] << 8 | cl->ncd_header[7];
1120 er->prid = cl->ncd_header[8] << 16 | cl->ncd_header[9] << 8 | cl->ncd_header[10];
1121 if(!er->caid)
1123 pi = cl->port_idx;
1124 if(cfg.ncd_ptab.nports && cfg.ncd_ptab.nports >= pi && cfg.ncd_ptab.ports[pi].ncd)
1125 { er->caid = cfg.ncd_ptab.ports[pi].ncd->ncd_ftab.filts[0].caid; }
1127 memcpy(er->ecm, buf + 2, er->ecmlen);
1128 get_cw(cl, er);
1131 static void newcamd_process_emm(uchar *buf, int32_t len)
1133 int32_t ok = 1, provid;
1134 uint16_t caid = 0;
1135 struct s_client *cl = cur_client();
1136 EMM_PACKET epg;
1138 if(len < 3)
1139 { return; }
1141 memset(&epg, 0, sizeof(epg));
1143 epg.emmlen = SCT_LEN(buf);
1144 if(epg.emmlen > MAX_EMM_SIZE || epg.emmlen > len)
1145 { return; }
1147 caid = cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].caid;
1148 epg.caid[0] = (uchar)(caid >> 8);
1149 epg.caid[1] = (uchar)(caid);
1151 provid = cfg.ncd_ptab.ports[cl->port_idx].ncd->ncd_ftab.filts[0].prids[0];
1153 epg.provid[0] = (uchar)(provid>>24);
1154 epg.provid[1] = (uchar)(provid>>16);
1155 epg.provid[2] = (uchar)(provid>>8);
1156 epg.provid[3] = (uchar)(provid);
1158 /* if (caid == 0x0500)
1160 uint16_t emm_head;
1162 emm_head = (buf[0]<<8) | buf[1];
1163 switch( emm_head )
1165 case 0x8e70: // EMM-S
1166 memcpy(epg.hexserial+1, buf+3, 4);
1167 epg.hexserial[4]=aureader->hexserial[4];
1168 break;
1169 case 0x8870: // EMM-U
1170 case 0x8c70: // confidential ?
1171 default:
1172 cs_log("unsupported emm type: %04X", emm_head);
1173 ok=0;
1175 if( !ok ) cs_log("only EMM-S supported");
1177 else*/
1179 memcpy(epg.emm, buf, epg.emmlen);
1180 if(ok)
1181 { do_emm(cl, &epg); }
1183 // Should always send an answer to client (also if au is disabled),
1184 // some clients will disconnect if they get no answer
1185 buf[1] = 0x10;
1186 buf[2] = 0x00;
1187 network_message_send(cl->udp_fd, &cl->ncd_msgid, buf, 3,
1188 cl->ncd_skey, COMMTYPE_SERVER, 0, NULL);
1191 static void newcamd_report_cards(struct s_client *client)
1193 int32_t j, k, l;
1194 uint8_t buf[512];
1195 custom_data_t *cd;
1196 if(!cs_malloc(&cd, sizeof(struct custom_data)))
1197 { return; }
1198 memset(buf, 0, sizeof(buf));
1200 cd->sid = cfg.ncd_ptab.ports[client->port_idx].s_port;
1202 buf[0] = MSG_SERVER_2_CLIENT_ADDCARD;
1203 struct s_reader *rdr;
1204 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
1206 int32_t flt = 0;
1207 if(!(rdr->grp & client->grp)) { continue; } //test - skip unaccesible readers
1208 if(rdr->ftab.filts)
1210 for(j = 0; j < rdr->ftab.nfilts; j++)
1212 if(rdr->ftab.filts[j].caid)
1214 cd->caid = rdr->ftab.filts[j].caid;
1215 if(!rdr->ftab.filts[j].nprids)
1217 cd->provid = 0;
1218 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X svc", cd->caid, cd->provid);
1219 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1221 for(k = 0; k < rdr->ftab.filts[j].nprids; k++)
1223 cd->provid = rdr->ftab.filts[j].prids[k];
1224 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X svc", cd->caid, cd->provid);
1225 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1226 flt = 1;
1232 if(rdr->caid && !flt)
1234 if((rdr->tcp_connected || rdr->card_status == CARD_INSERTED))
1236 cd->caid = rdr->caid;
1237 if(!rdr->nprov)
1239 cd->provid = 0;
1240 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X caid", cd->caid, cd->provid);
1241 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1243 for(j = 0; j < rdr->nprov; j++)
1245 cd->provid = (rdr->prid[j][1]) << 16 | (rdr->prid[j][2] << 8) | rdr->prid[j][3];
1246 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X caid", cd->caid, cd->provid);
1247 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1252 if(cfg.sidtab && client->account)
1254 struct s_sidtab *ptr;
1255 for(j = 0, ptr = cfg.sidtab; ptr; ptr = ptr->next, j++)
1257 if(client->account->sidtabs.ok & ((SIDTABBITS)1 << j))
1258 for(k = 0; k < ptr->num_caid; k++)
1260 cd->caid = ptr->caid[k];
1261 if(!ptr->num_provid)
1263 cd->provid = 0;
1264 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X acs", cd->caid, cd->provid);
1265 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1267 for(l = 0; l < ptr->num_provid; l++)
1269 cd->provid = ptr->provid[l];
1270 cs_log_dbg(D_CLIENT, "newcamd: extended: report card %04X@%06X acs", cd->caid, cd->provid);
1271 network_message_send(client->udp_fd, &client->ncd_msgid, buf, 3, client->ncd_skey, COMMTYPE_SERVER, 0, cd);
1276 NULLFREE(cd);
1280 static void newcamd_server_init(struct s_client *client)
1282 int8_t res = 0;
1284 client->ncd_server = 1;
1285 cs_log("client connected to %d port", cfg.ncd_ptab.ports[client->port_idx].s_port);
1287 if(cfg.ncd_ptab.ports[client->port_idx].ncd && cfg.ncd_ptab.ports[client->port_idx].ncd->ncd_key_is_set)
1289 //port has a des key specified
1290 res = newcamd_auth_client(client->ip, cfg.ncd_ptab.ports[client->port_idx].ncd->ncd_key);
1292 else
1294 //default global des key
1295 res = newcamd_auth_client(client->ip, cfg.ncd_key);
1298 if(res == -1)
1300 cs_disconnect_client(client);
1301 return;
1304 // report all cards if using extended mg proto
1305 if(cfg.ncd_mgclient)
1307 cs_log_dbg(D_CLIENT, "newcamd: extended: report all available cards");
1308 newcamd_report_cards(client);
1313 #define EXT_VERSION_STR "1.67"
1314 #define EXT_VERSION_LEN 4
1316 static void newcamd_send_version(struct s_client *client)
1318 uchar buf[30];
1319 memset(buf, 0, sizeof(buf));
1320 buf[0] = MSG_SERVER_2_CLIENT_GET_VERSION;
1321 buf[1] = EXT_VERSION_LEN >> 8;
1322 buf[2] = EXT_VERSION_LEN & 0xFF;
1323 memcpy(buf + 3, EXT_VERSION_STR, EXT_VERSION_LEN);
1324 network_message_send(client->udp_fd, &client->ncd_msgid, buf, EXT_VERSION_LEN + 3, client->ncd_skey, COMMTYPE_SERVER, 0, NULL);
1327 static void *newcamd_server(struct s_client *client, uchar *mbuf, int32_t len)
1329 // check for clienttimeout, if timeout occurs try to send keepalive / wait for answer
1330 // befor client was disconnected. If keepalive was disabled, exit after clienttimeout
1332 if(len < 3)
1333 { return NULL; }
1335 cs_log_dbg(D_CLIENT, "newcamd: got cmd %d", mbuf[2]);
1337 switch(mbuf[2])
1339 case 0x80:
1340 case 0x81:
1341 newcamd_process_ecm(client, mbuf, len);
1342 break;
1344 case MSG_SERVER_2_CLIENT_GET_VERSION:
1345 cs_log_dbg(D_CLIENT, "newcamd: extended: send Version 1.67");
1346 newcamd_send_version(client);
1347 break;
1349 case MSG_KEEPALIVE:
1350 newcamd_reply_ka();
1351 break;
1353 default:
1354 if(mbuf[2] > 0x81 && mbuf[2] < 0x92)
1355 { newcamd_process_emm(mbuf + 2, len - 2); }
1356 else
1358 cs_log_dbg(D_CLIENT, "unknown newcamd command! (%d)", mbuf[2]);
1362 return NULL;
1365 void newcamd_idle(void)
1367 struct s_client *client = cur_client();
1368 struct s_reader *rdr = client->reader;
1370 if(!rdr) { return; }
1372 if(rdr->tcp_ito > 0)
1374 // inactivitytimeout > 0 enables protocol keepalive packages
1375 time_t now;
1376 int32_t time_diff;
1377 time(&now);
1378 time_diff = llabs(now - rdr->last_s);
1379 if(time_diff > (rdr->tcp_ito))
1381 if(client->ncd_keepalive)
1382 { newcamd_reply_ka(); }
1383 else
1384 { network_tcp_connection_close(client->reader, "inactivity"); }
1387 else if(rdr->tcp_ito == -1)
1389 // idle reconnect
1390 newcamd_connect();
1395 * client functions
1398 int32_t newcamd_client_init(struct s_client *client)
1401 char ptxt[1] = { "\0" };
1403 client->ncd_proto = client->reader->ncd_proto;
1405 cs_log("proxy %s:%d newcamd52%d (fd=%d%s)",
1406 client->reader->device, client->reader->r_port,
1407 (client->reader->ncd_proto == NCD_525) ? 5 : 4, client->udp_fd, ptxt);
1409 // try to connect. ignore possible failures
1410 // idle reconnect (tcp_ito = -1) will trigger an additional connect anyway
1411 if(client->reader->ncd_connect_on_init && client->reader->tcp_ito != -1)
1412 { newcamd_connect(); }
1414 return (0);
1417 static int32_t newcamd_send_ecm(struct s_client *client, ECM_REQUEST *er)
1419 struct s_reader *rdr = client->reader;
1421 if(!newcamd_connect())
1422 { return (-1); }
1424 // check server filters
1425 if(!chk_rsfilter(rdr, er))
1426 { return (-1); }
1428 uint8_t *buf;
1429 if(!cs_malloc(&buf, er->ecmlen))
1430 { return (-1); }
1432 memcpy(buf, er->ecm, er->ecmlen);
1434 client->ncd_header[4] = er->srvid >> 8;
1435 client->ncd_header[5] = er->srvid & 0xFF;
1436 client->ncd_header[6] = er->caid >> 8;
1437 client->ncd_header[7] = er->caid & 0xFF;
1438 client->ncd_header[8] = er->prid >> 16;
1439 client->ncd_header[9] = er->prid >> 8;
1440 client->ncd_header[10] = er->prid & 0xFF;
1442 int32_t rc = ((newcamd_send(buf, er->ecmlen, er->srvid) < 1) ? (-1) : 0);
1444 NULLFREE(buf);
1445 return (rc);
1449 static int32_t newcamd_send_emm(EMM_PACKET *ep)
1451 uchar buf[ep->emmlen];
1453 if(!newcamd_connect())
1454 { return (-1); }
1456 memcpy(buf, ep->emm, ep->emmlen);
1457 return ((newcamd_send(buf, ep->emmlen, 0) < 1) ? 0 : 1);
1460 static int32_t newcamd_recv_chk(struct s_client *client, uchar *dcw, int32_t *rc, uchar *buf, int32_t n)
1462 uint16_t idx = -1;
1464 if(n < 5)
1465 { return -1; }
1467 switch(buf[2])
1469 case 0x80:
1470 case 0x81:
1471 idx = (buf[0] << 8) | buf[1];
1472 if(n == 5) //not found on server
1474 *rc = 0;
1475 memset(dcw, 0, 16);
1476 break;
1479 if(n < 21)
1481 cs_log_dbg(D_CLIENT, "invalid newcamd answer");
1482 return (-1);
1485 *rc = 1;
1486 memcpy(dcw, buf + 5, 16);
1487 break;
1489 case MSG_KEEPALIVE:
1490 return -1;
1492 case MSG_SERVER_2_CLIENT_ADDCARD:
1493 if(client->reader)
1495 client->reader->ncd_disable_server_filt = 1;
1497 return -1;
1499 default:
1500 if(buf[2] > 0x81 && buf[2] < 0x92) //answer to emm
1502 return -1;
1504 cs_log_dbg(D_CLIENT, "unknown newcamd command from server");
1505 return -1;
1507 return (idx);
1511 * resolve client type for newcamd protocol
1513 const char *newcamd_get_client_name(uint16_t client_id)
1515 // When adding new entries keep the list sorted!
1516 static const struct
1518 uint16_t id;
1519 const char *client;
1520 } ncd_service_ids[] =
1522 { 0x0000, "generic" },
1523 { 0x02C2, "Opticum" },
1524 { 0x0665, "rq-sssp-client-CS" },
1525 { 0x0666, "rqcamd" },
1526 { 0x0667, "rq-echo-client" },
1527 { 0x0669, "rq-sssp-client-CW" },
1528 { 0x0769, "JlsRq" },
1529 { 0x414C, "AlexCS" },
1530 { 0x4333, "camd3" },
1531 { 0x4343, "CCcam" },
1532 { 0x434C, "Cardlink" },
1533 { 0x4453, "DiabloCam-UW" },
1534 { 0x4543, "eyetvCamd" },
1535 { 0x4765, "Octagon" },
1536 { 0x4C43, "LCE" },
1537 { 0x4E58, "NextYE2k" },
1538 { 0x5342, "SBCL" },
1539 { 0x5456, "Tecview" },
1540 { 0x5644, "vdr-sc" },
1541 { 0x5743, "WiCard" },
1542 { 0x6378, "cx" },
1543 { 0x6502, "Tvheadend" },
1544 { 0x6576, "evocamd" },
1545 { 0x6762, "gbox2CS" },
1546 { 0x6B61, "Kaffeine" },
1547 { 0x6B63, "kpcs" },
1548 { 0x6D63, "mpcs" },
1549 { 0x6D67, "mgcamd" },
1550 { 0x6E65, "NextYE2k" },
1551 { 0x6E73, "NewCS" },
1552 { 0x7264, "radegast" },
1553 { 0x7363, "Scam" },
1554 { 0x7763, "WinCSC" },
1555 { 0x7878, "tsdecrypt" },
1556 { 0x8888, "OSCam" },
1557 { 0x9911, "ACamd" },
1558 { 0x9922, "DVBplug" },
1559 { 0xFFFF, NULL }
1561 int i = 0;
1562 while(1)
1564 if(!ncd_service_ids[i].client)
1565 { break; }
1566 if(ncd_service_ids[i].id == client_id)
1567 { return ncd_service_ids[i].client; }
1568 i++;
1570 return "unknown - please report";
1573 void module_newcamd(struct s_module *ph)
1575 ph->desc = "newcamd";
1576 ph->type = MOD_CONN_TCP;
1577 ph->large_ecm_support = 1;
1578 ph->listenertype = LIS_NEWCAMD;
1579 IP_ASSIGN(ph->s_ip, cfg.ncd_srvip);
1580 ph->s_handler = newcamd_server;
1581 ph->s_init = newcamd_server_init;
1582 ph->recv = newcamd_recv;
1583 ph->send_dcw = newcamd_send_dcw;
1584 ph->ptab = cfg.ncd_ptab;
1585 ph->c_init = newcamd_client_init;
1586 ph->c_recv_chk = newcamd_recv_chk;
1587 ph->c_send_ecm = newcamd_send_ecm;
1588 ph->c_send_emm = newcamd_send_emm;
1589 ph->c_idle = newcamd_idle;
1590 ph->num = R_NEWCAMD;
1592 #endif