Blame


1 2178c42e 2018-04-22 stsp /*
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>
4 2178c42e 2018-04-22 stsp *
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.
8 2178c42e 2018-04-22 stsp *
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.
16 2178c42e 2018-04-22 stsp */
17 2178c42e 2018-04-22 stsp
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>
22 2178c42e 2018-04-22 stsp
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>
37 2178c42e 2018-04-22 stsp
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"
42 2178c42e 2018-04-22 stsp
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"
52 336075a4 2022-06-25 op
53 336075a4 2022-06-25 op #include "got_privsep.h"
54 2178c42e 2018-04-22 stsp
55 2178c42e 2018-04-22 stsp #ifndef MIN
56 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 c39c25dd 2019-08-09 stsp #endif
58 c39c25dd 2019-08-09 stsp
59 c39c25dd 2019-08-09 stsp #ifndef nitems
60 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 2178c42e 2018-04-22 stsp #endif
62 2178c42e 2018-04-22 stsp
63 2178c42e 2018-04-22 stsp static const struct got_error *
64 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
65 fe36cf76 2018-04-23 stsp {
66 fe36cf76 2018-04-23 stsp const struct got_error *err;
67 e033d803 2018-04-23 stsp size_t n;
68 fe36cf76 2018-04-23 stsp
69 13b2bc37 2022-10-23 stsp err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
70 13b2bc37 2022-10-23 stsp if (err) {
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);
73 c9b75c7b 2022-05-19 stsp return err;
74 13b2bc37 2022-10-23 stsp }
75 fe36cf76 2018-04-23 stsp
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);
81 fe36cf76 2018-04-23 stsp }
82 fe36cf76 2018-04-23 stsp if (n == 0)
83 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
84 fe36cf76 2018-04-23 stsp
85 e033d803 2018-04-23 stsp return NULL;
86 e033d803 2018-04-23 stsp }
87 e033d803 2018-04-23 stsp
88 ad242220 2018-09-08 stsp const struct got_error *
89 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
90 876c234b 2018-09-10 stsp {
91 876c234b 2018-09-10 stsp int child_status;
92 876c234b 2018-09-10 stsp
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");
95 876c234b 2018-09-10 stsp
96 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
97 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
98 876c234b 2018-09-10 stsp
99 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
100 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
101 876c234b 2018-09-10 stsp
102 876c234b 2018-09-10 stsp return NULL;
103 876c234b 2018-09-10 stsp }
104 876c234b 2018-09-10 stsp
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)
107 73b7854a 2018-11-11 stsp {
108 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
109 73b7854a 2018-11-11 stsp
110 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
111 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
112 73b7854a 2018-11-11 stsp
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;
119 73b7854a 2018-11-11 stsp }
120 73b7854a 2018-11-11 stsp
121 73b7854a 2018-11-11 stsp return got_error(ierr->code);
122 73b7854a 2018-11-11 stsp }
123 73b7854a 2018-11-11 stsp
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)
127 e033d803 2018-04-23 stsp {
128 e033d803 2018-04-23 stsp const struct got_error *err;
129 e033d803 2018-04-23 stsp ssize_t n;
130 e033d803 2018-04-23 stsp
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");
134 876c234b 2018-09-10 stsp
135 876c234b 2018-09-10 stsp while (n == 0) {
136 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
137 876c234b 2018-09-10 stsp if (err)
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");
142 876c234b 2018-09-10 stsp }
143 fe36cf76 2018-04-23 stsp
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);
146 c4eae628 2018-04-23 stsp
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);
150 c4eae628 2018-04-23 stsp }
151 c4eae628 2018-04-23 stsp
152 73b7854a 2018-11-11 stsp return NULL;
153 c4eae628 2018-04-23 stsp }
154 c4eae628 2018-04-23 stsp
155 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
156 2178c42e 2018-04-22 stsp void
157 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
158 2178c42e 2018-04-22 stsp {
159 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
160 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
161 2178c42e 2018-04-22 stsp int ret;
162 2178c42e 2018-04-22 stsp
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;
166 2178c42e 2018-04-22 stsp else
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));
172 5d43e84d 2018-04-23 stsp return;
173 2178c42e 2018-04-22 stsp }
174 2178c42e 2018-04-22 stsp
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);
179 5d43e84d 2018-04-23 stsp return;
180 5d43e84d 2018-04-23 stsp }
181 2178c42e 2018-04-22 stsp
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);
187 5d43e84d 2018-04-23 stsp return;
188 5d43e84d 2018-04-23 stsp }
189 e033d803 2018-04-23 stsp }
190 e033d803 2018-04-23 stsp
191 e033d803 2018-04-23 stsp static const struct got_error *
192 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
193 e033d803 2018-04-23 stsp {
194 e033d803 2018-04-23 stsp const struct got_error *err;
195 e033d803 2018-04-23 stsp
196 13b2bc37 2022-10-23 stsp err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
197 e033d803 2018-04-23 stsp if (err)
198 e033d803 2018-04-23 stsp return err;
199 e033d803 2018-04-23 stsp
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");
203 8934ea8b 2022-02-14 op }
204 e033d803 2018-04-23 stsp
205 e033d803 2018-04-23 stsp return NULL;
206 ad242220 2018-09-08 stsp }
207 ad242220 2018-09-08 stsp
208 ad242220 2018-09-08 stsp const struct got_error *
209 e70bf110 2020-03-22 stsp got_privsep_flush_imsg(struct imsgbuf *ibuf)
210 e70bf110 2020-03-22 stsp {
211 e70bf110 2020-03-22 stsp return flush_imsg(ibuf);
212 e70bf110 2020-03-22 stsp }
213 e70bf110 2020-03-22 stsp
214 e70bf110 2020-03-22 stsp const struct got_error *
215 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
216 ad242220 2018-09-08 stsp {
217 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
218 ad242220 2018-09-08 stsp
219 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
220 ad242220 2018-09-08 stsp
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");
223 ad242220 2018-09-08 stsp
224 ad4e3a35 2023-10-26 op return flush_imsg(&ibuf);
225 7762fe12 2018-11-05 stsp }
226 93658fb9 2020-03-18 stsp
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)
230 ad242220 2018-09-08 stsp {
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");
234 59d1e4a0 2021-03-10 stsp
235 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
236 59d1e4a0 2021-03-10 stsp }
237 59d1e4a0 2021-03-10 stsp
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)
241 59d1e4a0 2021-03-10 stsp {
242 2161bc24 2023-11-03 op const struct got_error *err;
243 2161bc24 2023-11-03 op
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");
247 2161bc24 2023-11-03 op close(fd);
248 2161bc24 2023-11-03 op return err;
249 2161bc24 2023-11-03 op }
250 59d1e4a0 2021-03-10 stsp
251 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
252 59d1e4a0 2021-03-10 stsp }
253 59d1e4a0 2021-03-10 stsp
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)
256 59d1e4a0 2021-03-10 stsp {
257 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
258 59d1e4a0 2021-03-10 stsp
259 59d1e4a0 2021-03-10 stsp if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
260 59d1e4a0 2021-03-10 stsp == -1) {
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;
264 59d1e4a0 2021-03-10 stsp }
265 59d1e4a0 2021-03-10 stsp
266 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
267 59d1e4a0 2021-03-10 stsp }
268 59d1e4a0 2021-03-10 stsp
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)
272 59d1e4a0 2021-03-10 stsp {
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;
276 1785f84a 2018-12-23 stsp
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;
280 59d1e4a0 2021-03-10 stsp
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;
283 59d1e4a0 2021-03-10 stsp
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");
287 59d1e4a0 2021-03-10 stsp
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");
290 59d1e4a0 2021-03-10 stsp
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");
294 59d1e4a0 2021-03-10 stsp }
295 59d1e4a0 2021-03-10 stsp
296 59d1e4a0 2021-03-10 stsp wbuf->fd = -1;
297 59d1e4a0 2021-03-10 stsp imsg_close(ibuf, wbuf);
298 59d1e4a0 2021-03-10 stsp
299 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
300 1785f84a 2018-12-23 stsp }
301 1785f84a 2018-12-23 stsp
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)
305 59d1e4a0 2021-03-10 stsp {
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;
310 59d1e4a0 2021-03-10 stsp
311 59d1e4a0 2021-03-10 stsp *outbuf = NULL;
312 59d1e4a0 2021-03-10 stsp
313 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
314 59d1e4a0 2021-03-10 stsp if (err)
315 59d1e4a0 2021-03-10 stsp return err;
316 59d1e4a0 2021-03-10 stsp
317 59d1e4a0 2021-03-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
318 59d1e4a0 2021-03-10 stsp
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);
323 59d1e4a0 2021-03-10 stsp break;
324 59d1e4a0 2021-03-10 stsp }
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;
328 59d1e4a0 2021-03-10 stsp
329 59d1e4a0 2021-03-10 stsp if (datalen == sizeof(*iobj)) {
330 59d1e4a0 2021-03-10 stsp /* Data has been written to file descriptor. */
331 59d1e4a0 2021-03-10 stsp break;
332 59d1e4a0 2021-03-10 stsp }
333 59d1e4a0 2021-03-10 stsp
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);
337 59d1e4a0 2021-03-10 stsp break;
338 59d1e4a0 2021-03-10 stsp }
339 59d1e4a0 2021-03-10 stsp
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");
343 59d1e4a0 2021-03-10 stsp break;
344 59d1e4a0 2021-03-10 stsp }
345 a8591711 2021-06-18 stsp memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
346 59d1e4a0 2021-03-10 stsp break;
347 59d1e4a0 2021-03-10 stsp default:
348 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
349 59d1e4a0 2021-03-10 stsp break;
350 59d1e4a0 2021-03-10 stsp }
351 59d1e4a0 2021-03-10 stsp
352 59d1e4a0 2021-03-10 stsp imsg_free(&imsg);
353 59d1e4a0 2021-03-10 stsp
354 59d1e4a0 2021-03-10 stsp return err;
355 59d1e4a0 2021-03-10 stsp }
356 59d1e4a0 2021-03-10 stsp
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)
360 1785f84a 2018-12-23 stsp {
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;
365 1785f84a 2018-12-23 stsp
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);
372 1785f84a 2018-12-23 stsp } else {
373 d5c81d44 2021-07-08 stsp data = id;
374 d5c81d44 2021-07-08 stsp len = sizeof(*id);
375 1785f84a 2018-12-23 stsp }
376 1785f84a 2018-12-23 stsp
377 d5c81d44 2021-07-08 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
378 41496140 2019-02-21 stsp == -1) {
379 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
380 41496140 2019-02-21 stsp close(fd);
381 41496140 2019-02-21 stsp return err;
382 41496140 2019-02-21 stsp }
383 13c729f7 2018-12-24 stsp
384 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
385 13c729f7 2018-12-24 stsp }
386 13c729f7 2018-12-24 stsp
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)
390 13c729f7 2018-12-24 stsp {
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;
394 7f358e3b 2019-11-23 stsp
395 d5c81d44 2021-07-08 stsp if (pack_idx != -1)
396 d5c81d44 2021-07-08 stsp len = sizeof(struct got_imsg_packed_object);
397 d5c81d44 2021-07-08 stsp else
398 d5c81d44 2021-07-08 stsp len = sizeof(*id);
399 d5c81d44 2021-07-08 stsp
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)
404 2161bc24 2023-11-03 op close(fd);
405 2161bc24 2023-11-03 op return err;
406 2161bc24 2023-11-03 op }
407 13c729f7 2018-12-24 stsp
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)
411 2161bc24 2023-11-03 op close(fd);
412 2161bc24 2023-11-03 op return err;
413 2161bc24 2023-11-03 op }
414 13c729f7 2018-12-24 stsp
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)
419 2161bc24 2023-11-03 op close(fd);
420 2161bc24 2023-11-03 op return err;
421 2161bc24 2023-11-03 op }
422 41496140 2019-02-21 stsp }
423 268f7291 2018-12-24 stsp
424 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
425 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
426 7f358e3b 2019-11-23 stsp
427 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
428 268f7291 2018-12-24 stsp }
429 268f7291 2018-12-24 stsp
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)
433 268f7291 2018-12-24 stsp {
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;
438 268f7291 2018-12-24 stsp
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);
445 268f7291 2018-12-24 stsp } else {
446 d5c81d44 2021-07-08 stsp data = id;
447 d5c81d44 2021-07-08 stsp len = sizeof(*id);
448 268f7291 2018-12-24 stsp }
449 268f7291 2018-12-24 stsp
450 d5c81d44 2021-07-08 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
451 2161bc24 2023-11-03 op == -1) {
452 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_compose TAG_REQUEST");
453 2161bc24 2023-11-03 op if (fd != -1)
454 2161bc24 2023-11-03 op close(fd);
455 2161bc24 2023-11-03 op return err;
456 2161bc24 2023-11-03 op }
457 7762fe12 2018-11-05 stsp
458 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
459 7762fe12 2018-11-05 stsp }
460 7762fe12 2018-11-05 stsp
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)
464 55da3778 2018-09-10 stsp {
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;
469 ebc55e2d 2018-12-24 stsp
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);
476 ebc55e2d 2018-12-24 stsp } else {
477 d5c81d44 2021-07-08 stsp data = id;
478 d5c81d44 2021-07-08 stsp len = sizeof(*id);
479 ebc55e2d 2018-12-24 stsp }
480 ebc55e2d 2018-12-24 stsp
481 d5c81d44 2021-07-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
482 41496140 2019-02-21 stsp == -1) {
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;
486 41496140 2019-02-21 stsp }
487 ad242220 2018-09-08 stsp
488 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
489 55da3778 2018-09-10 stsp }
490 ad242220 2018-09-08 stsp
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)
493 55da3778 2018-09-10 stsp {
494 41496140 2019-02-21 stsp const struct got_error *err = NULL;
495 41496140 2019-02-21 stsp
496 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
497 41496140 2019-02-21 stsp == -1) {
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;
501 41496140 2019-02-21 stsp }
502 3840f4c9 2018-09-12 stsp
503 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
504 3840f4c9 2018-09-12 stsp }
505 3840f4c9 2018-09-12 stsp
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)
508 3840f4c9 2018-09-12 stsp {
509 41496140 2019-02-21 stsp const struct got_error *err = NULL;
510 41496140 2019-02-21 stsp
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");
513 41496140 2019-02-21 stsp close(fd);
514 41496140 2019-02-21 stsp return err;
515 41496140 2019-02-21 stsp }
516 ad242220 2018-09-08 stsp
517 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
518 ad242220 2018-09-08 stsp }
519 ad242220 2018-09-08 stsp
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)
522 73ab1060 2020-03-18 stsp {
523 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
524 73ab1060 2020-03-18 stsp }
525 73ab1060 2020-03-18 stsp
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)
528 2178c42e 2018-04-22 stsp {
529 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
530 1a69d37f 2022-06-18 op
531 1a69d37f 2022-06-18 op memset(&iobj, 0, sizeof(iobj));
532 2178c42e 2018-04-22 stsp
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;
541 c59b3346 2018-09-11 stsp }
542 2178c42e 2018-04-22 stsp
543 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
544 2178c42e 2018-04-22 stsp == -1)
545 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
546 2178c42e 2018-04-22 stsp
547 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
548 cfd633c2 2018-09-10 stsp }
549 cfd633c2 2018-09-10 stsp
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)
557 93658fb9 2020-03-18 stsp {
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);
563 93658fb9 2020-03-18 stsp
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;
567 118a625d 2023-02-18 mark }
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;
571 118a625d 2023-02-18 mark }
572 188f8dcf 2023-02-07 stsp
573 188f8dcf 2023-02-07 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
574 188f8dcf 2023-02-07 stsp close(fd);
575 188f8dcf 2023-02-07 stsp return got_error(GOT_ERR_NO_SPACE);
576 188f8dcf 2023-02-07 stsp }
577 188f8dcf 2023-02-07 stsp
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");
581 2161bc24 2023-11-03 op close(fd);
582 2161bc24 2023-11-03 op return err;
583 2161bc24 2023-11-03 op }
584 188f8dcf 2023-02-07 stsp
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)
604 2161bc24 2023-11-03 op == -1) {
605 2161bc24 2023-11-03 op err = got_error_from_errno("imsg_add FETCH_REQUEST");
606 2161bc24 2023-11-03 op close(fd);
607 2161bc24 2023-11-03 op return err;
608 2161bc24 2023-11-03 op }
609 33501562 2020-03-18 stsp }
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");
613 2161bc24 2023-11-03 op close(fd);
614 2161bc24 2023-11-03 op return err;
615 2161bc24 2023-11-03 op }
616 118a625d 2023-02-18 mark }
617 188f8dcf 2023-02-07 stsp wbuf->fd = fd;
618 2161bc24 2023-11-03 op fd = -1;
619 188f8dcf 2023-02-07 stsp imsg_close(ibuf, wbuf);
620 33501562 2020-03-18 stsp
621 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
622 2161bc24 2023-11-03 op if (err)
623 659e7fbd 2020-03-20 stsp return err;
624 33501562 2020-03-18 stsp
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;
629 33501562 2020-03-18 stsp
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");
634 4ba14133 2020-03-20 stsp
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");
642 4ba14133 2020-03-20 stsp
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);
646 4ba14133 2020-03-20 stsp if (err)
647 4ba14133 2020-03-20 stsp return err;
648 33501562 2020-03-18 stsp }
649 33501562 2020-03-18 stsp
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;
653 4ba14133 2020-03-20 stsp
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,
656 4ba14133 2020-03-20 stsp len);
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");
660 4ba14133 2020-03-20 stsp
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");
668 4ba14133 2020-03-20 stsp
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);
672 4ba14133 2020-03-20 stsp if (err)
673 4ba14133 2020-03-20 stsp return err;
674 4ba14133 2020-03-20 stsp }
675 4ba14133 2020-03-20 stsp
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;
679 0e4002ca 2020-03-21 stsp
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,
682 0e4002ca 2020-03-21 stsp len);
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");
686 0e4002ca 2020-03-21 stsp
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");
694 0e4002ca 2020-03-21 stsp
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);
698 0e4002ca 2020-03-21 stsp if (err)
699 0e4002ca 2020-03-21 stsp return err;
700 0e4002ca 2020-03-21 stsp }
701 0e4002ca 2020-03-21 stsp
702 4ba14133 2020-03-20 stsp return NULL;
703 93658fb9 2020-03-18 stsp }
704 93658fb9 2020-03-18 stsp
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)
707 f826addf 2020-03-18 stsp {
708 f826addf 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
709 531c3985 2020-03-18 stsp }
710 531c3985 2020-03-18 stsp
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)
715 b9f99abf 2020-03-18 stsp {
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;
721 abe0f35f 2020-03-18 stsp off_t off;
722 531c3985 2020-03-18 stsp int i;
723 b9f99abf 2020-03-18 stsp
724 8f2d01a6 2020-03-18 stsp *done = 0;
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);
730 b9f99abf 2020-03-18 stsp
731 531c3985 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
732 b9f99abf 2020-03-18 stsp if (err)
733 b9f99abf 2020-03-18 stsp return err;
734 b9f99abf 2020-03-18 stsp
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);
740 abe0f35f 2020-03-18 stsp break;
741 abe0f35f 2020-03-18 stsp }
742 abe0f35f 2020-03-18 stsp if (isymrefs != NULL) {
743 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
744 abe0f35f 2020-03-18 stsp break;
745 abe0f35f 2020-03-18 stsp }
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);
754 abe0f35f 2020-03-18 stsp goto done;
755 abe0f35f 2020-03-18 stsp }
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);
761 abe0f35f 2020-03-18 stsp goto done;
762 abe0f35f 2020-03-18 stsp }
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");
766 abe0f35f 2020-03-18 stsp goto done;
767 abe0f35f 2020-03-18 stsp }
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);
773 abe0f35f 2020-03-18 stsp goto done;
774 abe0f35f 2020-03-18 stsp }
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);
779 abe0f35f 2020-03-18 stsp goto done;
780 abe0f35f 2020-03-18 stsp }
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);
784 abe0f35f 2020-03-18 stsp if (err) {
785 abe0f35f 2020-03-18 stsp free(name);
786 abe0f35f 2020-03-18 stsp free(target);
787 abe0f35f 2020-03-18 stsp goto done;
788 abe0f35f 2020-03-18 stsp }
789 abe0f35f 2020-03-18 stsp }
790 abe0f35f 2020-03-18 stsp break;
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);
794 531c3985 2020-03-18 stsp break;
795 531c3985 2020-03-18 stsp }
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");
799 b9f99abf 2020-03-18 stsp break;
800 b9f99abf 2020-03-18 stsp }
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");
806 8f2d01a6 2020-03-18 stsp break;
807 8f2d01a6 2020-03-18 stsp }
808 8f2d01a6 2020-03-18 stsp break;
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);
812 531c3985 2020-03-18 stsp break;
813 531c3985 2020-03-18 stsp }
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");
817 531c3985 2020-03-18 stsp break;
818 531c3985 2020-03-18 stsp }
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;
825 531c3985 2020-03-18 stsp goto done;
826 531c3985 2020-03-18 stsp }
827 531c3985 2020-03-18 stsp }
828 531c3985 2020-03-18 stsp break;
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);
832 d2cdc636 2020-03-18 stsp break;
833 d2cdc636 2020-03-18 stsp }
834 d2cdc636 2020-03-18 stsp memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
835 d2cdc636 2020-03-18 stsp break;
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);
839 8f2d01a6 2020-03-18 stsp break;
840 8f2d01a6 2020-03-18 stsp }
841 1d72a2a0 2020-03-24 stsp memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
842 8f2d01a6 2020-03-18 stsp *done = 1;
843 b9f99abf 2020-03-18 stsp break;
844 b9f99abf 2020-03-18 stsp default:
845 b887aab6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
846 b887aab6 2020-03-18 stsp break;
847 b9f99abf 2020-03-18 stsp }
848 abe0f35f 2020-03-18 stsp done:
849 b887aab6 2020-03-18 stsp if (err) {
850 8f2d01a6 2020-03-18 stsp free(*id);
851 8f2d01a6 2020-03-18 stsp *id = NULL;
852 b887aab6 2020-03-18 stsp free(*refname);
853 b887aab6 2020-03-18 stsp *refname = NULL;
854 f8a36e22 2021-08-26 stsp }
855 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
856 f8a36e22 2021-08-26 stsp return err;
857 f8a36e22 2021-08-26 stsp }
858 f8a36e22 2021-08-26 stsp
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)
862 f8a36e22 2021-08-26 stsp {
863 f8a36e22 2021-08-26 stsp size_t len;
864 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
865 f8a36e22 2021-08-26 stsp
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");
870 f8a36e22 2021-08-26 stsp
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");
880 f8a36e22 2021-08-26 stsp
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);
884 f8a36e22 2021-08-26 stsp }
885 f8a36e22 2021-08-26 stsp
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)
891 f8a36e22 2021-08-26 stsp {
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;
896 f8a36e22 2021-08-26 stsp
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");
908 f8a36e22 2021-08-26 stsp goto done;
909 f8a36e22 2021-08-26 stsp }
910 f8a36e22 2021-08-26 stsp
911 2161bc24 2023-11-03 op fd = -1;
912 f8a36e22 2021-08-26 stsp err = flush_imsg(ibuf);
913 f8a36e22 2021-08-26 stsp if (err)
914 f8a36e22 2021-08-26 stsp goto done;
915 f8a36e22 2021-08-26 stsp
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);
921 f8a36e22 2021-08-26 stsp if (err)
922 f8a36e22 2021-08-26 stsp goto done;
923 f8a36e22 2021-08-26 stsp }
924 f8a36e22 2021-08-26 stsp
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);
929 f8a36e22 2021-08-26 stsp if (err)
930 f8a36e22 2021-08-26 stsp goto done;
931 f8a36e22 2021-08-26 stsp }
932 f8a36e22 2021-08-26 stsp done:
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;
936 f8a36e22 2021-08-26 stsp }
937 f8a36e22 2021-08-26 stsp
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)
941 f8a36e22 2021-08-26 stsp {
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;
950 f8a36e22 2021-08-26 stsp
951 f8a36e22 2021-08-26 stsp while (!done) {
952 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
953 f8a36e22 2021-08-26 stsp if (err)
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);
960 f8a36e22 2021-08-26 stsp goto done;
961 f8a36e22 2021-08-26 stsp }
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);
966 f8a36e22 2021-08-26 stsp goto done;
967 f8a36e22 2021-08-26 stsp }
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");
971 f8a36e22 2021-08-26 stsp goto done;
972 f8a36e22 2021-08-26 stsp }
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");
978 f8a36e22 2021-08-26 stsp goto done;
979 f8a36e22 2021-08-26 stsp }
980 f8a36e22 2021-08-26 stsp err = got_pathlist_insert(&new, remote_refs,
981 f8a36e22 2021-08-26 stsp refname, id);
982 f8a36e22 2021-08-26 stsp if (err)
983 f8a36e22 2021-08-26 stsp goto done;
984 f8a36e22 2021-08-26 stsp if (new == NULL) { /* duplicate which wasn't inserted */
985 f8a36e22 2021-08-26 stsp free(id);
986 f8a36e22 2021-08-26 stsp free(refname);
987 f8a36e22 2021-08-26 stsp }
988 f8a36e22 2021-08-26 stsp id = NULL;
989 f8a36e22 2021-08-26 stsp refname = NULL;
990 f8a36e22 2021-08-26 stsp break;
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);
994 f8a36e22 2021-08-26 stsp goto done;
995 f8a36e22 2021-08-26 stsp }
996 f8a36e22 2021-08-26 stsp /* got-send-pack is now waiting for a pack file. */
997 f8a36e22 2021-08-26 stsp done = 1;
998 f8a36e22 2021-08-26 stsp break;
999 f8a36e22 2021-08-26 stsp default:
1000 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1001 f8a36e22 2021-08-26 stsp break;
1002 f8a36e22 2021-08-26 stsp }
1003 b887aab6 2020-03-18 stsp }
1004 f8a36e22 2021-08-26 stsp done:
1005 f8a36e22 2021-08-26 stsp free(id);
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;
1009 93658fb9 2020-03-18 stsp }
1010 93658fb9 2020-03-18 stsp
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)
1013 f8a36e22 2021-08-26 stsp {
1014 f8a36e22 2021-08-26 stsp return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
1015 f8a36e22 2021-08-26 stsp }
1016 f8a36e22 2021-08-26 stsp
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)
1020 f8a36e22 2021-08-26 stsp {
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;
1025 f8a36e22 2021-08-26 stsp
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;
1031 f8a36e22 2021-08-26 stsp
1032 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1033 f8a36e22 2021-08-26 stsp if (err)
1034 f8a36e22 2021-08-26 stsp return err;
1035 f8a36e22 2021-08-26 stsp
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);
1041 f8a36e22 2021-08-26 stsp break;
1042 f8a36e22 2021-08-26 stsp }
1043 f8a36e22 2021-08-26 stsp memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1044 f8a36e22 2021-08-26 stsp break;
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);
1048 f8a36e22 2021-08-26 stsp break;
1049 f8a36e22 2021-08-26 stsp }
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);
1054 f8a36e22 2021-08-26 stsp break;
1055 f8a36e22 2021-08-26 stsp }
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);
1059 6242c45b 2022-11-14 op
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);
1063 f8a36e22 2021-08-26 stsp break;
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);
1067 f8a36e22 2021-08-26 stsp break;
1068 f8a36e22 2021-08-26 stsp }
1069 f8a36e22 2021-08-26 stsp *done = 1;
1070 f8a36e22 2021-08-26 stsp break;
1071 f8a36e22 2021-08-26 stsp default:
1072 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1073 f8a36e22 2021-08-26 stsp break;
1074 f8a36e22 2021-08-26 stsp }
1075 f8a36e22 2021-08-26 stsp
1076 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
1077 f8a36e22 2021-08-26 stsp return err;
1078 f8a36e22 2021-08-26 stsp }
1079 f8a36e22 2021-08-26 stsp
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,
1082 668a20f6 2020-03-18 stsp int fd)
1083 93658fb9 2020-03-18 stsp {
1084 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
1085 93658fb9 2020-03-18 stsp
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;
1092 93658fb9 2020-03-18 stsp }
1093 baa9fea0 2020-03-18 stsp return flush_imsg(ibuf);
1094 baa9fea0 2020-03-18 stsp }
1095 baa9fea0 2020-03-18 stsp
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)
1098 73ab1060 2020-03-18 stsp {
1099 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1100 73ab1060 2020-03-18 stsp }
1101 73ab1060 2020-03-18 stsp
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)
1106 93658fb9 2020-03-18 stsp {
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;
1111 93658fb9 2020-03-18 stsp
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;
1116 baa9fea0 2020-03-18 stsp
1117 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1118 93658fb9 2020-03-18 stsp if (err)
1119 93658fb9 2020-03-18 stsp return err;
1120 baa9fea0 2020-03-18 stsp
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);
1126 baa9fea0 2020-03-18 stsp break;
1127 baa9fea0 2020-03-18 stsp }
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);
1132 c98b0f0b 2022-06-14 op break;
1133 c98b0f0b 2022-06-14 op }
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;
1138 baa9fea0 2020-03-18 stsp break;
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);
1142 baa9fea0 2020-03-18 stsp break;
1143 baa9fea0 2020-03-18 stsp }
1144 baa9fea0 2020-03-18 stsp *done = 1;
1145 baa9fea0 2020-03-18 stsp break;
1146 baa9fea0 2020-03-18 stsp default:
1147 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1148 baa9fea0 2020-03-18 stsp break;
1149 baa9fea0 2020-03-18 stsp }
1150 baa9fea0 2020-03-18 stsp
1151 93658fb9 2020-03-18 stsp imsg_free(&imsg);
1152 baa9fea0 2020-03-18 stsp return err;
1153 93658fb9 2020-03-18 stsp }
1154 93658fb9 2020-03-18 stsp
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)
1158 cfd633c2 2018-09-10 stsp {
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;
1161 cfd633c2 2018-09-10 stsp
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;
1165 cfd633c2 2018-09-10 stsp
1166 c98b0f0b 2022-06-14 op if (iobj->pack_offset < 0)
1167 c98b0f0b 2022-06-14 op return got_error(GOT_ERR_PACK_OFFSET);
1168 c98b0f0b 2022-06-14 op
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");
1172 cfd633c2 2018-09-10 stsp
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;
1182 876c234b 2018-09-10 stsp }
1183 d1b988e3 2022-01-26 stsp STAILQ_INIT(&(*obj)->deltas.entries);
1184 9296b03b 2022-01-26 stsp return NULL;
1185 876c234b 2018-09-10 stsp }
1186 876c234b 2018-09-10 stsp
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)
1189 2178c42e 2018-04-22 stsp {
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));
1194 2178c42e 2018-04-22 stsp
1195 2178c42e 2018-04-22 stsp *obj = NULL;
1196 2178c42e 2018-04-22 stsp
1197 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1198 2178c42e 2018-04-22 stsp if (err)
1199 2178c42e 2018-04-22 stsp return err;
1200 2178c42e 2018-04-22 stsp
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);
1204 bff6ca00 2018-04-23 stsp break;
1205 bff6ca00 2018-04-23 stsp default:
1206 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1207 bff6ca00 2018-04-23 stsp break;
1208 bff6ca00 2018-04-23 stsp }
1209 bff6ca00 2018-04-23 stsp
1210 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
1211 bff6ca00 2018-04-23 stsp
1212 bff6ca00 2018-04-23 stsp return err;
1213 c75f7264 2018-09-11 stsp }
1214 c75f7264 2018-09-11 stsp
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)
1218 c75f7264 2018-09-11 stsp {
1219 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
1220 c75f7264 2018-09-11 stsp size_t offset, remain;
1221 c75f7264 2018-09-11 stsp
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);
1226 c75f7264 2018-09-11 stsp
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");
1231 fa4ffeb3 2018-11-04 stsp break;
1232 fa4ffeb3 2018-11-04 stsp }
1233 c75f7264 2018-09-11 stsp
1234 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
1235 fa4ffeb3 2018-11-04 stsp if (err)
1236 fa4ffeb3 2018-11-04 stsp break;
1237 c75f7264 2018-09-11 stsp
1238 c75f7264 2018-09-11 stsp offset += n;
1239 c75f7264 2018-09-11 stsp remain -= n;
1240 c75f7264 2018-09-11 stsp }
1241 c75f7264 2018-09-11 stsp
1242 fa4ffeb3 2018-11-04 stsp return err;
1243 bff6ca00 2018-04-23 stsp }
1244 bff6ca00 2018-04-23 stsp
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)
1247 bff6ca00 2018-04-23 stsp {
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);
1256 bff6ca00 2018-04-23 stsp
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);
1259 bff6ca00 2018-04-23 stsp
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");
1263 bff6ca00 2018-04-23 stsp
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;
1274 b9c33926 2018-11-07 stsp
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);
1283 bff6ca00 2018-04-23 stsp }
1284 bff6ca00 2018-04-23 stsp
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;
1288 bff6ca00 2018-04-23 stsp }
1289 bff6ca00 2018-04-23 stsp
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);
1293 904df868 2018-11-04 stsp if (err)
1294 904df868 2018-11-04 stsp goto done;
1295 904df868 2018-11-04 stsp }
1296 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
1297 bff6ca00 2018-04-23 stsp done:
1298 bff6ca00 2018-04-23 stsp free(buf);
1299 bff6ca00 2018-04-23 stsp return err;
1300 bff6ca00 2018-04-23 stsp }
1301 cfd633c2 2018-09-10 stsp
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)
1305 bff6ca00 2018-04-23 stsp {
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;
1309 bff6ca00 2018-04-23 stsp int i;
1310 bff6ca00 2018-04-23 stsp
1311 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
1312 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1313 bff6ca00 2018-04-23 stsp
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);
1319 bff6ca00 2018-04-23 stsp
1320 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
1321 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1322 ca6e02ac 2020-01-07 stsp
1323 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
1324 bff6ca00 2018-04-23 stsp
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");
1329 ca6e02ac 2020-01-07 stsp
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;
1336 ca6e02ac 2020-01-07 stsp
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");
1340 bf31a716 2022-06-14 op goto done;
1341 ca6e02ac 2020-01-07 stsp }
1342 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
1343 bff6ca00 2018-04-23 stsp
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");
1348 bf31a716 2022-06-14 op goto done;
1349 ca6e02ac 2020-01-07 stsp }
1350 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
1351 ca6e02ac 2020-01-07 stsp
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;
1357 ca6e02ac 2020-01-07 stsp }
1358 ca6e02ac 2020-01-07 stsp } else {
1359 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
1360 ca6e02ac 2020-01-07 stsp
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;
1365 bff6ca00 2018-04-23 stsp }
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,
1369 ca6e02ac 2020-01-07 stsp remain);
1370 6c281f94 2018-06-11 stsp
1371 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1372 ca6e02ac 2020-01-07 stsp if (err)
1373 ca6e02ac 2020-01-07 stsp goto done;
1374 bff6ca00 2018-04-23 stsp
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;
1378 bff6ca00 2018-04-23 stsp }
1379 c75f7264 2018-09-11 stsp
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;
1385 bff6ca00 2018-04-23 stsp }
1386 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
1387 ca6e02ac 2020-01-07 stsp }
1388 bff6ca00 2018-04-23 stsp
1389 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
1390 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1391 86acc566 2018-04-23 stsp
1392 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1393 ca6e02ac 2020-01-07 stsp if (err)
1394 ca6e02ac 2020-01-07 stsp break;
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++;
1399 ca6e02ac 2020-01-07 stsp }
1400 ca6e02ac 2020-01-07 stsp done:
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;
1404 ca6e02ac 2020-01-07 stsp }
1405 ca6e02ac 2020-01-07 stsp return err;
1406 ca6e02ac 2020-01-07 stsp }
1407 ca6e02ac 2020-01-07 stsp
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)
1410 ca6e02ac 2020-01-07 stsp {
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));
1417 ca6e02ac 2020-01-07 stsp
1418 ca6e02ac 2020-01-07 stsp *commit = NULL;
1419 ca6e02ac 2020-01-07 stsp
1420 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1421 ca6e02ac 2020-01-07 stsp if (err)
1422 ca6e02ac 2020-01-07 stsp return err;
1423 ca6e02ac 2020-01-07 stsp
1424 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1425 ca6e02ac 2020-01-07 stsp
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);
1429 2178c42e 2018-04-22 stsp break;
1430 8c580685 2018-04-22 stsp default:
1431 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1432 8c580685 2018-04-22 stsp break;
1433 2178c42e 2018-04-22 stsp }
1434 2178c42e 2018-04-22 stsp
1435 2178c42e 2018-04-22 stsp imsg_free(&imsg);
1436 e033d803 2018-04-23 stsp
1437 e033d803 2018-04-23 stsp return err;
1438 e033d803 2018-04-23 stsp }
1439 e033d803 2018-04-23 stsp
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)
1443 e033d803 2018-04-23 stsp {
1444 33fd69c2 2022-05-19 stsp struct ibuf *wbuf;
1445 33fd69c2 2022-05-19 stsp struct got_imsg_tree_entries ientries;
1446 33fd69c2 2022-05-19 stsp int i;
1447 e033d803 2018-04-23 stsp
1448 031611fc 2022-06-18 op memset(&ientries, 0, sizeof(ientries));
1449 031611fc 2022-06-18 op
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");
1453 e033d803 2018-04-23 stsp
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");
1457 e033d803 2018-04-23 stsp
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];
1460 e033d803 2018-04-23 stsp
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");
1468 b4b1b9c0 2022-06-14 op
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");
1472 33fd69c2 2022-05-19 stsp }
1473 3022d272 2019-11-14 stsp
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;
1477 33fd69c2 2022-05-19 stsp }
1478 3022d272 2019-11-14 stsp
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)
1482 33fd69c2 2022-05-19 stsp {
1483 33fd69c2 2022-05-19 stsp const struct got_error *err = NULL;
1484 33fd69c2 2022-05-19 stsp int i, j;
1485 0ab4c957 2022-06-13 stsp size_t entries_len = sizeof(struct got_imsg_tree_entries);
1486 33fd69c2 2022-05-19 stsp
1487 33fd69c2 2022-05-19 stsp i = 0;
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;
1491 33fd69c2 2022-05-19 stsp
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);
1496 33fd69c2 2022-05-19 stsp if (err)
1497 33fd69c2 2022-05-19 stsp return err;
1498 33fd69c2 2022-05-19 stsp i = j;
1499 33fd69c2 2022-05-19 stsp entries_len = sizeof(struct got_imsg_tree_entries);
1500 33fd69c2 2022-05-19 stsp }
1501 33fd69c2 2022-05-19 stsp
1502 33fd69c2 2022-05-19 stsp entries_len += len;
1503 e033d803 2018-04-23 stsp }
1504 e033d803 2018-04-23 stsp
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);
1508 33fd69c2 2022-05-19 stsp if (err)
1509 33fd69c2 2022-05-19 stsp return err;
1510 33fd69c2 2022-05-19 stsp }
1511 cee6a7ea 2022-06-07 stsp
1512 0ab4c957 2022-06-13 stsp return NULL;
1513 0ab4c957 2022-06-13 stsp }
1514 0ab4c957 2022-06-13 stsp
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)
1518 0ab4c957 2022-06-13 stsp {
1519 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1520 0ab4c957 2022-06-13 stsp struct got_imsg_tree_object itree;
1521 0ab4c957 2022-06-13 stsp
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))
1525 0ab4c957 2022-06-13 stsp == -1)
1526 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_compose TREE");
1527 0ab4c957 2022-06-13 stsp
1528 0ab4c957 2022-06-13 stsp err = send_tree_entries(ibuf, entries, nentries);
1529 0ab4c957 2022-06-13 stsp if (err)
1530 0ab4c957 2022-06-13 stsp return err;
1531 0ab4c957 2022-06-13 stsp
1532 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
1533 0ab4c957 2022-06-13 stsp }
1534 0ab4c957 2022-06-13 stsp
1535 0ab4c957 2022-06-13 stsp
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)
1539 0ab4c957 2022-06-13 stsp {
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;
1544 0ab4c957 2022-06-13 stsp size_t i;
1545 0ab4c957 2022-06-13 stsp
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);
1549 0ab4c957 2022-06-13 stsp
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");
1554 0ab4c957 2022-06-13 stsp }
1555 0ab4c957 2022-06-13 stsp
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;
1561 0ab4c957 2022-06-13 stsp
1562 0ab4c957 2022-06-13 stsp if (te_offset >= datalen) {
1563 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1564 0ab4c957 2022-06-13 stsp break;
1565 0ab4c957 2022-06-13 stsp }
1566 0ab4c957 2022-06-13 stsp
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));
1569 0ab4c957 2022-06-13 stsp
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);
1572 0ab4c957 2022-06-13 stsp break;
1573 0ab4c957 2022-06-13 stsp }
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);
1576 0ab4c957 2022-06-13 stsp break;
1577 0ab4c957 2022-06-13 stsp }
1578 0ab4c957 2022-06-13 stsp
1579 0ab4c957 2022-06-13 stsp if (*nentries >= tree->nentries) {
1580 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1581 0ab4c957 2022-06-13 stsp break;
1582 0ab4c957 2022-06-13 stsp }
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)++;
1591 0ab4c957 2022-06-13 stsp
1592 0ab4c957 2022-06-13 stsp te_offset += sizeof(ite) + ite.namelen;
1593 0ab4c957 2022-06-13 stsp }
1594 0ab4c957 2022-06-13 stsp
1595 0ab4c957 2022-06-13 stsp return err;
1596 cee6a7ea 2022-06-07 stsp }
1597 cee6a7ea 2022-06-07 stsp
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)
1600 e033d803 2018-04-23 stsp {
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;
1607 2178c42e 2018-04-22 stsp
1608 e033d803 2018-04-23 stsp *tree = NULL;
1609 7429ae85 2022-05-18 stsp
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;
1613 e033d803 2018-04-23 stsp
1614 80985c16 2022-07-07 op err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1615 80985c16 2022-07-07 op if (err)
1616 fca1f6ad 2020-08-27 stsp break;
1617 e033d803 2018-04-23 stsp
1618 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1619 e033d803 2018-04-23 stsp
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);
1625 e033d803 2018-04-23 stsp break;
1626 e033d803 2018-04-23 stsp }
1627 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
1628 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1629 e033d803 2018-04-23 stsp break;
1630 e033d803 2018-04-23 stsp }
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);
1634 13fcf1bf 2022-05-18 stsp break;
1635 13fcf1bf 2022-05-18 stsp }
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");
1639 e033d803 2018-04-23 stsp break;
1640 e033d803 2018-04-23 stsp }
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;
1647 56e0773d 2019-11-28 stsp break;
1648 56e0773d 2019-11-28 stsp }
1649 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1650 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1651 e033d803 2018-04-23 stsp break;
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);
1656 e033d803 2018-04-23 stsp break;
1657 e033d803 2018-04-23 stsp }
1658 0ab4c957 2022-06-13 stsp err = recv_tree_entries(imsg.data, datalen,
1659 0ab4c957 2022-06-13 stsp *tree, &nentries);
1660 e033d803 2018-04-23 stsp break;
1661 e033d803 2018-04-23 stsp default:
1662 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1663 e033d803 2018-04-23 stsp break;
1664 e033d803 2018-04-23 stsp }
1665 e033d803 2018-04-23 stsp
1666 e033d803 2018-04-23 stsp imsg_free(&imsg);
1667 d6b7d054 2020-08-27 stsp if (err)
1668 d6b7d054 2020-08-27 stsp break;
1669 e033d803 2018-04-23 stsp }
1670 80985c16 2022-07-07 op
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;
1676 ff6b18f8 2018-04-24 stsp }
1677 ff6b18f8 2018-04-24 stsp
1678 ff6b18f8 2018-04-24 stsp return err;
1679 ff6b18f8 2018-04-24 stsp }
1680 ff6b18f8 2018-04-24 stsp
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)
1684 ff6b18f8 2018-04-24 stsp {
1685 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1686 2967a784 2018-04-24 stsp
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;
1690 2967a784 2018-04-24 stsp
1691 ac544f8c 2019-01-13 stsp if (data) {
1692 ac544f8c 2019-01-13 stsp uint8_t *buf;
1693 ac544f8c 2019-01-13 stsp
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);
1696 ac544f8c 2019-01-13 stsp
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");
1700 ff6b18f8 2018-04-24 stsp
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");
1707 ac544f8c 2019-01-13 stsp }
1708 ac544f8c 2019-01-13 stsp free(buf);
1709 ac544f8c 2019-01-13 stsp } else {
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");
1714 ac544f8c 2019-01-13 stsp }
1715 ac544f8c 2019-01-13 stsp
1716 ac544f8c 2019-01-13 stsp
1717 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1718 ff6b18f8 2018-04-24 stsp }
1719 ff6b18f8 2018-04-24 stsp
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)
1723 ff6b18f8 2018-04-24 stsp {
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;
1728 ff6b18f8 2018-04-24 stsp
1729 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1730 ac544f8c 2019-01-13 stsp
1731 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1732 ff6b18f8 2018-04-24 stsp if (err)
1733 ff6b18f8 2018-04-24 stsp return err;
1734 ff6b18f8 2018-04-24 stsp
1735 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1736 ff6b18f8 2018-04-24 stsp
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);
1741 18336eed 2018-11-04 stsp break;
1742 18336eed 2018-11-04 stsp }
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;
1746 ac544f8c 2019-01-13 stsp
1747 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1748 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1749 ac544f8c 2019-01-13 stsp break;
1750 ac544f8c 2019-01-13 stsp }
1751 ac544f8c 2019-01-13 stsp
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);
1755 ac544f8c 2019-01-13 stsp break;
1756 ac544f8c 2019-01-13 stsp }
1757 ac544f8c 2019-01-13 stsp
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");
1761 ac544f8c 2019-01-13 stsp break;
1762 ac544f8c 2019-01-13 stsp }
1763 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1764 f4a881ce 2018-11-17 stsp break;
1765 f4a881ce 2018-11-17 stsp default:
1766 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1767 f4a881ce 2018-11-17 stsp break;
1768 f4a881ce 2018-11-17 stsp }
1769 f4a881ce 2018-11-17 stsp
1770 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1771 f4a881ce 2018-11-17 stsp
1772 f4a881ce 2018-11-17 stsp return err;
1773 f4a881ce 2018-11-17 stsp }
1774 f4a881ce 2018-11-17 stsp
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)
1777 f4a881ce 2018-11-17 stsp {
1778 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1779 f4a881ce 2018-11-17 stsp size_t offset, remain;
1780 f4a881ce 2018-11-17 stsp
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);
1785 f4a881ce 2018-11-17 stsp
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");
1789 f4a881ce 2018-11-17 stsp break;
1790 f4a881ce 2018-11-17 stsp }
1791 f4a881ce 2018-11-17 stsp
1792 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1793 f4a881ce 2018-11-17 stsp if (err)
1794 f4a881ce 2018-11-17 stsp break;
1795 f4a881ce 2018-11-17 stsp
1796 f4a881ce 2018-11-17 stsp offset += n;
1797 f4a881ce 2018-11-17 stsp remain -= n;
1798 f4a881ce 2018-11-17 stsp }
1799 f4a881ce 2018-11-17 stsp
1800 f4a881ce 2018-11-17 stsp return err;
1801 f4a881ce 2018-11-17 stsp }
1802 f4a881ce 2018-11-17 stsp
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)
1805 f4a881ce 2018-11-17 stsp {
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);
1813 f4a881ce 2018-11-17 stsp
1814 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1815 f4a881ce 2018-11-17 stsp
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");
1819 f4a881ce 2018-11-17 stsp
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;
1828 f4a881ce 2018-11-17 stsp
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;
1834 f4a881ce 2018-11-17 stsp
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;
1838 f4a881ce 2018-11-17 stsp }
1839 f4a881ce 2018-11-17 stsp
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);
1843 f4a881ce 2018-11-17 stsp if (err)
1844 f4a881ce 2018-11-17 stsp goto done;
1845 f4a881ce 2018-11-17 stsp }
1846 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1847 f4a881ce 2018-11-17 stsp done:
1848 f4a881ce 2018-11-17 stsp free(buf);
1849 f4a881ce 2018-11-17 stsp return err;
1850 f4a881ce 2018-11-17 stsp }
1851 f4a881ce 2018-11-17 stsp
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)
1854 f4a881ce 2018-11-17 stsp {
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));
1862 f4a881ce 2018-11-17 stsp
1863 f4a881ce 2018-11-17 stsp *tag = NULL;
1864 f4a881ce 2018-11-17 stsp
1865 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1866 f4a881ce 2018-11-17 stsp if (err)
1867 f4a881ce 2018-11-17 stsp return err;
1868 f4a881ce 2018-11-17 stsp
1869 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1870 f4a881ce 2018-11-17 stsp len = 0;
1871 f4a881ce 2018-11-17 stsp
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);
1876 f4a881ce 2018-11-17 stsp break;
1877 f4a881ce 2018-11-17 stsp }
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);
1882 f4a881ce 2018-11-17 stsp break;
1883 f4a881ce 2018-11-17 stsp }
1884 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1885 f4a881ce 2018-11-17 stsp
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");
1889 f4a881ce 2018-11-17 stsp break;
1890 f4a881ce 2018-11-17 stsp }
1891 f4a881ce 2018-11-17 stsp
1892 076fbedc 2023-02-19 op memcpy(&(*tag)->id, &itag->id, sizeof(itag->id));
1893 f4a881ce 2018-11-17 stsp
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");
1897 bf31a716 2022-06-14 op break;
1898 f4a881ce 2018-11-17 stsp }
1899 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1900 f4a881ce 2018-11-17 stsp
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;
1904 f4a881ce 2018-11-17 stsp
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");
1908 bf31a716 2022-06-14 op break;
1909 f4a881ce 2018-11-17 stsp }
1910 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1911 f4a881ce 2018-11-17 stsp
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");
1916 f4a881ce 2018-11-17 stsp break;
1917 f4a881ce 2018-11-17 stsp }
1918 f4a881ce 2018-11-17 stsp } else {
1919 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1920 f4a881ce 2018-11-17 stsp
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");
1924 f4a881ce 2018-11-17 stsp break;
1925 f4a881ce 2018-11-17 stsp }
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,
1929 f4a881ce 2018-11-17 stsp remain);
1930 f4a881ce 2018-11-17 stsp
1931 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1932 f4a881ce 2018-11-17 stsp if (err)
1933 f4a881ce 2018-11-17 stsp return err;
1934 f4a881ce 2018-11-17 stsp
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);
1937 f4a881ce 2018-11-17 stsp
1938 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1939 f4a881ce 2018-11-17 stsp n);
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;
1943 f4a881ce 2018-11-17 stsp }
1944 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1945 f4a881ce 2018-11-17 stsp }
1946 f4a881ce 2018-11-17 stsp
1947 ff6b18f8 2018-04-24 stsp break;
1948 ff6b18f8 2018-04-24 stsp default:
1949 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1950 ff6b18f8 2018-04-24 stsp break;
1951 e033d803 2018-04-23 stsp }
1952 e033d803 2018-04-23 stsp
1953 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1954 ff6b18f8 2018-04-24 stsp
1955 2178c42e 2018-04-22 stsp return err;
1956 2178c42e 2018-04-22 stsp }
1957 876c234b 2018-09-10 stsp
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)
1961 876c234b 2018-09-10 stsp {
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;
1965 876c234b 2018-09-10 stsp int fd;
1966 876c234b 2018-09-10 stsp
1967 031611fc 2022-06-18 op memset(&ipackidx, 0, sizeof(ipackidx));
1968 031611fc 2022-06-18 op memset(&ipack, 0, sizeof(ipack));
1969 031611fc 2022-06-18 op
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");
1975 876c234b 2018-09-10 stsp
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;
1981 41496140 2019-02-21 stsp }
1982 876c234b 2018-09-10 stsp
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;
1987 876c234b 2018-09-10 stsp
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");
1991 876c234b 2018-09-10 stsp
1992 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1993 41496140 2019-02-21 stsp == -1) {
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;
1997 41496140 2019-02-21 stsp }
1998 876c234b 2018-09-10 stsp
1999 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
2000 876c234b 2018-09-10 stsp }
2001 876c234b 2018-09-10 stsp
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)
2005 876c234b 2018-09-10 stsp {
2006 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
2007 876c234b 2018-09-10 stsp
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));
2011 876c234b 2018-09-10 stsp
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");
2016 aba9c984 2019-09-08 stsp
2017 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2018 aba9c984 2019-09-08 stsp }
2019 aba9c984 2019-09-08 stsp
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)
2023 59d1e4a0 2021-03-10 stsp {
2024 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
2025 59d1e4a0 2021-03-10 stsp
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));
2029 59d1e4a0 2021-03-10 stsp
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");
2034 59d1e4a0 2021-03-10 stsp
2035 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
2036 59d1e4a0 2021-03-10 stsp }
2037 59d1e4a0 2021-03-10 stsp
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)
2040 aba9c984 2019-09-08 stsp {
2041 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
2042 aba9c984 2019-09-08 stsp
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;
2049 aba9c984 2019-09-08 stsp }
2050 aba9c984 2019-09-08 stsp
2051 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2052 aba9c984 2019-09-08 stsp }
2053 aba9c984 2019-09-08 stsp
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)
2056 aba9c984 2019-09-08 stsp {
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");
2062 aba9c984 2019-09-08 stsp
2063 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2064 aba9c984 2019-09-08 stsp }
2065 aba9c984 2019-09-08 stsp
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)
2068 20b7abb3 2020-10-22 stsp {
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");
2074 20b7abb3 2020-10-22 stsp
2075 20b7abb3 2020-10-22 stsp return flush_imsg(ibuf);
2076 20b7abb3 2020-10-22 stsp }
2077 20b7abb3 2020-10-22 stsp
2078 20b7abb3 2020-10-22 stsp
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)
2081 aba9c984 2019-09-08 stsp {
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");
2086 aba9c984 2019-09-08 stsp
2087 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2088 aba9c984 2019-09-08 stsp }
2089 aba9c984 2019-09-08 stsp
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)
2092 aba9c984 2019-09-08 stsp {
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");
2097 cd95becd 2019-11-29 stsp
2098 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
2099 cd95becd 2019-11-29 stsp }
2100 cd95becd 2019-11-29 stsp
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)
2103 cd95becd 2019-11-29 stsp {
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");
2108 aba9c984 2019-09-08 stsp
2109 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
2110 aba9c984 2019-09-08 stsp }
2111 aba9c984 2019-09-08 stsp
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)
2114 9a1cc63f 2020-02-03 stsp {
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");
2119 9a1cc63f 2020-02-03 stsp
2120 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
2121 9a1cc63f 2020-02-03 stsp }
2122 9a1cc63f 2020-02-03 stsp
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)
2125 aba9c984 2019-09-08 stsp {
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;
2129 aba9c984 2019-09-08 stsp
2130 aba9c984 2019-09-08 stsp *str = NULL;
2131 aba9c984 2019-09-08 stsp
2132 3e6054c4 2022-06-14 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2133 aba9c984 2019-09-08 stsp if (err)
2134 aba9c984 2019-09-08 stsp return err;
2135 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2136 aba9c984 2019-09-08 stsp
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)
2140 aba9c984 2019-09-08 stsp break;
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");
2144 aba9c984 2019-09-08 stsp break;
2145 aba9c984 2019-09-08 stsp }
2146 aba9c984 2019-09-08 stsp break;
2147 aba9c984 2019-09-08 stsp default:
2148 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2149 aba9c984 2019-09-08 stsp break;
2150 27749ea2 2023-02-05 op }
2151 27749ea2 2023-02-05 op
2152 27749ea2 2023-02-05 op imsg_free(&imsg);
2153 27749ea2 2023-02-05 op return err;
2154 27749ea2 2023-02-05 op }
2155 27749ea2 2023-02-05 op
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)
2158 27749ea2 2023-02-05 op {
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;
2164 27749ea2 2023-02-05 op
2165 27749ea2 2023-02-05 op *key = *val = NULL;
2166 27749ea2 2023-02-05 op
2167 27749ea2 2023-02-05 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2168 27749ea2 2023-02-05 op if (err)
2169 27749ea2 2023-02-05 op return err;
2170 27749ea2 2023-02-05 op
2171 27749ea2 2023-02-05 op data = imsg.data;
2172 27749ea2 2023-02-05 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2173 27749ea2 2023-02-05 op
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);
2176 27749ea2 2023-02-05 op goto done;
2177 27749ea2 2023-02-05 op }
2178 27749ea2 2023-02-05 op
2179 27749ea2 2023-02-05 op if (datalen < sizeof(p)) {
2180 27749ea2 2023-02-05 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2181 27749ea2 2023-02-05 op goto done;
2182 27749ea2 2023-02-05 op }
2183 27749ea2 2023-02-05 op
2184 27749ea2 2023-02-05 op memcpy(&p, data, sizeof(p));
2185 27749ea2 2023-02-05 op data += sizeof(p);
2186 27749ea2 2023-02-05 op
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);
2189 27749ea2 2023-02-05 op goto done;
2190 27749ea2 2023-02-05 op }
2191 27749ea2 2023-02-05 op
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");
2195 27749ea2 2023-02-05 op goto done;
2196 27749ea2 2023-02-05 op }
2197 27749ea2 2023-02-05 op data += p.klen;
2198 27749ea2 2023-02-05 op
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");
2202 27749ea2 2023-02-05 op goto done;
2203 aba9c984 2019-09-08 stsp }
2204 876c234b 2018-09-10 stsp
2205 27749ea2 2023-02-05 op done:
2206 aba9c984 2019-09-08 stsp imsg_free(&imsg);
2207 aba9c984 2019-09-08 stsp return err;
2208 aba9c984 2019-09-08 stsp }
2209 aba9c984 2019-09-08 stsp
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)
2212 aba9c984 2019-09-08 stsp {
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));
2218 aba9c984 2019-09-08 stsp
2219 aba9c984 2019-09-08 stsp *val = 0;
2220 aba9c984 2019-09-08 stsp
2221 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2222 aba9c984 2019-09-08 stsp if (err)
2223 aba9c984 2019-09-08 stsp return err;
2224 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2225 aba9c984 2019-09-08 stsp
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);
2230 aba9c984 2019-09-08 stsp break;
2231 aba9c984 2019-09-08 stsp }
2232 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
2233 cd95becd 2019-11-29 stsp break;
2234 cd95becd 2019-11-29 stsp default:
2235 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2236 cd95becd 2019-11-29 stsp break;
2237 cd95becd 2019-11-29 stsp }
2238 cd95becd 2019-11-29 stsp
2239 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2240 cd95becd 2019-11-29 stsp return err;
2241 b8adfa55 2020-09-25 stsp }
2242 b8adfa55 2020-09-25 stsp
2243 b8adfa55 2020-09-25 stsp static void
2244 b8adfa55 2020-09-25 stsp free_remote_data(struct got_remote_repo *remote)
2245 b8adfa55 2020-09-25 stsp {
2246 b8adfa55 2020-09-25 stsp int i;
2247 b8adfa55 2020-09-25 stsp
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);
2260 cd95becd 2019-11-29 stsp }
2261 cd95becd 2019-11-29 stsp
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)
2265 cd95becd 2019-11-29 stsp {
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;
2271 cd95becd 2019-11-29 stsp
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;
2275 cd95becd 2019-11-29 stsp
2276 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2277 cd95becd 2019-11-29 stsp if (err)
2278 cd95becd 2019-11-29 stsp return err;
2279 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2280 cd95becd 2019-11-29 stsp
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);
2285 cd95becd 2019-11-29 stsp break;
2286 cd95becd 2019-11-29 stsp }
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;
2291 cd95becd 2019-11-29 stsp }
2292 aba9c984 2019-09-08 stsp break;
2293 aba9c984 2019-09-08 stsp default:
2294 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
2295 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2296 aba9c984 2019-09-08 stsp }
2297 aba9c984 2019-09-08 stsp
2298 aba9c984 2019-09-08 stsp imsg_free(&imsg);
2299 cd95becd 2019-11-29 stsp
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");
2303 cd95becd 2019-11-29 stsp
2304 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
2305 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
2306 3168e5da 2020-09-10 stsp
2307 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2308 cd95becd 2019-11-29 stsp if (err)
2309 cd95becd 2019-11-29 stsp break;
2310 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2311 cd95becd 2019-11-29 stsp
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);
2318 cd95becd 2019-11-29 stsp break;
2319 cd95becd 2019-11-29 stsp }
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);
2327 cd95becd 2019-11-29 stsp break;
2328 cd95becd 2019-11-29 stsp }
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");
2333 cd95becd 2019-11-29 stsp break;
2334 cd95becd 2019-11-29 stsp }
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);
2340 cd95becd 2019-11-29 stsp break;
2341 cd95becd 2019-11-29 stsp }
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);
2348 6480c871 2021-08-30 stsp break;
2349 6480c871 2021-08-30 stsp }
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)++;
2359 cd95becd 2019-11-29 stsp break;
2360 cd95becd 2019-11-29 stsp default:
2361 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2362 cd95becd 2019-11-29 stsp break;
2363 cd95becd 2019-11-29 stsp }
2364 cd95becd 2019-11-29 stsp
2365 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2366 cd95becd 2019-11-29 stsp if (err)
2367 cd95becd 2019-11-29 stsp break;
2368 cd95becd 2019-11-29 stsp }
2369 cd95becd 2019-11-29 stsp
2370 cd95becd 2019-11-29 stsp if (err) {
2371 cd95becd 2019-11-29 stsp int i;
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;
2377 cd95becd 2019-11-29 stsp }
2378 aba9c984 2019-09-08 stsp return err;
2379 257add31 2020-09-09 stsp }
2380 257add31 2020-09-09 stsp
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)
2383 257add31 2020-09-09 stsp {
2384 257add31 2020-09-09 stsp const struct got_error *err = NULL;
2385 257add31 2020-09-09 stsp
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;
2392 257add31 2020-09-09 stsp }
2393 257add31 2020-09-09 stsp
2394 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2395 257add31 2020-09-09 stsp }
2396 257add31 2020-09-09 stsp
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)
2399 257add31 2020-09-09 stsp {
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");
2404 4d5ee956 2022-07-02 jrick
2405 4d5ee956 2022-07-02 jrick return flush_imsg(ibuf);
2406 4d5ee956 2022-07-02 jrick }
2407 4d5ee956 2022-07-02 jrick
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)
2410 4d5ee956 2022-07-02 jrick {
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");
2415 4d5ee956 2022-07-02 jrick
2416 4d5ee956 2022-07-02 jrick return flush_imsg(ibuf);
2417 4d5ee956 2022-07-02 jrick }
2418 4d5ee956 2022-07-02 jrick
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)
2421 4d5ee956 2022-07-02 jrick {
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");
2426 257add31 2020-09-09 stsp
2427 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2428 ca6e02ac 2020-01-07 stsp }
2429 ca6e02ac 2020-01-07 stsp
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)
2432 d68f2c0e 2022-07-05 jrick {
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");
2437 d68f2c0e 2022-07-05 jrick
2438 d68f2c0e 2022-07-05 jrick return flush_imsg(ibuf);
2439 d68f2c0e 2022-07-05 jrick }
2440 d68f2c0e 2022-07-05 jrick
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)
2443 257add31 2020-09-09 stsp {
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");
2448 257add31 2020-09-09 stsp
2449 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2450 257add31 2020-09-09 stsp }
2451 257add31 2020-09-09 stsp
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)
2454 257add31 2020-09-09 stsp {
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;
2458 257add31 2020-09-09 stsp
2459 257add31 2020-09-09 stsp *str = NULL;
2460 257add31 2020-09-09 stsp
2461 3e6054c4 2022-06-14 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2462 257add31 2020-09-09 stsp if (err)
2463 257add31 2020-09-09 stsp return err;
2464 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2465 257add31 2020-09-09 stsp
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)
2469 257add31 2020-09-09 stsp break;
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");
2473 257add31 2020-09-09 stsp break;
2474 257add31 2020-09-09 stsp }
2475 257add31 2020-09-09 stsp break;
2476 257add31 2020-09-09 stsp default:
2477 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2478 257add31 2020-09-09 stsp break;
2479 257add31 2020-09-09 stsp }
2480 257add31 2020-09-09 stsp
2481 257add31 2020-09-09 stsp imsg_free(&imsg);
2482 257add31 2020-09-09 stsp return err;
2483 257add31 2020-09-09 stsp }
2484 257add31 2020-09-09 stsp
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)
2488 257add31 2020-09-09 stsp {
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));
2496 257add31 2020-09-09 stsp
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;
2500 257add31 2020-09-09 stsp
2501 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2502 257add31 2020-09-09 stsp if (err)
2503 257add31 2020-09-09 stsp return err;
2504 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2505 257add31 2020-09-09 stsp
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);
2510 257add31 2020-09-09 stsp break;
2511 257add31 2020-09-09 stsp }
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);
2515 c98b0f0b 2022-06-14 op break;
2516 c98b0f0b 2022-06-14 op }
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;
2520 257add31 2020-09-09 stsp }
2521 257add31 2020-09-09 stsp break;
2522 257add31 2020-09-09 stsp default:
2523 257add31 2020-09-09 stsp imsg_free(&imsg);
2524 257add31 2020-09-09 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2525 257add31 2020-09-09 stsp }
2526 257add31 2020-09-09 stsp
2527 257add31 2020-09-09 stsp imsg_free(&imsg);
2528 257add31 2020-09-09 stsp
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");
2532 257add31 2020-09-09 stsp
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));
2537 b8adfa55 2020-09-25 stsp int i;
2538 257add31 2020-09-09 stsp
2539 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2540 257add31 2020-09-09 stsp if (err)
2541 257add31 2020-09-09 stsp break;
2542 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2543 257add31 2020-09-09 stsp
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);
2550 257add31 2020-09-09 stsp break;
2551 257add31 2020-09-09 stsp }
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);
2560 257add31 2020-09-09 stsp break;
2561 257add31 2020-09-09 stsp }
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");
2566 257add31 2020-09-09 stsp break;
2567 257add31 2020-09-09 stsp }
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);
2574 6480c871 2021-08-30 stsp break;
2575 6480c871 2021-08-30 stsp }
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);
2582 257add31 2020-09-09 stsp break;
2583 257add31 2020-09-09 stsp }
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);
2592 b8adfa55 2020-09-25 stsp break;
2593 b8adfa55 2020-09-25 stsp }
2594 b8adfa55 2020-09-25 stsp }
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,
2599 b8adfa55 2020-09-25 stsp ibuf);
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;
2603 b8adfa55 2020-09-25 stsp }
2604 6480c871 2021-08-30 stsp remote->fetch_branches[i] = branch;
2605 6480c871 2021-08-30 stsp remote->nfetch_branches++;
2606 99495ddb 2021-01-10 stsp }
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);
2613 99495ddb 2021-01-10 stsp break;
2614 99495ddb 2021-01-10 stsp }
2615 b8adfa55 2020-09-25 stsp }
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,
2620 6480c871 2021-08-30 stsp ibuf);
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;
2624 6480c871 2021-08-30 stsp }
2625 6480c871 2021-08-30 stsp remote->send_branches[i] = branch;
2626 6480c871 2021-08-30 stsp remote->nsend_branches++;
2627 6480c871 2021-08-30 stsp }
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);
2634 6480c871 2021-08-30 stsp break;
2635 6480c871 2021-08-30 stsp }
2636 6480c871 2021-08-30 stsp }
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,
2641 99495ddb 2021-01-10 stsp ibuf);
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;
2645 99495ddb 2021-01-10 stsp }
2646 6480c871 2021-08-30 stsp remote->fetch_refs[i] = ref;
2647 6480c871 2021-08-30 stsp remote->nfetch_refs++;
2648 99495ddb 2021-01-10 stsp }
2649 257add31 2020-09-09 stsp (*nremotes)++;
2650 257add31 2020-09-09 stsp break;
2651 257add31 2020-09-09 stsp default:
2652 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2653 257add31 2020-09-09 stsp break;
2654 257add31 2020-09-09 stsp }
2655 257add31 2020-09-09 stsp
2656 257add31 2020-09-09 stsp imsg_free(&imsg);
2657 257add31 2020-09-09 stsp if (err)
2658 257add31 2020-09-09 stsp break;
2659 257add31 2020-09-09 stsp }
2660 b8adfa55 2020-09-25 stsp done:
2661 257add31 2020-09-09 stsp if (err) {
2662 257add31 2020-09-09 stsp int i;
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;
2668 257add31 2020-09-09 stsp }
2669 257add31 2020-09-09 stsp return err;
2670 257add31 2020-09-09 stsp }
2671 257add31 2020-09-09 stsp
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)
2675 ca6e02ac 2020-01-07 stsp {
2676 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2677 47cb6f8b 2023-02-26 op size_t path_len = strlen(path);
2678 ca6e02ac 2020-01-07 stsp
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");
2684 47cb6f8b 2023-02-26 op /*
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.
2687 47cb6f8b 2023-02-26 op */
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");
2700 ca6e02ac 2020-01-07 stsp
2701 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2702 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2703 ca6e02ac 2020-01-07 stsp
2704 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2705 ca6e02ac 2020-01-07 stsp }
2706 ca6e02ac 2020-01-07 stsp
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)
2711 ca6e02ac 2020-01-07 stsp {
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;
2718 ca6e02ac 2020-01-07 stsp
2719 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
2720 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
2721 ca6e02ac 2020-01-07 stsp
2722 ca6e02ac 2020-01-07 stsp while (!done) {
2723 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2724 ca6e02ac 2020-01-07 stsp if (err)
2725 ca6e02ac 2020-01-07 stsp return err;
2726 ca6e02ac 2020-01-07 stsp
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);
2734 ca6e02ac 2020-01-07 stsp break;
2735 ca6e02ac 2020-01-07 stsp }
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;
2739 076fbedc 2023-02-19 op
2740 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
2741 ca6e02ac 2020-01-07 stsp if (err)
2742 ca6e02ac 2020-01-07 stsp break;
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);
2745 ca6e02ac 2020-01-07 stsp
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");
2753 ca6e02ac 2020-01-07 stsp break;
2754 ca6e02ac 2020-01-07 stsp }
2755 ca6e02ac 2020-01-07 stsp }
2756 ca6e02ac 2020-01-07 stsp }
2757 ca6e02ac 2020-01-07 stsp break;
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);
2761 ca6e02ac 2020-01-07 stsp break;
2762 ca6e02ac 2020-01-07 stsp }
2763 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
2764 ca6e02ac 2020-01-07 stsp datalen, ibuf);
2765 ca6e02ac 2020-01-07 stsp break;
2766 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2767 ca6e02ac 2020-01-07 stsp done = 1;
2768 ca6e02ac 2020-01-07 stsp break;
2769 ca6e02ac 2020-01-07 stsp default:
2770 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2771 ca6e02ac 2020-01-07 stsp break;
2772 ca6e02ac 2020-01-07 stsp }
2773 ca6e02ac 2020-01-07 stsp
2774 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
2775 ca6e02ac 2020-01-07 stsp if (err)
2776 ca6e02ac 2020-01-07 stsp break;
2777 ca6e02ac 2020-01-07 stsp }
2778 ca6e02ac 2020-01-07 stsp
2779 ca6e02ac 2020-01-07 stsp if (err)
2780 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
2781 ca6e02ac 2020-01-07 stsp return err;
2782 0ab4c957 2022-06-13 stsp }
2783 0ab4c957 2022-06-13 stsp
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)
2788 0ab4c957 2022-06-13 stsp {
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;
2793 0ab4c957 2022-06-13 stsp
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");
2798 0ab4c957 2022-06-13 stsp
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");
2805 0ab4c957 2022-06-13 stsp
2806 0ab4c957 2022-06-13 stsp wbuf->fd = -1;
2807 0ab4c957 2022-06-13 stsp imsg_close(ibuf, wbuf);
2808 0ab4c957 2022-06-13 stsp
2809 0ab4c957 2022-06-13 stsp if (entries) {
2810 0ab4c957 2022-06-13 stsp err = send_tree_entries(ibuf, entries, nentries);
2811 0ab4c957 2022-06-13 stsp if (err)
2812 0ab4c957 2022-06-13 stsp return err;
2813 0ab4c957 2022-06-13 stsp }
2814 0ab4c957 2022-06-13 stsp
2815 0ab4c957 2022-06-13 stsp return flush_imsg(ibuf);
2816 67fd6849 2022-02-13 stsp }
2817 67fd6849 2022-02-13 stsp
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)
2820 0ab4c957 2022-06-13 stsp {
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");
2825 0ab4c957 2022-06-13 stsp
2826 0ab4c957 2022-06-13 stsp return flush_imsg(ibuf);
2827 0ab4c957 2022-06-13 stsp }
2828 0ab4c957 2022-06-13 stsp
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)
2831 0ab4c957 2022-06-13 stsp {
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");
2836 0ab4c957 2022-06-13 stsp
2837 0ab4c957 2022-06-13 stsp return flush_imsg(ibuf);
2838 0ab4c957 2022-06-13 stsp }
2839 0ab4c957 2022-06-13 stsp
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)
2842 db9b9b1c 2022-06-14 stsp {
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");
2847 db9b9b1c 2022-06-14 stsp
2848 db9b9b1c 2022-06-14 stsp return flush_imsg(ibuf);
2849 db9b9b1c 2022-06-14 stsp }
2850 db9b9b1c 2022-06-14 stsp
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)
2854 0ab4c957 2022-06-13 stsp {
2855 0ab4c957 2022-06-13 stsp struct ibuf *wbuf;
2856 0ab4c957 2022-06-13 stsp
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");
2861 0ab4c957 2022-06-13 stsp
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");
2867 0ab4c957 2022-06-13 stsp
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;
2872 0ab4c957 2022-06-13 stsp }
2873 0ab4c957 2022-06-13 stsp
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)
2880 0ab4c957 2022-06-13 stsp {
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;
2894 0ab4c957 2022-06-13 stsp
2895 db9b9b1c 2022-06-14 stsp *found_all_objects = 0;
2896 0ab4c957 2022-06-13 stsp memset(&tree, 0, sizeof(tree));
2897 0ab4c957 2022-06-13 stsp
2898 0ab4c957 2022-06-13 stsp while (!done) {
2899 0ab4c957 2022-06-13 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2900 0ab4c957 2022-06-13 stsp if (err)
2901 0ab4c957 2022-06-13 stsp break;
2902 0ab4c957 2022-06-13 stsp
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);
2908 0ab4c957 2022-06-13 stsp break;
2909 0ab4c957 2022-06-13 stsp }
2910 0ab4c957 2022-06-13 stsp if (datalen != sizeof(*icommit)) {
2911 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2912 0ab4c957 2022-06-13 stsp break;
2913 0ab4c957 2022-06-13 stsp }
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;
2918 0ab4c957 2022-06-13 stsp break;
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);
2923 0ab4c957 2022-06-13 stsp break;
2924 0ab4c957 2022-06-13 stsp }
2925 0ab4c957 2022-06-13 stsp if (datalen < sizeof(*itree)) {
2926 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2927 0ab4c957 2022-06-13 stsp break;
2928 0ab4c957 2022-06-13 stsp }
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);
2933 0ab4c957 2022-06-13 stsp break;
2934 0ab4c957 2022-06-13 stsp }
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");
2940 0ab4c957 2022-06-13 stsp break;
2941 0ab4c957 2022-06-13 stsp }
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");
2946 0ab4c957 2022-06-13 stsp break;
2947 0ab4c957 2022-06-13 stsp }
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);
2950 0ab4c957 2022-06-13 stsp break;
2951 0ab4c957 2022-06-13 stsp }
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';
2956 0ab4c957 2022-06-13 stsp } else {
2957 0ab4c957 2022-06-13 stsp err = got_canonpath(path, canon_path,
2958 0ab4c957 2022-06-13 stsp path_len + 1);
2959 0ab4c957 2022-06-13 stsp if (err)
2960 0ab4c957 2022-06-13 stsp break;
2961 0ab4c957 2022-06-13 stsp }
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);
2964 0ab4c957 2022-06-13 stsp break;
2965 0ab4c957 2022-06-13 stsp }
2966 0ab4c957 2022-06-13 stsp if (nentries != -1) {
2967 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2968 0ab4c957 2022-06-13 stsp break;
2969 0ab4c957 2022-06-13 stsp }
2970 0ab4c957 2022-06-13 stsp if (itree->nentries < -1) {
2971 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2972 0ab4c957 2022-06-13 stsp break;
2973 0ab4c957 2022-06-13 stsp }
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);
2978 0ab4c957 2022-06-13 stsp break;
2979 0ab4c957 2022-06-13 stsp }
2980 0ab4c957 2022-06-13 stsp if (itree->nentries > INT_MAX) {
2981 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2982 0ab4c957 2022-06-13 stsp break;
2983 0ab4c957 2022-06-13 stsp }
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");
2988 0ab4c957 2022-06-13 stsp break;
2989 0ab4c957 2022-06-13 stsp }
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);
2993 0ab4c957 2022-06-13 stsp if (err)
2994 0ab4c957 2022-06-13 stsp break;
2995 0ab4c957 2022-06-13 stsp
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;
3000 0ab4c957 2022-06-13 stsp } else {
3001 0ab4c957 2022-06-13 stsp tree.nentries = itree->nentries;
3002 0ab4c957 2022-06-13 stsp nentries = 0;
3003 0ab4c957 2022-06-13 stsp }
3004 0ab4c957 2022-06-13 stsp break;
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);
3009 0ab4c957 2022-06-13 stsp break;
3010 0ab4c957 2022-06-13 stsp }
3011 0ab4c957 2022-06-13 stsp err = recv_tree_entries(imsg.data, datalen,
3012 0ab4c957 2022-06-13 stsp &tree, &nentries);
3013 0ab4c957 2022-06-13 stsp if (err)
3014 0ab4c957 2022-06-13 stsp break;
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);
3018 0ab4c957 2022-06-13 stsp if (err)
3019 0ab4c957 2022-06-13 stsp break;
3020 0ab4c957 2022-06-13 stsp
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;
3025 0ab4c957 2022-06-13 stsp }
3026 0ab4c957 2022-06-13 stsp break;
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);
3031 0ab4c957 2022-06-13 stsp break;
3032 0ab4c957 2022-06-13 stsp }
3033 0ab4c957 2022-06-13 stsp err = cb_commit(cb_arg, mtime, &commit_id, repo);
3034 0ab4c957 2022-06-13 stsp if (err)
3035 0ab4c957 2022-06-13 stsp break;
3036 0ab4c957 2022-06-13 stsp have_commit = 0;
3037 0ab4c957 2022-06-13 stsp break;
3038 0ab4c957 2022-06-13 stsp case GOT_IMSG_OBJECT_ENUMERATION_DONE:
3039 db9b9b1c 2022-06-14 stsp *found_all_objects = 1;
3040 db9b9b1c 2022-06-14 stsp done = 1;
3041 db9b9b1c 2022-06-14 stsp break;
3042 db9b9b1c 2022-06-14 stsp case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
3043 0ab4c957 2022-06-13 stsp done = 1;
3044 0ab4c957 2022-06-13 stsp break;
3045 0ab4c957 2022-06-13 stsp default:
3046 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3047 0ab4c957 2022-06-13 stsp break;
3048 0ab4c957 2022-06-13 stsp }
3049 0ab4c957 2022-06-13 stsp
3050 0ab4c957 2022-06-13 stsp imsg_free(&imsg);
3051 0ab4c957 2022-06-13 stsp if (err)
3052 0ab4c957 2022-06-13 stsp break;
3053 0ab4c957 2022-06-13 stsp }
3054 0ab4c957 2022-06-13 stsp
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;
3059 0ab4c957 2022-06-13 stsp }
3060 0ab4c957 2022-06-13 stsp
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)
3064 67fd6849 2022-02-13 stsp {
3065 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta_request dreq;
3066 67fd6849 2022-02-13 stsp
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));
3070 67fd6849 2022-02-13 stsp
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");
3074 67fd6849 2022-02-13 stsp
3075 67fd6849 2022-02-13 stsp return flush_imsg(ibuf);
3076 67fd6849 2022-02-13 stsp }
3077 67fd6849 2022-02-13 stsp
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)
3080 67fd6849 2022-02-13 stsp {
3081 67fd6849 2022-02-13 stsp return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3082 67fd6849 2022-02-13 stsp }
3083 67fd6849 2022-02-13 stsp
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)
3088 67fd6849 2022-02-13 stsp {
3089 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta idelta;
3090 67fd6849 2022-02-13 stsp int ret;
3091 67fd6849 2022-02-13 stsp
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));
3100 67fd6849 2022-02-13 stsp
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");
3105 67fd6849 2022-02-13 stsp
3106 67fd6849 2022-02-13 stsp return flush_imsg(ibuf);
3107 ca6e02ac 2020-01-07 stsp }
3108 ca6e02ac 2020-01-07 stsp
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)
3114 67fd6849 2022-02-13 stsp {
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;
3119 67fd6849 2022-02-13 stsp
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;
3127 67fd6849 2022-02-13 stsp
3128 67fd6849 2022-02-13 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3129 67fd6849 2022-02-13 stsp if (err)
3130 67fd6849 2022-02-13 stsp return err;
3131 67fd6849 2022-02-13 stsp
3132 67fd6849 2022-02-13 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3133 67fd6849 2022-02-13 stsp
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);
3138 67fd6849 2022-02-13 stsp break;
3139 67fd6849 2022-02-13 stsp }
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");
3150 67fd6849 2022-02-13 stsp break;
3151 67fd6849 2022-02-13 stsp }
3152 407521c0 2023-01-31 op memcpy(*base_id, &delta->base_id, sizeof(**base_id));
3153 67fd6849 2022-02-13 stsp break;
3154 67fd6849 2022-02-13 stsp default:
3155 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3156 67fd6849 2022-02-13 stsp break;
3157 67fd6849 2022-02-13 stsp }
3158 67fd6849 2022-02-13 stsp
3159 67fd6849 2022-02-13 stsp imsg_free(&imsg);
3160 67fd6849 2022-02-13 stsp
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;
3164 fae7e038 2022-05-07 stsp }
3165 fae7e038 2022-05-07 stsp return err;
3166 fae7e038 2022-05-07 stsp }
3167 fae7e038 2022-05-07 stsp
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)
3170 fae7e038 2022-05-07 stsp {
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;
3174 fae7e038 2022-05-07 stsp size_t i;
3175 fae7e038 2022-05-07 stsp
3176 031611fc 2022-06-18 op memset(&idlist, 0, sizeof(idlist));
3177 031611fc 2022-06-18 op
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);
3180 fae7e038 2022-05-07 stsp
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;
3186 fae7e038 2022-05-07 stsp }
3187 fae7e038 2022-05-07 stsp
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");
3191 fae7e038 2022-05-07 stsp
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");
3196 fae7e038 2022-05-07 stsp }
3197 b4b1b9c0 2022-06-14 op
3198 fae7e038 2022-05-07 stsp wbuf->fd = -1;
3199 fae7e038 2022-05-07 stsp imsg_close(ibuf, wbuf);
3200 fae7e038 2022-05-07 stsp
3201 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3202 fae7e038 2022-05-07 stsp }
3203 fae7e038 2022-05-07 stsp
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)
3207 0ab4c957 2022-06-13 stsp {
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;
3211 0ab4c957 2022-06-13 stsp
3212 b4b1b9c0 2022-06-14 op for (i = 0; i < nids; i++) {
3213 a5e587e0 2022-06-14 stsp idlist[i % nitems(idlist)] = ids[i];
3214 a5e587e0 2022-06-14 stsp queued++;
3215 afd31498 2022-06-14 stsp if (queued >= nitems(idlist)) {
3216 a5e587e0 2022-06-14 stsp err = send_idlist(ibuf, idlist, queued);
3217 0ab4c957 2022-06-13 stsp if (err)
3218 0ab4c957 2022-06-13 stsp return err;
3219 a5e587e0 2022-06-14 stsp queued = 0;
3220 0ab4c957 2022-06-13 stsp }
3221 0ab4c957 2022-06-13 stsp }
3222 0ab4c957 2022-06-13 stsp
3223 a5e587e0 2022-06-14 stsp if (queued > 0) {
3224 a5e587e0 2022-06-14 stsp err = send_idlist(ibuf, idlist, queued);
3225 0ab4c957 2022-06-13 stsp if (err)
3226 0ab4c957 2022-06-13 stsp return err;
3227 0ab4c957 2022-06-13 stsp }
3228 0ab4c957 2022-06-13 stsp
3229 0ab4c957 2022-06-13 stsp return NULL;
3230 0ab4c957 2022-06-13 stsp }
3231 0ab4c957 2022-06-13 stsp
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)
3234 fae7e038 2022-05-07 stsp {
3235 fae7e038 2022-05-07 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3236 fae7e038 2022-05-07 stsp == -1)
3237 fae7e038 2022-05-07 stsp return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3238 fae7e038 2022-05-07 stsp
3239 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3240 fae7e038 2022-05-07 stsp }
3241 fae7e038 2022-05-07 stsp
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)
3245 fae7e038 2022-05-07 stsp {
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;
3250 fae7e038 2022-05-07 stsp
3251 fae7e038 2022-05-07 stsp *ids = NULL;
3252 fae7e038 2022-05-07 stsp *done = 0;
3253 fae7e038 2022-05-07 stsp *nids = 0;
3254 fae7e038 2022-05-07 stsp
3255 fae7e038 2022-05-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3256 fae7e038 2022-05-07 stsp if (err)
3257 fae7e038 2022-05-07 stsp return err;
3258 fae7e038 2022-05-07 stsp
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);
3264 fae7e038 2022-05-07 stsp break;
3265 fae7e038 2022-05-07 stsp }
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);
3270 fae7e038 2022-05-07 stsp break;
3271 fae7e038 2022-05-07 stsp }
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");
3276 fae7e038 2022-05-07 stsp break;
3277 fae7e038 2022-05-07 stsp }
3278 0fca100c 2022-06-14 op memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3279 fae7e038 2022-05-07 stsp *nids * sizeof(**ids));
3280 fae7e038 2022-05-07 stsp break;
3281 fae7e038 2022-05-07 stsp case GOT_IMSG_OBJ_ID_LIST_DONE:
3282 fae7e038 2022-05-07 stsp *done = 1;
3283 fae7e038 2022-05-07 stsp break;
3284 fae7e038 2022-05-07 stsp default:
3285 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3286 fae7e038 2022-05-07 stsp break;
3287 fae7e038 2022-05-07 stsp }
3288 fae7e038 2022-05-07 stsp
3289 fae7e038 2022-05-07 stsp imsg_free(&imsg);
3290 fae7e038 2022-05-07 stsp
3291 fae7e038 2022-05-07 stsp return err;
3292 fae7e038 2022-05-07 stsp }
3293 fae7e038 2022-05-07 stsp
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)
3296 fae7e038 2022-05-07 stsp {
3297 fae7e038 2022-05-07 stsp if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3298 fae7e038 2022-05-07 stsp == -1)
3299 fae7e038 2022-05-07 stsp return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3300 fae7e038 2022-05-07 stsp
3301 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3302 fae7e038 2022-05-07 stsp }
3303 fae7e038 2022-05-07 stsp
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)
3307 fae7e038 2022-05-07 stsp {
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;
3311 fae7e038 2022-05-07 stsp size_t i;
3312 fae7e038 2022-05-07 stsp
3313 031611fc 2022-06-18 op memset(&ideltas, 0, sizeof(ideltas));
3314 031611fc 2022-06-18 op
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);
3317 fae7e038 2022-05-07 stsp
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;
3323 67fd6849 2022-02-13 stsp }
3324 fae7e038 2022-05-07 stsp
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");
3328 fae7e038 2022-05-07 stsp
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");
3333 fae7e038 2022-05-07 stsp }
3334 b4b1b9c0 2022-06-14 op
3335 fae7e038 2022-05-07 stsp wbuf->fd = -1;
3336 fae7e038 2022-05-07 stsp imsg_close(ibuf, wbuf);
3337 fae7e038 2022-05-07 stsp
3338 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3339 fae7e038 2022-05-07 stsp }
3340 fae7e038 2022-05-07 stsp
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)
3343 fae7e038 2022-05-07 stsp {
3344 fae7e038 2022-05-07 stsp if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3345 fae7e038 2022-05-07 stsp == -1)
3346 fae7e038 2022-05-07 stsp return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3347 fae7e038 2022-05-07 stsp
3348 fae7e038 2022-05-07 stsp return flush_imsg(ibuf);
3349 fae7e038 2022-05-07 stsp }
3350 fae7e038 2022-05-07 stsp
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)
3354 fae7e038 2022-05-07 stsp {
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;
3359 fae7e038 2022-05-07 stsp
3360 fae7e038 2022-05-07 stsp *done = 0;
3361 fae7e038 2022-05-07 stsp *ndeltas = 0;
3362 fae7e038 2022-05-07 stsp
3363 fae7e038 2022-05-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3364 fae7e038 2022-05-07 stsp if (err)
3365 fae7e038 2022-05-07 stsp return err;
3366 fae7e038 2022-05-07 stsp
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);
3372 fae7e038 2022-05-07 stsp break;
3373 fae7e038 2022-05-07 stsp }
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);
3379 fae7e038 2022-05-07 stsp break;
3380 fae7e038 2022-05-07 stsp }
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));
3384 fae7e038 2022-05-07 stsp break;
3385 fae7e038 2022-05-07 stsp case GOT_IMSG_DELTA_REUSE_DONE:
3386 fae7e038 2022-05-07 stsp *done = 1;
3387 fae7e038 2022-05-07 stsp break;
3388 fae7e038 2022-05-07 stsp default:
3389 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
3390 fae7e038 2022-05-07 stsp break;
3391 fae7e038 2022-05-07 stsp }
3392 fae7e038 2022-05-07 stsp
3393 fae7e038 2022-05-07 stsp imsg_free(&imsg);
3394 61af9b21 2022-06-28 stsp
3395 61af9b21 2022-06-28 stsp return err;
3396 61af9b21 2022-06-28 stsp }
3397 61af9b21 2022-06-28 stsp
3398 61af9b21 2022-06-28 stsp const struct got_error *
3399 61af9b21 2022-06-28 stsp got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3400 61af9b21 2022-06-28 stsp {
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)
3403 61af9b21 2022-06-28 stsp == -1)
3404 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_compose "
3405 61af9b21 2022-06-28 stsp "COMMIT_PAINTING_INIT");
3406 61af9b21 2022-06-28 stsp
3407 61af9b21 2022-06-28 stsp return flush_imsg(ibuf);
3408 61af9b21 2022-06-28 stsp }
3409 61af9b21 2022-06-28 stsp
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)
3413 61af9b21 2022-06-28 stsp {
3414 61af9b21 2022-06-28 stsp struct got_imsg_commit_painting_request ireq;
3415 61af9b21 2022-06-28 stsp
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;
3420 61af9b21 2022-06-28 stsp
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");
3425 61af9b21 2022-06-28 stsp
3426 61af9b21 2022-06-28 stsp return flush_imsg(ibuf);
3427 61af9b21 2022-06-28 stsp }
3428 61af9b21 2022-06-28 stsp
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)
3432 61af9b21 2022-06-28 stsp {
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;
3439 61af9b21 2022-06-28 stsp
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);
3443 61af9b21 2022-06-28 stsp
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;
3448 61af9b21 2022-06-28 stsp }
3449 61af9b21 2022-06-28 stsp
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");
3455 61af9b21 2022-06-28 stsp
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;
3462 61af9b21 2022-06-28 stsp
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");
3468 61af9b21 2022-06-28 stsp
3469 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
3470 61af9b21 2022-06-28 stsp }
3471 61af9b21 2022-06-28 stsp
3472 61af9b21 2022-06-28 stsp wbuf->fd = -1;
3473 61af9b21 2022-06-28 stsp imsg_close(ibuf, wbuf);
3474 61af9b21 2022-06-28 stsp
3475 61af9b21 2022-06-28 stsp return flush_imsg(ibuf);
3476 61af9b21 2022-06-28 stsp }
3477 61af9b21 2022-06-28 stsp
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)
3482 61af9b21 2022-06-28 stsp {
3483 61af9b21 2022-06-28 stsp const struct got_error *err;
3484 61af9b21 2022-06-28 stsp size_t remain;
3485 fae7e038 2022-05-07 stsp
3486 61af9b21 2022-06-28 stsp if (*nids <= 0)
3487 61af9b21 2022-06-28 stsp return NULL;
3488 61af9b21 2022-06-28 stsp
3489 61af9b21 2022-06-28 stsp do {
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);
3495 61af9b21 2022-06-28 stsp if (err)
3496 61af9b21 2022-06-28 stsp return err;
3497 61af9b21 2022-06-28 stsp }
3498 61af9b21 2022-06-28 stsp } while (flush && *nids > 0);
3499 61af9b21 2022-06-28 stsp
3500 61af9b21 2022-06-28 stsp return NULL;
3501 61af9b21 2022-06-28 stsp }
3502 61af9b21 2022-06-28 stsp
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)
3505 61af9b21 2022-06-28 stsp {
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)
3508 61af9b21 2022-06-28 stsp == -1)
3509 61af9b21 2022-06-28 stsp return got_error_from_errno("imsg_compose "
3510 61af9b21 2022-06-28 stsp "COMMIT_PAINTING_DONE");
3511 61af9b21 2022-06-28 stsp
3512 61af9b21 2022-06-28 stsp return flush_imsg(ibuf);
3513 61af9b21 2022-06-28 stsp }
3514 61af9b21 2022-06-28 stsp
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)
3518 61af9b21 2022-06-28 stsp {
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;
3524 61af9b21 2022-06-28 stsp int i;
3525 61af9b21 2022-06-28 stsp
3526 61af9b21 2022-06-28 stsp for (;;) {
3527 61af9b21 2022-06-28 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3528 61af9b21 2022-06-28 stsp if (err)
3529 61af9b21 2022-06-28 stsp return err;
3530 61af9b21 2022-06-28 stsp
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)
3533 61af9b21 2022-06-28 stsp break;
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);
3536 61af9b21 2022-06-28 stsp
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);
3544 61af9b21 2022-06-28 stsp
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));
3549 61af9b21 2022-06-28 stsp
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);
3554 61af9b21 2022-06-28 stsp if (err)
3555 61af9b21 2022-06-28 stsp break;
3556 61af9b21 2022-06-28 stsp } else {
3557 61af9b21 2022-06-28 stsp struct got_object_qid *qid;
3558 61af9b21 2022-06-28 stsp err = got_object_qid_alloc_partial(&qid);
3559 61af9b21 2022-06-28 stsp if (err)
3560 61af9b21 2022-06-28 stsp break;
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);
3565 61af9b21 2022-06-28 stsp }
3566 61af9b21 2022-06-28 stsp }
3567 61af9b21 2022-06-28 stsp
3568 61af9b21 2022-06-28 stsp imsg_free(&imsg);
3569 61af9b21 2022-06-28 stsp }
3570 61af9b21 2022-06-28 stsp
3571 67fd6849 2022-02-13 stsp return err;
3572 67fd6849 2022-02-13 stsp }
3573 67fd6849 2022-02-13 stsp
3574 67fd6849 2022-02-13 stsp const struct got_error *
3575 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
3576 63219cd2 2019-01-04 stsp {
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,
3590 c39c25dd 2019-08-09 stsp };
3591 16aeacf7 2020-11-26 stsp size_t i;
3592 63219cd2 2019-01-04 stsp
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)
3595 c39c25dd 2019-08-09 stsp continue;
3596 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
3597 c39c25dd 2019-08-09 stsp }
3598 c39c25dd 2019-08-09 stsp
3599 63219cd2 2019-01-04 stsp return NULL;
3600 876c234b 2018-09-10 stsp }
3601 aba9c984 2019-09-08 stsp
3602 aba9c984 2019-09-08 stsp void
3603 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3604 aba9c984 2019-09-08 stsp {
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));
3607 aba9c984 2019-09-08 stsp _exit(1);
3608 aba9c984 2019-09-08 stsp }
3609 aba9c984 2019-09-08 stsp
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));
3612 aba9c984 2019-09-08 stsp _exit(1);
3613 aba9c984 2019-09-08 stsp }
3614 aba9c984 2019-09-08 stsp
3615 4a55b231 2021-12-09 stsp closefrom(GOT_IMSG_FD_CHILD + 1);
3616 4a55b231 2021-12-09 stsp
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));
3620 aba9c984 2019-09-08 stsp _exit(1);
3621 aba9c984 2019-09-08 stsp }
3622 aba9c984 2019-09-08 stsp }