2 * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
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.
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.
20 #include <sys/socket.h>
39 #define pledge(a, b) (0)
43 dial(const char *hostname, const char *port)
45 struct addrinfo hints, *res, *res0;
46 int error, save_errno, s;
47 const char *cause = NULL;
49 if (port == NULL || *port == '\0')
52 memset(&hints, 0, sizeof(hints));
53 hints.ai_family = AF_UNSPEC;
54 hints.ai_socktype = SOCK_STREAM;
55 error = getaddrinfo(hostname, port, &hints, &res0);
57 errx(1, "can't resolve %s: %s", hostname, gai_strerror(error));
60 for (res = res0; res; res = res->ai_next) {
61 s = socket(res->ai_family, res->ai_socktype,
68 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
77 if (fcntl(s, F_SETFL, O_NONBLOCK) == -1)
87 /* returns read bytes, or -1 on error */
89 iomux(struct tls *ctx, int fd, const char *in, size_t inlen, char *out,
96 memset(&pfd, 0, sizeof(pfd));
98 pfd.events = POLLIN|POLLOUT;
101 if (poll(&pfd, 1, INFTIM) == -1)
103 if (pfd.revents & (POLLERR|POLLNVAL))
104 errx(1, "bad fd %d", pfd.fd);
106 /* attempt to read */
108 switch (ret = tls_read(ctx, out, outlen)) {
109 case TLS_WANT_POLLIN:
110 case TLS_WANT_POLLOUT:
123 * don't write if we're reading; titan works
130 if (inlen == 0 && out == NULL)
135 switch (ret = tls_write(ctx, in, inlen)) {
136 case TLS_WANT_POLLIN:
137 case TLS_WANT_POLLOUT:
155 "usage: %s [-C cert] [-K key] [-m mime] [-t token] url file\n",
161 main(int argc, char **argv)
163 struct tls_config *config;
168 const char *cert = NULL, *key = NULL, *mime = NULL, *token = NULL;
169 const char *parse_err;
175 if (pledge("stdio rpath inet dns", NULL) == -1)
178 while ((ch = getopt(argc, argv, "C:K:m:t:")) != -1) {
197 if (cert == NULL && key != NULL)
199 if (cert != NULL && key == NULL)
205 if ((in = fopen(argv[1], "r")) == NULL)
206 err(1, "can't open %s", argv[1]);
209 if (pledge("stdio inet dns", NULL) == -1)
212 if (fstat(fileno(in), &sb) == -1)
215 /* prepare the URL */
217 if (asprintf(&req, "%s;size=%lld;token=%s;mime=%s\r\n", argv[0],
218 (long long)sb.st_size, token, mime) == -1)
221 if (asprintf(&req, "%s;size=%lld;token=%s\r\n", argv[0],
222 (long long)sb.st_size, token) == -1)
225 if (asprintf(&req, "%s;size=%lld;mime=%s\r\n", argv[0],
226 (long long)sb.st_size, mime) == -1)
229 if (asprintf(&req, "%s;size=%lld\r\n", argv[0],
230 (long long)sb.st_size) == -1)
234 if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
235 errx(1, "URL too long");
237 if (!parse_iri(iribuf, &iri, &parse_err))
238 errx(1, "invalid IRI: %s", parse_err);
240 if ((config = tls_config_new()) == NULL)
241 err(1, "tls_config_new");
242 tls_config_insecure_noverifycert(config);
243 tls_config_insecure_noverifyname(config);
245 if (cert && tls_config_set_keypair_file(config, cert, key) == -1)
246 errx(1, "cant load certificate client %s", cert);
248 if ((ctx = tls_client()) == NULL)
249 errx(1, "can't create tls context");
251 if (tls_configure(ctx, config) == -1)
252 errx(1, "tls_configure: %s", tls_error(ctx));
254 sock = dial(iri.host, iri.port);
255 if (tls_connect_socket(ctx, sock, iri.host) == -1)
256 errx(1, "failed to connect to %s:%s: %s", iri.host,
257 *iri.port == '\0' ? "1965" : iri.port, tls_error(ctx));
260 if (pledge("stdio", NULL) == -1)
264 if (iomux(ctx, sock, req, strlen(req), NULL, 0) == -1)
265 errx(1, "I/O error: %s", tls_error(ctx));
268 static char buf[BUFSIZ];
273 /* will be zero on EOF */
274 buflen = fread(buf, 1, sizeof(buf), in);
276 w = iomux(ctx, sock, buf, buflen, resbuf, sizeof(resbuf));
278 errx(1, "I/O error: %s", tls_error(ctx));
280 if ((m = memmem(resbuf, w, "\r\n", 2)) == NULL)
281 errx(1, "invalid reply");
289 /* close connection */
293 memset(&pfd, 0, sizeof(pfd));
295 pfd.events = POLLIN|POLLOUT;
297 switch (tls_close(ctx)) {
298 case TLS_WANT_POLLIN:
299 case TLS_WANT_POLLOUT:
300 if (poll(&pfd, 1, INFTIM) == -1)
304 warnx("tls_close: %s", tls_error(ctx));