2 * Emergency Access Daemon
3 * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
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>
17 #include <sys/select.h>
31 #include <t_defines.h>
38 #include "ead-crypt.h"
39 #include "libbridge.h"
44 #include <linux/if_packet.h>
47 #define PASSWD_FILE "/etc/passwd"
49 #ifndef DEFAULT_IFNAME
50 #define DEFAULT_IFNAME "eth0"
53 #ifndef DEFAULT_DEVNAME
54 #define DEFAULT_DEVNAME "Unknown"
58 #define PCAP_TIMEOUT 200
60 #if EAD_DEBUGLEVEL >= 1
61 #define DEBUG(n, format, ...) do { \
62 if (EAD_DEBUGLEVEL >= n) \
63 fprintf(stderr, format, ##__VA_ARGS__); \
67 #define DEBUG(n, format, ...) do {} while(0)
71 struct list_head list;
79 static char ethmac[6] = "\x00\x13\x37\x00\x00\x00"; /* last 3 bytes will be randomized */
80 static pcap_t *pcap_fp = NULL;
81 static pcap_t *pcap_fp_rx = NULL;
82 static char pktbuf_b[PCAP_MRU];
83 static struct ead_packet *pktbuf = (struct ead_packet *)pktbuf_b;
84 static u16_t nid = 0xffff; /* node id */
85 static char username[32] = "";
86 static int state = EAD_TYPE_SET_USERNAME;
87 static const char *passwd_file = PASSWD_FILE;
88 static const char password[MAXPARAMLEN];
89 static bool child_pending = false;
91 static unsigned char abuf[MAXPARAMLEN + 1];
92 static unsigned char pwbuf[MAXPARAMLEN];
93 static unsigned char saltbuf[MAXSALTLEN];
94 static unsigned char pw_saltbuf[MAXSALTLEN];
95 static struct list_head instances;
96 static const char *dev_name = DEFAULT_DEVNAME;
97 static bool nonfork = false;
98 static struct ead_instance *instance = NULL;
100 static struct t_pwent tpe = {
103 .password.data = pwbuf,
105 .salt.data = saltbuf,
108 struct t_confent *tce = NULL;
109 static struct t_server *ts = NULL;
110 static struct t_num A, *B = NULL;
114 set_recv_type(pcap_t *p, bool rx)
116 #ifdef PACKET_RECV_TYPE
117 struct sockaddr_ll sll;
122 fd = pcap_get_selectable_fd(p);
127 mask = 1 << PACKET_BROADCAST;
131 setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &mask, sizeof(mask));
137 ead_open_pcap(const char *ifname, char *errbuf, bool rx)
141 p = pcap_create(ifname, errbuf);
145 pcap_set_snaplen(p, PCAP_MRU);
146 pcap_set_promisc(p, rx);
147 pcap_set_timeout(p, PCAP_TIMEOUT);
148 pcap_set_protocol_linux(p, (rx ? htons(ETH_P_IP) : 0));
149 pcap_set_buffer_size(p, (rx ? 10 : 1) * PCAP_MRU);
151 set_recv_type(p, rx);
157 get_random_bytes(void *ptr, int len)
161 fd = open("/dev/urandom", O_RDONLY);
171 prepare_password(void)
173 static char lbuf[1024];
174 unsigned char dig[SHA_DIGESTSIZE];
175 BigInteger x, v, n, g;
177 int ulen = strlen(username);
180 lbuf[sizeof(lbuf) - 1] = 0;
182 f = fopen(passwd_file, "r");
186 while (fgets(lbuf, sizeof(lbuf) - 1, f) != NULL) {
189 if (strncmp(lbuf, username, ulen) != 0)
192 if (lbuf[ulen] != ':')
195 str = &lbuf[ulen + 1];
197 if (strncmp(str, "$1$", 3) != 0)
200 s2 = strchr(str + 3, '$');
204 if (s2 - str >= MAXSALTLEN)
207 strncpy((char *) pw_saltbuf, str, s2 - str);
208 pw_saltbuf[s2 - str] = 0;
210 s2 = strchr(s2, ':');
215 if (s2 - str >= MAXPARAMLEN)
218 strncpy((char *)password, str, MAXPARAMLEN);
228 tce = gettcid(tpe.index);
230 t_random(tpe.password.data, SALTLEN);
231 } while (memcmp(saltbuf, (char *)dig, sizeof(saltbuf)) == 0);
235 n = BigIntegerFromBytes(tce->modulus.data, tce->modulus.len);
236 g = BigIntegerFromBytes(tce->generator.data, tce->generator.len);
237 v = BigIntegerFromInt(0);
240 SHA1Update(&ctxt, (unsigned char *) username, strlen(username));
241 SHA1Update(&ctxt, (unsigned char *) ":", 1);
242 SHA1Update(&ctxt, (unsigned char *) password, strlen(password));
243 SHA1Final(dig, &ctxt);
246 SHA1Update(&ctxt, saltbuf, tpe.salt.len);
247 SHA1Update(&ctxt, dig, sizeof(dig));
248 SHA1Final(dig, &ctxt);
250 /* x = H(s, H(u, ':', p)) */
251 x = BigIntegerFromBytes(dig, sizeof(dig));
253 BigIntegerModExp(v, g, x, n);
254 tpe.password.len = BigIntegerToBytes(v, (unsigned char *)pwbuf);
264 chksum(u16_t sum, const u8_t *data, u16_t len)
268 const u8_t *last_byte;
271 last_byte = data + len - 1;
273 while(dataptr < last_byte) { /* At least two more bytes */
274 t = (dataptr[0] << 8) + dataptr[1];
282 if(dataptr == last_byte) {
283 t = (dataptr[0] << 8) + 0;
290 /* Return sum in host byte order. */
295 ead_send_packet_clone(struct ead_packet *pkt)
299 memcpy(pktbuf, pkt, offsetof(struct ead_packet, msg));
300 memcpy(pktbuf->eh.ether_shost, ethmac, 6);
301 memcpy(pktbuf->eh.ether_dhost, pkt->eh.ether_shost, 6);
304 len = sizeof(struct ead_packet) - sizeof(struct ether_header) + ntohl(pktbuf->msg.len);
305 pktbuf->len[0] = len >> 8;
306 pktbuf->len[1] = len & 0xff;
307 memcpy(pktbuf->srcipaddr, &pkt->msg.ip, 4);
308 memcpy(pktbuf->destipaddr, pkt->srcipaddr, 4);
311 pktbuf->ipchksum = 0;
312 sum = chksum(0, (void *) &pktbuf->vhl, UIP_IPH_LEN);
315 pktbuf->ipchksum = htons(~sum);
318 pktbuf->srcport = pkt->destport;
319 pktbuf->destport = pkt->srcport;
323 pktbuf->udplen = htons(len);
324 pktbuf->udpchksum = 0;
325 sum = len + UIP_PROTO_UDP;
326 sum = chksum(sum, (void *) &pktbuf->srcipaddr[0], 8); /* src, dest ip */
327 sum = chksum(sum, (void *) &pktbuf->srcport, len);
330 pktbuf->udpchksum = htons(~sum);
331 pcap_sendpacket(pcap_fp, (void *) pktbuf, sizeof(struct ead_packet) + ntohl(pktbuf->msg.len));
335 set_state(int nstate)
340 if (nstate < state) {
341 if ((nstate < EAD_TYPE_GET_PRIME) &&
342 (state >= EAD_TYPE_GET_PRIME)) {
350 case EAD_TYPE_SET_USERNAME:
351 if (!prepare_password())
353 ts = t_serveropenraw(&tpe, tce);
357 case EAD_TYPE_GET_PRIME:
358 B = t_servergenexp(ts);
360 case EAD_TYPE_SEND_A:
361 skey = t_servergetkey(ts, &A);
375 handle_ping(struct ead_packet *pkt, int len, int *nstate)
377 struct ead_msg *msg = &pktbuf->msg;
378 struct ead_msg_pong *pong = EAD_DATA(msg, pong);
381 slen = strlen(dev_name);
385 msg->len = htonl(sizeof(struct ead_msg_pong) + slen);
386 strncpy(pong->name, dev_name, slen);
387 pong->name[slen] = 0;
388 pong->auth_type = htons(EAD_AUTH_MD5);
394 handle_set_username(struct ead_packet *pkt, int len, int *nstate)
396 struct ead_msg *msg = &pkt->msg;
397 struct ead_msg_user *user = EAD_DATA(msg, user);
399 set_state(EAD_TYPE_SET_USERNAME); /* clear old state */
400 strncpy(username, user->username, sizeof(username));
401 username[sizeof(username) - 1] = 0;
406 *nstate = EAD_TYPE_GET_PRIME;
411 handle_get_prime(struct ead_packet *pkt, int len, int *nstate)
413 struct ead_msg *msg = &pktbuf->msg;
414 struct ead_msg_salt *salt = EAD_DATA(msg, salt);
416 msg->len = htonl(sizeof(struct ead_msg_salt));
417 salt->prime = tce->index - 1;
418 salt->len = ts->s.len;
419 memcpy(salt->salt, ts->s.data, ts->s.len);
420 memcpy(salt->ext_salt, pw_saltbuf, MAXSALTLEN);
422 *nstate = EAD_TYPE_SEND_A;
427 handle_send_a(struct ead_packet *pkt, int len, int *nstate)
429 struct ead_msg *msg = &pkt->msg;
430 struct ead_msg_number *number = EAD_DATA(msg, number);
431 len = ntohl(msg->len) - sizeof(struct ead_msg_number);
433 if (len > MAXPARAMLEN + 1)
438 memcpy(A.data, number->data, len);
441 number = EAD_DATA(msg, number);
442 msg->len = htonl(sizeof(struct ead_msg_number) + B->len);
443 memcpy(number->data, B->data, B->len);
445 *nstate = EAD_TYPE_SEND_AUTH;
450 handle_send_auth(struct ead_packet *pkt, int len, int *nstate)
452 struct ead_msg *msg = &pkt->msg;
453 struct ead_msg_auth *auth = EAD_DATA(msg, auth);
455 if (t_serververify(ts, auth->data) != 0) {
456 DEBUG(2, "Client authentication failed\n");
457 *nstate = EAD_TYPE_SET_USERNAME;
462 auth = EAD_DATA(msg, auth);
463 msg->len = htonl(sizeof(struct ead_msg_auth));
465 DEBUG(2, "Client authentication successful\n");
466 memcpy(auth->data, t_serverresponse(ts), sizeof(auth->data));
468 *nstate = EAD_TYPE_SEND_CMD;
473 handle_send_cmd(struct ead_packet *pkt, int len, int *nstate)
475 struct ead_msg *msg = &pkt->msg;
476 struct ead_msg_cmd *cmd = EAD_ENC_DATA(msg, cmd);
477 struct ead_msg_cmd_data *cmddata;
478 struct timeval tv, to, tn;
487 datalen = ead_decrypt_message(msg) - sizeof(struct ead_msg_cmd);
491 type = ntohs(cmd->type);
492 timeout = ntohs(cmd->timeout);
495 cmd->data[datalen] = 0;
501 fcntl(pfd[0], F_SETFL, O_NONBLOCK | fcntl(pfd[0], F_GETFL));
502 child_pending = true;
506 fd = open("/dev/null", O_RDWR);
512 system((char *)cmd->data);
514 } else if (pid > 0) {
517 timeout = EAD_CMD_TIMEOUT;
523 case EAD_CMD_BACKGROUND:
526 /* close stdin, stdout, stderr, replace with fd to /dev/null */
527 fd = open("/dev/null", O_RDWR);
533 system((char *)cmd->data);
535 } else if (pid > 0) {
544 cmddata = EAD_ENC_DATA(msg, cmd_data);
549 /* send keepalive packets every 200 ms so that the client doesn't timeout */
550 gettimeofday(&to, NULL);
551 memcpy(&tn, &to, sizeof(tn));
552 tv.tv_usec = PCAP_TIMEOUT * 1000;
556 FD_SET(pfd[0], &fds);
557 nfds = select(pfd[0] + 1, &fds, NULL, NULL, &tv);
560 bytes = read(pfd[0], cmddata->data, 1024);
564 if (!bytes && !child_pending)
566 DEBUG(3, "Sending %d bytes of console data, type=%d, timeout=%d\n", bytes, ntohl(msg->type), timeout);
567 ead_encrypt_message(msg, sizeof(struct ead_msg_cmd_data) + bytes);
568 ead_send_packet_clone(pkt);
569 gettimeofday(&tn, NULL);
570 } while (tn.tv_sec < to.tv_sec + timeout);
577 ead_encrypt_message(msg, sizeof(struct ead_msg_cmd_data));
585 parse_message(struct ead_packet *pkt, int len)
587 bool (*handler)(struct ead_packet *pkt, int len, int *nstate);
588 int min_len = sizeof(struct ead_packet);
590 int type = ntohl(pkt->msg.type);
592 if ((type >= EAD_TYPE_GET_PRIME) &&
596 if ((type != EAD_TYPE_PING) &&
597 ((ntohs(pkt->msg.sid) & EAD_INSTANCE_MASK) >>
598 EAD_INSTANCE_SHIFT) != instance->id)
603 handler = handle_ping;
605 case EAD_TYPE_SET_USERNAME:
606 handler = handle_set_username;
607 min_len += sizeof(struct ead_msg_user);
609 case EAD_TYPE_GET_PRIME:
610 handler = handle_get_prime;
612 case EAD_TYPE_SEND_A:
613 handler = handle_send_a;
614 min_len += sizeof(struct ead_msg_number);
616 case EAD_TYPE_SEND_AUTH:
617 handler = handle_send_auth;
618 min_len += sizeof(struct ead_msg_auth);
620 case EAD_TYPE_SEND_CMD:
621 handler = handle_send_cmd;
622 min_len += sizeof(struct ead_msg_cmd) + sizeof(struct ead_msg_encrypted);
629 DEBUG(2, "discarding packet: message too small\n");
633 pktbuf->msg.magic = htonl(EAD_MAGIC);
634 pktbuf->msg.type = htonl(type + 1);
635 pktbuf->msg.nid = htons(nid);
636 pktbuf->msg.sid = pkt->msg.sid;
639 if (handler(pkt, len, &nstate)) {
640 DEBUG(2, "sending response to packet type %d: %d\n", type + 1, ntohl(pktbuf->msg.len));
641 /* format response packet */
642 ead_send_packet_clone(pkt);
648 handle_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
650 struct ead_packet *pkt = (struct ead_packet *) bytes;
652 if (h->len < sizeof(struct ead_packet))
655 if (pkt->eh.ether_type != htons(ETHERTYPE_IP))
658 if (memcmp(pkt->eh.ether_dhost, "\xff\xff\xff\xff\xff\xff", 6) != 0)
661 if (pkt->proto != UIP_PROTO_UDP)
664 if (pkt->destport != htons(EAD_PORT))
667 if (pkt->msg.magic != htonl(EAD_MAGIC))
670 if (h->len < sizeof(struct ead_packet) + ntohl(pkt->msg.len))
673 if ((pkt->msg.nid != 0xffff) &&
674 (pkt->msg.nid != htons(nid)))
677 parse_message(pkt, h->len);
681 ead_pcap_reopen(bool first)
683 static char errbuf[PCAP_ERRBUF_SIZE] = "";
685 if (pcap_fp_rx && (pcap_fp_rx != pcap_fp))
686 pcap_close(pcap_fp_rx);
693 if (instance->bridge[0]) {
694 pcap_fp_rx = ead_open_pcap(instance->bridge, errbuf, 1);
695 pcap_fp = ead_open_pcap(instance->ifname, errbuf, 0);
697 pcap_fp = ead_open_pcap(instance->ifname, errbuf, 1);
701 pcap_fp_rx = pcap_fp;
702 if (first && !pcap_fp) {
703 DEBUG(1, "WARNING: unable to open interface '%s'\n", instance->ifname);
709 pcap_setfilter(pcap_fp_rx, &pktfilter);
717 if (pcap_dispatch(pcap_fp_rx, 1, handle_packet, NULL) < 0) {
718 ead_pcap_reopen(false);
726 usage(const char *prog)
728 fprintf(stderr, "Usage: %s [<options>]\n"
730 "\t-B Run in background mode\n"
731 "\t-d <device> Set the device to listen on\n"
732 "\t-D <name> Set the name of the device visible to clients\n"
733 "\t-p <file> Set the password file for authenticating\n"
734 "\t-P <file> Write a pidfile\n"
740 server_handle_sigchld(int sig)
742 struct ead_instance *in;
747 list_for_each(p, &instances) {
748 in = list_entry(p, struct ead_instance, list);
758 instance_handle_sigchld(int sig)
762 child_pending = false;
766 start_server(struct ead_instance *i)
778 signal(SIGCHLD, instance_handle_sigchld);
779 ead_pcap_reopen(true);
782 if (pcap_fp_rx != pcap_fp)
783 pcap_close(pcap_fp_rx);
790 start_servers(bool restart)
792 struct ead_instance *in;
795 list_for_each(p, &instances) {
796 in = list_entry(p, struct ead_instance, list);
806 stop_server(struct ead_instance *in, bool do_free)
809 kill(in->pid, SIGKILL);
818 server_handle_sigint(int sig)
820 struct ead_instance *in;
821 struct list_head *p, *tmp;
823 list_for_each_safe(p, tmp, &instances) {
824 in = list_entry(p, struct ead_instance, list);
825 stop_server(in, true);
831 check_bridge_port(const char *br, const char *port, void *arg)
833 struct ead_instance *in;
836 list_for_each(p, &instances) {
837 in = list_entry(p, struct ead_instance, list);
839 if (strcmp(in->ifname, port) != 0)
843 if (strcmp(in->bridge, br) == 0)
846 strncpy(in->bridge, br, sizeof(in->bridge));
847 DEBUG(2, "assigning port %s to bridge %s\n", in->ifname, in->bridge);
848 stop_server(in, false);
854 check_bridge(const char *name, void *arg)
856 br_foreach_port(name, check_bridge_port, arg);
861 check_all_interfaces(void)
863 struct ead_instance *in;
866 br_foreach_bridge(check_bridge, NULL);
868 /* look for interfaces that are no longer part of a bridge */
869 list_for_each(p, &instances) {
870 in = list_entry(p, struct ead_instance, list);
873 in->br_check = false;
874 } else if (in->bridge[0]) {
875 DEBUG(2, "removing port %s from bridge %s\n", in->ifname, in->bridge);
877 stop_server(in, false);
883 int main(int argc, char **argv)
885 struct ead_instance *in;
887 const char *pidfile = NULL;
888 bool background = false;
893 return usage(argv[0]);
895 INIT_LIST_HEAD(&instances);
896 while ((ch = getopt(argc, argv, "Bd:D:fhp:P:")) != -1) {
905 return usage(argv[0]);
907 in = malloc(sizeof(struct ead_instance));
908 memset(in, 0, sizeof(struct ead_instance));
909 INIT_LIST_HEAD(&in->list);
910 strncpy(in->ifname, optarg, sizeof(in->ifname) - 1);
911 list_add(&in->list, &instances);
918 passwd_file = optarg;
925 signal(SIGCHLD, server_handle_sigchld);
926 signal(SIGINT, server_handle_sigint);
927 signal(SIGTERM, server_handle_sigint);
928 signal(SIGKILL, server_handle_sigint);
931 fprintf(stderr, "Error: ead needs at least one interface\n");
939 fd = open("/dev/null", O_RDWR);
950 fd = open(pidfile, O_CREAT|O_WRONLY|O_EXCL, 0644);
952 len = sprintf(pid, "%d\n", getpid());
958 /* randomize the mac address */
959 get_random_bytes(ethmac + 3, 3);
960 nid = *(((u16_t *) ethmac) + 2);
962 start_servers(false);
967 check_all_interfaces();