Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "compat.h"
19 #include <errno.h>
20 #include <pwd.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <syslog.h>
25 #include <unistd.h>
27 #include "client.h"
28 #include "kamid.h"
29 #include "log.h"
30 #include "sandbox.h"
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);
42 ATTR_DEAD void
43 client(int debug, int verbose)
44 {
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");
55 event_init();
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)
69 fatal(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);
80 event_dispatch();
81 client_shutdown();
82 }
84 static ATTR_DEAD void
85 client_shutdown(void)
86 {
87 msgbuf_clear(&iev_listener->ibuf.w);
88 close(iev_listener->ibuf.fd);
90 free(iev_listener);
92 log_info("client exiting");
93 exit(0);
94 }
96 static void
97 client_sig_handler(int sig, short event, void *d)
98 {
99 /*
100 * Normal signal handler rules don't apply because libevent
101 * decouples for us.
102 */
104 switch (sig) {
105 case SIGINT:
106 case SIGTERM:
107 client_shutdown();
108 default:
109 fatalx("unexpected signal %d", sig);
113 #define AUTH_NONE 0
114 #define AUTH_USER 1
115 #define AUTH_DONE 2
117 static void
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};
123 struct imsg imsg;
124 struct imsgev *iev = d;
125 struct imsgbuf *ibuf;
126 ssize_t n;
127 int shut = 0;
129 ibuf = &iev->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 */
135 shut = 1;
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 */
141 shut = 1;
144 for (;;) {
145 if ((n = imsg_get(ibuf, &imsg)) == -1)
146 fatal("%s: imsg_get error", __func__);
147 if (n == 0) /* No more messages. */
148 break;
150 switch (imsg.hdr.type) {
151 case IMSG_AUTH:
152 if (auth)
153 fatalx("%s: IMSG_AUTH already done", __func__);
154 auth = AUTH_USER;
155 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
156 strlcpy(username, imsg.data, sizeof(username));
157 break;
158 case IMSG_AUTH_DIR:
159 if (auth != AUTH_USER)
160 fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
161 __func__);
162 auth = AUTH_DONE;
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));
168 break;
169 case IMSG_BUF:
170 /* echo! */
171 client_imsg_compose_listener(IMSG_BUF, imsg.hdr.peerid,
172 imsg.data, IMSG_DATA_SIZE(imsg));
173 break;
174 case IMSG_CONN_GONE:
175 log_debug("closing");
176 shut = 1;
177 break;
178 default:
179 log_debug("%s: unexpected imsg %d",
180 __func__, imsg.hdr.type);
181 break;
183 imsg_free(&imsg);
186 if (!shut)
187 imsg_event_add(iev);
188 else {
189 /* This pipe is dead. Remove its event handler. */
190 event_del(&iev->ev);
191 log_warnx("pipe closed, shutting down...");
192 event_loopexit(NULL);
196 static void
197 client_privdrop(const char *username, const char *dir)
199 struct passwd *pw;
201 setproctitle("client %s", username);
203 if ((pw = getpwnam(username)) == NULL)
204 fatalx("getpwnam(%s) failed", username);
206 if (chroot(dir) == -1)
207 fatal("chroot");
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");
216 sandbox_client();
217 log_debug("client ready");
220 static int
221 client_imsg_compose_listener(int type, uint32_t peerid,
222 const void *data, uint16_t len)
224 int ret;
226 if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
227 data, len)) != -1)
228 imsg_event_add(iev_listener);
230 return ret;