2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/limits.h>
22 #include <sys/syslimits.h>
32 #include "got_error.h"
33 #include "got_object.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_parse.h"
39 #include "got_lib_privsep.h"
42 main(int argc, char *argv[])
44 const struct got_error *err = NULL;
48 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
51 /* revoke access to most system calls */
52 if (pledge("stdio recvfd", NULL) == -1) {
53 err = got_error_from_errno();
54 got_privsep_send_error(&ibuf, err);
60 struct imsg imsg, imsg_outfd;
64 memset(&imsg, 0, sizeof(imsg));
66 memset(&imsg_outfd, 0, sizeof(imsg_outfd));
69 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
71 if (err->code == GOT_ERR_PRIVSEP_PIPE)
76 if (imsg.hdr.type == GOT_IMSG_STOP)
79 if (imsg.hdr.type != GOT_IMSG_BLOB_REQUEST) {
80 err = got_error(GOT_ERR_PRIVSEP_MSG);
84 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
86 err = got_error(GOT_ERR_PRIVSEP_LEN);
90 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
94 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
96 if (imsg.hdr.len == 0)
101 if (imsg_outfd.hdr.type == GOT_IMSG_STOP)
104 if (imsg_outfd.hdr.type != GOT_IMSG_BLOB_OUTFD) {
105 err = got_error(GOT_ERR_PRIVSEP_MSG);
109 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
111 err = got_error(GOT_ERR_PRIVSEP_LEN);
114 if (imsg_outfd.fd == -1) {
115 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
119 f = fdopen(imsg.fd, "rb");
121 err = got_error_from_errno();
125 err = got_inflate_to_fd(&size, f, imsg_outfd.fd);
129 err = got_privsep_send_blob(&ibuf, size);
133 else if (imsg.fd != -1)
135 if (imsg_outfd.fd != -1)
136 close(imsg_outfd.fd);
138 imsg_free(&imsg_outfd);
140 if (err->code == GOT_ERR_PRIVSEP_PIPE)
143 got_privsep_send_error(&ibuf, err);
150 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
151 close(GOT_IMSG_FD_CHILD);