2 5c7abf01 2021-12-29 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5c7abf01 2021-12-29 op * Permission to use, copy, modify, and distribute this software for any
5 5c7abf01 2021-12-29 op * purpose with or without fee is hereby granted, provided that the above
6 5c7abf01 2021-12-29 op * copyright notice and this permission notice appear in all copies.
8 5c7abf01 2021-12-29 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5c7abf01 2021-12-29 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5c7abf01 2021-12-29 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5c7abf01 2021-12-29 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5c7abf01 2021-12-29 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5c7abf01 2021-12-29 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5c7abf01 2021-12-29 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c7abf01 2021-12-29 op #include "gmid.h"
19 5c7abf01 2021-12-29 op #include <sys/socket.h>
21 5c7abf01 2021-12-29 op #include <assert.h>
22 5c7abf01 2021-12-29 op #include <ctype.h>
23 5c7abf01 2021-12-29 op #include <errno.h>
24 0046c1fe 2023-06-06 op #include <locale.h>
25 5c7abf01 2021-12-29 op #include <string.h>
37 5c7abf01 2021-12-29 op int dont_verify_name;
41 5c7abf01 2021-12-29 op int redirects = 5;
43 5c7abf01 2021-12-29 op const char *cert;
44 5c7abf01 2021-12-29 op const char *key;
45 5c7abf01 2021-12-29 op const char *proxy_host;
46 5c7abf01 2021-12-29 op const char *proxy_port;
47 5c7abf01 2021-12-29 op const char *sni;
50 5c7abf01 2021-12-29 op struct tls_config *tls_conf;
53 5c7abf01 2021-12-29 op timeout(int signo)
55 5c7abf01 2021-12-29 op dprintf(2, "%s: timer expired\n", getprogname());
60 5c7abf01 2021-12-29 op load_tls_conf(void)
62 5c7abf01 2021-12-29 op if ((tls_conf = tls_config_new()) == NULL)
63 5c7abf01 2021-12-29 op err(1, "tls_config_new");
65 5c7abf01 2021-12-29 op tls_config_insecure_noverifycert(tls_conf);
66 5c7abf01 2021-12-29 op if (dont_verify_name)
67 5c7abf01 2021-12-29 op tls_config_insecure_noverifyname(tls_conf);
70 5c7abf01 2021-12-29 op tls_config_set_protocols(tls_conf, TLS_PROTOCOL_TLSv1_2) == -1)
71 5c7abf01 2021-12-29 op errx(1, "can't set TLSv1.2");
73 5c7abf01 2021-12-29 op tls_config_set_protocols(tls_conf, TLS_PROTOCOL_TLSv1_3) == -1)
74 5c7abf01 2021-12-29 op errx(1, "can't set TLSv1.3");
76 5c7abf01 2021-12-29 op if (cert != NULL &&
77 5c7abf01 2021-12-29 op tls_config_set_keypair_file(tls_conf, cert, key) == -1)
78 5c7abf01 2021-12-29 op errx(1, "can't load client certificate %s", cert);
82 5c7abf01 2021-12-29 op connectto(struct tls *ctx, const char *host, const char *port)
84 5c7abf01 2021-12-29 op struct addrinfo hints, *res, *res0;
86 5c7abf01 2021-12-29 op int saved_errno;
88 5c7abf01 2021-12-29 op const char *cause = NULL;
89 5c7abf01 2021-12-29 op const char *sname;
91 5c7abf01 2021-12-29 op if (proxy_host != NULL) {
92 5c7abf01 2021-12-29 op host = proxy_host;
93 5c7abf01 2021-12-29 op port = proxy_port;
96 5c7abf01 2021-12-29 op if ((sname = sni) == NULL)
99 5c7abf01 2021-12-29 op memset(&hints, 0, sizeof(hints));
100 5c7abf01 2021-12-29 op hints.ai_family = AF_UNSPEC;
101 5c7abf01 2021-12-29 op hints.ai_socktype = SOCK_STREAM;
102 5c7abf01 2021-12-29 op error = getaddrinfo(host, port, &hints, &res0);
104 5c7abf01 2021-12-29 op errx(1, "%s", gai_strerror(error));
107 5c7abf01 2021-12-29 op for (res = res0; res != NULL; res = res->ai_next) {
108 5c7abf01 2021-12-29 op s = socket(res->ai_family, res->ai_socktype,
109 5c7abf01 2021-12-29 op res->ai_protocol);
110 5c7abf01 2021-12-29 op if (s == -1) {
111 5c7abf01 2021-12-29 op cause = "socket";
115 5c7abf01 2021-12-29 op if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
116 5c7abf01 2021-12-29 op cause = "connect";
117 5c7abf01 2021-12-29 op saved_errno = errno;
119 5c7abf01 2021-12-29 op errno = saved_errno;
128 5c7abf01 2021-12-29 op err(1, "%s: can't connect to %s:%s", cause,
131 5c7abf01 2021-12-29 op freeaddrinfo(res0);
133 5c7abf01 2021-12-29 op if (tls_connect_socket(ctx, s, sname) == -1)
134 5c7abf01 2021-12-29 op errx(1, "tls_connect_socket: %s", tls_error(ctx));
138 5c7abf01 2021-12-29 op doreq(struct tls *ctx, const char *buf)
143 5c7abf01 2021-12-29 op s = strlen(buf);
144 5c7abf01 2021-12-29 op while (s != 0) {
145 5c7abf01 2021-12-29 op switch (w = tls_write(ctx, buf, s)) {
148 5c7abf01 2021-12-29 op errx(1, "tls_write: %s", tls_error(ctx));
149 5c7abf01 2021-12-29 op case TLS_WANT_POLLIN:
150 5c7abf01 2021-12-29 op case TLS_WANT_POLLOUT:
159 5c7abf01 2021-12-29 op static size_t
160 10cc8193 2023-06-13 op dorep(struct tls *ctx, uint8_t *buf, size_t len)
163 5c7abf01 2021-12-29 op size_t tot = 0;
165 5c7abf01 2021-12-29 op while (len != 0) {
166 5c7abf01 2021-12-29 op switch (w = tls_read(ctx, buf, len)) {
170 5c7abf01 2021-12-29 op errx(1, "tls_write: %s", tls_error(ctx));
171 5c7abf01 2021-12-29 op case TLS_WANT_POLLIN:
172 5c7abf01 2021-12-29 op case TLS_WANT_POLLOUT:
185 5c7abf01 2021-12-29 op get(const char *r)
187 5c7abf01 2021-12-29 op struct tls *ctx;
188 5c7abf01 2021-12-29 op struct iri iri;
189 5c7abf01 2021-12-29 op int foundhdr = 0, code = -1, od;
190 5c7abf01 2021-12-29 op char iribuf[GEMINI_URL_LEN];
191 5c7abf01 2021-12-29 op char req[GEMINI_URL_LEN];
192 10cc8193 2023-06-13 op uint8_t buf[2048];
193 5c7abf01 2021-12-29 op const char *parse_err, *host, *port;
195 5c7abf01 2021-12-29 op if (strlcpy(iribuf, r, sizeof(iribuf)) >= sizeof(iribuf))
196 5c7abf01 2021-12-29 op errx(1, "iri too long: %s", r);
198 5c7abf01 2021-12-29 op if (strlcpy(req, r, sizeof(req)) >= sizeof(req))
199 5c7abf01 2021-12-29 op errx(1, "iri too long: %s", r);
201 5c7abf01 2021-12-29 op if (strlcat(req, "\r\n", sizeof(req)) >= sizeof(req))
202 5c7abf01 2021-12-29 op errx(1, "iri too long: %s", r);
204 5c7abf01 2021-12-29 op if (!parse_iri(iribuf, &iri, &parse_err))
205 5c7abf01 2021-12-29 op errx(1, "invalid IRI: %s", parse_err);
208 5c7abf01 2021-12-29 op errx(0, "IRI OK");
210 5c7abf01 2021-12-29 op if ((ctx = tls_client()) == NULL)
211 5c7abf01 2021-12-29 op errx(1, "can't create tls context");
213 5c7abf01 2021-12-29 op if (tls_configure(ctx, tls_conf) == -1)
214 5c7abf01 2021-12-29 op errx(1, "tls_configure: %s", tls_error(ctx));
216 5c7abf01 2021-12-29 op host = iri.host;
217 5c7abf01 2021-12-29 op port = "1965";
218 5c7abf01 2021-12-29 op if (*iri.port != '\0')
219 5c7abf01 2021-12-29 op port = iri.port;
221 5c7abf01 2021-12-29 op connectto(ctx, host, port);
224 5c7abf01 2021-12-29 op while (!od) {
225 5c7abf01 2021-12-29 op switch (tls_handshake(ctx)) {
230 5c7abf01 2021-12-29 op errx(1, "handshake: %s", tls_error(ctx));
234 5c7abf01 2021-12-29 op doreq(ctx, req);
240 5c7abf01 2021-12-29 op len = dorep(ctx, buf, sizeof(buf));
241 5c7abf01 2021-12-29 op if (len == 0)
244 5c7abf01 2021-12-29 op if (foundhdr) {
245 5c7abf01 2021-12-29 op write(1, buf, len);
248 5c7abf01 2021-12-29 op foundhdr = 1;
250 5c7abf01 2021-12-29 op if (memmem(buf, len, "\r\n", 2) == NULL)
251 5c7abf01 2021-12-29 op errx(1, "invalid reply: no \\r\\n");
252 6130e0ee 2022-11-17 op if (!isdigit((unsigned char)buf[0]) ||
253 6130e0ee 2022-11-17 op !isdigit((unsigned char)buf[1]) ||
254 6130e0ee 2022-11-17 op buf[2] != ' ')
255 5c7abf01 2021-12-29 op errx(1, "invalid reply: invalid response format");
257 5c7abf01 2021-12-29 op code = (buf[0] - '0') * 10 + buf[1] - '0';
259 5c7abf01 2021-12-29 op if (debug == DEBUG_CODE) {
260 5c7abf01 2021-12-29 op printf("%d\n", code);
264 5c7abf01 2021-12-29 op if (debug == DEBUG_HEADER) {
265 5c7abf01 2021-12-29 op t = memmem(buf, len, "\r\n", 2);
266 5c7abf01 2021-12-29 op assert(t != NULL);
268 5c7abf01 2021-12-29 op printf("%s\n", buf);
272 5c7abf01 2021-12-29 op if (debug == DEBUG_META) {
273 5c7abf01 2021-12-29 op t = memmem(buf, len, "\r\n", 2);
274 5c7abf01 2021-12-29 op assert(t != NULL);
276 5c7abf01 2021-12-29 op printf("%s\n", buf+3);
280 e89f4739 2022-01-27 op if (debug == DEBUG_ALL) {
281 5c7abf01 2021-12-29 op write(1, buf, len);
285 5c7abf01 2021-12-29 op /* skip the header */
286 5c7abf01 2021-12-29 op t = memmem(buf, len, "\r\n", 2);
287 5c7abf01 2021-12-29 op assert(t != NULL);
288 5c7abf01 2021-12-29 op t += 2; /* skip \r\n */
289 5c7abf01 2021-12-29 op len -= t - buf;
290 5c7abf01 2021-12-29 op write(1, t, len);
294 800aa93c 2023-07-22 op switch (tls_close(ctx)) {
295 800aa93c 2023-07-22 op case TLS_WANT_POLLIN:
296 800aa93c 2023-07-22 op case TLS_WANT_POLLOUT:
299 800aa93c 2023-07-22 op warnx("tls_close: %s", tls_error(ctx));
300 800aa93c 2023-07-22 op /* fallthrough */
302 800aa93c 2023-07-22 op tls_free(ctx);
308 88c03474 2023-07-23 op static void __attribute__((noreturn))
311 c5b4db93 2022-09-10 op fprintf(stderr, "version: " GG_STRING "\n");
312 21cf735f 2022-10-30 op fprintf(stderr, "usage: %s [-23Nn] [-C cert] [-d mode] [-H sni] "
313 febfcde8 2022-01-13 op "[-K key] [-P host[:port]]\n",
314 5c7abf01 2021-12-29 op getprogname());
315 5c7abf01 2021-12-29 op fprintf(stderr, " [-T seconds] gemini://...\n");
320 5c7abf01 2021-12-29 op parse_debug(const char *arg)
322 5c7abf01 2021-12-29 op if (!strcmp(arg, "none"))
323 5c7abf01 2021-12-29 op return DEBUG_NONE;
324 5c7abf01 2021-12-29 op if (!strcmp(arg, "code"))
325 5c7abf01 2021-12-29 op return DEBUG_CODE;
326 5c7abf01 2021-12-29 op if (!strcmp(arg, "header"))
327 5c7abf01 2021-12-29 op return DEBUG_HEADER;
328 5c7abf01 2021-12-29 op if (!strcmp(arg, "meta"))
329 5c7abf01 2021-12-29 op return DEBUG_META;
330 e89f4739 2022-01-27 op if (!strcmp(arg, "all"))
331 e89f4739 2022-01-27 op return DEBUG_ALL;
336 5c7abf01 2021-12-29 op parse_proxy(const char *arg)
340 5c7abf01 2021-12-29 op if ((proxy_host = strdup(arg)) == NULL)
341 5c7abf01 2021-12-29 op err(1, "strdup");
343 5c7abf01 2021-12-29 op proxy_port = "1965";
345 5c7abf01 2021-12-29 op if ((at = strchr(proxy_host, ':')) == NULL)
348 5c7abf01 2021-12-29 op proxy_port = ++at;
350 5c7abf01 2021-12-29 op if (strchr(proxy_port, ':') != NULL)
351 5c7abf01 2021-12-29 op errx(1, "invalid port %s", proxy_port);
355 5c7abf01 2021-12-29 op main(int argc, char **argv)
357 5c7abf01 2021-12-29 op int ch, code;
358 5c7abf01 2021-12-29 op const char *errstr;
360 0046c1fe 2023-06-06 op setlocale(LC_CTYPE, "");
362 21cf735f 2022-10-30 op while ((ch = getopt(argc, argv, "23C:d:H:K:NP:T:")) != -1) {
363 5c7abf01 2021-12-29 op switch (ch) {
371 5c7abf01 2021-12-29 op cert = optarg;
374 5c7abf01 2021-12-29 op debug = parse_debug(optarg);
377 5c7abf01 2021-12-29 op sni = optarg;
380 5c7abf01 2021-12-29 op key = optarg;
383 5c7abf01 2021-12-29 op dont_verify_name = 1;
389 5c7abf01 2021-12-29 op parse_proxy(optarg);
390 5c7abf01 2021-12-29 op dont_verify_name = 1;
393 5c7abf01 2021-12-29 op timer = strtonum(optarg, 1, 1000, &errstr);
394 5c7abf01 2021-12-29 op if (errstr != NULL)
395 5c7abf01 2021-12-29 op errx(1, "timeout is %s: %s",
396 5c7abf01 2021-12-29 op errstr, optarg);
397 5c7abf01 2021-12-29 op signal(SIGALRM, timeout);
398 5c7abf01 2021-12-29 op alarm(timer);
404 5c7abf01 2021-12-29 op argc -= optind;
405 5c7abf01 2021-12-29 op argv += optind;
407 5c7abf01 2021-12-29 op if (flag2 + flag3 > 1) {
408 5c7abf01 2021-12-29 op warnx("only -2 or -3 can be specified at the same time");
412 5c7abf01 2021-12-29 op if ((cert != NULL && key == NULL) ||
413 5c7abf01 2021-12-29 op (cert == NULL && key != NULL)) {
414 5c7abf01 2021-12-29 op warnx("cert or key is missing");
418 b3602923 2022-01-13 op if (argc != 1)
421 5c7abf01 2021-12-29 op load_tls_conf();
423 b3602923 2022-01-13 op signal(SIGPIPE, SIG_IGN);
425 5c7abf01 2021-12-29 op #ifdef __OpenBSD__
426 5c7abf01 2021-12-29 op if (pledge("stdio inet dns", NULL) == -1)
427 5c7abf01 2021-12-29 op err(1, "pledge");
430 5c7abf01 2021-12-29 op code = get(*argv);
432 5c7abf01 2021-12-29 op return code < 20 || code >= 30;