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>
32 #include "got_object.h"
33 #include "got_error.h"
35 #include "got_lib_sha1.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_parse.h"
40 #include "got_lib_privsep.h"
43 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
46 static const struct got_error *
47 poll_fd(int fd, int events, int timeout)
53 pfd[0].events = events;
55 n = poll(pfd, 1, timeout);
57 return got_error_from_errno();
59 return got_error(GOT_ERR_TIMEOUT);
60 if (pfd[0].revents & (POLLERR | POLLNVAL))
61 return got_error_from_errno();
62 if (pfd[0].revents & (events | POLLHUP))
65 return got_error(GOT_ERR_INTERRUPT);
68 static const struct got_error *
69 read_imsg(struct imsgbuf *ibuf)
71 const struct got_error *err;
74 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
80 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
81 return got_error(GOT_ERR_PRIVSEP_NO_FD);
82 return got_error(GOT_ERR_PRIVSEP_READ);
85 return got_error(GOT_ERR_PRIVSEP_PIPE);
90 static const struct got_error *
91 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
93 const struct got_error *err;
96 err = read_imsg(ibuf);
100 n = imsg_get(ibuf, imsg);
102 return got_error(GOT_ERR_PRIVSEP_READ);
104 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
105 return got_error(GOT_ERR_PRIVSEP_LEN);
110 static const struct got_error *
111 recv_imsg_error(struct imsg *imsg, size_t datalen)
113 struct got_imsg_error ierr;
115 if (datalen != sizeof(ierr))
116 return got_error(GOT_ERR_PRIVSEP_LEN);
118 memcpy(&ierr, imsg->data, sizeof(ierr));
119 if (ierr.code == GOT_ERR_ERRNO) {
120 static struct got_error serr;
121 serr.code = GOT_ERR_ERRNO;
122 serr.msg = strerror(ierr.errno_code);
126 return got_error(ierr.code);
129 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
131 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
133 const struct got_error *poll_err;
134 struct got_imsg_error ierr;
137 ierr.code = err->code;
138 if (err->code == GOT_ERR_ERRNO)
139 ierr.errno_code = errno;
142 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
144 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
145 getprogname(), err->code, err->msg, strerror(errno));
149 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
151 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
152 getprogname(), err->code, err->msg, poll_err->msg);
156 ret = imsg_flush(ibuf);
158 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
159 getprogname(), err->code, err->msg, strerror(errno));
164 static const struct got_error *
165 flush_imsg(struct imsgbuf *ibuf)
167 const struct got_error *err;
169 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
173 if (imsg_flush(ibuf) == -1)
174 return got_error_from_errno();
179 const struct got_error *
180 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
182 struct got_imsg_object iobj;
184 iobj.type = obj->type;
185 iobj.flags = obj->flags;
186 iobj.hdrlen = obj->hdrlen;
187 iobj.size = obj->size;
188 iobj.ndeltas = ndeltas;
191 /* TODO: Handle deltas */
194 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
196 return got_error_from_errno();
198 return flush_imsg(ibuf);
201 const struct got_error *
202 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
204 const struct got_error *err = NULL;
206 struct got_imsg_object iobj;
209 const size_t min_datalen =
210 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
214 err = recv_one_imsg(&imsg, ibuf, min_datalen);
218 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
220 switch (imsg.hdr.type) {
222 err = recv_imsg_error(&imsg, datalen);
224 case GOT_IMSG_OBJECT:
225 if (datalen != sizeof(iobj)) {
226 err = got_error(GOT_ERR_PRIVSEP_LEN);
230 memcpy(&iobj, imsg.data, sizeof(iobj));
231 if (iobj.ndeltas < 0 ||
232 iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
233 err = got_error(GOT_ERR_PRIVSEP_LEN);
237 *obj = calloc(1, sizeof(**obj));
239 err = got_error_from_errno();
243 (*obj)->type = iobj.type;
244 (*obj)->hdrlen = iobj.hdrlen;
245 (*obj)->size = iobj.size;
246 for (i = 0; i < iobj.ndeltas; i++) {
247 /* TODO: Handle deltas */
251 err = got_error(GOT_ERR_PRIVSEP_MSG);
260 const struct got_error *
261 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
263 const struct got_error *err = NULL;
264 struct got_imsg_commit_object icommit;
267 struct got_object_qid *qid;
269 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
270 icommit.author_len = strlen(commit->author);
271 memcpy(&icommit.tm_author, &commit->tm_author,
272 sizeof(icommit.tm_author));
273 icommit.committer_len = strlen(commit->committer);
274 memcpy(&icommit.tm_committer, &commit->tm_committer,
275 sizeof(icommit.tm_committer));
276 icommit.logmsg_len = strlen(commit->logmsg);
277 icommit.nparents = commit->nparents;
279 total = sizeof(icommit) + icommit.author_len +
280 icommit.committer_len + icommit.logmsg_len +
281 icommit.nparents * SHA1_DIGEST_LENGTH;
282 /* XXX TODO support very large log messages properly */
283 if (total > MAX_IMSGSIZE)
284 return got_error(GOT_ERR_NO_SPACE);
288 return got_error_from_errno();
291 memcpy(buf + len, &icommit, sizeof(icommit));
292 len += sizeof(icommit);
293 memcpy(buf + len, commit->author, icommit.author_len);
294 len += icommit.author_len;
295 memcpy(buf + len, commit->committer, icommit.committer_len);
296 len += icommit.committer_len;
297 memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
298 len += icommit.logmsg_len;
299 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
300 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
301 len += SHA1_DIGEST_LENGTH;
304 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
305 err = got_error_from_errno();
309 err = flush_imsg(ibuf);
314 const struct got_error *
315 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
317 const struct got_error *err = NULL;
319 struct got_imsg_commit_object icommit;
322 const size_t min_datalen =
323 MIN(sizeof(struct got_imsg_error),
324 sizeof(struct got_imsg_commit_object));
329 err = recv_one_imsg(&imsg, ibuf, min_datalen);
334 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
337 switch (imsg.hdr.type) {
339 err = recv_imsg_error(&imsg, datalen);
341 case GOT_IMSG_COMMIT:
342 if (datalen < sizeof(icommit)) {
343 err = got_error(GOT_ERR_PRIVSEP_LEN);
347 memcpy(&icommit, data, sizeof(icommit));
348 if (datalen != sizeof(icommit) + icommit.author_len +
349 icommit.committer_len + icommit.logmsg_len +
350 icommit.nparents * SHA1_DIGEST_LENGTH) {
351 err = got_error(GOT_ERR_PRIVSEP_LEN);
354 if (icommit.nparents < 0) {
355 err = got_error(GOT_ERR_PRIVSEP_LEN);
358 len += sizeof(icommit);
360 *commit = got_object_commit_alloc_partial();
361 if (*commit == NULL) {
362 err = got_error_from_errno();
366 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
368 memcpy(&(*commit)->tm_author, &icommit.tm_author,
369 sizeof((*commit)->tm_author));
370 memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
371 sizeof((*commit)->tm_committer));
373 if (icommit.author_len == 0) {
374 (*commit)->author = strdup("");
375 if ((*commit)->author == NULL) {
376 err = got_error_from_errno();
380 (*commit)->author = malloc(icommit.author_len + 1);
381 if ((*commit)->author == NULL) {
382 err = got_error_from_errno();
385 memcpy((*commit)->author, data + len,
387 (*commit)->author[icommit.author_len] = '\0';
389 len += icommit.author_len;
391 if (icommit.committer_len == 0) {
392 (*commit)->committer = strdup("");
393 if ((*commit)->committer == NULL) {
394 err = got_error_from_errno();
398 (*commit)->committer =
399 malloc(icommit.committer_len + 1);
400 if ((*commit)->committer == NULL) {
401 err = got_error_from_errno();
404 memcpy((*commit)->committer, data + len,
405 icommit.committer_len);
406 (*commit)->committer[icommit.committer_len] = '\0';
408 len += icommit.committer_len;
410 if (icommit.logmsg_len == 0) {
411 (*commit)->logmsg = strdup("");
412 if ((*commit)->logmsg == NULL) {
413 err = got_error_from_errno();
417 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
418 if ((*commit)->logmsg == NULL) {
419 err = got_error_from_errno();
422 memcpy((*commit)->logmsg, data + len,
424 (*commit)->logmsg[icommit.logmsg_len] = '\0';
426 len += icommit.logmsg_len;
428 for (i = 0; i < icommit.nparents; i++) {
429 struct got_object_qid *qid;
431 qid = calloc(1, sizeof(*qid));
433 err = got_error_from_errno();
436 qid->id = calloc(1, sizeof(*qid->id));
437 if (qid->id == NULL) {
438 err = got_error_from_errno();
443 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
445 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
446 (*commit)->nparents++;
450 err = got_error(GOT_ERR_PRIVSEP_MSG);
459 const struct got_error *
460 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
462 const struct got_error *err = NULL;
463 struct got_imsg_tree_object itree;
464 struct got_tree_entry *te;
466 itree.nentries = tree->entries.nentries;
467 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
469 return got_error_from_errno();
471 err = flush_imsg(ibuf);
475 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
476 struct got_imsg_tree_entry ite;
478 size_t len = sizeof(ite) + strlen(te->name);
480 if (len > MAX_IMSGSIZE)
481 return got_error(GOT_ERR_NO_SPACE);
485 return got_error_from_errno();
487 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
489 memcpy(buf, &ite, sizeof(ite));
490 memcpy(buf + sizeof(ite), te->name, strlen(te->name));
492 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
494 err = got_error_from_errno();
499 err = flush_imsg(ibuf);
507 const struct got_error *
508 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
510 const struct got_error *err = NULL;
511 const size_t min_datalen =
512 MIN(sizeof(struct got_imsg_error),
513 sizeof(struct got_imsg_tree_object));
514 struct got_imsg_tree_object itree = { 0 };
519 err = read_imsg(ibuf);
527 struct got_imsg_tree_entry ite;
528 struct got_tree_entry *te = NULL;
530 n = imsg_get(ibuf, &imsg);
532 if (*tree && (*tree)->entries.nentries != nentries)
537 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
538 return got_error(GOT_ERR_PRIVSEP_LEN);
540 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
542 switch (imsg.hdr.type) {
544 err = recv_imsg_error(&imsg, datalen);
547 /* This message should only appear once. */
549 err = got_error(GOT_ERR_PRIVSEP_MSG);
552 if (datalen != sizeof(itree)) {
553 err = got_error(GOT_ERR_PRIVSEP_LEN);
556 memcpy(&itree, imsg.data, sizeof(itree));
557 *tree = calloc(1, sizeof(**tree));
559 err = got_error_from_errno();
562 (*tree)->entries.nentries = itree.nentries;
563 SIMPLEQ_INIT(&(*tree)->entries.head);
565 case GOT_IMSG_TREE_ENTRY:
566 /* This message should be preceeded by GOT_IMSG_TREE. */
568 err = got_error(GOT_ERR_PRIVSEP_MSG);
571 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
572 err = got_error(GOT_ERR_PRIVSEP_LEN);
576 /* Remaining data contains the entry's name. */
577 datalen -= sizeof(ite);
578 memcpy(&ite, imsg.data, sizeof(ite));
579 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
580 err = got_error(GOT_ERR_PRIVSEP_LEN);
584 te = got_alloc_tree_entry_partial();
586 err = got_error_from_errno();
589 te->name = malloc(datalen + 1);
590 if (te->name == NULL) {
592 err = got_error_from_errno();
595 memcpy(te->name, imsg.data + sizeof(ite), datalen);
596 te->name[datalen] = '\0';
598 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
600 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
604 err = got_error(GOT_ERR_PRIVSEP_MSG);
611 if (*tree && (*tree)->entries.nentries != nentries) {
613 err = got_error(GOT_ERR_PRIVSEP_LEN);
614 got_object_tree_close(*tree);
621 const struct got_error *
622 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
624 struct got_imsg_blob iblob;
627 /* Data has already been written to file descriptor. */
629 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
631 return got_error_from_errno();
633 return flush_imsg(ibuf);
636 const struct got_error *
637 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
639 const struct got_error *err = NULL;
641 struct got_imsg_blob iblob;
644 err = recv_one_imsg(&imsg, ibuf, 0);
648 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
650 switch (imsg.hdr.type) {
652 err = recv_imsg_error(&imsg, datalen);
655 if (datalen != sizeof(iblob))
656 err = got_error(GOT_ERR_PRIVSEP_LEN);
657 memcpy(&iblob, imsg.data, sizeof(iblob));
659 /* Data has been written to file descriptor. */
662 err = got_error(GOT_ERR_PRIVSEP_MSG);