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