2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 static struct imsgev *iev_listener;
34 static ATTR_DEAD void client_shutdown(void);
35 static void client_sig_handler(int, short, void *);
36 static void client_dispatch_listener(int, short, void *);
37 static void client_privdrop(const char *, const char *);
39 static int client_imsg_compose_listener(int, uint32_t,
40 const void *, uint16_t);
43 client(int debug, int verbose)
45 struct event ev_sigint, ev_sigterm;
47 log_init(debug, LOG_DAEMON);
48 log_setverbose(verbose);
50 setproctitle("client");
51 log_procinit("client");
53 log_debug("warming up");
57 /* Setup signal handlers */
58 signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
59 signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
61 signal_add(&ev_sigint, NULL);
62 signal_add(&ev_sigterm, NULL);
64 signal(SIGPIPE, SIG_IGN);
65 signal(SIGHUP, SIG_IGN);
67 /* Setup pipe and event handler to the listener process */
68 if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
71 imsg_init(&iev_listener->ibuf, 3);
72 iev_listener->handler = client_dispatch_listener;
74 /* Setup event handlers. */
75 iev_listener->events = EV_READ;
76 event_set(&iev_listener->ev, iev_listener->ibuf.fd,
77 iev_listener->events, iev_listener->handler, iev_listener);
78 event_add(&iev_listener->ev, NULL);
87 msgbuf_clear(&iev_listener->ibuf.w);
88 close(iev_listener->ibuf.fd);
92 log_info("client exiting");
97 client_sig_handler(int sig, short event, void *d)
100 * Normal signal handler rules don't apply because libevent
109 fatalx("unexpected signal %d", sig);
118 client_dispatch_listener(int fd, short event, void *d)
120 static int auth = AUTH_NONE;
121 static char username[64] = {0};
122 static char dir[PATH_MAX] = {0};
124 struct imsgev *iev = d;
125 struct imsgbuf *ibuf;
131 if (event & EV_READ) {
132 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
133 fatal("imsg_read error");
134 if (n == 0) /* Connection closed */
137 if (event & EV_WRITE) {
138 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
139 fatal("msgbuf_write");
140 if (n == 0) /* Connection closed */
145 if ((n = imsg_get(ibuf, &imsg)) == -1)
146 fatal("%s: imsg_get error", __func__);
147 if (n == 0) /* No more messages. */
150 switch (imsg.hdr.type) {
153 fatalx("%s: IMSG_AUTH already done", __func__);
155 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
156 strlcpy(username, imsg.data, sizeof(username));
159 if (auth != AUTH_USER)
160 fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
163 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
164 strlcpy(dir, imsg.data, sizeof(dir));
165 client_privdrop(username, dir);
166 memset(username, 0, sizeof(username));
167 memset(dir, 0, sizeof(username));
171 client_imsg_compose_listener(IMSG_BUF, imsg.hdr.peerid,
172 imsg.data, IMSG_DATA_SIZE(imsg));
175 log_debug("closing");
179 log_debug("%s: unexpected imsg %d",
180 __func__, imsg.hdr.type);
189 /* This pipe is dead. Remove its event handler. */
191 log_warnx("pipe closed, shutting down...");
192 event_loopexit(NULL);
197 client_privdrop(const char *username, const char *dir)
201 setproctitle("client %s", username);
203 if ((pw = getpwnam(username)) == NULL)
204 fatalx("getpwnam(%s) failed", username);
206 if (chroot(dir) == -1)
208 if (chdir("/") == -1)
209 fatal("chdir(\"/\")");
211 if (setgroups(1, &pw->pw_gid) ||
212 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
213 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
214 fatal("can't drop privileges");
217 log_debug("client ready");
221 client_imsg_compose_listener(int type, uint32_t peerid,
222 const void *data, uint16_t len)
226 if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
228 imsg_event_add(iev_listener);