3 * $Id: admin.c,v 1.10 2004/04/08 01:36: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 /*----- Header files ------------------------------------------------------*/
33 /*----- Global variables --------------------------------------------------*/
37 const trace_opt tr_opts
[] = {
38 { 't', T_TUNNEL
, "tunnel events" },
39 { 'r', T_PEER
, "peer events" },
40 { 'a', T_ADMIN
, "admin interface" },
41 { 'p', T_PACKET
, "packet contents" },
42 { 'c', T_CRYPTO
, "crypto details" },
43 { 's', T_KEYSET
, "symmetric keyset management" },
44 { 'x', T_KEYEXCH
, "key exchange" },
45 { 'm', T_KEYMGMT
, "key management" },
46 { 'A', T_ALL
, "all of the above" },
50 unsigned tr_flags
= 0;
53 /*----- Static variables --------------------------------------------------*/
57 static const char *sockname
;
58 static unsigned flags
= 0;
59 static admin
*a_stdin
= 0;
60 static sig s_term
, s_int
, s_hup
;
65 #define T_RESOLVE SEC(30)
67 static void a_destroy(admin */
*a*/
);
68 static void a_lock(admin */
*a*/
);
69 static void a_unlock(admin */
*a*/
);
71 /*----- Output functions --------------------------------------------------*/
73 /* --- @trywrite@ --- *
75 * Arguments: @admin *a@ = pointer to an admin block
76 * @const char *p@ = pointer to buffer to write
77 * @size_t sz@ = size of data to write
79 * Returns: The number of bytes written, or less than zero on error.
81 * Use: Attempts to write data to a client.
84 static ssize_t
trywrite(admin
*a
, const char *p
, size_t sz
)
91 n
= write(a
->w
.fd
, p
, sz
);
101 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
103 a_warn("disconnecting admin client due to write errors: %s",
111 /* --- @dosend@ --- *
113 * Arguemnts: @admin *a@ = pointer to an admin block
114 * @const char *p@ = pointer to buffer to write
115 * @size_t sz@ = size of data to write
119 * Use: Sends data to an admin client.
122 static void dosend(admin
*a
, const char *p
, size_t sz
)
130 /* --- Try to send the data immediately --- */
133 if ((n
= trywrite(a
, p
, sz
)) < 0)
141 /* --- Fill buffers with the data until it's all gone --- */
146 else if (o
->p_in
< o
->buf
+ OBUFSZ
)
150 o
= xmalloc(sizeof(obuf
));
152 o
->p_in
= o
->p_out
= o
->buf
;
160 n
= o
->buf
+ OBUFSZ
- o
->p_in
;
163 memcpy(o
->p_in
, p
, n
);
170 /* --- @a_flush@ --- *
172 * Arguments: @int fd@ = file descriptor
173 * @unsigned mode@ = what's happening
174 * @void *v@ = pointer to my admin block
178 * Use: Flushes buffers when a client is ready to read again.
181 static void a_flush(int fd
, unsigned mode
, void *v
)
189 if ((n
= trywrite(a
, o
->p_out
, o
->p_in
- o
->p_out
)) < 0)
192 if (o
->p_in
< o
->p_out
)
205 /*----- Utility functions -------------------------------------------------*/
207 /* --- @a_write@ --- *
209 * Arguments: @admin *a@ = admin connection to write to
210 * @const char *fmt@ = pointer to format string
211 * @...@ = other arguments
215 * Use: Sends a message to an admin connection.
218 static void a_write(admin
*a
, const char *fmt
, ...)
223 dstr_vputf(&d
, fmt
, &ap
);
225 dosend(a
, d
.buf
, d
.len
);
229 /* --- @a_warn@ --- *
231 * Arguments: @const char *fmt@ = pointer to format string
232 * @...@ = other arguments
236 * Use: Informs all admin connections of a warning.
239 void a_warn(const char *fmt
, ...)
246 dstr_puts(&d
, "WARN ");
248 dstr_vputf(&d
, fmt
, &ap
);
250 if (!(flags
& F_INIT
))
254 for (a
= admins
; a
; a
= aa
) {
256 dosend(a
, d
.buf
, d
.len
);
262 /* --- @a_trace@ --- *
264 * Arguments: @const char *p@ = pointer to a buffer
265 * @size_t sz@ = size of the buffer
266 * @void *v@ = uninteresting pointer
270 * Use: Custom trace output handler.
274 static void a_trace(const char *p
, size_t sz
, void *v
)
279 dstr_puts(&d
, "TRACE ");
280 dstr_putm(&d
, p
, sz
);
282 for (a
= admins
; a
; a
= aa
) {
284 dosend(a
, d
.buf
, d
.len
);
290 /* --- @a_quit@ --- *
296 * Use: Shuts things down nicely.
306 /* --- @a_sigdie@ --- *
308 * Arguments: @int sig@ = signal number
309 * @void *v@ = an uninteresting argument
313 * Use Shuts down on receipt of a fatal signal.
316 static void a_sigdie(int sig
, void *v
)
322 case SIGTERM
: p
= "SIGTERM"; break;
323 case SIGINT
: p
= "SIGINT"; break;
325 sprintf(buf
, "signal %i", sig
);
329 a_warn("shutting down on %s", p
);
333 /* --- @a_sighup@ --- *
335 * Arguments: @int sig@ = signal number
336 * @void *v@ = an uninteresting argument
340 * Use Logs a message about SIGHUP not being useful.
343 static void a_sighup(int sig
, void *v
)
345 a_warn("received SIGHUP: ignoring");
348 /*----- Adding peers ------------------------------------------------------*/
350 /* --- @a_resolve@ --- *
352 * Arguments: @struct hostent *h@ = pointer to resolved hostname
353 * @void *v@ = pointer to admin block
357 * Use: Handles a completed name resolution.
360 static void a_resolve(struct hostent
*h
, void *v
)
365 T( trace(T_ADMIN
, "admin: %u resolved", a
->seq
); )
369 a_write(a
, "FAIL couldn't resolve hostname `%s'\n", a
->paddr
);
370 else if (p_find(a
->pname
))
371 a_write(a
, "FAIL peer `%s' already registered\n", a
->pname
);
373 memcpy(&a
->peer
.sin
.sin_addr
, h
->h_addr
, sizeof(struct in_addr
));
374 if (!p_create(a
->pname
, &a
->peer
.sa
, a
->sasz
))
375 a_write(a
, "FAIL couldn't create peer\n");
382 selbuf_enable(&a
->b
);
386 /* --- @a_timer@ --- *
388 * Arguments: @struct timeval *tv@ = timer
389 * @void *v@ = pointer to admin block
393 * Use: Times out a resolver.
396 static void a_timer(struct timeval
*tv
, void *v
)
401 T( trace(T_ADMIN
, "admin: %u resolver timeout", a
->seq
); )
403 a_write(a
, "FAIL timeout resolving `%s'\n", a
->paddr
);
407 selbuf_enable(&a
->b
);
411 /* --- @acmd_add@ --- *
413 * Arguments: @admin *a@ = connection which requested the addition
414 * @unsigned ac@ = argument count
415 * @char *av[]@ = pointer to the argument list
419 * Use: Adds a new peer.
422 static void acmd_add(admin
*a
, unsigned ac
, char *av
[])
428 /* --- Make sure someone's not got there already --- */
431 a_write(a
, "FAIL peer `%s' already registered\n", av
[0]);
435 /* --- Fill in the easy bits of address --- */
438 a
->peer
.sin
.sin_family
= AF_INET
;
439 a
->sasz
= sizeof(a
->peer
.sin
);
440 pt
= strtoul(av
[2], &p
, 0);
442 struct servent
*s
= getservbyname(av
[2], "udp");
444 a_write(a
, "FAIL service `%s' not known\n", av
[2]);
447 pt
= ntohs(s
->s_port
);
449 if (pt
== 0 || pt
>= 65536) {
450 a_write(a
, "FAIL bad port number %lu\n", pt
);
453 a
->peer
.sin
.sin_port
= htons(pt
);
455 /* --- If the name is numeric, do it the easy way --- */
457 if (inet_aton(av
[1], &a
->peer
.sin
.sin_addr
)) {
458 if (!p_create(av
[0], &a
->peer
.sa
, a
->sasz
))
459 a_write(a
, "FAIL couldn't create peer\n");
465 /* --- Store everything for later and crank up the resolver --- *
467 * We disable the line buffer until the resolver completes (or times out).
468 * This prevents other commands on the same connection (though the rest of
469 * the system continues regardless), but makes life simpler for the client.
472 a
->pname
= xstrdup(av
[0]);
473 a
->paddr
= xstrdup(av
[1]);
474 selbuf_disable(&a
->b
);
475 gettimeofday(&tv
, 0);
476 tv
.tv_sec
+= T_RESOLVE
;
477 sel_addtimer(&sel
, &a
->t
, &tv
, a_timer
, a
);
478 bres_byname(&a
->r
, a
->paddr
, a_resolve
, a
);
479 T( trace(T_ADMIN
, "admin: %u resolving hostname `%s'",
483 /*----- Administration commands -------------------------------------------*/
485 /* --- Miscellaneous commands --- */
489 static void acmd_trace(admin
*a
, unsigned ac
, char *av
[])
491 if (!ac
|| strcmp(av
[0], "?") == 0) {
493 a_write(a
, "INFO Trace options:\n");
494 for (t
= tr_opts
; t
->ch
; t
++) {
495 a_write(a
, "INFO %c %c %s\n",
496 t
->ch
, (tr_flags
& t
->f
) == t
->f ?
'*' : ' ', t
->help
);
500 unsigned f
= tr_flags
;
506 case '+': sense
= 1; break;
507 case '-': sense
= 0; break;
509 for (tt
= tr_opts
; tt
->ch
; tt
++) {
511 if (sense
) f
|= tt
->f
;
516 a_write(a
, "FAIL unknown trace option `%c'\n", *p
);
524 trace_level(tr_flags
);
531 static void acmd_port(admin
*a
, unsigned ac
, char *av
[])
533 a_write(a
, "INFO %u\nOK\n", p_port());
536 static void acmd_daemon(admin
*a
, unsigned ac
, char *av
[])
538 if (flags
& F_DAEMON
)
539 a_write(a
, "FAIL already running as a daemon\n");
541 a_warn("becoming a daemon");
545 a_write(a
, "FAIL error becoming a daemon: %s", strerror(errno
));
553 static void acmd_list(admin
*a
, unsigned ac
, char *av
[])
556 for (p
= p_first(); p
; p
= p_next(p
))
557 a_write(a
, "INFO %s\n", p_name(p
));
561 static void acmd_ifname(admin
*a
, unsigned ac
, char *av
[])
565 if ((p
= p_find(av
[0])) == 0)
566 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
568 a_write(a
, "INFO %s\nOK\n", p_ifname(p
));
571 static void acmd_addr(admin
*a
, unsigned ac
, char *av
[])
576 if ((p
= p_find(av
[0])) == 0)
577 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
580 assert(ad
->sa
.sa_family
== AF_INET
);
581 a_write(a
, "INFO %s %u\nOK\n",
582 inet_ntoa(ad
->sin
.sin_addr
),
583 (unsigned)ntohs(ad
->sin
.sin_port
));
587 static void acmd_stats(admin
*a
, unsigned ac
, char *av
[])
592 if ((p
= p_find(av
[0])) == 0)
593 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
596 a_write(a
, "INFO start-time=%s\n", timestr(st
->t_start
));
597 a_write(a
, "INFO last-packet-time=%s\n", timestr(st
->t_last
));
598 a_write(a
, "INFO packets-in=%lu bytes-in=%lu\n", st
->n_in
, st
->sz_in
);
599 a_write(a
, "INFO packets-out=%lu bytes-out=%lu\n",
600 st
->n_out
, st
->sz_out
);
601 a_write(a
, "INFO keyexch-packets-in=%lu keyexch-bytes-in=%lu\n",
602 st
->n_kxin
, st
->sz_kxin
);
603 a_write(a
, "INFO keyexch-packets-out=%lu keyexch-bytes-out=%lu\n",
604 st
->n_kxout
, st
->sz_kxout
);
605 a_write(a
, "INFO ip-packets-in=%lu ip-bytes-in=%lu\n",
606 st
->n_ipin
, st
->sz_ipin
);
607 a_write(a
, "INFO ip-packets-out=%lu ip-bytes-out=%lu\n",
608 st
->n_ipout
, st
->sz_ipout
);
609 a_write(a
, "INFO rejected-packets=%lu\n", st
->n_reject
);
614 static void acmd_kill(admin
*a
, unsigned ac
, char *av
[])
617 if ((p
= p_find(av
[0])) == 0)
618 a_write(a
, "FAIL peer `%s' not found\n", av
[0]);
625 static void acmd_quit(admin
*a
, unsigned ac
, char *av
[])
627 a_warn("closing down on admin request");
632 /* --- The command table and help --- */
634 typedef struct acmd
{
637 unsigned argmin
, argmax
;
638 void (*func
)(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
641 static void acmd_help(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
643 static const acmd acmdtab
[] = {
644 { "help", "HELP", 0, 0, acmd_help
},
646 { "trace", "TRACE [options]", 0, 1, acmd_trace
},
648 { "port", "PORT", 0, 0, acmd_port
},
649 { "daemon", "DAEMON", 0, 0, acmd_daemon
},
650 { "list", "LIST", 0, 0, acmd_list
},
651 { "ifname", "IFNAME peer", 1, 1, acmd_ifname
},
652 { "addr", "ADDR peer", 1, 1, acmd_addr
},
653 { "stats", "STATS peer", 1, 1, acmd_stats
},
654 { "kill", "KILL peer", 1, 1, acmd_kill
},
655 { "add", "ADD peer addr port", 3, 3, acmd_add
},
656 { "quit", "QUIT", 0, 0, acmd_quit
},
660 static void acmd_help(admin
*a
, unsigned ac
, char *av
[])
663 for (c
= acmdtab
; c
->name
; c
++)
664 a_write(a
, "INFO %s\n", c
->help
);
668 /*----- Connection handling -----------------------------------------------*/
670 /* --- @a_lock@ --- *
672 * Arguments: @admin *a@ = pointer to an admin block
676 * Use: Locks an admin block so that it won't be destroyed
680 static void a_lock(admin
*a
) { assert(!(a
->f
& AF_LOCK
)); a
->f
|= AF_LOCK
; }
682 /* --- @a_unlock@ --- *
684 * Arguments: @admin *a@ = pointer to an admin block
688 * Use: Unlocks an admin block, allowing its destruction. This is
689 * also the second half of @a_destroy@.
692 static void a_unlock(admin
*a
)
694 assert(a
->f
& AF_LOCK
);
695 if (!(a
->f
& AF_DEAD
)) {
700 T( trace(T_ADMIN
, "admin: completing destruction of connection %u",
703 selbuf_destroy(&a
->b
);
710 if (a
->b
.reader
.fd
!= a
->w
.fd
)
711 close(a
->b
.reader
.fd
);
717 a
->next
->prev
= a
->prev
;
719 a
->prev
->next
= a
->next
;
725 /* --- @a_destroy@ --- *
727 * Arguments: @admin *a@ = pointer to an admin block
731 * Use: Destroys an admin block. This requires a certain amount of
735 static void a_destroy(admin
*a
)
737 /* --- Don't multiply destroy admin blocks --- */
742 /* --- Make sure nobody expects it to work --- */
745 T( trace(T_ADMIN
, "admin: destroying connection %u", a
->seq
); )
747 /* --- Free the output buffers --- */
752 for (o
= a
->o_head
; o
; o
= oo
) {
759 /* --- If the block is locked, that's all we can manage --- */
761 if (a
->f
& AF_LOCK
) {
762 T( trace(T_ADMIN
, "admin: deferring destruction..."); )
769 /* --- @a_line@ --- *
771 * Arguments: @char *p@ = pointer to the line read
772 * @size_t len@ = length of the line
773 * @void *vp@ = pointer to my admin block
777 * Use: Handles a line of input.
780 static void a_line(char *p
, size_t len
, void *vp
)
794 ac
= str_qsplit(p
, av
, 4, 0, STRF_QUOTE
);
797 for (p
= av
[0]; *p
; p
++) *p
= tolower((unsigned char)*p
);
798 for (c
= acmdtab
; c
->name
; c
++) {
799 if (strcmp(av
[0], c
->name
) == 0) {
801 if (c
->argmin
> ac
|| ac
> c
->argmax
)
802 a_write(a
, "FAIL syntax: %s\n", c
->help
);
805 c
->func(a
, ac
, av
+ 1);
811 a_write(a
, "FAIL unknown command `%s'\n", av
[0]);
814 /* --- @a_create@ --- *
816 * Arguments: @int fd_in, fd_out@ = file descriptors to use
820 * Use: Creates a new admin connection.
823 void a_create(int fd_in
, int fd_out
)
825 admin
*a
= CREATE(admin
);
826 T( static unsigned seq
= 0;
828 T( trace(T_ADMIN
, "admin: accepted connection %u", a
->seq
); )
831 if (fd_in
== STDIN_FILENO
)
833 fdflags(fd_in
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
835 fdflags(fd_out
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
836 selbuf_init(&a
->b
, &sel
, fd_in
, a_line
, a
);
837 sel_initfile(&sel
, &a
->w
, fd_out
, SEL_WRITE
, a_flush
, a
);
847 /* --- @a_accept@ --- *
849 * Arguments: @int fd@ = file descriptor to accept
850 * @unsigned mode@ = what to do
851 * @void *v@ = uninteresting pointer
855 * Use: Accepts a new admin connection.
858 static void a_accept(int fd
, unsigned mode
, void *v
)
861 struct sockaddr_un sun
;
862 size_t sz
= sizeof(sun
);
864 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sz
)) < 0) {
865 if (errno
!= EINTR
&& errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
866 errno
!= ECONNABORTED
&& errno
!= EPROTO
)
867 a_warn("accept admin connection failed: %s", strerror(errno
));
873 /* --- @a_daemon@ --- *
879 * Use: Informs the admin module that it's a daemon.
887 /* --- @a_init@ --- *
889 * Arguments: @const char *name@ = socket name to create
893 * Use: Creates the admin listening socket.
896 void a_init(const char *name
)
900 struct sockaddr_un sun
;
904 /* --- Set up the socket address --- */
906 sz
= strlen(name
) + 1;
907 if (sz
> sizeof(sun
.sun_path
))
908 die(EXIT_FAILURE
, "socket name `%s' too long", name
);
910 sun
.sun_family
= AF_UNIX
;
911 memcpy(sun
.sun_path
, name
, sz
);
912 sz
+= offsetof(struct sockaddr_un
, sun_path
);
914 /* --- Attempt to bind to the socket --- */
918 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
919 die(EXIT_FAILURE
, "couldn't create socket: %s", strerror(errno
));
920 if (bind(fd
, (struct sockaddr
*)&sun
, sz
) < 0) {
923 if (errno
!= EADDRINUSE
) {
924 die(EXIT_FAILURE
, "couldn't bind to address `%s': %s",
925 sun
.sun_path
, strerror(e
));
928 die(EXIT_FAILURE
, "too many retries; giving up");
930 if (!connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
931 die(EXIT_FAILURE
, "server already listening on admin socket `%s'",
934 if (errno
!= ECONNREFUSED
)
935 die(EXIT_FAILURE
, "couldn't bind to address: %s", strerror(e
));
936 if (stat(sun
.sun_path
, &st
)) {
937 die(EXIT_FAILURE
, "couldn't stat `%s': %s",
938 sun
.sun_path
, strerror(errno
));
940 if (!S_ISSOCK(st
.st_mode
))
941 die(EXIT_FAILURE
, "object `%s' isn't a socket", sun
.sun_path
);
942 T( trace(T_ADMIN
, "admin: stale socket found; removing it"); )
943 unlink(sun
.sun_path
);
947 chmod(sun
.sun_path
, 0600);
948 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
950 die(EXIT_FAILURE
, "couldn't listen on socket: %s", strerror(errno
));
952 /* --- Listen to the socket --- */
954 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, a_accept
, 0);
958 T( trace_custom(a_trace
, 0);
959 trace(T_ADMIN
, "admin: enabled custom tracing"); )
962 /* --- Set up signal handlers --- */
964 sig_add(&s_term
, SIGTERM
, a_sigdie
, 0);
965 sig_add(&s_hup
, SIGHUP
, a_sighup
, 0);
966 sigaction(SIGINT
, 0, &sa
);
967 if (sa
.sa_handler
!= SIG_IGN
)
968 sig_add(&s_int
, SIGINT
, a_sigdie
, 0);
971 /*----- That's all, folks -------------------------------------------------*/