Blame


1 3e4749f7 2020-10-02 op /*
2 a555e0d6 2022-07-04 op * Copyright (c) 2020, 2021, 2022 Omar Polo <op@omarpolo.com>
3 3e4749f7 2020-10-02 op *
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
7 3e4749f7 2020-10-02 op *
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3e4749f7 2020-10-02 op */
16 52418c8d 2021-02-12 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 3e4749f7 2020-10-02 op
19 8443bff7 2021-01-25 op #include <sys/stat.h>
20 8443bff7 2021-01-25 op
21 592fd624 2020-10-07 op #include <errno.h>
22 3e4749f7 2020-10-02 op #include <fcntl.h>
23 5777923b 2021-06-29 op #include <getopt.h>
24 0046c1fe 2023-06-06 op #include <locale.h>
25 7e1df73d 2021-03-31 op #include <libgen.h>
26 bcf5d929 2021-02-01 op #include <limits.h>
27 c9e97a6e 2022-12-24 op #include <grp.h>
28 ae08ec7d 2021-01-25 op #include <pwd.h>
29 0cf902af 2020-11-03 op #include <signal.h>
30 3e4749f7 2020-10-02 op #include <string.h>
31 eae52ad4 2023-06-06 op #include <syslog.h>
32 cc68fe70 2020-10-07 op
33 eae52ad4 2023-06-06 op #include "log.h"
34 c26f2460 2023-06-08 op #include "proc.h"
35 df5058c9 2023-06-05 op
36 c26f2460 2023-06-08 op #ifndef nitems
37 c26f2460 2023-06-08 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38 c26f2460 2023-06-08 op #endif
39 5777923b 2021-06-29 op
40 c26f2460 2023-06-08 op static int main_configure(struct conf *);
41 c26f2460 2023-06-08 op static void main_configure_done(struct conf *);
42 c26f2460 2023-06-08 op static void main_reload(struct conf *);
43 c26f2460 2023-06-08 op static void main_sig_handler(int, short, void *);
44 c26f2460 2023-06-08 op static int main_dispatch_server(int, struct privsep_proc *, struct imsg *);
45 c26f2460 2023-06-08 op static int main_dispatch_logger(int, struct privsep_proc *, struct imsg *);
46 c26f2460 2023-06-08 op static void __dead main_shutdown(struct conf *);
47 5af19830 2023-06-09 op static void main_print_conf(struct conf *);
48 c26f2460 2023-06-08 op
49 c26f2460 2023-06-08 op static struct privsep_proc procs[] = {
50 c26f2460 2023-06-08 op { "server", PROC_SERVER, main_dispatch_server, server },
51 c26f2460 2023-06-08 op { "logger", PROC_LOGGER, main_dispatch_logger, logger },
52 c26f2460 2023-06-08 op };
53 c26f2460 2023-06-08 op
54 7fff8aa6 2023-06-09 op static const char *opts = "c:D:fI:hnP:T:U:VvX:";
55 c26f2460 2023-06-08 op
56 e5d82d94 2022-03-19 op static const struct option longopts[] = {
57 5777923b 2021-06-29 op {"help", no_argument, NULL, 'h'},
58 5777923b 2021-06-29 op {"version", no_argument, NULL, 'V'},
59 5777923b 2021-06-29 op {NULL, 0, NULL, 0},
60 5777923b 2021-06-29 op };
61 15902770 2021-01-15 op
62 bc99d868 2021-03-19 op int sock4, sock6;
63 c26f2460 2023-06-08 op int privsep_process;
64 c26f2460 2023-06-08 op int pidfd = -1;
65 d672b8fb 2021-02-03 op
66 ca84625a 2023-06-08 op int debug, verbose;
67 ca84625a 2023-06-08 op
68 32fbc478 2022-09-08 op const char *config_path = "/etc/gmid.conf";
69 32fbc478 2022-09-08 op const char *pidfile;
70 881a9dd9 2021-01-16 op
71 3abf91b0 2021-02-07 op static void
72 9327bc04 2021-06-29 op usage(void)
73 3abf91b0 2021-02-07 op {
74 3abf91b0 2021-02-07 op fprintf(stderr,
75 0be2a537 2021-06-29 op "Version: " GMID_STRING "\n"
76 0ac785a6 2023-06-05 op "Usage: %s [-fnv] [-c config] [-D macro=value] [-P pidfile]\n",
77 9327bc04 2021-06-29 op getprogname());
78 3abf91b0 2021-02-07 op }
79 3abf91b0 2021-02-07 op
80 47b0ff10 2023-06-08 op /* used by the server process, defined here so gg can provide its own impl. */
81 47b0ff10 2023-06-08 op void
82 47b0ff10 2023-06-08 op log_request(struct client *c, char *meta, size_t l)
83 47b0ff10 2023-06-08 op {
84 af1dab18 2023-06-09 op struct conf *conf = c->conf;
85 47b0ff10 2023-06-08 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
86 47b0ff10 2023-06-08 op char *fmted;
87 47b0ff10 2023-06-08 op const char *t;
88 47b0ff10 2023-06-08 op size_t len;
89 47b0ff10 2023-06-08 op int ec;
90 47b0ff10 2023-06-08 op
91 47b0ff10 2023-06-08 op len = sizeof(c->addr);
92 47b0ff10 2023-06-08 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
93 47b0ff10 2023-06-08 op hbuf, sizeof(hbuf),
94 47b0ff10 2023-06-08 op sbuf, sizeof(sbuf),
95 47b0ff10 2023-06-08 op NI_NUMERICHOST | NI_NUMERICSERV);
96 47b0ff10 2023-06-08 op if (ec != 0)
97 47b0ff10 2023-06-08 op fatalx("getnameinfo: %s", gai_strerror(ec));
98 47b0ff10 2023-06-08 op
99 47b0ff10 2023-06-08 op if (c->iri.schema != NULL) {
100 47b0ff10 2023-06-08 op /* serialize the IRI */
101 47b0ff10 2023-06-08 op strlcpy(b, c->iri.schema, sizeof(b));
102 47b0ff10 2023-06-08 op strlcat(b, "://", sizeof(b));
103 47b0ff10 2023-06-08 op
104 47b0ff10 2023-06-08 op /* log the decoded host name, but if it was invalid
105 47b0ff10 2023-06-08 op * use the raw one. */
106 47b0ff10 2023-06-08 op if (*c->domain != '\0')
107 47b0ff10 2023-06-08 op strlcat(b, c->domain, sizeof(b));
108 47b0ff10 2023-06-08 op else
109 47b0ff10 2023-06-08 op strlcat(b, c->iri.host, sizeof(b));
110 47b0ff10 2023-06-08 op
111 47b0ff10 2023-06-08 op if (*c->iri.path != '/')
112 47b0ff10 2023-06-08 op strlcat(b, "/", sizeof(b));
113 47b0ff10 2023-06-08 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
114 47b0ff10 2023-06-08 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
115 47b0ff10 2023-06-08 op strlcat(b, "?", sizeof(b));
116 47b0ff10 2023-06-08 op strlcat(b, c->iri.query, sizeof(b));
117 47b0ff10 2023-06-08 op }
118 47b0ff10 2023-06-08 op } else {
119 47b0ff10 2023-06-08 op if ((t = c->req) == NULL)
120 47b0ff10 2023-06-08 op t = "";
121 47b0ff10 2023-06-08 op strlcpy(b, t, sizeof(b));
122 47b0ff10 2023-06-08 op }
123 47b0ff10 2023-06-08 op
124 47b0ff10 2023-06-08 op if ((t = memchr(meta, '\r', l)) == NULL)
125 47b0ff10 2023-06-08 op t = meta + len;
126 47b0ff10 2023-06-08 op
127 47b0ff10 2023-06-08 op ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
128 47b0ff10 2023-06-08 op (int)(t-meta), meta);
129 47b0ff10 2023-06-08 op if (ec == -1)
130 792f302a 2023-06-09 op fatal("asprintf");
131 47b0ff10 2023-06-08 op
132 af1dab18 2023-06-09 op proc_compose(conf->ps, PROC_LOGGER, IMSG_LOG_REQUEST,
133 47b0ff10 2023-06-08 op fmted, ec + 1);
134 47b0ff10 2023-06-08 op
135 47b0ff10 2023-06-08 op free(fmted);
136 47b0ff10 2023-06-08 op }
137 47b0ff10 2023-06-08 op
138 419a4235 2021-04-28 op static int
139 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
140 419a4235 2021-04-28 op {
141 419a4235 2021-04-28 op struct flock lock;
142 419a4235 2021-04-28 op int fd;
143 d672b8fb 2021-02-03 op
144 419a4235 2021-04-28 op if (pidfile == NULL)
145 419a4235 2021-04-28 op return -1;
146 419a4235 2021-04-28 op
147 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
148 df5058c9 2023-06-05 op fatal("can't open pidfile %s", pidfile);
149 419a4235 2021-04-28 op
150 419a4235 2021-04-28 op lock.l_start = 0;
151 419a4235 2021-04-28 op lock.l_len = 0;
152 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
153 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
154 419a4235 2021-04-28 op
155 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
156 df5058c9 2023-06-05 op fatalx("can't lock %s, gmid is already running?", pidfile);
157 419a4235 2021-04-28 op
158 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
159 df5058c9 2023-06-05 op fatal("ftruncate %s", pidfile);
160 419a4235 2021-04-28 op
161 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
162 419a4235 2021-04-28 op
163 419a4235 2021-04-28 op return fd;
164 419a4235 2021-04-28 op }
165 419a4235 2021-04-28 op
166 8d6ae384 2021-01-24 op int
167 8d6ae384 2021-01-24 op main(int argc, char **argv)
168 8d6ae384 2021-01-24 op {
169 af1dab18 2023-06-09 op struct conf *conf;
170 c26f2460 2023-06-08 op struct privsep *ps;
171 c26f2460 2023-06-08 op const char *errstr, *title = NULL;
172 7fff8aa6 2023-06-09 op const char *user = NULL, *chroot = NULL;
173 c26f2460 2023-06-08 op size_t i;
174 c26f2460 2023-06-08 op int ch, conftest = 0;
175 c26f2460 2023-06-08 op int proc_instance = 0;
176 c26f2460 2023-06-08 op int proc_id = PROC_PARENT;
177 c26f2460 2023-06-08 op int argc0 = argc;
178 501e489c 2021-01-24 op
179 0046c1fe 2023-06-06 op setlocale(LC_CTYPE, "");
180 0046c1fe 2023-06-06 op
181 eae52ad4 2023-06-06 op /* log to stderr until daemonized */
182 eae52ad4 2023-06-06 op log_init(1, LOG_DAEMON);
183 8d6ae384 2021-01-24 op
184 5777923b 2021-06-29 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
185 3e4749f7 2020-10-02 op switch (ch) {
186 0ac785a6 2023-06-05 op case 'c':
187 0ac785a6 2023-06-05 op config_path = absolutify_path(optarg);
188 0ac785a6 2023-06-05 op break;
189 f98e9045 2021-06-29 op case 'D':
190 f98e9045 2021-06-29 op if (cmdline_symset(optarg) == -1)
191 df5058c9 2023-06-05 op fatalx("could not parse macro definition: %s",
192 8a50fc03 2021-07-07 op optarg);
193 f98e9045 2021-06-29 op break;
194 0ac785a6 2023-06-05 op case 'f':
195 ca84625a 2023-06-08 op debug = 1;
196 46af8c6c 2021-01-27 op break;
197 3e4749f7 2020-10-02 op case 'h':
198 9327bc04 2021-06-29 op usage();
199 3e4749f7 2020-10-02 op return 0;
200 c26f2460 2023-06-08 op case 'I':
201 c26f2460 2023-06-08 op proc_instance = strtonum(optarg, 0, PROC_MAX_INSTANCES,
202 c26f2460 2023-06-08 op &errstr);
203 c26f2460 2023-06-08 op if (errstr != NULL)
204 c26f2460 2023-06-08 op fatalx("invalid process instance");
205 c26f2460 2023-06-08 op break;
206 15902770 2021-01-15 op case 'n':
207 f0a01fc7 2021-10-09 op conftest++;
208 721e2325 2020-11-18 op break;
209 8e8b2e25 2021-04-28 op case 'P':
210 f1f13cb7 2023-06-08 op pidfile = absolutify_path(optarg);
211 8e8b2e25 2021-04-28 op break;
212 c26f2460 2023-06-08 op case 'T':
213 c26f2460 2023-06-08 op title = optarg;
214 c26f2460 2023-06-08 op proc_id = proc_getid(procs, nitems(procs), title);
215 c26f2460 2023-06-08 op if (proc_id == PROC_MAX)
216 c26f2460 2023-06-08 op fatalx("invalid process name");
217 c26f2460 2023-06-08 op break;
218 7fff8aa6 2023-06-09 op case 'U':
219 7fff8aa6 2023-06-09 op user = optarg;
220 7fff8aa6 2023-06-09 op break;
221 5777923b 2021-06-29 op case 'V':
222 fdb43a4c 2021-06-29 op puts("Version: " GMID_STRING);
223 5777923b 2021-06-29 op return 0;
224 8904fa0e 2021-01-27 op case 'v':
225 ca84625a 2023-06-08 op verbose = 1;
226 8904fa0e 2021-01-27 op break;
227 7fff8aa6 2023-06-09 op case 'X':
228 7fff8aa6 2023-06-09 op chroot = optarg;
229 7fff8aa6 2023-06-09 op break;
230 3e4749f7 2020-10-02 op default:
231 9327bc04 2021-06-29 op usage();
232 3e4749f7 2020-10-02 op return 1;
233 3e4749f7 2020-10-02 op }
234 3e4749f7 2020-10-02 op }
235 3e4749f7 2020-10-02 op
236 c26f2460 2023-06-08 op if (argc - optind != 0)
237 d29a2ee2 2022-09-06 op usage();
238 d29a2ee2 2022-09-06 op
239 af1dab18 2023-06-09 op conf = config_new();
240 af1dab18 2023-06-09 op
241 7fff8aa6 2023-06-09 op /*
242 7fff8aa6 2023-06-09 op * Only the parent loads the config, the others get user and
243 7fff8aa6 2023-06-09 op * chroot via flags and the rest via imsg.
244 7fff8aa6 2023-06-09 op */
245 7fff8aa6 2023-06-09 op if (proc_id == PROC_PARENT) {
246 7fff8aa6 2023-06-09 op if (parse_conf(conf, config_path) == -1)
247 7fff8aa6 2023-06-09 op fatalx("failed to load configuration file");
248 7fff8aa6 2023-06-09 op if (*conf->chroot != '\0' && *conf->user == '\0')
249 7fff8aa6 2023-06-09 op fatalx("can't chroot without a user to switch to.");
250 7fff8aa6 2023-06-09 op } else {
251 7fff8aa6 2023-06-09 op if (user)
252 7fff8aa6 2023-06-09 op strlcpy(conf->user, user, sizeof(conf->user));
253 7fff8aa6 2023-06-09 op if (chroot)
254 7fff8aa6 2023-06-09 op strlcpy(conf->chroot, chroot, sizeof(conf->chroot));
255 7fff8aa6 2023-06-09 op }
256 d672b8fb 2021-02-03 op
257 132cae8c 2021-01-18 op if (conftest) {
258 f0a01fc7 2021-10-09 op fprintf(stderr, "config OK\n");
259 f0a01fc7 2021-10-09 op if (conftest > 1)
260 5af19830 2023-06-09 op main_print_conf(conf);
261 132cae8c 2021-01-18 op return 0;
262 132cae8c 2021-01-18 op }
263 4a28dd01 2020-12-28 op
264 c26f2460 2023-06-08 op if ((ps = calloc(1, sizeof(*ps))) == NULL)
265 c26f2460 2023-06-08 op fatal("calloc");
266 af1dab18 2023-06-09 op ps->ps_env = conf;
267 af1dab18 2023-06-09 op conf->ps = ps;
268 af1dab18 2023-06-09 op if (*conf->user) {
269 c26f2460 2023-06-08 op if (geteuid())
270 c26f2460 2023-06-08 op fatalx("need root privileges");
271 af1dab18 2023-06-09 op if ((ps->ps_pw = getpwnam(conf->user)) == NULL)
272 af1dab18 2023-06-09 op fatalx("unknown user %s", conf->user);
273 c26f2460 2023-06-08 op }
274 8a50fc03 2021-07-07 op
275 af1dab18 2023-06-09 op ps->ps_instances[PROC_SERVER] = conf->prefork;
276 c26f2460 2023-06-08 op ps->ps_instance = proc_instance;
277 c26f2460 2023-06-08 op if (title != NULL)
278 c26f2460 2023-06-08 op ps->ps_title[proc_id] = title;
279 c26f2460 2023-06-08 op
280 af1dab18 2023-06-09 op if (*conf->chroot != '\0') {
281 c26f2460 2023-06-08 op for (i = 0; i < nitems(procs); ++i)
282 af1dab18 2023-06-09 op procs[i].p_chroot = conf->chroot;
283 0170ba02 2021-01-17 op }
284 c26f2460 2023-06-08 op
285 ca84625a 2023-06-08 op log_init(debug, LOG_DAEMON);
286 ca84625a 2023-06-08 op log_setverbose(verbose);
287 c26f2460 2023-06-08 op if (title != NULL)
288 c26f2460 2023-06-08 op log_procinit(title);
289 4a28dd01 2020-12-28 op
290 c26f2460 2023-06-08 op /* only the parent returns */
291 ca84625a 2023-06-08 op proc_init(ps, procs, nitems(procs), debug, argc0, argv, proc_id);
292 3e4749f7 2020-10-02 op
293 c26f2460 2023-06-08 op log_procinit("main");
294 ca84625a 2023-06-08 op if (!debug && daemon(0, 0) == -1)
295 c26f2460 2023-06-08 op fatal("daemon");
296 3841a369 2021-04-20 op
297 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
298 8e8b2e25 2021-04-28 op
299 c26f2460 2023-06-08 op sandbox_main_process();
300 c26f2460 2023-06-08 op
301 c26f2460 2023-06-08 op event_init();
302 c26f2460 2023-06-08 op
303 c26f2460 2023-06-08 op signal(SIGPIPE, SIG_IGN);
304 b9c9123b 2021-03-20 op
305 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigint, SIGINT, main_sig_handler, ps);
306 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigterm, SIGTERM, main_sig_handler, ps);
307 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigchld, SIGCHLD, main_sig_handler, ps);
308 c26f2460 2023-06-08 op signal_set(&ps->ps_evsighup, SIGHUP, main_sig_handler, ps);
309 ca21e100 2021-02-04 op
310 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigint, NULL);
311 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigterm, NULL);
312 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigchld, NULL);
313 c26f2460 2023-06-08 op signal_add(&ps->ps_evsighup, NULL);
314 1d3eb470 2021-03-20 op
315 c26f2460 2023-06-08 op proc_connect(ps);
316 ca21e100 2021-02-04 op
317 af1dab18 2023-06-09 op if (main_configure(conf) == -1)
318 c26f2460 2023-06-08 op fatal("configuration failed");
319 bc99d868 2021-03-19 op
320 c26f2460 2023-06-08 op event_dispatch();
321 af1dab18 2023-06-09 op main_shutdown(conf);
322 c26f2460 2023-06-08 op /* NOTREACHED */
323 c26f2460 2023-06-08 op return 0;
324 c26f2460 2023-06-08 op }
325 ca21e100 2021-02-04 op
326 c26f2460 2023-06-08 op static int
327 c26f2460 2023-06-08 op main_configure(struct conf *conf)
328 c26f2460 2023-06-08 op {
329 c26f2460 2023-06-08 op struct privsep *ps = conf->ps;
330 ca21e100 2021-02-04 op
331 c26f2460 2023-06-08 op conf->reload = conf->prefork;
332 ca21e100 2021-02-04 op
333 c26f2460 2023-06-08 op if (proc_compose(ps, PROC_SERVER, IMSG_RECONF_START, NULL, 0) == -1)
334 c26f2460 2023-06-08 op return -1;
335 ca21e100 2021-02-04 op
336 e45334e6 2023-06-09 op if (config_send(conf) == -1)
337 c26f2460 2023-06-08 op return -1;
338 c26f2460 2023-06-08 op
339 c26f2460 2023-06-08 op if (proc_compose(ps, PROC_SERVER, IMSG_RECONF_END, NULL, 0) == -1)
340 c26f2460 2023-06-08 op return -1;
341 c26f2460 2023-06-08 op
342 c26f2460 2023-06-08 op return 0;
343 c26f2460 2023-06-08 op }
344 c26f2460 2023-06-08 op
345 c26f2460 2023-06-08 op static void
346 c26f2460 2023-06-08 op main_configure_done(struct conf *conf)
347 c26f2460 2023-06-08 op {
348 c26f2460 2023-06-08 op if (conf->reload == 0) {
349 c26f2460 2023-06-08 op log_warnx("configuration already done");
350 c26f2460 2023-06-08 op return;
351 ca21e100 2021-02-04 op }
352 1d3eb470 2021-03-20 op
353 c26f2460 2023-06-08 op conf->reload--;
354 c26f2460 2023-06-08 op /* send IMSG_CTL_START? */
355 c26f2460 2023-06-08 op }
356 c26f2460 2023-06-08 op
357 c26f2460 2023-06-08 op static void
358 c26f2460 2023-06-08 op main_reload(struct conf *conf)
359 c26f2460 2023-06-08 op {
360 c26f2460 2023-06-08 op if (conf->reload) {
361 c26f2460 2023-06-08 op log_debug("%s: already in progress: %d pending",
362 c26f2460 2023-06-08 op __func__, conf->reload);
363 c26f2460 2023-06-08 op return;
364 5c485529 2022-09-10 op }
365 1d3eb470 2021-03-20 op
366 c26f2460 2023-06-08 op log_debug("%s: config file %s", __func__, config_path);
367 af1dab18 2023-06-09 op config_purge(conf);
368 1d3eb470 2021-03-20 op
369 68368f4c 2023-06-09 op if (parse_conf(conf, config_path) == -1) {
370 68368f4c 2023-06-09 op log_warnx("failed to parse the config");
371 68368f4c 2023-06-09 op return;
372 68368f4c 2023-06-09 op }
373 68368f4c 2023-06-09 op
374 c26f2460 2023-06-08 op main_configure(conf);
375 c26f2460 2023-06-08 op }
376 c26f2460 2023-06-08 op
377 c26f2460 2023-06-08 op static void
378 c26f2460 2023-06-08 op main_sig_handler(int sig, short ev, void *arg)
379 c26f2460 2023-06-08 op {
380 c26f2460 2023-06-08 op struct privsep *ps = arg;
381 c26f2460 2023-06-08 op
382 c26f2460 2023-06-08 op /*
383 c26f2460 2023-06-08 op * Normal signal handler rules don't apply here because libevent
384 c26f2460 2023-06-08 op * decouples for us.
385 c26f2460 2023-06-08 op */
386 c26f2460 2023-06-08 op
387 c26f2460 2023-06-08 op switch (sig) {
388 c26f2460 2023-06-08 op case SIGHUP:
389 c26f2460 2023-06-08 op if (privsep_process != PROC_PARENT)
390 c26f2460 2023-06-08 op return;
391 c26f2460 2023-06-08 op log_info("reload requested with SIGHUP");
392 c26f2460 2023-06-08 op main_reload(ps->ps_env);
393 c26f2460 2023-06-08 op break;
394 c26f2460 2023-06-08 op case SIGCHLD:
395 c26f2460 2023-06-08 op log_warnx("one child died, quitting");
396 c26f2460 2023-06-08 op /* fallthrough */
397 c26f2460 2023-06-08 op case SIGTERM:
398 c26f2460 2023-06-08 op case SIGINT:
399 c26f2460 2023-06-08 op main_shutdown(ps->ps_env);
400 c26f2460 2023-06-08 op break;
401 c26f2460 2023-06-08 op default:
402 c26f2460 2023-06-08 op fatalx("unexpected signal %d", sig);
403 c26f2460 2023-06-08 op }
404 c26f2460 2023-06-08 op }
405 c26f2460 2023-06-08 op
406 c26f2460 2023-06-08 op static int
407 c26f2460 2023-06-08 op main_dispatch_server(int fd, struct privsep_proc *p, struct imsg *imsg)
408 c26f2460 2023-06-08 op {
409 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
410 c26f2460 2023-06-08 op struct conf *conf = ps->ps_env;
411 c26f2460 2023-06-08 op
412 c26f2460 2023-06-08 op switch (imsg->hdr.type) {
413 c26f2460 2023-06-08 op case IMSG_RECONF_DONE:
414 c26f2460 2023-06-08 op main_configure_done(conf);
415 c26f2460 2023-06-08 op break;
416 c26f2460 2023-06-08 op default:
417 c26f2460 2023-06-08 op return -1;
418 c26f2460 2023-06-08 op }
419 c26f2460 2023-06-08 op
420 c26f2460 2023-06-08 op return 0;
421 c26f2460 2023-06-08 op }
422 c26f2460 2023-06-08 op
423 c26f2460 2023-06-08 op static int
424 c26f2460 2023-06-08 op main_dispatch_logger(int fd, struct privsep_proc *p, struct imsg *imsg)
425 c26f2460 2023-06-08 op {
426 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
427 c26f2460 2023-06-08 op struct conf *conf = ps->ps_env;
428 c26f2460 2023-06-08 op
429 c26f2460 2023-06-08 op switch (imsg->hdr.type) {
430 c26f2460 2023-06-08 op case IMSG_RECONF_DONE:
431 c26f2460 2023-06-08 op main_configure_done(conf);
432 c26f2460 2023-06-08 op break;
433 c26f2460 2023-06-08 op default:
434 c26f2460 2023-06-08 op return -1;
435 c26f2460 2023-06-08 op }
436 c26f2460 2023-06-08 op
437 c26f2460 2023-06-08 op return 0;
438 c26f2460 2023-06-08 op }
439 c26f2460 2023-06-08 op
440 c26f2460 2023-06-08 op static void __dead
441 c26f2460 2023-06-08 op main_shutdown(struct conf *conf)
442 c26f2460 2023-06-08 op {
443 c26f2460 2023-06-08 op proc_kill(conf->ps);
444 af1dab18 2023-06-09 op config_purge(conf);
445 c26f2460 2023-06-08 op free(conf->ps);
446 c26f2460 2023-06-08 op /* free(conf); */
447 c26f2460 2023-06-08 op
448 c26f2460 2023-06-08 op log_info("parent terminating, pid %d", getpid());
449 c26f2460 2023-06-08 op
450 8e8b2e25 2021-04-28 op if (pidfd != -1)
451 8e8b2e25 2021-04-28 op close(pidfd);
452 8e8b2e25 2021-04-28 op
453 c26f2460 2023-06-08 op exit(0);
454 5af19830 2023-06-09 op }
455 5af19830 2023-06-09 op
456 5af19830 2023-06-09 op static void
457 5af19830 2023-06-09 op main_print_conf(struct conf *conf)
458 5af19830 2023-06-09 op {
459 5af19830 2023-06-09 op struct vhost *h;
460 5af19830 2023-06-09 op /* struct location *l; */
461 5af19830 2023-06-09 op /* struct envlist *e; */
462 5af19830 2023-06-09 op /* struct alist *a; */
463 5af19830 2023-06-09 op
464 5af19830 2023-06-09 op if (*conf->chroot != '\0')
465 5af19830 2023-06-09 op printf("chroot \"%s\"\n", conf->chroot);
466 5af19830 2023-06-09 op printf("ipv6 %s\n", conf->ipv6 ? "on" : "off");
467 5af19830 2023-06-09 op /* XXX: defined mimes? */
468 5af19830 2023-06-09 op printf("port %d\n", conf->port);
469 5af19830 2023-06-09 op printf("prefork %d\n", conf->prefork);
470 5af19830 2023-06-09 op /* XXX: protocols? */
471 5af19830 2023-06-09 op if (*conf->user != '\0')
472 5af19830 2023-06-09 op printf("user \"%s\"\n", conf->user);
473 5af19830 2023-06-09 op
474 5af19830 2023-06-09 op TAILQ_FOREACH(h, &conf->hosts, vhosts) {
475 5af19830 2023-06-09 op printf("\nserver \"%s\" {\n", h->domain);
476 5af19830 2023-06-09 op printf(" cert \"%s\"\n", h->cert);
477 5af19830 2023-06-09 op printf(" key \"%s\"\n", h->key);
478 5af19830 2023-06-09 op /* TODO: print locations... */
479 5af19830 2023-06-09 op printf("}\n");
480 5af19830 2023-06-09 op }
481 3e4749f7 2020-10-02 op }