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 <pwd.h>
35 #include <signal.h>
36 #include <syslog.h>
37 #include <unistd.h>
38 #include <ctype.h>
40 #include "got_compat.h"
41 #include "got_opentemp.h"
42 #include "got_reference.h"
44 #include "proc.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 int gotwebd_dispatch_sockets(int, struct privsep_proc *, struct imsg *);
56 struct gotwebd *gotwebd_env;
58 static struct privsep_proc procs[] = {
59 { "sockets", PROC_SOCKS, gotwebd_dispatch_sockets, sockets,
60 sockets_shutdown },
61 };
63 int
64 gotwebd_dispatch_sockets(int fd, struct privsep_proc *p, struct imsg *imsg)
65 {
66 struct privsep *ps = p->p_ps;
67 struct gotwebd *env = ps->ps_env;
69 switch (imsg->hdr.type) {
70 case IMSG_CFG_DONE:
71 gotwebd_configure_done(env);
72 break;
73 default:
74 return (-1);
75 }
77 return (0);
78 }
80 void
81 gotwebd_sighdlr(int sig, short event, void *arg)
82 {
83 /* struct privsep *ps = arg; */
85 if (privsep_process != PROC_GOTWEBD)
86 return;
88 switch (sig) {
89 case SIGHUP:
90 log_info("%s: ignoring SIGHUP", __func__);
91 break;
92 case SIGPIPE:
93 log_info("%s: ignoring SIGPIPE", __func__);
94 break;
95 case SIGUSR1:
96 log_info("%s: ignoring SIGUSR1", __func__);
97 break;
98 case SIGTERM:
99 case SIGINT:
100 gotwebd_shutdown();
101 break;
102 default:
103 fatalx("unexpected signal");
107 __dead void
108 usage(void)
110 fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
111 getprogname());
112 exit(1);
115 int
116 main(int argc, char **argv)
118 struct gotwebd *env;
119 struct privsep *ps;
120 unsigned int proc;
121 int ch;
122 const char *conffile = GOTWEBD_CONF;
123 enum privsep_procid proc_id = PROC_GOTWEBD;
124 int proc_instance = 0;
125 const char *errp, *title = NULL;
126 int argc0 = argc;
128 env = calloc(1, sizeof(*env));
129 if (env == NULL)
130 fatal("%s: calloc", __func__);
132 /* XXX: add s and S for both sockets */
133 while ((ch = getopt(argc, argv, "D:df:I:nP:v")) != -1) {
134 switch (ch) {
135 case 'D':
136 if (cmdline_symset(optarg) < 0)
137 log_warnx("could not parse macro definition %s",
138 optarg);
139 break;
140 case 'd':
141 env->gotwebd_debug = 2;
142 break;
143 case 'f':
144 conffile = optarg;
145 break;
146 case 'I':
147 proc_instance = strtonum(optarg, 0,
148 PROC_MAX_INSTANCES, &errp);
149 if (errp)
150 fatalx("invalid process instance");
151 break;
152 case 'n':
153 env->gotwebd_debug = 2;
154 env->gotwebd_noaction = 1;
155 break;
156 case 'P':
157 title = optarg;
158 proc_id = proc_getid(procs, nitems(procs), title);
159 if (proc_id == PROC_MAX)
160 fatalx("invalid process name");
161 break;
162 case 'v':
163 env->gotwebd_verbose++;
164 break;
165 default:
166 usage();
170 /* log to stderr until daemonized */
171 log_init(env->gotwebd_debug ? env->gotwebd_debug : 1, LOG_DAEMON);
173 argc -= optind;
174 if (argc > 0)
175 usage();
177 ps = calloc(1, sizeof(*ps));
178 if (ps == NULL)
179 fatal("%s: calloc:", __func__);
181 gotwebd_env = env;
182 env->gotwebd_ps = ps;
183 ps->ps_env = env;
184 env->gotwebd_conffile = conffile;
186 if (parse_config(env->gotwebd_conffile, env) == -1)
187 exit(1);
189 if (env->gotwebd_noaction && !env->gotwebd_debug)
190 env->gotwebd_debug = 1;
192 /* check for root privileges */
193 if (env->gotwebd_noaction == 0) {
194 if (geteuid())
195 fatalx("need root privileges");
198 ps->ps_pw = getpwnam(GOTWEBD_USER);
199 if (ps->ps_pw == NULL)
200 fatalx("unknown user %s", GOTWEBD_USER);
202 log_init(env->gotwebd_debug, LOG_DAEMON);
203 log_setverbose(env->gotwebd_verbose);
205 if (env->gotwebd_noaction)
206 ps->ps_noaction = 1;
208 ps->ps_instances[PROC_SOCKS] = env->prefork_gotwebd;
209 ps->ps_instance = proc_instance;
210 if (title != NULL)
211 ps->ps_title[proc_id] = title;
213 for (proc = 0; proc < nitems(procs); proc++)
214 procs[proc].p_chroot = env->httpd_chroot;
216 /* only the gotwebd returns */
217 proc_init(ps, procs, nitems(procs), argc0, argv, proc_id);
219 log_procinit("gotwebd");
220 if (!env->gotwebd_debug && daemon(0, 0) == -1)
221 fatal("can't daemonize");
223 if (ps->ps_noaction == 0)
224 log_info("%s startup", getprogname());
226 event_init();
228 signal_set(&ps->ps_evsigint, SIGINT, gotwebd_sighdlr, ps);
229 signal_set(&ps->ps_evsigterm, SIGTERM, gotwebd_sighdlr, ps);
230 signal_set(&ps->ps_evsighup, SIGHUP, gotwebd_sighdlr, ps);
231 signal_set(&ps->ps_evsigpipe, SIGPIPE, gotwebd_sighdlr, ps);
232 signal_set(&ps->ps_evsigusr1, SIGUSR1, gotwebd_sighdlr, ps);
234 signal_add(&ps->ps_evsigint, NULL);
235 signal_add(&ps->ps_evsigterm, NULL);
236 signal_add(&ps->ps_evsighup, NULL);
237 signal_add(&ps->ps_evsigpipe, NULL);
238 signal_add(&ps->ps_evsigusr1, NULL);
240 if (!env->gotwebd_noaction)
241 proc_connect(ps);
243 if (gotwebd_configure(env) == -1)
244 fatalx("configuration failed");
246 #ifdef PROFILE
247 if (unveil("gmon.out", "rwc") != 0)
248 err(1, "gmon.out");
249 #endif
251 if (unveil(env->httpd_chroot, "rwc") == -1)
252 err(1, "unveil");
254 if (unveil(GOT_TMPDIR_STR, "rw") == -1)
255 err(1, "unveil");
257 if (unveil(GOTWEBD_CONF, "r") == -1)
258 err(1, "unveil");
260 if (unveil(NULL, NULL) != 0)
261 err(1, "unveil");
263 #ifndef PROFILE
264 if (pledge("stdio rpath wpath cpath inet unix", NULL) == -1)
265 err(1, "pledge");
266 #endif
268 event_dispatch();
270 log_debug("%s gotwebd exiting", getprogname());
272 return (0);
275 int
276 gotwebd_configure(struct gotwebd *env)
278 struct server *srv;
279 struct socket *sock;
280 int id;
282 if (env->gotwebd_noaction) {
283 fprintf(stderr, "configuration OK\n");
284 proc_kill(env->gotwebd_ps);
285 exit(0);
288 /* gotweb need to reload its config. */
289 env->gotwebd_reload = env->prefork_gotwebd;
291 /* send our gotweb servers */
292 TAILQ_FOREACH(srv, &env->servers, entry) {
293 if (config_setserver(env, srv) == -1)
294 fatalx("%s: send server error", __func__);
297 /* send our sockets */
298 TAILQ_FOREACH(sock, &env->sockets, entry) {
299 if (config_setsock(env, sock) == -1)
300 fatalx("%s: send socket error", __func__);
301 if (config_setfd(env, sock) == -1)
302 fatalx("%s: send priv_fd error", __func__);
305 for (id = 0; id < PROC_MAX; id++) {
306 if (id == privsep_process)
307 continue;
308 proc_compose(env->gotwebd_ps, id, IMSG_CFG_DONE, NULL, 0);
311 return (0);
314 void
315 gotwebd_configure_done(struct gotwebd *env)
317 int id;
319 if (env->gotwebd_reload == 0) {
320 log_warnx("%s: configuration already finished", __func__);
321 return;
324 env->gotwebd_reload--;
325 if (env->gotwebd_reload == 0) {
326 for (id = 0; id < PROC_MAX; id++) {
327 if (id == privsep_process)
328 continue;
329 proc_compose(env->gotwebd_ps, id, IMSG_CTL_START,
330 NULL, 0);
335 void
336 gotwebd_shutdown(void)
338 proc_kill(gotwebd_env->gotwebd_ps);
340 /* unlink(gotwebd_env->gotweb->gotweb_conf.gotweb_unix_socket_name); */
341 /* free(gotwebd_env->gotweb); */
342 free(gotwebd_env);
344 log_warnx("gotwebd terminating");
345 exit(0);