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