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
|
/*
* Copyright (c) 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 <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <locale.h>
#include <libgen.h>
#include <signal.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <vis.h>
#include "log.h"
int privsep_process;
static const struct option opts[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0},
};
void
log_request(struct client *c, int code, const char *meta)
{
char b[GEMINI_URL_LEN];
char cntmp[64], cn[64] = "-";
char rfc3339[32];
const char *t;
struct tm *tm;
time_t now;
if ((now = time(NULL)) == -1)
fatal("time");
if ((tm = localtime(&now)) == NULL)
fatal("localtime");
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);
}
}
fprintf(stderr, "%s %s %s %s %s 0 %d %s\n", rfc3339, c->rhost, cn,
*c->domain == '\0' ? c->iri.host : c->domain, b, code, meta);
}
void
load_local_cert(struct vhost *h, const char *hostname, const char *dir)
{
char *cert, *key;
if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
fatal("asprintf");
if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
fatal("asprintf");
if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
gen_certificate(hostname, cert, key);
h->cert = tls_load_file(cert, &h->certlen, NULL);
if (h->cert == NULL)
fatal("can't load %s", cert);
h->key = tls_load_file(key, &h->keylen, NULL);
if (h->key == NULL)
fatal("can't load %s", key);
strlcpy(h->domain, hostname, sizeof(h->domain));
}
/* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
static void
pdirname(const char *path, char *dn)
{
char p[PATH_MAX+1];
char *t;
strlcpy(p, path, sizeof(p));
t = dirname(p);
memmove(dn, t, strlen(t)+1);
}
static void
mkdirs(const char *path, mode_t mode)
{
char dname[PATH_MAX+1];
pdirname(path, dname);
if (!strcmp(dname, "/"))
return;
mkdirs(dname, mode);
if (mkdir(path, mode) != 0 && errno != EEXIST)
fatal("can't mkdir %s", path);
}
/* $XDG_DATA_HOME/gmid */
char *
data_dir(void)
{
const char *home, *xdg;
char *t;
if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
if ((home = getenv("HOME")) == NULL)
fatalx("XDG_DATA_HOME and HOME both empty");
if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
fatalx("asprintf");
} else {
if (asprintf(&t, "%s/gmid", xdg) == -1)
fatal("asprintf");
}
mkdirs(t, 0755);
return t;
}
static int
serve(struct conf *conf, const char *host, int port, const char *dir)
{
struct addrinfo hints, *res, *res0;
struct vhost *vh = TAILQ_FIRST(&conf->hosts);
struct address *addr, *acp;
int r, error, saved_errno, sock = -1;
const char *cause = NULL;
char service[32];
int any = 0;
event_init();
r = snprintf(service, sizeof(service), "%d", port);
if (r < 0 || (size_t)r >= sizeof(service))
fatal("snprintf");
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(host, service, &hints, &res0);
if (error)
fatalx("%s", gai_strerror(error));
for (res = res0; res; res = res->ai_next) {
sock = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if (sock == -1) {
cause = "socket";
continue;
}
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
cause = "bind";
saved_errno = errno;
close(sock);
errno = saved_errno;
continue;
}
if (listen(sock, 5) == -1)
fatal("listen");
any = 1;
addr = xcalloc(1, sizeof(*addr));
addr->ai_flags = res->ai_flags;
addr->ai_family = res->ai_family;
addr->ai_socktype = res->ai_socktype;
addr->ai_protocol = res->ai_protocol;
addr->slen = res->ai_addrlen;
memcpy(&addr->ss, res->ai_addr, res->ai_addrlen);
addr->conf = conf;
addr->sock = sock;
event_set(&addr->evsock, addr->sock, EV_READ|EV_PERSIST,
server_accept, addr);
if ((addr->ctx = tls_server()) == NULL)
fatal("tls_server failure");
TAILQ_INSERT_HEAD(&conf->addrs, addr, addrs);
acp = xcalloc(1, sizeof(*acp));
memcpy(acp, addr, sizeof(*acp));
acp->sock = -1;
memset(&acp->evsock, 0, sizeof(acp->evsock));
TAILQ_INSERT_HEAD(&vh->addrs, addr, addrs);
}
if (!any)
fatal("%s", cause);
freeaddrinfo(res0);
server_init(NULL, NULL, NULL);
if (server_configure_done(conf) == -1)
fatalx("server configuration failed");
log_info("serving %s on port %d", dir, port);
event_dispatch();
log_info("quitting");
return 0;
}
static __dead void
usage(void)
{
fprintf(stderr,
"Version: " GE_STRING "\n"
"Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
getprogname());
exit(1);
}
int
main(int argc, char **argv)
{
struct conf *conf;
struct vhost *host;
struct location *loc;
const char *errstr, *certs_dir = NULL, *hostname = "localhost";
char path[PATH_MAX];
int ch, port = 1965;
setlocale(LC_CTYPE, "");
log_init(1, LOG_DAEMON);
log_setverbose(0);
conf = config_new();
/* ge doesn't do privsep so no privsep crypto engine. */
conf->use_privsep_crypto = 0;
while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
switch (ch) {
case 'd':
certs_dir = optarg;
break;
case 'H':
hostname = optarg;
break;
case 'h':
usage();
break;
case 'p':
port = strtonum(optarg, 0, UINT16_MAX, &errstr);
if (errstr)
fatalx("port number is %s: %s", errstr,
optarg);
break;
case 'V':
puts("Version: " GE_STRING);
return 0;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (argc > 1)
usage();
/* prepare the configuration */
init_mime(&conf->mime);
if (certs_dir == NULL)
certs_dir = data_dir();
/* set up the implicit vhost and location */
host = xcalloc(1, sizeof(*host));
TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
loc = xcalloc(1, sizeof(*loc));
loc->fcgi = -1;
TAILQ_INSERT_HEAD(&host->locations, loc, locations);
load_local_cert(host, hostname, certs_dir);
strlcpy(host->domain, "*", sizeof(host->domain));
loc->auto_index = 1;
strlcpy(loc->match, "*", sizeof(loc->match));
if (*argv == NULL) {
if (getcwd(path, sizeof(path)) == NULL)
fatal("getcwd");
strlcpy(loc->dir, path, sizeof(loc->dir));
} else {
char *tmp;
tmp = absolutify_path(*argv);
strlcpy(loc->dir, tmp, sizeof(loc->dir));
free(tmp);
}
/* start the server */
signal(SIGPIPE, SIG_IGN);
setproctitle("%s", loc->dir);
return serve(conf, hostname, port, loc->dir);
}
|