forked from marinosi/fetch_c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_sandbox.c
1385 lines (1175 loc) · 35 KB
/
fetch_sandbox.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
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2013 Ilias Marinos
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <err.h>
#include <netdb.h>
#ifndef NO_SANDBOX
#include <sandbox.h>
/*#include <sandbox_rpc.h>*/
#include <sys/capability.h>
#endif
#include <fetch.h>
#include "fetch_internal.h"
/* DPRINTF */
#ifndef NDEBUG
#ifdef DEBUG
#define DPRINTF(format, ...) \
fprintf(stderr, "[%s-%d] (%s:%d) " format "\n", \
(fscb == NULL || fscb->sandbox_pid == 0) ? "SANDBOX" : "HOST", getpid(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif
#else
#define DPRINTF(...)
#endif
#define MMIN(a, b) ((a) < (b) ? (a) : (b))
/* Operations */
#define NO_OP 0
#define PROXIED_FETCH 1
#define PROXIED_FETCH_PARSE_URL 2
#define HOST_REP_FETCHCONN 3
#define HOST_REP_OUTF 4
#define HOST_REP_STAT_IMS 5
#define HOST_REP_STAT_RESTART 6
#define HOST_REP_UTIMES 7
#define HOST_REP_MKSTEMPS 8
#define HOST_REP_RENAME 9
#define HOST_REP_SYMLINK 10
#define HOST_REP_UNLINK 11
#define HOST_REP_GETSERV 12
#define TERMINATE_SANDBOX 13
#define SANDBOX_FINISHED 100
#define SANDBOX_REQ_FETCHCONN 101
#define SANDBOX_REQ_OUTF 102
#define SANDBOX_REQ_STAT_IMS 103
#define SANDBOX_REQ_STAT_RESTART 104
#define SANDBOX_REQ_UTIMES 105
#define SANDBOX_REQ_MKSTEMPS 106
#define SANDBOX_REQ_RENAME 107
#define SANDBOX_REQ_SYMLINK 108
#define SANDBOX_REQ_UNLINK 109
#define SANDBOX_REQ_GETSERVBYNAME 110
#ifndef NO_SANDBOX
/* fetch sandbox control block */
struct sandbox_cb *fscb;
struct fetch_req {
char hf_req_url[URL_MAX];
char hf_req_path[URL_MAX];
char hf_req_i_filename[PATH_MAX]; /* name of input file */
int v_level; /* -v: verbosity level */
int family; /* -[46]: address family to use */
int d_flag; /* -d: direct connection */
int A_flag; /* -A: do not follow 302 redirects */
int i_flag; /* -i: specify input file for mtime comparison */
int s_flag; /* -s: show size, don't fetch */
int o_stdout; /* output file is stdout */
int r_flag; /* -r: restart previously interrupted transfer */
off_t S_size; /* -S: require size to match */
int l_flag; /* -l: link rather than copy file: URLs */
int F_flag; /* -F: restart without checking mtime */
int R_flag; /* -R: don't delete partially transferred files */
int m_flag; /* -[Mm]: mirror mode */
off_t B_size; /* -B: buffer size */
long ftp_timeout; /* default timeout for FTP transfers */
long http_timeout; /* default timeout for HTTP transfers */
} __packed;
struct fetch_rep {
off_t hf_rep_retval;
} __packed;
struct fetch_parse_url_req {
char url_s[URL_MAX];
} __packed;
struct fetch_parse_url_rep {
char scheme[URL_SCHEMELEN+1];
char user[URL_USERLEN+1];
char pwd[URL_PWDLEN+1];
char host[MAXHOSTNAMELEN+1];
int port;
off_t offset;
size_t length;
time_t ims_time;
char doc[URL_MAX];
int ret;
} __packed;
struct fetchconn_req {
char host[MAXHOSTNAMELEN+1];
int port;
int af;
int verbose;
} __packed;
struct fetchconn_rep {
int ref;
} __packed;
struct getserv_req {
char name[URL_SCHEMELEN+1];
char proto[URL_SCHEMELEN+1];
} __packed;
struct getserv_rep {
struct servent se;
} __packed;
struct outf_req {
char fpath[256];
char mode[8];
} __packed;
struct outf_rep {
int ret;
} __packed;
struct stat_req {
char fpath[256];
} __packed;
struct stat_rep {
int ret;
struct stat s;
int stat_errno;
} __packed;
struct utimes_req {
char fname[256];
struct timeval times[2];
} __packed;
struct utimes_rep {
int ret;
} __packed;
struct mkstemps_req {
char template[256];
int suffixlen;
} __packed;
struct mkstemps_rep {
int ret;
char template[256];
} __packed;
struct rename_req {
char from[256];
char to[256];
} __packed;
struct rename_rep {
int ret;
} __packed;
struct symlink_req {
char target[256];
char linkpath[256];
} __packed;
struct symlink_rep {
int ret;
} __packed;
struct unlink_req {
char path[256];
} __packed;
struct unlink_rep {
int ret;
} __packed;
static void fsandbox(void);
void
fetch_sandbox_init(void)
{
DPRINTF("Creating sandbox");
fscb = calloc(1, sizeof(struct sandbox_cb));
if(!fscb) {
DPRINTF("[XXX] fscb wasn't initialized!");
exit(-1);
}
sandbox_create(fscb, &fsandbox);
}
void
fetch_sandbox_wait(void)
{
DPRINTF("Sending TERMINATE_SANDBOX message");
uint32_t seqno;
if (host_sendrpc(fscb, TERMINATE_SANDBOX, seqno, NULL, 0, NULL, 0) < 0)
err(-1, "host_sendrpc");
wait(&rv);
DPRINTF("Sandbox's exit status is %d", WEXITSTATUS(rv));
}
/* Called in parent to proxy the request though the sandbox */
static off_t
fetch_insandbox(char *origurl, const char *origpath)
{
struct fetch_req req;
struct fetch_rep rep;
struct iovec iov_req, iov_rep;
size_t len;
uint32_t seqno, opno;
u_char *buffer;
/* Clear out req */
bzero(&req, sizeof(req));
/* Pass needed data */
strlcpy(req.hf_req_url, origurl, sizeof(req.hf_req_url));
strlcpy(req.hf_req_path, origpath, sizeof(req.hf_req_url));
if (i_flag)
strlcpy(req.hf_req_i_filename, i_filename,
MMIN(sizeof(req.hf_req_i_filename), strlen(i_filename)+1));
req.v_level = v_level;
req.d_flag = d_flag;
req.A_flag = A_flag;
req.i_flag = i_flag;
req.s_flag = s_flag;
req.o_stdout = o_stdout;
req.r_flag = r_flag;
req.S_size = S_size;
req.l_flag = l_flag;
req.F_flag = F_flag;
req.R_flag = R_flag;
req.m_flag = m_flag;
req.B_size = B_size;
req.http_timeout = http_timeout;
req.ftp_timeout = ftp_timeout;
iov_req.iov_base = &req;
iov_req.iov_len = sizeof(req);
#if 0
if (host_rpc(fscb, PROXIED_FETCH, &iov_req, 1, &iov_rep, 1, &len) < 0)
err(-1, "host_rpc");
if (len != sizeof(rep))
errx(-1, "host_rpc");
#endif
DPRINTF("Proxying fetch() call to sandbox");
if (host_sendrpc(fscb, PROXIED_FETCH, seqno, &iov_req, 1, NULL, 0) < 0)
err(-1, "host_sendrpc");
struct fetchconn *fconn;
struct fetchconn_req fcreq;
struct fetchconn_rep fcrep;
struct servent *se;
struct getserv_req gsreq;
struct getserv_rep gsrep;
struct outf_req ofreq;
struct outf_rep ofrep;
FILE *ofstream;
int ofd;
struct stat_req streq;
struct stat_rep strep;
struct stat s;
struct utimes_req ureq;
struct utimes_rep urep;
struct mkstemps_req mkreq;
struct mkstemps_rep mkrep;
struct rename_req rnreq;
struct rename_rep rnrep;
struct symlink_req sreq;
struct symlink_rep srep;
struct unlink_req unreq;
struct unlink_rep unrep;
for (;;) {
if (host_recvrpc(fscb, &opno, &seqno, &buffer, &len) < 0) {
if (errno == EPIPE) {
DPRINTF("[XXX] EPIPE");
exit(-1);
} else {
DPRINTF("[XXX] sandbox_recvrpc");
err(-1, "sandbox_recvrpc");
}
}
switch(opno) {
case SANDBOX_REQ_FETCHCONN: {
DPRINTF("SANDBOX_REQ_FETCHCONN");
if(len != sizeof(struct fetchconn_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&fcreq, buffer, len);
free(buffer);
/* Let's get the actual conn */
DPRINTF("Host: %s\nPort: %d", fcreq.host, fcreq.port);
fconn = fetch_connect((const char *)fcreq.host, fcreq.port,
fcreq.af, fcreq.verbose);
/* Ops */
if (!fconn) {
DPRINTF("Failed to get fconn");
errx(-1, "fetch_connect()");
}
/* Send back to the sandbox what is needed */
fcrep.ref = fconn->ref;
iov_req.iov_base = &fcrep;
iov_req.iov_len = sizeof(fcrep);
if (host_rpc_rights(fscb, HOST_REP_FETCHCONN, &iov_req, 1, &fconn->sd, 1,
NULL, 0, NULL, NULL, NULL) < 0)
err(-1, "host_rpc");
#if 0
if (host_send_rights(fscb, (void *) &fcrep, sizeof(fcrep), 0, &fconn->sd, 1) < 0)
err(-1, "host_send_rights");
if (host_recvrpc(fscb, &opno, &seqno, &buffer, &len) < 0) {
if (errno == EPIPE) {
DPRINTF("[XXX] EPIPE");
exit(-1);
} else {
DPRINTF("[XXX] sandbox_recvrpc");
err(-1, "sandbox_recvrpc");
}
}
DPRINTF("OK - B");
if(len != sizeof(rep)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&rep, buffer, len);
free(buffer);
#endif
break;
}
case SANDBOX_REQ_GETSERVBYNAME: {
DPRINTF("SANDBOX_REQ_GETSERVBYNAME");
if(len != sizeof(struct getserv_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&gsreq, buffer, len);
free(buffer);
/* Let's get the actual conn */
DPRINTF("Name: %s Proto: %s", gsreq.name, gsreq.proto);
se = getservbyname(gsreq.name, gsreq.proto);
/* Ops */
if (!se) {
DPRINTF("Failed to get servent");
errx(-1, "getservbyname()");
}
DPRINTF("Got servent");
/* Send back to the sandbox what is needed */
memmove(&gsrep.se, se, sizeof(struct servent));
bzero(se, sizeof(struct servent));
iov_req.iov_base = &gsrep;
iov_req.iov_len = sizeof(gsrep);
if (host_rpc(fscb, HOST_REP_GETSERV, &iov_req, 1, NULL, 0, NULL) < 0)
err(-1, "host_rpc");
break;
}
case SANDBOX_REQ_OUTF: {
DPRINTF("SANDBOX_REQ_OUTF");
if(len != sizeof(struct outf_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&ofreq, buffer, len);
free(buffer);
ofstream = fopen(ofreq.fpath, ofreq.mode);
if(!ofstream) {
ofrep.ret = -1;
DPRINTF("We don't handle failure yet");
exit(-1);
}
/* Send back to the sandbox what is needed */
ofrep.ret = 0;
iov_req.iov_base = &ofrep;
iov_req.iov_len = sizeof(ofrep);
ofd = fileno(ofstream);
if (host_rpc_rights(fscb, HOST_REP_OUTF, &iov_req, 1,
&ofd, 1, NULL, 0, NULL, NULL, NULL) < 0)
err(-1, "host_rpc");
DPRINTF("Output file descriptor sent");
#if 0
if (host_recvrpc(fscb, &opno, &seqno, &buffer, &len) < 0) {
if (errno == EPIPE) {
DPRINTF("[XXX] EPIPE");
exit(-1);
} else {
DPRINTF("[XXX] sandbox_recvrpc");
err(-1, "sandbox_recvrpc");
}
}
if(len != sizeof(rep)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&rep, buffer, len);
free(buffer);
DPRINTF("Got the final response");
goto out;
#endif
break;
}
case SANDBOX_REQ_STAT_IMS: {
DPRINTF("SANDBOX_REQ_STAT_IMS");
if(len != sizeof(struct stat_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&streq, buffer, len);
free(buffer);
/* Send back to the sandbox what is needed */
strep.ret = stat(streq.fpath, &s);
strep.stat_errno = errno;
memmove(&strep.s, &s, sizeof(struct stat));
bzero(&s, sizeof(struct stat));
iov_req.iov_base = &strep;
iov_req.iov_len = sizeof(strep);
if (host_rpc(fscb, HOST_REP_STAT_IMS, &iov_req, 1, NULL, 0, NULL) < 0)
err(-1, "host_rpc");
break;
}
case SANDBOX_REQ_STAT_RESTART: {
DPRINTF("SANDBOX_REQ_STAT_RESTART");
if(len != sizeof(struct stat_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&streq, buffer, len);
free(buffer);
/* Send back to the sandbox what is needed */
DPRINTF("stat(\"%s\")", streq.fpath);
strep.ret = stat(streq.fpath, &s);
strep.stat_errno = errno;
DPRINTF("stat returned %d", strep.ret);
memmove(&strep.s, &s, sizeof(struct stat));
bzero(&s, sizeof(struct stat));
iov_req.iov_base = &strep;
iov_req.iov_len = sizeof(strep);
DPRINTF("Size of strep msg: %lu", sizeof(strep));
DPRINTF("Size of struct stat_rep: %lu", sizeof(struct stat_rep));
if (host_rpc(fscb, HOST_REP_STAT_RESTART, &iov_req, 1, NULL, 0, NULL) < 0)
err(-1, "host_rpc");
break;
}
case SANDBOX_REQ_UTIMES: {
DPRINTF("SANDBOX_REQ_UTIMES");
if(len != sizeof(struct utimes_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&ureq, buffer, len);
free(buffer);
/* Send back to the sandbox what is needed */
DPRINTF("utimes(\"%s\")", ureq.fname);
DPRINTF("sizeof(ureq.times): %lu", sizeof(ureq.times));
urep.ret = utimes(ureq.fname, ureq.times);
DPRINTF("utimes returned %d", urep.ret);
iov_req.iov_base = &urep;
iov_req.iov_len = sizeof(urep);
if (host_rpc(fscb, HOST_REP_UTIMES, &iov_req, 1, NULL, 0, NULL) < 0)
err(-1, "host_rpc");
break;
}
case SANDBOX_REQ_MKSTEMPS: {
DPRINTF("SANDBOX_REQ_MKSTEMPS");
if(len != sizeof(struct mkstemps_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&mkreq, buffer, len);
free(buffer);
DPRINTF("mkreq.template before call: %s", mkreq.template);
mkrep.ret = mkstemps(mkreq.template, mkreq.suffixlen);
strlcpy(mkrep.template, mkreq.template, strlen(mkreq.template)+1);
DPRINTF("mkreq.template after call: %s", mkreq.template);
/* Send back to the sandbox what is needed */
iov_req.iov_base = &mkrep;
iov_req.iov_len = sizeof(mkrep);
if (host_rpc_rights(fscb, HOST_REP_MKSTEMPS, &iov_req, 1,
&mkrep.ret, 1, NULL, 0, NULL, NULL, NULL) < 0)
err(-1, "host_rpc");
DPRINTF("file descriptor sent");
break;
}
case SANDBOX_REQ_RENAME: {
DPRINTF("SANDBOX_REQ_RENAME");
if(len != sizeof(struct rename_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&rnreq, buffer, len);
free(buffer);
/* Send back to the sandbox what is needed */
DPRINTF("rename(\"%s\",\"%s\")", rnreq.from, rnreq.to);
rnrep.ret = rename(rnreq.from, rnreq.to);
DPRINTF("return value: %d", rnrep.ret);
iov_req.iov_base = &rnrep;
iov_req.iov_len = sizeof(rnrep);
if (host_rpc(fscb, HOST_REP_RENAME, &iov_req, 1, NULL, 0, NULL) < 0)
err(-1, "host_rpc");
break;
}
case SANDBOX_REQ_SYMLINK: {
DPRINTF("SANDBOX_REQ_SYMLINK");
if(len != sizeof(struct symlink_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&sreq, buffer, len);
free(buffer);
/* Send back to the sandbox what is needed */
DPRINTF("symlink(\"%s\",\"%s\")", sreq.target, sreq.linkpath);
srep.ret = symlink(sreq.target, sreq.linkpath);
DPRINTF("return value: %d", srep.ret);
iov_req.iov_base = &srep;
iov_req.iov_len = sizeof(srep);
if (host_rpc(fscb, HOST_REP_SYMLINK, &iov_req, 1, NULL, 0, NULL) < 0)
err(-1, "host_rpc");
break;
}
case SANDBOX_REQ_UNLINK: {
DPRINTF("SANDBOX_REQ_UNLINK");
if(len != sizeof(struct unlink_req)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&unreq, buffer, len);
free(buffer);
/* Send back to the sandbox what is needed */
DPRINTF("unlink(\"%s\")", unreq.path);
unrep.ret = unlink(unreq.path);
DPRINTF("return value: %d", unrep.ret);
iov_req.iov_base = &unrep;
iov_req.iov_len = sizeof(unrep);
if (host_rpc(fscb, HOST_REP_UNLINK, &iov_req, 1, NULL, 0, NULL) < 0)
err(-1, "host_rpc");
break;
}
case SANDBOX_FINISHED: {
DPRINTF("SANDBOX_FINISHED");
if(len != sizeof(rep)) {
DPRINTF("Ouch receive size mismatch!");
exit(-1);
}
memmove(&rep, buffer, len);
free(buffer);
goto out;
}
default: {
DPRINTF("WTF");
}
}
}
out:
DPRINTF("Nice");
return (rep.hf_rep_retval);
}
/* Called in sandbox and wraps the actual fetch */
static void
sandbox_fetch(struct sandbox_cb *scb, uint32_t opno, uint32_t seqno, char
*buffer, size_t len)
{
struct fetch_req req;
struct fetch_rep rep;
struct iovec iov;
if (len != sizeof(req))
err(-1, "sandbox_fetch: len %zu", len);
/* Demangle data */
bcopy(buffer, &req, sizeof(req));
v_level = req.v_level;
d_flag = req.d_flag;
A_flag = req.A_flag;
i_flag = req.i_flag;
s_flag = req.s_flag;
o_stdout = req.o_stdout;
r_flag = req.r_flag;
S_size = req.S_size;
l_flag = req.l_flag;
F_flag = req.F_flag;
R_flag = req.R_flag;
m_flag = req.m_flag;
B_size = req.B_size;
http_timeout = http_timeout;
ftp_timeout = ftp_timeout;
i_filename = (i_flag ? req.hf_req_i_filename: NULL);
/* allocate buffer */
if (B_size < MINBUFSIZE)
B_size = MINBUFSIZE;
if ((buf = malloc(B_size)) == NULL)
errx(1, "%s", strerror(ENOMEM));
DPRINTF("Calling fetch");
rep.hf_rep_retval = fetch(req.hf_req_url, req.hf_req_path);
bzero(&rep, sizeof(rep));
iov.iov_base = &rep;
iov.iov_len = sizeof(rep);
DPRINTF("Sending retval %ld", rep.hf_rep_retval);
//if (sandbox_sendrpc(scb, opno, seqno, &iov, 1) < 0)
DPRINTF("SANDBOX_FINISHED");
if (sandbox_sendrpc(scb, SANDBOX_FINISHED, seqno, &iov, 1) < 0)
err(-1, "sandbox_sendrpc");
}
/* called in sandboxed process */
conn_t *
fetch_connect_inparent(const char *host, int port, int af, int verbose)
{
conn_t *conn;
struct fetchconn_req fcreq;
struct fetchconn_rep fcrep;
uint32_t seqno = 0;
struct iovec iov_req, iov_rep;
int fdarray[1], fdcount; /* We expect a fd for SSL_INIT op */
int *fdp;
uint32_t opno;
u_char *buffer;
size_t len;
bzero(&fcreq, sizeof(struct fetchconn_req));
bzero(&fcrep, sizeof(struct fetchconn_rep));
strlcpy(fcreq.host, host, MMIN(strlen(host) + 1, MAXHOSTNAMELEN));
fcreq.port = port;
fcreq.af = af;
fcreq.verbose = verbose;
/*bzero(&iov_req, sizeof(struct iovec));*/
/*bzero(&iov_rep, sizeof(struct iovec));*/
iov_req.iov_base = &fcreq;
iov_req.iov_len = sizeof(fcreq);
DPRINTF("Proxying fetch_connect call to parent");
if (sandbox_sendrpc(fscb, SANDBOX_REQ_FETCHCONN, seqno, &iov_req, 1) < 0)
err(-1, "sandbox_sendrpc");
/* Get a ptr to fdarry and update the number of fds we are expecting */
fdp = fdarray;
fdcount = 1;
if (sandbox_recvrpc_rights(fscb, &opno, &seqno, &buffer, &len, fdp, &fdcount)
< 0) {
if (errno == EPIPE)
DPRINTF("[XXX] EPIPE");
else
DPRINTF("[XXX] sandbox_recvrpc_rights");
exit(-1);
}
/* Demangle data */
if (len != sizeof(struct fetchconn_rep)) {
DPRINTF("Received len mismatch");
exit(-1);
}
memmove(&fcrep, buffer, len);
/*
* Okay, if we are here we can allocate conn_t and update the socket fd and
* refcount
*/
if ((conn = calloc(1, sizeof(*conn))) == NULL)
return NULL;
conn->sd = dup(fdarray[0]);
conn->ref = fcrep.ref;
DPRINTF("OK, we have the conn");
return conn;
}
/* called in sandboxed process */
struct servent *
getservbyname_inparent(const char *name, const char *proto)
{
struct servent *se;
struct getserv_req gsreq;
struct getserv_rep gsrep;
uint32_t seqno = 0;
struct iovec iov_req, iov_rep;
uint32_t opno;
u_char *buffer;
size_t len;
bzero(&gsreq, sizeof(struct getserv_req));
bzero(&gsrep, sizeof(struct getserv_rep));
strlcpy(gsreq.name, name, MMIN(strlen(name) + 1, URL_SCHEMELEN));
strlcpy(gsreq.proto, proto, MMIN(strlen(proto) + 1, URL_SCHEMELEN));
/*bzero(&iov_req, sizeof(struct iovec));*/
/*bzero(&iov_rep, sizeof(struct iovec));*/
iov_req.iov_base = &gsreq;
iov_req.iov_len = sizeof(gsreq);
DPRINTF("Proxying getservbyname call to parent");
if (sandbox_sendrpc(fscb, SANDBOX_REQ_GETSERVBYNAME, seqno, &iov_req, 1) < 0)
err(-1, "sandbox_sendrpc");
/* Get a ptr to fdarry and update the number of fds we are expecting */
if (sandbox_recvrpc(fscb, &opno, &seqno, &buffer, &len) < 0) {
if (errno == EPIPE)
DPRINTF("[XXX] EPIPE");
else
DPRINTF("[XXX] sandbox_recvrpc");
exit(-1);
}
/* Demangle data */
if (len != sizeof(struct getserv_rep)) {
DPRINTF("Received len mismatch");
exit(-1);
}
memmove(&gsrep, buffer, len);
// Allocate servent and copy received one into it
if ((se = calloc(1, sizeof(struct servent))) == NULL)
return NULL;
memmove(se, &gsrep.se, sizeof(struct servent));
DPRINTF("OK, we have the servent");
return se;
}
static void
sandbox_fetchParseURL(struct sandbox_cb *scb, uint32_t opno, uint32_t seqno, char
*buffer, size_t len)
{
struct fetch_parse_url_req req;
struct fetch_parse_url_rep rep;
struct url *urlptr;
struct iovec iov;
/* Initialize data */
bzero(&req, sizeof(req));
bzero(&rep, sizeof(rep));
bzero(&iov, sizeof(iov));
/* Demangle data */
bcopy(buffer, &req, sizeof(req));
/* Perform the risky call */
DPRINTF("Calling fetchParseURL");
if ((urlptr = fetchParseURL(req.url_s)) == NULL) {
warn("%s: parse error", req.url_s);
rep.ret = -1; /* Indicate failure */
}
/* If it was a success */
if (!rep.ret) {
/*bcopy(urlptr, &rep.url_r, sizeof(struct url));*/
strlcpy(rep.scheme, urlptr->scheme, sizeof(rep.scheme));
strlcpy(rep.user, urlptr->user, sizeof(rep.user));
strlcpy(rep.pwd, urlptr->pwd, sizeof(rep.pwd));
strlcpy(rep.host, urlptr->host, sizeof(rep.host));
rep.port = urlptr->port;
rep.offset = urlptr->offset;
rep.length = urlptr->length;
rep.ims_time = urlptr->ims_time;
strlcpy(rep.doc, urlptr->doc, sizeof(rep.doc));
}
iov.iov_base = &rep;
iov.iov_len = sizeof(rep);
/* Send the parsed URL back to the parent */
if (sandbox_sendrpc(scb, opno, seqno, &iov, 1) < 0)
err(-1, "sandbox_send_rpc");
if (urlptr)
fetchFreeURL(urlptr);
}
static void
fsandbox(void)
{
uint32_t opno, seqno;
u_char *buffer;
size_t len;
DPRINTF("Calling cap_enter()");
DPRINTF("pid: %d", getpid());
cap_enter(); // begin sandboxed execution
DPRINTF("===> In fetch_sandbox()");
for (;;) {
/* Get the output fd and URL from parent */
if (sandbox_recvrpc(fscb, &opno, &seqno, &buffer, &len) < 0) {
if (errno == EPIPE) {
DPRINTF("[XXX] EPIPE");
exit(-1);
}
else {
DPRINTF("[XXX] sandbox_recvrpc");
err(-1, "sandbox_recvrpc");
}
}
DPRINTF("Request received");
switch(opno) {
#ifdef SANDBOX_FETCH
case PROXIED_FETCH:
/* fetch the url and return */
DPRINTF("PROXIED_FETCH");
sandbox_fetch(fscb, opno, seqno, (char *)buffer, len);
break;
#endif
#ifdef SANDBOX_PARSE_URL
case PROXIED_FETCH_PARSE_URL:
DPRINTF("PROXIED_FETCH_PARSE_URL");
sandbox_fetchParseURL(fscb, opno, seqno, (char *)buffer, len);
break;
#endif
case TERMINATE_SANDBOX:
DPRINTF("TERMINATE_SANDBOX");
goto out;
/* For future expansion */
default:
errx(-1, "sandbox_main: unknown op %d", opno);
}
}
out:
/* Free buffer */
free(buffer);
DPRINTF("Sandbox exiting!");
/* exit */
exit(0);
}
#endif
int
fetch_wrapper(char *URL, const char *path)
{
/* Currently haven't tested using both sandboxes at once */
#ifdef SANDBOX_FETCH
#ifdef SANDBOX_EPHEMERAL
fetch_sandbox_init();
#endif
return (fetch_insandbox(URL, path));
#else
return (fetch(URL, path));
#endif
}
struct url *
fetchParseURL_wrapper(char *URL)
{
struct url *uptr;
#ifndef SANDBOX_PARSE_URL
DPRINTF("Directly calling fetchParseURL");
if ((uptr = fetchParseURL(URL)) == NULL) {
warnx("%s: parse error", URL);
return NULL;
}
#else
#ifdef SANDBOX_EPHEMERAL
fetch_sandbox_init();
#endif
DPRINTF("Proxying to sandbox");
size_t len;
struct fetch_parse_url_req req;
struct fetch_parse_url_rep rep;
struct iovec iov_req, iov_rep;
/* Init data */
bzero(&rep, sizeof(rep));
bzero(&iov_req, sizeof(iov_req));
bzero(&iov_rep, sizeof(iov_rep));
strlcpy(req.url_s, URL, sizeof(req.url_s));
iov_req.iov_base = &req;
iov_req.iov_len = sizeof(req);
iov_rep.iov_base = &rep;
iov_rep.iov_len = sizeof(rep);
if (host_rpc(fscb, PROXIED_FETCH_PARSE_URL, &iov_req, 1, &iov_rep, 1, &len)
< 0)
err(-1, "host_rpc");
if (len != sizeof(rep))
err(-1, "host_rpc");
/*ret should be 0 for success */
if (rep.ret)
return NULL;
DPRINTF("SCHEME: %s HOST: %s, PORT: %d DOC: %s", rep.scheme, rep.host,
rep.port, rep.doc);
uptr = fetchMakeURL(rep.scheme, rep.host, rep.port, rep.doc, rep.user, rep.pwd);
#endif
DPRINTF("SCHEME: %s HOST: %s, PORT: %d DOC: %s USR: %s PWD: %s OFFSET: %ld",
uptr->scheme, uptr->host, uptr->port, uptr->doc, uptr->user, uptr->pwd,
uptr->offset);
return uptr;
}