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 73b7854a 2018-11-11 stsp static const struct got_error *
110 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
112 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
114 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
115 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
117 73b7854a 2018-11-11 stsp ierr = imsg->data;
118 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
119 73b7854a 2018-11-11 stsp static struct got_error serr;
120 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
121 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
122 73b7854a 2018-11-11 stsp return &serr;
125 73b7854a 2018-11-11 stsp return got_error(ierr->code);
128 876c234b 2018-09-10 stsp const struct got_error *
129 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
130 46de5bfd 2018-11-11 stsp size_t min_datalen)
132 e033d803 2018-04-23 stsp const struct got_error *err;
135 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
136 876c234b 2018-09-10 stsp if (n == -1)
137 876c234b 2018-09-10 stsp return got_error_from_errno();
139 876c234b 2018-09-10 stsp while (n == 0) {
140 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
142 876c234b 2018-09-10 stsp return err;
143 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
146 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
147 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
149 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
150 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
151 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
154 73b7854a 2018-11-11 stsp return NULL;
157 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
159 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
161 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
162 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
165 2178c42e 2018-04-22 stsp ierr.code = err->code;
166 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
167 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
169 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
170 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
171 e93cd828 2018-11-11 stsp if (ret == -1) {
172 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
173 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
177 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
178 5d43e84d 2018-04-23 stsp if (poll_err) {
179 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
180 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
184 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
185 5d43e84d 2018-04-23 stsp if (ret == -1) {
186 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
187 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
192 e033d803 2018-04-23 stsp static const struct got_error *
193 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
195 e033d803 2018-04-23 stsp const struct got_error *err;
197 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
199 e033d803 2018-04-23 stsp return err;
201 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
202 e033d803 2018-04-23 stsp return got_error_from_errno();
204 e033d803 2018-04-23 stsp return NULL;
207 ad242220 2018-09-08 stsp const struct got_error *
208 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
210 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
211 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
213 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
215 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
216 ad242220 2018-09-08 stsp return got_error_from_errno();
218 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
219 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
220 ad242220 2018-09-08 stsp return err;
223 ad242220 2018-09-08 stsp const struct got_error *
224 ad242220 2018-09-08 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
226 ad242220 2018-09-08 stsp struct got_imsg_object iobj, *iobjp = NULL;
227 ad242220 2018-09-08 stsp size_t iobj_size = 0;
228 ad242220 2018-09-08 stsp int imsg_code = GOT_IMSG_OBJECT_REQUEST;
231 ad242220 2018-09-08 stsp switch (obj->type) {
232 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_TREE:
233 ad242220 2018-09-08 stsp imsg_code = GOT_IMSG_TREE_REQUEST;
235 ad242220 2018-09-08 stsp case GOT_OBJ_TYPE_COMMIT:
236 1785f84a 2018-12-23 stsp abort(); /* should not get here */
238 55da3778 2018-09-10 stsp case GOT_OBJ_TYPE_BLOB:
239 55da3778 2018-09-10 stsp imsg_code = GOT_IMSG_BLOB_REQUEST;
241 f4a881ce 2018-11-17 stsp case GOT_OBJ_TYPE_TAG:
242 f4a881ce 2018-11-17 stsp imsg_code = GOT_IMSG_TAG_REQUEST;
245 ad242220 2018-09-08 stsp return got_error(GOT_ERR_OBJ_TYPE);
248 41fa1437 2018-11-05 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
249 41fa1437 2018-11-05 stsp iobj.type = obj->type;
250 41fa1437 2018-11-05 stsp iobj.flags = obj->flags;
251 41fa1437 2018-11-05 stsp iobj.hdrlen = obj->hdrlen;
252 41fa1437 2018-11-05 stsp iobj.size = obj->size;
253 41fa1437 2018-11-05 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
254 41fa1437 2018-11-05 stsp iobj.pack_offset = obj->pack_offset;
255 41fa1437 2018-11-05 stsp iobj.pack_idx = obj->pack_idx;
258 ad242220 2018-09-08 stsp iobjp = &iobj;
259 ad242220 2018-09-08 stsp iobj_size = sizeof(iobj);
262 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
263 1785f84a 2018-12-23 stsp return got_error_from_errno();
265 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
268 1785f84a 2018-12-23 stsp const struct got_error *
269 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
270 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
272 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
273 1785f84a 2018-12-23 stsp size_t len;
275 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
276 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
277 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
278 1785f84a 2018-12-23 stsp iobjp = &iobj;
279 1785f84a 2018-12-23 stsp len = sizeof(iobj);
281 1785f84a 2018-12-23 stsp iobjp = NULL;
285 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
287 7762fe12 2018-11-05 stsp return got_error_from_errno();
289 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
292 7762fe12 2018-11-05 stsp const struct got_error *
293 55da3778 2018-09-10 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
295 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
297 ad242220 2018-09-08 stsp return got_error_from_errno();
299 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
302 55da3778 2018-09-10 stsp const struct got_error *
303 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
305 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
307 3840f4c9 2018-09-12 stsp return got_error_from_errno();
309 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
312 3840f4c9 2018-09-12 stsp const struct got_error *
313 3840f4c9 2018-09-12 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
315 3840f4c9 2018-09-12 stsp if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
317 ad242220 2018-09-08 stsp return got_error_from_errno();
319 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
322 ad242220 2018-09-08 stsp const struct got_error *
323 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
325 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
327 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
328 2178c42e 2018-04-22 stsp iobj.type = obj->type;
329 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
330 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
331 2178c42e 2018-04-22 stsp iobj.size = obj->size;
332 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
333 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
334 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
337 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
339 2178c42e 2018-04-22 stsp return got_error_from_errno();
341 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
344 cfd633c2 2018-09-10 stsp const struct got_error *
345 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
346 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
348 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
349 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
350 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
352 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
353 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
354 291624d8 2018-11-07 stsp iobj = imsg->data;
356 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
357 cfd633c2 2018-09-10 stsp if (*obj == NULL)
358 cfd633c2 2018-09-10 stsp return got_error_from_errno();
360 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
361 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
362 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
363 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
364 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
365 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
366 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
367 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
368 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
371 876c234b 2018-09-10 stsp return err;
374 2178c42e 2018-04-22 stsp const struct got_error *
375 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
377 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
378 2178c42e 2018-04-22 stsp struct imsg imsg;
379 2178c42e 2018-04-22 stsp size_t datalen;
380 c4eae628 2018-04-23 stsp const size_t min_datalen =
381 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
383 2178c42e 2018-04-22 stsp *obj = NULL;
385 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
387 2178c42e 2018-04-22 stsp return err;
389 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
391 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
392 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
393 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
396 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
400 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
402 bff6ca00 2018-04-23 stsp return err;
405 c75f7264 2018-09-11 stsp static const struct got_error *
406 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
407 c75f7264 2018-09-11 stsp size_t logmsg_len)
409 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
410 c75f7264 2018-09-11 stsp size_t offset, remain;
412 c75f7264 2018-09-11 stsp offset = 0;
413 c75f7264 2018-09-11 stsp remain = logmsg_len;
414 c75f7264 2018-09-11 stsp while (remain > 0) {
415 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
417 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
418 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
419 fa4ffeb3 2018-11-04 stsp err = got_error_from_errno();
423 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
427 c75f7264 2018-09-11 stsp offset += n;
428 c75f7264 2018-09-11 stsp remain -= n;
431 fa4ffeb3 2018-11-04 stsp return err;
434 bff6ca00 2018-04-23 stsp const struct got_error *
435 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
437 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
438 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
439 bff6ca00 2018-04-23 stsp uint8_t *buf;
440 bff6ca00 2018-04-23 stsp size_t len, total;
441 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
442 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
443 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
444 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
446 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
447 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
449 bff6ca00 2018-04-23 stsp buf = malloc(total);
450 bff6ca00 2018-04-23 stsp if (buf == NULL)
451 bff6ca00 2018-04-23 stsp return got_error_from_errno();
453 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
454 b9c33926 2018-11-07 stsp memcpy(icommit->tree_id, commit->tree_id->sha1, sizeof(icommit->tree_id));
455 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
456 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
457 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
458 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
459 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
460 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
461 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
462 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
464 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
465 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
466 b9c33926 2018-11-07 stsp len += author_len;
467 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
468 b9c33926 2018-11-07 stsp len += committer_len;
469 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
470 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
471 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
474 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
475 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
479 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
480 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
481 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
485 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
488 bff6ca00 2018-04-23 stsp return err;
491 bff6ca00 2018-04-23 stsp const struct got_error *
492 068fd2bf 2018-04-24 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
494 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
495 bff6ca00 2018-04-23 stsp struct imsg imsg;
496 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
497 bff6ca00 2018-04-23 stsp size_t len, datalen;
499 bff6ca00 2018-04-23 stsp const size_t min_datalen =
500 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
501 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
503 bff6ca00 2018-04-23 stsp *commit = NULL;
505 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
507 bff6ca00 2018-04-23 stsp return err;
509 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
512 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
513 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
514 291624d8 2018-11-07 stsp if (datalen < sizeof(*icommit)) {
515 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
518 291624d8 2018-11-07 stsp icommit = imsg.data;
519 291624d8 2018-11-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
520 291624d8 2018-11-07 stsp icommit->committer_len +
521 291624d8 2018-11-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH) {
522 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
525 291624d8 2018-11-07 stsp if (icommit->nparents < 0) {
526 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
529 291624d8 2018-11-07 stsp len += sizeof(*icommit);
531 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
532 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
533 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
537 291624d8 2018-11-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
538 86acc566 2018-04-23 stsp SHA1_DIGEST_LENGTH);
539 291624d8 2018-11-07 stsp (*commit)->author_time = icommit->author_time;
540 291624d8 2018-11-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
541 291624d8 2018-11-07 stsp (*commit)->committer_time = icommit->committer_time;
542 291624d8 2018-11-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
544 291624d8 2018-11-07 stsp if (icommit->author_len == 0) {
545 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
546 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
547 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
551 291624d8 2018-11-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
552 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
553 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
556 291624d8 2018-11-07 stsp memcpy((*commit)->author, imsg.data + len,
557 291624d8 2018-11-07 stsp icommit->author_len);
558 291624d8 2018-11-07 stsp (*commit)->author[icommit->author_len] = '\0';
560 291624d8 2018-11-07 stsp len += icommit->author_len;
562 291624d8 2018-11-07 stsp if (icommit->committer_len == 0) {
563 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
564 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
565 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
569 bff6ca00 2018-04-23 stsp (*commit)->committer =
570 291624d8 2018-11-07 stsp malloc(icommit->committer_len + 1);
571 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
572 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
575 291624d8 2018-11-07 stsp memcpy((*commit)->committer, imsg.data + len,
576 291624d8 2018-11-07 stsp icommit->committer_len);
577 291624d8 2018-11-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
579 291624d8 2018-11-07 stsp len += icommit->committer_len;
581 291624d8 2018-11-07 stsp if (icommit->logmsg_len == 0) {
582 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
583 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
584 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
588 291624d8 2018-11-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
590 291624d8 2018-11-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
591 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
592 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
595 c75f7264 2018-09-11 stsp while (remain > 0) {
596 c75f7264 2018-09-11 stsp struct imsg imsg_log;
597 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
600 c75f7264 2018-09-11 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
602 c75f7264 2018-09-11 stsp return err;
604 c75f7264 2018-09-11 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
605 c75f7264 2018-09-11 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
607 c75f7264 2018-09-11 stsp memcpy((*commit)->logmsg + offset,
608 c75f7264 2018-09-11 stsp imsg_log.data, n);
609 c75f7264 2018-09-11 stsp imsg_free(&imsg_log);
610 c75f7264 2018-09-11 stsp offset += n;
611 c75f7264 2018-09-11 stsp remain -= n;
613 291624d8 2018-11-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
616 291624d8 2018-11-07 stsp for (i = 0; i < icommit->nparents; i++) {
617 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
619 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
622 291624d8 2018-11-07 stsp memcpy(qid->id, imsg.data + len +
623 291624d8 2018-11-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
624 7762fe12 2018-11-05 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
625 7762fe12 2018-11-05 stsp (*commit)->nparents++;
629 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
633 2178c42e 2018-04-22 stsp imsg_free(&imsg);
635 e033d803 2018-04-23 stsp return err;
638 e033d803 2018-04-23 stsp const struct got_error *
639 068fd2bf 2018-04-24 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
641 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
642 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
643 e033d803 2018-04-23 stsp struct got_tree_entry *te;
644 b00c9821 2018-11-04 stsp size_t totlen;
645 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
647 883f0469 2018-06-23 stsp itree.nentries = tree->entries.nentries;
648 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
650 e033d803 2018-04-23 stsp return got_error_from_errno();
652 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
654 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
655 a58a49db 2018-11-07 stsp struct got_imsg_tree_entry *ite;
656 e033d803 2018-04-23 stsp uint8_t *buf = NULL;
657 a58a49db 2018-11-07 stsp size_t len = sizeof(*ite) + strlen(te->name);
659 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
660 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
663 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
664 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
666 b00c9821 2018-11-04 stsp return err;
670 e033d803 2018-04-23 stsp buf = malloc(len);
671 e033d803 2018-04-23 stsp if (buf == NULL)
672 e033d803 2018-04-23 stsp return got_error_from_errno();
674 a58a49db 2018-11-07 stsp ite = (struct got_imsg_tree_entry *)buf;
675 a58a49db 2018-11-07 stsp memcpy(ite->id, te->id->sha1, sizeof(ite->id));
676 a58a49db 2018-11-07 stsp ite->mode = te->mode;
677 a58a49db 2018-11-07 stsp memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
679 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
680 e033d803 2018-04-23 stsp buf, len) == -1)
681 e033d803 2018-04-23 stsp err = got_error_from_errno();
684 e033d803 2018-04-23 stsp return err;
685 b00c9821 2018-11-04 stsp totlen += len;
688 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
691 e033d803 2018-04-23 stsp const struct got_error *
692 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
694 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
695 e033d803 2018-04-23 stsp const size_t min_datalen =
696 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
697 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
698 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
699 e033d803 2018-04-23 stsp int nentries = 0;
701 e033d803 2018-04-23 stsp *tree = NULL;
703 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
707 e033d803 2018-04-23 stsp while (1) {
708 e033d803 2018-04-23 stsp struct imsg imsg;
710 e033d803 2018-04-23 stsp size_t datalen;
711 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
712 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
714 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
715 e033d803 2018-04-23 stsp if (n == 0) {
716 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries)
717 e033d803 2018-04-23 stsp goto get_more;
721 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
722 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
724 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
726 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
727 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
728 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
730 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
731 e033d803 2018-04-23 stsp /* This message should only appear once. */
732 e033d803 2018-04-23 stsp if (*tree != NULL) {
733 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
736 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
737 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
740 291624d8 2018-11-07 stsp itree = imsg.data;
741 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
742 e033d803 2018-04-23 stsp if (*tree == NULL) {
743 e033d803 2018-04-23 stsp err = got_error_from_errno();
746 291624d8 2018-11-07 stsp (*tree)->entries.nentries = itree->nentries;
747 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
748 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
750 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
751 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
752 e033d803 2018-04-23 stsp if (*tree == NULL) {
753 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
756 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
757 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
761 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
762 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
763 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
764 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
767 c0588d8d 2018-11-07 stsp ite = imsg.data;
769 e033d803 2018-04-23 stsp te = got_alloc_tree_entry_partial();
770 e033d803 2018-04-23 stsp if (te == NULL) {
771 e033d803 2018-04-23 stsp err = got_error_from_errno();
774 e033d803 2018-04-23 stsp te->name = malloc(datalen + 1);
775 e033d803 2018-04-23 stsp if (te->name == NULL) {
777 e033d803 2018-04-23 stsp err = got_error_from_errno();
780 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
781 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
783 c0588d8d 2018-11-07 stsp memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
784 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
785 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
786 e033d803 2018-04-23 stsp nentries++;
789 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
793 e033d803 2018-04-23 stsp imsg_free(&imsg);
796 883f0469 2018-06-23 stsp if (*tree && (*tree)->entries.nentries != nentries) {
797 1e51f5b9 2018-04-23 stsp if (err == NULL)
798 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
799 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
800 e033d803 2018-04-23 stsp *tree = NULL;
803 ff6b18f8 2018-04-24 stsp return err;
806 ff6b18f8 2018-04-24 stsp const struct got_error *
807 2967a784 2018-04-24 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
809 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
811 2967a784 2018-04-24 stsp iblob.size = size;
812 ff6b18f8 2018-04-24 stsp /* Data has already been written to file descriptor. */
814 2967a784 2018-04-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
816 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
818 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
821 ff6b18f8 2018-04-24 stsp const struct got_error *
822 2967a784 2018-04-24 stsp got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
824 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
825 ff6b18f8 2018-04-24 stsp struct imsg imsg;
826 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
827 ff6b18f8 2018-04-24 stsp size_t datalen;
829 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
831 ff6b18f8 2018-04-24 stsp return err;
833 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
835 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
836 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
837 291624d8 2018-11-07 stsp if (datalen != sizeof(*iblob)) {
838 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
841 291624d8 2018-11-07 stsp iblob = imsg.data;
842 291624d8 2018-11-07 stsp *size = iblob->size;
843 ff6b18f8 2018-04-24 stsp /* Data has been written to file descriptor. */
846 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
850 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
852 f4a881ce 2018-11-17 stsp return err;
855 f4a881ce 2018-11-17 stsp static const struct got_error *
856 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
858 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
859 f4a881ce 2018-11-17 stsp size_t offset, remain;
861 f4a881ce 2018-11-17 stsp offset = 0;
862 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
863 f4a881ce 2018-11-17 stsp while (remain > 0) {
864 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
866 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
867 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
868 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
872 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
876 f4a881ce 2018-11-17 stsp offset += n;
877 f4a881ce 2018-11-17 stsp remain -= n;
880 f4a881ce 2018-11-17 stsp return err;
883 f4a881ce 2018-11-17 stsp const struct got_error *
884 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
886 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
887 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
888 f4a881ce 2018-11-17 stsp uint8_t *buf;
889 f4a881ce 2018-11-17 stsp size_t len, total;
890 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
891 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
892 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
894 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
896 f4a881ce 2018-11-17 stsp buf = malloc(total);
897 f4a881ce 2018-11-17 stsp if (buf == NULL)
898 f4a881ce 2018-11-17 stsp return got_error_from_errno();
900 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
901 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
902 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
903 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
904 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
905 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
906 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
907 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
909 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
910 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
911 f4a881ce 2018-11-17 stsp len += tag_len;
912 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
913 f4a881ce 2018-11-17 stsp len += tagger_len;
915 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
916 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
920 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
921 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
922 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
926 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
929 f4a881ce 2018-11-17 stsp return err;
932 f4a881ce 2018-11-17 stsp const struct got_error *
933 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
935 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
936 f4a881ce 2018-11-17 stsp struct imsg imsg;
937 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
938 f4a881ce 2018-11-17 stsp size_t len, datalen;
939 f4a881ce 2018-11-17 stsp const size_t min_datalen =
940 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
941 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
943 f4a881ce 2018-11-17 stsp *tag = NULL;
945 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
947 f4a881ce 2018-11-17 stsp return err;
949 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
952 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
953 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
954 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
955 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
958 f4a881ce 2018-11-17 stsp itag = imsg.data;
959 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
960 f4a881ce 2018-11-17 stsp itag->tagger_len) {
961 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
964 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
966 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
967 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
968 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
972 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
974 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
975 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
976 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
977 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
981 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
982 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
983 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
986 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
987 f4a881ce 2018-11-17 stsp itag->tag_len);
988 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
990 f4a881ce 2018-11-17 stsp len += itag->tag_len;
992 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
993 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
994 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
996 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
997 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
998 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
999 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
1003 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1004 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1005 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
1008 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1009 f4a881ce 2018-11-17 stsp itag->tagger_len);
1010 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1012 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1014 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1015 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1016 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1017 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
1021 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1023 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1024 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1025 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
1028 f4a881ce 2018-11-17 stsp while (remain > 0) {
1029 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1030 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1033 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1035 f4a881ce 2018-11-17 stsp return err;
1037 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1038 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1040 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1042 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1043 f4a881ce 2018-11-17 stsp offset += n;
1044 f4a881ce 2018-11-17 stsp remain -= n;
1046 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1051 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1055 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1057 2178c42e 2018-04-22 stsp return err;
1060 876c234b 2018-09-10 stsp const struct got_error *
1061 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1062 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1064 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1065 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1068 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1069 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1070 876c234b 2018-09-10 stsp if (fd == -1)
1071 876c234b 2018-09-10 stsp return got_error_from_errno();
1073 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1074 876c234b 2018-09-10 stsp sizeof(ipackidx)) == -1)
1075 876c234b 2018-09-10 stsp return got_error_from_errno();
1077 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1078 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1079 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1080 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1082 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1083 876c234b 2018-09-10 stsp if (fd == -1)
1084 876c234b 2018-09-10 stsp return got_error_from_errno();
1086 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1088 876c234b 2018-09-10 stsp return got_error_from_errno();
1090 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1093 876c234b 2018-09-10 stsp const struct got_error *
1094 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1095 106807b4 2018-09-15 stsp struct got_object_id *id)
1097 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1099 876c234b 2018-09-10 stsp iobj.idx = idx;
1100 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1102 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1103 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1104 876c234b 2018-09-10 stsp return got_error_from_errno();
1106 876c234b 2018-09-10 stsp return flush_imsg(ibuf);