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