Blame


1 2ff1e2a9 2023-07-22 op /*
2 2ff1e2a9 2023-07-22 op * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
3 2ff1e2a9 2023-07-22 op *
4 2ff1e2a9 2023-07-22 op * Permission to use, copy, modify, and distribute this software for any
5 2ff1e2a9 2023-07-22 op * purpose with or without fee is hereby granted, provided that the above
6 2ff1e2a9 2023-07-22 op * copyright notice and this permission notice appear in all copies.
7 2ff1e2a9 2023-07-22 op *
8 2ff1e2a9 2023-07-22 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2ff1e2a9 2023-07-22 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2ff1e2a9 2023-07-22 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2ff1e2a9 2023-07-22 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2ff1e2a9 2023-07-22 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2ff1e2a9 2023-07-22 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2ff1e2a9 2023-07-22 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 2ff1e2a9 2023-07-22 op */
16 2ff1e2a9 2023-07-22 op
17 2ff1e2a9 2023-07-22 op #include "config.h"
18 2ff1e2a9 2023-07-22 op
19 2ff1e2a9 2023-07-22 op #include <sys/stat.h>
20 2ff1e2a9 2023-07-22 op #include <sys/socket.h>
21 2ff1e2a9 2023-07-22 op
22 2ff1e2a9 2023-07-22 op #include <errno.h>
23 2ff1e2a9 2023-07-22 op #include <fcntl.h>
24 2ff1e2a9 2023-07-22 op #include <netdb.h>
25 2ff1e2a9 2023-07-22 op #include <poll.h>
26 2ff1e2a9 2023-07-22 op #include <stdio.h>
27 2ff1e2a9 2023-07-22 op #include <stdlib.h>
28 2ff1e2a9 2023-07-22 op #include <string.h>
29 2ff1e2a9 2023-07-22 op #include <tls.h>
30 2ff1e2a9 2023-07-22 op #include <unistd.h>
31 2ff1e2a9 2023-07-22 op
32 2ff1e2a9 2023-07-22 op #include "iri.h"
33 da3fc66e 2023-07-22 op
34 da3fc66e 2023-07-22 op #ifndef INFTIM
35 da3fc66e 2023-07-22 op #define INFTIM -1
36 da3fc66e 2023-07-22 op #endif
37 da3fc66e 2023-07-22 op
38 da3fc66e 2023-07-22 op #ifndef __OpenBSD__
39 da3fc66e 2023-07-22 op #define pledge(a, b) (0)
40 da3fc66e 2023-07-22 op #endif
41 2ff1e2a9 2023-07-22 op
42 2ff1e2a9 2023-07-22 op static int
43 2ff1e2a9 2023-07-22 op dial(const char *hostname, const char *port)
44 2ff1e2a9 2023-07-22 op {
45 2ff1e2a9 2023-07-22 op struct addrinfo hints, *res, *res0;
46 2ff1e2a9 2023-07-22 op int error, save_errno, s;
47 2ff1e2a9 2023-07-22 op const char *cause = NULL;
48 2ff1e2a9 2023-07-22 op
49 2ff1e2a9 2023-07-22 op if (port == NULL || *port == '\0')
50 2ff1e2a9 2023-07-22 op port = "1965";
51 2ff1e2a9 2023-07-22 op
52 2ff1e2a9 2023-07-22 op memset(&hints, 0, sizeof(hints));
53 2ff1e2a9 2023-07-22 op hints.ai_family = AF_UNSPEC;
54 2ff1e2a9 2023-07-22 op hints.ai_socktype = SOCK_STREAM;
55 2ff1e2a9 2023-07-22 op error = getaddrinfo(hostname, port, &hints, &res0);
56 2ff1e2a9 2023-07-22 op if (error)
57 2ff1e2a9 2023-07-22 op errx(1, "can't resolve %s: %s", hostname, gai_strerror(error));
58 2ff1e2a9 2023-07-22 op
59 2ff1e2a9 2023-07-22 op s = -1;
60 2ff1e2a9 2023-07-22 op for (res = res0; res; res = res->ai_next) {
61 2ff1e2a9 2023-07-22 op s = socket(res->ai_family, res->ai_socktype,
62 2ff1e2a9 2023-07-22 op res->ai_protocol);
63 2ff1e2a9 2023-07-22 op if (s == -1) {
64 2ff1e2a9 2023-07-22 op cause = "socket";
65 2ff1e2a9 2023-07-22 op continue;
66 2ff1e2a9 2023-07-22 op }
67 2ff1e2a9 2023-07-22 op
68 2ff1e2a9 2023-07-22 op if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
69 2ff1e2a9 2023-07-22 op cause = "connect";
70 2ff1e2a9 2023-07-22 op save_errno = errno;
71 2ff1e2a9 2023-07-22 op close(s);
72 2ff1e2a9 2023-07-22 op errno = save_errno;
73 2ff1e2a9 2023-07-22 op s = -1;
74 2ff1e2a9 2023-07-22 op continue;
75 2ff1e2a9 2023-07-22 op }
76 2ff1e2a9 2023-07-22 op
77 2ff1e2a9 2023-07-22 op if (fcntl(s, F_SETFL, O_NONBLOCK) == -1)
78 2ff1e2a9 2023-07-22 op err(1, "fcntl");
79 2ff1e2a9 2023-07-22 op break; /* got one */
80 2ff1e2a9 2023-07-22 op }
81 2ff1e2a9 2023-07-22 op if (s == -1)
82 2ff1e2a9 2023-07-22 op err(1, "%s", cause);
83 2ff1e2a9 2023-07-22 op freeaddrinfo(res0);
84 2ff1e2a9 2023-07-22 op return (s);
85 2ff1e2a9 2023-07-22 op }
86 2ff1e2a9 2023-07-22 op
87 2ff1e2a9 2023-07-22 op /* returns read bytes, or -1 on error */
88 2ff1e2a9 2023-07-22 op static ssize_t
89 2ff1e2a9 2023-07-22 op iomux(struct tls *ctx, int fd, const char *in, size_t inlen, char *out,
90 2ff1e2a9 2023-07-22 op size_t outlen)
91 2ff1e2a9 2023-07-22 op {
92 2ff1e2a9 2023-07-22 op struct pollfd pfd;
93 2ff1e2a9 2023-07-22 op size_t outwrote = 0;
94 2ff1e2a9 2023-07-22 op ssize_t ret;
95 2ff1e2a9 2023-07-22 op
96 2ff1e2a9 2023-07-22 op memset(&pfd, 0, sizeof(pfd));
97 2ff1e2a9 2023-07-22 op pfd.fd = fd;
98 2ff1e2a9 2023-07-22 op pfd.events = POLLIN|POLLOUT;
99 2ff1e2a9 2023-07-22 op
100 2ff1e2a9 2023-07-22 op for (;;) {
101 2ff1e2a9 2023-07-22 op if (poll(&pfd, 1, INFTIM) == -1)
102 2ff1e2a9 2023-07-22 op err(1, "poll");
103 2ff1e2a9 2023-07-22 op if (pfd.revents & (POLLERR|POLLNVAL))
104 2ff1e2a9 2023-07-22 op errx(1, "bad fd %d", pfd.fd);
105 2ff1e2a9 2023-07-22 op
106 2ff1e2a9 2023-07-22 op /* attempt to read */
107 2ff1e2a9 2023-07-22 op if (out != NULL) {
108 2ff1e2a9 2023-07-22 op switch (ret = tls_read(ctx, out, outlen)) {
109 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLIN:
110 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLOUT:
111 2ff1e2a9 2023-07-22 op break;
112 2ff1e2a9 2023-07-22 op case -1:
113 2ff1e2a9 2023-07-22 op return -1;
114 2ff1e2a9 2023-07-22 op case 0:
115 2ff1e2a9 2023-07-22 op return outwrote;
116 2ff1e2a9 2023-07-22 op default:
117 2ff1e2a9 2023-07-22 op outwrote += ret;
118 2ff1e2a9 2023-07-22 op out += ret;
119 2ff1e2a9 2023-07-22 op outlen -= ret;
120 2ff1e2a9 2023-07-22 op }
121 2ff1e2a9 2023-07-22 op
122 2ff1e2a9 2023-07-22 op /*
123 2ff1e2a9 2023-07-22 op * don't write if we're reading; titan works
124 2ff1e2a9 2023-07-22 op * like this.
125 2ff1e2a9 2023-07-22 op */
126 2ff1e2a9 2023-07-22 op if (outwrote != 0)
127 2ff1e2a9 2023-07-22 op continue;
128 2ff1e2a9 2023-07-22 op }
129 2ff1e2a9 2023-07-22 op
130 2ff1e2a9 2023-07-22 op if (inlen == 0 && out == NULL)
131 2ff1e2a9 2023-07-22 op break;
132 2ff1e2a9 2023-07-22 op if (inlen == 0)
133 2ff1e2a9 2023-07-22 op continue;
134 2ff1e2a9 2023-07-22 op
135 2ff1e2a9 2023-07-22 op switch (ret = tls_write(ctx, in, inlen)) {
136 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLIN:
137 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLOUT:
138 2ff1e2a9 2023-07-22 op continue;
139 2ff1e2a9 2023-07-22 op case 0:
140 2ff1e2a9 2023-07-22 op case -1:
141 2ff1e2a9 2023-07-22 op return -1;
142 2ff1e2a9 2023-07-22 op default:
143 2ff1e2a9 2023-07-22 op in += ret;
144 2ff1e2a9 2023-07-22 op inlen -= ret;
145 2ff1e2a9 2023-07-22 op }
146 2ff1e2a9 2023-07-22 op }
147 2ff1e2a9 2023-07-22 op
148 2ff1e2a9 2023-07-22 op return 0;
149 2ff1e2a9 2023-07-22 op }
150 2ff1e2a9 2023-07-22 op
151 2ff1e2a9 2023-07-22 op static void __dead
152 2ff1e2a9 2023-07-22 op usage(void)
153 2ff1e2a9 2023-07-22 op {
154 2ff1e2a9 2023-07-22 op fprintf(stderr,
155 2ff1e2a9 2023-07-22 op "usage: %s [-C cert] [-K key] [-m mime] [-t token] url file\n",
156 2ff1e2a9 2023-07-22 op getprogname());
157 2ff1e2a9 2023-07-22 op exit(1);
158 2ff1e2a9 2023-07-22 op }
159 2ff1e2a9 2023-07-22 op
160 2ff1e2a9 2023-07-22 op int
161 2ff1e2a9 2023-07-22 op main(int argc, char **argv)
162 2ff1e2a9 2023-07-22 op {
163 2ff1e2a9 2023-07-22 op struct tls_config *config;
164 2ff1e2a9 2023-07-22 op struct tls *ctx;
165 2ff1e2a9 2023-07-22 op struct stat sb;
166 2ff1e2a9 2023-07-22 op struct iri iri;
167 2ff1e2a9 2023-07-22 op FILE *in;
168 2ff1e2a9 2023-07-22 op const char *cert = NULL, *key = NULL, *mime = NULL, *token = NULL;
169 2ff1e2a9 2023-07-22 op const char *parse_err;
170 2ff1e2a9 2023-07-22 op char iribuf[1025];
171 2ff1e2a9 2023-07-22 op char resbuf[1025];
172 2ff1e2a9 2023-07-22 op char *req;
173 2ff1e2a9 2023-07-22 op int sock, ch;
174 2ff1e2a9 2023-07-22 op
175 2ff1e2a9 2023-07-22 op if (pledge("stdio rpath inet dns", NULL) == -1)
176 2ff1e2a9 2023-07-22 op err(1, "pledge");
177 2ff1e2a9 2023-07-22 op
178 2ff1e2a9 2023-07-22 op while ((ch = getopt(argc, argv, "C:K:m:t:")) != -1) {
179 2ff1e2a9 2023-07-22 op switch (ch) {
180 2ff1e2a9 2023-07-22 op case 'C':
181 2ff1e2a9 2023-07-22 op cert = optarg;
182 2ff1e2a9 2023-07-22 op break;
183 2ff1e2a9 2023-07-22 op case 'K':
184 2ff1e2a9 2023-07-22 op key = optarg;
185 2ff1e2a9 2023-07-22 op break;
186 2ff1e2a9 2023-07-22 op case 'm':
187 2ff1e2a9 2023-07-22 op mime = optarg;
188 2ff1e2a9 2023-07-22 op break;
189 2ff1e2a9 2023-07-22 op case 't':
190 2ff1e2a9 2023-07-22 op token = optarg;
191 2ff1e2a9 2023-07-22 op break;
192 2ff1e2a9 2023-07-22 op }
193 2ff1e2a9 2023-07-22 op }
194 2ff1e2a9 2023-07-22 op argc -= optind;
195 2ff1e2a9 2023-07-22 op argv += optind;
196 2ff1e2a9 2023-07-22 op
197 2ff1e2a9 2023-07-22 op if (cert == NULL && key != NULL)
198 2ff1e2a9 2023-07-22 op usage();
199 2ff1e2a9 2023-07-22 op if (cert != NULL && key == NULL)
200 2ff1e2a9 2023-07-22 op key = cert;
201 2ff1e2a9 2023-07-22 op
202 2ff1e2a9 2023-07-22 op if (argc != 2)
203 2ff1e2a9 2023-07-22 op usage();
204 2ff1e2a9 2023-07-22 op
205 2ff1e2a9 2023-07-22 op if ((in = fopen(argv[1], "r")) == NULL)
206 2ff1e2a9 2023-07-22 op err(1, "can't open %s", argv[1]);
207 2ff1e2a9 2023-07-22 op
208 2ff1e2a9 2023-07-22 op /* drop rpath */
209 2ff1e2a9 2023-07-22 op if (pledge("stdio inet dns", NULL) == -1)
210 2ff1e2a9 2023-07-22 op err(1, "pledge");
211 2ff1e2a9 2023-07-22 op
212 2ff1e2a9 2023-07-22 op if (fstat(fileno(in), &sb) == -1)
213 2ff1e2a9 2023-07-22 op err(1, "fstat");
214 2ff1e2a9 2023-07-22 op
215 2ff1e2a9 2023-07-22 op /* prepare the URL */
216 2ff1e2a9 2023-07-22 op if (token && mime) {
217 2ff1e2a9 2023-07-22 op if (asprintf(&req, "%s;size=%lld;token=%s;mime=%s\r\n", argv[0],
218 2ff1e2a9 2023-07-22 op (long long)sb.st_size, token, mime) == -1)
219 2ff1e2a9 2023-07-22 op err(1, "asprintf");
220 2ff1e2a9 2023-07-22 op } else if (token) {
221 2ff1e2a9 2023-07-22 op if (asprintf(&req, "%s;size=%lld;token=%s\r\n", argv[0],
222 2ff1e2a9 2023-07-22 op (long long)sb.st_size, token) == -1)
223 2ff1e2a9 2023-07-22 op err(1, "asprintf");
224 2ff1e2a9 2023-07-22 op } else if (mime) {
225 2ff1e2a9 2023-07-22 op if (asprintf(&req, "%s;size=%lld;mime=%s\r\n", argv[0],
226 2ff1e2a9 2023-07-22 op (long long)sb.st_size, mime) == -1)
227 2ff1e2a9 2023-07-22 op err(1, "asprintf");
228 2ff1e2a9 2023-07-22 op } else {
229 2ff1e2a9 2023-07-22 op if (asprintf(&req, "%s;size=%lld\r\n", argv[0],
230 2ff1e2a9 2023-07-22 op (long long)sb.st_size) == -1)
231 2ff1e2a9 2023-07-22 op err(1, "asprintf");
232 2ff1e2a9 2023-07-22 op }
233 2ff1e2a9 2023-07-22 op
234 2ff1e2a9 2023-07-22 op if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
235 2ff1e2a9 2023-07-22 op errx(1, "URL too long");
236 2ff1e2a9 2023-07-22 op
237 2ff1e2a9 2023-07-22 op if (!parse_iri(iribuf, &iri, &parse_err))
238 2ff1e2a9 2023-07-22 op errx(1, "invalid IRI: %s", parse_err);
239 2ff1e2a9 2023-07-22 op
240 2ff1e2a9 2023-07-22 op if ((config = tls_config_new()) == NULL)
241 2ff1e2a9 2023-07-22 op err(1, "tls_config_new");
242 2ff1e2a9 2023-07-22 op tls_config_insecure_noverifycert(config);
243 2ff1e2a9 2023-07-22 op tls_config_insecure_noverifyname(config);
244 2ff1e2a9 2023-07-22 op
245 2ff1e2a9 2023-07-22 op if (cert && tls_config_set_keypair_file(config, cert, key) == -1)
246 2ff1e2a9 2023-07-22 op errx(1, "cant load certificate client %s", cert);
247 2ff1e2a9 2023-07-22 op
248 2ff1e2a9 2023-07-22 op if ((ctx = tls_client()) == NULL)
249 2ff1e2a9 2023-07-22 op errx(1, "can't create tls context");
250 2ff1e2a9 2023-07-22 op
251 2ff1e2a9 2023-07-22 op if (tls_configure(ctx, config) == -1)
252 2ff1e2a9 2023-07-22 op errx(1, "tls_configure: %s", tls_error(ctx));
253 2ff1e2a9 2023-07-22 op
254 2ff1e2a9 2023-07-22 op sock = dial(iri.host, iri.port);
255 2ff1e2a9 2023-07-22 op if (tls_connect_socket(ctx, sock, iri.host) == -1)
256 2ff1e2a9 2023-07-22 op errx(1, "failed to connect to %s:%s: %s", iri.host,
257 2ff1e2a9 2023-07-22 op *iri.port == '\0' ? "1965" : iri.port, tls_error(ctx));
258 2ff1e2a9 2023-07-22 op
259 2ff1e2a9 2023-07-22 op /* drop inet tls */
260 2ff1e2a9 2023-07-22 op if (pledge("stdio", NULL) == -1)
261 2ff1e2a9 2023-07-22 op err(1, "pledge");
262 2ff1e2a9 2023-07-22 op
263 2ff1e2a9 2023-07-22 op /* send request */
264 2ff1e2a9 2023-07-22 op if (iomux(ctx, sock, req, strlen(req), NULL, 0) == -1)
265 2ff1e2a9 2023-07-22 op errx(1, "I/O error: %s", tls_error(ctx));
266 2ff1e2a9 2023-07-22 op
267 2ff1e2a9 2023-07-22 op for (;;) {
268 2ff1e2a9 2023-07-22 op static char buf[BUFSIZ];
269 2ff1e2a9 2023-07-22 op size_t buflen;
270 2ff1e2a9 2023-07-22 op ssize_t w;
271 2ff1e2a9 2023-07-22 op char *m;
272 2ff1e2a9 2023-07-22 op
273 2ff1e2a9 2023-07-22 op /* will be zero on EOF */
274 2ff1e2a9 2023-07-22 op buflen = fread(buf, 1, sizeof(buf), in);
275 2ff1e2a9 2023-07-22 op
276 2ff1e2a9 2023-07-22 op w = iomux(ctx, sock, buf, buflen, resbuf, sizeof(resbuf));
277 2ff1e2a9 2023-07-22 op if (w == -1)
278 2ff1e2a9 2023-07-22 op errx(1, "I/O error: %s", tls_error(ctx));
279 2ff1e2a9 2023-07-22 op if (w != 0) {
280 2ff1e2a9 2023-07-22 op if ((m = memmem(resbuf, w, "\r\n", 2)) == NULL)
281 2ff1e2a9 2023-07-22 op errx(1, "invalid reply");
282 2ff1e2a9 2023-07-22 op *m = '\0';
283 2ff1e2a9 2023-07-22 op /* XXX parse */
284 2ff1e2a9 2023-07-22 op puts(resbuf);
285 2ff1e2a9 2023-07-22 op break;
286 2ff1e2a9 2023-07-22 op }
287 2ff1e2a9 2023-07-22 op }
288 2ff1e2a9 2023-07-22 op
289 2ff1e2a9 2023-07-22 op /* close connection */
290 2ff1e2a9 2023-07-22 op for (;;) {
291 2ff1e2a9 2023-07-22 op struct pollfd pfd;
292 2ff1e2a9 2023-07-22 op
293 2ff1e2a9 2023-07-22 op memset(&pfd, 0, sizeof(pfd));
294 2ff1e2a9 2023-07-22 op pfd.fd = sock;
295 2ff1e2a9 2023-07-22 op pfd.events = POLLIN|POLLOUT;
296 2ff1e2a9 2023-07-22 op
297 2ff1e2a9 2023-07-22 op switch (tls_close(ctx)) {
298 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLIN:
299 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLOUT:
300 2ff1e2a9 2023-07-22 op if (poll(&pfd, 1, INFTIM) == -1)
301 2ff1e2a9 2023-07-22 op err(1, "poll");
302 2ff1e2a9 2023-07-22 op break;
303 2ff1e2a9 2023-07-22 op case -1:
304 2ff1e2a9 2023-07-22 op warnx("tls_close: %s", tls_error(ctx));
305 2ff1e2a9 2023-07-22 op /* fallthrough */
306 2ff1e2a9 2023-07-22 op default:
307 2ff1e2a9 2023-07-22 op tls_free(ctx);
308 2ff1e2a9 2023-07-22 op return (0);
309 2ff1e2a9 2023-07-22 op }
310 2ff1e2a9 2023-07-22 op }
311 2ff1e2a9 2023-07-22 op }