2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 2178c42e 2018-04-22 stsp * Permission to use, copy, modify, and distribute this software for any
6 2178c42e 2018-04-22 stsp * purpose with or without fee is hereby granted, provided that the above
7 2178c42e 2018-04-22 stsp * copyright notice and this permission notice appear in all copies.
9 2178c42e 2018-04-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2178c42e 2018-04-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2178c42e 2018-04-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2178c42e 2018-04-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2178c42e 2018-04-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2178c42e 2018-04-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2178c42e 2018-04-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 2178c42e 2018-04-22 stsp #include <sys/types.h>
19 2178c42e 2018-04-22 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
22 876c234b 2018-09-10 stsp #include <sys/wait.h>
24 2178c42e 2018-04-22 stsp #include <stdio.h>
25 2178c42e 2018-04-22 stsp #include <stdlib.h>
26 2178c42e 2018-04-22 stsp #include <string.h>
27 2178c42e 2018-04-22 stsp #include <errno.h>
28 2178c42e 2018-04-22 stsp #include <stdint.h>
29 2178c42e 2018-04-22 stsp #include <poll.h>
30 2178c42e 2018-04-22 stsp #include <imsg.h>
31 2178c42e 2018-04-22 stsp #include <sha1.h>
32 2178c42e 2018-04-22 stsp #include <zlib.h>
33 788c352e 2018-06-16 stsp #include <time.h>
35 2178c42e 2018-04-22 stsp #include "got_object.h"
36 2178c42e 2018-04-22 stsp #include "got_error.h"
37 3022d272 2019-11-14 stsp #include "got_path.h"
38 cd95becd 2019-11-29 stsp #include "got_repository.h"
40 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
41 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
42 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
44 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
46 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
49 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 c39c25dd 2019-08-09 stsp #ifndef nitems
53 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 2178c42e 2018-04-22 stsp static const struct got_error *
57 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
59 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
62 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
63 2178c42e 2018-04-22 stsp pfd[0].events = events;
65 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
66 2178c42e 2018-04-22 stsp if (n == -1)
67 638f9024 2019-05-13 stsp return got_error_from_errno("poll");
69 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
70 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
71 638f9024 2019-05-13 stsp return got_error_from_errno("poll error");
72 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
73 2178c42e 2018-04-22 stsp return NULL;
75 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
78 c4eae628 2018-04-23 stsp static const struct got_error *
79 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
81 fe36cf76 2018-04-23 stsp const struct got_error *err;
84 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
88 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
89 fe36cf76 2018-04-23 stsp if (n == -1) {
90 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
91 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
92 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
95 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
97 e033d803 2018-04-23 stsp return NULL;
100 ad242220 2018-09-08 stsp const struct got_error *
101 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
103 876c234b 2018-09-10 stsp int child_status;
105 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
106 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
108 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
109 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
111 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
112 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
114 876c234b 2018-09-10 stsp return NULL;
117 73b7854a 2018-11-11 stsp static const struct got_error *
118 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
120 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
122 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
123 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
125 73b7854a 2018-11-11 stsp ierr = imsg->data;
126 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
127 73b7854a 2018-11-11 stsp static struct got_error serr;
128 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
129 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
130 73b7854a 2018-11-11 stsp return &serr;
133 73b7854a 2018-11-11 stsp return got_error(ierr->code);
136 876c234b 2018-09-10 stsp const struct got_error *
137 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
138 46de5bfd 2018-11-11 stsp size_t min_datalen)
140 e033d803 2018-04-23 stsp const struct got_error *err;
143 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
144 876c234b 2018-09-10 stsp if (n == -1)
145 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
147 876c234b 2018-09-10 stsp while (n == 0) {
148 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
150 876c234b 2018-09-10 stsp return err;
151 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
152 cbfaaf20 2020-01-06 stsp if (n == -1)
153 cbfaaf20 2020-01-06 stsp return got_error_from_errno("imsg_get");
156 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
157 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
159 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
160 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
161 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
164 73b7854a 2018-11-11 stsp return NULL;
167 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
169 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
171 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
172 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
175 2178c42e 2018-04-22 stsp ierr.code = err->code;
176 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
177 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
179 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
180 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
181 e93cd828 2018-11-11 stsp if (ret == -1) {
182 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
183 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
187 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
188 5d43e84d 2018-04-23 stsp if (poll_err) {
189 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
190 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
194 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
195 5d43e84d 2018-04-23 stsp if (ret == -1) {
196 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
197 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
202 e033d803 2018-04-23 stsp static const struct got_error *
203 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
205 e033d803 2018-04-23 stsp const struct got_error *err;
207 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
209 e033d803 2018-04-23 stsp return err;
211 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
212 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
214 e033d803 2018-04-23 stsp return NULL;
217 ad242220 2018-09-08 stsp const struct got_error *
218 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
220 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
221 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
223 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
225 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
226 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
228 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
229 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
230 ad242220 2018-09-08 stsp return err;
233 93658fb9 2020-03-18 stsp const struct got_error *
234 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
236 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
238 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
240 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
243 1785f84a 2018-12-23 stsp const struct got_error *
244 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
245 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
247 41496140 2019-02-21 stsp const struct got_error *err = NULL;
248 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
249 1785f84a 2018-12-23 stsp size_t len;
251 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
252 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
253 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
254 1785f84a 2018-12-23 stsp iobjp = &iobj;
255 1785f84a 2018-12-23 stsp len = sizeof(iobj);
257 1785f84a 2018-12-23 stsp iobjp = NULL;
261 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
263 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
265 41496140 2019-02-21 stsp return err;
268 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
271 13c729f7 2018-12-24 stsp const struct got_error *
272 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
273 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
275 41496140 2019-02-21 stsp const struct got_error *err = NULL;
276 7f358e3b 2019-11-23 stsp struct ibuf *wbuf;
277 7f358e3b 2019-11-23 stsp size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
279 7f358e3b 2019-11-23 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
280 7f358e3b 2019-11-23 stsp if (wbuf == NULL)
281 7f358e3b 2019-11-23 stsp return got_error_from_errno("imsg_create TREE_REQUEST");
283 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
284 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
285 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
286 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
287 7f358e3b 2019-11-23 stsp return err;
290 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
291 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
292 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
293 7f358e3b 2019-11-23 stsp return err;
297 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
298 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
300 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
303 268f7291 2018-12-24 stsp const struct got_error *
304 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
305 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
307 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
308 268f7291 2018-12-24 stsp size_t len;
310 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
311 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
312 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
313 268f7291 2018-12-24 stsp iobjp = &iobj;
314 268f7291 2018-12-24 stsp len = sizeof(iobj);
316 268f7291 2018-12-24 stsp iobjp = NULL;
320 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
322 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
324 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
327 7762fe12 2018-11-05 stsp const struct got_error *
328 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
329 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
331 41496140 2019-02-21 stsp const struct got_error *err = NULL;
332 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
333 ebc55e2d 2018-12-24 stsp size_t len;
335 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
336 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
337 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
338 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
339 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
341 ebc55e2d 2018-12-24 stsp iobjp = NULL;
345 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
347 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
348 41496140 2019-02-21 stsp close(infd);
349 41496140 2019-02-21 stsp return err;
352 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
355 55da3778 2018-09-10 stsp const struct got_error *
356 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
358 41496140 2019-02-21 stsp const struct got_error *err = NULL;
360 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
362 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
363 41496140 2019-02-21 stsp close(outfd);
364 41496140 2019-02-21 stsp return err;
367 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
370 3840f4c9 2018-09-12 stsp const struct got_error *
371 3840f4c9 2018-09-12 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
373 41496140 2019-02-21 stsp const struct got_error *err = NULL;
375 3840f4c9 2018-09-12 stsp if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
377 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
379 41496140 2019-02-21 stsp return err;
382 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
385 ad242220 2018-09-08 stsp const struct got_error *
386 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
388 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
390 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
391 2178c42e 2018-04-22 stsp iobj.type = obj->type;
392 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
393 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
394 2178c42e 2018-04-22 stsp iobj.size = obj->size;
395 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
396 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
397 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
400 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
402 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
404 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
407 cfd633c2 2018-09-10 stsp const struct got_error *
408 93658fb9 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd)
410 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
412 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
413 93658fb9 2020-03-18 stsp NULL, 0) == -1) {
414 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose FETCH_REQUEST");
416 93658fb9 2020-03-18 stsp return err;
418 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
421 93658fb9 2020-03-18 stsp const struct got_error *
422 93658fb9 2020-03-18 stsp got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
424 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
425 93658fb9 2020-03-18 stsp hash.sha1, SHA1_DIGEST_LENGTH) == -1)
426 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
427 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
430 93658fb9 2020-03-18 stsp const struct got_error *
431 93658fb9 2020-03-18 stsp got_privsep_wait_fetch_done(struct imsgbuf *ibuf, struct got_object_id *hash)
433 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
434 93658fb9 2020-03-18 stsp struct imsg imsg;
436 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
438 93658fb9 2020-03-18 stsp return err;
439 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_FETCH_DONE &&
440 93658fb9 2020-03-18 stsp imsg.hdr.len - sizeof(imsg.hdr) == SHA1_DIGEST_LENGTH)
441 93658fb9 2020-03-18 stsp return NULL;
443 93658fb9 2020-03-18 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
444 93658fb9 2020-03-18 stsp imsg_free(&imsg);
448 93658fb9 2020-03-18 stsp const struct got_error *
449 93658fb9 2020-03-18 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id hash)
451 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
453 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
454 93658fb9 2020-03-18 stsp hash.sha1, SHA1_DIGEST_LENGTH) == -1) {
455 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
457 93658fb9 2020-03-18 stsp return err;
459 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
462 93658fb9 2020-03-18 stsp const struct got_error *
463 93658fb9 2020-03-18 stsp got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
465 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
466 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
467 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
470 93658fb9 2020-03-18 stsp const struct got_error *
471 93658fb9 2020-03-18 stsp got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
473 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
474 93658fb9 2020-03-18 stsp struct imsg imsg;
476 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
478 93658fb9 2020-03-18 stsp return err;
479 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
480 93658fb9 2020-03-18 stsp return NULL;
482 93658fb9 2020-03-18 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
483 93658fb9 2020-03-18 stsp imsg_free(&imsg);
486 93658fb9 2020-03-18 stsp const struct got_error *
487 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
488 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
490 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
491 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
492 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
494 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
495 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
496 291624d8 2018-11-07 stsp iobj = imsg->data;
498 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
499 cfd633c2 2018-09-10 stsp if (*obj == NULL)
500 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
502 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
503 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
504 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
505 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
506 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
507 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
508 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
509 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
510 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
513 876c234b 2018-09-10 stsp return err;
516 2178c42e 2018-04-22 stsp const struct got_error *
517 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
519 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
520 2178c42e 2018-04-22 stsp struct imsg imsg;
521 c4eae628 2018-04-23 stsp const size_t min_datalen =
522 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
524 2178c42e 2018-04-22 stsp *obj = NULL;
526 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
528 2178c42e 2018-04-22 stsp return err;
530 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
531 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
532 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
535 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
539 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
541 bff6ca00 2018-04-23 stsp return err;
544 c75f7264 2018-09-11 stsp static const struct got_error *
545 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
546 c75f7264 2018-09-11 stsp size_t logmsg_len)
548 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
549 c75f7264 2018-09-11 stsp size_t offset, remain;
551 c75f7264 2018-09-11 stsp offset = 0;
552 c75f7264 2018-09-11 stsp remain = logmsg_len;
553 c75f7264 2018-09-11 stsp while (remain > 0) {
554 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
556 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
557 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
558 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
559 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
563 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
567 c75f7264 2018-09-11 stsp offset += n;
568 c75f7264 2018-09-11 stsp remain -= n;
571 fa4ffeb3 2018-11-04 stsp return err;
574 bff6ca00 2018-04-23 stsp const struct got_error *
575 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
577 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
578 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
579 bff6ca00 2018-04-23 stsp uint8_t *buf;
580 bff6ca00 2018-04-23 stsp size_t len, total;
581 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
582 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
583 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
584 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
586 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
587 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
589 bff6ca00 2018-04-23 stsp buf = malloc(total);
590 bff6ca00 2018-04-23 stsp if (buf == NULL)
591 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
593 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
594 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
595 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
596 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
597 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
598 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
599 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
600 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
601 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
602 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
603 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
605 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
606 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
607 b9c33926 2018-11-07 stsp len += author_len;
608 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
609 b9c33926 2018-11-07 stsp len += committer_len;
610 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
611 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
612 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
615 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
616 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
620 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
621 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
622 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
626 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
629 bff6ca00 2018-04-23 stsp return err;
632 ca6e02ac 2020-01-07 stsp static const struct got_error *
633 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
634 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
636 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
637 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
638 ca6e02ac 2020-01-07 stsp size_t len = 0;
641 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
642 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
644 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
645 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
646 ca6e02ac 2020-01-07 stsp icommit->committer_len +
647 ca6e02ac 2020-01-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH)
648 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
650 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
651 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
653 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
655 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
656 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
657 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
658 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
660 ca6e02ac 2020-01-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
661 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
662 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
663 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
664 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
665 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
667 ca6e02ac 2020-01-07 stsp if (icommit->author_len == 0) {
668 ca6e02ac 2020-01-07 stsp (*commit)->author = strdup("");
669 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
670 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
674 ca6e02ac 2020-01-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
675 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
676 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
679 ca6e02ac 2020-01-07 stsp memcpy((*commit)->author, imsg->data + len,
680 ca6e02ac 2020-01-07 stsp icommit->author_len);
681 ca6e02ac 2020-01-07 stsp (*commit)->author[icommit->author_len] = '\0';
683 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
685 ca6e02ac 2020-01-07 stsp if (icommit->committer_len == 0) {
686 ca6e02ac 2020-01-07 stsp (*commit)->committer = strdup("");
687 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
688 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
692 ca6e02ac 2020-01-07 stsp (*commit)->committer =
693 ca6e02ac 2020-01-07 stsp malloc(icommit->committer_len + 1);
694 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
695 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
698 ca6e02ac 2020-01-07 stsp memcpy((*commit)->committer, imsg->data + len,
699 ca6e02ac 2020-01-07 stsp icommit->committer_len);
700 ca6e02ac 2020-01-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
702 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
704 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
705 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
706 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
707 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
711 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
713 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
714 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
715 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
718 ca6e02ac 2020-01-07 stsp while (remain > 0) {
719 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
720 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
723 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
727 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
728 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
732 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
733 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
734 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
735 ca6e02ac 2020-01-07 stsp offset += n;
736 ca6e02ac 2020-01-07 stsp remain -= n;
738 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
741 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
742 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
744 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
747 ca6e02ac 2020-01-07 stsp memcpy(qid->id, imsg->data + len +
748 ca6e02ac 2020-01-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
749 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
750 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
754 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
755 ca6e02ac 2020-01-07 stsp *commit = NULL;
757 ca6e02ac 2020-01-07 stsp return err;
760 ca6e02ac 2020-01-07 stsp const struct got_error *
761 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
763 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
764 ca6e02ac 2020-01-07 stsp struct imsg imsg;
765 ca6e02ac 2020-01-07 stsp size_t datalen;
766 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
767 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
768 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
770 ca6e02ac 2020-01-07 stsp *commit = NULL;
772 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
774 ca6e02ac 2020-01-07 stsp return err;
776 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
778 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
779 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
780 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
783 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
787 2178c42e 2018-04-22 stsp imsg_free(&imsg);
789 e033d803 2018-04-23 stsp return err;
792 e033d803 2018-04-23 stsp const struct got_error *
793 3022d272 2019-11-14 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
794 3022d272 2019-11-14 stsp int nentries)
796 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
797 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
798 3022d272 2019-11-14 stsp struct got_pathlist_entry *pe;
799 b00c9821 2018-11-04 stsp size_t totlen;
800 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
802 3022d272 2019-11-14 stsp itree.nentries = nentries;
803 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
805 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
807 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
809 3022d272 2019-11-14 stsp TAILQ_FOREACH(pe, entries, entry) {
810 3022d272 2019-11-14 stsp const char *name = pe->path;
811 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte = pe->data;
812 3022d272 2019-11-14 stsp struct ibuf *wbuf;
813 3022d272 2019-11-14 stsp size_t namelen = strlen(name);
814 cd9e913a 2019-11-27 stsp size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
816 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
817 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
820 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
821 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
823 b00c9821 2018-11-04 stsp return err;
827 3022d272 2019-11-14 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
828 3022d272 2019-11-14 stsp if (wbuf == NULL)
829 3022d272 2019-11-14 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
831 3022d272 2019-11-14 stsp /* Keep in sync with struct got_imsg_tree_object definition! */
832 3b647085 2019-11-23 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
833 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
834 3b647085 2019-11-23 stsp ibuf_free(wbuf);
835 e033d803 2018-04-23 stsp return err;
837 3b647085 2019-11-23 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
838 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
839 3b647085 2019-11-23 stsp ibuf_free(wbuf);
840 3022d272 2019-11-14 stsp return err;
843 3b647085 2019-11-23 stsp if (imsg_add(wbuf, name, namelen) == -1) {
844 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
845 3b647085 2019-11-23 stsp ibuf_free(wbuf);
846 3022d272 2019-11-14 stsp return err;
849 3022d272 2019-11-14 stsp wbuf->fd = -1;
850 3022d272 2019-11-14 stsp imsg_close(ibuf, wbuf);
852 b00c9821 2018-11-04 stsp totlen += len;
855 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
858 e033d803 2018-04-23 stsp const struct got_error *
859 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
861 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
862 e033d803 2018-04-23 stsp const size_t min_datalen =
863 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
864 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
865 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
866 e033d803 2018-04-23 stsp int nentries = 0;
868 e033d803 2018-04-23 stsp *tree = NULL;
870 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
875 e033d803 2018-04-23 stsp struct imsg imsg;
877 e033d803 2018-04-23 stsp size_t datalen;
878 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
879 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
881 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
882 e033d803 2018-04-23 stsp if (n == 0) {
883 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries)
884 e033d803 2018-04-23 stsp goto get_more;
888 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
889 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
891 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
893 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
894 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
895 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
897 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
898 e033d803 2018-04-23 stsp /* This message should only appear once. */
899 e033d803 2018-04-23 stsp if (*tree != NULL) {
900 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
903 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
904 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
907 291624d8 2018-11-07 stsp itree = imsg.data;
908 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
909 e033d803 2018-04-23 stsp if (*tree == NULL) {
910 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
913 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
914 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
915 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
916 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
919 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
920 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
922 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
923 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
924 e033d803 2018-04-23 stsp if (*tree == NULL) {
925 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
928 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
929 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
933 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
934 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
935 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
936 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
939 c0588d8d 2018-11-07 stsp ite = imsg.data;
941 56e0773d 2019-11-28 stsp if (datalen + 1 > sizeof(te->name)) {
942 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
945 56e0773d 2019-11-28 stsp te = &(*tree)->entries[nentries];
946 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
947 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
949 56e0773d 2019-11-28 stsp memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
950 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
951 56e0773d 2019-11-28 stsp te->idx = nentries;
952 e033d803 2018-04-23 stsp nentries++;
955 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
959 e033d803 2018-04-23 stsp imsg_free(&imsg);
962 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
963 1e51f5b9 2018-04-23 stsp if (err == NULL)
964 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
965 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
966 e033d803 2018-04-23 stsp *tree = NULL;
969 ff6b18f8 2018-04-24 stsp return err;
972 ff6b18f8 2018-04-24 stsp const struct got_error *
973 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
974 ac544f8c 2019-01-13 stsp const uint8_t *data)
976 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
978 2967a784 2018-04-24 stsp iblob.size = size;
979 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
981 ac544f8c 2019-01-13 stsp if (data) {
982 ac544f8c 2019-01-13 stsp uint8_t *buf;
984 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
985 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
987 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
988 ac544f8c 2019-01-13 stsp if (buf == NULL)
989 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
991 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
992 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
993 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
994 ac544f8c 2019-01-13 stsp sizeof(iblob) + size) == -1) {
996 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1000 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1001 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1002 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1003 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1007 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1010 ff6b18f8 2018-04-24 stsp const struct got_error *
1011 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1012 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1014 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1015 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1016 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1017 ff6b18f8 2018-04-24 stsp size_t datalen;
1019 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1021 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1023 ff6b18f8 2018-04-24 stsp return err;
1025 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1027 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1028 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1029 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1030 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1033 291624d8 2018-11-07 stsp iblob = imsg.data;
1034 291624d8 2018-11-07 stsp *size = iblob->size;
1035 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1037 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1038 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1042 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1043 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1047 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1048 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1049 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1052 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1055 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1059 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1061 f4a881ce 2018-11-17 stsp return err;
1064 f4a881ce 2018-11-17 stsp static const struct got_error *
1065 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1067 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1068 f4a881ce 2018-11-17 stsp size_t offset, remain;
1070 f4a881ce 2018-11-17 stsp offset = 0;
1071 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1072 f4a881ce 2018-11-17 stsp while (remain > 0) {
1073 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1075 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1076 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1077 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1081 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1085 f4a881ce 2018-11-17 stsp offset += n;
1086 f4a881ce 2018-11-17 stsp remain -= n;
1089 f4a881ce 2018-11-17 stsp return err;
1092 f4a881ce 2018-11-17 stsp const struct got_error *
1093 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1095 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1096 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1097 f4a881ce 2018-11-17 stsp uint8_t *buf;
1098 f4a881ce 2018-11-17 stsp size_t len, total;
1099 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1100 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1101 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1103 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1105 f4a881ce 2018-11-17 stsp buf = malloc(total);
1106 f4a881ce 2018-11-17 stsp if (buf == NULL)
1107 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1109 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1110 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1111 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1112 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1113 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1114 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1115 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1116 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1118 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1119 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1120 f4a881ce 2018-11-17 stsp len += tag_len;
1121 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1122 f4a881ce 2018-11-17 stsp len += tagger_len;
1124 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1125 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1126 f4a881ce 2018-11-17 stsp goto done;
1129 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1130 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1131 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1133 f4a881ce 2018-11-17 stsp goto done;
1135 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1137 f4a881ce 2018-11-17 stsp free(buf);
1138 f4a881ce 2018-11-17 stsp return err;
1141 f4a881ce 2018-11-17 stsp const struct got_error *
1142 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1144 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1145 f4a881ce 2018-11-17 stsp struct imsg imsg;
1146 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1147 f4a881ce 2018-11-17 stsp size_t len, datalen;
1148 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1149 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1150 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1152 f4a881ce 2018-11-17 stsp *tag = NULL;
1154 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1156 f4a881ce 2018-11-17 stsp return err;
1158 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1161 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1162 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1163 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1164 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1167 f4a881ce 2018-11-17 stsp itag = imsg.data;
1168 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1169 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1170 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1173 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1175 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1176 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1177 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1181 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1183 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1184 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1185 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1186 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1190 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1191 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1192 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1195 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1196 f4a881ce 2018-11-17 stsp itag->tag_len);
1197 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1199 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1201 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1202 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1203 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1205 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1206 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1207 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1208 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1212 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1213 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1214 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1217 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1218 f4a881ce 2018-11-17 stsp itag->tagger_len);
1219 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1221 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1223 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1224 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1225 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1226 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1230 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1232 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1233 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1234 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1237 f4a881ce 2018-11-17 stsp while (remain > 0) {
1238 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1239 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1242 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1244 f4a881ce 2018-11-17 stsp return err;
1246 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1247 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1249 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1251 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1252 f4a881ce 2018-11-17 stsp offset += n;
1253 f4a881ce 2018-11-17 stsp remain -= n;
1255 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1260 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1264 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1266 2178c42e 2018-04-22 stsp return err;
1269 876c234b 2018-09-10 stsp const struct got_error *
1270 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1271 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1273 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1274 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1275 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1278 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1279 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1280 876c234b 2018-09-10 stsp if (fd == -1)
1281 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1283 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1284 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1285 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1286 41496140 2019-02-21 stsp close(fd);
1287 41496140 2019-02-21 stsp return err;
1290 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1291 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1292 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1293 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1295 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1296 876c234b 2018-09-10 stsp if (fd == -1)
1297 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1299 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1301 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1302 41496140 2019-02-21 stsp close(fd);
1303 41496140 2019-02-21 stsp return err;
1306 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1309 876c234b 2018-09-10 stsp const struct got_error *
1310 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1311 106807b4 2018-09-15 stsp struct got_object_id *id)
1313 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1315 876c234b 2018-09-10 stsp iobj.idx = idx;
1316 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1318 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1319 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1320 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1321 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1323 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1326 aba9c984 2019-09-08 stsp const struct got_error *
1327 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1329 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1331 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1332 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
1333 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
1334 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
1335 aba9c984 2019-09-08 stsp close(fd);
1336 aba9c984 2019-09-08 stsp return err;
1339 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1342 aba9c984 2019-09-08 stsp const struct got_error *
1343 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1345 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1346 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1347 aba9c984 2019-09-08 stsp NULL, 0) == -1)
1348 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1349 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1351 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1354 aba9c984 2019-09-08 stsp const struct got_error *
1355 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1357 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1358 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1359 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1360 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
1362 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1365 aba9c984 2019-09-08 stsp const struct got_error *
1366 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1368 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1369 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1370 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1371 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1373 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
1376 cd95becd 2019-11-29 stsp const struct got_error *
1377 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1379 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
1380 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1381 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
1382 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
1384 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1387 aba9c984 2019-09-08 stsp const struct got_error *
1388 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1390 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
1391 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1392 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
1393 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
1395 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
1398 9a1cc63f 2020-02-03 stsp const struct got_error *
1399 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1401 aba9c984 2019-09-08 stsp size_t len = value ? strlen(value) + 1 : 0;
1403 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1404 aba9c984 2019-09-08 stsp value, len) == -1)
1405 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1407 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1410 aba9c984 2019-09-08 stsp const struct got_error *
1411 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1413 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1414 aba9c984 2019-09-08 stsp struct imsg imsg;
1415 aba9c984 2019-09-08 stsp size_t datalen;
1416 aba9c984 2019-09-08 stsp const size_t min_datalen = 0;
1418 aba9c984 2019-09-08 stsp *str = NULL;
1420 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1422 aba9c984 2019-09-08 stsp return err;
1423 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1425 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1426 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
1427 aba9c984 2019-09-08 stsp if (datalen == 0)
1429 aba9c984 2019-09-08 stsp *str = malloc(datalen);
1430 aba9c984 2019-09-08 stsp if (*str == NULL) {
1431 aba9c984 2019-09-08 stsp err = got_error_from_errno("malloc");
1434 aba9c984 2019-09-08 stsp if (strlcpy(*str, imsg.data, datalen) >= datalen)
1435 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_NO_SPACE);
1438 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1442 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1443 aba9c984 2019-09-08 stsp return err;
1446 aba9c984 2019-09-08 stsp const struct got_error *
1447 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1449 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1450 aba9c984 2019-09-08 stsp &value, sizeof(value)) == -1)
1451 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1453 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1456 63219cd2 2019-01-04 stsp const struct got_error *
1457 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1459 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1460 aba9c984 2019-09-08 stsp struct imsg imsg;
1461 aba9c984 2019-09-08 stsp size_t datalen;
1462 aba9c984 2019-09-08 stsp const size_t min_datalen =
1463 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
1467 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1469 aba9c984 2019-09-08 stsp return err;
1470 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1472 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1473 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
1474 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
1475 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1478 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
1481 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1485 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1486 cd95becd 2019-11-29 stsp return err;
1489 cd95becd 2019-11-29 stsp const struct got_error *
1490 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1491 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes, int nremotes)
1493 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1494 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1497 cd95becd 2019-11-29 stsp iremotes.nremotes = nremotes;
1498 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1499 cd95becd 2019-11-29 stsp &iremotes, sizeof(iremotes)) == -1)
1500 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1502 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1503 cd95becd 2019-11-29 stsp imsg_clear(ibuf);
1505 cd95becd 2019-11-29 stsp return err;
1507 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++) {
1508 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1509 cd95becd 2019-11-29 stsp size_t len = sizeof(iremote);
1510 cd95becd 2019-11-29 stsp struct ibuf *wbuf;
1512 cd95becd 2019-11-29 stsp iremote.name_len = strlen(remotes[i].name);
1513 cd95becd 2019-11-29 stsp len += iremote.name_len;
1514 cd95becd 2019-11-29 stsp iremote.url_len = strlen(remotes[i].url);
1515 cd95becd 2019-11-29 stsp len += iremote.url_len;
1517 cd95becd 2019-11-29 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1518 cd95becd 2019-11-29 stsp if (wbuf == NULL)
1519 cd95becd 2019-11-29 stsp return got_error_from_errno(
1520 cd95becd 2019-11-29 stsp "imsg_create GITCONFIG_REMOTE");
1522 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1523 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1524 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1525 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1526 cd95becd 2019-11-29 stsp return err;
1529 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1530 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1531 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1532 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1533 cd95becd 2019-11-29 stsp return err;
1535 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1536 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1537 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1538 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1539 cd95becd 2019-11-29 stsp return err;
1542 cd95becd 2019-11-29 stsp wbuf->fd = -1;
1543 cd95becd 2019-11-29 stsp imsg_close(ibuf, wbuf);
1544 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1546 cd95becd 2019-11-29 stsp return err;
1549 cd95becd 2019-11-29 stsp return NULL;
1552 cd95becd 2019-11-29 stsp const struct got_error *
1553 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1554 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
1556 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1557 cd95becd 2019-11-29 stsp struct imsg imsg;
1558 cd95becd 2019-11-29 stsp size_t datalen;
1559 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1560 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1562 cd95becd 2019-11-29 stsp *remotes = NULL;
1563 cd95becd 2019-11-29 stsp *nremotes = 0;
1564 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
1566 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1568 cd95becd 2019-11-29 stsp return err;
1569 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1571 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1572 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
1573 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
1574 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1577 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
1578 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
1579 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1580 cd95becd 2019-11-29 stsp return NULL;
1584 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
1585 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1588 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1590 cd95becd 2019-11-29 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1591 cd95becd 2019-11-29 stsp if (*remotes == NULL)
1592 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
1594 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
1595 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
1597 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1600 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1602 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1603 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
1604 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
1605 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
1606 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1609 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
1610 cd95becd 2019-11-29 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
1611 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
1612 cd95becd 2019-11-29 stsp iremote.url_len) > datalen) {
1613 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1616 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
1617 cd95becd 2019-11-29 stsp iremote.name_len);
1618 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
1619 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1622 cd95becd 2019-11-29 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
1623 cd95becd 2019-11-29 stsp iremote.name_len, iremote.url_len);
1624 cd95becd 2019-11-29 stsp if (remote->url == NULL) {
1625 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1626 cd95becd 2019-11-29 stsp free(remote->name);
1629 cd95becd 2019-11-29 stsp (*nremotes)++;
1632 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1636 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1641 cd95becd 2019-11-29 stsp if (err) {
1643 cd95becd 2019-11-29 stsp for (i = 0; i < *nremotes; i++) {
1644 cd95becd 2019-11-29 stsp free((*remotes)[i].name);
1645 cd95becd 2019-11-29 stsp free((*remotes)[i].url);
1647 cd95becd 2019-11-29 stsp free(*remotes);
1648 cd95becd 2019-11-29 stsp *remotes = NULL;
1649 cd95becd 2019-11-29 stsp *nremotes = 0;
1651 aba9c984 2019-09-08 stsp return err;
1654 ca6e02ac 2020-01-07 stsp const struct got_error *
1655 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1656 ca6e02ac 2020-01-07 stsp struct got_object_id *id, int idx, const char *path)
1658 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1659 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
1660 ca6e02ac 2020-01-07 stsp size_t path_len = strlen(path) + 1;
1662 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1663 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
1664 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
1665 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
1666 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
1667 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1668 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1669 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1670 ca6e02ac 2020-01-07 stsp return err;
1672 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1673 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1674 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1675 ca6e02ac 2020-01-07 stsp return err;
1677 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, path, path_len) == -1) {
1678 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1679 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1680 ca6e02ac 2020-01-07 stsp return err;
1683 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
1684 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
1686 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1689 aba9c984 2019-09-08 stsp const struct got_error *
1690 ca6e02ac 2020-01-07 stsp got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1691 ca6e02ac 2020-01-07 stsp size_t ncommits, struct imsgbuf *ibuf)
1693 ca6e02ac 2020-01-07 stsp const struct got_error *err;
1694 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
1697 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1698 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_traversed_commits) +
1699 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH);
1700 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
1701 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1703 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1704 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1705 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1706 ca6e02ac 2020-01-07 stsp return err;
1708 ca6e02ac 2020-01-07 stsp for (i = 0; i < ncommits; i++) {
1709 ca6e02ac 2020-01-07 stsp struct got_object_id *id = &commit_ids[i];
1710 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1711 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
1712 ca6e02ac 2020-01-07 stsp "imsg_add TRAVERSED_COMMITS");
1713 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1714 ca6e02ac 2020-01-07 stsp return err;
1718 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
1719 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
1721 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1724 ca6e02ac 2020-01-07 stsp const struct got_error *
1725 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1726 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
1727 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1729 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1730 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1731 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
1732 ca6e02ac 2020-01-07 stsp size_t datalen;
1733 ca6e02ac 2020-01-07 stsp int i, done = 0;
1735 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
1736 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
1738 ca6e02ac 2020-01-07 stsp while (!done) {
1739 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1741 ca6e02ac 2020-01-07 stsp return err;
1743 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1744 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1745 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
1746 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
1747 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
1748 ca6e02ac 2020-01-07 stsp icommits->ncommits * SHA1_DIGEST_LENGTH) {
1749 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1752 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
1753 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1754 ca6e02ac 2020-01-07 stsp uint8_t *sha1 = (uint8_t *)imsg.data +
1755 ca6e02ac 2020-01-07 stsp sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1756 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1759 ca6e02ac 2020-01-07 stsp memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1760 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1762 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
1763 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
1764 ca6e02ac 2020-01-07 stsp *changed_commit_id =
1765 ca6e02ac 2020-01-07 stsp got_object_id_dup(qid->id);
1766 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
1767 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
1768 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
1774 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1775 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
1776 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1779 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
1780 ca6e02ac 2020-01-07 stsp datalen, ibuf);
1782 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1786 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1790 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
1796 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
1797 ca6e02ac 2020-01-07 stsp return err;
1800 ca6e02ac 2020-01-07 stsp const struct got_error *
1801 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1803 ca6e02ac 2020-01-07 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1804 ca6e02ac 2020-01-07 stsp NULL, 0) == -1)
1805 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1807 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1810 ca6e02ac 2020-01-07 stsp const struct got_error *
1811 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
1813 c39c25dd 2019-08-09 stsp const char *helpers[] = {
1814 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
1815 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
1816 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
1817 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
1818 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
1819 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
1820 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
1824 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
1825 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
1827 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
1830 63219cd2 2019-01-04 stsp return NULL;
1834 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1836 aba9c984 2019-09-08 stsp if (close(imsg_fds[0]) != 0) {
1837 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1841 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1842 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1845 aba9c984 2019-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1846 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1850 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
1851 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1852 aba9c984 2019-09-08 stsp strerror(errno));