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>
33 #include "got_error.h"
34 #include "got_object.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_parse.h"
40 #include "got_lib_privsep.h"
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 #define GOT_OBJ_TAG_COMMIT "commit"
47 #define GOT_OBJ_TAG_TREE "tree"
48 #define GOT_OBJ_TAG_BLOB "blob"
49 #define GOT_OBJ_TAG_TAG "tag"
51 static volatile sig_atomic_t sigint_received;
54 catch_sigint(int signo)
59 static const struct got_error *
60 send_raw_obj(struct imsgbuf *ibuf, struct got_object *obj, int fd, int outfd)
62 const struct got_error *err = NULL;
64 size_t len = 0, consumed;
67 if (lseek(fd, SEEK_SET, 0) == -1) {
68 err = got_error_from_errno("lseek");
75 err = got_error_from_errno("fdopen");
80 if (obj->size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
81 err = got_inflate_to_mem(&data, &len, &consumed, f);
83 err = got_inflate_to_fd(&len, f, outfd);
87 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
88 err = got_error(GOT_ERR_BAD_OBJ_HDR);
92 err = got_privsep_send_raw_obj(ibuf, len, obj->hdrlen, data);
95 if (fclose(f) == EOF && err == NULL)
96 err = got_error_from_errno("fclose");
102 main(int argc, char *argv[])
104 const struct got_error *err = NULL;
105 struct got_object *obj = NULL;
110 signal(SIGINT, catch_sigint);
112 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
115 /* revoke access to most system calls */
116 if (pledge("stdio recvfd", NULL) == -1) {
117 err = got_error_from_errno("pledge");
118 got_privsep_send_error(&ibuf, err);
124 if (sigint_received) {
125 err = got_error(GOT_ERR_CANCELLED);
129 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
131 if (err->code == GOT_ERR_PRIVSEP_PIPE)
136 if (imsg.hdr.type == GOT_IMSG_STOP)
139 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST &&
140 imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) {
141 err = got_error(GOT_ERR_PRIVSEP_MSG);
145 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
147 err = got_error(GOT_ERR_PRIVSEP_LEN);
152 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
156 err = got_object_read_header(&obj, imsg.fd);
160 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
161 struct imsg imsg_outfd;
162 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
164 if (imsg_outfd.hdr.len == 0)
169 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
170 imsg_free(&imsg_outfd);
174 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
175 err = got_error(GOT_ERR_PRIVSEP_MSG);
176 imsg_free(&imsg_outfd);
180 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
182 err = got_error(GOT_ERR_PRIVSEP_LEN);
183 imsg_free(&imsg_outfd);
186 if (imsg_outfd.fd == -1) {
187 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
188 imsg_free(&imsg_outfd);
191 err = send_raw_obj(&ibuf, obj, imsg.fd, imsg_outfd.fd);
192 imsg.fd = -1; /* imsg.fd is owned by send_raw_obj() */
193 if (close(imsg_outfd.fd) == -1 && err == NULL)
194 err = got_error_from_errno("close");
195 imsg_free(&imsg_outfd);
199 err = got_privsep_send_obj(&ibuf, obj);
201 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
202 err = got_error_from_errno("close");
205 got_object_close(obj);
212 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
213 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
214 got_privsep_send_error(&ibuf, err);
217 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
218 err = got_error_from_errno("close");