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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
|
#include <sys/socket.h>
#include <sys/time.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <tls.h>
#include "arg.h"
#define READ_BUF_SIZ 16384
#ifndef __OpenBSD__
#define pledge(p1,p2) 0
#define unveil(p1,p2) 0
#endif
#ifndef TLS_CA_CERT_FILE
#define TLS_CA_CERT_FILE "/etc/ssl/cert.pem"
#endif
/* URI */
struct uri {
char proto[48]; /* scheme including ":" or "://" */
char userinfo[256]; /* username [:password] */
char host[256];
char port[6]; /* numeric port */
char path[1024];
char query[1024];
char fragment[1024];
};
char *argv0;
/* raw header(s) to add */
static const char *config_headers = "";
/* max response size in bytes, 0 is unlimited */
static size_t config_maxresponsesiz = 0;
/* time-out in seconds */
static long long config_timeout = 0;
/* legacy ciphers? */
static int config_legacy = 0;
/* parsed uri */
static struct uri u;
/* raw command-line argument */
static char *url;
/* TLS config */
static struct tls_config *tls_config;
void
sighandler(int signo)
{
if (signo == SIGALRM)
_exit(2);
}
int
parse_content_length(const char *s, size_t *length)
{
const char *p;
char *end;
long long l;
if (!(p = strcasestr(s, "\r\nContent-Length:")))
return -1;
p += sizeof("\r\nContent-Length:") - 1;
p += strspn(p, " \t");
if (!isdigit((unsigned char)*p))
return -1;
errno = 0;
l = strtoll(p, &end, 10);
if (errno || p == end || (*end != '\0' && *end != '\r') || l < 0)
return -1;
*length = l;
return 0;
}
int
uri_parse(const char *s, struct uri *u)
{
const char *p = s;
char *endptr;
size_t i;
long l;
u->proto[0] = u->userinfo[0] = u->host[0] = u->port[0] = '\0';
u->path[0] = u->query[0] = u->fragment[0] = '\0';
/* protocol-relative */
if (*p == '/' && *(p + 1) == '/') {
p += 2; /* skip "//" */
goto parseauth;
}
/* scheme / protocol part */
for (; isalpha((unsigned char)*p) || isdigit((unsigned char)*p) ||
*p == '+' || *p == '-' || *p == '.'; p++)
;
/* scheme, except if empty and starts with ":" then it is a path */
if (*p == ':' && p != s) {
if (*(p + 1) == '/' && *(p + 2) == '/')
p += 3; /* skip "://" */
else
p++; /* skip ":" */
if ((size_t)(p - s) >= sizeof(u->proto))
return -1; /* protocol too long */
memcpy(u->proto, s, p - s);
u->proto[p - s] = '\0';
if (*(p - 1) != '/')
goto parsepath;
} else {
p = s; /* no scheme format, reset to start */
goto parsepath;
}
parseauth:
/* userinfo (username:password) */
i = strcspn(p, "@/?#");
if (p[i] == '@') {
if (i >= sizeof(u->userinfo))
return -1; /* userinfo too long */
memcpy(u->userinfo, p, i);
u->userinfo[i] = '\0';
p += i + 1;
}
/* IPv6 address */
if (*p == '[') {
/* bracket not found, host too short or too long */
i = strcspn(p, "]");
if (p[i] != ']' || i < 3)
return -1;
i++; /* including "]" */
} else {
/* domain / host part, skip until port, path or end. */
i = strcspn(p, ":/?#");
}
if (i >= sizeof(u->host))
return -1; /* host too long */
memcpy(u->host, p, i);
u->host[i] = '\0';
p += i;
/* port */
if (*p == ':') {
p++;
if ((i = strcspn(p, "/?#")) >= sizeof(u->port))
return -1; /* port too long */
memcpy(u->port, p, i);
u->port[i] = '\0';
/* check for valid port: range 1 - 65535, may be empty */
errno = 0;
l = strtol(u->port, &endptr, 10);
if (i && (errno || *endptr || l <= 0 || l > 65535))
return -1;
p += i;
}
parsepath:
/* path */
if ((i = strcspn(p, "?#")) >= sizeof(u->path))
return -1; /* path too long */
memcpy(u->path, p, i);
u->path[i] = '\0';
p += i;
/* query */
if (*p == '?') {
p++;
if ((i = strcspn(p, "#")) >= sizeof(u->query))
return -1; /* query too long */
memcpy(u->query, p, i);
u->query[i] = '\0';
p += i;
}
/* fragment */
if (*p == '#') {
p++;
if ((i = strlen(p)) >= sizeof(u->fragment))
return -1; /* fragment too long */
memcpy(u->fragment, p, i);
u->fragment[i] = '\0';
}
return 0;
}
int
edial(const char *host, const char *port)
{
struct addrinfo hints, *res, *res0;
int error, save_errno, s;
const char *cause = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV; /* numeric port only */
if ((error = getaddrinfo(host, port, &hints, &res0)))
errx(1, "%s: %s: %s:%s", __func__, gai_strerror(error), host, port);
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;
}
break;
}
if (s == -1)
errx(1, "%s: %s: %s:%s", __func__, cause, host, port);
freeaddrinfo(res0);
return s;
}
int
https_request(void)
{
struct tls *t;
char buf[READ_BUF_SIZ], *p;
const char *errstr;
size_t bodylen, expectedlen, n, len;
ssize_t r;
int cs, fd = -1, httpok = 0, ret = 1, stdport;
if (pledge("stdio dns inet rpath unveil", NULL) == -1)
err(1, "pledge");
if (unveil(TLS_CA_CERT_FILE, "r") == -1)
err(1, "unveil: %s", TLS_CA_CERT_FILE);
if (unveil(NULL, NULL) == -1)
err(1, "unveil");
if (!(t = tls_client())) {
fprintf(stderr, "tls_client: %s\n", tls_error(t));
goto err;
}
if (tls_configure(t, tls_config)) {
fprintf(stderr, "tls_configure: %s\n", tls_error(t));
goto err;
}
fd = edial(u.host, u.port);
if (tls_connect_socket(t, fd, u.host) == -1)
errx(1, "tls_connect: %s", tls_error(t));
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
stdport = u.port[0] == '\0' || strcmp(u.port, "443") == 0;
/* create and send HTTP header */
r = snprintf(buf, sizeof(buf),
"GET %s%s%s HTTP/1.0\r\n"
"Host: %s%s%s\r\n"
"Connection: close\r\n"
"%s%s"
"\r\n",
u.path[0] ? u.path : "/",
u.query[0] ? "?" : "", u.query,
u.host,
stdport ? "" : ":",
stdport ? "" : u.port,
config_headers, config_headers[0] ? "\r\n" : "");
if (r < 0 || (size_t)r >= sizeof(buf)) {
fprintf(stderr, "not writing header because it is truncated");
goto err;
}
for (len = r, p = buf; len > 0; ) {
r = tls_write(t, p, len);
if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
continue;
} else if (r == -1) {
fprintf(stderr, "tls_write: %s\n", tls_error(t));
goto err;
}
p += r;
len -= r;
}
/* NOTE: HTTP header must fit in the buffer */
for (len = 0; len < sizeof(buf);) {
/* NOTE: buffer size is -1 to NUL terminate the buffer for a
string comparison. */
r = tls_read(t, &buf[len], sizeof(buf) - len - 1);
if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
continue;
} else if (r == 0) {
break;
} else if (r == -1) {
errstr = tls_error(t);
fprintf(stderr, "tls_read: %s\n", errstr ? errstr : "");
goto err;
}
len += r;
}
buf[len] = '\0';
if (!strncmp(buf, "HTTP/1.0 200 ", sizeof("HTTP/1.0 200 ") - 1) ||
!strncmp(buf, "HTTP/1.1 200 ", sizeof("HTTP/1.1 200 ") - 1))
httpok = 1;
if (!(p = strstr(buf, "\r\n\r\n"))) {
fprintf(stderr, "no HTTP header found or header too big\n");
goto err;
}
*p = '\0'; /* NUL terminate header part */
cs = parse_content_length(buf, &expectedlen);
p += strlen("\r\n\r\n");
bodylen = len - (p - buf); /* (partial) body after header */
if (httpok) {
n = len - (p - buf);
r = fwrite(p, 1, n, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
goto err;
}
} else {
/* if not 200 OK print header */
fputs(buf, stderr);
fputs("\r\n\r\n", stderr);
/* NOTE: we are nice and keep reading (not closing) until the server is done. */
}
while (1) {
r = tls_read(t, &buf, sizeof(buf));
if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
continue;
} else if (r == 0) {
break;
} else if (r == -1) {
errstr = tls_error(t);
fprintf(stderr, "tls_read: %s\n", errstr ? errstr : "");
goto err;
}
len += r;
bodylen += r;
if (httpok) {
r = fwrite(buf, 1, r, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
goto err;
}
}
if (config_maxresponsesiz && len >= config_maxresponsesiz)
break;
}
if (config_maxresponsesiz && len >= config_maxresponsesiz) {
fprintf(stderr, "response too big: %zu >= %zu\n",
len, config_maxresponsesiz);
goto err;
}
if (cs != -1 && expectedlen != bodylen) {
fprintf(stderr, "Content-Length mismatch: %zu expected != %zu received\n",
expectedlen, bodylen);
goto err;
}
ret = 0;
err:
if (t) {
tls_close(t);
tls_free(t);
}
return httpok ? ret : 2;
}
int
http_request(void)
{
char buf[READ_BUF_SIZ], *p;
size_t bodylen, expectedlen, n, len;
ssize_t r;
int cs, fd = -1, httpok = 0, ret = 1, stdport;
if (pledge("stdio dns inet", NULL) == -1)
err(1, "pledge");
fd = edial(u.host, u.port);
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
stdport = u.port[0] == '\0' || strcmp(u.port, "80") == 0;
/* create and send HTTP header */
r = snprintf(buf, sizeof(buf),
"GET %s%s%s HTTP/1.0\r\n"
"Host: %s%s%s\r\n"
"Connection: close\r\n"
"%s%s"
"\r\n",
u.path[0] ? u.path : "/",
u.query[0] ? "?" : "", u.query,
u.host,
stdport ? "" : ":",
stdport ? "" : u.port,
config_headers, config_headers[0] ? "\r\n" : "");
if (r < 0 || (size_t)r >= sizeof(buf)) {
fprintf(stderr, "not writing header because it is truncated");
goto err;
}
if ((r = write(fd, buf, r)) == -1) {
fprintf(stderr, "write: %s\n", strerror(errno));
goto err;
}
/* NOTE: HTTP header must fit in the buffer */
for (len = 0; len < sizeof(buf); len += r) {
/* NOTE: buffer size is -1 to NUL terminate the buffer for a
string comparison. */
if ((r = read(fd, &buf[len], sizeof(buf) - len - 1)) == 0)
break;
if (r == -1) {
fprintf(stderr, "read: %s\n", strerror(errno));
goto err;
}
}
buf[len] = '\0';
if (!strncmp(buf, "HTTP/1.0 200 ", sizeof("HTTP/1.0 200 ") - 1) ||
!strncmp(buf, "HTTP/1.1 200 ", sizeof("HTTP/1.1 200 ") - 1))
httpok = 1;
if (!(p = strstr(buf, "\r\n\r\n"))) {
fprintf(stderr, "no HTTP header found or header too big\n");
goto err;
}
*p = '\0'; /* NUL terminate header part */
cs = parse_content_length(buf, &expectedlen);
p += strlen("\r\n\r\n");
bodylen = len - (p - buf); /* (partial) body after header */
if (httpok) {
n = len - (p - buf);
r = fwrite(p, 1, n, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
goto err;
}
} else {
/* if not 200 OK print header */
fputs(buf, stderr);
fputs("\r\n\r\n", stderr);
/* NOTE: we are nice and keep reading (not closing) until the server is done. */
}
while (1) {
r = read(fd, &buf, sizeof(buf));
if (r == 0)
break;
if (r == -1) {
fprintf(stderr, "read: %s\n", strerror(errno));
goto err;
}
len += r;
bodylen += r;
if (httpok) {
r = fwrite(buf, 1, r, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
goto err;
}
}
if (config_maxresponsesiz && len >= config_maxresponsesiz)
break;
}
if (config_maxresponsesiz && len >= config_maxresponsesiz) {
fprintf(stderr, "response too big: %zu >= %zu\n",
len, config_maxresponsesiz);
goto err;
}
if (cs != -1 && expectedlen != bodylen) {
fprintf(stderr, "Content-Length mismatch: %zu expected != %zu received\n",
expectedlen, bodylen);
goto err;
}
ret = 0;
err:
if (fd != -1)
close(fd);
return httpok ? ret : 2;
}
int
gopher_request(void)
{
char buf[READ_BUF_SIZ];
const char *path;
size_t len = 0;
ssize_t r;
int fd = -1, ret = 1;
if (pledge("stdio dns inet", NULL) == -1)
err(1, "pledge");
fd = edial(u.host, u.port);
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
/* create and send path, skip type part, empty path is allowed,
see RFC 4266 The gopher URI Scheme - section 2.1 */
path = u.path;
if (*path == '/') {
path++;
if (*path)
path++; /* skip type */
}
r = snprintf(buf, sizeof(buf), "%s%s%s\r\n",
path, u.query[0] ? "?" : "", u.query);
if (r < 0 || (size_t)r >= sizeof(buf)) {
fprintf(stderr, "not writing header because it is truncated");
goto err;
}
if ((r = write(fd, buf, strlen(buf))) == -1) {
fprintf(stderr, "write: %s\n", strerror(errno));
goto err;
}
while (1) {
r = read(fd, &buf, sizeof(buf));
if (r == 0) {
break;
} else if (r == -1) {
fprintf(stderr, "read: %s\n", strerror(errno));
goto err;
}
len += r;
r = fwrite(buf, 1, r, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
goto err;
}
if (config_maxresponsesiz && len >= config_maxresponsesiz)
break;
}
if (config_maxresponsesiz && len >= config_maxresponsesiz) {
fprintf(stderr, "response too big: %zu >= %zu\n",
len, config_maxresponsesiz);
goto err;
}
ret = 0;
err:
if (fd != -1)
close(fd);
return ret;
}
int
gophers_request(void)
{
struct tls *t;
char buf[READ_BUF_SIZ], *p;
const char *errstr, *path;
size_t len = 0;
ssize_t r;
int fd = -1, ret = 1;
if (pledge("stdio dns inet rpath unveil", NULL) == -1)
err(1, "pledge");
if (unveil(TLS_CA_CERT_FILE, "r") == -1)
err(1, "unveil: %s", TLS_CA_CERT_FILE);
if (unveil(NULL, NULL) == -1)
err(1, "unveil");
if (!(t = tls_client())) {
errstr = tls_error(t);
fprintf(stderr, "tls_client: %s\n", errstr ? errstr : "");
goto err;
}
if (tls_configure(t, tls_config)) {
errstr = tls_error(t);
fprintf(stderr, "tls_configure: %s\n", errstr ? errstr : "");
goto err;
}
fd = edial(u.host, u.port);
if (tls_connect_socket(t, fd, u.host) == -1)
errx(1, "tls_connect: %s", tls_error(t));
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
/* create and send path, skip type part, empty path is allowed,
see RFC 4266 The gopher URI Scheme - section 2.1 */
path = u.path;
if (*path == '/') {
path++;
if (*path)
path++; /* skip type */
}
r = snprintf(buf, sizeof(buf), "%s%s%s\r\n",
path, u.query[0] ? "?" : "", u.query);
if (r < 0 || (size_t)r >= sizeof(buf)) {
fprintf(stderr, "not writing header because it is truncated");
goto err;
}
for (len = r, p = buf; len > 0; ) {
r = tls_write(t, p, len);
if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
continue;
} else if (r == -1) {
fprintf(stderr, "tls_write: %s\n", tls_error(t));
goto err;
}
p += r;
len -= r;
}
while (1) {
r = tls_read(t, &buf, sizeof(buf));
if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
continue;
} else if (r == 0) {
break;
} else if (r == -1) {
errstr = tls_error(t);
fprintf(stderr, "tls_read: %s\n", errstr ? errstr : "");
goto err;
}
len += r;
r = fwrite(buf, 1, r, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
goto err;
}
if (config_maxresponsesiz && len >= config_maxresponsesiz)
break;
}
if (config_maxresponsesiz && len >= config_maxresponsesiz) {
fprintf(stderr, "response too big: %zu >= %zu\n",
len, config_maxresponsesiz);
goto err;
}
ret = 0;
err:
if (t) {
tls_close(t);
tls_free(t);
}
if (fd != -1)
close(fd);
return ret;
}
int
gemini_request(void)
{
struct tls *t;
char buf[READ_BUF_SIZ], *p;
const char *errstr, *path;
size_t len = 0;
ssize_t r;
int fd = -1, ret = 1, geminiok = 0;
if (pledge("stdio dns inet rpath unveil", NULL) == -1)
err(1, "pledge");
if (!(t = tls_client())) {
errstr = tls_error(t);
fprintf(stderr, "tls_client: %s\n", errstr ? errstr : "");
goto err;
}
if (tls_configure(t, tls_config)) {
errstr = tls_error(t);
fprintf(stderr, "tls_configure: %s\n", errstr ? errstr : "");
goto err;
}
tls_config_insecure_noverifycert(tls_config);
tls_config_insecure_noverifyname(tls_config);
fd = edial(u.host, u.port);
if (tls_connect_socket(t, fd, u.host) == -1)
errx(1, "tls_connect: %s", tls_error(t));
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
r = snprintf(buf, sizeof(buf), "%s%s%s%s%s\r\n",
u.proto,
u.host,
u.path[0] ? u.path : "/",
u.query[0] ? "?" : "", u.query);
if (r < 0 || (size_t)r >= sizeof(buf)) {
fprintf(stderr, "not writing header because it is truncated");
goto err;
}
for (len = r, p = buf; len > 0; ) {
r = tls_write(t, p, len);
if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
continue;
} else if (r == -1) {
fprintf(stderr, "tls_write: %s\n", tls_error(t));
goto err;
}
p += r;
len -= r;
}
for (len = 0; len < sizeof(buf);) {
/* NOTE: buffer size is -1 to NUL terminate the buffer for a
string comparison. */
r = tls_read(t, &buf[len], sizeof(buf) - len - 1);
if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
continue;
} else if (r == 0) {
break;
} else if (r == -1) {
errstr = tls_error(t);
fprintf(stderr, "tls_read: %s\n", errstr ? errstr : "");
goto err;
}
len += r;
}
buf[len] = '\0';
if (!strncmp(buf, "20", sizeof("20") - 1))
geminiok = 1;
if (!(p = strstr(buf, "\r\n"))) {
fprintf(stderr, "no gemini header found or header too big\n");
goto err;
}
*p = '\0'; /* NUL terminate header part */
//cs = parse_content_length(buf, &expectedlen);
p += strlen("\r\n");
//bodylen = len - (p - buf); /* (partial) body after header */
if (geminiok) {
int n = len - (p - buf);
r = fwrite(p, 1, n, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
goto err;
}
} else {
/* if not 20 print header */
fputs(buf, stderr);
fputs("\r\n", stderr);
/* NOTE: we are nice and keep reading (not closing) until the server is done. */
}
while (1) {
r = tls_read(t, &buf, sizeof(buf));
if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
continue;
} else if (r == 0) {
break;
} else if (r == -1) {
errstr = tls_error(t);
fprintf(stderr, "tls_read: %s\n", errstr ? errstr : "");
goto err;
}
len += r;
if (geminiok) {
r = fwrite(buf, 1, r, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
goto err;
}
}
if (config_maxresponsesiz && len >= config_maxresponsesiz)
break;
}
if (config_maxresponsesiz && len >= config_maxresponsesiz) {
fprintf(stderr, "response too big: %zu >= %zu\n",
len, config_maxresponsesiz);
goto err;
}
ret = 0;
err:
if (t) {
tls_close(t);
tls_free(t);
}
if (fd != -1)
close(fd);
return geminiok ? ret : 2;
}
void
usage(void)
{
fprintf(stderr, "usage: %s [-H headers] [-l] [-m maxresponse] "
"[-t timeout] url\n", argv0);
exit(1);
}
int
main(int argc, char **argv)
{
char *end;
int statuscode;
long long l;
ARGBEGIN {
case 'H': /* header(s) */
config_headers = EARGF(usage());
break;
case 'l': /* legacy ciphers */
config_legacy = 1;
break;
case 'm': /* max filesize */
errno = 0;
l = strtoll(EARGF(usage()), &end, 10);
if (errno || *end != '\0' || l < 0)
usage();
config_maxresponsesiz = l;
break;
case 't': /* timeout */
errno = 0;
l = strtoll(EARGF(usage()), &end, 10);
if (errno || *end != '\0' || l < 0 || l >= 65535)
usage();
config_timeout = l;
break;
default:
usage();
} ARGEND
if (argc != 1)
usage();
url = argv[0];
if (uri_parse(url, &u) == -1)
errx(1, "invalid URL: %s", url);
if (u.userinfo[0])
errx(1, "userinfo field not supported in the URL: %s", url);
if (config_timeout > 0) {
signal(SIGALRM, sighandler);
if (alarm(config_timeout) == -1)
err(1, "alarm");
}
if (!strcmp(u.proto, "https://")) {
if (tls_init())
errx(1, "tls_init failed");
if (!(tls_config = tls_config_new()))
errx(1, "tls config failed");
if (config_legacy) {
/* enable legacy cipher and negotiation. */
if (tls_config_set_ciphers(tls_config, "legacy"))
errx(1, "tls set ciphers failed: %s",
tls_config_error(tls_config));
}
if (!u.port[0])
memcpy(u.port, "443", 4);
statuscode = https_request();
} else if (!strcmp(u.proto, "http://")) {
if (!u.port[0])
memcpy(u.port, "80", 3);
statuscode = http_request();
} else if (!strcmp(u.proto, "gopher://")) {
if (!u.port[0])
memcpy(u.port, "70", 3);
statuscode = gopher_request();
} else if (!strcmp(u.proto, "gophers://")) {
if (tls_init())
errx(1, "tls_init failed");
if (!(tls_config = tls_config_new()))
errx(1, "tls config failed");
if (config_legacy) {
/* enable legacy cipher and negotiation. */
if (tls_config_set_ciphers(tls_config, "legacy"))
errx(1, "tls set ciphers failed: %s",
tls_config_error(tls_config));
}
if (!u.port[0])
memcpy(u.port, "70", 3);
statuscode = gophers_request();
} else if (!strcmp(u.proto, "gemini://")) {
if (tls_init())
errx(1, "tls_init failed");
if (!(tls_config = tls_config_new()))
errx(1, "tls config failed");
if (config_legacy) {
/* enable legacy cipher and negotiation. */
if (tls_config_set_ciphers(tls_config, "legacy"))
errx(1, "tls set ciphers failed: %s",
tls_config_error(tls_config));
}
if (!u.port[0])
memcpy(u.port, "1965", 5);
statuscode = gemini_request();
} else {
if (u.proto[0])
errx(1, "unsupported protocol specified: %s", u.proto);
else
errx(1, "no protocol specified");
}
return statuscode;
}
|