1 #define MODULE_LOG_PREFIX "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
23 int32_t portion_sid_num
= 0;
25 #define NCD_CLIENT_ID 0x8888
26 #define CWS_FIRSTCMDNO 0xE0
30 MSG_CLIENT_2_SERVER_LOGIN
= CWS_FIRSTCMDNO
,
31 MSG_CLIENT_2_SERVER_LOGIN_ACK
,
32 MSG_CLIENT_2_SERVER_LOGIN_NAK
,
35 MSG_SERVER_2_CLIENT_NAME
,
36 MSG_SERVER_2_CLIENT_NAME_ACK
,
37 MSG_SERVER_2_CLIENT_NAME_NAK
,
38 MSG_SERVER_2_CLIENT_LOGIN
,
39 MSG_SERVER_2_CLIENT_LOGIN_ACK
,
40 MSG_SERVER_2_CLIENT_LOGIN_NAK
,
47 MSG_ADMIN_COMMAND_ACK
,
48 MSG_ADMIN_COMMAND_NAK
,
49 MSG_KEEPALIVE
= CWS_FIRSTCMDNO
+ 0x1D,
50 MSG_SERVER_2_CLIENT_OSD
= 0xD1,
51 MSG_SERVER_2_CLIENT_ALLCARDS
= 0xD2,
52 MSG_SERVER_2_CLIENT_ADDCARD
= 0xD3,
53 MSG_SERVER_2_CLIENT_REMOVECARD
= 0xD4,
54 MSG_SERVER_2_CLIENT_CHANGE_KEY
= 0xD5,
55 MSG_SERVER_2_CLIENT_GET_VERSION
= 0xD6,
56 MSG_SERVER_2_CLIENT_ADDSID
= 0xD7,
57 MSG_CLIENT_2_SERVER_CARDDISCOVER
= 0xD8
66 typedef struct custom_data
76 static int32_t network_message_send(int32_t handle
, uint16_t *netMsgId
, uint8_t *buffer
, int32_t len
,
77 uint8_t *deskey
, comm_type_t commType
, uint16_t sid
, custom_data_t
*cd
)
79 uint8_t netbuf
[CWS_NETMSGSIZE
];
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)
90 buffer
[1] = (buffer
[1] & 0xF0) | (((len
- 3) >> 8) & 0x0F);
91 buffer
[2] = (len
- 3) & 0xFF;
93 memcpy(netbuf
+ head_size
, buffer
, len
);
98 if(commType
== COMMTYPE_CLIENT
)
103 netbuf
[2] = (*netMsgId
) >> 8;
104 netbuf
[3] = (*netMsgId
) & 0xFF;
112 memset(netbuf
+ 4, 0, (cl
->ncd_proto
== NCD_524
) ? 4 : 8);
116 if(cl
->reader
&& cl
->reader
->ncd_disable_server_filt
117 && sid
!= NCD_CLIENT_ID
&& cl
->ncd_proto
!= NCD_524
) // mgclient send header
119 memcpy(netbuf
+ 4, cl
->ncd_header
+ 4, 7);
122 netbuf
[(cl
->ncd_proto
== NCD_524
) ? 6 : 4] = (uint8_t)(sid
>> 8); // sid
123 netbuf
[(cl
->ncd_proto
== NCD_524
) ? 7 : 5] = (uint8_t)(sid
);
126 //if((!ncd_proto == NCD_524) && (buffer[0] >= 0xD1) && (buffer[0] <= 0xD8)) // extended proto for mg
128 // cs_log_dbg(D_CLIENT, "newcamd: extended: msg");
132 cs_log_dbg(D_CLIENT
, "newcamd: has cd");
134 netbuf
[4] = cd
->sid
>> 8;
135 netbuf
[5] = cd
->sid
& 0xFF;
136 netbuf
[6] = cd
->caid
>> 8;
137 netbuf
[7] = cd
->caid
& 0xFF;
138 netbuf
[8] = (cd
->provid
>> 16) & 0xFF;
139 netbuf
[9] = (cd
->provid
>> 8) & 0xFF;
140 netbuf
[10] = cd
->provid
& 0xFF;
144 if(NCD_525
== cl
->ncd_proto
&& cfg
.ncd_mgclient
&& MSG_CLIENT_2_SERVER_LOGIN_ACK
== buffer
[0])
146 netbuf
[4] = 0x6E; // From ExtNewcamSession.java CSP line 65-66 getLoginOkMsg()
151 if(NCD_525
== cl
->ncd_proto
&& MSG_SERVER_2_CLIENT_ADDSID
== buffer
[0])
156 //if(buffer[0] == MSG_CLIENT_2_SERVER_LOGIN && cur_client()->reader->ncd_disable_server_filt)
158 // netbuf[11] = 0x11; // From ChameleonCwsConnector.java CSP line 59-61 run()
161 netbuf
[0] = (len
- 2) >> 8;
162 netbuf
[1] = (len
- 2) & 0xFF;
164 cs_log_dump_dbg(D_CLIENT
, netbuf
, len
, "send %d bytes to %s", len
, remote_txt());
166 if((len
= nc_des_encrypt(netbuf
, len
, deskey
)) < 0)
171 netbuf
[0] = (len
- 2) >> 8;
172 netbuf
[1] = (len
- 2) & 0xFF;
174 return send(handle
, netbuf
, len
, 0);
177 static int32_t send_sid_list(void)
179 struct s_client
*cl
= cur_client();
181 if(1 != cl
->ftab
.nfilts
|| !cl
->sidtabs
.no
|| !cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
)
183 cs_log("SID list will not be send to mgcamd client.");
187 uint8_t mbuf
[CWS_NETMSGSIZE
];
188 int32_t n
= 0, nr
= 0, i
= 0, sid_num
= 0, portion_num
= 0;
192 cs_log_dbg(D_TRACE
, "Send SID list to mgcamd client.");
194 memset(&cd
, 0, sizeof(cd
));
195 FILTER
*pfilts
= cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
;
197 //memset(mbuf, 0, sizeof(mbuf)); // not nessesery
199 for(nr
= 0, sidtab
= cfg
.sidtab
; sidtab
; sidtab
= sidtab
->next
, nr
++)
201 if((cl
->sidtabs
.no
& ((SIDTABBITS
)1 << nr
)) && (sidtab
->num_caid
| sidtab
->num_provid
| sidtab
->num_srvid
))
203 for(n
= 0; n
< pfilts
[0].nprids
; n
++)
205 if(chk_srvid_match_by_caid_prov(pfilts
[0].caid
, pfilts
[0].prids
[n
], sidtab
))
207 for(i
= 0; i
< sidtab
->num_srvid
; i
++)
209 // First SID goes to header
210 if(0 == portion_sid_num
)
212 cd
.sid
= sidtab
->srvid
[i
]; // first sid
213 cd
.caid
= cfg
.ncd_ptab
.ports
[cl
->port_idx
].s_port
; // assigned port
214 cd
.provid
= 0x1; // mark as deny
217 mbuf
[portion_sid_num
* 3] = (uint8_t)(sidtab
->srvid
[i
] >> 8);
218 mbuf
[portion_sid_num
* 3 + 1] = (uint8_t)(sidtab
->srvid
[i
] & 0xFF);
219 mbuf
[portion_sid_num
* 3 + 2] = 0x1; // mark as deny
224 if(portion_sid_num
>= 50)
228 cs_log_dump_dbg(0x0800, mbuf
, (portion_sid_num
) * 3, "Portion %d contains %d SIDs",
229 portion_num
, portion_sid_num
);
231 mbuf
[0] = MSG_SERVER_2_CLIENT_ADDSID
;
235 network_message_send(cl
->udp_fd
, &cl
->ncd_msgid
, mbuf
, portion_sid_num
* 3,
236 cl
->ncd_skey
, COMMTYPE_SERVER
, 0, &cd
);
251 cs_log_dump_dbg(0x0800, mbuf
, (portion_sid_num
) * 3, "Portion %d contains %d SIDs",
252 portion_num
, portion_sid_num
);
254 mbuf
[0] = MSG_SERVER_2_CLIENT_ADDSID
;
258 network_message_send(cl
->udp_fd
, &cl
->ncd_msgid
, mbuf
, portion_sid_num
* 3,
259 cl
->ncd_skey
, COMMTYPE_SERVER
, 0, &cd
);
263 cs_log("%d deny SIDs in the %d messages were sent to the client.", sid_num
, portion_num
);
268 static int32_t network_message_receive(int32_t handle
, uint16_t *netMsgId
, uint8_t *buffer
,
269 uint8_t *deskey
, comm_type_t commType
)
271 int32_t len
, ncd_off
, msgid
;
272 uint8_t netbuf
[CWS_NETMSGSIZE
];
274 struct s_client
*cl
= cur_client();
276 if(!buffer
|| handle
< 0)
281 len
= cs_recv(handle
, netbuf
, 2, 0);
283 cs_log_dbg(D_CLIENT
, "nmr(): len=%d, errno=%d", len
, (len
== -1) ? errno
: 0);
287 cs_log_dbg(D_CLIENT
, "nmr: 1 return 0");
289 if(commType
== COMMTYPE_CLIENT
)
291 network_tcp_connection_close(cl
->reader
, "receive error1");
295 cs_disconnect_client(cl
);
303 cs_log_dbg(D_CLIENT
, "nmr: len!=2");
305 if(commType
== COMMTYPE_CLIENT
)
307 network_tcp_connection_close(cl
->reader
, "receive error2");
311 cs_disconnect_client(cl
);
317 if(((netbuf
[0] << 8) | netbuf
[1]) > CWS_NETMSGSIZE
- 2)
319 cs_log_dbg(D_CLIENT
, "nmr: received data len=%d longer than CWS_NETMSGSIZE=%d",
320 ((netbuf
[0] << 8) | netbuf
[1]), CWS_NETMSGSIZE
);
321 cs_log_dbg(D_CLIENT
, "nmr: 1 return -1");
325 len
= cs_recv(handle
, netbuf
+ 2, (netbuf
[0] << 8) | netbuf
[1], 0);
328 cs_log_dbg(D_CLIENT
, "nmr: 2 return 0");
332 if(len
!= ((netbuf
[0] << 8) | netbuf
[1]))
334 cs_log_dbg(D_CLIENT
, "nmr: 2 return -1");
340 if((len
= nc_des_decrypt(netbuf
, len
, deskey
)) < 11) // 15(newcamd525) or 11 ???
342 cs_log_dbg(D_CLIENT
, "nmr: can't decrypt, invalid des key?");
347 //cs_log_dump_dbg(D_CLIENT, netbuf, len, "nmr: decrypted data, len=%d", len);
349 msgid
= (netbuf
[2] << 8) | netbuf
[3];
351 if(cl
->ncd_proto
== NCD_AUTO
)
354 int32_t l5
= (((netbuf
[13] & 0x0F) << 8) | netbuf
[14]) + 3;
355 int32_t l4
= (((netbuf
[9] & 0x0F) << 8) | netbuf
[10]) + 3;
357 if((l5
<= len
- 12) && ((netbuf
[12] & 0xF0) == 0xE0 || (netbuf
[12] & 0xF0) == 0x80))
359 cl
->ncd_proto
= NCD_525
;
361 else if((l4
<= len
- 8) && ((netbuf
[8] & 0xF0) == 0xE0 || (netbuf
[9] & 0xF0) == 0x80))
363 cl
->ncd_proto
= NCD_524
;
367 cs_log_dbg(D_CLIENT
, "nmr: 4 return -1");
371 cs_log_dbg(D_CLIENT
, "nmr: autodetect: newcamd52%d used", (cl
->ncd_proto
== NCD_525
) ? 5 : 4);
374 ncd_off
= (cl
->ncd_proto
== NCD_525
) ? 4 : 0;
376 returnLen
= (((netbuf
[9 + ncd_off
] & 0x0F) << 8) | netbuf
[10 + ncd_off
]) + 3;
377 if(returnLen
> (len
- (8 + ncd_off
)))
379 cs_log_dbg(D_CLIENT
, "nmr: 4 return -1");
383 //cs_log_dump_dbg(D_CLIENT, netbuf, len, "nmr: decrypted data");
389 case COMMTYPE_SERVER
:
393 case COMMTYPE_CLIENT
:
394 //if(*netMsgId != ((netbuf[2] << 8) | netbuf[3]))
396 cs_log_dbg(D_CLIENT
, "nmr: netMsgId=%d, from server=%d, ", *netMsgId
, msgid
);
402 cs_log_dbg(D_CLIENT
, "nmr: 5 return -1");
410 case COMMTYPE_SERVER
:
411 memcpy(cl
->ncd_header
, netbuf
, (8 + ncd_off
));
412 buffer
[0] = (cl
->ncd_proto
== NCD_525
) ? netbuf
[4] : netbuf
[6]; // sid
413 buffer
[1] = (cl
->ncd_proto
== NCD_525
) ? netbuf
[5] : netbuf
[7];
416 case COMMTYPE_CLIENT
:
417 memcpy(cl
->ncd_header
, netbuf
, (8 + ncd_off
));
418 buffer
[0] = netbuf
[2]; // msgid
419 buffer
[1] = netbuf
[3];
423 memcpy(buffer
+ 2, netbuf
+ (8 + ncd_off
), returnLen
);
424 return returnLen
+ 2;
427 static void network_cmd_no_data_send(int32_t handle
, uint16_t *netMsgId
, net_msg_type_t cmd
,
428 uint8_t *deskey
, comm_type_t commType
)
436 network_message_send(handle
, netMsgId
, buffer
, 3, deskey
, commType
, 0, NULL
);
439 static int32_t network_cmd_no_data_receive(int32_t handle
, uint16_t *netMsgId
, uint8_t *deskey
,
440 comm_type_t commType
)
442 uint8_t buffer
[CWS_NETMSGSIZE
];
444 if(network_message_receive(handle
, netMsgId
, buffer
, deskey
, commType
) != 3 + 2)
452 void newcamd_reply_ka(void)
454 struct s_client
*cl
= cur_client();
463 cs_log_dbg(D_CLIENT
, "invalid client fd=%d", cl
->udp_fd
);
467 cs_log_dbg(D_CLIENT
, "send keepalive to client fd=%d", cl
->udp_fd
);
471 cl
->reader
->last_s
= time((time_t *)0);
474 network_cmd_no_data_send(cl
->udp_fd
, &cl
->ncd_msgid
, MSG_KEEPALIVE
, cl
->ncd_skey
, COMMTYPE_SERVER
);
477 static int32_t connect_newcamd_server(void)
480 uint8_t buf
[CWS_NETMSGSIZE
];
486 uint8_t passwdcrypt
[120];
487 uint8_t login_answer
;
488 int32_t bytes_received
;
489 struct s_client
*cl
= cur_client();
491 if(cl
->reader
->device
[0] == 0 || cl
->reader
->r_pwd
[0] == 0
492 || cl
->reader
->r_usr
[0] == 0 || cl
->reader
->r_port
== 0)
498 handle
= network_tcp_connection_open(cl
->reader
);
504 // 2. Get init sequence
506 if(read(handle
, keymod
, sizeof(keymod
)) != sizeof(keymod
))
508 cs_log("server does not return 14 bytes");
509 network_tcp_connection_close(cl
->reader
, "connect error");
513 cs_log_dump_dbg(D_CLIENT
, keymod
, sizeof(cl
->reader
->ncd_key
), "server init sequence:");
514 nc_des_login_key_get(keymod
, cl
->reader
->ncd_key
, sizeof(cl
->reader
->ncd_key
), key
);
516 // 3. Send login info
518 buf
[0] = MSG_CLIENT_2_SERVER_LOGIN
;
521 cs_strncpy((char *)buf
+ idx
, cl
->reader
->r_usr
, sizeof(buf
) - idx
);
522 __md5_crypt(cl
->reader
->r_pwd
, "$1$abcdefgh$", (char *)passwdcrypt
);
523 idx
+= cs_strlen(cl
->reader
->r_usr
) + 1;
524 cs_strncpy((char *)buf
+ idx
, (const char *)passwdcrypt
, sizeof(buf
) - idx
);
526 network_message_send(handle
, 0, buf
, idx
+ cs_strlen((char *)passwdcrypt
) + 1, key
,
527 COMMTYPE_CLIENT
, NCD_CLIENT_ID
, NULL
);
529 // 3.1 Get login answer
530 login_answer
= network_cmd_no_data_receive(handle
, &cl
->ncd_msgid
, key
, COMMTYPE_CLIENT
);
531 if(login_answer
== MSG_CLIENT_2_SERVER_LOGIN_NAK
)
533 cs_log("login failed for user '%s'", cl
->reader
->r_usr
);
534 network_tcp_connection_close(cl
->reader
, "login error1");
538 if(login_answer
!= MSG_CLIENT_2_SERVER_LOGIN_ACK
)
540 cs_log("expected MSG_CLIENT_2_SERVER_LOGIN_ACK (%02X), received %02X",
541 MSG_CLIENT_2_SERVER_LOGIN_ACK
, login_answer
);
543 network_tcp_connection_close(cl
->reader
, "login error2");
547 // 3.2 Set connection info
548 cl
->reader
->tcp_connected
= 1;
551 // 4. Send MSG_CARD_DATE_REQ
552 nc_des_login_key_get(cl
->reader
->ncd_key
, passwdcrypt
, cs_strlen((char *)passwdcrypt
), key
);
553 network_cmd_no_data_send(handle
, &cl
->ncd_msgid
, MSG_CARD_DATA_REQ
, key
, COMMTYPE_CLIENT
);
555 bytes_received
= network_message_receive(handle
, &cl
->ncd_msgid
, buf
, key
, COMMTYPE_CLIENT
);
556 if(bytes_received
< 16 || buf
[2] != MSG_CARD_DATA
)
558 cs_log("expected MSG_CARD_DATA (%02X), received %02X", MSG_CARD_DATA
, buf
[2]);
560 network_tcp_connection_close(cl
->reader
, "receive error");
564 // 5. Parse CAID and PROVID(s)
565 cl
->reader
->caid
= (uint16_t)((buf
[6] << 8) | buf
[7]);
567 // handle special serial format in newcamd. See newcamd_auth_client
568 newcamd_to_hexserial(buf
+ 10, cl
->reader
->hexserial
, cl
->reader
->caid
);
570 cs_log("Newcamd Server: %s:%d - UserID: %i", cl
->reader
->device
, cl
->reader
->r_port
, buf
[3 + 2]);
572 cs_log("CAID: %04X - UA: %02X%02X%02X%02X%02X%02X%02X%02X - Provider # %i",
573 cl
->reader
->caid
, cl
->reader
->hexserial
[0], cl
->reader
->hexserial
[1], cl
->reader
->hexserial
[2],
574 cl
->reader
->hexserial
[3], cl
->reader
->hexserial
[4], cl
->reader
->hexserial
[5], cl
->reader
->hexserial
[6],
575 cl
->reader
->hexserial
[7], buf
[14 + 2]);
577 cl
->reader
->nprov
= buf
[14 + 2];
578 memset(cl
->reader
->prid
, 0x00, sizeof(cl
->reader
->prid
));
580 for(i
= 0; i
< cl
->reader
->nprov
; i
++)
582 if(caid_is_betacrypt(cl
->reader
->caid
) || caid_is_irdeto(cl
->reader
->caid
))
584 memcpy(&cl
->reader
->prid
[i
], buf
+ 22 + 2 + 11 * i
, 4);
588 cl
->reader
->prid
[i
][1] = buf
[15 + 2 + 11 * i
];
589 cl
->reader
->prid
[i
][2] = buf
[16 + 2 + 11 * i
];
590 cl
->reader
->prid
[i
][3] = buf
[17 + 2 + 11 * i
];
593 memcpy(&cl
->reader
->sa
[i
], buf
+ 22 + 2 + 11 * i
, 4); // the 4 first bytes are not read
595 cs_log("Provider ID: %02X%02X%02X - SA: %02X%02X%02X%02X",
596 cl
->reader
->prid
[i
][1], cl
->reader
->prid
[i
][2], cl
->reader
->prid
[i
][3], cl
->reader
->sa
[i
][0],
597 cl
->reader
->sa
[i
][1], cl
->reader
->sa
[i
][2], cl
->reader
->sa
[i
][3]);
600 memcpy(cl
->reader
->ncd_skey
, key
, 16);
602 // 6. Set card inserted
603 cl
->reader
->tcp_connected
= 2;
604 cl
->reader
->card_status
= CARD_INSERTED
;
605 cl
->reader
->last_g
= cl
->reader
->last_s
= time((time_t *)0);
607 // Only after connect() on cl->udp_fd (Linux)
608 cl
->pfd
= cl
->udp_fd
;
610 if(cl
->reader
->ncd_disable_server_filt
) // act like mgclient
612 network_cmd_no_data_send(handle
, &cl
->ncd_msgid
, MSG_SERVER_2_CLIENT_GET_VERSION
, key
, COMMTYPE_CLIENT
);
618 static int32_t newcamd_connect(void)
620 struct s_client
*cl
= cur_client();
622 if(cl
->reader
->tcp_connected
< 2 && connect_newcamd_server() < 0)
635 static int32_t newcamd_send(uint8_t *buf
, int32_t ml
, uint16_t sid
)
637 struct s_client
*cl
= cur_client();
639 if(!newcamd_connect())
644 return network_message_send(cl
->udp_fd
, &cl
->ncd_msgid
, buf
, ml
,
645 cl
->reader
->ncd_skey
, COMMTYPE_CLIENT
, sid
, NULL
);
648 static int32_t newcamd_recv(struct s_client
*client
, uint8_t *buf
, int32_t UNUSED(l
))
652 if(client
->typ
== 'c')
654 rs
= network_message_receive(client
->udp_fd
, &client
->ncd_msgid
, buf
,
655 client
->ncd_skey
, COMMTYPE_SERVER
);
664 rs
= network_message_receive(client
->udp_fd
, &client
->ncd_msgid
, buf
,
665 client
->reader
->ncd_skey
, COMMTYPE_CLIENT
);
677 cs_log_dump_dbg(D_CLIENT
, buf
, rs
, "received %d bytes from %s", rs
, remote_txt());
679 client
->last
= time((time_t *) 0);
685 cs_log("packet is too small (%d bytes)", rs
);
689 cs_log("Connection closed to %s", remote_txt());
696 static void mk_user_au_ftab(struct s_reader
*aureader
, FILTER
*filt
)
701 memcpy(&old_filter
, filt
, sizeof(old_filter
));
702 memset(filt
, 0, sizeof(*filt
));
704 filt
->caid
= aureader
->caid
;
707 filt
->caid
= old_filter
.caid
;
710 for(i
= 0; i
< aureader
->nprov
&& filt
->nprids
< CS_MAXPROV
; i
++)
712 filt
->prids
[filt
->nprids
++] = b2i(3, &aureader
->prid
[i
][1]);
715 for(i
= 0; i
< old_filter
.nprids
&& filt
->nprids
< CS_MAXPROV
; i
++)
717 for(j
= found
= 0; (!found
) && (j
< filt
->nprids
); j
++)
719 if(old_filter
.prids
[i
] == filt
->prids
[j
])
727 filt
->prids
[filt
->nprids
++] = old_filter
.prids
[i
];
732 static void mk_user_ftab(FILTER
*filt
)
734 int32_t port_idx
, i
, j
, k
, c
;
735 struct s_client
*cl
= cur_client();
737 memset(filt
, 0, sizeof(*filt
));
739 port_idx
= cl
->port_idx
;
740 if(!cfg
.ncd_ptab
.ports
[port_idx
].ncd
)
745 FILTER
*psfilt
= &cfg
.ncd_ptab
.ports
[port_idx
].ncd
->ncd_ftab
.filts
[0];
748 // search server CAID in client CAID
749 for(c
= i
= 0; i
< cl
->ctab
.ctnum
; i
++)
751 CAIDTAB_DATA
*d
= &cl
->ctab
.ctdata
[i
];
753 int32_t ctab_caid
= d
->caid
& d
->mask
;
759 if(psfilt
->caid
== ctab_caid
)
761 filt
->caid
= ctab_caid
;
768 cs_log("no valid CAID found in CAID for user '%s'", cl
->account
->usr
);
772 // search CAID in client IDENT
773 cs_log_dbg(D_CLIENT
, "client[%8lX].%s nfilts=%d, filt.caid=%04X", (unsigned long)pthread_self(),
774 cl
->account
->usr
, cl
->ftab
.nfilts
, filt
->caid
);
776 if(!filt
->caid
&& cl
->ftab
.nfilts
)
779 for(i
= fcaids
= 0; i
< cl
->ftab
.nfilts
; i
++)
781 uint16_t ucaid
= cl
->ftab
.filts
[i
].caid
;
787 if(ucaid
&& psfilt
->caid
== ucaid
)
794 if(fcaids
== cl
->ftab
.nfilts
&& !filt
->caid
)
796 cs_log("no valid CAID found in IDENT for user '%s'", cl
->account
->usr
);
797 //cs_disconnect_client();
802 // empty client CAID - use server CAID
805 filt
->caid
= psfilt
->caid
;
812 for(i
= 0; i
< psfilt
->nprids
&& filt
->nprids
< CS_MAXPROV
; i
++)
814 // use server PROVID(s) (and only those which are in user's groups)
816 struct s_reader
*rdr
;
818 for(rdr
= first_active_reader
; rdr
; rdr
= rdr
->next
)
820 if(rdr
->grp
& cl
->grp
)
822 if(!rdr
->ftab
.nfilts
)
824 if(is_network_reader(rdr
))
829 for(j
= 0; !add
&& j
< rdr
->nprov
; j
++)
831 if(b2i(3, &rdr
->prid
[j
][1]) == psfilt
->prids
[i
])
839 for(j
= 0; !add
&& j
< rdr
->ftab
.nfilts
; j
++)
841 uint32_t rcaid
= rdr
->ftab
.filts
[j
].caid
;
842 if(!rcaid
|| rcaid
== filt
->caid
)
844 for(k
= 0; !add
&& k
< rdr
->ftab
.filts
[j
].nprids
; k
++)
846 if(rdr
->ftab
.filts
[j
].prids
[k
] == psfilt
->prids
[i
])
859 filt
->prids
[filt
->nprids
++] = psfilt
->prids
[i
];
863 memcpy(filt
, psfilt
, sizeof(*filt
));
867 // search in client IDENT
868 for(j
= 0; j
< cl
->ftab
.nfilts
; j
++)
870 uint32_t ucaid
= cl
->ftab
.filts
[j
].caid
;
872 cs_log_dbg(D_CLIENT
, "client caid %d: %04X", j
, ucaid
);
874 if(!ucaid
|| ucaid
== filt
->caid
)
876 for(i
= 0; i
< psfilt
->nprids
&& filt
->nprids
< CS_MAXPROV
; i
++)
878 cs_log_dbg(D_CLIENT
, "search server provid %d: %06X", i
, psfilt
->prids
[i
]);
880 if(cl
->ftab
.filts
[j
].nprids
)
882 for(k
= 0; k
< cl
->ftab
.filts
[j
].nprids
&& filt
->nprids
< CS_MAXPROV
; k
++)
884 if(cl
->ftab
.filts
[j
].prids
[k
] == psfilt
->prids
[i
])
886 filt
->prids
[filt
->nprids
++] = cl
->ftab
.filts
[j
].prids
[k
];
892 filt
->prids
[filt
->nprids
++] = psfilt
->prids
[i
];
893 // allow server PROVID(s) if no PROVID(s) specified in IDENT
901 cs_log("no valid PROVID(s) found in CAID for user '%s'", cl
->account
->usr
);
902 //cs_disconnect_client();
906 static int8_t newcamd_auth_client(IN_ADDR_T ip
, uint8_t *deskey
)
908 int32_t i
, ok
, rc
, sid_list
;
909 uint8_t *usr
= NULL
, *pwd
= NULL
;
910 struct s_auth
*account
;
913 uint8_t passwdcrypt
[120];
914 struct s_reader
*aureader
= NULL
, *rdr
= NULL
;
915 struct s_client
*cl
= cur_client();
916 uint8_t mbuf
[CWS_NETMSGSIZE
];
920 ok
= cfg
.ncd_allowed
? check_ip(cfg
.ncd_allowed
, ip
) : 1;
924 cs_auth_client(cl
, (struct s_auth
*)0, NULL
);
928 // make random 14 bytes
929 get_random_bytes(buf
, 14);
931 // send init sequence
932 send(cl
->udp_fd
, buf
, 14, 0);
933 nc_des_login_key_get(buf
, deskey
, 14, key
);
934 memcpy(cl
->ncd_skey
, key
, 16);
937 i
= process_input(mbuf
, sizeof(mbuf
), cfg
.cmaxidle
);
940 if(mbuf
[2] != MSG_CLIENT_2_SERVER_LOGIN
)
942 cs_log_dbg(D_CLIENT
, "expected MSG_CLIENT_2_SERVER_LOGIN (%02X), received %02X",
943 MSG_CLIENT_2_SERVER_LOGIN
, mbuf
[2]);
948 pwd
= usr
+ cs_strlen((char *)usr
) + 1;
952 cs_log_dbg(D_CLIENT
, "bad client login request");
956 cl
->ncd_client_id
= (mbuf
[0] << 8) | mbuf
[1];
957 const char *client_name
= newcamd_get_client_name(cl
->ncd_client_id
);
959 #if defined(TCP_KEEPIDLE)
960 if(cl
->ncd_client_id
== 0x4453) // DiabloWifi has problems with TCPKeepAlive
964 // send first keepalive packet after 600 seconds of
965 // last package received (keepalive packets included)
966 if(setsockopt(cl
->udp_fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, &flag
, sizeof(flag
)) && errno
!= EBADF
)
968 cs_log("Setting TCP_KEEPIDLE failed, errno=%d, %s", errno
, strerror(errno
));
972 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.");
977 if(cl
->ncd_proto
== NCD_525
&& 0x6D == mbuf
[0] && 0x67 == mbuf
[1] && 0x11 == cl
->ncd_header
[11])
982 for(ok
= 0, account
= cfg
.account
; usr
&& account
&& (!ok
); account
= account
->next
)
984 cs_log_dbg(D_CLIENT
, "account->usr=%s", account
->usr
);
986 if(strcmp((char *)usr
, account
->usr
) == 0)
988 __md5_crypt(ESTR(account
->pwd
), "$1$abcdefgh$", (char *)passwdcrypt
);
989 cs_log_dbg(D_CLIENT
, "account->pwd=%s", passwdcrypt
);
991 if(strcmp((char *)pwd
, (const char *)passwdcrypt
) == 0)
996 snprintf(e_txt
, 20, "%s:%d", "newcamd", cfg
.ncd_ptab
.ports
[cl
->port_idx
].s_port
);
998 if((rc
= cs_auth_client(cl
, account
, e_txt
)) == 2)
1000 cs_log("hostname or ip mismatch for user %s (%s)", usr
, client_name
);
1005 cs_log("account is invalid for user %s (%s)", usr
, client_name
);
1010 cs_log("user %s authenticated successfully (%s)", usr
, client_name
);
1017 cs_log("user %s is providing a wrong password (%s)", usr
, client_name
);
1024 cs_log("user %s is trying to connect but doesnt exist ! (%s)", usr
, client_name
);
1028 // check for non ready reader and reject client
1029 for(rdr
= first_active_reader
; rdr
; rdr
= rdr
->next
)
1031 if(!cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
)
1036 if(rdr
->caid
== cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
[0].caid
)
1038 if(rdr
->card_status
== CARD_NEED_INIT
)
1040 cs_log("init for reader %s not finished -> reject client", rdr
->label
);
1049 LL_ITER itr
= ll_iter_create(cl
->aureader_list
);
1050 while((rdr
= ll_iter_next(&itr
)))
1054 if(!cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
)
1059 if(cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
[0].caid
== 0
1060 && !rdr
->audisabled
&& (is_network_reader(rdr
) || rdr
->card_status
== CARD_INSERTED
))
1066 for(n
= 0; n
< cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
[0].nprids
; n
++)
1068 if(emm_reader_match(rdr
, cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
[0].caid
,
1069 cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
[0].prids
[n
]))
1084 cs_log("AU enabled for user %s on reader %s", usr
, aureader
->label
);
1088 cs_log("AU disabled for user %s", usr
);
1092 network_cmd_no_data_send(cl
->udp_fd
, &cl
->ncd_msgid
,
1093 (ok
) ? MSG_CLIENT_2_SERVER_LOGIN_ACK
: MSG_CLIENT_2_SERVER_LOGIN_NAK
,
1094 cl
->ncd_skey
, COMMTYPE_SERVER
);
1099 FILTER
*pufilt
= &usr_filter
;
1101 nc_des_login_key_get(deskey
, passwdcrypt
, cs_strlen((char *)passwdcrypt
), key
);
1102 memcpy(cl
->ncd_skey
, key
, 16);
1104 i
= process_input(mbuf
, sizeof(mbuf
), cfg
.cmaxidle
);
1107 int32_t j
, len
= 15;
1109 if(mbuf
[2] != MSG_CARD_DATA_REQ
)
1111 cs_log_dbg(D_CLIENT
, "expected MSG_CARD_DATA_REQ (%02X), received %02X",
1112 MSG_CARD_DATA_REQ
, mbuf
[2]);
1116 mk_user_ftab(&usr_filter
);
1118 // set userfilter for au enabled clients
1122 if(aureader
->typ
== R_EMU
)
1124 usr_filter
= *get_emu_prids_for_caid(aureader
, cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
[0].caid
);
1128 mk_user_au_ftab(aureader
, &usr_filter
);
1131 ftab_clear(&cl
->ftab
);
1133 if(!cfg
.ncd_mgclient
)
1135 ftab_add(&cl
->ftab
, &usr_filter
);
1138 mbuf
[0] = MSG_CARD_DATA
;
1148 mbuf
[3] = get_threadnum(cl
) + 10; // Unique user number
1151 mbuf
[4] = (uint8_t)(pufilt
->caid
>> 8);
1152 mbuf
[5] = (uint8_t)(pufilt
->caid
);
1158 hexserial_to_newcamd(aureader
->hexserial
, mbuf
+ 8, pufilt
->caid
);
1162 memset(&mbuf
[8], 0, 6); // mbuf[8] - mbuf[13]
1165 mbuf
[14] = pufilt
->nprids
;
1167 for(j
= 0; j
< pufilt
->nprids
; j
++)
1169 if(caid_is_betacrypt(pufilt
->caid
) || caid_is_irdeto(pufilt
->caid
))
1171 mbuf
[15 + 11 * j
] = 0;
1172 mbuf
[16 + 11 * j
] = 0;
1173 mbuf
[17 + 11 * j
] = j
;
1177 mbuf
[15 + 11 * j
] = (uint8_t)(pufilt
->prids
[j
] >> 16);
1178 mbuf
[16 + 11 * j
] = (uint8_t)(pufilt
->prids
[j
] >> 8);
1179 mbuf
[17 + 11 * j
] = (uint8_t)(pufilt
->prids
[j
]);
1182 mbuf
[18 + 11 * j
] = 0x00;
1183 mbuf
[19 + 11 * j
] = 0x00;
1184 mbuf
[20 + 11 * j
] = 0x00;
1185 mbuf
[21 + 11 * j
] = 0x00;
1189 // check if user provid from IDENT exists on card
1194 if(pufilt
->caid
== aureader
->caid
&& aureader
->typ
!= R_EMU
)
1196 for(k
= 0; k
< aureader
->nprov
; k
++)
1198 rprid
= b2i(3, &aureader
->prid
[k
][1]);
1199 if(rprid
== pufilt
->prids
[j
])
1201 if(caid_is_betacrypt(pufilt
->caid
) || caid_is_irdeto(pufilt
->caid
))
1203 mbuf
[22 + 11 * j
] = aureader
->prid
[k
][0];
1204 mbuf
[23 + 11 * j
] = aureader
->prid
[k
][1];
1205 mbuf
[24 + 11 * j
] = aureader
->prid
[k
][2];
1206 mbuf
[25 + 11 * j
] = aureader
->prid
[k
][3];
1210 mbuf
[22 + 11 * j
] = aureader
->sa
[k
][0];
1211 mbuf
[23 + 11 * j
] = aureader
->sa
[k
][1];
1212 mbuf
[24 + 11 * j
] = aureader
->sa
[k
][2];
1213 mbuf
[25 + 11 * j
] = aureader
->sa
[k
][3];
1224 mbuf
[22 + 11 * j
] = 0x00;
1225 mbuf
[23 + 11 * j
] = 0x00;
1226 mbuf
[24 + 11 * j
] = 0x00;
1227 mbuf
[25 + 11 * j
] = 0x00;
1232 if(caid_is_betacrypt(pufilt
->caid
) || caid_is_irdeto(pufilt
->caid
))
1234 mbuf
[22 + 11 * j
] = 0x00;
1235 mbuf
[23 + 11 * j
] = (uint8_t)(pufilt
->prids
[j
] >> 16);
1236 mbuf
[24 + 11 * j
] = (uint8_t)(pufilt
->prids
[j
] >> 8);
1237 mbuf
[25 + 11 * j
] = (uint8_t)(pufilt
->prids
[j
]);
1241 mbuf
[22 + 11 * j
] = 0x00;
1242 mbuf
[23 + 11 * j
] = 0x00;
1243 mbuf
[24 + 11 * j
] = 0x00;
1244 mbuf
[25 + 11 * j
] = 0x00;
1252 memset(&cd
, 0, sizeof(cd
));
1256 if((aureader
->blockemm
& EMM_GLOBAL
) && !(aureader
->saveemm
& EMM_GLOBAL
))
1261 if((aureader
->blockemm
& EMM_SHARED
) && !(aureader
->saveemm
& EMM_SHARED
))
1266 if((aureader
->blockemm
& EMM_UNIQUE
) && !(aureader
->saveemm
& EMM_UNIQUE
))
1272 if(network_message_send(cl
->udp_fd
, &cl
->ncd_msgid
, mbuf
, len
, key
, COMMTYPE_SERVER
, 0, &cd
) < 0)
1286 cs_auth_client(cl
, 0, usr
? "login failure" : "no such user");
1293 static void newcamd_send_dcw(struct s_client
*client
, ECM_REQUEST
*er
)
1301 cs_log_dbg(D_CLIENT
, "ncd_send_dcw: error: client->udp_fd=%d", client
->udp_fd
);
1305 cl_msgid
= er
->msgid
;
1306 mbuf
[0] = er
->ecm
[0];
1308 if(er
->rc
>= E_NOTFOUND
) // not found
1311 mbuf
[1] = mbuf
[2] = 0x00;
1316 mbuf
[1] = mbuf
[2] = 0x10;
1317 memcpy(mbuf
+ 3, er
->cw
, 16);
1320 cs_log_dbg(D_CLIENT
, "ncd_send_dcw: er->msgid=%d, cl_msgid=%d, %02X", er
->msgid
, cl_msgid
, mbuf
[0]);
1322 network_message_send(client
->udp_fd
, &cl_msgid
, mbuf
, len
, client
->ncd_skey
, COMMTYPE_SERVER
, 0, NULL
);
1325 static void newcamd_process_ecm(struct s_client
*cl
, uint8_t *buf
, int32_t len
)
1336 ecmlen
= SCT_LEN((&buf
[2]));
1337 if(ecmlen
< 4 || ecmlen
> MAX_ECM_SIZE
|| ecmlen
+ 2 > len
)
1342 if(!(er
= get_ecmtask()))
1347 // save client ncd_msgid
1348 er
->msgid
= cl
->ncd_msgid
;
1349 er
->ecmlen
= ecmlen
;
1351 cs_log_dbg(D_CLIENT
, "ncd_process_ecm: er->msgid=%d len=%d ecmlen=%d", er
->msgid
, len
, er
->ecmlen
);
1353 er
->srvid
= cl
->ncd_header
[4] << 8 | cl
->ncd_header
[5];
1354 er
->caid
= cl
->ncd_header
[6] << 8 | cl
->ncd_header
[7];
1355 er
->prid
= cl
->ncd_header
[8] << 16 | cl
->ncd_header
[9] << 8 | cl
->ncd_header
[10];
1360 if(cfg
.ncd_ptab
.nports
&& cfg
.ncd_ptab
.nports
>= pi
&& cfg
.ncd_ptab
.ports
[pi
].ncd
)
1362 er
->caid
= cfg
.ncd_ptab
.ports
[pi
].ncd
->ncd_ftab
.filts
[0].caid
;
1366 memcpy(er
->ecm
, buf
+ 2, er
->ecmlen
);
1370 static void newcamd_process_emm(uint8_t *buf
, int32_t len
)
1372 int32_t ok
= 1, provid
;
1374 struct s_client
*cl
= cur_client();
1382 memset(&epg
, 0, sizeof(epg
));
1384 epg
.emmlen
= SCT_LEN(buf
);
1385 if(epg
.emmlen
> MAX_EMM_SIZE
|| epg
.emmlen
> len
)
1390 caid
= cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
[0].caid
;
1392 epg
.caid
[0] = (uint8_t)(caid
>> 8);
1393 epg
.caid
[1] = (uint8_t)(caid
);
1395 provid
= cfg
.ncd_ptab
.ports
[cl
->port_idx
].ncd
->ncd_ftab
.filts
[0].prids
[0];
1397 epg
.provid
[0] = (uint8_t)(provid
>> 24);
1398 epg
.provid
[1] = (uint8_t)(provid
>> 16);
1399 epg
.provid
[2] = (uint8_t)(provid
>> 8);
1400 epg
.provid
[3] = (uint8_t)(provid
);
1402 /*if(caid == 0x0500)
1406 emm_head = (buf[0]<<8) | buf[1];
1409 case 0x8e70: // EMM-S
1410 memcpy(epg.hexserial+1, buf+3, 4);
1411 epg.hexserial[4]=aureader->hexserial[4];
1413 case 0x8870: // EMM-U
1414 case 0x8c70: // confidential?
1416 cs_log("unsupported emm type: %04X", emm_head);
1420 if(!ok) cs_log("only EMM-S supported");
1424 memcpy(epg
.emm
, buf
, epg
.emmlen
);
1431 // Should always send an answer to client (also if au is disabled),
1432 // some clients will disconnect if they get no answer
1436 network_message_send(cl
->udp_fd
, &cl
->ncd_msgid
, buf
, 3, cl
->ncd_skey
, COMMTYPE_SERVER
, 0, NULL
);
1439 static void newcamd_report_cards(struct s_client
*client
)
1445 if(!cs_malloc(&cd
, sizeof(struct custom_data
)))
1450 memset(buf
, 0, sizeof(buf
));
1452 cd
->sid
= cfg
.ncd_ptab
.ports
[client
->port_idx
].s_port
;
1453 buf
[0] = MSG_SERVER_2_CLIENT_ADDCARD
;
1455 struct s_reader
*rdr
;
1456 for(rdr
= first_active_reader
; rdr
; rdr
= rdr
->next
)
1460 if(!(rdr
->grp
& client
->grp
))
1462 continue; // test - skip unaccesible readers
1467 for(j
= 0; j
< rdr
->ftab
.nfilts
; j
++)
1469 if(rdr
->ftab
.filts
[j
].caid
)
1471 cd
->caid
= rdr
->ftab
.filts
[j
].caid
;
1473 if(!rdr
->ftab
.filts
[j
].nprids
)
1476 cs_log_dbg(D_CLIENT
, "newcamd: extended: report card %04X@%06X svc", cd
->caid
, cd
->provid
);
1478 network_message_send(client
->udp_fd
, &client
->ncd_msgid
, buf
, 3,
1479 client
->ncd_skey
, COMMTYPE_SERVER
, 0, cd
);
1482 for(k
= 0; k
< rdr
->ftab
.filts
[j
].nprids
; k
++)
1484 cd
->provid
= rdr
->ftab
.filts
[j
].prids
[k
];
1485 cs_log_dbg(D_CLIENT
, "newcamd: extended: report card %04X@%06X svc", cd
->caid
, cd
->provid
);
1487 network_message_send(client
->udp_fd
, &client
->ncd_msgid
, buf
, 3,
1488 client
->ncd_skey
, COMMTYPE_SERVER
, 0, cd
);
1495 if(rdr
->caid
&& !flt
)
1497 if((rdr
->tcp_connected
|| rdr
->card_status
== CARD_INSERTED
))
1499 cd
->caid
= rdr
->caid
;
1503 cs_log_dbg(D_CLIENT
, "newcamd: extended: report card %04X@%06X caid", cd
->caid
, cd
->provid
);
1505 network_message_send(client
->udp_fd
, &client
->ncd_msgid
, buf
, 3,
1506 client
->ncd_skey
, COMMTYPE_SERVER
, 0, cd
);
1509 for(j
= 0; j
< rdr
->nprov
; j
++)
1511 cd
->provid
= (rdr
->prid
[j
][1]) << 16 | (rdr
->prid
[j
][2] << 8) | rdr
->prid
[j
][3];
1512 cs_log_dbg(D_CLIENT
, "newcamd: extended: report card %04X@%06X caid", cd
->caid
, cd
->provid
);
1514 network_message_send(client
->udp_fd
, &client
->ncd_msgid
, buf
, 3,
1515 client
->ncd_skey
, COMMTYPE_SERVER
, 0, cd
);
1521 if(cfg
.sidtab
&& client
->account
)
1523 struct s_sidtab
*ptr
;
1524 for(j
= 0, ptr
= cfg
.sidtab
; ptr
; ptr
= ptr
->next
, j
++)
1526 if(client
->account
->sidtabs
.ok
& ((SIDTABBITS
)1 << j
))
1528 for(k
= 0; k
< ptr
->num_caid
; k
++)
1530 cd
->caid
= ptr
->caid
[k
];
1532 if(!ptr
->num_provid
)
1535 cs_log_dbg(D_CLIENT
, "newcamd: extended: report card %04X@%06X acs", cd
->caid
, cd
->provid
);
1537 network_message_send(client
->udp_fd
, &client
->ncd_msgid
, buf
, 3,
1538 client
->ncd_skey
, COMMTYPE_SERVER
, 0, cd
);
1541 for(l
= 0; l
< ptr
->num_provid
; l
++)
1543 cd
->provid
= ptr
->provid
[l
];
1544 cs_log_dbg(D_CLIENT
, "newcamd: extended: report card %04X@%06X acs", cd
->caid
, cd
->provid
);
1546 network_message_send(client
->udp_fd
, &client
->ncd_msgid
, buf
, 3,
1547 client
->ncd_skey
, COMMTYPE_SERVER
, 0, cd
);
1557 static void newcamd_server_init(struct s_client
*client
)
1561 client
->ncd_server
= 1;
1562 cs_log("client connected to %d port", cfg
.ncd_ptab
.ports
[client
->port_idx
].s_port
);
1564 if(cfg
.ncd_ptab
.ports
[client
->port_idx
].ncd
&& cfg
.ncd_ptab
.ports
[client
->port_idx
].ncd
->ncd_key_is_set
)
1566 // port has a des key specified
1567 res
= newcamd_auth_client(client
->ip
, cfg
.ncd_ptab
.ports
[client
->port_idx
].ncd
->ncd_key
);
1571 // default global des key
1572 res
= newcamd_auth_client(client
->ip
, cfg
.ncd_key
);
1577 cs_disconnect_client(client
);
1581 // report all cards if using extended mg proto
1582 if(cfg
.ncd_mgclient
)
1584 cs_log_dbg(D_CLIENT
, "newcamd: extended: report all available cards");
1585 newcamd_report_cards(client
);
1590 #define EXT_VERSION_STR "1.67"
1591 #define EXT_VERSION_LEN 4
1593 static void newcamd_send_version(struct s_client
*client
)
1596 memset(buf
, 0, sizeof(buf
));
1598 buf
[0] = MSG_SERVER_2_CLIENT_GET_VERSION
;
1599 buf
[1] = EXT_VERSION_LEN
>> 8;
1600 buf
[2] = EXT_VERSION_LEN
& 0xFF;
1601 memcpy(buf
+ 3, EXT_VERSION_STR
, EXT_VERSION_LEN
);
1603 network_message_send(client
->udp_fd
, &client
->ncd_msgid
, buf
, EXT_VERSION_LEN
+ 3,
1604 client
->ncd_skey
, COMMTYPE_SERVER
, 0, NULL
);
1607 static void *newcamd_server(struct s_client
*client
, uint8_t *mbuf
, int32_t len
)
1609 // check for clienttimeout, if timeout occurs try to send keepalive / wait for answer
1610 // befor client was disconnected. If keepalive was disabled, exit after clienttimeout
1617 cs_log_dbg(D_CLIENT
, "newcamd: got cmd %d", mbuf
[2]);
1624 newcamd_process_ecm(client
, mbuf
, len
);
1628 case MSG_SERVER_2_CLIENT_GET_VERSION
:
1630 cs_log_dbg(D_CLIENT
, "newcamd: extended: send Version 1.67");
1631 newcamd_send_version(client
);
1643 if(mbuf
[2] > 0x81 && mbuf
[2] < 0x92)
1645 newcamd_process_emm(mbuf
+ 2, len
- 2);
1649 cs_log_dbg(D_CLIENT
, "unknown newcamd command! (%d)", mbuf
[2]);
1657 void newcamd_idle(void)
1659 struct s_client
*client
= cur_client();
1660 struct s_reader
*rdr
= client
->reader
;
1667 if(rdr
->tcp_ito
> 0)
1669 // inactivitytimeout > 0 enables protocol keepalive packages
1674 time_diff
= llabs(now
- rdr
->last_s
);
1676 if(time_diff
> (rdr
->tcp_ito
))
1678 if(client
->ncd_keepalive
)
1684 network_tcp_connection_close(client
->reader
, "inactivity");
1688 else if(rdr
->tcp_ito
== -1)
1699 int32_t newcamd_client_init(struct s_client
*client
)
1701 char ptxt
[1] = { "\0" };
1703 client
->ncd_proto
= client
->reader
->ncd_proto
;
1705 cs_log("proxy %s:%d newcamd52%d (fd=%d%s)", client
->reader
->device
, client
->reader
->r_port
,
1706 (client
->reader
->ncd_proto
== NCD_525
) ? 5 : 4, client
->udp_fd
, ptxt
);
1708 // try to connect. ignore possible failures
1709 // idle reconnect (tcp_ito = -1) will trigger an additional connect anyway
1710 if(client
->reader
->ncd_connect_on_init
&& client
->reader
->tcp_ito
!= -1)
1718 static int32_t newcamd_send_ecm(struct s_client
*client
, ECM_REQUEST
*er
)
1720 struct s_reader
*rdr
= client
->reader
;
1722 if(!newcamd_connect())
1727 // check server filters
1728 if(!chk_rsfilter(rdr
, er
))
1734 if(!cs_malloc(&buf
, er
->ecmlen
))
1739 memcpy(buf
, er
->ecm
, er
->ecmlen
);
1741 client
->ncd_header
[4] = er
->srvid
>> 8;
1742 client
->ncd_header
[5] = er
->srvid
& 0xFF;
1743 client
->ncd_header
[6] = er
->caid
>> 8;
1744 client
->ncd_header
[7] = er
->caid
& 0xFF;
1745 client
->ncd_header
[8] = er
->prid
>> 16;
1746 client
->ncd_header
[9] = er
->prid
>> 8;
1747 client
->ncd_header
[10] = er
->prid
& 0xFF;
1749 int32_t rc
= (newcamd_send(buf
, er
->ecmlen
, er
->srvid
) < 1) ? -1 : 0;
1755 static int32_t newcamd_send_emm(EMM_PACKET
*ep
)
1757 uint8_t buf
[ep
->emmlen
];
1759 if(!newcamd_connect())
1764 memcpy(buf
, ep
->emm
, ep
->emmlen
);
1766 return (newcamd_send(buf
, ep
->emmlen
, 0) < 1) ? 0 : 1;
1769 static int32_t newcamd_recv_chk(struct s_client
*client
, uint8_t *dcw
, int32_t *rc
, uint8_t *buf
, int32_t n
)
1783 idx
= (buf
[0] << 8) | buf
[1];
1785 if(n
== 5) // not found on server
1794 cs_log_dbg(D_CLIENT
, "invalid newcamd answer");
1799 memcpy(dcw
, buf
+ 5, 16);
1808 case MSG_SERVER_2_CLIENT_ADDCARD
:
1812 client
->reader
->ncd_disable_server_filt
= 1;
1820 if(buf
[2] > 0x81 && buf
[2] < 0x92) // answer to emm
1825 cs_log_dbg(D_CLIENT
, "unknown newcamd command from server");
1834 * resolve client type for newcamd protocol
1836 const char *newcamd_get_client_name(uint16_t client_id
)
1838 // When adding new entries keep the list sorted!
1843 } ncd_service_ids
[] =
1845 { 0x0000, "generic" },
1846 { 0x02C2, "Opticum" },
1847 { 0x0665, "rq-sssp-client-CS" },
1848 { 0x0666, "rqcamd" },
1849 { 0x0667, "rq-echo-client" },
1850 { 0x0669, "rq-sssp-client-CW" },
1851 { 0x0769, "JlsRq" },
1852 { 0x414C, "AlexCS" },
1853 { 0x4333, "camd3" },
1854 { 0x4343, "CCcam" },
1855 { 0x434C, "Cardlink" },
1856 { 0x4453, "DiabloCam-UW" },
1857 { 0x4543, "eyetvCamd" },
1858 { 0x4765, "Octagon" },
1860 { 0x4E58, "NextYE2k" },
1862 { 0x5456, "Tecview" },
1863 { 0x5644, "vdr-sc" },
1864 { 0x5743, "WiCard" },
1866 { 0x6502, "Tvheadend" },
1867 { 0x6576, "evocamd" },
1868 { 0x6762, "gbox2CS" },
1869 { 0x6B61, "Kaffeine" },
1872 { 0x6D67, "mgcamd" },
1873 { 0x6E65, "NextYE2k" },
1874 { 0x6E73, "NewCS" },
1875 { 0x7264, "radegast" },
1877 { 0x7763, "WinCSC" },
1878 { 0x7878, "tsdecrypt" },
1879 { 0x8888, "OSCam" },
1880 { 0x9911, "ACamd" },
1881 { 0x9922, "DVBplug" },
1889 if(!ncd_service_ids
[i
].client
)
1894 if(ncd_service_ids
[i
].id
== client_id
)
1896 return ncd_service_ids
[i
].client
;
1902 return "unknown - please report";
1905 void module_newcamd(struct s_module
*ph
)
1907 ph
->desc
= "newcamd";
1908 ph
->type
= MOD_CONN_TCP
;
1909 ph
->large_ecm_support
= 1;
1910 ph
->listenertype
= LIS_NEWCAMD
;
1911 IP_ASSIGN(ph
->s_ip
, cfg
.ncd_srvip
);
1912 ph
->s_handler
= newcamd_server
;
1913 ph
->s_init
= newcamd_server_init
;
1914 ph
->recv
= newcamd_recv
;
1915 ph
->send_dcw
= newcamd_send_dcw
;
1916 ph
->ptab
= cfg
.ncd_ptab
;
1917 ph
->c_init
= newcamd_client_init
;
1918 ph
->c_recv_chk
= newcamd_recv_chk
;
1919 ph
->c_send_ecm
= newcamd_send_ecm
;
1920 ph
->c_send_emm
= newcamd_send_emm
;
1921 ph
->c_idle
= newcamd_idle
;
1922 ph
->num
= R_NEWCAMD
;