2 * Client for the Emergency Access Daemon
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
30 #include <t_defines.h>
33 #include "ead-crypt.h"
35 #include "pw_encrypt_md5.c"
37 #define EAD_TIMEOUT 400
38 #define EAD_TIMEOUT_LONG 2000
40 static char msgbuf[1500];
41 static struct ead_msg *msg = (struct ead_msg *) msgbuf;
42 static uint16_t nid = 0xffff;
43 struct sockaddr_in local, remote;
47 static unsigned char *skey = NULL;
48 static unsigned char bbuf[MAXPARAMLEN];
49 static unsigned char saltbuf[MAXSALTLEN];
50 static char *username = NULL;
51 static char password[MAXPARAMLEN] = "";
52 static char pw_md5[MD5_OUT_BUFSIZE];
53 static char pw_salt[MAXSALTLEN];
55 static struct t_client *tc = NULL;
56 static struct t_num salt = { .data = saltbuf };
57 static struct t_num *A, B;
58 static struct t_preconf *tcp;
59 static int auth_type = EAD_AUTH_DEFAULT;
60 static int timeout = EAD_TIMEOUT;
63 set_nonblock(int enable)
65 if (enable == !!(sockflags & O_NONBLOCK));
68 sockflags ^= O_NONBLOCK;
69 fcntl(s, F_SETFL, sockflags);
73 send_packet(int type, bool (*handler)(void), unsigned int max)
83 sendto(s, msgbuf, sizeof(struct ead_msg) + ntohl(msg->len), 0, (struct sockaddr *) &remote, sizeof(remote));
86 tv.tv_sec = timeout / 1000;
87 tv.tv_usec = (timeout % 1000) * 1000;
92 nfds = select(s + 1, &fds, NULL, NULL, &tv);
97 if (!FD_ISSET(s, &fds))
100 len = read(s, msgbuf, sizeof(msgbuf));
104 if (len < sizeof(struct ead_msg))
107 if (len < sizeof(struct ead_msg) + ntohl(msg->len))
110 if (msg->magic != htonl(EAD_MAGIC))
113 if ((nid != 0xffff) && (ntohs(msg->nid) != nid))
116 if (msg->type != type)
122 if ((max > 0) && (res >= max))
130 prepare_password(void)
133 case EAD_AUTH_DEFAULT:
136 md5_crypt(pw_md5, (unsigned char *) password, (unsigned char *) pw_salt);
137 strncpy(password, pw_md5, sizeof(password));
145 struct ead_msg_pong *pong = EAD_DATA(msg, pong);
146 int len = msg->len - sizeof(struct ead_msg_pong);
149 auth_type = ntohs(pong->auth_type);
151 printf("%04x: %s\n", ntohs(msg->nid), pong->name);
158 struct ead_msg_salt *sb = EAD_DATA(msg, salt);
161 memcpy(salt.data, sb->salt, salt.len);
163 if (auth_type == EAD_AUTH_MD5) {
164 memcpy(pw_salt, sb->ext_salt, MAXSALTLEN);
165 pw_salt[MAXSALTLEN - 1] = 0;
168 tcp = t_getpreparam(sb->prime);
169 tc = t_clientopen(username, &tcp->modulus, &tcp->generator, &salt);
171 fprintf(stderr, "Client open failed\n");
181 struct ead_msg_number *num = EAD_DATA(msg, number);
182 int len = ntohl(msg->len) - sizeof(struct ead_msg_number);
186 memcpy(bbuf, num->data, len);
197 handle_done_auth(void)
199 struct ead_msg_auth *auth = EAD_DATA(msg, auth);
200 if (t_clientverify(tc, auth->data) != 0) {
201 fprintf(stderr, "Client auth verify failed\n");
208 handle_cmd_data(void)
210 struct ead_msg_cmd_data *cmd = EAD_ENC_DATA(msg, cmd_data);
211 int datalen = ead_decrypt_message(msg) - sizeof(struct ead_msg_cmd_data);
217 write(1, cmd->data, datalen);
225 msg->type = htonl(EAD_TYPE_PING);
227 return send_packet(EAD_TYPE_PONG, handle_pong, (nid == 0xffff ? 0 : 1));
233 msg->type = htonl(EAD_TYPE_SET_USERNAME);
234 msg->len = htonl(sizeof(struct ead_msg_user));
235 strcpy(EAD_DATA(msg, user)->username, username);
236 return send_packet(EAD_TYPE_ACK_USERNAME, handle_none, 1);
242 msg->type = htonl(EAD_TYPE_GET_PRIME);
244 return send_packet(EAD_TYPE_PRIME, handle_prime, 1);
250 struct ead_msg_number *num = EAD_DATA(msg, number);
251 A = t_clientgenexp(tc);
252 msg->type = htonl(EAD_TYPE_SEND_A);
253 msg->len = htonl(sizeof(struct ead_msg_number) + A->len);
254 memcpy(num->data, A->data, A->len);
255 return send_packet(EAD_TYPE_SEND_B, handle_b, 1);
261 struct ead_msg_auth *auth = EAD_DATA(msg, auth);
264 t_clientpasswd(tc, password);
265 skey = t_clientgetkey(tc, &B);
270 msg->type = htonl(EAD_TYPE_SEND_AUTH);
271 msg->len = htonl(sizeof(struct ead_msg_auth));
272 memcpy(auth->data, t_clientresponse(tc), sizeof(auth->data));
273 return send_packet(EAD_TYPE_DONE_AUTH, handle_done_auth, 1);
277 send_command(const char *command)
279 struct ead_msg_cmd *cmd = EAD_ENC_DATA(msg, cmd);
281 msg->type = htonl(EAD_TYPE_SEND_CMD);
282 cmd->type = htons(EAD_CMD_NORMAL);
283 cmd->timeout = htons(10);
284 strncpy((char *)cmd->data, command, 1024);
285 ead_encrypt_message(msg, sizeof(struct ead_msg_cmd) + strlen(command) + 1);
286 return send_packet(EAD_TYPE_RESULT_CMD, handle_cmd_data, 1);
291 usage(const char *prog)
293 fprintf(stderr, "Usage: %s <node> <username>[:<password>]\n"
295 "\n<node>: Node ID (4 digits hex)\n"
296 "\n<username>: Username to authenticate with\n"
298 "\nPassing no arguments shows a list of active nodes on the network\n"
304 int main(int argc, char **argv)
308 const char *command = NULL;
310 msg->magic = htonl(EAD_MAGIC);
313 memset(&local, 0, sizeof(local));
314 memset(&remote, 0, sizeof(remote));
316 remote.sin_family = AF_INET;
317 remote.sin_addr.s_addr = 0xffffffff;
318 remote.sin_port = htons(EAD_PORT);
320 local.sin_family = AF_INET;
321 local.sin_addr.s_addr = INADDR_ANY;
330 st = strchr(username, ':');
334 strncpy(password, st, sizeof(password));
335 password[sizeof(password) - 1] = 0;
336 /* hide command line password */
337 memset(st, 0, strlen(st));
341 nid = strtoul(argv[1], &st, 16);
342 if (st && st[0] != 0)
343 return usage(argv[0]);
348 return usage(argv[0]);
351 msg->nid = htons(nid);
352 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
358 setsockopt(s, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));
360 if (bind(s, (struct sockaddr *)&local, sizeof(local)) < 0) {
364 sockflags = fcntl(s, F_GETFL);
367 fprintf(stderr, "No devices found\n");
374 if (!username || !password[0])
377 if (!send_username()) {
378 fprintf(stderr, "Device did not accept user name\n");
382 fprintf(stderr, "Failed to get user password info\n");
386 timeout = EAD_TIMEOUT_LONG;
388 fprintf(stderr, "Failed to send local authentication data\n");
392 fprintf(stderr, "Authentication failed\n");
396 fprintf(stderr, "Authentication succesful\n");
399 if (!send_command(command)) {
400 fprintf(stderr, "Command failed\n");