3 * $Id: admin.c,v 1.6 2001/02/19 19:11:09 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.6 2001/02/19 19:11:09 mdw
33 * Output buffering on admin connections.
35 * Revision 1.5 2001/02/16 21:22:51 mdw
36 * Support for displaying statistics. Make client connections blocking, so
37 * that things don't get dropped. (This might change again if I add
40 * Revision 1.4 2001/02/06 09:34:53 mdw
41 * Change ERR response to FAIL for consistency with other programs.
43 * Revision 1.3 2001/02/04 01:17:12 mdw
44 * The `DAEMON' notification to stdout is replaced by a warning. The
45 * `DAEMON' and `QUIT' command send `OK' on successful completion. Put
46 * assignment of sequence number in a T(...) guard.
48 * Revision 1.2 2001/02/03 22:40:29 mdw
49 * Put timer information into the entropy pool when packets are received
50 * and on similar events. Reseed the generator on the interval timer.
52 * Revision 1.1 2001/02/03 20:26:37 mdw
57 /*----- Header files ------------------------------------------------------*/
61 /*----- Global variables --------------------------------------------------*/
65 const trace_opt tr_opts
[] = {
66 { 't', T_TUNNEL
, "tunnel events" },
67 { 'r', T_PEER
, "peer events" },
68 { 'a', T_ADMIN
, "admin interface" },
69 { 'p', T_PACKET
, "packet contents" },
70 { 'c', T_CRYPTO
, "crypto details" },
71 { 's', T_KEYSET
, "symmetric keyset management" },
72 { 'x', T_KEYEXCH
, "key exchange" },
73 { 'm', T_KEYMGMT
, "key management" },
74 { 'A', T_ALL
, "all of the above" },
78 unsigned tr_flags
= 0;
81 /*----- Static variables --------------------------------------------------*/
85 static const char *sockname
;
86 static unsigned flags
= 0;
87 static admin
*a_stdin
= 0;
88 static sig s_term
, s_int
, s_hup
;
93 #define T_RESOLVE SEC(30)
95 static void a_destroy(admin */
*a*/
);
96 static void a_lock(admin */
*a*/
);
97 static void a_unlock(admin */
*a*/
);
99 /*----- Output functions --------------------------------------------------*/
101 /* --- @trywrite@ --- *
103 * Arguments: @admin *a@ = pointer to an admin block
104 * @const char *p@ = pointer to buffer to write
105 * @size_t sz@ = size of data to write
107 * Returns: The number of bytes written, or less than zero on error.
109 * Use: Attempts to write data to a client.
112 static ssize_t
trywrite(admin
*a
, const char *p
, size_t sz
)
119 n
= write(a
->w
.fd
, p
, sz
);
129 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
131 a_warn("disconnecting admin client due to write errors: %s",
139 /* --- @dosend@ --- *
141 * Arguemnts: @admin *a@ = pointer to an admin block
142 * @const char *p@ = pointer to buffer to write
143 * @size_t sz@ = size of data to write
147 * Use: Sends data to an admin client.
150 static void dosend(admin
*a
, const char *p
, size_t sz
)
158 /* --- Try to send the data immediately --- */
161 if ((n
= trywrite(a
, p
, sz
)) < 0)
169 /* --- Fill buffers with the data until it's all gone --- */
174 else if (o
->p_in
< o
->buf
+ OBUFSZ
)
178 o
= xmalloc(sizeof(obuf
));
180 o
->p_in
= o
->p_out
= o
->buf
;
188 n
= o
->buf
+ OBUFSZ
- o
->p_in
;
191 memcpy(o
->p_in
, p
, n
);
198 /* --- @a_flush@ --- *
200 * Arguments: @int fd@ = file descriptor
201 * @unsigned mode@ = what's happening
202 * @void *v@ = pointer to my admin block
206 * Use: Flushes buffers when a client is ready to read again.
209 static void a_flush(int fd
, unsigned mode
, void *v
)
217 if ((n
= trywrite(a
, o
->p_out
, o
->p_in
- o
->p_out
)) < 0)
220 if (o
->p_in
< o
->p_out
)
233 /*----- Utility functions -------------------------------------------------*/
235 /* --- @a_write@ --- *
237 * Arguments: @admin *a@ = admin connection to write to
238 * @const char *fmt@ = pointer to format string
239 * @...@ = other arguments
243 * Use: Sends a message to an admin connection.
246 static void a_write(admin
*a
, const char *fmt
, ...)
251 dstr_vputf(&d
, fmt
, ap
);
253 dosend(a
, d
.buf
, d
.len
);
257 /* --- @a_warn@ --- *
259 * Arguments: @const char *fmt@ = pointer to format string
260 * @...@ = other arguments
264 * Use: Informs all admin connections of a warning.
267 void a_warn(const char *fmt
, ...)
274 dstr_puts(&d
, "WARN ");
276 dstr_vputf(&d
, fmt
, ap
);
278 if (!(flags
& F_INIT
))
282 for (a
= admins
; a
; a
= aa
) {
284 dosend(a
, d
.buf
, d
.len
);
290 /* --- @a_trace@ --- *
292 * Arguments: @const char *p@ = pointer to a buffer
293 * @size_t sz@ = size of the buffer
294 * @void *v@ = uninteresting pointer
298 * Use: Custom trace output handler.
302 static void a_trace(const char *p
, size_t sz
, void *v
)
307 dstr_puts(&d
, "TRACE ");
308 dstr_putm(&d
, p
, sz
);
310 for (a
= admins
; a
; a
= aa
) {
312 dosend(a
, d
.buf
, d
.len
);
318 /* --- @a_quit@ --- *
324 * Use: Shuts things down nicely.
334 /* --- @a_sigdie@ --- *
336 * Arguments: @int sig@ = signal number
337 * @void *v@ = an uninteresting argument
341 * Use Shuts down on receipt of a fatal signal.
344 static void a_sigdie(int sig
, void *v
)
350 case SIGTERM
: p
= "SIGTERM"; break;
351 case SIGINT
: p
= "SIGINT"; break;
353 sprintf(buf
, "signal %i", sig
);
357 a_warn("shutting down on %s", p
);
361 /* --- @a_sighup@ --- *
363 * Arguments: @int sig@ = signal number
364 * @void *v@ = an uninteresting argument
368 * Use Logs a message about SIGHUP not being useful.
371 static void a_sighup(int sig
, void *v
)
373 a_warn("received SIGHUP: ignoring");
376 /*----- Adding peers ------------------------------------------------------*/
378 /* --- @a_resolve@ --- *
380 * Arguments: @struct hostent *h@ = pointer to resolved hostname
381 * @void *v@ = pointer to admin block
385 * Use: Handles a completed name resolution.
388 static void a_resolve(struct hostent
*h
, void *v
)
393 T( trace(T_ADMIN
, "admin: %u resolved", a
->seq
); )
397 a_write(a
, "FAIL couldn't resolve hostname `%s'\n", a
->paddr
);
398 else if (p_find(a
->pname
))
399 a_write(a
, "FAIL peer `%s' already registered\n", a
->pname
);
401 memcpy(&a
->peer
.sin
.sin_addr
, h
->h_addr
, sizeof(struct in_addr
));
402 if (!p_create(a
->pname
, &a
->peer
.sa
, a
->sasz
))
403 a_write(a
, "FAIL couldn't create peer\n");
410 selbuf_enable(&a
->b
);
414 /* --- @a_timer@ --- *
416 * Arguments: @struct timeval *tv@ = timer
417 * @void *v@ = pointer to admin block
421 * Use: Times out a resolver.
424 static void a_timer(struct timeval
*tv
, void *v
)
429 T( trace(T_ADMIN
, "admin: %u resolver timeout", a
->seq
); )
431 a_write(a
, "FAIL timeout resolving `%s'\n", a
->paddr
);
435 selbuf_enable(&a
->b
);
439 /* --- @acmd_add@ --- *
441 * Arguments: @admin *a@ = connection which requested the addition
442 * @unsigned ac@ = argument count
443 * @char *av[]@ = pointer to the argument list
447 * Use: Adds a new peer.
450 static void acmd_add(admin
*a
, unsigned ac
, char *av
[])
456 /* --- Make sure someone's not got there already --- */
459 a_write(a
, "FAIL peer `%s' already registered\n", av
[0]);
463 /* --- Fill in the easy bits of address --- */
466 a
->peer
.sin
.sin_family
= AF_INET
;
467 a
->sasz
= sizeof(a
->peer
.sin
);
468 pt
= strtoul(av
[2], &p
, 0);
470 struct servent
*s
= getservbyname(av
[2], "udp");
472 a_write(a
, "FAIL service `%s' not known\n", av
[2]);
475 pt
= ntohs(s
->s_port
);
477 if (pt
== 0 || pt
>= 65536) {
478 a_write(a
, "FAIL bad port number %lu\n", pt
);
481 a
->peer
.sin
.sin_port
= htons(pt
);
483 /* --- If the name is numeric, do it the easy way --- */
485 if (inet_aton(av
[1], &a
->peer
.sin
.sin_addr
)) {
486 if (!p_create(av
[0], &a
->peer
.sa
, a
->sasz
))
487 a_write(a
, "FAIL couldn't create peer\n");
493 /* --- Store everything for later and crank up the resolver --- *
495 * We disable the line buffer until the resolver completes (or times out).
496 * This prevents other commands on the same connection (though the rest of
497 * the system continues regardless), but makes life simpler for the client.
500 a
->pname
= xstrdup(av
[0]);
501 a
->paddr
= xstrdup(av
[1]);
502 selbuf_disable(&a
->b
);
503 gettimeofday(&tv
, 0);
504 tv
.tv_sec
+= T_RESOLVE
;
505 sel_addtimer(&sel
, &a
->t
, &tv
, a_timer
, a
);
506 bres_byname(&a
->r
, a
->paddr
, a_resolve
, a
);
507 T( trace(T_ADMIN
, "admin: %u resolving hostname `%s'",
511 /*----- Administration commands -------------------------------------------*/
513 /* --- Miscellaneous commands --- */
517 static void acmd_trace(admin
*a
, unsigned ac
, char *av
[])
519 if (!ac
|| strcmp(av
[0], "?") == 0) {
521 a_write(a
, "INFO Trace options:\n");
522 for (t
= tr_opts
; t
->ch
; t
++) {
523 a_write(a
, "INFO %c %c %s\n",
524 t
->ch
, (tr_flags
& t
->f
) == t
->f ?
'*' : ' ', t
->help
);
528 unsigned f
= tr_flags
;
534 case '+': sense
= 1; break;
535 case '-': sense
= 0; break;
537 for (tt
= tr_opts
; tt
->ch
; tt
++) {
539 if (sense
) f
|= tt
->f
;
544 a_write(a
, "FAIL unknown trace option `%c'\n", *p
);
552 trace_level(tr_flags
);
559 static void acmd_port(admin
*a
, unsigned ac
, char *av
[])
561 a_write(a
, "INFO %u\nOK\n", p_port());
564 static void acmd_daemon(admin
*a
, unsigned ac
, char *av
[])
566 if (flags
& F_DAEMON
)
567 a_write(a
, "FAIL already running as a daemon\n");
569 a_warn("becoming a daemon");
573 a_write(a
, "FAIL error becoming a daemon: %s", strerror(errno
));
581 static void acmd_list(admin
*a
, unsigned ac
, char *av
[])
584 for (p
= p_first(); p
; p
= p_next(p
))
585 a_write(a
, "INFO %s\n", p_name(p
));
589 static void acmd_ifname(admin
*a
, unsigned ac
, char *av
[])
593 if ((p
= p_find(av
[0])) == 0)
594 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
596 a_write(a
, "INFO %s\nOK\n", p_ifname(p
));
599 static void acmd_addr(admin
*a
, unsigned ac
, char *av
[])
604 if ((p
= p_find(av
[0])) == 0)
605 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
608 assert(ad
->sa
.sa_family
== AF_INET
);
609 a_write(a
, "INFO %s %u\nOK\n",
610 inet_ntoa(ad
->sin
.sin_addr
),
611 (unsigned)ntohs(ad
->sin
.sin_port
));
615 static void acmd_stats(admin
*a
, unsigned ac
, char *av
[])
620 if ((p
= p_find(av
[0])) == 0)
621 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
624 a_write(a
, "INFO start-time=%s\n", timestr(st
->t_start
));
625 a_write(a
, "INFO last-packet-time=%s\n", timestr(st
->t_last
));
626 a_write(a
, "INFO packets-in=%lu bytes-in=%lu\n", st
->n_in
, st
->sz_in
);
627 a_write(a
, "INFO packets-out=%lu bytes-out=%lu\n",
628 st
->n_out
, st
->sz_out
);
629 a_write(a
, "INFO keyexch-packets-in=%lu keyexch-bytes-in=%lu\n",
630 st
->n_kxin
, st
->sz_kxin
);
631 a_write(a
, "INFO keyexch-packets-out=%lu keyexch-bytes-out=%lu\n",
632 st
->n_kxout
, st
->sz_kxout
);
633 a_write(a
, "INFO ip-packets-in=%lu ip-bytes-in=%lu\n",
634 st
->n_ipin
, st
->sz_ipin
);
635 a_write(a
, "INFO ip-packets-out=%lu ip-bytes-out=%lu\n",
636 st
->n_ipout
, st
->sz_ipout
);
637 a_write(a
, "INFO rejected-packets=%lu\n", st
->n_reject
);
642 static void acmd_kill(admin
*a
, unsigned ac
, char *av
[])
645 if ((p
= p_find(av
[0])) == 0)
646 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
653 static void acmd_quit(admin
*a
, unsigned ac
, char *av
[])
655 a_warn("closing down on admin request");
660 /* --- The command table and help --- */
662 typedef struct acmd
{
665 unsigned argmin
, argmax
;
666 void (*func
)(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
669 static void acmd_help(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
671 static const acmd acmdtab
[] = {
672 { "help", "HELP", 0, 0, acmd_help
},
674 { "trace", "TRACE [options]", 0, 1, acmd_trace
},
676 { "port", "PORT", 0, 0, acmd_port
},
677 { "daemon", "DAEMON", 0, 0, acmd_daemon
},
678 { "list", "LIST", 0, 0, acmd_list
},
679 { "ifname", "IFNAME peer", 1, 1, acmd_ifname
},
680 { "addr", "ADDR peer", 1, 1, acmd_addr
},
681 { "stats", "STATS peer", 1, 1, acmd_stats
},
682 { "kill", "KILL peer", 1, 1, acmd_kill
},
683 { "add", "ADD peer addr port", 3, 3, acmd_add
},
684 { "quit", "QUIT", 0, 0, acmd_quit
},
688 static void acmd_help(admin
*a
, unsigned ac
, char *av
[])
691 for (c
= acmdtab
; c
->name
; c
++)
692 a_write(a
, "INFO %s\n", c
->help
);
696 /*----- Connection handling -----------------------------------------------*/
698 /* --- @a_lock@ --- *
700 * Arguments: @admin *a@ = pointer to an admin block
704 * Use: Locks an admin block so that it won't be destroyed
708 static void a_lock(admin
*a
) { assert(!(a
->f
& AF_LOCK
)); a
->f
|= AF_LOCK
; }
710 /* --- @a_unlock@ --- *
712 * Arguments: @admin *a@ = pointer to an admin block
716 * Use: Unlocks an admin block, allowing its destruction. This is
717 * also the second half of @a_destroy@.
720 static void a_unlock(admin
*a
)
722 assert(a
->f
& AF_LOCK
);
723 if (!(a
->f
& AF_DEAD
)) {
728 T( trace(T_ADMIN
, "admin: completing destruction of connection %u",
731 selbuf_destroy(&a
->b
);
738 if (a
->b
.reader
.fd
!= a
->w
.fd
)
739 close(a
->b
.reader
.fd
);
745 a
->next
->prev
= a
->prev
;
747 a
->prev
->next
= a
->next
;
753 /* --- @a_destroy@ --- *
755 * Arguments: @admin *a@ = pointer to an admin block
759 * Use: Destroys an admin block. This requires a certain amount of
763 static void a_destroy(admin
*a
)
765 /* --- Don't multiply destroy admin blocks --- */
770 /* --- Make sure nobody expects it to work --- */
773 T( trace(T_ADMIN
, "admin: destroying connection %u", a
->seq
); )
775 /* --- Free the output buffers --- */
780 for (o
= a
->o_head
; o
; o
= oo
) {
787 /* --- If the block is locked, that's all we can manage --- */
789 if (a
->f
& AF_LOCK
) {
790 T( trace(T_ADMIN
, "admin: deferring destruction..."); )
797 /* --- @a_line@ --- *
799 * Arguments: @char *p@ = pointer to the line read
800 * @void *vp@ = pointer to my admin block
804 * Use: Handles a line of input.
807 static void a_line(char *p
, void *vp
)
821 ac
= str_qsplit(p
, av
, 4, 0, STRF_QUOTE
);
824 for (p
= av
[0]; *p
; p
++) *p
= tolower((unsigned char)*p
);
825 for (c
= acmdtab
; c
->name
; c
++) {
826 if (strcmp(av
[0], c
->name
) == 0) {
828 if (c
->argmin
> ac
|| ac
> c
->argmax
)
829 a_write(a
, "FAIL syntax: %s\n", c
->help
);
832 c
->func(a
, ac
, av
+ 1);
838 a_write(a
, "FAIL unknown command `%s'\n", av
[0]);
841 /* --- @a_create@ --- *
843 * Arguments: @int fd_in, fd_out@ = file descriptors to use
847 * Use: Creates a new admin connection.
850 void a_create(int fd_in
, int fd_out
)
852 admin
*a
= CREATE(admin
);
853 T( static unsigned seq
= 0;
855 T( trace(T_ADMIN
, "admin: accepted connection %u", a
->seq
); )
858 if (fd_in
== STDIN_FILENO
)
860 fdflags(fd_in
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
862 fdflags(fd_out
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
863 selbuf_init(&a
->b
, &sel
, fd_in
, a_line
, a
);
864 sel_initfile(&sel
, &a
->w
, fd_out
, SEL_WRITE
, a_flush
, a
);
874 /* --- @a_accept@ --- *
876 * Arguments: @int fd@ = file descriptor to accept
877 * @unsigned mode@ = what to do
878 * @void *v@ = uninteresting pointer
882 * Use: Accepts a new admin connection.
885 static void a_accept(int fd
, unsigned mode
, void *v
)
888 struct sockaddr_un sun
;
889 size_t sz
= sizeof(sun
);
891 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sz
)) < 0) {
892 a_warn("accept admin connection failed: %s", strerror(errno
));
898 /* --- @a_daemon@ --- *
904 * Use: Informs the admin module that it's a daemon.
912 /* --- @a_init@ --- *
914 * Arguments: @const char *name@ = socket name to create
918 * Use: Creates the admin listening socket.
921 void a_init(const char *name
)
925 struct sockaddr_un sun
;
929 /* --- Set up the socket address --- */
931 sz
= strlen(name
) + 1;
932 if (sz
> sizeof(sun
.sun_path
))
933 die(EXIT_FAILURE
, "socket name `%s' too long", name
);
935 sun
.sun_family
= AF_UNIX
;
936 memcpy(sun
.sun_path
, name
, sz
);
937 sz
+= offsetof(struct sockaddr_un
, sun_path
);
939 /* --- Attempt to bind to the socket --- */
943 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
944 die(EXIT_FAILURE
, "couldn't create socket: %s", strerror(errno
));
945 if (bind(fd
, (struct sockaddr
*)&sun
, sz
) < 0) {
948 if (errno
!= EADDRINUSE
) {
949 die(EXIT_FAILURE
, "couldn't bind to address `%s': %s",
950 sun
.sun_path
, strerror(e
));
953 die(EXIT_FAILURE
, "too many retries; giving up");
955 if (!connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
956 die(EXIT_FAILURE
, "server already listening on admin socket `%s'",
959 if (errno
!= ECONNREFUSED
)
960 die(EXIT_FAILURE
, "couldn't bind to address: %s", strerror(e
));
961 if (stat(sun
.sun_path
, &st
)) {
962 die(EXIT_FAILURE
, "couldn't stat `%s': %s",
963 sun
.sun_path
, strerror(errno
));
965 if (!S_ISSOCK(st
.st_mode
))
966 die(EXIT_FAILURE
, "object `%s' isn't a socket", sun
.sun_path
);
967 T( trace(T_ADMIN
, "admin: stale socket found; removing it"); )
968 unlink(sun
.sun_path
);
972 chmod(sun
.sun_path
, 0600);
973 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
975 die(EXIT_FAILURE
, "couldn't listen on socket: %s", strerror(errno
));
977 /* --- Listen to the socket --- */
979 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, a_accept
, 0);
983 T( trace_custom(a_trace
, 0);
984 trace(T_ADMIN
, "admin: enabled custom tracing"); )
987 /* --- Set up signal handlers --- */
989 sig_add(&s_term
, SIGTERM
, a_sigdie
, 0);
990 sig_add(&s_hup
, SIGHUP
, a_sighup
, 0);
991 sigaction(SIGINT
, 0, &sa
);
992 if (sa
.sa_handler
!= SIG_IGN
)
993 sig_add(&s_int
, SIGINT
, a_sigdie
, 0);
996 /*----- That's all, folks -------------------------------------------------*/