2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/syslimits.h>
34 #include "got_object.h"
35 #include "got_error.h"
37 #include "got_lib_sha1.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 static const struct got_error *
50 poll_fd(int fd, int events, int timeout)
56 pfd[0].events = events;
58 n = poll(pfd, 1, timeout);
60 return got_error_from_errno();
62 return got_error(GOT_ERR_TIMEOUT);
63 if (pfd[0].revents & (POLLERR | POLLNVAL))
64 return got_error_from_errno();
65 if (pfd[0].revents & (events | POLLHUP))
68 return got_error(GOT_ERR_INTERRUPT);
71 static const struct got_error *
72 read_imsg(struct imsgbuf *ibuf)
74 const struct got_error *err;
77 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
83 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
84 return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 return got_error(GOT_ERR_PRIVSEP_READ);
88 return got_error(GOT_ERR_PRIVSEP_PIPE);
93 const struct got_error *
94 got_privsep_wait_for_child(pid_t pid)
98 waitpid(pid, &child_status, 0);
100 if (!WIFEXITED(child_status))
101 return got_error(GOT_ERR_PRIVSEP_DIED);
103 if (WEXITSTATUS(child_status) != 0)
104 return got_error(GOT_ERR_PRIVSEP_EXIT);
109 const struct got_error *
110 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
112 const struct got_error *err;
115 n = imsg_get(ibuf, imsg);
117 return got_error_from_errno();
120 err = read_imsg(ibuf);
123 n = imsg_get(ibuf, imsg);
126 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
127 return got_error(GOT_ERR_PRIVSEP_LEN);
132 static const struct got_error *
133 recv_imsg_error(struct imsg *imsg, size_t datalen)
135 struct got_imsg_error *ierr;
137 if (datalen != sizeof(*ierr))
138 return got_error(GOT_ERR_PRIVSEP_LEN);
141 if (ierr->code == GOT_ERR_ERRNO) {
142 static struct got_error serr;
143 serr.code = GOT_ERR_ERRNO;
144 serr.msg = strerror(ierr->errno_code);
148 return got_error(ierr->code);
151 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
153 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
155 const struct got_error *poll_err;
156 struct got_imsg_error ierr;
159 ierr.code = err->code;
160 if (err->code == GOT_ERR_ERRNO)
161 ierr.errno_code = errno;
164 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
166 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
167 getprogname(), err->code, err->msg, strerror(errno));
171 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
173 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
174 getprogname(), err->code, err->msg, poll_err->msg);
178 ret = imsg_flush(ibuf);
180 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
181 getprogname(), err->code, err->msg, strerror(errno));
186 static const struct got_error *
187 flush_imsg(struct imsgbuf *ibuf)
189 const struct got_error *err;
191 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
195 if (imsg_flush(ibuf) == -1)
196 return got_error_from_errno();
201 const struct got_error *
202 got_privsep_send_stop(int fd)
204 const struct got_error *err = NULL;
207 imsg_init(&ibuf, fd);
209 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
210 return got_error_from_errno();
212 err = flush_imsg(&ibuf);
217 const struct got_error *
218 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
220 struct got_imsg_object iobj, *iobjp = NULL;
221 size_t iobj_size = 0;
222 int imsg_code = GOT_IMSG_OBJECT_REQUEST;
226 case GOT_OBJ_TYPE_TREE:
227 imsg_code = GOT_IMSG_TREE_REQUEST;
229 case GOT_OBJ_TYPE_COMMIT:
230 imsg_code = GOT_IMSG_COMMIT_REQUEST;
232 case GOT_OBJ_TYPE_BLOB:
233 imsg_code = GOT_IMSG_BLOB_REQUEST;
236 return got_error(GOT_ERR_OBJ_TYPE);
239 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
240 iobj.type = obj->type;
241 iobj.flags = obj->flags;
242 iobj.hdrlen = obj->hdrlen;
243 iobj.size = obj->size;
244 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
245 iobj.pack_offset = obj->pack_offset;
246 iobj.pack_idx = obj->pack_idx;
250 iobj_size = sizeof(iobj);
253 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
254 return got_error_from_errno();
256 return flush_imsg(ibuf);
259 const struct got_error *
260 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
262 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
264 return got_error_from_errno();
266 return flush_imsg(ibuf);
269 const struct got_error *
270 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
272 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
274 return got_error_from_errno();
276 return flush_imsg(ibuf);
279 const struct got_error *
280 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
282 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
284 return got_error_from_errno();
286 return flush_imsg(ibuf);
289 const struct got_error *
290 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
292 struct got_imsg_object iobj;
294 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
295 iobj.type = obj->type;
296 iobj.flags = obj->flags;
297 iobj.hdrlen = obj->hdrlen;
298 iobj.size = obj->size;
299 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
300 iobj.pack_offset = obj->pack_offset;
301 iobj.pack_idx = obj->pack_idx;
304 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
306 return got_error_from_errno();
308 return flush_imsg(ibuf);
311 const struct got_error *
312 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
313 struct imsgbuf *ibuf)
315 const struct got_error *err = NULL;
316 struct got_imsg_object *iobj;
317 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
319 if (datalen != sizeof(*iobj))
320 return got_error(GOT_ERR_PRIVSEP_LEN);
323 *obj = calloc(1, sizeof(**obj));
325 return got_error_from_errno();
327 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
328 (*obj)->type = iobj->type;
329 (*obj)->flags = iobj->flags;
330 (*obj)->hdrlen = iobj->hdrlen;
331 (*obj)->size = iobj->size;
332 /* path_packfile is handled by caller */
333 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
334 (*obj)->pack_offset = iobj->pack_offset;
335 (*obj)->pack_idx = iobj->pack_idx;
341 const struct got_error *
342 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
344 const struct got_error *err = NULL;
347 const size_t min_datalen =
348 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
352 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
356 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
358 switch (imsg.hdr.type) {
360 err = recv_imsg_error(&imsg, datalen);
362 case GOT_IMSG_OBJECT:
363 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
366 err = got_error(GOT_ERR_PRIVSEP_MSG);
375 static const struct got_error *
376 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
379 const struct got_error *err = NULL;
380 size_t offset, remain;
385 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
387 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
388 commit->logmsg + offset, n) == -1) {
389 err = got_error_from_errno();
393 err = flush_imsg(ibuf);
404 const struct got_error *
405 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
407 const struct got_error *err = NULL;
408 struct got_imsg_commit_object icommit;
411 struct got_object_qid *qid;
412 size_t logmsg_len = strlen(commit->logmsg);
414 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
415 icommit.author_len = strlen(commit->author);
416 icommit.author_time = commit->author_time;
417 icommit.author_gmtoff = commit->author_gmtoff;
418 icommit.committer_len = strlen(commit->committer);
419 icommit.committer_time = commit->committer_time;
420 icommit.committer_gmtoff = commit->committer_gmtoff;
421 icommit.logmsg_len = logmsg_len;
422 icommit.nparents = commit->nparents;
424 total = sizeof(icommit) + icommit.author_len +
425 icommit.committer_len + icommit.nparents * SHA1_DIGEST_LENGTH;
429 return got_error_from_errno();
432 memcpy(buf + len, &icommit, sizeof(icommit));
433 len += sizeof(icommit);
434 memcpy(buf + len, commit->author, icommit.author_len);
435 len += icommit.author_len;
436 memcpy(buf + len, commit->committer, icommit.committer_len);
437 len += icommit.committer_len;
438 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
439 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
440 len += SHA1_DIGEST_LENGTH;
443 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
444 err = got_error_from_errno();
448 if (logmsg_len == 0 ||
449 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
450 err = flush_imsg(ibuf);
454 err = send_commit_logmsg(ibuf, commit, logmsg_len);
460 const struct got_error *
461 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
463 const struct got_error *err = NULL;
465 struct got_imsg_commit_object *icommit;
468 const size_t min_datalen =
469 MIN(sizeof(struct got_imsg_error),
470 sizeof(struct got_imsg_commit_object));
474 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
478 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
481 switch (imsg.hdr.type) {
483 err = recv_imsg_error(&imsg, datalen);
485 case GOT_IMSG_COMMIT:
486 if (datalen < sizeof(*icommit)) {
487 err = got_error(GOT_ERR_PRIVSEP_LEN);
491 if (datalen != sizeof(*icommit) + icommit->author_len +
492 icommit->committer_len +
493 icommit->nparents * SHA1_DIGEST_LENGTH) {
494 err = got_error(GOT_ERR_PRIVSEP_LEN);
497 if (icommit->nparents < 0) {
498 err = got_error(GOT_ERR_PRIVSEP_LEN);
501 len += sizeof(*icommit);
503 *commit = got_object_commit_alloc_partial();
504 if (*commit == NULL) {
505 err = got_error_from_errno();
509 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
511 (*commit)->author_time = icommit->author_time;
512 (*commit)->author_gmtoff = icommit->author_gmtoff;
513 (*commit)->committer_time = icommit->committer_time;
514 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
516 if (icommit->author_len == 0) {
517 (*commit)->author = strdup("");
518 if ((*commit)->author == NULL) {
519 err = got_error_from_errno();
523 (*commit)->author = malloc(icommit->author_len + 1);
524 if ((*commit)->author == NULL) {
525 err = got_error_from_errno();
528 memcpy((*commit)->author, imsg.data + len,
529 icommit->author_len);
530 (*commit)->author[icommit->author_len] = '\0';
532 len += icommit->author_len;
534 if (icommit->committer_len == 0) {
535 (*commit)->committer = strdup("");
536 if ((*commit)->committer == NULL) {
537 err = got_error_from_errno();
541 (*commit)->committer =
542 malloc(icommit->committer_len + 1);
543 if ((*commit)->committer == NULL) {
544 err = got_error_from_errno();
547 memcpy((*commit)->committer, imsg.data + len,
548 icommit->committer_len);
549 (*commit)->committer[icommit->committer_len] = '\0';
551 len += icommit->committer_len;
553 if (icommit->logmsg_len == 0) {
554 (*commit)->logmsg = strdup("");
555 if ((*commit)->logmsg == NULL) {
556 err = got_error_from_errno();
560 size_t offset = 0, remain = icommit->logmsg_len;
562 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
563 if ((*commit)->logmsg == NULL) {
564 err = got_error_from_errno();
568 struct imsg imsg_log;
569 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
572 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
576 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
577 return got_error(GOT_ERR_PRIVSEP_MSG);
579 memcpy((*commit)->logmsg + offset,
581 imsg_free(&imsg_log);
585 (*commit)->logmsg[icommit->logmsg_len] = '\0';
588 for (i = 0; i < icommit->nparents; i++) {
589 struct got_object_qid *qid;
591 err = got_object_qid_alloc_partial(&qid);
594 memcpy(qid->id, imsg.data + len +
595 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
596 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
597 (*commit)->nparents++;
601 err = got_error(GOT_ERR_PRIVSEP_MSG);
610 const struct got_error *
611 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
613 const struct got_error *err = NULL;
614 struct got_imsg_tree_object itree;
615 struct got_tree_entry *te;
617 int nimsg; /* number of imsg queued in ibuf */
619 itree.nentries = tree->entries.nentries;
620 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
622 return got_error_from_errno();
624 totlen = sizeof(itree);
626 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
627 struct got_imsg_tree_entry *ite;
629 size_t len = sizeof(*ite) + strlen(te->name);
631 if (len > MAX_IMSGSIZE)
632 return got_error(GOT_ERR_NO_SPACE);
635 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
636 err = flush_imsg(ibuf);
644 return got_error_from_errno();
646 ite = (struct got_imsg_tree_entry *)buf;
647 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
648 ite->mode = te->mode;
649 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
651 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
653 err = got_error_from_errno();
660 return flush_imsg(ibuf);
663 const struct got_error *
664 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
666 const struct got_error *err = NULL;
667 const size_t min_datalen =
668 MIN(sizeof(struct got_imsg_error),
669 sizeof(struct got_imsg_tree_object));
670 struct got_imsg_tree_object *itree;
675 err = read_imsg(ibuf);
683 struct got_imsg_tree_entry *ite;
684 struct got_tree_entry *te = NULL;
686 n = imsg_get(ibuf, &imsg);
688 if (*tree && (*tree)->entries.nentries != nentries)
693 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
694 return got_error(GOT_ERR_PRIVSEP_LEN);
696 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
698 switch (imsg.hdr.type) {
700 err = recv_imsg_error(&imsg, datalen);
703 /* This message should only appear once. */
705 err = got_error(GOT_ERR_PRIVSEP_MSG);
708 if (datalen != sizeof(*itree)) {
709 err = got_error(GOT_ERR_PRIVSEP_LEN);
713 *tree = malloc(sizeof(**tree));
715 err = got_error_from_errno();
718 (*tree)->entries.nentries = itree->nentries;
719 SIMPLEQ_INIT(&(*tree)->entries.head);
722 case GOT_IMSG_TREE_ENTRY:
723 /* This message should be preceeded by GOT_IMSG_TREE. */
725 err = got_error(GOT_ERR_PRIVSEP_MSG);
728 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
729 err = got_error(GOT_ERR_PRIVSEP_LEN);
733 /* Remaining data contains the entry's name. */
734 datalen -= sizeof(*ite);
735 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
736 err = got_error(GOT_ERR_PRIVSEP_LEN);
741 te = got_alloc_tree_entry_partial();
743 err = got_error_from_errno();
746 te->name = malloc(datalen + 1);
747 if (te->name == NULL) {
749 err = got_error_from_errno();
752 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
753 te->name[datalen] = '\0';
755 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
756 te->mode = ite->mode;
757 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
761 err = got_error(GOT_ERR_PRIVSEP_MSG);
768 if (*tree && (*tree)->entries.nentries != nentries) {
770 err = got_error(GOT_ERR_PRIVSEP_LEN);
771 got_object_tree_close(*tree);
778 const struct got_error *
779 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
781 struct got_imsg_blob iblob;
784 /* Data has already been written to file descriptor. */
786 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
788 return got_error_from_errno();
790 return flush_imsg(ibuf);
793 const struct got_error *
794 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
796 const struct got_error *err = NULL;
798 struct got_imsg_blob *iblob;
801 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
805 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
807 switch (imsg.hdr.type) {
809 err = recv_imsg_error(&imsg, datalen);
812 if (datalen != sizeof(*iblob)) {
813 err = got_error(GOT_ERR_PRIVSEP_LEN);
818 /* Data has been written to file descriptor. */
821 err = got_error(GOT_ERR_PRIVSEP_MSG);
830 const struct got_error *
831 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
832 struct got_packidx *packidx)
834 struct got_imsg_packidx ipackidx;
835 struct got_imsg_pack ipack;
838 ipackidx.len = packidx->len;
839 fd = dup(packidx->fd);
841 return got_error_from_errno();
843 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
844 sizeof(ipackidx)) == -1)
845 return got_error_from_errno();
847 if (strlcpy(ipack.path_packfile, pack->path_packfile,
848 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
849 return got_error(GOT_ERR_NO_SPACE);
850 ipack.filesize = pack->filesize;
854 return got_error_from_errno();
856 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
858 return got_error_from_errno();
860 return flush_imsg(ibuf);
863 const struct got_error *
864 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
865 struct got_object_id *id)
867 struct got_imsg_packed_object iobj;
870 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
872 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
873 &iobj, sizeof(iobj)) == -1)
874 return got_error_from_errno();
876 return flush_imsg(ibuf);