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 "
66 got_object_close(struct got_object *obj)
68 if (obj->refcnt > 0) {
74 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
75 struct got_delta *delta;
76 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
77 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
78 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
79 got_delta_close(delta);
82 if (obj->flags & GOT_OBJ_FLAG_PACKED)
83 free(obj->path_packfile);
88 got_object_qid_free(struct got_object_qid *qid)
94 static const struct got_error *
95 request_object(struct got_object **obj, struct got_repository *repo, int fd)
97 const struct got_error *err = NULL;
100 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
102 err = got_privsep_send_obj_req(ibuf, fd, NULL);
106 return got_privsep_recv_obj(obj, ibuf);
110 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
114 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
115 fprintf(stderr, "%s: %s\n", getprogname(),
119 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
120 fprintf(stderr, "%s: %s\n", getprogname(),
125 if (execl(path, path, repo_path, (char *)NULL) == -1) {
126 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
132 const struct got_error *
133 got_object_read_header_privsep(struct got_object **obj,
134 struct got_repository *repo, int obj_fd)
138 struct imsgbuf *ibuf;
140 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
141 return request_object(obj, repo, obj_fd);
143 ibuf = calloc(1, sizeof(*ibuf));
145 return got_error_from_errno();
147 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
148 return got_error_from_errno();
152 return got_error_from_errno();
154 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
160 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
162 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
163 imsg_init(ibuf, imsg_fds[0]);
164 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
166 return request_object(obj, repo, obj_fd);
169 static const struct got_error *
170 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
171 struct got_object_id *id)
173 const struct got_error *err = NULL;
174 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
176 err = got_privsep_send_packed_obj_req(ibuf, idx);
180 err = got_privsep_recv_obj(obj, ibuf);
184 (*obj)->path_packfile = strdup(pack->path_packfile);
185 if ((*obj)->path_packfile == NULL) {
186 err = got_error_from_errno();
189 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
194 const struct got_error *
195 got_object_packed_read_privsep(struct got_object **obj,
196 struct got_repository *repo, struct got_pack *pack,
197 struct got_packidx *packidx, int idx, struct got_object_id *id)
199 const struct got_error *err = NULL;
202 struct imsgbuf *ibuf;
204 if (pack->privsep_child)
205 return request_packed_object(obj, pack, idx, id);
207 ibuf = calloc(1, sizeof(*ibuf));
209 return got_error_from_errno();
211 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
212 if (pack->privsep_child == NULL) {
213 err = got_error_from_errno();
218 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
219 err = got_error_from_errno();
225 err = got_error_from_errno();
227 } else if (pid == 0) {
228 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
229 pack->path_packfile);
234 pack->privsep_child->imsg_fd = imsg_fds[0];
235 pack->privsep_child->pid = pid;
236 imsg_init(ibuf, imsg_fds[0]);
237 pack->privsep_child->ibuf = ibuf;
239 err = got_privsep_init_pack_child(ibuf, pack, packidx);
241 const struct got_error *child_err;
242 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
243 child_err = got_privsep_wait_for_child(
244 pack->privsep_child->pid);
245 if (child_err && err == NULL)
248 free(pack->privsep_child);
249 pack->privsep_child = NULL;
256 free(pack->privsep_child);
257 pack->privsep_child = NULL;
259 err = request_packed_object(obj, pack, idx, id);
263 struct got_commit_object *
264 got_object_commit_alloc_partial(void)
266 struct got_commit_object *commit;
268 commit = calloc(1, sizeof(*commit));
271 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
272 if (commit->tree_id == NULL) {
277 SIMPLEQ_INIT(&commit->parent_ids);
282 const struct got_error *
283 got_object_commit_add_parent(struct got_commit_object *commit,
286 const struct got_error *err = NULL;
287 struct got_object_qid *qid;
289 qid = malloc(sizeof(*qid));
291 return got_error_from_errno();
293 qid->id = malloc(sizeof(*qid->id));
294 if (qid->id == NULL) {
295 err = got_error_from_errno();
296 got_object_qid_free(qid);
300 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
301 err = got_error(GOT_ERR_BAD_OBJ_DATA);
307 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
313 static const struct got_error *
314 parse_gmtoff(time_t *gmtoff, const char *tzstr)
317 const char *p = tzstr;
325 return got_error(GOT_ERR_BAD_OBJ_DATA);
327 if (!isdigit(*p) && !isdigit(*(p + 1)))
328 return got_error(GOT_ERR_BAD_OBJ_DATA);
329 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
332 if (!isdigit(*p) && !isdigit(*(p + 1)))
333 return got_error(GOT_ERR_BAD_OBJ_DATA);
334 m = ((*p - '0') * 10) + (*(p + 1) - '0');
336 *gmtoff = (h * 60 * 60 + m * 60) * sign;
340 static const struct got_error *
341 parse_commit_time(struct tm *tm, char *committer)
343 const struct got_error *err = NULL;
349 /* Parse and strip off trailing timezone indicator string. */
350 space = strrchr(committer, ' ');
352 return got_error(GOT_ERR_BAD_OBJ_DATA);
353 tzstr = strdup(space + 1);
355 return got_error_from_errno();
356 err = parse_gmtoff(&gmtoff, tzstr);
362 /* Timestamp is separated from committer name + email by space. */
363 space = strrchr(committer, ' ');
365 return got_error(GOT_ERR_BAD_OBJ_DATA);
367 /* Timestamp parsed here is expressed in comitter's local time. */
368 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
370 return got_error(GOT_ERR_BAD_OBJ_DATA);
372 /* Express the time stamp in UTC. */
373 memset(tm, 0, sizeof(*tm));
375 if (localtime_r(&time, tm) == NULL)
376 return got_error_from_errno();
377 tm->tm_gmtoff = gmtoff;
379 /* Strip off parsed time information, leaving just author and email. */
386 got_object_commit_close(struct got_commit_object *commit)
388 struct got_object_qid *qid;
390 if (commit->refcnt > 0) {
392 if (commit->refcnt > 0)
396 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
397 qid = SIMPLEQ_FIRST(&commit->parent_ids);
398 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
399 got_object_qid_free(qid);
402 free(commit->tree_id);
403 free(commit->author);
404 free(commit->committer);
405 free(commit->logmsg);
409 const struct got_error *
410 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
412 const struct got_error *err = NULL;
415 ssize_t remain = (ssize_t)len;
417 *commit = got_object_commit_alloc_partial();
419 return got_error_from_errno();
421 tlen = strlen(GOT_COMMIT_TAG_TREE);
422 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
424 if (remain < SHA1_DIGEST_STRING_LENGTH) {
425 err = got_error(GOT_ERR_BAD_OBJ_DATA);
429 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
430 err = got_error(GOT_ERR_BAD_OBJ_DATA);
433 remain -= SHA1_DIGEST_STRING_LENGTH;
434 s += SHA1_DIGEST_STRING_LENGTH;
436 err = got_error(GOT_ERR_BAD_OBJ_DATA);
440 tlen = strlen(GOT_COMMIT_TAG_PARENT);
441 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
443 if (remain < SHA1_DIGEST_STRING_LENGTH) {
444 err = got_error(GOT_ERR_BAD_OBJ_DATA);
448 err = got_object_commit_add_parent(*commit, s);
452 remain -= SHA1_DIGEST_STRING_LENGTH;
453 s += SHA1_DIGEST_STRING_LENGTH;
456 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
457 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
463 err = got_error(GOT_ERR_BAD_OBJ_DATA);
469 err = got_error(GOT_ERR_BAD_OBJ_DATA);
474 err = parse_commit_time(&(*commit)->tm_author, s);
477 (*commit)->author = strdup(s);
478 if ((*commit)->author == NULL) {
479 err = got_error_from_errno();
486 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
487 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
493 err = got_error(GOT_ERR_BAD_OBJ_DATA);
499 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 err = parse_commit_time(&(*commit)->tm_committer, s);
507 (*commit)->committer = strdup(s);
508 if ((*commit)->committer == NULL) {
509 err = got_error_from_errno();
516 (*commit)->logmsg = strndup(s, remain);
517 if ((*commit)->logmsg == NULL) {
518 err = got_error_from_errno();
523 got_object_commit_close(*commit);
530 got_object_tree_entry_close(struct got_tree_entry *te)
538 got_object_tree_close(struct got_tree_object *tree)
540 struct got_tree_entry *te;
542 if (tree->refcnt > 0) {
544 if (tree->refcnt > 0)
548 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
549 te = SIMPLEQ_FIRST(&tree->entries.head);
550 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
551 got_object_tree_entry_close(te);
557 struct got_tree_entry *
558 got_alloc_tree_entry_partial(void)
560 struct got_tree_entry *te;
562 te = calloc(1, sizeof(*te));
566 te->id = calloc(1, sizeof(*te->id));
567 if (te->id == NULL) {
574 static const struct got_error *
575 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
578 char *p = buf, *space;
579 const struct got_error *err = NULL;
581 *te = got_alloc_tree_entry_partial();
583 return got_error_from_errno();
585 *elen = strlen(buf) + 1;
586 if (*elen > maxlen) {
589 return got_error(GOT_ERR_BAD_OBJ_DATA);
592 space = strchr(buf, ' ');
594 err = got_error(GOT_ERR_BAD_OBJ_DATA);
600 if (*p < '0' && *p > '7') {
601 err = got_error(GOT_ERR_BAD_OBJ_DATA);
605 (*te)->mode |= *p - '0';
609 (*te)->name = strdup(space + 1);
610 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
611 err = got_error(GOT_ERR_BAD_OBJ_DATA);
614 buf += strlen(buf) + 1;
615 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
616 *elen += SHA1_DIGEST_LENGTH;
619 got_object_tree_entry_close(*te);
625 const struct got_error *
626 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
628 const struct got_error *err;
631 *tree = calloc(1, sizeof(**tree));
633 return got_error_from_errno();
635 SIMPLEQ_INIT(&(*tree)->entries.head);
638 struct got_tree_entry *te;
641 err = parse_tree_entry(&te, &elen, buf, remain);
644 (*tree)->entries.nentries++;
645 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
651 got_object_tree_close(*tree);
652 return got_error(GOT_ERR_BAD_OBJ_DATA);
658 const struct got_error *
659 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
661 const struct got_error *err = NULL;
662 static const size_t blocksize = 512;
663 size_t n, total, remain;
669 buf = malloc(blocksize);
671 return got_error_from_errno();
678 newbuf = reallocarray(buf, 1, total + blocksize);
679 if (newbuf == NULL) {
680 err = got_error_from_errno();
686 n = fread(buf + total, 1, remain, f);
689 err = got_ferror(f, GOT_ERR_IO);
707 static const struct got_error *
708 request_commit(struct got_commit_object **commit, struct got_repository *repo,
709 struct got_object *obj, int fd)
711 const struct got_error *err = NULL;
712 struct imsgbuf *ibuf;
714 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
716 err = got_privsep_send_obj_req(ibuf, fd, obj);
720 return got_privsep_recv_commit(commit, ibuf);
723 const struct got_error *
724 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
725 struct got_object *obj, struct got_pack *pack)
727 const struct got_error *err = NULL;
729 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
733 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
736 const struct got_error *
737 got_object_read_commit_privsep(struct got_commit_object **commit,
738 struct got_object *obj, int obj_fd, struct got_repository *repo)
742 struct imsgbuf *ibuf;
744 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
745 return request_commit(commit, repo, obj, obj_fd);
747 ibuf = calloc(1, sizeof(*ibuf));
749 return got_error_from_errno();
751 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
752 return got_error_from_errno();
756 return got_error_from_errno();
758 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
764 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
766 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
767 imsg_init(ibuf, imsg_fds[0]);
768 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
770 return request_commit(commit, repo, obj, obj_fd);
773 static const struct got_error *
774 request_tree(struct got_tree_object **tree, struct got_repository *repo,
775 struct got_object *obj, int fd)
777 const struct got_error *err = NULL;
778 struct imsgbuf *ibuf;
780 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
782 err = got_privsep_send_obj_req(ibuf, fd, obj);
786 return got_privsep_recv_tree(tree, ibuf);
789 const struct got_error *
790 got_object_read_tree_privsep(struct got_tree_object **tree,
791 struct got_object *obj, int obj_fd, struct got_repository *repo)
795 struct imsgbuf *ibuf;
797 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
798 return request_tree(tree, repo, obj, obj_fd);
800 ibuf = calloc(1, sizeof(*ibuf));
802 return got_error_from_errno();
804 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
805 return got_error_from_errno();
809 return got_error_from_errno();
811 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
818 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
820 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
821 imsg_init(ibuf, imsg_fds[0]);
822 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
825 return request_tree(tree, repo, obj, obj_fd);
828 const struct got_error *
829 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
830 struct got_object *obj, struct got_pack *pack)
832 const struct got_error *err = NULL;
834 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
838 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
841 static const struct got_error *
842 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
844 const struct got_error *err = NULL;
847 outfd_child = dup(outfd);
848 if (outfd_child == -1)
849 return got_error_from_errno();
851 err = got_privsep_send_blob_req(ibuf, infd);
855 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
861 err = got_privsep_recv_blob(size, ibuf);
865 if (lseek(outfd, SEEK_SET, 0) == -1)
866 return got_error_from_errno();
871 const struct got_error *
872 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
873 struct got_repository *repo)
877 struct imsgbuf *ibuf;
879 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
880 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
881 return request_blob(size, outfd, infd, ibuf);
884 ibuf = calloc(1, sizeof(*ibuf));
886 return got_error_from_errno();
888 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
889 return got_error_from_errno();
893 return got_error_from_errno();
895 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
901 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
903 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
904 imsg_init(ibuf, imsg_fds[0]);
905 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
907 return request_blob(size, outfd, infd, ibuf);