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,
113 const struct got_error *err;
116 n = imsg_get(ibuf, imsg);
118 return got_error_from_errno();
121 err = read_imsg(ibuf);
124 n = imsg_get(ibuf, imsg);
127 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
128 return got_error(GOT_ERR_PRIVSEP_LEN);
133 static const struct got_error *
134 recv_imsg_error(struct imsg *imsg, size_t datalen)
136 struct got_imsg_error *ierr;
138 if (datalen != sizeof(*ierr))
139 return got_error(GOT_ERR_PRIVSEP_LEN);
142 if (ierr->code == GOT_ERR_ERRNO) {
143 static struct got_error serr;
144 serr.code = GOT_ERR_ERRNO;
145 serr.msg = strerror(ierr->errno_code);
149 return got_error(ierr->code);
152 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
154 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
156 const struct got_error *poll_err;
157 struct got_imsg_error ierr;
160 ierr.code = err->code;
161 if (err->code == GOT_ERR_ERRNO)
162 ierr.errno_code = errno;
165 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
167 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
168 getprogname(), err->code, err->msg, strerror(errno));
172 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
174 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
175 getprogname(), err->code, err->msg, poll_err->msg);
179 ret = imsg_flush(ibuf);
181 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
182 getprogname(), err->code, err->msg, strerror(errno));
187 static const struct got_error *
188 flush_imsg(struct imsgbuf *ibuf)
190 const struct got_error *err;
192 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
196 if (imsg_flush(ibuf) == -1)
197 return got_error_from_errno();
202 const struct got_error *
203 got_privsep_send_stop(int fd)
205 const struct got_error *err = NULL;
208 imsg_init(&ibuf, fd);
210 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
211 return got_error_from_errno();
213 err = flush_imsg(&ibuf);
218 const struct got_error *
219 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
221 struct got_imsg_object iobj, *iobjp = NULL;
222 size_t iobj_size = 0;
223 int imsg_code = GOT_IMSG_OBJECT_REQUEST;
227 case GOT_OBJ_TYPE_TREE:
228 imsg_code = GOT_IMSG_TREE_REQUEST;
230 case GOT_OBJ_TYPE_COMMIT:
231 imsg_code = GOT_IMSG_COMMIT_REQUEST;
233 case GOT_OBJ_TYPE_BLOB:
234 imsg_code = GOT_IMSG_BLOB_REQUEST;
237 return got_error(GOT_ERR_OBJ_TYPE);
240 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
241 iobj.type = obj->type;
242 iobj.flags = obj->flags;
243 iobj.hdrlen = obj->hdrlen;
244 iobj.size = obj->size;
245 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
246 iobj.pack_offset = obj->pack_offset;
247 iobj.pack_idx = obj->pack_idx;
251 iobj_size = sizeof(iobj);
254 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
255 return got_error_from_errno();
257 return flush_imsg(ibuf);
260 const struct got_error *
261 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
263 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
265 return got_error_from_errno();
267 return flush_imsg(ibuf);
270 const struct got_error *
271 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
273 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
275 return got_error_from_errno();
277 return flush_imsg(ibuf);
280 const struct got_error *
281 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
283 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
285 return got_error_from_errno();
287 return flush_imsg(ibuf);
290 const struct got_error *
291 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
293 struct got_imsg_object iobj;
295 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
296 iobj.type = obj->type;
297 iobj.flags = obj->flags;
298 iobj.hdrlen = obj->hdrlen;
299 iobj.size = obj->size;
300 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
301 iobj.pack_offset = obj->pack_offset;
302 iobj.pack_idx = obj->pack_idx;
305 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
307 return got_error_from_errno();
309 return flush_imsg(ibuf);
312 const struct got_error *
313 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
314 struct imsgbuf *ibuf)
316 const struct got_error *err = NULL;
317 struct got_imsg_object *iobj;
318 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
320 if (datalen != sizeof(*iobj))
321 return got_error(GOT_ERR_PRIVSEP_LEN);
324 *obj = calloc(1, sizeof(**obj));
326 return got_error_from_errno();
328 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
329 (*obj)->type = iobj->type;
330 (*obj)->flags = iobj->flags;
331 (*obj)->hdrlen = iobj->hdrlen;
332 (*obj)->size = iobj->size;
333 /* path_packfile is handled by caller */
334 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
335 (*obj)->pack_offset = iobj->pack_offset;
336 (*obj)->pack_idx = iobj->pack_idx;
342 const struct got_error *
343 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
345 const struct got_error *err = NULL;
348 const size_t min_datalen =
349 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
353 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
357 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
359 switch (imsg.hdr.type) {
361 err = recv_imsg_error(&imsg, datalen);
363 case GOT_IMSG_OBJECT:
364 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
367 err = got_error(GOT_ERR_PRIVSEP_MSG);
376 static const struct got_error *
377 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
380 const struct got_error *err = NULL;
381 size_t offset, remain;
386 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
388 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
389 commit->logmsg + offset, n) == -1) {
390 err = got_error_from_errno();
394 err = flush_imsg(ibuf);
405 const struct got_error *
406 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
408 const struct got_error *err = NULL;
409 struct got_imsg_commit_object *icommit;
412 struct got_object_qid *qid;
413 size_t author_len = strlen(commit->author);
414 size_t committer_len = strlen(commit->committer);
415 size_t logmsg_len = strlen(commit->logmsg);
417 total = sizeof(*icommit) + author_len + committer_len +
418 commit->nparents * SHA1_DIGEST_LENGTH;
422 return got_error_from_errno();
424 icommit = (struct got_imsg_commit_object *)buf;
425 memcpy(icommit->tree_id, commit->tree_id->sha1, sizeof(icommit->tree_id));
426 icommit->author_len = author_len;
427 icommit->author_time = commit->author_time;
428 icommit->author_gmtoff = commit->author_gmtoff;
429 icommit->committer_len = committer_len;
430 icommit->committer_time = commit->committer_time;
431 icommit->committer_gmtoff = commit->committer_gmtoff;
432 icommit->logmsg_len = logmsg_len;
433 icommit->nparents = commit->nparents;
435 len = sizeof(*icommit);
436 memcpy(buf + len, commit->author, author_len);
438 memcpy(buf + len, commit->committer, committer_len);
439 len += committer_len;
440 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
441 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
442 len += SHA1_DIGEST_LENGTH;
445 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
446 err = got_error_from_errno();
450 if (logmsg_len == 0 ||
451 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
452 err = flush_imsg(ibuf);
456 err = send_commit_logmsg(ibuf, commit, logmsg_len);
462 const struct got_error *
463 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
465 const struct got_error *err = NULL;
467 struct got_imsg_commit_object *icommit;
470 const size_t min_datalen =
471 MIN(sizeof(struct got_imsg_error),
472 sizeof(struct got_imsg_commit_object));
476 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
480 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
483 switch (imsg.hdr.type) {
485 err = recv_imsg_error(&imsg, datalen);
487 case GOT_IMSG_COMMIT:
488 if (datalen < sizeof(*icommit)) {
489 err = got_error(GOT_ERR_PRIVSEP_LEN);
493 if (datalen != sizeof(*icommit) + icommit->author_len +
494 icommit->committer_len +
495 icommit->nparents * SHA1_DIGEST_LENGTH) {
496 err = got_error(GOT_ERR_PRIVSEP_LEN);
499 if (icommit->nparents < 0) {
500 err = got_error(GOT_ERR_PRIVSEP_LEN);
503 len += sizeof(*icommit);
505 *commit = got_object_commit_alloc_partial();
506 if (*commit == NULL) {
507 err = got_error_from_errno();
511 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
513 (*commit)->author_time = icommit->author_time;
514 (*commit)->author_gmtoff = icommit->author_gmtoff;
515 (*commit)->committer_time = icommit->committer_time;
516 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
518 if (icommit->author_len == 0) {
519 (*commit)->author = strdup("");
520 if ((*commit)->author == NULL) {
521 err = got_error_from_errno();
525 (*commit)->author = malloc(icommit->author_len + 1);
526 if ((*commit)->author == NULL) {
527 err = got_error_from_errno();
530 memcpy((*commit)->author, imsg.data + len,
531 icommit->author_len);
532 (*commit)->author[icommit->author_len] = '\0';
534 len += icommit->author_len;
536 if (icommit->committer_len == 0) {
537 (*commit)->committer = strdup("");
538 if ((*commit)->committer == NULL) {
539 err = got_error_from_errno();
543 (*commit)->committer =
544 malloc(icommit->committer_len + 1);
545 if ((*commit)->committer == NULL) {
546 err = got_error_from_errno();
549 memcpy((*commit)->committer, imsg.data + len,
550 icommit->committer_len);
551 (*commit)->committer[icommit->committer_len] = '\0';
553 len += icommit->committer_len;
555 if (icommit->logmsg_len == 0) {
556 (*commit)->logmsg = strdup("");
557 if ((*commit)->logmsg == NULL) {
558 err = got_error_from_errno();
562 size_t offset = 0, remain = icommit->logmsg_len;
564 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
565 if ((*commit)->logmsg == NULL) {
566 err = got_error_from_errno();
570 struct imsg imsg_log;
571 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
574 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
578 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
579 return got_error(GOT_ERR_PRIVSEP_MSG);
581 memcpy((*commit)->logmsg + offset,
583 imsg_free(&imsg_log);
587 (*commit)->logmsg[icommit->logmsg_len] = '\0';
590 for (i = 0; i < icommit->nparents; i++) {
591 struct got_object_qid *qid;
593 err = got_object_qid_alloc_partial(&qid);
596 memcpy(qid->id, imsg.data + len +
597 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
598 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
599 (*commit)->nparents++;
603 err = got_error(GOT_ERR_PRIVSEP_MSG);
612 const struct got_error *
613 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
615 const struct got_error *err = NULL;
616 struct got_imsg_tree_object itree;
617 struct got_tree_entry *te;
619 int nimsg; /* number of imsg queued in ibuf */
621 itree.nentries = tree->entries.nentries;
622 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
624 return got_error_from_errno();
626 totlen = sizeof(itree);
628 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
629 struct got_imsg_tree_entry *ite;
631 size_t len = sizeof(*ite) + strlen(te->name);
633 if (len > MAX_IMSGSIZE)
634 return got_error(GOT_ERR_NO_SPACE);
637 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
638 err = flush_imsg(ibuf);
646 return got_error_from_errno();
648 ite = (struct got_imsg_tree_entry *)buf;
649 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
650 ite->mode = te->mode;
651 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
653 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
655 err = got_error_from_errno();
662 return flush_imsg(ibuf);
665 const struct got_error *
666 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
668 const struct got_error *err = NULL;
669 const size_t min_datalen =
670 MIN(sizeof(struct got_imsg_error),
671 sizeof(struct got_imsg_tree_object));
672 struct got_imsg_tree_object *itree;
677 err = read_imsg(ibuf);
685 struct got_imsg_tree_entry *ite;
686 struct got_tree_entry *te = NULL;
688 n = imsg_get(ibuf, &imsg);
690 if (*tree && (*tree)->entries.nentries != nentries)
695 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
696 return got_error(GOT_ERR_PRIVSEP_LEN);
698 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
700 switch (imsg.hdr.type) {
702 err = recv_imsg_error(&imsg, datalen);
705 /* This message should only appear once. */
707 err = got_error(GOT_ERR_PRIVSEP_MSG);
710 if (datalen != sizeof(*itree)) {
711 err = got_error(GOT_ERR_PRIVSEP_LEN);
715 *tree = malloc(sizeof(**tree));
717 err = got_error_from_errno();
720 (*tree)->entries.nentries = itree->nentries;
721 SIMPLEQ_INIT(&(*tree)->entries.head);
724 case GOT_IMSG_TREE_ENTRY:
725 /* This message should be preceeded by GOT_IMSG_TREE. */
727 err = got_error(GOT_ERR_PRIVSEP_MSG);
730 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
731 err = got_error(GOT_ERR_PRIVSEP_LEN);
735 /* Remaining data contains the entry's name. */
736 datalen -= sizeof(*ite);
737 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
738 err = got_error(GOT_ERR_PRIVSEP_LEN);
743 te = got_alloc_tree_entry_partial();
745 err = got_error_from_errno();
748 te->name = malloc(datalen + 1);
749 if (te->name == NULL) {
751 err = got_error_from_errno();
754 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
755 te->name[datalen] = '\0';
757 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
758 te->mode = ite->mode;
759 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
763 err = got_error(GOT_ERR_PRIVSEP_MSG);
770 if (*tree && (*tree)->entries.nentries != nentries) {
772 err = got_error(GOT_ERR_PRIVSEP_LEN);
773 got_object_tree_close(*tree);
780 const struct got_error *
781 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
783 struct got_imsg_blob iblob;
786 /* Data has already been written to file descriptor. */
788 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
790 return got_error_from_errno();
792 return flush_imsg(ibuf);
795 const struct got_error *
796 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
798 const struct got_error *err = NULL;
800 struct got_imsg_blob *iblob;
803 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
807 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
809 switch (imsg.hdr.type) {
811 err = recv_imsg_error(&imsg, datalen);
814 if (datalen != sizeof(*iblob)) {
815 err = got_error(GOT_ERR_PRIVSEP_LEN);
820 /* Data has been written to file descriptor. */
823 err = got_error(GOT_ERR_PRIVSEP_MSG);
832 const struct got_error *
833 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
834 struct got_packidx *packidx)
836 struct got_imsg_packidx ipackidx;
837 struct got_imsg_pack ipack;
840 ipackidx.len = packidx->len;
841 fd = dup(packidx->fd);
843 return got_error_from_errno();
845 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
846 sizeof(ipackidx)) == -1)
847 return got_error_from_errno();
849 if (strlcpy(ipack.path_packfile, pack->path_packfile,
850 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
851 return got_error(GOT_ERR_NO_SPACE);
852 ipack.filesize = pack->filesize;
856 return got_error_from_errno();
858 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
860 return got_error_from_errno();
862 return flush_imsg(ibuf);
865 const struct got_error *
866 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
867 struct got_object_id *id)
869 struct got_imsg_packed_object iobj;
872 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
874 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
875 &iobj, sizeof(iobj)) == -1)
876 return got_error_from_errno();
878 return flush_imsg(ibuf);