2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
6 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
7 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
9 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 8a35f56c 2022-07-16 thomas #include <sys/param.h>
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
21 8a35f56c 2022-07-16 thomas #include <sys/wait.h>
22 8a35f56c 2022-07-16 thomas #include <sys/cdefs.h>
24 8a35f56c 2022-07-16 thomas #include <net/if.h>
25 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
27 8a35f56c 2022-07-16 thomas #include <stdio.h>
28 8a35f56c 2022-07-16 thomas #include <stdlib.h>
29 8a35f56c 2022-07-16 thomas #include <string.h>
30 8a35f56c 2022-07-16 thomas #include <termios.h>
31 8a35f56c 2022-07-16 thomas #include <err.h>
32 8a35f56c 2022-07-16 thomas #include <errno.h>
33 8a35f56c 2022-07-16 thomas #include <event.h>
34 8a35f56c 2022-07-16 thomas #include <fcntl.h>
35 8a35f56c 2022-07-16 thomas #include <pwd.h>
36 8a35f56c 2022-07-16 thomas #include <signal.h>
37 8a35f56c 2022-07-16 thomas #include <syslog.h>
38 8a35f56c 2022-07-16 thomas #include <unistd.h>
39 8a35f56c 2022-07-16 thomas #include <ctype.h>
41 8a35f56c 2022-07-16 thomas #include "got_opentemp.h"
43 8a35f56c 2022-07-16 thomas #include "proc.h"
44 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
46 ff36aeea 2022-07-16 thomas #include "got_compat.h"
48 8a35f56c 2022-07-16 thomas __dead void usage(void);
50 8a35f56c 2022-07-16 thomas int main(int, char **);
51 8a35f56c 2022-07-16 thomas int gotwebd_configure(struct gotwebd *);
52 8a35f56c 2022-07-16 thomas void gotwebd_configure_done(struct gotwebd *);
53 8a35f56c 2022-07-16 thomas void gotwebd_sighdlr(int sig, short event, void *arg);
54 8a35f56c 2022-07-16 thomas void gotwebd_shutdown(void);
55 8a35f56c 2022-07-16 thomas int gotwebd_dispatch_sockets(int, struct privsep_proc *, struct imsg *);
57 8a35f56c 2022-07-16 thomas struct gotwebd *gotwebd_env;
59 8a35f56c 2022-07-16 thomas static struct privsep_proc procs[] = {
60 8a35f56c 2022-07-16 thomas { "sockets", PROC_SOCKS, gotwebd_dispatch_sockets, sockets,
61 8a35f56c 2022-07-16 thomas sockets_shutdown },
65 8a35f56c 2022-07-16 thomas gotwebd_dispatch_sockets(int fd, struct privsep_proc *p, struct imsg *imsg)
67 8a35f56c 2022-07-16 thomas struct privsep *ps = p->p_ps;
68 8a35f56c 2022-07-16 thomas struct gotwebd *env = ps->ps_env;
70 8a35f56c 2022-07-16 thomas switch (imsg->hdr.type) {
71 8a35f56c 2022-07-16 thomas case IMSG_CFG_DONE:
72 8a35f56c 2022-07-16 thomas gotwebd_configure_done(env);
75 8a35f56c 2022-07-16 thomas return (-1);
78 8a35f56c 2022-07-16 thomas return (0);
82 8a35f56c 2022-07-16 thomas gotwebd_sighdlr(int sig, short event, void *arg)
84 8a35f56c 2022-07-16 thomas /* struct privsep *ps = arg; */
86 8a35f56c 2022-07-16 thomas if (privsep_process != PROC_GOTWEBD)
89 8a35f56c 2022-07-16 thomas switch (sig) {
90 8a35f56c 2022-07-16 thomas case SIGHUP:
91 8a35f56c 2022-07-16 thomas log_info("%s: ignoring SIGHUP", __func__);
93 8a35f56c 2022-07-16 thomas case SIGPIPE:
94 8a35f56c 2022-07-16 thomas log_info("%s: ignoring SIGPIPE", __func__);
96 8a35f56c 2022-07-16 thomas case SIGUSR1:
97 8a35f56c 2022-07-16 thomas log_info("%s: ignoring SIGUSR1", __func__);
99 8a35f56c 2022-07-16 thomas case SIGTERM:
100 8a35f56c 2022-07-16 thomas case SIGINT:
101 8a35f56c 2022-07-16 thomas gotwebd_shutdown();
104 8a35f56c 2022-07-16 thomas fatalx("unexpected signal");
108 8a35f56c 2022-07-16 thomas __dead void
109 8a35f56c 2022-07-16 thomas usage(void)
111 8a35f56c 2022-07-16 thomas fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
112 8a35f56c 2022-07-16 thomas getprogname());
117 8a35f56c 2022-07-16 thomas main(int argc, char **argv)
119 8a35f56c 2022-07-16 thomas struct gotwebd *env;
120 8a35f56c 2022-07-16 thomas struct privsep *ps;
121 8a35f56c 2022-07-16 thomas unsigned int proc;
123 8a35f56c 2022-07-16 thomas const char *conffile = GOTWEBD_CONF;
124 8a35f56c 2022-07-16 thomas enum privsep_procid proc_id = PROC_GOTWEBD;
125 8a35f56c 2022-07-16 thomas int proc_instance = 0;
126 8a35f56c 2022-07-16 thomas const char *errp, *title = NULL;
127 8a35f56c 2022-07-16 thomas int argc0 = argc;
129 8a35f56c 2022-07-16 thomas env = calloc(1, sizeof(*env));
130 8a35f56c 2022-07-16 thomas if (env == NULL)
131 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
133 8a35f56c 2022-07-16 thomas /* XXX: add s and S for both sockets */
134 8a35f56c 2022-07-16 thomas while ((ch = getopt(argc, argv, "D:P:I:df:vn")) != -1) {
135 8a35f56c 2022-07-16 thomas switch (ch) {
136 8a35f56c 2022-07-16 thomas case 'D':
137 8a35f56c 2022-07-16 thomas if (cmdline_symset(optarg) < 0)
138 8a35f56c 2022-07-16 thomas log_warnx("could not parse macro definition %s",
141 8a35f56c 2022-07-16 thomas case 'd':
142 8a35f56c 2022-07-16 thomas env->gotwebd_debug = 2;
144 8a35f56c 2022-07-16 thomas case 'f':
145 8a35f56c 2022-07-16 thomas conffile = optarg;
147 8a35f56c 2022-07-16 thomas case 'v':
148 8a35f56c 2022-07-16 thomas env->gotwebd_verbose++;
150 8a35f56c 2022-07-16 thomas case 'n':
151 8a35f56c 2022-07-16 thomas env->gotwebd_debug = 2;
152 8a35f56c 2022-07-16 thomas env->gotwebd_noaction = 1;
154 8a35f56c 2022-07-16 thomas case 'P':
155 8a35f56c 2022-07-16 thomas title = optarg;
156 8a35f56c 2022-07-16 thomas proc_id = proc_getid(procs, nitems(procs), title);
157 8a35f56c 2022-07-16 thomas if (proc_id == PROC_MAX)
158 8a35f56c 2022-07-16 thomas fatalx("invalid process name");
160 8a35f56c 2022-07-16 thomas case 'I':
161 8a35f56c 2022-07-16 thomas proc_instance = strtonum(optarg, 0,
162 8a35f56c 2022-07-16 thomas PROC_MAX_INSTANCES, &errp);
163 8a35f56c 2022-07-16 thomas if (errp)
164 8a35f56c 2022-07-16 thomas fatalx("invalid process instance");
171 8a35f56c 2022-07-16 thomas /* log to stderr until daemonized */
172 8a35f56c 2022-07-16 thomas log_init(env->gotwebd_debug ? env->gotwebd_debug : 1, LOG_DAEMON);
174 8a35f56c 2022-07-16 thomas argc -= optind;
175 8a35f56c 2022-07-16 thomas if (argc > 0)
178 8a35f56c 2022-07-16 thomas ps = calloc(1, sizeof(*ps));
179 8a35f56c 2022-07-16 thomas if (ps == NULL)
180 8a35f56c 2022-07-16 thomas fatal("%s: calloc:", __func__);
182 8a35f56c 2022-07-16 thomas gotwebd_env = env;
183 8a35f56c 2022-07-16 thomas env->gotwebd_ps = ps;
184 8a35f56c 2022-07-16 thomas ps->ps_env = env;
185 8a35f56c 2022-07-16 thomas env->gotwebd_conffile = conffile;
187 8a35f56c 2022-07-16 thomas if (parse_config(env->gotwebd_conffile, env) == -1)
190 8a35f56c 2022-07-16 thomas if (env->gotwebd_noaction && !env->gotwebd_debug)
191 8a35f56c 2022-07-16 thomas env->gotwebd_debug = 1;
193 8a35f56c 2022-07-16 thomas /* check for root privileges */
194 8a35f56c 2022-07-16 thomas if (env->gotwebd_noaction == 0) {
195 8a35f56c 2022-07-16 thomas if (geteuid())
196 8a35f56c 2022-07-16 thomas fatalx("need root privileges");
199 8a35f56c 2022-07-16 thomas ps->ps_pw = getpwnam(GOTWEBD_USER);
200 8a35f56c 2022-07-16 thomas if (ps->ps_pw == NULL)
201 8a35f56c 2022-07-16 thomas fatalx("unknown user %s", GOTWEBD_USER);
203 8a35f56c 2022-07-16 thomas log_init(env->gotwebd_debug, LOG_DAEMON);
204 8a35f56c 2022-07-16 thomas log_setverbose(env->gotwebd_verbose);
206 8a35f56c 2022-07-16 thomas if (env->gotwebd_noaction)
207 8a35f56c 2022-07-16 thomas ps->ps_noaction = 1;
209 8a35f56c 2022-07-16 thomas ps->ps_instances[PROC_SOCKS] = env->prefork_gotwebd;
210 8a35f56c 2022-07-16 thomas ps->ps_instance = proc_instance;
211 8a35f56c 2022-07-16 thomas if (title != NULL)
212 8a35f56c 2022-07-16 thomas ps->ps_title[proc_id] = title;
214 8a35f56c 2022-07-16 thomas for (proc = 0; proc < nitems(procs); proc++)
215 8a35f56c 2022-07-16 thomas procs[proc].p_chroot = strlen(env->httpd_chroot) ?
216 8a35f56c 2022-07-16 thomas env->httpd_chroot : D_HTTPD_CHROOT;
218 8a35f56c 2022-07-16 thomas /* only the gotwebd returns */
219 8a35f56c 2022-07-16 thomas proc_init(ps, procs, nitems(procs), argc0, argv, proc_id);
221 8a35f56c 2022-07-16 thomas log_procinit("gotwebd");
222 8a35f56c 2022-07-16 thomas if (!env->gotwebd_debug && daemon(0, 0) == -1)
223 8a35f56c 2022-07-16 thomas fatal("can't daemonize");
225 8a35f56c 2022-07-16 thomas if (ps->ps_noaction == 0)
226 8a35f56c 2022-07-16 thomas log_info("%s startup", getprogname());
228 8a35f56c 2022-07-16 thomas event_init();
230 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigint, SIGINT, gotwebd_sighdlr, ps);
231 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigterm, SIGTERM, gotwebd_sighdlr, ps);
232 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsighup, SIGHUP, gotwebd_sighdlr, ps);
233 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigpipe, SIGPIPE, gotwebd_sighdlr, ps);
234 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigusr1, SIGUSR1, gotwebd_sighdlr, ps);
236 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigint, NULL);
237 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigterm, NULL);
238 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsighup, NULL);
239 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigpipe, NULL);
240 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigusr1, NULL);
242 8a35f56c 2022-07-16 thomas if (!env->gotwebd_noaction)
243 8a35f56c 2022-07-16 thomas proc_connect(ps);
245 8a35f56c 2022-07-16 thomas if (gotwebd_configure(env) == -1)
246 8a35f56c 2022-07-16 thomas fatalx("configuration failed");
248 8a35f56c 2022-07-16 thomas #ifdef PROFILE
249 8a35f56c 2022-07-16 thomas if (unveil("gmon.out", "rwc") != 0)
250 8a35f56c 2022-07-16 thomas err(1, "gmon.out");
253 8a35f56c 2022-07-16 thomas if (unveil(strlen(env->httpd_chroot) > 0 ? env->httpd_chroot :
254 8a35f56c 2022-07-16 thomas D_HTTPD_CHROOT, "rwc") == -1)
255 8a35f56c 2022-07-16 thomas err(1, "unveil");
257 8a35f56c 2022-07-16 thomas if (unveil(GOT_TMPDIR_STR, "rw") == -1)
258 8a35f56c 2022-07-16 thomas err(1, "unveil");
260 8a35f56c 2022-07-16 thomas if (unveil(GOTWEBD_CONF, "r") == -1)
261 8a35f56c 2022-07-16 thomas err(1, "unveil");
263 8a35f56c 2022-07-16 thomas if (unveil(NULL, NULL) != 0)
264 8a35f56c 2022-07-16 thomas err(1, "unveil");
266 8a35f56c 2022-07-16 thomas #ifndef PROFILE
267 8a35f56c 2022-07-16 thomas if (pledge("stdio rpath wpath cpath inet unix", NULL) == -1)
268 8a35f56c 2022-07-16 thomas err(1, "pledge");
271 8a35f56c 2022-07-16 thomas event_dispatch();
273 8a35f56c 2022-07-16 thomas log_debug("%s gotwebd exiting", getprogname());
275 8a35f56c 2022-07-16 thomas return (0);
279 8a35f56c 2022-07-16 thomas gotwebd_configure(struct gotwebd *env)
281 8a35f56c 2022-07-16 thomas struct server *srv;
282 8a35f56c 2022-07-16 thomas struct socket *sock;
285 8a35f56c 2022-07-16 thomas if (env->gotwebd_noaction) {
286 8a35f56c 2022-07-16 thomas fprintf(stderr, "configuration OK\n");
287 8a35f56c 2022-07-16 thomas proc_kill(env->gotwebd_ps);
291 8a35f56c 2022-07-16 thomas /* gotweb need to reload its config. */
292 8a35f56c 2022-07-16 thomas env->gotwebd_reload = env->prefork_gotwebd;
294 8a35f56c 2022-07-16 thomas /* send our gotweb servers */
295 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &env->servers, entry) {
296 8a35f56c 2022-07-16 thomas if (config_setserver(env, srv) == -1)
297 8a35f56c 2022-07-16 thomas fatalx("%s: send server error", __func__);
300 8a35f56c 2022-07-16 thomas /* send our sockets */
301 90d63d47 2022-08-16 thomas TAILQ_FOREACH(sock, &env->sockets, entry) {
302 8a35f56c 2022-07-16 thomas if (config_setsock(env, sock) == -1)
303 8a35f56c 2022-07-16 thomas fatalx("%s: send socket error", __func__);
304 8a35f56c 2022-07-16 thomas if (config_setfd(env, sock) == -1)
305 8a35f56c 2022-07-16 thomas fatalx("%s: send priv_fd error", __func__);
308 8a35f56c 2022-07-16 thomas for (id = 0; id < PROC_MAX; id++) {
309 8a35f56c 2022-07-16 thomas if (id == privsep_process)
310 8a35f56c 2022-07-16 thomas continue;
311 8a35f56c 2022-07-16 thomas proc_compose(env->gotwebd_ps, id, IMSG_CFG_DONE, NULL, 0);
314 8a35f56c 2022-07-16 thomas return (0);
318 8a35f56c 2022-07-16 thomas gotwebd_configure_done(struct gotwebd *env)
322 8a35f56c 2022-07-16 thomas if (env->gotwebd_reload == 0) {
323 8a35f56c 2022-07-16 thomas log_warnx("%s: configuration already finished", __func__);
327 8a35f56c 2022-07-16 thomas env->gotwebd_reload--;
328 8a35f56c 2022-07-16 thomas if (env->gotwebd_reload == 0) {
329 8a35f56c 2022-07-16 thomas for (id = 0; id < PROC_MAX; id++) {
330 8a35f56c 2022-07-16 thomas if (id == privsep_process)
331 8a35f56c 2022-07-16 thomas continue;
332 8a35f56c 2022-07-16 thomas proc_compose(env->gotwebd_ps, id, IMSG_CTL_START,
333 8a35f56c 2022-07-16 thomas NULL, 0);
339 8a35f56c 2022-07-16 thomas gotwebd_shutdown(void)
341 8a35f56c 2022-07-16 thomas proc_kill(gotwebd_env->gotwebd_ps);
343 8a35f56c 2022-07-16 thomas /* unlink(gotwebd_env->gotweb->gotweb_conf.gotweb_unix_socket_name); */
344 8a35f56c 2022-07-16 thomas /* free(gotwebd_env->gotweb); */
345 8a35f56c 2022-07-16 thomas free(gotwebd_env);
347 8a35f56c 2022-07-16 thomas log_warnx("gotwebd terminating");