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>
30 #include <time.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"
41 #ifndef MIN
42 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 #endif
45 static const struct got_error *
46 poll_fd(int fd, int events, int timeout)
47 {
48 struct pollfd pfd[1];
49 int n;
51 pfd[0].fd = fd;
52 pfd[0].events = events;
54 n = poll(pfd, 1, timeout);
55 if (n == -1)
56 return got_error_from_errno();
57 if (n == 0)
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))
62 return NULL;
64 return got_error(GOT_ERR_INTERRUPT);
65 }
67 static const struct got_error *
68 read_imsg(struct imsgbuf *ibuf)
69 {
70 const struct got_error *err;
71 size_t n;
73 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
74 if (err)
75 return err;
77 n = imsg_read(ibuf);
78 if (n == -1) {
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);
82 }
83 if (n == 0)
84 return got_error(GOT_ERR_PRIVSEP_PIPE);
86 return NULL;
87 }
89 static const struct got_error *
90 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
91 {
92 const struct got_error *err;
93 ssize_t n;
95 err = read_imsg(ibuf);
96 if (err)
97 return err;
99 n = imsg_get(ibuf, imsg);
100 if (n == 0)
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);
106 return NULL;
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);
122 return &serr;
125 return got_error(ierr.code);
128 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
129 void
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;
134 int ret;
136 ierr.code = err->code;
137 if (err->code == GOT_ERR_ERRNO)
138 ierr.errno_code = errno;
139 else
140 ierr.errno_code = 0;
141 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
142 if (ret != -1) {
143 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
144 getprogname(), err->code, err->msg, strerror(errno));
145 return;
148 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
149 if (poll_err) {
150 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
151 getprogname(), err->code, err->msg, poll_err->msg);
152 return;
155 ret = imsg_flush(ibuf);
156 if (ret == -1) {
157 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
158 getprogname(), err->code, err->msg, strerror(errno));
159 return;
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);
169 if (err)
170 return err;
172 if (imsg_flush(ibuf) == -1)
173 return got_error_from_errno();
175 return NULL;
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;
189 if (ndeltas > 0) {
190 /* TODO: Handle deltas */
193 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
194 == -1)
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;
204 struct imsg imsg;
205 struct got_imsg_object iobj;
206 size_t datalen;
207 int i;
208 const size_t min_datalen =
209 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
211 *obj = NULL;
213 err = recv_one_imsg(&imsg, ibuf, min_datalen);
214 if (err)
215 return err;
217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
219 switch (imsg.hdr.type) {
220 case GOT_IMSG_ERROR:
221 err = recv_imsg_error(&imsg, datalen);
222 break;
223 case GOT_IMSG_OBJECT:
224 if (datalen != sizeof(iobj)) {
225 err = got_error(GOT_ERR_PRIVSEP_LEN);
226 break;
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);
233 break;
236 *obj = calloc(1, sizeof(**obj));
237 if (*obj == NULL) {
238 err = got_error_from_errno();
239 break;
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 */
248 break;
249 default:
250 err = got_error(GOT_ERR_PRIVSEP_MSG);
251 break;
254 imsg_free(&imsg);
256 return err;
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;
264 uint8_t *buf;
265 size_t len, total;
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);
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->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();
305 goto done;
308 err = flush_imsg(ibuf);
309 done:
310 free(buf);
311 return err;
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;
317 struct imsg imsg;
318 struct got_imsg_commit_object icommit;
319 size_t len, datalen;
320 int i;
321 const size_t min_datalen =
322 MIN(sizeof(struct got_imsg_error),
323 sizeof(struct got_imsg_commit_object));
324 uint8_t *data;
326 *commit = NULL;
328 err = recv_one_imsg(&imsg, ibuf, min_datalen);
329 if (err)
330 return err;
332 data = imsg.data;
333 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
334 len = 0;
336 switch (imsg.hdr.type) {
337 case GOT_IMSG_ERROR:
338 err = recv_imsg_error(&imsg, datalen);
339 break;
340 case GOT_IMSG_COMMIT:
341 if (datalen < sizeof(icommit)) {
342 err = got_error(GOT_ERR_PRIVSEP_LEN);
343 break;
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);
351 break;
353 if (icommit.nparents < 0) {
354 err = got_error(GOT_ERR_PRIVSEP_LEN);
355 break;
357 len += sizeof(icommit);
359 *commit = got_object_commit_alloc_partial();
360 if (*commit == NULL) {
361 err = got_error_from_errno();
362 break;
365 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
366 SHA1_DIGEST_LENGTH);
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();
376 break;
378 } else {
379 (*commit)->author = malloc(icommit.author_len + 1);
380 if ((*commit)->author == NULL) {
381 err = got_error_from_errno();
382 break;
384 memcpy((*commit)->author, data + len,
385 icommit.author_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();
394 break;
396 } else {
397 (*commit)->committer =
398 malloc(icommit.committer_len + 1);
399 if ((*commit)->committer == NULL) {
400 err = got_error_from_errno();
401 break;
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();
413 break;
415 } else {
416 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
417 if ((*commit)->logmsg == NULL) {
418 err = got_error_from_errno();
419 break;
421 memcpy((*commit)->logmsg, data + len,
422 icommit.logmsg_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));
431 if (qid == NULL) {
432 err = got_error_from_errno();
433 break;
435 qid->id = calloc(1, sizeof(*qid->id));
436 if (qid->id == NULL) {
437 err = got_error_from_errno();
438 free(qid);
439 break;
442 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
443 sizeof(*qid->id));
444 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
445 (*commit)->nparents++;
447 break;
448 default:
449 err = got_error(GOT_ERR_PRIVSEP_MSG);
450 break;
453 imsg_free(&imsg);
455 return err;
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))
467 == -1)
468 return got_error_from_errno();
470 err = flush_imsg(ibuf);
471 if (err)
472 return err;
474 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
475 struct got_imsg_tree_entry ite;
476 uint8_t *buf = NULL;
477 size_t len = sizeof(ite) + strlen(te->name);
479 if (len > MAX_IMSGSIZE)
480 return got_error(GOT_ERR_NO_SPACE);
482 buf = malloc(len);
483 if (buf == NULL)
484 return got_error_from_errno();
486 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
487 ite.mode = te->mode;
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,
492 buf, len) == -1)
493 err = got_error_from_errno();
494 free(buf);
495 if (err)
496 return err;
498 err = flush_imsg(ibuf);
499 if (err)
500 return err;
503 return NULL;
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 };
514 int nentries = 0;
516 *tree = NULL;
517 get_more:
518 err = read_imsg(ibuf);
519 if (err)
520 goto done;
522 while (1) {
523 struct imsg imsg;
524 size_t n;
525 size_t datalen;
526 struct got_imsg_tree_entry ite;
527 struct got_tree_entry *te = NULL;
529 n = imsg_get(ibuf, &imsg);
530 if (n == 0) {
531 if (*tree && (*tree)->entries.nentries != nentries)
532 goto get_more;
533 break;
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) {
542 case GOT_IMSG_ERROR:
543 err = recv_imsg_error(&imsg, datalen);
544 break;
545 case GOT_IMSG_TREE:
546 /* This message should only appear once. */
547 if (*tree != NULL) {
548 err = got_error(GOT_ERR_PRIVSEP_MSG);
549 break;
551 if (datalen != sizeof(itree)) {
552 err = got_error(GOT_ERR_PRIVSEP_LEN);
553 break;
555 memcpy(&itree, imsg.data, sizeof(itree));
556 *tree = calloc(1, sizeof(**tree));
557 if (*tree == NULL) {
558 err = got_error_from_errno();
559 break;
561 (*tree)->entries.nentries = itree.nentries;
562 SIMPLEQ_INIT(&(*tree)->entries.head);
563 break;
564 case GOT_IMSG_TREE_ENTRY:
565 /* This message should be preceeded by GOT_IMSG_TREE. */
566 if (*tree == NULL) {
567 err = got_error(GOT_ERR_PRIVSEP_MSG);
568 break;
570 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
571 err = got_error(GOT_ERR_PRIVSEP_LEN);
572 break;
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);
580 break;
583 te = got_alloc_tree_entry_partial();
584 if (te == NULL) {
585 err = got_error_from_errno();
586 break;
588 te->name = malloc(datalen + 1);
589 if (te->name == NULL) {
590 free(te);
591 err = got_error_from_errno();
592 break;
594 memcpy(te->name, imsg.data + sizeof(ite), datalen);
595 te->name[datalen] = '\0';
597 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
598 te->mode = ite.mode;
599 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
600 nentries++;
601 break;
602 default:
603 err = got_error(GOT_ERR_PRIVSEP_MSG);
604 break;
607 imsg_free(&imsg);
609 done:
610 if (*tree && (*tree)->entries.nentries != nentries) {
611 if (err == NULL)
612 err = got_error(GOT_ERR_PRIVSEP_LEN);
613 got_object_tree_close(*tree);
614 *tree = NULL;
617 return err;
620 const struct got_error *
621 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
623 struct got_imsg_blob iblob;
625 iblob.size = size;
626 /* Data has already been written to file descriptor. */
628 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
629 == -1)
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;
639 struct imsg imsg;
640 struct got_imsg_blob iblob;
641 size_t datalen;
643 err = recv_one_imsg(&imsg, ibuf, 0);
644 if (err)
645 return err;
647 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
649 switch (imsg.hdr.type) {
650 case GOT_IMSG_ERROR:
651 err = recv_imsg_error(&imsg, datalen);
652 break;
653 case GOT_IMSG_BLOB:
654 if (datalen != sizeof(iblob))
655 err = got_error(GOT_ERR_PRIVSEP_LEN);
656 memcpy(&iblob, imsg.data, sizeof(iblob));
657 *size = iblob.size;
658 /* Data has been written to file descriptor. */
659 break;
660 default:
661 err = got_error(GOT_ERR_PRIVSEP_MSG);
662 break;
665 imsg_free(&imsg);
667 return err;