Blame


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