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"
41 #include "got_lib_sha1.h"
44 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 #define GOT_OBJ_TAG_COMMIT "commit"
48 #define GOT_OBJ_TAG_TREE "tree"
49 #define GOT_OBJ_TAG_BLOB "blob"
50 #define GOT_OBJ_TAG_TAG "tag"
52 static volatile sig_atomic_t sigint_received;
55 catch_sigint(int signo)
60 static const struct got_error *
61 send_raw_obj(struct imsgbuf *ibuf, struct got_object *obj,
62 struct got_object_id *expected_id,
65 const struct got_error *err = NULL;
67 size_t len = 0, consumed;
69 struct got_object_id id;
70 struct got_inflate_checksum csum;
74 memset(&csum, 0, sizeof(csum));
75 csum.output_sha1 = &sha1_ctx;
77 if (lseek(fd, SEEK_SET, 0) == -1) {
78 err = got_error_from_errno("lseek");
85 err = got_error_from_errno("fdopen");
90 if (obj->size + obj->hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
91 err = got_inflate_to_mem(&data, &len, &consumed, &csum, f);
93 err = got_inflate_to_fd(&len, f, &csum, outfd);
97 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
98 err = got_error(GOT_ERR_BAD_OBJ_HDR);
102 SHA1Final(id.sha1, &sha1_ctx);
103 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
104 char buf[SHA1_DIGEST_STRING_LENGTH];
105 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
106 "checksum failure for object %s",
107 got_sha1_digest_to_str(expected_id->sha1, buf,
113 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, data);
117 if (fclose(f) == EOF && err == NULL)
118 err = got_error_from_errno("fclose");
124 main(int argc, char *argv[])
126 const struct got_error *err = NULL;
127 struct got_object *obj = NULL;
131 struct got_object_id expected_id;
133 signal(SIGINT, catch_sigint);
135 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
138 /* revoke access to most system calls */
139 if (pledge("stdio recvfd", NULL) == -1) {
140 err = got_error_from_errno("pledge");
141 got_privsep_send_error(&ibuf, err);
147 if (sigint_received) {
148 err = got_error(GOT_ERR_CANCELLED);
152 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
154 if (err->code == GOT_ERR_PRIVSEP_PIPE)
159 if (imsg.hdr.type == GOT_IMSG_STOP)
162 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST &&
163 imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) {
164 err = got_error(GOT_ERR_PRIVSEP_MSG);
168 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
169 if (datalen != sizeof(expected_id)) {
170 err = got_error(GOT_ERR_PRIVSEP_LEN);
173 memcpy(&expected_id, imsg.data, sizeof(expected_id));
176 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
180 err = got_object_read_header(&obj, imsg.fd);
184 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
185 struct imsg imsg_outfd;
187 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
189 if (imsg_outfd.hdr.len == 0)
194 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
195 imsg_free(&imsg_outfd);
199 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
200 err = got_error(GOT_ERR_PRIVSEP_MSG);
201 imsg_free(&imsg_outfd);
205 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
207 err = got_error(GOT_ERR_PRIVSEP_LEN);
208 imsg_free(&imsg_outfd);
211 if (imsg_outfd.fd == -1) {
212 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
213 imsg_free(&imsg_outfd);
216 err = send_raw_obj(&ibuf, obj, &expected_id,
217 imsg.fd, imsg_outfd.fd);
218 imsg.fd = -1; /* imsg.fd is owned by send_raw_obj() */
219 if (close(imsg_outfd.fd) == -1 && err == NULL)
220 err = got_error_from_errno("close");
221 imsg_free(&imsg_outfd);
225 err = got_privsep_send_obj(&ibuf, obj);
227 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
228 err = got_error_from_errno("close");
231 got_object_close(obj);
238 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
239 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
240 got_privsep_send_error(&ibuf, err);
243 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
244 err = got_error_from_errno("close");