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/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
22 #include <stdint.h>
23 #include <imsg.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <unistd.h>
31 #include <zlib.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"
43 #ifndef nitems
44 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
45 #endif
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;
54 static void
55 catch_sigint(int signo)
56 {
57 sigint_received = 1;
58 }
60 static const struct got_error *
61 send_raw_obj(struct imsgbuf *ibuf, struct got_object *obj,
62 struct got_object_id *expected_id,
63 int fd, int outfd)
64 {
65 const struct got_error *err = NULL;
66 uint8_t *data = NULL;
67 size_t len = 0, consumed;
68 FILE *f;
69 struct got_object_id id;
70 struct got_inflate_checksum csum;
71 SHA1_CTX sha1_ctx;
73 SHA1Init(&sha1_ctx);
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");
79 close(fd);
80 return err;
81 }
83 f = fdopen(fd, "r");
84 if (f == NULL) {
85 err = got_error_from_errno("fdopen");
86 close(fd);
87 return err;
88 }
90 if (obj->size + obj->hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
91 err = got_inflate_to_mem(&data, &len, &consumed, &csum, f);
92 else
93 err = got_inflate_to_fd(&len, f, &csum, outfd);
94 if (err)
95 goto done;
97 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
98 err = got_error(GOT_ERR_BAD_OBJ_HDR);
99 goto done;
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,
108 sizeof(buf)));
109 goto done;
113 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, data);
115 done:
116 free(data);
117 if (fclose(f) == EOF && err == NULL)
118 err = got_error_from_errno("fclose");
120 return err;
123 int
124 main(int argc, char *argv[])
126 const struct got_error *err = NULL;
127 struct got_object *obj = NULL;
128 struct imsg imsg;
129 struct imsgbuf ibuf;
130 size_t datalen;
131 struct got_object_id expected_id;
133 signal(SIGINT, catch_sigint);
135 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
137 #ifndef PROFILE
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);
142 return 1;
144 #endif
146 for (;;) {
147 if (sigint_received) {
148 err = got_error(GOT_ERR_CANCELLED);
149 break;
152 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
153 if (err) {
154 if (err->code == GOT_ERR_PRIVSEP_PIPE)
155 err = NULL;
156 break;
159 if (imsg.hdr.type == GOT_IMSG_STOP)
160 break;
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);
165 goto done;
168 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
169 if (datalen != sizeof(expected_id)) {
170 err = got_error(GOT_ERR_PRIVSEP_LEN);
171 goto done;
173 memcpy(&expected_id, imsg.data, sizeof(expected_id));
175 if (imsg.fd == -1) {
176 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
177 goto done;
180 err = got_object_read_header(&obj, imsg.fd);
181 if (err)
182 goto done;
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);
188 if (err) {
189 if (imsg_outfd.hdr.len == 0)
190 err = NULL;
191 goto done;
194 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
195 imsg_free(&imsg_outfd);
196 goto done;
199 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
200 err = got_error(GOT_ERR_PRIVSEP_MSG);
201 imsg_free(&imsg_outfd);
202 goto done;
205 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
206 if (datalen != 0) {
207 err = got_error(GOT_ERR_PRIVSEP_LEN);
208 imsg_free(&imsg_outfd);
209 goto done;
211 if (imsg_outfd.fd == -1) {
212 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
213 imsg_free(&imsg_outfd);
214 goto done;
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);
222 if (err)
223 goto done;
224 } else
225 err = got_privsep_send_obj(&ibuf, obj);
226 done:
227 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
228 err = got_error_from_errno("close");
229 imsg_free(&imsg);
230 if (obj)
231 got_object_close(obj);
232 if (err)
233 break;
236 imsg_clear(&ibuf);
237 if (err) {
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");
245 return err ? 1 : 0;