Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/param.h>
19 #include <sys/queue.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <termios.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <event.h>
33 #include <fcntl.h>
34 #include <imsg.h>
35 #include <pwd.h>
36 #include <signal.h>
37 #include <syslog.h>
38 #include <unistd.h>
39 #include <ctype.h>
40 #include <util.h>
42 #include "got_opentemp.h"
43 #include "got_reference.h"
45 #include "gotwebd.h"
47 __dead void usage(void);
49 int main(int, char **);
50 int gotwebd_configure(struct gotwebd *);
51 void gotwebd_configure_done(struct gotwebd *);
52 void gotwebd_sighdlr(int sig, short event, void *arg);
53 void gotwebd_shutdown(void);
54 void gotwebd_dispatch_sockets(int, short, void *);
56 struct gotwebd *gotwebd_env;
58 void
59 imsg_event_add(struct imsgev *iev)
60 {
61 if (iev->handler == NULL) {
62 imsg_flush(&iev->ibuf);
63 return;
64 }
66 iev->events = EV_READ;
67 if (iev->ibuf.w.queued)
68 iev->events |= EV_WRITE;
70 event_del(&iev->ev);
71 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
72 event_add(&iev->ev, NULL);
73 }
75 int
76 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
77 pid_t pid, int fd, const void *data, uint16_t datalen)
78 {
79 int ret;
81 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
82 if (ret == -1)
83 return (ret);
84 imsg_event_add(iev);
85 return (ret);
86 }
88 int
89 main_compose_sockets(struct gotwebd *env, uint32_t type, int fd,
90 const void *data, uint16_t len)
91 {
92 size_t i;
93 int ret, d;
95 for (i = 0; i < env->nserver; ++i) {
96 d = -1;
97 if (fd != -1 && (d = dup(fd)) == -1)
98 return (-1);
100 ret = imsg_compose_event(&env->iev_server[i], type, 0, -1,
101 d, data, len);
102 if (ret == -1)
103 return (-1);
105 /* prevent fd exhaustion */
106 if (d != -1) {
107 do {
108 ret = imsg_flush(&env->iev_server[i].ibuf);
109 } while (ret == -1 && errno == EAGAIN);
110 if (ret == -1)
111 return (-1);
112 imsg_event_add(&env->iev_server[i]);
116 if (fd != -1)
117 close(fd);
119 return 0;
122 int
123 sockets_compose_main(struct gotwebd *env, uint32_t type, const void *d,
124 uint16_t len)
126 return (imsg_compose_event(env->iev_parent, type, 0, -1, -1, d, len));
129 void
130 gotwebd_dispatch_sockets(int fd, short event, void *arg)
132 struct imsgev *iev = arg;
133 struct imsgbuf *ibuf;
134 struct imsg imsg;
135 struct gotwebd *env = gotwebd_env;
136 ssize_t n;
137 int shut = 0;
139 ibuf = &iev->ibuf;
141 if (event & EV_READ) {
142 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
143 fatal("imsg_read error");
144 if (n == 0) /* Connection closed */
145 shut = 1;
147 if (event & EV_WRITE) {
148 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
149 fatal("msgbuf_write");
150 if (n == 0) /* Connection closed */
151 shut = 1;
154 for (;;) {
155 if ((n = imsg_get(ibuf, &imsg)) == -1)
156 fatal("imsg_get");
157 if (n == 0) /* No more messages. */
158 break;
160 switch (imsg.hdr.type) {
161 case IMSG_CFG_DONE:
162 gotwebd_configure_done(env);
163 break;
164 default:
165 fatalx("%s: unknown imsg type %d", __func__,
166 imsg.hdr.type);
169 imsg_free(&imsg);
172 if (!shut)
173 imsg_event_add(iev);
174 else {
175 /* This pipe is dead. Remove its event handler */
176 event_del(&iev->ev);
177 event_loopexit(NULL);
181 void
182 gotwebd_sighdlr(int sig, short event, void *arg)
184 /* struct privsep *ps = arg; */
186 switch (sig) {
187 case SIGHUP:
188 log_info("%s: ignoring SIGHUP", __func__);
189 break;
190 case SIGPIPE:
191 log_info("%s: ignoring SIGPIPE", __func__);
192 break;
193 case SIGUSR1:
194 log_info("%s: ignoring SIGUSR1", __func__);
195 break;
196 case SIGTERM:
197 case SIGINT:
198 gotwebd_shutdown();
199 break;
200 default:
201 fatalx("unexpected signal");
205 static int
206 spawn_socket_process(struct gotwebd *env, const char *argv0, int n)
208 const char *argv[5];
209 int argc = 0;
210 int p[2];
211 pid_t pid;
213 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
214 fatal("socketpair");
216 switch (pid = fork()) {
217 case -1:
218 fatal("fork");
219 case 0: /* child */
220 break;
221 default: /* parent */
222 close(p[0]);
223 imsg_init(&env->iev_server[n].ibuf, p[1]);
224 env->iev_server[n].handler = gotwebd_dispatch_sockets;
225 env->iev_server[n].data = &env->iev_server[n];
226 event_set(&env->iev_server[n].ev, p[1], EV_READ,
227 gotwebd_dispatch_sockets, &env->iev_server[n]);
228 event_add(&env->iev_server[n].ev, NULL);
229 return 0;
232 close(p[1]);
234 argv[argc++] = argv0;
235 argv[argc++] = "-S";
236 if (env->gotwebd_debug)
237 argv[argc++] = "-d";
238 if (env->gotwebd_verbose)
239 argv[argc++] = "-v";
240 argv[argc] = NULL;
242 if (p[0] != 3) {
243 if (dup2(p[0], 3) == -1)
244 fatal("dup2");
245 } else if (fcntl(p[0], F_SETFD, 0) == -1)
246 fatal("fcntl");
248 /* obnoxious cast */
249 execvp(argv0, (char * const *)argv);
250 fatal("execvp %s", argv0);
253 __dead void
254 usage(void)
256 fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
257 getprogname());
258 exit(1);
261 int
262 main(int argc, char **argv)
264 struct event sigint, sigterm, sighup, sigpipe, sigusr1;
265 struct gotwebd *env;
266 struct passwd *pw;
267 int ch, i;
268 int no_action = 0;
269 int server_proc = 0;
270 const char *conffile = GOTWEBD_CONF;
271 const char *argv0;
273 if ((argv0 = argv[0]) == NULL)
274 argv0 = "gotwebd";
276 env = calloc(1, sizeof(*env));
277 if (env == NULL)
278 fatal("%s: calloc", __func__);
279 config_init(env);
281 /* log to stderr until daemonized */
282 log_init(1, LOG_DAEMON);
284 while ((ch = getopt(argc, argv, "D:df:nSv")) != -1) {
285 switch (ch) {
286 case 'D':
287 if (cmdline_symset(optarg) < 0)
288 log_warnx("could not parse macro definition %s",
289 optarg);
290 break;
291 case 'd':
292 env->gotwebd_debug = 1;
293 break;
294 case 'f':
295 conffile = optarg;
296 break;
297 case 'n':
298 no_action = 1;
299 break;
300 case 'S':
301 server_proc = 1;
302 break;
303 case 'v':
304 env->gotwebd_verbose++;
305 break;
306 default:
307 usage();
311 argc -= optind;
312 if (argc > 0)
313 usage();
315 gotwebd_env = env;
316 env->gotwebd_conffile = conffile;
318 if (parse_config(env->gotwebd_conffile, env) == -1)
319 exit(1);
321 if (no_action) {
322 fprintf(stderr, "configuration OK\n");
323 exit(0);
326 /* check for root privileges */
327 if (geteuid())
328 fatalx("need root privileges");
330 pw = getpwnam(GOTWEBD_USER);
331 if (pw == NULL)
332 fatalx("unknown user %s", GOTWEBD_USER);
333 env->pw = pw;
335 log_init(env->gotwebd_debug, LOG_DAEMON);
336 log_setverbose(env->gotwebd_verbose);
338 if (server_proc) {
339 setproctitle("sockets");
340 log_procinit("sockets");
342 if (chroot(pw->pw_dir) == -1)
343 fatal("chroot %s", pw->pw_dir);
344 if (chdir("/") == -1)
345 fatal("chdir /");
346 if (setgroups(1, &pw->pw_gid) == -1 ||
347 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
348 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
349 fatal("failed to drop privileges");
351 sockets(env, 3);
352 return 1;
355 if (!env->gotwebd_debug && daemon(0, 0) == -1)
356 fatal("daemon");
358 event_init();
360 env->nserver = env->prefork_gotwebd;
361 env->iev_server = calloc(env->nserver, sizeof(*env->iev_server));
362 if (env->iev_server == NULL)
363 fatal("calloc");
365 for (i = 0; i < env->nserver; ++i) {
366 if (spawn_socket_process(env, argv0, i) == -1)
367 fatal("spawn_socket_process");
370 log_procinit("gotwebd");
372 log_info("%s startup", getprogname());
374 signal_set(&sigint, SIGINT, gotwebd_sighdlr, env);
375 signal_set(&sigterm, SIGTERM, gotwebd_sighdlr, env);
376 signal_set(&sighup, SIGHUP, gotwebd_sighdlr, env);
377 signal_set(&sigpipe, SIGPIPE, gotwebd_sighdlr, env);
378 signal_set(&sigusr1, SIGUSR1, gotwebd_sighdlr, env);
380 signal_add(&sigint, NULL);
381 signal_add(&sigterm, NULL);
382 signal_add(&sighup, NULL);
383 signal_add(&sigpipe, NULL);
384 signal_add(&sigusr1, NULL);
386 if (gotwebd_configure(env) == -1)
387 fatalx("configuration failed");
389 #ifdef PROFILE
390 if (unveil("gmon.out", "rwc") != 0)
391 err(1, "gmon.out");
392 #endif
394 if (unveil(env->httpd_chroot, "r") == -1)
395 err(1, "unveil");
397 if (unveil(GOTWEBD_CONF, "r") == -1)
398 err(1, "unveil");
400 if (unveil(NULL, NULL) != 0)
401 err(1, "unveil");
403 #ifndef PROFILE
404 if (pledge("stdio", NULL) == -1)
405 err(1, "pledge");
406 #endif
408 event_dispatch();
410 log_debug("%s gotwebd exiting", getprogname());
412 return (0);
415 int
416 gotwebd_configure(struct gotwebd *env)
418 struct server *srv;
419 struct socket *sock;
421 /* gotweb need to reload its config. */
422 env->gotwebd_reload = env->prefork_gotwebd;
424 /* send our gotweb servers */
425 TAILQ_FOREACH(srv, &env->servers, entry) {
426 if (config_setserver(env, srv) == -1)
427 fatalx("%s: send server error", __func__);
430 /* send our sockets */
431 TAILQ_FOREACH(sock, &env->sockets, entry) {
432 if (config_setsock(env, sock) == -1)
433 fatalx("%s: send socket error", __func__);
434 if (config_setfd(env, sock) == -1)
435 fatalx("%s: send priv_fd error", __func__);
438 if (main_compose_sockets(env, IMSG_CFG_DONE, -1, NULL, 0) == -1)
439 fatal("main_compose_sockets IMSG_CFG_DONE");
441 return (0);
444 void
445 gotwebd_configure_done(struct gotwebd *env)
447 if (env->gotwebd_reload == 0) {
448 log_warnx("%s: configuration already finished", __func__);
449 return;
452 env->gotwebd_reload--;
453 if (env->gotwebd_reload == 0 &&
454 main_compose_sockets(env, IMSG_CTL_START, -1, NULL, 0) == -1)
455 fatal("main_compose_sockets IMSG_CTL_START");
458 void
459 gotwebd_shutdown(void)
461 struct gotwebd *env = gotwebd_env;
462 pid_t pid;
463 int i, status;
465 for (i = 0; i < env->nserver; ++i) {
466 event_del(&env->iev_server[i].ev);
467 imsg_clear(&env->iev_server[i].ibuf);
468 close(env->iev_server[i].ibuf.fd);
469 env->iev_server[i].ibuf.fd = -1;
472 do {
473 pid = waitpid(WAIT_ANY, &status, 0);
474 if (pid <= 0)
475 continue;
477 if (WIFSIGNALED(status))
478 log_warnx("lost child: pid %u terminated; signal %d",
479 pid, WTERMSIG(status));
480 else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
481 log_warnx("lost child: pid %u exited abnormally",
482 pid);
483 } while (pid != -1 || (pid == -1 && errno == EINTR));
485 free(gotwebd_env);
487 log_warnx("gotwebd terminating");
488 exit(0);