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