002
2021-01-16
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
004
2021-01-16
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-01-16
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-01-16
op
* copyright notice and this permission notice appear in all copies.
008
2021-01-16
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-01-16
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-01-16
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-01-16
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-01-16
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-01-16
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-01-16
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2021-02-12
op
#include "gmid.h"
019
2021-05-09
op
#include <sys/un.h>
021
2021-01-16
op
#include <err.h>
022
2021-01-16
op
#include <errno.h>
024
2021-03-03
op
#include <event.h>
025
2021-01-16
op
#include <fcntl.h>
026
2021-02-01
op
#include <libgen.h>
027
2021-02-01
op
#include <limits.h>
028
2021-01-16
op
#include <signal.h>
029
2021-02-01
op
#include <stdarg.h>
030
2021-01-16
op
#include <string.h>
032
2021-03-19
op
static void handle_imsg_cgi_req(struct imsgbuf*, struct imsg*, size_t);
033
2021-05-09
op
static void handle_imsg_fcgi_req(struct imsgbuf*, struct imsg*, size_t);
034
2021-12-29
op
static void handle_imsg_conn_req(struct imsgbuf *, struct imsg *, size_t);
035
2021-03-19
op
static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
036
2021-03-19
op
static void handle_dispatch_imsg(int, short, void*);
038
2021-03-19
op
static imsg_handlerfn *handlers[] = {
039
2021-05-09
op
[IMSG_FCGI_REQ] = handle_imsg_fcgi_req,
040
2021-03-19
op
[IMSG_CGI_REQ] = handle_imsg_cgi_req,
041
2021-12-29
op
[IMSG_CONN_REQ] = handle_imsg_conn_req,
042
2021-03-19
op
[IMSG_QUIT] = handle_imsg_quit,
045
2021-01-16
op
static inline void
046
2021-01-16
op
safe_setenv(const char *name, const char *val)
048
2021-01-16
op
if (val == NULL)
049
2021-01-16
op
val = "";
050
2021-01-16
op
setenv(name, val, 1);
053
2021-02-01
op
static char *
054
2021-02-01
op
xasprintf(const char *fmt, ...)
056
2021-02-01
op
va_list ap;
059
2021-02-01
op
va_start(ap, fmt);
060
2021-02-01
op
if (vasprintf(&s, fmt, ap) == -1)
061
2021-02-01
op
s = NULL;
062
2021-02-01
op
va_end(ap);
064
2021-02-01
op
return s;
067
2021-02-07
op
static void
068
2021-02-07
op
do_exec(const char *ex, const char *spath, char *query)
070
2021-02-07
op
char **argv, buf[PATH_MAX], *sname, *t;
071
2021-02-07
op
size_t i, n;
073
2021-04-20
op
/* restore the default handlers */
074
2021-04-20
op
signal(SIGPIPE, SIG_DFL);
075
2021-04-20
op
signal(SIGCHLD, SIG_DFL);
076
2021-04-20
op
signal(SIGHUP, SIG_DFL);
077
2021-04-20
op
signal(SIGINT, SIG_DFL);
078
2021-04-20
op
signal(SIGTERM, SIG_DFL);
080
2021-02-07
op
strlcpy(buf, spath, sizeof(buf));
081
2021-02-07
op
sname = basename(buf);
083
2021-02-07
op
if (query == NULL || strchr(query, '=') != NULL) {
084
2021-02-07
op
if ((argv = calloc(2, sizeof(char*))) == NULL)
085
2021-02-07
op
err(1, "calloc");
086
2021-02-07
op
argv[0] = sname;
087
2021-02-07
op
execvp(ex, argv);
088
2021-02-07
op
warn("execvp: %s", argv[0]);
093
2021-02-07
op
for (t = query ;; t++, n++) {
094
2021-02-07
op
if ((t = strchr(t, '+')) == NULL)
098
2021-02-07
op
if ((argv = calloc(n+2, sizeof(char*))) == NULL)
099
2021-02-07
op
err(1, "calloc");
101
2021-02-07
op
argv[0] = sname;
102
2021-02-07
op
for (i = 0; i < n; ++i) {
103
2021-02-07
op
t = strchr(query, '+');
104
2021-02-07
op
if (t != NULL)
105
2021-02-07
op
*t = '\0';
106
2021-02-07
op
argv[i+1] = pct_decode_str(query);
107
2021-02-07
op
query = t+1;
110
2021-02-07
op
execvp(ex, argv);
111
2021-02-07
op
warn("execvp: %s", argv[0]);
114
2021-02-07
op
static inline void
115
2021-02-07
op
setenv_time(const char *var, time_t t)
117
2021-02-07
op
char timebuf[21];
118
2021-02-07
op
struct tm tminfo;
120
2021-02-07
op
if (t == -1)
123
2021-02-07
op
strftime(timebuf, sizeof(timebuf), "%FT%TZ",
124
2021-02-07
op
gmtime_r(&t, &tminfo));
125
2021-02-07
op
setenv(var, timebuf, 1);
128
2021-01-16
op
/* fd or -1 on error */
129
2021-01-16
op
static int
130
2021-04-30
op
launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost,
131
2021-04-30
op
struct location *loc)
133
2021-07-06
op
int p[2], errp[2]; /* read end, write end */
135
2021-10-02
op
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
136
2021-01-16
op
return -1;
137
2021-10-02
op
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, errp) == -1)
138
2021-07-06
op
return -1;
140
2021-01-16
op
switch (fork()) {
142
2021-07-08
op
log_err(NULL, "fork failed: %s", strerror(errno));
143
2021-07-08
op
close(p[0]);
144
2021-07-08
op
close(p[1]);
145
2021-07-08
op
close(errp[0]);
146
2021-07-08
op
close(errp[1]);
147
2021-01-16
op
return -1;
149
2021-01-16
op
case 0: { /* child */
150
2021-02-01
op
char *ex, *pwd;
151
2021-02-01
op
char iribuf[GEMINI_URL_LEN];
152
2021-02-01
op
char path[PATH_MAX];
153
2021-04-28
op
struct envlist *e;
155
2021-01-16
op
close(p[0]);
156
2021-01-16
op
if (dup2(p[1], 1) == -1)
157
2021-01-16
op
goto childerr;
159
2021-07-06
op
close(errp[0]);
160
2021-07-06
op
if (dup2(errp[1], 2) == -1)
161
2021-07-06
op
goto childerr;
163
2021-04-30
op
ex = xasprintf("%s/%s", loc->dir, req->spath);
165
2021-02-01
op
serialize_iri(iri, iribuf, sizeof(iribuf));
167
2021-01-16
op
safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
168
2021-04-30
op
safe_setenv("GEMINI_DOCUMENT_ROOT", loc->dir);
169
2021-02-01
op
safe_setenv("GEMINI_SCRIPT_FILENAME",
170
2021-04-30
op
xasprintf("%s/%s", loc->dir, req->spath));
171
2021-02-01
op
safe_setenv("GEMINI_URL", iribuf);
173
2021-02-01
op
strlcpy(path, "/", sizeof(path));
174
2021-03-19
op
strlcat(path, req->spath, sizeof(path));
175
2021-02-01
op
safe_setenv("GEMINI_URL_PATH", path);
177
2021-03-19
op
if (*req->relpath != '\0') {
178
2021-02-01
op
strlcpy(path, "/", sizeof(path));
179
2021-03-19
op
strlcat(path, req->relpath, sizeof(path));
180
2021-02-01
op
safe_setenv("PATH_INFO", path);
182
2021-04-30
op
strlcpy(path, loc->dir, sizeof(path));
183
2021-02-01
op
strlcat(path, "/", sizeof(path));
184
2021-03-19
op
strlcat(path, req->relpath, sizeof(path));
185
2021-02-01
op
safe_setenv("PATH_TRANSLATED", path);
188
2021-02-01
op
safe_setenv("QUERY_STRING", iri->query);
189
2021-03-19
op
safe_setenv("REMOTE_ADDR", req->addr);
190
2021-03-19
op
safe_setenv("REMOTE_HOST", req->addr);
191
2021-02-01
op
safe_setenv("REQUEST_METHOD", "");
193
2021-02-01
op
strlcpy(path, "/", sizeof(path));
194
2021-03-19
op
strlcat(path, req->spath, sizeof(path));
195
2021-02-01
op
safe_setenv("SCRIPT_NAME", path);
197
2021-02-01
op
safe_setenv("SERVER_NAME", iri->host);
199
2021-02-01
op
snprintf(path, sizeof(path), "%d", conf.port);
200
2021-02-01
op
safe_setenv("SERVER_PORT", path);
202
2021-02-01
op
safe_setenv("SERVER_PROTOCOL", "GEMINI");
203
2021-05-15
op
safe_setenv("SERVER_SOFTWARE", GMID_VERSION);
205
2021-03-19
op
if (*req->subject != '\0')
206
2021-01-16
op
safe_setenv("AUTH_TYPE", "Certificate");
208
2021-02-01
op
safe_setenv("AUTH_TYPE", "");
210
2021-03-19
op
safe_setenv("REMOTE_USER", req->subject);
211
2021-03-19
op
safe_setenv("TLS_CLIENT_ISSUER", req->issuer);
212
2021-03-19
op
safe_setenv("TLS_CLIENT_HASH", req->hash);
213
2021-04-13
op
safe_setenv("TLS_VERSION", req->version);
214
2021-04-13
op
safe_setenv("TLS_CIPHER", req->cipher);
216
2021-04-13
op
snprintf(path, sizeof(path), "%d", req->cipher_strength);
217
2021-04-13
op
safe_setenv("TLS_CIPHER_STRENGTH", path);
219
2021-03-19
op
setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter);
220
2021-03-19
op
setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore);
222
2021-04-28
op
TAILQ_FOREACH(e, &vhost->env, envs) {
223
2021-04-28
op
safe_setenv(e->name, e->value);
226
2021-02-07
op
strlcpy(path, ex, sizeof(path));
228
2021-02-01
op
pwd = dirname(path);
229
2021-02-01
op
if (chdir(pwd)) {
230
2021-02-01
op
warn("chdir");
231
2021-02-01
op
goto childerr;
234
2021-03-19
op
do_exec(ex, req->spath, iri->query);
235
2021-01-16
op
goto childerr;
239
2021-01-16
op
close(p[1]);
240
2021-07-08
op
close(errp[0]);
241
2021-07-06
op
close(errp[1]);
242
2021-02-06
op
mark_nonblock(p[0]);
243
2021-01-16
op
return p[0];
247
2021-01-16
op
dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
248
2021-01-16
op
_exit(1);
251
2021-03-31
op
static struct vhost *
252
2021-03-31
op
host_nth(size_t n)
254
2021-03-31
op
struct vhost *h;
256
2021-03-31
op
TAILQ_FOREACH(h, &hosts, vhosts) {
257
2021-03-31
op
if (n == 0)
258
2021-03-31
op
return h;
262
2021-03-31
op
return NULL;
265
2021-04-30
op
static struct location *
266
2021-04-30
op
loc_nth(struct vhost *vhost, size_t n)
268
2021-04-30
op
struct location *loc;
270
2021-04-30
op
TAILQ_FOREACH(loc, &vhost->locations, locations) {
271
2021-04-30
op
if (n == 0)
272
2021-04-30
op
return loc;
276
2021-04-30
op
return NULL;
279
2021-03-03
op
static void
280
2021-03-19
op
handle_imsg_cgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
282
2021-03-31
op
struct vhost *h;
283
2021-04-30
op
struct location *l;
284
2021-03-31
op
struct cgireq req;
285
2021-03-31
op
struct iri iri;
288
2021-03-19
op
if (datalen != sizeof(req))
291
2022-03-27
op
memcpy(&req, imsg->data, sizeof(req));
293
2021-03-19
op
iri.schema = req.iri_schema_off + req.buf;
294
2021-03-19
op
iri.host = req.iri_host_off + req.buf;
295
2021-03-19
op
iri.port = req.iri_port_off + req.buf;
296
2021-03-19
op
iri.path = req.iri_path_off + req.buf;
297
2021-03-19
op
iri.query = req.iri_query_off + req.buf;
298
2021-03-19
op
iri.fragment = req.iri_fragment_off + req.buf;
300
2021-03-19
op
/* patch the query, otherwise do_exec will always pass "" as
301
2021-03-19
op
* first argument to the script. */
302
2021-03-19
op
if (*iri.query == '\0')
303
2021-03-19
op
iri.query = NULL;
305
2021-03-31
op
if ((h = host_nth(req.host_off)) == NULL)
308
2021-05-15
op
if ((l = loc_nth(h, req.loc_off)) == NULL)
311
2021-04-30
op
fd = launch_cgi(&iri, &req, h, l);
312
2021-03-19
op
imsg_compose(ibuf, IMSG_CGI_RES, imsg->hdr.peerid, 0, fd, NULL, 0);
313
2021-03-19
op
imsg_flush(ibuf);
316
2021-05-09
op
static int
317
2021-05-09
op
fcgi_open_prog(struct fcgi *f)
319
2021-05-09
op
int s[2];
322
2021-05-09
op
/* XXX! */
324
2021-07-08
op
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, s) == -1)
325
2021-05-09
op
err(1, "socketpair");
327
2021-05-09
op
switch (p = fork()) {
329
2021-05-09
op
err(1, "fork");
331
2021-05-09
op
close(s[0]);
332
2021-05-09
op
if (dup2(s[1], 0) == -1)
333
2021-05-09
op
err(1, "dup2");
334
2021-05-09
op
execl(f->prog, f->prog, NULL);
335
2021-05-09
op
err(1, "execl %s", f->prog);
337
2021-05-09
op
close(s[1]);
338
2021-05-09
op
return s[0];
342
2021-05-09
op
static int
343
2021-05-09
op
fcgi_open_sock(struct fcgi *f)
345
2021-05-09
op
struct sockaddr_un addr;
348
2021-05-09
op
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
349
2021-05-09
op
log_err(NULL, "socket: %s", strerror(errno));
350
2021-05-09
op
return -1;
353
2021-05-09
op
memset(&addr, 0, sizeof(addr));
354
2021-05-09
op
addr.sun_family = AF_UNIX;
355
2021-05-09
op
strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
357
2021-05-09
op
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
358
2021-05-09
op
log_warn(NULL, "failed to connect to %s: %s", f->path,
359
2021-05-09
op
strerror(errno));
360
2021-05-09
op
close(fd);
361
2021-05-09
op
return -1;
364
2021-05-09
op
return fd;
367
2021-05-09
op
static int
368
2021-05-09
op
fcgi_open_conn(struct fcgi *f)
370
2021-05-09
op
struct addrinfo hints, *servinfo, *p;
371
2021-05-09
op
int r, sock;
373
2021-05-09
op
memset(&hints, 0, sizeof(hints));
374
2021-05-09
op
hints.ai_family = AF_UNSPEC;
375
2021-05-09
op
hints.ai_socktype = SOCK_STREAM;
376
2021-05-09
op
hints.ai_flags = AI_ADDRCONFIG;
378
2021-05-09
op
if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
379
2021-05-09
op
log_warn(NULL, "getaddrinfo %s:%s: %s", f->path, f->port,
380
2021-05-09
op
gai_strerror(r));
381
2021-05-09
op
return -1;
384
2021-05-09
op
for (p = servinfo; p != NULL; p = p->ai_next) {
385
2021-05-09
op
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
386
2021-05-09
op
if (sock == -1)
387
2021-05-09
op
continue;
388
2021-05-09
op
if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
389
2021-05-09
op
close(sock);
390
2021-05-09
op
continue;
395
2021-05-09
op
if (p == NULL) {
396
2021-05-09
op
log_warn(NULL, "couldn't connect to %s:%s", f->path, f->port);
397
2021-05-09
op
sock = -1;
400
2021-05-09
op
freeaddrinfo(servinfo);
401
2021-05-09
op
return sock;
404
2021-03-19
op
static void
405
2021-05-09
op
handle_imsg_fcgi_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
407
2021-05-09
op
struct fcgi *f;
408
2021-05-09
op
int id, fd;
410
2021-05-09
op
if (datalen != sizeof(id))
412
2022-03-27
op
memcpy(&id, imsg->data, sizeof(id));
414
2021-05-09
op
if (id > FCGI_MAX || (fcgi[id].path == NULL && fcgi[id].prog == NULL))
417
2021-05-09
op
f = &fcgi[id];
418
2021-05-09
op
if (f->prog != NULL)
419
2021-05-09
op
fd = fcgi_open_prog(f);
420
2021-05-09
op
else if (f->port != NULL)
421
2021-05-09
op
fd = fcgi_open_conn(f);
423
2021-05-09
op
fd = fcgi_open_sock(f);
425
2021-10-07
op
imsg_compose(ibuf, IMSG_FCGI_FD, imsg->hdr.peerid, 0, fd, NULL, 0);
426
2021-12-29
op
imsg_flush(ibuf);
429
2021-12-29
op
static void
430
2021-12-29
op
handle_imsg_conn_req(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
432
2021-12-29
op
struct addrinfo hints, *res, *res0;
433
2021-12-29
op
struct connreq req;
434
2021-12-29
op
int r, sock;
436
2021-12-29
op
if (datalen != sizeof(req))
438
2021-12-29
op
memcpy(&req, imsg->data, sizeof(req));
439
2021-12-29
op
req.flag = 0;
441
2021-12-29
op
memset(&hints, 0, sizeof(hints));
442
2021-12-29
op
hints.ai_family = AF_UNSPEC;
443
2021-12-29
op
hints.ai_socktype = SOCK_STREAM;
445
2021-12-29
op
/* XXX: do this asynchronously if possible */
446
2021-12-29
op
r = getaddrinfo(req.host, req.port, &hints, &res0);
447
2021-12-29
op
if (r != 0) {
448
2021-12-29
op
log_warn(NULL, "getaddrinfo(\"%s\", \"%s\"): %s",
449
2021-12-29
op
req.host, req.port, gai_strerror(r));
450
2021-12-29
op
goto err;
453
2021-12-29
op
for (res = res0; res; res = res->ai_next) {
454
2021-12-29
op
sock = socket(res->ai_family, res->ai_socktype,
455
2021-12-29
op
res->ai_protocol);
456
2021-12-29
op
if (sock == -1)
457
2021-12-29
op
continue;
459
2021-12-29
op
if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
460
2021-12-29
op
close(sock);
461
2021-12-29
op
sock = -1;
462
2021-12-29
op
continue;
468
2021-12-29
op
freeaddrinfo(res0);
470
2021-12-29
op
if (sock == -1) {
471
2021-12-29
op
log_warn(NULL, "can't connect to %s:%s", req.host,
472
2021-12-29
op
req.port);
473
2021-12-29
op
goto err;
476
2021-12-29
op
imsg_compose(ibuf, IMSG_CONN_FD, imsg->hdr.peerid, 0, sock, NULL, 0);
477
2021-05-09
op
imsg_flush(ibuf);
481
2021-12-29
op
imsg_compose(ibuf, IMSG_CONN_FD, imsg->hdr.peerid, 0, -1, NULL, 0);
482
2021-12-29
op
imsg_flush(ibuf);
485
2021-05-09
op
static void
486
2021-03-19
op
handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
490
2021-03-19
op
for (i = 0; i < conf.prefork; ++i) {
491
2021-03-19
op
imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
492
2021-03-19
op
imsg_flush(&exibuf);
493
2021-03-19
op
close(servibuf[i].fd);
496
2021-03-19
op
event_loopbreak();
499
2021-03-19
op
static void
500
2021-03-19
op
handle_dispatch_imsg(int fd, short ev, void *d)
502
2021-03-19
op
struct imsgbuf *ibuf = d;
503
2021-03-19
op
dispatch_imsg(ibuf, handlers, sizeof(handlers));
507
2021-03-19
op
executor_main(struct imsgbuf *ibuf)
509
2021-03-19
op
struct event evs[PROC_MAX], imsgev;
512
2021-03-03
op
event_init();
514
2021-03-19
op
if (ibuf != NULL) {
515
2021-03-19
op
event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
516
2021-03-19
op
handle_dispatch_imsg, ibuf);
517
2021-03-19
op
event_add(&imsgev, NULL);
520
2021-03-03
op
for (i = 0; i < conf.prefork; ++i) {
521
2021-03-19
op
event_set(&evs[i], servibuf[i].fd, EV_READ | EV_PERSIST,
522
2021-03-19
op
handle_dispatch_imsg, &servibuf[i]);
523
2021-03-03
op
event_add(&evs[i], NULL);
526
2021-03-20
op
sandbox_executor_process();
528
2021-03-03
op
event_dispatch();
530
2021-01-16
op
return 1;