002
2021-12-29
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
004
2021-12-29
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-12-29
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-12-29
op
* copyright notice and this permission notice appear in all copies.
008
2021-12-29
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-12-29
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-12-29
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-12-29
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-12-29
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-12-29
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-12-29
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2021-12-29
op
#include "gmid.h"
019
2021-12-29
op
#include <sys/socket.h>
021
2021-12-29
op
#include <assert.h>
022
2021-12-29
op
#include <ctype.h>
023
2021-12-29
op
#include <errno.h>
024
2021-12-29
op
#include <string.h>
026
2021-12-29
op
enum debug {
027
2021-12-29
op
DEBUG_NONE,
028
2021-12-29
op
DEBUG_CODE,
029
2021-12-29
op
DEBUG_HEADER,
030
2021-12-29
op
DEBUG_META,
031
2022-01-27
op
DEBUG_ALL,
034
2021-12-29
op
/* flags */
035
2021-12-29
op
int debug;
036
2021-12-29
op
int dont_verify_name;
037
2021-12-29
op
int flag2;
038
2021-12-29
op
int flag3;
040
2021-12-29
op
int redirects = 5;
041
2021-12-29
op
int timer;
042
2021-12-29
op
int verbose;
043
2021-12-29
op
const char *cert;
044
2021-12-29
op
const char *key;
045
2021-12-29
op
const char *proxy_host;
046
2021-12-29
op
const char *proxy_port;
047
2021-12-29
op
const char *sni;
049
2021-12-29
op
/* state */
050
2021-12-29
op
struct tls_config *tls_conf;
052
2021-12-29
op
static void
053
2021-12-29
op
timeout(int signo)
055
2021-12-29
op
dprintf(2, "%s: timer expired\n", getprogname());
059
2021-12-29
op
static void
060
2021-12-29
op
load_tls_conf(void)
062
2021-12-29
op
if ((tls_conf = tls_config_new()) == NULL)
063
2021-12-29
op
err(1, "tls_config_new");
065
2021-12-29
op
tls_config_insecure_noverifycert(tls_conf);
066
2021-12-29
op
if (dont_verify_name)
067
2021-12-29
op
tls_config_insecure_noverifyname(tls_conf);
069
2021-12-29
op
if (flag2 &&
070
2021-12-29
op
tls_config_set_protocols(tls_conf, TLS_PROTOCOL_TLSv1_2) == -1)
071
2021-12-29
op
errx(1, "can't set TLSv1.2");
072
2021-12-29
op
if (flag3 &&
073
2021-12-29
op
tls_config_set_protocols(tls_conf, TLS_PROTOCOL_TLSv1_3) == -1)
074
2021-12-29
op
errx(1, "can't set TLSv1.3");
076
2021-12-29
op
if (cert != NULL &&
077
2021-12-29
op
tls_config_set_keypair_file(tls_conf, cert, key) == -1)
078
2021-12-29
op
errx(1, "can't load client certificate %s", cert);
081
2021-12-29
op
static void
082
2021-12-29
op
connectto(struct tls *ctx, const char *host, const char *port)
084
2021-12-29
op
struct addrinfo hints, *res, *res0;
085
2021-12-29
op
int error;
086
2021-12-29
op
int saved_errno;
088
2021-12-29
op
const char *cause = NULL;
089
2021-12-29
op
const char *sname;
091
2021-12-29
op
if (proxy_host != NULL) {
092
2021-12-29
op
host = proxy_host;
093
2021-12-29
op
port = proxy_port;
096
2021-12-29
op
if ((sname = sni) == NULL)
097
2021-12-29
op
sname = host;
099
2021-12-29
op
memset(&hints, 0, sizeof(hints));
100
2021-12-29
op
hints.ai_family = AF_UNSPEC;
101
2021-12-29
op
hints.ai_socktype = SOCK_STREAM;
102
2021-12-29
op
error = getaddrinfo(host, port, &hints, &res0);
103
2021-12-29
op
if (error)
104
2021-12-29
op
errx(1, "%s", gai_strerror(error));
107
2021-12-29
op
for (res = res0; res != NULL; res = res->ai_next) {
108
2021-12-29
op
s = socket(res->ai_family, res->ai_socktype,
109
2021-12-29
op
res->ai_protocol);
110
2021-12-29
op
if (s == -1) {
111
2021-12-29
op
cause = "socket";
112
2021-12-29
op
continue;
115
2021-12-29
op
if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
116
2021-12-29
op
cause = "connect";
117
2021-12-29
op
saved_errno = errno;
118
2021-12-29
op
close(s);
119
2021-12-29
op
errno = saved_errno;
121
2021-12-29
op
continue;
127
2021-12-29
op
if (s == -1)
128
2021-12-29
op
err(1, "%s: can't connect to %s:%s", cause,
129
2021-12-29
op
host, port);
131
2021-12-29
op
freeaddrinfo(res0);
133
2021-12-29
op
if (tls_connect_socket(ctx, s, sname) == -1)
134
2021-12-29
op
errx(1, "tls_connect_socket: %s", tls_error(ctx));
137
2021-12-29
op
static void
138
2021-12-29
op
doreq(struct tls *ctx, const char *buf)
140
2021-12-29
op
size_t s;
141
2021-12-29
op
ssize_t w;
143
2021-12-29
op
s = strlen(buf);
144
2021-12-29
op
while (s != 0) {
145
2021-12-29
op
switch (w = tls_write(ctx, buf, s)) {
148
2021-12-29
op
errx(1, "tls_write: %s", tls_error(ctx));
149
2021-12-29
op
case TLS_WANT_POLLIN:
150
2021-12-29
op
case TLS_WANT_POLLOUT:
151
2021-12-29
op
continue;
155
2021-12-29
op
buf += w;
159
2021-12-29
op
static size_t
160
2021-12-29
op
dorep(struct tls *ctx, void *buf, size_t len)
162
2021-12-29
op
ssize_t w;
163
2021-12-29
op
size_t tot = 0;
165
2021-12-29
op
while (len != 0) {
166
2021-12-29
op
switch (w = tls_read(ctx, buf, len)) {
168
2021-12-29
op
return tot;
170
2021-12-29
op
errx(1, "tls_write: %s", tls_error(ctx));
171
2021-12-29
op
case TLS_WANT_POLLIN:
172
2021-12-29
op
case TLS_WANT_POLLOUT:
173
2021-12-29
op
continue;
176
2021-12-29
op
len -= w;
177
2021-12-29
op
buf += w;
178
2021-12-29
op
tot += w;
181
2021-12-29
op
return tot;
184
2021-12-29
op
static int
185
2021-12-29
op
get(const char *r)
187
2021-12-29
op
struct tls *ctx;
188
2021-12-29
op
struct iri iri;
189
2021-12-29
op
int foundhdr = 0, code = -1, od;
190
2021-12-29
op
char iribuf[GEMINI_URL_LEN];
191
2021-12-29
op
char req[GEMINI_URL_LEN];
192
2021-12-29
op
char buf[2048];
193
2021-12-29
op
const char *parse_err, *host, *port;
195
2021-12-29
op
if (strlcpy(iribuf, r, sizeof(iribuf)) >= sizeof(iribuf))
196
2021-12-29
op
errx(1, "iri too long: %s", r);
198
2021-12-29
op
if (strlcpy(req, r, sizeof(req)) >= sizeof(req))
199
2021-12-29
op
errx(1, "iri too long: %s", r);
201
2021-12-29
op
if (strlcat(req, "\r\n", sizeof(req)) >= sizeof(req))
202
2021-12-29
op
errx(1, "iri too long: %s", r);
204
2021-12-29
op
if (!parse_iri(iribuf, &iri, &parse_err))
205
2021-12-29
op
errx(1, "invalid IRI: %s", parse_err);
208
2021-12-29
op
errx(0, "IRI OK");
210
2021-12-29
op
if ((ctx = tls_client()) == NULL)
211
2021-12-29
op
errx(1, "can't create tls context");
213
2021-12-29
op
if (tls_configure(ctx, tls_conf) == -1)
214
2021-12-29
op
errx(1, "tls_configure: %s", tls_error(ctx));
216
2021-12-29
op
host = iri.host;
217
2021-12-29
op
port = "1965";
218
2021-12-29
op
if (*iri.port != '\0')
219
2021-12-29
op
port = iri.port;
221
2021-12-29
op
connectto(ctx, host, port);
224
2021-12-29
op
while (!od) {
225
2021-12-29
op
switch (tls_handshake(ctx)) {
230
2021-12-29
op
errx(1, "handshake: %s", tls_error(ctx));
234
2021-12-29
op
if (verbose)
235
2021-12-29
op
printf("%s", req);
237
2021-12-29
op
doreq(ctx, req);
239
2021-12-29
op
for (;;) {
241
2021-12-29
op
size_t len;
243
2021-12-29
op
len = dorep(ctx, buf, sizeof(buf));
244
2021-12-29
op
if (len == 0)
245
2021-12-29
op
goto close;
247
2021-12-29
op
if (foundhdr) {
248
2021-12-29
op
write(1, buf, len);
249
2021-12-29
op
continue;
251
2021-12-29
op
foundhdr = 1;
253
2021-12-29
op
if (memmem(buf, len, "\r\n", 2) == NULL)
254
2021-12-29
op
errx(1, "invalid reply: no \\r\\n");
255
2021-12-29
op
if (!isdigit(buf[0]) || !isdigit(buf[1]) || buf[2] != ' ')
256
2021-12-29
op
errx(1, "invalid reply: invalid response format");
258
2021-12-29
op
code = (buf[0] - '0') * 10 + buf[1] - '0';
260
2021-12-29
op
if (debug == DEBUG_CODE) {
261
2021-12-29
op
printf("%d\n", code);
262
2021-12-29
op
goto close;
265
2021-12-29
op
if (debug == DEBUG_HEADER) {
266
2021-12-29
op
t = memmem(buf, len, "\r\n", 2);
267
2021-12-29
op
assert(t != NULL);
268
2021-12-29
op
*t = '\0';
269
2021-12-29
op
printf("%s\n", buf);
270
2021-12-29
op
goto close;
273
2021-12-29
op
if (debug == DEBUG_META) {
274
2021-12-29
op
t = memmem(buf, len, "\r\n", 2);
275
2021-12-29
op
assert(t != NULL);
276
2021-12-29
op
*t = '\0';
277
2021-12-29
op
printf("%s\n", buf+3);
278
2021-12-29
op
goto close;
281
2022-01-27
op
if (debug == DEBUG_ALL) {
282
2021-12-29
op
write(1, buf, len);
283
2021-12-29
op
continue;
286
2021-12-29
op
/* skip the header */
287
2021-12-29
op
t = memmem(buf, len, "\r\n", 2);
288
2021-12-29
op
assert(t != NULL);
289
2021-12-29
op
t += 2; /* skip \r\n */
290
2021-12-29
op
len -= t - buf;
291
2021-12-29
op
write(1, t, len);
295
2021-12-29
op
od = tls_close(ctx);
296
2021-12-29
op
if (od == TLS_WANT_POLLIN || od == TLS_WANT_POLLOUT)
297
2021-12-29
op
goto close;
299
2021-12-29
op
tls_close(ctx);
300
2021-12-29
op
tls_free(ctx);
301
2021-12-29
op
return code;
304
2021-12-29
op
static void __attribute__((noreturn))
305
2021-12-29
op
usage(void)
307
2021-12-29
op
fprintf(stderr, "usage: %s [-23Nnv] [-C cert] [-d mode] [-H sni] "
308
2022-01-13
op
"[-K key] [-P host[:port]]\n",
309
2021-12-29
op
getprogname());
310
2021-12-29
op
fprintf(stderr, " [-T seconds] gemini://...\n");
314
2021-12-29
op
static int
315
2021-12-29
op
parse_debug(const char *arg)
317
2021-12-29
op
if (!strcmp(arg, "none"))
318
2021-12-29
op
return DEBUG_NONE;
319
2021-12-29
op
if (!strcmp(arg, "code"))
320
2021-12-29
op
return DEBUG_CODE;
321
2021-12-29
op
if (!strcmp(arg, "header"))
322
2021-12-29
op
return DEBUG_HEADER;
323
2021-12-29
op
if (!strcmp(arg, "meta"))
324
2021-12-29
op
return DEBUG_META;
325
2022-01-27
op
if (!strcmp(arg, "all"))
326
2022-01-27
op
return DEBUG_ALL;
330
2021-12-29
op
static void
331
2021-12-29
op
parse_proxy(const char *arg)
333
2021-12-29
op
char *at;
335
2021-12-29
op
if ((proxy_host = strdup(arg)) == NULL)
336
2021-12-29
op
err(1, "strdup");
338
2021-12-29
op
proxy_port = "1965";
340
2021-12-29
op
if ((at = strchr(proxy_host, ':')) == NULL)
342
2021-12-29
op
*at = '\0';
343
2021-12-29
op
proxy_port = ++at;
345
2021-12-29
op
if (strchr(proxy_port, ':') != NULL)
346
2021-12-29
op
errx(1, "invalid port %s", proxy_port);
350
2021-12-29
op
main(int argc, char **argv)
352
2021-12-29
op
int ch, code;
353
2021-12-29
op
const char *errstr;
355
2021-12-29
op
while ((ch = getopt(argc, argv, "23C:d:H:K:NP:T:v")) != -1) {
356
2021-12-29
op
switch (ch) {
357
2021-12-29
op
case '2':
358
2021-12-29
op
flag2 = 1;
360
2021-12-29
op
case '3':
361
2021-12-29
op
flag3 = 1;
363
2021-12-29
op
case 'C':
364
2021-12-29
op
cert = optarg;
366
2021-12-29
op
case 'd':
367
2021-12-29
op
debug = parse_debug(optarg);
369
2021-12-29
op
case 'H':
370
2021-12-29
op
sni = optarg;
372
2021-12-29
op
case 'K':
373
2021-12-29
op
key = optarg;
375
2021-12-29
op
case 'N':
376
2021-12-29
op
dont_verify_name = 1;
378
2021-12-29
op
case 'n':
381
2021-12-29
op
case 'P':
382
2021-12-29
op
parse_proxy(optarg);
383
2021-12-29
op
dont_verify_name = 1;
385
2021-12-29
op
case 'T':
386
2021-12-29
op
timer = strtonum(optarg, 1, 1000, &errstr);
387
2021-12-29
op
if (errstr != NULL)
388
2021-12-29
op
errx(1, "timeout is %s: %s",
389
2021-12-29
op
errstr, optarg);
390
2021-12-29
op
signal(SIGALRM, timeout);
391
2021-12-29
op
alarm(timer);
393
2021-12-29
op
case 'v':
394
2021-12-29
op
verbose++;
400
2021-12-29
op
argc -= optind;
401
2021-12-29
op
argv += optind;
403
2021-12-29
op
if (flag2 + flag3 > 1) {
404
2021-12-29
op
warnx("only -2 or -3 can be specified at the same time");
408
2021-12-29
op
if ((cert != NULL && key == NULL) ||
409
2021-12-29
op
(cert == NULL && key != NULL)) {
410
2021-12-29
op
warnx("cert or key is missing");
414
2022-01-13
op
if (argc != 1)
417
2021-12-29
op
load_tls_conf();
419
2022-01-13
op
signal(SIGPIPE, SIG_IGN);
421
2021-12-29
op
#ifdef __OpenBSD__
422
2021-12-29
op
if (pledge("stdio inet dns", NULL) == -1)
423
2021-12-29
op
err(1, "pledge");
426
2021-12-29
op
code = get(*argv);
428
2021-12-29
op
return code < 20 || code >= 30;