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/wait.h>
23 531c3985 2020-03-18 stsp #include <ctype.h>
24 23c57b28 2020-09-11 naddy #include <limits.h>
25 a486b62b 2021-05-18 stsp #include <signal.h>
26 2178c42e 2018-04-22 stsp #include <stdio.h>
27 2178c42e 2018-04-22 stsp #include <stdlib.h>
28 2178c42e 2018-04-22 stsp #include <string.h>
29 2178c42e 2018-04-22 stsp #include <errno.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 2178c42e 2018-04-22 stsp #include <poll.h>
32 2178c42e 2018-04-22 stsp #include <imsg.h>
33 2178c42e 2018-04-22 stsp #include <sha1.h>
34 5822e79e 2023-02-23 op #include <sha2.h>
35 81a12da5 2020-09-09 naddy #include <unistd.h>
36 2178c42e 2018-04-22 stsp #include <zlib.h>
38 2178c42e 2018-04-22 stsp #include "got_object.h"
39 2178c42e 2018-04-22 stsp #include "got_error.h"
40 3022d272 2019-11-14 stsp #include "got_path.h"
41 cd95becd 2019-11-29 stsp #include "got_repository.h"
43 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
44 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
45 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
47 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
48 2f43cd69 2023-04-14 stsp #include "got_lib_object_qid.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
51 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
53 336075a4 2022-06-25 op #include "got_privsep.h"
56 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 c39c25dd 2019-08-09 stsp #ifndef nitems
60 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 2178c42e 2018-04-22 stsp static const struct got_error *
64 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
66 fe36cf76 2018-04-23 stsp const struct got_error *err;
69 13b2bc37 2022-10-23 stsp err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
71 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF)
72 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
76 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
77 fe36cf76 2018-04-23 stsp if (n == -1) {
78 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
79 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
80 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
83 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
85 e033d803 2018-04-23 stsp return NULL;
88 ad242220 2018-09-08 stsp const struct got_error *
89 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
91 876c234b 2018-09-10 stsp int child_status;
93 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
94 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
96 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
97 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
99 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
100 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
102 876c234b 2018-09-10 stsp return NULL;
105 73b7854a 2018-11-11 stsp static const struct got_error *
106 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
108 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
110 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
111 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
113 73b7854a 2018-11-11 stsp ierr = imsg->data;
114 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
115 73b7854a 2018-11-11 stsp static struct got_error serr;
116 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
117 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
118 73b7854a 2018-11-11 stsp return &serr;
121 73b7854a 2018-11-11 stsp return got_error(ierr->code);
124 876c234b 2018-09-10 stsp const struct got_error *
125 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
126 46de5bfd 2018-11-11 stsp size_t min_datalen)
128 e033d803 2018-04-23 stsp const struct got_error *err;
131 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
132 876c234b 2018-09-10 stsp if (n == -1)
133 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
135 876c234b 2018-09-10 stsp while (n == 0) {
136 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
138 876c234b 2018-09-10 stsp return err;
139 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
140 cbfaaf20 2020-01-06 stsp if (n == -1)
141 cbfaaf20 2020-01-06 stsp return got_error_from_errno("imsg_get");
144 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
145 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
147 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
148 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
149 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
152 73b7854a 2018-11-11 stsp return NULL;
155 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
157 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
159 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
160 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
163 2178c42e 2018-04-22 stsp ierr.code = err->code;
164 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
165 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
167 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
168 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
169 e93cd828 2018-11-11 stsp if (ret == -1) {
170 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
171 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
175 13b2bc37 2022-10-23 stsp poll_err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
176 5d43e84d 2018-04-23 stsp if (poll_err) {
177 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
178 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
182 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
183 5d43e84d 2018-04-23 stsp if (ret == -1) {
184 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
185 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
186 8934ea8b 2022-02-14 op imsg_clear(ibuf);
191 e033d803 2018-04-23 stsp static const struct got_error *
192 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
194 e033d803 2018-04-23 stsp const struct got_error *err;
196 13b2bc37 2022-10-23 stsp err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
198 e033d803 2018-04-23 stsp return err;
200 8934ea8b 2022-02-14 op if (imsg_flush(ibuf) == -1) {
201 8934ea8b 2022-02-14 op imsg_clear(ibuf);
202 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
205 e033d803 2018-04-23 stsp return NULL;
208 ad242220 2018-09-08 stsp const struct got_error *
209 e70bf110 2020-03-22 stsp got_privsep_flush_imsg(struct imsgbuf *ibuf)
211 e70bf110 2020-03-22 stsp return flush_imsg(ibuf);
214 e70bf110 2020-03-22 stsp const struct got_error *
215 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
217 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
219 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
221 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
222 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
224 ad4e3a35 2023-10-26 op return flush_imsg(&ibuf);
227 93658fb9 2020-03-18 stsp const struct got_error *
228 d5c81d44 2021-07-08 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
229 d5c81d44 2021-07-08 stsp struct got_object_id *id)
231 d5c81d44 2021-07-08 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
232 d5c81d44 2021-07-08 stsp id, sizeof(*id)) == -1)
233 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
235 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
238 59d1e4a0 2021-03-10 stsp const struct got_error *
239 d5c81d44 2021-07-08 stsp got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
240 d5c81d44 2021-07-08 stsp struct got_object_id *id)
242 2161bc24 2023-11-03 op const struct got_error *err;
244 d5c81d44 2021-07-08 stsp if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
245 2161bc24 2023-11-03 op id, sizeof(*id)) == -1) {
246 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
251 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
254 59d1e4a0 2021-03-10 stsp const struct got_error *
255 59d1e4a0 2021-03-10 stsp got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
257 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
259 59d1e4a0 2021-03-10 stsp if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
261 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
262 59d1e4a0 2021-03-10 stsp close(outfd);
263 59d1e4a0 2021-03-10 stsp return err;
266 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
269 59d1e4a0 2021-03-10 stsp const struct got_error *
270 59d1e4a0 2021-03-10 stsp got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
271 59d1e4a0 2021-03-10 stsp uint8_t *data)
273 59d1e4a0 2021-03-10 stsp struct got_imsg_raw_obj iobj;
274 59d1e4a0 2021-03-10 stsp size_t len = sizeof(iobj);
275 59d1e4a0 2021-03-10 stsp struct ibuf *wbuf;
277 031611fc 2022-06-18 op memset(&iobj, 0, sizeof(iobj));
278 59d1e4a0 2021-03-10 stsp iobj.hdrlen = hdrlen;
279 59d1e4a0 2021-03-10 stsp iobj.size = size;
281 a8591711 2021-06-18 stsp if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
282 a8591711 2021-06-18 stsp len += (size_t)size + hdrlen;
284 59d1e4a0 2021-03-10 stsp wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
285 ad4e3a35 2023-10-26 op if (wbuf == NULL)
286 ad4e3a35 2023-10-26 op return got_error_from_errno("imsg_create RAW_OBJECT");
288 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
289 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add RAW_OBJECT");
291 a8591711 2021-06-18 stsp if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
292 1453347d 2022-05-19 stsp if (imsg_add(wbuf, data, size + hdrlen) == -1)
293 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add RAW_OBJECT");
296 59d1e4a0 2021-03-10 stsp wbuf->fd = -1;
297 59d1e4a0 2021-03-10 stsp imsg_close(ibuf, wbuf);
299 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
302 1785f84a 2018-12-23 stsp const struct got_error *
303 59d1e4a0 2021-03-10 stsp got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
304 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf)
306 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
307 59d1e4a0 2021-03-10 stsp struct imsg imsg;
308 59d1e4a0 2021-03-10 stsp struct got_imsg_raw_obj *iobj;
309 59d1e4a0 2021-03-10 stsp size_t datalen;
311 59d1e4a0 2021-03-10 stsp *outbuf = NULL;
313 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
315 59d1e4a0 2021-03-10 stsp return err;
317 59d1e4a0 2021-03-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
319 59d1e4a0 2021-03-10 stsp switch (imsg.hdr.type) {
320 59d1e4a0 2021-03-10 stsp case GOT_IMSG_RAW_OBJECT:
321 59d1e4a0 2021-03-10 stsp if (datalen < sizeof(*iobj)) {
322 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
325 59d1e4a0 2021-03-10 stsp iobj = imsg.data;
326 59d1e4a0 2021-03-10 stsp *size = iobj->size;
327 59d1e4a0 2021-03-10 stsp *hdrlen = iobj->hdrlen;
329 59d1e4a0 2021-03-10 stsp if (datalen == sizeof(*iobj)) {
330 59d1e4a0 2021-03-10 stsp /* Data has been written to file descriptor. */
334 c98b0f0b 2022-06-14 op if (*size < 0 ||
335 c98b0f0b 2022-06-14 op *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
336 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
340 a8591711 2021-06-18 stsp *outbuf = malloc(*size + *hdrlen);
341 59d1e4a0 2021-03-10 stsp if (*outbuf == NULL) {
342 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("malloc");
345 a8591711 2021-06-18 stsp memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
348 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
352 59d1e4a0 2021-03-10 stsp imsg_free(&imsg);
354 59d1e4a0 2021-03-10 stsp return err;
357 59d1e4a0 2021-03-10 stsp const struct got_error *
358 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
359 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
361 41496140 2019-02-21 stsp const struct got_error *err = NULL;
362 d5c81d44 2021-07-08 stsp struct got_imsg_packed_object iobj;
363 d5c81d44 2021-07-08 stsp void *data;
364 1785f84a 2018-12-23 stsp size_t len;
366 031611fc 2022-06-18 op memset(&iobj, 0, sizeof(iobj));
367 d5c81d44 2021-07-08 stsp if (pack_idx != -1) { /* commit is packed */
368 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
369 265df21f 2023-01-31 op memcpy(&iobj.id, id, sizeof(iobj.id));
370 d5c81d44 2021-07-08 stsp data = &iobj;
371 1785f84a 2018-12-23 stsp len = sizeof(iobj);
374 d5c81d44 2021-07-08 stsp len = sizeof(*id);
377 d5c81d44 2021-07-08 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
379 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
381 41496140 2019-02-21 stsp return err;
384 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
387 13c729f7 2018-12-24 stsp const struct got_error *
388 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
389 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
391 2161bc24 2023-11-03 op const struct got_error *err;
392 7f358e3b 2019-11-23 stsp struct ibuf *wbuf;
393 d5c81d44 2021-07-08 stsp size_t len;
395 d5c81d44 2021-07-08 stsp if (pack_idx != -1)
396 d5c81d44 2021-07-08 stsp len = sizeof(struct got_imsg_packed_object);
398 d5c81d44 2021-07-08 stsp len = sizeof(*id);
400 7f358e3b 2019-11-23 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
401 2161bc24 2023-11-03 op if (wbuf == NULL) {
402 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_create TREE_REQUEST");
403 2161bc24 2023-11-03 op if (fd != -1)
408 2161bc24 2023-11-03 op if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
409 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_add TREE_REQUEST");
410 2161bc24 2023-11-03 op if (fd != -1)
415 d5c81d44 2021-07-08 stsp if (pack_idx != -1) { /* tree is packed */
416 2161bc24 2023-11-03 op if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
417 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_add TREE_REQUEST");
418 2161bc24 2023-11-03 op if (fd != -1)
424 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
425 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
427 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
430 268f7291 2018-12-24 stsp const struct got_error *
431 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
432 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
434 2161bc24 2023-11-03 op const struct got_error *err;
435 d5c81d44 2021-07-08 stsp struct got_imsg_packed_object iobj;
436 d5c81d44 2021-07-08 stsp void *data;
437 268f7291 2018-12-24 stsp size_t len;
439 031611fc 2022-06-18 op memset(&iobj, 0, sizeof(iobj));
440 d5c81d44 2021-07-08 stsp if (pack_idx != -1) { /* tag is packed */
441 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
442 265df21f 2023-01-31 op memcpy(&iobj.id, id, sizeof(iobj.id));
443 d5c81d44 2021-07-08 stsp data = &iobj;
444 268f7291 2018-12-24 stsp len = sizeof(iobj);
447 d5c81d44 2021-07-08 stsp len = sizeof(*id);
450 d5c81d44 2021-07-08 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
452 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_compose TAG_REQUEST");
453 2161bc24 2023-11-03 op if (fd != -1)
458 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
461 7762fe12 2018-11-05 stsp const struct got_error *
462 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
463 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
465 41496140 2019-02-21 stsp const struct got_error *err = NULL;
466 d5c81d44 2021-07-08 stsp struct got_imsg_packed_object iobj;
467 d5c81d44 2021-07-08 stsp void *data;
468 ebc55e2d 2018-12-24 stsp size_t len;
470 031611fc 2022-06-18 op memset(&iobj, 0, sizeof(iobj));
471 d5c81d44 2021-07-08 stsp if (pack_idx != -1) { /* blob is packed */
472 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
473 265df21f 2023-01-31 op memcpy(&iobj.id, id, sizeof(iobj.id));
474 d5c81d44 2021-07-08 stsp data = &iobj;
475 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
478 d5c81d44 2021-07-08 stsp len = sizeof(*id);
481 d5c81d44 2021-07-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
483 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
484 41496140 2019-02-21 stsp close(infd);
485 41496140 2019-02-21 stsp return err;
488 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
491 55da3778 2018-09-10 stsp const struct got_error *
492 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
494 41496140 2019-02-21 stsp const struct got_error *err = NULL;
496 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
498 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
499 41496140 2019-02-21 stsp close(outfd);
500 41496140 2019-02-21 stsp return err;
503 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
506 73ab1060 2020-03-18 stsp static const struct got_error *
507 73ab1060 2020-03-18 stsp send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
509 41496140 2019-02-21 stsp const struct got_error *err = NULL;
511 73ab1060 2020-03-18 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
512 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
514 41496140 2019-02-21 stsp return err;
517 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
520 ad242220 2018-09-08 stsp const struct got_error *
521 73ab1060 2020-03-18 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
523 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
526 73ab1060 2020-03-18 stsp const struct got_error *
527 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
529 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
531 1a69d37f 2022-06-18 op memset(&iobj, 0, sizeof(iobj));
533 7841c0d1 2023-02-01 op memcpy(&iobj.id, &obj->id, sizeof(iobj.id));
534 2178c42e 2018-04-22 stsp iobj.type = obj->type;
535 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
536 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
537 2178c42e 2018-04-22 stsp iobj.size = obj->size;
538 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
539 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
540 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
543 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
545 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
547 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
550 cfd633c2 2018-09-10 stsp const struct got_error *
551 33501562 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
552 abc59930 2021-09-05 naddy struct got_pathlist_head *have_refs, int fetch_all_branches,
553 abc59930 2021-09-05 naddy struct got_pathlist_head *wanted_branches,
554 188f8dcf 2023-02-07 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
555 118a625d 2023-02-18 mark const char *worktree_branch, const char *remote_head,
556 118a625d 2023-02-18 mark int no_head, int verbosity)
558 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
559 33501562 2020-03-18 stsp struct ibuf *wbuf;
560 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
561 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetchreq;
562 118a625d 2023-02-18 mark size_t remote_head_len, worktree_branch_len, len = sizeof(fetchreq);
564 ec218e16 2023-02-07 mark if (worktree_branch) {
565 ec218e16 2023-02-07 mark worktree_branch_len = strlen(worktree_branch);
566 118a625d 2023-02-18 mark len += worktree_branch_len;
568 118a625d 2023-02-18 mark if (remote_head) {
569 118a625d 2023-02-18 mark remote_head_len = strlen(remote_head);
570 118a625d 2023-02-18 mark len += remote_head_len;
573 188f8dcf 2023-02-07 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
575 188f8dcf 2023-02-07 stsp return got_error(GOT_ERR_NO_SPACE);
578 188f8dcf 2023-02-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
579 2161bc24 2023-11-03 op if (wbuf == NULL) {
580 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_create FETCH_HAVE_REF");
585 4ba14133 2020-03-20 stsp memset(&fetchreq, 0, sizeof(fetchreq));
586 0dd7613c 2023-02-13 mark fetchreq.no_head = no_head;
587 4ba14133 2020-03-20 stsp fetchreq.fetch_all_branches = fetch_all_branches;
588 41b0de12 2020-03-21 stsp fetchreq.list_refs_only = list_refs_only;
589 2690194b 2020-03-21 stsp fetchreq.verbosity = verbosity;
590 188f8dcf 2023-02-07 stsp if (worktree_branch != NULL)
591 ec218e16 2023-02-07 mark fetchreq.worktree_branch_len = worktree_branch_len;
592 118a625d 2023-02-18 mark if (remote_head != NULL)
593 118a625d 2023-02-18 mark fetchreq.remote_head_len = remote_head_len;
594 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, have_refs, entry)
595 4ba14133 2020-03-20 stsp fetchreq.n_have_refs++;
596 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry)
597 4ba14133 2020-03-20 stsp fetchreq.n_wanted_branches++;
598 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry)
599 0e4002ca 2020-03-21 stsp fetchreq.n_wanted_refs++;
600 188f8dcf 2023-02-07 stsp if (imsg_add(wbuf, &fetchreq, sizeof(fetchreq)) == -1)
601 188f8dcf 2023-02-07 stsp return got_error_from_errno("imsg_add FETCH_REQUEST");
602 188f8dcf 2023-02-07 stsp if (worktree_branch) {
603 2161bc24 2023-11-03 op if (imsg_add(wbuf, worktree_branch, worktree_branch_len)
605 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_add FETCH_REQUEST");
610 118a625d 2023-02-18 mark if (remote_head) {
611 2161bc24 2023-11-03 op if (imsg_add(wbuf, remote_head, remote_head_len) == -1) {
612 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_add FETCH_REQUEST");
617 188f8dcf 2023-02-07 stsp wbuf->fd = fd;
619 188f8dcf 2023-02-07 stsp imsg_close(ibuf, wbuf);
621 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
623 659e7fbd 2020-03-20 stsp return err;
625 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
626 33501562 2020-03-18 stsp const char *name = pe->path;
627 33501562 2020-03-18 stsp size_t name_len = pe->path_len;
628 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
630 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
631 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
632 4ba14133 2020-03-20 stsp if (wbuf == NULL)
633 4ba14133 2020-03-20 stsp return got_error_from_errno("imsg_create FETCH_HAVE_REF");
635 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_ref! */
636 4b4da3bb 2023-02-01 op if (imsg_add(wbuf, id, sizeof(*id)) == -1)
637 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_HAVE_REF");
638 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
639 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_HAVE_REF");
640 1453347d 2022-05-19 stsp if (imsg_add(wbuf, name, name_len) == -1)
641 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_HAVE_REF");
643 4ba14133 2020-03-20 stsp wbuf->fd = -1;
644 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
645 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
647 4ba14133 2020-03-20 stsp return err;
650 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
651 4ba14133 2020-03-20 stsp const char *name = pe->path;
652 4ba14133 2020-03-20 stsp size_t name_len = pe->path_len;
654 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
655 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
657 4ba14133 2020-03-20 stsp if (wbuf == NULL)
658 4ba14133 2020-03-20 stsp return got_error_from_errno(
659 62d463ca 2020-10-20 naddy "imsg_create FETCH_WANTED_BRANCH");
661 4ba14133 2020-03-20 stsp /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
662 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
663 1453347d 2022-05-19 stsp return got_error_from_errno(
664 4ba14133 2020-03-20 stsp "imsg_add FETCH_WANTED_BRANCH");
665 1453347d 2022-05-19 stsp if (imsg_add(wbuf, name, name_len) == -1)
666 1453347d 2022-05-19 stsp return got_error_from_errno(
667 62d463ca 2020-10-20 naddy "imsg_add FETCH_WANTED_BRANCH");
669 4ba14133 2020-03-20 stsp wbuf->fd = -1;
670 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
671 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
673 4ba14133 2020-03-20 stsp return err;
676 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
677 0e4002ca 2020-03-21 stsp const char *name = pe->path;
678 0e4002ca 2020-03-21 stsp size_t name_len = pe->path_len;
680 0e4002ca 2020-03-21 stsp len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
681 0e4002ca 2020-03-21 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
683 0e4002ca 2020-03-21 stsp if (wbuf == NULL)
684 0e4002ca 2020-03-21 stsp return got_error_from_errno(
685 62d463ca 2020-10-20 naddy "imsg_create FETCH_WANTED_REF");
687 0e4002ca 2020-03-21 stsp /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
688 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
689 1453347d 2022-05-19 stsp return got_error_from_errno(
690 0e4002ca 2020-03-21 stsp "imsg_add FETCH_WANTED_REF");
691 1453347d 2022-05-19 stsp if (imsg_add(wbuf, name, name_len) == -1)
692 1453347d 2022-05-19 stsp return got_error_from_errno(
693 62d463ca 2020-10-20 naddy "imsg_add FETCH_WANTED_REF");
695 0e4002ca 2020-03-21 stsp wbuf->fd = -1;
696 0e4002ca 2020-03-21 stsp imsg_close(ibuf, wbuf);
697 0e4002ca 2020-03-21 stsp err = flush_imsg(ibuf);
699 0e4002ca 2020-03-21 stsp return err;
702 4ba14133 2020-03-20 stsp return NULL;
705 93658fb9 2020-03-18 stsp const struct got_error *
706 f826addf 2020-03-18 stsp got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
708 f826addf 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
711 531c3985 2020-03-18 stsp const struct got_error *
712 8f2d01a6 2020-03-18 stsp got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
713 531c3985 2020-03-18 stsp char **refname, struct got_pathlist_head *symrefs, char **server_progress,
714 1d72a2a0 2020-03-24 stsp off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
716 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
717 b9f99abf 2020-03-18 stsp struct imsg imsg;
718 b9f99abf 2020-03-18 stsp size_t datalen;
719 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symrefs *isymrefs = NULL;
720 abe0f35f 2020-03-18 stsp size_t n, remain;
725 8f2d01a6 2020-03-18 stsp *id = NULL;
726 b9f99abf 2020-03-18 stsp *refname = NULL;
727 531c3985 2020-03-18 stsp *server_progress = NULL;
728 5a489642 2020-03-19 stsp *packfile_size = 0;
729 1d72a2a0 2020-03-24 stsp memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
731 531c3985 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
733 b9f99abf 2020-03-18 stsp return err;
735 b9f99abf 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
736 b9f99abf 2020-03-18 stsp switch (imsg.hdr.type) {
737 abe0f35f 2020-03-18 stsp case GOT_IMSG_FETCH_SYMREFS:
738 abe0f35f 2020-03-18 stsp if (datalen < sizeof(*isymrefs)) {
739 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
742 abe0f35f 2020-03-18 stsp if (isymrefs != NULL) {
743 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
746 abe0f35f 2020-03-18 stsp isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
747 abe0f35f 2020-03-18 stsp off = sizeof(*isymrefs);
748 abe0f35f 2020-03-18 stsp remain = datalen - off;
749 abe0f35f 2020-03-18 stsp for (n = 0; n < isymrefs->nsymrefs; n++) {
750 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symref *s;
751 abe0f35f 2020-03-18 stsp char *name, *target;
752 abe0f35f 2020-03-18 stsp if (remain < sizeof(struct got_imsg_fetch_symref)) {
753 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
756 abe0f35f 2020-03-18 stsp s = (struct got_imsg_fetch_symref *)(imsg.data + off);
757 abe0f35f 2020-03-18 stsp off += sizeof(*s);
758 abe0f35f 2020-03-18 stsp remain -= sizeof(*s);
759 abe0f35f 2020-03-18 stsp if (remain < s->name_len) {
760 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
763 abe0f35f 2020-03-18 stsp name = strndup(imsg.data + off, s->name_len);
764 abe0f35f 2020-03-18 stsp if (name == NULL) {
765 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
768 abe0f35f 2020-03-18 stsp off += s->name_len;
769 abe0f35f 2020-03-18 stsp remain -= s->name_len;
770 abe0f35f 2020-03-18 stsp if (remain < s->target_len) {
771 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
772 abe0f35f 2020-03-18 stsp free(name);
775 abe0f35f 2020-03-18 stsp target = strndup(imsg.data + off, s->target_len);
776 abe0f35f 2020-03-18 stsp if (target == NULL) {
777 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
778 abe0f35f 2020-03-18 stsp free(name);
781 abe0f35f 2020-03-18 stsp off += s->target_len;
782 abe0f35f 2020-03-18 stsp remain -= s->target_len;
783 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
785 abe0f35f 2020-03-18 stsp free(name);
786 abe0f35f 2020-03-18 stsp free(target);
791 ea7396b9 2020-03-18 stsp case GOT_IMSG_FETCH_REF:
792 0701e66c 2023-02-01 op if (datalen <= sizeof(**id)) {
793 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
796 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
797 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
798 b9f99abf 2020-03-18 stsp err = got_error_from_errno("malloc");
801 0701e66c 2023-02-01 op memcpy(*id, imsg.data, sizeof(**id));
802 0701e66c 2023-02-01 op *refname = strndup(imsg.data + sizeof(**id),
803 0701e66c 2023-02-01 op datalen - sizeof(**id));
804 b9f99abf 2020-03-18 stsp if (*refname == NULL) {
805 b9f99abf 2020-03-18 stsp err = got_error_from_errno("strndup");
809 531c3985 2020-03-18 stsp case GOT_IMSG_FETCH_SERVER_PROGRESS:
810 531c3985 2020-03-18 stsp if (datalen == 0) {
811 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
814 531c3985 2020-03-18 stsp *server_progress = strndup(imsg.data, datalen);
815 531c3985 2020-03-18 stsp if (*server_progress == NULL) {
816 531c3985 2020-03-18 stsp err = got_error_from_errno("strndup");
819 531c3985 2020-03-18 stsp for (i = 0; i < datalen; i++) {
820 531c3985 2020-03-18 stsp if (!isprint((unsigned char)(*server_progress)[i]) &&
821 531c3985 2020-03-18 stsp !isspace((unsigned char)(*server_progress)[i])) {
822 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
823 531c3985 2020-03-18 stsp free(*server_progress);
824 531c3985 2020-03-18 stsp *server_progress = NULL;
829 d2cdc636 2020-03-18 stsp case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
830 d2cdc636 2020-03-18 stsp if (datalen < sizeof(*packfile_size)) {
831 d2cdc636 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
834 d2cdc636 2020-03-18 stsp memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
836 8f2d01a6 2020-03-18 stsp case GOT_IMSG_FETCH_DONE:
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 1d72a2a0 2020-03-24 stsp memcpy(pack_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 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
856 f8a36e22 2021-08-26 stsp return err;
859 f8a36e22 2021-08-26 stsp static const struct got_error *
860 f8a36e22 2021-08-26 stsp send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
861 f8a36e22 2021-08-26 stsp int delete, struct imsgbuf *ibuf)
863 f8a36e22 2021-08-26 stsp size_t len;
864 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
866 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_ref) + name_len;
867 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
868 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
869 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REF");
871 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_ref! */
872 5eb14fb9 2023-02-01 op if (imsg_add(wbuf, id, sizeof(*id)) == -1)
873 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add SEND_REF");
874 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
875 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add SEND_REF");
876 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
877 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add SEND_REF");
878 1453347d 2022-05-19 stsp if (imsg_add(wbuf, name, name_len) == -1)
879 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add SEND_REF");
881 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
882 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
883 f8a36e22 2021-08-26 stsp return flush_imsg(ibuf);
886 f8a36e22 2021-08-26 stsp const struct got_error *
887 f8a36e22 2021-08-26 stsp got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
888 abc59930 2021-09-05 naddy struct got_pathlist_head *have_refs,
889 abc59930 2021-09-05 naddy struct got_pathlist_head *delete_refs,
890 abc59930 2021-09-05 naddy int verbosity)
892 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
893 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
894 f8a36e22 2021-08-26 stsp struct got_imsg_send_request sendreq;
895 f8a36e22 2021-08-26 stsp struct got_object_id zero_id;
897 f8a36e22 2021-08-26 stsp memset(&zero_id, 0, sizeof(zero_id));
898 f8a36e22 2021-08-26 stsp memset(&sendreq, 0, sizeof(sendreq));
899 f8a36e22 2021-08-26 stsp sendreq.verbosity = verbosity;
900 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, have_refs, entry)
901 f8a36e22 2021-08-26 stsp sendreq.nrefs++;
902 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry)
903 f8a36e22 2021-08-26 stsp sendreq.nrefs++;
904 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
905 f8a36e22 2021-08-26 stsp &sendreq, sizeof(sendreq)) == -1) {
906 f8a36e22 2021-08-26 stsp err = got_error_from_errno(
907 f8a36e22 2021-08-26 stsp "imsg_compose FETCH_SERVER_PROGRESS");
912 f8a36e22 2021-08-26 stsp err = flush_imsg(ibuf);
916 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, have_refs, entry) {
917 f8a36e22 2021-08-26 stsp const char *name = pe->path;
918 f8a36e22 2021-08-26 stsp size_t name_len = pe->path_len;
919 f8a36e22 2021-08-26 stsp struct got_object_id *id = pe->data;
920 f8a36e22 2021-08-26 stsp err = send_send_ref(name, name_len, id, 0, ibuf);
925 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
926 f8a36e22 2021-08-26 stsp const char *name = pe->path;
927 f8a36e22 2021-08-26 stsp size_t name_len = pe->path_len;
928 f8a36e22 2021-08-26 stsp err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
933 f8a36e22 2021-08-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
934 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
935 f8a36e22 2021-08-26 stsp return err;
938 f8a36e22 2021-08-26 stsp const struct got_error *
939 f8a36e22 2021-08-26 stsp got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
940 f8a36e22 2021-08-26 stsp struct imsgbuf *ibuf)
942 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
943 f8a36e22 2021-08-26 stsp struct imsg imsg;
944 f8a36e22 2021-08-26 stsp size_t datalen;
945 f8a36e22 2021-08-26 stsp int done = 0;
946 f8a36e22 2021-08-26 stsp struct got_imsg_send_remote_ref iremote_ref;
947 f8a36e22 2021-08-26 stsp struct got_object_id *id = NULL;
948 f8a36e22 2021-08-26 stsp char *refname = NULL;
949 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
951 f8a36e22 2021-08-26 stsp while (!done) {
952 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
954 f8a36e22 2021-08-26 stsp return err;
955 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
956 f8a36e22 2021-08-26 stsp switch (imsg.hdr.type) {
957 f8a36e22 2021-08-26 stsp case GOT_IMSG_SEND_REMOTE_REF:
958 f8a36e22 2021-08-26 stsp if (datalen < sizeof(iremote_ref)) {
959 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
962 f8a36e22 2021-08-26 stsp memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
963 f8a36e22 2021-08-26 stsp if (datalen != sizeof(iremote_ref) +
964 f8a36e22 2021-08-26 stsp iremote_ref.name_len) {
965 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
968 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
969 f8a36e22 2021-08-26 stsp if (id == NULL) {
970 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
973 427f294c 2023-02-01 op memcpy(id, &iremote_ref.id, sizeof(*id));
974 f8a36e22 2021-08-26 stsp refname = strndup(imsg.data + sizeof(iremote_ref),
975 f8a36e22 2021-08-26 stsp datalen - sizeof(iremote_ref));
976 f8a36e22 2021-08-26 stsp if (refname == NULL) {
977 f8a36e22 2021-08-26 stsp err = got_error_from_errno("strndup");
980 f8a36e22 2021-08-26 stsp err = got_pathlist_insert(&new, remote_refs,
981 f8a36e22 2021-08-26 stsp refname, id);
984 f8a36e22 2021-08-26 stsp if (new == NULL) { /* duplicate which wasn't inserted */
986 f8a36e22 2021-08-26 stsp free(refname);
989 f8a36e22 2021-08-26 stsp refname = NULL;
991 f8a36e22 2021-08-26 stsp case GOT_IMSG_SEND_PACK_REQUEST:
992 f8a36e22 2021-08-26 stsp if (datalen != 0) {
993 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
996 f8a36e22 2021-08-26 stsp /* got-send-pack is now waiting for a pack file. */
1000 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1006 f8a36e22 2021-08-26 stsp free(refname);
1007 b9f99abf 2020-03-18 stsp imsg_free(&imsg);
1008 b9f99abf 2020-03-18 stsp return err;
1011 93658fb9 2020-03-18 stsp const struct got_error *
1012 f8a36e22 2021-08-26 stsp got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
1014 f8a36e22 2021-08-26 stsp return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
1017 f8a36e22 2021-08-26 stsp const struct got_error *
1018 f8a36e22 2021-08-26 stsp got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1019 6242c45b 2022-11-14 op int *success, char **refname, char **errmsg, struct imsgbuf *ibuf)
1021 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
1022 f8a36e22 2021-08-26 stsp struct imsg imsg;
1023 f8a36e22 2021-08-26 stsp size_t datalen;
1024 f8a36e22 2021-08-26 stsp struct got_imsg_send_ref_status iref_status;
1026 f8a36e22 2021-08-26 stsp /* Do not reset the current value of 'bytes_sent', it accumulates. */
1027 f8a36e22 2021-08-26 stsp *done = 0;
1028 f8a36e22 2021-08-26 stsp *success = 0;
1029 f8a36e22 2021-08-26 stsp *refname = NULL;
1030 6242c45b 2022-11-14 op *errmsg = NULL;
1032 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1034 f8a36e22 2021-08-26 stsp return err;
1036 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1037 f8a36e22 2021-08-26 stsp switch (imsg.hdr.type) {
1038 f8a36e22 2021-08-26 stsp case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1039 f8a36e22 2021-08-26 stsp if (datalen < sizeof(*bytes_sent)) {
1040 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1043 f8a36e22 2021-08-26 stsp memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1045 f8a36e22 2021-08-26 stsp case GOT_IMSG_SEND_REF_STATUS:
1046 f8a36e22 2021-08-26 stsp if (datalen < sizeof(iref_status)) {
1047 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1050 f8a36e22 2021-08-26 stsp memcpy(&iref_status, imsg.data, sizeof(iref_status));
1051 6242c45b 2022-11-14 op if (datalen != sizeof(iref_status) + iref_status.name_len +
1052 6242c45b 2022-11-14 op iref_status.errmsg_len) {
1053 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1056 f8a36e22 2021-08-26 stsp *success = iref_status.success;
1057 f8a36e22 2021-08-26 stsp *refname = strndup(imsg.data + sizeof(iref_status),
1058 f8a36e22 2021-08-26 stsp iref_status.name_len);
1060 6242c45b 2022-11-14 op if (iref_status.errmsg_len != 0)
1061 6242c45b 2022-11-14 op *errmsg = strndup(imsg.data + sizeof(iref_status) +
1062 6242c45b 2022-11-14 op iref_status.name_len, iref_status.errmsg_len);
1064 f8a36e22 2021-08-26 stsp case GOT_IMSG_SEND_DONE:
1065 f8a36e22 2021-08-26 stsp if (datalen != 0) {
1066 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1069 f8a36e22 2021-08-26 stsp *done = 1;
1072 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1076 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
1077 f8a36e22 2021-08-26 stsp return err;
1080 f8a36e22 2021-08-26 stsp const struct got_error *
1081 fd251256 2020-03-24 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1084 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
1086 668a20f6 2020-03-18 stsp /* Keep in sync with struct got_imsg_index_pack_request */
1087 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1088 fd251256 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1089 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1090 93658fb9 2020-03-18 stsp close(fd);
1091 93658fb9 2020-03-18 stsp return err;
1093 baa9fea0 2020-03-18 stsp return flush_imsg(ibuf);
1096 baa9fea0 2020-03-18 stsp const struct got_error *
1097 73ab1060 2020-03-18 stsp got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1099 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1102 73ab1060 2020-03-18 stsp const struct got_error *
1103 668a20f6 2020-03-18 stsp got_privsep_recv_index_progress(int *done, int *nobj_total,
1104 668a20f6 2020-03-18 stsp int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1105 668a20f6 2020-03-18 stsp struct imsgbuf *ibuf)
1107 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
1108 93658fb9 2020-03-18 stsp struct imsg imsg;
1109 baa9fea0 2020-03-18 stsp struct got_imsg_index_pack_progress *iprogress;
1110 baa9fea0 2020-03-18 stsp size_t datalen;
1112 baa9fea0 2020-03-18 stsp *done = 0;
1113 668a20f6 2020-03-18 stsp *nobj_total = 0;
1114 668a20f6 2020-03-18 stsp *nobj_indexed = 0;
1115 668a20f6 2020-03-18 stsp *nobj_resolved = 0;
1117 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1119 93658fb9 2020-03-18 stsp return err;
1121 baa9fea0 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1122 baa9fea0 2020-03-18 stsp switch (imsg.hdr.type) {
1123 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_PROGRESS:
1124 baa9fea0 2020-03-18 stsp if (datalen < sizeof(*iprogress)) {
1125 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1128 baa9fea0 2020-03-18 stsp iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1129 c98b0f0b 2022-06-14 op if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1130 c98b0f0b 2022-06-14 op iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1131 c98b0f0b 2022-06-14 op err = got_error(GOT_ERR_RANGE);
1134 668a20f6 2020-03-18 stsp *nobj_total = iprogress->nobj_total;
1135 668a20f6 2020-03-18 stsp *nobj_indexed = iprogress->nobj_indexed;
1136 668a20f6 2020-03-18 stsp *nobj_loose = iprogress->nobj_loose;
1137 668a20f6 2020-03-18 stsp *nobj_resolved = iprogress->nobj_resolved;
1139 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_DONE:
1140 baa9fea0 2020-03-18 stsp if (datalen != 0) {
1141 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1144 baa9fea0 2020-03-18 stsp *done = 1;
1147 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1151 93658fb9 2020-03-18 stsp imsg_free(&imsg);
1152 baa9fea0 2020-03-18 stsp return err;
1155 93658fb9 2020-03-18 stsp const struct got_error *
1156 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1157 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
1159 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
1160 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1162 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
1163 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1164 291624d8 2018-11-07 stsp iobj = imsg->data;
1166 c98b0f0b 2022-06-14 op if (iobj->pack_offset < 0)
1167 c98b0f0b 2022-06-14 op return got_error(GOT_ERR_PACK_OFFSET);
1169 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
1170 cfd633c2 2018-09-10 stsp if (*obj == NULL)
1171 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1173 7841c0d1 2023-02-01 op memcpy(&(*obj)->id, &iobj->id, sizeof(iobj->id));
1174 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
1175 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
1176 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
1177 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
1178 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
1179 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1180 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
1181 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
1183 d1b988e3 2022-01-26 stsp STAILQ_INIT(&(*obj)->deltas.entries);
1184 9296b03b 2022-01-26 stsp return NULL;
1187 2178c42e 2018-04-22 stsp const struct got_error *
1188 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1190 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
1191 2178c42e 2018-04-22 stsp struct imsg imsg;
1192 c4eae628 2018-04-23 stsp const size_t min_datalen =
1193 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1195 2178c42e 2018-04-22 stsp *obj = NULL;
1197 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1199 2178c42e 2018-04-22 stsp return err;
1201 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
1202 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
1203 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1206 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1210 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
1212 bff6ca00 2018-04-23 stsp return err;
1215 c75f7264 2018-09-11 stsp static const struct got_error *
1216 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1217 c75f7264 2018-09-11 stsp size_t logmsg_len)
1219 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
1220 c75f7264 2018-09-11 stsp size_t offset, remain;
1222 c75f7264 2018-09-11 stsp offset = 0;
1223 c75f7264 2018-09-11 stsp remain = logmsg_len;
1224 c75f7264 2018-09-11 stsp while (remain > 0) {
1225 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1227 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1228 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
1229 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
1230 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
1234 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
1238 c75f7264 2018-09-11 stsp offset += n;
1239 c75f7264 2018-09-11 stsp remain -= n;
1242 fa4ffeb3 2018-11-04 stsp return err;
1245 bff6ca00 2018-04-23 stsp const struct got_error *
1246 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1248 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
1249 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
1250 bff6ca00 2018-04-23 stsp uint8_t *buf;
1251 bff6ca00 2018-04-23 stsp size_t len, total;
1252 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1253 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
1254 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
1255 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
1257 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
1258 472dfe05 2023-02-01 op commit->nparents * sizeof(struct got_object_id);
1260 bff6ca00 2018-04-23 stsp buf = malloc(total);
1261 bff6ca00 2018-04-23 stsp if (buf == NULL)
1262 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1264 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
1265 472dfe05 2023-02-01 op memcpy(&icommit->tree_id, commit->tree_id, sizeof(icommit->tree_id));
1266 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
1267 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
1268 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
1269 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
1270 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
1271 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
1272 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
1273 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
1275 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
1276 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
1277 b9c33926 2018-11-07 stsp len += author_len;
1278 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
1279 b9c33926 2018-11-07 stsp len += committer_len;
1280 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1281 472dfe05 2023-02-01 op memcpy(buf + len, &qid->id, sizeof(qid->id));
1282 472dfe05 2023-02-01 op len += sizeof(qid->id);
1285 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1286 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
1287 bff6ca00 2018-04-23 stsp goto done;
1290 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
1291 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1292 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
1294 904df868 2018-11-04 stsp goto done;
1296 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
1298 bff6ca00 2018-04-23 stsp free(buf);
1299 bff6ca00 2018-04-23 stsp return err;
1302 ca6e02ac 2020-01-07 stsp static const struct got_error *
1303 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
1304 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1306 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
1307 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
1308 ca6e02ac 2020-01-07 stsp size_t len = 0;
1311 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
1312 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1314 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
1315 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
1316 ca6e02ac 2020-01-07 stsp icommit->committer_len +
1317 472dfe05 2023-02-01 op icommit->nparents * sizeof(struct got_object_id))
1318 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1320 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
1321 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1323 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
1325 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
1326 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
1327 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
1328 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
1330 472dfe05 2023-02-01 op memcpy((*commit)->tree_id, &icommit->tree_id,
1331 472dfe05 2023-02-01 op sizeof(icommit->tree_id));
1332 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
1333 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
1334 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
1335 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1337 bf31a716 2022-06-14 op (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1338 bf31a716 2022-06-14 op if ((*commit)->author == NULL) {
1339 bf31a716 2022-06-14 op err = got_error_from_errno("strndup");
1342 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
1344 bf31a716 2022-06-14 op (*commit)->committer = strndup(imsg->data + len,
1345 bf31a716 2022-06-14 op icommit->committer_len);
1346 bf31a716 2022-06-14 op if ((*commit)->committer == NULL) {
1347 bf31a716 2022-06-14 op err = got_error_from_errno("strndup");
1350 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
1352 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
1353 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
1354 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1355 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1356 ca6e02ac 2020-01-07 stsp goto done;
1359 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
1361 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1362 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1363 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1364 ca6e02ac 2020-01-07 stsp goto done;
1366 ca6e02ac 2020-01-07 stsp while (remain > 0) {
1367 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
1368 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1371 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1373 ca6e02ac 2020-01-07 stsp goto done;
1375 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1376 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1377 ca6e02ac 2020-01-07 stsp goto done;
1380 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
1381 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
1382 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
1383 ca6e02ac 2020-01-07 stsp offset += n;
1384 ca6e02ac 2020-01-07 stsp remain -= n;
1386 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
1389 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
1390 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1392 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1395 d7b5a0e8 2022-04-20 stsp memcpy(&qid->id, imsg->data + len +
1396 472dfe05 2023-02-01 op i * sizeof(qid->id), sizeof(qid->id));
1397 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1398 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
1401 ca6e02ac 2020-01-07 stsp if (err) {
1402 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
1403 ca6e02ac 2020-01-07 stsp *commit = NULL;
1405 ca6e02ac 2020-01-07 stsp return err;
1408 ca6e02ac 2020-01-07 stsp const struct got_error *
1409 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1411 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1412 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1413 ca6e02ac 2020-01-07 stsp size_t datalen;
1414 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
1415 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
1416 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
1418 ca6e02ac 2020-01-07 stsp *commit = NULL;
1420 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1422 ca6e02ac 2020-01-07 stsp return err;
1424 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1426 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1427 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1428 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1431 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1435 2178c42e 2018-04-22 stsp imsg_free(&imsg);
1437 e033d803 2018-04-23 stsp return err;
1440 33fd69c2 2022-05-19 stsp static const struct got_error *
1441 0ab4c957 2022-06-13 stsp send_tree_entries_batch(struct imsgbuf *ibuf,
1442 0ab4c957 2022-06-13 stsp struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1444 33fd69c2 2022-05-19 stsp struct ibuf *wbuf;
1445 33fd69c2 2022-05-19 stsp struct got_imsg_tree_entries ientries;
1448 031611fc 2022-06-18 op memset(&ientries, 0, sizeof(ientries));
1450 33fd69c2 2022-05-19 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1451 33fd69c2 2022-05-19 stsp if (wbuf == NULL)
1452 33fd69c2 2022-05-19 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
1454 33fd69c2 2022-05-19 stsp ientries.nentries = idxN - idx0 + 1;
1455 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1456 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add TREE_ENTRY");
1458 33fd69c2 2022-05-19 stsp for (i = idx0; i <= idxN; i++) {
1459 33fd69c2 2022-05-19 stsp struct got_parsed_tree_entry *pte = &entries[i];
1461 c4d35c5b 2023-02-02 op /* Keep in sync with struct got_imsg_tree_entry definition! */
1462 1453347d 2022-05-19 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1463 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add TREE_ENTRY");
1464 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1465 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add TREE_ENTRY");
1466 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1467 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add TREE_ENTRY");
1469 33fd69c2 2022-05-19 stsp /* Remaining bytes are the entry's name. */
1470 1453347d 2022-05-19 stsp if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1471 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add TREE_ENTRY");
1474 33fd69c2 2022-05-19 stsp wbuf->fd = -1;
1475 33fd69c2 2022-05-19 stsp imsg_close(ibuf, wbuf);
1476 33fd69c2 2022-05-19 stsp return NULL;
1479 0ab4c957 2022-06-13 stsp static const struct got_error *
1480 0ab4c957 2022-06-13 stsp send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1481 0ab4c957 2022-06-13 stsp int nentries)
1483 33fd69c2 2022-05-19 stsp const struct got_error *err = NULL;
1485 0ab4c957 2022-06-13 stsp size_t entries_len = sizeof(struct got_imsg_tree_entries);
1488 33fd69c2 2022-05-19 stsp for (j = 0; j < nentries; j++) {
1489 33fd69c2 2022-05-19 stsp struct got_parsed_tree_entry *pte = &entries[j];
1490 ff08dcd6 2023-02-13 op size_t len = sizeof(struct got_imsg_tree_entry) + pte->namelen;
1492 33fd69c2 2022-05-19 stsp if (j > 0 &&
1493 33fd69c2 2022-05-19 stsp entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1494 0ab4c957 2022-06-13 stsp err = send_tree_entries_batch(ibuf, entries,
1495 33fd69c2 2022-05-19 stsp i, j - 1, entries_len);
1497 33fd69c2 2022-05-19 stsp return err;
1499 33fd69c2 2022-05-19 stsp entries_len = sizeof(struct got_imsg_tree_entries);
1502 33fd69c2 2022-05-19 stsp entries_len += len;
1505 33fd69c2 2022-05-19 stsp if (j > 0) {
1506 0ab4c957 2022-06-13 stsp err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1507 0ab4c957 2022-06-13 stsp entries_len);
1509 33fd69c2 2022-05-19 stsp return err;
1512 0ab4c957 2022-06-13 stsp return NULL;
1515 0ab4c957 2022-06-13 stsp const struct got_error *
1516 0ab4c957 2022-06-13 stsp got_privsep_send_tree(struct imsgbuf *ibuf,
1517 0ab4c957 2022-06-13 stsp struct got_parsed_tree_entry *entries, int nentries)
1519 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1520 0ab4c957 2022-06-13 stsp struct got_imsg_tree_object itree;
1522 031611fc 2022-06-18 op memset(&itree, 0, sizeof(itree));
1523 0ab4c957 2022-06-13 stsp itree.nentries = nentries;
1524 0ab4c957 2022-06-13 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1526 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_compose TREE");
1528 0ab4c957 2022-06-13 stsp err = send_tree_entries(ibuf, entries, nentries);
1530 0ab4c957 2022-06-13 stsp return err;
1532 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
1536 0ab4c957 2022-06-13 stsp static const struct got_error *
1537 0ab4c957 2022-06-13 stsp recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1538 0ab4c957 2022-06-13 stsp int *nentries)
1540 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1541 0ab4c957 2022-06-13 stsp struct got_imsg_tree_entries *ientries;
1542 0ab4c957 2022-06-13 stsp struct got_tree_entry *te;
1543 0ab4c957 2022-06-13 stsp size_t te_offset;
1546 0ab4c957 2022-06-13 stsp if (datalen <= sizeof(*ientries) ||
1547 0ab4c957 2022-06-13 stsp datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1548 0ab4c957 2022-06-13 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1550 0ab4c957 2022-06-13 stsp ientries = (struct got_imsg_tree_entries *)data;
1551 0ab4c957 2022-06-13 stsp if (ientries->nentries > INT_MAX) {
1552 0ab4c957 2022-06-13 stsp return got_error_msg(GOT_ERR_NO_SPACE,
1553 0ab4c957 2022-06-13 stsp "too many tree entries");
1556 0ab4c957 2022-06-13 stsp te_offset = sizeof(*ientries);
1557 0ab4c957 2022-06-13 stsp for (i = 0; i < ientries->nentries; i++) {
1558 0ab4c957 2022-06-13 stsp struct got_imsg_tree_entry ite;
1559 0ab4c957 2022-06-13 stsp const char *te_name;
1560 0ab4c957 2022-06-13 stsp uint8_t *buf = (uint8_t *)data + te_offset;
1562 0ab4c957 2022-06-13 stsp if (te_offset >= datalen) {
1563 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1567 0ab4c957 2022-06-13 stsp /* Might not be aligned, size is ~32 bytes. */
1568 0ab4c957 2022-06-13 stsp memcpy(&ite, buf, sizeof(ite));
1570 0ab4c957 2022-06-13 stsp if (ite.namelen >= sizeof(te->name)) {
1571 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1574 0ab4c957 2022-06-13 stsp if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1575 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1579 0ab4c957 2022-06-13 stsp if (*nentries >= tree->nentries) {
1580 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1583 0ab4c957 2022-06-13 stsp te = &tree->entries[*nentries];
1584 0ab4c957 2022-06-13 stsp te_name = buf + sizeof(ite);
1585 0ab4c957 2022-06-13 stsp memcpy(te->name, te_name, ite.namelen);
1586 0ab4c957 2022-06-13 stsp te->name[ite.namelen] = '\0';
1587 0ab4c957 2022-06-13 stsp memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1588 0ab4c957 2022-06-13 stsp te->mode = ite.mode;
1589 0ab4c957 2022-06-13 stsp te->idx = *nentries;
1590 0ab4c957 2022-06-13 stsp (*nentries)++;
1592 0ab4c957 2022-06-13 stsp te_offset += sizeof(ite) + ite.namelen;
1595 0ab4c957 2022-06-13 stsp return err;
1598 e033d803 2018-04-23 stsp const struct got_error *
1599 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1601 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1602 e033d803 2018-04-23 stsp const size_t min_datalen =
1603 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
1604 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
1605 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
1606 e033d803 2018-04-23 stsp int nentries = 0;
1608 e033d803 2018-04-23 stsp *tree = NULL;
1610 80985c16 2022-07-07 op while (*tree == NULL || nentries < (*tree)->nentries) {
1611 e033d803 2018-04-23 stsp struct imsg imsg;
1612 e033d803 2018-04-23 stsp size_t datalen;
1614 80985c16 2022-07-07 op err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1618 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1620 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
1621 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
1622 e033d803 2018-04-23 stsp /* This message should only appear once. */
1623 e033d803 2018-04-23 stsp if (*tree != NULL) {
1624 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1627 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
1628 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1631 291624d8 2018-11-07 stsp itree = imsg.data;
1632 13fcf1bf 2022-05-18 stsp if (itree->nentries < 0) {
1633 13fcf1bf 2022-05-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1636 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
1637 e033d803 2018-04-23 stsp if (*tree == NULL) {
1638 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1641 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
1642 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
1643 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
1644 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
1645 baef4d75 2022-05-18 stsp free(*tree);
1646 baef4d75 2022-05-18 stsp *tree = NULL;
1649 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1650 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1652 33fd69c2 2022-05-19 stsp case GOT_IMSG_TREE_ENTRIES:
1653 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
1654 e033d803 2018-04-23 stsp if (*tree == NULL) {
1655 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1658 0ab4c957 2022-06-13 stsp err = recv_tree_entries(imsg.data, datalen,
1659 0ab4c957 2022-06-13 stsp *tree, &nentries);
1662 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1666 e033d803 2018-04-23 stsp imsg_free(&imsg);
1671 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
1672 1e51f5b9 2018-04-23 stsp if (err == NULL)
1673 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1674 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
1675 e033d803 2018-04-23 stsp *tree = NULL;
1678 ff6b18f8 2018-04-24 stsp return err;
1681 ff6b18f8 2018-04-24 stsp const struct got_error *
1682 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1683 ac544f8c 2019-01-13 stsp const uint8_t *data)
1685 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1687 031611fc 2022-06-18 op memset(&iblob, 0, sizeof(iblob));
1688 2967a784 2018-04-24 stsp iblob.size = size;
1689 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
1691 ac544f8c 2019-01-13 stsp if (data) {
1692 ac544f8c 2019-01-13 stsp uint8_t *buf;
1694 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1695 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
1697 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
1698 ac544f8c 2019-01-13 stsp if (buf == NULL)
1699 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1701 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
1702 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
1703 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1704 62d463ca 2020-10-20 naddy sizeof(iblob) + size) == -1) {
1705 ac544f8c 2019-01-13 stsp free(buf);
1706 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1708 ac544f8c 2019-01-13 stsp free(buf);
1710 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1711 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1712 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1713 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1717 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1720 ff6b18f8 2018-04-24 stsp const struct got_error *
1721 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1722 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1724 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1725 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1726 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1727 ff6b18f8 2018-04-24 stsp size_t datalen;
1729 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1731 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1733 ff6b18f8 2018-04-24 stsp return err;
1735 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1737 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1738 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1739 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1740 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1743 291624d8 2018-11-07 stsp iblob = imsg.data;
1744 291624d8 2018-11-07 stsp *size = iblob->size;
1745 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1747 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1748 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1752 c98b0f0b 2022-06-14 op if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1753 c98b0f0b 2022-06-14 op *size > datalen + sizeof(*iblob)) {
1754 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1758 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1759 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1760 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1763 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1766 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1770 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1772 f4a881ce 2018-11-17 stsp return err;
1775 f4a881ce 2018-11-17 stsp static const struct got_error *
1776 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1778 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1779 f4a881ce 2018-11-17 stsp size_t offset, remain;
1781 f4a881ce 2018-11-17 stsp offset = 0;
1782 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1783 f4a881ce 2018-11-17 stsp while (remain > 0) {
1784 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1786 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1787 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1788 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1792 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1796 f4a881ce 2018-11-17 stsp offset += n;
1797 f4a881ce 2018-11-17 stsp remain -= n;
1800 f4a881ce 2018-11-17 stsp return err;
1803 f4a881ce 2018-11-17 stsp const struct got_error *
1804 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1806 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1807 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1808 f4a881ce 2018-11-17 stsp uint8_t *buf;
1809 f4a881ce 2018-11-17 stsp size_t len, total;
1810 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1811 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1812 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1814 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1816 f4a881ce 2018-11-17 stsp buf = malloc(total);
1817 f4a881ce 2018-11-17 stsp if (buf == NULL)
1818 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1820 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1821 076fbedc 2023-02-19 op memcpy(&itag->id, &tag->id, sizeof(itag->id));
1822 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1823 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1824 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1825 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1826 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1827 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1829 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1830 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1831 f4a881ce 2018-11-17 stsp len += tag_len;
1832 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1833 f4a881ce 2018-11-17 stsp len += tagger_len;
1835 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1836 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1837 f4a881ce 2018-11-17 stsp goto done;
1840 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1841 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1842 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1844 f4a881ce 2018-11-17 stsp goto done;
1846 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1848 f4a881ce 2018-11-17 stsp free(buf);
1849 f4a881ce 2018-11-17 stsp return err;
1852 f4a881ce 2018-11-17 stsp const struct got_error *
1853 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1855 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1856 f4a881ce 2018-11-17 stsp struct imsg imsg;
1857 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1858 f4a881ce 2018-11-17 stsp size_t len, datalen;
1859 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1860 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1861 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1863 f4a881ce 2018-11-17 stsp *tag = NULL;
1865 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1867 f4a881ce 2018-11-17 stsp return err;
1869 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1872 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1873 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1874 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1875 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1878 f4a881ce 2018-11-17 stsp itag = imsg.data;
1879 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1880 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1881 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1884 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1886 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1887 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1888 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1892 076fbedc 2023-02-19 op memcpy(&(*tag)->id, &itag->id, sizeof(itag->id));
1894 bf31a716 2022-06-14 op (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1895 bf31a716 2022-06-14 op if ((*tag)->tag == NULL) {
1896 bf31a716 2022-06-14 op err = got_error_from_errno("strndup");
1899 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1901 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1902 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1903 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1905 bf31a716 2022-06-14 op (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1906 bf31a716 2022-06-14 op if ((*tag)->tagger == NULL) {
1907 bf31a716 2022-06-14 op err = got_error_from_errno("strndup");
1910 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1912 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1913 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1914 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1915 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1919 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1921 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1922 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1923 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1926 f4a881ce 2018-11-17 stsp while (remain > 0) {
1927 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1928 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1931 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1933 f4a881ce 2018-11-17 stsp return err;
1935 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1936 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1938 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1940 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1941 f4a881ce 2018-11-17 stsp offset += n;
1942 f4a881ce 2018-11-17 stsp remain -= n;
1944 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1949 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1953 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1955 2178c42e 2018-04-22 stsp return err;
1958 876c234b 2018-09-10 stsp const struct got_error *
1959 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1960 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1962 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1963 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1964 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1967 031611fc 2022-06-18 op memset(&ipackidx, 0, sizeof(ipackidx));
1968 031611fc 2022-06-18 op memset(&ipack, 0, sizeof(ipack));
1970 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1971 c3564dfa 2021-07-15 stsp ipackidx.packfile_size = pack->filesize;
1972 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1973 876c234b 2018-09-10 stsp if (fd == -1)
1974 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1976 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1977 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1978 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1979 41496140 2019-02-21 stsp close(fd);
1980 41496140 2019-02-21 stsp return err;
1983 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1984 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1985 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1986 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1988 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1989 876c234b 2018-09-10 stsp if (fd == -1)
1990 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1992 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1994 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1995 41496140 2019-02-21 stsp close(fd);
1996 41496140 2019-02-21 stsp return err;
1999 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
2002 876c234b 2018-09-10 stsp const struct got_error *
2003 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2004 106807b4 2018-09-15 stsp struct got_object_id *id)
2006 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
2008 031611fc 2022-06-18 op memset(&iobj, 0, sizeof(iobj));
2009 876c234b 2018-09-10 stsp iobj.idx = idx;
2010 265df21f 2023-01-31 op memcpy(&iobj.id, id, sizeof(iobj.id));
2012 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2013 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
2014 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
2015 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
2017 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2020 aba9c984 2019-09-08 stsp const struct got_error *
2021 59d1e4a0 2021-03-10 stsp got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2022 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
2024 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
2026 031611fc 2022-06-18 op memset(&iobj, 0, sizeof(iobj));
2027 59d1e4a0 2021-03-10 stsp iobj.idx = idx;
2028 265df21f 2023-01-31 op memcpy(&iobj.id, id, sizeof(iobj.id));
2030 59d1e4a0 2021-03-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2031 59d1e4a0 2021-03-10 stsp &iobj, sizeof(iobj)) == -1)
2032 59d1e4a0 2021-03-10 stsp return got_error_from_errno("imsg_compose "
2033 59d1e4a0 2021-03-10 stsp "PACKED_OBJECT_REQUEST");
2035 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
2038 59d1e4a0 2021-03-10 stsp const struct got_error *
2039 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2041 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
2043 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2044 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
2045 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
2046 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
2047 aba9c984 2019-09-08 stsp close(fd);
2048 aba9c984 2019-09-08 stsp return err;
2051 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2054 aba9c984 2019-09-08 stsp const struct got_error *
2055 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2057 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
2058 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2059 aba9c984 2019-09-08 stsp NULL, 0) == -1)
2060 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
2061 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2063 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2066 aba9c984 2019-09-08 stsp const struct got_error *
2067 20b7abb3 2020-10-22 stsp got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2069 20b7abb3 2020-10-22 stsp if (imsg_compose(ibuf,
2070 20b7abb3 2020-10-22 stsp GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2071 20b7abb3 2020-10-22 stsp NULL, 0) == -1)
2072 20b7abb3 2020-10-22 stsp return got_error_from_errno("imsg_compose "
2073 20b7abb3 2020-10-22 stsp "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2075 20b7abb3 2020-10-22 stsp return flush_imsg(ibuf);
2079 20b7abb3 2020-10-22 stsp const struct got_error *
2080 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2082 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
2083 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2084 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
2085 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
2087 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2090 aba9c984 2019-09-08 stsp const struct got_error *
2091 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2093 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
2094 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2095 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
2096 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2098 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
2101 cd95becd 2019-11-29 stsp const struct got_error *
2102 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2104 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
2105 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2106 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
2107 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
2109 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2112 aba9c984 2019-09-08 stsp const struct got_error *
2113 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2115 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
2116 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2117 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
2118 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
2120 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
2123 9a1cc63f 2020-02-03 stsp const struct got_error *
2124 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2126 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
2127 aba9c984 2019-09-08 stsp struct imsg imsg;
2128 aba9c984 2019-09-08 stsp size_t datalen;
2130 aba9c984 2019-09-08 stsp *str = NULL;
2132 3e6054c4 2022-06-14 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2134 aba9c984 2019-09-08 stsp return err;
2135 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2137 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
2138 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
2139 aba9c984 2019-09-08 stsp if (datalen == 0)
2141 00b3e9ae 2023-01-11 op *str = strndup(imsg.data, datalen);
2142 aba9c984 2019-09-08 stsp if (*str == NULL) {
2143 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
2148 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2152 27749ea2 2023-02-05 op imsg_free(&imsg);
2156 27749ea2 2023-02-05 op const struct got_error *
2157 27749ea2 2023-02-05 op got_privsep_recv_gitconfig_pair(char **key, char **val, struct imsgbuf *ibuf)
2159 27749ea2 2023-02-05 op const struct got_error *err = NULL;
2160 27749ea2 2023-02-05 op struct got_imsg_gitconfig_pair p;
2161 27749ea2 2023-02-05 op struct imsg imsg;
2162 27749ea2 2023-02-05 op size_t datalen;
2163 27749ea2 2023-02-05 op uint8_t *data;
2165 27749ea2 2023-02-05 op *key = *val = NULL;
2167 27749ea2 2023-02-05 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2171 27749ea2 2023-02-05 op data = imsg.data;
2172 27749ea2 2023-02-05 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2174 27749ea2 2023-02-05 op if (imsg.hdr.type != GOT_IMSG_GITCONFIG_PAIR) {
2175 27749ea2 2023-02-05 op err = got_error(GOT_ERR_PRIVSEP_MSG);
2179 27749ea2 2023-02-05 op if (datalen < sizeof(p)) {
2180 27749ea2 2023-02-05 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2184 27749ea2 2023-02-05 op memcpy(&p, data, sizeof(p));
2185 27749ea2 2023-02-05 op data += sizeof(p);
2187 27749ea2 2023-02-05 op if (datalen != sizeof(p) + p.klen + p.vlen) {
2188 27749ea2 2023-02-05 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2192 27749ea2 2023-02-05 op *key = strndup(data, p.klen);
2193 27749ea2 2023-02-05 op if (*key == NULL) {
2194 27749ea2 2023-02-05 op err = got_error_from_errno("strndup");
2197 27749ea2 2023-02-05 op data += p.klen;
2199 27749ea2 2023-02-05 op *val = strndup(data, p.vlen);
2200 27749ea2 2023-02-05 op if (*val == NULL) {
2201 27749ea2 2023-02-05 op err = got_error_from_errno("strndup");
2206 aba9c984 2019-09-08 stsp imsg_free(&imsg);
2207 aba9c984 2019-09-08 stsp return err;
2210 aba9c984 2019-09-08 stsp const struct got_error *
2211 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2213 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
2214 aba9c984 2019-09-08 stsp struct imsg imsg;
2215 aba9c984 2019-09-08 stsp size_t datalen;
2216 aba9c984 2019-09-08 stsp const size_t min_datalen =
2217 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
2221 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2223 aba9c984 2019-09-08 stsp return err;
2224 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2226 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
2227 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
2228 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
2229 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2232 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
2235 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2239 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2240 cd95becd 2019-11-29 stsp return err;
2243 b8adfa55 2020-09-25 stsp static void
2244 b8adfa55 2020-09-25 stsp free_remote_data(struct got_remote_repo *remote)
2248 b8adfa55 2020-09-25 stsp free(remote->name);
2249 6480c871 2021-08-30 stsp free(remote->fetch_url);
2250 6480c871 2021-08-30 stsp free(remote->send_url);
2251 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++)
2252 6480c871 2021-08-30 stsp free(remote->fetch_branches[i]);
2253 6480c871 2021-08-30 stsp free(remote->fetch_branches);
2254 6480c871 2021-08-30 stsp for (i = 0; i < remote->nsend_branches; i++)
2255 6480c871 2021-08-30 stsp free(remote->send_branches[i]);
2256 6480c871 2021-08-30 stsp free(remote->send_branches);
2257 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++)
2258 6480c871 2021-08-30 stsp free(remote->fetch_refs[i]);
2259 6480c871 2021-08-30 stsp free(remote->fetch_refs);
2262 cd95becd 2019-11-29 stsp const struct got_error *
2263 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2264 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
2266 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
2267 cd95becd 2019-11-29 stsp struct imsg imsg;
2268 cd95becd 2019-11-29 stsp size_t datalen;
2269 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
2270 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
2272 cd95becd 2019-11-29 stsp *remotes = NULL;
2273 cd95becd 2019-11-29 stsp *nremotes = 0;
2274 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
2276 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2278 cd95becd 2019-11-29 stsp return err;
2279 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2281 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
2282 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
2283 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
2284 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2287 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
2288 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
2289 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2290 cd95becd 2019-11-29 stsp return NULL;
2294 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
2295 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2298 aba9c984 2019-09-08 stsp imsg_free(&imsg);
2300 5146eb39 2020-03-20 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2301 cd95becd 2019-11-29 stsp if (*remotes == NULL)
2302 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
2304 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
2305 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
2307 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2310 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2312 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
2313 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
2314 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
2315 b8adfa55 2020-09-25 stsp memset(remote, 0, sizeof(*remote));
2316 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
2317 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2320 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
2321 6480c871 2021-08-30 stsp if (iremote.name_len == 0 ||
2322 6480c871 2021-08-30 stsp iremote.fetch_url_len == 0 ||
2323 6480c871 2021-08-30 stsp iremote.send_url_len == 0 ||
2324 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
2325 6480c871 2021-08-30 stsp iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2326 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2329 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
2330 cd95becd 2019-11-29 stsp iremote.name_len);
2331 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
2332 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
2335 6480c871 2021-08-30 stsp remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2336 6480c871 2021-08-30 stsp iremote.name_len, iremote.fetch_url_len);
2337 6480c871 2021-08-30 stsp if (remote->fetch_url == NULL) {
2338 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
2339 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2342 6480c871 2021-08-30 stsp remote->send_url = strndup(imsg.data + sizeof(iremote) +
2343 6480c871 2021-08-30 stsp iremote.name_len + iremote.fetch_url_len,
2344 6480c871 2021-08-30 stsp iremote.send_url_len);
2345 6480c871 2021-08-30 stsp if (remote->send_url == NULL) {
2346 6480c871 2021-08-30 stsp err = got_error_from_errno("strndup");
2347 6480c871 2021-08-30 stsp free_remote_data(remote);
2350 469dd726 2020-03-20 stsp remote->mirror_references = iremote.mirror_references;
2351 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = iremote.fetch_all_branches;
2352 6480c871 2021-08-30 stsp remote->nfetch_branches = 0;
2353 6480c871 2021-08-30 stsp remote->fetch_branches = NULL;
2354 6480c871 2021-08-30 stsp remote->nsend_branches = 0;
2355 6480c871 2021-08-30 stsp remote->send_branches = NULL;
2356 6480c871 2021-08-30 stsp remote->nfetch_refs = 0;
2357 6480c871 2021-08-30 stsp remote->fetch_refs = NULL;
2358 cd95becd 2019-11-29 stsp (*nremotes)++;
2361 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2365 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2370 cd95becd 2019-11-29 stsp if (err) {
2372 b8adfa55 2020-09-25 stsp for (i = 0; i < *nremotes; i++)
2373 b8adfa55 2020-09-25 stsp free_remote_data(&(*remotes)[i]);
2374 cd95becd 2019-11-29 stsp free(*remotes);
2375 cd95becd 2019-11-29 stsp *remotes = NULL;
2376 cd95becd 2019-11-29 stsp *nremotes = 0;
2378 aba9c984 2019-09-08 stsp return err;
2381 257add31 2020-09-09 stsp const struct got_error *
2382 257add31 2020-09-09 stsp got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2384 257add31 2020-09-09 stsp const struct got_error *err = NULL;
2386 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2387 257add31 2020-09-09 stsp NULL, 0) == -1) {
2388 257add31 2020-09-09 stsp err = got_error_from_errno("imsg_compose "
2389 257add31 2020-09-09 stsp "GOTCONFIG_PARSE_REQUEST");
2390 257add31 2020-09-09 stsp close(fd);
2391 257add31 2020-09-09 stsp return err;
2394 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2397 257add31 2020-09-09 stsp const struct got_error *
2398 257add31 2020-09-09 stsp got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2400 257add31 2020-09-09 stsp if (imsg_compose(ibuf,
2401 257add31 2020-09-09 stsp GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2402 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose "
2403 257add31 2020-09-09 stsp "GOTCONFIG_AUTHOR_REQUEST");
2405 4d5ee956 2022-07-02 jrick return flush_imsg(ibuf);
2408 4d5ee956 2022-07-02 jrick const struct got_error *
2409 4d5ee956 2022-07-02 jrick got_privsep_send_gotconfig_allowed_signers_req(struct imsgbuf *ibuf)
2411 4d5ee956 2022-07-02 jrick if (imsg_compose(ibuf,
2412 4d5ee956 2022-07-02 jrick GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2413 4d5ee956 2022-07-02 jrick return got_error_from_errno("imsg_compose "
2414 4d5ee956 2022-07-02 jrick "GOTCONFIG_ALLOWEDSIGNERS_REQUEST");
2416 4d5ee956 2022-07-02 jrick return flush_imsg(ibuf);
2419 4d5ee956 2022-07-02 jrick const struct got_error *
2420 4d5ee956 2022-07-02 jrick got_privsep_send_gotconfig_revoked_signers_req(struct imsgbuf *ibuf)
2422 4d5ee956 2022-07-02 jrick if (imsg_compose(ibuf,
2423 4d5ee956 2022-07-02 jrick GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2424 4d5ee956 2022-07-02 jrick return got_error_from_errno("imsg_compose "
2425 4d5ee956 2022-07-02 jrick "GOTCONFIG_REVOKEDSIGNERS_REQUEST");
2427 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2430 ca6e02ac 2020-01-07 stsp const struct got_error *
2431 d68f2c0e 2022-07-05 jrick got_privsep_send_gotconfig_signer_id_req(struct imsgbuf *ibuf)
2433 d68f2c0e 2022-07-05 jrick if (imsg_compose(ibuf,
2434 d68f2c0e 2022-07-05 jrick GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST, 0, 0, -1, NULL, 0) == -1)
2435 d68f2c0e 2022-07-05 jrick return got_error_from_errno("imsg_compose "
2436 d68f2c0e 2022-07-05 jrick "GOTCONFIG_SIGNERID_REQUEST");
2438 d68f2c0e 2022-07-05 jrick return flush_imsg(ibuf);
2441 d68f2c0e 2022-07-05 jrick const struct got_error *
2442 257add31 2020-09-09 stsp got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2444 257add31 2020-09-09 stsp if (imsg_compose(ibuf,
2445 257add31 2020-09-09 stsp GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2446 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose "
2447 257add31 2020-09-09 stsp "GOTCONFIG_REMOTE_REQUEST");
2449 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2452 257add31 2020-09-09 stsp const struct got_error *
2453 257add31 2020-09-09 stsp got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2455 257add31 2020-09-09 stsp const struct got_error *err = NULL;
2456 257add31 2020-09-09 stsp struct imsg imsg;
2457 257add31 2020-09-09 stsp size_t datalen;
2459 257add31 2020-09-09 stsp *str = NULL;
2461 3e6054c4 2022-06-14 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2463 257add31 2020-09-09 stsp return err;
2464 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2466 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
2467 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_STR_VAL:
2468 257add31 2020-09-09 stsp if (datalen == 0)
2470 00b3e9ae 2023-01-11 op *str = strndup(imsg.data, datalen);
2471 257add31 2020-09-09 stsp if (*str == NULL) {
2472 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
2477 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2481 257add31 2020-09-09 stsp imsg_free(&imsg);
2482 257add31 2020-09-09 stsp return err;
2485 257add31 2020-09-09 stsp const struct got_error *
2486 257add31 2020-09-09 stsp got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2487 257add31 2020-09-09 stsp int *nremotes, struct imsgbuf *ibuf)
2489 257add31 2020-09-09 stsp const struct got_error *err = NULL;
2490 257add31 2020-09-09 stsp struct imsg imsg;
2491 257add31 2020-09-09 stsp size_t datalen;
2492 257add31 2020-09-09 stsp struct got_imsg_remotes iremotes;
2493 257add31 2020-09-09 stsp struct got_imsg_remote iremote;
2494 257add31 2020-09-09 stsp const size_t min_datalen =
2495 257add31 2020-09-09 stsp MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2497 257add31 2020-09-09 stsp *remotes = NULL;
2498 257add31 2020-09-09 stsp *nremotes = 0;
2499 257add31 2020-09-09 stsp iremotes.nremotes = 0;
2501 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2503 257add31 2020-09-09 stsp return err;
2504 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2506 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
2507 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTES:
2508 257add31 2020-09-09 stsp if (datalen != sizeof(iremotes)) {
2509 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2512 257add31 2020-09-09 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
2513 c98b0f0b 2022-06-14 op if (iremotes.nremotes < 0) {
2514 c98b0f0b 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2517 257add31 2020-09-09 stsp if (iremotes.nremotes == 0) {
2518 257add31 2020-09-09 stsp imsg_free(&imsg);
2519 257add31 2020-09-09 stsp return NULL;
2523 257add31 2020-09-09 stsp imsg_free(&imsg);
2524 257add31 2020-09-09 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2527 257add31 2020-09-09 stsp imsg_free(&imsg);
2529 257add31 2020-09-09 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2530 257add31 2020-09-09 stsp if (*remotes == NULL)
2531 257add31 2020-09-09 stsp return got_error_from_errno("recallocarray");
2533 257add31 2020-09-09 stsp while (*nremotes < iremotes.nremotes) {
2534 257add31 2020-09-09 stsp struct got_remote_repo *remote;
2535 257add31 2020-09-09 stsp const size_t min_datalen =
2536 257add31 2020-09-09 stsp MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2539 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2542 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2544 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
2545 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTE:
2546 257add31 2020-09-09 stsp remote = &(*remotes)[*nremotes];
2547 b8adfa55 2020-09-25 stsp memset(remote, 0, sizeof(*remote));
2548 257add31 2020-09-09 stsp if (datalen < sizeof(iremote)) {
2549 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2552 257add31 2020-09-09 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
2553 6480c871 2021-08-30 stsp if (iremote.name_len == 0 ||
2554 6480c871 2021-08-30 stsp (iremote.fetch_url_len == 0 &&
2555 6480c871 2021-08-30 stsp iremote.send_url_len == 0) ||
2556 257add31 2020-09-09 stsp (sizeof(iremote) + iremote.name_len +
2557 6480c871 2021-08-30 stsp iremote.fetch_url_len + iremote.send_url_len) >
2558 6480c871 2021-08-30 stsp datalen) {
2559 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2562 257add31 2020-09-09 stsp remote->name = strndup(imsg.data + sizeof(iremote),
2563 257add31 2020-09-09 stsp iremote.name_len);
2564 257add31 2020-09-09 stsp if (remote->name == NULL) {
2565 257add31 2020-09-09 stsp err = got_error_from_errno("strndup");
2568 6480c871 2021-08-30 stsp remote->fetch_url = strndup(imsg.data +
2569 6480c871 2021-08-30 stsp sizeof(iremote) + iremote.name_len,
2570 6480c871 2021-08-30 stsp iremote.fetch_url_len);
2571 6480c871 2021-08-30 stsp if (remote->fetch_url == NULL) {
2572 6480c871 2021-08-30 stsp err = got_error_from_errno("strndup");
2573 6480c871 2021-08-30 stsp free_remote_data(remote);
2576 6480c871 2021-08-30 stsp remote->send_url = strndup(imsg.data +
2577 6480c871 2021-08-30 stsp sizeof(iremote) + iremote.name_len +
2578 6480c871 2021-08-30 stsp iremote.fetch_url_len, iremote.send_url_len);
2579 6480c871 2021-08-30 stsp if (remote->send_url == NULL) {
2580 257add31 2020-09-09 stsp err = got_error_from_errno("strndup");
2581 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2584 257add31 2020-09-09 stsp remote->mirror_references = iremote.mirror_references;
2585 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = iremote.fetch_all_branches;
2586 6480c871 2021-08-30 stsp if (iremote.nfetch_branches > 0) {
2587 6480c871 2021-08-30 stsp remote->fetch_branches = recallocarray(NULL, 0,
2588 6480c871 2021-08-30 stsp iremote.nfetch_branches, sizeof(char *));
2589 6480c871 2021-08-30 stsp if (remote->fetch_branches == NULL) {
2590 b8adfa55 2020-09-25 stsp err = got_error_from_errno("calloc");
2591 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2595 6480c871 2021-08-30 stsp remote->nfetch_branches = 0;
2596 6480c871 2021-08-30 stsp for (i = 0; i < iremote.nfetch_branches; i++) {
2597 b8adfa55 2020-09-25 stsp char *branch;
2598 b8adfa55 2020-09-25 stsp err = got_privsep_recv_gotconfig_str(&branch,
2600 b8adfa55 2020-09-25 stsp if (err) {
2601 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2602 b8adfa55 2020-09-25 stsp goto done;
2604 6480c871 2021-08-30 stsp remote->fetch_branches[i] = branch;
2605 6480c871 2021-08-30 stsp remote->nfetch_branches++;
2607 6480c871 2021-08-30 stsp if (iremote.nsend_branches > 0) {
2608 6480c871 2021-08-30 stsp remote->send_branches = recallocarray(NULL, 0,
2609 6480c871 2021-08-30 stsp iremote.nsend_branches, sizeof(char *));
2610 6480c871 2021-08-30 stsp if (remote->send_branches == NULL) {
2611 99495ddb 2021-01-10 stsp err = got_error_from_errno("calloc");
2612 99495ddb 2021-01-10 stsp free_remote_data(remote);
2616 6480c871 2021-08-30 stsp remote->nsend_branches = 0;
2617 6480c871 2021-08-30 stsp for (i = 0; i < iremote.nsend_branches; i++) {
2618 6480c871 2021-08-30 stsp char *branch;
2619 6480c871 2021-08-30 stsp err = got_privsep_recv_gotconfig_str(&branch,
2621 6480c871 2021-08-30 stsp if (err) {
2622 6480c871 2021-08-30 stsp free_remote_data(remote);
2623 6480c871 2021-08-30 stsp goto done;
2625 6480c871 2021-08-30 stsp remote->send_branches[i] = branch;
2626 6480c871 2021-08-30 stsp remote->nsend_branches++;
2628 6480c871 2021-08-30 stsp if (iremote.nfetch_refs > 0) {
2629 6480c871 2021-08-30 stsp remote->fetch_refs = recallocarray(NULL, 0,
2630 6480c871 2021-08-30 stsp iremote.nfetch_refs, sizeof(char *));
2631 6480c871 2021-08-30 stsp if (remote->fetch_refs == NULL) {
2632 6480c871 2021-08-30 stsp err = got_error_from_errno("calloc");
2633 6480c871 2021-08-30 stsp free_remote_data(remote);
2637 6480c871 2021-08-30 stsp remote->nfetch_refs = 0;
2638 6480c871 2021-08-30 stsp for (i = 0; i < iremote.nfetch_refs; i++) {
2639 99495ddb 2021-01-10 stsp char *ref;
2640 99495ddb 2021-01-10 stsp err = got_privsep_recv_gotconfig_str(&ref,
2642 99495ddb 2021-01-10 stsp if (err) {
2643 99495ddb 2021-01-10 stsp free_remote_data(remote);
2644 99495ddb 2021-01-10 stsp goto done;
2646 6480c871 2021-08-30 stsp remote->fetch_refs[i] = ref;
2647 6480c871 2021-08-30 stsp remote->nfetch_refs++;
2649 257add31 2020-09-09 stsp (*nremotes)++;
2652 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2656 257add31 2020-09-09 stsp imsg_free(&imsg);
2661 257add31 2020-09-09 stsp if (err) {
2663 b8adfa55 2020-09-25 stsp for (i = 0; i < *nremotes; i++)
2664 b8adfa55 2020-09-25 stsp free_remote_data(&(*remotes)[i]);
2665 257add31 2020-09-09 stsp free(*remotes);
2666 257add31 2020-09-09 stsp *remotes = NULL;
2667 257add31 2020-09-09 stsp *nremotes = 0;
2669 257add31 2020-09-09 stsp return err;
2672 257add31 2020-09-09 stsp const struct got_error *
2673 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2674 abc59930 2021-09-05 naddy struct got_object_id *id, int idx, const char *path)
2676 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2677 47cb6f8b 2023-02-26 op size_t path_len = strlen(path);
2679 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2680 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
2681 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
2682 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
2683 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
2685 47cb6f8b 2023-02-26 op * Keep in sync with struct got_imsg_commit_traversal_request
2686 47cb6f8b 2023-02-26 op * and struct got_imsg_packed_object.
2688 47cb6f8b 2023-02-26 op if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2689 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add "
2690 1453347d 2022-05-19 stsp "COMMIT_TRAVERSAL_REQUEST");
2691 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2692 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add "
2693 1453347d 2022-05-19 stsp "COMMIT_TRAVERSAL_REQUEST");
2694 47cb6f8b 2023-02-26 op if (imsg_add(wbuf, &path_len, sizeof(path_len)) == -1)
2695 47cb6f8b 2023-02-26 op return got_error_from_errno("imsg_add "
2696 47cb6f8b 2023-02-26 op "COMMIT_TRAVERSAL_REQUEST");
2697 1453347d 2022-05-19 stsp if (imsg_add(wbuf, path, path_len) == -1)
2698 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add "
2699 1453347d 2022-05-19 stsp "COMMIT_TRAVERSAL_REQUEST");
2701 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2702 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2704 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2707 ca6e02ac 2020-01-07 stsp const struct got_error *
2708 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2709 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
2710 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2712 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2713 ca6e02ac 2020-01-07 stsp struct imsg imsg;
2714 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
2715 076fbedc 2023-02-19 op struct got_object_id *ids;
2716 ca6e02ac 2020-01-07 stsp size_t datalen;
2717 ca6e02ac 2020-01-07 stsp int i, done = 0;
2719 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
2720 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
2722 ca6e02ac 2020-01-07 stsp while (!done) {
2723 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2725 ca6e02ac 2020-01-07 stsp return err;
2727 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2728 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
2729 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
2730 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
2731 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
2732 076fbedc 2023-02-19 op icommits->ncommits * sizeof(*ids)) {
2733 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2736 076fbedc 2023-02-19 op ids = imsg.data + sizeof(*icommits);
2737 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
2738 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
2740 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
2743 076fbedc 2023-02-19 op memcpy(&qid->id, &ids[i], sizeof(ids[i]));
2744 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2746 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
2747 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
2748 ca6e02ac 2020-01-07 stsp *changed_commit_id =
2749 d7b5a0e8 2022-04-20 stsp got_object_id_dup(&qid->id);
2750 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2751 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2752 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
2758 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
2759 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2760 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2763 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
2764 ca6e02ac 2020-01-07 stsp datalen, ibuf);
2766 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2770 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2774 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
2780 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
2781 ca6e02ac 2020-01-07 stsp return err;
2784 0ab4c957 2022-06-13 stsp const struct got_error *
2785 0ab4c957 2022-06-13 stsp got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2786 0ab4c957 2022-06-13 stsp struct got_object_id *tree_id, const char *path,
2787 0ab4c957 2022-06-13 stsp struct got_parsed_tree_entry *entries, int nentries)
2789 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
2790 0ab4c957 2022-06-13 stsp struct ibuf *wbuf;
2791 0ab4c957 2022-06-13 stsp size_t path_len = strlen(path);
2792 0ab4c957 2022-06-13 stsp size_t msglen;
2794 0ab4c957 2022-06-13 stsp msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2795 0ab4c957 2022-06-13 stsp wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2796 0ab4c957 2022-06-13 stsp if (wbuf == NULL)
2797 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_create ENUMERATED_TREE");
2799 0ab4c957 2022-06-13 stsp if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2800 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_add ENUMERATED_TREE");
2801 0ab4c957 2022-06-13 stsp if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2802 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_add ENUMERATED_TREE");
2803 0ab4c957 2022-06-13 stsp if (imsg_add(wbuf, path, path_len) == -1)
2804 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_add ENUMERATED_TREE");
2806 0ab4c957 2022-06-13 stsp wbuf->fd = -1;
2807 0ab4c957 2022-06-13 stsp imsg_close(ibuf, wbuf);
2809 0ab4c957 2022-06-13 stsp if (entries) {
2810 0ab4c957 2022-06-13 stsp err = send_tree_entries(ibuf, entries, nentries);
2812 0ab4c957 2022-06-13 stsp return err;
2815 0ab4c957 2022-06-13 stsp return flush_imsg(ibuf);
2818 67fd6849 2022-02-13 stsp const struct got_error *
2819 0ab4c957 2022-06-13 stsp got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2821 0ab4c957 2022-06-13 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2822 0ab4c957 2022-06-13 stsp 0, 0, -1, NULL, 0) == -1)
2823 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_compose "
2824 0ab4c957 2022-06-13 stsp "OBJECT_ENUMERATION_REQUEST");
2826 0ab4c957 2022-06-13 stsp return flush_imsg(ibuf);
2829 0ab4c957 2022-06-13 stsp const struct got_error *
2830 0ab4c957 2022-06-13 stsp got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2832 0ab4c957 2022-06-13 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2833 0ab4c957 2022-06-13 stsp 0, 0, -1, NULL, 0) == -1)
2834 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_compose "
2835 0ab4c957 2022-06-13 stsp "OBJECT_ENUMERATION_DONE");
2837 0ab4c957 2022-06-13 stsp return flush_imsg(ibuf);
2840 0ab4c957 2022-06-13 stsp const struct got_error *
2841 db9b9b1c 2022-06-14 stsp got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2843 db9b9b1c 2022-06-14 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2844 db9b9b1c 2022-06-14 stsp 0, 0, -1, NULL, 0) == -1)
2845 db9b9b1c 2022-06-14 stsp return got_error_from_errno("imsg_compose "
2846 db9b9b1c 2022-06-14 stsp "OBJECT_ENUMERATION_INCOMPLETE");
2848 db9b9b1c 2022-06-14 stsp return flush_imsg(ibuf);
2851 db9b9b1c 2022-06-14 stsp const struct got_error *
2852 0ab4c957 2022-06-13 stsp got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2853 0ab4c957 2022-06-13 stsp struct got_object_id *id, time_t mtime)
2855 0ab4c957 2022-06-13 stsp struct ibuf *wbuf;
2857 0ab4c957 2022-06-13 stsp wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2858 0ab4c957 2022-06-13 stsp sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2859 0ab4c957 2022-06-13 stsp if (wbuf == NULL)
2860 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2862 0ab4c957 2022-06-13 stsp /* Keep in sync with struct got_imsg_enumerated_commit! */
2863 0ab4c957 2022-06-13 stsp if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2864 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2865 0ab4c957 2022-06-13 stsp if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2866 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2868 0ab4c957 2022-06-13 stsp wbuf->fd = -1;
2869 0ab4c957 2022-06-13 stsp imsg_close(ibuf, wbuf);
2870 0ab4c957 2022-06-13 stsp /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2871 0ab4c957 2022-06-13 stsp return NULL;
2874 0ab4c957 2022-06-13 stsp const struct got_error *
2875 db9b9b1c 2022-06-14 stsp got_privsep_recv_enumerated_objects(int *found_all_objects,
2876 db9b9b1c 2022-06-14 stsp struct imsgbuf *ibuf,
2877 0ab4c957 2022-06-13 stsp got_object_enumerate_commit_cb cb_commit,
2878 0ab4c957 2022-06-13 stsp got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2879 0ab4c957 2022-06-13 stsp struct got_repository *repo)
2881 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
2882 0ab4c957 2022-06-13 stsp struct imsg imsg;
2883 0ab4c957 2022-06-13 stsp struct got_imsg_enumerated_commit *icommit = NULL;
2884 0ab4c957 2022-06-13 stsp struct got_object_id commit_id;
2885 0ab4c957 2022-06-13 stsp int have_commit = 0;
2886 0ab4c957 2022-06-13 stsp time_t mtime = 0;
2887 0ab4c957 2022-06-13 stsp struct got_tree_object tree;
2888 0ab4c957 2022-06-13 stsp struct got_imsg_enumerated_tree *itree;
2889 0ab4c957 2022-06-13 stsp struct got_object_id tree_id;
2890 0ab4c957 2022-06-13 stsp char *path = NULL, *canon_path = NULL;
2891 0ab4c957 2022-06-13 stsp size_t datalen, path_len;
2892 0ab4c957 2022-06-13 stsp int nentries = -1;
2893 0ab4c957 2022-06-13 stsp int done = 0;
2895 db9b9b1c 2022-06-14 stsp *found_all_objects = 0;
2896 0ab4c957 2022-06-13 stsp memset(&tree, 0, sizeof(tree));
2898 0ab4c957 2022-06-13 stsp while (!done) {
2899 0ab4c957 2022-06-13 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2903 0ab4c957 2022-06-13 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2904 0ab4c957 2022-06-13 stsp switch (imsg.hdr.type) {
2905 b4b1b9c0 2022-06-14 op case GOT_IMSG_ENUMERATED_COMMIT:
2906 0ab4c957 2022-06-13 stsp if (have_commit && nentries != -1) {
2907 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2910 0ab4c957 2022-06-13 stsp if (datalen != sizeof(*icommit)) {
2911 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2914 0ab4c957 2022-06-13 stsp icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2915 0ab4c957 2022-06-13 stsp memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2916 0ab4c957 2022-06-13 stsp mtime = icommit->mtime;
2917 0ab4c957 2022-06-13 stsp have_commit = 1;
2919 0ab4c957 2022-06-13 stsp case GOT_IMSG_ENUMERATED_TREE:
2920 0ab4c957 2022-06-13 stsp /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2921 0ab4c957 2022-06-13 stsp if (!have_commit) {
2922 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2925 0ab4c957 2022-06-13 stsp if (datalen < sizeof(*itree)) {
2926 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2929 0ab4c957 2022-06-13 stsp itree = imsg.data;
2930 0ab4c957 2022-06-13 stsp path_len = datalen - sizeof(*itree);
2931 0ab4c957 2022-06-13 stsp if (path_len == 0) {
2932 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2935 0ab4c957 2022-06-13 stsp memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2936 0ab4c957 2022-06-13 stsp free(path);
2937 00b3e9ae 2023-01-11 op path = strndup(imsg.data + sizeof(*itree), path_len);
2938 0ab4c957 2022-06-13 stsp if (path == NULL) {
2939 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
2942 0ab4c957 2022-06-13 stsp free(canon_path);
2943 0ab4c957 2022-06-13 stsp canon_path = malloc(path_len + 1);
2944 0ab4c957 2022-06-13 stsp if (canon_path == NULL) {
2945 0ab4c957 2022-06-13 stsp err = got_error_from_errno("malloc");
2948 0ab4c957 2022-06-13 stsp if (!got_path_is_absolute(path)) {
2949 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_BAD_PATH);
2952 0ab4c957 2022-06-13 stsp if (got_path_is_root_dir(path)) {
2953 0ab4c957 2022-06-13 stsp /* XXX check what got_canonpath() does wrong */
2954 0ab4c957 2022-06-13 stsp canon_path[0] = '/';
2955 0ab4c957 2022-06-13 stsp canon_path[1] = '\0';
2957 0ab4c957 2022-06-13 stsp err = got_canonpath(path, canon_path,
2958 0ab4c957 2022-06-13 stsp path_len + 1);
2962 0ab4c957 2022-06-13 stsp if (strcmp(path, canon_path) != 0) {
2963 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_BAD_PATH);
2966 0ab4c957 2022-06-13 stsp if (nentries != -1) {
2967 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2970 0ab4c957 2022-06-13 stsp if (itree->nentries < -1) {
2971 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2974 0ab4c957 2022-06-13 stsp if (itree->nentries == -1) {
2975 0ab4c957 2022-06-13 stsp /* Tree was not found in pack file. */
2976 0ab4c957 2022-06-13 stsp err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2977 0ab4c957 2022-06-13 stsp path, repo);
2980 0ab4c957 2022-06-13 stsp if (itree->nentries > INT_MAX) {
2981 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2984 0ab4c957 2022-06-13 stsp tree.entries = calloc(itree->nentries,
2985 0ab4c957 2022-06-13 stsp sizeof(struct got_tree_entry));
2986 0ab4c957 2022-06-13 stsp if (tree.entries == NULL) {
2987 0ab4c957 2022-06-13 stsp err = got_error_from_errno("calloc");
2990 0ab4c957 2022-06-13 stsp if (itree->nentries == 0) {
2991 0ab4c957 2022-06-13 stsp err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2992 0ab4c957 2022-06-13 stsp path, repo);
2996 0ab4c957 2022-06-13 stsp /* Prepare for next tree. */
2997 0ab4c957 2022-06-13 stsp free(tree.entries);
2998 0ab4c957 2022-06-13 stsp memset(&tree, 0, sizeof(tree));
2999 0ab4c957 2022-06-13 stsp nentries = -1;
3001 0ab4c957 2022-06-13 stsp tree.nentries = itree->nentries;
3002 0ab4c957 2022-06-13 stsp nentries = 0;
3005 0ab4c957 2022-06-13 stsp case GOT_IMSG_TREE_ENTRIES:
3006 0ab4c957 2022-06-13 stsp /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
3007 0ab4c957 2022-06-13 stsp if (nentries <= -1) {
3008 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3011 0ab4c957 2022-06-13 stsp err = recv_tree_entries(imsg.data, datalen,
3012 0ab4c957 2022-06-13 stsp &tree, &nentries);
3015 0ab4c957 2022-06-13 stsp if (tree.nentries == nentries) {
3016 0ab4c957 2022-06-13 stsp err = cb_tree(cb_arg, &tree, mtime, &tree_id,
3017 0ab4c957 2022-06-13 stsp path, repo);
3021 0ab4c957 2022-06-13 stsp /* Prepare for next tree. */
3022 0ab4c957 2022-06-13 stsp free(tree.entries);
3023 0ab4c957 2022-06-13 stsp memset(&tree, 0, sizeof(tree));
3024 0ab4c957 2022-06-13 stsp nentries = -1;
3027 0ab4c957 2022-06-13 stsp case GOT_IMSG_TREE_ENUMERATION_DONE:
3028 0ab4c957 2022-06-13 stsp /* All trees have been found and traversed. */
3029 0ab4c957 2022-06-13 stsp if (!have_commit || path == NULL || nentries != -1) {
3030 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3033 0ab4c957 2022-06-13 stsp err = cb_commit(cb_arg, mtime, &commit_id, repo);
3036 0ab4c957 2022-06-13 stsp have_commit = 0;
3038 0ab4c957 2022-06-13 stsp case GOT_IMSG_OBJECT_ENUMERATION_DONE:
3039 db9b9b1c 2022-06-14 stsp *found_all_objects = 1;
3042 db9b9b1c 2022-06-14 stsp case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
3046 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3050 0ab4c957 2022-06-13 stsp imsg_free(&imsg);
3055 0ab4c957 2022-06-13 stsp free(path);
3056 0ab4c957 2022-06-13 stsp free(canon_path);
3057 0ab4c957 2022-06-13 stsp free(tree.entries);
3058 0ab4c957 2022-06-13 stsp return err;
3061 0ab4c957 2022-06-13 stsp const struct got_error *
3062 67fd6849 2022-02-13 stsp got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
3063 67fd6849 2022-02-13 stsp struct got_object_id *id)
3065 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta_request dreq;
3067 031611fc 2022-06-18 op memset(&dreq, 0, sizeof(dreq));
3068 67fd6849 2022-02-13 stsp dreq.idx = idx;
3069 babd9f5d 2023-01-31 op memcpy(&dreq.id, id, sizeof(dreq.id));
3071 67fd6849 2022-02-13 stsp if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
3072 67fd6849 2022-02-13 stsp &dreq, sizeof(dreq)) == -1)
3073 67fd6849 2022-02-13 stsp return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
3075 67fd6849 2022-02-13 stsp return flush_imsg(ibuf);
3078 67fd6849 2022-02-13 stsp const struct got_error *
3079 67fd6849 2022-02-13 stsp got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
3081 67fd6849 2022-02-13 stsp return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3084 67fd6849 2022-02-13 stsp const struct got_error *
3085 67fd6849 2022-02-13 stsp got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3086 2d9e6abf 2022-05-04 stsp uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3087 2d9e6abf 2022-05-04 stsp off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3089 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta idelta;
3092 031611fc 2022-06-18 op memset(&idelta, 0, sizeof(idelta));
3093 67fd6849 2022-02-13 stsp idelta.base_size = base_size;
3094 67fd6849 2022-02-13 stsp idelta.result_size = result_size;
3095 67fd6849 2022-02-13 stsp idelta.delta_size = delta_size;
3096 2d9e6abf 2022-05-04 stsp idelta.delta_compressed_size = delta_compressed_size;
3097 67fd6849 2022-02-13 stsp idelta.delta_offset = delta_offset;
3098 67fd6849 2022-02-13 stsp idelta.delta_out_offset = delta_out_offset;
3099 407521c0 2023-01-31 op memcpy(&idelta.base_id, &base_id, sizeof(idelta.base_id));
3101 67fd6849 2022-02-13 stsp ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3102 67fd6849 2022-02-13 stsp &idelta, sizeof(idelta));
3103 67fd6849 2022-02-13 stsp if (ret == -1)
3104 67fd6849 2022-02-13 stsp return got_error_from_errno("imsg_compose RAW_DELTA");
3106 67fd6849 2022-02-13 stsp return flush_imsg(ibuf);
3109 ca6e02ac 2020-01-07 stsp const struct got_error *
3110 67fd6849 2022-02-13 stsp got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3111 2d9e6abf 2022-05-04 stsp off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3112 d283eca6 2023-01-31 op off_t *delta_out_offset, struct got_object_id **base_id,
3113 d283eca6 2023-01-31 op struct imsgbuf *ibuf)
3115 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
3116 67fd6849 2022-02-13 stsp struct imsg imsg;
3117 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta *delta;
3118 67fd6849 2022-02-13 stsp size_t datalen;
3120 67fd6849 2022-02-13 stsp *base_size = 0;
3121 67fd6849 2022-02-13 stsp *result_size = 0;
3122 67fd6849 2022-02-13 stsp *delta_size = 0;
3123 2d9e6abf 2022-05-04 stsp *delta_compressed_size = 0;
3124 67fd6849 2022-02-13 stsp *delta_offset = 0;
3125 67fd6849 2022-02-13 stsp *delta_out_offset = 0;
3126 67fd6849 2022-02-13 stsp *base_id = NULL;
3128 67fd6849 2022-02-13 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3130 67fd6849 2022-02-13 stsp return err;
3132 67fd6849 2022-02-13 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3134 67fd6849 2022-02-13 stsp switch (imsg.hdr.type) {
3135 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA:
3136 67fd6849 2022-02-13 stsp if (datalen != sizeof(*delta)) {
3137 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
3140 67fd6849 2022-02-13 stsp delta = imsg.data;
3141 67fd6849 2022-02-13 stsp *base_size = delta->base_size;
3142 67fd6849 2022-02-13 stsp *result_size = delta->result_size;
3143 67fd6849 2022-02-13 stsp *delta_size = delta->delta_size;
3144 2d9e6abf 2022-05-04 stsp *delta_compressed_size = delta->delta_compressed_size;
3145 67fd6849 2022-02-13 stsp *delta_offset = delta->delta_offset;
3146 67fd6849 2022-02-13 stsp *delta_out_offset = delta->delta_out_offset;
3147 67fd6849 2022-02-13 stsp *base_id = calloc(1, sizeof(**base_id));
3148 67fd6849 2022-02-13 stsp if (*base_id == NULL) {
3149 67fd6849 2022-02-13 stsp err = got_error_from_errno("malloc");
3152 407521c0 2023-01-31 op memcpy(*base_id, &delta->base_id, sizeof(**base_id));
3155 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3159 67fd6849 2022-02-13 stsp imsg_free(&imsg);
3161 67fd6849 2022-02-13 stsp if (err) {
3162 67fd6849 2022-02-13 stsp free(*base_id);
3163 67fd6849 2022-02-13 stsp *base_id = NULL;
3165 fae7e038 2022-05-07 stsp return err;
3168 0ab4c957 2022-06-13 stsp static const struct got_error *
3169 0ab4c957 2022-06-13 stsp send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3171 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
3172 fae7e038 2022-05-07 stsp struct got_imsg_object_idlist idlist;
3173 fae7e038 2022-05-07 stsp struct ibuf *wbuf;
3176 031611fc 2022-06-18 op memset(&idlist, 0, sizeof(idlist));
3178 fae7e038 2022-05-07 stsp if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3179 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_SPACE);
3181 fae7e038 2022-05-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3182 fae7e038 2022-05-07 stsp sizeof(idlist) + nids * sizeof(**ids));
3183 fae7e038 2022-05-07 stsp if (wbuf == NULL) {
3184 fae7e038 2022-05-07 stsp err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3185 fae7e038 2022-05-07 stsp return err;
3188 fae7e038 2022-05-07 stsp idlist.nids = nids;
3189 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3190 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add OBJ_ID_LIST");
3192 fae7e038 2022-05-07 stsp for (i = 0; i < nids; i++) {
3193 fae7e038 2022-05-07 stsp struct got_object_id *id = ids[i];
3194 1453347d 2022-05-19 stsp if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3195 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add OBJ_ID_LIST");
3198 fae7e038 2022-05-07 stsp wbuf->fd = -1;
3199 fae7e038 2022-05-07 stsp imsg_close(ibuf, wbuf);
3201 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3204 fae7e038 2022-05-07 stsp const struct got_error *
3205 0ab4c957 2022-06-13 stsp got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3206 0ab4c957 2022-06-13 stsp struct got_object_id **ids, size_t nids)
3208 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
3209 0ab4c957 2022-06-13 stsp struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3210 a5e587e0 2022-06-14 stsp int i, queued = 0;
3212 b4b1b9c0 2022-06-14 op for (i = 0; i < nids; i++) {
3213 a5e587e0 2022-06-14 stsp idlist[i % nitems(idlist)] = ids[i];
3215 afd31498 2022-06-14 stsp if (queued >= nitems(idlist)) {
3216 a5e587e0 2022-06-14 stsp err = send_idlist(ibuf, idlist, queued);
3218 0ab4c957 2022-06-13 stsp return err;
3219 a5e587e0 2022-06-14 stsp queued = 0;
3223 a5e587e0 2022-06-14 stsp if (queued > 0) {
3224 a5e587e0 2022-06-14 stsp err = send_idlist(ibuf, idlist, queued);
3226 0ab4c957 2022-06-13 stsp return err;
3229 0ab4c957 2022-06-13 stsp return NULL;
3232 0ab4c957 2022-06-13 stsp const struct got_error *
3233 fae7e038 2022-05-07 stsp got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3235 fae7e038 2022-05-07 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3237 fae7e038 2022-05-07 stsp return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3239 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3242 fae7e038 2022-05-07 stsp const struct got_error *
3243 fae7e038 2022-05-07 stsp got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3244 fae7e038 2022-05-07 stsp size_t *nids, struct imsgbuf *ibuf)
3246 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
3247 fae7e038 2022-05-07 stsp struct imsg imsg;
3248 fae7e038 2022-05-07 stsp struct got_imsg_object_idlist *idlist;
3249 fae7e038 2022-05-07 stsp size_t datalen;
3251 fae7e038 2022-05-07 stsp *ids = NULL;
3252 fae7e038 2022-05-07 stsp *done = 0;
3253 fae7e038 2022-05-07 stsp *nids = 0;
3255 fae7e038 2022-05-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3257 fae7e038 2022-05-07 stsp return err;
3259 fae7e038 2022-05-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3260 fae7e038 2022-05-07 stsp switch (imsg.hdr.type) {
3261 fae7e038 2022-05-07 stsp case GOT_IMSG_OBJ_ID_LIST:
3262 fae7e038 2022-05-07 stsp if (datalen < sizeof(*idlist)) {
3263 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
3266 fae7e038 2022-05-07 stsp idlist = imsg.data;
3267 c98b0f0b 2022-06-14 op if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3268 c98b0f0b 2022-06-14 op idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3269 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
3272 fae7e038 2022-05-07 stsp *nids = idlist->nids;
3273 fae7e038 2022-05-07 stsp *ids = calloc(*nids, sizeof(**ids));
3274 fae7e038 2022-05-07 stsp if (*ids == NULL) {
3275 fae7e038 2022-05-07 stsp err = got_error_from_errno("calloc");
3278 0fca100c 2022-06-14 op memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3279 fae7e038 2022-05-07 stsp *nids * sizeof(**ids));
3281 fae7e038 2022-05-07 stsp case GOT_IMSG_OBJ_ID_LIST_DONE:
3282 fae7e038 2022-05-07 stsp *done = 1;
3285 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3289 fae7e038 2022-05-07 stsp imsg_free(&imsg);
3291 fae7e038 2022-05-07 stsp return err;
3294 fae7e038 2022-05-07 stsp const struct got_error *
3295 fae7e038 2022-05-07 stsp got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3297 fae7e038 2022-05-07 stsp if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3299 fae7e038 2022-05-07 stsp return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3301 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3304 fae7e038 2022-05-07 stsp const struct got_error *
3305 fae7e038 2022-05-07 stsp got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3306 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *deltas, size_t ndeltas)
3308 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
3309 fae7e038 2022-05-07 stsp struct ibuf *wbuf;
3310 fae7e038 2022-05-07 stsp struct got_imsg_reused_deltas ideltas;
3313 031611fc 2022-06-18 op memset(&ideltas, 0, sizeof(ideltas));
3315 fae7e038 2022-05-07 stsp if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3316 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_SPACE);
3318 fae7e038 2022-05-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3319 fae7e038 2022-05-07 stsp sizeof(ideltas) + ndeltas * sizeof(*deltas));
3320 fae7e038 2022-05-07 stsp if (wbuf == NULL) {
3321 fae7e038 2022-05-07 stsp err = got_error_from_errno("imsg_create REUSED_DELTAS");
3322 fae7e038 2022-05-07 stsp return err;
3325 fae7e038 2022-05-07 stsp ideltas.ndeltas = ndeltas;
3326 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3327 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add REUSED_DELTAS");
3329 fae7e038 2022-05-07 stsp for (i = 0; i < ndeltas; i++) {
3330 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta = &deltas[i];
3331 1453347d 2022-05-19 stsp if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3332 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add REUSED_DELTAS");
3335 fae7e038 2022-05-07 stsp wbuf->fd = -1;
3336 fae7e038 2022-05-07 stsp imsg_close(ibuf, wbuf);
3338 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3341 fae7e038 2022-05-07 stsp const struct got_error *
3342 fae7e038 2022-05-07 stsp got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3344 fae7e038 2022-05-07 stsp if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3346 fae7e038 2022-05-07 stsp return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3348 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3351 fae7e038 2022-05-07 stsp const struct got_error *
3352 fae7e038 2022-05-07 stsp got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3353 fae7e038 2022-05-07 stsp size_t *ndeltas, struct imsgbuf *ibuf)
3355 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
3356 fae7e038 2022-05-07 stsp struct imsg imsg;
3357 fae7e038 2022-05-07 stsp struct got_imsg_reused_deltas *ideltas;
3358 fae7e038 2022-05-07 stsp size_t datalen;
3360 fae7e038 2022-05-07 stsp *done = 0;
3361 fae7e038 2022-05-07 stsp *ndeltas = 0;
3363 fae7e038 2022-05-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3365 fae7e038 2022-05-07 stsp return err;
3367 fae7e038 2022-05-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3368 fae7e038 2022-05-07 stsp switch (imsg.hdr.type) {
3369 fae7e038 2022-05-07 stsp case GOT_IMSG_REUSED_DELTAS:
3370 fae7e038 2022-05-07 stsp if (datalen < sizeof(*ideltas)) {
3371 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
3374 fae7e038 2022-05-07 stsp ideltas = imsg.data;
3375 c98b0f0b 2022-06-14 op if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3376 c98b0f0b 2022-06-14 op ideltas->ndeltas * sizeof(*deltas) >
3377 c98b0f0b 2022-06-14 op datalen - sizeof(*ideltas)) {
3378 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
3381 fae7e038 2022-05-07 stsp *ndeltas = ideltas->ndeltas;
3382 0fca100c 2022-06-14 op memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3383 fae7e038 2022-05-07 stsp *ndeltas * sizeof(*deltas));
3385 fae7e038 2022-05-07 stsp case GOT_IMSG_DELTA_REUSE_DONE:
3386 fae7e038 2022-05-07 stsp *done = 1;
3389 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3393 fae7e038 2022-05-07 stsp imsg_free(&imsg);
3395 61af9b21 2022-06-28 stsp return err;
3398 61af9b21 2022-06-28 stsp const struct got_error *
3399 61af9b21 2022-06-28 stsp got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3401 61af9b21 2022-06-28 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_INIT,
3402 61af9b21 2022-06-28 stsp 0, 0, -1, NULL, 0)
3404 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_compose "
3405 61af9b21 2022-06-28 stsp "COMMIT_PAINTING_INIT");
3407 61af9b21 2022-06-28 stsp return flush_imsg(ibuf);
3410 61af9b21 2022-06-28 stsp const struct got_error *
3411 61af9b21 2022-06-28 stsp got_privsep_send_painting_request(struct imsgbuf *ibuf, int idx,
3412 61af9b21 2022-06-28 stsp struct got_object_id *id, intptr_t color)
3414 61af9b21 2022-06-28 stsp struct got_imsg_commit_painting_request ireq;
3416 61af9b21 2022-06-28 stsp memset(&ireq, 0, sizeof(ireq));
3417 076fbedc 2023-02-19 op memcpy(&ireq.id, id, sizeof(ireq.id));
3418 61af9b21 2022-06-28 stsp ireq.idx = idx;
3419 61af9b21 2022-06-28 stsp ireq.color = color;
3421 61af9b21 2022-06-28 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_REQUEST, 0, 0, -1,
3422 61af9b21 2022-06-28 stsp &ireq, sizeof(ireq)) == -1)
3423 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_compose "
3424 61af9b21 2022-06-28 stsp "COMMIT_PAINTING_REQUEST");
3426 61af9b21 2022-06-28 stsp return flush_imsg(ibuf);
3429 61af9b21 2022-06-28 stsp static const struct got_error *
3430 61af9b21 2022-06-28 stsp send_painted_commits(struct got_object_id_queue *ids, int *nids,
3431 61af9b21 2022-06-28 stsp size_t remain, int present_in_pack, struct imsgbuf *ibuf)
3433 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
3434 61af9b21 2022-06-28 stsp struct ibuf *wbuf = NULL;
3435 61af9b21 2022-06-28 stsp struct got_object_qid *qid;
3436 61af9b21 2022-06-28 stsp size_t msglen;
3437 61af9b21 2022-06-28 stsp int ncommits;
3438 61af9b21 2022-06-28 stsp intptr_t color;
3440 61af9b21 2022-06-28 stsp msglen = MIN(remain, MAX_IMSGSIZE - IMSG_HEADER_SIZE);
3441 61af9b21 2022-06-28 stsp ncommits = (msglen - sizeof(struct got_imsg_painted_commits)) /
3442 61af9b21 2022-06-28 stsp sizeof(struct got_imsg_painted_commit);
3444 61af9b21 2022-06-28 stsp wbuf = imsg_create(ibuf, GOT_IMSG_PAINTED_COMMITS, 0, 0, msglen);
3445 61af9b21 2022-06-28 stsp if (wbuf == NULL) {
3446 61af9b21 2022-06-28 stsp err = got_error_from_errno("imsg_create PAINTED_COMMITS");
3447 61af9b21 2022-06-28 stsp return err;
3450 61af9b21 2022-06-28 stsp /* Keep in sync with struct got_imsg_painted_commits! */
3451 61af9b21 2022-06-28 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
3452 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_add PAINTED_COMMITS");
3453 61af9b21 2022-06-28 stsp if (imsg_add(wbuf, &present_in_pack, sizeof(present_in_pack)) == -1)
3454 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_add PAINTED_COMMITS");
3456 61af9b21 2022-06-28 stsp while (ncommits > 0) {
3457 61af9b21 2022-06-28 stsp qid = STAILQ_FIRST(ids);
3458 61af9b21 2022-06-28 stsp STAILQ_REMOVE_HEAD(ids, entry);
3459 61af9b21 2022-06-28 stsp ncommits--;
3460 61af9b21 2022-06-28 stsp (*nids)--;
3461 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
3463 61af9b21 2022-06-28 stsp /* Keep in sync with struct got_imsg_painted_commit! */
3464 61af9b21 2022-06-28 stsp if (imsg_add(wbuf, qid->id.sha1, SHA1_DIGEST_LENGTH) == -1)
3465 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_add PAINTED_COMMITS");
3466 61af9b21 2022-06-28 stsp if (imsg_add(wbuf, &color, sizeof(color)) == -1)
3467 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_add PAINTED_COMMITS");
3469 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
3472 61af9b21 2022-06-28 stsp wbuf->fd = -1;
3473 61af9b21 2022-06-28 stsp imsg_close(ibuf, wbuf);
3475 61af9b21 2022-06-28 stsp return flush_imsg(ibuf);
3478 61af9b21 2022-06-28 stsp const struct got_error *
3479 61af9b21 2022-06-28 stsp got_privsep_send_painted_commits(struct imsgbuf *ibuf,
3480 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids, int *nids,
3481 61af9b21 2022-06-28 stsp int present_in_pack, int flush)
3483 61af9b21 2022-06-28 stsp const struct got_error *err;
3484 61af9b21 2022-06-28 stsp size_t remain;
3486 61af9b21 2022-06-28 stsp if (*nids <= 0)
3487 61af9b21 2022-06-28 stsp return NULL;
3490 61af9b21 2022-06-28 stsp remain = (sizeof(struct got_imsg_painted_commits)) +
3491 61af9b21 2022-06-28 stsp *nids * sizeof(struct got_imsg_painted_commit);
3492 61af9b21 2022-06-28 stsp if (flush || remain >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
3493 61af9b21 2022-06-28 stsp err = send_painted_commits(ids, nids, remain,
3494 61af9b21 2022-06-28 stsp present_in_pack, ibuf);
3496 61af9b21 2022-06-28 stsp return err;
3498 61af9b21 2022-06-28 stsp } while (flush && *nids > 0);
3500 61af9b21 2022-06-28 stsp return NULL;
3503 61af9b21 2022-06-28 stsp const struct got_error *
3504 61af9b21 2022-06-28 stsp got_privsep_send_painting_commits_done(struct imsgbuf *ibuf)
3506 61af9b21 2022-06-28 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_DONE,
3507 61af9b21 2022-06-28 stsp 0, 0, -1, NULL, 0)
3509 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_compose "
3510 61af9b21 2022-06-28 stsp "COMMIT_PAINTING_DONE");
3512 61af9b21 2022-06-28 stsp return flush_imsg(ibuf);
3515 61af9b21 2022-06-28 stsp const struct got_error *
3516 61af9b21 2022-06-28 stsp got_privsep_recv_painted_commits(struct got_object_id_queue *new_ids,
3517 61af9b21 2022-06-28 stsp got_privsep_recv_painted_commit_cb cb, void *cb_arg, struct imsgbuf *ibuf)
3519 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
3520 61af9b21 2022-06-28 stsp struct imsg imsg;
3521 61af9b21 2022-06-28 stsp struct got_imsg_painted_commits icommits;
3522 61af9b21 2022-06-28 stsp struct got_imsg_painted_commit icommit;
3523 61af9b21 2022-06-28 stsp size_t datalen;
3526 61af9b21 2022-06-28 stsp for (;;) {
3527 61af9b21 2022-06-28 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3529 61af9b21 2022-06-28 stsp return err;
3531 61af9b21 2022-06-28 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3532 61af9b21 2022-06-28 stsp if (imsg.hdr.type == GOT_IMSG_COMMIT_PAINTING_DONE)
3534 61af9b21 2022-06-28 stsp if (imsg.hdr.type != GOT_IMSG_PAINTED_COMMITS)
3535 61af9b21 2022-06-28 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
3537 61af9b21 2022-06-28 stsp if (datalen < sizeof(icommits))
3538 61af9b21 2022-06-28 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
3539 61af9b21 2022-06-28 stsp memcpy(&icommits, imsg.data, sizeof(icommits));
3540 61af9b21 2022-06-28 stsp if (icommits.ncommits * sizeof(icommit) < icommits.ncommits ||
3541 61af9b21 2022-06-28 stsp datalen < sizeof(icommits) +
3542 61af9b21 2022-06-28 stsp icommits.ncommits * sizeof(icommit))
3543 61af9b21 2022-06-28 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
3545 61af9b21 2022-06-28 stsp for (i = 0; i < icommits.ncommits; i++) {
3546 61af9b21 2022-06-28 stsp memcpy(&icommit,
3547 61af9b21 2022-06-28 stsp (uint8_t *)imsg.data + sizeof(icommits) + i * sizeof(icommit),
3548 61af9b21 2022-06-28 stsp sizeof(icommit));
3550 61af9b21 2022-06-28 stsp if (icommits.present_in_pack) {
3551 61af9b21 2022-06-28 stsp struct got_object_id id;
3552 61af9b21 2022-06-28 stsp memcpy(id.sha1, icommit.id, SHA1_DIGEST_LENGTH);
3553 61af9b21 2022-06-28 stsp err = cb(cb_arg, &id, icommit.color);
3557 61af9b21 2022-06-28 stsp struct got_object_qid *qid;
3558 61af9b21 2022-06-28 stsp err = got_object_qid_alloc_partial(&qid);
3561 61af9b21 2022-06-28 stsp memcpy(qid->id.sha1, icommit.id,
3562 61af9b21 2022-06-28 stsp SHA1_DIGEST_LENGTH);
3563 61af9b21 2022-06-28 stsp qid->data = (void *)icommit.color;
3564 61af9b21 2022-06-28 stsp STAILQ_INSERT_TAIL(new_ids, qid, entry);
3568 61af9b21 2022-06-28 stsp imsg_free(&imsg);
3571 67fd6849 2022-02-13 stsp return err;
3574 67fd6849 2022-02-13 stsp const struct got_error *
3575 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
3577 c39c25dd 2019-08-09 stsp const char *helpers[] = {
3578 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
3579 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
3580 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
3581 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
3582 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
3583 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
3584 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
3585 257add31 2020-09-09 stsp GOT_PATH_PROG_READ_GOTCONFIG,
3586 e9ce266e 2022-03-07 op GOT_PATH_PROG_READ_PATCH,
3587 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK,
3588 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK,
3589 f8a36e22 2021-08-26 stsp GOT_PATH_PROG_SEND_PACK,
3593 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
3594 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
3596 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
3599 63219cd2 2019-01-04 stsp return NULL;
3603 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3605 08578a35 2021-01-22 stsp if (close(imsg_fds[0]) == -1) {
3606 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3610 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3611 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3615 4a55b231 2021-12-09 stsp closefrom(GOT_IMSG_FD_CHILD + 1);
3617 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
3618 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3619 aba9c984 2019-09-08 stsp strerror(errno));