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);
140 memcpy(&ierr, imsg->data, sizeof(ierr));
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_obj(struct imsgbuf *ibuf, struct got_object *obj)
282 struct got_imsg_object iobj;
284 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
285 iobj.type = obj->type;
286 iobj.flags = obj->flags;
287 iobj.hdrlen = obj->hdrlen;
288 iobj.size = obj->size;
289 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
290 iobj.pack_offset = obj->pack_offset;
291 iobj.pack_idx = obj->pack_idx;
294 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
296 return got_error_from_errno();
298 return flush_imsg(ibuf);
301 const struct got_error *
302 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
303 struct imsgbuf *ibuf)
305 const struct got_error *err = NULL;
306 struct got_imsg_object iobj;
307 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
309 if (datalen != sizeof(iobj))
310 return got_error(GOT_ERR_PRIVSEP_LEN);
312 memcpy(&iobj, imsg->data, sizeof(iobj));
314 *obj = calloc(1, sizeof(**obj));
316 return got_error_from_errno();
318 memcpy((*obj)->id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
319 (*obj)->type = iobj.type;
320 (*obj)->flags = iobj.flags;
321 (*obj)->hdrlen = iobj.hdrlen;
322 (*obj)->size = iobj.size;
323 /* path_packfile is handled by caller */
324 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
325 (*obj)->pack_offset = iobj.pack_offset;
326 (*obj)->pack_idx = iobj.pack_idx;
332 const struct got_error *
333 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
335 const struct got_error *err = NULL;
338 const size_t min_datalen =
339 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
343 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
347 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
349 switch (imsg.hdr.type) {
351 err = recv_imsg_error(&imsg, datalen);
353 case GOT_IMSG_OBJECT:
354 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
357 err = got_error(GOT_ERR_PRIVSEP_MSG);
366 const struct got_error *
367 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
369 const struct got_error *err = NULL;
370 struct got_imsg_commit_object icommit;
373 struct got_object_qid *qid;
375 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
376 icommit.author_len = strlen(commit->author);
377 memcpy(&icommit.tm_author, &commit->tm_author,
378 sizeof(icommit.tm_author));
379 icommit.committer_len = strlen(commit->committer);
380 memcpy(&icommit.tm_committer, &commit->tm_committer,
381 sizeof(icommit.tm_committer));
382 icommit.logmsg_len = strlen(commit->logmsg);
383 icommit.nparents = commit->nparents;
385 total = sizeof(icommit) + icommit.author_len +
386 icommit.committer_len + icommit.logmsg_len +
387 icommit.nparents * SHA1_DIGEST_LENGTH;
388 /* XXX TODO support very large log messages properly */
389 if (total > MAX_IMSGSIZE)
390 return got_error(GOT_ERR_NO_SPACE);
394 return got_error_from_errno();
397 memcpy(buf + len, &icommit, sizeof(icommit));
398 len += sizeof(icommit);
399 memcpy(buf + len, commit->author, icommit.author_len);
400 len += icommit.author_len;
401 memcpy(buf + len, commit->committer, icommit.committer_len);
402 len += icommit.committer_len;
403 memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
404 len += icommit.logmsg_len;
405 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
406 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
407 len += SHA1_DIGEST_LENGTH;
410 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
411 err = got_error_from_errno();
415 err = flush_imsg(ibuf);
421 const struct got_error *
422 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
424 const struct got_error *err = NULL;
426 struct got_imsg_commit_object icommit;
429 const size_t min_datalen =
430 MIN(sizeof(struct got_imsg_error),
431 sizeof(struct got_imsg_commit_object));
436 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
441 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
444 switch (imsg.hdr.type) {
446 err = recv_imsg_error(&imsg, datalen);
448 case GOT_IMSG_COMMIT:
449 if (datalen < sizeof(icommit)) {
450 err = got_error(GOT_ERR_PRIVSEP_LEN);
454 memcpy(&icommit, data, sizeof(icommit));
455 if (datalen != sizeof(icommit) + icommit.author_len +
456 icommit.committer_len + icommit.logmsg_len +
457 icommit.nparents * SHA1_DIGEST_LENGTH) {
458 err = got_error(GOT_ERR_PRIVSEP_LEN);
461 if (icommit.nparents < 0) {
462 err = got_error(GOT_ERR_PRIVSEP_LEN);
465 len += sizeof(icommit);
467 *commit = got_object_commit_alloc_partial();
468 if (*commit == NULL) {
469 err = got_error_from_errno();
473 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
475 memcpy(&(*commit)->tm_author, &icommit.tm_author,
476 sizeof((*commit)->tm_author));
477 memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
478 sizeof((*commit)->tm_committer));
480 if (icommit.author_len == 0) {
481 (*commit)->author = strdup("");
482 if ((*commit)->author == NULL) {
483 err = got_error_from_errno();
487 (*commit)->author = malloc(icommit.author_len + 1);
488 if ((*commit)->author == NULL) {
489 err = got_error_from_errno();
492 memcpy((*commit)->author, data + len,
494 (*commit)->author[icommit.author_len] = '\0';
496 len += icommit.author_len;
498 if (icommit.committer_len == 0) {
499 (*commit)->committer = strdup("");
500 if ((*commit)->committer == NULL) {
501 err = got_error_from_errno();
505 (*commit)->committer =
506 malloc(icommit.committer_len + 1);
507 if ((*commit)->committer == NULL) {
508 err = got_error_from_errno();
511 memcpy((*commit)->committer, data + len,
512 icommit.committer_len);
513 (*commit)->committer[icommit.committer_len] = '\0';
515 len += icommit.committer_len;
517 if (icommit.logmsg_len == 0) {
518 (*commit)->logmsg = strdup("");
519 if ((*commit)->logmsg == NULL) {
520 err = got_error_from_errno();
524 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
525 if ((*commit)->logmsg == NULL) {
526 err = got_error_from_errno();
529 memcpy((*commit)->logmsg, data + len,
531 (*commit)->logmsg[icommit.logmsg_len] = '\0';
533 len += icommit.logmsg_len;
535 for (i = 0; i < icommit.nparents; i++) {
536 struct got_object_qid *qid;
538 qid = calloc(1, sizeof(*qid));
540 err = got_error_from_errno();
543 qid->id = calloc(1, sizeof(*qid->id));
544 if (qid->id == NULL) {
545 err = got_error_from_errno();
550 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
552 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
553 (*commit)->nparents++;
557 err = got_error(GOT_ERR_PRIVSEP_MSG);
566 const struct got_error *
567 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
569 const struct got_error *err = NULL;
570 struct got_imsg_tree_object itree;
571 struct got_tree_entry *te;
573 itree.nentries = tree->entries.nentries;
574 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
576 return got_error_from_errno();
578 err = flush_imsg(ibuf);
582 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
583 struct got_imsg_tree_entry ite;
585 size_t len = sizeof(ite) + strlen(te->name);
587 if (len > MAX_IMSGSIZE)
588 return got_error(GOT_ERR_NO_SPACE);
592 return got_error_from_errno();
594 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
596 memcpy(buf, &ite, sizeof(ite));
597 memcpy(buf + sizeof(ite), te->name, strlen(te->name));
599 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
601 err = got_error_from_errno();
606 err = flush_imsg(ibuf);
614 const struct got_error *
615 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
617 const struct got_error *err = NULL;
618 const size_t min_datalen =
619 MIN(sizeof(struct got_imsg_error),
620 sizeof(struct got_imsg_tree_object));
621 struct got_imsg_tree_object itree = { 0 };
626 err = read_imsg(ibuf);
634 struct got_imsg_tree_entry ite;
635 struct got_tree_entry *te = NULL;
637 n = imsg_get(ibuf, &imsg);
639 if (*tree && (*tree)->entries.nentries != nentries)
644 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
645 return got_error(GOT_ERR_PRIVSEP_LEN);
647 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
649 switch (imsg.hdr.type) {
651 err = recv_imsg_error(&imsg, datalen);
654 /* This message should only appear once. */
656 err = got_error(GOT_ERR_PRIVSEP_MSG);
659 if (datalen != sizeof(itree)) {
660 err = got_error(GOT_ERR_PRIVSEP_LEN);
663 memcpy(&itree, imsg.data, sizeof(itree));
664 *tree = calloc(1, sizeof(**tree));
666 err = got_error_from_errno();
669 (*tree)->entries.nentries = itree.nentries;
670 SIMPLEQ_INIT(&(*tree)->entries.head);
672 case GOT_IMSG_TREE_ENTRY:
673 /* This message should be preceeded by GOT_IMSG_TREE. */
675 err = got_error(GOT_ERR_PRIVSEP_MSG);
678 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
679 err = got_error(GOT_ERR_PRIVSEP_LEN);
683 /* Remaining data contains the entry's name. */
684 datalen -= sizeof(ite);
685 memcpy(&ite, imsg.data, sizeof(ite));
686 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
687 err = got_error(GOT_ERR_PRIVSEP_LEN);
691 te = got_alloc_tree_entry_partial();
693 err = got_error_from_errno();
696 te->name = malloc(datalen + 1);
697 if (te->name == NULL) {
699 err = got_error_from_errno();
702 memcpy(te->name, imsg.data + sizeof(ite), datalen);
703 te->name[datalen] = '\0';
705 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
707 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
711 err = got_error(GOT_ERR_PRIVSEP_MSG);
718 if (*tree && (*tree)->entries.nentries != nentries) {
720 err = got_error(GOT_ERR_PRIVSEP_LEN);
721 got_object_tree_close(*tree);
728 const struct got_error *
729 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
731 struct got_imsg_blob iblob;
734 /* Data has already been written to file descriptor. */
736 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
738 return got_error_from_errno();
740 return flush_imsg(ibuf);
743 const struct got_error *
744 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
746 const struct got_error *err = NULL;
748 struct got_imsg_blob iblob;
751 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
755 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
757 switch (imsg.hdr.type) {
759 err = recv_imsg_error(&imsg, datalen);
762 if (datalen != sizeof(iblob))
763 err = got_error(GOT_ERR_PRIVSEP_LEN);
764 memcpy(&iblob, imsg.data, sizeof(iblob));
766 /* Data has been written to file descriptor. */
769 err = got_error(GOT_ERR_PRIVSEP_MSG);
778 const struct got_error *
779 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
780 struct got_packidx *packidx)
782 struct got_imsg_packidx ipackidx;
783 struct got_imsg_pack ipack;
786 ipackidx.len = packidx->len;
787 fd = dup(packidx->fd);
789 return got_error_from_errno();
791 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
792 sizeof(ipackidx)) == -1)
793 return got_error_from_errno();
795 if (strlcpy(ipack.path_packfile, pack->path_packfile,
796 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
797 return got_error(GOT_ERR_NO_SPACE);
798 ipack.filesize = pack->filesize;
802 return got_error_from_errno();
804 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
806 return got_error_from_errno();
808 return flush_imsg(ibuf);
811 const struct got_error *
812 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx)
814 struct got_imsg_packed_object iobj;
818 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
819 &iobj, sizeof(iobj)) == -1)
820 return got_error_from_errno();
822 return flush_imsg(ibuf);