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/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <imsg.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
39 #include "got_opentemp.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_pack.h"
44 #include "got_lib_path.h"
45 #include "got_lib_zbuf.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 #define GOT_OBJ_TAG_COMMIT "commit"
58 #define GOT_OBJ_TAG_TREE "tree"
59 #define GOT_OBJ_TAG_BLOB "blob"
61 #define GOT_COMMIT_TAG_TREE "tree "
62 #define GOT_COMMIT_TAG_PARENT "parent "
63 #define GOT_COMMIT_TAG_AUTHOR "author "
64 #define GOT_COMMIT_TAG_COMMITTER "committer "
66 const struct got_error *
67 got_object_id_str(char **outbuf, struct got_object_id *id)
68 {
69 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
71 *outbuf = calloc(1, len);
72 if (*outbuf == NULL)
73 return got_error_from_errno();
75 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
76 free(*outbuf);
77 *outbuf = NULL;
78 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
79 }
81 return NULL;
82 }
84 int
85 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
86 {
87 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
88 }
90 struct got_object_id *
91 got_object_id_dup(struct got_object_id *id1)
92 {
93 struct got_object_id *id2;
95 id2 = malloc(sizeof(*id2));
96 if (id2 == NULL)
97 return NULL;
98 memcpy(id2, id1, sizeof(*id2));
99 return id2;
102 struct got_object_id *
103 got_object_get_id(struct got_object *obj)
105 return got_object_id_dup(&obj->id);
108 const struct got_error *
109 got_object_get_id_str(char **outbuf, struct got_object *obj)
111 return got_object_id_str(outbuf, &obj->id);
114 int
115 got_object_get_type(struct got_object *obj)
117 switch (obj->type) {
118 case GOT_OBJ_TYPE_COMMIT:
119 case GOT_OBJ_TYPE_TREE:
120 case GOT_OBJ_TYPE_BLOB:
121 case GOT_OBJ_TYPE_TAG:
122 return obj->type;
123 default:
124 abort();
125 break;
128 /* not reached */
129 return 0;
132 static const struct got_error *
133 parse_object_header(struct got_object **obj, char *buf, size_t len)
135 const char *obj_tags[] = {
136 GOT_OBJ_TAG_COMMIT,
137 GOT_OBJ_TAG_TREE,
138 GOT_OBJ_TAG_BLOB
139 };
140 const int obj_types[] = {
141 GOT_OBJ_TYPE_COMMIT,
142 GOT_OBJ_TYPE_TREE,
143 GOT_OBJ_TYPE_BLOB,
144 };
145 int type = 0;
146 size_t size = 0, hdrlen = 0;
147 int i;
148 char *p = strchr(buf, '\0');
150 if (p == NULL)
151 return got_error(GOT_ERR_BAD_OBJ_HDR);
153 hdrlen = strlen(buf) + 1 /* '\0' */;
155 for (i = 0; i < nitems(obj_tags); i++) {
156 const char *tag = obj_tags[i];
157 size_t tlen = strlen(tag);
158 const char *errstr;
160 if (strncmp(buf, tag, tlen) != 0)
161 continue;
163 type = obj_types[i];
164 if (len <= tlen)
165 return got_error(GOT_ERR_BAD_OBJ_HDR);
166 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
167 if (errstr != NULL)
168 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 break;
172 if (type == 0)
173 return got_error(GOT_ERR_BAD_OBJ_HDR);
175 *obj = calloc(1, sizeof(**obj));
176 if (*obj == NULL)
177 return got_error_from_errno();
178 (*obj)->type = type;
179 (*obj)->hdrlen = hdrlen;
180 (*obj)->size = size;
181 return NULL;
184 static const struct got_error *
185 read_object_header(struct got_object **obj, FILE *f)
187 const struct got_error *err;
188 struct got_zstream_buf zb;
189 char *buf;
190 const size_t zbsize = 64;
191 size_t outlen, totlen;
192 int i;
194 buf = calloc(zbsize, sizeof(char));
195 if (buf == NULL)
196 return got_error_from_errno();
198 err = got_inflate_init(&zb, NULL, zbsize);
199 if (err)
200 return err;
202 i = 0;
203 totlen = 0;
204 do {
205 err = got_inflate_read(&zb, f, &outlen);
206 if (err)
207 goto done;
208 if (strchr(zb.outbuf, '\0') == NULL) {
209 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
210 if (buf == NULL) {
211 err = got_error_from_errno();
212 goto done;
215 memcpy(buf + totlen, zb.outbuf, outlen);
216 totlen += outlen;
217 i++;
218 } while (strchr(zb.outbuf, '\0') == NULL);
220 err = parse_object_header(obj, buf, totlen);
221 done:
222 got_inflate_end(&zb);
223 return err;
226 static void
227 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
229 const struct got_error *err = NULL;
230 struct got_object *obj = NULL;
231 struct imsgbuf ibuf;
232 FILE *f = NULL;
233 int status = 0;
235 setproctitle("read object header");
236 close(imsg_fds[0]);
237 imsg_init(&ibuf, imsg_fds[1]);
239 /* revoke access to most system calls */
240 if (pledge("stdio", NULL) == -1) {
241 err = got_error_from_errno();
242 goto done;
245 f = fdopen(obj_fd, "rb");
246 if (f == NULL) {
247 err = got_error_from_errno();
248 close(obj_fd);
249 goto done;
252 err = read_object_header(&obj, f);
253 if (err)
254 goto done;
256 err = got_privsep_send_obj(&ibuf, obj, 0);
257 done:
258 if (obj)
259 got_object_close(obj);
260 if (err) {
261 got_privsep_send_error(&ibuf, err);
262 status = 1;
264 if (f)
265 fclose(f);
266 imsg_clear(&ibuf);
267 close(imsg_fds[1]);
268 _exit(status);
271 static const struct got_error *
272 wait_for_child(pid_t pid)
274 int child_status;
276 waitpid(pid, &child_status, 0);
278 if (!WIFEXITED(child_status))
279 return got_error(GOT_ERR_PRIVSEP_DIED);
281 if (WEXITSTATUS(child_status) != 0)
282 return got_error(GOT_ERR_PRIVSEP_EXIT);
284 return NULL;
287 static const struct got_error *
288 read_object_header_privsep(struct got_object **obj, int fd)
290 struct imsgbuf parent_ibuf;
291 int imsg_fds[2];
292 const struct got_error *err = NULL, *err_child = NULL;
293 pid_t pid;
295 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
296 return got_error_from_errno();
298 pid = fork();
299 if (pid == -1)
300 return got_error_from_errno();
301 else if (pid == 0) {
302 read_object_header_privsep_child(fd, imsg_fds);
303 /* not reached */
306 close(imsg_fds[1]);
307 imsg_init(&parent_ibuf, imsg_fds[0]);
308 err = got_privsep_recv_obj(obj, &parent_ibuf);
309 imsg_clear(&parent_ibuf);
310 err_child = wait_for_child(pid);
311 close(imsg_fds[0]);
312 return err ? err : err_child;
315 static const struct got_error *
316 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
318 const struct got_error *err = NULL;
319 char *hex;
320 char *path_objects = got_repo_get_path_objects(repo);
322 *path = NULL;
324 if (path_objects == NULL)
325 return got_error_from_errno();
327 err = got_object_id_str(&hex, id);
328 if (err)
329 return err;
331 if (asprintf(path, "%s/%.2x/%s", path_objects,
332 id->sha1[0], hex + 2) == -1)
333 err = got_error_from_errno();
335 free(hex);
336 free(path_objects);
337 return err;
340 static const struct got_error *
341 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
343 const struct got_error *err = NULL;
344 char *path;
346 err = object_path(&path, &obj->id, repo);
347 if (err)
348 return err;
349 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
350 if (*fd == -1) {
351 err = got_error_from_errno();
352 goto done;
354 done:
355 free(path);
356 return err;
359 const struct got_error *
360 got_object_open(struct got_object **obj, struct got_repository *repo,
361 struct got_object_id *id)
363 const struct got_error *err = NULL;
364 char *path;
365 int fd;
367 err = object_path(&path, id, repo);
368 if (err)
369 return err;
371 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
372 if (fd == -1) {
373 if (errno != ENOENT) {
374 err = got_error_from_errno();
375 goto done;
377 err = got_packfile_open_object(obj, id, repo);
378 if (err)
379 goto done;
380 if (*obj == NULL)
381 err = got_error(GOT_ERR_NO_OBJ);
382 } else {
383 err = read_object_header_privsep(obj, fd);
384 if (err)
385 goto done;
386 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
388 done:
389 free(path);
390 if (fd != -1)
391 close(fd);
392 return err;
396 const struct got_error *
397 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
398 const char *id_str)
400 struct got_object_id id;
402 if (!got_parse_sha1_digest(id.sha1, id_str))
403 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
405 return got_object_open(obj, repo, &id);
408 void
409 got_object_close(struct got_object *obj)
411 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
412 struct got_delta *delta;
413 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
414 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
415 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
416 got_delta_close(delta);
419 if (obj->flags & GOT_OBJ_FLAG_PACKED)
420 free(obj->path_packfile);
421 free(obj);
424 struct got_commit_object *
425 got_object_commit_alloc_partial(void)
427 struct got_commit_object *commit;
429 commit = calloc(1, sizeof(*commit));
430 if (commit == NULL)
431 return NULL;
432 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
433 if (commit->tree_id == NULL) {
434 free(commit);
435 return NULL;
438 SIMPLEQ_INIT(&commit->parent_ids);
440 return commit;
443 const struct got_error *
444 got_object_commit_add_parent(struct got_commit_object *commit,
445 const char *id_str)
447 const struct got_error *err = NULL;
448 struct got_object_qid *qid;
450 qid = calloc(1, sizeof(*qid));
451 if (qid == NULL)
452 return got_error_from_errno();
454 qid->id = calloc(1, sizeof(*qid->id));
455 if (qid->id == NULL) {
456 err = got_error_from_errno();
457 free(qid);
458 return err;
461 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
462 err = got_error(GOT_ERR_BAD_OBJ_DATA);
463 free(qid->id);
464 free(qid);
465 return err;
468 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
469 commit->nparents++;
471 return NULL;
474 static const struct got_error *
475 parse_commit_time(time_t *time, char **tzoff, char *committer)
477 const char *errstr;
478 char *space;
480 *time = 0;
482 /* Parse and then strip trailing timezone indicator. */
483 space = strrchr(committer, ' ');
484 if (space == NULL)
485 return got_error(GOT_ERR_BAD_OBJ_DATA);
486 *tzoff = strdup(space + 1);
487 if (*tzoff == NULL)
488 return got_error_from_errno();
489 *space = '\0';
491 /* Timestamp is separated from committer name + email by space. */
492 space = strrchr(committer, ' ');
493 if (space == NULL)
494 return got_error(GOT_ERR_BAD_OBJ_DATA);
496 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
497 if (errstr)
498 return got_error(GOT_ERR_BAD_OBJ_DATA);
500 /* Strip off parsed time information, leaving just author and email. */
501 *space = '\0';
502 return NULL;
505 static const struct got_error *
506 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
508 const struct got_error *err = NULL;
509 char *s = buf;
510 size_t tlen;
511 ssize_t remain = (ssize_t)len;
513 *commit = got_object_commit_alloc_partial();
514 if (*commit == NULL)
515 return got_error_from_errno();
517 tlen = strlen(GOT_COMMIT_TAG_TREE);
518 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
519 remain -= tlen;
520 if (remain < SHA1_DIGEST_STRING_LENGTH) {
521 err = got_error(GOT_ERR_BAD_OBJ_DATA);
522 goto done;
524 s += tlen;
525 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
526 err = got_error(GOT_ERR_BAD_OBJ_DATA);
527 goto done;
529 remain -= SHA1_DIGEST_STRING_LENGTH;
530 s += SHA1_DIGEST_STRING_LENGTH;
531 } else {
532 err = got_error(GOT_ERR_BAD_OBJ_DATA);
533 goto done;
536 tlen = strlen(GOT_COMMIT_TAG_PARENT);
537 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
538 remain -= tlen;
539 if (remain < SHA1_DIGEST_STRING_LENGTH) {
540 err = got_error(GOT_ERR_BAD_OBJ_DATA);
541 goto done;
543 s += tlen;
544 err = got_object_commit_add_parent(*commit, s);
545 if (err)
546 goto done;
548 remain -= SHA1_DIGEST_STRING_LENGTH;
549 s += SHA1_DIGEST_STRING_LENGTH;
552 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
553 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
554 char *p;
555 size_t slen;
557 remain -= tlen;
558 if (remain <= 0) {
559 err = got_error(GOT_ERR_BAD_OBJ_DATA);
560 goto done;
562 s += tlen;
563 p = strchr(s, '\n');
564 if (p == NULL) {
565 err = got_error(GOT_ERR_BAD_OBJ_DATA);
566 goto done;
568 *p = '\0';
569 slen = strlen(s);
570 err = parse_commit_time(&(*commit)->author_time,
571 &(*commit)->author_tzoff, s);
572 if (err)
573 goto done;
574 (*commit)->author = strdup(s);
575 if ((*commit)->author == NULL) {
576 err = got_error_from_errno();
577 goto done;
579 s += slen + 1;
580 remain -= slen + 1;
583 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
584 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
585 char *p;
586 size_t slen;
588 remain -= tlen;
589 if (remain <= 0) {
590 err = got_error(GOT_ERR_BAD_OBJ_DATA);
591 goto done;
593 s += tlen;
594 p = strchr(s, '\n');
595 if (p == NULL) {
596 err = got_error(GOT_ERR_BAD_OBJ_DATA);
597 goto done;
599 *p = '\0';
600 slen = strlen(s);
601 err = parse_commit_time(&(*commit)->committer_time,
602 &(*commit)->committer_tzoff, s);
603 if (err)
604 goto done;
605 (*commit)->committer = strdup(s);
606 if ((*commit)->committer == NULL) {
607 err = got_error_from_errno();
608 goto done;
610 s += slen + 1;
611 remain -= slen + 1;
614 (*commit)->logmsg = strndup(s, remain);
615 if ((*commit)->logmsg == NULL) {
616 err = got_error_from_errno();
617 goto done;
619 done:
620 if (err) {
621 got_object_commit_close(*commit);
622 *commit = NULL;
624 return err;
627 static void
628 tree_entry_close(struct got_tree_entry *te)
630 free(te->id);
631 free(te->name);
632 free(te);
635 struct got_tree_entry *
636 got_alloc_tree_entry_partial(void)
638 struct got_tree_entry *te;
640 te = calloc(1, sizeof(*te));
641 if (te == NULL)
642 return NULL;
644 te->id = calloc(1, sizeof(*te->id));
645 if (te->id == NULL) {
646 free(te);
647 te = NULL;
649 return te;
652 static const struct got_error *
653 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
654 size_t maxlen)
656 char *p = buf, *space;
657 const struct got_error *err = NULL;
659 *te = got_alloc_tree_entry_partial();
660 if (*te == NULL)
661 return got_error_from_errno();
663 *elen = strlen(buf) + 1;
664 if (*elen > maxlen) {
665 free(*te);
666 *te = NULL;
667 return got_error(GOT_ERR_BAD_OBJ_DATA);
670 space = strchr(buf, ' ');
671 if (space == NULL) {
672 err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 free(*te);
674 *te = NULL;
675 return err;
677 while (*p != ' ') {
678 if (*p < '0' && *p > '7') {
679 err = got_error(GOT_ERR_BAD_OBJ_DATA);
680 goto done;
682 (*te)->mode <<= 3;
683 (*te)->mode |= *p - '0';
684 p++;
687 (*te)->name = strdup(space + 1);
688 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
689 err = got_error(GOT_ERR_BAD_OBJ_DATA);
690 goto done;
692 buf += strlen(buf) + 1;
693 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
694 *elen += SHA1_DIGEST_LENGTH;
695 done:
696 if (err) {
697 tree_entry_close(*te);
698 *te = NULL;
700 return err;
703 static const struct got_error *
704 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
706 const struct got_error *err;
707 size_t remain = len;
709 *tree = calloc(1, sizeof(**tree));
710 if (*tree == NULL)
711 return got_error_from_errno();
713 SIMPLEQ_INIT(&(*tree)->entries);
715 while (remain > 0) {
716 struct got_tree_entry *te;
717 size_t elen;
719 err = parse_tree_entry(&te, &elen, buf, remain);
720 if (err)
721 return err;
722 (*tree)->nentries++;
723 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
724 buf += elen;
725 remain -= elen;
728 if (remain != 0) {
729 got_object_tree_close(*tree);
730 return got_error(GOT_ERR_BAD_OBJ_DATA);
733 return NULL;
736 static const struct got_error *
737 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
739 const struct got_error *err = NULL;
740 static const size_t blocksize = 512;
741 size_t n, total, remain;
742 uint8_t *buf;
744 *outbuf = NULL;
745 *outlen = 0;
747 buf = calloc(1, blocksize);
748 if (buf == NULL)
749 return got_error_from_errno();
751 remain = blocksize;
752 total = 0;
753 while (1) {
754 if (remain == 0) {
755 uint8_t *newbuf;
756 newbuf = reallocarray(buf, 1, total + blocksize);
757 if (newbuf == NULL) {
758 err = got_error_from_errno();
759 goto done;
761 buf = newbuf;
762 remain += blocksize;
764 n = fread(buf + total, 1, remain, f);
765 if (n == 0) {
766 if (ferror(f)) {
767 err = got_ferror(f, GOT_ERR_IO);
768 goto done;
770 break; /* EOF */
772 remain -= n;
773 total += n;
774 };
776 done:
777 if (err == NULL) {
778 *outbuf = buf;
779 *outlen = total;
780 } else
781 free(buf);
782 return err;
785 static const struct got_error *
786 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
787 FILE *f)
789 const struct got_error *err = NULL;
790 size_t len;
791 uint8_t *p;
793 if (obj->flags & GOT_OBJ_FLAG_PACKED)
794 err = read_to_mem(&p, &len, f);
795 else
796 err = got_inflate_to_mem(&p, &len, f);
797 if (err)
798 return err;
800 if (len < obj->hdrlen + obj->size) {
801 err = got_error(GOT_ERR_BAD_OBJ_DATA);
802 goto done;
805 /* Skip object header. */
806 len -= obj->hdrlen;
807 err = parse_commit_object(commit, p + obj->hdrlen, len);
808 free(p);
809 done:
810 return err;
813 static void
814 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
815 int imsg_fds[2])
817 const struct got_error *err = NULL;
818 struct got_commit_object *commit = NULL;
819 struct imsgbuf ibuf;
820 FILE *f = NULL;
821 int status = 0;
823 setproctitle("read commit object");
824 close(imsg_fds[0]);
825 imsg_init(&ibuf, imsg_fds[1]);
827 /* revoke access to most system calls */
828 if (pledge("stdio", NULL) == -1) {
829 err = got_error_from_errno();
830 goto done;
833 f = fdopen(obj_fd, "rb");
834 if (f == NULL) {
835 err = got_error_from_errno();
836 close(obj_fd);
837 goto done;
840 err = read_commit_object(&commit, obj, f);
841 if (err)
842 goto done;
844 err = got_privsep_send_commit(&ibuf, commit);
845 done:
846 if (commit)
847 got_object_commit_close(commit);
848 if (err) {
849 got_privsep_send_error(&ibuf, err);
850 status = 1;
852 if (f)
853 fclose(f);
854 imsg_clear(&ibuf);
855 close(imsg_fds[1]);
856 _exit(status);
859 static const struct got_error *
860 read_commit_object_privsep(struct got_commit_object **commit,
861 struct got_repository *repo, struct got_object *obj, int fd)
863 const struct got_error *err = NULL, *err_child = NULL;
864 struct imsgbuf parent_ibuf;
865 int imsg_fds[2];
866 pid_t pid;
868 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
869 return got_error_from_errno();
871 pid = fork();
872 if (pid == -1)
873 return got_error_from_errno();
874 else if (pid == 0) {
875 read_commit_object_privsep_child(obj, fd, imsg_fds);
876 /* not reached */
879 close(imsg_fds[1]);
880 imsg_init(&parent_ibuf, imsg_fds[0]);
881 err = got_privsep_recv_commit(commit, &parent_ibuf);
882 imsg_clear(&parent_ibuf);
883 err_child = wait_for_child(pid);
884 close(imsg_fds[0]);
885 return err ? err : err_child;
888 const struct got_error *
889 got_object_commit_open(struct got_commit_object **commit,
890 struct got_repository *repo, struct got_object *obj)
892 const struct got_error *err = NULL;
894 if (obj->type != GOT_OBJ_TYPE_COMMIT)
895 return got_error(GOT_ERR_OBJ_TYPE);
897 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
898 uint8_t *buf;
899 size_t len;
900 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
901 if (err)
902 return err;
903 obj->size = len;
904 err = parse_commit_object(commit, buf, len);
905 free(buf);
906 } else {
907 int fd;
908 err = open_loose_object(&fd, obj, repo);
909 if (err)
910 return err;
911 err = read_commit_object_privsep(commit, repo, obj, fd);
912 close(fd);
914 return err;
917 void
918 got_object_commit_close(struct got_commit_object *commit)
920 struct got_object_qid *qid;
922 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
923 qid = SIMPLEQ_FIRST(&commit->parent_ids);
924 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
925 free(qid->id);
926 free(qid);
929 free(commit->tree_id);
930 free(commit->author);
931 free(commit->author_tzoff);
932 free(commit->committer);
933 free(commit->committer_tzoff);
934 free(commit->logmsg);
935 free(commit);
938 static const struct got_error *
939 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
941 const struct got_error *err = NULL;
942 size_t len;
943 uint8_t *p;
945 if (obj->flags & GOT_OBJ_FLAG_PACKED)
946 err = read_to_mem(&p, &len, f);
947 else
948 err = got_inflate_to_mem(&p, &len, f);
949 if (err)
950 return err;
952 if (len < obj->hdrlen + obj->size) {
953 err = got_error(GOT_ERR_BAD_OBJ_DATA);
954 goto done;
957 /* Skip object header. */
958 len -= obj->hdrlen;
959 err = parse_tree_object(tree, p + obj->hdrlen, len);
960 free(p);
961 done:
962 return err;
965 static void
966 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
967 int imsg_fds[2])
969 const struct got_error *err = NULL;
970 struct got_tree_object *tree = NULL;
971 struct imsgbuf ibuf;
972 FILE *f = NULL;
973 int status = 0;
975 setproctitle("read tree object");
976 close(imsg_fds[0]);
977 imsg_init(&ibuf, imsg_fds[1]);
979 /* revoke access to most system calls */
980 if (pledge("stdio", NULL) == -1) {
981 err = got_error_from_errno();
982 goto done;
985 f = fdopen(obj_fd, "rb");
986 if (f == NULL) {
987 err = got_error_from_errno();
988 close(obj_fd);
989 goto done;
992 err = read_tree_object(&tree, obj, f);
993 if (err)
994 goto done;
996 err = got_privsep_send_tree(&ibuf, tree);
997 done:
998 if (tree)
999 got_object_tree_close(tree);
1000 if (err) {
1001 got_privsep_send_error(&ibuf, err);
1002 status = 1;
1004 if (f)
1005 fclose(f);
1006 imsg_clear(&ibuf);
1007 close(imsg_fds[1]);
1008 _exit(status);
1011 static const struct got_error *
1012 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1013 int fd)
1015 const struct got_error *err = NULL, *err_child = NULL;
1016 struct imsgbuf parent_ibuf;
1017 int imsg_fds[2];
1018 pid_t pid;
1020 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1021 return got_error_from_errno();
1023 pid = fork();
1024 if (pid == -1)
1025 return got_error_from_errno();
1026 else if (pid == 0) {
1027 read_tree_object_privsep_child(obj, fd, imsg_fds);
1028 /* not reached */
1031 close(imsg_fds[1]);
1032 imsg_init(&parent_ibuf, imsg_fds[0]);
1033 err = got_privsep_recv_tree(tree, &parent_ibuf);
1034 imsg_clear(&parent_ibuf);
1035 err_child = wait_for_child(pid);
1036 close(imsg_fds[0]);
1037 return err ? err : err_child;
1040 const struct got_error *
1041 got_object_tree_open(struct got_tree_object **tree,
1042 struct got_repository *repo, struct got_object *obj)
1044 const struct got_error *err = NULL;
1046 if (obj->type != GOT_OBJ_TYPE_TREE)
1047 return got_error(GOT_ERR_OBJ_TYPE);
1049 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1050 uint8_t *buf;
1051 size_t len;
1052 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1053 if (err)
1054 return err;
1055 obj->size = len;
1056 err = parse_tree_object(tree, buf, len);
1057 free(buf);
1058 } else {
1059 int fd;
1060 err = open_loose_object(&fd, obj, repo);
1061 if (err)
1062 return err;
1063 err = read_tree_object_privsep(tree, obj, fd);
1064 close(fd);
1066 return err;
1069 void
1070 got_object_tree_close(struct got_tree_object *tree)
1072 struct got_tree_entry *te;
1074 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1075 te = SIMPLEQ_FIRST(&tree->entries);
1076 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1077 tree_entry_close(te);
1080 free(tree);
1083 static const struct got_error *
1084 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1086 const struct got_error *err = NULL;
1087 struct imsgbuf ibuf;
1088 int status = 0;
1089 size_t size;
1090 FILE *infile = NULL;
1092 setproctitle("read blob object");
1093 close(imsg_fds[0]);
1094 imsg_init(&ibuf, imsg_fds[1]);
1096 /* revoke access to most system calls */
1097 if (pledge("stdio", NULL) == -1) {
1098 err = got_error_from_errno();
1099 goto done;
1102 infile = fdopen(infd, "rb");
1103 if (infile == NULL) {
1104 err = got_error_from_errno();
1105 close(infd);
1106 goto done;
1108 err = got_inflate_to_fd(&size, infile, outfd);
1109 fclose(infile);
1110 if (err)
1111 goto done;
1113 err = got_privsep_send_blob(&ibuf, size);
1114 done:
1115 if (err) {
1116 got_privsep_send_error(&ibuf, err);
1117 status = 1;
1119 close(outfd);
1120 imsg_clear(&ibuf);
1121 close(imsg_fds[1]);
1122 _exit(status);
1125 static const struct got_error *
1126 read_blob_object_privsep(size_t *size, int outfd, int infd)
1128 struct imsgbuf parent_ibuf;
1129 int imsg_fds[2];
1130 const struct got_error *err = NULL, *err_child = NULL;
1131 pid_t pid;
1133 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1134 return got_error_from_errno();
1136 pid = fork();
1137 if (pid == -1)
1138 return got_error_from_errno();
1139 else if (pid == 0) {
1140 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1141 /* not reached */
1144 close(imsg_fds[1]);
1145 imsg_init(&parent_ibuf, imsg_fds[0]);
1146 err = got_privsep_recv_blob(size, &parent_ibuf);
1147 imsg_clear(&parent_ibuf);
1148 err_child = wait_for_child(pid);
1149 close(imsg_fds[0]);
1150 if (lseek(outfd, SEEK_SET, 0) == -1)
1151 err = got_error_from_errno();
1152 return err ? err : err_child;
1155 const struct got_error *
1156 got_object_blob_open(struct got_blob_object **blob,
1157 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1159 const struct got_error *err = NULL;
1161 if (obj->type != GOT_OBJ_TYPE_BLOB)
1162 return got_error(GOT_ERR_OBJ_TYPE);
1164 if (blocksize < obj->hdrlen)
1165 return got_error(GOT_ERR_NO_SPACE);
1167 *blob = calloc(1, sizeof(**blob));
1168 if (*blob == NULL)
1169 return got_error_from_errno();
1171 (*blob)->read_buf = calloc(1, blocksize);
1172 if ((*blob)->read_buf == NULL) {
1173 err = got_error_from_errno();
1174 goto done;
1176 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1177 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1178 if (err)
1179 goto done;
1180 } else {
1181 int infd, outfd;
1182 size_t size;
1183 struct stat sb;
1185 err = open_loose_object(&infd, obj, repo);
1186 if (err)
1187 goto done;
1190 outfd = got_opentempfd();
1191 if (outfd == -1) {
1192 err = got_error_from_errno();
1193 close(infd);
1194 goto done;
1197 err = read_blob_object_privsep(&size, outfd, infd);
1198 close(infd);
1199 if (err)
1200 goto done;
1202 if (size != obj->hdrlen + obj->size) {
1203 err = got_error(GOT_ERR_PRIVSEP_LEN);
1204 close(outfd);
1205 goto done;
1208 if (fstat(outfd, &sb) == -1) {
1209 err = got_error_from_errno();
1210 close(outfd);
1211 goto done;
1214 if (sb.st_size != size) {
1215 err = got_error(GOT_ERR_PRIVSEP_LEN);
1216 close(outfd);
1217 goto done;
1220 (*blob)->f = fdopen(outfd, "rb");
1221 if ((*blob)->f == NULL) {
1222 err = got_error_from_errno();
1223 close(outfd);
1224 goto done;
1228 (*blob)->hdrlen = obj->hdrlen;
1229 (*blob)->blocksize = blocksize;
1230 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1232 done:
1233 if (err && *blob) {
1234 if ((*blob)->f)
1235 fclose((*blob)->f);
1236 free((*blob)->read_buf);
1237 free(*blob);
1238 *blob = NULL;
1240 return err;
1243 void
1244 got_object_blob_close(struct got_blob_object *blob)
1246 free(blob->read_buf);
1247 fclose(blob->f);
1248 free(blob);
1251 char *
1252 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1254 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1257 size_t
1258 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1260 return blob->hdrlen;
1263 const uint8_t *
1264 got_object_blob_get_read_buf(struct got_blob_object *blob)
1266 return blob->read_buf;
1269 const struct got_error *
1270 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1272 size_t n;
1274 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1275 if (n == 0 && ferror(blob->f))
1276 return got_ferror(blob->f, GOT_ERR_IO);
1277 *outlenp = n;
1278 return NULL;