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