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
|
/*
* Copyright (c) 2021 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 <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <string.h>
#include "gmid.h"
int
send_string(int fd, const char *str)
{
ssize_t len;
if (str == NULL)
len = 0;
else
len = strlen(str);
if (write(fd, &len, sizeof(len)) != sizeof(len))
return 0;
if (len != 0)
if (write(fd, str, len) != len)
return 0;
return 1;
}
int
recv_string(int fd, char **ret)
{
ssize_t len;
if (read(fd, &len, sizeof(len)) != sizeof(len))
return 0;
if (len == 0) {
*ret = NULL;
return 1;
}
if ((*ret = calloc(1, len+1)) == NULL)
return 0;
if (read(fd, *ret, len) != len)
return 0;
return 1;
}
int
send_iri(int fd, struct iri *i)
{
return send_string(fd, i->schema)
&& send_string(fd, i->host)
&& send_string(fd, i->port)
&& send_string(fd, i->path)
&& send_string(fd, i->query);
}
int
recv_iri(int fd, struct iri *i)
{
memset(i, 0, sizeof(*i));
if (!recv_string(fd, &i->schema)
|| !recv_string(fd, &i->host)
|| !recv_string(fd, &i->port)
|| !recv_string(fd, &i->path)
|| !recv_string(fd, &i->query))
return 0;
return 1;
}
void
free_recvd_iri(struct iri *i)
{
free(i->schema);
free(i->host);
free(i->port);
free(i->path);
free(i->query);
}
int
send_vhost(int fd, struct vhost *vhost)
{
ssize_t n;
if (vhost < hosts || vhost > hosts + HOSTSLEN)
return 0;
n = vhost - hosts;
return write(fd, &n, sizeof(n)) == sizeof(n);
}
int
recv_vhost(int fd, struct vhost **vhost)
{
ssize_t n;
if (read(fd, &n, sizeof(n)) != sizeof(n))
return 0;
if (n < 0 || n > HOSTSLEN)
return 0;
*vhost = &hosts[n];
if ((*vhost)->domain == NULL)
return 0;
return 1;
}
/* send d though fd. see /usr/src/usr.sbin/syslogd/privsep_fdpass.c
* for an example */
int
send_fd(int fd, int d)
{
struct msghdr msg;
union {
struct cmsghdr hdr;
unsigned char buf[CMSG_SPACE(sizeof(int))];
} cmsgbuf;
struct cmsghdr *cmsg;
struct iovec vec;
int result = 1;
ssize_t n;
memset(&msg, 0, sizeof(msg));
if (d >= 0) {
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int*)CMSG_DATA(cmsg) = d;
} else
result = 0;
vec.iov_base = &result;
vec.iov_len = sizeof(int);
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
if ((n = sendmsg(fd, &msg, 0)) == -1 || n != sizeof(int)) {
fprintf(stderr, "sendmsg: got %zu but wanted %zu: (errno) %s",
n, sizeof(int), strerror(errno));
return 0;
}
return 1;
}
/* receive a descriptor via fd */
int
recv_fd(int fd)
{
struct msghdr msg;
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(int))];
} cmsgbuf;
struct cmsghdr *cmsg;
struct iovec vec;
ssize_t n;
int result;
memset(&msg, 0, sizeof(msg));
vec.iov_base = &result;
vec.iov_len = sizeof(int);
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
if ((n = recvmsg(fd, &msg, 0)) != sizeof(int)) {
fprintf(stderr, "read %zu bytes bu wanted %zu\n", n, sizeof(int));
return -1;
}
if (result) {
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS)
return -1;
return (*(int *)CMSG_DATA(cmsg));
} else
return -1;
}
static inline void
safe_setenv(const char *name, const char *val)
{
if (val == NULL)
val = "";
setenv(name, val, 1);
}
static char *
xasprintf(const char *fmt, ...)
{
va_list ap;
char *s;
va_start(ap, fmt);
if (vasprintf(&s, fmt, ap) == -1)
s = NULL;
va_end(ap);
return s;
}
/* fd or -1 on error */
static int
launch_cgi(struct iri *iri, const char *spath, char *relpath,
const char *addr, const char *ruser, const char *cissuer,
const char *chash, struct vhost *vhost)
{
int p[2]; /* read end, write end */
if (pipe2(p, O_NONBLOCK) == -1)
return -1;
switch (fork()) {
case -1:
return -1;
case 0: { /* child */
char *argv[] = {NULL, NULL};
char *ex, *pwd;
char iribuf[GEMINI_URL_LEN];
char path[PATH_MAX];
close(p[0]);
if (dup2(p[1], 1) == -1)
goto childerr;
ex = xasprintf("%s/%s", vhost->dir, spath);
argv[0] = ex;
serialize_iri(iri, iribuf, sizeof(iribuf));
safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
safe_setenv("GEMINI_DOCUMENT_ROOT", vhost->dir);
safe_setenv("GEMINI_SCRIPT_FILENAME",
xasprintf("%s/%s", vhost->dir, spath));
safe_setenv("GEMINI_URL", iribuf);
strlcpy(path, "/", sizeof(path));
strlcat(path, spath, sizeof(path));
safe_setenv("GEMINI_URL_PATH", path);
if (relpath != NULL) {
strlcpy(path, "/", sizeof(path));
strlcat(path, relpath, sizeof(path));
safe_setenv("PATH_INFO", path);
strlcpy(path, vhost->dir, sizeof(path));
strlcat(path, "/", sizeof(path));
strlcat(path, relpath, sizeof(path));
safe_setenv("PATH_TRANSLATED", path);
}
safe_setenv("QUERY_STRING", iri->query);
safe_setenv("REMOTE_ADDR", addr);
safe_setenv("REMOTE_HOST", addr);
safe_setenv("REQUEST_METHOD", "");
strlcpy(path, "/", sizeof(path));
strlcat(path, spath, sizeof(path));
safe_setenv("SCRIPT_NAME", path);
safe_setenv("SERVER_NAME", iri->host);
snprintf(path, sizeof(path), "%d", conf.port);
safe_setenv("SERVER_PORT", path);
safe_setenv("SERVER_PROTOCOL", "GEMINI");
safe_setenv("SERVER_SOFTWARE", "gmid/1.5");
if (ruser != NULL)
safe_setenv("AUTH_TYPE", "Certificate");
else
safe_setenv("AUTH_TYPE", "");
safe_setenv("REMOTE_USER", ruser);
safe_setenv("TLS_CLIENT_ISSUER", cissuer);
safe_setenv("TLS_CLIENT_HASH", chash);
strlcpy(path, argv[0], sizeof(path));
pwd = dirname(path);
if (chdir(pwd)) {
warn("chdir");
goto childerr;
}
execvp(argv[0], argv);
warn("execvp: %s", argv[0]);
goto childerr;
}
default:
close(p[1]);
return p[0];
}
childerr:
dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
_exit(1);
}
int
executor_main(int fd)
{
char *spath, *relpath, *addr, *ruser, *cissuer, *chash;
struct vhost *vhost;
struct iri iri;
int d;
#ifdef __OpenBSD__
for (vhost = hosts; vhost->domain != NULL; ++vhost) {
/* r so we can chdir into the correct directory */
if (unveil(vhost->dir, "rx") == -1)
err(1, "unveil %s for domain %s",
vhost->dir, vhost->domain);
}
/* rpath to chdir into the correct directory */
if (pledge("stdio rpath sendfd proc exec", NULL))
err(1, "pledge");
#endif
for (;;) {
if (!recv_iri(fd, &iri)
|| !recv_string(fd, &spath)
|| !recv_string(fd, &relpath)
|| !recv_string(fd, &addr)
|| !recv_string(fd, &ruser)
|| !recv_string(fd, &cissuer)
|| !recv_string(fd, &chash)
|| !recv_vhost(fd, &vhost))
break;
d = launch_cgi(&iri, spath, relpath, addr, ruser, cissuer, chash,
vhost);
if (!send_fd(fd, d))
break;
close(d);
free_recvd_iri(&iri);
free(spath);
free(relpath);
free(addr);
free(ruser);
free(cissuer);
free(chash);
}
/* kill all process in my group. This means the listener and
* every pending CGI script. */
kill(0, SIGINT);
return 1;
}
|