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