2 497977d5 2021-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 497977d5 2021-01-23 op * Permission to use, copy, modify, and distribute this software for any
5 497977d5 2021-01-23 op * purpose with or without fee is hereby granted, provided that the above
6 497977d5 2021-01-23 op * copyright notice and this permission notice appear in all copies.
8 497977d5 2021-01-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 497977d5 2021-01-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 497977d5 2021-01-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 497977d5 2021-01-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 497977d5 2021-01-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 497977d5 2021-01-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 497977d5 2021-01-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 497977d5 2021-01-23 op #include <string.h>
19 497977d5 2021-01-23 op #include "gmid.h"
22 497977d5 2021-01-23 op main(int argc, char **argv)
24 497977d5 2021-01-23 op struct iri iri;
25 497977d5 2021-01-23 op struct tls_config *conf;
26 497977d5 2021-01-23 op struct tls *ctx;
27 497977d5 2021-01-23 op char iribuf[GEMINI_URL_LEN], req[GEMINI_URL_LEN], buf[1024];
28 497977d5 2021-01-23 op const char *parse_err = "unknown error", *port = "1965";
30 497977d5 2021-01-23 op int ch, flag2, flag3, bflag, cflag, hflag, Nflag, Vflag;
33 497977d5 2021-01-23 op flag2 = flag3 = bflag = cflag = hflag = Nflag = Vflag = 0;
34 497977d5 2021-01-23 op while ((ch = getopt(argc, argv, "23cbhNV")) != -1) {
58 497977d5 2021-01-23 op fprintf(stderr, "USAGE: %s [-23cbhNV]", *argv);
62 497977d5 2021-01-23 op argc -= optind;
63 497977d5 2021-01-23 op argv += optind;
65 497977d5 2021-01-23 op if ((bflag + cflag + hflag + Vflag) > 1)
66 497977d5 2021-01-23 op errx(1, "only one of bchr flags can be used.");
68 497977d5 2021-01-23 op if (flag2 + flag3 > 1)
69 497977d5 2021-01-23 op errx(1, "only -2 or -3 can be specified at the same time.");
71 497977d5 2021-01-23 op if (argc != 1)
72 497977d5 2021-01-23 op errx(1, "missing IRI");
74 497977d5 2021-01-23 op if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
75 497977d5 2021-01-23 op errx(1, "request too long: %s", argv[0]);
76 497977d5 2021-01-23 op if (strlcpy(req, argv[0], sizeof(req)) >= sizeof(iribuf))
77 497977d5 2021-01-23 op errx(1, "request too long: %s", argv[0]);
78 497977d5 2021-01-23 op if (strlcat(req, "\r\n", sizeof(req)) >= sizeof(req))
79 497977d5 2021-01-23 op errx(1, "request too long: %s", argv[0]);
81 497977d5 2021-01-23 op if (!parse_iri(iribuf, &iri, &parse_err))
82 497977d5 2021-01-23 op errx(1, "invalid IRI: %s", parse_err);
85 497977d5 2021-01-23 op errx(0, "IRI: OK");
87 497977d5 2021-01-23 op if ((conf = tls_config_new()) == NULL)
88 497977d5 2021-01-23 op errx(1, "tls_config_new");
90 497977d5 2021-01-23 op tls_config_insecure_noverifycert(conf);
92 497977d5 2021-01-23 op tls_config_insecure_noverifyname(conf);
94 497977d5 2021-01-23 op if (flag2 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_2) == -1)
95 497977d5 2021-01-23 op errx(1, "cannot set TLSv1.2");
96 497977d5 2021-01-23 op if (flag3 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_3) == -1)
97 497977d5 2021-01-23 op errx(1, "cannot set TLSv1.3");
99 497977d5 2021-01-23 op if ((ctx = tls_client()) == NULL)
100 497977d5 2021-01-23 op errx(1, "tls_client creation failed");
102 497977d5 2021-01-23 op if (tls_configure(ctx, conf) == -1)
103 497977d5 2021-01-23 op errx(1, "tls_configure: %s", tls_error(ctx));
105 497977d5 2021-01-23 op if (*iri.port == '\0')
106 497977d5 2021-01-23 op iri.port = (char*)port;
107 497977d5 2021-01-23 op if (tls_connect(ctx, iri.host, iri.port) == -1)
108 497977d5 2021-01-23 op errx(1, "tls_connect: %s", tls_error(ctx));
110 497977d5 2021-01-23 op tls_write(ctx, req, strlen(req));
111 497977d5 2021-01-23 op /* if (tls_write(ctx, req, strlen(req)) != -1) */
112 497977d5 2021-01-23 op /* errx(1, "tls_write: %s", tls_error(ctx)); */
115 497977d5 2021-01-23 op len = tls_read(ctx, buf, sizeof(buf));
116 497977d5 2021-01-23 op if (len == 0 || len == -1)
121 497977d5 2021-01-23 op if ((t = strchr(buf, '\r')) != NULL)
123 497977d5 2021-01-23 op else if ((t = strchr(buf, '\n')) != NULL)
127 497977d5 2021-01-23 op len -= t - buf;
128 497977d5 2021-01-23 op write(1, t, len);
133 497977d5 2021-01-23 op write(1, buf, 2);
134 497977d5 2021-01-23 op write(1, "\n", 1);
139 497977d5 2021-01-23 op t = strchr(buf, '\r');
140 497977d5 2021-01-23 op if (t == NULL)
141 497977d5 2021-01-23 op t = strchr(buf, '\n');
142 497977d5 2021-01-23 op if (t == NULL)
143 497977d5 2021-01-23 op t = &buf[len];
144 497977d5 2021-01-23 op write(1, buf, t - buf);
145 497977d5 2021-01-23 op write(1, "\n", 1);
149 497977d5 2021-01-23 op write(1, buf, len);
152 497977d5 2021-01-23 op tls_close(ctx);
153 497977d5 2021-01-23 op tls_free(ctx);