Blame


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