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_privsep.h"
42 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
45 static const struct got_error *
46 poll_fd(int fd, int events, int timeout)
52 pfd[0].events = events;
54 n = poll(pfd, 1, timeout);
56 return got_error_from_errno();
58 return got_error(GOT_ERR_TIMEOUT);
59 if (pfd[0].revents & (POLLERR | POLLNVAL))
60 return got_error_from_errno();
61 if (pfd[0].revents & (events | POLLHUP))
64 return got_error(GOT_ERR_INTERRUPT);
67 static const struct got_error *
68 read_imsg(struct imsgbuf *ibuf)
70 const struct got_error *err;
73 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
79 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
80 return got_error(GOT_ERR_PRIVSEP_NO_FD);
81 return got_error(GOT_ERR_PRIVSEP_READ);
84 return got_error(GOT_ERR_PRIVSEP_PIPE);
89 static const struct got_error *
90 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
92 const struct got_error *err;
95 err = read_imsg(ibuf);
99 n = imsg_get(ibuf, imsg);
101 return got_error(GOT_ERR_PRIVSEP_READ);
103 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
104 return got_error(GOT_ERR_PRIVSEP_LEN);
109 static const struct got_error *
110 recv_imsg_error(struct imsg *imsg, size_t datalen)
112 struct got_imsg_error ierr;
114 if (datalen != sizeof(ierr))
115 return got_error(GOT_ERR_PRIVSEP_LEN);
117 memcpy(&ierr, imsg->data, sizeof(ierr));
118 if (ierr.code == GOT_ERR_ERRNO) {
119 static struct got_error serr;
120 serr.code = GOT_ERR_ERRNO;
121 serr.msg = strerror(ierr.errno_code);
125 return got_error(ierr.code);
128 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
130 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
132 const struct got_error *poll_err;
133 struct got_imsg_error ierr;
136 ierr.code = err->code;
137 if (err->code == GOT_ERR_ERRNO)
138 ierr.errno_code = errno;
141 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
143 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
144 getprogname(), err->code, err->msg, strerror(errno));
148 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
150 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
151 getprogname(), err->code, err->msg, poll_err->msg);
155 ret = imsg_flush(ibuf);
157 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
158 getprogname(), err->code, err->msg, strerror(errno));
163 static const struct got_error *
164 flush_imsg(struct imsgbuf *ibuf)
166 const struct got_error *err;
168 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
172 if (imsg_flush(ibuf) == -1)
173 return got_error_from_errno();
178 const struct got_error *
179 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
181 struct got_imsg_object iobj;
183 iobj.type = obj->type;
184 iobj.flags = obj->flags;
185 iobj.hdrlen = obj->hdrlen;
186 iobj.size = obj->size;
187 iobj.ndeltas = ndeltas;
190 /* TODO: Handle deltas */
193 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
195 return got_error_from_errno();
197 return flush_imsg(ibuf);
200 const struct got_error *
201 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
203 const struct got_error *err = NULL;
205 struct got_imsg_object iobj;
208 const size_t min_datalen =
209 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
213 err = recv_one_imsg(&imsg, ibuf, min_datalen);
217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
219 switch (imsg.hdr.type) {
221 err = recv_imsg_error(&imsg, datalen);
223 case GOT_IMSG_OBJECT:
224 if (datalen != sizeof(iobj)) {
225 err = got_error(GOT_ERR_PRIVSEP_LEN);
229 memcpy(&iobj, imsg.data, sizeof(iobj));
230 if (iobj.ndeltas < 0 ||
231 iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
232 err = got_error(GOT_ERR_PRIVSEP_LEN);
236 *obj = calloc(1, sizeof(**obj));
238 err = got_error_from_errno();
242 (*obj)->type = iobj.type;
243 (*obj)->hdrlen = iobj.hdrlen;
244 (*obj)->size = iobj.size;
245 for (i = 0; i < iobj.ndeltas; i++) {
246 /* TODO: Handle deltas */
250 err = got_error(GOT_ERR_PRIVSEP_MSG);
259 const struct got_error *
260 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
262 const struct got_error *err = NULL;
263 struct got_imsg_commit_object icommit;
266 struct got_object_qid *qid;
268 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
269 icommit.author_len = strlen(commit->author);
270 memcpy(&icommit.tm_author, &commit->tm_author,
271 sizeof(icommit.tm_author));
272 icommit.committer_len = strlen(commit->committer);
273 memcpy(&icommit.tm_committer, &commit->tm_committer,
274 sizeof(icommit.tm_committer));
275 icommit.logmsg_len = strlen(commit->logmsg);
276 icommit.nparents = commit->nparents;
278 total = sizeof(icommit) + icommit.author_len +
279 icommit.committer_len + icommit.logmsg_len +
280 icommit.nparents * SHA1_DIGEST_LENGTH;
281 /* XXX TODO support very large log messages properly */
282 if (total > MAX_IMSGSIZE)
283 return got_error(GOT_ERR_NO_SPACE);
287 return got_error_from_errno();
290 memcpy(buf + len, &icommit, sizeof(icommit));
291 len += sizeof(icommit);
292 memcpy(buf + len, commit->author, icommit.author_len);
293 len += icommit.author_len;
294 memcpy(buf + len, commit->committer, icommit.committer_len);
295 len += icommit.committer_len;
296 memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
297 len += icommit.logmsg_len;
298 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
299 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
300 len += SHA1_DIGEST_LENGTH;
303 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
304 err = got_error_from_errno();
308 err = flush_imsg(ibuf);
313 const struct got_error *
314 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
316 const struct got_error *err = NULL;
318 struct got_imsg_commit_object icommit;
321 const size_t min_datalen =
322 MIN(sizeof(struct got_imsg_error),
323 sizeof(struct got_imsg_commit_object));
328 err = recv_one_imsg(&imsg, ibuf, min_datalen);
333 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
336 switch (imsg.hdr.type) {
338 err = recv_imsg_error(&imsg, datalen);
340 case GOT_IMSG_COMMIT:
341 if (datalen < sizeof(icommit)) {
342 err = got_error(GOT_ERR_PRIVSEP_LEN);
346 memcpy(&icommit, data, sizeof(icommit));
347 if (datalen != sizeof(icommit) + icommit.author_len +
348 icommit.committer_len + icommit.logmsg_len +
349 icommit.nparents * SHA1_DIGEST_LENGTH) {
350 err = got_error(GOT_ERR_PRIVSEP_LEN);
353 if (icommit.nparents < 0) {
354 err = got_error(GOT_ERR_PRIVSEP_LEN);
357 len += sizeof(icommit);
359 *commit = got_object_commit_alloc_partial();
360 if (*commit == NULL) {
361 err = got_error_from_errno();
365 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
367 memcpy(&(*commit)->tm_author, &icommit.tm_author,
368 sizeof((*commit)->tm_author));
369 memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
370 sizeof((*commit)->tm_committer));
372 if (icommit.author_len == 0) {
373 (*commit)->author = strdup("");
374 if ((*commit)->author == NULL) {
375 err = got_error_from_errno();
379 (*commit)->author = malloc(icommit.author_len + 1);
380 if ((*commit)->author == NULL) {
381 err = got_error_from_errno();
384 memcpy((*commit)->author, data + len,
386 (*commit)->author[icommit.author_len] = '\0';
388 len += icommit.author_len;
390 if (icommit.committer_len == 0) {
391 (*commit)->committer = strdup("");
392 if ((*commit)->committer == NULL) {
393 err = got_error_from_errno();
397 (*commit)->committer =
398 malloc(icommit.committer_len + 1);
399 if ((*commit)->committer == NULL) {
400 err = got_error_from_errno();
403 memcpy((*commit)->committer, data + len,
404 icommit.committer_len);
405 (*commit)->committer[icommit.committer_len] = '\0';
407 len += icommit.committer_len;
409 if (icommit.logmsg_len == 0) {
410 (*commit)->logmsg = strdup("");
411 if ((*commit)->logmsg == NULL) {
412 err = got_error_from_errno();
416 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
417 if ((*commit)->logmsg == NULL) {
418 err = got_error_from_errno();
421 memcpy((*commit)->logmsg, data + len,
423 (*commit)->logmsg[icommit.logmsg_len] = '\0';
425 len += icommit.logmsg_len;
427 for (i = 0; i < icommit.nparents; i++) {
428 struct got_object_qid *qid;
430 qid = calloc(1, sizeof(*qid));
432 err = got_error_from_errno();
435 qid->id = calloc(1, sizeof(*qid->id));
436 if (qid->id == NULL) {
437 err = got_error_from_errno();
442 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
444 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
445 (*commit)->nparents++;
449 err = got_error(GOT_ERR_PRIVSEP_MSG);
458 const struct got_error *
459 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
461 const struct got_error *err = NULL;
462 struct got_imsg_tree_object itree;
463 struct got_tree_entry *te;
465 itree.nentries = tree->entries.nentries;
466 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
468 return got_error_from_errno();
470 err = flush_imsg(ibuf);
474 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
475 struct got_imsg_tree_entry ite;
477 size_t len = sizeof(ite) + strlen(te->name);
479 if (len > MAX_IMSGSIZE)
480 return got_error(GOT_ERR_NO_SPACE);
484 return got_error_from_errno();
486 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
488 memcpy(buf, &ite, sizeof(ite));
489 memcpy(buf + sizeof(ite), te->name, strlen(te->name));
491 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
493 err = got_error_from_errno();
498 err = flush_imsg(ibuf);
506 const struct got_error *
507 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
509 const struct got_error *err = NULL;
510 const size_t min_datalen =
511 MIN(sizeof(struct got_imsg_error),
512 sizeof(struct got_imsg_tree_object));
513 struct got_imsg_tree_object itree = { 0 };
518 err = read_imsg(ibuf);
526 struct got_imsg_tree_entry ite;
527 struct got_tree_entry *te = NULL;
529 n = imsg_get(ibuf, &imsg);
531 if (*tree && (*tree)->entries.nentries != nentries)
536 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
537 return got_error(GOT_ERR_PRIVSEP_LEN);
539 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
541 switch (imsg.hdr.type) {
543 err = recv_imsg_error(&imsg, datalen);
546 /* This message should only appear once. */
548 err = got_error(GOT_ERR_PRIVSEP_MSG);
551 if (datalen != sizeof(itree)) {
552 err = got_error(GOT_ERR_PRIVSEP_LEN);
555 memcpy(&itree, imsg.data, sizeof(itree));
556 *tree = calloc(1, sizeof(**tree));
558 err = got_error_from_errno();
561 (*tree)->entries.nentries = itree.nentries;
562 SIMPLEQ_INIT(&(*tree)->entries.head);
564 case GOT_IMSG_TREE_ENTRY:
565 /* This message should be preceeded by GOT_IMSG_TREE. */
567 err = got_error(GOT_ERR_PRIVSEP_MSG);
570 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
571 err = got_error(GOT_ERR_PRIVSEP_LEN);
575 /* Remaining data contains the entry's name. */
576 datalen -= sizeof(ite);
577 memcpy(&ite, imsg.data, sizeof(ite));
578 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
579 err = got_error(GOT_ERR_PRIVSEP_LEN);
583 te = got_alloc_tree_entry_partial();
585 err = got_error_from_errno();
588 te->name = malloc(datalen + 1);
589 if (te->name == NULL) {
591 err = got_error_from_errno();
594 memcpy(te->name, imsg.data + sizeof(ite), datalen);
595 te->name[datalen] = '\0';
597 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
599 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
603 err = got_error(GOT_ERR_PRIVSEP_MSG);
610 if (*tree && (*tree)->entries.nentries != nentries) {
612 err = got_error(GOT_ERR_PRIVSEP_LEN);
613 got_object_tree_close(*tree);
620 const struct got_error *
621 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
623 struct got_imsg_blob iblob;
626 /* Data has already been written to file descriptor. */
628 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
630 return got_error_from_errno();
632 return flush_imsg(ibuf);
635 const struct got_error *
636 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
638 const struct got_error *err = NULL;
640 struct got_imsg_blob iblob;
643 err = recv_one_imsg(&imsg, ibuf, 0);
647 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
649 switch (imsg.hdr.type) {
651 err = recv_imsg_error(&imsg, datalen);
654 if (datalen != sizeof(iblob))
655 err = got_error(GOT_ERR_PRIVSEP_LEN);
656 memcpy(&iblob, imsg.data, sizeof(iblob));
658 /* Data has been written to file descriptor. */
661 err = got_error(GOT_ERR_PRIVSEP_MSG);