portable: set next version
[got-portable.git] / lib / privsep.c
blob097787082caaa1182b3a16d565c2bdee8e33d6de
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/wait.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdint.h>
31 #include <poll.h>
32 #include <unistd.h>
33 #include <zlib.h>
35 #include "got_compat.h"
37 #include "got_object.h"
38 #include "got_error.h"
39 #include "got_path.h"
40 #include "got_repository.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_poll.h"
51 #include "got_privsep.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 static const struct got_error *
62 read_imsg(struct imsgbuf *ibuf)
64 const struct got_error *err;
65 size_t n;
67 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
68 if (err) {
69 if (err->code == GOT_ERR_EOF)
70 return got_error(GOT_ERR_PRIVSEP_PIPE);
71 return err;
74 n = imsg_read(ibuf);
75 if (n == -1) {
76 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
77 return got_error(GOT_ERR_PRIVSEP_NO_FD);
78 return got_error(GOT_ERR_PRIVSEP_READ);
80 if (n == 0)
81 return got_error(GOT_ERR_PRIVSEP_PIPE);
83 return NULL;
86 const struct got_error *
87 got_privsep_wait_for_child(pid_t pid)
89 int child_status;
91 if (waitpid(pid, &child_status, 0) == -1)
92 return got_error_from_errno("waitpid");
94 if (!WIFEXITED(child_status))
95 return got_error(GOT_ERR_PRIVSEP_DIED);
97 if (WEXITSTATUS(child_status) != 0)
98 return got_error(GOT_ERR_PRIVSEP_EXIT);
100 return NULL;
103 static const struct got_error *
104 recv_imsg_error(struct imsg *imsg, size_t datalen)
106 struct got_imsg_error *ierr;
108 if (datalen != sizeof(*ierr))
109 return got_error(GOT_ERR_PRIVSEP_LEN);
111 ierr = imsg->data;
112 if (ierr->code == GOT_ERR_ERRNO) {
113 static struct got_error serr;
114 serr.code = GOT_ERR_ERRNO;
115 serr.msg = strerror(ierr->errno_code);
116 return &serr;
119 return got_error(ierr->code);
122 const struct got_error *
123 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
124 size_t min_datalen)
126 const struct got_error *err;
127 ssize_t n;
129 n = imsg_get(ibuf, imsg);
130 if (n == -1)
131 return got_error_from_errno("imsg_get");
133 while (n == 0) {
134 err = read_imsg(ibuf);
135 if (err)
136 return err;
137 n = imsg_get(ibuf, imsg);
138 if (n == -1)
139 return got_error_from_errno("imsg_get");
142 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
143 return got_error(GOT_ERR_PRIVSEP_LEN);
145 if (imsg->hdr.type == GOT_IMSG_ERROR) {
146 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
147 return recv_imsg_error(imsg, datalen);
150 return NULL;
153 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
154 void
155 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
157 const struct got_error *poll_err;
158 struct got_imsg_error ierr;
159 int ret;
161 ierr.code = err->code;
162 if (err->code == GOT_ERR_ERRNO)
163 ierr.errno_code = errno;
164 else
165 ierr.errno_code = 0;
166 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
167 if (ret == -1) {
168 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
169 getprogname(), err->code, err->msg, strerror(errno));
170 return;
173 poll_err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
174 if (poll_err) {
175 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
176 getprogname(), err->code, err->msg, poll_err->msg);
177 return;
180 ret = imsg_flush(ibuf);
181 if (ret == -1) {
182 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
183 getprogname(), err->code, err->msg, strerror(errno));
184 imsg_clear(ibuf);
185 return;
189 static const struct got_error *
190 flush_imsg(struct imsgbuf *ibuf)
192 const struct got_error *err;
194 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
195 if (err)
196 return err;
198 if (imsg_flush(ibuf) == -1) {
199 imsg_clear(ibuf);
200 return got_error_from_errno("imsg_flush");
203 return NULL;
206 const struct got_error *
207 got_privsep_flush_imsg(struct imsgbuf *ibuf)
209 return flush_imsg(ibuf);
212 const struct got_error *
213 got_privsep_send_stop(int fd)
215 const struct got_error *err = NULL;
216 struct imsgbuf ibuf;
218 imsg_init(&ibuf, fd);
220 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
221 return got_error_from_errno("imsg_compose STOP");
223 err = flush_imsg(&ibuf);
224 return err;
227 const struct got_error *
228 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
229 struct got_object_id *id)
231 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
232 id, sizeof(*id)) == -1)
233 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
235 return flush_imsg(ibuf);
238 const struct got_error *
239 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
240 struct got_object_id *id)
242 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
243 id, sizeof(*id)) == -1)
244 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
246 return flush_imsg(ibuf);
249 const struct got_error *
250 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
252 const struct got_error *err = NULL;
254 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
255 == -1) {
256 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
257 close(outfd);
258 return err;
261 return flush_imsg(ibuf);
264 const struct got_error *
265 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
266 uint8_t *data)
268 const struct got_error *err = NULL;
269 struct got_imsg_raw_obj iobj;
270 size_t len = sizeof(iobj);
271 struct ibuf *wbuf;
273 memset(&iobj, 0, sizeof(iobj));
274 iobj.hdrlen = hdrlen;
275 iobj.size = size;
277 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
278 len += (size_t)size + hdrlen;
280 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
281 if (wbuf == NULL) {
282 err = got_error_from_errno("imsg_create RAW_OBJECT");
283 return err;
286 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
287 return got_error_from_errno("imsg_add RAW_OBJECT");
289 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
290 if (imsg_add(wbuf, data, size + hdrlen) == -1)
291 return got_error_from_errno("imsg_add RAW_OBJECT");
294 wbuf->fd = -1;
295 imsg_close(ibuf, wbuf);
297 return flush_imsg(ibuf);
300 const struct got_error *
301 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
302 struct imsgbuf *ibuf)
304 const struct got_error *err = NULL;
305 struct imsg imsg;
306 struct got_imsg_raw_obj *iobj;
307 size_t datalen;
309 *outbuf = NULL;
311 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
312 if (err)
313 return err;
315 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
317 switch (imsg.hdr.type) {
318 case GOT_IMSG_RAW_OBJECT:
319 if (datalen < sizeof(*iobj)) {
320 err = got_error(GOT_ERR_PRIVSEP_LEN);
321 break;
323 iobj = imsg.data;
324 *size = iobj->size;
325 *hdrlen = iobj->hdrlen;
327 if (datalen == sizeof(*iobj)) {
328 /* Data has been written to file descriptor. */
329 break;
332 if (*size < 0 ||
333 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
334 err = got_error(GOT_ERR_PRIVSEP_LEN);
335 break;
338 *outbuf = malloc(*size + *hdrlen);
339 if (*outbuf == NULL) {
340 err = got_error_from_errno("malloc");
341 break;
343 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
344 break;
345 default:
346 err = got_error(GOT_ERR_PRIVSEP_MSG);
347 break;
350 imsg_free(&imsg);
352 return err;
355 const struct got_error *
356 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
357 struct got_object_id *id, int pack_idx)
359 const struct got_error *err = NULL;
360 struct got_imsg_packed_object iobj;
361 void *data;
362 size_t len;
364 memset(&iobj, 0, sizeof(iobj));
365 if (pack_idx != -1) { /* commit is packed */
366 iobj.idx = pack_idx;
367 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
368 data = &iobj;
369 len = sizeof(iobj);
370 } else {
371 data = id;
372 len = sizeof(*id);
375 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
376 == -1) {
377 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
378 close(fd);
379 return err;
382 return flush_imsg(ibuf);
385 const struct got_error *
386 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
387 struct got_object_id *id, int pack_idx)
389 struct ibuf *wbuf;
390 size_t len;
392 if (pack_idx != -1)
393 len = sizeof(struct got_imsg_packed_object);
394 else
395 len = sizeof(*id);
397 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
398 if (wbuf == NULL)
399 return got_error_from_errno("imsg_create TREE_REQUEST");
401 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
402 return got_error_from_errno("imsg_add TREE_REQUEST");
404 if (pack_idx != -1) { /* tree is packed */
405 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
406 return got_error_from_errno("imsg_add TREE_REQUEST");
409 wbuf->fd = fd;
410 imsg_close(ibuf, wbuf);
412 return flush_imsg(ibuf);
415 const struct got_error *
416 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
417 struct got_object_id *id, int pack_idx)
419 struct got_imsg_packed_object iobj;
420 void *data;
421 size_t len;
423 memset(&iobj, 0, sizeof(iobj));
424 if (pack_idx != -1) { /* tag is packed */
425 iobj.idx = pack_idx;
426 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
427 data = &iobj;
428 len = sizeof(iobj);
429 } else {
430 data = id;
431 len = sizeof(*id);
434 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
435 == -1)
436 return got_error_from_errno("imsg_compose TAG_REQUEST");
438 return flush_imsg(ibuf);
441 const struct got_error *
442 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
443 struct got_object_id *id, int pack_idx)
445 const struct got_error *err = NULL;
446 struct got_imsg_packed_object iobj;
447 void *data;
448 size_t len;
450 memset(&iobj, 0, sizeof(iobj));
451 if (pack_idx != -1) { /* blob is packed */
452 iobj.idx = pack_idx;
453 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
454 data = &iobj;
455 len = sizeof(iobj);
456 } else {
457 data = id;
458 len = sizeof(*id);
461 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
462 == -1) {
463 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
464 close(infd);
465 return err;
468 return flush_imsg(ibuf);
471 const struct got_error *
472 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
474 const struct got_error *err = NULL;
476 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
477 == -1) {
478 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
479 close(outfd);
480 return err;
483 return flush_imsg(ibuf);
486 static const struct got_error *
487 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
489 const struct got_error *err = NULL;
491 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
492 err = got_error_from_errno("imsg_compose TMPFD");
493 close(fd);
494 return err;
497 return flush_imsg(ibuf);
500 const struct got_error *
501 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
503 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
506 const struct got_error *
507 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
509 struct got_imsg_object iobj;
511 memset(&iobj, 0, sizeof(iobj));
513 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
514 iobj.type = obj->type;
515 iobj.flags = obj->flags;
516 iobj.hdrlen = obj->hdrlen;
517 iobj.size = obj->size;
518 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
519 iobj.pack_offset = obj->pack_offset;
520 iobj.pack_idx = obj->pack_idx;
523 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
524 == -1)
525 return got_error_from_errno("imsg_compose OBJECT");
527 return flush_imsg(ibuf);
530 const struct got_error *
531 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
532 struct got_pathlist_head *have_refs, int fetch_all_branches,
533 struct got_pathlist_head *wanted_branches,
534 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
536 const struct got_error *err = NULL;
537 struct ibuf *wbuf;
538 size_t len;
539 struct got_pathlist_entry *pe;
540 struct got_imsg_fetch_request fetchreq;
542 memset(&fetchreq, 0, sizeof(fetchreq));
543 fetchreq.fetch_all_branches = fetch_all_branches;
544 fetchreq.list_refs_only = list_refs_only;
545 fetchreq.verbosity = verbosity;
546 TAILQ_FOREACH(pe, have_refs, entry)
547 fetchreq.n_have_refs++;
548 TAILQ_FOREACH(pe, wanted_branches, entry)
549 fetchreq.n_wanted_branches++;
550 TAILQ_FOREACH(pe, wanted_refs, entry)
551 fetchreq.n_wanted_refs++;
552 len = sizeof(struct got_imsg_fetch_request);
553 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
554 close(fd);
555 return got_error(GOT_ERR_NO_SPACE);
558 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
559 &fetchreq, sizeof(fetchreq)) == -1)
560 return got_error_from_errno(
561 "imsg_compose FETCH_SERVER_PROGRESS");
563 err = flush_imsg(ibuf);
564 if (err) {
565 close(fd);
566 return err;
568 fd = -1;
570 TAILQ_FOREACH(pe, have_refs, entry) {
571 const char *name = pe->path;
572 size_t name_len = pe->path_len;
573 struct got_object_id *id = pe->data;
575 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
576 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
577 if (wbuf == NULL)
578 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
580 /* Keep in sync with struct got_imsg_fetch_have_ref! */
581 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
582 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
583 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
584 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
585 if (imsg_add(wbuf, name, name_len) == -1)
586 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
588 wbuf->fd = -1;
589 imsg_close(ibuf, wbuf);
590 err = flush_imsg(ibuf);
591 if (err)
592 return err;
595 TAILQ_FOREACH(pe, wanted_branches, entry) {
596 const char *name = pe->path;
597 size_t name_len = pe->path_len;
599 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
600 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
601 len);
602 if (wbuf == NULL)
603 return got_error_from_errno(
604 "imsg_create FETCH_WANTED_BRANCH");
606 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
607 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
608 return got_error_from_errno(
609 "imsg_add FETCH_WANTED_BRANCH");
610 if (imsg_add(wbuf, name, name_len) == -1)
611 return got_error_from_errno(
612 "imsg_add FETCH_WANTED_BRANCH");
614 wbuf->fd = -1;
615 imsg_close(ibuf, wbuf);
616 err = flush_imsg(ibuf);
617 if (err)
618 return err;
621 TAILQ_FOREACH(pe, wanted_refs, entry) {
622 const char *name = pe->path;
623 size_t name_len = pe->path_len;
625 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
626 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
627 len);
628 if (wbuf == NULL)
629 return got_error_from_errno(
630 "imsg_create FETCH_WANTED_REF");
632 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
633 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
634 return got_error_from_errno(
635 "imsg_add FETCH_WANTED_REF");
636 if (imsg_add(wbuf, name, name_len) == -1)
637 return got_error_from_errno(
638 "imsg_add FETCH_WANTED_REF");
640 wbuf->fd = -1;
641 imsg_close(ibuf, wbuf);
642 err = flush_imsg(ibuf);
643 if (err)
644 return err;
648 return NULL;
652 const struct got_error *
653 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
655 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
658 const struct got_error *
659 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
660 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
661 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
663 const struct got_error *err = NULL;
664 struct imsg imsg;
665 size_t datalen;
666 struct got_imsg_fetch_symrefs *isymrefs = NULL;
667 size_t n, remain;
668 off_t off;
669 int i;
671 *done = 0;
672 *id = NULL;
673 *refname = NULL;
674 *server_progress = NULL;
675 *packfile_size = 0;
676 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
678 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
679 if (err)
680 return err;
682 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
683 switch (imsg.hdr.type) {
684 case GOT_IMSG_FETCH_SYMREFS:
685 if (datalen < sizeof(*isymrefs)) {
686 err = got_error(GOT_ERR_PRIVSEP_LEN);
687 break;
689 if (isymrefs != NULL) {
690 err = got_error(GOT_ERR_PRIVSEP_MSG);
691 break;
693 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
694 off = sizeof(*isymrefs);
695 remain = datalen - off;
696 for (n = 0; n < isymrefs->nsymrefs; n++) {
697 struct got_imsg_fetch_symref *s;
698 char *name, *target;
699 if (remain < sizeof(struct got_imsg_fetch_symref)) {
700 err = got_error(GOT_ERR_PRIVSEP_LEN);
701 goto done;
703 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
704 off += sizeof(*s);
705 remain -= sizeof(*s);
706 if (remain < s->name_len) {
707 err = got_error(GOT_ERR_PRIVSEP_LEN);
708 goto done;
710 name = strndup(imsg.data + off, s->name_len);
711 if (name == NULL) {
712 err = got_error_from_errno("strndup");
713 goto done;
715 off += s->name_len;
716 remain -= s->name_len;
717 if (remain < s->target_len) {
718 err = got_error(GOT_ERR_PRIVSEP_LEN);
719 free(name);
720 goto done;
722 target = strndup(imsg.data + off, s->target_len);
723 if (target == NULL) {
724 err = got_error_from_errno("strndup");
725 free(name);
726 goto done;
728 off += s->target_len;
729 remain -= s->target_len;
730 err = got_pathlist_append(symrefs, name, target);
731 if (err) {
732 free(name);
733 free(target);
734 goto done;
737 break;
738 case GOT_IMSG_FETCH_REF:
739 if (datalen <= SHA1_DIGEST_LENGTH) {
740 err = got_error(GOT_ERR_PRIVSEP_MSG);
741 break;
743 *id = malloc(sizeof(**id));
744 if (*id == NULL) {
745 err = got_error_from_errno("malloc");
746 break;
748 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
749 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
750 datalen - SHA1_DIGEST_LENGTH);
751 if (*refname == NULL) {
752 err = got_error_from_errno("strndup");
753 break;
755 break;
756 case GOT_IMSG_FETCH_SERVER_PROGRESS:
757 if (datalen == 0) {
758 err = got_error(GOT_ERR_PRIVSEP_LEN);
759 break;
761 *server_progress = strndup(imsg.data, datalen);
762 if (*server_progress == NULL) {
763 err = got_error_from_errno("strndup");
764 break;
766 for (i = 0; i < datalen; i++) {
767 if (!isprint((unsigned char)(*server_progress)[i]) &&
768 !isspace((unsigned char)(*server_progress)[i])) {
769 err = got_error(GOT_ERR_PRIVSEP_MSG);
770 free(*server_progress);
771 *server_progress = NULL;
772 goto done;
775 break;
776 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
777 if (datalen < sizeof(*packfile_size)) {
778 err = got_error(GOT_ERR_PRIVSEP_MSG);
779 break;
781 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
782 break;
783 case GOT_IMSG_FETCH_DONE:
784 if (datalen != SHA1_DIGEST_LENGTH) {
785 err = got_error(GOT_ERR_PRIVSEP_MSG);
786 break;
788 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
789 *done = 1;
790 break;
791 default:
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
793 break;
795 done:
796 if (err) {
797 free(*id);
798 *id = NULL;
799 free(*refname);
800 *refname = NULL;
802 imsg_free(&imsg);
803 return err;
806 static const struct got_error *
807 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
808 int delete, struct imsgbuf *ibuf)
810 size_t len;
811 struct ibuf *wbuf;
813 len = sizeof(struct got_imsg_send_ref) + name_len;
814 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
815 if (wbuf == NULL)
816 return got_error_from_errno("imsg_create SEND_REF");
818 /* Keep in sync with struct got_imsg_send_ref! */
819 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
820 return got_error_from_errno("imsg_add SEND_REF");
821 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
822 return got_error_from_errno("imsg_add SEND_REF");
823 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
824 return got_error_from_errno("imsg_add SEND_REF");
825 if (imsg_add(wbuf, name, name_len) == -1)
826 return got_error_from_errno("imsg_add SEND_REF");
828 wbuf->fd = -1;
829 imsg_close(ibuf, wbuf);
830 return flush_imsg(ibuf);
833 const struct got_error *
834 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
835 struct got_pathlist_head *have_refs,
836 struct got_pathlist_head *delete_refs,
837 int verbosity)
839 const struct got_error *err = NULL;
840 struct got_pathlist_entry *pe;
841 struct got_imsg_send_request sendreq;
842 struct got_object_id zero_id;
844 memset(&zero_id, 0, sizeof(zero_id));
845 memset(&sendreq, 0, sizeof(sendreq));
846 sendreq.verbosity = verbosity;
847 TAILQ_FOREACH(pe, have_refs, entry)
848 sendreq.nrefs++;
849 TAILQ_FOREACH(pe, delete_refs, entry)
850 sendreq.nrefs++;
851 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
852 &sendreq, sizeof(sendreq)) == -1) {
853 err = got_error_from_errno(
854 "imsg_compose FETCH_SERVER_PROGRESS");
855 goto done;
858 err = flush_imsg(ibuf);
859 if (err)
860 goto done;
861 fd = -1;
863 TAILQ_FOREACH(pe, have_refs, entry) {
864 const char *name = pe->path;
865 size_t name_len = pe->path_len;
866 struct got_object_id *id = pe->data;
867 err = send_send_ref(name, name_len, id, 0, ibuf);
868 if (err)
869 goto done;
872 TAILQ_FOREACH(pe, delete_refs, entry) {
873 const char *name = pe->path;
874 size_t name_len = pe->path_len;
875 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
876 if (err)
877 goto done;
879 done:
880 if (fd != -1 && close(fd) == -1 && err == NULL)
881 err = got_error_from_errno("close");
882 return err;
886 const struct got_error *
887 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
888 struct imsgbuf *ibuf)
890 const struct got_error *err = NULL;
891 struct imsg imsg;
892 size_t datalen;
893 int done = 0;
894 struct got_imsg_send_remote_ref iremote_ref;
895 struct got_object_id *id = NULL;
896 char *refname = NULL;
897 struct got_pathlist_entry *new;
899 while (!done) {
900 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
901 if (err)
902 return err;
903 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
904 switch (imsg.hdr.type) {
905 case GOT_IMSG_SEND_REMOTE_REF:
906 if (datalen < sizeof(iremote_ref)) {
907 err = got_error(GOT_ERR_PRIVSEP_MSG);
908 goto done;
910 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
911 if (datalen != sizeof(iremote_ref) +
912 iremote_ref.name_len) {
913 err = got_error(GOT_ERR_PRIVSEP_MSG);
914 goto done;
916 id = malloc(sizeof(*id));
917 if (id == NULL) {
918 err = got_error_from_errno("malloc");
919 goto done;
921 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
922 refname = strndup(imsg.data + sizeof(iremote_ref),
923 datalen - sizeof(iremote_ref));
924 if (refname == NULL) {
925 err = got_error_from_errno("strndup");
926 goto done;
928 err = got_pathlist_insert(&new, remote_refs,
929 refname, id);
930 if (err)
931 goto done;
932 if (new == NULL) { /* duplicate which wasn't inserted */
933 free(id);
934 free(refname);
936 id = NULL;
937 refname = NULL;
938 break;
939 case GOT_IMSG_SEND_PACK_REQUEST:
940 if (datalen != 0) {
941 err = got_error(GOT_ERR_PRIVSEP_MSG);
942 goto done;
944 /* got-send-pack is now waiting for a pack file. */
945 done = 1;
946 break;
947 default:
948 err = got_error(GOT_ERR_PRIVSEP_MSG);
949 break;
952 done:
953 free(id);
954 free(refname);
955 imsg_free(&imsg);
956 return err;
959 const struct got_error *
960 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
962 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
965 const struct got_error *
966 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
967 int *success, char **refname, struct imsgbuf *ibuf)
969 const struct got_error *err = NULL;
970 struct imsg imsg;
971 size_t datalen;
972 struct got_imsg_send_ref_status iref_status;
974 /* Do not reset the current value of 'bytes_sent', it accumulates. */
975 *done = 0;
976 *success = 0;
977 *refname = NULL;
979 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
980 if (err)
981 return err;
983 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
984 switch (imsg.hdr.type) {
985 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
986 if (datalen < sizeof(*bytes_sent)) {
987 err = got_error(GOT_ERR_PRIVSEP_MSG);
988 break;
990 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
991 break;
992 case GOT_IMSG_SEND_REF_STATUS:
993 if (datalen < sizeof(iref_status)) {
994 err = got_error(GOT_ERR_PRIVSEP_MSG);
995 break;
997 memcpy(&iref_status, imsg.data, sizeof(iref_status));
998 if (datalen != sizeof(iref_status) + iref_status.name_len) {
999 err = got_error(GOT_ERR_PRIVSEP_MSG);
1000 break;
1002 *success = iref_status.success;
1003 *refname = strndup(imsg.data + sizeof(iref_status),
1004 iref_status.name_len);
1005 break;
1006 case GOT_IMSG_SEND_DONE:
1007 if (datalen != 0) {
1008 err = got_error(GOT_ERR_PRIVSEP_MSG);
1009 break;
1011 *done = 1;
1012 break;
1013 default:
1014 err = got_error(GOT_ERR_PRIVSEP_MSG);
1015 break;
1018 imsg_free(&imsg);
1019 return err;
1022 const struct got_error *
1023 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1024 int fd)
1026 const struct got_error *err = NULL;
1028 /* Keep in sync with struct got_imsg_index_pack_request */
1029 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1030 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1031 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1032 close(fd);
1033 return err;
1035 return flush_imsg(ibuf);
1038 const struct got_error *
1039 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1041 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1044 const struct got_error *
1045 got_privsep_recv_index_progress(int *done, int *nobj_total,
1046 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1047 struct imsgbuf *ibuf)
1049 const struct got_error *err = NULL;
1050 struct imsg imsg;
1051 struct got_imsg_index_pack_progress *iprogress;
1052 size_t datalen;
1054 *done = 0;
1055 *nobj_total = 0;
1056 *nobj_indexed = 0;
1057 *nobj_resolved = 0;
1059 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1060 if (err)
1061 return err;
1063 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1064 switch (imsg.hdr.type) {
1065 case GOT_IMSG_IDXPACK_PROGRESS:
1066 if (datalen < sizeof(*iprogress)) {
1067 err = got_error(GOT_ERR_PRIVSEP_LEN);
1068 break;
1070 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1071 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1072 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1073 err = got_error(GOT_ERR_RANGE);
1074 break;
1076 *nobj_total = iprogress->nobj_total;
1077 *nobj_indexed = iprogress->nobj_indexed;
1078 *nobj_loose = iprogress->nobj_loose;
1079 *nobj_resolved = iprogress->nobj_resolved;
1080 break;
1081 case GOT_IMSG_IDXPACK_DONE:
1082 if (datalen != 0) {
1083 err = got_error(GOT_ERR_PRIVSEP_LEN);
1084 break;
1086 *done = 1;
1087 break;
1088 default:
1089 err = got_error(GOT_ERR_PRIVSEP_MSG);
1090 break;
1093 imsg_free(&imsg);
1094 return err;
1097 const struct got_error *
1098 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1099 struct imsgbuf *ibuf)
1101 struct got_imsg_object *iobj;
1102 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1104 if (datalen != sizeof(*iobj))
1105 return got_error(GOT_ERR_PRIVSEP_LEN);
1106 iobj = imsg->data;
1108 if (iobj->pack_offset < 0)
1109 return got_error(GOT_ERR_PACK_OFFSET);
1111 *obj = calloc(1, sizeof(**obj));
1112 if (*obj == NULL)
1113 return got_error_from_errno("calloc");
1115 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1116 (*obj)->type = iobj->type;
1117 (*obj)->flags = iobj->flags;
1118 (*obj)->hdrlen = iobj->hdrlen;
1119 (*obj)->size = iobj->size;
1120 /* path_packfile is handled by caller */
1121 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1122 (*obj)->pack_offset = iobj->pack_offset;
1123 (*obj)->pack_idx = iobj->pack_idx;
1125 STAILQ_INIT(&(*obj)->deltas.entries);
1126 return NULL;
1129 const struct got_error *
1130 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1132 const struct got_error *err = NULL;
1133 struct imsg imsg;
1134 const size_t min_datalen =
1135 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1137 *obj = NULL;
1139 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1140 if (err)
1141 return err;
1143 switch (imsg.hdr.type) {
1144 case GOT_IMSG_OBJECT:
1145 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1146 break;
1147 default:
1148 err = got_error(GOT_ERR_PRIVSEP_MSG);
1149 break;
1152 imsg_free(&imsg);
1154 return err;
1157 static const struct got_error *
1158 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1159 size_t logmsg_len)
1161 const struct got_error *err = NULL;
1162 size_t offset, remain;
1164 offset = 0;
1165 remain = logmsg_len;
1166 while (remain > 0) {
1167 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1169 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1170 commit->logmsg + offset, n) == -1) {
1171 err = got_error_from_errno("imsg_compose "
1172 "COMMIT_LOGMSG");
1173 break;
1176 err = flush_imsg(ibuf);
1177 if (err)
1178 break;
1180 offset += n;
1181 remain -= n;
1184 return err;
1187 const struct got_error *
1188 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1190 const struct got_error *err = NULL;
1191 struct got_imsg_commit_object *icommit;
1192 uint8_t *buf;
1193 size_t len, total;
1194 struct got_object_qid *qid;
1195 size_t author_len = strlen(commit->author);
1196 size_t committer_len = strlen(commit->committer);
1197 size_t logmsg_len = strlen(commit->logmsg);
1199 total = sizeof(*icommit) + author_len + committer_len +
1200 commit->nparents * SHA1_DIGEST_LENGTH;
1202 buf = malloc(total);
1203 if (buf == NULL)
1204 return got_error_from_errno("malloc");
1206 icommit = (struct got_imsg_commit_object *)buf;
1207 memcpy(icommit->tree_id, commit->tree_id->sha1,
1208 sizeof(icommit->tree_id));
1209 icommit->author_len = author_len;
1210 icommit->author_time = commit->author_time;
1211 icommit->author_gmtoff = commit->author_gmtoff;
1212 icommit->committer_len = committer_len;
1213 icommit->committer_time = commit->committer_time;
1214 icommit->committer_gmtoff = commit->committer_gmtoff;
1215 icommit->logmsg_len = logmsg_len;
1216 icommit->nparents = commit->nparents;
1218 len = sizeof(*icommit);
1219 memcpy(buf + len, commit->author, author_len);
1220 len += author_len;
1221 memcpy(buf + len, commit->committer, committer_len);
1222 len += committer_len;
1223 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1224 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1225 len += SHA1_DIGEST_LENGTH;
1228 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1229 err = got_error_from_errno("imsg_compose COMMIT");
1230 goto done;
1233 if (logmsg_len == 0 ||
1234 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1235 err = flush_imsg(ibuf);
1236 if (err)
1237 goto done;
1239 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1240 done:
1241 free(buf);
1242 return err;
1245 static const struct got_error *
1246 get_commit_from_imsg(struct got_commit_object **commit,
1247 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1249 const struct got_error *err = NULL;
1250 struct got_imsg_commit_object *icommit;
1251 size_t len = 0;
1252 int i;
1254 if (datalen < sizeof(*icommit))
1255 return got_error(GOT_ERR_PRIVSEP_LEN);
1257 icommit = imsg->data;
1258 if (datalen != sizeof(*icommit) + icommit->author_len +
1259 icommit->committer_len +
1260 icommit->nparents * SHA1_DIGEST_LENGTH)
1261 return got_error(GOT_ERR_PRIVSEP_LEN);
1263 if (icommit->nparents < 0)
1264 return got_error(GOT_ERR_PRIVSEP_LEN);
1266 len += sizeof(*icommit);
1268 *commit = got_object_commit_alloc_partial();
1269 if (*commit == NULL)
1270 return got_error_from_errno(
1271 "got_object_commit_alloc_partial");
1273 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1274 SHA1_DIGEST_LENGTH);
1275 (*commit)->author_time = icommit->author_time;
1276 (*commit)->author_gmtoff = icommit->author_gmtoff;
1277 (*commit)->committer_time = icommit->committer_time;
1278 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1280 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1281 if ((*commit)->author == NULL) {
1282 err = got_error_from_errno("strndup");
1283 goto done;
1285 len += icommit->author_len;
1287 (*commit)->committer = strndup(imsg->data + len,
1288 icommit->committer_len);
1289 if ((*commit)->committer == NULL) {
1290 err = got_error_from_errno("strndup");
1291 goto done;
1293 len += icommit->committer_len;
1295 if (icommit->logmsg_len == 0) {
1296 (*commit)->logmsg = strdup("");
1297 if ((*commit)->logmsg == NULL) {
1298 err = got_error_from_errno("strdup");
1299 goto done;
1301 } else {
1302 size_t offset = 0, remain = icommit->logmsg_len;
1304 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1305 if ((*commit)->logmsg == NULL) {
1306 err = got_error_from_errno("malloc");
1307 goto done;
1309 while (remain > 0) {
1310 struct imsg imsg_log;
1311 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1312 remain);
1314 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1315 if (err)
1316 goto done;
1318 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1319 err = got_error(GOT_ERR_PRIVSEP_MSG);
1320 goto done;
1323 memcpy((*commit)->logmsg + offset,
1324 imsg_log.data, n);
1325 imsg_free(&imsg_log);
1326 offset += n;
1327 remain -= n;
1329 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1332 for (i = 0; i < icommit->nparents; i++) {
1333 struct got_object_qid *qid;
1335 err = got_object_qid_alloc_partial(&qid);
1336 if (err)
1337 break;
1338 memcpy(&qid->id, imsg->data + len +
1339 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1340 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1341 (*commit)->nparents++;
1343 done:
1344 if (err) {
1345 got_object_commit_close(*commit);
1346 *commit = NULL;
1348 return err;
1351 const struct got_error *
1352 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1354 const struct got_error *err = NULL;
1355 struct imsg imsg;
1356 size_t datalen;
1357 const size_t min_datalen =
1358 MIN(sizeof(struct got_imsg_error),
1359 sizeof(struct got_imsg_commit_object));
1361 *commit = NULL;
1363 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1364 if (err)
1365 return err;
1367 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1369 switch (imsg.hdr.type) {
1370 case GOT_IMSG_COMMIT:
1371 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1372 break;
1373 default:
1374 err = got_error(GOT_ERR_PRIVSEP_MSG);
1375 break;
1378 imsg_free(&imsg);
1380 return err;
1383 static const struct got_error *
1384 send_tree_entries_batch(struct imsgbuf *ibuf,
1385 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1387 struct ibuf *wbuf;
1388 struct got_imsg_tree_entries ientries;
1389 int i;
1391 memset(&ientries, 0, sizeof(ientries));
1393 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1394 if (wbuf == NULL)
1395 return got_error_from_errno("imsg_create TREE_ENTRY");
1397 ientries.nentries = idxN - idx0 + 1;
1398 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1399 return got_error_from_errno("imsg_add TREE_ENTRY");
1401 for (i = idx0; i <= idxN; i++) {
1402 struct got_parsed_tree_entry *pte = &entries[i];
1404 /* Keep in sync with struct got_imsg_tree_object definition! */
1405 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1406 return got_error_from_errno("imsg_add TREE_ENTRY");
1407 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1408 return got_error_from_errno("imsg_add TREE_ENTRY");
1409 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1410 return got_error_from_errno("imsg_add TREE_ENTRY");
1412 /* Remaining bytes are the entry's name. */
1413 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1414 return got_error_from_errno("imsg_add TREE_ENTRY");
1417 wbuf->fd = -1;
1418 imsg_close(ibuf, wbuf);
1419 return NULL;
1422 static const struct got_error *
1423 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1424 int nentries)
1426 const struct got_error *err = NULL;
1427 int i, j;
1428 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1430 i = 0;
1431 for (j = 0; j < nentries; j++) {
1432 struct got_parsed_tree_entry *pte = &entries[j];
1433 size_t len = SHA1_DIGEST_LENGTH + sizeof(pte->mode) +
1434 sizeof(pte->namelen) + pte->namelen;
1436 if (j > 0 &&
1437 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1438 err = send_tree_entries_batch(ibuf, entries,
1439 i, j - 1, entries_len);
1440 if (err)
1441 return err;
1442 i = j;
1443 entries_len = sizeof(struct got_imsg_tree_entries);
1446 entries_len += len;
1449 if (j > 0) {
1450 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1451 entries_len);
1452 if (err)
1453 return err;
1456 return NULL;
1459 const struct got_error *
1460 got_privsep_send_tree(struct imsgbuf *ibuf,
1461 struct got_parsed_tree_entry *entries, int nentries)
1463 const struct got_error *err = NULL;
1464 struct got_imsg_tree_object itree;
1466 memset(&itree, 0, sizeof(itree));
1467 itree.nentries = nentries;
1468 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1469 == -1)
1470 return got_error_from_errno("imsg_compose TREE");
1472 err = send_tree_entries(ibuf, entries, nentries);
1473 if (err)
1474 return err;
1476 return flush_imsg(ibuf);
1480 static const struct got_error *
1481 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1482 int *nentries)
1484 const struct got_error *err = NULL;
1485 struct got_imsg_tree_entries *ientries;
1486 struct got_tree_entry *te;
1487 size_t te_offset;
1488 size_t i;
1490 if (datalen <= sizeof(*ientries) ||
1491 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1492 return got_error(GOT_ERR_PRIVSEP_LEN);
1494 ientries = (struct got_imsg_tree_entries *)data;
1495 if (ientries->nentries > INT_MAX) {
1496 return got_error_msg(GOT_ERR_NO_SPACE,
1497 "too many tree entries");
1500 te_offset = sizeof(*ientries);
1501 for (i = 0; i < ientries->nentries; i++) {
1502 struct got_imsg_tree_entry ite;
1503 const char *te_name;
1504 uint8_t *buf = (uint8_t *)data + te_offset;
1506 if (te_offset >= datalen) {
1507 err = got_error(GOT_ERR_PRIVSEP_LEN);
1508 break;
1511 /* Might not be aligned, size is ~32 bytes. */
1512 memcpy(&ite, buf, sizeof(ite));
1514 if (ite.namelen >= sizeof(te->name)) {
1515 err = got_error(GOT_ERR_PRIVSEP_LEN);
1516 break;
1518 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1519 err = got_error(GOT_ERR_PRIVSEP_LEN);
1520 break;
1523 if (*nentries >= tree->nentries) {
1524 err = got_error(GOT_ERR_PRIVSEP_LEN);
1525 break;
1527 te = &tree->entries[*nentries];
1528 te_name = buf + sizeof(ite);
1529 memcpy(te->name, te_name, ite.namelen);
1530 te->name[ite.namelen] = '\0';
1531 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1532 te->mode = ite.mode;
1533 te->idx = *nentries;
1534 (*nentries)++;
1536 te_offset += sizeof(ite) + ite.namelen;
1539 return err;
1542 const struct got_error *
1543 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1545 const struct got_error *err = NULL;
1546 const size_t min_datalen =
1547 MIN(sizeof(struct got_imsg_error),
1548 sizeof(struct got_imsg_tree_object));
1549 struct got_imsg_tree_object *itree;
1550 int nentries = 0;
1552 *tree = NULL;
1554 while (*tree == NULL || nentries < (*tree)->nentries) {
1555 struct imsg imsg;
1556 size_t datalen;
1558 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1559 if (err)
1560 break;
1562 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1564 switch (imsg.hdr.type) {
1565 case GOT_IMSG_TREE:
1566 /* This message should only appear once. */
1567 if (*tree != NULL) {
1568 err = got_error(GOT_ERR_PRIVSEP_MSG);
1569 break;
1571 if (datalen != sizeof(*itree)) {
1572 err = got_error(GOT_ERR_PRIVSEP_LEN);
1573 break;
1575 itree = imsg.data;
1576 if (itree->nentries < 0) {
1577 err = got_error(GOT_ERR_PRIVSEP_LEN);
1578 break;
1580 *tree = malloc(sizeof(**tree));
1581 if (*tree == NULL) {
1582 err = got_error_from_errno("malloc");
1583 break;
1585 (*tree)->entries = calloc(itree->nentries,
1586 sizeof(struct got_tree_entry));
1587 if ((*tree)->entries == NULL) {
1588 err = got_error_from_errno("malloc");
1589 free(*tree);
1590 *tree = NULL;
1591 break;
1593 (*tree)->nentries = itree->nentries;
1594 (*tree)->refcnt = 0;
1595 break;
1596 case GOT_IMSG_TREE_ENTRIES:
1597 /* This message should be preceeded by GOT_IMSG_TREE. */
1598 if (*tree == NULL) {
1599 err = got_error(GOT_ERR_PRIVSEP_MSG);
1600 break;
1602 err = recv_tree_entries(imsg.data, datalen,
1603 *tree, &nentries);
1604 break;
1605 default:
1606 err = got_error(GOT_ERR_PRIVSEP_MSG);
1607 break;
1610 imsg_free(&imsg);
1611 if (err)
1612 break;
1615 if (*tree && (*tree)->nentries != nentries) {
1616 if (err == NULL)
1617 err = got_error(GOT_ERR_PRIVSEP_LEN);
1618 got_object_tree_close(*tree);
1619 *tree = NULL;
1622 return err;
1625 const struct got_error *
1626 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1627 const uint8_t *data)
1629 struct got_imsg_blob iblob;
1631 memset(&iblob, 0, sizeof(iblob));
1632 iblob.size = size;
1633 iblob.hdrlen = hdrlen;
1635 if (data) {
1636 uint8_t *buf;
1638 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1639 return got_error(GOT_ERR_NO_SPACE);
1641 buf = malloc(sizeof(iblob) + size);
1642 if (buf == NULL)
1643 return got_error_from_errno("malloc");
1645 memcpy(buf, &iblob, sizeof(iblob));
1646 memcpy(buf + sizeof(iblob), data, size);
1647 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1648 sizeof(iblob) + size) == -1) {
1649 free(buf);
1650 return got_error_from_errno("imsg_compose BLOB");
1652 free(buf);
1653 } else {
1654 /* Data has already been written to file descriptor. */
1655 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1656 sizeof(iblob)) == -1)
1657 return got_error_from_errno("imsg_compose BLOB");
1661 return flush_imsg(ibuf);
1664 const struct got_error *
1665 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1666 struct imsgbuf *ibuf)
1668 const struct got_error *err = NULL;
1669 struct imsg imsg;
1670 struct got_imsg_blob *iblob;
1671 size_t datalen;
1673 *outbuf = NULL;
1675 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1676 if (err)
1677 return err;
1679 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1681 switch (imsg.hdr.type) {
1682 case GOT_IMSG_BLOB:
1683 if (datalen < sizeof(*iblob)) {
1684 err = got_error(GOT_ERR_PRIVSEP_LEN);
1685 break;
1687 iblob = imsg.data;
1688 *size = iblob->size;
1689 *hdrlen = iblob->hdrlen;
1691 if (datalen == sizeof(*iblob)) {
1692 /* Data has been written to file descriptor. */
1693 break;
1696 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1697 *size > datalen + sizeof(*iblob)) {
1698 err = got_error(GOT_ERR_PRIVSEP_LEN);
1699 break;
1702 *outbuf = malloc(*size);
1703 if (*outbuf == NULL) {
1704 err = got_error_from_errno("malloc");
1705 break;
1707 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1708 break;
1709 default:
1710 err = got_error(GOT_ERR_PRIVSEP_MSG);
1711 break;
1714 imsg_free(&imsg);
1716 return err;
1719 static const struct got_error *
1720 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1722 const struct got_error *err = NULL;
1723 size_t offset, remain;
1725 offset = 0;
1726 remain = tagmsg_len;
1727 while (remain > 0) {
1728 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1730 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1731 tag->tagmsg + offset, n) == -1) {
1732 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1733 break;
1736 err = flush_imsg(ibuf);
1737 if (err)
1738 break;
1740 offset += n;
1741 remain -= n;
1744 return err;
1747 const struct got_error *
1748 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1750 const struct got_error *err = NULL;
1751 struct got_imsg_tag_object *itag;
1752 uint8_t *buf;
1753 size_t len, total;
1754 size_t tag_len = strlen(tag->tag);
1755 size_t tagger_len = strlen(tag->tagger);
1756 size_t tagmsg_len = strlen(tag->tagmsg);
1758 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1760 buf = malloc(total);
1761 if (buf == NULL)
1762 return got_error_from_errno("malloc");
1764 itag = (struct got_imsg_tag_object *)buf;
1765 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1766 itag->obj_type = tag->obj_type;
1767 itag->tag_len = tag_len;
1768 itag->tagger_len = tagger_len;
1769 itag->tagger_time = tag->tagger_time;
1770 itag->tagger_gmtoff = tag->tagger_gmtoff;
1771 itag->tagmsg_len = tagmsg_len;
1773 len = sizeof(*itag);
1774 memcpy(buf + len, tag->tag, tag_len);
1775 len += tag_len;
1776 memcpy(buf + len, tag->tagger, tagger_len);
1777 len += tagger_len;
1779 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1780 err = got_error_from_errno("imsg_compose TAG");
1781 goto done;
1784 if (tagmsg_len == 0 ||
1785 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1786 err = flush_imsg(ibuf);
1787 if (err)
1788 goto done;
1790 err = send_tagmsg(ibuf, tag, tagmsg_len);
1791 done:
1792 free(buf);
1793 return err;
1796 const struct got_error *
1797 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1799 const struct got_error *err = NULL;
1800 struct imsg imsg;
1801 struct got_imsg_tag_object *itag;
1802 size_t len, datalen;
1803 const size_t min_datalen =
1804 MIN(sizeof(struct got_imsg_error),
1805 sizeof(struct got_imsg_tag_object));
1807 *tag = NULL;
1809 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1810 if (err)
1811 return err;
1813 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1814 len = 0;
1816 switch (imsg.hdr.type) {
1817 case GOT_IMSG_TAG:
1818 if (datalen < sizeof(*itag)) {
1819 err = got_error(GOT_ERR_PRIVSEP_LEN);
1820 break;
1822 itag = imsg.data;
1823 if (datalen != sizeof(*itag) + itag->tag_len +
1824 itag->tagger_len) {
1825 err = got_error(GOT_ERR_PRIVSEP_LEN);
1826 break;
1828 len += sizeof(*itag);
1830 *tag = calloc(1, sizeof(**tag));
1831 if (*tag == NULL) {
1832 err = got_error_from_errno("calloc");
1833 break;
1836 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1838 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1839 if ((*tag)->tag == NULL) {
1840 err = got_error_from_errno("strndup");
1841 break;
1843 len += itag->tag_len;
1845 (*tag)->obj_type = itag->obj_type;
1846 (*tag)->tagger_time = itag->tagger_time;
1847 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1849 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1850 if ((*tag)->tagger == NULL) {
1851 err = got_error_from_errno("strndup");
1852 break;
1854 len += itag->tagger_len;
1856 if (itag->tagmsg_len == 0) {
1857 (*tag)->tagmsg = strdup("");
1858 if ((*tag)->tagmsg == NULL) {
1859 err = got_error_from_errno("strdup");
1860 break;
1862 } else {
1863 size_t offset = 0, remain = itag->tagmsg_len;
1865 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1866 if ((*tag)->tagmsg == NULL) {
1867 err = got_error_from_errno("malloc");
1868 break;
1870 while (remain > 0) {
1871 struct imsg imsg_log;
1872 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1873 remain);
1875 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1876 if (err)
1877 return err;
1879 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1880 return got_error(GOT_ERR_PRIVSEP_MSG);
1882 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1884 imsg_free(&imsg_log);
1885 offset += n;
1886 remain -= n;
1888 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1891 break;
1892 default:
1893 err = got_error(GOT_ERR_PRIVSEP_MSG);
1894 break;
1897 imsg_free(&imsg);
1899 return err;
1902 const struct got_error *
1903 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1904 struct got_packidx *packidx)
1906 const struct got_error *err = NULL;
1907 struct got_imsg_packidx ipackidx;
1908 struct got_imsg_pack ipack;
1909 int fd;
1911 memset(&ipackidx, 0, sizeof(ipackidx));
1912 memset(&ipack, 0, sizeof(ipack));
1914 ipackidx.len = packidx->len;
1915 ipackidx.packfile_size = pack->filesize;
1916 fd = dup(packidx->fd);
1917 if (fd == -1)
1918 return got_error_from_errno("dup");
1920 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1921 sizeof(ipackidx)) == -1) {
1922 err = got_error_from_errno("imsg_compose PACKIDX");
1923 close(fd);
1924 return err;
1927 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1928 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1929 return got_error(GOT_ERR_NO_SPACE);
1930 ipack.filesize = pack->filesize;
1932 fd = dup(pack->fd);
1933 if (fd == -1)
1934 return got_error_from_errno("dup");
1936 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1937 == -1) {
1938 err = got_error_from_errno("imsg_compose PACK");
1939 close(fd);
1940 return err;
1943 return flush_imsg(ibuf);
1946 const struct got_error *
1947 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1948 struct got_object_id *id)
1950 struct got_imsg_packed_object iobj;
1952 memset(&iobj, 0, sizeof(iobj));
1953 iobj.idx = idx;
1954 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1956 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1957 &iobj, sizeof(iobj)) == -1)
1958 return got_error_from_errno("imsg_compose "
1959 "PACKED_OBJECT_REQUEST");
1961 return flush_imsg(ibuf);
1964 const struct got_error *
1965 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1966 struct got_object_id *id)
1968 struct got_imsg_packed_object iobj;
1970 memset(&iobj, 0, sizeof(iobj));
1971 iobj.idx = idx;
1972 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1974 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
1975 &iobj, sizeof(iobj)) == -1)
1976 return got_error_from_errno("imsg_compose "
1977 "PACKED_OBJECT_REQUEST");
1979 return flush_imsg(ibuf);
1982 const struct got_error *
1983 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1985 const struct got_error *err = NULL;
1987 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1988 NULL, 0) == -1) {
1989 err = got_error_from_errno("imsg_compose "
1990 "GITCONFIG_PARSE_REQUEST");
1991 close(fd);
1992 return err;
1995 return flush_imsg(ibuf);
1998 const struct got_error *
1999 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2001 if (imsg_compose(ibuf,
2002 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2003 NULL, 0) == -1)
2004 return got_error_from_errno("imsg_compose "
2005 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2007 return flush_imsg(ibuf);
2010 const struct got_error *
2011 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2013 if (imsg_compose(ibuf,
2014 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2015 NULL, 0) == -1)
2016 return got_error_from_errno("imsg_compose "
2017 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2019 return flush_imsg(ibuf);
2023 const struct got_error *
2024 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2026 if (imsg_compose(ibuf,
2027 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2028 return got_error_from_errno("imsg_compose "
2029 "GITCONFIG_AUTHOR_NAME_REQUEST");
2031 return flush_imsg(ibuf);
2034 const struct got_error *
2035 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2037 if (imsg_compose(ibuf,
2038 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2039 return got_error_from_errno("imsg_compose "
2040 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2042 return flush_imsg(ibuf);
2045 const struct got_error *
2046 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2048 if (imsg_compose(ibuf,
2049 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2050 return got_error_from_errno("imsg_compose "
2051 "GITCONFIG_REMOTE_REQUEST");
2053 return flush_imsg(ibuf);
2056 const struct got_error *
2057 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2059 if (imsg_compose(ibuf,
2060 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2061 return got_error_from_errno("imsg_compose "
2062 "GITCONFIG_OWNER_REQUEST");
2064 return flush_imsg(ibuf);
2067 const struct got_error *
2068 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2070 const struct got_error *err = NULL;
2071 struct imsg imsg;
2072 size_t datalen;
2074 *str = NULL;
2076 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2077 if (err)
2078 return err;
2079 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2081 switch (imsg.hdr.type) {
2082 case GOT_IMSG_GITCONFIG_STR_VAL:
2083 if (datalen == 0)
2084 break;
2085 /* datalen does not include terminating \0 */
2086 *str = malloc(datalen + 1);
2087 if (*str == NULL) {
2088 err = got_error_from_errno("malloc");
2089 break;
2091 memcpy(*str, imsg.data, datalen);
2092 (*str)[datalen] = '\0';
2093 break;
2094 default:
2095 err = got_error(GOT_ERR_PRIVSEP_MSG);
2096 break;
2099 imsg_free(&imsg);
2100 return err;
2103 const struct got_error *
2104 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2106 const struct got_error *err = NULL;
2107 struct imsg imsg;
2108 size_t datalen;
2109 const size_t min_datalen =
2110 MIN(sizeof(struct got_imsg_error), sizeof(int));
2112 *val = 0;
2114 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2115 if (err)
2116 return err;
2117 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2119 switch (imsg.hdr.type) {
2120 case GOT_IMSG_GITCONFIG_INT_VAL:
2121 if (datalen != sizeof(*val)) {
2122 err = got_error(GOT_ERR_PRIVSEP_LEN);
2123 break;
2125 memcpy(val, imsg.data, sizeof(*val));
2126 break;
2127 default:
2128 err = got_error(GOT_ERR_PRIVSEP_MSG);
2129 break;
2132 imsg_free(&imsg);
2133 return err;
2136 static void
2137 free_remote_data(struct got_remote_repo *remote)
2139 int i;
2141 free(remote->name);
2142 free(remote->fetch_url);
2143 free(remote->send_url);
2144 for (i = 0; i < remote->nfetch_branches; i++)
2145 free(remote->fetch_branches[i]);
2146 free(remote->fetch_branches);
2147 for (i = 0; i < remote->nsend_branches; i++)
2148 free(remote->send_branches[i]);
2149 free(remote->send_branches);
2150 for (i = 0; i < remote->nfetch_refs; i++)
2151 free(remote->fetch_refs[i]);
2152 free(remote->fetch_refs);
2155 const struct got_error *
2156 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2157 int *nremotes, struct imsgbuf *ibuf)
2159 const struct got_error *err = NULL;
2160 struct imsg imsg;
2161 size_t datalen;
2162 struct got_imsg_remotes iremotes;
2163 struct got_imsg_remote iremote;
2165 *remotes = NULL;
2166 *nremotes = 0;
2167 iremotes.nremotes = 0;
2169 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2170 if (err)
2171 return err;
2172 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2174 switch (imsg.hdr.type) {
2175 case GOT_IMSG_GITCONFIG_REMOTES:
2176 if (datalen != sizeof(iremotes)) {
2177 err = got_error(GOT_ERR_PRIVSEP_LEN);
2178 break;
2180 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2181 if (iremotes.nremotes == 0) {
2182 imsg_free(&imsg);
2183 return NULL;
2185 break;
2186 default:
2187 imsg_free(&imsg);
2188 return got_error(GOT_ERR_PRIVSEP_MSG);
2191 imsg_free(&imsg);
2193 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2194 if (*remotes == NULL)
2195 return got_error_from_errno("recallocarray");
2197 while (*nremotes < iremotes.nremotes) {
2198 struct got_remote_repo *remote;
2200 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2201 if (err)
2202 break;
2203 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2205 switch (imsg.hdr.type) {
2206 case GOT_IMSG_GITCONFIG_REMOTE:
2207 remote = &(*remotes)[*nremotes];
2208 memset(remote, 0, sizeof(*remote));
2209 if (datalen < sizeof(iremote)) {
2210 err = got_error(GOT_ERR_PRIVSEP_LEN);
2211 break;
2213 memcpy(&iremote, imsg.data, sizeof(iremote));
2214 if (iremote.name_len == 0 ||
2215 iremote.fetch_url_len == 0 ||
2216 iremote.send_url_len == 0 ||
2217 (sizeof(iremote) + iremote.name_len +
2218 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2219 err = got_error(GOT_ERR_PRIVSEP_LEN);
2220 break;
2222 remote->name = strndup(imsg.data + sizeof(iremote),
2223 iremote.name_len);
2224 if (remote->name == NULL) {
2225 err = got_error_from_errno("strndup");
2226 break;
2228 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2229 iremote.name_len, iremote.fetch_url_len);
2230 if (remote->fetch_url == NULL) {
2231 err = got_error_from_errno("strndup");
2232 free_remote_data(remote);
2233 break;
2235 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2236 iremote.name_len + iremote.fetch_url_len,
2237 iremote.send_url_len);
2238 if (remote->send_url == NULL) {
2239 err = got_error_from_errno("strndup");
2240 free_remote_data(remote);
2241 break;
2243 remote->mirror_references = iremote.mirror_references;
2244 remote->fetch_all_branches = iremote.fetch_all_branches;
2245 remote->nfetch_branches = 0;
2246 remote->fetch_branches = NULL;
2247 remote->nsend_branches = 0;
2248 remote->send_branches = NULL;
2249 remote->nfetch_refs = 0;
2250 remote->fetch_refs = NULL;
2251 (*nremotes)++;
2252 break;
2253 default:
2254 err = got_error(GOT_ERR_PRIVSEP_MSG);
2255 break;
2258 imsg_free(&imsg);
2259 if (err)
2260 break;
2263 if (err) {
2264 int i;
2265 for (i = 0; i < *nremotes; i++)
2266 free_remote_data(&(*remotes)[i]);
2267 free(*remotes);
2268 *remotes = NULL;
2269 *nremotes = 0;
2271 return err;
2274 const struct got_error *
2275 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2277 const struct got_error *err = NULL;
2279 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2280 NULL, 0) == -1) {
2281 err = got_error_from_errno("imsg_compose "
2282 "GOTCONFIG_PARSE_REQUEST");
2283 close(fd);
2284 return err;
2287 return flush_imsg(ibuf);
2290 const struct got_error *
2291 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2293 if (imsg_compose(ibuf,
2294 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2295 return got_error_from_errno("imsg_compose "
2296 "GOTCONFIG_AUTHOR_REQUEST");
2298 return flush_imsg(ibuf);
2301 const struct got_error *
2302 got_privsep_send_gotconfig_allowed_signers_req(struct imsgbuf *ibuf)
2304 if (imsg_compose(ibuf,
2305 GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2306 return got_error_from_errno("imsg_compose "
2307 "GOTCONFIG_ALLOWEDSIGNERS_REQUEST");
2309 return flush_imsg(ibuf);
2312 const struct got_error *
2313 got_privsep_send_gotconfig_revoked_signers_req(struct imsgbuf *ibuf)
2315 if (imsg_compose(ibuf,
2316 GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2317 return got_error_from_errno("imsg_compose "
2318 "GOTCONFIG_REVOKEDSIGNERS_REQUEST");
2320 return flush_imsg(ibuf);
2323 const struct got_error *
2324 got_privsep_send_gotconfig_signer_id_req(struct imsgbuf *ibuf)
2326 if (imsg_compose(ibuf,
2327 GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST, 0, 0, -1, NULL, 0) == -1)
2328 return got_error_from_errno("imsg_compose "
2329 "GOTCONFIG_SIGNERID_REQUEST");
2331 return flush_imsg(ibuf);
2334 const struct got_error *
2335 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2337 if (imsg_compose(ibuf,
2338 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2339 return got_error_from_errno("imsg_compose "
2340 "GOTCONFIG_REMOTE_REQUEST");
2342 return flush_imsg(ibuf);
2345 const struct got_error *
2346 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2348 const struct got_error *err = NULL;
2349 struct imsg imsg;
2350 size_t datalen;
2352 *str = NULL;
2354 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2355 if (err)
2356 return err;
2357 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2359 switch (imsg.hdr.type) {
2360 case GOT_IMSG_GOTCONFIG_STR_VAL:
2361 if (datalen == 0)
2362 break;
2363 /* datalen does not include terminating \0 */
2364 *str = malloc(datalen + 1);
2365 if (*str == NULL) {
2366 err = got_error_from_errno("malloc");
2367 break;
2369 memcpy(*str, imsg.data, datalen);
2370 (*str)[datalen] = '\0';
2371 break;
2372 default:
2373 err = got_error(GOT_ERR_PRIVSEP_MSG);
2374 break;
2377 imsg_free(&imsg);
2378 return err;
2381 const struct got_error *
2382 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2383 int *nremotes, struct imsgbuf *ibuf)
2385 const struct got_error *err = NULL;
2386 struct imsg imsg;
2387 size_t datalen;
2388 struct got_imsg_remotes iremotes;
2389 struct got_imsg_remote iremote;
2390 const size_t min_datalen =
2391 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2393 *remotes = NULL;
2394 *nremotes = 0;
2395 iremotes.nremotes = 0;
2397 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2398 if (err)
2399 return err;
2400 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2402 switch (imsg.hdr.type) {
2403 case GOT_IMSG_GOTCONFIG_REMOTES:
2404 if (datalen != sizeof(iremotes)) {
2405 err = got_error(GOT_ERR_PRIVSEP_LEN);
2406 break;
2408 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2409 if (iremotes.nremotes < 0) {
2410 err = got_error(GOT_ERR_PRIVSEP_LEN);
2411 break;
2413 if (iremotes.nremotes == 0) {
2414 imsg_free(&imsg);
2415 return NULL;
2417 break;
2418 default:
2419 imsg_free(&imsg);
2420 return got_error(GOT_ERR_PRIVSEP_MSG);
2423 imsg_free(&imsg);
2425 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2426 if (*remotes == NULL)
2427 return got_error_from_errno("recallocarray");
2429 while (*nremotes < iremotes.nremotes) {
2430 struct got_remote_repo *remote;
2431 const size_t min_datalen =
2432 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2433 int i;
2435 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2436 if (err)
2437 break;
2438 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2440 switch (imsg.hdr.type) {
2441 case GOT_IMSG_GOTCONFIG_REMOTE:
2442 remote = &(*remotes)[*nremotes];
2443 memset(remote, 0, sizeof(*remote));
2444 if (datalen < sizeof(iremote)) {
2445 err = got_error(GOT_ERR_PRIVSEP_LEN);
2446 break;
2448 memcpy(&iremote, imsg.data, sizeof(iremote));
2449 if (iremote.name_len == 0 ||
2450 (iremote.fetch_url_len == 0 &&
2451 iremote.send_url_len == 0) ||
2452 (sizeof(iremote) + iremote.name_len +
2453 iremote.fetch_url_len + iremote.send_url_len) >
2454 datalen) {
2455 err = got_error(GOT_ERR_PRIVSEP_LEN);
2456 break;
2458 remote->name = strndup(imsg.data + sizeof(iremote),
2459 iremote.name_len);
2460 if (remote->name == NULL) {
2461 err = got_error_from_errno("strndup");
2462 break;
2464 remote->fetch_url = strndup(imsg.data +
2465 sizeof(iremote) + iremote.name_len,
2466 iremote.fetch_url_len);
2467 if (remote->fetch_url == NULL) {
2468 err = got_error_from_errno("strndup");
2469 free_remote_data(remote);
2470 break;
2472 remote->send_url = strndup(imsg.data +
2473 sizeof(iremote) + iremote.name_len +
2474 iremote.fetch_url_len, iremote.send_url_len);
2475 if (remote->send_url == NULL) {
2476 err = got_error_from_errno("strndup");
2477 free_remote_data(remote);
2478 break;
2480 remote->mirror_references = iremote.mirror_references;
2481 remote->fetch_all_branches = iremote.fetch_all_branches;
2482 if (iremote.nfetch_branches > 0) {
2483 remote->fetch_branches = recallocarray(NULL, 0,
2484 iremote.nfetch_branches, sizeof(char *));
2485 if (remote->fetch_branches == NULL) {
2486 err = got_error_from_errno("calloc");
2487 free_remote_data(remote);
2488 break;
2491 remote->nfetch_branches = 0;
2492 for (i = 0; i < iremote.nfetch_branches; i++) {
2493 char *branch;
2494 err = got_privsep_recv_gotconfig_str(&branch,
2495 ibuf);
2496 if (err) {
2497 free_remote_data(remote);
2498 goto done;
2500 remote->fetch_branches[i] = branch;
2501 remote->nfetch_branches++;
2503 if (iremote.nsend_branches > 0) {
2504 remote->send_branches = recallocarray(NULL, 0,
2505 iremote.nsend_branches, sizeof(char *));
2506 if (remote->send_branches == NULL) {
2507 err = got_error_from_errno("calloc");
2508 free_remote_data(remote);
2509 break;
2512 remote->nsend_branches = 0;
2513 for (i = 0; i < iremote.nsend_branches; i++) {
2514 char *branch;
2515 err = got_privsep_recv_gotconfig_str(&branch,
2516 ibuf);
2517 if (err) {
2518 free_remote_data(remote);
2519 goto done;
2521 remote->send_branches[i] = branch;
2522 remote->nsend_branches++;
2524 if (iremote.nfetch_refs > 0) {
2525 remote->fetch_refs = recallocarray(NULL, 0,
2526 iremote.nfetch_refs, sizeof(char *));
2527 if (remote->fetch_refs == NULL) {
2528 err = got_error_from_errno("calloc");
2529 free_remote_data(remote);
2530 break;
2533 remote->nfetch_refs = 0;
2534 for (i = 0; i < iremote.nfetch_refs; i++) {
2535 char *ref;
2536 err = got_privsep_recv_gotconfig_str(&ref,
2537 ibuf);
2538 if (err) {
2539 free_remote_data(remote);
2540 goto done;
2542 remote->fetch_refs[i] = ref;
2543 remote->nfetch_refs++;
2545 (*nremotes)++;
2546 break;
2547 default:
2548 err = got_error(GOT_ERR_PRIVSEP_MSG);
2549 break;
2552 imsg_free(&imsg);
2553 if (err)
2554 break;
2556 done:
2557 if (err) {
2558 int i;
2559 for (i = 0; i < *nremotes; i++)
2560 free_remote_data(&(*remotes)[i]);
2561 free(*remotes);
2562 *remotes = NULL;
2563 *nremotes = 0;
2565 return err;
2568 const struct got_error *
2569 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2570 struct got_object_id *id, int idx, const char *path)
2572 struct ibuf *wbuf;
2573 size_t path_len = strlen(path) + 1;
2575 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2576 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2577 if (wbuf == NULL)
2578 return got_error_from_errno(
2579 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2580 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2581 return got_error_from_errno("imsg_add "
2582 "COMMIT_TRAVERSAL_REQUEST");
2583 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2584 return got_error_from_errno("imsg_add "
2585 "COMMIT_TRAVERSAL_REQUEST");
2586 if (imsg_add(wbuf, path, path_len) == -1)
2587 return got_error_from_errno("imsg_add "
2588 "COMMIT_TRAVERSAL_REQUEST");
2590 wbuf->fd = -1;
2591 imsg_close(ibuf, wbuf);
2593 return flush_imsg(ibuf);
2596 const struct got_error *
2597 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2598 struct got_object_id **changed_commit_id,
2599 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2601 const struct got_error *err = NULL;
2602 struct imsg imsg;
2603 struct got_imsg_traversed_commits *icommits;
2604 size_t datalen;
2605 int i, done = 0;
2607 *changed_commit = NULL;
2608 *changed_commit_id = NULL;
2610 while (!done) {
2611 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2612 if (err)
2613 return err;
2615 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2616 switch (imsg.hdr.type) {
2617 case GOT_IMSG_TRAVERSED_COMMITS:
2618 icommits = imsg.data;
2619 if (datalen != sizeof(*icommits) +
2620 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2621 err = got_error(GOT_ERR_PRIVSEP_LEN);
2622 break;
2624 for (i = 0; i < icommits->ncommits; i++) {
2625 struct got_object_qid *qid;
2626 uint8_t *sha1 = (uint8_t *)imsg.data +
2627 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2628 err = got_object_qid_alloc_partial(&qid);
2629 if (err)
2630 break;
2631 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2632 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2634 /* The last commit may contain a change. */
2635 if (i == icommits->ncommits - 1) {
2636 *changed_commit_id =
2637 got_object_id_dup(&qid->id);
2638 if (*changed_commit_id == NULL) {
2639 err = got_error_from_errno(
2640 "got_object_id_dup");
2641 break;
2645 break;
2646 case GOT_IMSG_COMMIT:
2647 if (*changed_commit_id == NULL) {
2648 err = got_error(GOT_ERR_PRIVSEP_MSG);
2649 break;
2651 err = get_commit_from_imsg(changed_commit, &imsg,
2652 datalen, ibuf);
2653 break;
2654 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2655 done = 1;
2656 break;
2657 default:
2658 err = got_error(GOT_ERR_PRIVSEP_MSG);
2659 break;
2662 imsg_free(&imsg);
2663 if (err)
2664 break;
2667 if (err)
2668 got_object_id_queue_free(commit_ids);
2669 return err;
2672 const struct got_error *
2673 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2674 struct got_object_id *tree_id, const char *path,
2675 struct got_parsed_tree_entry *entries, int nentries)
2677 const struct got_error *err = NULL;
2678 struct ibuf *wbuf;
2679 size_t path_len = strlen(path);
2680 size_t msglen;
2682 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2683 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2684 if (wbuf == NULL)
2685 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2687 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2688 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2689 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2690 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2691 if (imsg_add(wbuf, path, path_len) == -1)
2692 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2694 wbuf->fd = -1;
2695 imsg_close(ibuf, wbuf);
2697 if (entries) {
2698 err = send_tree_entries(ibuf, entries, nentries);
2699 if (err)
2700 return err;
2703 return flush_imsg(ibuf);
2706 const struct got_error *
2707 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2709 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2710 0, 0, -1, NULL, 0) == -1)
2711 return got_error_from_errno("imsg_compose "
2712 "OBJECT_ENUMERATION_REQUEST");
2714 return flush_imsg(ibuf);
2717 const struct got_error *
2718 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2720 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2721 0, 0, -1, NULL, 0) == -1)
2722 return got_error_from_errno("imsg_compose "
2723 "OBJECT_ENUMERATION_DONE");
2725 return flush_imsg(ibuf);
2728 const struct got_error *
2729 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2731 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2732 0, 0, -1, NULL, 0) == -1)
2733 return got_error_from_errno("imsg_compose "
2734 "OBJECT_ENUMERATION_INCOMPLETE");
2736 return flush_imsg(ibuf);
2739 const struct got_error *
2740 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2741 struct got_object_id *id, time_t mtime)
2743 struct ibuf *wbuf;
2745 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2746 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2747 if (wbuf == NULL)
2748 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2750 /* Keep in sync with struct got_imsg_enumerated_commit! */
2751 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2752 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2753 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2754 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2756 wbuf->fd = -1;
2757 imsg_close(ibuf, wbuf);
2758 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2759 return NULL;
2762 const struct got_error *
2763 got_privsep_recv_enumerated_objects(int *found_all_objects,
2764 struct imsgbuf *ibuf,
2765 got_object_enumerate_commit_cb cb_commit,
2766 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2767 struct got_repository *repo)
2769 const struct got_error *err = NULL;
2770 struct imsg imsg;
2771 struct got_imsg_enumerated_commit *icommit = NULL;
2772 struct got_object_id commit_id;
2773 int have_commit = 0;
2774 time_t mtime = 0;
2775 struct got_tree_object tree;
2776 struct got_imsg_enumerated_tree *itree;
2777 struct got_object_id tree_id;
2778 char *path = NULL, *canon_path = NULL;
2779 size_t datalen, path_len;
2780 int nentries = -1;
2781 int done = 0;
2783 *found_all_objects = 0;
2784 memset(&tree, 0, sizeof(tree));
2786 while (!done) {
2787 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2788 if (err)
2789 break;
2791 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2792 switch (imsg.hdr.type) {
2793 case GOT_IMSG_ENUMERATED_COMMIT:
2794 if (have_commit && nentries != -1) {
2795 err = got_error(GOT_ERR_PRIVSEP_MSG);
2796 break;
2798 if (datalen != sizeof(*icommit)) {
2799 err = got_error(GOT_ERR_PRIVSEP_LEN);
2800 break;
2802 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2803 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2804 mtime = icommit->mtime;
2805 have_commit = 1;
2806 break;
2807 case GOT_IMSG_ENUMERATED_TREE:
2808 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2809 if (!have_commit) {
2810 err = got_error(GOT_ERR_PRIVSEP_MSG);
2811 break;
2813 if (datalen < sizeof(*itree)) {
2814 err = got_error(GOT_ERR_PRIVSEP_LEN);
2815 break;
2817 itree = imsg.data;
2818 path_len = datalen - sizeof(*itree);
2819 if (path_len == 0) {
2820 err = got_error(GOT_ERR_PRIVSEP_LEN);
2821 break;
2823 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2824 free(path);
2825 path = malloc(path_len + 1);
2826 if (path == NULL) {
2827 err = got_error_from_errno("malloc");
2828 break;
2830 free(canon_path);
2831 canon_path = malloc(path_len + 1);
2832 if (canon_path == NULL) {
2833 err = got_error_from_errno("malloc");
2834 break;
2836 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2837 path_len);
2838 path[path_len] = '\0';
2839 if (!got_path_is_absolute(path)) {
2840 err = got_error(GOT_ERR_BAD_PATH);
2841 break;
2843 if (got_path_is_root_dir(path)) {
2844 /* XXX check what got_canonpath() does wrong */
2845 canon_path[0] = '/';
2846 canon_path[1] = '\0';
2847 } else {
2848 err = got_canonpath(path, canon_path,
2849 path_len + 1);
2850 if (err)
2851 break;
2853 if (strcmp(path, canon_path) != 0) {
2854 err = got_error(GOT_ERR_BAD_PATH);
2855 break;
2857 if (nentries != -1) {
2858 err = got_error(GOT_ERR_PRIVSEP_MSG);
2859 break;
2861 if (itree->nentries < -1) {
2862 err = got_error(GOT_ERR_PRIVSEP_MSG);
2863 break;
2865 if (itree->nentries == -1) {
2866 /* Tree was not found in pack file. */
2867 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2868 path, repo);
2869 break;
2871 if (itree->nentries > INT_MAX) {
2872 err = got_error(GOT_ERR_PRIVSEP_LEN);
2873 break;
2875 tree.entries = calloc(itree->nentries,
2876 sizeof(struct got_tree_entry));
2877 if (tree.entries == NULL) {
2878 err = got_error_from_errno("calloc");
2879 break;
2881 if (itree->nentries == 0) {
2882 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2883 path, repo);
2884 if (err)
2885 break;
2887 /* Prepare for next tree. */
2888 free(tree.entries);
2889 memset(&tree, 0, sizeof(tree));
2890 nentries = -1;
2891 } else {
2892 tree.nentries = itree->nentries;
2893 nentries = 0;
2895 break;
2896 case GOT_IMSG_TREE_ENTRIES:
2897 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2898 if (nentries <= -1) {
2899 err = got_error(GOT_ERR_PRIVSEP_MSG);
2900 break;
2902 err = recv_tree_entries(imsg.data, datalen,
2903 &tree, &nentries);
2904 if (err)
2905 break;
2906 if (tree.nentries == nentries) {
2907 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2908 path, repo);
2909 if (err)
2910 break;
2912 /* Prepare for next tree. */
2913 free(tree.entries);
2914 memset(&tree, 0, sizeof(tree));
2915 nentries = -1;
2917 break;
2918 case GOT_IMSG_TREE_ENUMERATION_DONE:
2919 /* All trees have been found and traversed. */
2920 if (!have_commit || path == NULL || nentries != -1) {
2921 err = got_error(GOT_ERR_PRIVSEP_MSG);
2922 break;
2924 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2925 if (err)
2926 break;
2927 have_commit = 0;
2928 break;
2929 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2930 *found_all_objects = 1;
2931 done = 1;
2932 break;
2933 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
2934 done = 1;
2935 break;
2936 default:
2937 err = got_error(GOT_ERR_PRIVSEP_MSG);
2938 break;
2941 imsg_free(&imsg);
2942 if (err)
2943 break;
2946 free(path);
2947 free(canon_path);
2948 free(tree.entries);
2949 return err;
2952 const struct got_error *
2953 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2954 struct got_object_id *id)
2956 struct got_imsg_raw_delta_request dreq;
2958 memset(&dreq, 0, sizeof(dreq));
2959 dreq.idx = idx;
2960 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2962 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2963 &dreq, sizeof(dreq)) == -1)
2964 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2966 return flush_imsg(ibuf);
2969 const struct got_error *
2970 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2972 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2975 const struct got_error *
2976 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2977 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2978 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2980 struct got_imsg_raw_delta idelta;
2981 int ret;
2983 memset(&idelta, 0, sizeof(idelta));
2984 idelta.base_size = base_size;
2985 idelta.result_size = result_size;
2986 idelta.delta_size = delta_size;
2987 idelta.delta_compressed_size = delta_compressed_size;
2988 idelta.delta_offset = delta_offset;
2989 idelta.delta_out_offset = delta_out_offset;
2990 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2992 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2993 &idelta, sizeof(idelta));
2994 if (ret == -1)
2995 return got_error_from_errno("imsg_compose RAW_DELTA");
2997 return flush_imsg(ibuf);
3000 const struct got_error *
3001 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3002 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3003 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3005 const struct got_error *err = NULL;
3006 struct imsg imsg;
3007 struct got_imsg_raw_delta *delta;
3008 size_t datalen;
3010 *base_size = 0;
3011 *result_size = 0;
3012 *delta_size = 0;
3013 *delta_compressed_size = 0;
3014 *delta_offset = 0;
3015 *delta_out_offset = 0;
3016 *base_id = NULL;
3018 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3019 if (err)
3020 return err;
3022 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3024 switch (imsg.hdr.type) {
3025 case GOT_IMSG_RAW_DELTA:
3026 if (datalen != sizeof(*delta)) {
3027 err = got_error(GOT_ERR_PRIVSEP_LEN);
3028 break;
3030 delta = imsg.data;
3031 *base_size = delta->base_size;
3032 *result_size = delta->result_size;
3033 *delta_size = delta->delta_size;
3034 *delta_compressed_size = delta->delta_compressed_size;
3035 *delta_offset = delta->delta_offset;
3036 *delta_out_offset = delta->delta_out_offset;
3037 *base_id = calloc(1, sizeof(**base_id));
3038 if (*base_id == NULL) {
3039 err = got_error_from_errno("malloc");
3040 break;
3042 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3043 break;
3044 default:
3045 err = got_error(GOT_ERR_PRIVSEP_MSG);
3046 break;
3049 imsg_free(&imsg);
3051 if (err) {
3052 free(*base_id);
3053 *base_id = NULL;
3055 return err;
3058 static const struct got_error *
3059 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3061 const struct got_error *err = NULL;
3062 struct got_imsg_object_idlist idlist;
3063 struct ibuf *wbuf;
3064 size_t i;
3066 memset(&idlist, 0, sizeof(idlist));
3068 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3069 return got_error(GOT_ERR_NO_SPACE);
3071 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3072 sizeof(idlist) + nids * sizeof(**ids));
3073 if (wbuf == NULL) {
3074 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3075 return err;
3078 idlist.nids = nids;
3079 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3080 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3082 for (i = 0; i < nids; i++) {
3083 struct got_object_id *id = ids[i];
3084 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3085 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3088 wbuf->fd = -1;
3089 imsg_close(ibuf, wbuf);
3091 return flush_imsg(ibuf);
3094 const struct got_error *
3095 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3096 struct got_object_id **ids, size_t nids)
3098 const struct got_error *err = NULL;
3099 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3100 int i, queued = 0;
3102 for (i = 0; i < nids; i++) {
3103 idlist[i % nitems(idlist)] = ids[i];
3104 queued++;
3105 if (queued >= nitems(idlist)) {
3106 err = send_idlist(ibuf, idlist, queued);
3107 if (err)
3108 return err;
3109 queued = 0;
3113 if (queued > 0) {
3114 err = send_idlist(ibuf, idlist, queued);
3115 if (err)
3116 return err;
3119 return NULL;
3122 const struct got_error *
3123 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3125 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3126 == -1)
3127 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3129 return flush_imsg(ibuf);
3132 const struct got_error *
3133 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3134 size_t *nids, struct imsgbuf *ibuf)
3136 const struct got_error *err = NULL;
3137 struct imsg imsg;
3138 struct got_imsg_object_idlist *idlist;
3139 size_t datalen;
3141 *ids = NULL;
3142 *done = 0;
3143 *nids = 0;
3145 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3146 if (err)
3147 return err;
3149 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3150 switch (imsg.hdr.type) {
3151 case GOT_IMSG_OBJ_ID_LIST:
3152 if (datalen < sizeof(*idlist)) {
3153 err = got_error(GOT_ERR_PRIVSEP_LEN);
3154 break;
3156 idlist = imsg.data;
3157 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3158 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3159 err = got_error(GOT_ERR_PRIVSEP_LEN);
3160 break;
3162 *nids = idlist->nids;
3163 *ids = calloc(*nids, sizeof(**ids));
3164 if (*ids == NULL) {
3165 err = got_error_from_errno("calloc");
3166 break;
3168 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3169 *nids * sizeof(**ids));
3170 break;
3171 case GOT_IMSG_OBJ_ID_LIST_DONE:
3172 *done = 1;
3173 break;
3174 default:
3175 err = got_error(GOT_ERR_PRIVSEP_MSG);
3176 break;
3179 imsg_free(&imsg);
3181 return err;
3184 const struct got_error *
3185 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3187 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3188 == -1)
3189 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3191 return flush_imsg(ibuf);
3194 const struct got_error *
3195 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3196 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3198 const struct got_error *err = NULL;
3199 struct ibuf *wbuf;
3200 struct got_imsg_reused_deltas ideltas;
3201 size_t i;
3203 memset(&ideltas, 0, sizeof(ideltas));
3205 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3206 return got_error(GOT_ERR_NO_SPACE);
3208 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3209 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3210 if (wbuf == NULL) {
3211 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3212 return err;
3215 ideltas.ndeltas = ndeltas;
3216 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3217 return got_error_from_errno("imsg_add REUSED_DELTAS");
3219 for (i = 0; i < ndeltas; i++) {
3220 struct got_imsg_reused_delta *delta = &deltas[i];
3221 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3222 return got_error_from_errno("imsg_add REUSED_DELTAS");
3225 wbuf->fd = -1;
3226 imsg_close(ibuf, wbuf);
3228 return flush_imsg(ibuf);
3231 const struct got_error *
3232 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3234 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3235 == -1)
3236 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3238 return flush_imsg(ibuf);
3241 const struct got_error *
3242 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3243 size_t *ndeltas, struct imsgbuf *ibuf)
3245 const struct got_error *err = NULL;
3246 struct imsg imsg;
3247 struct got_imsg_reused_deltas *ideltas;
3248 size_t datalen;
3250 *done = 0;
3251 *ndeltas = 0;
3253 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3254 if (err)
3255 return err;
3257 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3258 switch (imsg.hdr.type) {
3259 case GOT_IMSG_REUSED_DELTAS:
3260 if (datalen < sizeof(*ideltas)) {
3261 err = got_error(GOT_ERR_PRIVSEP_LEN);
3262 break;
3264 ideltas = imsg.data;
3265 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3266 ideltas->ndeltas * sizeof(*deltas) >
3267 datalen - sizeof(*ideltas)) {
3268 err = got_error(GOT_ERR_PRIVSEP_LEN);
3269 break;
3271 *ndeltas = ideltas->ndeltas;
3272 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3273 *ndeltas * sizeof(*deltas));
3274 break;
3275 case GOT_IMSG_DELTA_REUSE_DONE:
3276 *done = 1;
3277 break;
3278 default:
3279 err = got_error(GOT_ERR_PRIVSEP_MSG);
3280 break;
3283 imsg_free(&imsg);
3285 return err;
3288 const struct got_error *
3289 got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3291 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_INIT,
3292 0, 0, -1, NULL, 0)
3293 == -1)
3294 return got_error_from_errno("imsg_compose "
3295 "COMMIT_PAINTING_INIT");
3297 return flush_imsg(ibuf);
3300 const struct got_error *
3301 got_privsep_send_painting_request(struct imsgbuf *ibuf, int idx,
3302 struct got_object_id *id, intptr_t color)
3304 struct got_imsg_commit_painting_request ireq;
3306 memset(&ireq, 0, sizeof(ireq));
3307 memcpy(ireq.id, id->sha1, sizeof(ireq.id));
3308 ireq.idx = idx;
3309 ireq.color = color;
3311 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_REQUEST, 0, 0, -1,
3312 &ireq, sizeof(ireq)) == -1)
3313 return got_error_from_errno("imsg_compose "
3314 "COMMIT_PAINTING_REQUEST");
3316 return flush_imsg(ibuf);
3319 static const struct got_error *
3320 send_painted_commits(struct got_object_id_queue *ids, int *nids,
3321 size_t remain, int present_in_pack, struct imsgbuf *ibuf)
3323 const struct got_error *err = NULL;
3324 struct ibuf *wbuf = NULL;
3325 struct got_object_qid *qid;
3326 size_t msglen;
3327 int ncommits;
3328 intptr_t color;
3330 msglen = MIN(remain, MAX_IMSGSIZE - IMSG_HEADER_SIZE);
3331 ncommits = (msglen - sizeof(struct got_imsg_painted_commits)) /
3332 sizeof(struct got_imsg_painted_commit);
3334 wbuf = imsg_create(ibuf, GOT_IMSG_PAINTED_COMMITS, 0, 0, msglen);
3335 if (wbuf == NULL) {
3336 err = got_error_from_errno("imsg_create PAINTED_COMMITS");
3337 return err;
3340 /* Keep in sync with struct got_imsg_painted_commits! */
3341 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
3342 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3343 if (imsg_add(wbuf, &present_in_pack, sizeof(present_in_pack)) == -1)
3344 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3346 while (ncommits > 0) {
3347 qid = STAILQ_FIRST(ids);
3348 STAILQ_REMOVE_HEAD(ids, entry);
3349 ncommits--;
3350 (*nids)--;
3351 color = (intptr_t)qid->data;
3353 /* Keep in sync with struct got_imsg_painted_commit! */
3354 if (imsg_add(wbuf, qid->id.sha1, SHA1_DIGEST_LENGTH) == -1)
3355 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3356 if (imsg_add(wbuf, &color, sizeof(color)) == -1)
3357 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3359 got_object_qid_free(qid);
3362 wbuf->fd = -1;
3363 imsg_close(ibuf, wbuf);
3365 return flush_imsg(ibuf);
3368 const struct got_error *
3369 got_privsep_send_painted_commits(struct imsgbuf *ibuf,
3370 struct got_object_id_queue *ids, int *nids,
3371 int present_in_pack, int flush)
3373 const struct got_error *err;
3374 size_t remain;
3376 if (*nids <= 0)
3377 return NULL;
3379 do {
3380 remain = (sizeof(struct got_imsg_painted_commits)) +
3381 *nids * sizeof(struct got_imsg_painted_commit);
3382 if (flush || remain >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
3383 err = send_painted_commits(ids, nids, remain,
3384 present_in_pack, ibuf);
3385 if (err)
3386 return err;
3388 } while (flush && *nids > 0);
3390 return NULL;
3393 const struct got_error *
3394 got_privsep_send_painting_commits_done(struct imsgbuf *ibuf)
3396 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_DONE,
3397 0, 0, -1, NULL, 0)
3398 == -1)
3399 return got_error_from_errno("imsg_compose "
3400 "COMMIT_PAINTING_DONE");
3402 return flush_imsg(ibuf);
3405 const struct got_error *
3406 got_privsep_recv_painted_commits(struct got_object_id_queue *new_ids,
3407 got_privsep_recv_painted_commit_cb cb, void *cb_arg, struct imsgbuf *ibuf)
3409 const struct got_error *err = NULL;
3410 struct imsg imsg;
3411 struct got_imsg_painted_commits icommits;
3412 struct got_imsg_painted_commit icommit;
3413 size_t datalen;
3414 int i;
3416 for (;;) {
3417 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3418 if (err)
3419 return err;
3421 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3422 if (imsg.hdr.type == GOT_IMSG_COMMIT_PAINTING_DONE)
3423 break;
3424 if (imsg.hdr.type != GOT_IMSG_PAINTED_COMMITS)
3425 return got_error(GOT_ERR_PRIVSEP_MSG);
3427 if (datalen < sizeof(icommits))
3428 return got_error(GOT_ERR_PRIVSEP_LEN);
3429 memcpy(&icommits, imsg.data, sizeof(icommits));
3430 if (icommits.ncommits * sizeof(icommit) < icommits.ncommits ||
3431 datalen < sizeof(icommits) +
3432 icommits.ncommits * sizeof(icommit))
3433 return got_error(GOT_ERR_PRIVSEP_LEN);
3435 for (i = 0; i < icommits.ncommits; i++) {
3436 memcpy(&icommit,
3437 (uint8_t *)imsg.data + sizeof(icommits) + i * sizeof(icommit),
3438 sizeof(icommit));
3440 if (icommits.present_in_pack) {
3441 struct got_object_id id;
3442 memcpy(id.sha1, icommit.id, SHA1_DIGEST_LENGTH);
3443 err = cb(cb_arg, &id, icommit.color);
3444 if (err)
3445 break;
3446 } else {
3447 struct got_object_qid *qid;
3448 err = got_object_qid_alloc_partial(&qid);
3449 if (err)
3450 break;
3451 memcpy(qid->id.sha1, icommit.id,
3452 SHA1_DIGEST_LENGTH);
3453 qid->data = (void *)icommit.color;
3454 STAILQ_INSERT_TAIL(new_ids, qid, entry);
3458 imsg_free(&imsg);
3461 return err;
3464 const struct got_error *
3465 got_privsep_unveil_exec_helpers(void)
3467 const char *helpers[] = {
3468 GOT_PATH_PROG_READ_PACK,
3469 GOT_PATH_PROG_READ_OBJECT,
3470 GOT_PATH_PROG_READ_COMMIT,
3471 GOT_PATH_PROG_READ_TREE,
3472 GOT_PATH_PROG_READ_BLOB,
3473 GOT_PATH_PROG_READ_TAG,
3474 GOT_PATH_PROG_READ_GITCONFIG,
3475 GOT_PATH_PROG_READ_GOTCONFIG,
3476 GOT_PATH_PROG_READ_PATCH,
3477 GOT_PATH_PROG_FETCH_PACK,
3478 GOT_PATH_PROG_INDEX_PACK,
3479 GOT_PATH_PROG_SEND_PACK,
3481 size_t i;
3483 for (i = 0; i < nitems(helpers); i++) {
3484 if (unveil(helpers[i], "x") == 0)
3485 continue;
3486 return got_error_from_errno2("unveil", helpers[i]);
3489 return NULL;
3492 void
3493 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3495 if (close(imsg_fds[0]) == -1) {
3496 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3497 _exit(1);
3500 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3501 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3502 _exit(1);
3505 closefrom(GOT_IMSG_FD_CHILD + 1);
3507 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3508 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3509 strerror(errno));
3510 _exit(1);