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 <unistd.h>
29 #include <zlib.h>
31 #include "got_compat.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_hash.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 off_t size;
68 size_t hdrlen;
70 if (lseek(fd, SEEK_SET, 0) == -1) {
71 err = got_error_from_errno("lseek");
72 goto done;
73 }
75 err = got_object_read_raw(&data, &size, &hdrlen,
76 GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, outfd, expected_id, fd);
77 if (err)
78 goto done;
80 err = got_privsep_send_raw_obj(ibuf, size, hdrlen, data);
81 done:
82 free(data);
83 if (close(fd) == -1 && err == NULL)
84 err = got_error_from_errno("close");
85 return err;
86 }
88 int
89 main(int argc, char *argv[])
90 {
91 const struct got_error *err = NULL;
92 struct got_object *obj = NULL;
93 struct imsg imsg;
94 struct imsgbuf ibuf;
95 size_t datalen;
96 struct got_object_id expected_id;
98 signal(SIGINT, catch_sigint);
100 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
102 #ifndef PROFILE
103 /* revoke access to most system calls */
104 if (pledge("stdio recvfd", NULL) == -1) {
105 err = got_error_from_errno("pledge");
106 got_privsep_send_error(&ibuf, err);
107 return 1;
110 /* revoke fs access */
111 if (landlock_no_fs() == -1) {
112 err = got_error_from_errno("landlock_no_fs");
113 got_privsep_send_error(&ibuf, err);
114 return 1;
116 if (cap_enter() == -1) {
117 err = got_error_from_errno("cap_enter");
118 got_privsep_send_error(&ibuf, err);
119 return 1;
121 #endif
123 for (;;) {
124 int fd = -1, outfd = -1;
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 fd = imsg_get_fd(&imsg);
155 if (fd == -1) {
156 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
157 goto done;
160 err = got_object_read_header(&obj, fd);
161 if (err)
162 goto done;
164 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
165 struct imsg imsg_outfd;
167 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
168 if (err) {
169 if (imsg_outfd.hdr.len == 0)
170 err = NULL;
171 goto done;
174 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
175 imsg_free(&imsg_outfd);
176 goto done;
179 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
180 err = got_error(GOT_ERR_PRIVSEP_MSG);
181 imsg_free(&imsg_outfd);
182 goto done;
185 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
186 if (datalen != 0) {
187 err = got_error(GOT_ERR_PRIVSEP_LEN);
188 imsg_free(&imsg_outfd);
189 goto done;
191 outfd = imsg_get_fd(&imsg_outfd);
192 if (outfd == -1) {
193 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
194 imsg_free(&imsg_outfd);
195 goto done;
197 err = send_raw_obj(&ibuf, obj, &expected_id,
198 fd, outfd);
199 fd = -1; /* fd is owned by send_raw_obj() */
200 if (close(outfd) == -1 && err == NULL)
201 err = got_error_from_errno("close");
202 imsg_free(&imsg_outfd);
203 if (err)
204 goto done;
205 } else
206 err = got_privsep_send_obj(&ibuf, obj);
207 done:
208 if (fd != -1 && close(fd) == -1 && err == NULL)
209 err = got_error_from_errno("close");
210 imsg_free(&imsg);
211 if (obj)
212 got_object_close(obj);
213 if (err)
214 break;
217 imsg_clear(&ibuf);
218 if (err) {
219 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
220 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
221 got_privsep_send_error(&ibuf, err);
224 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
225 err = got_error_from_errno("close");
226 return err ? 1 : 0;