2 2178c42e 2018-04-22 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 2178c42e 2018-04-22 stsp * Permission to use, copy, modify, and distribute this software for any
5 2178c42e 2018-04-22 stsp * purpose with or without fee is hereby granted, provided that the above
6 2178c42e 2018-04-22 stsp * copyright notice and this permission notice appear in all copies.
8 2178c42e 2018-04-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2178c42e 2018-04-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2178c42e 2018-04-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2178c42e 2018-04-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2178c42e 2018-04-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2178c42e 2018-04-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2178c42e 2018-04-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 2178c42e 2018-04-22 stsp #include <sys/queue.h>
19 2178c42e 2018-04-22 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
21 876c234b 2018-09-10 stsp #include <sys/wait.h>
23 2178c42e 2018-04-22 stsp #include <stdio.h>
24 2178c42e 2018-04-22 stsp #include <stdlib.h>
25 2178c42e 2018-04-22 stsp #include <string.h>
26 2178c42e 2018-04-22 stsp #include <errno.h>
27 2178c42e 2018-04-22 stsp #include <stdint.h>
28 2178c42e 2018-04-22 stsp #include <poll.h>
29 2178c42e 2018-04-22 stsp #include <imsg.h>
30 2178c42e 2018-04-22 stsp #include <sha1.h>
31 2178c42e 2018-04-22 stsp #include <zlib.h>
32 788c352e 2018-06-16 stsp #include <time.h>
34 2178c42e 2018-04-22 stsp #include "got_object.h"
35 2178c42e 2018-04-22 stsp #include "got_error.h"
37 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
38 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
39 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
40 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
41 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
42 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
43 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
46 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 2178c42e 2018-04-22 stsp static const struct got_error *
50 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
52 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
55 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
56 2178c42e 2018-04-22 stsp pfd[0].events = events;
58 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
59 2178c42e 2018-04-22 stsp if (n == -1)
60 2178c42e 2018-04-22 stsp return got_error_from_errno();
62 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
63 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
64 2178c42e 2018-04-22 stsp return got_error_from_errno();
65 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
66 2178c42e 2018-04-22 stsp return NULL;
68 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
71 c4eae628 2018-04-23 stsp static const struct got_error *
72 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
74 fe36cf76 2018-04-23 stsp const struct got_error *err;
77 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
81 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
82 fe36cf76 2018-04-23 stsp if (n == -1) {
83 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
84 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
88 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
90 e033d803 2018-04-23 stsp return NULL;
93 ad242220 2018-09-08 stsp const struct got_error *
94 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
96 876c234b 2018-09-10 stsp int child_status;
98 876c234b 2018-09-10 stsp waitpid(pid, &child_status, 0);
100 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
101 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
103 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
104 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
106 876c234b 2018-09-10 stsp return NULL;
109 876c234b 2018-09-10 stsp const struct got_error *
110 ad242220 2018-09-08 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
112 e033d803 2018-04-23 stsp const struct got_error *err;
115 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
116 876c234b 2018-09-10 stsp if (n == -1)
117 876c234b 2018-09-10 stsp return got_error_from_errno();
119 876c234b 2018-09-10 stsp while (n == 0) {
120 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
122 876c234b 2018-09-10 stsp return err;
123 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
126 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
127 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
129 fe36cf76 2018-04-23 stsp return NULL;
132 fe36cf76 2018-04-23 stsp static const struct got_error *
133 c4eae628 2018-04-23 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
135 c4eae628 2018-04-23 stsp struct got_imsg_error ierr;
137 c4eae628 2018-04-23 stsp if (datalen != sizeof(ierr))
138 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
140 c4eae628 2018-04-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
141 c4eae628 2018-04-23 stsp if (ierr.code == GOT_ERR_ERRNO) {
142 c4eae628 2018-04-23 stsp static struct got_error serr;
143 c4eae628 2018-04-23 stsp serr.code = GOT_ERR_ERRNO;
144 c4eae628 2018-04-23 stsp serr.msg = strerror(ierr.errno_code);
145 c4eae628 2018-04-23 stsp return &serr;
148 c4eae628 2018-04-23 stsp return got_error(ierr.code);
151 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
153 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
155 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
156 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
159 2178c42e 2018-04-22 stsp ierr.code = err->code;
160 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
161 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
163 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
164 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
165 2178c42e 2018-04-22 stsp if (ret != -1) {
166 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
167 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
171 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
172 5d43e84d 2018-04-23 stsp if (poll_err) {
173 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
174 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
178 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
179 5d43e84d 2018-04-23 stsp if (ret == -1) {
180 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
181 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
186 e033d803 2018-04-23 stsp static const struct got_error *
187 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
189 e033d803 2018-04-23 stsp const struct got_error *err;
191 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
193 e033d803 2018-04-23 stsp return err;
195 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
196 e033d803 2018-04-23 stsp return got_error_from_errno();
198 e033d803 2018-04-23 stsp return NULL;
201 ad242220 2018-09-08 stsp const struct got_error *
202 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
204 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
205 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
207 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
209 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
210 ad242220 2018-09-08 stsp return got_error_from_errno();
212 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
213 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
214 ad242220 2018-09-08 stsp return err;
217 d6bda086 2018-09-10 stsp static const struct got_error *
218 d6bda086 2018-09-10 stsp send_delta(struct got_delta *delta, struct imsgbuf *ibuf)
220 d6bda086 2018-09-10 stsp struct got_imsg_delta idelta;
221 d6bda086 2018-09-10 stsp size_t offset, remain;
223 d6bda086 2018-09-10 stsp idelta.offset = delta->offset;
224 d6bda086 2018-09-10 stsp idelta.tslen = delta->tslen;
225 d6bda086 2018-09-10 stsp idelta.type = delta->type;
226 d6bda086 2018-09-10 stsp idelta.size = delta->size;
227 d6bda086 2018-09-10 stsp idelta.data_offset = delta->data_offset;
228 d6bda086 2018-09-10 stsp idelta.delta_len = delta->delta_len;
230 d6bda086 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_DELTA, 0, 0, -1,
231 d6bda086 2018-09-10 stsp &idelta, sizeof(idelta)) == -1)
232 d6bda086 2018-09-10 stsp return got_error_from_errno();
234 d6bda086 2018-09-10 stsp if (imsg_flush(ibuf) == -1)
235 d6bda086 2018-09-10 stsp return got_error_from_errno();
237 d6bda086 2018-09-10 stsp offset = 0;
238 d6bda086 2018-09-10 stsp remain = delta->delta_len;
239 d6bda086 2018-09-10 stsp while (remain > 0) {
240 d6bda086 2018-09-10 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
242 d6bda086 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_DELTA_STREAM, 0, 0, -1,
243 d6bda086 2018-09-10 stsp delta->delta_buf + offset, n) == -1)
244 d6bda086 2018-09-10 stsp return got_error_from_errno();
246 d6bda086 2018-09-10 stsp if (imsg_flush(ibuf) == -1)
247 d6bda086 2018-09-10 stsp return got_error_from_errno();
249 d6bda086 2018-09-10 stsp offset += n;
250 d6bda086 2018-09-10 stsp remain -= n;
253 d6bda086 2018-09-10 stsp return NULL;
256 ad242220 2018-09-08 stsp const struct got_error *
257 ad242220 2018-09-08 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
259 d6bda086 2018-09-10 stsp const struct got_error *err = NULL;
260 ad242220 2018-09-08 stsp struct got_imsg_object iobj, *iobjp = NULL;
261 ad242220 2018-09-08 stsp size_t iobj_size = 0;
262 ad242220 2018-09-08 stsp int imsg_code = GOT_IMSG_OBJECT_REQUEST;
265 ad242220 2018-09-08 stsp switch (obj->type) {
266 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_TREE:
267 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_TREE_REQUEST;
269 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_COMMIT:
270 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_COMMIT_REQUEST;
272 55da3778 2018-09-10 stsp case GOT_OBJ_TYPE_BLOB:
273 55da3778 2018-09-10 stsp imsg_code = GOT_IMSG_BLOB_REQUEST;
276 ad242220 2018-09-08 stsp return got_error(GOT_ERR_OBJ_TYPE);
279 ad242220 2018-09-08 stsp iobj.type = obj->type;
280 ad242220 2018-09-08 stsp iobj.flags = obj->flags;
281 ad242220 2018-09-08 stsp iobj.hdrlen = obj->hdrlen;
282 ad242220 2018-09-08 stsp iobj.size = obj->size;
283 e7885405 2018-09-10 stsp iobj.ndeltas = obj->deltas.nentries;
284 876c234b 2018-09-10 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED)
285 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
287 ad242220 2018-09-08 stsp iobjp = &iobj;
288 ad242220 2018-09-08 stsp iobj_size = sizeof(iobj);
291 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
292 ad242220 2018-09-08 stsp return got_error_from_errno();
294 d6bda086 2018-09-10 stsp err = flush_imsg(ibuf);
296 d6bda086 2018-09-10 stsp return err;
298 d6bda086 2018-09-10 stsp if (obj && obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
299 d6bda086 2018-09-10 stsp struct got_delta *delta;
300 d6bda086 2018-09-10 stsp SIMPLEQ_FOREACH(delta, &obj->deltas.entries, entry) {
301 d6bda086 2018-09-10 stsp err = send_delta(delta, ibuf);
307 d6bda086 2018-09-10 stsp return err;
310 2178c42e 2018-04-22 stsp const struct got_error *
311 55da3778 2018-09-10 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
313 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
315 ad242220 2018-09-08 stsp return got_error_from_errno();
317 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
320 55da3778 2018-09-10 stsp const struct got_error *
321 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
323 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
325 ad242220 2018-09-08 stsp return got_error_from_errno();
327 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
330 ad242220 2018-09-08 stsp const struct got_error *
331 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
333 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
334 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
335 876c234b 2018-09-10 stsp struct got_delta *delta;
337 2178c42e 2018-04-22 stsp iobj.type = obj->type;
338 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
339 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
340 2178c42e 2018-04-22 stsp iobj.size = obj->size;
341 876c234b 2018-09-10 stsp iobj.ndeltas = obj->deltas.nentries;
342 876c234b 2018-09-10 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED)
343 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
345 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
347 2178c42e 2018-04-22 stsp return got_error_from_errno();
349 876c234b 2018-09-10 stsp err = flush_imsg(ibuf);
351 876c234b 2018-09-10 stsp return err;
353 876c234b 2018-09-10 stsp SIMPLEQ_FOREACH(delta, &obj->deltas.entries, entry) {
354 876c234b 2018-09-10 stsp err = send_delta(delta, ibuf);
359 876c234b 2018-09-10 stsp return err;
362 876c234b 2018-09-10 stsp static const struct got_error *
363 876c234b 2018-09-10 stsp receive_delta(struct got_delta **delta, struct imsgbuf *ibuf)
365 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
366 876c234b 2018-09-10 stsp struct imsg imsg;
367 876c234b 2018-09-10 stsp struct got_imsg_delta idelta;
368 876c234b 2018-09-10 stsp uint8_t *delta_buf = NULL;
369 876c234b 2018-09-10 stsp const size_t min_datalen =
370 876c234b 2018-09-10 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_delta));
371 876c234b 2018-09-10 stsp size_t datalen, offset, remain;
373 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
375 876c234b 2018-09-10 stsp return err;
377 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
378 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_ERROR)
379 876c234b 2018-09-10 stsp return recv_imsg_error(&imsg, datalen);
381 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_DELTA)
382 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
383 876c234b 2018-09-10 stsp if (datalen != sizeof(idelta))
384 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
386 876c234b 2018-09-10 stsp memcpy(&idelta, imsg.data, sizeof(idelta));
387 876c234b 2018-09-10 stsp imsg_free(&imsg);
389 876c234b 2018-09-10 stsp switch (idelta.type) {
390 876c234b 2018-09-10 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
391 876c234b 2018-09-10 stsp case GOT_OBJ_TYPE_REF_DELTA:
392 876c234b 2018-09-10 stsp if (idelta.delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
393 876c234b 2018-09-10 stsp return got_error(GOT_ERR_BAD_DELTA);
396 876c234b 2018-09-10 stsp if (idelta.delta_len != 0)
397 876c234b 2018-09-10 stsp return got_error(GOT_ERR_BAD_DELTA);
401 876c234b 2018-09-10 stsp if (idelta.delta_len > 0) {
402 876c234b 2018-09-10 stsp delta_buf = calloc(1, idelta.delta_len);
403 876c234b 2018-09-10 stsp if (delta_buf == NULL)
404 876c234b 2018-09-10 stsp return got_error_from_errno();
406 876c234b 2018-09-10 stsp offset = 0;
407 876c234b 2018-09-10 stsp remain = idelta.delta_len;
408 876c234b 2018-09-10 stsp while (remain > 0) {
409 876c234b 2018-09-10 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
411 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, n);
413 876c234b 2018-09-10 stsp return err;
415 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_ERROR)
416 876c234b 2018-09-10 stsp return recv_imsg_error(&imsg, datalen);
418 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
421 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_DELTA_STREAM)
422 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
424 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
425 876c234b 2018-09-10 stsp if (datalen != n)
426 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
428 876c234b 2018-09-10 stsp memcpy(delta_buf + offset, imsg.data, n);
429 876c234b 2018-09-10 stsp imsg_free(&imsg);
431 876c234b 2018-09-10 stsp offset += n;
432 876c234b 2018-09-10 stsp remain -= n;
436 876c234b 2018-09-10 stsp *delta = got_delta_open(idelta.offset, idelta.tslen, idelta.type,
437 876c234b 2018-09-10 stsp idelta.size, idelta.data_offset, delta_buf, idelta.delta_len);
438 876c234b 2018-09-10 stsp if (*delta == NULL) {
439 876c234b 2018-09-10 stsp err = got_error_from_errno();
440 876c234b 2018-09-10 stsp free(delta_buf);
443 cfd633c2 2018-09-10 stsp return err;
446 cfd633c2 2018-09-10 stsp const struct got_error *
447 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
448 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
450 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
451 cfd633c2 2018-09-10 stsp struct got_imsg_object iobj;
452 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
455 cfd633c2 2018-09-10 stsp if (datalen != sizeof(iobj))
456 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
458 cfd633c2 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
459 cfd633c2 2018-09-10 stsp if (iobj.ndeltas < 0 ||
460 cfd633c2 2018-09-10 stsp iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX)
461 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
463 cfd633c2 2018-09-10 stsp if (iobj.ndeltas > 0 &&
464 cfd633c2 2018-09-10 stsp (iobj.flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
465 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
467 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
468 cfd633c2 2018-09-10 stsp if (*obj == NULL)
469 cfd633c2 2018-09-10 stsp return got_error_from_errno();
471 cfd633c2 2018-09-10 stsp (*obj)->type = iobj.type;
472 cfd633c2 2018-09-10 stsp (*obj)->flags = iobj.flags;
473 cfd633c2 2018-09-10 stsp (*obj)->hdrlen = iobj.hdrlen;
474 cfd633c2 2018-09-10 stsp (*obj)->size = iobj.size;
475 cfd633c2 2018-09-10 stsp /* id and path_packfile might be copied in by caller */
476 cfd633c2 2018-09-10 stsp (*obj)->pack_offset = iobj.pack_offset;
477 cfd633c2 2018-09-10 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
478 cfd633c2 2018-09-10 stsp for (i = 0; i < iobj.ndeltas; i++) {
479 cfd633c2 2018-09-10 stsp struct got_delta *delta;
480 cfd633c2 2018-09-10 stsp err = receive_delta(&delta, ibuf);
483 cfd633c2 2018-09-10 stsp (*obj)->deltas.nentries++;
484 cfd633c2 2018-09-10 stsp SIMPLEQ_INSERT_TAIL(&(*obj)->deltas.entries, delta,
488 876c234b 2018-09-10 stsp return err;
491 2178c42e 2018-04-22 stsp const struct got_error *
492 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
494 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
495 2178c42e 2018-04-22 stsp struct imsg imsg;
496 2178c42e 2018-04-22 stsp size_t datalen;
497 c4eae628 2018-04-23 stsp const size_t min_datalen =
498 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
500 2178c42e 2018-04-22 stsp *obj = NULL;
502 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
504 2178c42e 2018-04-22 stsp return err;
506 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
508 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
509 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
510 c4eae628 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
512 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
513 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
516 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
520 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
522 bff6ca00 2018-04-23 stsp return err;
525 bff6ca00 2018-04-23 stsp const struct got_error *
526 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
528 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
529 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
530 bff6ca00 2018-04-23 stsp uint8_t *buf;
531 bff6ca00 2018-04-23 stsp size_t len, total;
532 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
534 86acc566 2018-04-23 stsp memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
535 bff6ca00 2018-04-23 stsp icommit.author_len = strlen(commit->author);
536 788c352e 2018-06-16 stsp memcpy(&icommit.tm_author, &commit->tm_author,
537 788c352e 2018-06-16 stsp sizeof(icommit.tm_author));
538 bff6ca00 2018-04-23 stsp icommit.committer_len = strlen(commit->committer);
539 788c352e 2018-06-16 stsp memcpy(&icommit.tm_committer, &commit->tm_committer,
540 788c352e 2018-06-16 stsp sizeof(icommit.tm_committer));
541 bff6ca00 2018-04-23 stsp icommit.logmsg_len = strlen(commit->logmsg);
542 bff6ca00 2018-04-23 stsp icommit.nparents = commit->nparents;
544 bff6ca00 2018-04-23 stsp total = sizeof(icommit) + icommit.author_len +
545 788c352e 2018-06-16 stsp icommit.committer_len + icommit.logmsg_len +
546 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH;
547 bff6ca00 2018-04-23 stsp /* XXX TODO support very large log messages properly */
548 bff6ca00 2018-04-23 stsp if (total > MAX_IMSGSIZE)
549 bff6ca00 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
551 bff6ca00 2018-04-23 stsp buf = malloc(total);
552 bff6ca00 2018-04-23 stsp if (buf == NULL)
553 bff6ca00 2018-04-23 stsp return got_error_from_errno();
556 bff6ca00 2018-04-23 stsp memcpy(buf + len, &icommit, sizeof(icommit));
557 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
558 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->author, icommit.author_len);
559 bff6ca00 2018-04-23 stsp len += icommit.author_len;
560 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->committer, icommit.committer_len);
561 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
562 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
563 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
564 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
565 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
566 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
569 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
570 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
574 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
577 bff6ca00 2018-04-23 stsp return err;
580 bff6ca00 2018-04-23 stsp const struct got_error *
581 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
583 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
584 bff6ca00 2018-04-23 stsp struct imsg imsg;
585 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
586 bff6ca00 2018-04-23 stsp size_t len, datalen;
588 bff6ca00 2018-04-23 stsp const size_t min_datalen =
589 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
590 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
591 bff6ca00 2018-04-23 stsp uint8_t *data;
593 bff6ca00 2018-04-23 stsp *commit = NULL;
595 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
597 bff6ca00 2018-04-23 stsp return err;
599 bff6ca00 2018-04-23 stsp data = imsg.data;
600 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
603 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
604 bff6ca00 2018-04-23 stsp case GOT_IMSG_ERROR:
605 bff6ca00 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
607 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
608 bff6ca00 2018-04-23 stsp if (datalen < sizeof(icommit)) {
609 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
613 bff6ca00 2018-04-23 stsp memcpy(&icommit, data, sizeof(icommit));
614 bff6ca00 2018-04-23 stsp if (datalen != sizeof(icommit) + icommit.author_len +
615 788c352e 2018-06-16 stsp icommit.committer_len + icommit.logmsg_len +
616 86acc566 2018-04-23 stsp icommit.nparents * SHA1_DIGEST_LENGTH) {
617 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
620 bff6ca00 2018-04-23 stsp if (icommit.nparents < 0) {
621 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
624 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
626 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
627 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
628 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
632 86acc566 2018-04-23 stsp memcpy((*commit)->tree_id->sha1, icommit.tree_id,
633 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
634 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_author, &icommit.tm_author,
635 788c352e 2018-06-16 stsp sizeof((*commit)->tm_author));
636 788c352e 2018-06-16 stsp memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
637 788c352e 2018-06-16 stsp sizeof((*commit)->tm_committer));
639 bff6ca00 2018-04-23 stsp if (icommit.author_len == 0) {
640 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
641 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
642 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
646 bff6ca00 2018-04-23 stsp (*commit)->author = malloc(icommit.author_len + 1);
647 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
648 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
651 bff6ca00 2018-04-23 stsp memcpy((*commit)->author, data + len,
652 bff6ca00 2018-04-23 stsp icommit.author_len);
653 bff6ca00 2018-04-23 stsp (*commit)->author[icommit.author_len] = '\0';
655 bff6ca00 2018-04-23 stsp len += icommit.author_len;
657 bff6ca00 2018-04-23 stsp if (icommit.committer_len == 0) {
658 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
659 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
660 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
664 bff6ca00 2018-04-23 stsp (*commit)->committer =
665 bff6ca00 2018-04-23 stsp malloc(icommit.committer_len + 1);
666 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
667 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
670 bff6ca00 2018-04-23 stsp memcpy((*commit)->committer, data + len,
671 bff6ca00 2018-04-23 stsp icommit.committer_len);
672 bff6ca00 2018-04-23 stsp (*commit)->committer[icommit.committer_len] = '\0';
674 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
676 bff6ca00 2018-04-23 stsp if (icommit.logmsg_len == 0) {
677 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
678 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
679 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
683 bff6ca00 2018-04-23 stsp (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
684 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
685 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
688 bff6ca00 2018-04-23 stsp memcpy((*commit)->logmsg, data + len,
689 bff6ca00 2018-04-23 stsp icommit.logmsg_len);
690 bff6ca00 2018-04-23 stsp (*commit)->logmsg[icommit.logmsg_len] = '\0';
692 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
694 bff6ca00 2018-04-23 stsp for (i = 0; i < icommit.nparents; i++) {
695 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
697 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
698 79f35eb3 2018-06-11 stsp if (qid == NULL) {
699 86acc566 2018-04-23 stsp err = got_error_from_errno();
702 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
703 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
704 86acc566 2018-04-23 stsp err = got_error_from_errno();
709 79f35eb3 2018-06-11 stsp memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
710 79f35eb3 2018-06-11 stsp sizeof(*qid->id));
711 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
712 86acc566 2018-04-23 stsp (*commit)->nparents++;
716 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
720 2178c42e 2018-04-22 stsp imsg_free(&imsg);
722 e033d803 2018-04-23 stsp return err;
725 e033d803 2018-04-23 stsp const struct got_error *
726 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
728 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
729 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
730 e033d803 2018-04-23 stsp struct got_tree_entry *te;
732 883f0469 2018-06-23 stsp itree.nentries = tree->entries.nentries;
733 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
735 e033d803 2018-04-23 stsp return got_error_from_errno();
737 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
739 e033d803 2018-04-23 stsp return err;
741 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
742 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
743 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
744 e033d803 2018-04-23 stsp size_t len = sizeof(ite) + strlen(te->name);
746 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
747 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
749 e033d803 2018-04-23 stsp buf = malloc(len);
750 e033d803 2018-04-23 stsp if (buf == NULL)
751 e033d803 2018-04-23 stsp return got_error_from_errno();
753 e033d803 2018-04-23 stsp memcpy(ite.id, te->id->sha1, sizeof(ite.id));
754 e033d803 2018-04-23 stsp ite.mode = te->mode;
755 e033d803 2018-04-23 stsp memcpy(buf, &ite, sizeof(ite));
756 e033d803 2018-04-23 stsp memcpy(buf + sizeof(ite), te->name, strlen(te->name));
758 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
759 e033d803 2018-04-23 stsp buf, len) == -1)
760 e033d803 2018-04-23 stsp err = got_error_from_errno();
763 e033d803 2018-04-23 stsp return err;
765 e033d803 2018-04-23 stsp err = flush_imsg(ibuf);
767 e033d803 2018-04-23 stsp return err;
770 e033d803 2018-04-23 stsp return NULL;
773 e033d803 2018-04-23 stsp const struct got_error *
774 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
776 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
777 e033d803 2018-04-23 stsp const size_t min_datalen =
778 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
779 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
780 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree = { 0 };
781 e033d803 2018-04-23 stsp int nentries = 0;
783 e033d803 2018-04-23 stsp *tree = NULL;
785 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
789 e033d803 2018-04-23 stsp while (1) {
790 e033d803 2018-04-23 stsp struct imsg imsg;
792 e033d803 2018-04-23 stsp size_t datalen;
793 e033d803 2018-04-23 stsp struct got_imsg_tree_entry ite;
794 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
796 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
797 e033d803 2018-04-23 stsp if (n == 0) {
798 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries)
799 e033d803 2018-04-23 stsp goto get_more;
803 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
804 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
806 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
808 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
809 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
810 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
812 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
813 e033d803 2018-04-23 stsp /* This message should only appear once. */
814 e033d803 2018-04-23 stsp if (*tree != NULL) {
815 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
818 e033d803 2018-04-23 stsp if (datalen != sizeof(itree)) {
819 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
822 e033d803 2018-04-23 stsp memcpy(&itree, imsg.data, sizeof(itree));
823 e033d803 2018-04-23 stsp *tree = calloc(1, sizeof(**tree));
824 e033d803 2018-04-23 stsp if (*tree == NULL) {
825 e033d803 2018-04-23 stsp err = got_error_from_errno();
828 883f0469 2018-06-23 stsp (*tree)->entries.nentries = itree.nentries;
829 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
831 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
832 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
833 e033d803 2018-04-23 stsp if (*tree == NULL) {
834 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
837 e033d803 2018-04-23 stsp if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
838 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
842 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
843 e033d803 2018-04-23 stsp datalen -= sizeof(ite);
844 e033d803 2018-04-23 stsp memcpy(&ite, imsg.data, sizeof(ite));
845 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
846 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
850 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
851 e033d803 2018-04-23 stsp if (te == NULL) {
852 e033d803 2018-04-23 stsp err = got_error_from_errno();
855 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
856 e033d803 2018-04-23 stsp if (te->name == NULL) {
858 e033d803 2018-04-23 stsp err = got_error_from_errno();
861 052d4dc3 2018-04-23 stsp memcpy(te->name, imsg.data + sizeof(ite), datalen);
862 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
864 e033d803 2018-04-23 stsp memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
865 e033d803 2018-04-23 stsp te->mode = ite.mode;
866 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
867 e033d803 2018-04-23 stsp nentries++;
870 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
874 e033d803 2018-04-23 stsp imsg_free(&imsg);
877 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries) {
878 1e51f5b9 2018-04-23 stsp if (err == NULL)
879 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
880 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
881 e033d803 2018-04-23 stsp *tree = NULL;
884 ff6b18f8 2018-04-24 stsp return err;
887 ff6b18f8 2018-04-24 stsp const struct got_error *
888 2967a784 2018-04-24 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
890 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
892 2967a784 2018-04-24 stsp iblob.size = size;
893 ff6b18f8 2018-04-24 stsp /* Data has already been written to file descriptor. */
895 2967a784 2018-04-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
897 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
899 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
902 ff6b18f8 2018-04-24 stsp const struct got_error *
903 2967a784 2018-04-24 stsp got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
905 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
906 ff6b18f8 2018-04-24 stsp struct imsg imsg;
907 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
908 ff6b18f8 2018-04-24 stsp size_t datalen;
910 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
912 ff6b18f8 2018-04-24 stsp return err;
914 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
916 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
917 ff6b18f8 2018-04-24 stsp case GOT_IMSG_ERROR:
918 ff6b18f8 2018-04-24 stsp err = recv_imsg_error(&imsg, datalen);
920 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
921 2967a784 2018-04-24 stsp if (datalen != sizeof(iblob))
922 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
923 2967a784 2018-04-24 stsp memcpy(&iblob, imsg.data, sizeof(iblob));
924 2967a784 2018-04-24 stsp *size = iblob.size;
925 ff6b18f8 2018-04-24 stsp /* Data has been written to file descriptor. */
928 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
932 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
934 2178c42e 2018-04-22 stsp return err;
937 876c234b 2018-09-10 stsp const struct got_error *
938 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
939 876c234b 2018-09-10 stsp struct got_packidx *packidx)
941 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
942 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
945 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
946 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
947 876c234b 2018-09-10 stsp if (fd == -1)
948 876c234b 2018-09-10 stsp return got_error_from_errno();
950 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
951 876c234b 2018-09-10 stsp sizeof(ipackidx)) == -1)
952 876c234b 2018-09-10 stsp return got_error_from_errno();
954 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
955 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
956 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
957 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
959 876c234b 2018-09-10 stsp fd = dup(pack->fd);
960 876c234b 2018-09-10 stsp if (fd == -1)
961 876c234b 2018-09-10 stsp return got_error_from_errno();
963 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
965 876c234b 2018-09-10 stsp return got_error_from_errno();
967 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
970 876c234b 2018-09-10 stsp const struct got_error *
971 876c234b 2018-09-10 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx)
973 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
975 876c234b 2018-09-10 stsp iobj.idx = idx;
977 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
978 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
979 876c234b 2018-09-10 stsp return got_error_from_errno();
981 876c234b 2018-09-10 stsp return flush_imsg(ibuf);