Blame


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