2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 2178c42e 2018-04-22 stsp * Permission to use, copy, modify, and distribute this software for any
6 2178c42e 2018-04-22 stsp * purpose with or without fee is hereby granted, provided that the above
7 2178c42e 2018-04-22 stsp * copyright notice and this permission notice appear in all copies.
9 2178c42e 2018-04-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2178c42e 2018-04-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2178c42e 2018-04-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2178c42e 2018-04-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2178c42e 2018-04-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2178c42e 2018-04-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2178c42e 2018-04-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 2178c42e 2018-04-22 stsp #include <sys/types.h>
19 2178c42e 2018-04-22 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
22 876c234b 2018-09-10 stsp #include <sys/wait.h>
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>
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"
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"
50 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 c39c25dd 2019-08-09 stsp #ifndef nitems
54 c39c25dd 2019-08-09 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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)
60 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
63 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
64 2178c42e 2018-04-22 stsp pfd[0].events = events;
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");
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;
76 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
79 c4eae628 2018-04-23 stsp static const struct got_error *
80 e033d803 2018-04-23 stsp read_imsg(struct imsgbuf *ibuf)
82 fe36cf76 2018-04-23 stsp const struct got_error *err;
85 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
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);
96 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
98 e033d803 2018-04-23 stsp return NULL;
101 ad242220 2018-09-08 stsp const struct got_error *
102 876c234b 2018-09-10 stsp got_privsep_wait_for_child(pid_t pid)
104 876c234b 2018-09-10 stsp int child_status;
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");
109 876c234b 2018-09-10 stsp if (!WIFEXITED(child_status))
110 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
112 876c234b 2018-09-10 stsp if (WEXITSTATUS(child_status) != 0)
113 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
115 876c234b 2018-09-10 stsp return NULL;
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)
121 73b7854a 2018-11-11 stsp struct got_imsg_error *ierr;
123 73b7854a 2018-11-11 stsp if (datalen != sizeof(*ierr))
124 73b7854a 2018-11-11 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
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;
134 73b7854a 2018-11-11 stsp return got_error(ierr->code);
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)
141 e033d803 2018-04-23 stsp const struct got_error *err;
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");
148 876c234b 2018-09-10 stsp while (n == 0) {
149 876c234b 2018-09-10 stsp err = read_imsg(ibuf);
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");
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);
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);
165 73b7854a 2018-11-11 stsp return NULL;
168 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
170 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
173 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
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;
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));
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);
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));
203 e033d803 2018-04-23 stsp static const struct got_error *
204 e033d803 2018-04-23 stsp flush_imsg(struct imsgbuf *ibuf)
206 e033d803 2018-04-23 stsp const struct got_error *err;
208 e033d803 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
210 e033d803 2018-04-23 stsp return err;
212 e033d803 2018-04-23 stsp if (imsg_flush(ibuf) == -1)
213 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_flush");
215 e033d803 2018-04-23 stsp return NULL;
218 ad242220 2018-09-08 stsp const struct got_error *
219 ad242220 2018-09-08 stsp got_privsep_send_stop(int fd)
221 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
222 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
224 ad242220 2018-09-08 stsp imsg_init(&ibuf, fd);
226 ad242220 2018-09-08 stsp if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
227 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose STOP");
229 ad242220 2018-09-08 stsp err = flush_imsg(&ibuf);
230 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
231 ad242220 2018-09-08 stsp return err;
234 93658fb9 2020-03-18 stsp const struct got_error *
235 aea5f015 2018-12-24 stsp got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
237 aea5f015 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
239 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT_REQUEST");
241 1785f84a 2018-12-23 stsp return flush_imsg(ibuf);
244 1785f84a 2018-12-23 stsp const struct got_error *
245 1785f84a 2018-12-23 stsp got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
246 1785f84a 2018-12-23 stsp struct got_object_id *id, int pack_idx)
248 41496140 2019-02-21 stsp const struct got_error *err = NULL;
249 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj, *iobjp;
250 1785f84a 2018-12-23 stsp size_t len;
252 1785f84a 2018-12-23 stsp if (id) { /* commit is packed */
253 1785f84a 2018-12-23 stsp iobj.idx = pack_idx;
254 1785f84a 2018-12-23 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
255 1785f84a 2018-12-23 stsp iobjp = &iobj;
256 1785f84a 2018-12-23 stsp len = sizeof(iobj);
258 1785f84a 2018-12-23 stsp iobjp = NULL;
262 1785f84a 2018-12-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
264 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
266 41496140 2019-02-21 stsp return err;
269 13c729f7 2018-12-24 stsp return flush_imsg(ibuf);
272 13c729f7 2018-12-24 stsp const struct got_error *
273 13c729f7 2018-12-24 stsp got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
274 13c729f7 2018-12-24 stsp struct got_object_id *id, int pack_idx)
276 41496140 2019-02-21 stsp const struct got_error *err = NULL;
277 7f358e3b 2019-11-23 stsp struct ibuf *wbuf;
278 7f358e3b 2019-11-23 stsp size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
280 7f358e3b 2019-11-23 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
281 7f358e3b 2019-11-23 stsp if (wbuf == NULL)
282 7f358e3b 2019-11-23 stsp return got_error_from_errno("imsg_create TREE_REQUEST");
284 13c729f7 2018-12-24 stsp if (id) { /* tree is packed */
285 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 7f358e3b 2019-11-23 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
287 7f358e3b 2019-11-23 stsp ibuf_free(wbuf);
288 7f358e3b 2019-11-23 stsp return err;
291 7f358e3b 2019-11-23 stsp if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -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;
298 7f358e3b 2019-11-23 stsp wbuf->fd = fd;
299 7f358e3b 2019-11-23 stsp imsg_close(ibuf, wbuf);
301 268f7291 2018-12-24 stsp return flush_imsg(ibuf);
304 268f7291 2018-12-24 stsp const struct got_error *
305 268f7291 2018-12-24 stsp got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
306 268f7291 2018-12-24 stsp struct got_object_id *id, int pack_idx)
308 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
309 268f7291 2018-12-24 stsp size_t len;
311 268f7291 2018-12-24 stsp if (id) { /* tag is packed */
312 268f7291 2018-12-24 stsp iobj.idx = pack_idx;
313 268f7291 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
314 268f7291 2018-12-24 stsp iobjp = &iobj;
315 268f7291 2018-12-24 stsp len = sizeof(iobj);
317 268f7291 2018-12-24 stsp iobjp = NULL;
321 268f7291 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
323 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TAG_REQUEST");
325 7762fe12 2018-11-05 stsp return flush_imsg(ibuf);
328 7762fe12 2018-11-05 stsp const struct got_error *
329 ebc55e2d 2018-12-24 stsp got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
330 ebc55e2d 2018-12-24 stsp struct got_object_id *id, int pack_idx)
332 41496140 2019-02-21 stsp const struct got_error *err = NULL;
333 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj, *iobjp;
334 ebc55e2d 2018-12-24 stsp size_t len;
336 ebc55e2d 2018-12-24 stsp if (id) { /* blob is packed */
337 ebc55e2d 2018-12-24 stsp iobj.idx = pack_idx;
338 ebc55e2d 2018-12-24 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
339 ebc55e2d 2018-12-24 stsp iobjp = &iobj;
340 ebc55e2d 2018-12-24 stsp len = sizeof(iobj);
342 ebc55e2d 2018-12-24 stsp iobjp = NULL;
346 ebc55e2d 2018-12-24 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
348 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_REQUEST");
349 41496140 2019-02-21 stsp close(infd);
350 41496140 2019-02-21 stsp return err;
353 55da3778 2018-09-10 stsp return flush_imsg(ibuf);
356 55da3778 2018-09-10 stsp const struct got_error *
357 55da3778 2018-09-10 stsp got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
359 41496140 2019-02-21 stsp const struct got_error *err = NULL;
361 ad242220 2018-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
363 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose BLOB_OUTFD");
364 41496140 2019-02-21 stsp close(outfd);
365 41496140 2019-02-21 stsp return err;
368 3840f4c9 2018-09-12 stsp return flush_imsg(ibuf);
371 3840f4c9 2018-09-12 stsp const struct got_error *
372 3840f4c9 2018-09-12 stsp got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
374 41496140 2019-02-21 stsp const struct got_error *err = NULL;
376 3840f4c9 2018-09-12 stsp if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
378 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TMPFD");
380 41496140 2019-02-21 stsp return err;
383 ad242220 2018-09-08 stsp return flush_imsg(ibuf);
386 ad242220 2018-09-08 stsp const struct got_error *
387 876c234b 2018-09-10 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
389 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
391 c59b3346 2018-09-11 stsp memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
392 2178c42e 2018-04-22 stsp iobj.type = obj->type;
393 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
394 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
395 2178c42e 2018-04-22 stsp iobj.size = obj->size;
396 c59b3346 2018-09-11 stsp if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
397 876c234b 2018-09-10 stsp iobj.pack_offset = obj->pack_offset;
398 c59b3346 2018-09-11 stsp iobj.pack_idx = obj->pack_idx;
401 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
403 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose OBJECT");
405 c59b3346 2018-09-11 stsp return flush_imsg(ibuf);
408 cfd633c2 2018-09-10 stsp const struct got_error *
409 33501562 2020-03-18 stsp got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
410 33501562 2020-03-18 stsp struct got_pathlist_head *have_refs)
412 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
413 33501562 2020-03-18 stsp struct ibuf *wbuf;
414 33501562 2020-03-18 stsp size_t len, n_have_refs = 0;
415 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
417 33501562 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_symrefs);
418 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
419 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
420 33501562 2020-03-18 stsp len += sizeof(struct got_imsg_fetch_have_ref) +
421 33501562 2020-03-18 stsp pe->path_len + sizeof(id->sha1);
422 33501562 2020-03-18 stsp n_have_refs++;
424 33501562 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
426 33501562 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
429 33501562 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
430 33501562 2020-03-18 stsp if (wbuf == NULL) {
432 33501562 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_REQUEST");
435 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_refs definition! */
436 33501562 2020-03-18 stsp if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
437 33501562 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REQUEST");
438 33501562 2020-03-18 stsp ibuf_free(wbuf);
440 93658fb9 2020-03-18 stsp return err;
443 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
444 33501562 2020-03-18 stsp const char *name = pe->path;
445 33501562 2020-03-18 stsp size_t name_len = pe->path_len;
446 33501562 2020-03-18 stsp struct got_object_id *id = pe->data;
448 33501562 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_have_ref! */
449 33501562 2020-03-18 stsp if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
450 33501562 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REQUEST");
451 33501562 2020-03-18 stsp ibuf_free(wbuf);
453 33501562 2020-03-18 stsp return err;
455 33501562 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
456 33501562 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REQUEST");
457 33501562 2020-03-18 stsp ibuf_free(wbuf);
459 33501562 2020-03-18 stsp return err;
461 33501562 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
462 33501562 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REQUEST");
463 33501562 2020-03-18 stsp ibuf_free(wbuf);
465 33501562 2020-03-18 stsp return err;
469 33501562 2020-03-18 stsp wbuf->fd = fd;
470 33501562 2020-03-18 stsp imsg_close(ibuf, wbuf);
471 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
474 93658fb9 2020-03-18 stsp const struct got_error *
475 abe0f35f 2020-03-18 stsp got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
476 abe0f35f 2020-03-18 stsp struct got_pathlist_head *symrefs)
478 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
479 abe0f35f 2020-03-18 stsp struct ibuf *wbuf;
480 abe0f35f 2020-03-18 stsp size_t len, nsymrefs = 0;
481 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
483 abe0f35f 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_symrefs);
484 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
485 abe0f35f 2020-03-18 stsp const char *target = pe->data;
486 abe0f35f 2020-03-18 stsp len += sizeof(struct got_imsg_fetch_symref) +
487 abe0f35f 2020-03-18 stsp pe->path_len + strlen(target);
488 abe0f35f 2020-03-18 stsp nsymrefs++;
491 abe0f35f 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
492 abe0f35f 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
494 abe0f35f 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
495 abe0f35f 2020-03-18 stsp if (wbuf == NULL)
496 abe0f35f 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
498 abe0f35f 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
499 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
500 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
501 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
502 abe0f35f 2020-03-18 stsp return err;
505 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
506 abe0f35f 2020-03-18 stsp const char *name = pe->path;
507 abe0f35f 2020-03-18 stsp size_t name_len = pe->path_len;
508 abe0f35f 2020-03-18 stsp const char *target = pe->data;
509 abe0f35f 2020-03-18 stsp size_t target_len = strlen(target);
511 abe0f35f 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
512 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
513 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
514 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
515 abe0f35f 2020-03-18 stsp return err;
517 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
518 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
519 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
520 abe0f35f 2020-03-18 stsp return err;
522 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, name, name_len) == -1) {
523 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
524 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
525 abe0f35f 2020-03-18 stsp return err;
527 abe0f35f 2020-03-18 stsp if (imsg_add(wbuf, target, target_len) == -1) {
528 abe0f35f 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
529 abe0f35f 2020-03-18 stsp ibuf_free(wbuf);
530 abe0f35f 2020-03-18 stsp return err;
534 abe0f35f 2020-03-18 stsp wbuf->fd = -1;
535 abe0f35f 2020-03-18 stsp imsg_close(ibuf, wbuf);
536 abe0f35f 2020-03-18 stsp return flush_imsg(ibuf);
539 abe0f35f 2020-03-18 stsp const struct got_error *
540 ea7396b9 2020-03-18 stsp got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
541 b9f99abf 2020-03-18 stsp struct got_object_id *refid, const char *refname)
543 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
544 b9f99abf 2020-03-18 stsp struct ibuf *wbuf;
545 b9f99abf 2020-03-18 stsp size_t len, reflen = strlen(refname);
547 ea7396b9 2020-03-18 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
548 b9f99abf 2020-03-18 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
549 b9f99abf 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
551 ea7396b9 2020-03-18 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
552 b9f99abf 2020-03-18 stsp if (wbuf == NULL)
553 ea7396b9 2020-03-18 stsp return got_error_from_errno("imsg_create FETCH_REF");
555 ea7396b9 2020-03-18 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
556 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
557 ea7396b9 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REF");
558 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
559 b9f99abf 2020-03-18 stsp return err;
561 b9f99abf 2020-03-18 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
562 ea7396b9 2020-03-18 stsp err = got_error_from_errno("imsg_add FETCH_REF");
563 b9f99abf 2020-03-18 stsp ibuf_free(wbuf);
564 b9f99abf 2020-03-18 stsp return err;
567 b9f99abf 2020-03-18 stsp wbuf->fd = -1;
568 b9f99abf 2020-03-18 stsp imsg_close(ibuf, wbuf);
569 531c3985 2020-03-18 stsp return flush_imsg(ibuf);
572 531c3985 2020-03-18 stsp const struct got_error *
573 531c3985 2020-03-18 stsp got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
574 531c3985 2020-03-18 stsp size_t msglen)
576 531c3985 2020-03-18 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
577 531c3985 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
579 531c3985 2020-03-18 stsp if (msglen == 0)
580 531c3985 2020-03-18 stsp return NULL;
582 531c3985 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
583 531c3985 2020-03-18 stsp msg, msglen) == -1)
584 531c3985 2020-03-18 stsp return got_error_from_errno(
585 531c3985 2020-03-18 stsp "imsg_compose FETCH_SERVER_PROGRESS");
587 b9f99abf 2020-03-18 stsp return flush_imsg(ibuf);
590 b9f99abf 2020-03-18 stsp const struct got_error *
591 93658fb9 2020-03-18 stsp got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
593 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
594 93658fb9 2020-03-18 stsp hash.sha1, SHA1_DIGEST_LENGTH) == -1)
595 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
596 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
600 93658fb9 2020-03-18 stsp const struct got_error *
601 8f2d01a6 2020-03-18 stsp got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
602 531c3985 2020-03-18 stsp char **refname, struct got_pathlist_head *symrefs, char **server_progress,
603 531c3985 2020-03-18 stsp struct imsgbuf *ibuf)
605 b9f99abf 2020-03-18 stsp const struct got_error *err = NULL;
606 b9f99abf 2020-03-18 stsp struct imsg imsg;
607 b9f99abf 2020-03-18 stsp size_t datalen;
608 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symrefs *isymrefs = NULL;
609 abe0f35f 2020-03-18 stsp size_t n, remain;
614 8f2d01a6 2020-03-18 stsp *id = NULL;
615 b9f99abf 2020-03-18 stsp *refname = NULL;
616 531c3985 2020-03-18 stsp *server_progress = NULL;
618 531c3985 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
620 b9f99abf 2020-03-18 stsp return err;
622 b9f99abf 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
623 b9f99abf 2020-03-18 stsp switch (imsg.hdr.type) {
624 b9f99abf 2020-03-18 stsp case GOT_IMSG_ERROR:
625 531c3985 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_error)) {
626 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
629 b9f99abf 2020-03-18 stsp err = recv_imsg_error(&imsg, datalen);
631 abe0f35f 2020-03-18 stsp case GOT_IMSG_FETCH_SYMREFS:
632 abe0f35f 2020-03-18 stsp if (datalen < sizeof(*isymrefs)) {
633 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
636 abe0f35f 2020-03-18 stsp if (isymrefs != NULL) {
637 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
640 abe0f35f 2020-03-18 stsp isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
641 abe0f35f 2020-03-18 stsp off = sizeof(*isymrefs);
642 abe0f35f 2020-03-18 stsp remain = datalen - off;
643 abe0f35f 2020-03-18 stsp for (n = 0; n < isymrefs->nsymrefs; n++) {
644 abe0f35f 2020-03-18 stsp struct got_imsg_fetch_symref *s;
645 abe0f35f 2020-03-18 stsp char *name, *target;
646 abe0f35f 2020-03-18 stsp if (remain < sizeof(struct got_imsg_fetch_symref)) {
647 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
650 abe0f35f 2020-03-18 stsp s = (struct got_imsg_fetch_symref *)(imsg.data + off);
651 abe0f35f 2020-03-18 stsp off += sizeof(*s);
652 abe0f35f 2020-03-18 stsp remain -= sizeof(*s);
653 abe0f35f 2020-03-18 stsp if (remain < s->name_len) {
654 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
657 abe0f35f 2020-03-18 stsp name = strndup(imsg.data + off, s->name_len);
658 abe0f35f 2020-03-18 stsp if (name == NULL) {
659 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
662 abe0f35f 2020-03-18 stsp off += s->name_len;
663 abe0f35f 2020-03-18 stsp remain -= s->name_len;
664 abe0f35f 2020-03-18 stsp if (remain < s->target_len) {
665 abe0f35f 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
666 abe0f35f 2020-03-18 stsp free(name);
669 abe0f35f 2020-03-18 stsp target = strndup(imsg.data + off, s->target_len);
670 abe0f35f 2020-03-18 stsp if (target == NULL) {
671 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strndup");
672 abe0f35f 2020-03-18 stsp free(name);
675 abe0f35f 2020-03-18 stsp off += s->target_len;
676 abe0f35f 2020-03-18 stsp remain -= s->target_len;
677 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
679 abe0f35f 2020-03-18 stsp free(name);
680 abe0f35f 2020-03-18 stsp free(target);
685 ea7396b9 2020-03-18 stsp case GOT_IMSG_FETCH_REF:
686 531c3985 2020-03-18 stsp if (datalen <= SHA1_DIGEST_LENGTH) {
687 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
690 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
691 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
692 b9f99abf 2020-03-18 stsp err = got_error_from_errno("malloc");
695 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
696 b9f99abf 2020-03-18 stsp *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
697 b9f99abf 2020-03-18 stsp datalen - SHA1_DIGEST_LENGTH);
698 b9f99abf 2020-03-18 stsp if (*refname == NULL) {
699 b9f99abf 2020-03-18 stsp err = got_error_from_errno("strndup");
703 531c3985 2020-03-18 stsp case GOT_IMSG_FETCH_SERVER_PROGRESS:
704 531c3985 2020-03-18 stsp if (datalen == 0) {
705 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
708 531c3985 2020-03-18 stsp *server_progress = strndup(imsg.data, datalen);
709 531c3985 2020-03-18 stsp if (*server_progress == NULL) {
710 531c3985 2020-03-18 stsp err = got_error_from_errno("strndup");
713 531c3985 2020-03-18 stsp for (i = 0; i < datalen; i++) {
714 531c3985 2020-03-18 stsp if (!isprint((unsigned char)(*server_progress)[i]) &&
715 531c3985 2020-03-18 stsp !isspace((unsigned char)(*server_progress)[i])) {
716 531c3985 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
717 531c3985 2020-03-18 stsp free(*server_progress);
718 531c3985 2020-03-18 stsp *server_progress = NULL;
723 8f2d01a6 2020-03-18 stsp case GOT_IMSG_FETCH_DONE:
724 8f2d01a6 2020-03-18 stsp *id = malloc(sizeof(**id));
725 8f2d01a6 2020-03-18 stsp if (*id == NULL) {
726 8f2d01a6 2020-03-18 stsp err = got_error_from_errno("malloc");
729 8f2d01a6 2020-03-18 stsp if (datalen != SHA1_DIGEST_LENGTH) {
730 8f2d01a6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
733 8f2d01a6 2020-03-18 stsp memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
737 b887aab6 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
743 8f2d01a6 2020-03-18 stsp *id = NULL;
744 b887aab6 2020-03-18 stsp free(*refname);
745 b887aab6 2020-03-18 stsp *refname = NULL;
747 b9f99abf 2020-03-18 stsp imsg_free(&imsg);
748 b9f99abf 2020-03-18 stsp return err;
751 93658fb9 2020-03-18 stsp const struct got_error *
752 279090e1 2020-03-18 stsp got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
754 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
756 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
757 279090e1 2020-03-18 stsp hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
758 93658fb9 2020-03-18 stsp err = got_error_from_errno("imsg_compose INDEX_REQUEST");
760 93658fb9 2020-03-18 stsp return err;
762 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
765 93658fb9 2020-03-18 stsp const struct got_error *
766 93658fb9 2020-03-18 stsp got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
768 93658fb9 2020-03-18 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
769 93658fb9 2020-03-18 stsp return got_error_from_errno("imsg_compose FETCH");
770 93658fb9 2020-03-18 stsp return flush_imsg(ibuf);
773 93658fb9 2020-03-18 stsp const struct got_error *
774 93658fb9 2020-03-18 stsp got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
776 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
777 93658fb9 2020-03-18 stsp struct imsg imsg;
779 93658fb9 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
781 93658fb9 2020-03-18 stsp return err;
782 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
783 93658fb9 2020-03-18 stsp return NULL;
785 93658fb9 2020-03-18 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
786 93658fb9 2020-03-18 stsp imsg_free(&imsg);
789 93658fb9 2020-03-18 stsp const struct got_error *
790 cfd633c2 2018-09-10 stsp got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
791 cfd633c2 2018-09-10 stsp struct imsgbuf *ibuf)
793 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
794 291624d8 2018-11-07 stsp struct got_imsg_object *iobj;
795 cfd633c2 2018-09-10 stsp size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
797 291624d8 2018-11-07 stsp if (datalen != sizeof(*iobj))
798 cfd633c2 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
799 291624d8 2018-11-07 stsp iobj = imsg->data;
801 cfd633c2 2018-09-10 stsp *obj = calloc(1, sizeof(**obj));
802 cfd633c2 2018-09-10 stsp if (*obj == NULL)
803 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
805 291624d8 2018-11-07 stsp memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
806 291624d8 2018-11-07 stsp (*obj)->type = iobj->type;
807 291624d8 2018-11-07 stsp (*obj)->flags = iobj->flags;
808 291624d8 2018-11-07 stsp (*obj)->hdrlen = iobj->hdrlen;
809 291624d8 2018-11-07 stsp (*obj)->size = iobj->size;
810 c59b3346 2018-09-11 stsp /* path_packfile is handled by caller */
811 291624d8 2018-11-07 stsp if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
812 291624d8 2018-11-07 stsp (*obj)->pack_offset = iobj->pack_offset;
813 291624d8 2018-11-07 stsp (*obj)->pack_idx = iobj->pack_idx;
816 876c234b 2018-09-10 stsp return err;
819 2178c42e 2018-04-22 stsp const struct got_error *
820 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
822 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
823 2178c42e 2018-04-22 stsp struct imsg imsg;
824 c4eae628 2018-04-23 stsp const size_t min_datalen =
825 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
827 2178c42e 2018-04-22 stsp *obj = NULL;
829 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
831 2178c42e 2018-04-22 stsp return err;
833 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
834 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
835 cfd633c2 2018-09-10 stsp err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
838 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
842 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
844 bff6ca00 2018-04-23 stsp return err;
847 c75f7264 2018-09-11 stsp static const struct got_error *
848 c75f7264 2018-09-11 stsp send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
849 c75f7264 2018-09-11 stsp size_t logmsg_len)
851 fa4ffeb3 2018-11-04 stsp const struct got_error *err = NULL;
852 c75f7264 2018-09-11 stsp size_t offset, remain;
854 c75f7264 2018-09-11 stsp offset = 0;
855 c75f7264 2018-09-11 stsp remain = logmsg_len;
856 c75f7264 2018-09-11 stsp while (remain > 0) {
857 c75f7264 2018-09-11 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
859 c75f7264 2018-09-11 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
860 fa4ffeb3 2018-11-04 stsp commit->logmsg + offset, n) == -1) {
861 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose "
862 230a42bd 2019-05-11 jcs "COMMIT_LOGMSG");
866 fa4ffeb3 2018-11-04 stsp err = flush_imsg(ibuf);
870 c75f7264 2018-09-11 stsp offset += n;
871 c75f7264 2018-09-11 stsp remain -= n;
874 fa4ffeb3 2018-11-04 stsp return err;
877 bff6ca00 2018-04-23 stsp const struct got_error *
878 068fd2bf 2018-04-24 stsp got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
880 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
881 b9c33926 2018-11-07 stsp struct got_imsg_commit_object *icommit;
882 bff6ca00 2018-04-23 stsp uint8_t *buf;
883 bff6ca00 2018-04-23 stsp size_t len, total;
884 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
885 b9c33926 2018-11-07 stsp size_t author_len = strlen(commit->author);
886 b9c33926 2018-11-07 stsp size_t committer_len = strlen(commit->committer);
887 c75f7264 2018-09-11 stsp size_t logmsg_len = strlen(commit->logmsg);
889 b9c33926 2018-11-07 stsp total = sizeof(*icommit) + author_len + committer_len +
890 b9c33926 2018-11-07 stsp commit->nparents * SHA1_DIGEST_LENGTH;
892 bff6ca00 2018-04-23 stsp buf = malloc(total);
893 bff6ca00 2018-04-23 stsp if (buf == NULL)
894 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
896 b9c33926 2018-11-07 stsp icommit = (struct got_imsg_commit_object *)buf;
897 a7403916 2018-12-24 stsp memcpy(icommit->tree_id, commit->tree_id->sha1,
898 a7403916 2018-12-24 stsp sizeof(icommit->tree_id));
899 b9c33926 2018-11-07 stsp icommit->author_len = author_len;
900 b9c33926 2018-11-07 stsp icommit->author_time = commit->author_time;
901 b9c33926 2018-11-07 stsp icommit->author_gmtoff = commit->author_gmtoff;
902 b9c33926 2018-11-07 stsp icommit->committer_len = committer_len;
903 b9c33926 2018-11-07 stsp icommit->committer_time = commit->committer_time;
904 b9c33926 2018-11-07 stsp icommit->committer_gmtoff = commit->committer_gmtoff;
905 b9c33926 2018-11-07 stsp icommit->logmsg_len = logmsg_len;
906 b9c33926 2018-11-07 stsp icommit->nparents = commit->nparents;
908 b9c33926 2018-11-07 stsp len = sizeof(*icommit);
909 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->author, author_len);
910 b9c33926 2018-11-07 stsp len += author_len;
911 b9c33926 2018-11-07 stsp memcpy(buf + len, commit->committer, committer_len);
912 b9c33926 2018-11-07 stsp len += committer_len;
913 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
914 79f35eb3 2018-06-11 stsp memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
915 86acc566 2018-04-23 stsp len += SHA1_DIGEST_LENGTH;
918 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
919 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose COMMIT");
923 904df868 2018-11-04 stsp if (logmsg_len == 0 ||
924 904df868 2018-11-04 stsp logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
925 904df868 2018-11-04 stsp err = flush_imsg(ibuf);
929 c75f7264 2018-09-11 stsp err = send_commit_logmsg(ibuf, commit, logmsg_len);
932 bff6ca00 2018-04-23 stsp return err;
935 ca6e02ac 2020-01-07 stsp static const struct got_error *
936 ca6e02ac 2020-01-07 stsp get_commit_from_imsg(struct got_commit_object **commit,
937 ca6e02ac 2020-01-07 stsp struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
939 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
940 291624d8 2018-11-07 stsp struct got_imsg_commit_object *icommit;
941 ca6e02ac 2020-01-07 stsp size_t len = 0;
944 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(*icommit))
945 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
947 ca6e02ac 2020-01-07 stsp icommit = imsg->data;
948 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommit) + icommit->author_len +
949 ca6e02ac 2020-01-07 stsp icommit->committer_len +
950 ca6e02ac 2020-01-07 stsp icommit->nparents * SHA1_DIGEST_LENGTH)
951 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
953 ca6e02ac 2020-01-07 stsp if (icommit->nparents < 0)
954 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
956 ca6e02ac 2020-01-07 stsp len += sizeof(*icommit);
958 ca6e02ac 2020-01-07 stsp *commit = got_object_commit_alloc_partial();
959 ca6e02ac 2020-01-07 stsp if (*commit == NULL)
960 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
961 ca6e02ac 2020-01-07 stsp "got_object_commit_alloc_partial");
963 ca6e02ac 2020-01-07 stsp memcpy((*commit)->tree_id->sha1, icommit->tree_id,
964 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
965 ca6e02ac 2020-01-07 stsp (*commit)->author_time = icommit->author_time;
966 ca6e02ac 2020-01-07 stsp (*commit)->author_gmtoff = icommit->author_gmtoff;
967 ca6e02ac 2020-01-07 stsp (*commit)->committer_time = icommit->committer_time;
968 ca6e02ac 2020-01-07 stsp (*commit)->committer_gmtoff = icommit->committer_gmtoff;
970 ca6e02ac 2020-01-07 stsp if (icommit->author_len == 0) {
971 ca6e02ac 2020-01-07 stsp (*commit)->author = strdup("");
972 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
973 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
977 ca6e02ac 2020-01-07 stsp (*commit)->author = malloc(icommit->author_len + 1);
978 ca6e02ac 2020-01-07 stsp if ((*commit)->author == NULL) {
979 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
982 ca6e02ac 2020-01-07 stsp memcpy((*commit)->author, imsg->data + len,
983 ca6e02ac 2020-01-07 stsp icommit->author_len);
984 ca6e02ac 2020-01-07 stsp (*commit)->author[icommit->author_len] = '\0';
986 ca6e02ac 2020-01-07 stsp len += icommit->author_len;
988 ca6e02ac 2020-01-07 stsp if (icommit->committer_len == 0) {
989 ca6e02ac 2020-01-07 stsp (*commit)->committer = strdup("");
990 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
991 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
995 ca6e02ac 2020-01-07 stsp (*commit)->committer =
996 ca6e02ac 2020-01-07 stsp malloc(icommit->committer_len + 1);
997 ca6e02ac 2020-01-07 stsp if ((*commit)->committer == NULL) {
998 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1001 ca6e02ac 2020-01-07 stsp memcpy((*commit)->committer, imsg->data + len,
1002 ca6e02ac 2020-01-07 stsp icommit->committer_len);
1003 ca6e02ac 2020-01-07 stsp (*commit)->committer[icommit->committer_len] = '\0';
1005 ca6e02ac 2020-01-07 stsp len += icommit->committer_len;
1007 ca6e02ac 2020-01-07 stsp if (icommit->logmsg_len == 0) {
1008 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = strdup("");
1009 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1010 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("strdup");
1011 ca6e02ac 2020-01-07 stsp goto done;
1014 ca6e02ac 2020-01-07 stsp size_t offset = 0, remain = icommit->logmsg_len;
1016 ca6e02ac 2020-01-07 stsp (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1017 ca6e02ac 2020-01-07 stsp if ((*commit)->logmsg == NULL) {
1018 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("malloc");
1019 ca6e02ac 2020-01-07 stsp goto done;
1021 ca6e02ac 2020-01-07 stsp while (remain > 0) {
1022 ca6e02ac 2020-01-07 stsp struct imsg imsg_log;
1023 ca6e02ac 2020-01-07 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1026 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1028 ca6e02ac 2020-01-07 stsp goto done;
1030 ca6e02ac 2020-01-07 stsp if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1031 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1032 ca6e02ac 2020-01-07 stsp goto done;
1035 ca6e02ac 2020-01-07 stsp memcpy((*commit)->logmsg + offset,
1036 ca6e02ac 2020-01-07 stsp imsg_log.data, n);
1037 ca6e02ac 2020-01-07 stsp imsg_free(&imsg_log);
1038 ca6e02ac 2020-01-07 stsp offset += n;
1039 ca6e02ac 2020-01-07 stsp remain -= n;
1041 ca6e02ac 2020-01-07 stsp (*commit)->logmsg[icommit->logmsg_len] = '\0';
1044 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommit->nparents; i++) {
1045 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
1047 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
1050 ca6e02ac 2020-01-07 stsp memcpy(qid->id, imsg->data + len +
1051 ca6e02ac 2020-01-07 stsp i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1052 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1053 ca6e02ac 2020-01-07 stsp (*commit)->nparents++;
1056 ca6e02ac 2020-01-07 stsp if (err) {
1057 ca6e02ac 2020-01-07 stsp got_object_commit_close(*commit);
1058 ca6e02ac 2020-01-07 stsp *commit = NULL;
1060 ca6e02ac 2020-01-07 stsp return err;
1063 ca6e02ac 2020-01-07 stsp const struct got_error *
1064 ca6e02ac 2020-01-07 stsp got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1066 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1067 ca6e02ac 2020-01-07 stsp struct imsg imsg;
1068 ca6e02ac 2020-01-07 stsp size_t datalen;
1069 ca6e02ac 2020-01-07 stsp const size_t min_datalen =
1070 ca6e02ac 2020-01-07 stsp MIN(sizeof(struct got_imsg_error),
1071 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_object));
1073 ca6e02ac 2020-01-07 stsp *commit = NULL;
1075 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1077 ca6e02ac 2020-01-07 stsp return err;
1079 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1081 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
1082 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
1083 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1086 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1090 2178c42e 2018-04-22 stsp imsg_free(&imsg);
1092 e033d803 2018-04-23 stsp return err;
1095 e033d803 2018-04-23 stsp const struct got_error *
1096 3022d272 2019-11-14 stsp got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1097 3022d272 2019-11-14 stsp int nentries)
1099 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1100 e033d803 2018-04-23 stsp struct got_imsg_tree_object itree;
1101 3022d272 2019-11-14 stsp struct got_pathlist_entry *pe;
1102 b00c9821 2018-11-04 stsp size_t totlen;
1103 6eb07a17 2018-11-04 stsp int nimsg; /* number of imsg queued in ibuf */
1105 3022d272 2019-11-14 stsp itree.nentries = nentries;
1106 e033d803 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1108 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose TREE");
1110 b00c9821 2018-11-04 stsp totlen = sizeof(itree);
1111 6eb07a17 2018-11-04 stsp nimsg = 1;
1112 3022d272 2019-11-14 stsp TAILQ_FOREACH(pe, entries, entry) {
1113 3022d272 2019-11-14 stsp const char *name = pe->path;
1114 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte = pe->data;
1115 3022d272 2019-11-14 stsp struct ibuf *wbuf;
1116 3022d272 2019-11-14 stsp size_t namelen = strlen(name);
1117 cd9e913a 2019-11-27 stsp size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1119 e033d803 2018-04-23 stsp if (len > MAX_IMSGSIZE)
1120 e033d803 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
1123 6eb07a17 2018-11-04 stsp if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1124 b00c9821 2018-11-04 stsp err = flush_imsg(ibuf);
1126 b00c9821 2018-11-04 stsp return err;
1127 6eb07a17 2018-11-04 stsp nimsg = 0;
1130 3022d272 2019-11-14 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1131 3022d272 2019-11-14 stsp if (wbuf == NULL)
1132 3022d272 2019-11-14 stsp return got_error_from_errno("imsg_create TREE_ENTRY");
1134 3022d272 2019-11-14 stsp /* Keep in sync with struct got_imsg_tree_object definition! */
1135 3b647085 2019-11-23 stsp if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1136 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1137 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1138 e033d803 2018-04-23 stsp return err;
1140 3b647085 2019-11-23 stsp if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1141 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1142 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1143 3022d272 2019-11-14 stsp return err;
1146 3b647085 2019-11-23 stsp if (imsg_add(wbuf, name, namelen) == -1) {
1147 3022d272 2019-11-14 stsp err = got_error_from_errno("imsg_add TREE_ENTRY");
1148 3b647085 2019-11-23 stsp ibuf_free(wbuf);
1149 3022d272 2019-11-14 stsp return err;
1152 3022d272 2019-11-14 stsp wbuf->fd = -1;
1153 3022d272 2019-11-14 stsp imsg_close(ibuf, wbuf);
1155 b00c9821 2018-11-04 stsp totlen += len;
1158 b00c9821 2018-11-04 stsp return flush_imsg(ibuf);
1161 e033d803 2018-04-23 stsp const struct got_error *
1162 068fd2bf 2018-04-24 stsp got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1164 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1165 e033d803 2018-04-23 stsp const size_t min_datalen =
1166 e033d803 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
1167 e033d803 2018-04-23 stsp sizeof(struct got_imsg_tree_object));
1168 291624d8 2018-11-07 stsp struct got_imsg_tree_object *itree;
1169 e033d803 2018-04-23 stsp int nentries = 0;
1171 e033d803 2018-04-23 stsp *tree = NULL;
1173 e033d803 2018-04-23 stsp err = read_imsg(ibuf);
1175 1e51f5b9 2018-04-23 stsp goto done;
1178 e033d803 2018-04-23 stsp struct imsg imsg;
1180 e033d803 2018-04-23 stsp size_t datalen;
1181 c0588d8d 2018-11-07 stsp struct got_imsg_tree_entry *ite;
1182 e033d803 2018-04-23 stsp struct got_tree_entry *te = NULL;
1184 e033d803 2018-04-23 stsp n = imsg_get(ibuf, &imsg);
1185 e033d803 2018-04-23 stsp if (n == 0) {
1186 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries)
1187 e033d803 2018-04-23 stsp goto get_more;
1191 e033d803 2018-04-23 stsp if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1192 e033d803 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1194 e033d803 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1196 e033d803 2018-04-23 stsp switch (imsg.hdr.type) {
1197 e033d803 2018-04-23 stsp case GOT_IMSG_ERROR:
1198 e033d803 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
1200 e033d803 2018-04-23 stsp case GOT_IMSG_TREE:
1201 e033d803 2018-04-23 stsp /* This message should only appear once. */
1202 e033d803 2018-04-23 stsp if (*tree != NULL) {
1203 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1206 291624d8 2018-11-07 stsp if (datalen != sizeof(*itree)) {
1207 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1210 291624d8 2018-11-07 stsp itree = imsg.data;
1211 c3b78ecc 2018-11-07 stsp *tree = malloc(sizeof(**tree));
1212 e033d803 2018-04-23 stsp if (*tree == NULL) {
1213 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1216 56e0773d 2019-11-28 stsp (*tree)->entries = calloc(itree->nentries,
1217 56e0773d 2019-11-28 stsp sizeof(struct got_tree_entry));
1218 56e0773d 2019-11-28 stsp if ((*tree)->entries == NULL) {
1219 56e0773d 2019-11-28 stsp err = got_error_from_errno("malloc");
1222 56e0773d 2019-11-28 stsp (*tree)->nentries = itree->nentries;
1223 c3b78ecc 2018-11-07 stsp (*tree)->refcnt = 0;
1225 e033d803 2018-04-23 stsp case GOT_IMSG_TREE_ENTRY:
1226 e033d803 2018-04-23 stsp /* This message should be preceeded by GOT_IMSG_TREE. */
1227 e033d803 2018-04-23 stsp if (*tree == NULL) {
1228 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1231 c0588d8d 2018-11-07 stsp if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1232 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1236 e033d803 2018-04-23 stsp /* Remaining data contains the entry's name. */
1237 c0588d8d 2018-11-07 stsp datalen -= sizeof(*ite);
1238 e033d803 2018-04-23 stsp if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1239 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1242 c0588d8d 2018-11-07 stsp ite = imsg.data;
1244 56e0773d 2019-11-28 stsp if (datalen + 1 > sizeof(te->name)) {
1245 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1248 56e0773d 2019-11-28 stsp te = &(*tree)->entries[nentries];
1249 c0588d8d 2018-11-07 stsp memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1250 e033d803 2018-04-23 stsp te->name[datalen] = '\0';
1252 56e0773d 2019-11-28 stsp memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1253 c0588d8d 2018-11-07 stsp te->mode = ite->mode;
1254 56e0773d 2019-11-28 stsp te->idx = nentries;
1255 e033d803 2018-04-23 stsp nentries++;
1258 e033d803 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1262 e033d803 2018-04-23 stsp imsg_free(&imsg);
1265 56e0773d 2019-11-28 stsp if (*tree && (*tree)->nentries != nentries) {
1266 1e51f5b9 2018-04-23 stsp if (err == NULL)
1267 1e51f5b9 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1268 e033d803 2018-04-23 stsp got_object_tree_close(*tree);
1269 e033d803 2018-04-23 stsp *tree = NULL;
1272 ff6b18f8 2018-04-24 stsp return err;
1275 ff6b18f8 2018-04-24 stsp const struct got_error *
1276 ac544f8c 2019-01-13 stsp got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1277 ac544f8c 2019-01-13 stsp const uint8_t *data)
1279 2967a784 2018-04-24 stsp struct got_imsg_blob iblob;
1281 2967a784 2018-04-24 stsp iblob.size = size;
1282 ebc55e2d 2018-12-24 stsp iblob.hdrlen = hdrlen;
1284 ac544f8c 2019-01-13 stsp if (data) {
1285 ac544f8c 2019-01-13 stsp uint8_t *buf;
1287 ac544f8c 2019-01-13 stsp if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1288 ac544f8c 2019-01-13 stsp return got_error(GOT_ERR_NO_SPACE);
1290 ac544f8c 2019-01-13 stsp buf = malloc(sizeof(iblob) + size);
1291 ac544f8c 2019-01-13 stsp if (buf == NULL)
1292 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1294 ac544f8c 2019-01-13 stsp memcpy(buf, &iblob, sizeof(iblob));
1295 ac544f8c 2019-01-13 stsp memcpy(buf + sizeof(iblob), data, size);
1296 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1297 ac544f8c 2019-01-13 stsp sizeof(iblob) + size) == -1) {
1298 ac544f8c 2019-01-13 stsp free(buf);
1299 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1301 ac544f8c 2019-01-13 stsp free(buf);
1303 ac544f8c 2019-01-13 stsp /* Data has already been written to file descriptor. */
1304 ac544f8c 2019-01-13 stsp if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1305 ac544f8c 2019-01-13 stsp sizeof(iblob)) == -1)
1306 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose BLOB");
1310 ff6b18f8 2018-04-24 stsp return flush_imsg(ibuf);
1313 ff6b18f8 2018-04-24 stsp const struct got_error *
1314 ac544f8c 2019-01-13 stsp got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1315 ac544f8c 2019-01-13 stsp struct imsgbuf *ibuf)
1317 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1318 ff6b18f8 2018-04-24 stsp struct imsg imsg;
1319 291624d8 2018-11-07 stsp struct got_imsg_blob *iblob;
1320 ff6b18f8 2018-04-24 stsp size_t datalen;
1322 ac544f8c 2019-01-13 stsp *outbuf = NULL;
1324 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1326 ff6b18f8 2018-04-24 stsp return err;
1328 ff6b18f8 2018-04-24 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1330 ff6b18f8 2018-04-24 stsp switch (imsg.hdr.type) {
1331 ff6b18f8 2018-04-24 stsp case GOT_IMSG_BLOB:
1332 ac544f8c 2019-01-13 stsp if (datalen < sizeof(*iblob)) {
1333 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1336 291624d8 2018-11-07 stsp iblob = imsg.data;
1337 291624d8 2018-11-07 stsp *size = iblob->size;
1338 ebc55e2d 2018-12-24 stsp *hdrlen = iblob->hdrlen;
1340 ac544f8c 2019-01-13 stsp if (datalen == sizeof(*iblob)) {
1341 ac544f8c 2019-01-13 stsp /* Data has been written to file descriptor. */
1345 ac544f8c 2019-01-13 stsp if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1346 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1350 ac544f8c 2019-01-13 stsp *outbuf = malloc(*size);
1351 ac544f8c 2019-01-13 stsp if (*outbuf == NULL) {
1352 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1355 ac544f8c 2019-01-13 stsp memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1358 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1362 f4a881ce 2018-11-17 stsp imsg_free(&imsg);
1364 f4a881ce 2018-11-17 stsp return err;
1367 f4a881ce 2018-11-17 stsp static const struct got_error *
1368 f4a881ce 2018-11-17 stsp send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1370 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1371 f4a881ce 2018-11-17 stsp size_t offset, remain;
1373 f4a881ce 2018-11-17 stsp offset = 0;
1374 f4a881ce 2018-11-17 stsp remain = tagmsg_len;
1375 f4a881ce 2018-11-17 stsp while (remain > 0) {
1376 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1378 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1379 f4a881ce 2018-11-17 stsp tag->tagmsg + offset, n) == -1) {
1380 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1384 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1388 f4a881ce 2018-11-17 stsp offset += n;
1389 f4a881ce 2018-11-17 stsp remain -= n;
1392 f4a881ce 2018-11-17 stsp return err;
1395 f4a881ce 2018-11-17 stsp const struct got_error *
1396 f4a881ce 2018-11-17 stsp got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1398 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1399 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1400 f4a881ce 2018-11-17 stsp uint8_t *buf;
1401 f4a881ce 2018-11-17 stsp size_t len, total;
1402 f4a881ce 2018-11-17 stsp size_t tag_len = strlen(tag->tag);
1403 f4a881ce 2018-11-17 stsp size_t tagger_len = strlen(tag->tagger);
1404 f4a881ce 2018-11-17 stsp size_t tagmsg_len = strlen(tag->tagmsg);
1406 f4a881ce 2018-11-17 stsp total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1408 f4a881ce 2018-11-17 stsp buf = malloc(total);
1409 f4a881ce 2018-11-17 stsp if (buf == NULL)
1410 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1412 f4a881ce 2018-11-17 stsp itag = (struct got_imsg_tag_object *)buf;
1413 f4a881ce 2018-11-17 stsp memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1414 f4a881ce 2018-11-17 stsp itag->obj_type = tag->obj_type;
1415 f4a881ce 2018-11-17 stsp itag->tag_len = tag_len;
1416 f4a881ce 2018-11-17 stsp itag->tagger_len = tagger_len;
1417 f4a881ce 2018-11-17 stsp itag->tagger_time = tag->tagger_time;
1418 f4a881ce 2018-11-17 stsp itag->tagger_gmtoff = tag->tagger_gmtoff;
1419 f4a881ce 2018-11-17 stsp itag->tagmsg_len = tagmsg_len;
1421 f4a881ce 2018-11-17 stsp len = sizeof(*itag);
1422 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tag, tag_len);
1423 f4a881ce 2018-11-17 stsp len += tag_len;
1424 f4a881ce 2018-11-17 stsp memcpy(buf + len, tag->tagger, tagger_len);
1425 f4a881ce 2018-11-17 stsp len += tagger_len;
1427 f4a881ce 2018-11-17 stsp if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1428 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose TAG");
1429 f4a881ce 2018-11-17 stsp goto done;
1432 f4a881ce 2018-11-17 stsp if (tagmsg_len == 0 ||
1433 f4a881ce 2018-11-17 stsp tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1434 f4a881ce 2018-11-17 stsp err = flush_imsg(ibuf);
1436 f4a881ce 2018-11-17 stsp goto done;
1438 f4a881ce 2018-11-17 stsp err = send_tagmsg(ibuf, tag, tagmsg_len);
1440 f4a881ce 2018-11-17 stsp free(buf);
1441 f4a881ce 2018-11-17 stsp return err;
1444 f4a881ce 2018-11-17 stsp const struct got_error *
1445 f4a881ce 2018-11-17 stsp got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1447 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1448 f4a881ce 2018-11-17 stsp struct imsg imsg;
1449 f4a881ce 2018-11-17 stsp struct got_imsg_tag_object *itag;
1450 f4a881ce 2018-11-17 stsp size_t len, datalen;
1451 f4a881ce 2018-11-17 stsp const size_t min_datalen =
1452 f4a881ce 2018-11-17 stsp MIN(sizeof(struct got_imsg_error),
1453 f4a881ce 2018-11-17 stsp sizeof(struct got_imsg_tag_object));
1455 f4a881ce 2018-11-17 stsp *tag = NULL;
1457 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1459 f4a881ce 2018-11-17 stsp return err;
1461 f4a881ce 2018-11-17 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1464 f4a881ce 2018-11-17 stsp switch (imsg.hdr.type) {
1465 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG:
1466 f4a881ce 2018-11-17 stsp if (datalen < sizeof(*itag)) {
1467 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1470 f4a881ce 2018-11-17 stsp itag = imsg.data;
1471 f4a881ce 2018-11-17 stsp if (datalen != sizeof(*itag) + itag->tag_len +
1472 f4a881ce 2018-11-17 stsp itag->tagger_len) {
1473 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1476 f4a881ce 2018-11-17 stsp len += sizeof(*itag);
1478 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
1479 f4a881ce 2018-11-17 stsp if (*tag == NULL) {
1480 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1484 f4a881ce 2018-11-17 stsp memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1486 f4a881ce 2018-11-17 stsp if (itag->tag_len == 0) {
1487 f4a881ce 2018-11-17 stsp (*tag)->tag = strdup("");
1488 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1493 f4a881ce 2018-11-17 stsp (*tag)->tag = malloc(itag->tag_len + 1);
1494 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1495 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1498 f4a881ce 2018-11-17 stsp memcpy((*tag)->tag, imsg.data + len,
1499 f4a881ce 2018-11-17 stsp itag->tag_len);
1500 f4a881ce 2018-11-17 stsp (*tag)->tag[itag->tag_len] = '\0';
1502 f4a881ce 2018-11-17 stsp len += itag->tag_len;
1504 f4a881ce 2018-11-17 stsp (*tag)->obj_type = itag->obj_type;
1505 f4a881ce 2018-11-17 stsp (*tag)->tagger_time = itag->tagger_time;
1506 f4a881ce 2018-11-17 stsp (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1508 f4a881ce 2018-11-17 stsp if (itag->tagger_len == 0) {
1509 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup("");
1510 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1511 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1515 f4a881ce 2018-11-17 stsp (*tag)->tagger = malloc(itag->tagger_len + 1);
1516 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1517 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1520 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagger, imsg.data + len,
1521 f4a881ce 2018-11-17 stsp itag->tagger_len);
1522 f4a881ce 2018-11-17 stsp (*tag)->tagger[itag->tagger_len] = '\0';
1524 f4a881ce 2018-11-17 stsp len += itag->tagger_len;
1526 f4a881ce 2018-11-17 stsp if (itag->tagmsg_len == 0) {
1527 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strdup("");
1528 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1529 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1533 f4a881ce 2018-11-17 stsp size_t offset = 0, remain = itag->tagmsg_len;
1535 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1536 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1537 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1540 f4a881ce 2018-11-17 stsp while (remain > 0) {
1541 f4a881ce 2018-11-17 stsp struct imsg imsg_log;
1542 f4a881ce 2018-11-17 stsp size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1545 f4a881ce 2018-11-17 stsp err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1547 f4a881ce 2018-11-17 stsp return err;
1549 f4a881ce 2018-11-17 stsp if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1550 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1552 f4a881ce 2018-11-17 stsp memcpy((*tag)->tagmsg + offset, imsg_log.data,
1554 f4a881ce 2018-11-17 stsp imsg_free(&imsg_log);
1555 f4a881ce 2018-11-17 stsp offset += n;
1556 f4a881ce 2018-11-17 stsp remain -= n;
1558 f4a881ce 2018-11-17 stsp (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1563 ff6b18f8 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1567 ff6b18f8 2018-04-24 stsp imsg_free(&imsg);
1569 2178c42e 2018-04-22 stsp return err;
1572 876c234b 2018-09-10 stsp const struct got_error *
1573 876c234b 2018-09-10 stsp got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1574 876c234b 2018-09-10 stsp struct got_packidx *packidx)
1576 41496140 2019-02-21 stsp const struct got_error *err = NULL;
1577 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1578 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1581 876c234b 2018-09-10 stsp ipackidx.len = packidx->len;
1582 876c234b 2018-09-10 stsp fd = dup(packidx->fd);
1583 876c234b 2018-09-10 stsp if (fd == -1)
1584 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1586 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1587 41496140 2019-02-21 stsp sizeof(ipackidx)) == -1) {
1588 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACKIDX");
1589 41496140 2019-02-21 stsp close(fd);
1590 41496140 2019-02-21 stsp return err;
1593 876c234b 2018-09-10 stsp if (strlcpy(ipack.path_packfile, pack->path_packfile,
1594 876c234b 2018-09-10 stsp sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1595 876c234b 2018-09-10 stsp return got_error(GOT_ERR_NO_SPACE);
1596 876c234b 2018-09-10 stsp ipack.filesize = pack->filesize;
1598 876c234b 2018-09-10 stsp fd = dup(pack->fd);
1599 876c234b 2018-09-10 stsp if (fd == -1)
1600 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1602 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1604 638f9024 2019-05-13 stsp err = got_error_from_errno("imsg_compose PACK");
1605 41496140 2019-02-21 stsp close(fd);
1606 41496140 2019-02-21 stsp return err;
1609 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1612 876c234b 2018-09-10 stsp const struct got_error *
1613 106807b4 2018-09-15 stsp got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1614 106807b4 2018-09-15 stsp struct got_object_id *id)
1616 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
1618 876c234b 2018-09-10 stsp iobj.idx = idx;
1619 106807b4 2018-09-15 stsp memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1621 876c234b 2018-09-10 stsp if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1622 876c234b 2018-09-10 stsp &iobj, sizeof(iobj)) == -1)
1623 638f9024 2019-05-13 stsp return got_error_from_errno("imsg_compose "
1624 230a42bd 2019-05-11 jcs "PACKED_OBJECT_REQUEST");
1626 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1629 aba9c984 2019-09-08 stsp const struct got_error *
1630 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1632 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1634 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1635 aba9c984 2019-09-08 stsp NULL, 0) == -1) {
1636 aba9c984 2019-09-08 stsp err = got_error_from_errno("imsg_compose "
1637 aba9c984 2019-09-08 stsp "GITCONFIG_PARSE_REQUEST");
1638 aba9c984 2019-09-08 stsp close(fd);
1639 aba9c984 2019-09-08 stsp return err;
1642 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1645 aba9c984 2019-09-08 stsp const struct got_error *
1646 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1648 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1649 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1650 aba9c984 2019-09-08 stsp NULL, 0) == -1)
1651 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1652 aba9c984 2019-09-08 stsp "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1654 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1657 aba9c984 2019-09-08 stsp const struct got_error *
1658 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1660 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1661 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1662 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1663 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_NAME_REQUEST");
1665 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1668 aba9c984 2019-09-08 stsp const struct got_error *
1669 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1671 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf,
1672 aba9c984 2019-09-08 stsp GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1673 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose "
1674 aba9c984 2019-09-08 stsp "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1676 cd95becd 2019-11-29 stsp return flush_imsg(ibuf);
1679 cd95becd 2019-11-29 stsp const struct got_error *
1680 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1682 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf,
1683 cd95becd 2019-11-29 stsp GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1684 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose "
1685 cd95becd 2019-11-29 stsp "GITCONFIG_REMOTE_REQUEST");
1687 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1690 aba9c984 2019-09-08 stsp const struct got_error *
1691 9a1cc63f 2020-02-03 stsp got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1693 9a1cc63f 2020-02-03 stsp if (imsg_compose(ibuf,
1694 9a1cc63f 2020-02-03 stsp GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1695 9a1cc63f 2020-02-03 stsp return got_error_from_errno("imsg_compose "
1696 9a1cc63f 2020-02-03 stsp "GITCONFIG_OWNER_REQUEST");
1698 9a1cc63f 2020-02-03 stsp return flush_imsg(ibuf);
1701 9a1cc63f 2020-02-03 stsp const struct got_error *
1702 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1704 aba9c984 2019-09-08 stsp size_t len = value ? strlen(value) + 1 : 0;
1706 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1707 aba9c984 2019-09-08 stsp value, len) == -1)
1708 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1710 aba9c984 2019-09-08 stsp return flush_imsg(ibuf);
1713 aba9c984 2019-09-08 stsp const struct got_error *
1714 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1716 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1717 aba9c984 2019-09-08 stsp struct imsg imsg;
1718 aba9c984 2019-09-08 stsp size_t datalen;
1719 aba9c984 2019-09-08 stsp const size_t min_datalen = 0;
1721 aba9c984 2019-09-08 stsp *str = NULL;
1723 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1725 aba9c984 2019-09-08 stsp return err;
1726 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1728 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1729 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_STR_VAL:
1730 aba9c984 2019-09-08 stsp if (datalen == 0)
1732 aba9c984 2019-09-08 stsp *str = malloc(datalen);
1733 aba9c984 2019-09-08 stsp if (*str == NULL) {
1734 aba9c984 2019-09-08 stsp err = got_error_from_errno("malloc");
1737 aba9c984 2019-09-08 stsp if (strlcpy(*str, imsg.data, datalen) >= datalen)
1738 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_NO_SPACE);
1741 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1745 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1746 aba9c984 2019-09-08 stsp return err;
1749 aba9c984 2019-09-08 stsp const struct got_error *
1750 aba9c984 2019-09-08 stsp got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1752 aba9c984 2019-09-08 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1753 aba9c984 2019-09-08 stsp &value, sizeof(value)) == -1)
1754 aba9c984 2019-09-08 stsp return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1756 876c234b 2018-09-10 stsp return flush_imsg(ibuf);
1759 63219cd2 2019-01-04 stsp const struct got_error *
1760 aba9c984 2019-09-08 stsp got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1762 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
1763 aba9c984 2019-09-08 stsp struct imsg imsg;
1764 aba9c984 2019-09-08 stsp size_t datalen;
1765 aba9c984 2019-09-08 stsp const size_t min_datalen =
1766 aba9c984 2019-09-08 stsp MIN(sizeof(struct got_imsg_error), sizeof(int));
1770 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1772 aba9c984 2019-09-08 stsp return err;
1773 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1775 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
1776 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_INT_VAL:
1777 aba9c984 2019-09-08 stsp if (datalen != sizeof(*val)) {
1778 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1781 aba9c984 2019-09-08 stsp memcpy(val, imsg.data, sizeof(*val));
1784 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1788 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1789 cd95becd 2019-11-29 stsp return err;
1792 cd95becd 2019-11-29 stsp const struct got_error *
1793 cd95becd 2019-11-29 stsp got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1794 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes, int nremotes)
1796 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1797 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1800 cd95becd 2019-11-29 stsp iremotes.nremotes = nremotes;
1801 cd95becd 2019-11-29 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1802 cd95becd 2019-11-29 stsp &iremotes, sizeof(iremotes)) == -1)
1803 cd95becd 2019-11-29 stsp return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1805 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1806 cd95becd 2019-11-29 stsp imsg_clear(ibuf);
1808 cd95becd 2019-11-29 stsp return err;
1810 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++) {
1811 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1812 cd95becd 2019-11-29 stsp size_t len = sizeof(iremote);
1813 cd95becd 2019-11-29 stsp struct ibuf *wbuf;
1815 cd95becd 2019-11-29 stsp iremote.name_len = strlen(remotes[i].name);
1816 cd95becd 2019-11-29 stsp len += iremote.name_len;
1817 cd95becd 2019-11-29 stsp iremote.url_len = strlen(remotes[i].url);
1818 cd95becd 2019-11-29 stsp len += iremote.url_len;
1820 cd95becd 2019-11-29 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1821 cd95becd 2019-11-29 stsp if (wbuf == NULL)
1822 cd95becd 2019-11-29 stsp return got_error_from_errno(
1823 cd95becd 2019-11-29 stsp "imsg_create GITCONFIG_REMOTE");
1825 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1826 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1827 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1828 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1829 cd95becd 2019-11-29 stsp return err;
1832 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1833 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1834 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1835 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1836 cd95becd 2019-11-29 stsp return err;
1838 cd95becd 2019-11-29 stsp if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1839 cd95becd 2019-11-29 stsp err = got_error_from_errno(
1840 cd95becd 2019-11-29 stsp "imsg_add GIITCONFIG_REMOTE");
1841 cd95becd 2019-11-29 stsp ibuf_free(wbuf);
1842 cd95becd 2019-11-29 stsp return err;
1845 cd95becd 2019-11-29 stsp wbuf->fd = -1;
1846 cd95becd 2019-11-29 stsp imsg_close(ibuf, wbuf);
1847 cd95becd 2019-11-29 stsp err = flush_imsg(ibuf);
1849 cd95becd 2019-11-29 stsp return err;
1852 cd95becd 2019-11-29 stsp return NULL;
1855 cd95becd 2019-11-29 stsp const struct got_error *
1856 cd95becd 2019-11-29 stsp got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1857 cd95becd 2019-11-29 stsp int *nremotes, struct imsgbuf *ibuf)
1859 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
1860 cd95becd 2019-11-29 stsp struct imsg imsg;
1861 cd95becd 2019-11-29 stsp size_t datalen;
1862 cd95becd 2019-11-29 stsp struct got_imsg_remotes iremotes;
1863 cd95becd 2019-11-29 stsp struct got_imsg_remote iremote;
1865 cd95becd 2019-11-29 stsp *remotes = NULL;
1866 cd95becd 2019-11-29 stsp *nremotes = 0;
1867 d669b9c9 2020-02-22 stsp iremotes.nremotes = 0;
1869 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1871 cd95becd 2019-11-29 stsp return err;
1872 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1874 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1875 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES:
1876 cd95becd 2019-11-29 stsp if (datalen != sizeof(iremotes)) {
1877 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1880 cd95becd 2019-11-29 stsp memcpy(&iremotes, imsg.data, sizeof(iremotes));
1881 cd95becd 2019-11-29 stsp if (iremotes.nremotes == 0) {
1882 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1883 cd95becd 2019-11-29 stsp return NULL;
1887 54b1c5b5 2020-02-22 stsp imsg_free(&imsg);
1888 54b1c5b5 2020-02-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1891 aba9c984 2019-09-08 stsp imsg_free(&imsg);
1893 cd95becd 2019-11-29 stsp *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1894 cd95becd 2019-11-29 stsp if (*remotes == NULL)
1895 cd95becd 2019-11-29 stsp return got_error_from_errno("recallocarray");
1897 cd95becd 2019-11-29 stsp while (*nremotes < iremotes.nremotes) {
1898 cd95becd 2019-11-29 stsp struct got_remote_repo *remote;
1900 cd95becd 2019-11-29 stsp err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1903 cd95becd 2019-11-29 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1905 cd95becd 2019-11-29 stsp switch (imsg.hdr.type) {
1906 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTE:
1907 cd95becd 2019-11-29 stsp remote = &(*remotes)[*nremotes];
1908 cd95becd 2019-11-29 stsp if (datalen < sizeof(iremote)) {
1909 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1912 cd95becd 2019-11-29 stsp memcpy(&iremote, imsg.data, sizeof(iremote));
1913 cd95becd 2019-11-29 stsp if (iremote.name_len == 0 || iremote.url_len == 0 ||
1914 cd95becd 2019-11-29 stsp (sizeof(iremote) + iremote.name_len +
1915 cd95becd 2019-11-29 stsp iremote.url_len) > datalen) {
1916 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1919 cd95becd 2019-11-29 stsp remote->name = strndup(imsg.data + sizeof(iremote),
1920 cd95becd 2019-11-29 stsp iremote.name_len);
1921 cd95becd 2019-11-29 stsp if (remote->name == NULL) {
1922 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1925 cd95becd 2019-11-29 stsp remote->url = strndup(imsg.data + sizeof(iremote) +
1926 cd95becd 2019-11-29 stsp iremote.name_len, iremote.url_len);
1927 cd95becd 2019-11-29 stsp if (remote->url == NULL) {
1928 cd95becd 2019-11-29 stsp err = got_error_from_errno("strndup");
1929 cd95becd 2019-11-29 stsp free(remote->name);
1932 cd95becd 2019-11-29 stsp (*nremotes)++;
1935 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1939 cd95becd 2019-11-29 stsp imsg_free(&imsg);
1944 cd95becd 2019-11-29 stsp if (err) {
1946 cd95becd 2019-11-29 stsp for (i = 0; i < *nremotes; i++) {
1947 cd95becd 2019-11-29 stsp free((*remotes)[i].name);
1948 cd95becd 2019-11-29 stsp free((*remotes)[i].url);
1950 cd95becd 2019-11-29 stsp free(*remotes);
1951 cd95becd 2019-11-29 stsp *remotes = NULL;
1952 cd95becd 2019-11-29 stsp *nremotes = 0;
1954 aba9c984 2019-09-08 stsp return err;
1957 ca6e02ac 2020-01-07 stsp const struct got_error *
1958 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1959 ca6e02ac 2020-01-07 stsp struct got_object_id *id, int idx, const char *path)
1961 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1962 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
1963 ca6e02ac 2020-01-07 stsp size_t path_len = strlen(path) + 1;
1965 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1966 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_commit_traversal_request) + path_len);
1967 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
1968 ca6e02ac 2020-01-07 stsp return got_error_from_errno(
1969 ca6e02ac 2020-01-07 stsp "imsg_create COMMIT_TRAVERSAL_REQUEST");
1970 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1971 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1972 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1973 ca6e02ac 2020-01-07 stsp return err;
1975 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1976 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1977 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1978 ca6e02ac 2020-01-07 stsp return err;
1980 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, path, path_len) == -1) {
1981 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1982 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
1983 ca6e02ac 2020-01-07 stsp return err;
1986 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
1987 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
1989 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
1992 aba9c984 2019-09-08 stsp const struct got_error *
1993 ca6e02ac 2020-01-07 stsp got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1994 ca6e02ac 2020-01-07 stsp size_t ncommits, struct imsgbuf *ibuf)
1996 ca6e02ac 2020-01-07 stsp const struct got_error *err;
1997 ca6e02ac 2020-01-07 stsp struct ibuf *wbuf;
2000 ca6e02ac 2020-01-07 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2001 ca6e02ac 2020-01-07 stsp sizeof(struct got_imsg_traversed_commits) +
2002 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH);
2003 ca6e02ac 2020-01-07 stsp if (wbuf == NULL)
2004 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2006 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2007 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2008 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2009 ca6e02ac 2020-01-07 stsp return err;
2011 ca6e02ac 2020-01-07 stsp for (i = 0; i < ncommits; i++) {
2012 ca6e02ac 2020-01-07 stsp struct got_object_id *id = &commit_ids[i];
2013 ca6e02ac 2020-01-07 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2014 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2015 ca6e02ac 2020-01-07 stsp "imsg_add TRAVERSED_COMMITS");
2016 ca6e02ac 2020-01-07 stsp ibuf_free(wbuf);
2017 ca6e02ac 2020-01-07 stsp return err;
2021 ca6e02ac 2020-01-07 stsp wbuf->fd = -1;
2022 ca6e02ac 2020-01-07 stsp imsg_close(ibuf, wbuf);
2024 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2027 ca6e02ac 2020-01-07 stsp const struct got_error *
2028 ca6e02ac 2020-01-07 stsp got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2029 ca6e02ac 2020-01-07 stsp struct got_object_id **changed_commit_id,
2030 ca6e02ac 2020-01-07 stsp struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2032 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2033 ca6e02ac 2020-01-07 stsp struct imsg imsg;
2034 ca6e02ac 2020-01-07 stsp struct got_imsg_traversed_commits *icommits;
2035 ca6e02ac 2020-01-07 stsp size_t datalen;
2036 ca6e02ac 2020-01-07 stsp int i, done = 0;
2038 ca6e02ac 2020-01-07 stsp *changed_commit = NULL;
2039 ca6e02ac 2020-01-07 stsp *changed_commit_id = NULL;
2041 ca6e02ac 2020-01-07 stsp while (!done) {
2042 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2044 ca6e02ac 2020-01-07 stsp return err;
2046 ca6e02ac 2020-01-07 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2047 ca6e02ac 2020-01-07 stsp switch (imsg.hdr.type) {
2048 ca6e02ac 2020-01-07 stsp case GOT_IMSG_TRAVERSED_COMMITS:
2049 ca6e02ac 2020-01-07 stsp icommits = imsg.data;
2050 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(*icommits) +
2051 ca6e02ac 2020-01-07 stsp icommits->ncommits * SHA1_DIGEST_LENGTH) {
2052 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
2055 ca6e02ac 2020-01-07 stsp for (i = 0; i < icommits->ncommits; i++) {
2056 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
2057 ca6e02ac 2020-01-07 stsp uint8_t *sha1 = (uint8_t *)imsg.data +
2058 ca6e02ac 2020-01-07 stsp sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2059 ca6e02ac 2020-01-07 stsp err = got_object_qid_alloc_partial(&qid);
2062 ca6e02ac 2020-01-07 stsp memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2063 ca6e02ac 2020-01-07 stsp SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2065 ca6e02ac 2020-01-07 stsp /* The last commit may contain a change. */
2066 ca6e02ac 2020-01-07 stsp if (i == icommits->ncommits - 1) {
2067 ca6e02ac 2020-01-07 stsp *changed_commit_id =
2068 ca6e02ac 2020-01-07 stsp got_object_id_dup(qid->id);
2069 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2070 ca6e02ac 2020-01-07 stsp err = got_error_from_errno(
2071 ca6e02ac 2020-01-07 stsp "got_object_id_dup");
2077 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT:
2078 ca6e02ac 2020-01-07 stsp if (*changed_commit_id == NULL) {
2079 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2082 ca6e02ac 2020-01-07 stsp err = get_commit_from_imsg(changed_commit, &imsg,
2083 ca6e02ac 2020-01-07 stsp datalen, ibuf);
2085 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2089 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2093 ca6e02ac 2020-01-07 stsp imsg_free(&imsg);
2099 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(commit_ids);
2100 ca6e02ac 2020-01-07 stsp return err;
2103 ca6e02ac 2020-01-07 stsp const struct got_error *
2104 ca6e02ac 2020-01-07 stsp got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2106 ca6e02ac 2020-01-07 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2107 ca6e02ac 2020-01-07 stsp NULL, 0) == -1)
2108 ca6e02ac 2020-01-07 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2110 ca6e02ac 2020-01-07 stsp return flush_imsg(ibuf);
2113 ca6e02ac 2020-01-07 stsp const struct got_error *
2114 63219cd2 2019-01-04 stsp got_privsep_unveil_exec_helpers(void)
2116 c39c25dd 2019-08-09 stsp const char *helpers[] = {
2117 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_PACK,
2118 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_OBJECT,
2119 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_COMMIT,
2120 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TREE,
2121 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_BLOB,
2122 c39c25dd 2019-08-09 stsp GOT_PATH_PROG_READ_TAG,
2123 aba9c984 2019-09-08 stsp GOT_PATH_PROG_READ_GITCONFIG,
2127 c39c25dd 2019-08-09 stsp for (i = 0; i < nitems(helpers); i++) {
2128 c39c25dd 2019-08-09 stsp if (unveil(helpers[i], "x") == 0)
2130 c39c25dd 2019-08-09 stsp return got_error_from_errno2("unveil", helpers[i]);
2133 63219cd2 2019-01-04 stsp return NULL;
2137 aba9c984 2019-09-08 stsp got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2139 aba9c984 2019-09-08 stsp if (close(imsg_fds[0]) != 0) {
2140 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2144 aba9c984 2019-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2145 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2148 aba9c984 2019-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2149 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2153 aba9c984 2019-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
2154 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2155 aba9c984 2019-09-08 stsp strerror(errno));