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