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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
42 8a35f56c 2022-07-16 thomas #include "got_opentemp.h"
43 161663e7 2023-03-11 thomas #include "got_reference.h"
45 8a35f56c 2022-07-16 thomas #include "proc.h"
46 8a35f56c 2022-07-16 thomas #include "gotwebd.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 f7065961 2022-10-27 thomas while ((ch = getopt(argc, argv, "D:df:I:nP:v")) != -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 f7065961 2022-10-27 thomas case 'I':
148 f7065961 2022-10-27 thomas proc_instance = strtonum(optarg, 0,
149 f7065961 2022-10-27 thomas PROC_MAX_INSTANCES, &errp);
150 f7065961 2022-10-27 thomas if (errp)
151 f7065961 2022-10-27 thomas fatalx("invalid process instance");
153 8a35f56c 2022-07-16 thomas case 'n':
154 8a35f56c 2022-07-16 thomas env->gotwebd_debug = 2;
155 8a35f56c 2022-07-16 thomas env->gotwebd_noaction = 1;
157 8a35f56c 2022-07-16 thomas case 'P':
158 8a35f56c 2022-07-16 thomas title = optarg;
159 8a35f56c 2022-07-16 thomas proc_id = proc_getid(procs, nitems(procs), title);
160 8a35f56c 2022-07-16 thomas if (proc_id == PROC_MAX)
161 8a35f56c 2022-07-16 thomas fatalx("invalid process name");
163 f7065961 2022-10-27 thomas case 'v':
164 f7065961 2022-10-27 thomas env->gotwebd_verbose++;
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 0b16f49b 2023-06-22 thomas procs[proc].p_chroot = env->httpd_chroot;
217 8a35f56c 2022-07-16 thomas /* only the gotwebd returns */
218 8a35f56c 2022-07-16 thomas proc_init(ps, procs, nitems(procs), argc0, argv, proc_id);
220 8a35f56c 2022-07-16 thomas log_procinit("gotwebd");
221 8a35f56c 2022-07-16 thomas if (!env->gotwebd_debug && daemon(0, 0) == -1)
222 8a35f56c 2022-07-16 thomas fatal("can't daemonize");
224 8a35f56c 2022-07-16 thomas if (ps->ps_noaction == 0)
225 8a35f56c 2022-07-16 thomas log_info("%s startup", getprogname());
227 8a35f56c 2022-07-16 thomas event_init();
229 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigint, SIGINT, gotwebd_sighdlr, ps);
230 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigterm, SIGTERM, gotwebd_sighdlr, ps);
231 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsighup, SIGHUP, gotwebd_sighdlr, ps);
232 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigpipe, SIGPIPE, gotwebd_sighdlr, ps);
233 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigusr1, SIGUSR1, gotwebd_sighdlr, ps);
235 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigint, NULL);
236 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigterm, NULL);
237 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsighup, NULL);
238 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigpipe, NULL);
239 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigusr1, NULL);
241 8a35f56c 2022-07-16 thomas if (!env->gotwebd_noaction)
242 8a35f56c 2022-07-16 thomas proc_connect(ps);
244 8a35f56c 2022-07-16 thomas if (gotwebd_configure(env) == -1)
245 8a35f56c 2022-07-16 thomas fatalx("configuration failed");
247 8a35f56c 2022-07-16 thomas #ifdef PROFILE
248 8a35f56c 2022-07-16 thomas if (unveil("gmon.out", "rwc") != 0)
249 8a35f56c 2022-07-16 thomas err(1, "gmon.out");
252 0b16f49b 2023-06-22 thomas if (unveil(env->httpd_chroot, "rwc") == -1)
253 8a35f56c 2022-07-16 thomas err(1, "unveil");
255 8a35f56c 2022-07-16 thomas if (unveil(GOT_TMPDIR_STR, "rw") == -1)
256 8a35f56c 2022-07-16 thomas err(1, "unveil");
258 8a35f56c 2022-07-16 thomas if (unveil(GOTWEBD_CONF, "r") == -1)
259 8a35f56c 2022-07-16 thomas err(1, "unveil");
261 8a35f56c 2022-07-16 thomas if (unveil(NULL, NULL) != 0)
262 8a35f56c 2022-07-16 thomas err(1, "unveil");
264 8a35f56c 2022-07-16 thomas #ifndef PROFILE
265 8a35f56c 2022-07-16 thomas if (pledge("stdio rpath wpath cpath inet unix", NULL) == -1)
266 8a35f56c 2022-07-16 thomas err(1, "pledge");
269 8a35f56c 2022-07-16 thomas event_dispatch();
271 8a35f56c 2022-07-16 thomas log_debug("%s gotwebd exiting", getprogname());
273 8a35f56c 2022-07-16 thomas return (0);
277 8a35f56c 2022-07-16 thomas gotwebd_configure(struct gotwebd *env)
279 8a35f56c 2022-07-16 thomas struct server *srv;
280 8a35f56c 2022-07-16 thomas struct socket *sock;
283 8a35f56c 2022-07-16 thomas if (env->gotwebd_noaction) {
284 8a35f56c 2022-07-16 thomas fprintf(stderr, "configuration OK\n");
285 8a35f56c 2022-07-16 thomas proc_kill(env->gotwebd_ps);
289 8a35f56c 2022-07-16 thomas /* gotweb need to reload its config. */
290 8a35f56c 2022-07-16 thomas env->gotwebd_reload = env->prefork_gotwebd;
292 8a35f56c 2022-07-16 thomas /* send our gotweb servers */
293 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &env->servers, entry) {
294 8a35f56c 2022-07-16 thomas if (config_setserver(env, srv) == -1)
295 8a35f56c 2022-07-16 thomas fatalx("%s: send server error", __func__);
298 8a35f56c 2022-07-16 thomas /* send our sockets */
299 90d63d47 2022-08-16 thomas TAILQ_FOREACH(sock, &env->sockets, entry) {
300 8a35f56c 2022-07-16 thomas if (config_setsock(env, sock) == -1)
301 8a35f56c 2022-07-16 thomas fatalx("%s: send socket error", __func__);
302 8a35f56c 2022-07-16 thomas if (config_setfd(env, sock) == -1)
303 8a35f56c 2022-07-16 thomas fatalx("%s: send priv_fd error", __func__);
306 8a35f56c 2022-07-16 thomas for (id = 0; id < PROC_MAX; id++) {
307 8a35f56c 2022-07-16 thomas if (id == privsep_process)
308 8a35f56c 2022-07-16 thomas continue;
309 8a35f56c 2022-07-16 thomas proc_compose(env->gotwebd_ps, id, IMSG_CFG_DONE, NULL, 0);
312 8a35f56c 2022-07-16 thomas return (0);
316 8a35f56c 2022-07-16 thomas gotwebd_configure_done(struct gotwebd *env)
320 8a35f56c 2022-07-16 thomas if (env->gotwebd_reload == 0) {
321 8a35f56c 2022-07-16 thomas log_warnx("%s: configuration already finished", __func__);
325 8a35f56c 2022-07-16 thomas env->gotwebd_reload--;
326 8a35f56c 2022-07-16 thomas if (env->gotwebd_reload == 0) {
327 8a35f56c 2022-07-16 thomas for (id = 0; id < PROC_MAX; id++) {
328 8a35f56c 2022-07-16 thomas if (id == privsep_process)
329 8a35f56c 2022-07-16 thomas continue;
330 8a35f56c 2022-07-16 thomas proc_compose(env->gotwebd_ps, id, IMSG_CTL_START,
331 8a35f56c 2022-07-16 thomas NULL, 0);
337 8a35f56c 2022-07-16 thomas gotwebd_shutdown(void)
339 8a35f56c 2022-07-16 thomas proc_kill(gotwebd_env->gotwebd_ps);
341 8a35f56c 2022-07-16 thomas /* unlink(gotwebd_env->gotweb->gotweb_conf.gotweb_unix_socket_name); */
342 8a35f56c 2022-07-16 thomas /* free(gotwebd_env->gotweb); */
343 8a35f56c 2022-07-16 thomas free(gotwebd_env);
345 8a35f56c 2022-07-16 thomas log_warnx("gotwebd terminating");