002
2022-09-07
op
* Copyright (c) 2022 Omar Polo <op@omarpolo.com>
004
2022-09-07
op
* Permission to use, copy, modify, and distribute this software for any
005
2022-09-07
op
* purpose with or without fee is hereby granted, provided that the above
006
2022-09-07
op
* copyright notice and this permission notice appear in all copies.
008
2022-09-07
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2022-09-07
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2022-09-07
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2022-09-07
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2022-09-07
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2022-09-07
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2022-09-07
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2022-09-07
op
#include "gmid.h"
019
2022-09-07
op
#include <sys/stat.h>
020
2022-09-07
op
#include <sys/wait.h>
022
2022-09-07
op
#include <errno.h>
023
2022-09-07
op
#include <fcntl.h>
024
2022-09-07
op
#include <getopt.h>
025
2022-09-07
op
#include <libgen.h>
026
2022-09-07
op
#include <signal.h>
027
2022-09-07
op
#include <string.h>
028
2022-09-07
op
#include <unistd.h>
030
2022-09-07
op
struct imsgbuf ibuf, logibuf;
031
2022-09-07
op
struct conf conf;
033
2022-09-07
op
struct fcgi fcgi[FCGI_MAX]; /* just because it's referenced */
034
2022-09-07
op
struct vhosthead hosts;
037
2022-09-07
op
static const struct option opts[] = {
038
2022-09-07
op
{"help", no_argument, NULL, 'h'},
039
2022-09-07
op
{"version", no_argument, NULL, 'V'},
040
2022-09-07
op
{NULL, 0, NULL, 0},
044
2022-09-07
op
load_local_cert(struct vhost *h, const char *hostname, const char *dir)
046
2022-09-07
op
char *cert, *key;
048
2022-09-07
op
if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
049
2022-09-07
op
errx(1, "asprintf");
050
2022-09-07
op
if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
051
2022-09-07
op
errx(1, "asprintf");
053
2022-09-07
op
if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
054
2022-09-07
op
gen_certificate(hostname, cert, key);
056
2022-10-05
op
strlcpy(h->cert, cert, sizeof(h->cert));
057
2022-10-05
op
strlcpy(h->key, key, sizeof(h->key));
058
2022-10-05
op
strlcpy(h->domain, hostname, sizeof(h->domain));
061
2022-09-07
op
/* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
062
2022-09-07
op
static void
063
2022-09-07
op
pdirname(const char *path, char *dn)
065
2022-09-07
op
char p[PATH_MAX+1];
068
2022-09-07
op
strlcpy(p, path, sizeof(p));
069
2022-09-07
op
t = dirname(p);
070
2022-09-07
op
memmove(dn, t, strlen(t)+1);
073
2022-09-07
op
static void
074
2022-09-07
op
mkdirs(const char *path, mode_t mode)
076
2022-09-07
op
char dname[PATH_MAX+1];
078
2022-09-07
op
pdirname(path, dname);
079
2022-09-07
op
if (!strcmp(dname, "/"))
081
2022-09-07
op
mkdirs(dname, mode);
082
2022-09-07
op
if (mkdir(path, mode) != 0 && errno != EEXIST)
083
2022-09-07
op
fatal("can't mkdir %s: %s", path, strerror(errno));
086
2022-09-07
op
/* $XDG_DATA_HOME/gmid */
088
2022-09-07
op
data_dir(void)
090
2022-09-07
op
const char *home, *xdg;
093
2022-09-07
op
if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
094
2022-09-07
op
if ((home = getenv("HOME")) == NULL)
095
2022-09-07
op
errx(1, "XDG_DATA_HOME and HOME both empty");
096
2022-09-07
op
if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
097
2022-09-07
op
err(1, "asprintf");
099
2022-09-07
op
if (asprintf(&t, "%s/gmid", xdg) == -1)
100
2022-09-07
op
err(1, "asprintf");
103
2022-09-07
op
mkdirs(t, 0755);
104
2022-09-07
op
return t;
107
2022-09-07
op
static void
108
2022-09-07
op
logger_init(void)
110
2022-09-07
op
int p[2];
112
2022-09-07
op
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
113
2022-09-07
op
err(1, "socketpair");
115
2022-09-07
op
switch (fork()) {
117
2022-09-07
op
err(1, "fork");
119
2022-09-07
op
close(p[0]);
120
2022-09-07
op
setproctitle("logger");
121
2022-09-07
op
imsg_init(&logibuf, p[1]);
122
2022-09-07
op
_exit(logger_main(p[1], &logibuf));
124
2022-09-07
op
close(p[1]);
125
2022-09-07
op
imsg_init(&logibuf, p[0]);
130
2022-09-07
op
static int
131
2022-09-07
op
serve(const char *host, int port, const char *dir, struct tls *ctx)
133
2022-09-07
op
struct addrinfo hints, *res, *res0;
134
2022-09-07
op
int error, saved_errno, sock = -1;
135
2022-09-07
op
const char *cause = NULL;
136
2022-09-07
op
char service[32];
138
2022-09-07
op
if (snprintf(service, sizeof(service), "%d", port) < 0)
139
2022-09-07
op
fatal("snprintf");
141
2022-09-07
op
memset(&hints, 0, sizeof(hints));
142
2022-09-07
op
hints.ai_family = AF_UNSPEC;
143
2022-09-07
op
hints.ai_socktype = SOCK_STREAM;
144
2022-09-07
op
hints.ai_flags = AI_PASSIVE;
145
2022-09-07
op
error = getaddrinfo(host, service, &hints, &res0);
146
2022-09-07
op
if (error)
147
2022-09-07
op
fatal("%s", gai_strerror(error));
148
2022-09-07
op
for (res = res0; res; res = res->ai_next) {
149
2022-09-07
op
sock = socket(res->ai_family, res->ai_socktype,
150
2022-09-07
op
res->ai_protocol);
151
2022-09-07
op
if (sock == -1) {
152
2022-09-07
op
cause = "socket";
153
2022-09-07
op
continue;
156
2022-09-07
op
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
157
2022-09-07
op
cause = "bind";
158
2022-09-07
op
saved_errno = errno;
159
2022-09-07
op
close(sock);
160
2022-09-07
op
errno = saved_errno;
161
2022-09-07
op
continue;
164
2022-09-07
op
if (listen(sock, 5) == -1)
165
2022-09-07
op
fatal("listen");
168
2022-09-07
op
* for the time being, we're happy as soon as
169
2022-09-07
op
* something binds.
174
2022-09-07
op
if (sock == -1)
175
2022-09-07
op
fatal("%s", cause);
176
2022-09-07
op
freeaddrinfo(res0);
178
2022-09-07
op
log_notice(NULL, "serving %s on port %d", dir, port);
179
2022-09-07
op
loop(ctx, sock, -1, NULL);
180
2022-09-07
op
return 0;
183
2022-09-07
op
static __dead void
184
2022-09-07
op
usage(void)
186
2022-09-07
op
fprintf(stderr,
187
2022-09-10
op
"Version: " GE_STRING "\n"
188
2022-09-07
op
"Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
189
2022-09-07
op
getprogname());
194
2022-09-07
op
main(int argc, char **argv)
196
2022-09-07
op
struct tls_config *tlsconf;
197
2022-09-07
op
struct tls *ctx;
198
2022-09-07
op
struct vhost *host;
199
2022-09-07
op
struct location *loc;
200
2022-09-07
op
const char *errstr, *certs_dir = NULL, *hostname = "localhost";
201
2022-09-07
op
char path[PATH_MAX];
204
2022-09-07
op
logger_init();
205
2022-09-07
op
conf.port = 1965;
207
2022-09-07
op
while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
208
2022-09-07
op
switch (ch) {
209
2022-09-07
op
case 'd':
210
2022-09-07
op
certs_dir = optarg;
212
2022-09-07
op
case 'H':
213
2022-09-07
op
hostname = optarg;
215
2022-09-07
op
case 'h':
218
2022-09-07
op
case 'p':
219
2022-09-07
op
conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
220
2022-09-07
op
if (errstr)
221
2022-09-07
op
fatal("port number is %s: %s", errstr, optarg);
223
2022-09-07
op
case 'V':
224
2022-09-10
op
puts("Version: " GE_STRING);
225
2022-09-07
op
return 0;
231
2022-09-07
op
argc -= optind;
232
2022-09-07
op
argv += optind;
234
2022-09-07
op
if (argc > 1)
237
2022-09-07
op
/* prepare the configuration */
238
2022-09-07
op
conf.verbose = 1;
239
2022-09-07
op
init_mime(&conf.mime);
241
2022-09-07
op
if (certs_dir == NULL)
242
2022-09-07
op
certs_dir = data_dir();
244
2022-09-07
op
if (load_default_mime(&conf.mime) == -1)
245
2022-09-07
op
fatal("can't load default mime types");
246
2022-09-07
op
sort_mime(&conf.mime);
248
2022-09-07
op
/* set up the implicit vhost and location */
250
2022-09-07
op
host = xcalloc(1, sizeof(*host));
251
2022-09-07
op
TAILQ_INSERT_HEAD(&hosts, host, vhosts);
253
2022-09-07
op
loc = xcalloc(1, sizeof(*loc));
254
2022-09-07
op
loc->fcgi = -1;
255
2022-09-07
op
TAILQ_INSERT_HEAD(&host->locations, loc, locations);
257
2022-09-07
op
load_local_cert(host, hostname, certs_dir);
259
2022-10-05
op
strlcpy(host->domain, "*", sizeof(host->domain));
260
2022-09-07
op
loc->auto_index = 1;
261
2022-10-05
op
strlcpy(loc->match, "*", sizeof(loc->match));
263
2022-09-07
op
if (*argv == NULL) {
264
2022-09-07
op
if (getcwd(path, sizeof(path)) == NULL)
265
2022-09-07
op
fatal("getcwd");
266
2022-10-05
op
strlcpy(loc->dir, path, sizeof(loc->dir));
268
2022-10-05
op
char *tmp;
270
2022-10-05
op
tmp = absolutify_path(*argv);
271
2022-10-05
op
strlcpy(loc->dir, tmp, sizeof(loc->dir));
272
2022-10-05
op
free(tmp);
275
2022-09-07
op
if ((loc->dirfd = open(loc->dir, O_RDONLY|O_DIRECTORY)) == -1)
276
2022-09-07
op
fatal("can't open %s", loc->dir);
278
2022-09-07
op
/* setup tls */
280
2022-09-07
op
if ((tlsconf = tls_config_new()) == NULL)
281
2022-09-07
op
fatal("tls_config_new"); /* XXX: fatalx */
283
2022-09-07
op
/* optionally accept client certs but don't try to verify them */
284
2022-09-07
op
tls_config_verify_client_optional(tlsconf);
285
2022-09-07
op
tls_config_insecure_noverifycert(tlsconf);
287
2022-09-07
op
if ((ctx = tls_server()) == NULL)
288
2022-09-07
op
fatal("tls_server failure"); /* XXX: fatalx */
290
2022-09-07
op
if (tls_config_set_keypair_file(tlsconf, host->cert, host->key))
291
2022-09-07
op
fatal("can't load the keypair (%s, %s)",
292
2022-09-07
op
host->cert, host->key);
294
2022-09-07
op
if (tls_configure(ctx, tlsconf) == -1)
295
2022-09-07
op
fatal("tls_configure: %s", tls_error(ctx));
297
2022-09-07
op
/* start the server */
298
2022-09-07
op
signal(SIGPIPE, SIG_IGN);
299
2022-09-07
op
setproctitle("%s", loc->dir);
300
2022-09-07
op
return serve(hostname, conf.port, loc->dir, ctx);