Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdint.h>
26 #include <poll.h>
27 #include <imsg.h>
28 #include <sha1.h>
29 #include <zlib.h>
31 #include "got_object.h"
32 #include "got_error.h"
34 #include "got_lib_sha1.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_zbuf.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
40 #ifndef MIN
41 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 #endif
44 static const struct got_error *
45 poll_fd(int fd, int events, int timeout)
46 {
47 struct pollfd pfd[1];
48 int n;
50 pfd[0].fd = fd;
51 pfd[0].events = events;
53 n = poll(pfd, 1, timeout);
54 if (n == -1)
55 return got_error_from_errno();
56 if (n == 0)
57 return got_error(GOT_ERR_TIMEOUT);
58 if (pfd[0].revents & (POLLERR | POLLNVAL))
59 return got_error_from_errno();
60 if (pfd[0].revents & (events | POLLHUP))
61 return NULL;
63 return got_error(GOT_ERR_INTERRUPT);
64 }
66 static const struct got_error *
67 read_imsg(struct imsgbuf *ibuf)
68 {
69 const struct got_error *err;
70 size_t n;
72 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
73 if (err)
74 return err;
76 n = imsg_read(ibuf);
77 if (n == -1) {
78 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
79 return got_error(GOT_ERR_PRIVSEP_NO_FD);
80 return got_error(GOT_ERR_PRIVSEP_READ);
81 }
82 if (n == 0)
83 return got_error(GOT_ERR_PRIVSEP_PIPE);
85 return NULL;
86 }
88 static const struct got_error *
89 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
90 {
91 const struct got_error *err;
92 ssize_t n;
94 err = read_imsg(ibuf);
95 if (err)
96 return err;
98 n = imsg_get(ibuf, imsg);
99 if (n == 0)
100 return got_error(GOT_ERR_PRIVSEP_READ);
102 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
103 return got_error(GOT_ERR_PRIVSEP_LEN);
105 return NULL;
108 static const struct got_error *
109 recv_imsg_error(struct imsg *imsg, size_t datalen)
111 struct got_imsg_error ierr;
113 if (datalen != sizeof(ierr))
114 return got_error(GOT_ERR_PRIVSEP_LEN);
116 memcpy(&ierr, imsg->data, sizeof(ierr));
117 if (ierr.code == GOT_ERR_ERRNO) {
118 static struct got_error serr;
119 serr.code = GOT_ERR_ERRNO;
120 serr.msg = strerror(ierr.errno_code);
121 return &serr;
124 return got_error(ierr.code);
127 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
128 void
129 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
131 const struct got_error *poll_err;
132 struct got_imsg_error ierr;
133 int ret;
135 ierr.code = err->code;
136 if (err->code == GOT_ERR_ERRNO)
137 ierr.errno_code = errno;
138 else
139 ierr.errno_code = 0;
140 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
141 if (ret != -1) {
142 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
143 getprogname(), err->code, err->msg, strerror(errno));
144 return;
147 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
148 if (poll_err) {
149 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
150 getprogname(), err->code, err->msg, poll_err->msg);
151 return;
154 ret = imsg_flush(ibuf);
155 if (ret == -1) {
156 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
157 getprogname(), err->code, err->msg, strerror(errno));
158 return;
162 static const struct got_error *
163 flush_imsg(struct imsgbuf *ibuf)
165 const struct got_error *err;
167 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
168 if (err)
169 return err;
171 if (imsg_flush(ibuf) == -1)
172 return got_error_from_errno();
174 return NULL;
177 const struct got_error *
178 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
180 struct got_imsg_object iobj;
182 iobj.type = obj->type;
183 iobj.flags = obj->flags;
184 iobj.hdrlen = obj->hdrlen;
185 iobj.size = obj->size;
186 iobj.ndeltas = ndeltas;
188 if (ndeltas > 0) {
189 /* TODO: Handle deltas */
192 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
193 == -1)
194 return got_error_from_errno();
196 return flush_imsg(ibuf);
199 const struct got_error *
200 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
202 const struct got_error *err = NULL;
203 struct imsg imsg;
204 struct got_imsg_object iobj;
205 size_t datalen;
206 int i;
207 const size_t min_datalen =
208 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
210 *obj = NULL;
212 err = recv_one_imsg(&imsg, ibuf, min_datalen);
213 if (err)
214 return err;
216 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
218 switch (imsg.hdr.type) {
219 case GOT_IMSG_ERROR:
220 err = recv_imsg_error(&imsg, datalen);
221 break;
222 case GOT_IMSG_OBJECT:
223 if (datalen != sizeof(iobj)) {
224 err = got_error(GOT_ERR_PRIVSEP_LEN);
225 break;
228 memcpy(&iobj, imsg.data, sizeof(iobj));
229 if (iobj.ndeltas < 0 ||
230 iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
231 err = got_error(GOT_ERR_PRIVSEP_LEN);
232 break;
235 *obj = calloc(1, sizeof(**obj));
236 if (*obj == NULL) {
237 err = got_error_from_errno();
238 break;
241 (*obj)->type = iobj.type;
242 (*obj)->hdrlen = iobj.hdrlen;
243 (*obj)->size = iobj.size;
244 for (i = 0; i < iobj.ndeltas; i++) {
245 /* TODO: Handle deltas */
247 break;
248 default:
249 err = got_error(GOT_ERR_PRIVSEP_MSG);
250 break;
253 imsg_free(&imsg);
255 return err;
258 const struct got_error *
259 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
261 const struct got_error *err = NULL;
262 struct got_imsg_commit_object icommit;
263 uint8_t *buf;
264 size_t len, total;
265 struct got_object_qid *qid;
267 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
268 icommit.author_len = strlen(commit->author);
269 icommit.author_time = commit->author_time;
270 icommit.author_tzoff_len = strlen(commit->author_tzoff);
271 icommit.committer_len = strlen(commit->committer);
272 icommit.committer_time = commit->committer_time;
273 icommit.committer_tzoff_len = strlen(commit->committer_tzoff);
274 icommit.logmsg_len = strlen(commit->logmsg);
275 icommit.nparents = commit->nparents;
277 total = sizeof(icommit) + icommit.author_len +
278 icommit.author_tzoff_len + icommit.committer_len +
279 icommit.committer_tzoff_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);
285 buf = malloc(total);
286 if (buf == NULL)
287 return got_error_from_errno();
289 len = 0;
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->author_tzoff, icommit.author_tzoff_len);
295 len += icommit.author_tzoff_len;
296 memcpy(buf + len, commit->committer, icommit.committer_len);
297 len += icommit.committer_len;
298 memcpy(buf + len, commit->committer_tzoff, icommit.committer_tzoff_len);
299 len += icommit.committer_tzoff_len;
300 memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
301 len += icommit.logmsg_len;
302 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
303 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
304 len += SHA1_DIGEST_LENGTH;
307 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
308 err = got_error_from_errno();
309 goto done;
312 err = flush_imsg(ibuf);
313 done:
314 free(buf);
315 return err;
317 const struct got_error *
318 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
320 const struct got_error *err = NULL;
321 struct imsg imsg;
322 struct got_imsg_commit_object icommit;
323 size_t len, datalen;
324 int i;
325 const size_t min_datalen =
326 MIN(sizeof(struct got_imsg_error),
327 sizeof(struct got_imsg_commit_object));
328 uint8_t *data;
330 *commit = NULL;
332 err = recv_one_imsg(&imsg, ibuf, min_datalen);
333 if (err)
334 return err;
336 data = imsg.data;
337 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
338 len = 0;
340 switch (imsg.hdr.type) {
341 case GOT_IMSG_ERROR:
342 err = recv_imsg_error(&imsg, datalen);
343 break;
344 case GOT_IMSG_COMMIT:
345 if (datalen < sizeof(icommit)) {
346 err = got_error(GOT_ERR_PRIVSEP_LEN);
347 break;
350 memcpy(&icommit, data, sizeof(icommit));
351 if (datalen != sizeof(icommit) + icommit.author_len +
352 icommit.author_tzoff_len + icommit.committer_len +
353 icommit.committer_tzoff_len + icommit.logmsg_len +
354 icommit.nparents * SHA1_DIGEST_LENGTH) {
355 err = got_error(GOT_ERR_PRIVSEP_LEN);
356 break;
358 if (icommit.nparents < 0) {
359 err = got_error(GOT_ERR_PRIVSEP_LEN);
360 break;
362 len += sizeof(icommit);
364 *commit = got_object_commit_alloc_partial();
365 if (*commit == NULL) {
366 err = got_error_from_errno();
367 break;
370 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
371 SHA1_DIGEST_LENGTH);
373 if (icommit.author_len == 0) {
374 (*commit)->author = strdup("");
375 if ((*commit)->author == NULL) {
376 err = got_error_from_errno();
377 break;
379 (*commit)->author_time = 0;
380 } else {
381 (*commit)->author = malloc(icommit.author_len + 1);
382 if ((*commit)->author == NULL) {
383 err = got_error_from_errno();
384 break;
386 memcpy((*commit)->author, data + len,
387 icommit.author_len);
388 (*commit)->author[icommit.author_len] = '\0';
390 len += icommit.author_len;
392 (*commit)->author_time = icommit.author_time;
393 if (icommit.author_tzoff_len == 0) {
394 (*commit)->author_tzoff = strdup("");
395 if ((*commit)->author_tzoff == NULL) {
396 err = got_error_from_errno();
397 break;
399 } else {
400 (*commit)->author_tzoff =
401 malloc(icommit.author_tzoff_len + 1);
402 if ((*commit)->author_tzoff == NULL) {
403 err = got_error_from_errno();
404 break;
406 memcpy((*commit)->author_tzoff, data + len,
407 icommit.author_tzoff_len);
408 (*commit)->author_tzoff[icommit.author_tzoff_len] =
409 '\0';
411 len += icommit.author_tzoff_len;
413 if (icommit.committer_len == 0) {
414 (*commit)->committer = strdup("");
415 if ((*commit)->committer == NULL) {
416 err = got_error_from_errno();
417 break;
419 (*commit)->committer_time = 0;
420 } else {
421 (*commit)->committer =
422 malloc(icommit.committer_len + 1);
423 if ((*commit)->committer == NULL) {
424 err = got_error_from_errno();
425 break;
427 memcpy((*commit)->committer, data + len,
428 icommit.committer_len);
429 (*commit)->committer[icommit.committer_len] = '\0';
431 len += icommit.committer_len;
433 (*commit)->committer_time = icommit.committer_time;
434 if (icommit.committer_tzoff_len == 0) {
435 (*commit)->committer_tzoff = strdup("");
436 if ((*commit)->committer_tzoff == NULL) {
437 err = got_error_from_errno();
438 break;
440 } else {
441 (*commit)->committer_tzoff =
442 malloc(icommit.committer_tzoff_len + 1);
443 if ((*commit)->committer_tzoff == NULL) {
444 err = got_error_from_errno();
445 break;
447 memcpy((*commit)->committer_tzoff, data + len,
448 icommit.committer_tzoff_len);
449 (*commit)->committer_tzoff[icommit.committer_tzoff_len]
450 = '\0';
452 len += icommit.committer_tzoff_len;
454 if (icommit.logmsg_len == 0) {
455 (*commit)->logmsg = strdup("");
456 if ((*commit)->logmsg == NULL) {
457 err = got_error_from_errno();
458 break;
460 } else {
461 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
462 if ((*commit)->logmsg == NULL) {
463 err = got_error_from_errno();
464 break;
466 memcpy((*commit)->logmsg, data + len,
467 icommit.logmsg_len);
468 (*commit)->logmsg[icommit.logmsg_len] = '\0';
470 len += icommit.logmsg_len;
472 for (i = 0; i < icommit.nparents; i++) {
473 struct got_object_qid *qid;
475 qid = calloc(1, sizeof(*qid));
476 if (qid == NULL) {
477 err = got_error_from_errno();
478 break;
480 qid->id = calloc(1, sizeof(*qid->id));
481 if (qid->id == NULL) {
482 err = got_error_from_errno();
483 free(qid);
484 break;
487 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
488 sizeof(*qid->id));
489 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
490 (*commit)->nparents++;
492 break;
493 default:
494 err = got_error(GOT_ERR_PRIVSEP_MSG);
495 break;
498 imsg_free(&imsg);
500 return err;
503 const struct got_error *
504 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
506 const struct got_error *err = NULL;
507 struct got_imsg_tree_object itree;
508 struct got_tree_entry *te;
510 itree.nentries = tree->nentries;
511 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
512 == -1)
513 return got_error_from_errno();
515 err = flush_imsg(ibuf);
516 if (err)
517 return err;
519 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
520 struct got_imsg_tree_entry ite;
521 uint8_t *buf = NULL;
522 size_t len = sizeof(ite) + strlen(te->name);
524 if (len > MAX_IMSGSIZE)
525 return got_error(GOT_ERR_NO_SPACE);
527 buf = malloc(len);
528 if (buf == NULL)
529 return got_error_from_errno();
531 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
532 ite.mode = te->mode;
533 memcpy(buf, &ite, sizeof(ite));
534 memcpy(buf + sizeof(ite), te->name, strlen(te->name));
536 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
537 buf, len) == -1)
538 err = got_error_from_errno();
539 free(buf);
540 if (err)
541 return err;
543 err = flush_imsg(ibuf);
544 if (err)
545 return err;
548 return NULL;
551 const struct got_error *
552 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
554 const struct got_error *err = NULL;
555 const size_t min_datalen =
556 MIN(sizeof(struct got_imsg_error),
557 sizeof(struct got_imsg_tree_object));
558 struct got_imsg_tree_object itree = { 0 };
559 int nentries = 0;
561 *tree = NULL;
562 get_more:
563 err = read_imsg(ibuf);
564 if (err)
565 goto done;
567 while (1) {
568 struct imsg imsg;
569 size_t n;
570 size_t datalen;
571 struct got_imsg_tree_entry ite;
572 struct got_tree_entry *te = NULL;
574 n = imsg_get(ibuf, &imsg);
575 if (n == 0) {
576 if (*tree && (*tree)->nentries != nentries)
577 goto get_more;
578 break;
581 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
582 return got_error(GOT_ERR_PRIVSEP_LEN);
584 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
586 switch (imsg.hdr.type) {
587 case GOT_IMSG_ERROR:
588 err = recv_imsg_error(&imsg, datalen);
589 break;
590 case GOT_IMSG_TREE:
591 /* This message should only appear once. */
592 if (*tree != NULL) {
593 err = got_error(GOT_ERR_PRIVSEP_MSG);
594 break;
596 if (datalen != sizeof(itree)) {
597 err = got_error(GOT_ERR_PRIVSEP_LEN);
598 break;
600 memcpy(&itree, imsg.data, sizeof(itree));
601 *tree = calloc(1, sizeof(**tree));
602 if (*tree == NULL) {
603 err = got_error_from_errno();
604 break;
606 (*tree)->nentries = itree.nentries;
607 SIMPLEQ_INIT(&(*tree)->entries);
608 break;
609 case GOT_IMSG_TREE_ENTRY:
610 /* This message should be preceeded by GOT_IMSG_TREE. */
611 if (*tree == NULL) {
612 err = got_error(GOT_ERR_PRIVSEP_MSG);
613 break;
615 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
616 err = got_error(GOT_ERR_PRIVSEP_LEN);
617 break;
620 /* Remaining data contains the entry's name. */
621 datalen -= sizeof(ite);
622 memcpy(&ite, imsg.data, sizeof(ite));
623 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
624 err = got_error(GOT_ERR_PRIVSEP_LEN);
625 break;
628 te = got_alloc_tree_entry_partial();
629 if (te == NULL) {
630 err = got_error_from_errno();
631 break;
633 te->name = malloc(datalen + 1);
634 if (te->name == NULL) {
635 free(te);
636 err = got_error_from_errno();
637 break;
639 memcpy(te->name, imsg.data + sizeof(ite), datalen);
640 te->name[datalen] = '\0';
642 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
643 te->mode = ite.mode;
644 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
645 nentries++;
646 break;
647 default:
648 err = got_error(GOT_ERR_PRIVSEP_MSG);
649 break;
652 imsg_free(&imsg);
654 done:
655 if (*tree && (*tree)->nentries != nentries) {
656 if (err == NULL)
657 err = got_error(GOT_ERR_PRIVSEP_LEN);
658 got_object_tree_close(*tree);
659 *tree = NULL;
662 return err;
665 const struct got_error *
666 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
668 struct got_imsg_blob iblob;
670 iblob.size = size;
671 /* Data has already been written to file descriptor. */
673 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
674 == -1)
675 return got_error_from_errno();
677 return flush_imsg(ibuf);
680 const struct got_error *
681 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
683 const struct got_error *err = NULL;
684 struct imsg imsg;
685 struct got_imsg_blob iblob;
686 size_t datalen;
688 err = recv_one_imsg(&imsg, ibuf, 0);
689 if (err)
690 return err;
692 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
694 switch (imsg.hdr.type) {
695 case GOT_IMSG_ERROR:
696 err = recv_imsg_error(&imsg, datalen);
697 break;
698 case GOT_IMSG_BLOB:
699 if (datalen != sizeof(iblob))
700 err = got_error(GOT_ERR_PRIVSEP_LEN);
701 memcpy(&iblob, imsg.data, sizeof(iblob));
702 *size = iblob.size;
703 /* Data has been written to file descriptor. */
704 break;
705 default:
706 err = got_error(GOT_ERR_PRIVSEP_MSG);
707 break;
710 imsg_free(&imsg);
712 return err;