2 2ff1e2a9 2023-07-22 op * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
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.
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.
17 2ff1e2a9 2023-07-22 op #include "config.h"
19 2ff1e2a9 2023-07-22 op #include <sys/stat.h>
20 2ff1e2a9 2023-07-22 op #include <sys/socket.h>
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>
32 2ff1e2a9 2023-07-22 op #include "iri.h"
34 da3fc66e 2023-07-22 op #ifndef INFTIM
35 da3fc66e 2023-07-22 op #define INFTIM -1
38 da3fc66e 2023-07-22 op #ifndef __OpenBSD__
39 da3fc66e 2023-07-22 op #define pledge(a, b) (0)
43 2ff1e2a9 2023-07-22 op dial(const char *hostname, const char *port)
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;
49 2ff1e2a9 2023-07-22 op if (port == NULL || *port == '\0')
50 2ff1e2a9 2023-07-22 op port = "1965";
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);
57 2ff1e2a9 2023-07-22 op errx(1, "can't resolve %s: %s", hostname, gai_strerror(error));
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";
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;
72 2ff1e2a9 2023-07-22 op errno = save_errno;
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 */
82 2ff1e2a9 2023-07-22 op err(1, "%s", cause);
83 2ff1e2a9 2023-07-22 op freeaddrinfo(res0);
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)
92 2ff1e2a9 2023-07-22 op struct pollfd pfd;
93 2ff1e2a9 2023-07-22 op size_t outwrote = 0;
96 2ff1e2a9 2023-07-22 op memset(&pfd, 0, sizeof(pfd));
98 2ff1e2a9 2023-07-22 op pfd.events = POLLIN|POLLOUT;
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);
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:
115 1b1a6fb7 2023-08-04 op if (outwrote == 0)
117 2ff1e2a9 2023-07-22 op return outwrote;
119 2ff1e2a9 2023-07-22 op outwrote += ret;
121 2ff1e2a9 2023-07-22 op outlen -= ret;
125 2ff1e2a9 2023-07-22 op * don't write if we're reading; titan works
128 2ff1e2a9 2023-07-22 op if (outwrote != 0)
132 2ff1e2a9 2023-07-22 op if (inlen == 0 && out == NULL)
134 2ff1e2a9 2023-07-22 op if (inlen == 0)
137 2ff1e2a9 2023-07-22 op switch (ret = tls_write(ctx, in, inlen)) {
138 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLIN:
139 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLOUT:
146 2ff1e2a9 2023-07-22 op inlen -= ret;
153 9888507c 2023-07-24 op static FILE *
154 9888507c 2023-07-24 op open_input_file(int argc, char **argv)
157 9888507c 2023-07-24 op char buf[BUFSIZ];
158 9888507c 2023-07-24 op char sfn[22];
162 9888507c 2023-07-24 op if (argc > 1) {
163 9888507c 2023-07-24 op if ((fp = fopen(argv[1], "r")) == NULL)
164 9888507c 2023-07-24 op err(1, "can't open %s", argv[1]);
168 9888507c 2023-07-24 op strlcpy(sfn, "/tmp/titan.XXXXXXXXXX", sizeof(sfn));
169 9888507c 2023-07-24 op if ((fd = mkstemp(sfn)) == -1 ||
170 9888507c 2023-07-24 op (fp = fdopen(fd, "w+")) == NULL) {
171 9888507c 2023-07-24 op warn("%s", sfn);
172 9888507c 2023-07-24 op if (fd != -1) {
176 9888507c 2023-07-24 op errx(1, "can't create temp file");
181 9888507c 2023-07-24 op r = fread(buf, 1, sizeof(buf), stdin);
184 9888507c 2023-07-24 op fwrite(buf, 1, r, fp);
186 9888507c 2023-07-24 op if (ferror(fp))
187 9888507c 2023-07-24 op err(1, "I/O error");
189 9888507c 2023-07-24 op if (fseeko(fp, 0, SEEK_SET) == -1)
190 9888507c 2023-07-24 op err(1, "fseeko");
196 39273368 2023-07-24 op parse_response(char *r)
200 39273368 2023-07-24 op if (r[0] < '0' || r[0] > '9' ||
201 39273368 2023-07-24 op r[1] < '0' || r[1] > '9' ||
203 39273368 2023-07-24 op errx(1, "illegal response");
205 39273368 2023-07-24 op code = (r[0] - '0') * 10 + (r[1] - '0');
206 39273368 2023-07-24 op if (code < 10 || code >= 70)
207 39273368 2023-07-24 op errx(1, "invalid response code: %d", code);
208 39273368 2023-07-24 op if (code >= 20 && code < 30)
210 39273368 2023-07-24 op if (code >= 30 && code < 40) {
214 39273368 2023-07-24 op warnx("server error: %s", r + 3);
218 2ff1e2a9 2023-07-22 op static void __dead
221 2ff1e2a9 2023-07-22 op fprintf(stderr,
222 9888507c 2023-07-24 op "usage: %s [-C cert] [-K key] [-m mime] [-t token] url [file]\n",
223 2ff1e2a9 2023-07-22 op getprogname());
228 2ff1e2a9 2023-07-22 op main(int argc, char **argv)
230 2ff1e2a9 2023-07-22 op struct tls_config *config;
231 2ff1e2a9 2023-07-22 op struct tls *ctx;
232 2ff1e2a9 2023-07-22 op struct stat sb;
233 2ff1e2a9 2023-07-22 op struct iri iri;
235 2ff1e2a9 2023-07-22 op const char *cert = NULL, *key = NULL, *mime = NULL, *token = NULL;
236 59051566 2023-08-04 op const char *errstr;
237 2ff1e2a9 2023-07-22 op char iribuf[1025];
238 fcc5a371 2023-08-04 op char reqbuf[1025];
239 2ff1e2a9 2023-07-22 op char resbuf[1025];
241 39273368 2023-07-24 op int sock, ch, ret = 0;
243 9888507c 2023-07-24 op if (pledge("stdio rpath tmppath inet dns", NULL) == -1)
244 2ff1e2a9 2023-07-22 op err(1, "pledge");
246 2ff1e2a9 2023-07-22 op while ((ch = getopt(argc, argv, "C:K:m:t:")) != -1) {
247 2ff1e2a9 2023-07-22 op switch (ch) {
249 2ff1e2a9 2023-07-22 op cert = optarg;
252 2ff1e2a9 2023-07-22 op key = optarg;
255 2ff1e2a9 2023-07-22 op mime = optarg;
258 2ff1e2a9 2023-07-22 op token = optarg;
264 2ff1e2a9 2023-07-22 op argc -= optind;
265 2ff1e2a9 2023-07-22 op argv += optind;
267 2ff1e2a9 2023-07-22 op if (cert == NULL && key != NULL)
269 2ff1e2a9 2023-07-22 op if (cert != NULL && key == NULL)
272 dd3f04b2 2024-01-08 op if (argc != 1 && argc != 2)
275 9888507c 2023-07-24 op in = open_input_file(argc, argv);
277 9888507c 2023-07-24 op /* drop rpath tmppath */
278 2ff1e2a9 2023-07-22 op if (pledge("stdio inet dns", NULL) == -1)
279 2ff1e2a9 2023-07-22 op err(1, "pledge");
281 2ff1e2a9 2023-07-22 op if (fstat(fileno(in), &sb) == -1)
282 2ff1e2a9 2023-07-22 op err(1, "fstat");
284 5d38e5d8 2023-08-04 op /* prepare the IRI */
285 fcc5a371 2023-08-04 op if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
286 5d38e5d8 2023-08-04 op errx(1, "IRI too long");
288 59051566 2023-08-04 op if (!parse_iri(iribuf, &iri, &errstr))
289 59051566 2023-08-04 op errx(1, "invalid IRI: %s", errstr);
291 11ff7f93 2023-08-04 op if (strcmp(iri.schema, "titan") != 0)
292 5d38e5d8 2023-08-04 op errx(1, "not a titan:// IRI");
294 2ff1e2a9 2023-07-22 op if (token && mime) {
295 fcc5a371 2023-08-04 op if (asprintf(&path, "%s;size=%lld;token=%s;mime=%s", iri.path,
296 2ff1e2a9 2023-07-22 op (long long)sb.st_size, token, mime) == -1)
297 2ff1e2a9 2023-07-22 op err(1, "asprintf");
298 2ff1e2a9 2023-07-22 op } else if (token) {
299 fcc5a371 2023-08-04 op if (asprintf(&path, "%s;size=%lld;token=%s", iri.path,
300 2ff1e2a9 2023-07-22 op (long long)sb.st_size, token) == -1)
301 2ff1e2a9 2023-07-22 op err(1, "asprintf");
302 2ff1e2a9 2023-07-22 op } else if (mime) {
303 fcc5a371 2023-08-04 op if (asprintf(&path, "%s;size=%lld;mime=%s", iri.path,
304 2ff1e2a9 2023-07-22 op (long long)sb.st_size, mime) == -1)
305 2ff1e2a9 2023-07-22 op err(1, "asprintf");
307 fcc5a371 2023-08-04 op if (asprintf(&path, "%s;size=%lld", iri.path,
308 2ff1e2a9 2023-07-22 op (long long)sb.st_size) == -1)
309 2ff1e2a9 2023-07-22 op err(1, "asprintf");
312 fcc5a371 2023-08-04 op iri.path = path;
313 fcc5a371 2023-08-04 op if (!serialize_iri(&iri, reqbuf, sizeof(reqbuf)) ||
314 fcc5a371 2023-08-04 op strlcat(reqbuf, "\r\n", sizeof(reqbuf)) >= sizeof(reqbuf))
315 5d38e5d8 2023-08-04 op errx(1, "IRI too long");
317 2ff1e2a9 2023-07-22 op if ((config = tls_config_new()) == NULL)
318 2ff1e2a9 2023-07-22 op err(1, "tls_config_new");
319 2ff1e2a9 2023-07-22 op tls_config_insecure_noverifycert(config);
320 2ff1e2a9 2023-07-22 op tls_config_insecure_noverifyname(config);
322 2ff1e2a9 2023-07-22 op if (cert && tls_config_set_keypair_file(config, cert, key) == -1)
323 2ff1e2a9 2023-07-22 op errx(1, "cant load certificate client %s", cert);
325 2ff1e2a9 2023-07-22 op if ((ctx = tls_client()) == NULL)
326 2ff1e2a9 2023-07-22 op errx(1, "can't create tls context");
328 2ff1e2a9 2023-07-22 op if (tls_configure(ctx, config) == -1)
329 2ff1e2a9 2023-07-22 op errx(1, "tls_configure: %s", tls_error(ctx));
331 2ff1e2a9 2023-07-22 op sock = dial(iri.host, iri.port);
333 2ff1e2a9 2023-07-22 op /* drop inet tls */
334 2ff1e2a9 2023-07-22 op if (pledge("stdio", NULL) == -1)
335 2ff1e2a9 2023-07-22 op err(1, "pledge");
337 32b1638e 2023-07-24 op if (tls_connect_socket(ctx, sock, iri.host) == -1)
338 32b1638e 2023-07-24 op errx(1, "failed to connect to %s:%s: %s", iri.host,
339 32b1638e 2023-07-24 op *iri.port == '\0' ? "1965" : iri.port, tls_error(ctx));
341 2ff1e2a9 2023-07-22 op /* send request */
342 fcc5a371 2023-08-04 op if (iomux(ctx, sock, reqbuf, strlen(reqbuf), NULL, 0) == -1)
343 2ff1e2a9 2023-07-22 op errx(1, "I/O error: %s", tls_error(ctx));
346 2ff1e2a9 2023-07-22 op static char buf[BUFSIZ];
347 2ff1e2a9 2023-07-22 op size_t buflen;
351 2ff1e2a9 2023-07-22 op /* will be zero on EOF */
352 2ff1e2a9 2023-07-22 op buflen = fread(buf, 1, sizeof(buf), in);
354 2ff1e2a9 2023-07-22 op w = iomux(ctx, sock, buf, buflen, resbuf, sizeof(resbuf));
355 d671434b 2023-08-04 op if (w == -1) {
356 d671434b 2023-08-04 op errstr = tls_error(ctx);
357 d671434b 2023-08-04 op if (errstr == NULL)
358 d671434b 2023-08-04 op errstr = "unexpected EOF";
359 d671434b 2023-08-04 op errx(1, "I/O error: %s", errstr);
361 2ff1e2a9 2023-07-22 op if (w != 0) {
362 2ff1e2a9 2023-07-22 op if ((m = memmem(resbuf, w, "\r\n", 2)) == NULL)
363 2ff1e2a9 2023-07-22 op errx(1, "invalid reply");
365 39273368 2023-07-24 op ret = parse_response(resbuf);
370 2ff1e2a9 2023-07-22 op /* close connection */
372 2ff1e2a9 2023-07-22 op struct pollfd pfd;
374 2ff1e2a9 2023-07-22 op memset(&pfd, 0, sizeof(pfd));
375 2ff1e2a9 2023-07-22 op pfd.fd = sock;
376 2ff1e2a9 2023-07-22 op pfd.events = POLLIN|POLLOUT;
378 2ff1e2a9 2023-07-22 op switch (tls_close(ctx)) {
379 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLIN:
380 2ff1e2a9 2023-07-22 op case TLS_WANT_POLLOUT:
381 2ff1e2a9 2023-07-22 op if (poll(&pfd, 1, INFTIM) == -1)
382 2ff1e2a9 2023-07-22 op err(1, "poll");
385 2ff1e2a9 2023-07-22 op warnx("tls_close: %s", tls_error(ctx));
386 2ff1e2a9 2023-07-22 op /* fallthrough */
388 2ff1e2a9 2023-07-22 op tls_free(ctx);
389 39273368 2023-07-24 op return (ret);