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 if (cap_enter() == -1) {
151 err = got_error_from_errno("cap_enter");
152 got_privsep_send_error(&ibuf, err);
153 return 1;
155 #endif
157 for (;;) {
158 if (sigint_received) {
159 err = got_error(GOT_ERR_CANCELLED);
160 break;
163 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
164 if (err) {
165 if (err->code == GOT_ERR_PRIVSEP_PIPE)
166 err = NULL;
167 break;
170 if (imsg.hdr.type == GOT_IMSG_STOP)
171 break;
173 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST &&
174 imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) {
175 err = got_error(GOT_ERR_PRIVSEP_MSG);
176 goto done;
179 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
180 if (datalen != sizeof(expected_id)) {
181 err = got_error(GOT_ERR_PRIVSEP_LEN);
182 goto done;
184 memcpy(&expected_id, imsg.data, sizeof(expected_id));
186 if (imsg.fd == -1) {
187 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
188 goto done;
191 err = got_object_read_header(&obj, imsg.fd);
192 if (err)
193 goto done;
195 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
196 struct imsg imsg_outfd;
198 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
199 if (err) {
200 if (imsg_outfd.hdr.len == 0)
201 err = NULL;
202 goto done;
205 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
206 imsg_free(&imsg_outfd);
207 goto done;
210 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
211 err = got_error(GOT_ERR_PRIVSEP_MSG);
212 imsg_free(&imsg_outfd);
213 goto done;
216 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
217 if (datalen != 0) {
218 err = got_error(GOT_ERR_PRIVSEP_LEN);
219 imsg_free(&imsg_outfd);
220 goto done;
222 if (imsg_outfd.fd == -1) {
223 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
224 imsg_free(&imsg_outfd);
225 goto done;
227 err = send_raw_obj(&ibuf, obj, &expected_id,
228 imsg.fd, imsg_outfd.fd);
229 imsg.fd = -1; /* imsg.fd is owned by send_raw_obj() */
230 if (close(imsg_outfd.fd) == -1 && err == NULL)
231 err = got_error_from_errno("close");
232 imsg_free(&imsg_outfd);
233 if (err)
234 goto done;
235 } else
236 err = got_privsep_send_obj(&ibuf, obj);
237 done:
238 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
239 err = got_error_from_errno("close");
240 imsg_free(&imsg);
241 if (obj)
242 got_object_close(obj);
243 if (err)
244 break;
247 imsg_clear(&ibuf);
248 if (err) {
249 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
250 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
251 got_privsep_send_error(&ibuf, err);
254 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
255 err = got_error_from_errno("close");
256 return err ? 1 : 0;