2 f48e3b85 2021-05-24 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 f48e3b85 2021-05-24 op * Permission to use, copy, modify, and distribute this software for any
5 f48e3b85 2021-05-24 op * purpose with or without fee is hereby granted, provided that the above
6 f48e3b85 2021-05-24 op * copyright notice and this permission notice appear in all copies.
8 f48e3b85 2021-05-24 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 f48e3b85 2021-05-24 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 f48e3b85 2021-05-24 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 f48e3b85 2021-05-24 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 f48e3b85 2021-05-24 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 f48e3b85 2021-05-24 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 f48e3b85 2021-05-24 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 f48e3b85 2021-05-24 op #include "../gmid.h"
19 f48e3b85 2021-05-24 op #include <string.h>
21 f48e3b85 2021-05-24 op int flag2, flag3, bflag, cflag, hflag, Nflag, Vflag, vflag;
22 f48e3b85 2021-05-24 op const char *cert, *key;
25 f48e3b85 2021-05-24 op main(int argc, char **argv)
27 f48e3b85 2021-05-24 op struct iri iri;
28 f48e3b85 2021-05-24 op struct tls_config *conf;
29 f48e3b85 2021-05-24 op struct tls *ctx;
30 f48e3b85 2021-05-24 op char iribuf[GEMINI_URL_LEN], buf[GEMINI_URL_LEN];
31 f48e3b85 2021-05-24 op const char *parse_err = "unknown error", *port = "1965";
32 f48e3b85 2021-05-24 op const char *hostname;
35 f48e3b85 2021-05-24 op int handshake;
38 f48e3b85 2021-05-24 op hostname = NULL;
39 f48e3b85 2021-05-24 op while ((ch = getopt(argc, argv, "23C:cbH:hK:NVv")) != -1) {
51 f48e3b85 2021-05-24 op cert = optarg;
57 f48e3b85 2021-05-24 op hostname = optarg;
75 f48e3b85 2021-05-24 op fprintf(stderr, "USAGE: %s [-23cbhNVv] [-H hostname]\n",
80 f48e3b85 2021-05-24 op argc -= optind;
81 f48e3b85 2021-05-24 op argv += optind;
83 f48e3b85 2021-05-24 op if ((bflag + cflag + hflag + Vflag) > 1)
84 f48e3b85 2021-05-24 op errx(1, "only one of bchr flags can be used.");
86 f48e3b85 2021-05-24 op if (flag2 + flag3 > 1)
87 f48e3b85 2021-05-24 op errx(1, "only -2 or -3 can be specified at the same time.");
89 f48e3b85 2021-05-24 op if ((cert != NULL && key == NULL) || (cert == NULL && key != NULL))
90 f48e3b85 2021-05-24 op errx(1, "missing certificate or key");
92 f48e3b85 2021-05-24 op if (argc != 1)
93 f48e3b85 2021-05-24 op errx(1, "missing IRI");
95 f48e3b85 2021-05-24 op if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
96 f48e3b85 2021-05-24 op errx(1, "request too long: %s", argv[0]);
97 f48e3b85 2021-05-24 op if (strlcpy(buf, argv[0], sizeof(buf)) >= sizeof(iribuf))
98 f48e3b85 2021-05-24 op errx(1, "request too long: %s", argv[0]);
99 f48e3b85 2021-05-24 op if (strlcat(buf, "\r\n", sizeof(buf)) >= sizeof(buf))
100 f48e3b85 2021-05-24 op errx(1, "request too long: %s", argv[0]);
102 f48e3b85 2021-05-24 op if (!parse_iri(iribuf, &iri, &parse_err))
103 f48e3b85 2021-05-24 op errx(1, "invalid IRI: %s", parse_err);
106 f48e3b85 2021-05-24 op errx(0, "IRI: OK");
108 f48e3b85 2021-05-24 op if ((conf = tls_config_new()) == NULL)
109 f48e3b85 2021-05-24 op errx(1, "tls_config_new");
111 f48e3b85 2021-05-24 op tls_config_insecure_noverifycert(conf);
113 f48e3b85 2021-05-24 op tls_config_insecure_noverifyname(conf);
115 f48e3b85 2021-05-24 op if (flag2 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_2) == -1)
116 f48e3b85 2021-05-24 op errx(1, "cannot set TLSv1.2");
117 f48e3b85 2021-05-24 op if (flag3 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_3) == -1)
118 f48e3b85 2021-05-24 op errx(1, "cannot set TLSv1.3");
120 f48e3b85 2021-05-24 op if (cert != NULL && tls_config_set_keypair_file(conf, cert, key))
121 f48e3b85 2021-05-24 op errx(1, "couldn't load cert: %s", cert);
123 f48e3b85 2021-05-24 op if ((ctx = tls_client()) == NULL)
124 f48e3b85 2021-05-24 op errx(1, "tls_client creation failed");
126 f48e3b85 2021-05-24 op if (tls_configure(ctx, conf) == -1)
127 f48e3b85 2021-05-24 op errx(1, "tls_configure: %s", tls_error(ctx));
129 f48e3b85 2021-05-24 op if (*iri.port != '\0')
130 f48e3b85 2021-05-24 op port = iri.port;
132 f48e3b85 2021-05-24 op if (hostname == NULL)
133 f48e3b85 2021-05-24 op hostname = iri.host;
135 f48e3b85 2021-05-24 op if (tls_connect_servername(ctx, iri.host, port, hostname) == -1)
136 f48e3b85 2021-05-24 op errx(1, "tls_connect: %s", tls_error(ctx));
138 f48e3b85 2021-05-24 op for (handshake = 0; !handshake;) {
139 f48e3b85 2021-05-24 op switch (tls_handshake(ctx)) {
142 f48e3b85 2021-05-24 op handshake = 1;
148 f48e3b85 2021-05-24 op printf("%s", buf);
149 f48e3b85 2021-05-24 op if (tls_write(ctx, buf, strlen(buf)) == -1)
150 f48e3b85 2021-05-24 op errx(1, "tls_write: %s", tls_error(ctx));
153 f48e3b85 2021-05-24 op switch (len = tls_read(ctx, buf, sizeof(buf))) {
157 f48e3b85 2021-05-24 op case TLS_WANT_POLLIN:
158 f48e3b85 2021-05-24 op case TLS_WANT_POLLOUT:
164 f48e3b85 2021-05-24 op if ((t = strchr(buf, '\r')) != NULL)
166 f48e3b85 2021-05-24 op else if ((t = strchr(buf, '\n')) != NULL)
170 f48e3b85 2021-05-24 op len -= t - buf;
171 f48e3b85 2021-05-24 op write(1, t, len);
176 f48e3b85 2021-05-24 op write(1, buf, 2);
177 f48e3b85 2021-05-24 op write(1, "\n", 1);
182 f48e3b85 2021-05-24 op t = strchr(buf, '\r');
183 f48e3b85 2021-05-24 op if (t == NULL)
184 f48e3b85 2021-05-24 op t = strchr(buf, '\n');
185 f48e3b85 2021-05-24 op if (t == NULL)
186 f48e3b85 2021-05-24 op t = &buf[len];
187 f48e3b85 2021-05-24 op write(1, buf, t - buf);
188 f48e3b85 2021-05-24 op write(1, "\n", 1);
192 f48e3b85 2021-05-24 op write(1, buf, len);
196 f48e3b85 2021-05-24 op tls_close(ctx);
197 f48e3b85 2021-05-24 op tls_free(ctx);