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 2178c42e 2018-04-22 stsp #include <stdio.h>
26 2178c42e 2018-04-22 stsp #include <stdlib.h>
27 2178c42e 2018-04-22 stsp #include <string.h>
28 2178c42e 2018-04-22 stsp #include <errno.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 2178c42e 2018-04-22 stsp #include <poll.h>
31 2178c42e 2018-04-22 stsp #include <imsg.h>
32 2178c42e 2018-04-22 stsp #include <sha1.h>
33 81a12da5 2020-09-09 naddy #include <unistd.h>
34 2178c42e 2018-04-22 stsp #include <zlib.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 2178c42e 2018-04-22 stsp
37 2178c42e 2018-04-22 stsp #include "got_object.h"
38 2178c42e 2018-04-22 stsp #include "got_error.h"
39 3022d272 2019-11-14 stsp #include "got_path.h"
40 cd95becd 2019-11-29 stsp #include "got_repository.h"
41 2178c42e 2018-04-22 stsp
42 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
43 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
44 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
47 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
48 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
49 2178c42e 2018-04-22 stsp
50 2178c42e 2018-04-22 stsp #ifndef MIN
51 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 c39c25dd 2019-08-09 stsp #endif
53 c39c25dd 2019-08-09 stsp
54 c39c25dd 2019-08-09 stsp #ifndef nitems
55 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 2178c42e 2018-04-22 stsp #endif
57 2178c42e 2018-04-22 stsp
58 2178c42e 2018-04-22 stsp static const struct got_error *
59 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
60 2178c42e 2018-04-22 stsp {
61 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
62 2178c42e 2018-04-22 stsp int n;
63 2178c42e 2018-04-22 stsp
64 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
65 2178c42e 2018-04-22 stsp pfd[0].events = events;
66 2178c42e 2018-04-22 stsp
67 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
68 2178c42e 2018-04-22 stsp if (n == -1)
69 638f9024 2019-05-13 stsp return got_error_from_errno("poll");
70 2178c42e 2018-04-22 stsp if (n == 0)
71 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
72 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
73 638f9024 2019-05-13 stsp return got_error_from_errno("poll error");
74 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
75 2178c42e 2018-04-22 stsp return NULL;
76 2178c42e 2018-04-22 stsp
77 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
78 2178c42e 2018-04-22 stsp }
79 2178c42e 2018-04-22 stsp
80 c4eae628 2018-04-23 stsp static const struct got_error *
81 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
82 fe36cf76 2018-04-23 stsp {
83 fe36cf76 2018-04-23 stsp const struct got_error *err;
84 e033d803 2018-04-23 stsp size_t n;
85 fe36cf76 2018-04-23 stsp
86 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
87 fe36cf76 2018-04-23 stsp if (err)
88 fe36cf76 2018-04-23 stsp return err;
89 fe36cf76 2018-04-23 stsp
90 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
91 fe36cf76 2018-04-23 stsp if (n == -1) {
92 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
93 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
94 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
95 fe36cf76 2018-04-23 stsp }
96 fe36cf76 2018-04-23 stsp if (n == 0)
97 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
98 fe36cf76 2018-04-23 stsp
99 e033d803 2018-04-23 stsp return NULL;
100 e033d803 2018-04-23 stsp }
101 e033d803 2018-04-23 stsp
102 ad242220 2018-09-08 stsp const struct got_error *
103 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
104 876c234b 2018-09-10 stsp {
105 876c234b 2018-09-10 stsp int child_status;
106 876c234b 2018-09-10 stsp
107 2cb49fa8 2019-05-10 stsp if (waitpid(pid, &child_status, 0) == -1)
108 638f9024 2019-05-13 stsp return got_error_from_errno("waitpid");
109 876c234b 2018-09-10 stsp
110 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
111 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
112 876c234b 2018-09-10 stsp
113 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
114 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
115 876c234b 2018-09-10 stsp
116 876c234b 2018-09-10 stsp return NULL;
117 876c234b 2018-09-10 stsp }
118 876c234b 2018-09-10 stsp
119 73b7854a 2018-11-11 stsp static const struct got_error *
120 73b7854a 2018-11-11 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
121 73b7854a 2018-11-11 stsp {
122 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
123 73b7854a 2018-11-11 stsp
124 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
125 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
126 73b7854a 2018-11-11 stsp
127 73b7854a 2018-11-11 stsp ierr = imsg->data;
128 73b7854a 2018-11-11 stsp if (ierr->code == GOT_ERR_ERRNO) {
129 73b7854a 2018-11-11 stsp static struct got_error serr;
130 73b7854a 2018-11-11 stsp serr.code = GOT_ERR_ERRNO;
131 73b7854a 2018-11-11 stsp serr.msg = strerror(ierr->errno_code);
132 73b7854a 2018-11-11 stsp return &serr;
133 73b7854a 2018-11-11 stsp }
134 73b7854a 2018-11-11 stsp
135 73b7854a 2018-11-11 stsp return got_error(ierr->code);
136 73b7854a 2018-11-11 stsp }
137 73b7854a 2018-11-11 stsp
138 876c234b 2018-09-10 stsp const struct got_error *
139 46de5bfd 2018-11-11 stsp got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
140 46de5bfd 2018-11-11 stsp size_t min_datalen)
141 e033d803 2018-04-23 stsp {
142 e033d803 2018-04-23 stsp const struct got_error *err;
143 e033d803 2018-04-23 stsp ssize_t n;
144 e033d803 2018-04-23 stsp
145 e033d803 2018-04-23 stsp n = imsg_get(ibuf, imsg);
146 876c234b 2018-09-10 stsp if (n == -1)
147 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_get");
148 876c234b 2018-09-10 stsp
149 876c234b 2018-09-10 stsp while (n == 0) {
150 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
151 876c234b 2018-09-10 stsp if (err)
152 876c234b 2018-09-10 stsp return err;
153 876c234b 2018-09-10 stsp n = imsg_get(ibuf, imsg);
154 cbfaaf20 2020-01-06 stsp if (n == -1)
155 cbfaaf20 2020-01-06 stsp return got_error_from_errno("imsg_get");
156 876c234b 2018-09-10 stsp }
157 fe36cf76 2018-04-23 stsp
158 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
159 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
160 c4eae628 2018-04-23 stsp
161 73b7854a 2018-11-11 stsp if (imsg->hdr.type == GOT_IMSG_ERROR) {
162 73b7854a 2018-11-11 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
163 73b7854a 2018-11-11 stsp return recv_imsg_error(imsg, datalen);
164 c4eae628 2018-04-23 stsp }
165 c4eae628 2018-04-23 stsp
166 73b7854a 2018-11-11 stsp return NULL;
167 c4eae628 2018-04-23 stsp }
168 c4eae628 2018-04-23 stsp
169 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
170 2178c42e 2018-04-22 stsp void
171 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 2178c42e 2018-04-22 stsp {
173 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
174 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
175 2178c42e 2018-04-22 stsp int ret;
176 2178c42e 2018-04-22 stsp
177 2178c42e 2018-04-22 stsp ierr.code = err->code;
178 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
179 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
180 2178c42e 2018-04-22 stsp else
181 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
182 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
183 e93cd828 2018-11-11 stsp if (ret == -1) {
184 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
185 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
186 5d43e84d 2018-04-23 stsp return;
187 2178c42e 2018-04-22 stsp }
188 2178c42e 2018-04-22 stsp
189 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
190 5d43e84d 2018-04-23 stsp if (poll_err) {
191 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
192 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
193 5d43e84d 2018-04-23 stsp return;
194 5d43e84d 2018-04-23 stsp }
195 2178c42e 2018-04-22 stsp
196 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
197 5d43e84d 2018-04-23 stsp if (ret == -1) {
198 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
199 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
200 5d43e84d 2018-04-23 stsp return;
201 5d43e84d 2018-04-23 stsp }
202 e033d803 2018-04-23 stsp }
203 e033d803 2018-04-23 stsp
204 e033d803 2018-04-23 stsp static const struct got_error *
205 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
206 e033d803 2018-04-23 stsp {
207 e033d803 2018-04-23 stsp const struct got_error *err;
208 e033d803 2018-04-23 stsp
209 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
210 e033d803 2018-04-23 stsp if (err)
211 e033d803 2018-04-23 stsp return err;
212 e033d803 2018-04-23 stsp
213 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
214 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
215 e033d803 2018-04-23 stsp
216 e033d803 2018-04-23 stsp return NULL;
217 ad242220 2018-09-08 stsp }
218 ad242220 2018-09-08 stsp
219 ad242220 2018-09-08 stsp const struct got_error *
220 e70bf110 2020-03-22 stsp got_privsep_flush_imsg(struct imsgbuf *ibuf)
221 e70bf110 2020-03-22 stsp {
222 e70bf110 2020-03-22 stsp return flush_imsg(ibuf);
223 e70bf110 2020-03-22 stsp }
224 e70bf110 2020-03-22 stsp
225 e70bf110 2020-03-22 stsp const struct got_error *
226 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
227 ad242220 2018-09-08 stsp {
228 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
229 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
230 ad242220 2018-09-08 stsp
231 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
232 ad242220 2018-09-08 stsp
233 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
235 ad242220 2018-09-08 stsp
236 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
237 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
238 ad242220 2018-09-08 stsp return err;
239 7762fe12 2018-11-05 stsp }
240 93658fb9 2020-03-18 stsp
241 93658fb9 2020-03-18 stsp const struct got_error *
242 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
243 ad242220 2018-09-08 stsp {
244 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
245 aea5f015 2018-12-24 stsp == -1)
246 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
247 59d1e4a0 2021-03-10 stsp
248 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
249 59d1e4a0 2021-03-10 stsp }
250 59d1e4a0 2021-03-10 stsp
251 59d1e4a0 2021-03-10 stsp const struct got_error *
252 59d1e4a0 2021-03-10 stsp got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd)
253 59d1e4a0 2021-03-10 stsp {
254 59d1e4a0 2021-03-10 stsp if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
255 59d1e4a0 2021-03-10 stsp == -1)
256 59d1e4a0 2021-03-10 stsp return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
257 59d1e4a0 2021-03-10 stsp
258 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
259 59d1e4a0 2021-03-10 stsp }
260 59d1e4a0 2021-03-10 stsp
261 59d1e4a0 2021-03-10 stsp const struct got_error *
262 59d1e4a0 2021-03-10 stsp got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
263 59d1e4a0 2021-03-10 stsp {
264 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
265 59d1e4a0 2021-03-10 stsp
266 59d1e4a0 2021-03-10 stsp if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
267 59d1e4a0 2021-03-10 stsp == -1) {
268 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
269 59d1e4a0 2021-03-10 stsp close(outfd);
270 59d1e4a0 2021-03-10 stsp return err;
271 59d1e4a0 2021-03-10 stsp }
272 59d1e4a0 2021-03-10 stsp
273 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
274 59d1e4a0 2021-03-10 stsp }
275 59d1e4a0 2021-03-10 stsp
276 59d1e4a0 2021-03-10 stsp const struct got_error *
277 59d1e4a0 2021-03-10 stsp got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
278 59d1e4a0 2021-03-10 stsp uint8_t *data)
279 59d1e4a0 2021-03-10 stsp {
280 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
281 59d1e4a0 2021-03-10 stsp struct got_imsg_raw_obj iobj;
282 59d1e4a0 2021-03-10 stsp size_t len = sizeof(iobj);
283 59d1e4a0 2021-03-10 stsp struct ibuf *wbuf;
284 1785f84a 2018-12-23 stsp
285 59d1e4a0 2021-03-10 stsp iobj.hdrlen = hdrlen;
286 59d1e4a0 2021-03-10 stsp iobj.size = size;
287 59d1e4a0 2021-03-10 stsp
288 59d1e4a0 2021-03-10 stsp if (data && size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
289 59d1e4a0 2021-03-10 stsp len += (size_t)size;
290 59d1e4a0 2021-03-10 stsp
291 59d1e4a0 2021-03-10 stsp wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
292 59d1e4a0 2021-03-10 stsp if (wbuf == NULL) {
293 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("imsg_create RAW_OBJECT");
294 59d1e4a0 2021-03-10 stsp return err;
295 59d1e4a0 2021-03-10 stsp }
296 59d1e4a0 2021-03-10 stsp
297 59d1e4a0 2021-03-10 stsp if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1) {
298 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("imsg_add RAW_OBJECT");
299 59d1e4a0 2021-03-10 stsp ibuf_free(wbuf);
300 59d1e4a0 2021-03-10 stsp return err;
301 59d1e4a0 2021-03-10 stsp }
302 59d1e4a0 2021-03-10 stsp
303 59d1e4a0 2021-03-10 stsp if (data && size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
304 59d1e4a0 2021-03-10 stsp if (imsg_add(wbuf, data, size) == -1) {
305 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("imsg_add RAW_OBJECT");
306 59d1e4a0 2021-03-10 stsp ibuf_free(wbuf);
307 59d1e4a0 2021-03-10 stsp return err;
308 59d1e4a0 2021-03-10 stsp }
309 59d1e4a0 2021-03-10 stsp }
310 59d1e4a0 2021-03-10 stsp
311 59d1e4a0 2021-03-10 stsp wbuf->fd = -1;
312 59d1e4a0 2021-03-10 stsp imsg_close(ibuf, wbuf);
313 59d1e4a0 2021-03-10 stsp
314 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
315 1785f84a 2018-12-23 stsp }
316 1785f84a 2018-12-23 stsp
317 1785f84a 2018-12-23 stsp const struct got_error *
318 59d1e4a0 2021-03-10 stsp got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
319 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf)
320 59d1e4a0 2021-03-10 stsp {
321 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
322 59d1e4a0 2021-03-10 stsp struct imsg imsg;
323 59d1e4a0 2021-03-10 stsp struct got_imsg_raw_obj *iobj;
324 59d1e4a0 2021-03-10 stsp size_t datalen;
325 59d1e4a0 2021-03-10 stsp
326 59d1e4a0 2021-03-10 stsp *outbuf = NULL;
327 59d1e4a0 2021-03-10 stsp
328 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
329 59d1e4a0 2021-03-10 stsp if (err)
330 59d1e4a0 2021-03-10 stsp return err;
331 59d1e4a0 2021-03-10 stsp
332 59d1e4a0 2021-03-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
333 59d1e4a0 2021-03-10 stsp
334 59d1e4a0 2021-03-10 stsp switch (imsg.hdr.type) {
335 59d1e4a0 2021-03-10 stsp case GOT_IMSG_RAW_OBJECT:
336 59d1e4a0 2021-03-10 stsp if (datalen < sizeof(*iobj)) {
337 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
338 59d1e4a0 2021-03-10 stsp break;
339 59d1e4a0 2021-03-10 stsp }
340 59d1e4a0 2021-03-10 stsp iobj = imsg.data;
341 59d1e4a0 2021-03-10 stsp *size = iobj->size;
342 59d1e4a0 2021-03-10 stsp *hdrlen = iobj->hdrlen;
343 59d1e4a0 2021-03-10 stsp
344 59d1e4a0 2021-03-10 stsp if (datalen == sizeof(*iobj)) {
345 59d1e4a0 2021-03-10 stsp /* Data has been written to file descriptor. */
346 59d1e4a0 2021-03-10 stsp break;
347 59d1e4a0 2021-03-10 stsp }
348 59d1e4a0 2021-03-10 stsp
349 59d1e4a0 2021-03-10 stsp if (*size > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
350 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
351 59d1e4a0 2021-03-10 stsp break;
352 59d1e4a0 2021-03-10 stsp }
353 59d1e4a0 2021-03-10 stsp
354 59d1e4a0 2021-03-10 stsp *outbuf = malloc(*size);
355 59d1e4a0 2021-03-10 stsp if (*outbuf == NULL) {
356 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("malloc");
357 59d1e4a0 2021-03-10 stsp break;
358 59d1e4a0 2021-03-10 stsp }
359 59d1e4a0 2021-03-10 stsp memcpy(*outbuf, imsg.data + sizeof(*iobj), *size);
360 59d1e4a0 2021-03-10 stsp break;
361 59d1e4a0 2021-03-10 stsp default:
362 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
363 59d1e4a0 2021-03-10 stsp break;
364 59d1e4a0 2021-03-10 stsp }
365 59d1e4a0 2021-03-10 stsp
366 59d1e4a0 2021-03-10 stsp imsg_free(&imsg);
367 59d1e4a0 2021-03-10 stsp
368 59d1e4a0 2021-03-10 stsp return err;
369 59d1e4a0 2021-03-10 stsp }
370 59d1e4a0 2021-03-10 stsp
371 59d1e4a0 2021-03-10 stsp const struct got_error *
372 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
373 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
374 1785f84a 2018-12-23 stsp {
375 41496140 2019-02-21 stsp const struct got_error *err = NULL;
376 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
377 1785f84a 2018-12-23 stsp size_t len;
378 1785f84a 2018-12-23 stsp
379 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
380 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
381 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
382 1785f84a 2018-12-23 stsp iobjp = &iobj;
383 1785f84a 2018-12-23 stsp len = sizeof(iobj);
384 1785f84a 2018-12-23 stsp } else {
385 1785f84a 2018-12-23 stsp iobjp = NULL;
386 1785f84a 2018-12-23 stsp len = 0;
387 1785f84a 2018-12-23 stsp }
388 1785f84a 2018-12-23 stsp
389 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
390 41496140 2019-02-21 stsp == -1) {
391 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
392 41496140 2019-02-21 stsp close(fd);
393 41496140 2019-02-21 stsp return err;
394 41496140 2019-02-21 stsp }
395 13c729f7 2018-12-24 stsp
396 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
397 13c729f7 2018-12-24 stsp }
398 13c729f7 2018-12-24 stsp
399 13c729f7 2018-12-24 stsp const struct got_error *
400 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
401 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
402 13c729f7 2018-12-24 stsp {
403 41496140 2019-02-21 stsp const struct got_error *err = NULL;
404 7f358e3b 2019-11-23 stsp struct ibuf *wbuf;
405 7f358e3b 2019-11-23 stsp size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
406 7f358e3b 2019-11-23 stsp
407 7f358e3b 2019-11-23 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
408 7f358e3b 2019-11-23 stsp if (wbuf == NULL)
409 7f358e3b 2019-11-23 stsp return got_error_from_errno("imsg_create TREE_REQUEST");
410 13c729f7 2018-12-24 stsp
411 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
412 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
413 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
414 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
415 7f358e3b 2019-11-23 stsp return err;
416 7f358e3b 2019-11-23 stsp }
417 13c729f7 2018-12-24 stsp
418 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
419 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
420 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
421 7f358e3b 2019-11-23 stsp return err;
422 7f358e3b 2019-11-23 stsp }
423 41496140 2019-02-21 stsp }
424 268f7291 2018-12-24 stsp
425 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
426 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
427 7f358e3b 2019-11-23 stsp
428 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
429 268f7291 2018-12-24 stsp }
430 268f7291 2018-12-24 stsp
431 268f7291 2018-12-24 stsp const struct got_error *
432 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
433 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
434 268f7291 2018-12-24 stsp {
435 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
436 268f7291 2018-12-24 stsp size_t len;
437 268f7291 2018-12-24 stsp
438 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
439 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
440 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
441 268f7291 2018-12-24 stsp iobjp = &iobj;
442 268f7291 2018-12-24 stsp len = sizeof(iobj);
443 268f7291 2018-12-24 stsp } else {
444 268f7291 2018-12-24 stsp iobjp = NULL;
445 268f7291 2018-12-24 stsp len = 0;
446 268f7291 2018-12-24 stsp }
447 268f7291 2018-12-24 stsp
448 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
449 1785f84a 2018-12-23 stsp == -1)
450 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
451 7762fe12 2018-11-05 stsp
452 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
453 7762fe12 2018-11-05 stsp }
454 7762fe12 2018-11-05 stsp
455 7762fe12 2018-11-05 stsp const struct got_error *
456 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
457 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
458 55da3778 2018-09-10 stsp {
459 41496140 2019-02-21 stsp const struct got_error *err = NULL;
460 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
461 ebc55e2d 2018-12-24 stsp size_t len;
462 ebc55e2d 2018-12-24 stsp
463 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
464 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
465 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
466 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
467 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
468 ebc55e2d 2018-12-24 stsp } else {
469 ebc55e2d 2018-12-24 stsp iobjp = NULL;
470 ebc55e2d 2018-12-24 stsp len = 0;
471 ebc55e2d 2018-12-24 stsp }
472 ebc55e2d 2018-12-24 stsp
473 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
474 41496140 2019-02-21 stsp == -1) {
475 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
476 41496140 2019-02-21 stsp close(infd);
477 41496140 2019-02-21 stsp return err;
478 41496140 2019-02-21 stsp }
479 ad242220 2018-09-08 stsp
480 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
481 55da3778 2018-09-10 stsp }
482 ad242220 2018-09-08 stsp
483 55da3778 2018-09-10 stsp const struct got_error *
484 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
485 55da3778 2018-09-10 stsp {
486 41496140 2019-02-21 stsp const struct got_error *err = NULL;
487 41496140 2019-02-21 stsp
488 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
489 41496140 2019-02-21 stsp == -1) {
490 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
491 41496140 2019-02-21 stsp close(outfd);
492 41496140 2019-02-21 stsp return err;
493 41496140 2019-02-21 stsp }
494 3840f4c9 2018-09-12 stsp
495 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
496 3840f4c9 2018-09-12 stsp }
497 3840f4c9 2018-09-12 stsp
498 73ab1060 2020-03-18 stsp static const struct got_error *
499 73ab1060 2020-03-18 stsp send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
500 3840f4c9 2018-09-12 stsp {
501 41496140 2019-02-21 stsp const struct got_error *err = NULL;
502 41496140 2019-02-21 stsp
503 73ab1060 2020-03-18 stsp if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
504 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
505 41496140 2019-02-21 stsp close(fd);
506 41496140 2019-02-21 stsp return err;
507 41496140 2019-02-21 stsp }
508 ad242220 2018-09-08 stsp
509 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
510 ad242220 2018-09-08 stsp }
511 ad242220 2018-09-08 stsp
512 ad242220 2018-09-08 stsp const struct got_error *
513 73ab1060 2020-03-18 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
514 73ab1060 2020-03-18 stsp {
515 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
516 73ab1060 2020-03-18 stsp }
517 73ab1060 2020-03-18 stsp
518 73ab1060 2020-03-18 stsp const struct got_error *
519 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
520 2178c42e 2018-04-22 stsp {
521 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
522 2178c42e 2018-04-22 stsp
523 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
524 2178c42e 2018-04-22 stsp iobj.type = obj->type;
525 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
526 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
527 2178c42e 2018-04-22 stsp iobj.size = obj->size;
528 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
529 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
530 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
531 c59b3346 2018-09-11 stsp }
532 2178c42e 2018-04-22 stsp
533 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
534 2178c42e 2018-04-22 stsp == -1)
535 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
536 2178c42e 2018-04-22 stsp
537 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
538 cfd633c2 2018-09-10 stsp }
539 cfd633c2 2018-09-10 stsp
540 cfd633c2 2018-09-10 stsp const struct got_error *
541 33501562 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
542 4ba14133 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
543 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
544 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
545 93658fb9 2020-03-18 stsp {
546 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
547 33501562 2020-03-18 stsp struct ibuf *wbuf;
548 4ba14133 2020-03-20 stsp size_t len;
549 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
550 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetchreq;
551 93658fb9 2020-03-18 stsp
552 4ba14133 2020-03-20 stsp memset(&fetchreq, 0, sizeof(fetchreq));
553 4ba14133 2020-03-20 stsp fetchreq.fetch_all_branches = fetch_all_branches;
554 41b0de12 2020-03-21 stsp fetchreq.list_refs_only = list_refs_only;
555 2690194b 2020-03-21 stsp fetchreq.verbosity = verbosity;
556 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, have_refs, entry)
557 4ba14133 2020-03-20 stsp fetchreq.n_have_refs++;
558 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry)
559 4ba14133 2020-03-20 stsp fetchreq.n_wanted_branches++;
560 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry)
561 0e4002ca 2020-03-21 stsp fetchreq.n_wanted_refs++;
562 659e7fbd 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_request);
563 33501562 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
564 33501562 2020-03-18 stsp close(fd);
565 33501562 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
566 33501562 2020-03-18 stsp }
567 33501562 2020-03-18 stsp
568 4ba14133 2020-03-20 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
569 4ba14133 2020-03-20 stsp &fetchreq, sizeof(fetchreq)) == -1)
570 4ba14133 2020-03-20 stsp return got_error_from_errno(
571 4ba14133 2020-03-20 stsp "imsg_compose FETCH_SERVER_PROGRESS");
572 33501562 2020-03-18 stsp
573 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
574 4ba14133 2020-03-20 stsp if (err) {
575 659e7fbd 2020-03-20 stsp close(fd);
576 659e7fbd 2020-03-20 stsp return err;
577 659e7fbd 2020-03-20 stsp }
578 4ba14133 2020-03-20 stsp fd = -1;
579 33501562 2020-03-18 stsp
580 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
581 33501562 2020-03-18 stsp const char *name = pe->path;
582 33501562 2020-03-18 stsp size_t name_len = pe->path_len;
583 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
584 33501562 2020-03-18 stsp
585 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
586 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
587 4ba14133 2020-03-20 stsp if (wbuf == NULL)
588 4ba14133 2020-03-20 stsp return got_error_from_errno("imsg_create FETCH_HAVE_REF");
589 4ba14133 2020-03-20 stsp
590 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_ref! */
591 33501562 2020-03-18 stsp if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
592 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
593 33501562 2020-03-18 stsp ibuf_free(wbuf);
594 33501562 2020-03-18 stsp return err;
595 33501562 2020-03-18 stsp }
596 33501562 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
597 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
598 33501562 2020-03-18 stsp ibuf_free(wbuf);
599 33501562 2020-03-18 stsp return err;
600 33501562 2020-03-18 stsp }
601 33501562 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
602 4ba14133 2020-03-20 stsp err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
603 33501562 2020-03-18 stsp ibuf_free(wbuf);
604 33501562 2020-03-18 stsp return err;
605 33501562 2020-03-18 stsp }
606 4ba14133 2020-03-20 stsp
607 4ba14133 2020-03-20 stsp wbuf->fd = -1;
608 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
609 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
610 4ba14133 2020-03-20 stsp if (err)
611 4ba14133 2020-03-20 stsp return err;
612 33501562 2020-03-18 stsp }
613 33501562 2020-03-18 stsp
614 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
615 4ba14133 2020-03-20 stsp const char *name = pe->path;
616 4ba14133 2020-03-20 stsp size_t name_len = pe->path_len;
617 4ba14133 2020-03-20 stsp
618 4ba14133 2020-03-20 stsp len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
619 4ba14133 2020-03-20 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
620 4ba14133 2020-03-20 stsp len);
621 4ba14133 2020-03-20 stsp if (wbuf == NULL)
622 4ba14133 2020-03-20 stsp return got_error_from_errno(
623 62d463ca 2020-10-20 naddy "imsg_create FETCH_WANTED_BRANCH");
624 4ba14133 2020-03-20 stsp
625 4ba14133 2020-03-20 stsp /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
626 4ba14133 2020-03-20 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
627 4ba14133 2020-03-20 stsp err = got_error_from_errno(
628 4ba14133 2020-03-20 stsp "imsg_add FETCH_WANTED_BRANCH");
629 4ba14133 2020-03-20 stsp ibuf_free(wbuf);
630 4ba14133 2020-03-20 stsp return err;
631 4ba14133 2020-03-20 stsp }
632 4ba14133 2020-03-20 stsp if (imsg_add(wbuf, name, name_len) == -1) {
633 4ba14133 2020-03-20 stsp err = got_error_from_errno(
634 62d463ca 2020-10-20 naddy "imsg_add FETCH_WANTED_BRANCH");
635 4ba14133 2020-03-20 stsp ibuf_free(wbuf);
636 4ba14133 2020-03-20 stsp return err;
637 4ba14133 2020-03-20 stsp }
638 4ba14133 2020-03-20 stsp
639 4ba14133 2020-03-20 stsp wbuf->fd = -1;
640 4ba14133 2020-03-20 stsp imsg_close(ibuf, wbuf);
641 4ba14133 2020-03-20 stsp err = flush_imsg(ibuf);
642 4ba14133 2020-03-20 stsp if (err)
643 4ba14133 2020-03-20 stsp return err;
644 4ba14133 2020-03-20 stsp }
645 4ba14133 2020-03-20 stsp
646 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
647 0e4002ca 2020-03-21 stsp const char *name = pe->path;
648 0e4002ca 2020-03-21 stsp size_t name_len = pe->path_len;
649 0e4002ca 2020-03-21 stsp
650 0e4002ca 2020-03-21 stsp len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
651 0e4002ca 2020-03-21 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
652 0e4002ca 2020-03-21 stsp len);
653 0e4002ca 2020-03-21 stsp if (wbuf == NULL)
654 0e4002ca 2020-03-21 stsp return got_error_from_errno(
655 62d463ca 2020-10-20 naddy "imsg_create FETCH_WANTED_REF");
656 0e4002ca 2020-03-21 stsp
657 0e4002ca 2020-03-21 stsp /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
658 0e4002ca 2020-03-21 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
659 0e4002ca 2020-03-21 stsp err = got_error_from_errno(
660 0e4002ca 2020-03-21 stsp "imsg_add FETCH_WANTED_REF");
661 0e4002ca 2020-03-21 stsp ibuf_free(wbuf);
662 0e4002ca 2020-03-21 stsp return err;
663 0e4002ca 2020-03-21 stsp }
664 0e4002ca 2020-03-21 stsp if (imsg_add(wbuf, name, name_len) == -1) {
665 0e4002ca 2020-03-21 stsp err = got_error_from_errno(
666 62d463ca 2020-10-20 naddy "imsg_add FETCH_WANTED_REF");
667 0e4002ca 2020-03-21 stsp ibuf_free(wbuf);
668 0e4002ca 2020-03-21 stsp return err;
669 0e4002ca 2020-03-21 stsp }
670 0e4002ca 2020-03-21 stsp
671 0e4002ca 2020-03-21 stsp wbuf->fd = -1;
672 0e4002ca 2020-03-21 stsp imsg_close(ibuf, wbuf);
673 0e4002ca 2020-03-21 stsp err = flush_imsg(ibuf);
674 0e4002ca 2020-03-21 stsp if (err)
675 0e4002ca 2020-03-21 stsp return err;
676 0e4002ca 2020-03-21 stsp }
677 0e4002ca 2020-03-21 stsp
678 0e4002ca 2020-03-21 stsp
679 4ba14133 2020-03-20 stsp return NULL;
680 4ba14133 2020-03-20 stsp
681 93658fb9 2020-03-18 stsp }
682 93658fb9 2020-03-18 stsp
683 93658fb9 2020-03-18 stsp const struct got_error *
684 f826addf 2020-03-18 stsp got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
685 f826addf 2020-03-18 stsp {
686 f826addf 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
687 531c3985 2020-03-18 stsp }
688 531c3985 2020-03-18 stsp
689 531c3985 2020-03-18 stsp const struct got_error *
690 8f2d01a6 2020-03-18 stsp got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
691 531c3985 2020-03-18 stsp char **refname, struct got_pathlist_head *symrefs, char **server_progress,
692 1d72a2a0 2020-03-24 stsp off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
693 b9f99abf 2020-03-18 stsp {
694 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
695 b9f99abf 2020-03-18 stsp struct imsg imsg;
696 b9f99abf 2020-03-18 stsp size_t datalen;
697 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symrefs *isymrefs = NULL;
698 abe0f35f 2020-03-18 stsp size_t n, remain;
699 abe0f35f 2020-03-18 stsp off_t off;
700 531c3985 2020-03-18 stsp int i;
701 b9f99abf 2020-03-18 stsp
702 8f2d01a6 2020-03-18 stsp *done = 0;
703 8f2d01a6 2020-03-18 stsp *id = NULL;
704 b9f99abf 2020-03-18 stsp *refname = NULL;
705 531c3985 2020-03-18 stsp *server_progress = NULL;
706 5a489642 2020-03-19 stsp *packfile_size = 0;
707 1d72a2a0 2020-03-24 stsp memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
708 b9f99abf 2020-03-18 stsp
709 531c3985 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
710 b9f99abf 2020-03-18 stsp if (err)
711 b9f99abf 2020-03-18 stsp return err;
712 b9f99abf 2020-03-18 stsp
713 b9f99abf 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
714 b9f99abf 2020-03-18 stsp switch (imsg.hdr.type) {
715 b9f99abf 2020-03-18 stsp case GOT_IMSG_ERROR:
716 531c3985 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
717 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
718 531c3985 2020-03-18 stsp break;
719 531c3985 2020-03-18 stsp }
720 b9f99abf 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
721 b9f99abf 2020-03-18 stsp break;
722 abe0f35f 2020-03-18 stsp case GOT_IMSG_FETCH_SYMREFS:
723 abe0f35f 2020-03-18 stsp if (datalen < sizeof(*isymrefs)) {
724 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
725 abe0f35f 2020-03-18 stsp break;
726 abe0f35f 2020-03-18 stsp }
727 abe0f35f 2020-03-18 stsp if (isymrefs != NULL) {
728 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
729 abe0f35f 2020-03-18 stsp break;
730 abe0f35f 2020-03-18 stsp }
731 abe0f35f 2020-03-18 stsp isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
732 abe0f35f 2020-03-18 stsp off = sizeof(*isymrefs);
733 abe0f35f 2020-03-18 stsp remain = datalen - off;
734 abe0f35f 2020-03-18 stsp for (n = 0; n < isymrefs->nsymrefs; n++) {
735 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symref *s;
736 abe0f35f 2020-03-18 stsp char *name, *target;
737 abe0f35f 2020-03-18 stsp if (remain < sizeof(struct got_imsg_fetch_symref)) {
738 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
739 abe0f35f 2020-03-18 stsp goto done;
740 abe0f35f 2020-03-18 stsp }
741 abe0f35f 2020-03-18 stsp s = (struct got_imsg_fetch_symref *)(imsg.data + off);
742 abe0f35f 2020-03-18 stsp off += sizeof(*s);
743 abe0f35f 2020-03-18 stsp remain -= sizeof(*s);
744 abe0f35f 2020-03-18 stsp if (remain < s->name_len) {
745 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
746 abe0f35f 2020-03-18 stsp goto done;
747 abe0f35f 2020-03-18 stsp }
748 abe0f35f 2020-03-18 stsp name = strndup(imsg.data + off, s->name_len);
749 abe0f35f 2020-03-18 stsp if (name == NULL) {
750 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
751 abe0f35f 2020-03-18 stsp goto done;
752 abe0f35f 2020-03-18 stsp }
753 abe0f35f 2020-03-18 stsp off += s->name_len;
754 abe0f35f 2020-03-18 stsp remain -= s->name_len;
755 abe0f35f 2020-03-18 stsp if (remain < s->target_len) {
756 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
757 abe0f35f 2020-03-18 stsp free(name);
758 abe0f35f 2020-03-18 stsp goto done;
759 abe0f35f 2020-03-18 stsp }
760 abe0f35f 2020-03-18 stsp target = strndup(imsg.data + off, s->target_len);
761 abe0f35f 2020-03-18 stsp if (target == NULL) {
762 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
763 abe0f35f 2020-03-18 stsp free(name);
764 abe0f35f 2020-03-18 stsp goto done;
765 abe0f35f 2020-03-18 stsp }
766 abe0f35f 2020-03-18 stsp off += s->target_len;
767 abe0f35f 2020-03-18 stsp remain -= s->target_len;
768 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
769 abe0f35f 2020-03-18 stsp if (err) {
770 abe0f35f 2020-03-18 stsp free(name);
771 abe0f35f 2020-03-18 stsp free(target);
772 abe0f35f 2020-03-18 stsp goto done;
773 abe0f35f 2020-03-18 stsp }
774 abe0f35f 2020-03-18 stsp }
775 abe0f35f 2020-03-18 stsp break;
776 ea7396b9 2020-03-18 stsp case GOT_IMSG_FETCH_REF:
777 531c3985 2020-03-18 stsp if (datalen <= SHA1_DIGEST_LENGTH) {
778 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
779 531c3985 2020-03-18 stsp break;
780 531c3985 2020-03-18 stsp }
781 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
782 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
783 b9f99abf 2020-03-18 stsp err = got_error_from_errno("malloc");
784 b9f99abf 2020-03-18 stsp break;
785 b9f99abf 2020-03-18 stsp }
786 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
787 b9f99abf 2020-03-18 stsp *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
788 b9f99abf 2020-03-18 stsp datalen - SHA1_DIGEST_LENGTH);
789 b9f99abf 2020-03-18 stsp if (*refname == NULL) {
790 b9f99abf 2020-03-18 stsp err = got_error_from_errno("strndup");
791 8f2d01a6 2020-03-18 stsp break;
792 8f2d01a6 2020-03-18 stsp }
793 8f2d01a6 2020-03-18 stsp break;
794 531c3985 2020-03-18 stsp case GOT_IMSG_FETCH_SERVER_PROGRESS:
795 531c3985 2020-03-18 stsp if (datalen == 0) {
796 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
797 531c3985 2020-03-18 stsp break;
798 531c3985 2020-03-18 stsp }
799 531c3985 2020-03-18 stsp *server_progress = strndup(imsg.data, datalen);
800 531c3985 2020-03-18 stsp if (*server_progress == NULL) {
801 531c3985 2020-03-18 stsp err = got_error_from_errno("strndup");
802 531c3985 2020-03-18 stsp break;
803 531c3985 2020-03-18 stsp }
804 531c3985 2020-03-18 stsp for (i = 0; i < datalen; i++) {
805 531c3985 2020-03-18 stsp if (!isprint((unsigned char)(*server_progress)[i]) &&
806 531c3985 2020-03-18 stsp !isspace((unsigned char)(*server_progress)[i])) {
807 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
808 531c3985 2020-03-18 stsp free(*server_progress);
809 531c3985 2020-03-18 stsp *server_progress = NULL;
810 531c3985 2020-03-18 stsp goto done;
811 531c3985 2020-03-18 stsp }
812 531c3985 2020-03-18 stsp }
813 531c3985 2020-03-18 stsp break;
814 d2cdc636 2020-03-18 stsp case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
815 d2cdc636 2020-03-18 stsp if (datalen < sizeof(*packfile_size)) {
816 d2cdc636 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
817 d2cdc636 2020-03-18 stsp break;
818 d2cdc636 2020-03-18 stsp }
819 d2cdc636 2020-03-18 stsp memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
820 d2cdc636 2020-03-18 stsp break;
821 8f2d01a6 2020-03-18 stsp case GOT_IMSG_FETCH_DONE:
822 8f2d01a6 2020-03-18 stsp if (datalen != SHA1_DIGEST_LENGTH) {
823 8f2d01a6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
824 8f2d01a6 2020-03-18 stsp break;
825 8f2d01a6 2020-03-18 stsp }
826 1d72a2a0 2020-03-24 stsp memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
827 8f2d01a6 2020-03-18 stsp *done = 1;
828 b9f99abf 2020-03-18 stsp break;
829 b9f99abf 2020-03-18 stsp default:
830 b887aab6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
831 b887aab6 2020-03-18 stsp break;
832 b9f99abf 2020-03-18 stsp }
833 abe0f35f 2020-03-18 stsp done:
834 b887aab6 2020-03-18 stsp if (err) {
835 8f2d01a6 2020-03-18 stsp free(*id);
836 8f2d01a6 2020-03-18 stsp *id = NULL;
837 b887aab6 2020-03-18 stsp free(*refname);
838 b887aab6 2020-03-18 stsp *refname = NULL;
839 b887aab6 2020-03-18 stsp }
840 b9f99abf 2020-03-18 stsp imsg_free(&imsg);
841 b9f99abf 2020-03-18 stsp return err;
842 93658fb9 2020-03-18 stsp }
843 93658fb9 2020-03-18 stsp
844 93658fb9 2020-03-18 stsp const struct got_error *
845 fd251256 2020-03-24 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
846 668a20f6 2020-03-18 stsp int fd)
847 93658fb9 2020-03-18 stsp {
848 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
849 93658fb9 2020-03-18 stsp
850 668a20f6 2020-03-18 stsp /* Keep in sync with struct got_imsg_index_pack_request */
851 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
852 fd251256 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
853 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
854 93658fb9 2020-03-18 stsp close(fd);
855 93658fb9 2020-03-18 stsp return err;
856 93658fb9 2020-03-18 stsp }
857 baa9fea0 2020-03-18 stsp return flush_imsg(ibuf);
858 baa9fea0 2020-03-18 stsp }
859 baa9fea0 2020-03-18 stsp
860 baa9fea0 2020-03-18 stsp const struct got_error *
861 73ab1060 2020-03-18 stsp got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
862 73ab1060 2020-03-18 stsp {
863 73ab1060 2020-03-18 stsp return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
864 73ab1060 2020-03-18 stsp }
865 73ab1060 2020-03-18 stsp
866 73ab1060 2020-03-18 stsp const struct got_error *
867 668a20f6 2020-03-18 stsp got_privsep_recv_index_progress(int *done, int *nobj_total,
868 668a20f6 2020-03-18 stsp int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
869 668a20f6 2020-03-18 stsp struct imsgbuf *ibuf)
870 93658fb9 2020-03-18 stsp {
871 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
872 93658fb9 2020-03-18 stsp struct imsg imsg;
873 baa9fea0 2020-03-18 stsp struct got_imsg_index_pack_progress *iprogress;
874 baa9fea0 2020-03-18 stsp size_t datalen;
875 93658fb9 2020-03-18 stsp
876 baa9fea0 2020-03-18 stsp *done = 0;
877 668a20f6 2020-03-18 stsp *nobj_total = 0;
878 668a20f6 2020-03-18 stsp *nobj_indexed = 0;
879 668a20f6 2020-03-18 stsp *nobj_resolved = 0;
880 baa9fea0 2020-03-18 stsp
881 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
882 93658fb9 2020-03-18 stsp if (err)
883 93658fb9 2020-03-18 stsp return err;
884 baa9fea0 2020-03-18 stsp
885 baa9fea0 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
886 baa9fea0 2020-03-18 stsp switch (imsg.hdr.type) {
887 baa9fea0 2020-03-18 stsp case GOT_IMSG_ERROR:
888 baa9fea0 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
889 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
890 baa9fea0 2020-03-18 stsp break;
891 baa9fea0 2020-03-18 stsp }
892 baa9fea0 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
893 baa9fea0 2020-03-18 stsp break;
894 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_PROGRESS:
895 baa9fea0 2020-03-18 stsp if (datalen < sizeof(*iprogress)) {
896 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
897 baa9fea0 2020-03-18 stsp break;
898 baa9fea0 2020-03-18 stsp }
899 baa9fea0 2020-03-18 stsp iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
900 668a20f6 2020-03-18 stsp *nobj_total = iprogress->nobj_total;
901 668a20f6 2020-03-18 stsp *nobj_indexed = iprogress->nobj_indexed;
902 668a20f6 2020-03-18 stsp *nobj_loose = iprogress->nobj_loose;
903 668a20f6 2020-03-18 stsp *nobj_resolved = iprogress->nobj_resolved;
904 baa9fea0 2020-03-18 stsp break;
905 baa9fea0 2020-03-18 stsp case GOT_IMSG_IDXPACK_DONE:
906 baa9fea0 2020-03-18 stsp if (datalen != 0) {
907 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
908 baa9fea0 2020-03-18 stsp break;
909 baa9fea0 2020-03-18 stsp }
910 baa9fea0 2020-03-18 stsp *done = 1;
911 baa9fea0 2020-03-18 stsp break;
912 baa9fea0 2020-03-18 stsp default:
913 baa9fea0 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
914 baa9fea0 2020-03-18 stsp break;
915 baa9fea0 2020-03-18 stsp }
916 baa9fea0 2020-03-18 stsp
917 93658fb9 2020-03-18 stsp imsg_free(&imsg);
918 baa9fea0 2020-03-18 stsp return err;
919 93658fb9 2020-03-18 stsp }
920 93658fb9 2020-03-18 stsp
921 93658fb9 2020-03-18 stsp const struct got_error *
922 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
923 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
924 cfd633c2 2018-09-10 stsp {
925 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
926 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
927 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
928 cfd633c2 2018-09-10 stsp
929 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
930 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
931 291624d8 2018-11-07 stsp iobj = imsg->data;
932 cfd633c2 2018-09-10 stsp
933 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
934 cfd633c2 2018-09-10 stsp if (*obj == NULL)
935 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
936 cfd633c2 2018-09-10 stsp
937 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
938 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
939 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
940 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
941 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
942 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
943 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
944 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
945 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
946 876c234b 2018-09-10 stsp }
947 876c234b 2018-09-10 stsp
948 876c234b 2018-09-10 stsp return err;
949 876c234b 2018-09-10 stsp }
950 876c234b 2018-09-10 stsp
951 2178c42e 2018-04-22 stsp const struct got_error *
952 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
953 2178c42e 2018-04-22 stsp {
954 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
955 2178c42e 2018-04-22 stsp struct imsg imsg;
956 c4eae628 2018-04-23 stsp const size_t min_datalen =
957 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
958 2178c42e 2018-04-22 stsp
959 2178c42e 2018-04-22 stsp *obj = NULL;
960 2178c42e 2018-04-22 stsp
961 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
962 2178c42e 2018-04-22 stsp if (err)
963 2178c42e 2018-04-22 stsp return err;
964 2178c42e 2018-04-22 stsp
965 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
966 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
967 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
968 bff6ca00 2018-04-23 stsp break;
969 bff6ca00 2018-04-23 stsp default:
970 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
971 bff6ca00 2018-04-23 stsp break;
972 bff6ca00 2018-04-23 stsp }
973 bff6ca00 2018-04-23 stsp
974 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
975 bff6ca00 2018-04-23 stsp
976 bff6ca00 2018-04-23 stsp return err;
977 c75f7264 2018-09-11 stsp }
978 c75f7264 2018-09-11 stsp
979 c75f7264 2018-09-11 stsp static const struct got_error *
980 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
981 c75f7264 2018-09-11 stsp size_t logmsg_len)
982 c75f7264 2018-09-11 stsp {
983 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
984 c75f7264 2018-09-11 stsp size_t offset, remain;
985 c75f7264 2018-09-11 stsp
986 c75f7264 2018-09-11 stsp offset = 0;
987 c75f7264 2018-09-11 stsp remain = logmsg_len;
988 c75f7264 2018-09-11 stsp while (remain > 0) {
989 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
990 c75f7264 2018-09-11 stsp
991 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
992 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
993 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
994 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
995 fa4ffeb3 2018-11-04 stsp break;
996 fa4ffeb3 2018-11-04 stsp }
997 c75f7264 2018-09-11 stsp
998 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
999 fa4ffeb3 2018-11-04 stsp if (err)
1000 fa4ffeb3 2018-11-04 stsp break;
1001 c75f7264 2018-09-11 stsp
1002 c75f7264 2018-09-11 stsp offset += n;
1003 c75f7264 2018-09-11 stsp remain -= n;
1004 c75f7264 2018-09-11 stsp }
1005 c75f7264 2018-09-11 stsp
1006 fa4ffeb3 2018-11-04 stsp return err;
1007 bff6ca00 2018-04-23 stsp }
1008 bff6ca00 2018-04-23 stsp
1009 bff6ca00 2018-04-23 stsp const struct got_error *
1010 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1011 bff6ca00 2018-04-23 stsp {
1012 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
1013 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
1014 bff6ca00 2018-04-23 stsp uint8_t *buf;
1015 bff6ca00 2018-04-23 stsp size_t len, total;
1016 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1017 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
1018 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
1019 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
1020 bff6ca00 2018-04-23 stsp
1021 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
1022 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
1023 bff6ca00 2018-04-23 stsp
1024 bff6ca00 2018-04-23 stsp buf = malloc(total);
1025 bff6ca00 2018-04-23 stsp if (buf == NULL)
1026 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1027 bff6ca00 2018-04-23 stsp
1028 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
1029 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
1030 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
1031 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
1032 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
1033 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
1034 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
1035 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
1036 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
1037 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
1038 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
1039 b9c33926 2018-11-07 stsp
1040 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
1041 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
1042 b9c33926 2018-11-07 stsp len += author_len;
1043 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
1044 b9c33926 2018-11-07 stsp len += committer_len;
1045 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1046 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1047 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
1048 bff6ca00 2018-04-23 stsp }
1049 bff6ca00 2018-04-23 stsp
1050 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1051 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
1052 bff6ca00 2018-04-23 stsp goto done;
1053 bff6ca00 2018-04-23 stsp }
1054 bff6ca00 2018-04-23 stsp
1055 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
1056 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1057 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
1058 904df868 2018-11-04 stsp if (err)
1059 904df868 2018-11-04 stsp goto done;
1060 904df868 2018-11-04 stsp }
1061 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
1062 bff6ca00 2018-04-23 stsp done:
1063 bff6ca00 2018-04-23 stsp free(buf);
1064 bff6ca00 2018-04-23 stsp return err;
1065 bff6ca00 2018-04-23 stsp }
1066 cfd633c2 2018-09-10 stsp
1067 ca6e02ac 2020-01-07 stsp static const struct got_error *
1068 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
1069 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1070 bff6ca00 2018-04-23 stsp {
1071 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
1072 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
1073 ca6e02ac 2020-01-07 stsp size_t len = 0;
1074 bff6ca00 2018-04-23 stsp int i;
1075 bff6ca00 2018-04-23 stsp
1076 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
1077 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1078 bff6ca00 2018-04-23 stsp
1079 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
1080 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
1081 ca6e02ac 2020-01-07 stsp icommit->committer_len +
1082 ca6e02ac 2020-01-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH)
1083 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1084 bff6ca00 2018-04-23 stsp
1085 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
1086 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1087 ca6e02ac 2020-01-07 stsp
1088 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
1089 bff6ca00 2018-04-23 stsp
1090 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
1091 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
1092 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
1093 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
1094 ca6e02ac 2020-01-07 stsp
1095 ca6e02ac 2020-01-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1096 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
1097 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
1098 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
1099 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
1100 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1101 ca6e02ac 2020-01-07 stsp
1102 ca6e02ac 2020-01-07 stsp if (icommit->author_len == 0) {
1103 ca6e02ac 2020-01-07 stsp (*commit)->author = strdup("");
1104 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
1105 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1106 ca6e02ac 2020-01-07 stsp goto done;
1107 bff6ca00 2018-04-23 stsp }
1108 ca6e02ac 2020-01-07 stsp } else {
1109 ca6e02ac 2020-01-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
1110 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
1111 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1112 ca6e02ac 2020-01-07 stsp goto done;
1113 ca6e02ac 2020-01-07 stsp }
1114 ca6e02ac 2020-01-07 stsp memcpy((*commit)->author, imsg->data + len,
1115 ca6e02ac 2020-01-07 stsp icommit->author_len);
1116 ca6e02ac 2020-01-07 stsp (*commit)->author[icommit->author_len] = '\0';
1117 ca6e02ac 2020-01-07 stsp }
1118 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
1119 bff6ca00 2018-04-23 stsp
1120 ca6e02ac 2020-01-07 stsp if (icommit->committer_len == 0) {
1121 ca6e02ac 2020-01-07 stsp (*commit)->committer = strdup("");
1122 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
1123 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1124 ca6e02ac 2020-01-07 stsp goto done;
1125 ca6e02ac 2020-01-07 stsp }
1126 ca6e02ac 2020-01-07 stsp } else {
1127 ca6e02ac 2020-01-07 stsp (*commit)->committer =
1128 ca6e02ac 2020-01-07 stsp malloc(icommit->committer_len + 1);
1129 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
1130 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1131 ca6e02ac 2020-01-07 stsp goto done;
1132 ca6e02ac 2020-01-07 stsp }
1133 ca6e02ac 2020-01-07 stsp memcpy((*commit)->committer, imsg->data + len,
1134 ca6e02ac 2020-01-07 stsp icommit->committer_len);
1135 ca6e02ac 2020-01-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
1136 ca6e02ac 2020-01-07 stsp }
1137 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
1138 ca6e02ac 2020-01-07 stsp
1139 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
1140 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
1141 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1142 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1143 ca6e02ac 2020-01-07 stsp goto done;
1144 ca6e02ac 2020-01-07 stsp }
1145 ca6e02ac 2020-01-07 stsp } else {
1146 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
1147 ca6e02ac 2020-01-07 stsp
1148 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1149 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1150 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1151 ca6e02ac 2020-01-07 stsp goto done;
1152 bff6ca00 2018-04-23 stsp }
1153 ca6e02ac 2020-01-07 stsp while (remain > 0) {
1154 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
1155 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1156 ca6e02ac 2020-01-07 stsp remain);
1157 6c281f94 2018-06-11 stsp
1158 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1159 ca6e02ac 2020-01-07 stsp if (err)
1160 ca6e02ac 2020-01-07 stsp goto done;
1161 bff6ca00 2018-04-23 stsp
1162 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1163 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1164 ca6e02ac 2020-01-07 stsp goto done;
1165 bff6ca00 2018-04-23 stsp }
1166 c75f7264 2018-09-11 stsp
1167 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
1168 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
1169 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
1170 ca6e02ac 2020-01-07 stsp offset += n;
1171 ca6e02ac 2020-01-07 stsp remain -= n;
1172 bff6ca00 2018-04-23 stsp }
1173 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
1174 ca6e02ac 2020-01-07 stsp }
1175 bff6ca00 2018-04-23 stsp
1176 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
1177 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1178 86acc566 2018-04-23 stsp
1179 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1180 ca6e02ac 2020-01-07 stsp if (err)
1181 ca6e02ac 2020-01-07 stsp break;
1182 ca6e02ac 2020-01-07 stsp memcpy(qid->id, imsg->data + len +
1183 ca6e02ac 2020-01-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1184 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1185 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
1186 ca6e02ac 2020-01-07 stsp }
1187 ca6e02ac 2020-01-07 stsp done:
1188 ca6e02ac 2020-01-07 stsp if (err) {
1189 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
1190 ca6e02ac 2020-01-07 stsp *commit = NULL;
1191 ca6e02ac 2020-01-07 stsp }
1192 ca6e02ac 2020-01-07 stsp return err;
1193 ca6e02ac 2020-01-07 stsp }
1194 ca6e02ac 2020-01-07 stsp
1195 ca6e02ac 2020-01-07 stsp const struct got_error *
1196 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1197 ca6e02ac 2020-01-07 stsp {
1198 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1199 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1200 ca6e02ac 2020-01-07 stsp size_t datalen;
1201 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
1202 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
1203 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
1204 ca6e02ac 2020-01-07 stsp
1205 ca6e02ac 2020-01-07 stsp *commit = NULL;
1206 ca6e02ac 2020-01-07 stsp
1207 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1208 ca6e02ac 2020-01-07 stsp if (err)
1209 ca6e02ac 2020-01-07 stsp return err;
1210 ca6e02ac 2020-01-07 stsp
1211 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1212 ca6e02ac 2020-01-07 stsp
1213 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1214 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1215 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1216 2178c42e 2018-04-22 stsp break;
1217 8c580685 2018-04-22 stsp default:
1218 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1219 8c580685 2018-04-22 stsp break;
1220 2178c42e 2018-04-22 stsp }
1221 2178c42e 2018-04-22 stsp
1222 2178c42e 2018-04-22 stsp imsg_free(&imsg);
1223 e033d803 2018-04-23 stsp
1224 e033d803 2018-04-23 stsp return err;
1225 e033d803 2018-04-23 stsp }
1226 e033d803 2018-04-23 stsp
1227 e033d803 2018-04-23 stsp const struct got_error *
1228 3022d272 2019-11-14 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1229 3022d272 2019-11-14 stsp int nentries)
1230 e033d803 2018-04-23 stsp {
1231 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1232 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
1233 3022d272 2019-11-14 stsp struct got_pathlist_entry *pe;
1234 b00c9821 2018-11-04 stsp size_t totlen;
1235 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
1236 e033d803 2018-04-23 stsp
1237 3022d272 2019-11-14 stsp itree.nentries = nentries;
1238 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1239 e033d803 2018-04-23 stsp == -1)
1240 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
1241 e033d803 2018-04-23 stsp
1242 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
1243 6eb07a17 2018-11-04 stsp nimsg = 1;
1244 3022d272 2019-11-14 stsp TAILQ_FOREACH(pe, entries, entry) {
1245 3022d272 2019-11-14 stsp const char *name = pe->path;
1246 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte = pe->data;
1247 3022d272 2019-11-14 stsp struct ibuf *wbuf;
1248 3022d272 2019-11-14 stsp size_t namelen = strlen(name);
1249 cd9e913a 2019-11-27 stsp size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1250 e033d803 2018-04-23 stsp
1251 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
1252 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
1253 e033d803 2018-04-23 stsp
1254 6eb07a17 2018-11-04 stsp nimsg++;
1255 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1256 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
1257 b00c9821 2018-11-04 stsp if (err)
1258 b00c9821 2018-11-04 stsp return err;
1259 6eb07a17 2018-11-04 stsp nimsg = 0;
1260 b00c9821 2018-11-04 stsp }
1261 b00c9821 2018-11-04 stsp
1262 3022d272 2019-11-14 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1263 3022d272 2019-11-14 stsp if (wbuf == NULL)
1264 3022d272 2019-11-14 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
1265 e033d803 2018-04-23 stsp
1266 3022d272 2019-11-14 stsp /* Keep in sync with struct got_imsg_tree_object definition! */
1267 3b647085 2019-11-23 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1268 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1269 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1270 e033d803 2018-04-23 stsp return err;
1271 3b647085 2019-11-23 stsp }
1272 3b647085 2019-11-23 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1273 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1274 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1275 3022d272 2019-11-14 stsp return err;
1276 3b647085 2019-11-23 stsp }
1277 3022d272 2019-11-14 stsp
1278 3b647085 2019-11-23 stsp if (imsg_add(wbuf, name, namelen) == -1) {
1279 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1280 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1281 3022d272 2019-11-14 stsp return err;
1282 3b647085 2019-11-23 stsp }
1283 3022d272 2019-11-14 stsp
1284 3022d272 2019-11-14 stsp wbuf->fd = -1;
1285 3022d272 2019-11-14 stsp imsg_close(ibuf, wbuf);
1286 3022d272 2019-11-14 stsp
1287 b00c9821 2018-11-04 stsp totlen += len;
1288 e033d803 2018-04-23 stsp }
1289 e033d803 2018-04-23 stsp
1290 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
1291 e033d803 2018-04-23 stsp }
1292 e033d803 2018-04-23 stsp
1293 e033d803 2018-04-23 stsp const struct got_error *
1294 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1295 e033d803 2018-04-23 stsp {
1296 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1297 e033d803 2018-04-23 stsp const size_t min_datalen =
1298 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
1299 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
1300 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
1301 e033d803 2018-04-23 stsp int nentries = 0;
1302 2178c42e 2018-04-22 stsp
1303 e033d803 2018-04-23 stsp *tree = NULL;
1304 e033d803 2018-04-23 stsp get_more:
1305 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
1306 e033d803 2018-04-23 stsp if (err)
1307 1e51f5b9 2018-04-23 stsp goto done;
1308 e033d803 2018-04-23 stsp
1309 656b1f76 2019-05-11 jcs for (;;) {
1310 e033d803 2018-04-23 stsp struct imsg imsg;
1311 e033d803 2018-04-23 stsp size_t n;
1312 e033d803 2018-04-23 stsp size_t datalen;
1313 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
1314 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
1315 e033d803 2018-04-23 stsp
1316 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
1317 e033d803 2018-04-23 stsp if (n == 0) {
1318 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries)
1319 e033d803 2018-04-23 stsp goto get_more;
1320 e033d803 2018-04-23 stsp break;
1321 e033d803 2018-04-23 stsp }
1322 e033d803 2018-04-23 stsp
1323 fca1f6ad 2020-08-27 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1324 fca1f6ad 2020-08-27 stsp imsg_free(&imsg);
1325 fca1f6ad 2020-08-27 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1326 fca1f6ad 2020-08-27 stsp break;
1327 fca1f6ad 2020-08-27 stsp }
1328 e033d803 2018-04-23 stsp
1329 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1330 e033d803 2018-04-23 stsp
1331 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
1332 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
1333 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
1334 e033d803 2018-04-23 stsp break;
1335 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
1336 e033d803 2018-04-23 stsp /* This message should only appear once. */
1337 e033d803 2018-04-23 stsp if (*tree != NULL) {
1338 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1339 e033d803 2018-04-23 stsp break;
1340 e033d803 2018-04-23 stsp }
1341 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
1342 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1343 e033d803 2018-04-23 stsp break;
1344 e033d803 2018-04-23 stsp }
1345 291624d8 2018-11-07 stsp itree = imsg.data;
1346 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
1347 e033d803 2018-04-23 stsp if (*tree == NULL) {
1348 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1349 e033d803 2018-04-23 stsp break;
1350 e033d803 2018-04-23 stsp }
1351 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
1352 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
1353 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
1354 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
1355 56e0773d 2019-11-28 stsp break;
1356 56e0773d 2019-11-28 stsp }
1357 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1358 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1359 e033d803 2018-04-23 stsp break;
1360 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
1361 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
1362 e033d803 2018-04-23 stsp if (*tree == NULL) {
1363 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1364 e033d803 2018-04-23 stsp break;
1365 e033d803 2018-04-23 stsp }
1366 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1367 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1368 e033d803 2018-04-23 stsp break;
1369 e033d803 2018-04-23 stsp }
1370 e033d803 2018-04-23 stsp
1371 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
1372 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
1373 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1374 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1375 e033d803 2018-04-23 stsp break;
1376 e033d803 2018-04-23 stsp }
1377 c0588d8d 2018-11-07 stsp ite = imsg.data;
1378 052d4dc3 2018-04-23 stsp
1379 56e0773d 2019-11-28 stsp if (datalen + 1 > sizeof(te->name)) {
1380 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1381 e033d803 2018-04-23 stsp break;
1382 e033d803 2018-04-23 stsp }
1383 56e0773d 2019-11-28 stsp te = &(*tree)->entries[nentries];
1384 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1385 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
1386 e033d803 2018-04-23 stsp
1387 56e0773d 2019-11-28 stsp memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1388 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
1389 56e0773d 2019-11-28 stsp te->idx = nentries;
1390 e033d803 2018-04-23 stsp nentries++;
1391 e033d803 2018-04-23 stsp break;
1392 e033d803 2018-04-23 stsp default:
1393 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1394 e033d803 2018-04-23 stsp break;
1395 e033d803 2018-04-23 stsp }
1396 e033d803 2018-04-23 stsp
1397 e033d803 2018-04-23 stsp imsg_free(&imsg);
1398 d6b7d054 2020-08-27 stsp if (err)
1399 d6b7d054 2020-08-27 stsp break;
1400 e033d803 2018-04-23 stsp }
1401 1e51f5b9 2018-04-23 stsp done:
1402 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
1403 1e51f5b9 2018-04-23 stsp if (err == NULL)
1404 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1405 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
1406 e033d803 2018-04-23 stsp *tree = NULL;
1407 ff6b18f8 2018-04-24 stsp }
1408 ff6b18f8 2018-04-24 stsp
1409 ff6b18f8 2018-04-24 stsp return err;
1410 ff6b18f8 2018-04-24 stsp }
1411 ff6b18f8 2018-04-24 stsp
1412 ff6b18f8 2018-04-24 stsp const struct got_error *
1413 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1414 ac544f8c 2019-01-13 stsp const uint8_t *data)
1415 ff6b18f8 2018-04-24 stsp {
1416 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1417 2967a784 2018-04-24 stsp
1418 2967a784 2018-04-24 stsp iblob.size = size;
1419 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
1420 2967a784 2018-04-24 stsp
1421 ac544f8c 2019-01-13 stsp if (data) {
1422 ac544f8c 2019-01-13 stsp uint8_t *buf;
1423 ac544f8c 2019-01-13 stsp
1424 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1425 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
1426 ac544f8c 2019-01-13 stsp
1427 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
1428 ac544f8c 2019-01-13 stsp if (buf == NULL)
1429 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1430 ff6b18f8 2018-04-24 stsp
1431 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
1432 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
1433 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1434 62d463ca 2020-10-20 naddy sizeof(iblob) + size) == -1) {
1435 ac544f8c 2019-01-13 stsp free(buf);
1436 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1437 ac544f8c 2019-01-13 stsp }
1438 ac544f8c 2019-01-13 stsp free(buf);
1439 ac544f8c 2019-01-13 stsp } else {
1440 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1441 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1442 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1443 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1444 ac544f8c 2019-01-13 stsp }
1445 ac544f8c 2019-01-13 stsp
1446 ac544f8c 2019-01-13 stsp
1447 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1448 ff6b18f8 2018-04-24 stsp }
1449 ff6b18f8 2018-04-24 stsp
1450 ff6b18f8 2018-04-24 stsp const struct got_error *
1451 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1452 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1453 ff6b18f8 2018-04-24 stsp {
1454 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1455 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1456 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1457 ff6b18f8 2018-04-24 stsp size_t datalen;
1458 ff6b18f8 2018-04-24 stsp
1459 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1460 ac544f8c 2019-01-13 stsp
1461 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1462 ff6b18f8 2018-04-24 stsp if (err)
1463 ff6b18f8 2018-04-24 stsp return err;
1464 ff6b18f8 2018-04-24 stsp
1465 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1466 ff6b18f8 2018-04-24 stsp
1467 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1468 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1469 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1470 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1471 18336eed 2018-11-04 stsp break;
1472 18336eed 2018-11-04 stsp }
1473 291624d8 2018-11-07 stsp iblob = imsg.data;
1474 291624d8 2018-11-07 stsp *size = iblob->size;
1475 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1476 ac544f8c 2019-01-13 stsp
1477 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1478 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1479 ac544f8c 2019-01-13 stsp break;
1480 ac544f8c 2019-01-13 stsp }
1481 ac544f8c 2019-01-13 stsp
1482 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1483 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1484 ac544f8c 2019-01-13 stsp break;
1485 ac544f8c 2019-01-13 stsp }
1486 ac544f8c 2019-01-13 stsp
1487 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1488 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1490 ac544f8c 2019-01-13 stsp break;
1491 ac544f8c 2019-01-13 stsp }
1492 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1493 f4a881ce 2018-11-17 stsp break;
1494 f4a881ce 2018-11-17 stsp default:
1495 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1496 f4a881ce 2018-11-17 stsp break;
1497 f4a881ce 2018-11-17 stsp }
1498 f4a881ce 2018-11-17 stsp
1499 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1500 f4a881ce 2018-11-17 stsp
1501 f4a881ce 2018-11-17 stsp return err;
1502 f4a881ce 2018-11-17 stsp }
1503 f4a881ce 2018-11-17 stsp
1504 f4a881ce 2018-11-17 stsp static const struct got_error *
1505 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1506 f4a881ce 2018-11-17 stsp {
1507 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1508 f4a881ce 2018-11-17 stsp size_t offset, remain;
1509 f4a881ce 2018-11-17 stsp
1510 f4a881ce 2018-11-17 stsp offset = 0;
1511 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1512 f4a881ce 2018-11-17 stsp while (remain > 0) {
1513 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1514 f4a881ce 2018-11-17 stsp
1515 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1516 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1517 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1518 f4a881ce 2018-11-17 stsp break;
1519 f4a881ce 2018-11-17 stsp }
1520 f4a881ce 2018-11-17 stsp
1521 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1522 f4a881ce 2018-11-17 stsp if (err)
1523 f4a881ce 2018-11-17 stsp break;
1524 f4a881ce 2018-11-17 stsp
1525 f4a881ce 2018-11-17 stsp offset += n;
1526 f4a881ce 2018-11-17 stsp remain -= n;
1527 f4a881ce 2018-11-17 stsp }
1528 f4a881ce 2018-11-17 stsp
1529 f4a881ce 2018-11-17 stsp return err;
1530 f4a881ce 2018-11-17 stsp }
1531 f4a881ce 2018-11-17 stsp
1532 f4a881ce 2018-11-17 stsp const struct got_error *
1533 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1534 f4a881ce 2018-11-17 stsp {
1535 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1536 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1537 f4a881ce 2018-11-17 stsp uint8_t *buf;
1538 f4a881ce 2018-11-17 stsp size_t len, total;
1539 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1540 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1541 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1542 f4a881ce 2018-11-17 stsp
1543 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1544 f4a881ce 2018-11-17 stsp
1545 f4a881ce 2018-11-17 stsp buf = malloc(total);
1546 f4a881ce 2018-11-17 stsp if (buf == NULL)
1547 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1548 f4a881ce 2018-11-17 stsp
1549 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1550 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1551 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1552 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1553 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1554 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1555 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1556 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1557 f4a881ce 2018-11-17 stsp
1558 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1559 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1560 f4a881ce 2018-11-17 stsp len += tag_len;
1561 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1562 f4a881ce 2018-11-17 stsp len += tagger_len;
1563 f4a881ce 2018-11-17 stsp
1564 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1565 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1566 f4a881ce 2018-11-17 stsp goto done;
1567 f4a881ce 2018-11-17 stsp }
1568 f4a881ce 2018-11-17 stsp
1569 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1570 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1571 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1572 f4a881ce 2018-11-17 stsp if (err)
1573 f4a881ce 2018-11-17 stsp goto done;
1574 f4a881ce 2018-11-17 stsp }
1575 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1576 f4a881ce 2018-11-17 stsp done:
1577 f4a881ce 2018-11-17 stsp free(buf);
1578 f4a881ce 2018-11-17 stsp return err;
1579 f4a881ce 2018-11-17 stsp }
1580 f4a881ce 2018-11-17 stsp
1581 f4a881ce 2018-11-17 stsp const struct got_error *
1582 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1583 f4a881ce 2018-11-17 stsp {
1584 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1585 f4a881ce 2018-11-17 stsp struct imsg imsg;
1586 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1587 f4a881ce 2018-11-17 stsp size_t len, datalen;
1588 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1589 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1590 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1591 f4a881ce 2018-11-17 stsp
1592 f4a881ce 2018-11-17 stsp *tag = NULL;
1593 f4a881ce 2018-11-17 stsp
1594 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1595 f4a881ce 2018-11-17 stsp if (err)
1596 f4a881ce 2018-11-17 stsp return err;
1597 f4a881ce 2018-11-17 stsp
1598 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1599 f4a881ce 2018-11-17 stsp len = 0;
1600 f4a881ce 2018-11-17 stsp
1601 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1602 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1603 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1604 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1605 f4a881ce 2018-11-17 stsp break;
1606 f4a881ce 2018-11-17 stsp }
1607 f4a881ce 2018-11-17 stsp itag = imsg.data;
1608 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1609 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1610 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1611 f4a881ce 2018-11-17 stsp break;
1612 f4a881ce 2018-11-17 stsp }
1613 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1614 f4a881ce 2018-11-17 stsp
1615 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1616 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1617 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1618 f4a881ce 2018-11-17 stsp break;
1619 f4a881ce 2018-11-17 stsp }
1620 f4a881ce 2018-11-17 stsp
1621 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1622 f4a881ce 2018-11-17 stsp
1623 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1624 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1625 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1626 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1627 f4a881ce 2018-11-17 stsp break;
1628 f4a881ce 2018-11-17 stsp }
1629 f4a881ce 2018-11-17 stsp } else {
1630 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1631 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1632 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1633 f4a881ce 2018-11-17 stsp break;
1634 f4a881ce 2018-11-17 stsp }
1635 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1636 f4a881ce 2018-11-17 stsp itag->tag_len);
1637 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1638 f4a881ce 2018-11-17 stsp }
1639 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1640 f4a881ce 2018-11-17 stsp
1641 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1642 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1643 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1644 f4a881ce 2018-11-17 stsp
1645 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1646 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1647 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1648 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1649 f4a881ce 2018-11-17 stsp break;
1650 f4a881ce 2018-11-17 stsp }
1651 f4a881ce 2018-11-17 stsp } else {
1652 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1653 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1654 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1655 f4a881ce 2018-11-17 stsp break;
1656 f4a881ce 2018-11-17 stsp }
1657 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1658 f4a881ce 2018-11-17 stsp itag->tagger_len);
1659 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1660 f4a881ce 2018-11-17 stsp }
1661 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1662 f4a881ce 2018-11-17 stsp
1663 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1664 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1665 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1666 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1667 f4a881ce 2018-11-17 stsp break;
1668 f4a881ce 2018-11-17 stsp }
1669 f4a881ce 2018-11-17 stsp } else {
1670 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1671 f4a881ce 2018-11-17 stsp
1672 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1673 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1674 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1675 f4a881ce 2018-11-17 stsp break;
1676 f4a881ce 2018-11-17 stsp }
1677 f4a881ce 2018-11-17 stsp while (remain > 0) {
1678 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1679 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1680 f4a881ce 2018-11-17 stsp remain);
1681 f4a881ce 2018-11-17 stsp
1682 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1683 f4a881ce 2018-11-17 stsp if (err)
1684 f4a881ce 2018-11-17 stsp return err;
1685 f4a881ce 2018-11-17 stsp
1686 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1687 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1688 f4a881ce 2018-11-17 stsp
1689 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1690 f4a881ce 2018-11-17 stsp n);
1691 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1692 f4a881ce 2018-11-17 stsp offset += n;
1693 f4a881ce 2018-11-17 stsp remain -= n;
1694 f4a881ce 2018-11-17 stsp }
1695 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1696 f4a881ce 2018-11-17 stsp }
1697 f4a881ce 2018-11-17 stsp
1698 ff6b18f8 2018-04-24 stsp break;
1699 ff6b18f8 2018-04-24 stsp default:
1700 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1701 ff6b18f8 2018-04-24 stsp break;
1702 e033d803 2018-04-23 stsp }
1703 e033d803 2018-04-23 stsp
1704 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1705 ff6b18f8 2018-04-24 stsp
1706 2178c42e 2018-04-22 stsp return err;
1707 2178c42e 2018-04-22 stsp }
1708 876c234b 2018-09-10 stsp
1709 876c234b 2018-09-10 stsp const struct got_error *
1710 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1711 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1712 876c234b 2018-09-10 stsp {
1713 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1714 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1715 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1716 876c234b 2018-09-10 stsp int fd;
1717 876c234b 2018-09-10 stsp
1718 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1719 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1720 876c234b 2018-09-10 stsp if (fd == -1)
1721 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1722 876c234b 2018-09-10 stsp
1723 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1724 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1725 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1726 41496140 2019-02-21 stsp close(fd);
1727 41496140 2019-02-21 stsp return err;
1728 41496140 2019-02-21 stsp }
1729 876c234b 2018-09-10 stsp
1730 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1731 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1732 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1733 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1734 876c234b 2018-09-10 stsp
1735 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1736 876c234b 2018-09-10 stsp if (fd == -1)
1737 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1738 876c234b 2018-09-10 stsp
1739 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1740 41496140 2019-02-21 stsp == -1) {
1741 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1742 41496140 2019-02-21 stsp close(fd);
1743 41496140 2019-02-21 stsp return err;
1744 41496140 2019-02-21 stsp }
1745 876c234b 2018-09-10 stsp
1746 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1747 876c234b 2018-09-10 stsp }
1748 876c234b 2018-09-10 stsp
1749 876c234b 2018-09-10 stsp const struct got_error *
1750 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1751 106807b4 2018-09-15 stsp struct got_object_id *id)
1752 876c234b 2018-09-10 stsp {
1753 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1754 876c234b 2018-09-10 stsp
1755 876c234b 2018-09-10 stsp iobj.idx = idx;
1756 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1757 876c234b 2018-09-10 stsp
1758 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1759 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1760 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1761 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1762 aba9c984 2019-09-08 stsp
1763 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1764 aba9c984 2019-09-08 stsp }
1765 aba9c984 2019-09-08 stsp
1766 aba9c984 2019-09-08 stsp const struct got_error *
1767 59d1e4a0 2021-03-10 stsp got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1768 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
1769 59d1e4a0 2021-03-10 stsp {
1770 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
1771 59d1e4a0 2021-03-10 stsp
1772 59d1e4a0 2021-03-10 stsp iobj.idx = idx;
1773 59d1e4a0 2021-03-10 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1774 59d1e4a0 2021-03-10 stsp
1775 59d1e4a0 2021-03-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
1776 59d1e4a0 2021-03-10 stsp &iobj, sizeof(iobj)) == -1)
1777 59d1e4a0 2021-03-10 stsp return got_error_from_errno("imsg_compose "
1778 59d1e4a0 2021-03-10 stsp "PACKED_OBJECT_REQUEST");
1779 59d1e4a0 2021-03-10 stsp
1780 59d1e4a0 2021-03-10 stsp return flush_imsg(ibuf);
1781 59d1e4a0 2021-03-10 stsp }
1782 59d1e4a0 2021-03-10 stsp
1783 59d1e4a0 2021-03-10 stsp const struct got_error *
1784 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1785 aba9c984 2019-09-08 stsp {
1786 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1787 aba9c984 2019-09-08 stsp
1788 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1789 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
1790 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
1791 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
1792 aba9c984 2019-09-08 stsp close(fd);
1793 aba9c984 2019-09-08 stsp return err;
1794 aba9c984 2019-09-08 stsp }
1795 aba9c984 2019-09-08 stsp
1796 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1797 aba9c984 2019-09-08 stsp }
1798 aba9c984 2019-09-08 stsp
1799 aba9c984 2019-09-08 stsp const struct got_error *
1800 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1801 aba9c984 2019-09-08 stsp {
1802 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1803 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1804 aba9c984 2019-09-08 stsp NULL, 0) == -1)
1805 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1806 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1807 aba9c984 2019-09-08 stsp
1808 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1809 aba9c984 2019-09-08 stsp }
1810 aba9c984 2019-09-08 stsp
1811 aba9c984 2019-09-08 stsp const struct got_error *
1812 20b7abb3 2020-10-22 stsp got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
1813 20b7abb3 2020-10-22 stsp {
1814 20b7abb3 2020-10-22 stsp if (imsg_compose(ibuf,
1815 20b7abb3 2020-10-22 stsp GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
1816 20b7abb3 2020-10-22 stsp NULL, 0) == -1)
1817 20b7abb3 2020-10-22 stsp return got_error_from_errno("imsg_compose "
1818 20b7abb3 2020-10-22 stsp "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
1819 20b7abb3 2020-10-22 stsp
1820 20b7abb3 2020-10-22 stsp return flush_imsg(ibuf);
1821 20b7abb3 2020-10-22 stsp }
1822 20b7abb3 2020-10-22 stsp
1823 20b7abb3 2020-10-22 stsp
1824 20b7abb3 2020-10-22 stsp const struct got_error *
1825 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1826 aba9c984 2019-09-08 stsp {
1827 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1828 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1829 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1830 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
1831 aba9c984 2019-09-08 stsp
1832 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1833 aba9c984 2019-09-08 stsp }
1834 aba9c984 2019-09-08 stsp
1835 aba9c984 2019-09-08 stsp const struct got_error *
1836 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1837 aba9c984 2019-09-08 stsp {
1838 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1839 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1840 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1841 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1842 cd95becd 2019-11-29 stsp
1843 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
1844 cd95becd 2019-11-29 stsp }
1845 cd95becd 2019-11-29 stsp
1846 cd95becd 2019-11-29 stsp const struct got_error *
1847 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1848 cd95becd 2019-11-29 stsp {
1849 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
1850 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1851 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
1852 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
1853 aba9c984 2019-09-08 stsp
1854 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1855 aba9c984 2019-09-08 stsp }
1856 aba9c984 2019-09-08 stsp
1857 aba9c984 2019-09-08 stsp const struct got_error *
1858 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1859 9a1cc63f 2020-02-03 stsp {
1860 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
1861 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1862 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
1863 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
1864 9a1cc63f 2020-02-03 stsp
1865 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
1866 9a1cc63f 2020-02-03 stsp }
1867 9a1cc63f 2020-02-03 stsp
1868 9a1cc63f 2020-02-03 stsp const struct got_error *
1869 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1870 aba9c984 2019-09-08 stsp {
1871 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1872 aba9c984 2019-09-08 stsp struct imsg imsg;
1873 aba9c984 2019-09-08 stsp size_t datalen;
1874 aba9c984 2019-09-08 stsp const size_t min_datalen = 0;
1875 aba9c984 2019-09-08 stsp
1876 aba9c984 2019-09-08 stsp *str = NULL;
1877 aba9c984 2019-09-08 stsp
1878 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1879 aba9c984 2019-09-08 stsp if (err)
1880 aba9c984 2019-09-08 stsp return err;
1881 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1882 aba9c984 2019-09-08 stsp
1883 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1884 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
1885 aba9c984 2019-09-08 stsp if (datalen == 0)
1886 aba9c984 2019-09-08 stsp break;
1887 6c13dcd2 2020-09-18 stsp /* datalen does not include terminating \0 */
1888 6c13dcd2 2020-09-18 stsp *str = malloc(datalen + 1);
1889 aba9c984 2019-09-08 stsp if (*str == NULL) {
1890 aba9c984 2019-09-08 stsp err = got_error_from_errno("malloc");
1891 aba9c984 2019-09-08 stsp break;
1892 aba9c984 2019-09-08 stsp }
1893 6c13dcd2 2020-09-18 stsp memcpy(*str, imsg.data, datalen);
1894 6c13dcd2 2020-09-18 stsp (*str)[datalen] = '\0';
1895 aba9c984 2019-09-08 stsp break;
1896 aba9c984 2019-09-08 stsp default:
1897 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1898 aba9c984 2019-09-08 stsp break;
1899 aba9c984 2019-09-08 stsp }
1900 876c234b 2018-09-10 stsp
1901 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1902 aba9c984 2019-09-08 stsp return err;
1903 aba9c984 2019-09-08 stsp }
1904 aba9c984 2019-09-08 stsp
1905 aba9c984 2019-09-08 stsp const struct got_error *
1906 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1907 aba9c984 2019-09-08 stsp {
1908 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1909 aba9c984 2019-09-08 stsp struct imsg imsg;
1910 aba9c984 2019-09-08 stsp size_t datalen;
1911 aba9c984 2019-09-08 stsp const size_t min_datalen =
1912 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
1913 aba9c984 2019-09-08 stsp
1914 aba9c984 2019-09-08 stsp *val = 0;
1915 aba9c984 2019-09-08 stsp
1916 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1917 aba9c984 2019-09-08 stsp if (err)
1918 aba9c984 2019-09-08 stsp return err;
1919 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1920 aba9c984 2019-09-08 stsp
1921 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1922 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
1923 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
1924 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1925 aba9c984 2019-09-08 stsp break;
1926 aba9c984 2019-09-08 stsp }
1927 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
1928 cd95becd 2019-11-29 stsp break;
1929 cd95becd 2019-11-29 stsp default:
1930 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1931 cd95becd 2019-11-29 stsp break;
1932 cd95becd 2019-11-29 stsp }
1933 cd95becd 2019-11-29 stsp
1934 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1935 cd95becd 2019-11-29 stsp return err;
1936 b8adfa55 2020-09-25 stsp }
1937 b8adfa55 2020-09-25 stsp
1938 b8adfa55 2020-09-25 stsp static void
1939 b8adfa55 2020-09-25 stsp free_remote_data(struct got_remote_repo *remote)
1940 b8adfa55 2020-09-25 stsp {
1941 b8adfa55 2020-09-25 stsp int i;
1942 b8adfa55 2020-09-25 stsp
1943 b8adfa55 2020-09-25 stsp free(remote->name);
1944 b8adfa55 2020-09-25 stsp free(remote->url);
1945 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++)
1946 b8adfa55 2020-09-25 stsp free(remote->branches[i]);
1947 b8adfa55 2020-09-25 stsp free(remote->branches);
1948 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++)
1949 99495ddb 2021-01-10 stsp free(remote->refs[i]);
1950 99495ddb 2021-01-10 stsp free(remote->refs);
1951 cd95becd 2019-11-29 stsp }
1952 cd95becd 2019-11-29 stsp
1953 cd95becd 2019-11-29 stsp const struct got_error *
1954 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1955 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
1956 cd95becd 2019-11-29 stsp {
1957 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1958 cd95becd 2019-11-29 stsp struct imsg imsg;
1959 cd95becd 2019-11-29 stsp size_t datalen;
1960 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1961 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1962 cd95becd 2019-11-29 stsp
1963 cd95becd 2019-11-29 stsp *remotes = NULL;
1964 cd95becd 2019-11-29 stsp *nremotes = 0;
1965 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
1966 cd95becd 2019-11-29 stsp
1967 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1968 cd95becd 2019-11-29 stsp if (err)
1969 cd95becd 2019-11-29 stsp return err;
1970 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1971 cd95becd 2019-11-29 stsp
1972 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1973 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
1974 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
1975 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1976 cd95becd 2019-11-29 stsp break;
1977 cd95becd 2019-11-29 stsp }
1978 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
1979 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
1980 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1981 cd95becd 2019-11-29 stsp return NULL;
1982 cd95becd 2019-11-29 stsp }
1983 aba9c984 2019-09-08 stsp break;
1984 aba9c984 2019-09-08 stsp default:
1985 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
1986 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1987 aba9c984 2019-09-08 stsp }
1988 aba9c984 2019-09-08 stsp
1989 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1990 cd95becd 2019-11-29 stsp
1991 5146eb39 2020-03-20 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1992 cd95becd 2019-11-29 stsp if (*remotes == NULL)
1993 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
1994 cd95becd 2019-11-29 stsp
1995 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
1996 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
1997 3168e5da 2020-09-10 stsp
1998 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1999 cd95becd 2019-11-29 stsp if (err)
2000 cd95becd 2019-11-29 stsp break;
2001 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2002 cd95becd 2019-11-29 stsp
2003 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
2004 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
2005 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
2006 b8adfa55 2020-09-25 stsp memset(remote, 0, sizeof(*remote));
2007 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
2008 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2009 cd95becd 2019-11-29 stsp break;
2010 cd95becd 2019-11-29 stsp }
2011 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
2012 cd95becd 2019-11-29 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
2013 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
2014 cd95becd 2019-11-29 stsp iremote.url_len) > datalen) {
2015 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2016 cd95becd 2019-11-29 stsp break;
2017 cd95becd 2019-11-29 stsp }
2018 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
2019 cd95becd 2019-11-29 stsp iremote.name_len);
2020 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
2021 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
2022 cd95becd 2019-11-29 stsp break;
2023 cd95becd 2019-11-29 stsp }
2024 cd95becd 2019-11-29 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
2025 cd95becd 2019-11-29 stsp iremote.name_len, iremote.url_len);
2026 cd95becd 2019-11-29 stsp if (remote->url == NULL) {
2027 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
2028 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2029 cd95becd 2019-11-29 stsp break;
2030 cd95becd 2019-11-29 stsp }
2031 469dd726 2020-03-20 stsp remote->mirror_references = iremote.mirror_references;
2032 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = iremote.fetch_all_branches;
2033 b8adfa55 2020-09-25 stsp remote->nbranches = 0;
2034 b8adfa55 2020-09-25 stsp remote->branches = NULL;
2035 99495ddb 2021-01-10 stsp remote->nrefs = 0;
2036 99495ddb 2021-01-10 stsp remote->refs = NULL;
2037 cd95becd 2019-11-29 stsp (*nremotes)++;
2038 cd95becd 2019-11-29 stsp break;
2039 cd95becd 2019-11-29 stsp default:
2040 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2041 cd95becd 2019-11-29 stsp break;
2042 cd95becd 2019-11-29 stsp }
2043 cd95becd 2019-11-29 stsp
2044 cd95becd 2019-11-29 stsp imsg_free(&imsg);
2045 cd95becd 2019-11-29 stsp if (err)
2046 cd95becd 2019-11-29 stsp break;
2047 cd95becd 2019-11-29 stsp }
2048 cd95becd 2019-11-29 stsp
2049 cd95becd 2019-11-29 stsp if (err) {
2050 cd95becd 2019-11-29 stsp int i;
2051 b8adfa55 2020-09-25 stsp for (i = 0; i < *nremotes; i++)
2052 b8adfa55 2020-09-25 stsp free_remote_data(&(*remotes)[i]);
2053 cd95becd 2019-11-29 stsp free(*remotes);
2054 cd95becd 2019-11-29 stsp *remotes = NULL;
2055 cd95becd 2019-11-29 stsp *nremotes = 0;
2056 cd95becd 2019-11-29 stsp }
2057 aba9c984 2019-09-08 stsp return err;
2058 257add31 2020-09-09 stsp }
2059 257add31 2020-09-09 stsp
2060 257add31 2020-09-09 stsp const struct got_error *
2061 257add31 2020-09-09 stsp got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2062 257add31 2020-09-09 stsp {
2063 257add31 2020-09-09 stsp const struct got_error *err = NULL;
2064 257add31 2020-09-09 stsp
2065 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2066 257add31 2020-09-09 stsp NULL, 0) == -1) {
2067 257add31 2020-09-09 stsp err = got_error_from_errno("imsg_compose "
2068 257add31 2020-09-09 stsp "GOTCONFIG_PARSE_REQUEST");
2069 257add31 2020-09-09 stsp close(fd);
2070 257add31 2020-09-09 stsp return err;
2071 257add31 2020-09-09 stsp }
2072 257add31 2020-09-09 stsp
2073 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2074 257add31 2020-09-09 stsp }
2075 257add31 2020-09-09 stsp
2076 257add31 2020-09-09 stsp const struct got_error *
2077 257add31 2020-09-09 stsp got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2078 257add31 2020-09-09 stsp {
2079 257add31 2020-09-09 stsp if (imsg_compose(ibuf,
2080 257add31 2020-09-09 stsp GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2081 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose "
2082 257add31 2020-09-09 stsp "GOTCONFIG_AUTHOR_REQUEST");
2083 257add31 2020-09-09 stsp
2084 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2085 ca6e02ac 2020-01-07 stsp }
2086 ca6e02ac 2020-01-07 stsp
2087 ca6e02ac 2020-01-07 stsp const struct got_error *
2088 257add31 2020-09-09 stsp got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2089 257add31 2020-09-09 stsp {
2090 257add31 2020-09-09 stsp if (imsg_compose(ibuf,
2091 257add31 2020-09-09 stsp GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2092 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose "
2093 257add31 2020-09-09 stsp "GOTCONFIG_REMOTE_REQUEST");
2094 257add31 2020-09-09 stsp
2095 257add31 2020-09-09 stsp return flush_imsg(ibuf);
2096 257add31 2020-09-09 stsp }
2097 257add31 2020-09-09 stsp
2098 257add31 2020-09-09 stsp const struct got_error *
2099 257add31 2020-09-09 stsp got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2100 257add31 2020-09-09 stsp {
2101 257add31 2020-09-09 stsp const struct got_error *err = NULL;
2102 257add31 2020-09-09 stsp struct imsg imsg;
2103 257add31 2020-09-09 stsp size_t datalen;
2104 257add31 2020-09-09 stsp const size_t min_datalen = 0;
2105 257add31 2020-09-09 stsp
2106 257add31 2020-09-09 stsp *str = NULL;
2107 257add31 2020-09-09 stsp
2108 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2109 257add31 2020-09-09 stsp if (err)
2110 257add31 2020-09-09 stsp return err;
2111 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2112 257add31 2020-09-09 stsp
2113 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
2114 257add31 2020-09-09 stsp case GOT_IMSG_ERROR:
2115 257add31 2020-09-09 stsp if (datalen < sizeof(struct got_imsg_error)) {
2116 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2117 257add31 2020-09-09 stsp break;
2118 257add31 2020-09-09 stsp }
2119 257add31 2020-09-09 stsp err = recv_imsg_error(&imsg, datalen);
2120 257add31 2020-09-09 stsp break;
2121 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_STR_VAL:
2122 257add31 2020-09-09 stsp if (datalen == 0)
2123 257add31 2020-09-09 stsp break;
2124 5874ea87 2020-09-18 stsp /* datalen does not include terminating \0 */
2125 5874ea87 2020-09-18 stsp *str = malloc(datalen + 1);
2126 257add31 2020-09-09 stsp if (*str == NULL) {
2127 257add31 2020-09-09 stsp err = got_error_from_errno("malloc");
2128 257add31 2020-09-09 stsp break;
2129 257add31 2020-09-09 stsp }
2130 5874ea87 2020-09-18 stsp memcpy(*str, imsg.data, datalen);
2131 5874ea87 2020-09-18 stsp (*str)[datalen] = '\0';
2132 257add31 2020-09-09 stsp break;
2133 257add31 2020-09-09 stsp default:
2134 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2135 257add31 2020-09-09 stsp break;
2136 257add31 2020-09-09 stsp }
2137 257add31 2020-09-09 stsp
2138 257add31 2020-09-09 stsp imsg_free(&imsg);
2139 257add31 2020-09-09 stsp return err;
2140 257add31 2020-09-09 stsp }
2141 257add31 2020-09-09 stsp
2142 b8adfa55 2020-09-25 stsp
2143 257add31 2020-09-09 stsp const struct got_error *
2144 257add31 2020-09-09 stsp got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2145 257add31 2020-09-09 stsp int *nremotes, struct imsgbuf *ibuf)
2146 257add31 2020-09-09 stsp {
2147 257add31 2020-09-09 stsp const struct got_error *err = NULL;
2148 257add31 2020-09-09 stsp struct imsg imsg;
2149 257add31 2020-09-09 stsp size_t datalen;
2150 257add31 2020-09-09 stsp struct got_imsg_remotes iremotes;
2151 257add31 2020-09-09 stsp struct got_imsg_remote iremote;
2152 257add31 2020-09-09 stsp const size_t min_datalen =
2153 257add31 2020-09-09 stsp MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2154 257add31 2020-09-09 stsp
2155 257add31 2020-09-09 stsp *remotes = NULL;
2156 257add31 2020-09-09 stsp *nremotes = 0;
2157 257add31 2020-09-09 stsp iremotes.nremotes = 0;
2158 257add31 2020-09-09 stsp
2159 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2160 257add31 2020-09-09 stsp if (err)
2161 257add31 2020-09-09 stsp return err;
2162 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2163 257add31 2020-09-09 stsp
2164 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
2165 257add31 2020-09-09 stsp case GOT_IMSG_ERROR:
2166 257add31 2020-09-09 stsp if (datalen < sizeof(struct got_imsg_error)) {
2167 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2168 257add31 2020-09-09 stsp break;
2169 257add31 2020-09-09 stsp }
2170 257add31 2020-09-09 stsp err = recv_imsg_error(&imsg, datalen);
2171 257add31 2020-09-09 stsp break;
2172 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTES:
2173 257add31 2020-09-09 stsp if (datalen != sizeof(iremotes)) {
2174 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2175 257add31 2020-09-09 stsp break;
2176 257add31 2020-09-09 stsp }
2177 257add31 2020-09-09 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
2178 257add31 2020-09-09 stsp if (iremotes.nremotes == 0) {
2179 257add31 2020-09-09 stsp imsg_free(&imsg);
2180 257add31 2020-09-09 stsp return NULL;
2181 257add31 2020-09-09 stsp }
2182 257add31 2020-09-09 stsp break;
2183 257add31 2020-09-09 stsp default:
2184 257add31 2020-09-09 stsp imsg_free(&imsg);
2185 257add31 2020-09-09 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2186 257add31 2020-09-09 stsp }
2187 257add31 2020-09-09 stsp
2188 257add31 2020-09-09 stsp imsg_free(&imsg);
2189 257add31 2020-09-09 stsp
2190 257add31 2020-09-09 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2191 257add31 2020-09-09 stsp if (*remotes == NULL)
2192 257add31 2020-09-09 stsp return got_error_from_errno("recallocarray");
2193 257add31 2020-09-09 stsp
2194 257add31 2020-09-09 stsp while (*nremotes < iremotes.nremotes) {
2195 257add31 2020-09-09 stsp struct got_remote_repo *remote;
2196 257add31 2020-09-09 stsp const size_t min_datalen =
2197 257add31 2020-09-09 stsp MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2198 b8adfa55 2020-09-25 stsp int i;
2199 257add31 2020-09-09 stsp
2200 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2201 257add31 2020-09-09 stsp if (err)
2202 257add31 2020-09-09 stsp break;
2203 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2204 257add31 2020-09-09 stsp
2205 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
2206 257add31 2020-09-09 stsp case GOT_IMSG_ERROR:
2207 257add31 2020-09-09 stsp if (datalen < sizeof(struct got_imsg_error)) {
2208 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2209 257add31 2020-09-09 stsp break;
2210 257add31 2020-09-09 stsp }
2211 257add31 2020-09-09 stsp err = recv_imsg_error(&imsg, datalen);
2212 257add31 2020-09-09 stsp break;
2213 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTE:
2214 257add31 2020-09-09 stsp remote = &(*remotes)[*nremotes];
2215 b8adfa55 2020-09-25 stsp memset(remote, 0, sizeof(*remote));
2216 257add31 2020-09-09 stsp if (datalen < sizeof(iremote)) {
2217 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2218 257add31 2020-09-09 stsp break;
2219 257add31 2020-09-09 stsp }
2220 257add31 2020-09-09 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
2221 257add31 2020-09-09 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
2222 257add31 2020-09-09 stsp (sizeof(iremote) + iremote.name_len +
2223 257add31 2020-09-09 stsp iremote.url_len) > datalen) {
2224 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2225 257add31 2020-09-09 stsp break;
2226 257add31 2020-09-09 stsp }
2227 257add31 2020-09-09 stsp remote->name = strndup(imsg.data + sizeof(iremote),
2228 257add31 2020-09-09 stsp iremote.name_len);
2229 257add31 2020-09-09 stsp if (remote->name == NULL) {
2230 257add31 2020-09-09 stsp err = got_error_from_errno("strndup");
2231 257add31 2020-09-09 stsp break;
2232 257add31 2020-09-09 stsp }
2233 257add31 2020-09-09 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
2234 257add31 2020-09-09 stsp iremote.name_len, iremote.url_len);
2235 257add31 2020-09-09 stsp if (remote->url == NULL) {
2236 257add31 2020-09-09 stsp err = got_error_from_errno("strndup");
2237 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2238 257add31 2020-09-09 stsp break;
2239 257add31 2020-09-09 stsp }
2240 257add31 2020-09-09 stsp remote->mirror_references = iremote.mirror_references;
2241 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = iremote.fetch_all_branches;
2242 b8adfa55 2020-09-25 stsp if (iremote.nbranches > 0) {
2243 b8adfa55 2020-09-25 stsp remote->branches = recallocarray(NULL, 0,
2244 b8adfa55 2020-09-25 stsp iremote.nbranches, sizeof(char *));
2245 b8adfa55 2020-09-25 stsp if (remote->branches == NULL) {
2246 b8adfa55 2020-09-25 stsp err = got_error_from_errno("calloc");
2247 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2248 b8adfa55 2020-09-25 stsp break;
2249 b8adfa55 2020-09-25 stsp }
2250 b8adfa55 2020-09-25 stsp }
2251 b8adfa55 2020-09-25 stsp remote->nbranches = 0;
2252 b8adfa55 2020-09-25 stsp for (i = 0; i < iremote.nbranches; i++) {
2253 b8adfa55 2020-09-25 stsp char *branch;
2254 b8adfa55 2020-09-25 stsp err = got_privsep_recv_gotconfig_str(&branch,
2255 b8adfa55 2020-09-25 stsp ibuf);
2256 b8adfa55 2020-09-25 stsp if (err) {
2257 b8adfa55 2020-09-25 stsp free_remote_data(remote);
2258 b8adfa55 2020-09-25 stsp goto done;
2259 b8adfa55 2020-09-25 stsp }
2260 b8adfa55 2020-09-25 stsp remote->branches[i] = branch;
2261 b8adfa55 2020-09-25 stsp remote->nbranches++;
2262 99495ddb 2021-01-10 stsp }
2263 99495ddb 2021-01-10 stsp if (iremote.nrefs > 0) {
2264 99495ddb 2021-01-10 stsp remote->refs = recallocarray(NULL, 0,
2265 99495ddb 2021-01-10 stsp iremote.nrefs, sizeof(char *));
2266 99495ddb 2021-01-10 stsp if (remote->refs == NULL) {
2267 99495ddb 2021-01-10 stsp err = got_error_from_errno("calloc");
2268 99495ddb 2021-01-10 stsp free_remote_data(remote);
2269 99495ddb 2021-01-10 stsp break;
2270 99495ddb 2021-01-10 stsp }
2271 b8adfa55 2020-09-25 stsp }
2272 99495ddb 2021-01-10 stsp remote->nrefs = 0;
2273 99495ddb 2021-01-10 stsp for (i = 0; i < iremote.nrefs; i++) {
2274 99495ddb 2021-01-10 stsp char *ref;
2275 99495ddb 2021-01-10 stsp err = got_privsep_recv_gotconfig_str(&ref,
2276 99495ddb 2021-01-10 stsp ibuf);
2277 99495ddb 2021-01-10 stsp if (err) {
2278 99495ddb 2021-01-10 stsp free_remote_data(remote);
2279 99495ddb 2021-01-10 stsp goto done;
2280 99495ddb 2021-01-10 stsp }
2281 99495ddb 2021-01-10 stsp remote->refs[i] = ref;
2282 99495ddb 2021-01-10 stsp remote->nrefs++;
2283 99495ddb 2021-01-10 stsp }
2284 257add31 2020-09-09 stsp (*nremotes)++;
2285 257add31 2020-09-09 stsp break;
2286 257add31 2020-09-09 stsp default:
2287 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2288 257add31 2020-09-09 stsp break;
2289 257add31 2020-09-09 stsp }
2290 257add31 2020-09-09 stsp
2291 257add31 2020-09-09 stsp imsg_free(&imsg);
2292 257add31 2020-09-09 stsp if (err)
2293 257add31 2020-09-09 stsp break;
2294 257add31 2020-09-09 stsp }
2295 b8adfa55 2020-09-25 stsp done:
2296 257add31 2020-09-09 stsp if (err) {
2297 257add31 2020-09-09 stsp int i;
2298 b8adfa55 2020-09-25 stsp for (i = 0; i < *nremotes; i++)
2299 b8adfa55 2020-09-25 stsp free_remote_data(&(*remotes)[i]);
2300 257add31 2020-09-09 stsp free(*remotes);
2301 257add31 2020-09-09 stsp *remotes = NULL;
2302 257add31 2020-09-09 stsp *nremotes = 0;
2303 257add31 2020-09-09 stsp }
2304 257add31 2020-09-09 stsp return err;
2305 257add31 2020-09-09 stsp }
2306 257add31 2020-09-09 stsp
2307 257add31 2020-09-09 stsp const struct got_error *
2308 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2309 ca6e02ac 2020-01-07 stsp struct got_object_id *id, int idx, const char *path)
2310 ca6e02ac 2020-01-07 stsp {
2311 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2312 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2313 ca6e02ac 2020-01-07 stsp size_t path_len = strlen(path) + 1;
2314 ca6e02ac 2020-01-07 stsp
2315 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2316 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
2317 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
2318 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
2319 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
2320 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2321 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2322 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2323 ca6e02ac 2020-01-07 stsp return err;
2324 ca6e02ac 2020-01-07 stsp }
2325 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2326 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2327 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2328 ca6e02ac 2020-01-07 stsp return err;
2329 ca6e02ac 2020-01-07 stsp }
2330 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, path, path_len) == -1) {
2331 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2332 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2333 ca6e02ac 2020-01-07 stsp return err;
2334 ca6e02ac 2020-01-07 stsp }
2335 ca6e02ac 2020-01-07 stsp
2336 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2337 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2338 ca6e02ac 2020-01-07 stsp
2339 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2340 ca6e02ac 2020-01-07 stsp }
2341 ca6e02ac 2020-01-07 stsp
2342 ca6e02ac 2020-01-07 stsp const struct got_error *
2343 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2344 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
2345 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2346 ca6e02ac 2020-01-07 stsp {
2347 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2348 ca6e02ac 2020-01-07 stsp struct imsg imsg;
2349 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
2350 ca6e02ac 2020-01-07 stsp size_t datalen;
2351 ca6e02ac 2020-01-07 stsp int i, done = 0;
2352 ca6e02ac 2020-01-07 stsp
2353 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
2354 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
2355 ca6e02ac 2020-01-07 stsp
2356 ca6e02ac 2020-01-07 stsp while (!done) {
2357 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2358 ca6e02ac 2020-01-07 stsp if (err)
2359 ca6e02ac 2020-01-07 stsp return err;
2360 ca6e02ac 2020-01-07 stsp
2361 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2362 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
2363 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
2364 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
2365 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
2366 ca6e02ac 2020-01-07 stsp icommits->ncommits * SHA1_DIGEST_LENGTH) {
2367 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2368 ca6e02ac 2020-01-07 stsp break;
2369 ca6e02ac 2020-01-07 stsp }
2370 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
2371 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
2372 ca6e02ac 2020-01-07 stsp uint8_t *sha1 = (uint8_t *)imsg.data +
2373 ca6e02ac 2020-01-07 stsp sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2374 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
2375 ca6e02ac 2020-01-07 stsp if (err)
2376 ca6e02ac 2020-01-07 stsp break;
2377 ca6e02ac 2020-01-07 stsp memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2378 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2379 ca6e02ac 2020-01-07 stsp
2380 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
2381 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
2382 ca6e02ac 2020-01-07 stsp *changed_commit_id =
2383 ca6e02ac 2020-01-07 stsp got_object_id_dup(qid->id);
2384 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2385 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2386 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
2387 ca6e02ac 2020-01-07 stsp break;
2388 ca6e02ac 2020-01-07 stsp }
2389 ca6e02ac 2020-01-07 stsp }
2390 ca6e02ac 2020-01-07 stsp }
2391 ca6e02ac 2020-01-07 stsp break;
2392 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
2393 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2394 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2395 ca6e02ac 2020-01-07 stsp break;
2396 ca6e02ac 2020-01-07 stsp }
2397 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
2398 ca6e02ac 2020-01-07 stsp datalen, ibuf);
2399 ca6e02ac 2020-01-07 stsp break;
2400 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2401 ca6e02ac 2020-01-07 stsp done = 1;
2402 ca6e02ac 2020-01-07 stsp break;
2403 ca6e02ac 2020-01-07 stsp default:
2404 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2405 ca6e02ac 2020-01-07 stsp break;
2406 ca6e02ac 2020-01-07 stsp }
2407 ca6e02ac 2020-01-07 stsp
2408 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
2409 ca6e02ac 2020-01-07 stsp if (err)
2410 ca6e02ac 2020-01-07 stsp break;
2411 ca6e02ac 2020-01-07 stsp }
2412 ca6e02ac 2020-01-07 stsp
2413 ca6e02ac 2020-01-07 stsp if (err)
2414 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
2415 ca6e02ac 2020-01-07 stsp return err;
2416 ca6e02ac 2020-01-07 stsp }
2417 ca6e02ac 2020-01-07 stsp
2418 ca6e02ac 2020-01-07 stsp const struct got_error *
2419 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
2420 63219cd2 2019-01-04 stsp {
2421 c39c25dd 2019-08-09 stsp const char *helpers[] = {
2422 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
2423 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
2424 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
2425 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
2426 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
2427 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
2428 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
2429 257add31 2020-09-09 stsp GOT_PATH_PROG_READ_GOTCONFIG,
2430 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK,
2431 ee448f5f 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK,
2432 c39c25dd 2019-08-09 stsp };
2433 16aeacf7 2020-11-26 stsp size_t i;
2434 63219cd2 2019-01-04 stsp
2435 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
2436 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
2437 c39c25dd 2019-08-09 stsp continue;
2438 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
2439 c39c25dd 2019-08-09 stsp }
2440 c39c25dd 2019-08-09 stsp
2441 63219cd2 2019-01-04 stsp return NULL;
2442 876c234b 2018-09-10 stsp }
2443 aba9c984 2019-09-08 stsp
2444 aba9c984 2019-09-08 stsp void
2445 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2446 aba9c984 2019-09-08 stsp {
2447 08578a35 2021-01-22 stsp if (close(imsg_fds[0]) == -1) {
2448 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2449 aba9c984 2019-09-08 stsp _exit(1);
2450 aba9c984 2019-09-08 stsp }
2451 aba9c984 2019-09-08 stsp
2452 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2453 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2454 aba9c984 2019-09-08 stsp _exit(1);
2455 aba9c984 2019-09-08 stsp }
2456 aba9c984 2019-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2457 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2458 aba9c984 2019-09-08 stsp _exit(1);
2459 aba9c984 2019-09-08 stsp }
2460 aba9c984 2019-09-08 stsp
2461 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
2462 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2463 aba9c984 2019-09-08 stsp strerror(errno));
2464 aba9c984 2019-09-08 stsp _exit(1);
2465 aba9c984 2019-09-08 stsp }
2466 aba9c984 2019-09-08 stsp }