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 531c3985 2020-03-18 stsp #include <ctype.h>
25 2178c42e 2018-04-22 stsp #include <stdio.h>
26 2178c42e 2018-04-22 stsp #include <stdlib.h>
27 2178c42e 2018-04-22 stsp #include <string.h>
28 2178c42e 2018-04-22 stsp #include <errno.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 2178c42e 2018-04-22 stsp #include <poll.h>
31 2178c42e 2018-04-22 stsp #include <imsg.h>
32 2178c42e 2018-04-22 stsp #include <sha1.h>
33 2178c42e 2018-04-22 stsp #include <zlib.h>
34 788c352e 2018-06-16 stsp #include <time.h>
36 2178c42e 2018-04-22 stsp #include "got_object.h"
37 2178c42e 2018-04-22 stsp #include "got_error.h"
38 3022d272 2019-11-14 stsp #include "got_path.h"
39 cd95becd 2019-11-29 stsp #include "got_repository.h"
41 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
42 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
43 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
44 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
45 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
47 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
50 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 c39c25dd 2019-08-09 stsp #ifndef nitems
54 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 2178c42e 2018-04-22 stsp static const struct got_error *
58 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
60 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
63 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
64 2178c42e 2018-04-22 stsp pfd[0].events = events;
66 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
67 2178c42e 2018-04-22 stsp if (n == -1)
68 638f9024 2019-05-13 stsp return got_error_from_errno("poll");
70 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
71 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
72 638f9024 2019-05-13 stsp return got_error_from_errno("poll error");
73 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
74 2178c42e 2018-04-22 stsp return NULL;
76 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
79 c4eae628 2018-04-23 stsp static const struct got_error *
80 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
82 fe36cf76 2018-04-23 stsp const struct got_error *err;
85 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
89 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
90 fe36cf76 2018-04-23 stsp if (n == -1) {
91 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
92 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
96 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
98 e033d803 2018-04-23 stsp return NULL;
101 ad242220 2018-09-08 stsp const struct got_error *
102 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
104 876c234b 2018-09-10 stsp int child_status;
106 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
107 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
109 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
110 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
112 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
113 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
115 876c234b 2018-09-10 stsp return NULL;
118 73b7854a 2018-11-11 stsp static const struct got_error *
119 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
121 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
123 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
124 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
126 73b7854a 2018-11-11 stsp ierr = imsg->data;
127 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
128 73b7854a 2018-11-11 stsp static struct got_error serr;
129 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
130 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
131 73b7854a 2018-11-11 stsp return &serr;
134 73b7854a 2018-11-11 stsp return got_error(ierr->code);
137 876c234b 2018-09-10 stsp const struct got_error *
138 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
139 46de5bfd 2018-11-11 stsp size_t min_datalen)
141 e033d803 2018-04-23 stsp const struct got_error *err;
144 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
145 876c234b 2018-09-10 stsp if (n == -1)
146 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
148 876c234b 2018-09-10 stsp while (n == 0) {
149 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
151 876c234b 2018-09-10 stsp return err;
152 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
153 cbfaaf20 2020-01-06 stsp if (n == -1)
154 cbfaaf20 2020-01-06 stsp return got_error_from_errno("imsg_get");
157 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
158 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
160 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
161 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
165 73b7854a 2018-11-11 stsp return NULL;
168 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
170 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
173 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
176 2178c42e 2018-04-22 stsp ierr.code = err->code;
177 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
178 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
180 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
181 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
182 e93cd828 2018-11-11 stsp if (ret == -1) {
183 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
184 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
188 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
189 5d43e84d 2018-04-23 stsp if (poll_err) {
190 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
191 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
195 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
196 5d43e84d 2018-04-23 stsp if (ret == -1) {
197 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
198 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
203 e033d803 2018-04-23 stsp static const struct got_error *
204 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
206 e033d803 2018-04-23 stsp const struct got_error *err;
208 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
210 e033d803 2018-04-23 stsp return err;
212 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
213 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
215 e033d803 2018-04-23 stsp return NULL;
218 ad242220 2018-09-08 stsp const struct got_error *
219 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
221 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
222 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
224 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
226 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
227 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
229 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
230 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
231 ad242220 2018-09-08 stsp return err;
234 93658fb9 2020-03-18 stsp const struct got_error *
235 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
237 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
239 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
241 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
244 1785f84a 2018-12-23 stsp const struct got_error *
245 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
246 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
248 41496140 2019-02-21 stsp const struct got_error *err = NULL;
249 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
250 1785f84a 2018-12-23 stsp size_t len;
252 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
253 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
254 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
255 1785f84a 2018-12-23 stsp iobjp = &iobj;
256 1785f84a 2018-12-23 stsp len = sizeof(iobj);
258 1785f84a 2018-12-23 stsp iobjp = NULL;
262 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
264 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
266 41496140 2019-02-21 stsp return err;
269 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
272 13c729f7 2018-12-24 stsp const struct got_error *
273 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
274 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
276 41496140 2019-02-21 stsp const struct got_error *err = NULL;
277 7f358e3b 2019-11-23 stsp struct ibuf *wbuf;
278 7f358e3b 2019-11-23 stsp size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
280 7f358e3b 2019-11-23 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
281 7f358e3b 2019-11-23 stsp if (wbuf == NULL)
282 7f358e3b 2019-11-23 stsp return got_error_from_errno("imsg_create TREE_REQUEST");
284 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
285 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
287 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
288 7f358e3b 2019-11-23 stsp return err;
291 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
292 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
293 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
294 7f358e3b 2019-11-23 stsp return err;
298 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
299 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
301 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
304 268f7291 2018-12-24 stsp const struct got_error *
305 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
306 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
308 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
309 268f7291 2018-12-24 stsp size_t len;
311 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
312 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
313 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
314 268f7291 2018-12-24 stsp iobjp = &iobj;
315 268f7291 2018-12-24 stsp len = sizeof(iobj);
317 268f7291 2018-12-24 stsp iobjp = NULL;
321 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
323 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
325 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
328 7762fe12 2018-11-05 stsp const struct got_error *
329 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
330 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
332 41496140 2019-02-21 stsp const struct got_error *err = NULL;
333 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
334 ebc55e2d 2018-12-24 stsp size_t len;
336 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
337 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
338 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
339 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
340 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
342 ebc55e2d 2018-12-24 stsp iobjp = NULL;
346 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
348 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
349 41496140 2019-02-21 stsp close(infd);
350 41496140 2019-02-21 stsp return err;
353 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
356 55da3778 2018-09-10 stsp const struct got_error *
357 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
359 41496140 2019-02-21 stsp const struct got_error *err = NULL;
361 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
363 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
364 41496140 2019-02-21 stsp close(outfd);
365 41496140 2019-02-21 stsp return err;
368 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
371 73ab1060 2020-03-18 stsp static const struct got_error *
372 73ab1060 2020-03-18 stsp send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
374 41496140 2019-02-21 stsp const struct got_error *err = NULL;
376 73ab1060 2020-03-18 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
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 73ab1060 2020-03-18 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
388 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
391 73ab1060 2020-03-18 stsp const struct got_error *
392 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
394 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
396 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
397 2178c42e 2018-04-22 stsp iobj.type = obj->type;
398 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
399 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
400 2178c42e 2018-04-22 stsp iobj.size = obj->size;
401 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
402 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
403 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
406 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
408 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
410 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
413 cfd633c2 2018-09-10 stsp const struct got_error *
414 33501562 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
415 4ba14133 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
416 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
417 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
419 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
420 33501562 2020-03-18 stsp struct ibuf *wbuf;
421 4ba14133 2020-03-20 stsp size_t len;
422 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
423 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetchreq;
425 4ba14133 2020-03-20 stsp memset(&fetchreq, 0, sizeof(fetchreq));
426 4ba14133 2020-03-20 stsp fetchreq.fetch_all_branches = fetch_all_branches;
427 41b0de12 2020-03-21 stsp fetchreq.list_refs_only = list_refs_only;
428 2690194b 2020-03-21 stsp fetchreq.verbosity = verbosity;
429 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, have_refs, entry)
430 4ba14133 2020-03-20 stsp fetchreq.n_have_refs++;
431 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry)
432 4ba14133 2020-03-20 stsp fetchreq.n_wanted_branches++;
433 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry)
434 0e4002ca 2020-03-21 stsp fetchreq.n_wanted_refs++;
435 659e7fbd 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_request);
436 33501562 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
438 33501562 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
441 4ba14133 2020-03-20 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
442 4ba14133 2020-03-20 stsp &fetchreq, sizeof(fetchreq)) == -1)
443 4ba14133 2020-03-20 stsp return got_error_from_errno(
444 4ba14133 2020-03-20 stsp "imsg_compose FETCH_SERVER_PROGRESS");
446 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
449 659e7fbd 2020-03-20 stsp return err;
453 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
454 33501562 2020-03-18 stsp const char *name = pe->path;
455 33501562 2020-03-18 stsp size_t name_len = pe->path_len;
456 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
458 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
459 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
460 4ba14133 2020-03-20 stsp if (wbuf == NULL)
461 4ba14133 2020-03-20 stsp return got_error_from_errno("imsg_create FETCH_HAVE_REF");
463 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_ref! */
464 33501562 2020-03-18 stsp if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
465 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
466 33501562 2020-03-18 stsp ibuf_free(wbuf);
467 33501562 2020-03-18 stsp return err;
469 33501562 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
470 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
471 33501562 2020-03-18 stsp ibuf_free(wbuf);
472 33501562 2020-03-18 stsp return err;
474 33501562 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
475 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
476 33501562 2020-03-18 stsp ibuf_free(wbuf);
477 33501562 2020-03-18 stsp return err;
480 4ba14133 2020-03-20 stsp wbuf->fd = -1;
481 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
482 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
484 4ba14133 2020-03-20 stsp return err;
487 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
488 4ba14133 2020-03-20 stsp const char *name = pe->path;
489 4ba14133 2020-03-20 stsp size_t name_len = pe->path_len;
491 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
492 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
494 4ba14133 2020-03-20 stsp if (wbuf == NULL)
495 4ba14133 2020-03-20 stsp return got_error_from_errno(
496 4ba14133 2020-03-20 stsp "imsg_create FETCH_WANTED_BRANCH");
498 4ba14133 2020-03-20 stsp /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
499 4ba14133 2020-03-20 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
500 4ba14133 2020-03-20 stsp err = got_error_from_errno(
501 4ba14133 2020-03-20 stsp "imsg_add FETCH_WANTED_BRANCH");
502 4ba14133 2020-03-20 stsp ibuf_free(wbuf);
503 4ba14133 2020-03-20 stsp return err;
505 4ba14133 2020-03-20 stsp if (imsg_add(wbuf, name, name_len) == -1) {
506 4ba14133 2020-03-20 stsp err = got_error_from_errno(
507 4ba14133 2020-03-20 stsp "imsg_add FETCH_WANTED_BRANCH");
508 4ba14133 2020-03-20 stsp ibuf_free(wbuf);
509 4ba14133 2020-03-20 stsp return err;
512 4ba14133 2020-03-20 stsp wbuf->fd = -1;
513 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
514 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
516 4ba14133 2020-03-20 stsp return err;
519 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
520 0e4002ca 2020-03-21 stsp const char *name = pe->path;
521 0e4002ca 2020-03-21 stsp size_t name_len = pe->path_len;
523 0e4002ca 2020-03-21 stsp len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
524 0e4002ca 2020-03-21 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
526 0e4002ca 2020-03-21 stsp if (wbuf == NULL)
527 0e4002ca 2020-03-21 stsp return got_error_from_errno(
528 0e4002ca 2020-03-21 stsp "imsg_create FETCH_WANTED_REF");
530 0e4002ca 2020-03-21 stsp /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
531 0e4002ca 2020-03-21 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
532 0e4002ca 2020-03-21 stsp err = got_error_from_errno(
533 0e4002ca 2020-03-21 stsp "imsg_add FETCH_WANTED_REF");
534 0e4002ca 2020-03-21 stsp ibuf_free(wbuf);
535 0e4002ca 2020-03-21 stsp return err;
537 0e4002ca 2020-03-21 stsp if (imsg_add(wbuf, name, name_len) == -1) {
538 0e4002ca 2020-03-21 stsp err = got_error_from_errno(
539 0e4002ca 2020-03-21 stsp "imsg_add FETCH_WANTED_REF");
540 0e4002ca 2020-03-21 stsp ibuf_free(wbuf);
541 0e4002ca 2020-03-21 stsp return err;
544 0e4002ca 2020-03-21 stsp wbuf->fd = -1;
545 0e4002ca 2020-03-21 stsp imsg_close(ibuf, wbuf);
546 0e4002ca 2020-03-21 stsp err = flush_imsg(ibuf);
548 0e4002ca 2020-03-21 stsp return err;
552 4ba14133 2020-03-20 stsp return NULL;
556 93658fb9 2020-03-18 stsp const struct got_error *
557 f826addf 2020-03-18 stsp got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
559 f826addf 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
563 f826addf 2020-03-18 stsp const struct got_error *
564 abe0f35f 2020-03-18 stsp got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
565 abe0f35f 2020-03-18 stsp struct got_pathlist_head *symrefs)
567 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
568 abe0f35f 2020-03-18 stsp struct ibuf *wbuf;
569 abe0f35f 2020-03-18 stsp size_t len, nsymrefs = 0;
570 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
572 abe0f35f 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_symrefs);
573 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
574 abe0f35f 2020-03-18 stsp const char *target = pe->data;
575 abe0f35f 2020-03-18 stsp len += sizeof(struct got_imsg_fetch_symref) +
576 abe0f35f 2020-03-18 stsp pe->path_len + strlen(target);
577 abe0f35f 2020-03-18 stsp nsymrefs++;
580 abe0f35f 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
581 abe0f35f 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
583 abe0f35f 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
584 abe0f35f 2020-03-18 stsp if (wbuf == NULL)
585 abe0f35f 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
587 abe0f35f 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
588 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
589 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
590 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
591 abe0f35f 2020-03-18 stsp return err;
594 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
595 abe0f35f 2020-03-18 stsp const char *name = pe->path;
596 abe0f35f 2020-03-18 stsp size_t name_len = pe->path_len;
597 abe0f35f 2020-03-18 stsp const char *target = pe->data;
598 abe0f35f 2020-03-18 stsp size_t target_len = strlen(target);
600 abe0f35f 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
601 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
602 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
603 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
604 abe0f35f 2020-03-18 stsp return err;
606 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
607 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
608 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
609 abe0f35f 2020-03-18 stsp return err;
611 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
612 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
613 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
614 abe0f35f 2020-03-18 stsp return err;
616 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, target, target_len) == -1) {
617 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
618 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
619 abe0f35f 2020-03-18 stsp return err;
623 abe0f35f 2020-03-18 stsp wbuf->fd = -1;
624 abe0f35f 2020-03-18 stsp imsg_close(ibuf, wbuf);
625 abe0f35f 2020-03-18 stsp return flush_imsg(ibuf);
628 abe0f35f 2020-03-18 stsp const struct got_error *
629 ea7396b9 2020-03-18 stsp got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
630 b9f99abf 2020-03-18 stsp struct got_object_id *refid, const char *refname)
632 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
633 b9f99abf 2020-03-18 stsp struct ibuf *wbuf;
634 b9f99abf 2020-03-18 stsp size_t len, reflen = strlen(refname);
636 ea7396b9 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
637 b9f99abf 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
638 b9f99abf 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
640 ea7396b9 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
641 b9f99abf 2020-03-18 stsp if (wbuf == NULL)
642 ea7396b9 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_REF");
644 ea7396b9 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
645 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
646 ea7396b9 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REF");
647 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
648 b9f99abf 2020-03-18 stsp return err;
650 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
651 ea7396b9 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REF");
652 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
653 b9f99abf 2020-03-18 stsp return err;
656 b9f99abf 2020-03-18 stsp wbuf->fd = -1;
657 b9f99abf 2020-03-18 stsp imsg_close(ibuf, wbuf);
658 531c3985 2020-03-18 stsp return flush_imsg(ibuf);
661 531c3985 2020-03-18 stsp const struct got_error *
662 531c3985 2020-03-18 stsp got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
663 531c3985 2020-03-18 stsp size_t msglen)
665 531c3985 2020-03-18 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
666 531c3985 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
668 531c3985 2020-03-18 stsp if (msglen == 0)
669 531c3985 2020-03-18 stsp return NULL;
671 531c3985 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
672 531c3985 2020-03-18 stsp msg, msglen) == -1)
673 531c3985 2020-03-18 stsp return got_error_from_errno(
674 531c3985 2020-03-18 stsp "imsg_compose FETCH_SERVER_PROGRESS");
676 d2cdc636 2020-03-18 stsp return flush_imsg(ibuf);
679 d2cdc636 2020-03-18 stsp const struct got_error *
680 d2cdc636 2020-03-18 stsp got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
682 d2cdc636 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
683 d2cdc636 2020-03-18 stsp &bytes, sizeof(bytes)) == -1)
684 d2cdc636 2020-03-18 stsp return got_error_from_errno(
685 d2cdc636 2020-03-18 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
687 b9f99abf 2020-03-18 stsp return flush_imsg(ibuf);
690 b9f99abf 2020-03-18 stsp const struct got_error *
691 93658fb9 2020-03-18 stsp got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
693 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
694 93658fb9 2020-03-18 stsp hash.sha1, SHA1_DIGEST_LENGTH) == -1)
695 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
696 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
700 93658fb9 2020-03-18 stsp const struct got_error *
701 8f2d01a6 2020-03-18 stsp got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
702 531c3985 2020-03-18 stsp char **refname, struct got_pathlist_head *symrefs, char **server_progress,
703 d2cdc636 2020-03-18 stsp off_t *packfile_size, struct imsgbuf *ibuf)
705 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
706 b9f99abf 2020-03-18 stsp struct imsg imsg;
707 b9f99abf 2020-03-18 stsp size_t datalen;
708 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symrefs *isymrefs = NULL;
709 abe0f35f 2020-03-18 stsp size_t n, remain;
714 8f2d01a6 2020-03-18 stsp *id = NULL;
715 b9f99abf 2020-03-18 stsp *refname = NULL;
716 531c3985 2020-03-18 stsp *server_progress = NULL;
717 5a489642 2020-03-19 stsp *packfile_size = 0;
719 531c3985 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
721 b9f99abf 2020-03-18 stsp return err;
723 b9f99abf 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
724 b9f99abf 2020-03-18 stsp switch (imsg.hdr.type) {
725 b9f99abf 2020-03-18 stsp case GOT_IMSG_ERROR:
726 531c3985 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
727 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
730 b9f99abf 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
732 abe0f35f 2020-03-18 stsp case GOT_IMSG_FETCH_SYMREFS:
733 abe0f35f 2020-03-18 stsp if (datalen < sizeof(*isymrefs)) {
734 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
737 abe0f35f 2020-03-18 stsp if (isymrefs != NULL) {
738 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
741 abe0f35f 2020-03-18 stsp isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
742 abe0f35f 2020-03-18 stsp off = sizeof(*isymrefs);
743 abe0f35f 2020-03-18 stsp remain = datalen - off;
744 abe0f35f 2020-03-18 stsp for (n = 0; n < isymrefs->nsymrefs; n++) {
745 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symref *s;
746 abe0f35f 2020-03-18 stsp char *name, *target;
747 abe0f35f 2020-03-18 stsp if (remain < sizeof(struct got_imsg_fetch_symref)) {
748 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
751 abe0f35f 2020-03-18 stsp s = (struct got_imsg_fetch_symref *)(imsg.data + off);
752 abe0f35f 2020-03-18 stsp off += sizeof(*s);
753 abe0f35f 2020-03-18 stsp remain -= sizeof(*s);
754 abe0f35f 2020-03-18 stsp if (remain < s->name_len) {
755 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
758 abe0f35f 2020-03-18 stsp name = strndup(imsg.data + off, s->name_len);
759 abe0f35f 2020-03-18 stsp if (name == NULL) {
760 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
763 abe0f35f 2020-03-18 stsp off += s->name_len;
764 abe0f35f 2020-03-18 stsp remain -= s->name_len;
765 abe0f35f 2020-03-18 stsp if (remain < s->target_len) {
766 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
767 abe0f35f 2020-03-18 stsp free(name);
770 abe0f35f 2020-03-18 stsp target = strndup(imsg.data + off, s->target_len);
771 abe0f35f 2020-03-18 stsp if (target == NULL) {
772 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
773 abe0f35f 2020-03-18 stsp free(name);
776 abe0f35f 2020-03-18 stsp off += s->target_len;
777 abe0f35f 2020-03-18 stsp remain -= s->target_len;
778 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
780 abe0f35f 2020-03-18 stsp free(name);
781 abe0f35f 2020-03-18 stsp free(target);
786 ea7396b9 2020-03-18 stsp case GOT_IMSG_FETCH_REF:
787 531c3985 2020-03-18 stsp if (datalen <= SHA1_DIGEST_LENGTH) {
788 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
791 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
792 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
793 b9f99abf 2020-03-18 stsp err = got_error_from_errno("malloc");
796 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
797 b9f99abf 2020-03-18 stsp *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
798 b9f99abf 2020-03-18 stsp datalen - SHA1_DIGEST_LENGTH);
799 b9f99abf 2020-03-18 stsp if (*refname == NULL) {
800 b9f99abf 2020-03-18 stsp err = got_error_from_errno("strndup");
804 531c3985 2020-03-18 stsp case GOT_IMSG_FETCH_SERVER_PROGRESS:
805 531c3985 2020-03-18 stsp if (datalen == 0) {
806 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
809 531c3985 2020-03-18 stsp *server_progress = strndup(imsg.data, datalen);
810 531c3985 2020-03-18 stsp if (*server_progress == NULL) {
811 531c3985 2020-03-18 stsp err = got_error_from_errno("strndup");
814 531c3985 2020-03-18 stsp for (i = 0; i < datalen; i++) {
815 531c3985 2020-03-18 stsp if (!isprint((unsigned char)(*server_progress)[i]) &&
816 531c3985 2020-03-18 stsp !isspace((unsigned char)(*server_progress)[i])) {
817 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
818 531c3985 2020-03-18 stsp free(*server_progress);
819 531c3985 2020-03-18 stsp *server_progress = NULL;
824 d2cdc636 2020-03-18 stsp case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
825 d2cdc636 2020-03-18 stsp if (datalen < sizeof(*packfile_size)) {
826 d2cdc636 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
829 d2cdc636 2020-03-18 stsp memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
831 8f2d01a6 2020-03-18 stsp case GOT_IMSG_FETCH_DONE:
832 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
833 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
834 8f2d01a6 2020-03-18 stsp err = got_error_from_errno("malloc");
837 8f2d01a6 2020-03-18 stsp if (datalen != SHA1_DIGEST_LENGTH) {
838 8f2d01a6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
841 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
845 b887aab6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
851 8f2d01a6 2020-03-18 stsp *id = NULL;
852 b887aab6 2020-03-18 stsp free(*refname);
853 b887aab6 2020-03-18 stsp *refname = NULL;
855 b9f99abf 2020-03-18 stsp imsg_free(&imsg);
856 b9f99abf 2020-03-18 stsp return err;
859 93658fb9 2020-03-18 stsp const struct got_error *
860 668a20f6 2020-03-18 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
863 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
865 668a20f6 2020-03-18 stsp /* Keep in sync with struct got_imsg_index_pack_request */
866 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
867 668a20f6 2020-03-18 stsp pack_hash, SHA1_DIGEST_LENGTH) == -1) {
868 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
870 93658fb9 2020-03-18 stsp return err;
872 baa9fea0 2020-03-18 stsp return flush_imsg(ibuf);
875 baa9fea0 2020-03-18 stsp const struct got_error *
876 73ab1060 2020-03-18 stsp got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
878 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
881 73ab1060 2020-03-18 stsp const struct got_error *
882 668a20f6 2020-03-18 stsp got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
883 668a20f6 2020-03-18 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
885 baa9fea0 2020-03-18 stsp struct got_imsg_index_pack_progress iprogress;
887 668a20f6 2020-03-18 stsp iprogress.nobj_total = nobj_total;
888 668a20f6 2020-03-18 stsp iprogress.nobj_indexed = nobj_indexed;
889 668a20f6 2020-03-18 stsp iprogress.nobj_loose = nobj_loose;
890 668a20f6 2020-03-18 stsp iprogress.nobj_resolved = nobj_resolved;
892 baa9fea0 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
893 baa9fea0 2020-03-18 stsp &iprogress, sizeof(iprogress)) == -1)
894 baa9fea0 2020-03-18 stsp return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
896 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
899 93658fb9 2020-03-18 stsp const struct got_error *
900 93658fb9 2020-03-18 stsp got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
902 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
903 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
904 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
907 93658fb9 2020-03-18 stsp const struct got_error *
908 668a20f6 2020-03-18 stsp got_privsep_recv_index_progress(int *done, int *nobj_total,
909 668a20f6 2020-03-18 stsp int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
910 668a20f6 2020-03-18 stsp struct imsgbuf *ibuf)
912 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
913 93658fb9 2020-03-18 stsp struct imsg imsg;
914 baa9fea0 2020-03-18 stsp struct got_imsg_index_pack_progress *iprogress;
915 baa9fea0 2020-03-18 stsp size_t datalen;
918 668a20f6 2020-03-18 stsp *nobj_total = 0;
919 668a20f6 2020-03-18 stsp *nobj_indexed = 0;
920 668a20f6 2020-03-18 stsp *nobj_resolved = 0;
922 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
924 93658fb9 2020-03-18 stsp return err;
926 baa9fea0 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
927 baa9fea0 2020-03-18 stsp switch (imsg.hdr.type) {
928 baa9fea0 2020-03-18 stsp case GOT_IMSG_ERROR:
929 baa9fea0 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
930 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
933 baa9fea0 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
935 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_PROGRESS:
936 baa9fea0 2020-03-18 stsp if (datalen < sizeof(*iprogress)) {
937 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
940 baa9fea0 2020-03-18 stsp iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
941 668a20f6 2020-03-18 stsp *nobj_total = iprogress->nobj_total;
942 668a20f6 2020-03-18 stsp *nobj_indexed = iprogress->nobj_indexed;
943 668a20f6 2020-03-18 stsp *nobj_loose = iprogress->nobj_loose;
944 668a20f6 2020-03-18 stsp *nobj_resolved = iprogress->nobj_resolved;
946 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_DONE:
947 baa9fea0 2020-03-18 stsp if (datalen != 0) {
948 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
954 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
958 93658fb9 2020-03-18 stsp imsg_free(&imsg);
959 baa9fea0 2020-03-18 stsp return err;
962 93658fb9 2020-03-18 stsp const struct got_error *
963 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
964 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
966 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
967 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
968 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
970 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
971 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
972 291624d8 2018-11-07 stsp iobj = imsg->data;
974 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
975 cfd633c2 2018-09-10 stsp if (*obj == NULL)
976 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
978 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
979 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
980 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
981 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
982 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
983 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
984 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
985 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
986 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
989 876c234b 2018-09-10 stsp return err;
992 2178c42e 2018-04-22 stsp const struct got_error *
993 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
995 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
996 2178c42e 2018-04-22 stsp struct imsg imsg;
997 c4eae628 2018-04-23 stsp const size_t min_datalen =
998 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1000 2178c42e 2018-04-22 stsp *obj = NULL;
1002 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1004 2178c42e 2018-04-22 stsp return err;
1006 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
1007 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
1008 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1011 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1015 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
1017 bff6ca00 2018-04-23 stsp return err;
1020 c75f7264 2018-09-11 stsp static const struct got_error *
1021 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1022 c75f7264 2018-09-11 stsp size_t logmsg_len)
1024 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
1025 c75f7264 2018-09-11 stsp size_t offset, remain;
1027 c75f7264 2018-09-11 stsp offset = 0;
1028 c75f7264 2018-09-11 stsp remain = logmsg_len;
1029 c75f7264 2018-09-11 stsp while (remain > 0) {
1030 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1032 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1033 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
1034 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
1035 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
1039 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
1043 c75f7264 2018-09-11 stsp offset += n;
1044 c75f7264 2018-09-11 stsp remain -= n;
1047 fa4ffeb3 2018-11-04 stsp return err;
1050 bff6ca00 2018-04-23 stsp const struct got_error *
1051 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1053 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
1054 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
1055 bff6ca00 2018-04-23 stsp uint8_t *buf;
1056 bff6ca00 2018-04-23 stsp size_t len, total;
1057 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1058 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
1059 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
1060 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
1062 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
1063 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
1065 bff6ca00 2018-04-23 stsp buf = malloc(total);
1066 bff6ca00 2018-04-23 stsp if (buf == NULL)
1067 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1069 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
1070 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
1071 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
1072 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
1073 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
1074 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
1075 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
1076 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
1077 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
1078 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
1079 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
1081 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
1082 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
1083 b9c33926 2018-11-07 stsp len += author_len;
1084 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
1085 b9c33926 2018-11-07 stsp len += committer_len;
1086 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1087 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1088 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
1091 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1092 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
1093 bff6ca00 2018-04-23 stsp goto done;
1096 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
1097 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1098 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
1100 904df868 2018-11-04 stsp goto done;
1102 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
1104 bff6ca00 2018-04-23 stsp free(buf);
1105 bff6ca00 2018-04-23 stsp return err;
1108 ca6e02ac 2020-01-07 stsp static const struct got_error *
1109 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
1110 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1112 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
1113 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
1114 ca6e02ac 2020-01-07 stsp size_t len = 0;
1117 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
1118 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1120 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
1121 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
1122 ca6e02ac 2020-01-07 stsp icommit->committer_len +
1123 ca6e02ac 2020-01-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH)
1124 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1126 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
1127 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1129 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
1131 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
1132 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
1133 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
1134 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
1136 ca6e02ac 2020-01-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1137 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
1138 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
1139 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
1140 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
1141 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1143 ca6e02ac 2020-01-07 stsp if (icommit->author_len == 0) {
1144 ca6e02ac 2020-01-07 stsp (*commit)->author = strdup("");
1145 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
1146 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1147 ca6e02ac 2020-01-07 stsp goto done;
1150 ca6e02ac 2020-01-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
1151 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
1152 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1153 ca6e02ac 2020-01-07 stsp goto done;
1155 ca6e02ac 2020-01-07 stsp memcpy((*commit)->author, imsg->data + len,
1156 ca6e02ac 2020-01-07 stsp icommit->author_len);
1157 ca6e02ac 2020-01-07 stsp (*commit)->author[icommit->author_len] = '\0';
1159 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
1161 ca6e02ac 2020-01-07 stsp if (icommit->committer_len == 0) {
1162 ca6e02ac 2020-01-07 stsp (*commit)->committer = strdup("");
1163 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
1164 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1165 ca6e02ac 2020-01-07 stsp goto done;
1168 ca6e02ac 2020-01-07 stsp (*commit)->committer =
1169 ca6e02ac 2020-01-07 stsp malloc(icommit->committer_len + 1);
1170 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
1171 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1172 ca6e02ac 2020-01-07 stsp goto done;
1174 ca6e02ac 2020-01-07 stsp memcpy((*commit)->committer, imsg->data + len,
1175 ca6e02ac 2020-01-07 stsp icommit->committer_len);
1176 ca6e02ac 2020-01-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
1178 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
1180 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
1181 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
1182 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1183 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1184 ca6e02ac 2020-01-07 stsp goto done;
1187 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
1189 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1190 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1191 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1192 ca6e02ac 2020-01-07 stsp goto done;
1194 ca6e02ac 2020-01-07 stsp while (remain > 0) {
1195 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
1196 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1199 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1201 ca6e02ac 2020-01-07 stsp goto done;
1203 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1204 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1205 ca6e02ac 2020-01-07 stsp goto done;
1208 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
1209 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
1210 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
1211 ca6e02ac 2020-01-07 stsp offset += n;
1212 ca6e02ac 2020-01-07 stsp remain -= n;
1214 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
1217 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
1218 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1220 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1223 ca6e02ac 2020-01-07 stsp memcpy(qid->id, imsg->data + len +
1224 ca6e02ac 2020-01-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1225 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1226 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
1229 ca6e02ac 2020-01-07 stsp if (err) {
1230 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
1231 ca6e02ac 2020-01-07 stsp *commit = NULL;
1233 ca6e02ac 2020-01-07 stsp return err;
1236 ca6e02ac 2020-01-07 stsp const struct got_error *
1237 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1239 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1240 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1241 ca6e02ac 2020-01-07 stsp size_t datalen;
1242 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
1243 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
1244 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
1246 ca6e02ac 2020-01-07 stsp *commit = NULL;
1248 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1250 ca6e02ac 2020-01-07 stsp return err;
1252 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1254 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1255 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1256 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1259 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1263 2178c42e 2018-04-22 stsp imsg_free(&imsg);
1265 e033d803 2018-04-23 stsp return err;
1268 e033d803 2018-04-23 stsp const struct got_error *
1269 3022d272 2019-11-14 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1270 3022d272 2019-11-14 stsp int nentries)
1272 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1273 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
1274 3022d272 2019-11-14 stsp struct got_pathlist_entry *pe;
1275 b00c9821 2018-11-04 stsp size_t totlen;
1276 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
1278 3022d272 2019-11-14 stsp itree.nentries = nentries;
1279 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1281 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
1283 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
1284 6eb07a17 2018-11-04 stsp nimsg = 1;
1285 3022d272 2019-11-14 stsp TAILQ_FOREACH(pe, entries, entry) {
1286 3022d272 2019-11-14 stsp const char *name = pe->path;
1287 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte = pe->data;
1288 3022d272 2019-11-14 stsp struct ibuf *wbuf;
1289 3022d272 2019-11-14 stsp size_t namelen = strlen(name);
1290 cd9e913a 2019-11-27 stsp size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1292 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
1293 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
1296 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1297 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
1299 b00c9821 2018-11-04 stsp return err;
1300 6eb07a17 2018-11-04 stsp nimsg = 0;
1303 3022d272 2019-11-14 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1304 3022d272 2019-11-14 stsp if (wbuf == NULL)
1305 3022d272 2019-11-14 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
1307 3022d272 2019-11-14 stsp /* Keep in sync with struct got_imsg_tree_object definition! */
1308 3b647085 2019-11-23 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1309 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1310 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1311 e033d803 2018-04-23 stsp return err;
1313 3b647085 2019-11-23 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1314 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1315 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1316 3022d272 2019-11-14 stsp return err;
1319 3b647085 2019-11-23 stsp if (imsg_add(wbuf, name, namelen) == -1) {
1320 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1321 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1322 3022d272 2019-11-14 stsp return err;
1325 3022d272 2019-11-14 stsp wbuf->fd = -1;
1326 3022d272 2019-11-14 stsp imsg_close(ibuf, wbuf);
1328 b00c9821 2018-11-04 stsp totlen += len;
1331 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
1334 e033d803 2018-04-23 stsp const struct got_error *
1335 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1337 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1338 e033d803 2018-04-23 stsp const size_t min_datalen =
1339 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
1340 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
1341 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
1342 e033d803 2018-04-23 stsp int nentries = 0;
1344 e033d803 2018-04-23 stsp *tree = NULL;
1346 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
1348 1e51f5b9 2018-04-23 stsp goto done;
1351 e033d803 2018-04-23 stsp struct imsg imsg;
1353 e033d803 2018-04-23 stsp size_t datalen;
1354 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
1355 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
1357 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
1358 e033d803 2018-04-23 stsp if (n == 0) {
1359 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries)
1360 e033d803 2018-04-23 stsp goto get_more;
1364 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1365 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1367 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1369 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
1370 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
1371 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
1373 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
1374 e033d803 2018-04-23 stsp /* This message should only appear once. */
1375 e033d803 2018-04-23 stsp if (*tree != NULL) {
1376 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1379 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
1380 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1383 291624d8 2018-11-07 stsp itree = imsg.data;
1384 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
1385 e033d803 2018-04-23 stsp if (*tree == NULL) {
1386 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1389 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
1390 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
1391 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
1392 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
1395 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1396 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1398 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
1399 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
1400 e033d803 2018-04-23 stsp if (*tree == NULL) {
1401 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1404 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1405 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1409 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
1410 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
1411 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1412 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1415 c0588d8d 2018-11-07 stsp ite = imsg.data;
1417 56e0773d 2019-11-28 stsp if (datalen + 1 > sizeof(te->name)) {
1418 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1421 56e0773d 2019-11-28 stsp te = &(*tree)->entries[nentries];
1422 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1423 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
1425 56e0773d 2019-11-28 stsp memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1426 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
1427 56e0773d 2019-11-28 stsp te->idx = nentries;
1428 e033d803 2018-04-23 stsp nentries++;
1431 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1435 e033d803 2018-04-23 stsp imsg_free(&imsg);
1438 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
1439 1e51f5b9 2018-04-23 stsp if (err == NULL)
1440 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1441 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
1442 e033d803 2018-04-23 stsp *tree = NULL;
1445 ff6b18f8 2018-04-24 stsp return err;
1448 ff6b18f8 2018-04-24 stsp const struct got_error *
1449 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1450 ac544f8c 2019-01-13 stsp const uint8_t *data)
1452 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1454 2967a784 2018-04-24 stsp iblob.size = size;
1455 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
1457 ac544f8c 2019-01-13 stsp if (data) {
1458 ac544f8c 2019-01-13 stsp uint8_t *buf;
1460 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1461 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
1463 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
1464 ac544f8c 2019-01-13 stsp if (buf == NULL)
1465 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1467 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
1468 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
1469 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1470 ac544f8c 2019-01-13 stsp sizeof(iblob) + size) == -1) {
1471 ac544f8c 2019-01-13 stsp free(buf);
1472 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1474 ac544f8c 2019-01-13 stsp free(buf);
1476 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1477 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1478 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1479 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1483 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1486 ff6b18f8 2018-04-24 stsp const struct got_error *
1487 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1488 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1490 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1491 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1492 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1493 ff6b18f8 2018-04-24 stsp size_t datalen;
1495 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1497 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1499 ff6b18f8 2018-04-24 stsp return err;
1501 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1503 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1504 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1505 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1506 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1509 291624d8 2018-11-07 stsp iblob = imsg.data;
1510 291624d8 2018-11-07 stsp *size = iblob->size;
1511 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1513 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1514 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1518 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1519 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1523 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1524 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1525 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1528 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1531 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1535 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1537 f4a881ce 2018-11-17 stsp return err;
1540 f4a881ce 2018-11-17 stsp static const struct got_error *
1541 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1543 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1544 f4a881ce 2018-11-17 stsp size_t offset, remain;
1546 f4a881ce 2018-11-17 stsp offset = 0;
1547 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1548 f4a881ce 2018-11-17 stsp while (remain > 0) {
1549 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1551 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1552 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1553 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1557 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1561 f4a881ce 2018-11-17 stsp offset += n;
1562 f4a881ce 2018-11-17 stsp remain -= n;
1565 f4a881ce 2018-11-17 stsp return err;
1568 f4a881ce 2018-11-17 stsp const struct got_error *
1569 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1571 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1572 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1573 f4a881ce 2018-11-17 stsp uint8_t *buf;
1574 f4a881ce 2018-11-17 stsp size_t len, total;
1575 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1576 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1577 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1579 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1581 f4a881ce 2018-11-17 stsp buf = malloc(total);
1582 f4a881ce 2018-11-17 stsp if (buf == NULL)
1583 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1585 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1586 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1587 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1588 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1589 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1590 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1591 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1592 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1594 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1595 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1596 f4a881ce 2018-11-17 stsp len += tag_len;
1597 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1598 f4a881ce 2018-11-17 stsp len += tagger_len;
1600 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1601 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1602 f4a881ce 2018-11-17 stsp goto done;
1605 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1606 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1607 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1609 f4a881ce 2018-11-17 stsp goto done;
1611 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1613 f4a881ce 2018-11-17 stsp free(buf);
1614 f4a881ce 2018-11-17 stsp return err;
1617 f4a881ce 2018-11-17 stsp const struct got_error *
1618 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1620 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1621 f4a881ce 2018-11-17 stsp struct imsg imsg;
1622 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1623 f4a881ce 2018-11-17 stsp size_t len, datalen;
1624 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1625 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1626 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1628 f4a881ce 2018-11-17 stsp *tag = NULL;
1630 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1632 f4a881ce 2018-11-17 stsp return err;
1634 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1637 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1638 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1639 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1640 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1643 f4a881ce 2018-11-17 stsp itag = imsg.data;
1644 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1645 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1646 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1649 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1651 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1652 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1653 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1657 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1659 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1660 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1661 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1662 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1666 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1667 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1668 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1671 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1672 f4a881ce 2018-11-17 stsp itag->tag_len);
1673 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1675 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1677 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1678 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1679 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1681 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1682 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1683 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1688 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1689 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1690 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1693 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1694 f4a881ce 2018-11-17 stsp itag->tagger_len);
1695 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1697 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1699 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1700 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1701 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1702 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1706 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1708 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1709 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1710 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1713 f4a881ce 2018-11-17 stsp while (remain > 0) {
1714 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1715 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1718 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1720 f4a881ce 2018-11-17 stsp return err;
1722 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1723 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1725 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1727 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1728 f4a881ce 2018-11-17 stsp offset += n;
1729 f4a881ce 2018-11-17 stsp remain -= n;
1731 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1736 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1740 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1742 2178c42e 2018-04-22 stsp return err;
1745 876c234b 2018-09-10 stsp const struct got_error *
1746 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1747 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1749 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1750 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1751 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1754 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1755 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1756 876c234b 2018-09-10 stsp if (fd == -1)
1757 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1759 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1760 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1761 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1762 41496140 2019-02-21 stsp close(fd);
1763 41496140 2019-02-21 stsp return err;
1766 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1767 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1768 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1769 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1771 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1772 876c234b 2018-09-10 stsp if (fd == -1)
1773 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1775 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1777 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1778 41496140 2019-02-21 stsp close(fd);
1779 41496140 2019-02-21 stsp return err;
1782 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1785 876c234b 2018-09-10 stsp const struct got_error *
1786 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1787 106807b4 2018-09-15 stsp struct got_object_id *id)
1789 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1791 876c234b 2018-09-10 stsp iobj.idx = idx;
1792 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1794 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1795 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1796 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1797 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1799 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1802 aba9c984 2019-09-08 stsp const struct got_error *
1803 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1805 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1807 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1808 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
1809 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
1810 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
1811 aba9c984 2019-09-08 stsp close(fd);
1812 aba9c984 2019-09-08 stsp return err;
1815 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1818 aba9c984 2019-09-08 stsp const struct got_error *
1819 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1821 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1822 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1823 aba9c984 2019-09-08 stsp NULL, 0) == -1)
1824 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1825 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1827 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1830 aba9c984 2019-09-08 stsp const struct got_error *
1831 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1833 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1834 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1835 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1836 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
1838 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1841 aba9c984 2019-09-08 stsp const struct got_error *
1842 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1844 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1845 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1846 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1847 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1849 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
1852 cd95becd 2019-11-29 stsp const struct got_error *
1853 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1855 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
1856 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1857 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
1858 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
1860 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1863 aba9c984 2019-09-08 stsp const struct got_error *
1864 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1866 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
1867 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1868 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
1869 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
1871 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
1874 9a1cc63f 2020-02-03 stsp const struct got_error *
1875 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1877 aba9c984 2019-09-08 stsp size_t len = value ? strlen(value) + 1 : 0;
1879 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1880 aba9c984 2019-09-08 stsp value, len) == -1)
1881 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1883 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1886 aba9c984 2019-09-08 stsp const struct got_error *
1887 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1889 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1890 aba9c984 2019-09-08 stsp struct imsg imsg;
1891 aba9c984 2019-09-08 stsp size_t datalen;
1892 aba9c984 2019-09-08 stsp const size_t min_datalen = 0;
1894 aba9c984 2019-09-08 stsp *str = NULL;
1896 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1898 aba9c984 2019-09-08 stsp return err;
1899 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1901 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1902 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
1903 aba9c984 2019-09-08 stsp if (datalen == 0)
1905 aba9c984 2019-09-08 stsp *str = malloc(datalen);
1906 aba9c984 2019-09-08 stsp if (*str == NULL) {
1907 aba9c984 2019-09-08 stsp err = got_error_from_errno("malloc");
1910 aba9c984 2019-09-08 stsp if (strlcpy(*str, imsg.data, datalen) >= datalen)
1911 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_NO_SPACE);
1914 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1918 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1919 aba9c984 2019-09-08 stsp return err;
1922 aba9c984 2019-09-08 stsp const struct got_error *
1923 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1925 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1926 aba9c984 2019-09-08 stsp &value, sizeof(value)) == -1)
1927 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1929 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1932 63219cd2 2019-01-04 stsp const struct got_error *
1933 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1935 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1936 aba9c984 2019-09-08 stsp struct imsg imsg;
1937 aba9c984 2019-09-08 stsp size_t datalen;
1938 aba9c984 2019-09-08 stsp const size_t min_datalen =
1939 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
1943 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1945 aba9c984 2019-09-08 stsp return err;
1946 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1948 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1949 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
1950 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
1951 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1954 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
1957 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1961 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1962 cd95becd 2019-11-29 stsp return err;
1965 cd95becd 2019-11-29 stsp const struct got_error *
1966 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1967 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes, int nremotes)
1969 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1970 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1973 cd95becd 2019-11-29 stsp iremotes.nremotes = nremotes;
1974 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1975 cd95becd 2019-11-29 stsp &iremotes, sizeof(iremotes)) == -1)
1976 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1978 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1979 cd95becd 2019-11-29 stsp imsg_clear(ibuf);
1981 cd95becd 2019-11-29 stsp return err;
1983 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++) {
1984 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1985 cd95becd 2019-11-29 stsp size_t len = sizeof(iremote);
1986 cd95becd 2019-11-29 stsp struct ibuf *wbuf;
1988 fe6043b8 2020-03-20 stsp iremote.mirror_references = remotes[i].mirror_references;
1989 cd95becd 2019-11-29 stsp iremote.name_len = strlen(remotes[i].name);
1990 cd95becd 2019-11-29 stsp len += iremote.name_len;
1991 cd95becd 2019-11-29 stsp iremote.url_len = strlen(remotes[i].url);
1992 cd95becd 2019-11-29 stsp len += iremote.url_len;
1994 cd95becd 2019-11-29 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1995 cd95becd 2019-11-29 stsp if (wbuf == NULL)
1996 cd95becd 2019-11-29 stsp return got_error_from_errno(
1997 cd95becd 2019-11-29 stsp "imsg_create GITCONFIG_REMOTE");
1999 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
2000 cd95becd 2019-11-29 stsp err = got_error_from_errno(
2001 b364b1c2 2020-03-20 stsp "imsg_add GITCONFIG_REMOTE");
2002 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
2003 cd95becd 2019-11-29 stsp return err;
2006 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
2007 cd95becd 2019-11-29 stsp err = got_error_from_errno(
2008 b364b1c2 2020-03-20 stsp "imsg_add GITCONFIG_REMOTE");
2009 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
2010 cd95becd 2019-11-29 stsp return err;
2012 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
2013 cd95becd 2019-11-29 stsp err = got_error_from_errno(
2014 b364b1c2 2020-03-20 stsp "imsg_add GITCONFIG_REMOTE");
2015 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
2016 cd95becd 2019-11-29 stsp return err;
2019 cd95becd 2019-11-29 stsp wbuf->fd = -1;
2020 cd95becd 2019-11-29 stsp imsg_close(ibuf, wbuf);
2021 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
2023 cd95becd 2019-11-29 stsp return err;
2026 cd95becd 2019-11-29 stsp return NULL;
2029 cd95becd 2019-11-29 stsp const struct got_error *
2030 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2031 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
2033 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
2034 cd95becd 2019-11-29 stsp struct imsg imsg;
2035 cd95becd 2019-11-29 stsp size_t datalen;
2036 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
2037 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
2039 cd95becd 2019-11-29 stsp *remotes = NULL;
2040 cd95becd 2019-11-29 stsp *nremotes = 0;
2041 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
2043 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2045 cd95becd 2019-11-29 stsp return err;
2046 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2048 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
2049 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
2050 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
2051 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2054 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
2055 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
2056 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2057 cd95becd 2019-11-29 stsp return NULL;
2061 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
2062 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2065 aba9c984 2019-09-08 stsp imsg_free(&imsg);
2067 5146eb39 2020-03-20 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2068 cd95becd 2019-11-29 stsp if (*remotes == NULL)
2069 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
2071 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
2072 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
2074 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2077 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2079 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
2080 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
2081 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
2082 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
2083 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2086 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
2087 cd95becd 2019-11-29 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
2088 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
2089 cd95becd 2019-11-29 stsp iremote.url_len) > datalen) {
2090 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2093 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
2094 cd95becd 2019-11-29 stsp iremote.name_len);
2095 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
2096 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
2099 cd95becd 2019-11-29 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
2100 cd95becd 2019-11-29 stsp iremote.name_len, iremote.url_len);
2101 cd95becd 2019-11-29 stsp if (remote->url == NULL) {
2102 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
2103 cd95becd 2019-11-29 stsp free(remote->name);
2106 469dd726 2020-03-20 stsp remote->mirror_references = iremote.mirror_references;
2107 cd95becd 2019-11-29 stsp (*nremotes)++;
2110 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2114 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2119 cd95becd 2019-11-29 stsp if (err) {
2121 cd95becd 2019-11-29 stsp for (i = 0; i < *nremotes; i++) {
2122 cd95becd 2019-11-29 stsp free((*remotes)[i].name);
2123 cd95becd 2019-11-29 stsp free((*remotes)[i].url);
2125 cd95becd 2019-11-29 stsp free(*remotes);
2126 cd95becd 2019-11-29 stsp *remotes = NULL;
2127 cd95becd 2019-11-29 stsp *nremotes = 0;
2129 aba9c984 2019-09-08 stsp return err;
2132 ca6e02ac 2020-01-07 stsp const struct got_error *
2133 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2134 ca6e02ac 2020-01-07 stsp struct got_object_id *id, int idx, const char *path)
2136 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2137 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2138 ca6e02ac 2020-01-07 stsp size_t path_len = strlen(path) + 1;
2140 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2141 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
2142 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
2143 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
2144 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
2145 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2146 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2147 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2148 ca6e02ac 2020-01-07 stsp return err;
2150 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2151 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2152 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2153 ca6e02ac 2020-01-07 stsp return err;
2155 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, path, path_len) == -1) {
2156 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2157 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2158 ca6e02ac 2020-01-07 stsp return err;
2161 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2162 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2164 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2167 aba9c984 2019-09-08 stsp const struct got_error *
2168 ca6e02ac 2020-01-07 stsp got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2169 ca6e02ac 2020-01-07 stsp size_t ncommits, struct imsgbuf *ibuf)
2171 ca6e02ac 2020-01-07 stsp const struct got_error *err;
2172 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2175 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2176 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_traversed_commits) +
2177 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH);
2178 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
2179 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2181 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2182 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2183 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2184 ca6e02ac 2020-01-07 stsp return err;
2186 ca6e02ac 2020-01-07 stsp for (i = 0; i < ncommits; i++) {
2187 ca6e02ac 2020-01-07 stsp struct got_object_id *id = &commit_ids[i];
2188 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2189 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2190 ca6e02ac 2020-01-07 stsp "imsg_add TRAVERSED_COMMITS");
2191 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2192 ca6e02ac 2020-01-07 stsp return err;
2196 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2197 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2199 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2202 ca6e02ac 2020-01-07 stsp const struct got_error *
2203 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2204 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
2205 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2207 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2208 ca6e02ac 2020-01-07 stsp struct imsg imsg;
2209 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
2210 ca6e02ac 2020-01-07 stsp size_t datalen;
2211 ca6e02ac 2020-01-07 stsp int i, done = 0;
2213 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
2214 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
2216 ca6e02ac 2020-01-07 stsp while (!done) {
2217 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2219 ca6e02ac 2020-01-07 stsp return err;
2221 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2222 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
2223 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
2224 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
2225 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
2226 ca6e02ac 2020-01-07 stsp icommits->ncommits * SHA1_DIGEST_LENGTH) {
2227 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2230 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
2231 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
2232 ca6e02ac 2020-01-07 stsp uint8_t *sha1 = (uint8_t *)imsg.data +
2233 ca6e02ac 2020-01-07 stsp sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2234 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
2237 ca6e02ac 2020-01-07 stsp memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2238 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2240 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
2241 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
2242 ca6e02ac 2020-01-07 stsp *changed_commit_id =
2243 ca6e02ac 2020-01-07 stsp got_object_id_dup(qid->id);
2244 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2245 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2246 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
2252 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
2253 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2254 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2257 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
2258 ca6e02ac 2020-01-07 stsp datalen, ibuf);
2260 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2264 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2268 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
2274 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
2275 ca6e02ac 2020-01-07 stsp return err;
2278 ca6e02ac 2020-01-07 stsp const struct got_error *
2279 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2281 ca6e02ac 2020-01-07 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2282 ca6e02ac 2020-01-07 stsp NULL, 0) == -1)
2283 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2285 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2288 ca6e02ac 2020-01-07 stsp const struct got_error *
2289 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
2291 c39c25dd 2019-08-09 stsp const char *helpers[] = {
2292 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
2293 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
2294 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
2295 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
2296 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
2297 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
2298 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
2299 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK,
2300 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK,
2304 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
2305 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
2307 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
2310 63219cd2 2019-01-04 stsp return NULL;
2314 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2316 aba9c984 2019-09-08 stsp if (close(imsg_fds[0]) != 0) {
2317 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2321 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2322 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2325 aba9c984 2019-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2326 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2330 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
2331 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2332 aba9c984 2019-09-08 stsp strerror(errno));