Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <sys/time.h>
21 #include <stdint.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <zlib.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"
42 #ifndef nitems
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 #endif
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;
53 static void
54 catch_sigint(int signo)
55 {
56 sigint_received = 1;
57 }
59 static const struct got_error *
60 send_raw_obj(struct imsgbuf *ibuf, struct got_object *obj,
61 struct got_object_id *expected_id,
62 int fd, int outfd)
63 {
64 const struct got_error *err = NULL;
65 uint8_t *data = NULL;
66 size_t len = 0, consumed;
67 FILE *f;
68 struct got_object_id id;
69 struct got_inflate_checksum csum;
70 SHA1_CTX sha1_ctx;
72 SHA1Init(&sha1_ctx);
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");
78 close(fd);
79 return err;
80 }
82 f = fdopen(fd, "r");
83 if (f == NULL) {
84 err = got_error_from_errno("fdopen");
85 close(fd);
86 return err;
87 }
89 if (obj->size + obj->hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
90 err = got_inflate_to_mem(&data, &len, &consumed, &csum, f);
91 else
92 err = got_inflate_to_fd(&len, f, &csum, outfd);
93 if (err)
94 goto done;
96 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
97 err = got_error(GOT_ERR_BAD_OBJ_HDR);
98 goto done;
99 }
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,
107 sizeof(buf)));
108 goto done;
112 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, data);
114 done:
115 free(data);
116 if (fclose(f) == EOF && err == NULL)
117 err = got_error_from_errno("fclose");
119 return err;
122 int
123 main(int argc, char *argv[])
125 const struct got_error *err = NULL;
126 struct got_object *obj = NULL;
127 struct imsg imsg;
128 struct imsgbuf ibuf;
129 size_t datalen;
130 struct got_object_id expected_id;
132 signal(SIGINT, catch_sigint);
134 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
136 #ifndef PROFILE
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);
141 return 1;
144 /* revoke fs access */
145 if (landlock_no_fs() == -1) {
146 err = got_error_from_errno("landlock_no_fs");
147 got_privsep_send_error(&ibuf, err);
148 return 1;
150 #endif
152 for (;;) {
153 if (sigint_received) {
154 err = got_error(GOT_ERR_CANCELLED);
155 break;
158 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
159 if (err) {
160 if (err->code == GOT_ERR_PRIVSEP_PIPE)
161 err = NULL;
162 break;
165 if (imsg.hdr.type == GOT_IMSG_STOP)
166 break;
168 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST &&
169 imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) {
170 err = got_error(GOT_ERR_PRIVSEP_MSG);
171 goto done;
174 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
175 if (datalen != sizeof(expected_id)) {
176 err = got_error(GOT_ERR_PRIVSEP_LEN);
177 goto done;
179 memcpy(&expected_id, imsg.data, sizeof(expected_id));
181 if (imsg.fd == -1) {
182 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
183 goto done;
186 err = got_object_read_header(&obj, imsg.fd);
187 if (err)
188 goto done;
190 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
191 struct imsg imsg_outfd;
193 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
194 if (err) {
195 if (imsg_outfd.hdr.len == 0)
196 err = NULL;
197 goto done;
200 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
201 imsg_free(&imsg_outfd);
202 goto done;
205 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
206 err = got_error(GOT_ERR_PRIVSEP_MSG);
207 imsg_free(&imsg_outfd);
208 goto done;
211 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
212 if (datalen != 0) {
213 err = got_error(GOT_ERR_PRIVSEP_LEN);
214 imsg_free(&imsg_outfd);
215 goto done;
217 if (imsg_outfd.fd == -1) {
218 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
219 imsg_free(&imsg_outfd);
220 goto done;
222 err = send_raw_obj(&ibuf, obj, &expected_id,
223 imsg.fd, imsg_outfd.fd);
224 imsg.fd = -1; /* imsg.fd is owned by send_raw_obj() */
225 if (close(imsg_outfd.fd) == -1 && err == NULL)
226 err = got_error_from_errno("close");
227 imsg_free(&imsg_outfd);
228 if (err)
229 goto done;
230 } else
231 err = got_privsep_send_obj(&ibuf, obj);
232 done:
233 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
234 err = got_error_from_errno("close");
235 imsg_free(&imsg);
236 if (obj)
237 got_object_close(obj);
238 if (err)
239 break;
242 imsg_clear(&ibuf);
243 if (err) {
244 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
245 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
246 got_privsep_send_error(&ibuf, err);
249 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
250 err = got_error_from_errno("close");
251 return err ? 1 : 0;