2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/syslimits.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_repository.h"
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #define GOT_OBJ_TAG_COMMIT "commit"
57 #define GOT_OBJ_TAG_TREE "tree"
58 #define GOT_OBJ_TAG_BLOB "blob"
60 #define GOT_COMMIT_TAG_TREE "tree "
61 #define GOT_COMMIT_TAG_PARENT "parent "
62 #define GOT_COMMIT_TAG_AUTHOR "author "
63 #define GOT_COMMIT_TAG_COMMITTER "committer "
65 const struct got_error *
66 got_object_id_str(char **outbuf, struct got_object_id *id)
68 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 *outbuf = malloc(len);
72 return got_error_from_errno();
74 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
77 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
84 got_object_close(struct got_object *obj)
86 if (obj->refcnt > 0) {
92 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
93 struct got_delta *delta;
94 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
95 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
96 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
97 got_delta_close(delta);
100 if (obj->flags & GOT_OBJ_FLAG_PACKED)
101 free(obj->path_packfile);
106 got_object_qid_free(struct got_object_qid *qid)
112 static const struct got_error *
113 request_object(struct got_object **obj, struct got_repository *repo, int fd)
115 const struct got_error *err = NULL;
116 struct imsgbuf *ibuf;
118 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
120 err = got_privsep_send_obj_req(ibuf, fd, NULL);
124 return got_privsep_recv_obj(obj, ibuf);
128 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
132 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
133 fprintf(stderr, "%s: %s\n", getprogname(),
137 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
138 fprintf(stderr, "%s: %s\n", getprogname(),
143 if (execl(path, path, repo_path, (char *)NULL) == -1) {
144 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
150 const struct got_error *
151 got_object_read_header_privsep(struct got_object **obj,
152 struct got_repository *repo, int obj_fd)
156 struct imsgbuf *ibuf;
158 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
159 return request_object(obj, repo, obj_fd);
161 ibuf = calloc(1, sizeof(*ibuf));
163 return got_error_from_errno();
165 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
166 return got_error_from_errno();
170 return got_error_from_errno();
172 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
178 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
180 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
181 imsg_init(ibuf, imsg_fds[0]);
182 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
184 return request_object(obj, repo, obj_fd);
187 static const struct got_error *
188 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
189 struct got_object_id *id)
191 const struct got_error *err = NULL;
192 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
194 err = got_privsep_send_packed_obj_req(ibuf, idx);
198 err = got_privsep_recv_obj(obj, ibuf);
202 (*obj)->path_packfile = strdup(pack->path_packfile);
203 if ((*obj)->path_packfile == NULL) {
204 err = got_error_from_errno();
207 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
212 const struct got_error *
213 got_object_packed_read_privsep(struct got_object **obj,
214 struct got_repository *repo, struct got_pack *pack,
215 struct got_packidx *packidx, int idx, struct got_object_id *id)
217 const struct got_error *err = NULL;
220 struct imsgbuf *ibuf;
222 if (pack->privsep_child)
223 return request_packed_object(obj, pack, idx, id);
225 ibuf = calloc(1, sizeof(*ibuf));
227 return got_error_from_errno();
229 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
230 if (pack->privsep_child == NULL) {
231 err = got_error_from_errno();
236 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
237 err = got_error_from_errno();
243 err = got_error_from_errno();
245 } else if (pid == 0) {
246 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
247 pack->path_packfile);
252 pack->privsep_child->imsg_fd = imsg_fds[0];
253 pack->privsep_child->pid = pid;
254 imsg_init(ibuf, imsg_fds[0]);
255 pack->privsep_child->ibuf = ibuf;
257 err = got_privsep_init_pack_child(ibuf, pack, packidx);
259 const struct got_error *child_err;
260 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
261 child_err = got_privsep_wait_for_child(
262 pack->privsep_child->pid);
263 if (child_err && err == NULL)
266 free(pack->privsep_child);
267 pack->privsep_child = NULL;
274 free(pack->privsep_child);
275 pack->privsep_child = NULL;
277 err = request_packed_object(obj, pack, idx, id);
281 struct got_commit_object *
282 got_object_commit_alloc_partial(void)
284 struct got_commit_object *commit;
286 commit = calloc(1, sizeof(*commit));
289 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
290 if (commit->tree_id == NULL) {
295 SIMPLEQ_INIT(&commit->parent_ids);
300 const struct got_error *
301 got_object_commit_add_parent(struct got_commit_object *commit,
304 const struct got_error *err = NULL;
305 struct got_object_qid *qid;
307 qid = malloc(sizeof(*qid));
309 return got_error_from_errno();
311 qid->id = malloc(sizeof(*qid->id));
312 if (qid->id == NULL) {
313 err = got_error_from_errno();
314 got_object_qid_free(qid);
318 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
319 err = got_error(GOT_ERR_BAD_OBJ_DATA);
325 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
331 static const struct got_error *
332 parse_gmtoff(time_t *gmtoff, const char *tzstr)
335 const char *p = tzstr;
343 return got_error(GOT_ERR_BAD_OBJ_DATA);
345 if (!isdigit(*p) && !isdigit(*(p + 1)))
346 return got_error(GOT_ERR_BAD_OBJ_DATA);
347 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
350 if (!isdigit(*p) && !isdigit(*(p + 1)))
351 return got_error(GOT_ERR_BAD_OBJ_DATA);
352 m = ((*p - '0') * 10) + (*(p + 1) - '0');
354 *gmtoff = (h * 60 * 60 + m * 60) * sign;
358 static const struct got_error *
359 parse_commit_time(struct tm *tm, char *committer)
361 const struct got_error *err = NULL;
367 /* Parse and strip off trailing timezone indicator string. */
368 space = strrchr(committer, ' ');
370 return got_error(GOT_ERR_BAD_OBJ_DATA);
371 tzstr = strdup(space + 1);
373 return got_error_from_errno();
374 err = parse_gmtoff(&gmtoff, tzstr);
380 /* Timestamp is separated from committer name + email by space. */
381 space = strrchr(committer, ' ');
383 return got_error(GOT_ERR_BAD_OBJ_DATA);
385 /* Timestamp parsed here is expressed in comitter's local time. */
386 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
388 return got_error(GOT_ERR_BAD_OBJ_DATA);
390 /* Express the time stamp in UTC. */
391 memset(tm, 0, sizeof(*tm));
393 if (localtime_r(&time, tm) == NULL)
394 return got_error_from_errno();
395 tm->tm_gmtoff = gmtoff;
397 /* Strip off parsed time information, leaving just author and email. */
404 got_object_commit_close(struct got_commit_object *commit)
406 struct got_object_qid *qid;
408 if (commit->refcnt > 0) {
410 if (commit->refcnt > 0)
414 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
415 qid = SIMPLEQ_FIRST(&commit->parent_ids);
416 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
417 got_object_qid_free(qid);
420 free(commit->tree_id);
421 free(commit->author);
422 free(commit->committer);
423 free(commit->logmsg);
427 const struct got_error *
428 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
430 const struct got_error *err = NULL;
433 ssize_t remain = (ssize_t)len;
435 *commit = got_object_commit_alloc_partial();
437 return got_error_from_errno();
439 tlen = strlen(GOT_COMMIT_TAG_TREE);
440 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
442 if (remain < SHA1_DIGEST_STRING_LENGTH) {
443 err = got_error(GOT_ERR_BAD_OBJ_DATA);
447 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
448 err = got_error(GOT_ERR_BAD_OBJ_DATA);
451 remain -= SHA1_DIGEST_STRING_LENGTH;
452 s += SHA1_DIGEST_STRING_LENGTH;
454 err = got_error(GOT_ERR_BAD_OBJ_DATA);
458 tlen = strlen(GOT_COMMIT_TAG_PARENT);
459 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
461 if (remain < SHA1_DIGEST_STRING_LENGTH) {
462 err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 err = got_object_commit_add_parent(*commit, s);
470 remain -= SHA1_DIGEST_STRING_LENGTH;
471 s += SHA1_DIGEST_STRING_LENGTH;
474 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
475 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
481 err = got_error(GOT_ERR_BAD_OBJ_DATA);
487 err = got_error(GOT_ERR_BAD_OBJ_DATA);
492 err = parse_commit_time(&(*commit)->tm_author, s);
495 (*commit)->author = strdup(s);
496 if ((*commit)->author == NULL) {
497 err = got_error_from_errno();
504 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
505 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
511 err = got_error(GOT_ERR_BAD_OBJ_DATA);
517 err = got_error(GOT_ERR_BAD_OBJ_DATA);
522 err = parse_commit_time(&(*commit)->tm_committer, s);
525 (*commit)->committer = strdup(s);
526 if ((*commit)->committer == NULL) {
527 err = got_error_from_errno();
534 (*commit)->logmsg = strndup(s, remain);
535 if ((*commit)->logmsg == NULL) {
536 err = got_error_from_errno();
541 got_object_commit_close(*commit);
548 got_object_tree_entry_close(struct got_tree_entry *te)
556 got_object_tree_close(struct got_tree_object *tree)
558 struct got_tree_entry *te;
560 if (tree->refcnt > 0) {
562 if (tree->refcnt > 0)
566 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
567 te = SIMPLEQ_FIRST(&tree->entries.head);
568 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
569 got_object_tree_entry_close(te);
575 struct got_tree_entry *
576 got_alloc_tree_entry_partial(void)
578 struct got_tree_entry *te;
580 te = calloc(1, sizeof(*te));
584 te->id = calloc(1, sizeof(*te->id));
585 if (te->id == NULL) {
592 static const struct got_error *
593 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
596 char *p = buf, *space;
597 const struct got_error *err = NULL;
599 *te = got_alloc_tree_entry_partial();
601 return got_error_from_errno();
603 *elen = strlen(buf) + 1;
604 if (*elen > maxlen) {
607 return got_error(GOT_ERR_BAD_OBJ_DATA);
610 space = strchr(buf, ' ');
612 err = got_error(GOT_ERR_BAD_OBJ_DATA);
618 if (*p < '0' && *p > '7') {
619 err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 (*te)->mode |= *p - '0';
627 (*te)->name = strdup(space + 1);
628 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
629 err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 buf += strlen(buf) + 1;
633 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
634 *elen += SHA1_DIGEST_LENGTH;
637 got_object_tree_entry_close(*te);
643 const struct got_error *
644 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
646 const struct got_error *err;
649 *tree = calloc(1, sizeof(**tree));
651 return got_error_from_errno();
653 SIMPLEQ_INIT(&(*tree)->entries.head);
656 struct got_tree_entry *te;
659 err = parse_tree_entry(&te, &elen, buf, remain);
662 (*tree)->entries.nentries++;
663 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
669 got_object_tree_close(*tree);
670 return got_error(GOT_ERR_BAD_OBJ_DATA);
676 const struct got_error *
677 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
679 const struct got_error *err = NULL;
680 static const size_t blocksize = 512;
681 size_t n, total, remain;
687 buf = malloc(blocksize);
689 return got_error_from_errno();
696 newbuf = reallocarray(buf, 1, total + blocksize);
697 if (newbuf == NULL) {
698 err = got_error_from_errno();
704 n = fread(buf + total, 1, remain, f);
707 err = got_ferror(f, GOT_ERR_IO);
725 static const struct got_error *
726 request_commit(struct got_commit_object **commit, struct got_repository *repo,
727 struct got_object *obj, int fd)
729 const struct got_error *err = NULL;
730 struct imsgbuf *ibuf;
732 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
734 err = got_privsep_send_obj_req(ibuf, fd, obj);
738 return got_privsep_recv_commit(commit, ibuf);
741 const struct got_error *
742 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
743 struct got_object *obj, struct got_pack *pack)
745 const struct got_error *err = NULL;
747 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
751 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
754 const struct got_error *
755 got_object_read_commit_privsep(struct got_commit_object **commit,
756 struct got_object *obj, int obj_fd, struct got_repository *repo)
760 struct imsgbuf *ibuf;
762 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
763 return request_commit(commit, repo, obj, obj_fd);
765 ibuf = calloc(1, sizeof(*ibuf));
767 return got_error_from_errno();
769 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
770 return got_error_from_errno();
774 return got_error_from_errno();
776 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
782 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
784 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
785 imsg_init(ibuf, imsg_fds[0]);
786 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
788 return request_commit(commit, repo, obj, obj_fd);
791 static const struct got_error *
792 request_tree(struct got_tree_object **tree, struct got_repository *repo,
793 struct got_object *obj, int fd)
795 const struct got_error *err = NULL;
796 struct imsgbuf *ibuf;
798 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
800 err = got_privsep_send_obj_req(ibuf, fd, obj);
804 return got_privsep_recv_tree(tree, ibuf);
807 const struct got_error *
808 got_object_read_tree_privsep(struct got_tree_object **tree,
809 struct got_object *obj, int obj_fd, struct got_repository *repo)
813 struct imsgbuf *ibuf;
815 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
816 return request_tree(tree, repo, obj, obj_fd);
818 ibuf = calloc(1, sizeof(*ibuf));
820 return got_error_from_errno();
822 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
823 return got_error_from_errno();
827 return got_error_from_errno();
829 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
836 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
838 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
839 imsg_init(ibuf, imsg_fds[0]);
840 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
843 return request_tree(tree, repo, obj, obj_fd);
846 const struct got_error *
847 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
848 struct got_object *obj, struct got_pack *pack)
850 const struct got_error *err = NULL;
852 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
856 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
859 static const struct got_error *
860 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
862 const struct got_error *err = NULL;
865 outfd_child = dup(outfd);
866 if (outfd_child == -1)
867 return got_error_from_errno();
869 err = got_privsep_send_blob_req(ibuf, infd);
873 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
879 err = got_privsep_recv_blob(size, ibuf);
883 if (lseek(outfd, SEEK_SET, 0) == -1)
884 return got_error_from_errno();
889 const struct got_error *
890 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
891 struct got_repository *repo)
895 struct imsgbuf *ibuf;
897 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
898 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
899 return request_blob(size, outfd, infd, ibuf);
902 ibuf = calloc(1, sizeof(*ibuf));
904 return got_error_from_errno();
906 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
907 return got_error_from_errno();
911 return got_error_from_errno();
913 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
919 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
921 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
922 imsg_init(ibuf, imsg_fds[0]);
923 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
925 return request_blob(size, outfd, infd, ibuf);