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_parent_id *pid;
450 pid = calloc(1, sizeof(*pid));
451 if (pid == NULL)
452 return got_error_from_errno();
454 pid->id = calloc(1, sizeof(*pid->id));
455 if (pid->id == NULL) {
456 err = got_error_from_errno();
457 free(pid);
458 return err;
461 if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
462 err = got_error(GOT_ERR_BAD_OBJ_DATA);
463 free(pid->id);
464 free(pid);
465 return err;
468 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
469 commit->nparents++;
471 return NULL;
474 static const struct got_error *
475 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
477 const struct got_error *err = NULL;
478 char *s = buf;
479 size_t tlen;
480 ssize_t remain = (ssize_t)len;
482 *commit = got_object_commit_alloc_partial();
483 if (*commit == NULL)
484 return got_error_from_errno();
486 tlen = strlen(GOT_COMMIT_TAG_TREE);
487 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
488 remain -= tlen;
489 if (remain < SHA1_DIGEST_STRING_LENGTH) {
490 err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 goto done;
493 s += tlen;
494 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
495 err = got_error(GOT_ERR_BAD_OBJ_DATA);
496 goto done;
498 remain -= SHA1_DIGEST_STRING_LENGTH;
499 s += SHA1_DIGEST_STRING_LENGTH;
500 } else {
501 err = got_error(GOT_ERR_BAD_OBJ_DATA);
502 goto done;
505 tlen = strlen(GOT_COMMIT_TAG_PARENT);
506 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
507 remain -= tlen;
508 if (remain < SHA1_DIGEST_STRING_LENGTH) {
509 err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 goto done;
512 s += tlen;
513 err = got_object_commit_add_parent(*commit, s);
514 if (err)
515 goto done;
517 remain -= SHA1_DIGEST_STRING_LENGTH;
518 s += SHA1_DIGEST_STRING_LENGTH;
521 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
522 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
523 char *p;
525 remain -= tlen;
526 if (remain <= 0) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 goto done;
530 s += tlen;
531 p = strchr(s, '\n');
532 if (p == NULL) {
533 err = got_error(GOT_ERR_BAD_OBJ_DATA);
534 goto done;
536 *p = '\0';
537 (*commit)->author = strdup(s);
538 if ((*commit)->author == NULL) {
539 err = got_error_from_errno();
540 goto done;
542 s += strlen((*commit)->author) + 1;
543 remain -= strlen((*commit)->author) + 1;
546 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
547 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
548 char *p;
550 remain -= tlen;
551 if (remain <= 0) {
552 err = got_error(GOT_ERR_BAD_OBJ_DATA);
553 goto done;
555 s += tlen;
556 p = strchr(s, '\n');
557 if (p == NULL) {
558 err = got_error(GOT_ERR_BAD_OBJ_DATA);
559 goto done;
561 *p = '\0';
562 (*commit)->committer = strdup(s);
563 if ((*commit)->committer == NULL) {
564 err = got_error_from_errno();
565 goto done;
567 s += strlen((*commit)->committer) + 1;
568 remain -= strlen((*commit)->committer) + 1;
571 (*commit)->logmsg = strndup(s, remain);
572 if ((*commit)->logmsg == NULL) {
573 err = got_error_from_errno();
574 goto done;
576 done:
577 if (err) {
578 got_object_commit_close(*commit);
579 *commit = NULL;
581 return err;
584 static const struct got_error *
585 parse_commit_time(time_t *time, const char *author_str)
587 const struct got_error *err = NULL;
588 const char *errstr;
589 char *committer, *space;
591 *time = 0;
593 committer = strdup(author_str);
594 if (committer == NULL)
595 return got_error_from_errno();
597 /* Strip off trailing timezone indicator. */
598 space = strrchr(committer, ' ');
599 if (space == NULL) {
600 err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 goto done;
603 *space = '\0';
605 /* Timestamp is separated from committer name + email by space. */
606 space = strrchr(committer, ' ');
607 if (space == NULL) {
608 err = got_error(GOT_ERR_BAD_OBJ_DATA);
609 goto done;
612 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
613 if (errstr)
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
616 done:
617 free(committer);
618 return err;
621 const struct got_error *
622 got_object_commit_get_committer_time(time_t *time,
623 struct got_commit_object *commit)
625 return parse_commit_time(time, commit->committer);
628 const struct got_error *
629 got_object_commit_get_author_time(time_t *time,
630 struct got_commit_object *commit)
632 return parse_commit_time(time, commit->committer);
635 static void
636 tree_entry_close(struct got_tree_entry *te)
638 free(te->id);
639 free(te->name);
640 free(te);
643 struct got_tree_entry *
644 got_alloc_tree_entry_partial(void)
646 struct got_tree_entry *te;
648 te = calloc(1, sizeof(*te));
649 if (te == NULL)
650 return NULL;
652 te->id = calloc(1, sizeof(*te->id));
653 if (te->id == NULL) {
654 free(te);
655 te = NULL;
657 return te;
660 static const struct got_error *
661 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
662 size_t maxlen)
664 char *p = buf, *space;
665 const struct got_error *err = NULL;
667 *te = got_alloc_tree_entry_partial();
668 if (*te == NULL)
669 return got_error_from_errno();
671 *elen = strlen(buf) + 1;
672 if (*elen > maxlen) {
673 free(*te);
674 *te = NULL;
675 return got_error(GOT_ERR_BAD_OBJ_DATA);
678 space = strchr(buf, ' ');
679 if (space == NULL) {
680 err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 free(*te);
682 *te = NULL;
683 return err;
685 while (*p != ' ') {
686 if (*p < '0' && *p > '7') {
687 err = got_error(GOT_ERR_BAD_OBJ_DATA);
688 goto done;
690 (*te)->mode <<= 3;
691 (*te)->mode |= *p - '0';
692 p++;
695 (*te)->name = strdup(space + 1);
696 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
697 err = got_error(GOT_ERR_BAD_OBJ_DATA);
698 goto done;
700 buf += strlen(buf) + 1;
701 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
702 *elen += SHA1_DIGEST_LENGTH;
703 done:
704 if (err) {
705 tree_entry_close(*te);
706 *te = NULL;
708 return err;
711 static const struct got_error *
712 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
714 const struct got_error *err;
715 size_t remain = len;
717 *tree = calloc(1, sizeof(**tree));
718 if (*tree == NULL)
719 return got_error_from_errno();
721 SIMPLEQ_INIT(&(*tree)->entries);
723 while (remain > 0) {
724 struct got_tree_entry *te;
725 size_t elen;
727 err = parse_tree_entry(&te, &elen, buf, remain);
728 if (err)
729 return err;
730 (*tree)->nentries++;
731 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
732 buf += elen;
733 remain -= elen;
736 if (remain != 0) {
737 got_object_tree_close(*tree);
738 return got_error(GOT_ERR_BAD_OBJ_DATA);
741 return NULL;
744 static const struct got_error *
745 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
747 const struct got_error *err = NULL;
748 static const size_t blocksize = 512;
749 size_t n, total, remain;
750 uint8_t *buf;
752 *outbuf = NULL;
753 *outlen = 0;
755 buf = calloc(1, blocksize);
756 if (buf == NULL)
757 return got_error_from_errno();
759 remain = blocksize;
760 total = 0;
761 while (1) {
762 if (remain == 0) {
763 uint8_t *newbuf;
764 newbuf = reallocarray(buf, 1, total + blocksize);
765 if (newbuf == NULL) {
766 err = got_error_from_errno();
767 goto done;
769 buf = newbuf;
770 remain += blocksize;
772 n = fread(buf + total, 1, remain, f);
773 if (n == 0) {
774 if (ferror(f)) {
775 err = got_ferror(f, GOT_ERR_IO);
776 goto done;
778 break; /* EOF */
780 remain -= n;
781 total += n;
782 };
784 done:
785 if (err == NULL) {
786 *outbuf = buf;
787 *outlen = total;
788 } else
789 free(buf);
790 return err;
793 static const struct got_error *
794 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
795 FILE *f)
797 const struct got_error *err = NULL;
798 size_t len;
799 uint8_t *p;
801 if (obj->flags & GOT_OBJ_FLAG_PACKED)
802 err = read_to_mem(&p, &len, f);
803 else
804 err = got_inflate_to_mem(&p, &len, f);
805 if (err)
806 return err;
808 if (len < obj->hdrlen + obj->size) {
809 err = got_error(GOT_ERR_BAD_OBJ_DATA);
810 goto done;
813 /* Skip object header. */
814 len -= obj->hdrlen;
815 err = parse_commit_object(commit, p + obj->hdrlen, len);
816 free(p);
817 done:
818 return err;
821 static void
822 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
823 int imsg_fds[2])
825 const struct got_error *err = NULL;
826 struct got_commit_object *commit = NULL;
827 struct imsgbuf ibuf;
828 FILE *f = NULL;
829 int status = 0;
831 setproctitle("read commit object");
832 close(imsg_fds[0]);
833 imsg_init(&ibuf, imsg_fds[1]);
835 /* revoke access to most system calls */
836 if (pledge("stdio", NULL) == -1) {
837 err = got_error_from_errno();
838 goto done;
841 f = fdopen(obj_fd, "rb");
842 if (f == NULL) {
843 err = got_error_from_errno();
844 close(obj_fd);
845 goto done;
848 err = read_commit_object(&commit, obj, f);
849 if (err)
850 goto done;
852 err = got_privsep_send_commit(&ibuf, commit);
853 done:
854 if (commit)
855 got_object_commit_close(commit);
856 if (err) {
857 got_privsep_send_error(&ibuf, err);
858 status = 1;
860 if (f)
861 fclose(f);
862 imsg_clear(&ibuf);
863 close(imsg_fds[1]);
864 _exit(status);
867 static const struct got_error *
868 read_commit_object_privsep(struct got_commit_object **commit,
869 struct got_repository *repo, struct got_object *obj, int fd)
871 const struct got_error *err = NULL, *err_child = NULL;
872 struct imsgbuf parent_ibuf;
873 int imsg_fds[2];
874 pid_t pid;
876 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
877 return got_error_from_errno();
879 pid = fork();
880 if (pid == -1)
881 return got_error_from_errno();
882 else if (pid == 0) {
883 read_commit_object_privsep_child(obj, fd, imsg_fds);
884 /* not reached */
887 close(imsg_fds[1]);
888 imsg_init(&parent_ibuf, imsg_fds[0]);
889 err = got_privsep_recv_commit(commit, &parent_ibuf);
890 imsg_clear(&parent_ibuf);
891 err_child = wait_for_child(pid);
892 close(imsg_fds[0]);
893 return err ? err : err_child;
896 const struct got_error *
897 got_object_commit_open(struct got_commit_object **commit,
898 struct got_repository *repo, struct got_object *obj)
900 const struct got_error *err = NULL;
902 if (obj->type != GOT_OBJ_TYPE_COMMIT)
903 return got_error(GOT_ERR_OBJ_TYPE);
905 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
906 uint8_t *buf;
907 size_t len;
908 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
909 if (err)
910 return err;
911 obj->size = len;
912 err = parse_commit_object(commit, buf, len);
913 free(buf);
914 } else {
915 int fd;
916 err = open_loose_object(&fd, obj, repo);
917 if (err)
918 return err;
919 err = read_commit_object_privsep(commit, repo, obj, fd);
920 close(fd);
922 return err;
925 void
926 got_object_commit_close(struct got_commit_object *commit)
928 struct got_parent_id *pid;
930 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
931 pid = SIMPLEQ_FIRST(&commit->parent_ids);
932 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
933 free(pid->id);
934 free(pid);
937 free(commit->tree_id);
938 free(commit->author);
939 free(commit->committer);
940 free(commit->logmsg);
941 free(commit);
944 static const struct got_error *
945 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
947 const struct got_error *err = NULL;
948 size_t len;
949 uint8_t *p;
951 if (obj->flags & GOT_OBJ_FLAG_PACKED)
952 err = read_to_mem(&p, &len, f);
953 else
954 err = got_inflate_to_mem(&p, &len, f);
955 if (err)
956 return err;
958 if (len < obj->hdrlen + obj->size) {
959 err = got_error(GOT_ERR_BAD_OBJ_DATA);
960 goto done;
963 /* Skip object header. */
964 len -= obj->hdrlen;
965 err = parse_tree_object(tree, p + obj->hdrlen, len);
966 free(p);
967 done:
968 return err;
971 static void
972 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
973 int imsg_fds[2])
975 const struct got_error *err = NULL;
976 struct got_tree_object *tree = NULL;
977 struct imsgbuf ibuf;
978 FILE *f = NULL;
979 int status = 0;
981 setproctitle("read tree object");
982 close(imsg_fds[0]);
983 imsg_init(&ibuf, imsg_fds[1]);
985 /* revoke access to most system calls */
986 if (pledge("stdio", NULL) == -1) {
987 err = got_error_from_errno();
988 goto done;
991 f = fdopen(obj_fd, "rb");
992 if (f == NULL) {
993 err = got_error_from_errno();
994 close(obj_fd);
995 goto done;
998 err = read_tree_object(&tree, obj, f);
999 if (err)
1000 goto done;
1002 err = got_privsep_send_tree(&ibuf, tree);
1003 done:
1004 if (tree)
1005 got_object_tree_close(tree);
1006 if (err) {
1007 got_privsep_send_error(&ibuf, err);
1008 status = 1;
1010 if (f)
1011 fclose(f);
1012 imsg_clear(&ibuf);
1013 close(imsg_fds[1]);
1014 _exit(status);
1017 static const struct got_error *
1018 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1019 int fd)
1021 const struct got_error *err = NULL, *err_child = NULL;
1022 struct imsgbuf parent_ibuf;
1023 int imsg_fds[2];
1024 pid_t pid;
1026 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1027 return got_error_from_errno();
1029 pid = fork();
1030 if (pid == -1)
1031 return got_error_from_errno();
1032 else if (pid == 0) {
1033 read_tree_object_privsep_child(obj, fd, imsg_fds);
1034 /* not reached */
1037 close(imsg_fds[1]);
1038 imsg_init(&parent_ibuf, imsg_fds[0]);
1039 err = got_privsep_recv_tree(tree, &parent_ibuf);
1040 imsg_clear(&parent_ibuf);
1041 err_child = wait_for_child(pid);
1042 close(imsg_fds[0]);
1043 return err ? err : err_child;
1046 const struct got_error *
1047 got_object_tree_open(struct got_tree_object **tree,
1048 struct got_repository *repo, struct got_object *obj)
1050 const struct got_error *err = NULL;
1052 if (obj->type != GOT_OBJ_TYPE_TREE)
1053 return got_error(GOT_ERR_OBJ_TYPE);
1055 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1056 uint8_t *buf;
1057 size_t len;
1058 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1059 if (err)
1060 return err;
1061 obj->size = len;
1062 err = parse_tree_object(tree, buf, len);
1063 free(buf);
1064 } else {
1065 int fd;
1066 err = open_loose_object(&fd, obj, repo);
1067 if (err)
1068 return err;
1069 err = read_tree_object_privsep(tree, obj, fd);
1070 close(fd);
1072 return err;
1075 void
1076 got_object_tree_close(struct got_tree_object *tree)
1078 struct got_tree_entry *te;
1080 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1081 te = SIMPLEQ_FIRST(&tree->entries);
1082 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1083 tree_entry_close(te);
1086 free(tree);
1089 static const struct got_error *
1090 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1092 const struct got_error *err = NULL;
1093 struct imsgbuf ibuf;
1094 int status = 0;
1095 size_t size;
1096 FILE *infile = NULL;
1098 setproctitle("read blob object");
1099 close(imsg_fds[0]);
1100 imsg_init(&ibuf, imsg_fds[1]);
1102 /* revoke access to most system calls */
1103 if (pledge("stdio", NULL) == -1) {
1104 err = got_error_from_errno();
1105 goto done;
1108 infile = fdopen(infd, "rb");
1109 if (infile == NULL) {
1110 err = got_error_from_errno();
1111 close(infd);
1112 goto done;
1114 err = got_inflate_to_fd(&size, infile, outfd);
1115 fclose(infile);
1116 if (err)
1117 goto done;
1119 err = got_privsep_send_blob(&ibuf, size);
1120 done:
1121 if (err) {
1122 got_privsep_send_error(&ibuf, err);
1123 status = 1;
1125 close(outfd);
1126 imsg_clear(&ibuf);
1127 close(imsg_fds[1]);
1128 _exit(status);
1131 static const struct got_error *
1132 read_blob_object_privsep(size_t *size, int outfd, int infd)
1134 struct imsgbuf parent_ibuf;
1135 int imsg_fds[2];
1136 const struct got_error *err = NULL, *err_child = NULL;
1137 pid_t pid;
1139 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1140 return got_error_from_errno();
1142 pid = fork();
1143 if (pid == -1)
1144 return got_error_from_errno();
1145 else if (pid == 0) {
1146 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1147 /* not reached */
1150 close(imsg_fds[1]);
1151 imsg_init(&parent_ibuf, imsg_fds[0]);
1152 err = got_privsep_recv_blob(size, &parent_ibuf);
1153 imsg_clear(&parent_ibuf);
1154 err_child = wait_for_child(pid);
1155 close(imsg_fds[0]);
1156 if (lseek(outfd, SEEK_SET, 0) == -1)
1157 err = got_error_from_errno();
1158 return err ? err : err_child;
1161 const struct got_error *
1162 got_object_blob_open(struct got_blob_object **blob,
1163 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1165 const struct got_error *err = NULL;
1167 if (obj->type != GOT_OBJ_TYPE_BLOB)
1168 return got_error(GOT_ERR_OBJ_TYPE);
1170 if (blocksize < obj->hdrlen)
1171 return got_error(GOT_ERR_NO_SPACE);
1173 *blob = calloc(1, sizeof(**blob));
1174 if (*blob == NULL)
1175 return got_error_from_errno();
1177 (*blob)->read_buf = calloc(1, blocksize);
1178 if ((*blob)->read_buf == NULL) {
1179 err = got_error_from_errno();
1180 goto done;
1182 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1183 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1184 if (err)
1185 goto done;
1186 } else {
1187 int infd, outfd;
1188 size_t size;
1189 struct stat sb;
1191 err = open_loose_object(&infd, obj, repo);
1192 if (err)
1193 goto done;
1196 outfd = got_opentempfd();
1197 if (outfd == -1) {
1198 err = got_error_from_errno();
1199 close(infd);
1200 goto done;
1203 err = read_blob_object_privsep(&size, outfd, infd);
1204 close(infd);
1205 if (err)
1206 goto done;
1208 if (size != obj->hdrlen + obj->size) {
1209 err = got_error(GOT_ERR_PRIVSEP_LEN);
1210 close(outfd);
1211 goto done;
1214 if (fstat(outfd, &sb) == -1) {
1215 err = got_error_from_errno();
1216 close(outfd);
1217 goto done;
1220 if (sb.st_size != size) {
1221 err = got_error(GOT_ERR_PRIVSEP_LEN);
1222 close(outfd);
1223 goto done;
1226 (*blob)->f = fdopen(outfd, "rb");
1227 if ((*blob)->f == NULL) {
1228 err = got_error_from_errno();
1229 close(outfd);
1230 goto done;
1234 (*blob)->hdrlen = obj->hdrlen;
1235 (*blob)->blocksize = blocksize;
1236 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1238 done:
1239 if (err && *blob) {
1240 if ((*blob)->f)
1241 fclose((*blob)->f);
1242 free((*blob)->read_buf);
1243 free(*blob);
1244 *blob = NULL;
1246 return err;
1249 void
1250 got_object_blob_close(struct got_blob_object *blob)
1252 free(blob->read_buf);
1253 fclose(blob->f);
1254 free(blob);
1257 char *
1258 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1260 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1263 size_t
1264 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1266 return blob->hdrlen;
1269 const uint8_t *
1270 got_object_blob_get_read_buf(struct got_blob_object *blob)
1272 return blob->read_buf;
1275 const struct got_error *
1276 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1278 size_t n;
1280 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1281 if (n == 0 && ferror(blob->f))
1282 return got_ferror(blob->f, GOT_ERR_IO);
1283 *outlenp = n;
1284 return NULL;