b6a9310f0fb3382d76ec109524d23806c1b1d49a
2 * 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>
17 #include <sys/select.h>
31 #include <t_defines.h>
37 #include "ead-crypt.h"
42 #include "libbridge_init.c"
45 #define PASSWD_FILE "/etc/passwd"
47 #ifndef DEFAULT_IFNAME
48 #define DEFAULT_IFNAME "eth0"
51 #ifndef DEFAULT_DEVNAME
52 #define DEFAULT_DEVNAME "Unknown"
56 #define PCAP_TIMEOUT 200
58 #if EAD_DEBUGLEVEL >= 1
59 #define DEBUG(n, format, ...) do { \
60 if (EAD_DEBUGLEVEL >= n) \
61 fprintf(stderr, format, ##__VA_ARGS__); \
65 #define DEBUG(n, format, ...) do {} while(0)
68 static char ethmac
[6] = "\x00\x13\x37\x00\x00\x00"; /* last 3 bytes will be randomized */
69 static pcap_t
*pcap_fp
= NULL
;
70 static pcap_t
*pcap_fp_rx
= NULL
;
71 static const char *ifname
= DEFAULT_IFNAME
;
72 static char pktbuf_b
[PCAP_MRU
];
73 static struct ead_packet
*pktbuf
= (struct ead_packet
*)pktbuf_b
;
74 static u16_t nid
= 0xffff; /* node id */
75 static char username
[32] = "";
76 static int state
= EAD_TYPE_SET_USERNAME
;
77 static const char *passwd_file
= PASSWD_FILE
;
78 static const char password
[MAXPARAMLEN
];
79 static bool child_pending
= false;
81 static unsigned char abuf
[MAXPARAMLEN
+ 1];
82 static unsigned char pwbuf
[MAXPARAMLEN
];
83 static unsigned char saltbuf
[MAXSALTLEN
];
84 static unsigned char pw_saltbuf
[MAXSALTLEN
];
85 static struct list_head instances
;
86 static const char *dev_name
= DEFAULT_DEVNAME
;
87 static bool nonfork
= false;
90 static const char *brname
= NULL
;
94 struct list_head list
;
103 static struct t_pwent tpe
= {
106 .password
.data
= pwbuf
,
108 .salt
.data
= saltbuf
,
111 struct t_confent
*tce
= NULL
;
112 static struct t_server
*ts
= NULL
;
113 static struct t_num A
, *B
= NULL
;
117 prepare_password(void)
119 static char lbuf
[1024];
120 unsigned char dig
[SHA_DIGESTSIZE
];
121 BigInteger x
, v
, n
, g
;
123 int ulen
= strlen(username
);
126 lbuf
[sizeof(lbuf
) - 1] = 0;
128 f
= fopen(passwd_file
, "r");
132 while (fgets(lbuf
, sizeof(lbuf
) - 1, f
) != NULL
) {
135 if (strncmp(lbuf
, username
, ulen
) != 0)
138 if (lbuf
[ulen
] != ':')
141 str
= &lbuf
[ulen
+ 1];
143 if (strncmp(str
, "$1$", 3) != 0)
146 s2
= strchr(str
+ 3, '$');
150 if (s2
- str
>= MAXSALTLEN
)
153 strncpy((char *) pw_saltbuf
, str
, s2
- str
);
154 pw_saltbuf
[s2
- str
] = 0;
156 s2
= strchr(s2
, ':');
161 if (s2
- str
>= MAXPARAMLEN
)
164 strncpy((char *)password
, str
, MAXPARAMLEN
);
174 tce
= gettcid(tpe
.index
);
176 t_random(tpe
.password
.data
, SALTLEN
);
177 } while (memcmp(saltbuf
, (char *)dig
, sizeof(saltbuf
)) == 0);
181 n
= BigIntegerFromBytes(tce
->modulus
.data
, tce
->modulus
.len
);
182 g
= BigIntegerFromBytes(tce
->generator
.data
, tce
->generator
.len
);
183 v
= BigIntegerFromInt(0);
186 SHA1Update(&ctxt
, (unsigned char *) username
, strlen(username
));
187 SHA1Update(&ctxt
, (unsigned char *) ":", 1);
188 SHA1Update(&ctxt
, (unsigned char *) password
, strlen(password
));
189 SHA1Final(dig
, &ctxt
);
192 SHA1Update(&ctxt
, saltbuf
, tpe
.salt
.len
);
193 SHA1Update(&ctxt
, dig
, sizeof(dig
));
194 SHA1Final(dig
, &ctxt
);
196 /* x = H(s, H(u, ':', p)) */
197 x
= BigIntegerFromBytes(dig
, sizeof(dig
));
199 BigIntegerModExp(v
, g
, x
, n
);
200 tpe
.password
.len
= BigIntegerToBytes(v
, (unsigned char *)pwbuf
);
210 chksum(u16_t sum
, const u8_t
*data
, u16_t len
)
214 const u8_t
*last_byte
;
217 last_byte
= data
+ len
- 1;
219 while(dataptr
< last_byte
) { /* At least two more bytes */
220 t
= (dataptr
[0] << 8) + dataptr
[1];
228 if(dataptr
== last_byte
) {
229 t
= (dataptr
[0] << 8) + 0;
236 /* Return sum in host byte order. */
241 ead_send_packet_clone(struct ead_packet
*pkt
)
245 memcpy(pktbuf
, pkt
, offsetof(struct ead_packet
, msg
));
246 memcpy(pktbuf
->eh
.ether_shost
, ethmac
, 6);
247 memcpy(pktbuf
->eh
.ether_dhost
, pkt
->eh
.ether_shost
, 6);
250 len
= sizeof(struct ead_packet
) - sizeof(struct ether_header
) + ntohl(pktbuf
->msg
.len
);
251 pktbuf
->len
[0] = len
>> 8;
252 pktbuf
->len
[1] = len
& 0xff;
253 memcpy(pktbuf
->srcipaddr
, pkt
->destipaddr
, 4);
254 memcpy(pktbuf
->destipaddr
, pkt
->srcipaddr
, 4);
257 pktbuf
->ipchksum
= 0;
258 sum
= chksum(0, (void *) &pktbuf
->vhl
, UIP_IPH_LEN
);
261 pktbuf
->ipchksum
= htons(~sum
);
264 pktbuf
->srcport
= pkt
->destport
;
265 pktbuf
->destport
= pkt
->srcport
;
269 pktbuf
->udplen
= htons(len
);
270 pktbuf
->udpchksum
= 0;
271 sum
= len
+ UIP_PROTO_UDP
;
272 sum
= chksum(sum
, (void *) &pktbuf
->srcipaddr
[0], 8); /* src, dest ip */
273 sum
= chksum(sum
, (void *) &pktbuf
->srcport
, len
);
276 pktbuf
->udpchksum
= htons(~sum
);
277 pcap_sendpacket(pcap_fp
, (void *) pktbuf
, sizeof(struct ead_packet
) + ntohl(pktbuf
->msg
.len
));
281 set_state(int nstate
)
286 if (nstate
< state
) {
287 if ((nstate
< EAD_TYPE_GET_PRIME
) &&
288 (state
>= EAD_TYPE_GET_PRIME
)) {
296 case EAD_TYPE_SET_USERNAME
:
297 if (!prepare_password())
299 ts
= t_serveropenraw(&tpe
, tce
);
303 case EAD_TYPE_GET_PRIME
:
304 B
= t_servergenexp(ts
);
306 case EAD_TYPE_SEND_A
:
307 skey
= t_servergetkey(ts
, &A
);
321 handle_ping(struct ead_packet
*pkt
, int len
, int *nstate
)
323 struct ead_msg
*msg
= &pktbuf
->msg
;
324 struct ead_msg_pong
*pong
= EAD_DATA(msg
, pong
);
327 slen
= strlen(dev_name
);
331 msg
->len
= htonl(sizeof(struct ead_msg_pong
) + slen
);
332 strncpy(pong
->name
, dev_name
, slen
);
333 pong
->name
[slen
] = 0;
334 pong
->auth_type
= htons(EAD_AUTH_MD5
);
340 handle_set_username(struct ead_packet
*pkt
, int len
, int *nstate
)
342 struct ead_msg
*msg
= &pkt
->msg
;
343 struct ead_msg_user
*user
= EAD_DATA(msg
, user
);
345 set_state(EAD_TYPE_SET_USERNAME
); /* clear old state */
346 strncpy(username
, user
->username
, sizeof(username
));
347 username
[sizeof(username
)] = 0;
352 *nstate
= EAD_TYPE_GET_PRIME
;
357 handle_get_prime(struct ead_packet
*pkt
, int len
, int *nstate
)
359 struct ead_msg
*msg
= &pktbuf
->msg
;
360 struct ead_msg_salt
*salt
= EAD_DATA(msg
, salt
);
362 msg
->len
= htonl(sizeof(struct ead_msg_salt
));
363 salt
->prime
= tce
->index
- 1;
364 salt
->len
= ts
->s
.len
;
365 memcpy(salt
->salt
, ts
->s
.data
, ts
->s
.len
);
366 memcpy(salt
->ext_salt
, pw_saltbuf
, MAXSALTLEN
);
368 *nstate
= EAD_TYPE_SEND_A
;
373 handle_send_a(struct ead_packet
*pkt
, int len
, int *nstate
)
375 struct ead_msg
*msg
= &pkt
->msg
;
376 struct ead_msg_number
*number
= EAD_DATA(msg
, number
);
377 len
= ntohl(msg
->len
) - sizeof(struct ead_msg_number
);
379 if (len
> MAXPARAMLEN
+ 1)
384 memcpy(A
.data
, number
->data
, len
);
387 number
= EAD_DATA(msg
, number
);
388 msg
->len
= htonl(sizeof(struct ead_msg_number
) + B
->len
);
389 memcpy(number
->data
, B
->data
, B
->len
);
391 *nstate
= EAD_TYPE_SEND_AUTH
;
396 handle_send_auth(struct ead_packet
*pkt
, int len
, int *nstate
)
398 struct ead_msg
*msg
= &pkt
->msg
;
399 struct ead_msg_auth
*auth
= EAD_DATA(msg
, auth
);
401 if (t_serververify(ts
, auth
->data
) != 0) {
402 DEBUG(2, "Client authentication failed\n");
403 *nstate
= EAD_TYPE_SET_USERNAME
;
408 auth
= EAD_DATA(msg
, auth
);
409 msg
->len
= htonl(sizeof(struct ead_msg_auth
));
411 DEBUG(2, "Client authentication successful\n");
412 memcpy(auth
->data
, t_serverresponse(ts
), sizeof(auth
->data
));
414 *nstate
= EAD_TYPE_SEND_CMD
;
419 handle_send_cmd(struct ead_packet
*pkt
, int len
, int *nstate
)
421 struct ead_msg
*msg
= &pkt
->msg
;
422 struct ead_msg_cmd
*cmd
= EAD_ENC_DATA(msg
, cmd
);
423 struct ead_msg_cmd_data
*cmddata
;
424 struct timeval tv
, to
, tn
;
433 datalen
= ead_decrypt_message(msg
) - sizeof(struct ead_msg_cmd
);
437 type
= ntohs(cmd
->type
);
438 timeout
= ntohs(cmd
->timeout
);
441 cmd
->data
[datalen
] = 0;
447 fcntl(pfd
[0], F_SETFL
, O_NONBLOCK
| fcntl(pfd
[0], F_GETFL
));
448 child_pending
= true;
452 fd
= open("/dev/null", O_RDWR
);
458 system((char *)cmd
->data
);
460 } else if (pid
> 0) {
463 timeout
= EAD_CMD_TIMEOUT
;
469 case EAD_CMD_BACKGROUND
:
472 /* close stdin, stdout, stderr, replace with fd to /dev/null */
473 fd
= open("/dev/null", O_RDWR
);
479 system((char *)cmd
->data
);
481 } else if (pid
> 0) {
490 cmddata
= EAD_ENC_DATA(msg
, cmd_data
);
495 /* send keepalive packets every 200 ms so that the client doesn't timeout */
496 gettimeofday(&to
, NULL
);
497 memcpy(&tn
, &to
, sizeof(tn
));
498 tv
.tv_usec
= PCAP_TIMEOUT
* 1000;
502 FD_SET(pfd
[0], &fds
);
503 nfds
= select(pfd
[0] + 1, &fds
, NULL
, NULL
, &tv
);
506 bytes
= read(pfd
[0], cmddata
->data
, 1024);
510 if (!bytes
&& !child_pending
)
512 DEBUG(3, "Sending %d bytes of console data, type=%d, timeout=%d\n", bytes
, ntohl(msg
->type
), timeout
);
513 ead_encrypt_message(msg
, sizeof(struct ead_msg_cmd_data
) + bytes
);
514 ead_send_packet_clone(pkt
);
515 gettimeofday(&tn
, NULL
);
516 } while (tn
.tv_sec
< to
.tv_sec
+ timeout
);
523 ead_encrypt_message(msg
, sizeof(struct ead_msg_cmd_data
));
531 parse_message(struct ead_packet
*pkt
, int len
)
533 bool (*handler
)(struct ead_packet
*pkt
, int len
, int *nstate
);
534 int min_len
= sizeof(struct ead_packet
);
536 int type
= ntohl(pkt
->msg
.type
);
538 if ((type
>= EAD_TYPE_GET_PRIME
) &&
544 handler
= handle_ping
;
546 case EAD_TYPE_SET_USERNAME
:
547 handler
= handle_set_username
;
548 min_len
+= sizeof(struct ead_msg_user
);
550 case EAD_TYPE_GET_PRIME
:
551 handler
= handle_get_prime
;
553 case EAD_TYPE_SEND_A
:
554 handler
= handle_send_a
;
555 min_len
+= sizeof(struct ead_msg_number
);
557 case EAD_TYPE_SEND_AUTH
:
558 handler
= handle_send_auth
;
559 min_len
+= sizeof(struct ead_msg_auth
);
561 case EAD_TYPE_SEND_CMD
:
562 handler
= handle_send_cmd
;
563 min_len
+= sizeof(struct ead_msg_cmd
) + sizeof(struct ead_msg_encrypted
);
570 DEBUG(2, "discarding packet: message too small\n");
574 pktbuf
->msg
.magic
= htonl(EAD_MAGIC
);
575 pktbuf
->msg
.type
= htonl(type
+ 1);
576 pktbuf
->msg
.nid
= htons(nid
);
579 if (handler(pkt
, len
, &nstate
)) {
580 DEBUG(2, "sending response to packet type %d: %d\n", type
+ 1, ntohl(pktbuf
->msg
.len
));
581 /* format response packet */
582 ead_send_packet_clone(pkt
);
588 handle_packet(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*bytes
)
590 struct ead_packet
*pkt
= (struct ead_packet
*) bytes
;
592 if (h
->len
< sizeof(struct ead_packet
))
595 if (pkt
->eh
.ether_type
!= htons(ETHERTYPE_IP
))
598 if (memcmp(pkt
->eh
.ether_dhost
, "\xff\xff\xff\xff\xff\xff", 6) != 0)
601 if (pkt
->proto
!= UIP_PROTO_UDP
)
604 if (pkt
->destport
!= htons(EAD_PORT
))
607 if (pkt
->msg
.magic
!= htonl(EAD_MAGIC
))
610 if (h
->len
< sizeof(struct ead_packet
) + ntohl(pkt
->msg
.len
))
613 if ((pkt
->msg
.nid
!= 0xffff) &&
614 (pkt
->msg
.nid
!= htons(nid
)))
617 parse_message(pkt
, h
->len
);
621 ead_pcap_reopen(bool first
)
623 static char errbuf
[PCAP_ERRBUF_SIZE
] = "";
625 if (pcap_fp_rx
&& (pcap_fp_rx
!= pcap_fp
))
626 pcap_close(pcap_fp_rx
);
633 pcap_fp
= pcap_open_live(ifname
, PCAP_MRU
, 1, PCAP_TIMEOUT
, errbuf
);
636 pcap_fp_rx
= pcap_open_live(brname
, PCAP_MRU
, 1, PCAP_TIMEOUT
, errbuf
);
639 pcap_fp_rx
= pcap_fp
;
640 pcap_setfilter(pcap_fp_rx
, &pktfilter
);
641 if (first
&& !pcap_fp
) {
642 DEBUG(1, "WARNING: unable to open interface '%s'\n", ifname
);
655 if (pcap_dispatch(pcap_fp_rx
, 1, handle_packet
, NULL
) < 0) {
656 ead_pcap_reopen(false);
664 usage(const char *prog
)
666 fprintf(stderr
, "Usage: %s [<options>]\n"
668 "\t-B Run in background mode\n"
669 "\t-d <device> Set the device to listen on\n"
670 "\t-D <name> Set the name of the device visible to clients\n"
671 "\t-p <file> Set the password file for authenticating\n"
672 "\t-P <file> Write a pidfile\n"
678 server_handle_sigchld(int sig
)
680 struct ead_instance
*in
;
685 list_for_each(p
, &instances
) {
686 in
= list_entry(p
, struct ead_instance
, list
);
696 instance_handle_sigchld(int sig
)
700 child_pending
= false;
704 start_server(struct ead_instance
*i
)
715 signal(SIGCHLD
, instance_handle_sigchld
);
721 ead_pcap_reopen(true);
724 if (pcap_fp_rx
!= pcap_fp
)
725 pcap_close(pcap_fp_rx
);
732 start_servers(bool restart
)
734 struct ead_instance
*in
;
737 list_for_each(p
, &instances
) {
738 in
= list_entry(p
, struct ead_instance
, list
);
748 stop_server(struct ead_instance
*in
, bool do_free
)
751 kill(in
->pid
, SIGKILL
);
760 server_handle_sigint(int sig
)
762 struct ead_instance
*in
;
763 struct list_head
*p
, *tmp
;
765 list_for_each_safe(p
, tmp
, &instances
) {
766 in
= list_entry(p
, struct ead_instance
, list
);
767 stop_server(in
, true);
774 check_bridge_port(const char *br
, const char *port
, void *arg
)
776 struct ead_instance
*in
;
777 struct list_head
*p
, *tmp
;
779 list_for_each(p
, &instances
) {
780 in
= list_entry(p
, struct ead_instance
, list
);
782 if (strcmp(in
->name
, port
) != 0)
786 if (strcmp(in
->bridge
, br
) == 0)
789 strncpy(in
->bridge
, br
, sizeof(in
->bridge
));
790 DEBUG(2, "assigning port %s to bridge %s\n", in
->name
, in
->bridge
);
791 stop_server(in
, false);
797 check_bridge(const char *name
, void *arg
)
799 br_foreach_port(name
, check_bridge_port
, arg
);
805 check_all_interfaces(void)
808 struct ead_instance
*in
;
809 struct list_head
*p
, *tmp
;
811 br_foreach_bridge(check_bridge
, NULL
);
813 /* look for interfaces that are no longer part of a bridge */
814 list_for_each(p
, &instances
) {
815 in
= list_entry(p
, struct ead_instance
, list
);
818 in
->br_check
= false;
819 } else if (in
->bridge
[0]) {
820 DEBUG(2, "removing port %s from bridge %s\n", in
->name
, in
->bridge
);
822 stop_server(in
, false);
829 int main(int argc
, char **argv
)
831 struct ead_instance
*in
;
834 const char *pidfile
= NULL
;
835 bool background
= false;
839 return usage(argv
[0]);
841 INIT_LIST_HEAD(&instances
);
842 while ((ch
= getopt(argc
, argv
, "Bd:D:fhp:P:")) != -1) {
851 return usage(argv
[0]);
853 in
= malloc(sizeof(struct ead_instance
));
854 memset(in
, 0, sizeof(struct ead_instance
));
855 INIT_LIST_HEAD(&in
->list
);
856 strncpy(in
->name
, optarg
, sizeof(in
->name
) - 1);
857 list_add(&in
->list
, &instances
);
864 passwd_file
= optarg
;
871 signal(SIGCHLD
, server_handle_sigchld
);
872 signal(SIGINT
, server_handle_sigint
);
873 signal(SIGTERM
, server_handle_sigint
);
874 signal(SIGKILL
, server_handle_sigint
);
877 fprintf(stderr
, "Error: ead needs at least one interface\n");
885 fd
= open("/dev/null", O_RDWR
);
896 fd
= open(pidfile
, O_CREAT
|O_WRONLY
|O_EXCL
, 0644);
898 len
= sprintf(pid
, "%d\n", getpid());
904 /* randomize the mac address */
905 fd
= open("/dev/urandom", O_RDONLY
);
910 read(fd
, ethmac
+ 3, 3);
912 nid
= *(((u16_t
*) ethmac
) + 2);
914 start_servers(false);
921 check_all_interfaces();
This page took 0.100339 seconds and 3 git commands to generate.