commit 6c98745568f382c8a563faf0213ab1f03189d951 from: Tracey Emery date: Wed Apr 17 15:10:39 2019 UTC Plumbing finally in place for imsg. A few leak fixes and logging tightening. commit - d787e10cbbf4150651da83a2051f93dec8895afa commit + 6c98745568f382c8a563faf0213ab1f03189d951 blob - 14e9e5885f37df972b18488564014fe3d5b4acd4 blob + dd84c43f77625c298ccc9d44d5790cefc0ac3504 --- Makefile +++ Makefile @@ -1,4 +1,3 @@ -#SUBDIR = thingsd thingsctl -SUBDIR = thingsd +SUBDIR = thingsd thingsctl .include blob - 1dae85c27d3382ffac3cef59504c7a8af8040783 blob + 23d30b4f3482f22eb2b469acddfff46ee746c308 --- thingsctl/Makefile +++ thingsctl/Makefile @@ -1,6 +1,6 @@ PROG= thingsctl -SRCS+= thingsctl.c +SRCS+= thingsctl.c parser.c INC_C_DIR= /usr/local BINDIR?= ${INC_C_DIR}/sbin @@ -8,14 +8,14 @@ MANDIR?= ${INC_C_DIR}/man/man DEBUG= -g -DDEBUG=3 -O0 +LDADD= -lutil CFLAGS+= -Wall -I${.CURDIR} CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes CFLAGS+= -Wmissing-declarations CFLAGS+= -Wshadow -Wpointer-arith CFLAGS+= -Wsign-compare -pthread +CFLAGS+= -I${.CURDIR} -I${.CURDIR}/../thingsd -CLEANFILES+= y.tab.h - MAN= thingsctl.8 .include blob - 6cd0f872e8b251c2243065f7bb1e1860f2fc8e6e blob + e63d8f3427c115c8c279a6ad054f72b47a4363e6 --- thingsctl/thingsctl.8 +++ thingsctl/thingsctl.8 @@ -14,20 +14,62 @@ .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: January 26 2019 $ -.Dt THINGSCTL 1 +.Dd $Mdocdate: April 17 2019 $ +.Dt THINGSCTL 8 .Os .Sh NAME .Nm thingsctl -.Nd thingsd control utility +.Nd control the thingsd daemon .Sh SYNOPSIS -.Nm thingsctl -.Op Fl sv -.Op Fl d Ar host : Ns Ar port -.Op Fl f Ar thing -.Op Ar command args ... +.Nm +.Op Fl s Ar socket +.Ar command +.Op Ar argument ... .Sh DESCRIPTION The .Nm -program is a control utility for the thingsd proxy daemon. +program controls the +.Xr thingsd 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/thingsd.sock +to communicate with +.Xr thingsd 8 . +.El +.Pp +The following commands are available: +.Bl -tag -width Ds +.It Cm log brief +Disable verbose logging. +.It Cm log verbose +Enable verbose logging. +.It Cm log debug +Enable very noisy debug logging. +.It Cm list clients +Show all clients connected to things. +.It Cm list things +Show all things. This will not list non-persistent things. +.It Cm list sockets +Show a summary of all sockets being used by the thingsd daemon. +.El +.Sh FILES +.Bl -tag -width "/var/run/thingsd.sockXX" -compact +.It Pa /var/run/thingsd.sock +.Ux Ns -domain +socket used for communication with +.Xr thingsd 8 . +.El +.Sh SEE ALSO +.Xr thingsd.conf 5 , +.Xr thingsd 8 +.Sh HISTORY +The +.Nm +program first appeared in +.Ox 6.5 . blob - /dev/null blob + ff61bf4c6d6317f427a08c26920d1842b186ebcc (mode 644) --- /dev/null +++ thingsctl/parser.c @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2004 Esben Norby + * Copyright (c) 2003, 2004 Henning Brauer + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "thingsd.h" +#include "parser.h" + +enum token_type { + NOTOKEN, + ENDTOKEN, + 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_list[]; + +static const struct token t_main[] = { + {KEYWORD, "list", NONE, t_list}, + {KEYWORD, "log", NONE, t_log}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_log[] = { + {KEYWORD, "debug", LOG_DEBUG, NULL}, + {KEYWORD, "verbose", LOG_VERBOSE, NULL}, + {KEYWORD, "brief", LOG_BRIEF, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_list[] = { + {KEYWORD, "clients", LIST_CLTS, NULL}, + {KEYWORD, "things", LIST_THGS, NULL}, + {KEYWORD, "sockets", LIST_SOCKS, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +/* static const struct token t_list[] = { */ +/* {NOTOKEN, "", NONE, NULL}, */ +/* {ENDTOKEN, "", LIST, 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 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, " \n"); + break; + case KEYWORD: + fprintf(stderr, " %s\n", table[i].keyword); + break; + case ENDTOKEN: + break; + } + } +} blob - e53212ee333bc156aa10c4d21a120d3acd5e17a9 blob + 39b9fcb6a7ea1497aedc11da572c9cc3290ddfbc --- thingsctl/thingsctl.c +++ thingsctl/thingsctl.c @@ -1,5 +1,7 @@ /* - * Copyright (c) 2016-2019 Tracey Emery + * Copyright (c) 2005 Claudio Jeker + * Copyright (c) 2004, 2005 Esben Norby + * Copyright (c) 2003 Henning Brauer * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,12 +16,282 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include #include +#include +#include -int main(int, char *[]); +#include "thingsd.h" +#include "parser.h" +__dead void usage(void); +int show_list_msg(struct imsg *); +void print_clts(struct clt_imsg *); +void print_thgs(struct thg_imsg *); +void print_socks(struct sock_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[]) { - return EXIT_SUCCESS; + struct sockaddr_un sun; + struct parse_result *res; + struct imsg imsg; + int ctl_sock; + int done = 0; + int n, verbose = 0; + int ch; + int type; + char *sockname; + + sockname = THINGSD_SOCK; + 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; + + /* Check for root-only actions */ + switch (res->action) { + case LOG_DEBUG: + case LOG_VERBOSE: + case LOG_BRIEF: + if (geteuid() != 0) + errx(1, "need root privileges"); + break; + default: + break; + } + + /* Process user request. */ + switch (res->action) { + case LOG_DEBUG: + verbose++; + /* FALLTHROUGH */ + case LOG_VERBOSE: + verbose++; + /* 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 LIST_CLTS: + type = CTL_LIST_CLTS; + imsg_compose(ibuf, IMSG_CTL_LIST, 0, 0, -1, &type, + sizeof(type)); + break; + case LIST_THGS: + type = CTL_LIST_THGS; + imsg_compose(ibuf, IMSG_CTL_LIST, 0, 0, -1, &type, + sizeof(type)); + break; + case LIST_SOCKS: + type = CTL_LIST_SOCKS; + imsg_compose(ibuf, IMSG_CTL_LIST, 0, 0, -1, &type, + sizeof(type)); + 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 LIST_CLTS: + case LIST_THGS: + case LIST_SOCKS: + done = show_list_msg(&imsg); + break; + default: + break; + } + imsg_free(&imsg); + } + } + close(ctl_sock); + free(ibuf); + + return (0); } + +int +show_list_msg(struct imsg *imsg) +{ + struct clt_imsg *comp_clt; + struct thg_imsg *comp_thg; + struct sock_imsg *comp_sock; + + switch (imsg->hdr.type) { + case IMSG_LIST_CLTS: + comp_clt = (struct clt_imsg *)imsg->data; + print_clts(comp_clt); + break; + case IMSG_LIST_THGS: + comp_thg = (struct thg_imsg *)imsg->data; + print_thgs(comp_thg); + break; + case IMSG_LIST_SOCKS: + comp_sock = (struct sock_imsg *)imsg->data; + print_socks(comp_sock); + break; + case IMSG_CTL_END: + return (1); + default: + break; + } + return (0); +} + +void +print_clts(struct clt_imsg *pclts) +{ + if (pclts->subscribed == false) + return; + + printf("Client Name:\t\t\t%s\n", pclts->name); + + printf("\tfd:\t\t\t%d\n", pclts->fd); + printf("\tPort:\t\t\t%d\n", pclts->port); + if (pclts->tls == true) + printf("\tTLS:\t\t\tyes\n"); + printf("\tSubscriptions:\t\t%zu\n", pclts->subs); +} + +void +print_thgs(struct thg_imsg *pthgs) +{ + if (pthgs->exists == false) + return; + + printf("Thing Name:\t\t\t%s\n", pthgs->name); + + switch(pthgs->type) { + case TCP: + printf("\tIP Addr:\t\t%s\n", pthgs->ipaddr); + printf("\tConnect Port:\t\t%d\n", pthgs->conn_port); + printf("\tPersists:\t\t%d\n", pthgs->persist); + break; + case UDP: + printf("\tUDP Listener:\t\t%s\n", pthgs->udp); + printf("\tConnect Port:\t\t%d\n", pthgs->conn_port); + break; + case DEV: + printf("\tDevice:\t\t\t%s\n", pthgs->location); + printf("\tBaud:\t\t\t%d\n", pthgs->baud); + printf("\tData:\t\t\t%d\n", pthgs->data_bits); + printf("\tStop:\t\t\t%d\n", pthgs->stop_bits); + printf("\tHardware:\t\t%d\n", pthgs->hw_ctl); + printf("\tSoftware:\t\t%d\n", pthgs->sw_ctl); + printf("\tParity:\t\t\t%s\n", pthgs->parity); + break; + } + if (strncmp(pthgs->iface, "", BUFF) == 0) + printf("\tBind Interface:\t\tall\n"); + else + printf("\tBind Interface:\t\t%s\n", pthgs->iface); + printf("\tListen Port:\t\t%d\n", pthgs->port); + if (pthgs->max_clt == 0) + printf("\tMax Clients:\t\tunlimited\n"); + else + printf("\tMax Clients:\t\t%d\n", pthgs->max_clt); + if (geteuid() != 0) + printf("\tPassword:\t\t********\n"); + else + printf("\tPassword:\t\t%s\n", pthgs->password); + printf("\tClient Count:\t\t%zu\n", pthgs->clt_cnt); + if (pthgs->tls == false) + return; + printf("\tTLS:\t\t\t%d\n", pthgs->tls); + printf("\tCert:\t\t\t%s\n", pthgs->tls_cert_file); + printf("\tKey:\t\t\t%s\n", pthgs->tls_key_file); + printf("\tCA:\t\t\t%s\n", pthgs->tls_ca_file); + printf("\tCRL:\t\t\t%s\n", pthgs->tls_crl_file); + printf("\tOCSP:\t\t\t%s\n", pthgs->tls_ocsp_staple_file); +} + +void +print_socks(struct sock_imsg *psocks) +{ + if (strcmp(psocks->name, "") == 0) + printf("Socket Name:\t\t\tReceive socket\n"); + else + printf("Socket Name:\t\t\t%s\n", psocks->name); + printf("\tfd:\t\t\t%d\n", psocks->fd); + printf("\tPort:\t\t\t%d\n", psocks->port); + if (psocks->tls == true) + printf("\tTLS:\t\t\tyes\n\n"); + printf("\tClient Count:\t\t%zu\n", psocks->clt_cnt); + if (psocks->max_clts == 0) + printf("\tMax Clients:\t\tunlimited\n"); + else + printf("\tMax Clients:\t\t%zu\n", psocks->max_clts); +} blob - /dev/null blob + ba5487449ddaec55930a96cb6532c9f582a13111 (mode 644) --- /dev/null +++ thingsctl/parser.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2004 Esben Norby + * Copyright (c) 2003, 2004 Henning Brauer + * + * 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_DEBUG, + LOG_VERBOSE, + LOG_BRIEF, + LIST_CLTS, + LIST_THGS, + LIST_SOCKS, +}; + +struct parse_result { + enum actions action; +}; + +struct parse_result *parse(int, char *[]); blob - 044c23967594d24108770faa501ff2dbc33e411c blob + e3f22a628d18b0b02294125a87602076bebddbeb --- thingsd/Makefile +++ thingsd/Makefile @@ -1,7 +1,8 @@ PROG= thingsd SRCS= parse.y -SRCS+= log.c serial.c sockets.c client.c events.c tls.c +SRCS+= log.c serial.c sockets.c client.c events.c tls.c control.c +SRCS+= ctl.c SRCS+= thingsd.c blob - c766554f0f8749d09b579e9a5d4708e1e4a00fd0 blob + 46fea3c02773e70d25df53948ad60eca18459954 --- thingsd/client.c +++ thingsd/client.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 Tracey Emery + * Copyright (c) 2016, 2019 Tracey Emery * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -28,10 +29,10 @@ #include "thingsd.h" -extern void bufferevent_read_pressure_cb(struct evbuffer *, size_t, - size_t, void *); +extern void bufferevent_read_pressure_cb(struct evbuffer *, size_t, + size_t, void *); - void +void clt_conn(int fd, short event, void *arg) { struct sockaddr_storage ss; @@ -43,7 +44,7 @@ clt_conn(int fd, short event, void *arg) if ((clt_fd = accept4(fd, (struct sockaddr *)&ss, &len, SOCK_NONBLOCK)) == -1) { - log_info("clt accept failed"); + log_warnx("clt accept failed"); return; } if ((clt = calloc(1, sizeof(*clt))) == NULL) @@ -55,7 +56,7 @@ clt_conn(int fd, short event, void *arg) if (sock->tls) { if (tls_accept_socket(sock->tls_ctx, &clt->tls_ctx, clt_fd) == -1) { - log_info("tls accept failed: %s", + log_warnx("tls accept failed: %s", tls_error(sock->tls_ctx)); goto err; } @@ -67,9 +68,10 @@ clt_conn(int fd, short event, void *arg) sock->clt_cnt++; pthgsd->clt_cnt++; if (sock->max_clts > 0 && sock->clt_cnt > sock->max_clts) { - log_info("%s max clients reached", sock->name); + log_debug("%s: %s max clients reached", __func__, sock->name); sock->clt_cnt--; pthgsd->clt_cnt--; + free(clt->sub_names); goto err; } clt->port = sock->port; @@ -79,6 +81,7 @@ clt_conn(int fd, short event, void *arg) if (clt->evb == NULL) { sock->clt_cnt--; pthgsd->clt_cnt--; + free(clt->sub_names); goto err; } clt->fd = clt_fd; @@ -88,23 +91,25 @@ clt_conn(int fd, short event, void *arg) clt->tls = true; event_del(clt->ev); event_set(clt->ev, clt->fd, EV_READ|EV_PERSIST, - sock_tls_handshake, pthgsd); - if (event_add(clt->ev, NULL)) + sock_tls_handshake, pthgsd); + if (event_add(clt->ev, NULL)) { + free(clt->sub_names); goto err; + } return; } clt_add(pthgsd, clt); return; err: - log_info("client error"); + log_debug("%s: client error", __func__); if (clt_fd != -1) { close(clt_fd); if (sock->tls) { tls_free(clt->tls_ctx); free(clt->ev); } - free(clt); } + free(clt); } void @@ -115,7 +120,7 @@ clt_add(struct thgsd *pthgsd, struct clt *pclt) evbuffercb cltrd = clt_rd; evbuffercb cltwr = clt_wr; - log_info("client connected"); + log_debug("%s: client connected, %d", __func__, clt->fd); clt->bev = bufferevent_new(clt->fd, cltrd, cltwr, clt_err, pthgsd); if (clt->bev == NULL) { sock->clt_cnt--; @@ -134,15 +139,15 @@ clt_add(struct thgsd *pthgsd, struct clt *pclt) start_clt_chk(pthgsd); return; err: - log_info("client error"); + log_debug("%s: client error", __func__); if (clt->fd != -1) { close(clt->fd); if (sock->tls) { tls_free(clt->tls_ctx); free(clt->ev); } - free(clt); } + free(clt); } void @@ -167,7 +172,7 @@ clt_del(struct thgsd *pthgsd, struct clt *pclt) pthgsd->clt_cnt--; clt->sock->clt_cnt--; if (clt->subscribed == false) - log_info("client disconnected"); + log_debug("%s: client disconnected", __func__); else log_info("client disconnected: %s", clt->name); TAILQ_REMOVE(&pthgsd->clts, clt, entry); @@ -298,7 +303,8 @@ clt_wr_thgs(struct clt *clt, struct thg *thg, size_t l if (thg->persist == false) { if ((thg->fd = open_clt_sock(thg->ipaddr, thg->conn_port)) == -1) { - log_info("temporary ipaddr connection failed"); + log_warnx("%s: temporary ipaddr connection" + " failed", __func__); return; } evbuffer_remove(clt->evb, pkt, len); @@ -324,6 +330,7 @@ clt_wr_thgs(struct clt *clt, struct thg *thg, size_t l void clt_wr(struct bufferevent *bev, void *arg) { + /* nothing here */ } void @@ -377,7 +384,7 @@ clt_err(struct bufferevent *bev, short error, void *ar int fd = bev->ev_read.ev_fd; if ((error & EVBUFFER_EOF) == 0) - log_info("client socket error, disconnecting"); + log_warnx("%s: client socket error, disconnecting", __func__); /* client disconnect */ TAILQ_FOREACH(clt, &pthgsd->clts, entry) { if (clt->fd == fd) { @@ -422,7 +429,7 @@ start_clt_chk(struct thgsd *pthgsd) rclt_chk = pthread_create(&tclt_chk, NULL, clt_chk, (void *)pthgsd); if (rclt_chk) { - log_info("thread creation failed"); + log_warnx("%s: thread creation failed", __func__); clt_chk((void *) pthgsd); } } blob - /dev/null blob + d8caa1ca067fd66ff7e9e9a127c0c47fb2e4a337 (mode 644) --- /dev/null +++ thingsd/control.c @@ -0,0 +1,282 @@ +/* + * Copyright (c) 2003, 2004 Henning Brauer + * + * 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 +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "ctl.h" +#include "thingsd.h" +#include "control.h" + +#define CONTROL_BACKLOG 5 + +struct ctl_conn *control_connbyfd(int); +struct ctl_conn *control_connbypid(pid_t); +void control_close(int); + +int +control_init(char *path) +{ + struct sockaddr_un sun; + int fd; + mode_t old_umask; + + if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + 0)) == -1) { + log_warn("%s: socket", __func__); + return (-1); + } + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); + + if (unlink(path) == -1) + if (errno != ENOENT) { + log_warn("%s: unlink %s", __func__, path); + close(fd); + return (-1); + } + + old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); + if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { + log_warn("%s: bind: %s", __func__, path); + close(fd); + umask(old_umask); + return (-1); + } + umask(old_umask); + + if (chmod(path, + S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) == -1) { + log_warn("%s: chmod", __func__); + close(fd); + (void)unlink(path); + return (-1); + } + + return (fd); +} + +int +control_listen(void) +{ + if (listen(control_state.fd, CONTROL_BACKLOG) == -1) { + log_warn("%s: listen", __func__); + return (-1); + } + + event_set(&control_state.ev, control_state.fd, EV_READ, + control_accept, NULL); + event_add(&control_state.ev, NULL); + evtimer_set(&control_state.evt, control_accept, NULL); + + return (0); +} + +void +control_accept(int listenfd, short event, void *bula) +{ + int connfd; + socklen_t len; + struct sockaddr_un sun; + struct ctl_conn *c; + + event_add(&control_state.ev, NULL); + if ((event & EV_TIMEOUT)) + return; + len = sizeof(sun); + if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len, + SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) { + /* + * Pause accept if we are out of file descriptors, or + * libevent will haunt us here too. + */ + if (errno == ENFILE || errno == EMFILE) { + struct timeval evtpause = { 1, 0 }; + + event_del(&control_state.ev); + evtimer_add(&control_state.evt, &evtpause); + } else if (errno != EWOULDBLOCK && errno != EINTR && + errno != ECONNABORTED) + log_warn("%s: accept4", __func__); + return; + } + + if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { + log_warn("%s: calloc", __func__); + close(connfd); + return; + } + + imsg_init(&c->iev.ibuf, connfd); + c->iev.handler = control_dispatch_imsg; + c->iev.events = EV_READ; + event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, c->iev.handler, + &c->iev); + event_add(&c->iev.ev, NULL); + + TAILQ_INSERT_TAIL(&ctl_conns, c, entry); +} + +struct ctl_conn * +control_connbyfd(int fd) +{ + struct ctl_conn *c; + + TAILQ_FOREACH(c, &ctl_conns, entry) { + if (c->iev.ibuf.fd == fd) + break; + } + + return (c); +} + +struct ctl_conn * +control_connbypid(pid_t pid) +{ + struct ctl_conn *c; + + TAILQ_FOREACH(c, &ctl_conns, entry) { + if (c->iev.ibuf.pid == pid) + break; + } + + return (c); +} + +void +control_close(int fd) +{ + struct ctl_conn *c; + + if ((c = control_connbyfd(fd)) == NULL) { + log_warnx("%s: fd %d: not found", __func__, fd); + return; + } + + msgbuf_clear(&c->iev.ibuf.w); + TAILQ_REMOVE(&ctl_conns, c, entry); + + event_del(&c->iev.ev); + close(c->iev.ibuf.fd); + + /* Some file descriptors are available again. */ + if (evtimer_pending(&control_state.evt, NULL)) { + evtimer_del(&control_state.evt); + event_add(&control_state.ev, NULL); + } + + free(c); +} + +void +control_dispatch_imsg(int fd, short event, void *bula) +{ + struct ctl_conn *c; + struct imsg imsg; + ssize_t n; + uid_t euid; + gid_t egid; + int verbose; + + if ((c = control_connbyfd(fd)) == NULL) { + log_warnx("%s: fd %d: not found", __func__, fd); + return; + } + + if (event & EV_READ) { + if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) || + n == 0) { + control_close(fd); + return; + } + } + if (event & EV_WRITE) { + if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) { + control_close(fd); + return; + } + } + + if (getpeereid(fd, &euid, &egid) == -1) { + control_close(fd); + return; + } + + for (;;) { + if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { + control_close(fd); + return; + } + if (n == 0) + break; + + switch (imsg.hdr.type) { + default: + break; + } + + switch (imsg.hdr.type) { + case IMSG_CTL_LIST: + if (IMSG_DATA_SIZE(imsg) != + sizeof(enum ctl_list_type)) + break; + ctl_imsg_compose_main(imsg.hdr.type, 0, + imsg.data, IMSG_DATA_SIZE(imsg)); + break; + case IMSG_CTL_LOG_VERBOSE: + if (imsg.hdr.len != IMSG_HEADER_SIZE + + sizeof(verbose)) + break; + /* forward to other process */ + ctl_imsg_compose_main(imsg.hdr.type, 0, + imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + break; + default: + log_debug("%s: error handling imsg %d", __func__, + imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + + imsg_event_add(&c->iev); +} + +int +control_imsg_relay(struct imsg *imsg) +{ + struct ctl_conn *c; + + if ((c = control_connbypid(imsg->hdr.pid)) == NULL) + return (0); + + return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid, + -1, imsg->data, IMSG_DATA_SIZE(*imsg))); +} blob - /dev/null blob + af4a2ab3d598696eca7b47f898f6a99045657fa7 (mode 644) --- /dev/null +++ thingsd/control.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2003, 2004 Henning Brauer + * + * 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 "log.h" + +extern int main_imsg_compose_ctl(int, pid_t, void *, + uint16_t); + +struct { + struct event ev; + struct event evt; + int fd; +} control_state; + +struct ctl_conn { + TAILQ_ENTRY(ctl_conn) entry; + struct imsgev iev; +}; + +int control_init(char *); +int control_listen(void); +void control_accept(int, short, void *); +void control_dispatch_imsg(int, short, void *); +int control_imsg_relay(struct imsg *); blob - 0fe9a6e76a8d522e8640b71aa77b8ff5bb86e12a blob + 876a08519e2c4b5305cf038b62db49331a4a7806 --- thingsd/log.c +++ thingsd/log.c @@ -22,35 +22,35 @@ #include #include -static int debug; -static int verbose; -const char *log_procname; +static int debug; +static int verbose; +const char *log_procname; -void log_init(int, int); -void log_procinit(const char *); -void log_setverbose(int); -int log_getverbose(void); -void log_warn(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_warnx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_info(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_debug(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void logit(int, const char *, ...) - __attribute__((__format__ (printf, 2, 3))); -void vlog(int, const char *, va_list) - __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -__dead void fatalx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); +void log_init(int, int); +void log_procinit(const char *); +void log_setverbose(int); +int log_getverbose(void); +void log_warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_info(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_debug(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void logit(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +void vlog(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); void log_init(int n_debug, int facility) { - extern char *__progname; + extern char *__progname; debug = n_debug; verbose = n_debug; @@ -84,7 +84,7 @@ log_getverbose(void) void logit(int pri, const char *fmt, ...) { - va_list ap; + va_list ap; va_start(ap, fmt); vlog(pri, fmt, ap); @@ -94,8 +94,8 @@ logit(int pri, const char *fmt, ...) void vlog(int pri, const char *fmt, va_list ap) { - char *nfmt; - int saved_errno = errno; + char *nfmt; + int saved_errno = errno; if (debug) { /* best effort in out of mem situations */ @@ -116,9 +116,9 @@ vlog(int pri, const char *fmt, va_list ap) void log_warn(const char *emsg, ...) { - char *nfmt; - va_list ap; - int saved_errno = errno; + char *nfmt; + va_list ap; + int saved_errno = errno; /* best effort to even work in out of memory situations */ if (emsg == NULL) @@ -144,7 +144,7 @@ log_warn(const char *emsg, ...) void log_warnx(const char *emsg, ...) { - va_list ap; + va_list ap; va_start(ap, emsg); vlog(LOG_ERR, emsg, ap); @@ -154,7 +154,7 @@ log_warnx(const char *emsg, ...) void log_info(const char *emsg, ...) { - va_list ap; + va_list ap; va_start(ap, emsg); vlog(LOG_INFO, emsg, ap); @@ -164,7 +164,7 @@ log_info(const char *emsg, ...) void log_debug(const char *emsg, ...) { - va_list ap; + va_list ap; if (verbose > 1) { va_start(ap, emsg); @@ -176,8 +176,8 @@ log_debug(const char *emsg, ...) static void vfatalc(int code, const char *emsg, va_list ap) { - static char s[BUFSIZ]; - const char *sep; + static char s[BUFSIZ]; + const char *sep; if (emsg != NULL) { (void)vsnprintf(s, sizeof(s), emsg, ap); @@ -196,7 +196,7 @@ vfatalc(int code, const char *emsg, va_list ap) void fatal(const char *emsg, ...) { - va_list ap; + va_list ap; va_start(ap, emsg); vfatalc(errno, emsg, ap); @@ -207,7 +207,7 @@ fatal(const char *emsg, ...) void fatalx(const char *emsg, ...) { - va_list ap; + va_list ap; va_start(ap, emsg); vfatalc(0, emsg, ap); blob - /dev/null blob + e34a116d08ee3c6143c31f88c6799c0bb7d19df5 (mode 644) --- /dev/null +++ thingsd/ctl.c @@ -0,0 +1,197 @@ +/* + * Copyright (c) 2016, 2019 Tracey Emery + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +#include "thingsd.h" +#include "ctl.h" +#include "control.h" + +extern struct thgsd *pthgsd; +extern struct dthgs *pdthgs; +struct imsgev *iev_main; + +void +ctl_sighdlr(int sig, short event, void *bula) +{ + struct dthgs *zdthgs = (struct dthgs *)bula; + + switch (sig) { + case SIGINT: + case SIGTERM: + case SIGQUIT: + ctl_shutdown(zdthgs); + default: + fatalx("unexpected signal"); + } +} + +void +thgs_ctl(int debug, int verbose, char *ctl_sock) +{ + struct passwd *pw; + int ctl_fd; + + log_init(debug, LOG_DAEMON); + log_setverbose(verbose); + + thgsd_process = PROC_CTL; + setproctitle("%s", log_procnames[thgsd_process]); + log_procinit(log_procnames[thgsd_process]); + + if ((ctl_fd = control_init(ctl_sock)) == -1) + fatalx("ctl_sock failed"); + control_state.fd = ctl_fd; + if ((pw = getpwnam(TH_USER)) != NULL) { + if(setuid(pthgsd->pw->pw_uid) != 0) + err(1, "unable to set user id of %s: %s", TH_USER, + strerror(errno)); + else + log_info("running as root. %s user not found", TH_USER); + } + + if (pledge("stdio unix recvfd", NULL) == -1) + fatal("pledge"); + + pthgsd->ctl_eb = event_init(); + + signal_set(&pthgsd->ctl_evsigquit, SIGQUIT, ctl_sighdlr, pdthgs); + signal_set(&pthgsd->ctl_evsigterm, SIGTERM, ctl_sighdlr, pdthgs); + signal_set(&pthgsd->ctl_evsigint, SIGINT, ctl_sighdlr, pdthgs); + + signal_add(&pthgsd->ctl_evsigquit, NULL); + signal_add(&pthgsd->ctl_evsigterm, NULL); + signal_add(&pthgsd->ctl_evsigint, NULL); + signal(SIGPIPE, SIG_IGN); + signal(SIGHUP, SIG_IGN); + + /* Setup pipe and event handler to the parent process. */ + if ((iev_main = malloc(sizeof(struct imsgev))) == NULL) + fatal(NULL); + imsg_init(&iev_main->ibuf, 3); + iev_main->handler = ctl_dispatch_main; + iev_main->events = EV_READ; + event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events, + iev_main->handler, iev_main); + event_add(&iev_main->ev, NULL); + + TAILQ_INIT(&ctl_conns); + control_listen(); + event_dispatch(); +} + +void +imsg_event_add(struct imsgev *iev) +{ + iev->events = EV_READ; + if (iev->ibuf.w.queued) + iev->events |= EV_WRITE; + + event_del(&iev->ev); + event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); + event_add(&iev->ev, NULL); +} + +int +imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid, + pid_t pid, int fd, void *data, uint16_t datalen) +{ + int ret; + + if ((ret = imsg_compose(&iev->ibuf, type, peerid, + pid, fd, data, datalen)) != -1) + imsg_event_add(iev); + return (ret); +} + +void +ctl_dispatch_main(int fd, short event, void *bula) +{ + struct imsg imsg; + struct imsgev *iev = bula; + struct imsgbuf *ibuf = &iev->ibuf; + int n, shut = 0, verbose; + + if (event & EV_READ) { + if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) + fatal("imsg_read error"); + if (n == 0) /* Connection closed. */ + shut = 1; + } + if (event & EV_WRITE) { + if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) + fatal("msgbuf_write"); + if (n == 0) /* Connection closed. */ + shut = 1; + } + + for (;;) { + if ((n = imsg_get(ibuf, &imsg)) == -1) + fatal("imsg_get error"); + if (n == 0) /* No more messages. */ + break; + + switch (imsg.hdr.type) { + case IMSG_LIST_CLTS: + case IMSG_LIST_THGS: + case IMSG_LIST_SOCKS: + case IMSG_CTL_END: + control_imsg_relay(&imsg); + break; + case IMSG_CTL_LOG_VERBOSE: + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + break; + default: + log_debug("%s: error handling imsg %d", __func__, + imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + if (!shut) + imsg_event_add(iev); + else { + /* This pipe is dead. Remove its event handler. */ + event_del(&iev->ev); + event_loopexit(NULL); + } +} + +void +ctl_shutdown(struct dthgs *zdthgs) +{ + /* Close pipes. */ + msgbuf_write(&iev_main->ibuf.w); + msgbuf_clear(&iev_main->ibuf.w); + close(iev_main->ibuf.fd); + free(iev_main); + log_debug("%s control exiting", getprogname()); + exit(0); +} + +int +ctl_imsg_compose_main(int type, pid_t pid, void *data, uint16_t datalen) +{ + return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen)); +} blob - b51f3c5ea0f7308f8f8ba5119863583596dd65bd blob + 330169729184ded77ba2bced0db8a81f5b34da4d --- thingsd/parse.y +++ thingsd/parse.y @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "thingsd.h" @@ -158,7 +159,7 @@ subthgs : THING '{' STRING ',' STRING '}' optcomma { if (strcmp(clt->name, my_name) == 0) { if (my_fd != clt->fd) { fail = true; - log_info("client exists"); + log_warn("client exists"); } for (n = 0; n < clt->le; n++) if (strcmp(clt->sub_names[n], @@ -182,7 +183,7 @@ subthgs : THING '{' STRING ',' STRING '}' optcomma { if (strcmp(thg->password, $5) == 0) { if (clt->subs++ >= pthgsd->max_sub) { - log_info("max " + log_warn("max " "subscriptions " "reached"); continue; @@ -302,7 +303,7 @@ locopts1 : LISTEN ON opttls PORT NUMBER { } | PASSWORD STRING { if ((newthg->password = strdup($2)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); free($2); } | bindopts2 @@ -331,12 +332,12 @@ socopts1 : LISTEN ON opttls PORT NUMBER { | CONNECT ON PORT NUMBER { newthg->conn_port = $4; } - | RECEIVE STRING PORT NUMBER { + | RECEIVE ON PORT NUMBER { newthg->conn_port = $4; } | PASSWORD STRING { if ((newthg->password = strdup($2)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); free($2); } | PERSISTENT NUMBER { @@ -388,9 +389,9 @@ thing : THING STRING { newthg->tls_protocols = TLS_PROTOCOLS_DEFAULT; newthg->tls_flags = 0; if ((newthg->tls_cert_file = strdup(TLS_CERT)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); if ((newthg->tls_key_file = strdup(TLS_KEY)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); strlcpy(newthg->tls_ciphers, TLS_CIPHERS, sizeof(newthg->tls_ciphers)); strlcpy(newthg->tls_dhe_params, TLS_DHE_PARAMS, @@ -428,19 +429,19 @@ thing : THING STRING { tlsopts : CERTIFICATE STRING { free(newthg->tls_cert_file); if ((newthg->tls_cert_file = strdup($2)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); free($2); } | KEY STRING { free(newthg->tls_key_file); if ((newthg->tls_key_file = strdup($2)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); free($2); } | OCSP STRING { free(newthg->tls_ocsp_staple_file); if ((newthg->tls_ocsp_staple_file = strdup($2)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); free($2); } | CIPHERS STRING { @@ -457,7 +458,7 @@ tlsopts : CERTIFICATE STRING { newthg->tls_flags |= TLSFLAG_CA; free(newthg->tls_ca_file); if ((newthg->tls_ca_file = strdup($3)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); free($3); } | DHE STRING { @@ -495,7 +496,7 @@ tlscltopt : /* empty */ newthg->tls_flags = TLSFLAG_CRL; free(newthg->tls_crl_file); if ((newthg->tls_crl_file = strdup($3)) == NULL) - fatal("out of memory"); + fatalx("out of memory"); free($3); } | tlscltopt OPTIONAL { @@ -527,7 +528,7 @@ yyerror(const char *fmt, ...) if (vasprintf(&msg, fmt, ap) == -1) fatalx("yyerror vasprintf"); va_end(ap); - log_warnx("%s:%d: %s", file->name, yylval.lineno, msg); + logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg); free(msg); return (0); } @@ -743,7 +744,7 @@ yylex(void) } yylval.v.string = strdup(buf); if (yylval.v.string == NULL) - fatal("yylex: strdup"); + fatalx("yylex: strdup"); return (STRING); } #define allowed_to_end_number(x) \ @@ -797,7 +798,7 @@ nodigits: *p = '\0'; if ((token = lookup(buf)) == STRING) if ((yylval.v.string = strdup(buf)) == NULL) - fatal("yylex: strdup"); + fatalx("yylex: strdup"); return (token); } if (c == '\n') { blob - /dev/null blob + cce92b3cd1e7ce1ed60f0d811bcec4e1f4497a07 (mode 644) --- /dev/null +++ thingsd/ctl.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Tracey Emery + * + * 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 "thingsd.h" + +void ctl_sighdlr(int, short, void *); +void ctl_shutdown(struct dthgs *); +void ctl_dispatch_main(int, short, void *); + +/* ctl.c */ +int ctl_imsg_compose_main(int, pid_t, void *, uint16_t); blob - ea59f6a58db95a7e0fcd6197056cac593251f382 blob + b0f4f1c371d64137a3b13ff10a8178c4e7fbaa34 --- thingsd/serial.c +++ thingsd/serial.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 Tracey Emery + * Copyright (c) 2016, 2019 Tracey Emery * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,7 +27,7 @@ #include "thingsd.h" -extern struct dthgs *pdthgs; +extern struct dthgs *pdthgs; void open_thgs(struct thgsd *pthgsd, bool reconn) @@ -54,7 +54,7 @@ open_thgs(struct thgsd *pthgsd, bool reconn) fd = open(thg->location, O_RDWR | O_NONBLOCK | O_NOCTTY | O_NDELAY); if (fd == -1) { - log_info("failed to open %s", thg->location); + log_warnx("failed to open %s", thg->location); if (reconn) return; dthg = new_dthg(thg); @@ -192,7 +192,7 @@ open_thgs(struct thgsd *pthgsd, bool reconn) /* Set the new options for the port */ tcsetattr(fd, TCSANOW, &s_opts); if ((thg->fd = fd) == '\0') { - log_info("serial device not opened"); + log_warnx("serial device not opened"); if (reconn) return; thg->exists = false; blob - c77a6d9e7c9b21b6e441603f5ea8d8bbc4da990e blob + ab31098b815794d980c086fc095cb74a37a4e3d0 --- thingsd/sockets.c +++ thingsd/sockets.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 Tracey Emery + * Copyright (c) 2016, 2019 Tracey Emery * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,7 +30,7 @@ #include "thingsd.h" -extern struct dthgs *pdthgs; +extern struct dthgs *pdthgs; void create_socks(struct thgsd *pthgsd, bool reconn) @@ -64,7 +64,7 @@ create_socks(struct thgsd *pthgsd, bool reconn) if (thg->type == DEV) { if((csock = sock->fd = create_sock(thg->port, iface, TCP)) == -1) - log_info("fd create socket failed"); + log_warnx("fd create socket failed"); else if (thg->fd != -1) thg->exists = true; else { @@ -77,13 +77,13 @@ create_socks(struct thgsd *pthgsd, bool reconn) conn_sock = new_sock(thg->conn_port); if ((csock = sock->fd = create_sock(thg->port, iface, TCP)) == -1) - log_info("udp create socket failed"); + log_warnx("udp create socket failed"); else thg->exists = true; if ((conn_csock = conn_sock->fd = create_sock(thg->conn_port, iface, thg->type)) == -1) - log_info("udp create socket failed"); + log_warnx("udp create socket failed"); else thg->exists = true; thg->fd = conn_sock->fd; @@ -94,7 +94,7 @@ create_socks(struct thgsd *pthgsd, bool reconn) if (thg->persist == 1) { if ((thg->fd = open_clt_sock(thg->ipaddr, thg->conn_port)) == -1) { - log_info("ipaddr connection failed"); + log_warnx("ipaddr connection failed"); if (reconn) continue; thg->exists = false; @@ -117,7 +117,7 @@ create_socks(struct thgsd *pthgsd, bool reconn) if (reconn == false) { if ((csock = sock->fd = create_sock(thg->port, iface, TCP)) == -1) { - log_info("tcp create socket failed"); + log_warnx("tcp create socket failed"); thg->exists = false; add_reconn(thg); } else { @@ -188,22 +188,17 @@ create_sock(int iport, char *iface, int type) addr_hints.ai_flags |= AI_PASSIVE; break; } - if ((gai = getaddrinfo(iface, port, &addr_hints, &addr_res)) != 0) { + if ((gai = getaddrinfo(iface, port, &addr_hints, &addr_res)) != 0) fatalx("getaddrinfo failed: %s", gai_strerror(gai)); - return -1; - } for (loop_res = addr_res; loop_res != NULL; loop_res = loop_res->ai_next) { if ((sock_fd = socket(loop_res->ai_family, - loop_res->ai_socktype, loop_res->ai_protocol)) == -1) { + loop_res->ai_socktype, loop_res->ai_protocol)) == -1) fatalx("unable to create socket"); - return -1; - } if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &o_val, sizeof(int)) == -1) { - fatalx("setsockopt error"); freeaddrinfo(addr_res); - return -1; + fatalx("setsockopt error"); } /* non-blocking */ flags = fcntl(sock_fd, F_GETFL); @@ -219,9 +214,8 @@ create_sock(int iport, char *iface, int type) break; } if (loop_res == NULL) { - fatalx("can't bind to port"); freeaddrinfo(addr_res); - return -1; + fatalx("can't bind to port"); } freeaddrinfo(addr_res); if (type == TCP) { @@ -236,9 +230,9 @@ create_sock(int iport, char *iface, int type) int open_clt_sock(char *ip_addr, int cport) { - struct addrinfo hints, *res, *res0; - int client_fd, error; - char port[6]; + struct addrinfo hints, *res, *res0; + int client_fd, error; + char port[6]; memset(&hints, 0, sizeof(hints)); snprintf(port, sizeof(port), "%d", cport); @@ -246,7 +240,7 @@ open_clt_sock(char *ip_addr, int cport) hints.ai_socktype = SOCK_STREAM; error = getaddrinfo(ip_addr, port, &hints, &res0); if (error) - fatalx("getaddrinfo: %s", gai_strerror(error)); + fatalx("getaddrinfo failed: %s", gai_strerror(error)); client_fd = -1; for (res = res0; res; res = res->ai_next) { client_fd = socket(res->ai_family, res->ai_socktype, @@ -269,9 +263,9 @@ open_clt_sock(char *ip_addr, int cport) char * get_ifaddrs(char *name) { - struct ifaddrs *ifap; - struct sockaddr *sa; - char *addr = NULL; + struct ifaddrs *ifap; + struct sockaddr *sa; + char *addr = NULL; if (getifaddrs(&ifap) == -1) fatalx("getifaddrs error"); @@ -386,7 +380,7 @@ sock_err(struct bufferevent *bev, short error, void *a thg->fd = -1; thg->exists = false; add_reconn(thg); - log_info("thing error: %s disconnected", + log_warnx("thing error: %s disconnected", thg->name); } } blob - /dev/null blob + 5fb2a6288e718aa69dceb45175bc5e931a542702 (mode 644) --- /dev/null +++ thingsd/log.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2003, 2004 Henning Brauer + * + * 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. + */ + +#ifndef LOG_H +#define LOG_H + +#include +void log_init(int, int); +void log_procinit(const char *); +void log_setverbose(int); +int log_getverbose(void); +void log_warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_info(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_debug(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void logit(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +void vlog(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +#endif /* LOG_H */ blob - 749666fbf597fd806cfc25e2e6276ba4ff5c7591 blob + f471d11c2070887dc317ab264b3616306e92479e --- thingsd/thingsd.c +++ thingsd/thingsd.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 Tracey Emery + * Copyright (c) 2016, 2019 Tracey Emery * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -39,10 +40,22 @@ int main(int, char *[]); void do_reconn(void); void thingsd_sighdlr(int, short, void *); void thingsd_shutdown(struct dthgs *); +static pid_t start_child(int, char *, int, int, int); +static void main_dispatch_ctl(int, short, void *); +int main_imsg_compose_ctl(int, pid_t, void *, uint16_t); +void show_list(enum ctl_list_type, pid_t); struct thgsd *pthgsd; struct dthgs *pdthgs; +struct thg_imsg *compose_thgs(struct thg *); +struct clt_imsg *compose_clts(struct clt *); +struct sock_imsg *compose_socks(struct sock *); +bool ctl_chld = false; + +pid_t ctl_pid; +static struct imsgev *iev_ctl; + __dead void usage(void) { @@ -75,14 +88,29 @@ main(int argc, char *argv[]) { int ch; struct timeval eb_timeout; + char *ctl_sock, *saved_argv0; + int pipe_ctl[2]; + ctl_sock = THINGSD_SOCK; + + /* log to stderr until daemonized */ + log_init(1, LOG_DAEMON); + log_setverbose(1); + if ((pthgsd = calloc(1, sizeof(*pthgsd))) == NULL) fatalx("no thgsd calloc"); if ((pdthgs = calloc(1, sizeof(*pdthgs))) == NULL) fatalx("no dthgs calloc"); - while ((ch = getopt(argc, argv, "dv")) != -1) { + saved_argv0 = argv[0]; + if (saved_argv0 == NULL) + saved_argv0 = "thingsd"; + + while ((ch = getopt(argc, argv, "dvC")) != -1) { switch (ch) { + case 'C': + ctl_chld = true; + break; case 'd': pthgsd->debug++; break; @@ -94,12 +122,12 @@ main(int argc, char *argv[]) } } - /* log to stderr until daemonized */ - log_init(pthgsd->debug ? pthgsd->debug : 1, LOG_DAEMON); argc -= optind; if (argc > 0) usage(); + if (ctl_chld) + thgs_ctl(pthgsd->debug, pthgsd->verbose, ctl_sock); if (parse_conf(PATH_CONF)) fatalx("config parsing failed"); if (geteuid()) @@ -109,12 +137,22 @@ main(int argc, char *argv[]) log_setverbose(pthgsd->verbose); /* make parent daemon */ - log_procinit("parent"); + thgsd_process = PROC_MAIN; + setproctitle("%s", log_procnames[thgsd_process]); + log_procinit(log_procnames[thgsd_process]); + if (!pthgsd->debug && daemon(1, 0) == -1) - fatalx("daemon"); + fatalx("daemon"); log_info("%s started", getprogname()); + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + PF_UNSPEC, pipe_ctl) == -1) + fatalx("ctl socketpair"); + + ctl_pid = start_child(PROC_CTL, saved_argv0, pipe_ctl[1], pthgsd->debug, + pthgsd->verbose); + pthgsd->eb = event_init(); pdthgs->run = 1; @@ -130,18 +168,31 @@ main(int argc, char *argv[]) signal_add(&pthgsd->evsigterm, NULL); signal_add(&pthgsd->evsigint, NULL); signal_add(&pthgsd->evsighup, NULL); + signal(SIGPIPE, SIG_IGN); + /* setup ctl child pipe */ + if ((iev_ctl = malloc(sizeof(struct imsgev))) == NULL) + fatalx("iev_ctl malloc"); + imsg_init(&iev_ctl->ibuf, pipe_ctl[0]); + iev_ctl->handler = main_dispatch_ctl; + + /* setup event handler */ + iev_ctl->events = EV_READ; + event_set(&iev_ctl->ev, pipe_ctl[0], iev_ctl->events, iev_ctl->handler, + iev_ctl); + event_add(&iev_ctl->ev, NULL); + if ((pthgsd->pw = getpwnam(TH_USER)) != NULL) { if(setuid(pthgsd->pw->pw_uid) != 0) - err(1, "unable to set user id of %s: %s", TH_USER, + errx(1, "unable to set user id of %s: %s", TH_USER, strerror(errno)); } else log_info("running as root. %s user not found", TH_USER); if (unveil("/dev", "rw") == -1) - err(1, "unveil"); + errx(1, "unveil"); if (pledge("stdio rpath wpath inet proc tty dns", NULL) == -1) - err(1, "pledge"); + errx(1, "pledge"); /* begin thing watchdog */ eb_timeout.tv_sec = EB_TIMEOUT; @@ -166,6 +217,7 @@ struct dthg if ((dthg = calloc(1, sizeof(*dthg))) == NULL) fatalx("no calloc dthg"); + log_debug("%s: adding detached thing, %s", __func__, pthg->name); dthg->name = pthg->name; dthg->type = pthg->type; dthg->dtime = time(NULL); @@ -228,6 +280,12 @@ thingsd_shutdown(struct dthgs *zdthgs) const char *progname = getprogname(); zdthgs->run = 0; + + /* close pipe */ + msgbuf_write(&iev_ctl->ibuf.w); + msgbuf_clear(&iev_ctl->ibuf.w); + close(iev_ctl->ibuf.fd); + free(iev_ctl); /* clean up things */ TAILQ_FOREACH_SAFE(thg, &pthgsd->thgs, entry, tthg) { close(thg->fd); @@ -263,3 +321,262 @@ thingsd_shutdown(struct dthgs *zdthgs) free(pthgsd); exit(0); } + +static pid_t +start_child(int p, char *argv0, int fd, int debug, int verbose) +{ + int argc = 0, argvc = 10, ac; + char *argv[argvc], bufa[argvc], bufb[argvc]; + pid_t pid; + const char *dash = "-"; + + switch (pid = fork()) { + case -1: + fatal("cannot fork"); + case 0: + break; + default: + close(fd); + return (pid); + } + + if (fd != 3) { + if (dup2(fd, 3) == -1) + fatal("cannot setup imsg fd"); + } else if (fcntl(fd, F_SETFD, 0) == -1) + fatal("cannot setup imsg fd"); + + argv[argc++] = argv0; + switch (p) { + case PROC_MAIN: + fatalx("Can not start main process"); + case PROC_CTL: + argv[argc++] = "-C"; + break; + } + memset(bufa, 0, sizeof(bufa)); + memset(bufb, 0, sizeof(bufb)); + if (debug) { + strlcpy(bufa, dash, sizeof(bufa)); + for (ac = 0; ac < debug; ac++) { + if (ac >= argvc) + continue; + strlcat(bufa, "d", sizeof(bufa)); + } + argv[argc++] = bufa; + } + if (verbose) { + strlcpy(bufb, dash, sizeof(bufb)); + for (ac = 0; ac < verbose; ac++) { + if (ac >= argvc) + continue; + strlcat(bufb, "v", sizeof(bufb)); + } + argv[argc++] = bufb; + } + argv[argc++] = NULL; + + execvp(argv0, argv); + fatal("execvp"); +} + +static void +main_dispatch_ctl(int fd, short event, void *bula) +{ + struct imsgev *iev = bula; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + enum ctl_list_type type; + ssize_t n; + int shut = 0, verbose; + + if (event & EV_READ) { + if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) + fatal("imsg_read error"); + if (n == 0) /* connection closed */ + shut = 1; + } + if (event & EV_WRITE) { + if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) + fatal("msgbuf_write"); + if (n == 0) + shut = 1; + } + + for (;;) { + if ((n = imsg_get(ibuf, &imsg)) == -1) + fatal("imsg_get"); + + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CTL_LIST: + if (IMSG_DATA_SIZE(imsg) != sizeof(type)) + fatalx("%s: IMSG_CTL_STATUS wrong length: %lu", + __func__, IMSG_DATA_SIZE(imsg)); + memcpy(&type, imsg.data, sizeof(type)); + show_list(type, imsg.hdr.pid); + break; + case IMSG_CTL_LOG_VERBOSE: + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + main_imsg_compose_ctl(imsg.hdr.type, 0, + imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); + break; + default: + log_debug("%s: error handling imsg %d", __func__, + imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + if (!shut) + imsg_event_add(iev); + else { + /* this pipe is dead, so remove the event handler */ + log_warnx("%s: ctl pipe dead, event deleted", __func__); + event_del(&iev->ev); + event_loopexit(NULL); + } +} + +int +main_imsg_compose_ctl(int type, pid_t pid, void *data, uint16_t datalen) +{ + return (imsg_compose_event(iev_ctl, type, 0, pid, -1, data, datalen)); +} + +void +show_list(enum ctl_list_type type, pid_t pid) +{ + struct clt *clt; + struct thg *thg; + struct sock *sock; + struct clt_imsg *clt_imsg; + struct thg_imsg *thg_imsg; + struct sock_imsg *sock_imsg; + + switch(type) { + case CTL_LIST_CLTS: + TAILQ_FOREACH(clt, &pthgsd->clts, entry) { + clt_imsg = compose_clts(clt); + main_imsg_compose_ctl(IMSG_LIST_CLTS, pid, clt_imsg, + sizeof(*clt_imsg)); + free(clt_imsg); + } + break; + case CTL_LIST_THGS: + TAILQ_FOREACH(thg, &pthgsd->thgs, entry) { + thg_imsg = compose_thgs(thg); + main_imsg_compose_ctl(IMSG_LIST_THGS, pid, thg_imsg, + sizeof(*thg_imsg)); + free(thg_imsg); + } + break; + case CTL_LIST_SOCKS: + TAILQ_FOREACH(sock, &pthgsd->socks, entry) { + sock_imsg = compose_socks(sock); + main_imsg_compose_ctl(IMSG_LIST_SOCKS, pid, sock_imsg, + sizeof(*sock_imsg)); + free(sock_imsg); + } + break; + default: + fatalx("unknown resolver type %d", type); + break; + } + main_imsg_compose_ctl(IMSG_CTL_END, pid, NULL, 0); +} + +struct clt_imsg * +compose_clts(struct clt *pclt) +{ + struct clt_imsg *comp_clt; + + if ((comp_clt = calloc(1, sizeof(*comp_clt))) == NULL) + fatalx("no com_clt calloc"); + + comp_clt->subscribed = pclt->subscribed; + + if (pclt->name != NULL) + strlcpy(comp_clt->name, pclt->name, BUFF); + + comp_clt->fd = pclt->fd; + comp_clt->port = pclt->port; + comp_clt->subs = pclt->subs; + comp_clt->tls = pclt->tls; + return(comp_clt); +}; + +struct thg_imsg * +compose_thgs(struct thg *pthg) +{ + struct thg_imsg *comp_thg; + + if ((comp_thg = calloc(1, sizeof(*comp_thg))) == NULL) + fatalx("no com_thg calloc"); + comp_thg->exists = pthg->exists; + comp_thg->hw_ctl = pthg->hw_ctl; + comp_thg->persist = pthg->persist; + comp_thg->sw_ctl = pthg->sw_ctl; + + if (pthg->iface != NULL) + strlcpy(comp_thg->iface, pthg->iface, BUFF); + if (pthg->ipaddr != NULL) + strlcpy(comp_thg->ipaddr, pthg->ipaddr, BUFF); + if (pthg->name != NULL) + strlcpy(comp_thg->name, pthg->name, BUFF); + if (pthg->parity != NULL) + strlcpy(comp_thg->parity, pthg->parity, BUFF); + if (pthg->password != NULL) + strlcpy(comp_thg->password, pthg->password, BUFF); + if (pthg->location != NULL) + strlcpy(comp_thg->location, pthg->location, BUFF); + if (pthg->udp != NULL) + strlcpy(comp_thg->udp, pthg->udp, BUFF); + + comp_thg->fd = pthg->fd; + comp_thg->baud = pthg->baud; + comp_thg->conn_port = pthg->conn_port; + comp_thg->data_bits = pthg->data_bits; + comp_thg->max_clt = pthg->max_clt; + comp_thg->port = pthg->port; + comp_thg->stop_bits = pthg->stop_bits; + comp_thg->type = pthg->type; + comp_thg->clt_cnt = pthg->clt_cnt; + + comp_thg->tls = pthg->tls; + + if (pthg->tls_cert_file != NULL) + strlcpy(comp_thg->tls_cert_file, pthg->tls_cert_file, BUFF); + if (pthg->tls_key_file != NULL) + strlcpy(comp_thg->tls_key_file, pthg->tls_key_file, BUFF); + if (pthg->tls_ca_file != NULL) + strlcpy(comp_thg->tls_ca_file, pthg->tls_ca_file, BUFF); + if (pthg->tls_crl_file != NULL) + strlcpy(comp_thg->tls_crl_file, pthg->tls_crl_file, BUFF); + if (pthg->tls_ocsp_staple_file != NULL) + strlcpy(comp_thg->tls_ocsp_staple_file, + pthg->tls_ocsp_staple_file, BUFF); + return(comp_thg); +}; + +struct sock_imsg * +compose_socks(struct sock *psock) +{ + struct sock_imsg *comp_sock; + + if ((comp_sock = calloc(1, sizeof(*comp_sock))) == NULL) + fatalx("no com_sock calloc"); + + if (psock->name != NULL) + strlcpy(comp_sock->name, psock->name, BUFF); + + comp_sock->fd = psock->fd; + comp_sock->port = psock->port; + comp_sock->clt_cnt = psock->clt_cnt; + comp_sock->max_clts = psock->max_clts; + comp_sock->tls = psock->tls; + return(comp_sock); +}; blob - d57c58997dcc926ac7dc9548e650fc6a25f92aa1 blob + 63747f669a140eaca6999267b35fc5b80ba1152c --- thingsd/thingsd.h +++ thingsd/thingsd.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 Tracey Emery + * Copyright (c) 2016, 2019 Tracey Emery * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,14 +21,17 @@ #include #include +#include #include #include #include #include +#include "log.h" + #define PATH_CONF "/etc/thingsd.conf" #define TH_USER "_thingsd" -#define THINGSD_SOCK "/var/run/httpd.sock" +#define THINGSD_SOCK "/var/run/thingsd.sock" #define CONN_RTRY 30 #define MIN_RTRY 10 #define MAX_RTRY 600 @@ -67,6 +70,36 @@ struct dthg { time_t dtime; }; +struct thg_imsg { + bool exists; + bool hw_ctl; + bool persist; + bool sw_ctl; + char iface[BUFF]; + char ipaddr[BUFF]; + char name[BUFF]; + char parity[BUFF]; + char password[BUFF]; + char location[BUFF]; + char udp[BUFF]; + int fd; + int baud; + int conn_port; + int data_bits; + int max_clt; + int port; + int stop_bits; + int type; + size_t clt_cnt; + + bool tls; + char tls_cert_file[BUFF]; + char tls_key_file[BUFF]; + char tls_ca_file[BUFF]; + char tls_crl_file[BUFF]; + char tls_ocsp_staple_file[BUFF]; +}; + struct thg { TAILQ_ENTRY(thg) entry; struct bufferevent *bev; @@ -115,6 +148,16 @@ struct thg { uint32_t tls_protocols; }; +struct sock_imsg { + char name[BUFF]; + int fd; + int port; + size_t clt_cnt; + size_t max_clts; + + bool tls; +}; + struct sock { TAILQ_ENTRY(sock) entry; struct event *ev; @@ -131,6 +174,16 @@ struct sock { struct tls *tls_ctx; }; +struct clt_imsg { + bool subscribed; + char name[BUFF]; + int fd; + int port; + size_t subs; + + bool tls; +}; + struct clt { TAILQ_ENTRY(clt) entry; struct event *ev; @@ -176,8 +229,69 @@ struct thgsd { struct event evsigterm; struct event evsigint; struct event evsighup; + + /* ctl */ + struct event_base *ctl_eb; + struct event ctl_evsigquit; + struct event ctl_evsigterm; + struct event ctl_evsigint; }; +/* control.c */ +#define IMSG_DATA_SIZE(imsg) ((imsg).hdr.len - IMSG_HEADER_SIZE) + +enum { + PROC_MAIN, + PROC_CTL, +} thgsd_process; + +static const char * const log_procnames[] = { + "thingsd main", + "thingsd control", +}; + +enum ctl_list_type { + CTL_LIST_CLTS, + CTL_LIST_THGS, + CTL_LIST_SOCKS, +}; + +struct imsgev { + struct imsgbuf ibuf; + void (*handler)(int, short, void *); + struct event ev; + short events; +}; + +int control_init(char *); +int control_listen(void); +void control_accept(int, short, void *); +void control_dispatch_imsg(int, short, void *); +int control_imsg_relay(struct imsg *); + +TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns; + +enum imsg_type { + IMSG_NONE, + IMSG_CTL_LOG_VERBOSE, + IMSG_CTL_LIST, + IMSG_CTL_END, + IMSG_LIST_CLTS, + IMSG_LIST_THGS, + IMSG_LIST_SOCKS, + IMSG_KILL_CLT, + IMSG_ADD_THG, + IMSG_DEL_THG, + IMSG_SHOW_PKTS +}; + +void imsg_event_add(struct imsgev *); +int imsg_compose_event(struct imsgev *, uint16_t, + uint32_t, pid_t, int, void *, uint16_t); + +/* ctl.c */ +void thgs_ctl(int, int, char*); + /* thingsd.c */ struct dthg *new_dthg(struct thg *); void add_reconn(struct thg *); @@ -229,26 +343,4 @@ int tls_load_crl(struct thg *); int tls_load_ocsp(struct thg *); int sock_tls_init(struct sock *, struct thg *); void sock_tls_handshake(int, short, void *); - -/* log.c */ -void log_init(int, int); -void log_procinit(const char *); -void log_setverbose(int); -int log_getverbose(void); -void log_warn(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_warnx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_info(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_debug(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void logit(int, const char *, ...) - __attribute__((__format__ (printf, 2, 3))); -void vlog(int, const char *, va_list) - __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -__dead void fatalx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); #endif /* _THINGSD_H */ blob - 066f64f7210595514d0489dcc44dd084f388f408 blob + 01c0d50d1ffab875f9bd8151221102b3773eb7dd --- thingsd/tls.c +++ thingsd/tls.c @@ -101,7 +101,7 @@ sock_tls_init(struct sock *psock, struct thg *pthg) return (-1); } if ((psock->tls_config = tls_config_new()) == NULL) { - log_warnx("%s: failed to get tls pthgig", __func__); + log_warnx("%s: failed to get tls config", __func__); return (-1); } if ((psock->tls_ctx = tls_server()) == NULL) { @@ -207,13 +207,13 @@ sock_tls_handshake(int fd, short event, void *arg) event_set(clt->ev, clt->fd, EV_READ|EV_PERSIST, sock_tls_handshake, pthgsd); if (event_add(clt->ev, NULL)) - log_info("event add clt"); + log_debug("%s: event add clt", __func__); } else if (ret == TLS_WANT_POLLOUT) { event_del(clt->ev); event_set(clt->ev, clt->fd, EV_WRITE|EV_PERSIST, sock_tls_handshake, pthgsd); if (event_add(clt->ev, NULL)) - log_info("event add clt"); + log_debug("%s: event add clt", __func__); } else { event_del(clt->ev); log_debug("%s: tls handshake failed - %s", __func__,