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>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_parse.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_hash.h"
45 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #define GOT_OBJ_TAG_COMMIT "commit"
49 #define GOT_OBJ_TAG_TREE "tree"
50 #define GOT_OBJ_TAG_BLOB "blob"
51 #define GOT_OBJ_TAG_TAG "tag"
53 static volatile sig_atomic_t sigint_received;
56 catch_sigint(int signo)
61 static const struct got_error *
62 send_raw_obj(struct imsgbuf *ibuf, struct got_object *obj,
63 struct got_object_id *expected_id,
66 const struct got_error *err = NULL;
71 if (lseek(fd, SEEK_SET, 0) == -1) {
72 err = got_error_from_errno("lseek");
76 err = got_object_read_raw(&data, &size, &hdrlen,
77 GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, outfd, expected_id, fd);
81 err = got_privsep_send_raw_obj(ibuf, size, hdrlen, data);
84 if (close(fd) == -1 && err == NULL)
85 err = got_error_from_errno("close");
90 main(int argc, char *argv[])
92 const struct got_error *err = NULL;
93 struct got_object *obj = NULL;
97 struct got_object_id expected_id;
99 signal(SIGINT, catch_sigint);
101 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
104 /* revoke access to most system calls */
105 if (pledge("stdio recvfd", NULL) == -1) {
106 err = got_error_from_errno("pledge");
107 got_privsep_send_error(&ibuf, err);
113 if (sigint_received) {
114 err = got_error(GOT_ERR_CANCELLED);
118 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
120 if (err->code == GOT_ERR_PRIVSEP_PIPE)
125 if (imsg.hdr.type == GOT_IMSG_STOP)
128 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST &&
129 imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) {
130 err = got_error(GOT_ERR_PRIVSEP_MSG);
134 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
135 if (datalen != sizeof(expected_id)) {
136 err = got_error(GOT_ERR_PRIVSEP_LEN);
139 memcpy(&expected_id, imsg.data, sizeof(expected_id));
142 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
146 err = got_object_read_header(&obj, imsg.fd);
150 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
151 struct imsg imsg_outfd;
153 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
155 if (imsg_outfd.hdr.len == 0)
160 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
161 imsg_free(&imsg_outfd);
165 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
166 err = got_error(GOT_ERR_PRIVSEP_MSG);
167 imsg_free(&imsg_outfd);
171 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
173 err = got_error(GOT_ERR_PRIVSEP_LEN);
174 imsg_free(&imsg_outfd);
177 if (imsg_outfd.fd == -1) {
178 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
179 imsg_free(&imsg_outfd);
182 err = send_raw_obj(&ibuf, obj, &expected_id,
183 imsg.fd, imsg_outfd.fd);
184 imsg.fd = -1; /* imsg.fd is owned by send_raw_obj() */
185 if (close(imsg_outfd.fd) == -1 && err == NULL)
186 err = got_error_from_errno("close");
187 imsg_free(&imsg_outfd);
191 err = got_privsep_send_obj(&ibuf, obj);
193 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
194 err = got_error_from_errno("close");
197 got_object_close(obj);
204 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
205 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
206 got_privsep_send_error(&ibuf, err);
209 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
210 err = got_error_from_errno("close");