1 | #include "globals.h"
|
---|
2 |
|
---|
3 | #define CWS_NETMSGSIZE 272
|
---|
4 | #define NCD_CLIENT_ID 0x8888
|
---|
5 |
|
---|
6 | #define CWS_FIRSTCMDNO 0xe0
|
---|
7 | typedef enum
|
---|
8 | {
|
---|
9 | MSG_CLIENT_2_SERVER_LOGIN = CWS_FIRSTCMDNO,
|
---|
10 | MSG_CLIENT_2_SERVER_LOGIN_ACK,
|
---|
11 | MSG_CLIENT_2_SERVER_LOGIN_NAK,
|
---|
12 | MSG_CARD_DATA_REQ,
|
---|
13 | MSG_CARD_DATA,
|
---|
14 | MSG_SERVER_2_CLIENT_NAME,
|
---|
15 | MSG_SERVER_2_CLIENT_NAME_ACK,
|
---|
16 | MSG_SERVER_2_CLIENT_NAME_NAK,
|
---|
17 | MSG_SERVER_2_CLIENT_LOGIN,
|
---|
18 | MSG_SERVER_2_CLIENT_LOGIN_ACK,
|
---|
19 | MSG_SERVER_2_CLIENT_LOGIN_NAK,
|
---|
20 | MSG_ADMIN,
|
---|
21 | MSG_ADMIN_ACK,
|
---|
22 | MSG_ADMIN_LOGIN,
|
---|
23 | MSG_ADMIN_LOGIN_ACK,
|
---|
24 | MSG_ADMIN_LOGIN_NAK,
|
---|
25 | MSG_ADMIN_COMMAND,
|
---|
26 | MSG_ADMIN_COMMAND_ACK,
|
---|
27 | MSG_ADMIN_COMMAND_NAK,
|
---|
28 | MSG_KEEPALIVE = CWS_FIRSTCMDNO + 0x1d,
|
---|
29 | MSG_SERVER_2_CLIENT_OSD = 0xd1,
|
---|
30 | MSG_SERVER_2_CLIENT_ALLCARDS = 0xd2,
|
---|
31 | MSG_SERVER_2_CLIENT_ADDCARD = 0xd3,
|
---|
32 | MSG_SERVER_2_CLIENT_REMOVECARD = 0xd4,
|
---|
33 | MSG_SERVER_2_CLIENT_CHANGE_KEY = 0xd5,
|
---|
34 | MSG_SERVER_2_CLIENT_GET_VERSION = 0xd6,
|
---|
35 | MSG_SERVER_2_CLIENT_ADDSID = 0xd7,
|
---|
36 | MSG_CLIENT_2_SERVER_CARDDISCOVER = 0xd8
|
---|
37 | } net_msg_type_t;
|
---|
38 |
|
---|
39 | typedef enum
|
---|
40 | {
|
---|
41 | COMMTYPE_CLIENT,
|
---|
42 | COMMTYPE_SERVER
|
---|
43 | } comm_type_t;
|
---|
44 |
|
---|
45 |
|
---|
46 | typedef struct custom_data
|
---|
47 | {
|
---|
48 | unsigned short sid;
|
---|
49 | unsigned short caid;
|
---|
50 | int provid;
|
---|
51 | uchar x;
|
---|
52 | } custom_data_t;
|
---|
53 |
|
---|
54 | #define REQ_SIZE 2
|
---|
55 |
|
---|
56 | static int network_message_send(int handle, uint16 *netMsgId, uint8 *buffer,
|
---|
57 | int len, uint8 *deskey, comm_type_t commType,
|
---|
58 | ushort sid, custom_data_t *cd)
|
---|
59 | {
|
---|
60 | uint8 netbuf[CWS_NETMSGSIZE];
|
---|
61 | int head_size;
|
---|
62 | struct s_client *cl = cur_client();
|
---|
63 |
|
---|
64 | head_size = (cl->ncd_proto==NCD_524)?8:12;
|
---|
65 |
|
---|
66 | if (len < 3 || len + head_size > CWS_NETMSGSIZE || handle < 0)
|
---|
67 | return -1;
|
---|
68 | buffer[1] = (buffer[1] & 0xf0) | (((len - 3) >> 8) & 0x0f);
|
---|
69 | buffer[2] = (len - 3) & 0xff;
|
---|
70 | memcpy(netbuf+head_size, buffer, len);
|
---|
71 | len += head_size;
|
---|
72 | if (netMsgId) {
|
---|
73 | switch(commType)
|
---|
74 | {
|
---|
75 | case COMMTYPE_CLIENT:
|
---|
76 | (*netMsgId)++;
|
---|
77 | break;
|
---|
78 | case COMMTYPE_SERVER:
|
---|
79 | if( *netMsgId == 0xFFFE ) *netMsgId = 0; // ??? 0xFFFF ?
|
---|
80 | break;
|
---|
81 | }
|
---|
82 | netbuf[2] = (*netMsgId) >> 8;
|
---|
83 | netbuf[3] = (*netMsgId) & 0xff;
|
---|
84 | }
|
---|
85 | else
|
---|
86 | netbuf[2] = netbuf[3] = 0;
|
---|
87 | memset(netbuf+4, 0, (cl->ncd_proto==NCD_524)?4:8);
|
---|
88 | if( sid ) {
|
---|
89 | netbuf[(cl->ncd_proto==NCD_524)?6:4] = (uchar)(sid>>8); //sid
|
---|
90 | netbuf[(cl->ncd_proto==NCD_524)?7:5] = (uchar)(sid);
|
---|
91 | }
|
---|
92 | //if ((!ncd_proto==NCD_524) && (buffer[0] >= 0xd1) && (buffer[0]<= 0xd8)) { // extended proto for mg
|
---|
93 | //cs_debug_mask(D_CLIENT, "newcamd: extended: msg");
|
---|
94 | if (cd) {
|
---|
95 | cs_debug_mask(D_CLIENT, "newcamd: has cd");
|
---|
96 | netbuf[4] = cd->sid >> 8;
|
---|
97 | netbuf[5] = cd->sid & 0xff;
|
---|
98 | netbuf[6] = cd->caid >> 8;
|
---|
99 | netbuf[7] = cd->caid & 0xff;
|
---|
100 | netbuf[8] = (cd->provid >> 16) & 0xFF;
|
---|
101 | netbuf[9] = (cd->provid >> 8) & 0xff;
|
---|
102 | netbuf[10] = cd->provid & 0xff;
|
---|
103 | }
|
---|
104 | //}
|
---|
105 | netbuf[0] = (len - 2) >> 8;
|
---|
106 | netbuf[1] = (len - 2) & 0xff;
|
---|
107 | cs_ddump_mask(D_CLIENT, netbuf, len, "send %d bytes to %s", len, remote_txt());
|
---|
108 | if ((len = des_encrypt(netbuf, len, deskey)) < 0)
|
---|
109 | return -1;
|
---|
110 | netbuf[0] = (len - 2) >> 8;
|
---|
111 | netbuf[1] = (len - 2) & 0xff;
|
---|
112 | return send(handle, netbuf, len, 0);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static int network_message_receive(int handle, uint16 *netMsgId, uint8 *buffer,
|
---|
116 | uint8 *deskey, comm_type_t commType)
|
---|
117 | {
|
---|
118 | int len, ncd_off, msgid;
|
---|
119 | uint8 netbuf[CWS_NETMSGSIZE];
|
---|
120 | int returnLen;
|
---|
121 | struct s_client *cl = cur_client();
|
---|
122 |
|
---|
123 | if (!buffer || handle < 0)
|
---|
124 | return -1;
|
---|
125 | len = recv(handle, netbuf, 2, 0);
|
---|
126 | cs_debug_mask(D_CLIENT, "nmr(): len=%d, errno=%d", len, (len==-1)?errno:0);
|
---|
127 | if (!len) {
|
---|
128 | cs_debug_mask(D_CLIENT, "nmr: 1 return 0");
|
---|
129 | network_tcp_connection_close(cl, handle);
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 | if (len != 2) {
|
---|
133 | cs_debug_mask(D_CLIENT, "nmr: len!=2");
|
---|
134 | network_tcp_connection_close(cl, handle);
|
---|
135 | return -1;
|
---|
136 | }
|
---|
137 | if (((netbuf[0] << 8) | netbuf[1]) > CWS_NETMSGSIZE - 2) {
|
---|
138 | cs_debug_mask(D_CLIENT, "nmr: 1 return -1");
|
---|
139 | return -1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | len = recv(handle, netbuf+2, (netbuf[0] << 8) | netbuf[1], 0);
|
---|
143 | if (!len) {
|
---|
144 | cs_debug_mask(D_CLIENT, "nmr: 2 return 0");
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 | if (len != ((netbuf[0] << 8) | netbuf[1])) {
|
---|
148 | cs_debug_mask(D_CLIENT, "nmr: 2 return -1");
|
---|
149 | return -1;
|
---|
150 | }
|
---|
151 | len += 2;
|
---|
152 | if ((len = des_decrypt(netbuf, len, deskey)) < 11 ) { // 15(newcamd525) or 11 ???
|
---|
153 | cs_debug_mask(D_CLIENT, "nmr: can't decrypt, invalid des key?");
|
---|
154 | cs_sleepms(2000);
|
---|
155 | return -1;
|
---|
156 | }
|
---|
157 | //cs_ddump_mask(D_CLIENT, netbuf, len, "nmr: decrypted data, len=%d", len);
|
---|
158 | msgid = (netbuf[2] << 8) | netbuf[3];
|
---|
159 |
|
---|
160 | if( cl->ncd_proto==NCD_AUTO ) {
|
---|
161 | // auto detect
|
---|
162 | int l5 = (((netbuf[13] & 0x0f) << 8) | netbuf[14]) + 3;
|
---|
163 | int l4 = (((netbuf[9] & 0x0f) << 8) | netbuf[10]) + 3;
|
---|
164 |
|
---|
165 | if( (l5<=len-12) && ((netbuf[12]&0xF0)==0xE0 || (netbuf[12]&0xF0)==0x80) )
|
---|
166 | cl->ncd_proto = NCD_525;
|
---|
167 | else if( (l4<=len-8) && ((netbuf[8]&0xF0)==0xE0 || (netbuf[9]&0xF0)==0x80) )
|
---|
168 | cl->ncd_proto = NCD_524;
|
---|
169 | else {
|
---|
170 | cs_debug_mask(D_CLIENT, "nmr: 4 return -1");
|
---|
171 | return -1;
|
---|
172 | }
|
---|
173 |
|
---|
174 | cs_debug_mask(D_CLIENT, "nmr: autodetect: newcamd52%d used", (cl->ncd_proto==NCD_525)?5:4);
|
---|
175 | }
|
---|
176 |
|
---|
177 | ncd_off=(cl->ncd_proto==NCD_525)?4:0;
|
---|
178 |
|
---|
179 | returnLen = (((netbuf[9+ncd_off] & 0x0f) << 8) | netbuf[10+ncd_off]) + 3;
|
---|
180 | if ( returnLen > (len-(8+ncd_off)) ) {
|
---|
181 | cs_debug_mask(D_CLIENT, "nmr: 4 return -1");
|
---|
182 | return -1;
|
---|
183 | }
|
---|
184 |
|
---|
185 | //cs_ddump_mask(D_CLIENT, netbuf, len, "nmr: decrypted data");
|
---|
186 | if (netMsgId)
|
---|
187 | {
|
---|
188 | switch (commType)
|
---|
189 | {
|
---|
190 | case COMMTYPE_SERVER:
|
---|
191 | *netMsgId = msgid;
|
---|
192 | break;
|
---|
193 |
|
---|
194 | case COMMTYPE_CLIENT:
|
---|
195 | //if (*netMsgId != ((netbuf[2] << 8) | netbuf[3])) {
|
---|
196 | cs_debug_mask(D_CLIENT, "nmr: netMsgId=%d, from server=%d, ", *netMsgId, msgid );
|
---|
197 | //return -2;
|
---|
198 | //}
|
---|
199 | break;
|
---|
200 |
|
---|
201 | default:
|
---|
202 | cs_debug_mask(D_CLIENT, "nmr: 5 return -1");
|
---|
203 | return -1;
|
---|
204 | break;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | switch(commType)
|
---|
208 | {
|
---|
209 | case COMMTYPE_SERVER:
|
---|
210 | buffer[0]=(cl->ncd_proto==NCD_525)?netbuf[4]:netbuf[6]; // sid
|
---|
211 | buffer[1]=(cl->ncd_proto==NCD_525)?netbuf[5]:netbuf[7];
|
---|
212 | break;
|
---|
213 | case COMMTYPE_CLIENT:
|
---|
214 | buffer[0]=netbuf[2]; // msgid
|
---|
215 | buffer[1]=netbuf[3];
|
---|
216 | break;
|
---|
217 | }
|
---|
218 |
|
---|
219 | memcpy(buffer+2, netbuf+(8+ncd_off), returnLen);
|
---|
220 | return returnLen+2;
|
---|
221 | }
|
---|
222 |
|
---|
223 | static void network_cmd_no_data_send(int handle, uint16 *netMsgId,
|
---|
224 | net_msg_type_t cmd, uint8 *deskey,
|
---|
225 | comm_type_t commType)
|
---|
226 | {
|
---|
227 | uint8 buffer[CWS_NETMSGSIZE];
|
---|
228 |
|
---|
229 | buffer[0] = cmd; buffer[1] = 0;
|
---|
230 | network_message_send(handle, netMsgId, buffer, 3, deskey, commType, 0, NULL);
|
---|
231 | }
|
---|
232 |
|
---|
233 | static int network_cmd_no_data_receive(int handle, uint16 *netMsgId,
|
---|
234 | uint8 *deskey, comm_type_t commType)
|
---|
235 | {
|
---|
236 | uint8 buffer[CWS_NETMSGSIZE];
|
---|
237 |
|
---|
238 | if (network_message_receive(handle, netMsgId, buffer, deskey, commType) != 3+2)
|
---|
239 | return -1;
|
---|
240 | return buffer[2];
|
---|
241 | }
|
---|
242 |
|
---|
243 | void newcamd_reply_ka()
|
---|
244 | {
|
---|
245 | struct s_client *cl = cur_client();
|
---|
246 |
|
---|
247 | if(!cl->udp_fd)
|
---|
248 | {
|
---|
249 | cs_debug_mask(D_CLIENT, "invalid client fd=%d", cl->udp_fd);
|
---|
250 | return;
|
---|
251 | }
|
---|
252 |
|
---|
253 | cs_debug_mask(D_CLIENT, "send keepalive to client fd=%d", cl->udp_fd);
|
---|
254 |
|
---|
255 | network_cmd_no_data_send(cl->udp_fd, &cl->ncd_msgid,
|
---|
256 | MSG_KEEPALIVE, cl->ncd_skey,COMMTYPE_SERVER);
|
---|
257 | }
|
---|
258 |
|
---|
259 | static int connect_newcamd_server()
|
---|
260 | {
|
---|
261 | int i;
|
---|
262 | uint8 buf[CWS_NETMSGSIZE];
|
---|
263 | uint8 keymod[14];
|
---|
264 | uint8 key[16];
|
---|
265 | int handle=0;
|
---|
266 |
|
---|
267 | uint32 index;
|
---|
268 | uchar passwdcrypt[120];
|
---|
269 | uint8 login_answer;
|
---|
270 | int bytes_received;
|
---|
271 | struct s_client *cl = cur_client();
|
---|
272 |
|
---|
273 | if(cl->reader->device[0] == 0 || cl->reader->r_pwd[0] == 0 ||
|
---|
274 | cl->reader->r_usr[0] == 0 || cl->reader->r_port == 0)
|
---|
275 | return -5;
|
---|
276 |
|
---|
277 | // 1. Connect
|
---|
278 | handle = network_tcp_connection_open();
|
---|
279 | if(handle < 0) return -1;
|
---|
280 |
|
---|
281 | // 2. Get init sequence
|
---|
282 | cl->reader->ncd_msgid = 0;
|
---|
283 | if( read(handle, keymod, sizeof(keymod)) != sizeof(keymod)) {
|
---|
284 | cs_log("server does not return 14 bytes");
|
---|
285 | network_tcp_connection_close(cl, handle);
|
---|
286 | return -2;
|
---|
287 | }
|
---|
288 | cs_ddump_mask(D_CLIENT, keymod, 14, "server init sequence:");
|
---|
289 | des_login_key_get(keymod, cl->reader->ncd_key, 14, key);
|
---|
290 |
|
---|
291 | // 3. Send login info
|
---|
292 | index = 3;
|
---|
293 | buf[0] = MSG_CLIENT_2_SERVER_LOGIN;
|
---|
294 | buf[1] = 0;
|
---|
295 | cs_strncpy((char *)buf+index, cl->reader->r_usr, sizeof(buf)-index);
|
---|
296 | __md5_crypt(cl->reader->r_pwd, "$1$abcdefgh$", (char *)passwdcrypt);
|
---|
297 | index += strlen(cl->reader->r_usr)+1;
|
---|
298 | cs_strncpy((char *)buf+index, (const char *)passwdcrypt, sizeof(buf)-index);
|
---|
299 |
|
---|
300 | network_message_send(handle, 0, buf, index+strlen((char *)passwdcrypt)+1, key,
|
---|
301 | COMMTYPE_CLIENT, NCD_CLIENT_ID, NULL);
|
---|
302 |
|
---|
303 | // 3.1 Get login answer
|
---|
304 | login_answer=network_cmd_no_data_receive(handle, &cl->reader->ncd_msgid,
|
---|
305 | key, COMMTYPE_CLIENT);
|
---|
306 | if( login_answer == MSG_CLIENT_2_SERVER_LOGIN_NAK )
|
---|
307 | {
|
---|
308 | cs_log("login failed for user '%s'", cl->reader->r_usr);
|
---|
309 | network_tcp_connection_close(cl, handle);
|
---|
310 | return -3;
|
---|
311 | }
|
---|
312 | if( login_answer != MSG_CLIENT_2_SERVER_LOGIN_ACK )
|
---|
313 | {
|
---|
314 | cs_log("expected MSG_CLIENT_2_SERVER_LOGIN_ACK (%02X), received %02X",
|
---|
315 | MSG_CLIENT_2_SERVER_LOGIN_ACK, login_answer);
|
---|
316 | network_tcp_connection_close(cl, handle);
|
---|
317 | return -3;
|
---|
318 | }
|
---|
319 |
|
---|
320 | // 3.2 Set connection info
|
---|
321 | cl->reader->tcp_connected = 1;
|
---|
322 | cl->crypted = 1;
|
---|
323 |
|
---|
324 | // 4. Send MSG_CARD_DATE_REQ
|
---|
325 | des_login_key_get(cl->reader->ncd_key, passwdcrypt, strlen((char *)passwdcrypt), key);
|
---|
326 |
|
---|
327 | network_cmd_no_data_send(handle, &cl->reader->ncd_msgid, MSG_CARD_DATA_REQ,
|
---|
328 | key, COMMTYPE_CLIENT);
|
---|
329 | bytes_received = network_message_receive(handle, &cl->reader->ncd_msgid, buf,
|
---|
330 | key, COMMTYPE_CLIENT);
|
---|
331 | if( bytes_received < 16 || buf[2] != MSG_CARD_DATA ) {
|
---|
332 | cs_log("expected MSG_CARD_DATA (%02X), received %02X",
|
---|
333 | MSG_CARD_DATA, buf[2]);
|
---|
334 | network_tcp_connection_close(cl, handle);
|
---|
335 | return -4;
|
---|
336 | }
|
---|
337 |
|
---|
338 | // 5. Parse CAID and PROVID(s)
|
---|
339 | cl->reader->caid = (ushort)((buf[6]<<8) | buf[7]);
|
---|
340 |
|
---|
341 | /* handle special serial format in newcamd. See newcamd_auth_client */
|
---|
342 | newcamd_to_hexserial(buf+10, cl->reader->hexserial, cl->reader->caid);
|
---|
343 | cs_log("Newcamd Server: %s:%d - UserID: %i", cl->reader->device, cl->reader->r_port, buf[3+2]);
|
---|
344 | 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]);
|
---|
345 | cl->reader->nprov = buf[14+2];
|
---|
346 | memset(cl->reader->prid, 0x00, sizeof(cl->reader->prid));
|
---|
347 | for (i=0; i < cl->reader->nprov; i++) {
|
---|
348 | cl->reader->availkeys[i][0] = 1;
|
---|
349 | cl->reader->prid[i][1] = buf[15+2+11*i];
|
---|
350 | cl->reader->prid[i][2] = buf[16+2+11*i];
|
---|
351 | cl->reader->prid[i][3] = buf[17+2+11*i];
|
---|
352 | memcpy(&cl->reader->sa[i], buf+22+2+11*i, 4); // the 4 first bytes are not read
|
---|
353 | 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]);
|
---|
354 | }
|
---|
355 | memcpy(cl->reader->ncd_skey, key, 16);
|
---|
356 |
|
---|
357 | // 6. Set card inserted
|
---|
358 | cl->reader->tcp_connected = 2;
|
---|
359 | cl->reader->card_status = CARD_INSERTED;
|
---|
360 | cl->reader->last_g = cl->reader->last_s = time((time_t *)0);
|
---|
361 |
|
---|
362 | // Only after connect() on cl->udp_fd (Linux)
|
---|
363 | cl->pfd=cl->udp_fd;
|
---|
364 |
|
---|
365 | return 0;
|
---|
366 | }
|
---|
367 |
|
---|
368 | static int newcamd_connect()
|
---|
369 | {
|
---|
370 | struct s_client *cl = cur_client();
|
---|
371 |
|
---|
372 | if (cl->reader->tcp_connected < 2 && connect_newcamd_server() < 0)
|
---|
373 | return 0;
|
---|
374 |
|
---|
375 | if (!cl->udp_fd)
|
---|
376 | return 0;
|
---|
377 |
|
---|
378 | return 1;
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | static int newcamd_send(uchar *buf, int ml, ushort sid)
|
---|
383 | {
|
---|
384 | struct s_client *cl = cur_client();
|
---|
385 |
|
---|
386 | if(!newcamd_connect())
|
---|
387 | return(-1);
|
---|
388 |
|
---|
389 | return(network_message_send(cl->udp_fd, &cl->reader->ncd_msgid,
|
---|
390 | buf, ml, cl->reader->ncd_skey, COMMTYPE_CLIENT, sid, NULL));
|
---|
391 | }
|
---|
392 |
|
---|
393 | static int newcamd_recv(struct s_client *client, uchar *buf, int UNUSED(l))
|
---|
394 | {
|
---|
395 | int rc, rs;
|
---|
396 |
|
---|
397 | if (client->typ == 'c')
|
---|
398 | {
|
---|
399 | rs=network_message_receive(client->udp_fd,
|
---|
400 | &client->ncd_msgid, buf,
|
---|
401 | client->ncd_skey, COMMTYPE_SERVER);
|
---|
402 | }
|
---|
403 | else
|
---|
404 | {
|
---|
405 | if (!client->udp_fd) return(-1);
|
---|
406 | rs=network_message_receive(client->udp_fd,
|
---|
407 | &client->reader->ncd_msgid,buf,
|
---|
408 | client->reader->ncd_skey, COMMTYPE_CLIENT);
|
---|
409 | }
|
---|
410 |
|
---|
411 | if (rs<5) rc=(-1);
|
---|
412 | else rc=rs;
|
---|
413 |
|
---|
414 | cs_ddump_mask(D_CLIENT, buf, rs, "received %d bytes from %s", rs, remote_txt());
|
---|
415 | client->last = time((time_t *) 0);
|
---|
416 |
|
---|
417 | if( rc==-1 )
|
---|
418 | {
|
---|
419 | if (rs > 0)
|
---|
420 | cs_log("packet to small (%d bytes)", rs);
|
---|
421 | else
|
---|
422 | cs_log("Connection closed to %s", remote_txt());
|
---|
423 | }
|
---|
424 | return(rc);
|
---|
425 | }
|
---|
426 |
|
---|
427 | static FILTER mk_user_au_ftab(struct s_reader *aureader)
|
---|
428 | {
|
---|
429 | int i,j,found;
|
---|
430 | struct s_client *cl = cur_client();
|
---|
431 | FILTER filt;
|
---|
432 | FILTER *pufilt;
|
---|
433 |
|
---|
434 | filt.caid = aureader->caid;
|
---|
435 | if (filt.caid == 0) filt.caid = cl->ftab.filts[0].caid;
|
---|
436 | filt.nprids = 0;
|
---|
437 | memset(&filt.prids, 0, sizeof(filt.prids));
|
---|
438 | pufilt = &cl->ftab.filts[0];
|
---|
439 |
|
---|
440 | for( i=0; i<aureader->nprov; i++ )
|
---|
441 | filt.prids[filt.nprids++] = b2i(3, &aureader->prid[i][1]);
|
---|
442 |
|
---|
443 | for( i=0; i<pufilt->nprids; i++ )
|
---|
444 | {
|
---|
445 | for( j=found=0; (!found)&&(j<filt.nprids); j++ )
|
---|
446 | if (pufilt->prids[i] == filt.prids[j]) found=1;
|
---|
447 | if( !found )
|
---|
448 | filt.prids[filt.nprids++] = pufilt->prids[i];
|
---|
449 | }
|
---|
450 |
|
---|
451 | return filt;
|
---|
452 | }
|
---|
453 |
|
---|
454 | static FILTER mk_user_ftab()
|
---|
455 | {
|
---|
456 | FILTER *psfilt = 0;
|
---|
457 | FILTER filt;
|
---|
458 | int port_idx,i,j,k,c;
|
---|
459 | struct s_client *cl = cur_client();
|
---|
460 |
|
---|
461 | filt.caid = 0;
|
---|
462 | filt.nprids = 0;
|
---|
463 | memset(&filt.prids, 0, sizeof(filt.prids));
|
---|
464 |
|
---|
465 | port_idx = cl->port_idx;
|
---|
466 | psfilt = &cfg.ncd_ptab.ports[port_idx].ftab.filts[0];
|
---|
467 |
|
---|
468 | // 1. CAID
|
---|
469 | // search server CAID in client CAID
|
---|
470 | for( c=i=0; i<CS_MAXCAIDTAB; i++ )
|
---|
471 | {
|
---|
472 | int ctab_caid;
|
---|
473 | ctab_caid = cl->ctab.caid[i]&cl->ctab.mask[i];
|
---|
474 | if( ctab_caid ) c++;
|
---|
475 |
|
---|
476 | if( psfilt->caid==ctab_caid )
|
---|
477 | {
|
---|
478 | filt.caid=ctab_caid;
|
---|
479 | break;
|
---|
480 | }
|
---|
481 | }
|
---|
482 | if( c && !filt.caid )
|
---|
483 | {
|
---|
484 | cs_log("no valid CAID found in CAID for user '%s'", cl->account->usr);
|
---|
485 | return filt;
|
---|
486 | }
|
---|
487 |
|
---|
488 | // search CAID in client IDENT
|
---|
489 | cs_debug_mask(D_CLIENT, "client[%8X].%s nfilts=%d, filt.caid=%04X", pthread_self(),
|
---|
490 | cl->account->usr, cl->ftab.nfilts, filt.caid);
|
---|
491 |
|
---|
492 | if( !filt.caid && cl->ftab.nfilts )
|
---|
493 | {
|
---|
494 | int fcaids;
|
---|
495 | for( i=fcaids=0; i<cl->ftab.nfilts; i++ )
|
---|
496 | {
|
---|
497 | ushort ucaid=cl->ftab.filts[i].caid;
|
---|
498 | if( ucaid ) fcaids++;
|
---|
499 | if( ucaid && psfilt->caid==ucaid )
|
---|
500 | {
|
---|
501 | filt.caid = ucaid;
|
---|
502 | break;
|
---|
503 | }
|
---|
504 | }
|
---|
505 | if( fcaids==cl->ftab.nfilts && !filt.caid )
|
---|
506 | {
|
---|
507 | cs_log("no valid CAID found in IDENT for user '%s'", cl->account->usr);
|
---|
508 | //cs_disconnect_client();
|
---|
509 | return filt;
|
---|
510 | }
|
---|
511 | }
|
---|
512 | // empty client CAID - use server CAID
|
---|
513 | if( !filt.caid ) filt.caid=psfilt->caid;
|
---|
514 |
|
---|
515 | // 2. PROVID
|
---|
516 | if( !cl->ftab.nfilts )
|
---|
517 | {
|
---|
518 | int add;
|
---|
519 | for (i=0; i<psfilt->nprids; i++) {
|
---|
520 | // use server PROVID(s) (and only those which are in user's groups)
|
---|
521 | add = 0;
|
---|
522 | struct s_reader *rdr;
|
---|
523 | for (rdr=first_active_reader; rdr ; rdr=rdr->next)
|
---|
524 | if (rdr->grp & cl->grp) {
|
---|
525 | if (!rdr->ftab.nfilts) {
|
---|
526 | if (rdr->typ & R_IS_NETWORK) add = 1;
|
---|
527 | for (j=0; !add && j<rdr->nprov; j++)
|
---|
528 | if (b2i(3, &rdr->prid[j][1]) == psfilt->prids[i]) add = 1;
|
---|
529 | } else {
|
---|
530 | for (j=0; !add && j<rdr->ftab.nfilts; j++) {
|
---|
531 | ulong rcaid = rdr->ftab.filts[j].caid;
|
---|
532 | if (!rcaid || rcaid == filt.caid) {
|
---|
533 | for (k=0; !add && k<rdr->ftab.filts[j].nprids; k++)
|
---|
534 | if (rdr->ftab.filts[j].prids[k] == psfilt->prids[i]) add = 1;
|
---|
535 | }
|
---|
536 | }
|
---|
537 | }
|
---|
538 | }
|
---|
539 | if (add) filt.prids[filt.nprids++] = psfilt->prids[i];
|
---|
540 | }
|
---|
541 | return filt;
|
---|
542 | }
|
---|
543 |
|
---|
544 | // search in client IDENT
|
---|
545 | for( j=0; j<cl->ftab.nfilts; j++ )
|
---|
546 | {
|
---|
547 | ulong ucaid = cl->ftab.filts[j].caid;
|
---|
548 | cs_debug_mask(D_CLIENT, "client caid #%d: %04X", j, ucaid);
|
---|
549 | if( !ucaid || ucaid==filt.caid )
|
---|
550 | {
|
---|
551 | for (i=0; i<psfilt->nprids; i++)
|
---|
552 | {
|
---|
553 | cs_debug_mask(D_CLIENT, "search server provid #%d: %06X", i, psfilt->prids[i]);
|
---|
554 | if( cl->ftab.filts[j].nprids )
|
---|
555 | {
|
---|
556 | for( k=0; k<cl->ftab.filts[j].nprids; k++ )
|
---|
557 | if (cl->ftab.filts[j].prids[k] == psfilt->prids[i])
|
---|
558 | filt.prids[filt.nprids++]=cl->ftab.filts[j].prids[k];
|
---|
559 | } else {
|
---|
560 | filt.prids[filt.nprids++] = psfilt->prids[i];
|
---|
561 | // allow server PROVID(s) if no PROVID(s) specified in IDENT
|
---|
562 | }
|
---|
563 | }
|
---|
564 | }
|
---|
565 | }
|
---|
566 |
|
---|
567 | if( !filt.nprids )
|
---|
568 | {
|
---|
569 | cs_log("no valid PROVID(s) found in CAID for user '%s'", cl->account->usr);
|
---|
570 | //cs_disconnect_client();
|
---|
571 | }
|
---|
572 |
|
---|
573 | return filt;
|
---|
574 | }
|
---|
575 |
|
---|
576 | static void newcamd_auth_client(in_addr_t ip, uint8 *deskey)
|
---|
577 | {
|
---|
578 | int i, ok;
|
---|
579 | uchar *usr = NULL, *pwd = NULL;
|
---|
580 | char *client_name = NULL;
|
---|
581 | struct s_auth *account;
|
---|
582 | uchar buf[14];
|
---|
583 | uchar key[16];
|
---|
584 | uchar passwdcrypt[120];
|
---|
585 | struct s_reader *aureader = NULL, *rdr = NULL;
|
---|
586 | struct s_client *cl = cur_client();
|
---|
587 | uchar mbuf[1024];
|
---|
588 |
|
---|
589 | ok = cfg.ncd_allowed ? check_ip(cfg.ncd_allowed, ip) : 1;
|
---|
590 |
|
---|
591 | if (!ok)
|
---|
592 | {
|
---|
593 | cs_auth_client(cl, (struct s_auth *)0, NULL);
|
---|
594 | cs_exit(0);
|
---|
595 | }
|
---|
596 |
|
---|
597 | // make random 14 bytes
|
---|
598 | for( i=0; i<14; i++ ) buf[i]=fast_rnd();
|
---|
599 |
|
---|
600 | // send init sequence
|
---|
601 | send(cl->udp_fd, buf, 14, 0);
|
---|
602 | des_login_key_get(buf, deskey, 14, key);
|
---|
603 | memcpy(cl->ncd_skey, key, 16);
|
---|
604 | cl->ncd_msgid = 0;
|
---|
605 |
|
---|
606 | i=process_input(mbuf, sizeof(mbuf), cfg.cmaxidle);
|
---|
607 | if ( i>0 )
|
---|
608 | {
|
---|
609 | if( mbuf[2] != MSG_CLIENT_2_SERVER_LOGIN )
|
---|
610 | {
|
---|
611 | cs_debug_mask(D_CLIENT, "expected MSG_CLIENT_2_SERVER_LOGIN (%02X), received %02X",
|
---|
612 | MSG_CLIENT_2_SERVER_LOGIN, mbuf[2]);
|
---|
613 | NULLFREE(cl->req);
|
---|
614 | cs_exit(0);
|
---|
615 | }
|
---|
616 | usr=mbuf+5;
|
---|
617 | pwd=usr+strlen((char *)usr)+1;
|
---|
618 | }
|
---|
619 | else
|
---|
620 | {
|
---|
621 | cs_debug_mask(D_CLIENT, "bad client login request");
|
---|
622 | NULLFREE(cl->req);
|
---|
623 | cs_exit(0);
|
---|
624 | }
|
---|
625 |
|
---|
626 | snprintf(cl->ncd_client_id, sizeof(cl->ncd_client_id), "%02X%02X", mbuf[0], mbuf[1]);
|
---|
627 | client_name = get_ncd_client_name(cl->ncd_client_id);
|
---|
628 |
|
---|
629 | for (ok=0, account=cfg.account; (usr) && (account) && (!ok); account=account->next)
|
---|
630 | {
|
---|
631 | cs_debug_mask(D_CLIENT, "account->usr=%s", account->usr);
|
---|
632 | if (strcmp((char *)usr, account->usr) == 0)
|
---|
633 | {
|
---|
634 | __md5_crypt(account->pwd, "$1$abcdefgh$", (char *)passwdcrypt);
|
---|
635 | cs_debug_mask(D_CLIENT, "account->pwd=%s", passwdcrypt);
|
---|
636 | if (strcmp((char *)pwd, (const char *)passwdcrypt) == 0)
|
---|
637 | {
|
---|
638 | cl->crypted=1;
|
---|
639 | char e_txt[20];
|
---|
640 | snprintf(e_txt, 20, "%s:%d", ph[cl->ctyp].desc, cfg.ncd_ptab.ports[cl->port_idx].s_port);
|
---|
641 | if(cs_auth_client(cl, account, e_txt) == 2) {
|
---|
642 | cs_log("hostname or ip mismatch for user %s (%s)", usr, client_name);
|
---|
643 | break;
|
---|
644 | }
|
---|
645 | else
|
---|
646 | {
|
---|
647 | cs_log("user %s authenticated successfully (%s)", usr, client_name);
|
---|
648 | ok = 1;
|
---|
649 | break;
|
---|
650 | }
|
---|
651 | }
|
---|
652 | else
|
---|
653 | cs_log("user %s is providing a wrong password (%s)", usr, client_name);
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | if (!ok && !account)
|
---|
658 | {
|
---|
659 | cs_log("user %s is trying to connect but doesnt exist ! (%s)", usr, client_name);
|
---|
660 | usr = 0;
|
---|
661 | }
|
---|
662 |
|
---|
663 | // check for non ready reader and reject client
|
---|
664 | for (rdr=first_active_reader; rdr ; rdr=rdr->next) {
|
---|
665 | if(rdr->caid==cfg.ncd_ptab.ports[cl->port_idx].ftab.filts[0].caid) {
|
---|
666 | if(rdr->card_status == CARD_NEED_INIT) {
|
---|
667 | cs_log("init for reader %s not finished -> reject client", rdr->label);
|
---|
668 | ok = 0;
|
---|
669 | }
|
---|
670 | break;
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | if (ok) {
|
---|
675 | LL_ITER *itr = ll_iter_create(cl->aureader_list);
|
---|
676 | while ((rdr = ll_iter_next(itr))) {
|
---|
677 | int n;
|
---|
678 | for (n=0;n<cfg.ncd_ptab.ports[cl->port_idx].ftab.filts[0].nprids;n++) {
|
---|
679 | 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])) {
|
---|
680 | aureader=rdr;
|
---|
681 | break;
|
---|
682 | }
|
---|
683 | }
|
---|
684 | if (aureader)
|
---|
685 | break;
|
---|
686 | }
|
---|
687 | ll_iter_release(itr);
|
---|
688 |
|
---|
689 | if (aureader) {
|
---|
690 | cs_log("AU enabled for user %s on reader %s", usr, aureader->label);
|
---|
691 | } else {
|
---|
692 | cs_log("AU disabled for user %s", usr);
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | network_cmd_no_data_send(cl->udp_fd, &cl->ncd_msgid,
|
---|
697 | (ok)?MSG_CLIENT_2_SERVER_LOGIN_ACK:MSG_CLIENT_2_SERVER_LOGIN_NAK,
|
---|
698 | cl->ncd_skey, COMMTYPE_SERVER);
|
---|
699 |
|
---|
700 | if (ok)
|
---|
701 | {
|
---|
702 | FILTER *pufilt = 0;
|
---|
703 |
|
---|
704 | des_login_key_get(deskey, passwdcrypt, strlen((char *)passwdcrypt), key);
|
---|
705 | memcpy(cl->ncd_skey, key, 16);
|
---|
706 |
|
---|
707 | i=process_input(mbuf, sizeof(mbuf), cfg.cmaxidle);
|
---|
708 | if( i>0 )
|
---|
709 | {
|
---|
710 | int j,len=15;
|
---|
711 | if( mbuf[2] != MSG_CARD_DATA_REQ)
|
---|
712 | {
|
---|
713 | cs_debug_mask(D_CLIENT, "expected MSG_CARD_DATA_REQ (%02X), received %02X",
|
---|
714 | MSG_CARD_DATA_REQ, mbuf[2]);
|
---|
715 | NULLFREE(cl->req);
|
---|
716 | cs_exit(0);
|
---|
717 | }
|
---|
718 |
|
---|
719 | // set userfilter
|
---|
720 | cl->ftab.filts[0] = mk_user_ftab();
|
---|
721 |
|
---|
722 | // set userfilter for au enabled clients
|
---|
723 | if (aureader)
|
---|
724 | cl->ftab.filts[0] = mk_user_au_ftab(aureader);
|
---|
725 |
|
---|
726 | pufilt = &cl->ftab.filts[0];
|
---|
727 | cl->ftab.nfilts = 1;
|
---|
728 |
|
---|
729 | mbuf[0] = MSG_CARD_DATA;
|
---|
730 | mbuf[1] = 0x00;
|
---|
731 | mbuf[2] = 0x00;
|
---|
732 |
|
---|
733 | if(aureader)
|
---|
734 | mbuf[3] = 1;
|
---|
735 | else
|
---|
736 | mbuf[3] = get_threadnum(cl)+10; // Unique user number
|
---|
737 |
|
---|
738 | mbuf[4] = (uchar)(pufilt->caid>>8);
|
---|
739 | mbuf[5] = (uchar)(pufilt->caid);
|
---|
740 | mbuf[6] = 0x00;
|
---|
741 | mbuf[7] = 0x00;
|
---|
742 |
|
---|
743 | if (aureader)
|
---|
744 | hexserial_to_newcamd(aureader->hexserial, mbuf+8, pufilt->caid);
|
---|
745 | else
|
---|
746 | memset(&mbuf[8], 0, 6); //mbuf[8] - mbuf[13]
|
---|
747 |
|
---|
748 | mbuf[14] = pufilt->nprids;
|
---|
749 | for( j=0; j<pufilt->nprids; j++)
|
---|
750 | {
|
---|
751 | if (((pufilt->caid >> 8) == 0x17) || ((pufilt->caid >> 8) == 0x06)) // Betacrypt or Irdeto
|
---|
752 | {
|
---|
753 | mbuf[15+11*j] = 0;
|
---|
754 | mbuf[16+11*j] = 0;
|
---|
755 | mbuf[17+11*j] = j;
|
---|
756 | }
|
---|
757 | else
|
---|
758 | {
|
---|
759 | mbuf[15+11*j] = (uchar)(pufilt->prids[j]>>16);
|
---|
760 | mbuf[16+11*j] = (uchar)(pufilt->prids[j]>>8);
|
---|
761 | mbuf[17+11*j] = (uchar)(pufilt->prids[j]);
|
---|
762 | }
|
---|
763 | mbuf[18+11*j] = 0x00;
|
---|
764 | mbuf[19+11*j] = 0x00;
|
---|
765 | mbuf[20+11*j] = 0x00;
|
---|
766 | mbuf[21+11*j] = 0x00;
|
---|
767 | if (aureader)
|
---|
768 | {
|
---|
769 | // check if user provid from IDENT exists on card
|
---|
770 | int k, found;
|
---|
771 | ulong rprid;
|
---|
772 | found=0;
|
---|
773 | if( pufilt->caid==aureader->caid )
|
---|
774 | {
|
---|
775 | for( k=0; (k<aureader->nprov); k++ )
|
---|
776 | {
|
---|
777 | rprid=b2i(3, &aureader->prid[k][1]);
|
---|
778 | if( rprid==pufilt->prids[j] )
|
---|
779 | {
|
---|
780 | if (((pufilt->caid >> 8) == 0x17) || ((pufilt->caid >> 8) == 0x06)) // Betacrypt or Irdeto
|
---|
781 | {
|
---|
782 | mbuf[22+11*j] = aureader->prid[k][0];
|
---|
783 | mbuf[23+11*j] = aureader->prid[k][1];
|
---|
784 | mbuf[24+11*j] = aureader->prid[k][2];
|
---|
785 | mbuf[25+11*j] = aureader->prid[k][3];
|
---|
786 | }
|
---|
787 | else
|
---|
788 | {
|
---|
789 | mbuf[22+11*j] = aureader->sa[k][0];
|
---|
790 | mbuf[23+11*j] = aureader->sa[k][1];
|
---|
791 | mbuf[24+11*j] = aureader->sa[k][2];
|
---|
792 | mbuf[25+11*j] = aureader->sa[k][3];
|
---|
793 | }
|
---|
794 | found=1;
|
---|
795 | break;
|
---|
796 | }
|
---|
797 | }
|
---|
798 | }
|
---|
799 | if( !found )
|
---|
800 | {
|
---|
801 | mbuf[22+11*j] = 0x00;
|
---|
802 | mbuf[23+11*j] = 0x00;
|
---|
803 | mbuf[24+11*j] = 0x00;
|
---|
804 | mbuf[25+11*j] = 0x00;
|
---|
805 | }
|
---|
806 | }
|
---|
807 | else
|
---|
808 | {
|
---|
809 | if (((pufilt->caid >> 8) == 0x17) || ((pufilt->caid >> 8) == 0x06)) // Betacrypt or Irdeto
|
---|
810 | {
|
---|
811 | mbuf[22+11*j] = 0x00;
|
---|
812 | mbuf[23+11*j] = (uchar)(pufilt->prids[j]>>16);
|
---|
813 | mbuf[24+11*j] = (uchar)(pufilt->prids[j]>>8);
|
---|
814 | mbuf[25+11*j] = (uchar)(pufilt->prids[j]);
|
---|
815 | }
|
---|
816 | else
|
---|
817 | {
|
---|
818 | mbuf[22+11*j] = 0x00;
|
---|
819 | mbuf[23+11*j] = 0x00;
|
---|
820 | mbuf[24+11*j] = 0x00;
|
---|
821 | mbuf[25+11*j] = 0x00;
|
---|
822 | }
|
---|
823 | }
|
---|
824 | len+=11;
|
---|
825 | }
|
---|
826 |
|
---|
827 | custom_data_t cd;
|
---|
828 | memset(&cd, 0, sizeof(cd));
|
---|
829 |
|
---|
830 | if (aureader)
|
---|
831 | {
|
---|
832 | if (aureader->blockemm & EMM_GLOBAL)
|
---|
833 | cd.sid |= 4;
|
---|
834 | if (aureader->blockemm & EMM_SHARED)
|
---|
835 | cd.sid |= 2;
|
---|
836 | if (aureader->blockemm & EMM_UNIQUE)
|
---|
837 | cd.sid |= 1;
|
---|
838 | }
|
---|
839 |
|
---|
840 | if( network_message_send(cl->udp_fd, &cl->ncd_msgid,
|
---|
841 | mbuf, len, key, COMMTYPE_SERVER, 0, &cd) <0 )
|
---|
842 | {
|
---|
843 | NULLFREE(cl->req);
|
---|
844 | cs_exit(0);
|
---|
845 | }
|
---|
846 | }
|
---|
847 | }
|
---|
848 | else
|
---|
849 | {
|
---|
850 | cs_auth_client(cl, 0, usr ? "login failure" : "no such user");
|
---|
851 | NULLFREE(cl->req);
|
---|
852 | cs_exit(0);
|
---|
853 | }
|
---|
854 | }
|
---|
855 |
|
---|
856 | static void newcamd_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
857 | {
|
---|
858 | int len;
|
---|
859 | ushort cl_msgid;
|
---|
860 | uchar mbuf[1024];
|
---|
861 |
|
---|
862 | if (!client->udp_fd) {
|
---|
863 | cs_debug_mask(D_CLIENT, "ncd_send_dcw: error: client->udp_fd=%d", client->udp_fd);
|
---|
864 | return;
|
---|
865 | }
|
---|
866 | memcpy(&cl_msgid, client->req+(er->cpti*REQ_SIZE), 2); // get client ncd_msgid + 0x8x
|
---|
867 | mbuf[0] = er->ecm[0];
|
---|
868 | if( client->ftab.filts[0].nprids==0 || er->rc >= E_NOTFOUND /*not found*/)
|
---|
869 | {
|
---|
870 | len=3;
|
---|
871 | mbuf[1] = mbuf[2] = 0x00;
|
---|
872 | }
|
---|
873 | else
|
---|
874 | {
|
---|
875 | len = 19;
|
---|
876 | mbuf[1] = mbuf[2] = 0x10;
|
---|
877 | memcpy(mbuf+3, er->cw, 16);
|
---|
878 | }
|
---|
879 |
|
---|
880 | cs_debug_mask(D_CLIENT, "ncd_send_dcw: er->cpti=%d, cl_msgid=%d, %02X", er->cpti, cl_msgid, mbuf[0]);
|
---|
881 |
|
---|
882 | network_message_send(client->udp_fd, &cl_msgid, mbuf, len,
|
---|
883 | client->ncd_skey, COMMTYPE_SERVER, 0, NULL);
|
---|
884 | }
|
---|
885 |
|
---|
886 | static void newcamd_process_ecm(uchar *buf)
|
---|
887 | {
|
---|
888 | int pi;
|
---|
889 | struct s_client *cl = cur_client();
|
---|
890 | ECM_REQUEST *er;
|
---|
891 |
|
---|
892 | if (!(er=get_ecmtask())) {
|
---|
893 | return;
|
---|
894 | }
|
---|
895 | // save client ncd_msgid
|
---|
896 | memcpy(cl->req+(er->cpti*REQ_SIZE), &cl->ncd_msgid, 2);
|
---|
897 | cs_debug_mask(D_CLIENT, "ncd_process_ecm: er->cpti=%d, cl_msgid=%d, %02X", er->cpti,
|
---|
898 | cl->ncd_msgid, buf[2]);
|
---|
899 | er->l=buf[4]+3;
|
---|
900 | er->srvid = (buf[0]<<8)|buf[1];
|
---|
901 | er->caid = 0;
|
---|
902 | pi = cl->port_idx;
|
---|
903 | if( cfg.ncd_ptab.nports && cfg.ncd_ptab.nports >= pi )
|
---|
904 | er->caid=cfg.ncd_ptab.ports[pi].ftab.filts[0].caid;
|
---|
905 | memcpy(er->ecm, buf+2, er->l);
|
---|
906 | get_cw(cl, er);
|
---|
907 | }
|
---|
908 |
|
---|
909 | static void newcamd_process_emm(uchar *buf)
|
---|
910 | {
|
---|
911 | int ok=1;
|
---|
912 | ushort caid;
|
---|
913 | struct s_client *cl = cur_client();
|
---|
914 | EMM_PACKET epg;
|
---|
915 |
|
---|
916 | memset(&epg, 0, sizeof(epg));
|
---|
917 |
|
---|
918 | epg.l=buf[2]+3;
|
---|
919 | caid = cl->ftab.filts[0].caid;
|
---|
920 | epg.caid[0] = (uchar)(caid>>8);
|
---|
921 | epg.caid[1] = (uchar)(caid);
|
---|
922 |
|
---|
923 | /*
|
---|
924 | epg.provid[0] = (uchar)(aureader->auprovid>>24);
|
---|
925 | epg.provid[1] = (uchar)(aureader->auprovid>>16);
|
---|
926 | epg.provid[2] = (uchar)(aureader->auprovid>>8);
|
---|
927 | epg.provid[3] = (uchar)(aureader->auprovid);
|
---|
928 | */
|
---|
929 | /* if (caid == 0x0500)
|
---|
930 | {
|
---|
931 | ushort emm_head;
|
---|
932 |
|
---|
933 | emm_head = (buf[0]<<8) | buf[1];
|
---|
934 | switch( emm_head )
|
---|
935 | {
|
---|
936 | case 0x8e70: // EMM-S
|
---|
937 | memcpy(epg.hexserial+1, buf+3, 4);
|
---|
938 | epg.hexserial[4]=aureader->hexserial[4];
|
---|
939 | break;
|
---|
940 | case 0x8870: // EMM-U
|
---|
941 | case 0x8c70: // confidential ?
|
---|
942 | default:
|
---|
943 | cs_log("unsupported emm type: %04X", emm_head);
|
---|
944 | ok=0;
|
---|
945 | }
|
---|
946 | if( !ok ) cs_log("only EMM-S supported");
|
---|
947 | }
|
---|
948 | else*/
|
---|
949 |
|
---|
950 | memcpy(epg.emm, buf, epg.l);
|
---|
951 | if( ok )
|
---|
952 | do_emm(cl, &epg);
|
---|
953 |
|
---|
954 | // Should always send an answer to client (also if au is disabled),
|
---|
955 | // some clients will disconnect if they get no answer
|
---|
956 | buf[1] = 0x10;
|
---|
957 | buf[2] = 0x00;
|
---|
958 | network_message_send(cl->udp_fd, &cl->ncd_msgid, buf, 3,
|
---|
959 | cl->ncd_skey, COMMTYPE_SERVER, 0, NULL);
|
---|
960 | }
|
---|
961 |
|
---|
962 | static void * newcamd_server(void *cli)
|
---|
963 | {
|
---|
964 | int rc;
|
---|
965 | struct s_client * client = (struct s_client *) cli;
|
---|
966 | client->thread=pthread_self();
|
---|
967 | pthread_setspecific(getclient, cli);
|
---|
968 | uchar mbuf[1024];
|
---|
969 |
|
---|
970 | client->req=(uchar *)malloc(CS_MAXPENDING*REQ_SIZE);
|
---|
971 | if (!client->req)
|
---|
972 | {
|
---|
973 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
974 | cs_exit(1);
|
---|
975 | }
|
---|
976 |
|
---|
977 | memset(client->req, 0, CS_MAXPENDING*REQ_SIZE);
|
---|
978 | client->ncd_server = 1;
|
---|
979 | cs_log("client connected to %d port", cfg.ncd_ptab.ports[client->port_idx].s_port);
|
---|
980 |
|
---|
981 | if (cfg.ncd_ptab.ports[client->port_idx].ncd_key_is_set) {
|
---|
982 | //port has a des key specified
|
---|
983 | newcamd_auth_client(client->ip, cfg.ncd_ptab.ports[client->port_idx].ncd_key);
|
---|
984 | } else {
|
---|
985 | //default global des key
|
---|
986 | newcamd_auth_client(client->ip, cfg.ncd_key);
|
---|
987 | }
|
---|
988 |
|
---|
989 | // report all cards if using extended mg proto
|
---|
990 | if (cfg.ncd_mgclient) {
|
---|
991 | cs_debug_mask(D_CLIENT, "newcamd: extended: report all available cards");
|
---|
992 | int j, k;
|
---|
993 | uint8 buf[512];
|
---|
994 | custom_data_t *cd = malloc(sizeof(struct custom_data));
|
---|
995 | memset(cd, 0, sizeof(struct custom_data));
|
---|
996 | memset(buf, 0, sizeof(buf));
|
---|
997 |
|
---|
998 | buf[0] = MSG_SERVER_2_CLIENT_ADDCARD;
|
---|
999 |
|
---|
1000 | struct s_reader *rdr;
|
---|
1001 | for (rdr=first_active_reader; rdr ; rdr=rdr->next) {
|
---|
1002 | int flt = 0;
|
---|
1003 | if (!(rdr->grp & client->grp)) continue; //test - skip unaccesible readers
|
---|
1004 | if (rdr->ftab.filts) {
|
---|
1005 | for (j=0; j<CS_MAXFILTERS; j++) {
|
---|
1006 | if (rdr->ftab.filts[j].caid) {
|
---|
1007 | cd->caid = rdr->ftab.filts[j].caid;
|
---|
1008 | for (k=0; k<rdr->ftab.filts[j].nprids; k++) {
|
---|
1009 | cd->provid = rdr->ftab.filts[j].prids[k];
|
---|
1010 | cs_debug_mask(D_CLIENT, "newcamd: extended: report card");
|
---|
1011 |
|
---|
1012 | network_message_send(client->udp_fd,
|
---|
1013 | &client->ncd_msgid, buf, 3,
|
---|
1014 | client->ncd_skey, COMMTYPE_SERVER, 0, cd);
|
---|
1015 |
|
---|
1016 | flt = 1;
|
---|
1017 | }
|
---|
1018 | }
|
---|
1019 | }
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | if (rdr->caid && !flt) {
|
---|
1023 | if ((rdr->tcp_connected || rdr->card_status == CARD_INSERTED)) {
|
---|
1024 | cd->caid = rdr->caid;
|
---|
1025 | for (j=0; j<rdr->nprov; j++) {
|
---|
1026 | cd->provid = (rdr->prid[j][1]) << 16 | (rdr->prid[j][2] << 8) | rdr->prid[j][3];
|
---|
1027 | cs_debug_mask(D_CLIENT, "newcamd: extended: report card");
|
---|
1028 | network_message_send(client->udp_fd,
|
---|
1029 | &client->ncd_msgid, buf, 3,
|
---|
1030 | client->ncd_skey, COMMTYPE_SERVER, 0, cd);
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 | free(cd);
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | // check for clienttimeout, if timeout occurs try to send keepalive / wait for answer
|
---|
1039 | // befor client was disconnected. If keepalive was disabled, exit after clienttimeout
|
---|
1040 | rc=-9;
|
---|
1041 | while(rc==-9)
|
---|
1042 | {
|
---|
1043 | if (!client->pfd) break;
|
---|
1044 | // process_input returns -9 on clienttimeout
|
---|
1045 | while ((rc=process_input(mbuf, sizeof(mbuf), cfg.cmaxidle))>0)
|
---|
1046 | {
|
---|
1047 | switch(mbuf[2])
|
---|
1048 | {
|
---|
1049 | case 0x80:
|
---|
1050 | case 0x81:
|
---|
1051 | newcamd_process_ecm(mbuf);
|
---|
1052 | break;
|
---|
1053 |
|
---|
1054 | case MSG_KEEPALIVE:
|
---|
1055 | newcamd_reply_ka();
|
---|
1056 | break;
|
---|
1057 |
|
---|
1058 | default:
|
---|
1059 | if(mbuf[2]>0x81 && mbuf[2]<0x90)
|
---|
1060 | newcamd_process_emm(mbuf+2);
|
---|
1061 | else
|
---|
1062 | {
|
---|
1063 | cs_debug_mask(D_CLIENT, "unknown newcamd command! (%d)", mbuf[2]);
|
---|
1064 | }
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | if(rc==-9)
|
---|
1069 | {
|
---|
1070 | if (client->ncd_keepalive)
|
---|
1071 | newcamd_reply_ka();
|
---|
1072 | else
|
---|
1073 | rc=0;
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | NULLFREE(client->req);
|
---|
1078 | cs_disconnect_client(client);
|
---|
1079 | return NULL;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | /*
|
---|
1083 | * client functions
|
---|
1084 | */
|
---|
1085 |
|
---|
1086 | int newcamd_client_init(struct s_client *client)
|
---|
1087 | {
|
---|
1088 | struct sockaddr_in loc_sa;
|
---|
1089 | struct protoent *ptrp;
|
---|
1090 | int p_proto;
|
---|
1091 | char ptxt[16];
|
---|
1092 |
|
---|
1093 | client->pfd=0;
|
---|
1094 | if (client->reader->r_port<=0)
|
---|
1095 | {
|
---|
1096 | cs_log("invalid port %d for server %s", client->reader->r_port, client->reader->device);
|
---|
1097 | return(1);
|
---|
1098 | }
|
---|
1099 | if( (ptrp=getprotobyname("tcp")) )
|
---|
1100 | p_proto=ptrp->p_proto;
|
---|
1101 | else
|
---|
1102 | p_proto=6;
|
---|
1103 |
|
---|
1104 | client->ip=0;
|
---|
1105 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
1106 | loc_sa.sin_family = AF_INET;
|
---|
1107 | #ifdef LALL
|
---|
1108 | if (cfg.serverip[0])
|
---|
1109 | loc_sa.sin_addr.s_addr = inet_addr(cfg.serverip);
|
---|
1110 | else
|
---|
1111 | #endif
|
---|
1112 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
1113 | loc_sa.sin_port = htons(client->reader->l_port);
|
---|
1114 |
|
---|
1115 | if ((client->udp_fd=socket(PF_INET, SOCK_STREAM, p_proto))<0)
|
---|
1116 | {
|
---|
1117 | cs_log("Socket creation failed (errno=%d)", errno);
|
---|
1118 | cs_exit(1);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | #ifdef SO_PRIORITY
|
---|
1122 | if (cfg.netprio)
|
---|
1123 | setsockopt(client->udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
1124 | (void *)&cfg.netprio, sizeof(ulong));
|
---|
1125 | #endif
|
---|
1126 | if (!client->reader->tcp_ito) {
|
---|
1127 | ulong keep_alive = client->reader->tcp_ito?1:0;
|
---|
1128 | setsockopt(client->udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
1129 | (void *)&keep_alive, sizeof(ulong));
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | if (client->reader->l_port>0)
|
---|
1133 | {
|
---|
1134 | if (bind(client->udp_fd, (struct sockaddr *)&loc_sa, sizeof (loc_sa))<0)
|
---|
1135 | {
|
---|
1136 | cs_log("bind failed (errno=%d)", errno);
|
---|
1137 | close(client->udp_fd);
|
---|
1138 | return(1);
|
---|
1139 | }
|
---|
1140 | snprintf(ptxt, sizeof(ptxt), ", port=%d", client->reader->l_port);
|
---|
1141 | }
|
---|
1142 | else
|
---|
1143 | ptxt[0]='\0';
|
---|
1144 |
|
---|
1145 | memset((char *)&client->udp_sa,0,sizeof(client->udp_sa));
|
---|
1146 | client->udp_sa.sin_family = AF_INET;
|
---|
1147 | client->udp_sa.sin_port = htons((u_short)client->reader->r_port);
|
---|
1148 |
|
---|
1149 | client->ncd_proto=client->reader->ncd_proto;
|
---|
1150 |
|
---|
1151 | cs_log("proxy %s:%d newcamd52%d (fd=%d%s)",
|
---|
1152 | client->reader->device, client->reader->r_port,
|
---|
1153 | (client->reader->ncd_proto==NCD_525)?5:4, client->udp_fd, ptxt);
|
---|
1154 |
|
---|
1155 | return(0);
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | static int newcamd_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *buf)
|
---|
1159 | {
|
---|
1160 | struct s_reader *rdr = client->reader;
|
---|
1161 |
|
---|
1162 | if(!newcamd_connect())
|
---|
1163 | return (-1);
|
---|
1164 |
|
---|
1165 | // check server filters
|
---|
1166 | if(!chk_rsfilter(rdr, er))
|
---|
1167 | return(-1);
|
---|
1168 |
|
---|
1169 | memcpy(buf, er->ecm, er->l);
|
---|
1170 | return((newcamd_send(buf, er->l, er->srvid)<1) ? (-1) : 0);
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 |
|
---|
1174 | static int newcamd_send_emm(EMM_PACKET *ep)
|
---|
1175 | {
|
---|
1176 | uchar buf[ep->l];
|
---|
1177 |
|
---|
1178 | if(!newcamd_connect())
|
---|
1179 | return (-1);
|
---|
1180 |
|
---|
1181 | memcpy(buf, ep->emm, ep->l);
|
---|
1182 | return((newcamd_send(buf, ep->l, 0)<1) ? 0 : 1);
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | static int newcamd_recv_chk(struct s_client *client, uchar *dcw, int *rc, uchar *buf, int n)
|
---|
1186 | {
|
---|
1187 | ushort idx = -1;
|
---|
1188 | *client = *client; //suppress compiler error, but recv_chk should comply to other recv_chk routines...
|
---|
1189 |
|
---|
1190 | if (n<5)
|
---|
1191 | return -1;
|
---|
1192 |
|
---|
1193 | switch(buf[2]) {
|
---|
1194 | case 0x80:
|
---|
1195 | case 0x81:
|
---|
1196 | idx = (buf[0] << 8) | buf[1];
|
---|
1197 | if (n==5) { //not found on server
|
---|
1198 | *rc = 0;
|
---|
1199 | memset(dcw, 0, 16);
|
---|
1200 | break;
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | if (n<21) {
|
---|
1204 | cs_debug_mask(D_CLIENT, "invaild newcamd answer");
|
---|
1205 | return(-1);
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | *rc = 1;
|
---|
1209 | memcpy(dcw, buf+5, 16);
|
---|
1210 | break;
|
---|
1211 | case MSG_KEEPALIVE:
|
---|
1212 | return -1;
|
---|
1213 | default:
|
---|
1214 | if (buf[2]>0x81 && buf[2]<0x90) { //answer to emm
|
---|
1215 | return -1;
|
---|
1216 | }
|
---|
1217 | cs_debug_mask(D_CLIENT, "unknown newcamd command from server");
|
---|
1218 | return -1;
|
---|
1219 | }
|
---|
1220 | return(idx);
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | void module_newcamd(struct s_module *ph)
|
---|
1224 | {
|
---|
1225 | cs_strncpy(ph->desc, "newcamd", sizeof(ph->desc));
|
---|
1226 | ph->type=MOD_CONN_TCP;
|
---|
1227 | ph->logtxt = ", crypted";
|
---|
1228 | ph->multi=1;
|
---|
1229 | ph->s_ip=cfg.ncd_srvip;
|
---|
1230 | ph->s_handler=newcamd_server;
|
---|
1231 | ph->recv=newcamd_recv;
|
---|
1232 | ph->send_dcw=newcamd_send_dcw;
|
---|
1233 | ph->ptab=&cfg.ncd_ptab;
|
---|
1234 | if( ph->ptab->nports==0 )
|
---|
1235 | ph->ptab->nports=1; // show disabled in log
|
---|
1236 | ph->c_multi=1;
|
---|
1237 | ph->c_init=newcamd_client_init;
|
---|
1238 | ph->c_recv_chk=newcamd_recv_chk;
|
---|
1239 | ph->c_send_ecm=newcamd_send_ecm;
|
---|
1240 | ph->c_send_emm=newcamd_send_emm;
|
---|
1241 | ph->num=R_NEWCAMD;
|
---|
1242 | }
|
---|