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_object_parse.h"
40 #include "got_lib_privsep.h"
42 #ifndef MIN
43 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
44 #endif
46 static const struct got_error *
47 poll_fd(int fd, int events, int timeout)
48 {
49 struct pollfd pfd[1];
50 int n;
52 pfd[0].fd = fd;
53 pfd[0].events = events;
55 n = poll(pfd, 1, timeout);
56 if (n == -1)
57 return got_error_from_errno();
58 if (n == 0)
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))
63 return NULL;
65 return got_error(GOT_ERR_INTERRUPT);
66 }
68 static const struct got_error *
69 read_imsg(struct imsgbuf *ibuf)
70 {
71 const struct got_error *err;
72 size_t n;
74 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
75 if (err)
76 return err;
78 n = imsg_read(ibuf);
79 if (n == -1) {
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);
83 }
84 if (n == 0)
85 return got_error(GOT_ERR_PRIVSEP_PIPE);
87 return NULL;
88 }
90 static const struct got_error *
91 recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
92 {
93 const struct got_error *err;
94 ssize_t n;
96 err = read_imsg(ibuf);
97 if (err)
98 return err;
100 n = imsg_get(ibuf, imsg);
101 if (n == 0)
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);
107 return NULL;
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);
123 return &serr;
126 return got_error(ierr.code);
129 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
130 void
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;
135 int ret;
137 ierr.code = err->code;
138 if (err->code == GOT_ERR_ERRNO)
139 ierr.errno_code = errno;
140 else
141 ierr.errno_code = 0;
142 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
143 if (ret != -1) {
144 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
145 getprogname(), err->code, err->msg, strerror(errno));
146 return;
149 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
150 if (poll_err) {
151 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
152 getprogname(), err->code, err->msg, poll_err->msg);
153 return;
156 ret = imsg_flush(ibuf);
157 if (ret == -1) {
158 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
159 getprogname(), err->code, err->msg, strerror(errno));
160 return;
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);
170 if (err)
171 return err;
173 if (imsg_flush(ibuf) == -1)
174 return got_error_from_errno();
176 return NULL;
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;
190 if (ndeltas > 0) {
191 /* TODO: Handle deltas */
194 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
195 == -1)
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;
205 struct imsg imsg;
206 struct got_imsg_object iobj;
207 size_t datalen;
208 int i;
209 const size_t min_datalen =
210 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
212 *obj = NULL;
214 err = recv_one_imsg(&imsg, ibuf, min_datalen);
215 if (err)
216 return err;
218 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
220 switch (imsg.hdr.type) {
221 case GOT_IMSG_ERROR:
222 err = recv_imsg_error(&imsg, datalen);
223 break;
224 case GOT_IMSG_OBJECT:
225 if (datalen != sizeof(iobj)) {
226 err = got_error(GOT_ERR_PRIVSEP_LEN);
227 break;
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);
234 break;
237 *obj = calloc(1, sizeof(**obj));
238 if (*obj == NULL) {
239 err = got_error_from_errno();
240 break;
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 */
249 break;
250 default:
251 err = got_error(GOT_ERR_PRIVSEP_MSG);
252 break;
255 imsg_free(&imsg);
257 return err;
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;
265 uint8_t *buf;
266 size_t len, total;
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);
286 buf = malloc(total);
287 if (buf == NULL)
288 return got_error_from_errno();
290 len = 0;
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();
306 goto done;
309 err = flush_imsg(ibuf);
310 done:
311 free(buf);
312 return err;
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;
318 struct imsg imsg;
319 struct got_imsg_commit_object icommit;
320 size_t len, datalen;
321 int i;
322 const size_t min_datalen =
323 MIN(sizeof(struct got_imsg_error),
324 sizeof(struct got_imsg_commit_object));
325 uint8_t *data;
327 *commit = NULL;
329 err = recv_one_imsg(&imsg, ibuf, min_datalen);
330 if (err)
331 return err;
333 data = imsg.data;
334 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
335 len = 0;
337 switch (imsg.hdr.type) {
338 case GOT_IMSG_ERROR:
339 err = recv_imsg_error(&imsg, datalen);
340 break;
341 case GOT_IMSG_COMMIT:
342 if (datalen < sizeof(icommit)) {
343 err = got_error(GOT_ERR_PRIVSEP_LEN);
344 break;
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);
352 break;
354 if (icommit.nparents < 0) {
355 err = got_error(GOT_ERR_PRIVSEP_LEN);
356 break;
358 len += sizeof(icommit);
360 *commit = got_object_commit_alloc_partial();
361 if (*commit == NULL) {
362 err = got_error_from_errno();
363 break;
366 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
367 SHA1_DIGEST_LENGTH);
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();
377 break;
379 } else {
380 (*commit)->author = malloc(icommit.author_len + 1);
381 if ((*commit)->author == NULL) {
382 err = got_error_from_errno();
383 break;
385 memcpy((*commit)->author, data + len,
386 icommit.author_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();
395 break;
397 } else {
398 (*commit)->committer =
399 malloc(icommit.committer_len + 1);
400 if ((*commit)->committer == NULL) {
401 err = got_error_from_errno();
402 break;
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();
414 break;
416 } else {
417 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
418 if ((*commit)->logmsg == NULL) {
419 err = got_error_from_errno();
420 break;
422 memcpy((*commit)->logmsg, data + len,
423 icommit.logmsg_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));
432 if (qid == NULL) {
433 err = got_error_from_errno();
434 break;
436 qid->id = calloc(1, sizeof(*qid->id));
437 if (qid->id == NULL) {
438 err = got_error_from_errno();
439 free(qid);
440 break;
443 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
444 sizeof(*qid->id));
445 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
446 (*commit)->nparents++;
448 break;
449 default:
450 err = got_error(GOT_ERR_PRIVSEP_MSG);
451 break;
454 imsg_free(&imsg);
456 return err;
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))
468 == -1)
469 return got_error_from_errno();
471 err = flush_imsg(ibuf);
472 if (err)
473 return err;
475 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
476 struct got_imsg_tree_entry ite;
477 uint8_t *buf = NULL;
478 size_t len = sizeof(ite) + strlen(te->name);
480 if (len > MAX_IMSGSIZE)
481 return got_error(GOT_ERR_NO_SPACE);
483 buf = malloc(len);
484 if (buf == NULL)
485 return got_error_from_errno();
487 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
488 ite.mode = te->mode;
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,
493 buf, len) == -1)
494 err = got_error_from_errno();
495 free(buf);
496 if (err)
497 return err;
499 err = flush_imsg(ibuf);
500 if (err)
501 return err;
504 return NULL;
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 };
515 int nentries = 0;
517 *tree = NULL;
518 get_more:
519 err = read_imsg(ibuf);
520 if (err)
521 goto done;
523 while (1) {
524 struct imsg imsg;
525 size_t n;
526 size_t datalen;
527 struct got_imsg_tree_entry ite;
528 struct got_tree_entry *te = NULL;
530 n = imsg_get(ibuf, &imsg);
531 if (n == 0) {
532 if (*tree && (*tree)->entries.nentries != nentries)
533 goto get_more;
534 break;
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) {
543 case GOT_IMSG_ERROR:
544 err = recv_imsg_error(&imsg, datalen);
545 break;
546 case GOT_IMSG_TREE:
547 /* This message should only appear once. */
548 if (*tree != NULL) {
549 err = got_error(GOT_ERR_PRIVSEP_MSG);
550 break;
552 if (datalen != sizeof(itree)) {
553 err = got_error(GOT_ERR_PRIVSEP_LEN);
554 break;
556 memcpy(&itree, imsg.data, sizeof(itree));
557 *tree = calloc(1, sizeof(**tree));
558 if (*tree == NULL) {
559 err = got_error_from_errno();
560 break;
562 (*tree)->entries.nentries = itree.nentries;
563 SIMPLEQ_INIT(&(*tree)->entries.head);
564 break;
565 case GOT_IMSG_TREE_ENTRY:
566 /* This message should be preceeded by GOT_IMSG_TREE. */
567 if (*tree == NULL) {
568 err = got_error(GOT_ERR_PRIVSEP_MSG);
569 break;
571 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
572 err = got_error(GOT_ERR_PRIVSEP_LEN);
573 break;
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);
581 break;
584 te = got_alloc_tree_entry_partial();
585 if (te == NULL) {
586 err = got_error_from_errno();
587 break;
589 te->name = malloc(datalen + 1);
590 if (te->name == NULL) {
591 free(te);
592 err = got_error_from_errno();
593 break;
595 memcpy(te->name, imsg.data + sizeof(ite), datalen);
596 te->name[datalen] = '\0';
598 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
599 te->mode = ite.mode;
600 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
601 nentries++;
602 break;
603 default:
604 err = got_error(GOT_ERR_PRIVSEP_MSG);
605 break;
608 imsg_free(&imsg);
610 done:
611 if (*tree && (*tree)->entries.nentries != nentries) {
612 if (err == NULL)
613 err = got_error(GOT_ERR_PRIVSEP_LEN);
614 got_object_tree_close(*tree);
615 *tree = NULL;
618 return err;
621 const struct got_error *
622 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
624 struct got_imsg_blob iblob;
626 iblob.size = size;
627 /* Data has already been written to file descriptor. */
629 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
630 == -1)
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;
640 struct imsg imsg;
641 struct got_imsg_blob iblob;
642 size_t datalen;
644 err = recv_one_imsg(&imsg, ibuf, 0);
645 if (err)
646 return err;
648 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
650 switch (imsg.hdr.type) {
651 case GOT_IMSG_ERROR:
652 err = recv_imsg_error(&imsg, datalen);
653 break;
654 case GOT_IMSG_BLOB:
655 if (datalen != sizeof(iblob))
656 err = got_error(GOT_ERR_PRIVSEP_LEN);
657 memcpy(&iblob, imsg.data, sizeof(iblob));
658 *size = iblob.size;
659 /* Data has been written to file descriptor. */
660 break;
661 default:
662 err = got_error(GOT_ERR_PRIVSEP_MSG);
663 break;
666 imsg_free(&imsg);
668 return err;