3 * $Id: admin.c,v 1.9 2004/04/03 10:22:10 mdw Exp $
5 * Admin interface for configuration
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.9 2004/04/03 10:22:10 mdw
33 * Don't warn about boring errors from @accept@.
35 * Revision 1.8 2003/04/06 10:25:17 mdw
36 * Support Linux TUN/TAP device. Fix some bugs.
38 * Revision 1.7 2002/01/13 14:57:33 mdw
39 * Track @lbuf@ and @dstr_vputf@ changes in mLib.
41 * Revision 1.6 2001/02/19 19:11:09 mdw
42 * Output buffering on admin connections.
44 * Revision 1.5 2001/02/16 21:22:51 mdw
45 * Support for displaying statistics. Make client connections blocking, so
46 * that things don't get dropped. (This might change again if I add
49 * Revision 1.4 2001/02/06 09:34:53 mdw
50 * Change ERR response to FAIL for consistency with other programs.
52 * Revision 1.3 2001/02/04 01:17:12 mdw
53 * The `DAEMON' notification to stdout is replaced by a warning. The
54 * `DAEMON' and `QUIT' command send `OK' on successful completion. Put
55 * assignment of sequence number in a T(...) guard.
57 * Revision 1.2 2001/02/03 22:40:29 mdw
58 * Put timer information into the entropy pool when packets are received
59 * and on similar events. Reseed the generator on the interval timer.
61 * Revision 1.1 2001/02/03 20:26:37 mdw
66 /*----- Header files ------------------------------------------------------*/
70 /*----- Global variables --------------------------------------------------*/
74 const trace_opt tr_opts
[] = {
75 { 't', T_TUNNEL
, "tunnel events" },
76 { 'r', T_PEER
, "peer events" },
77 { 'a', T_ADMIN
, "admin interface" },
78 { 'p', T_PACKET
, "packet contents" },
79 { 'c', T_CRYPTO
, "crypto details" },
80 { 's', T_KEYSET
, "symmetric keyset management" },
81 { 'x', T_KEYEXCH
, "key exchange" },
82 { 'm', T_KEYMGMT
, "key management" },
83 { 'A', T_ALL
, "all of the above" },
87 unsigned tr_flags
= 0;
90 /*----- Static variables --------------------------------------------------*/
94 static const char *sockname
;
95 static unsigned flags
= 0;
96 static admin
*a_stdin
= 0;
97 static sig s_term
, s_int
, s_hup
;
102 #define T_RESOLVE SEC(30)
104 static void a_destroy(admin */
*a*/
);
105 static void a_lock(admin */
*a*/
);
106 static void a_unlock(admin */
*a*/
);
108 /*----- Output functions --------------------------------------------------*/
110 /* --- @trywrite@ --- *
112 * Arguments: @admin *a@ = pointer to an admin block
113 * @const char *p@ = pointer to buffer to write
114 * @size_t sz@ = size of data to write
116 * Returns: The number of bytes written, or less than zero on error.
118 * Use: Attempts to write data to a client.
121 static ssize_t
trywrite(admin
*a
, const char *p
, size_t sz
)
128 n
= write(a
->w
.fd
, p
, sz
);
138 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
140 a_warn("disconnecting admin client due to write errors: %s",
148 /* --- @dosend@ --- *
150 * Arguemnts: @admin *a@ = pointer to an admin block
151 * @const char *p@ = pointer to buffer to write
152 * @size_t sz@ = size of data to write
156 * Use: Sends data to an admin client.
159 static void dosend(admin
*a
, const char *p
, size_t sz
)
167 /* --- Try to send the data immediately --- */
170 if ((n
= trywrite(a
, p
, sz
)) < 0)
178 /* --- Fill buffers with the data until it's all gone --- */
183 else if (o
->p_in
< o
->buf
+ OBUFSZ
)
187 o
= xmalloc(sizeof(obuf
));
189 o
->p_in
= o
->p_out
= o
->buf
;
197 n
= o
->buf
+ OBUFSZ
- o
->p_in
;
200 memcpy(o
->p_in
, p
, n
);
207 /* --- @a_flush@ --- *
209 * Arguments: @int fd@ = file descriptor
210 * @unsigned mode@ = what's happening
211 * @void *v@ = pointer to my admin block
215 * Use: Flushes buffers when a client is ready to read again.
218 static void a_flush(int fd
, unsigned mode
, void *v
)
226 if ((n
= trywrite(a
, o
->p_out
, o
->p_in
- o
->p_out
)) < 0)
229 if (o
->p_in
< o
->p_out
)
242 /*----- Utility functions -------------------------------------------------*/
244 /* --- @a_write@ --- *
246 * Arguments: @admin *a@ = admin connection to write to
247 * @const char *fmt@ = pointer to format string
248 * @...@ = other arguments
252 * Use: Sends a message to an admin connection.
255 static void a_write(admin
*a
, const char *fmt
, ...)
260 dstr_vputf(&d
, fmt
, &ap
);
262 dosend(a
, d
.buf
, d
.len
);
266 /* --- @a_warn@ --- *
268 * Arguments: @const char *fmt@ = pointer to format string
269 * @...@ = other arguments
273 * Use: Informs all admin connections of a warning.
276 void a_warn(const char *fmt
, ...)
283 dstr_puts(&d
, "WARN ");
285 dstr_vputf(&d
, fmt
, &ap
);
287 if (!(flags
& F_INIT
))
291 for (a
= admins
; a
; a
= aa
) {
293 dosend(a
, d
.buf
, d
.len
);
299 /* --- @a_trace@ --- *
301 * Arguments: @const char *p@ = pointer to a buffer
302 * @size_t sz@ = size of the buffer
303 * @void *v@ = uninteresting pointer
307 * Use: Custom trace output handler.
311 static void a_trace(const char *p
, size_t sz
, void *v
)
316 dstr_puts(&d
, "TRACE ");
317 dstr_putm(&d
, p
, sz
);
319 for (a
= admins
; a
; a
= aa
) {
321 dosend(a
, d
.buf
, d
.len
);
327 /* --- @a_quit@ --- *
333 * Use: Shuts things down nicely.
343 /* --- @a_sigdie@ --- *
345 * Arguments: @int sig@ = signal number
346 * @void *v@ = an uninteresting argument
350 * Use Shuts down on receipt of a fatal signal.
353 static void a_sigdie(int sig
, void *v
)
359 case SIGTERM
: p
= "SIGTERM"; break;
360 case SIGINT
: p
= "SIGINT"; break;
362 sprintf(buf
, "signal %i", sig
);
366 a_warn("shutting down on %s", p
);
370 /* --- @a_sighup@ --- *
372 * Arguments: @int sig@ = signal number
373 * @void *v@ = an uninteresting argument
377 * Use Logs a message about SIGHUP not being useful.
380 static void a_sighup(int sig
, void *v
)
382 a_warn("received SIGHUP: ignoring");
385 /*----- Adding peers ------------------------------------------------------*/
387 /* --- @a_resolve@ --- *
389 * Arguments: @struct hostent *h@ = pointer to resolved hostname
390 * @void *v@ = pointer to admin block
394 * Use: Handles a completed name resolution.
397 static void a_resolve(struct hostent
*h
, void *v
)
402 T( trace(T_ADMIN
, "admin: %u resolved", a
->seq
); )
406 a_write(a
, "FAIL couldn't resolve hostname `%s'\n", a
->paddr
);
407 else if (p_find(a
->pname
))
408 a_write(a
, "FAIL peer `%s' already registered\n", a
->pname
);
410 memcpy(&a
->peer
.sin
.sin_addr
, h
->h_addr
, sizeof(struct in_addr
));
411 if (!p_create(a
->pname
, &a
->peer
.sa
, a
->sasz
))
412 a_write(a
, "FAIL couldn't create peer\n");
419 selbuf_enable(&a
->b
);
423 /* --- @a_timer@ --- *
425 * Arguments: @struct timeval *tv@ = timer
426 * @void *v@ = pointer to admin block
430 * Use: Times out a resolver.
433 static void a_timer(struct timeval
*tv
, void *v
)
438 T( trace(T_ADMIN
, "admin: %u resolver timeout", a
->seq
); )
440 a_write(a
, "FAIL timeout resolving `%s'\n", a
->paddr
);
444 selbuf_enable(&a
->b
);
448 /* --- @acmd_add@ --- *
450 * Arguments: @admin *a@ = connection which requested the addition
451 * @unsigned ac@ = argument count
452 * @char *av[]@ = pointer to the argument list
456 * Use: Adds a new peer.
459 static void acmd_add(admin
*a
, unsigned ac
, char *av
[])
465 /* --- Make sure someone's not got there already --- */
468 a_write(a
, "FAIL peer `%s' already registered\n", av
[0]);
472 /* --- Fill in the easy bits of address --- */
475 a
->peer
.sin
.sin_family
= AF_INET
;
476 a
->sasz
= sizeof(a
->peer
.sin
);
477 pt
= strtoul(av
[2], &p
, 0);
479 struct servent
*s
= getservbyname(av
[2], "udp");
481 a_write(a
, "FAIL service `%s' not known\n", av
[2]);
484 pt
= ntohs(s
->s_port
);
486 if (pt
== 0 || pt
>= 65536) {
487 a_write(a
, "FAIL bad port number %lu\n", pt
);
490 a
->peer
.sin
.sin_port
= htons(pt
);
492 /* --- If the name is numeric, do it the easy way --- */
494 if (inet_aton(av
[1], &a
->peer
.sin
.sin_addr
)) {
495 if (!p_create(av
[0], &a
->peer
.sa
, a
->sasz
))
496 a_write(a
, "FAIL couldn't create peer\n");
502 /* --- Store everything for later and crank up the resolver --- *
504 * We disable the line buffer until the resolver completes (or times out).
505 * This prevents other commands on the same connection (though the rest of
506 * the system continues regardless), but makes life simpler for the client.
509 a
->pname
= xstrdup(av
[0]);
510 a
->paddr
= xstrdup(av
[1]);
511 selbuf_disable(&a
->b
);
512 gettimeofday(&tv
, 0);
513 tv
.tv_sec
+= T_RESOLVE
;
514 sel_addtimer(&sel
, &a
->t
, &tv
, a_timer
, a
);
515 bres_byname(&a
->r
, a
->paddr
, a_resolve
, a
);
516 T( trace(T_ADMIN
, "admin: %u resolving hostname `%s'",
520 /*----- Administration commands -------------------------------------------*/
522 /* --- Miscellaneous commands --- */
526 static void acmd_trace(admin
*a
, unsigned ac
, char *av
[])
528 if (!ac
|| strcmp(av
[0], "?") == 0) {
530 a_write(a
, "INFO Trace options:\n");
531 for (t
= tr_opts
; t
->ch
; t
++) {
532 a_write(a
, "INFO %c %c %s\n",
533 t
->ch
, (tr_flags
& t
->f
) == t
->f ?
'*' : ' ', t
->help
);
537 unsigned f
= tr_flags
;
543 case '+': sense
= 1; break;
544 case '-': sense
= 0; break;
546 for (tt
= tr_opts
; tt
->ch
; tt
++) {
548 if (sense
) f
|= tt
->f
;
553 a_write(a
, "FAIL unknown trace option `%c'\n", *p
);
561 trace_level(tr_flags
);
568 static void acmd_port(admin
*a
, unsigned ac
, char *av
[])
570 a_write(a
, "INFO %u\nOK\n", p_port());
573 static void acmd_daemon(admin
*a
, unsigned ac
, char *av
[])
575 if (flags
& F_DAEMON
)
576 a_write(a
, "FAIL already running as a daemon\n");
578 a_warn("becoming a daemon");
582 a_write(a
, "FAIL error becoming a daemon: %s", strerror(errno
));
590 static void acmd_list(admin
*a
, unsigned ac
, char *av
[])
593 for (p
= p_first(); p
; p
= p_next(p
))
594 a_write(a
, "INFO %s\n", p_name(p
));
598 static void acmd_ifname(admin
*a
, unsigned ac
, char *av
[])
602 if ((p
= p_find(av
[0])) == 0)
603 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
605 a_write(a
, "INFO %s\nOK\n", p_ifname(p
));
608 static void acmd_addr(admin
*a
, unsigned ac
, char *av
[])
613 if ((p
= p_find(av
[0])) == 0)
614 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
617 assert(ad
->sa
.sa_family
== AF_INET
);
618 a_write(a
, "INFO %s %u\nOK\n",
619 inet_ntoa(ad
->sin
.sin_addr
),
620 (unsigned)ntohs(ad
->sin
.sin_port
));
624 static void acmd_stats(admin
*a
, unsigned ac
, char *av
[])
629 if ((p
= p_find(av
[0])) == 0)
630 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
633 a_write(a
, "INFO start-time=%s\n", timestr(st
->t_start
));
634 a_write(a
, "INFO last-packet-time=%s\n", timestr(st
->t_last
));
635 a_write(a
, "INFO packets-in=%lu bytes-in=%lu\n", st
->n_in
, st
->sz_in
);
636 a_write(a
, "INFO packets-out=%lu bytes-out=%lu\n",
637 st
->n_out
, st
->sz_out
);
638 a_write(a
, "INFO keyexch-packets-in=%lu keyexch-bytes-in=%lu\n",
639 st
->n_kxin
, st
->sz_kxin
);
640 a_write(a
, "INFO keyexch-packets-out=%lu keyexch-bytes-out=%lu\n",
641 st
->n_kxout
, st
->sz_kxout
);
642 a_write(a
, "INFO ip-packets-in=%lu ip-bytes-in=%lu\n",
643 st
->n_ipin
, st
->sz_ipin
);
644 a_write(a
, "INFO ip-packets-out=%lu ip-bytes-out=%lu\n",
645 st
->n_ipout
, st
->sz_ipout
);
646 a_write(a
, "INFO rejected-packets=%lu\n", st
->n_reject
);
651 static void acmd_kill(admin
*a
, unsigned ac
, char *av
[])
654 if ((p
= p_find(av
[0])) == 0)
655 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
662 static void acmd_quit(admin
*a
, unsigned ac
, char *av
[])
664 a_warn("closing down on admin request");
669 /* --- The command table and help --- */
671 typedef struct acmd
{
674 unsigned argmin
, argmax
;
675 void (*func
)(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
678 static void acmd_help(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
680 static const acmd acmdtab
[] = {
681 { "help", "HELP", 0, 0, acmd_help
},
683 { "trace", "TRACE [options]", 0, 1, acmd_trace
},
685 { "port", "PORT", 0, 0, acmd_port
},
686 { "daemon", "DAEMON", 0, 0, acmd_daemon
},
687 { "list", "LIST", 0, 0, acmd_list
},
688 { "ifname", "IFNAME peer", 1, 1, acmd_ifname
},
689 { "addr", "ADDR peer", 1, 1, acmd_addr
},
690 { "stats", "STATS peer", 1, 1, acmd_stats
},
691 { "kill", "KILL peer", 1, 1, acmd_kill
},
692 { "add", "ADD peer addr port", 3, 3, acmd_add
},
693 { "quit", "QUIT", 0, 0, acmd_quit
},
697 static void acmd_help(admin
*a
, unsigned ac
, char *av
[])
700 for (c
= acmdtab
; c
->name
; c
++)
701 a_write(a
, "INFO %s\n", c
->help
);
705 /*----- Connection handling -----------------------------------------------*/
707 /* --- @a_lock@ --- *
709 * Arguments: @admin *a@ = pointer to an admin block
713 * Use: Locks an admin block so that it won't be destroyed
717 static void a_lock(admin
*a
) { assert(!(a
->f
& AF_LOCK
)); a
->f
|= AF_LOCK
; }
719 /* --- @a_unlock@ --- *
721 * Arguments: @admin *a@ = pointer to an admin block
725 * Use: Unlocks an admin block, allowing its destruction. This is
726 * also the second half of @a_destroy@.
729 static void a_unlock(admin
*a
)
731 assert(a
->f
& AF_LOCK
);
732 if (!(a
->f
& AF_DEAD
)) {
737 T( trace(T_ADMIN
, "admin: completing destruction of connection %u",
740 selbuf_destroy(&a
->b
);
747 if (a
->b
.reader
.fd
!= a
->w
.fd
)
748 close(a
->b
.reader
.fd
);
754 a
->next
->prev
= a
->prev
;
756 a
->prev
->next
= a
->next
;
762 /* --- @a_destroy@ --- *
764 * Arguments: @admin *a@ = pointer to an admin block
768 * Use: Destroys an admin block. This requires a certain amount of
772 static void a_destroy(admin
*a
)
774 /* --- Don't multiply destroy admin blocks --- */
779 /* --- Make sure nobody expects it to work --- */
782 T( trace(T_ADMIN
, "admin: destroying connection %u", a
->seq
); )
784 /* --- Free the output buffers --- */
789 for (o
= a
->o_head
; o
; o
= oo
) {
796 /* --- If the block is locked, that's all we can manage --- */
798 if (a
->f
& AF_LOCK
) {
799 T( trace(T_ADMIN
, "admin: deferring destruction..."); )
806 /* --- @a_line@ --- *
808 * Arguments: @char *p@ = pointer to the line read
809 * @size_t len@ = length of the line
810 * @void *vp@ = pointer to my admin block
814 * Use: Handles a line of input.
817 static void a_line(char *p
, size_t len
, void *vp
)
831 ac
= str_qsplit(p
, av
, 4, 0, STRF_QUOTE
);
834 for (p
= av
[0]; *p
; p
++) *p
= tolower((unsigned char)*p
);
835 for (c
= acmdtab
; c
->name
; c
++) {
836 if (strcmp(av
[0], c
->name
) == 0) {
838 if (c
->argmin
> ac
|| ac
> c
->argmax
)
839 a_write(a
, "FAIL syntax: %s\n", c
->help
);
842 c
->func(a
, ac
, av
+ 1);
848 a_write(a
, "FAIL unknown command `%s'\n", av
[0]);
851 /* --- @a_create@ --- *
853 * Arguments: @int fd_in, fd_out@ = file descriptors to use
857 * Use: Creates a new admin connection.
860 void a_create(int fd_in
, int fd_out
)
862 admin
*a
= CREATE(admin
);
863 T( static unsigned seq
= 0;
865 T( trace(T_ADMIN
, "admin: accepted connection %u", a
->seq
); )
868 if (fd_in
== STDIN_FILENO
)
870 fdflags(fd_in
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
872 fdflags(fd_out
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
873 selbuf_init(&a
->b
, &sel
, fd_in
, a_line
, a
);
874 sel_initfile(&sel
, &a
->w
, fd_out
, SEL_WRITE
, a_flush
, a
);
884 /* --- @a_accept@ --- *
886 * Arguments: @int fd@ = file descriptor to accept
887 * @unsigned mode@ = what to do
888 * @void *v@ = uninteresting pointer
892 * Use: Accepts a new admin connection.
895 static void a_accept(int fd
, unsigned mode
, void *v
)
898 struct sockaddr_un sun
;
899 size_t sz
= sizeof(sun
);
901 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sz
)) < 0) {
902 if (errno
!= EINTR
&& errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
903 errno
!= ECONNABORTED
&& errno
!= EPROTO
)
904 a_warn("accept admin connection failed: %s", strerror(errno
));
910 /* --- @a_daemon@ --- *
916 * Use: Informs the admin module that it's a daemon.
924 /* --- @a_init@ --- *
926 * Arguments: @const char *name@ = socket name to create
930 * Use: Creates the admin listening socket.
933 void a_init(const char *name
)
937 struct sockaddr_un sun
;
941 /* --- Set up the socket address --- */
943 sz
= strlen(name
) + 1;
944 if (sz
> sizeof(sun
.sun_path
))
945 die(EXIT_FAILURE
, "socket name `%s' too long", name
);
947 sun
.sun_family
= AF_UNIX
;
948 memcpy(sun
.sun_path
, name
, sz
);
949 sz
+= offsetof(struct sockaddr_un
, sun_path
);
951 /* --- Attempt to bind to the socket --- */
955 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
956 die(EXIT_FAILURE
, "couldn't create socket: %s", strerror(errno
));
957 if (bind(fd
, (struct sockaddr
*)&sun
, sz
) < 0) {
960 if (errno
!= EADDRINUSE
) {
961 die(EXIT_FAILURE
, "couldn't bind to address `%s': %s",
962 sun
.sun_path
, strerror(e
));
965 die(EXIT_FAILURE
, "too many retries; giving up");
967 if (!connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
968 die(EXIT_FAILURE
, "server already listening on admin socket `%s'",
971 if (errno
!= ECONNREFUSED
)
972 die(EXIT_FAILURE
, "couldn't bind to address: %s", strerror(e
));
973 if (stat(sun
.sun_path
, &st
)) {
974 die(EXIT_FAILURE
, "couldn't stat `%s': %s",
975 sun
.sun_path
, strerror(errno
));
977 if (!S_ISSOCK(st
.st_mode
))
978 die(EXIT_FAILURE
, "object `%s' isn't a socket", sun
.sun_path
);
979 T( trace(T_ADMIN
, "admin: stale socket found; removing it"); )
980 unlink(sun
.sun_path
);
984 chmod(sun
.sun_path
, 0600);
985 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
987 die(EXIT_FAILURE
, "couldn't listen on socket: %s", strerror(errno
));
989 /* --- Listen to the socket --- */
991 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, a_accept
, 0);
995 T( trace_custom(a_trace
, 0);
996 trace(T_ADMIN
, "admin: enabled custom tracing"); )
999 /* --- Set up signal handlers --- */
1001 sig_add(&s_term
, SIGTERM
, a_sigdie
, 0);
1002 sig_add(&s_hup
, SIGHUP
, a_sighup
, 0);
1003 sigaction(SIGINT
, 0, &sa
);
1004 if (sa
.sa_handler
!= SIG_IGN
)
1005 sig_add(&s_int
, SIGINT
, a_sigdie
, 0);
1008 /*----- That's all, folks -------------------------------------------------*/