Blame


1 497977d5 2021-01-23 op /*
2 497977d5 2021-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 497977d5 2021-01-23 op *
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.
7 497977d5 2021-01-23 op *
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.
15 497977d5 2021-01-23 op */
16 497977d5 2021-01-23 op
17 497977d5 2021-01-23 op #include <string.h>
18 497977d5 2021-01-23 op
19 497977d5 2021-01-23 op #include "gmid.h"
20 497977d5 2021-01-23 op
21 497977d5 2021-01-23 op int
22 497977d5 2021-01-23 op main(int argc, char **argv)
23 497977d5 2021-01-23 op {
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 9adde3d8 2021-01-23 op char iribuf[GEMINI_URL_LEN], buf[GEMINI_URL_LEN];
28 497977d5 2021-01-23 op const char *parse_err = "unknown error", *port = "1965";
29 497977d5 2021-01-23 op char *t;
30 497977d5 2021-01-23 op int ch, flag2, flag3, bflag, cflag, hflag, Nflag, Vflag;
31 497977d5 2021-01-23 op ssize_t len;
32 497977d5 2021-01-23 op
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) {
35 497977d5 2021-01-23 op switch (ch) {
36 497977d5 2021-01-23 op case '2':
37 497977d5 2021-01-23 op flag2 = 1;
38 497977d5 2021-01-23 op break;
39 497977d5 2021-01-23 op case '3':
40 497977d5 2021-01-23 op flag3 = 1;
41 497977d5 2021-01-23 op break;
42 497977d5 2021-01-23 op case 'b':
43 497977d5 2021-01-23 op bflag = 1;
44 497977d5 2021-01-23 op break;
45 497977d5 2021-01-23 op case 'c':
46 497977d5 2021-01-23 op cflag = 1;
47 497977d5 2021-01-23 op break;
48 497977d5 2021-01-23 op case 'h':
49 497977d5 2021-01-23 op hflag = 1;
50 497977d5 2021-01-23 op break;
51 497977d5 2021-01-23 op case 'N':
52 497977d5 2021-01-23 op Nflag = 1;
53 497977d5 2021-01-23 op break;
54 497977d5 2021-01-23 op case 'V':
55 497977d5 2021-01-23 op Vflag = 1;
56 497977d5 2021-01-23 op break;
57 497977d5 2021-01-23 op default:
58 497977d5 2021-01-23 op fprintf(stderr, "USAGE: %s [-23cbhNV]", *argv);
59 497977d5 2021-01-23 op return 1;
60 497977d5 2021-01-23 op }
61 497977d5 2021-01-23 op }
62 497977d5 2021-01-23 op argc -= optind;
63 497977d5 2021-01-23 op argv += optind;
64 497977d5 2021-01-23 op
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.");
67 497977d5 2021-01-23 op
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.");
70 497977d5 2021-01-23 op
71 497977d5 2021-01-23 op if (argc != 1)
72 497977d5 2021-01-23 op errx(1, "missing IRI");
73 497977d5 2021-01-23 op
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 9adde3d8 2021-01-23 op if (strlcpy(buf, argv[0], sizeof(buf)) >= sizeof(iribuf))
77 497977d5 2021-01-23 op errx(1, "request too long: %s", argv[0]);
78 9adde3d8 2021-01-23 op if (strlcat(buf, "\r\n", sizeof(buf)) >= sizeof(buf))
79 497977d5 2021-01-23 op errx(1, "request too long: %s", argv[0]);
80 497977d5 2021-01-23 op
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);
83 497977d5 2021-01-23 op
84 497977d5 2021-01-23 op if (Vflag)
85 497977d5 2021-01-23 op errx(0, "IRI: OK");
86 497977d5 2021-01-23 op
87 497977d5 2021-01-23 op if ((conf = tls_config_new()) == NULL)
88 497977d5 2021-01-23 op errx(1, "tls_config_new");
89 497977d5 2021-01-23 op
90 497977d5 2021-01-23 op tls_config_insecure_noverifycert(conf);
91 497977d5 2021-01-23 op if (Nflag)
92 497977d5 2021-01-23 op tls_config_insecure_noverifyname(conf);
93 497977d5 2021-01-23 op
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");
98 497977d5 2021-01-23 op
99 497977d5 2021-01-23 op if ((ctx = tls_client()) == NULL)
100 497977d5 2021-01-23 op errx(1, "tls_client creation failed");
101 497977d5 2021-01-23 op
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));
104 497977d5 2021-01-23 op
105 d760973a 2021-01-23 op if (*iri.port != '\0')
106 d760973a 2021-01-23 op port = iri.port;
107 d760973a 2021-01-23 op if (tls_connect(ctx, iri.host, port) == -1)
108 497977d5 2021-01-23 op errx(1, "tls_connect: %s", tls_error(ctx));
109 497977d5 2021-01-23 op
110 9adde3d8 2021-01-23 op tls_write(ctx, buf, strlen(buf));
111 9adde3d8 2021-01-23 op /* if (tls_write(ctx, buf, strlen(buf)) != -1) */
112 497977d5 2021-01-23 op /* errx(1, "tls_write: %s", tls_error(ctx)); */
113 497977d5 2021-01-23 op
114 497977d5 2021-01-23 op for (;;) {
115 f62aab51 2021-01-23 op switch (len = tls_read(ctx, buf, sizeof(buf))) {
116 f62aab51 2021-01-23 op case 0:
117 f62aab51 2021-01-23 op case -1:
118 f62aab51 2021-01-23 op goto end;
119 f62aab51 2021-01-23 op case TLS_WANT_POLLIN:
120 f62aab51 2021-01-23 op case TLS_WANT_POLLOUT:
121 f62aab51 2021-01-23 op continue;
122 f62aab51 2021-01-23 op }
123 497977d5 2021-01-23 op
124 497977d5 2021-01-23 op if (bflag) {
125 497977d5 2021-01-23 op bflag = 0;
126 497977d5 2021-01-23 op if ((t = strchr(buf, '\r')) != NULL)
127 497977d5 2021-01-23 op t += 2;
128 497977d5 2021-01-23 op else if ((t = strchr(buf, '\n')) != NULL)
129 497977d5 2021-01-23 op t += 1;
130 497977d5 2021-01-23 op else
131 497977d5 2021-01-23 op continue;
132 497977d5 2021-01-23 op len -= t - buf;
133 497977d5 2021-01-23 op write(1, t, len);
134 497977d5 2021-01-23 op continue;
135 497977d5 2021-01-23 op }
136 497977d5 2021-01-23 op
137 497977d5 2021-01-23 op if (cflag) {
138 497977d5 2021-01-23 op write(1, buf, 2);
139 497977d5 2021-01-23 op write(1, "\n", 1);
140 497977d5 2021-01-23 op break;
141 497977d5 2021-01-23 op }
142 497977d5 2021-01-23 op
143 497977d5 2021-01-23 op if (hflag) {
144 497977d5 2021-01-23 op t = strchr(buf, '\r');
145 497977d5 2021-01-23 op if (t == NULL)
146 497977d5 2021-01-23 op t = strchr(buf, '\n');
147 497977d5 2021-01-23 op if (t == NULL)
148 497977d5 2021-01-23 op t = &buf[len];
149 497977d5 2021-01-23 op write(1, buf, t - buf);
150 497977d5 2021-01-23 op write(1, "\n", 1);
151 497977d5 2021-01-23 op break;
152 497977d5 2021-01-23 op }
153 497977d5 2021-01-23 op
154 497977d5 2021-01-23 op write(1, buf, len);
155 497977d5 2021-01-23 op }
156 f62aab51 2021-01-23 op end:
157 497977d5 2021-01-23 op
158 497977d5 2021-01-23 op tls_close(ctx);
159 497977d5 2021-01-23 op tls_free(ctx);
160 497977d5 2021-01-23 op
161 497977d5 2021-01-23 op return 0;
162 497977d5 2021-01-23 op }