3 * $Id: admin.c,v 1.3 2001/02/04 01:17:12 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.3 2001/02/04 01:17:12 mdw
33 * The `DAEMON' notification to stdout is replaced by a warning. The
34 * `DAEMON' and `QUIT' command send `OK' on successful completion. Put
35 * assignment of sequence number in a T(...) guard.
37 * Revision 1.2 2001/02/03 22:40:29 mdw
38 * Put timer information into the entropy pool when packets are received
39 * and on similar events. Reseed the generator on the interval timer.
41 * Revision 1.1 2001/02/03 20:26:37 mdw
46 /*----- Header files ------------------------------------------------------*/
50 /*----- Global variables --------------------------------------------------*/
54 const trace_opt tr_opts
[] = {
55 { 't', T_TUNNEL
, "tunnel events" },
56 { 'r', T_PEER
, "peer events" },
57 { 'a', T_ADMIN
, "admin interface" },
58 { 'p', T_PACKET
, "packet contents" },
59 { 'c', T_CRYPTO
, "crypto details" },
60 { 's', T_KEYSET
, "symmetric keyset management" },
61 { 'x', T_KEYEXCH
, "key exchange" },
62 { 'm', T_KEYMGMT
, "key management" },
63 { 'A', T_ALL
, "all of the above" },
67 unsigned tr_flags
= 0;
70 /*----- Static variables --------------------------------------------------*/
74 static const char *sockname
;
75 static unsigned flags
= 0;
76 static admin
*a_stdin
= 0;
77 static sig s_term
, s_int
, s_hup
;
82 #define T_RESOLVE SEC(30)
84 /*----- Utility functions -------------------------------------------------*/
86 /* --- @a_write@ --- *
88 * Arguments: @admin *a@ = admin connection to write to
89 * @const char *fmt@ = pointer to format string
90 * @...@ = other arguments
94 * Use: Sends a message to an admin connection.
97 static void a_write(admin
*a
, const char *fmt
, ...)
102 dstr_vputf(&d
, fmt
, ap
);
104 write(a
->fd
, d
.buf
, d
.len
);
108 /* --- @a_warn@ --- *
110 * Arguments: @const char *fmt@ = pointer to format string
111 * @...@ = other arguments
115 * Use: Informs all admin connections of a warning.
118 void a_warn(const char *fmt
, ...)
125 dstr_puts(&d
, "WARN ");
127 dstr_vputf(&d
, fmt
, ap
);
129 if (!(flags
& F_INIT
))
133 for (a
= admins
; a
; a
= a
->next
)
134 write(a
->fd
, d
.buf
, d
.len
);
139 /* --- @a_trace@ --- *
141 * Arguments: @const char *p@ = pointer to a buffer
142 * @size_t sz@ = size of the buffer
143 * @void *v@ = uninteresting pointer
147 * Use: Custom trace output handler.
151 static void a_trace(const char *p
, size_t sz
, void *v
)
156 dstr_puts(&d
, "TRACE ");
157 dstr_putm(&d
, p
, sz
);
159 for (a
= admins
; a
; a
= a
->next
)
160 write(a
->fd
, d
.buf
, d
.len
);
165 /* --- @a_quit@ --- *
171 * Use: Shuts things down nicely.
181 /* --- @a_sigdie@ --- *
183 * Arguments: @int sig@ = signal number
184 * @void *v@ = an uninteresting argument
188 * Use Shuts down on receipt of a fatal signal.
191 static void a_sigdie(int sig
, void *v
)
197 case SIGTERM
: p
= "SIGTERM"; break;
198 case SIGINT
: p
= "SIGINT"; break;
200 sprintf(buf
, "signal %i", sig
);
204 a_warn("shutting down on %s", p
);
208 /* --- @a_sighup@ --- *
210 * Arguments: @int sig@ = signal number
211 * @void *v@ = an uninteresting argument
215 * Use Logs a message about SIGHUP not being useful.
218 static void a_sighup(int sig
, void *v
)
220 a_warn("received SIGHUP: ignoring");
223 /*----- Adding peers ------------------------------------------------------*/
225 /* --- @a_resolve@ --- *
227 * Arguments: @struct hostent *h@ = pointer to resolved hostname
228 * @void *v@ = pointer to admin block
232 * Use: Handles a completed name resolution.
235 static void a_resolve(struct hostent
*h
, void *v
)
238 T( trace(T_ADMIN
, "admin: %u resolved", a
->seq
); )
242 a_write(a
, "ERR couldn't resolve hostname `%s'\n", a
->paddr
);
243 else if (p_find(a
->pname
))
244 a_write(a
, "ERR peer `%s' already registered\n", a
->pname
);
246 memcpy(&a
->peer
.sin
.sin_addr
, h
->h_addr
, sizeof(struct in_addr
));
247 if (!p_create(a
->pname
, &a
->peer
.sa
, a
->sasz
))
248 a_write(a
, "ERR couldn't create peer\n");
255 selbuf_enable(&a
->b
);
258 /* --- @a_timer@ --- *
260 * Arguments: @struct timeval *tv@ = timer
261 * @void *v@ = pointer to admin block
265 * Use: Times out a resolver.
268 static void a_timer(struct timeval
*tv
, void *v
)
271 T( trace(T_ADMIN
, "admin: %u resolver timeout", a
->seq
); )
273 a_write(a
, "ERR timeout resolving `%s'\n", a
->paddr
);
277 selbuf_enable(&a
->b
);
280 /* --- @acmd_add@ --- *
282 * Arguments: @admin *a@ = connection which requested the addition
283 * @unsigned ac@ = argument count
284 * @char *av[]@ = pointer to the argument list
288 * Use: Adds a new peer.
291 static void acmd_add(admin
*a
, unsigned ac
, char *av
[])
297 /* --- Make sure someone's not got there already --- */
300 a_write(a
, "ERR peer `%s' already registered\n", av
[0]);
304 /* --- Fill in the easy bits of address --- */
307 a
->peer
.sin
.sin_family
= AF_INET
;
308 a
->sasz
= sizeof(a
->peer
.sin
);
309 pt
= strtoul(av
[2], &p
, 0);
311 struct servent
*s
= getservbyname(av
[2], "udp");
313 a_write(a
, "ERR service `%s' not known\n", av
[2]);
316 pt
= ntohs(s
->s_port
);
318 if (pt
== 0 || pt
>= 65536) {
319 a_write(a
, "ERR bad port number %lu\n", pt
);
322 a
->peer
.sin
.sin_port
= htons(pt
);
324 /* --- If the name is numeric, do it the easy way --- */
326 if (inet_aton(av
[1], &a
->peer
.sin
.sin_addr
)) {
327 if (!p_create(av
[0], &a
->peer
.sa
, a
->sasz
))
328 a_write(a
, "ERR couldn't create peer\n");
334 /* --- Store everything for later and crank up the resolver --- *
336 * We disable the line buffer until the resolver completes (or times out).
337 * This prevents other commands on the same connection (though the rest of
338 * the system continues regardless), but makes life simpler for the client.
341 a
->pname
= xstrdup(av
[0]);
342 a
->paddr
= xstrdup(av
[1]);
343 selbuf_disable(&a
->b
);
344 gettimeofday(&tv
, 0);
345 tv
.tv_sec
+= T_RESOLVE
;
346 sel_addtimer(&sel
, &a
->t
, &tv
, a_timer
, a
);
347 bres_byname(&a
->r
, a
->paddr
, a_resolve
, a
);
348 T( trace(T_ADMIN
, "admin: %u resolving hostname `%s'",
352 /*----- Administration commands -------------------------------------------*/
354 /* --- Miscellaneous commands --- */
358 static void acmd_trace(admin
*a
, unsigned ac
, char *av
[])
360 if (!ac
|| strcmp(av
[0], "?") == 0) {
362 a_write(a
, "INFO Trace options:\n");
363 for (t
= tr_opts
; t
->ch
; t
++) {
364 a_write(a
, "INFO %c %c %s\n",
365 t
->ch
, (tr_flags
& t
->f
) == t
->f ?
'*' : ' ', t
->help
);
369 unsigned f
= tr_flags
;
375 case '+': sense
= 1; break;
376 case '-': sense
= 0; break;
378 for (tt
= tr_opts
; tt
->ch
; tt
++) {
380 if (sense
) f
|= tt
->f
;
385 a_write(a
, "ERR unknown trace option `%c'\n", *p
);
393 trace_level(tr_flags
);
400 static void acmd_port(admin
*a
, unsigned ac
, char *av
[])
402 a_write(a
, "INFO %u\nOK\n", p_port());
405 static void a_destroy(admin */
*a*/
);
407 static void acmd_daemon(admin
*a
, unsigned ac
, char *av
[])
409 if (flags
& F_DAEMON
)
410 a_write(a
, "ERR already running as a daemon\n");
412 a_warn("becoming a daemon");
416 a_write(a
, "ERR error becoming a daemon: %s", strerror(errno
));
424 static void acmd_list(admin
*a
, unsigned ac
, char *av
[])
427 for (p
= p_first(); p
; p
= p_next(p
))
428 a_write(a
, "INFO %s\n", p_name(p
));
432 static void acmd_ifname(admin
*a
, unsigned ac
, char *av
[])
436 if ((p
= p_find(av
[0])) == 0)
437 a_write(a
, "ERR peer `%s' not found\n", av
[0]);
439 a_write(a
, "INFO %s\nOK\n", p_ifname(p
));
442 static void acmd_addr(admin
*a
, unsigned ac
, char *av
[])
447 if ((p
= p_find(av
[0])) == 0)
448 a_write(a
, "ERR peer `%s' not found\n", av
[0]);
451 assert(ad
->sa
.sa_family
== AF_INET
);
452 a_write(a
, "INFO %s %u\nOK\n",
453 inet_ntoa(ad
->sin
.sin_addr
),
454 (unsigned)ntohs(ad
->sin
.sin_port
));
458 static void acmd_kill(admin
*a
, unsigned ac
, char *av
[])
461 if ((p
= p_find(av
[0])) == 0)
462 a_write(a
, "ERR peer `%s' not found\n", av
[0]);
469 static void acmd_quit(admin
*a
, unsigned ac
, char *av
[])
471 a_warn("closing down on admin request");
476 /* --- The command table and help --- */
478 typedef struct acmd
{
481 unsigned argmin
, argmax
;
482 void (*func
)(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
485 static void acmd_help(admin */
*a*/
, unsigned /*ac*/, char */
*av*/
[]);
487 static const acmd acmdtab
[] = {
488 { "help", "HELP", 0, 0, acmd_help
},
490 { "trace", "TRACE [options]", 0, 1, acmd_trace
},
492 { "port", "PORT", 0, 0, acmd_port
},
493 { "daemon", "DAEMON", 0, 0, acmd_daemon
},
494 { "list", "LIST", 0, 0, acmd_list
},
495 { "ifname", "IFNAME peer", 1, 1, acmd_ifname
},
496 { "addr", "ADDR peer", 1, 1, acmd_addr
},
497 { "kill", "KILL peer", 1, 1, acmd_kill
},
498 { "add", "ADD peer addr port", 3, 3, acmd_add
},
499 { "quit", "QUIT", 0, 0, acmd_quit
},
503 static void acmd_help(admin
*a
, unsigned ac
, char *av
[])
506 for (c
= acmdtab
; c
->name
; c
++)
507 a_write(a
, "INFO %s\n", c
->help
);
511 /*----- Connection handling -----------------------------------------------*/
513 /* --- @a_destroy@ --- *
515 * Arguments: @admin *a@ = pointer to an admin block
519 * Use: Destroys an admin block.
522 static void a_destroy(admin
*a
)
524 T( trace(T_ADMIN
, "admin: destroying connection %u", a
->seq
); )
525 selbuf_destroy(&a
->b
);
526 if (a
->b
.reader
.fd
!= a
->fd
)
527 close(a
->b
.reader
.fd
);
536 a
->next
->prev
= a
->prev
;
538 a
->prev
->next
= a
->next
;
546 /* --- @a_line@ --- *
548 * Arguments: @char *p@ = pointer to the line read
549 * @void *vp@ = pointer to my admin block
553 * Use: Handles a line of input.
556 static void a_line(char *p
, void *vp
)
568 ac
= str_qsplit(p
, av
, 4, 0, STRF_QUOTE
);
571 for (p
= av
[0]; *p
; p
++) *p
= tolower((unsigned char)*p
);
572 for (c
= acmdtab
; c
->name
; c
++) {
573 if (strcmp(av
[0], c
->name
) == 0) {
575 if (c
->argmin
> ac
|| ac
> c
->argmax
)
576 a_write(a
, "ERR syntax: %s\n", c
->help
);
578 c
->func(a
, ac
, av
+ 1);
582 a_write(a
, "ERR unknown command `%s'\n", av
[0]);
585 /* --- @a_create@ --- *
587 * Arguments: @int fd_in, fd_out@ = file descriptors to use
591 * Use: Creates a new admin connection.
594 void a_create(int fd_in
, int fd_out
)
596 admin
*a
= CREATE(admin
);
597 T( static unsigned seq
= 0;
599 T( trace(T_ADMIN
, "admin: accepted connection %u", a
->seq
); )
601 if (fd_in
== STDIN_FILENO
)
603 fdflags(fd_in
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
605 fdflags(fd_out
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
607 selbuf_init(&a
->b
, &sel
, fd_in
, a_line
, a
);
615 /* --- @a_accept@ --- *
617 * Arguments: @int fd@ = file descriptor to accept
618 * @unsigned mode@ = what to do
619 * @void *v@ = uninteresting pointer
623 * Use: Accepts a new admin connection.
626 static void a_accept(int fd
, unsigned mode
, void *v
)
629 struct sockaddr_un sun
;
630 size_t sz
= sizeof(sun
);
632 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sz
)) < 0) {
633 a_warn("accept admin connection failed: %s", strerror(errno
));
639 /* --- @a_daemon@ --- *
645 * Use: Informs the admin module that it's a daemon.
653 /* --- @a_init@ --- *
655 * Arguments: @const char *name@ = socket name to create
659 * Use: Creates the admin listening socket.
662 void a_init(const char *name
)
666 struct sockaddr_un sun
;
670 /* --- Set up the socket address --- */
672 sz
= strlen(name
) + 1;
673 if (sz
> sizeof(sun
.sun_path
))
674 die(EXIT_FAILURE
, "socket name `%s' too long", name
);
676 sun
.sun_family
= AF_UNIX
;
677 memcpy(sun
.sun_path
, name
, sz
);
678 sz
+= offsetof(struct sockaddr_un
, sun_path
);
680 /* --- Attempt to bind to the socket --- */
684 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
685 die(EXIT_FAILURE
, "couldn't create socket: %s", strerror(errno
));
686 if (bind(fd
, (struct sockaddr
*)&sun
, sz
) < 0) {
689 if (errno
!= EADDRINUSE
) {
690 die(EXIT_FAILURE
, "couldn't bind to address `%s': %s",
691 sun
.sun_path
, strerror(e
));
694 die(EXIT_FAILURE
, "too many retries; giving up");
696 if (!connect(fd
, (struct sockaddr
*)&sun
, sz
)) {
697 die(EXIT_FAILURE
, "server already listening on admin socket `%s'",
700 if (errno
!= ECONNREFUSED
)
701 die(EXIT_FAILURE
, "couldn't bind to address: %s", strerror(e
));
702 if (stat(sun
.sun_path
, &st
)) {
703 die(EXIT_FAILURE
, "couldn't stat `%s': %s",
704 sun
.sun_path
, strerror(errno
));
706 if (!S_ISSOCK(st
.st_mode
))
707 die(EXIT_FAILURE
, "object `%s' isn't a socket", sun
.sun_path
);
708 T( trace(T_ADMIN
, "stale socket found; removing it"); )
709 unlink(sun
.sun_path
);
713 chmod(sun
.sun_path
, 0600);
714 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
716 die(EXIT_FAILURE
, "couldn't listen on socket: %s", strerror(errno
));
718 /* --- Listen to the socket --- */
720 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, a_accept
, 0);
724 T( trace_custom(a_trace
, 0);
725 trace(T_ADMIN
, "admin: enabled custom tracing"); )
728 /* --- Set up signal handlers --- */
730 sig_add(&s_term
, SIGTERM
, a_sigdie
, 0);
731 sig_add(&s_hup
, SIGHUP
, a_sighup
, 0);
732 sigaction(SIGINT
, 0, &sa
);
733 if (sa
.sa_handler
!= SIG_IGN
)
734 sig_add(&s_int
, SIGINT
, a_sigdie
, 0);
737 /*----- That's all, folks -------------------------------------------------*/