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