3 * $Id: admin.c,v 1.8 2003/04/06 10:25:17 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.8 2003/04/06 10:25:17 mdw
33 * Support Linux TUN/TAP device. Fix some bugs.
35 * Revision 1.7 2002/01/13 14:57:33 mdw
36 * Track @lbuf@ and @dstr_vputf@ changes in mLib.
38 * Revision 1.6 2001/02/19 19:11:09 mdw
39 * Output buffering on admin connections.
41 * Revision 1.5 2001/02/16 21:22:51 mdw
42 * Support for displaying statistics. Make client connections blocking, so
43 * that things don't get dropped. (This might change again if I add
46 * Revision 1.4 2001/02/06 09:34:53 mdw
47 * Change ERR response to FAIL for consistency with other programs.
49 * Revision 1.3 2001/02/04 01:17:12 mdw
50 * The `DAEMON' notification to stdout is replaced by a warning. The
51 * `DAEMON' and `QUIT' command send `OK' on successful completion. Put
52 * assignment of sequence number in a T(...) guard.
54 * Revision 1.2 2001/02/03 22:40:29 mdw
55 * Put timer information into the entropy pool when packets are received
56 * and on similar events. Reseed the generator on the interval timer.
58 * Revision 1.1 2001/02/03 20:26:37 mdw
63 /*----- Header files ------------------------------------------------------*/
67 /*----- Global variables --------------------------------------------------*/
71 const trace_opt tr_opts
[] = {
72 { 't', T_TUNNEL
, "tunnel events" },
73 { 'r', T_PEER
, "peer events" },
74 { 'a', T_ADMIN
, "admin interface" },
75 { 'p', T_PACKET
, "packet contents" },
76 { 'c', T_CRYPTO
, "crypto details" },
77 { 's', T_KEYSET
, "symmetric keyset management" },
78 { 'x', T_KEYEXCH
, "key exchange" },
79 { 'm', T_KEYMGMT
, "key management" },
80 { 'A', T_ALL
, "all of the above" },
84 unsigned tr_flags
= 0;
87 /*----- Static variables --------------------------------------------------*/
91 static const char *sockname
;
92 static unsigned flags
= 0;
93 static admin
*a_stdin
= 0;
94 static sig s_term
, s_int
, s_hup
;
99 #define T_RESOLVE SEC(30)
101 static void a_destroy(admin */
*a*/
);
102 static void a_lock(admin */
*a*/
);
103 static void a_unlock(admin */
*a*/
);
105 /*----- Output functions --------------------------------------------------*/
107 /* --- @trywrite@ --- *
109 * Arguments: @admin *a@ = pointer to an admin block
110 * @const char *p@ = pointer to buffer to write
111 * @size_t sz@ = size of data to write
113 * Returns: The number of bytes written, or less than zero on error.
115 * Use: Attempts to write data to a client.
118 static ssize_t
trywrite(admin
*a
, const char *p
, size_t sz
)
125 n
= write(a
->w
.fd
, p
, sz
);
135 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
137 a_warn("disconnecting admin client due to write errors: %s",
145 /* --- @dosend@ --- *
147 * Arguemnts: @admin *a@ = pointer to an admin block
148 * @const char *p@ = pointer to buffer to write
149 * @size_t sz@ = size of data to write
153 * Use: Sends data to an admin client.
156 static void dosend(admin
*a
, const char *p
, size_t sz
)
164 /* --- Try to send the data immediately --- */
167 if ((n
= trywrite(a
, p
, sz
)) < 0)
175 /* --- Fill buffers with the data until it's all gone --- */
180 else if (o
->p_in
< o
->buf
+ OBUFSZ
)
184 o
= xmalloc(sizeof(obuf
));
186 o
->p_in
= o
->p_out
= o
->buf
;
194 n
= o
->buf
+ OBUFSZ
- o
->p_in
;
197 memcpy(o
->p_in
, p
, n
);
204 /* --- @a_flush@ --- *
206 * Arguments: @int fd@ = file descriptor
207 * @unsigned mode@ = what's happening
208 * @void *v@ = pointer to my admin block
212 * Use: Flushes buffers when a client is ready to read again.
215 static void a_flush(int fd
, unsigned mode
, void *v
)
223 if ((n
= trywrite(a
, o
->p_out
, o
->p_in
- o
->p_out
)) < 0)
226 if (o
->p_in
< o
->p_out
)
239 /*----- Utility functions -------------------------------------------------*/
241 /* --- @a_write@ --- *
243 * Arguments: @admin *a@ = admin connection to write to
244 * @const char *fmt@ = pointer to format string
245 * @...@ = other arguments
249 * Use: Sends a message to an admin connection.
252 static void a_write(admin
*a
, const char *fmt
, ...)
257 dstr_vputf(&d
, fmt
, &ap
);
259 dosend(a
, d
.buf
, d
.len
);
263 /* --- @a_warn@ --- *
265 * Arguments: @const char *fmt@ = pointer to format string
266 * @...@ = other arguments
270 * Use: Informs all admin connections of a warning.
273 void a_warn(const char *fmt
, ...)
280 dstr_puts(&d
, "WARN ");
282 dstr_vputf(&d
, fmt
, &ap
);
284 if (!(flags
& F_INIT
))
288 for (a
= admins
; a
; a
= aa
) {
290 dosend(a
, d
.buf
, d
.len
);
296 /* --- @a_trace@ --- *
298 * Arguments: @const char *p@ = pointer to a buffer
299 * @size_t sz@ = size of the buffer
300 * @void *v@ = uninteresting pointer
304 * Use: Custom trace output handler.
308 static void a_trace(const char *p
, size_t sz
, void *v
)
313 dstr_puts(&d
, "TRACE ");
314 dstr_putm(&d
, p
, sz
);
316 for (a
= admins
; a
; a
= aa
) {
318 dosend(a
, d
.buf
, d
.len
);
324 /* --- @a_quit@ --- *
330 * Use: Shuts things down nicely.
340 /* --- @a_sigdie@ --- *
342 * Arguments: @int sig@ = signal number
343 * @void *v@ = an uninteresting argument
347 * Use Shuts down on receipt of a fatal signal.
350 static void a_sigdie(int sig
, void *v
)
356 case SIGTERM
: p
= "SIGTERM"; break;
357 case SIGINT
: p
= "SIGINT"; break;
359 sprintf(buf
, "signal %i", sig
);
363 a_warn("shutting down on %s", p
);
367 /* --- @a_sighup@ --- *
369 * Arguments: @int sig@ = signal number
370 * @void *v@ = an uninteresting argument
374 * Use Logs a message about SIGHUP not being useful.
377 static void a_sighup(int sig
, void *v
)
379 a_warn("received SIGHUP: ignoring");
382 /*----- Adding peers ------------------------------------------------------*/
384 /* --- @a_resolve@ --- *
386 * Arguments: @struct hostent *h@ = pointer to resolved hostname
387 * @void *v@ = pointer to admin block
391 * Use: Handles a completed name resolution.
394 static void a_resolve(struct hostent
*h
, void *v
)
399 T( trace(T_ADMIN
, "admin: %u resolved", a
->seq
); )
403 a_write(a
, "FAIL couldn't resolve hostname `%s'\n", a
->paddr
);
404 else if (p_find(a
->pname
))
405 a_write(a
, "FAIL peer `%s' already registered\n", a
->pname
);
407 memcpy(&a
->peer
.sin
.sin_addr
, h
->h_addr
, sizeof(struct in_addr
));
408 if (!p_create(a
->pname
, &a
->peer
.sa
, a
->sasz
))
409 a_write(a
, "FAIL couldn't create peer\n");
416 selbuf_enable(&a
->b
);
420 /* --- @a_timer@ --- *
422 * Arguments: @struct timeval *tv@ = timer
423 * @void *v@ = pointer to admin block
427 * Use: Times out a resolver.
430 static void a_timer(struct timeval
*tv
, void *v
)
435 T( trace(T_ADMIN
, "admin: %u resolver timeout", a
->seq
); )
437 a_write(a
, "FAIL timeout resolving `%s'\n", a
->paddr
);
441 selbuf_enable(&a
->b
);
445 /* --- @acmd_add@ --- *
447 * Arguments: @admin *a@ = connection which requested the addition
448 * @unsigned ac@ = argument count
449 * @char *av[]@ = pointer to the argument list
453 * Use: Adds a new peer.
456 static void acmd_add(admin
*a
, unsigned ac
, char *av
[])
462 /* --- Make sure someone's not got there already --- */
465 a_write(a
, "FAIL peer `%s' already registered\n", av
[0]);
469 /* --- Fill in the easy bits of address --- */
472 a
->peer
.sin
.sin_family
= AF_INET
;
473 a
->sasz
= sizeof(a
->peer
.sin
);
474 pt
= strtoul(av
[2], &p
, 0);
476 struct servent
*s
= getservbyname(av
[2], "udp");
478 a_write(a
, "FAIL service `%s' not known\n", av
[2]);
481 pt
= ntohs(s
->s_port
);
483 if (pt
== 0 || pt
>= 65536) {
484 a_write(a
, "FAIL bad port number %lu\n", pt
);
487 a
->peer
.sin
.sin_port
= htons(pt
);
489 /* --- If the name is numeric, do it the easy way --- */
491 if (inet_aton(av
[1], &a
->peer
.sin
.sin_addr
)) {
492 if (!p_create(av
[0], &a
->peer
.sa
, a
->sasz
))
493 a_write(a
, "FAIL couldn't create peer\n");
499 /* --- Store everything for later and crank up the resolver --- *
501 * We disable the line buffer until the resolver completes (or times out).
502 * This prevents other commands on the same connection (though the rest of
503 * the system continues regardless), but makes life simpler for the client.
506 a
->pname
= xstrdup(av
[0]);
507 a
->paddr
= xstrdup(av
[1]);
508 selbuf_disable(&a
->b
);
509 gettimeofday(&tv
, 0);
510 tv
.tv_sec
+= T_RESOLVE
;
511 sel_addtimer(&sel
, &a
->t
, &tv
, a_timer
, a
);
512 bres_byname(&a
->r
, a
->paddr
, a_resolve
, a
);
513 T( trace(T_ADMIN
, "admin: %u resolving hostname `%s'",
517 /*----- Administration commands -------------------------------------------*/
519 /* --- Miscellaneous commands --- */
523 static void acmd_trace(admin
*a
, unsigned ac
, char *av
[])
525 if (!ac
|| strcmp(av
[0], "?") == 0) {
527 a_write(a
, "INFO Trace options:\n");
528 for (t
= tr_opts
; t
->ch
; t
++) {
529 a_write(a
, "INFO %c %c %s\n",
530 t
->ch
, (tr_flags
& t
->f
) == t
->f ?
'*' : ' ', t
->help
);
534 unsigned f
= tr_flags
;
540 case '+': sense
= 1; break;
541 case '-': sense
= 0; break;
543 for (tt
= tr_opts
; tt
->ch
; tt
++) {
545 if (sense
) f
|= tt
->f
;
550 a_write(a
, "FAIL unknown trace option `%c'\n", *p
);
558 trace_level(tr_flags
);
565 static void acmd_port(admin
*a
, unsigned ac
, char *av
[])
567 a_write(a
, "INFO %u\nOK\n", p_port());
570 static void acmd_daemon(admin
*a
, unsigned ac
, char *av
[])
572 if (flags
& F_DAEMON
)
573 a_write(a
, "FAIL already running as a daemon\n");
575 a_warn("becoming a daemon");
579 a_write(a
, "FAIL error becoming a daemon: %s", strerror(errno
));
587 static void acmd_list(admin
*a
, unsigned ac
, char *av
[])
590 for (p
= p_first(); p
; p
= p_next(p
))
591 a_write(a
, "INFO %s\n", p_name(p
));
595 static void acmd_ifname(admin
*a
, unsigned ac
, char *av
[])
599 if ((p
= p_find(av
[0])) == 0)
600 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
602 a_write(a
, "INFO %s\nOK\n", p_ifname(p
));
605 static void acmd_addr(admin
*a
, unsigned ac
, char *av
[])
610 if ((p
= p_find(av
[0])) == 0)
611 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
614 assert(ad
->sa
.sa_family
== AF_INET
);
615 a_write(a
, "INFO %s %u\nOK\n",
616 inet_ntoa(ad
->sin
.sin_addr
),
617 (unsigned)ntohs(ad
->sin
.sin_port
));
621 static void acmd_stats(admin
*a
, unsigned ac
, char *av
[])
626 if ((p
= p_find(av
[0])) == 0)
627 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
630 a_write(a
, "INFO start-time=%s\n", timestr(st
->t_start
));
631 a_write(a
, "INFO last-packet-time=%s\n", timestr(st
->t_last
));
632 a_write(a
, "INFO packets-in=%lu bytes-in=%lu\n", st
->n_in
, st
->sz_in
);
633 a_write(a
, "INFO packets-out=%lu bytes-out=%lu\n",
634 st
->n_out
, st
->sz_out
);
635 a_write(a
, "INFO keyexch-packets-in=%lu keyexch-bytes-in=%lu\n",
636 st
->n_kxin
, st
->sz_kxin
);
637 a_write(a
, "INFO keyexch-packets-out=%lu keyexch-bytes-out=%lu\n",
638 st
->n_kxout
, st
->sz_kxout
);
639 a_write(a
, "INFO ip-packets-in=%lu ip-bytes-in=%lu\n",
640 st
->n_ipin
, st
->sz_ipin
);
641 a_write(a
, "INFO ip-packets-out=%lu ip-bytes-out=%lu\n",
642 st
->n_ipout
, st
->sz_ipout
);
643 a_write(a
, "INFO rejected-packets=%lu\n", st
->n_reject
);
648 static void acmd_kill(admin
*a
, unsigned ac
, char *av
[])
651 if ((p
= p_find(av
[0])) == 0)
652 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
659 static void acmd_quit(admin
*a
, unsigned ac
, char *av
[])
661 a_warn("closing down on admin request");
666 /* --- The command table and help --- */
668 typedef struct acmd
{
671 unsigned argmin
, argmax
;
672 void (*func
)(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
675 static void acmd_help(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
677 static const acmd acmdtab
[] = {
678 { "help", "HELP", 0, 0, acmd_help
},
680 { "trace", "TRACE [options]", 0, 1, acmd_trace
},
682 { "port", "PORT", 0, 0, acmd_port
},
683 { "daemon", "DAEMON", 0, 0, acmd_daemon
},
684 { "list", "LIST", 0, 0, acmd_list
},
685 { "ifname", "IFNAME peer", 1, 1, acmd_ifname
},
686 { "addr", "ADDR peer", 1, 1, acmd_addr
},
687 { "stats", "STATS peer", 1, 1, acmd_stats
},
688 { "kill", "KILL peer", 1, 1, acmd_kill
},
689 { "add", "ADD peer addr port", 3, 3, acmd_add
},
690 { "quit", "QUIT", 0, 0, acmd_quit
},
694 static void acmd_help(admin
*a
, unsigned ac
, char *av
[])
697 for (c
= acmdtab
; c
->name
; c
++)
698 a_write(a
, "INFO %s\n", c
->help
);
702 /*----- Connection handling -----------------------------------------------*/
704 /* --- @a_lock@ --- *
706 * Arguments: @admin *a@ = pointer to an admin block
710 * Use: Locks an admin block so that it won't be destroyed
714 static void a_lock(admin
*a
) { assert(!(a
->f
& AF_LOCK
)); a
->f
|= AF_LOCK
; }
716 /* --- @a_unlock@ --- *
718 * Arguments: @admin *a@ = pointer to an admin block
722 * Use: Unlocks an admin block, allowing its destruction. This is
723 * also the second half of @a_destroy@.
726 static void a_unlock(admin
*a
)
728 assert(a
->f
& AF_LOCK
);
729 if (!(a
->f
& AF_DEAD
)) {
734 T( trace(T_ADMIN
, "admin: completing destruction of connection %u",
737 selbuf_destroy(&a
->b
);
744 if (a
->b
.reader
.fd
!= a
->w
.fd
)
745 close(a
->b
.reader
.fd
);
751 a
->next
->prev
= a
->prev
;
753 a
->prev
->next
= a
->next
;
759 /* --- @a_destroy@ --- *
761 * Arguments: @admin *a@ = pointer to an admin block
765 * Use: Destroys an admin block. This requires a certain amount of
769 static void a_destroy(admin
*a
)
771 /* --- Don't multiply destroy admin blocks --- */
776 /* --- Make sure nobody expects it to work --- */
779 T( trace(T_ADMIN
, "admin: destroying connection %u", a
->seq
); )
781 /* --- Free the output buffers --- */
786 for (o
= a
->o_head
; o
; o
= oo
) {
793 /* --- If the block is locked, that's all we can manage --- */
795 if (a
->f
& AF_LOCK
) {
796 T( trace(T_ADMIN
, "admin: deferring destruction..."); )
803 /* --- @a_line@ --- *
805 * Arguments: @char *p@ = pointer to the line read
806 * @size_t len@ = length of the line
807 * @void *vp@ = pointer to my admin block
811 * Use: Handles a line of input.
814 static void a_line(char *p
, size_t len
, void *vp
)
828 ac
= str_qsplit(p
, av
, 4, 0, STRF_QUOTE
);
831 for (p
= av
[0]; *p
; p
++) *p
= tolower((unsigned char)*p
);
832 for (c
= acmdtab
; c
->name
; c
++) {
833 if (strcmp(av
[0], c
->name
) == 0) {
835 if (c
->argmin
> ac
|| ac
> c
->argmax
)
836 a_write(a
, "FAIL syntax: %s\n", c
->help
);
839 c
->func(a
, ac
, av
+ 1);
845 a_write(a
, "FAIL unknown command `%s'\n", av
[0]);
848 /* --- @a_create@ --- *
850 * Arguments: @int fd_in, fd_out@ = file descriptors to use
854 * Use: Creates a new admin connection.
857 void a_create(int fd_in
, int fd_out
)
859 admin
*a
= CREATE(admin
);
860 T( static unsigned seq
= 0;
862 T( trace(T_ADMIN
, "admin: accepted connection %u", a
->seq
); )
865 if (fd_in
== STDIN_FILENO
)
867 fdflags(fd_in
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
869 fdflags(fd_out
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
870 selbuf_init(&a
->b
, &sel
, fd_in
, a_line
, a
);
871 sel_initfile(&sel
, &a
->w
, fd_out
, SEL_WRITE
, a_flush
, a
);
881 /* --- @a_accept@ --- *
883 * Arguments: @int fd@ = file descriptor to accept
884 * @unsigned mode@ = what to do
885 * @void *v@ = uninteresting pointer
889 * Use: Accepts a new admin connection.
892 static void a_accept(int fd
, unsigned mode
, void *v
)
895 struct sockaddr_un sun
;
896 size_t sz
= sizeof(sun
);
898 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sz
)) < 0) {
899 a_warn("accept admin connection failed: %s", strerror(errno
));
905 /* --- @a_daemon@ --- *
911 * Use: Informs the admin module that it's a daemon.
919 /* --- @a_init@ --- *
921 * Arguments: @const char *name@ = socket name to create
925 * Use: Creates the admin listening socket.
928 void a_init(const char *name
)
932 struct sockaddr_un sun
;
936 /* --- Set up the socket address --- */
938 sz
= strlen(name
) + 1;
939 if (sz
> sizeof(sun
.sun_path
))
940 die(EXIT_FAILURE
, "socket name `%s' too long", name
);
942 sun
.sun_family
= AF_UNIX
;
943 memcpy(sun
.sun_path
, name
, sz
);
944 sz
+= offsetof(struct sockaddr_un
, sun_path
);
946 /* --- Attempt to bind to the socket --- */
950 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
951 die(EXIT_FAILURE
, "couldn't create socket: %s", strerror(errno
));
952 if (bind(fd
, (struct sockaddr
*)&sun
, sz
) < 0) {
955 if (errno
!= EADDRINUSE
) {
956 die(EXIT_FAILURE
, "couldn't bind to address `%s': %s",
957 sun
.sun_path
, strerror(e
));
960 die(EXIT_FAILURE
, "too many retries; giving up");
962 if (!connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
963 die(EXIT_FAILURE
, "server already listening on admin socket `%s'",
966 if (errno
!= ECONNREFUSED
)
967 die(EXIT_FAILURE
, "couldn't bind to address: %s", strerror(e
));
968 if (stat(sun
.sun_path
, &st
)) {
969 die(EXIT_FAILURE
, "couldn't stat `%s': %s",
970 sun
.sun_path
, strerror(errno
));
972 if (!S_ISSOCK(st
.st_mode
))
973 die(EXIT_FAILURE
, "object `%s' isn't a socket", sun
.sun_path
);
974 T( trace(T_ADMIN
, "admin: stale socket found; removing it"); )
975 unlink(sun
.sun_path
);
979 chmod(sun
.sun_path
, 0600);
980 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
982 die(EXIT_FAILURE
, "couldn't listen on socket: %s", strerror(errno
));
984 /* --- Listen to the socket --- */
986 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, a_accept
, 0);
990 T( trace_custom(a_trace
, 0);
991 trace(T_ADMIN
, "admin: enabled custom tracing"); )
994 /* --- Set up signal handlers --- */
996 sig_add(&s_term
, SIGTERM
, a_sigdie
, 0);
997 sig_add(&s_hup
, SIGHUP
, a_sighup
, 0);
998 sigaction(SIGINT
, 0, &sa
);
999 if (sa
.sa_handler
!= SIG_IGN
)
1000 sig_add(&s_int
, SIGINT
, a_sigdie
, 0);
1003 /*----- That's all, folks -------------------------------------------------*/