Blame
Date:
Wed Sep 7 20:47:33 2022 UTC
Message:
add ge: gemini export!
001
2022-09-07
op
/*
002
2022-09-07
op
* Copyright (c) 2022 Omar Polo <op@omarpolo.com>
003
2022-09-07
op
*
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.
007
2022-09-07
op
*
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.
015
2022-09-07
op
*/
016
2022-09-07
op
017
2022-09-07
op
#include "gmid.h"
018
2022-09-07
op
019
2022-09-07
op
#include <sys/stat.h>
020
2022-09-07
op
#include <sys/wait.h>
021
2022-09-07
op
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>
029
2022-09-07
op
030
2022-09-07
op
struct imsgbuf ibuf, logibuf;
031
2022-09-07
op
struct conf conf;
032
2022-09-07
op
033
2022-09-07
op
struct fcgi fcgi[FCGI_MAX]; /* just because it's referenced */
034
2022-09-07
op
struct vhosthead hosts;
035
2022-09-07
op
036
2022-09-07
op
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},
041
2022-09-07
op
};
042
2022-09-07
op
043
2022-09-07
op
void
044
2022-09-07
op
load_local_cert(struct vhost *h, const char *hostname, const char *dir)
045
2022-09-07
op
{
046
2022-09-07
op
char *cert, *key;
047
2022-09-07
op
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");
052
2022-09-07
op
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);
055
2022-09-07
op
056
2022-09-07
op
h->cert = cert;
057
2022-09-07
op
h->key = key;
058
2022-09-07
op
h->domain = hostname;
059
2022-09-07
op
}
060
2022-09-07
op
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)
064
2022-09-07
op
{
065
2022-09-07
op
char p[PATH_MAX+1];
066
2022-09-07
op
char *t;
067
2022-09-07
op
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);
071
2022-09-07
op
}
072
2022-09-07
op
073
2022-09-07
op
static void
074
2022-09-07
op
mkdirs(const char *path, mode_t mode)
075
2022-09-07
op
{
076
2022-09-07
op
char dname[PATH_MAX+1];
077
2022-09-07
op
078
2022-09-07
op
pdirname(path, dname);
079
2022-09-07
op
if (!strcmp(dname, "/"))
080
2022-09-07
op
return;
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));
084
2022-09-07
op
}
085
2022-09-07
op
086
2022-09-07
op
/* $XDG_DATA_HOME/gmid */
087
2022-09-07
op
char *
088
2022-09-07
op
data_dir(void)
089
2022-09-07
op
{
090
2022-09-07
op
const char *home, *xdg;
091
2022-09-07
op
char *t;
092
2022-09-07
op
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");
098
2022-09-07
op
} else {
099
2022-09-07
op
if (asprintf(&t, "%s/gmid", xdg) == -1)
100
2022-09-07
op
err(1, "asprintf");
101
2022-09-07
op
}
102
2022-09-07
op
103
2022-09-07
op
mkdirs(t, 0755);
104
2022-09-07
op
return t;
105
2022-09-07
op
}
106
2022-09-07
op
107
2022-09-07
op
static void
108
2022-09-07
op
logger_init(void)
109
2022-09-07
op
{
110
2022-09-07
op
int p[2];
111
2022-09-07
op
112
2022-09-07
op
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
113
2022-09-07
op
err(1, "socketpair");
114
2022-09-07
op
115
2022-09-07
op
switch (fork()) {
116
2022-09-07
op
case -1:
117
2022-09-07
op
err(1, "fork");
118
2022-09-07
op
case 0:
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));
123
2022-09-07
op
default:
124
2022-09-07
op
close(p[1]);
125
2022-09-07
op
imsg_init(&logibuf, p[0]);
126
2022-09-07
op
return;
127
2022-09-07
op
}
128
2022-09-07
op
}
129
2022-09-07
op
130
2022-09-07
op
static int
131
2022-09-07
op
serve(const char *host, int port, const char *dir, struct tls *ctx)
132
2022-09-07
op
{
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];
137
2022-09-07
op
138
2022-09-07
op
if (snprintf(service, sizeof(service), "%d", port) < 0)
139
2022-09-07
op
fatal("snprintf");
140
2022-09-07
op
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;
154
2022-09-07
op
}
155
2022-09-07
op
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;
162
2022-09-07
op
}
163
2022-09-07
op
164
2022-09-07
op
if (listen(sock, 5) == -1)
165
2022-09-07
op
fatal("listen");
166
2022-09-07
op
167
2022-09-07
op
/*
168
2022-09-07
op
* for the time being, we're happy as soon as
169
2022-09-07
op
* something binds.
170
2022-09-07
op
*/
171
2022-09-07
op
break;
172
2022-09-07
op
}
173
2022-09-07
op
174
2022-09-07
op
if (sock == -1)
175
2022-09-07
op
fatal("%s", cause);
176
2022-09-07
op
freeaddrinfo(res0);
177
2022-09-07
op
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;
181
2022-09-07
op
}
182
2022-09-07
op
183
2022-09-07
op
static __dead void
184
2022-09-07
op
usage(void)
185
2022-09-07
op
{
186
2022-09-07
op
fprintf(stderr,
187
2022-09-07
op
"Version: " GMID_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());
190
2022-09-07
op
exit(1);
191
2022-09-07
op
}
192
2022-09-07
op
193
2022-09-07
op
int
194
2022-09-07
op
main(int argc, char **argv)
195
2022-09-07
op
{
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];
202
2022-09-07
op
int ch;
203
2022-09-07
op
204
2022-09-07
op
logger_init();
205
2022-09-07
op
conf.port = 1965;
206
2022-09-07
op
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;
211
2022-09-07
op
break;
212
2022-09-07
op
case 'H':
213
2022-09-07
op
hostname = optarg;
214
2022-09-07
op
break;
215
2022-09-07
op
case 'h':
216
2022-09-07
op
usage();
217
2022-09-07
op
break;
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);
222
2022-09-07
op
break;
223
2022-09-07
op
case 'V':
224
2022-09-07
op
puts("Version: " GMID_STRING);
225
2022-09-07
op
return 0;
226
2022-09-07
op
default:
227
2022-09-07
op
usage();
228
2022-09-07
op
break;
229
2022-09-07
op
}
230
2022-09-07
op
}
231
2022-09-07
op
argc -= optind;
232
2022-09-07
op
argv += optind;
233
2022-09-07
op
234
2022-09-07
op
if (argc > 1)
235
2022-09-07
op
usage();
236
2022-09-07
op
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);
240
2022-09-07
op
241
2022-09-07
op
if (certs_dir == NULL)
242
2022-09-07
op
certs_dir = data_dir();
243
2022-09-07
op
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);
247
2022-09-07
op
248
2022-09-07
op
/* set up the implicit vhost and location */
249
2022-09-07
op
250
2022-09-07
op
host = xcalloc(1, sizeof(*host));
251
2022-09-07
op
TAILQ_INSERT_HEAD(&hosts, host, vhosts);
252
2022-09-07
op
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);
256
2022-09-07
op
257
2022-09-07
op
load_local_cert(host, hostname, certs_dir);
258
2022-09-07
op
259
2022-09-07
op
host->domain = "*";
260
2022-09-07
op
loc->auto_index = 1;
261
2022-09-07
op
loc->match = "*";
262
2022-09-07
op
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-09-07
op
loc->dir = path;
267
2022-09-07
op
} else
268
2022-09-07
op
loc->dir = absolutify_path(*argv);
269
2022-09-07
op
270
2022-09-07
op
if ((loc->dirfd = open(loc->dir, O_RDONLY|O_DIRECTORY)) == -1)
271
2022-09-07
op
fatal("can't open %s", loc->dir);
272
2022-09-07
op
273
2022-09-07
op
/* setup tls */
274
2022-09-07
op
275
2022-09-07
op
if ((tlsconf = tls_config_new()) == NULL)
276
2022-09-07
op
fatal("tls_config_new"); /* XXX: fatalx */
277
2022-09-07
op
278
2022-09-07
op
/* optionally accept client certs but don't try to verify them */
279
2022-09-07
op
tls_config_verify_client_optional(tlsconf);
280
2022-09-07
op
tls_config_insecure_noverifycert(tlsconf);
281
2022-09-07
op
282
2022-09-07
op
if ((ctx = tls_server()) == NULL)
283
2022-09-07
op
fatal("tls_server failure"); /* XXX: fatalx */
284
2022-09-07
op
285
2022-09-07
op
if (tls_config_set_keypair_file(tlsconf, host->cert, host->key))
286
2022-09-07
op
fatal("can't load the keypair (%s, %s)",
287
2022-09-07
op
host->cert, host->key);
288
2022-09-07
op
289
2022-09-07
op
if (tls_configure(ctx, tlsconf) == -1)
290
2022-09-07
op
fatal("tls_configure: %s", tls_error(ctx));
291
2022-09-07
op
292
2022-09-07
op
/* start the server */
293
2022-09-07
op
signal(SIGPIPE, SIG_IGN);
294
2022-09-07
op
setproctitle("%s", loc->dir);
295
2022-09-07
op
return serve(hostname, conf.port, loc->dir, ctx);
296
2022-09-07
op
}
Omar Polo