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 <limits.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sha1.h>
29 #include <sha2.h>
30 #include <unistd.h>
31 #include <zlib.h>
33 #include "got_compat.h"
35 #include "got_error.h"
36 #include "got_object.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_hash.h"
45 #ifndef nitems
46 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 #endif
49 #define GOT_OBJ_TAG_COMMIT "commit"
50 #define GOT_OBJ_TAG_TREE "tree"
51 #define GOT_OBJ_TAG_BLOB "blob"
52 #define GOT_OBJ_TAG_TAG "tag"
54 static volatile sig_atomic_t sigint_received;
56 static void
57 catch_sigint(int signo)
58 {
59 sigint_received = 1;
60 }
62 static const struct got_error *
63 send_raw_obj(struct imsgbuf *ibuf, struct got_object *obj,
64 struct got_object_id *expected_id,
65 int fd, int outfd)
66 {
67 const struct got_error *err = NULL;
68 uint8_t *data = NULL;
69 off_t size;
70 size_t hdrlen;
72 if (lseek(fd, SEEK_SET, 0) == -1) {
73 err = got_error_from_errno("lseek");
74 goto done;
75 }
77 err = got_object_read_raw(&data, &size, &hdrlen,
78 GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, outfd, expected_id, fd);
79 if (err)
80 goto done;
82 err = got_privsep_send_raw_obj(ibuf, size, hdrlen, data);
83 done:
84 free(data);
85 if (close(fd) == -1 && err == NULL)
86 err = got_error_from_errno("close");
87 return err;
88 }
90 int
91 main(int argc, char *argv[])
92 {
93 const struct got_error *err = NULL;
94 struct got_object *obj = NULL;
95 struct imsg imsg;
96 struct imsgbuf ibuf;
97 size_t datalen;
98 struct got_object_id expected_id;
100 signal(SIGINT, catch_sigint);
102 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
104 #ifndef PROFILE
105 /* revoke access to most system calls */
106 if (pledge("stdio recvfd", NULL) == -1) {
107 err = got_error_from_errno("pledge");
108 got_privsep_send_error(&ibuf, err);
109 return 1;
112 /* revoke fs access */
113 if (landlock_no_fs() == -1) {
114 err = got_error_from_errno("landlock_no_fs");
115 got_privsep_send_error(&ibuf, err);
116 return 1;
118 if (cap_enter() == -1) {
119 err = got_error_from_errno("cap_enter");
120 got_privsep_send_error(&ibuf, err);
121 return 1;
123 #endif
125 for (;;) {
126 if (sigint_received) {
127 err = got_error(GOT_ERR_CANCELLED);
128 break;
131 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
132 if (err) {
133 if (err->code == GOT_ERR_PRIVSEP_PIPE)
134 err = NULL;
135 break;
138 if (imsg.hdr.type == GOT_IMSG_STOP)
139 break;
141 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST &&
142 imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) {
143 err = got_error(GOT_ERR_PRIVSEP_MSG);
144 goto done;
147 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
148 if (datalen != sizeof(expected_id)) {
149 err = got_error(GOT_ERR_PRIVSEP_LEN);
150 goto done;
152 memcpy(&expected_id, imsg.data, sizeof(expected_id));
154 if (imsg.fd == -1) {
155 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
156 goto done;
159 err = got_object_read_header(&obj, imsg.fd);
160 if (err)
161 goto done;
163 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
164 struct imsg imsg_outfd;
166 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
167 if (err) {
168 if (imsg_outfd.hdr.len == 0)
169 err = NULL;
170 goto done;
173 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
174 imsg_free(&imsg_outfd);
175 goto done;
178 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
179 err = got_error(GOT_ERR_PRIVSEP_MSG);
180 imsg_free(&imsg_outfd);
181 goto done;
184 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
185 if (datalen != 0) {
186 err = got_error(GOT_ERR_PRIVSEP_LEN);
187 imsg_free(&imsg_outfd);
188 goto done;
190 if (imsg_outfd.fd == -1) {
191 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
192 imsg_free(&imsg_outfd);
193 goto done;
195 err = send_raw_obj(&ibuf, obj, &expected_id,
196 imsg.fd, imsg_outfd.fd);
197 imsg.fd = -1; /* imsg.fd is owned by send_raw_obj() */
198 if (close(imsg_outfd.fd) == -1 && err == NULL)
199 err = got_error_from_errno("close");
200 imsg_free(&imsg_outfd);
201 if (err)
202 goto done;
203 } else
204 err = got_privsep_send_obj(&ibuf, obj);
205 done:
206 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
207 err = got_error_from_errno("close");
208 imsg_free(&imsg);
209 if (obj)
210 got_object_close(obj);
211 if (err)
212 break;
215 imsg_clear(&ibuf);
216 if (err) {
217 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
218 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
219 got_privsep_send_error(&ibuf, err);
222 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
223 err = got_error_from_errno("close");
224 return err ? 1 : 0;