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 <sha2.h>
31 #include <unistd.h>
32 #include <zlib.h>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_parse.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_hash.h"
44 #ifndef nitems
45 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 #endif
48 #define GOT_OBJ_TAG_COMMIT "commit"
49 #define GOT_OBJ_TAG_TREE "tree"
50 #define GOT_OBJ_TAG_BLOB "blob"
51 #define GOT_OBJ_TAG_TAG "tag"
53 static volatile sig_atomic_t sigint_received;
55 static void
56 catch_sigint(int signo)
57 {
58 sigint_received = 1;
59 }
61 static const struct got_error *
62 send_raw_obj(struct imsgbuf *ibuf, struct got_object *obj,
63 struct got_object_id *expected_id,
64 int fd, int outfd)
65 {
66 const struct got_error *err = NULL;
67 uint8_t *data = NULL;
68 off_t size;
69 size_t hdrlen;
71 if (lseek(fd, SEEK_SET, 0) == -1) {
72 err = got_error_from_errno("lseek");
73 goto done;
74 }
76 err = got_object_read_raw(&data, &size, &hdrlen,
77 GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, outfd, expected_id, fd);
78 if (err)
79 goto done;
81 err = got_privsep_send_raw_obj(ibuf, size, hdrlen, data);
82 done:
83 free(data);
84 if (close(fd) == -1 && err == NULL)
85 err = got_error_from_errno("close");
86 return err;
87 }
89 int
90 main(int argc, char *argv[])
91 {
92 const struct got_error *err = NULL;
93 struct got_object *obj = NULL;
94 struct imsg imsg;
95 struct imsgbuf ibuf;
96 size_t datalen;
97 struct got_object_id expected_id;
99 signal(SIGINT, catch_sigint);
101 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
103 #ifndef PROFILE
104 /* revoke access to most system calls */
105 if (pledge("stdio recvfd", NULL) == -1) {
106 err = got_error_from_errno("pledge");
107 got_privsep_send_error(&ibuf, err);
108 return 1;
110 #endif
112 for (;;) {
113 int fd = -1, outfd = -1;
115 if (sigint_received) {
116 err = got_error(GOT_ERR_CANCELLED);
117 break;
120 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
121 if (err) {
122 if (err->code == GOT_ERR_PRIVSEP_PIPE)
123 err = NULL;
124 break;
127 if (imsg.hdr.type == GOT_IMSG_STOP)
128 break;
130 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST &&
131 imsg.hdr.type != GOT_IMSG_RAW_OBJECT_REQUEST) {
132 err = got_error(GOT_ERR_PRIVSEP_MSG);
133 goto done;
136 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
137 if (datalen != sizeof(expected_id)) {
138 err = got_error(GOT_ERR_PRIVSEP_LEN);
139 goto done;
141 memcpy(&expected_id, imsg.data, sizeof(expected_id));
143 fd = imsg_get_fd(&imsg);
144 if (fd == -1) {
145 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
146 goto done;
149 err = got_object_read_header(&obj, fd);
150 if (err)
151 goto done;
153 if (imsg.hdr.type == GOT_IMSG_RAW_OBJECT_REQUEST) {
154 struct imsg imsg_outfd;
156 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
157 if (err) {
158 if (imsg_outfd.hdr.len == 0)
159 err = NULL;
160 goto done;
163 if (imsg_outfd.hdr.type == GOT_IMSG_STOP) {
164 imsg_free(&imsg_outfd);
165 goto done;
168 if (imsg_outfd.hdr.type != GOT_IMSG_RAW_OBJECT_OUTFD) {
169 err = got_error(GOT_ERR_PRIVSEP_MSG);
170 imsg_free(&imsg_outfd);
171 goto done;
174 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
175 if (datalen != 0) {
176 err = got_error(GOT_ERR_PRIVSEP_LEN);
177 imsg_free(&imsg_outfd);
178 goto done;
180 outfd = imsg_get_fd(&imsg_outfd);
181 if (outfd == -1) {
182 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
183 imsg_free(&imsg_outfd);
184 goto done;
186 err = send_raw_obj(&ibuf, obj, &expected_id,
187 fd, outfd);
188 fd = -1; /* fd is owned by send_raw_obj() */
189 if (close(outfd) == -1 && err == NULL)
190 err = got_error_from_errno("close");
191 imsg_free(&imsg_outfd);
192 if (err)
193 goto done;
194 } else
195 err = got_privsep_send_obj(&ibuf, obj);
196 done:
197 if (fd != -1 && close(fd) == -1 && err == NULL)
198 err = got_error_from_errno("close");
199 imsg_free(&imsg);
200 if (obj)
201 got_object_close(obj);
202 if (err)
203 break;
206 imsg_clear(&ibuf);
207 if (err) {
208 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
209 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
210 got_privsep_send_error(&ibuf, err);
213 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
214 err = got_error_from_errno("close");
215 return err ? 1 : 0;