Blob


1 /*
2 * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "config.h"
19 #include <sys/stat.h>
20 #include <sys/socket.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <netdb.h>
25 #include <poll.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <tls.h>
30 #include <unistd.h>
32 #include "iri.h"
34 #ifndef INFTIM
35 #define INFTIM -1
36 #endif
38 #ifndef __OpenBSD__
39 #define pledge(a, b) (0)
40 #endif
42 static int
43 dial(const char *hostname, const char *port)
44 {
45 struct addrinfo hints, *res, *res0;
46 int error, save_errno, s;
47 const char *cause = NULL;
49 if (port == NULL || *port == '\0')
50 port = "1965";
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);
56 if (error)
57 errx(1, "can't resolve %s: %s", hostname, gai_strerror(error));
59 s = -1;
60 for (res = res0; res; res = res->ai_next) {
61 s = socket(res->ai_family, res->ai_socktype,
62 res->ai_protocol);
63 if (s == -1) {
64 cause = "socket";
65 continue;
66 }
68 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
69 cause = "connect";
70 save_errno = errno;
71 close(s);
72 errno = save_errno;
73 s = -1;
74 continue;
75 }
77 if (fcntl(s, F_SETFL, O_NONBLOCK) == -1)
78 err(1, "fcntl");
79 break; /* got one */
80 }
81 if (s == -1)
82 err(1, "%s", cause);
83 freeaddrinfo(res0);
84 return (s);
85 }
87 /* returns read bytes, or -1 on error */
88 static ssize_t
89 iomux(struct tls *ctx, int fd, const char *in, size_t inlen, char *out,
90 size_t outlen)
91 {
92 struct pollfd pfd;
93 size_t outwrote = 0;
94 ssize_t ret;
96 memset(&pfd, 0, sizeof(pfd));
97 pfd.fd = fd;
98 pfd.events = POLLIN|POLLOUT;
100 for (;;) {
101 if (poll(&pfd, 1, INFTIM) == -1)
102 err(1, "poll");
103 if (pfd.revents & (POLLERR|POLLNVAL))
104 errx(1, "bad fd %d", pfd.fd);
106 /* attempt to read */
107 if (out != NULL) {
108 switch (ret = tls_read(ctx, out, outlen)) {
109 case TLS_WANT_POLLIN:
110 case TLS_WANT_POLLOUT:
111 break;
112 case -1:
113 return -1;
114 case 0:
115 if (outwrote == 0)
116 return -1;
117 return outwrote;
118 default:
119 outwrote += ret;
120 out += ret;
121 outlen -= ret;
124 /*
125 * don't write if we're reading; titan works
126 * like this.
127 */
128 if (outwrote != 0)
129 continue;
132 if (inlen == 0 && out == NULL)
133 break;
134 if (inlen == 0)
135 continue;
137 switch (ret = tls_write(ctx, in, inlen)) {
138 case TLS_WANT_POLLIN:
139 case TLS_WANT_POLLOUT:
140 continue;
141 case 0:
142 case -1:
143 return -1;
144 default:
145 in += ret;
146 inlen -= ret;
150 return 0;
153 static FILE *
154 open_input_file(int argc, char **argv)
156 FILE *fp;
157 char buf[BUFSIZ];
158 char sfn[22];
159 size_t r;
160 int fd;
162 if (argc > 1) {
163 if ((fp = fopen(argv[1], "r")) == NULL)
164 err(1, "can't open %s", argv[1]);
165 return fp;
168 strlcpy(sfn, "/tmp/titan.XXXXXXXXXX", sizeof(sfn));
169 if ((fd = mkstemp(sfn)) == -1 ||
170 (fp = fdopen(fd, "w+")) == NULL) {
171 warn("%s", sfn);
172 if (fd != -1) {
173 unlink(sfn);
174 close(fd);
176 errx(1, "can't create temp file");
178 unlink(sfn);
180 for (;;) {
181 r = fread(buf, 1, sizeof(buf), stdin);
182 if (r == 0)
183 break;
184 fwrite(buf, 1, r, fp);
186 if (ferror(fp))
187 err(1, "I/O error");
189 if (fseeko(fp, 0, SEEK_SET) == -1)
190 err(1, "fseeko");
192 return fp;
195 static int
196 parse_response(char *r)
198 int code;
200 if (r[0] < '0' || r[0] > '9' ||
201 r[1] < '0' || r[1] > '9' ||
202 r[2] != ' ')
203 errx(1, "illegal response");
205 code = (r[0] - '0') * 10 + (r[1] - '0');
206 if (code < 10 || code >= 70)
207 errx(1, "invalid response code: %d", code);
208 if (code >= 20 && code < 30)
209 return 0;
210 if (code >= 30 && code < 40) {
211 puts(r + 3);
212 return 0;
214 warnx("server error: %s", r + 3);
215 return 2;
218 static void __dead
219 usage(void)
221 fprintf(stderr,
222 "usage: %s [-C cert] [-K key] [-m mime] [-t token] url [file]\n",
223 getprogname());
224 exit(1);
227 int
228 main(int argc, char **argv)
230 struct tls_config *config;
231 struct tls *ctx;
232 struct stat sb;
233 struct iri iri;
234 FILE *in;
235 const char *cert = NULL, *key = NULL, *mime = NULL, *token = NULL;
236 const char *errstr;
237 char iribuf[1025];
238 char reqbuf[1025];
239 char resbuf[1025];
240 char *path;
241 int sock, ch, ret = 0;
243 if (pledge("stdio rpath tmppath inet dns", NULL) == -1)
244 err(1, "pledge");
246 while ((ch = getopt(argc, argv, "C:K:m:t:")) != -1) {
247 switch (ch) {
248 case 'C':
249 cert = optarg;
250 break;
251 case 'K':
252 key = optarg;
253 break;
254 case 'm':
255 mime = optarg;
256 break;
257 case 't':
258 token = optarg;
259 break;
260 default:
261 usage();
264 argc -= optind;
265 argv += optind;
267 if (cert == NULL && key != NULL)
268 usage();
269 if (cert != NULL && key == NULL)
270 key = cert;
272 if (argc != 1 && argc != 2)
273 usage();
275 in = open_input_file(argc, argv);
277 /* drop rpath tmppath */
278 if (pledge("stdio inet dns", NULL) == -1)
279 err(1, "pledge");
281 if (fstat(fileno(in), &sb) == -1)
282 err(1, "fstat");
284 /* prepare the IRI */
285 if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
286 errx(1, "IRI too long");
288 if (!parse_iri(iribuf, &iri, &errstr))
289 errx(1, "invalid IRI: %s", errstr);
291 if (strcmp(iri.schema, "titan") != 0)
292 errx(1, "not a titan:// IRI");
294 if (token && mime) {
295 if (asprintf(&path, "%s;size=%lld;token=%s;mime=%s", iri.path,
296 (long long)sb.st_size, token, mime) == -1)
297 err(1, "asprintf");
298 } else if (token) {
299 if (asprintf(&path, "%s;size=%lld;token=%s", iri.path,
300 (long long)sb.st_size, token) == -1)
301 err(1, "asprintf");
302 } else if (mime) {
303 if (asprintf(&path, "%s;size=%lld;mime=%s", iri.path,
304 (long long)sb.st_size, mime) == -1)
305 err(1, "asprintf");
306 } else {
307 if (asprintf(&path, "%s;size=%lld", iri.path,
308 (long long)sb.st_size) == -1)
309 err(1, "asprintf");
312 iri.path = path;
313 if (!serialize_iri(&iri, reqbuf, sizeof(reqbuf)) ||
314 strlcat(reqbuf, "\r\n", sizeof(reqbuf)) >= sizeof(reqbuf))
315 errx(1, "IRI too long");
317 if ((config = tls_config_new()) == NULL)
318 err(1, "tls_config_new");
319 tls_config_insecure_noverifycert(config);
320 tls_config_insecure_noverifyname(config);
322 if (cert && tls_config_set_keypair_file(config, cert, key) == -1)
323 errx(1, "cant load certificate client %s", cert);
325 if ((ctx = tls_client()) == NULL)
326 errx(1, "can't create tls context");
328 if (tls_configure(ctx, config) == -1)
329 errx(1, "tls_configure: %s", tls_error(ctx));
331 sock = dial(iri.host, iri.port);
333 /* drop inet tls */
334 if (pledge("stdio", NULL) == -1)
335 err(1, "pledge");
337 if (tls_connect_socket(ctx, sock, iri.host) == -1)
338 errx(1, "failed to connect to %s:%s: %s", iri.host,
339 *iri.port == '\0' ? "1965" : iri.port, tls_error(ctx));
341 /* send request */
342 if (iomux(ctx, sock, reqbuf, strlen(reqbuf), NULL, 0) == -1)
343 errx(1, "I/O error: %s", tls_error(ctx));
345 for (;;) {
346 static char buf[BUFSIZ];
347 size_t buflen;
348 ssize_t w;
349 char *m;
351 /* will be zero on EOF */
352 buflen = fread(buf, 1, sizeof(buf), in);
354 w = iomux(ctx, sock, buf, buflen, resbuf, sizeof(resbuf));
355 if (w == -1) {
356 errstr = tls_error(ctx);
357 if (errstr == NULL)
358 errstr = "unexpected EOF";
359 errx(1, "I/O error: %s", errstr);
361 if (w != 0) {
362 if ((m = memmem(resbuf, w, "\r\n", 2)) == NULL)
363 errx(1, "invalid reply");
364 *m = '\0';
365 ret = parse_response(resbuf);
366 break;
370 /* close connection */
371 for (;;) {
372 struct pollfd pfd;
374 memset(&pfd, 0, sizeof(pfd));
375 pfd.fd = sock;
376 pfd.events = POLLIN|POLLOUT;
378 switch (tls_close(ctx)) {
379 case TLS_WANT_POLLIN:
380 case TLS_WANT_POLLOUT:
381 if (poll(&pfd, 1, INFTIM) == -1)
382 err(1, "poll");
383 break;
384 case -1:
385 warnx("tls_close: %s", tls_error(ctx));
386 /* fallthrough */
387 default:
388 tls_free(ctx);
389 return (ret);