Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "gmid.h"
19 #include <string.h>
21 int flag2, flag3, bflag, cflag, hflag, Nflag, Vflag, vflag;
22 const char *cert, *key;
24 int
25 main(int argc, char **argv)
26 {
27 struct iri iri;
28 struct tls_config *conf;
29 struct tls *ctx;
30 char iribuf[GEMINI_URL_LEN], buf[GEMINI_URL_LEN];
31 const char *parse_err = "unknown error", *port = "1965";
32 const char *hostname;
33 char *t;
34 int ch;
35 int handshake;
36 ssize_t len;
38 hostname = NULL;
39 while ((ch = getopt(argc, argv, "23C:cbH:hK:NVv")) != -1) {
40 switch (ch) {
41 case '2':
42 flag2 = 1;
43 break;
44 case '3':
45 flag3 = 1;
46 break;
47 case 'b':
48 bflag = 1;
49 break;
50 case 'C':
51 cert = optarg;
52 break;
53 case 'c':
54 cflag = 1;
55 break;
56 case 'H':
57 hostname = optarg;
58 break;
59 case 'h':
60 hflag = 1;
61 break;
62 case 'K':
63 key = optarg;
64 break;
65 case 'N':
66 Nflag = 1;
67 break;
68 case 'V':
69 Vflag = 1;
70 break;
71 case 'v':
72 vflag = 1;
73 break;
74 default:
75 fprintf(stderr, "USAGE: %s [-23cbhNVv] [-H hostname]\n",
76 *argv);
77 return 1;
78 }
79 }
80 argc -= optind;
81 argv += optind;
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");
92 if (argc != 1)
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);
105 if (Vflag)
106 errx(0, "IRI: OK");
108 if ((conf = tls_config_new()) == NULL)
109 errx(1, "tls_config_new");
111 tls_config_insecure_noverifycert(conf);
112 if (Nflag)
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')
130 port = iri.port;
132 if (hostname == NULL)
133 hostname = iri.host;
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)) {
140 case 0:
141 case -1:
142 handshake = 1;
143 break;
147 if (vflag)
148 printf("%s", buf);
149 if (tls_write(ctx, buf, strlen(buf)) == -1)
150 errx(1, "tls_write: %s", tls_error(ctx));
152 for (;;) {
153 switch (len = tls_read(ctx, buf, sizeof(buf))) {
154 case 0:
155 case -1:
156 goto end;
157 case TLS_WANT_POLLIN:
158 case TLS_WANT_POLLOUT:
159 continue;
162 if (bflag) {
163 bflag = 0;
164 if ((t = strchr(buf, '\r')) != NULL)
165 t += 2;
166 else if ((t = strchr(buf, '\n')) != NULL)
167 t += 1;
168 else
169 continue;
170 len -= t - buf;
171 write(1, t, len);
172 continue;
175 if (cflag) {
176 write(1, buf, 2);
177 write(1, "\n", 1);
178 break;
181 if (hflag) {
182 t = strchr(buf, '\r');
183 if (t == NULL)
184 t = strchr(buf, '\n');
185 if (t == NULL)
186 t = &buf[len];
187 write(1, buf, t - buf);
188 write(1, "\n", 1);
189 break;
192 write(1, buf, len);
194 end:
196 tls_close(ctx);
197 tls_free(ctx);
199 return 0;