source: trunk/module-newcamd.c@ 8457

Last change on this file since 8457 was 8457, checked in by gf, 9 years ago

newcamd: Add paranoid checks for per port newcamd data.

Better safe than sorry.

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