diff options
author | Florian Obser <florian@cvs.openbsd.org> | 2017-04-10 13:35:43 +0000 |
---|---|---|
committer | Florian Obser <florian@cvs.openbsd.org> | 2017-04-10 13:35:43 +0000 |
commit | ea411818aa119d87ea17a05933a3013692b335df (patch) | |
tree | fa871376e099a257937ad7062dc0027613289ceb /usr.sbin/slaacctl | |
parent | 349979f58c9485846af6536f1f48fd1e542a2544 (diff) |
The canonical name for ctl programs is without 'd'.
Pointed out by Brad.
OK deraadt@
Diffstat (limited to 'usr.sbin/slaacctl')
-rw-r--r-- | usr.sbin/slaacctl/Makefile | 17 | ||||
-rw-r--r-- | usr.sbin/slaacctl/parser.c | 207 | ||||
-rw-r--r-- | usr.sbin/slaacctl/parser.h | 34 | ||||
-rw-r--r-- | usr.sbin/slaacctl/slaacctl.8 | 82 | ||||
-rw-r--r-- | usr.sbin/slaacctl/slaacctl.c | 273 |
5 files changed, 613 insertions, 0 deletions
diff --git a/usr.sbin/slaacctl/Makefile b/usr.sbin/slaacctl/Makefile new file mode 100644 index 00000000000..487819d2e45 --- /dev/null +++ b/usr.sbin/slaacctl/Makefile @@ -0,0 +1,17 @@ +# $OpenBSD: Makefile,v 1.1 2017/04/10 13:35:42 florian Exp $ + +PROG= slaacctl +SRCS= slaacctl.c parser.c + +MAN= slaacctl.8 + +CFLAGS+= -Wall +CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes +CFLAGS+= -Wmissing-declarations +CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual +CFLAGS+= -Wsign-compare +CFLAGS+= -I${.CURDIR} -I${.CURDIR}/../slaacd +LDADD= -lutil +DPADD= ${LIBUTIL} + +.include <bsd.prog.mk> diff --git a/usr.sbin/slaacctl/parser.c b/usr.sbin/slaacctl/parser.c new file mode 100644 index 00000000000..f4a1858cc87 --- /dev/null +++ b/usr.sbin/slaacctl/parser.c @@ -0,0 +1,207 @@ +/* $OpenBSD: parser.c,v 1.1 2017/04/10 13:35:42 florian Exp $ */ + +/* + * Copyright (c) 2004 Esben Norby <norby@openbsd.org> + * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <sys/queue.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> + +#include <net/if.h> +#include <netinet/in.h> +#include <netinet/if_ether.h> + +#include <err.h> +#include <errno.h> +#include <event.h> +#include <imsg.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "slaacd.h" +#include "parser.h" + +enum token_type { + NOTOKEN, + ENDTOKEN, + INTERFACENAME, + KEYWORD +}; + +struct token { + enum token_type type; + const char *keyword; + int value; + const struct token *next; +}; + +static const struct token t_main[]; +static const struct token t_log[]; +static const struct token t_show[]; +static const struct token t_show_interface[]; +static const struct token t_send[]; +static const struct token t_send_sol[]; + +static const struct token t_main[] = { + {KEYWORD, "show", SHOW, t_show}, + {KEYWORD, "log", NONE, t_log}, + {KEYWORD, "send", NONE, t_send}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_log[] = { + {KEYWORD, "verbose", LOG_VERBOSE, NULL}, + {KEYWORD, "brief", LOG_BRIEF, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_show[] = { + {KEYWORD, "interface", SHOW_INTERFACE, t_show_interface}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_send[] = { + {KEYWORD, "solicitation", SEND_SOLICITATION, t_send_sol}, + {ENDTOKEN, "", NONE, NULL} +}; +static const struct token t_send_sol[] = { + {INTERFACENAME, "", SEND_SOLICITATION, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_show_interface[] = { + {NOTOKEN, "", NONE, NULL}, + {INTERFACENAME, "", SHOW_INTERFACE, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token *match_token(const char *, const struct token *, + struct parse_result *); +static void show_valid_args(const struct token *); + +struct parse_result * +parse(int argc, char *argv[]) +{ + static struct parse_result res; + const struct token *table = t_main; + const struct token *match; + + memset(&res, 0, sizeof(res)); + + while (argc >= 0) { + if ((match = match_token(argv[0], table, &res)) == NULL) { + fprintf(stderr, "valid commands/args:\n"); + show_valid_args(table); + return (NULL); + } + + argc--; + argv++; + + if (match->type == NOTOKEN || match->next == NULL) + break; + + table = match->next; + } + + if (argc > 0) { + fprintf(stderr, "superfluous argument: %s\n", argv[0]); + return (NULL); + } + + return (&res); +} + +static const struct token * +match_token(const char *word, const struct token *table, + struct parse_result *res) +{ + u_int i, match; + const struct token *t = NULL; + + match = 0; + + for (i = 0; table[i].type != ENDTOKEN; i++) { + switch (table[i].type) { + case NOTOKEN: + if (word == NULL || strlen(word) == 0) { + match++; + t = &table[i]; + } + break; + case INTERFACENAME: + if (!match && word != NULL && strlen(word) > 0) { + if ((res->if_index = if_nametoindex(word)) == 0) + errx(1, "unknown interface"); + match++; + t = &table[i]; + if (t->value) + res->action = t->value; + } + break; + case KEYWORD: + if (word != NULL && strncmp(word, table[i].keyword, + strlen(word)) == 0) { + match++; + t = &table[i]; + if (t->value) + res->action = t->value; + } + break; + case ENDTOKEN: + break; + } + } + + if (match != 1) { + if (word == NULL) + fprintf(stderr, "missing argument:\n"); + else if (match > 1) + fprintf(stderr, "ambiguous argument: %s\n", word); + else if (match < 1) + fprintf(stderr, "unknown argument: %s\n", word); + return (NULL); + } + + return (t); +} + +static void +show_valid_args(const struct token *table) +{ + int i; + + for (i = 0; table[i].type != ENDTOKEN; i++) { + switch (table[i].type) { + case NOTOKEN: + fprintf(stderr, " <cr>\n"); + break; + case INTERFACENAME: + fprintf(stderr, " <interface>\n"); + break; + case KEYWORD: + fprintf(stderr, " %s\n", table[i].keyword); + break; + case ENDTOKEN: + break; + } + } +} diff --git a/usr.sbin/slaacctl/parser.h b/usr.sbin/slaacctl/parser.h new file mode 100644 index 00000000000..210b79fd3b3 --- /dev/null +++ b/usr.sbin/slaacctl/parser.h @@ -0,0 +1,34 @@ +/* $OpenBSD: parser.h,v 1.1 2017/04/10 13:35:42 florian Exp $ */ + +/* + * Copyright (c) 2004 Esben Norby <norby@openbsd.org> + * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +enum actions { + NONE, + LOG_VERBOSE, + LOG_BRIEF, + SHOW, + SHOW_INTERFACE, + SEND_SOLICITATION +}; + +struct parse_result { + enum actions action; + uint32_t if_index; +}; + +struct parse_result *parse(int, char *[]); diff --git a/usr.sbin/slaacctl/slaacctl.8 b/usr.sbin/slaacctl/slaacctl.8 new file mode 100644 index 00000000000..775bbc73d79 --- /dev/null +++ b/usr.sbin/slaacctl/slaacctl.8 @@ -0,0 +1,82 @@ +.\" $OpenBSD: slaacctl.8,v 1.1 2017/04/10 13:35:42 florian Exp $ +.\" +.\" Copyright (c) 2017 Florian Obser <florian@openbsd.org> +.\" Copyright (c) 2016 Kenneth R Westerback <kwesterback@gmail.com> +.\" Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org> +.\" +.\" Permission to use, copy, modify, and distribute this software for any +.\" purpose with or without fee is hereby granted, provided that the above +.\" copyright notice and this permission notice appear in all copies. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.\" +.Dd $Mdocdate: April 10 2017 $ +.Dt SLAACDCTL 8 +.Os +.Sh NAME +.Nm slaacdctl +.Nd control the slaacd daemon +.Sh SYNOPSIS +.Nm +.Op Fl s Ar socket +.Ar command +.Op Ar argument ... +.Sh DESCRIPTION +The +.Nm +program controls the +.Xr slaacd 8 +daemon. +.Pp +The following options are available: +.Bl -tag -width Ds +.It Fl s Ar socket +Use +.Ar socket +instead of the default +.Pa /var/run/slaacd.sock +to communicate with +.Xr slaacd 8 . +.El +.Pp +The following commands are available: +.Bl -tag -width Ds +.It Cm log brief +Disable verbose debug logging. +.It Cm log verbose +Enable verbose debug logging. +.It Cm send solicitation Ar interfacename +Send a router solicitation on interface +.Ar interfacename +.It Cm show interface Op Ar interfacename +Display status about network interfaces. +.Pp +If +.Ar interfacename +is specified only information relative to +.Ar interfacename +is shown. +.Pp +Otherwise information on all interfaces is shown. +.El +.Sh FILES +.Bl -tag -width "/var/run/slaacd.sockXX" -compact +.It Pa /var/run/slaacd.sock +.Ux Ns -domain +socket used for communication with +.Xr slaacd 8 . +.El +.Sh SEE ALSO +.Xr slaacd.conf 5 , +.Xr slaacd 8 +.Sh HISTORY +The +.Nm +program first appeared in +.Ox 6.2 . diff --git a/usr.sbin/slaacctl/slaacctl.c b/usr.sbin/slaacctl/slaacctl.c new file mode 100644 index 00000000000..ffbd8775cfb --- /dev/null +++ b/usr.sbin/slaacctl/slaacctl.c @@ -0,0 +1,273 @@ +/* $OpenBSD: slaacctl.c,v 1.1 2017/04/10 13:35:42 florian Exp $ */ + +/* + * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> + * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org> + * Copyright (c) 2003 Henning Brauer <henning@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <sys/queue.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <arpa/inet.h> + +#include <net/if.h> +#include <net/if_media.h> +#include <net/if_types.h> + +#include <netinet/in.h> +#include <netinet/if_ether.h> + +#include <err.h> +#include <errno.h> +#include <event.h> +#include <imsg.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "slaacd.h" +#include "frontend.h" +#include "parser.h" + +__dead void usage(void); +int show_interface_msg(struct imsg *); + +struct imsgbuf *ibuf; + +__dead void +usage(void) +{ + extern char *__progname; + + fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n", + __progname); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + struct sockaddr_un sun; + struct parse_result *res; + struct imsg imsg; + int ctl_sock; + int done = 0; + int n, verbose = 0; + int ch; + char *sockname; + + sockname = SLAACD_SOCKET; + while ((ch = getopt(argc, argv, "s:")) != -1) { + switch (ch) { + case 's': + sockname = optarg; + break; + default: + usage(); + } + } + argc -= optind; + argv += optind; + + /* Parse command line. */ + if ((res = parse(argc, argv)) == NULL) + exit(1); + + /* Connect to control socket. */ + if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) + err(1, "socket"); + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + + strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path)); + if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) + err(1, "connect: %s", sockname); + + if (pledge("stdio", NULL) == -1) + err(1, "pledge"); + + if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) + err(1, NULL); + imsg_init(ibuf, ctl_sock); + done = 0; + + /* Process user request. */ + switch (res->action) { + case LOG_VERBOSE: + verbose = 1; + /* FALLTHROUGH */ + case LOG_BRIEF: + imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1, + &verbose, sizeof(verbose)); + printf("logging request sent.\n"); + done = 1; + break; + case SHOW_INTERFACE: + imsg_compose(ibuf, IMSG_CTL_SHOW_INTERFACE_INFO, 0, 0, -1, + &res->if_index, sizeof(res->if_index)); + break; + case SEND_SOLICITATION: + imsg_compose(ibuf, IMSG_CTL_SEND_SOLICITATION, 0, 0, -1, + &res->if_index, sizeof(res->if_index)); + done = 1; + break; + default: + usage(); + } + + while (ibuf->w.queued) + if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) + err(1, "write error"); + + while (!done) { + if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) + errx(1, "imsg_read error"); + if (n == 0) + errx(1, "pipe closed"); + + while (!done) { + if ((n = imsg_get(ibuf, &imsg)) == -1) + errx(1, "imsg_get error"); + if (n == 0) + break; + + switch (res->action) { + case SHOW_INTERFACE: + done = show_interface_msg(&imsg); + break; + default: + break; + } + + imsg_free(&imsg); + } + } + close(ctl_sock); + free(ibuf); + + return (0); +} + +int +show_interface_msg(struct imsg *imsg) +{ + static int if_count = 0; + struct ctl_engine_info *cei; + struct ctl_engine_info_ra *cei_ra; + struct ctl_engine_info_ra_prefix *cei_ra_prefix; + struct ctl_engine_info_ra_rdns *cei_ra_rdns; + struct ctl_engine_info_ra_dnssl *cei_ra_dnssl; + struct tm *t; + struct timespec now, diff; + char buf[IF_NAMESIZE], *bufp; + char hbuf[NI_MAXHOST], whenbuf[255]; + char ntopbuf[INET6_ADDRSTRLEN]; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_INTERFACE_INFO: + cei = imsg->data; + + if (if_count++ > 0) + printf("\n"); + + bufp = if_indextoname(cei->if_index, buf); + printf("%s:\n", bufp != NULL ? bufp : "unknown"); + printf("\t index: %3u ", cei->if_index); + printf("running: %3s ", cei->running ? "yes" : "no"); + printf("privacy: %3s\n", cei->autoconfprivacy ? "yes" : "no"); + printf("\tlladdr: %s\n", ether_ntoa(&cei->hw_address)); + if (getnameinfo((struct sockaddr *)&cei->ll_address, + cei->ll_address.sin6_len, hbuf, sizeof(hbuf), NULL, 0, + NI_NUMERICHOST | NI_NUMERICSERV)) + err(1, "cannot get link local address"); + printf("\t inet6: %s\n", hbuf); + break; + case IMSG_CTL_SHOW_INTERFACE_INFO_RA: + cei_ra = imsg->data; + + if (getnameinfo((struct sockaddr *)&cei_ra->from, + cei_ra->from.sin6_len, hbuf, sizeof(hbuf), NULL, 0, + NI_NUMERICHOST | NI_NUMERICSERV)) + err(1, "cannot get router IP"); + + if (clock_gettime(CLOCK_MONOTONIC, &now)) + err(1, "clock_gettime"); + + timespecsub(&now, &cei_ra->uptime, &diff); + + t = localtime(&cei_ra->when.tv_sec); + strftime(whenbuf, sizeof(whenbuf), "%F %T", t); + printf("\tRouter Advertisement from %s\n", hbuf); + printf("\t\treceived: %s.%09ld; %lld.%09lds ago\n", + whenbuf, cei_ra->when.tv_nsec, diff.tv_sec, diff.tv_nsec); + printf("\t\tCur Hop Limit: %3u, M: %d, O: %d, Router Lifetime:" + " %5us\n", cei_ra->curhoplimit, cei_ra->managed ? 1: 0, + cei_ra->other ? 1 : 0, cei_ra->router_lifetime); + printf("\t\tReachable Time: %9ums, Retrans Timer: %9ums\n", + cei_ra->reachable_time, cei_ra->retrans_time); + break; + case IMSG_CTL_SHOW_INTERFACE_INFO_RA_PREFIX: + cei_ra_prefix = imsg->data; + printf("\t\tprefix: %s/%hhu\n", inet_ntop(AF_INET6, + &cei_ra_prefix->prefix, ntopbuf, INET6_ADDRSTRLEN), + cei_ra_prefix->prefix_len); + printf("\t\t\tOn-link: %d, Autonomous address-configuration: %d" + "\n", cei_ra_prefix->onlink ? 1 : 0, + cei_ra_prefix->autonomous ? 1 : 0); + printf("\t\t\tvltime: %9u, pltime: %9u\n", + cei_ra_prefix->vltime, cei_ra_prefix->pltime); + if (getnameinfo((struct sockaddr *)&cei_ra_prefix->addr, + cei_ra_prefix->addr.sin6_len, hbuf, sizeof(hbuf), NULL, 0, + NI_NUMERICHOST | NI_NUMERICSERV)) + err(1, "cannot generated address"); + printf("\t\t\t address: %s\n", hbuf); + if (!IN6_IS_ADDR_UNSPECIFIED( + &cei_ra_prefix->priv_addr.sin6_addr)) { + if (getnameinfo((struct sockaddr *) + &cei_ra_prefix->priv_addr, + cei_ra_prefix->priv_addr.sin6_len, hbuf, + sizeof(hbuf), NULL, 0, NI_NUMERICHOST | + NI_NUMERICSERV)) + err(1, "cannot generated address"); + printf("\t\t\tprivacy address: %s\n", hbuf); + } + printf("\t\t\t mask: %s\n", inet_ntop(AF_INET6, + &cei_ra_prefix->mask, ntopbuf, INET6_ADDRSTRLEN)); + break; + case IMSG_CTL_SHOW_INTERFACE_INFO_RA_RDNS: + cei_ra_rdns = imsg->data; + printf("\t\trdns: %s, lifetime: %u\n", inet_ntop(AF_INET6, + &cei_ra_rdns->rdns, ntopbuf, INET6_ADDRSTRLEN), + cei_ra_rdns->lifetime); + break; + case IMSG_CTL_SHOW_INTERFACE_INFO_RA_DNSSL: + cei_ra_dnssl = imsg->data; + printf("\t\tsearch: %s, lifetime: %u\n", cei_ra_dnssl->dnssl, + cei_ra_dnssl->lifetime); + break; + case IMSG_CTL_END: + printf("\n"); + return (1); + default: + break; + } + + return (0); +} |