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>
30 #include "got_compat.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"
40 #include "got_lib_sha1.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,
61 struct got_object_id *expected_id,
64 const struct got_error *err = NULL;
66 size_t len = 0, consumed;
68 struct got_object_id id;
69 struct got_inflate_checksum csum;
73 memset(&csum, 0, sizeof(csum));
74 csum.output_sha1 = &sha1_ctx;
76 if (lseek(fd, SEEK_SET, 0) == -1) {
77 err = got_error_from_errno("lseek");
84 err = got_error_from_errno("fdopen");
89 if (obj->size + obj->hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
90 err = got_inflate_to_mem(&data, &len, &consumed, &csum, f);
92 err = got_inflate_to_fd(&len, f, &csum, outfd);
96 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
97 err = got_error(GOT_ERR_BAD_OBJ_HDR);
101 SHA1Final(id.sha1, &sha1_ctx);
102 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
103 char buf[SHA1_DIGEST_STRING_LENGTH];
104 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
105 "checksum failure for object %s",
106 got_sha1_digest_to_str(expected_id->sha1, buf,
112 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, data);
116 if (fclose(f) == EOF && err == NULL)
117 err = got_error_from_errno("fclose");
123 main(int argc, char *argv[])
125 const struct got_error *err = NULL;
126 struct got_object *obj = NULL;
130 struct got_object_id expected_id;
132 signal(SIGINT, catch_sigint);
134 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
137 /* revoke access to most system calls */
138 if (pledge("stdio recvfd", NULL) == -1) {
139 err = got_error_from_errno("pledge");
140 got_privsep_send_error(&ibuf, err);
146 if (sigint_received) {
147 err = got_error(GOT_ERR_CANCELLED);
151 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
153 if (err->code == GOT_ERR_PRIVSEP_PIPE)
158 if (imsg.hdr.type == GOT_IMSG_STOP)
161 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST &&
162 imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) {
163 err = got_error(GOT_ERR_PRIVSEP_MSG);
167 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
168 if (datalen != sizeof(expected_id)) {
169 err = got_error(GOT_ERR_PRIVSEP_LEN);
172 memcpy(&expected_id, imsg.data, sizeof(expected_id));
175 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
179 err = got_object_read_header(&obj, imsg.fd);
183 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
184 struct imsg imsg_outfd;
186 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
188 if (imsg_outfd.hdr.len == 0)
193 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
194 imsg_free(&imsg_outfd);
198 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
199 err = got_error(GOT_ERR_PRIVSEP_MSG);
200 imsg_free(&imsg_outfd);
204 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
206 err = got_error(GOT_ERR_PRIVSEP_LEN);
207 imsg_free(&imsg_outfd);
210 if (imsg_outfd.fd == -1) {
211 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
212 imsg_free(&imsg_outfd);
215 err = send_raw_obj(&ibuf, obj, &expected_id,
216 imsg.fd, imsg_outfd.fd);
217 imsg.fd = -1; /* imsg.fd is owned by send_raw_obj() */
218 if (close(imsg_outfd.fd) == -1 && err == NULL)
219 err = got_error_from_errno("close");
220 imsg_free(&imsg_outfd);
224 err = got_privsep_send_obj(&ibuf, obj);
226 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
227 err = got_error_from_errno("close");
230 got_object_close(obj);
237 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
238 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
239 got_privsep_send_error(&ibuf, err);
242 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
243 err = got_error_from_errno("close");