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 86693a33 2023-06-11 op static int main_dispatch_crypto(int, struct privsep_proc *, struct imsg *);
46 c26f2460 2023-06-08 op static int main_dispatch_logger(int, struct privsep_proc *, struct imsg *);
47 c26f2460 2023-06-08 op static void __dead main_shutdown(struct conf *);
48 5af19830 2023-06-09 op static void main_print_conf(struct conf *);
49 c26f2460 2023-06-08 op
50 c26f2460 2023-06-08 op static struct privsep_proc procs[] = {
51 c26f2460 2023-06-08 op { "server", PROC_SERVER, main_dispatch_server, server },
52 86693a33 2023-06-11 op { "crypto", PROC_CRYPTO, main_dispatch_crypto, crypto },
53 c26f2460 2023-06-08 op { "logger", PROC_LOGGER, main_dispatch_logger, logger },
54 c26f2460 2023-06-08 op };
55 c26f2460 2023-06-08 op
56 7fff8aa6 2023-06-09 op static const char *opts = "c:D:fI:hnP:T:U:VvX:";
57 c26f2460 2023-06-08 op
58 e5d82d94 2022-03-19 op static const struct option longopts[] = {
59 5777923b 2021-06-29 op {"help", no_argument, NULL, 'h'},
60 5777923b 2021-06-29 op {"version", no_argument, NULL, 'V'},
61 5777923b 2021-06-29 op {NULL, 0, NULL, 0},
62 5777923b 2021-06-29 op };
63 15902770 2021-01-15 op
64 bc99d868 2021-03-19 op int sock4, sock6;
65 c26f2460 2023-06-08 op int privsep_process;
66 c26f2460 2023-06-08 op int pidfd = -1;
67 d672b8fb 2021-02-03 op
68 ca84625a 2023-06-08 op int debug, verbose;
69 ca84625a 2023-06-08 op
70 32fbc478 2022-09-08 op const char *config_path = "/etc/gmid.conf";
71 32fbc478 2022-09-08 op const char *pidfile;
72 881a9dd9 2021-01-16 op
73 3abf91b0 2021-02-07 op static void
74 9327bc04 2021-06-29 op usage(void)
75 3abf91b0 2021-02-07 op {
76 3abf91b0 2021-02-07 op fprintf(stderr,
77 0be2a537 2021-06-29 op "Version: " GMID_STRING "\n"
78 0ac785a6 2023-06-05 op "Usage: %s [-fnv] [-c config] [-D macro=value] [-P pidfile]\n",
79 9327bc04 2021-06-29 op getprogname());
80 3abf91b0 2021-02-07 op }
81 3abf91b0 2021-02-07 op
82 47b0ff10 2023-06-08 op /* used by the server process, defined here so gg can provide its own impl. */
83 47b0ff10 2023-06-08 op void
84 2c381068 2023-07-01 op log_request(struct client *c, int code, const char *meta)
85 47b0ff10 2023-06-08 op {
86 af1dab18 2023-06-09 op struct conf *conf = c->conf;
87 abd261d2 2023-07-25 op char tstamp[64], rfc3339[32];
88 ed164e72 2023-06-26 op char b[GEMINI_URL_LEN];
89 47b0ff10 2023-06-08 op char *fmted;
90 47b0ff10 2023-06-08 op const char *t;
91 abd261d2 2023-07-25 op struct tm *tm;
92 abd261d2 2023-07-25 op time_t now;
93 47b0ff10 2023-06-08 op int ec;
94 47b0ff10 2023-06-08 op
95 abd261d2 2023-07-25 op if ((now = time(NULL)) == -1)
96 abd261d2 2023-07-25 op fatal("time");
97 abd261d2 2023-07-25 op if ((tm = localtime(&now)) == NULL)
98 abd261d2 2023-07-25 op fatal("localtime");
99 abd261d2 2023-07-25 op if (strftime(tstamp, sizeof(tstamp), "%d/%b%Y:%H:%M:%S %z", tm) == 0)
100 abd261d2 2023-07-25 op fatal("strftime");
101 abd261d2 2023-07-25 op if (strftime(rfc3339, sizeof(rfc3339), "%FT%T%z", tm) == 0)
102 abd261d2 2023-07-25 op fatal("strftime");
103 abd261d2 2023-07-25 op
104 47b0ff10 2023-06-08 op if (c->iri.schema != NULL) {
105 47b0ff10 2023-06-08 op /* serialize the IRI */
106 47b0ff10 2023-06-08 op strlcpy(b, c->iri.schema, sizeof(b));
107 47b0ff10 2023-06-08 op strlcat(b, "://", sizeof(b));
108 47b0ff10 2023-06-08 op
109 47b0ff10 2023-06-08 op /* log the decoded host name, but if it was invalid
110 47b0ff10 2023-06-08 op * use the raw one. */
111 47b0ff10 2023-06-08 op if (*c->domain != '\0')
112 47b0ff10 2023-06-08 op strlcat(b, c->domain, sizeof(b));
113 47b0ff10 2023-06-08 op else
114 47b0ff10 2023-06-08 op strlcat(b, c->iri.host, sizeof(b));
115 47b0ff10 2023-06-08 op
116 47b0ff10 2023-06-08 op if (*c->iri.path != '/')
117 47b0ff10 2023-06-08 op strlcat(b, "/", sizeof(b));
118 47b0ff10 2023-06-08 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
119 47b0ff10 2023-06-08 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
120 47b0ff10 2023-06-08 op strlcat(b, "?", sizeof(b));
121 47b0ff10 2023-06-08 op strlcat(b, c->iri.query, sizeof(b));
122 47b0ff10 2023-06-08 op }
123 47b0ff10 2023-06-08 op } else {
124 47b0ff10 2023-06-08 op if ((t = c->req) == NULL)
125 47b0ff10 2023-06-08 op t = "";
126 47b0ff10 2023-06-08 op strlcpy(b, t, sizeof(b));
127 47b0ff10 2023-06-08 op }
128 47b0ff10 2023-06-08 op
129 abd261d2 2023-07-25 op switch (conf->log_format) {
130 abd261d2 2023-07-25 op case LOG_FORMAT_LEGACY:
131 abd261d2 2023-07-25 op ec = asprintf(&fmted, "%s:%s GET %s %d %s", c->rhost,
132 abd261d2 2023-07-25 op c->rserv, b, code, meta);
133 abd261d2 2023-07-25 op break;
134 abd261d2 2023-07-25 op
135 abd261d2 2023-07-25 op case LOG_FORMAT_CONDENSED:
136 abd261d2 2023-07-25 op /*
137 abd261d2 2023-07-25 op * XXX the first '-' is the remote user name, we
138 abd261d2 2023-07-25 op * could use the client cert for it.
139 abd261d2 2023-07-25 op *
140 abd261d2 2023-07-25 op * XXX it should log the size of the response
141 abd261d2 2023-07-25 op */
142 abd261d2 2023-07-25 op ec = asprintf(&fmted, "%s %s - %s %s 0 %d %s", rfc3339,
143 abd261d2 2023-07-25 op c->rhost, *c->domain == '\0' ? c->iri.host : c->domain,
144 abd261d2 2023-07-25 op b, code, meta);
145 abd261d2 2023-07-25 op break;
146 abd261d2 2023-07-25 op
147 abd261d2 2023-07-25 op /*
148 abd261d2 2023-07-25 op * Attempt to be compatible with the default Apache httpd'
149 abd261d2 2023-07-25 op * LogFormat "%h %l %u %t \"%r\" %>s %b"
150 abd261d2 2023-07-25 op * see <https://httpd.apache.org/docs/current/mod/mod_log_config.html>
151 abd261d2 2023-07-25 op */
152 abd261d2 2023-07-25 op case LOG_FORMAT_COMMON:
153 abd261d2 2023-07-25 op /*
154 abd261d2 2023-07-25 op * XXX the second '-' is the remote user name, we
155 abd261d2 2023-07-25 op * could use the client cert for it.
156 abd261d2 2023-07-25 op *
157 abd261d2 2023-07-25 op * XXX it should log the size of the response.
158 abd261d2 2023-07-25 op */
159 abd261d2 2023-07-25 op ec = asprintf(&fmted, "%s %s - - %s \"%s\" %d 0",
160 abd261d2 2023-07-25 op *c->domain == '\0' ? c->iri.host : c->domain,
161 abd261d2 2023-07-25 op c->rhost, tstamp, b, code);
162 abd261d2 2023-07-25 op break;
163 abd261d2 2023-07-25 op
164 abd261d2 2023-07-25 op /*
165 abd261d2 2023-07-25 op * Attempt to be compatible with the default nginx' log_format
166 abd261d2 2023-07-25 op * combined:
167 abd261d2 2023-07-25 op * '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
168 abd261d2 2023-07-25 op */
169 abd261d2 2023-07-25 op case LOG_FORMAT_COMBINED:
170 abd261d2 2023-07-25 op default:
171 abd261d2 2023-07-25 op /*
172 abd261d2 2023-07-25 op * XXX the second '-' is the remote user name, we
173 abd261d2 2023-07-25 op * could use the client cert for it.
174 abd261d2 2023-07-25 op *
175 abd261d2 2023-07-25 op * XXX it should log the size of the response.
176 abd261d2 2023-07-25 op */
177 abd261d2 2023-07-25 op ec = asprintf(&fmted, "%s - - [%s] \"%s\" %d 0 \"-\" \"\"",
178 abd261d2 2023-07-25 op c->rhost, tstamp, b, code);
179 abd261d2 2023-07-25 op break;
180 abd261d2 2023-07-25 op }
181 abd261d2 2023-07-25 op
182 47b0ff10 2023-06-08 op if (ec == -1)
183 792f302a 2023-06-09 op fatal("asprintf");
184 e0750210 2023-07-24 op
185 e0750210 2023-07-24 op if (debug)
186 e0750210 2023-07-24 op fprintf(stderr, "%s\n", fmted);
187 47b0ff10 2023-06-08 op
188 af1dab18 2023-06-09 op proc_compose(conf->ps, PROC_LOGGER, IMSG_LOG_REQUEST,
189 47b0ff10 2023-06-08 op fmted, ec + 1);
190 47b0ff10 2023-06-08 op
191 47b0ff10 2023-06-08 op free(fmted);
192 47b0ff10 2023-06-08 op }
193 47b0ff10 2023-06-08 op
194 419a4235 2021-04-28 op static int
195 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
196 419a4235 2021-04-28 op {
197 419a4235 2021-04-28 op struct flock lock;
198 419a4235 2021-04-28 op int fd;
199 d672b8fb 2021-02-03 op
200 419a4235 2021-04-28 op if (pidfile == NULL)
201 419a4235 2021-04-28 op return -1;
202 419a4235 2021-04-28 op
203 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
204 df5058c9 2023-06-05 op fatal("can't open pidfile %s", pidfile);
205 419a4235 2021-04-28 op
206 419a4235 2021-04-28 op lock.l_start = 0;
207 419a4235 2021-04-28 op lock.l_len = 0;
208 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
209 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
210 419a4235 2021-04-28 op
211 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
212 df5058c9 2023-06-05 op fatalx("can't lock %s, gmid is already running?", pidfile);
213 419a4235 2021-04-28 op
214 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
215 df5058c9 2023-06-05 op fatal("ftruncate %s", pidfile);
216 419a4235 2021-04-28 op
217 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
218 419a4235 2021-04-28 op
219 419a4235 2021-04-28 op return fd;
220 419a4235 2021-04-28 op }
221 419a4235 2021-04-28 op
222 8d6ae384 2021-01-24 op int
223 8d6ae384 2021-01-24 op main(int argc, char **argv)
224 8d6ae384 2021-01-24 op {
225 af1dab18 2023-06-09 op struct conf *conf;
226 c26f2460 2023-06-08 op struct privsep *ps;
227 c26f2460 2023-06-08 op const char *errstr, *title = NULL;
228 7fff8aa6 2023-06-09 op const char *user = NULL, *chroot = NULL;
229 c26f2460 2023-06-08 op size_t i;
230 c26f2460 2023-06-08 op int ch, conftest = 0;
231 c26f2460 2023-06-08 op int proc_instance = 0;
232 c26f2460 2023-06-08 op int proc_id = PROC_PARENT;
233 c26f2460 2023-06-08 op int argc0 = argc;
234 501e489c 2021-01-24 op
235 0046c1fe 2023-06-06 op setlocale(LC_CTYPE, "");
236 0046c1fe 2023-06-06 op
237 eae52ad4 2023-06-06 op /* log to stderr until daemonized */
238 eae52ad4 2023-06-06 op log_init(1, LOG_DAEMON);
239 8d6ae384 2021-01-24 op
240 5777923b 2021-06-29 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
241 3e4749f7 2020-10-02 op switch (ch) {
242 0ac785a6 2023-06-05 op case 'c':
243 0ac785a6 2023-06-05 op config_path = absolutify_path(optarg);
244 0ac785a6 2023-06-05 op break;
245 f98e9045 2021-06-29 op case 'D':
246 f98e9045 2021-06-29 op if (cmdline_symset(optarg) == -1)
247 df5058c9 2023-06-05 op fatalx("could not parse macro definition: %s",
248 8a50fc03 2021-07-07 op optarg);
249 f98e9045 2021-06-29 op break;
250 0ac785a6 2023-06-05 op case 'f':
251 ca84625a 2023-06-08 op debug = 1;
252 46af8c6c 2021-01-27 op break;
253 3e4749f7 2020-10-02 op case 'h':
254 9327bc04 2021-06-29 op usage();
255 3e4749f7 2020-10-02 op return 0;
256 c26f2460 2023-06-08 op case 'I':
257 c26f2460 2023-06-08 op proc_instance = strtonum(optarg, 0, PROC_MAX_INSTANCES,
258 c26f2460 2023-06-08 op &errstr);
259 c26f2460 2023-06-08 op if (errstr != NULL)
260 c26f2460 2023-06-08 op fatalx("invalid process instance");
261 c26f2460 2023-06-08 op break;
262 15902770 2021-01-15 op case 'n':
263 f0a01fc7 2021-10-09 op conftest++;
264 721e2325 2020-11-18 op break;
265 8e8b2e25 2021-04-28 op case 'P':
266 f1f13cb7 2023-06-08 op pidfile = absolutify_path(optarg);
267 8e8b2e25 2021-04-28 op break;
268 c26f2460 2023-06-08 op case 'T':
269 c26f2460 2023-06-08 op title = optarg;
270 c26f2460 2023-06-08 op proc_id = proc_getid(procs, nitems(procs), title);
271 c26f2460 2023-06-08 op if (proc_id == PROC_MAX)
272 c26f2460 2023-06-08 op fatalx("invalid process name");
273 c26f2460 2023-06-08 op break;
274 7fff8aa6 2023-06-09 op case 'U':
275 7fff8aa6 2023-06-09 op user = optarg;
276 7fff8aa6 2023-06-09 op break;
277 5777923b 2021-06-29 op case 'V':
278 fdb43a4c 2021-06-29 op puts("Version: " GMID_STRING);
279 5777923b 2021-06-29 op return 0;
280 8904fa0e 2021-01-27 op case 'v':
281 ca84625a 2023-06-08 op verbose = 1;
282 8904fa0e 2021-01-27 op break;
283 7fff8aa6 2023-06-09 op case 'X':
284 7fff8aa6 2023-06-09 op chroot = optarg;
285 7fff8aa6 2023-06-09 op break;
286 3e4749f7 2020-10-02 op default:
287 9327bc04 2021-06-29 op usage();
288 3e4749f7 2020-10-02 op return 1;
289 3e4749f7 2020-10-02 op }
290 3e4749f7 2020-10-02 op }
291 3e4749f7 2020-10-02 op
292 c26f2460 2023-06-08 op if (argc - optind != 0)
293 d29a2ee2 2022-09-06 op usage();
294 d29a2ee2 2022-09-06 op
295 af1dab18 2023-06-09 op conf = config_new();
296 af1dab18 2023-06-09 op
297 7fff8aa6 2023-06-09 op /*
298 7fff8aa6 2023-06-09 op * Only the parent loads the config, the others get user and
299 7fff8aa6 2023-06-09 op * chroot via flags and the rest via imsg.
300 7fff8aa6 2023-06-09 op */
301 7fff8aa6 2023-06-09 op if (proc_id == PROC_PARENT) {
302 7fff8aa6 2023-06-09 op if (parse_conf(conf, config_path) == -1)
303 7fff8aa6 2023-06-09 op fatalx("failed to load configuration file");
304 7fff8aa6 2023-06-09 op if (*conf->chroot != '\0' && *conf->user == '\0')
305 7fff8aa6 2023-06-09 op fatalx("can't chroot without a user to switch to.");
306 7fff8aa6 2023-06-09 op } else {
307 7fff8aa6 2023-06-09 op if (user)
308 7fff8aa6 2023-06-09 op strlcpy(conf->user, user, sizeof(conf->user));
309 7fff8aa6 2023-06-09 op if (chroot)
310 7fff8aa6 2023-06-09 op strlcpy(conf->chroot, chroot, sizeof(conf->chroot));
311 7fff8aa6 2023-06-09 op }
312 d672b8fb 2021-02-03 op
313 132cae8c 2021-01-18 op if (conftest) {
314 f0a01fc7 2021-10-09 op fprintf(stderr, "config OK\n");
315 f0a01fc7 2021-10-09 op if (conftest > 1)
316 5af19830 2023-06-09 op main_print_conf(conf);
317 132cae8c 2021-01-18 op return 0;
318 132cae8c 2021-01-18 op }
319 4a28dd01 2020-12-28 op
320 c26f2460 2023-06-08 op if ((ps = calloc(1, sizeof(*ps))) == NULL)
321 c26f2460 2023-06-08 op fatal("calloc");
322 af1dab18 2023-06-09 op ps->ps_env = conf;
323 af1dab18 2023-06-09 op conf->ps = ps;
324 af1dab18 2023-06-09 op if (*conf->user) {
325 c26f2460 2023-06-08 op if (geteuid())
326 c26f2460 2023-06-08 op fatalx("need root privileges");
327 af1dab18 2023-06-09 op if ((ps->ps_pw = getpwnam(conf->user)) == NULL)
328 af1dab18 2023-06-09 op fatalx("unknown user %s", conf->user);
329 287ab865 2023-06-24 op if (*conf->chroot == '\0')
330 287ab865 2023-06-24 op strlcpy(conf->chroot, ps->ps_pw->pw_dir,
331 287ab865 2023-06-24 op sizeof(conf->chroot));
332 c26f2460 2023-06-08 op }
333 8a50fc03 2021-07-07 op
334 af1dab18 2023-06-09 op ps->ps_instances[PROC_SERVER] = conf->prefork;
335 c26f2460 2023-06-08 op ps->ps_instance = proc_instance;
336 c26f2460 2023-06-08 op if (title != NULL)
337 c26f2460 2023-06-08 op ps->ps_title[proc_id] = title;
338 c26f2460 2023-06-08 op
339 af1dab18 2023-06-09 op if (*conf->chroot != '\0') {
340 c26f2460 2023-06-08 op for (i = 0; i < nitems(procs); ++i)
341 af1dab18 2023-06-09 op procs[i].p_chroot = conf->chroot;
342 0170ba02 2021-01-17 op }
343 c26f2460 2023-06-08 op
344 ca84625a 2023-06-08 op log_init(debug, LOG_DAEMON);
345 ca84625a 2023-06-08 op log_setverbose(verbose);
346 c26f2460 2023-06-08 op if (title != NULL)
347 c26f2460 2023-06-08 op log_procinit(title);
348 4a28dd01 2020-12-28 op
349 c26f2460 2023-06-08 op /* only the parent returns */
350 ca84625a 2023-06-08 op proc_init(ps, procs, nitems(procs), debug, argc0, argv, proc_id);
351 3e4749f7 2020-10-02 op
352 c26f2460 2023-06-08 op log_procinit("main");
353 ca84625a 2023-06-08 op if (!debug && daemon(0, 0) == -1)
354 c26f2460 2023-06-08 op fatal("daemon");
355 3841a369 2021-04-20 op
356 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
357 8e8b2e25 2021-04-28 op
358 c26f2460 2023-06-08 op sandbox_main_process();
359 c26f2460 2023-06-08 op
360 c26f2460 2023-06-08 op event_init();
361 c26f2460 2023-06-08 op
362 c26f2460 2023-06-08 op signal(SIGPIPE, SIG_IGN);
363 b9c9123b 2021-03-20 op
364 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigint, SIGINT, main_sig_handler, ps);
365 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigterm, SIGTERM, main_sig_handler, ps);
366 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigchld, SIGCHLD, main_sig_handler, ps);
367 c26f2460 2023-06-08 op signal_set(&ps->ps_evsighup, SIGHUP, main_sig_handler, ps);
368 3bda540e 2023-07-24 op signal_set(&ps->ps_evsigusr1, SIGUSR1, main_sig_handler, ps);
369 ca21e100 2021-02-04 op
370 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigint, NULL);
371 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigterm, NULL);
372 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigchld, NULL);
373 c26f2460 2023-06-08 op signal_add(&ps->ps_evsighup, NULL);
374 3bda540e 2023-07-24 op signal_add(&ps->ps_evsigusr1, NULL);
375 1d3eb470 2021-03-20 op
376 c26f2460 2023-06-08 op proc_connect(ps);
377 ca21e100 2021-02-04 op
378 af1dab18 2023-06-09 op if (main_configure(conf) == -1)
379 c26f2460 2023-06-08 op fatal("configuration failed");
380 bc99d868 2021-03-19 op
381 c26f2460 2023-06-08 op event_dispatch();
382 af1dab18 2023-06-09 op main_shutdown(conf);
383 c26f2460 2023-06-08 op /* NOTREACHED */
384 c26f2460 2023-06-08 op return 0;
385 c26f2460 2023-06-08 op }
386 ca21e100 2021-02-04 op
387 c26f2460 2023-06-08 op static int
388 3bda540e 2023-07-24 op main_send_logfd(struct conf *conf)
389 c26f2460 2023-06-08 op {
390 c26f2460 2023-06-08 op struct privsep *ps = conf->ps;
391 4acf495f 2023-07-24 op char path[PATH_MAX];
392 4acf495f 2023-07-24 op int r, fd = -1;
393 226f13ec 2023-07-24 op
394 3bda540e 2023-07-24 op if (conf->log_access) {
395 4acf495f 2023-07-24 op r = snprintf(path, sizeof(path), "%s%s%s", conf->chroot,
396 4acf495f 2023-07-24 op *conf->chroot == '\0' ? "" : "/", conf->log_access);
397 4acf495f 2023-07-24 op if (r < 0 || (size_t)r >= sizeof(path)) {
398 4acf495f 2023-07-24 op log_warnx("path too long: %s", conf->log_access);
399 4acf495f 2023-07-24 op goto done;
400 4acf495f 2023-07-24 op }
401 4acf495f 2023-07-24 op
402 3bda540e 2023-07-24 op fd = open(conf->log_access, O_WRONLY|O_CREAT|O_APPEND, 0600);
403 3bda540e 2023-07-24 op if (fd == -1)
404 3bda540e 2023-07-24 op log_warn("can't open %s", conf->log_access);
405 226f13ec 2023-07-24 op }
406 4acf495f 2023-07-24 op
407 4acf495f 2023-07-24 op done:
408 3bda540e 2023-07-24 op if (proc_compose_imsg(ps, PROC_LOGGER, -1, IMSG_LOG_TYPE, -1, fd,
409 3bda540e 2023-07-24 op NULL, 0) == -1)
410 3bda540e 2023-07-24 op return -1;
411 3bda540e 2023-07-24 op return 0;
412 3bda540e 2023-07-24 op }
413 ca21e100 2021-02-04 op
414 3bda540e 2023-07-24 op static int
415 3bda540e 2023-07-24 op main_configure(struct conf *conf)
416 3bda540e 2023-07-24 op {
417 3bda540e 2023-07-24 op struct privsep *ps = conf->ps;
418 3bda540e 2023-07-24 op
419 3bda540e 2023-07-24 op if (main_send_logfd(conf) == -1)
420 3bda540e 2023-07-24 op return -1;
421 3bda540e 2023-07-24 op
422 86693a33 2023-06-11 op conf->reload = conf->prefork + 1; /* servers, crypto */
423 ca21e100 2021-02-04 op
424 c26f2460 2023-06-08 op if (proc_compose(ps, PROC_SERVER, IMSG_RECONF_START, NULL, 0) == -1)
425 86693a33 2023-06-11 op return -1;
426 86693a33 2023-06-11 op if (proc_compose(ps, PROC_CRYPTO, IMSG_RECONF_START, NULL, 0) == -1)
427 c26f2460 2023-06-08 op return -1;
428 ca21e100 2021-02-04 op
429 e45334e6 2023-06-09 op if (config_send(conf) == -1)
430 c26f2460 2023-06-08 op return -1;
431 c26f2460 2023-06-08 op
432 c26f2460 2023-06-08 op if (proc_compose(ps, PROC_SERVER, IMSG_RECONF_END, NULL, 0) == -1)
433 c26f2460 2023-06-08 op return -1;
434 86693a33 2023-06-11 op if (proc_compose(ps, PROC_CRYPTO, IMSG_RECONF_END, NULL, 0) == -1)
435 86693a33 2023-06-11 op return -1;
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
441 c26f2460 2023-06-08 op main_configure_done(struct conf *conf)
442 c26f2460 2023-06-08 op {
443 c26f2460 2023-06-08 op if (conf->reload == 0) {
444 c26f2460 2023-06-08 op log_warnx("configuration already done");
445 c26f2460 2023-06-08 op return;
446 ca21e100 2021-02-04 op }
447 1d3eb470 2021-03-20 op
448 c26f2460 2023-06-08 op conf->reload--;
449 c26f2460 2023-06-08 op /* send IMSG_CTL_START? */
450 c26f2460 2023-06-08 op }
451 c26f2460 2023-06-08 op
452 c26f2460 2023-06-08 op static void
453 c26f2460 2023-06-08 op main_reload(struct conf *conf)
454 c26f2460 2023-06-08 op {
455 c26f2460 2023-06-08 op if (conf->reload) {
456 c26f2460 2023-06-08 op log_debug("%s: already in progress: %d pending",
457 c26f2460 2023-06-08 op __func__, conf->reload);
458 c26f2460 2023-06-08 op return;
459 5c485529 2022-09-10 op }
460 1d3eb470 2021-03-20 op
461 c26f2460 2023-06-08 op log_debug("%s: config file %s", __func__, config_path);
462 af1dab18 2023-06-09 op config_purge(conf);
463 1d3eb470 2021-03-20 op
464 68368f4c 2023-06-09 op if (parse_conf(conf, config_path) == -1) {
465 68368f4c 2023-06-09 op log_warnx("failed to parse the config");
466 68368f4c 2023-06-09 op return;
467 68368f4c 2023-06-09 op }
468 68368f4c 2023-06-09 op
469 c26f2460 2023-06-08 op main_configure(conf);
470 c26f2460 2023-06-08 op }
471 c26f2460 2023-06-08 op
472 c26f2460 2023-06-08 op static void
473 c26f2460 2023-06-08 op main_sig_handler(int sig, short ev, void *arg)
474 c26f2460 2023-06-08 op {
475 c26f2460 2023-06-08 op struct privsep *ps = arg;
476 c26f2460 2023-06-08 op
477 c26f2460 2023-06-08 op /*
478 c26f2460 2023-06-08 op * Normal signal handler rules don't apply here because libevent
479 c26f2460 2023-06-08 op * decouples for us.
480 c26f2460 2023-06-08 op */
481 c26f2460 2023-06-08 op
482 c26f2460 2023-06-08 op switch (sig) {
483 c26f2460 2023-06-08 op case SIGHUP:
484 c26f2460 2023-06-08 op if (privsep_process != PROC_PARENT)
485 c26f2460 2023-06-08 op return;
486 c26f2460 2023-06-08 op log_info("reload requested with SIGHUP");
487 c26f2460 2023-06-08 op main_reload(ps->ps_env);
488 c26f2460 2023-06-08 op break;
489 c26f2460 2023-06-08 op case SIGCHLD:
490 c26f2460 2023-06-08 op log_warnx("one child died, quitting");
491 c26f2460 2023-06-08 op /* fallthrough */
492 c26f2460 2023-06-08 op case SIGTERM:
493 c26f2460 2023-06-08 op case SIGINT:
494 c26f2460 2023-06-08 op main_shutdown(ps->ps_env);
495 c26f2460 2023-06-08 op break;
496 3bda540e 2023-07-24 op case SIGUSR1:
497 3bda540e 2023-07-24 op main_send_logfd(ps->ps_env);
498 3bda540e 2023-07-24 op break;
499 c26f2460 2023-06-08 op default:
500 c26f2460 2023-06-08 op fatalx("unexpected signal %d", sig);
501 c26f2460 2023-06-08 op }
502 c26f2460 2023-06-08 op }
503 c26f2460 2023-06-08 op
504 c26f2460 2023-06-08 op static int
505 c26f2460 2023-06-08 op main_dispatch_server(int fd, struct privsep_proc *p, struct imsg *imsg)
506 c26f2460 2023-06-08 op {
507 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
508 c26f2460 2023-06-08 op struct conf *conf = ps->ps_env;
509 c26f2460 2023-06-08 op
510 c26f2460 2023-06-08 op switch (imsg->hdr.type) {
511 c26f2460 2023-06-08 op case IMSG_RECONF_DONE:
512 c26f2460 2023-06-08 op main_configure_done(conf);
513 c26f2460 2023-06-08 op break;
514 c26f2460 2023-06-08 op default:
515 c26f2460 2023-06-08 op return -1;
516 c26f2460 2023-06-08 op }
517 c26f2460 2023-06-08 op
518 c26f2460 2023-06-08 op return 0;
519 c26f2460 2023-06-08 op }
520 c26f2460 2023-06-08 op
521 c26f2460 2023-06-08 op static int
522 86693a33 2023-06-11 op main_dispatch_crypto(int fd, struct privsep_proc *p, struct imsg *imsg)
523 86693a33 2023-06-11 op {
524 86693a33 2023-06-11 op struct privsep *ps = p->p_ps;
525 86693a33 2023-06-11 op struct conf *conf = ps->ps_env;
526 86693a33 2023-06-11 op
527 86693a33 2023-06-11 op switch (imsg->hdr.type) {
528 86693a33 2023-06-11 op case IMSG_RECONF_DONE:
529 86693a33 2023-06-11 op main_configure_done(conf);
530 86693a33 2023-06-11 op break;
531 86693a33 2023-06-11 op default:
532 86693a33 2023-06-11 op return -1;
533 86693a33 2023-06-11 op }
534 86693a33 2023-06-11 op
535 86693a33 2023-06-11 op return 0;
536 86693a33 2023-06-11 op }
537 86693a33 2023-06-11 op
538 86693a33 2023-06-11 op static int
539 c26f2460 2023-06-08 op main_dispatch_logger(int fd, struct privsep_proc *p, struct imsg *imsg)
540 c26f2460 2023-06-08 op {
541 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
542 c26f2460 2023-06-08 op struct conf *conf = ps->ps_env;
543 c26f2460 2023-06-08 op
544 c26f2460 2023-06-08 op switch (imsg->hdr.type) {
545 c26f2460 2023-06-08 op case IMSG_RECONF_DONE:
546 c26f2460 2023-06-08 op main_configure_done(conf);
547 c26f2460 2023-06-08 op break;
548 c26f2460 2023-06-08 op default:
549 c26f2460 2023-06-08 op return -1;
550 c26f2460 2023-06-08 op }
551 c26f2460 2023-06-08 op
552 c26f2460 2023-06-08 op return 0;
553 c26f2460 2023-06-08 op }
554 c26f2460 2023-06-08 op
555 c26f2460 2023-06-08 op static void __dead
556 c26f2460 2023-06-08 op main_shutdown(struct conf *conf)
557 c26f2460 2023-06-08 op {
558 c26f2460 2023-06-08 op proc_kill(conf->ps);
559 af1dab18 2023-06-09 op config_purge(conf);
560 c26f2460 2023-06-08 op free(conf->ps);
561 c26f2460 2023-06-08 op /* free(conf); */
562 c26f2460 2023-06-08 op
563 c26f2460 2023-06-08 op log_info("parent terminating, pid %d", getpid());
564 c26f2460 2023-06-08 op
565 8e8b2e25 2021-04-28 op if (pidfd != -1)
566 8e8b2e25 2021-04-28 op close(pidfd);
567 8e8b2e25 2021-04-28 op
568 c26f2460 2023-06-08 op exit(0);
569 5af19830 2023-06-09 op }
570 5af19830 2023-06-09 op
571 5af19830 2023-06-09 op static void
572 5af19830 2023-06-09 op main_print_conf(struct conf *conf)
573 5af19830 2023-06-09 op {
574 5af19830 2023-06-09 op struct vhost *h;
575 5af19830 2023-06-09 op /* struct location *l; */
576 5af19830 2023-06-09 op /* struct envlist *e; */
577 5af19830 2023-06-09 op /* struct alist *a; */
578 5af19830 2023-06-09 op
579 5af19830 2023-06-09 op if (*conf->chroot != '\0')
580 5af19830 2023-06-09 op printf("chroot \"%s\"\n", conf->chroot);
581 5af19830 2023-06-09 op /* XXX: defined mimes? */
582 5af19830 2023-06-09 op printf("prefork %d\n", conf->prefork);
583 5af19830 2023-06-09 op /* XXX: protocols? */
584 5af19830 2023-06-09 op if (*conf->user != '\0')
585 5af19830 2023-06-09 op printf("user \"%s\"\n", conf->user);
586 5af19830 2023-06-09 op
587 5af19830 2023-06-09 op TAILQ_FOREACH(h, &conf->hosts, vhosts) {
588 5af19830 2023-06-09 op printf("\nserver \"%s\" {\n", h->domain);
589 5af19830 2023-06-09 op printf(" cert \"%s\"\n", h->cert);
590 5af19830 2023-06-09 op printf(" key \"%s\"\n", h->key);
591 5af19830 2023-06-09 op /* TODO: print locations... */
592 5af19830 2023-06-09 op printf("}\n");
593 5af19830 2023-06-09 op }
594 3e4749f7 2020-10-02 op }