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