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_repository.h"
52 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #define GOT_OBJ_TAG_COMMIT "commit"
56 #define GOT_OBJ_TAG_TREE "tree"
57 #define GOT_OBJ_TAG_BLOB "blob"
59 #define GOT_COMMIT_TAG_TREE "tree "
60 #define GOT_COMMIT_TAG_PARENT "parent "
61 #define GOT_COMMIT_TAG_AUTHOR "author "
62 #define GOT_COMMIT_TAG_COMMITTER "committer "
65 got_object_close(struct got_object *obj)
67 if (obj->refcnt > 0) {
73 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
74 struct got_delta *delta;
75 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
76 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
77 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
78 got_delta_close(delta);
81 if (obj->flags & GOT_OBJ_FLAG_PACKED)
82 free(obj->path_packfile);
87 got_object_qid_free(struct got_object_qid *qid)
93 static const struct got_error *
94 request_object(struct got_object **obj, struct got_repository *repo, int fd)
96 const struct got_error *err = NULL;
99 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
101 err = got_privsep_send_obj_req(ibuf, fd, NULL);
105 return got_privsep_recv_obj(obj, ibuf);
109 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
113 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
114 fprintf(stderr, "%s: %s\n", getprogname(),
118 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
119 fprintf(stderr, "%s: %s\n", getprogname(),
124 if (execl(path, path, repo_path, (char *)NULL) == -1) {
125 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
131 const struct got_error *
132 got_object_read_header_privsep(struct got_object **obj,
133 struct got_repository *repo, int obj_fd)
137 struct imsgbuf *ibuf;
139 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
140 return request_object(obj, repo, obj_fd);
142 ibuf = calloc(1, sizeof(*ibuf));
144 return got_error_from_errno();
146 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
147 return got_error_from_errno();
151 return got_error_from_errno();
153 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
159 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
161 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
162 imsg_init(ibuf, imsg_fds[0]);
163 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
165 return request_object(obj, repo, obj_fd);
168 static const struct got_error *
169 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
170 struct got_object_id *id)
172 const struct got_error *err = NULL;
173 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 err = got_privsep_send_packed_obj_req(ibuf, idx);
179 err = got_privsep_recv_obj(obj, ibuf);
183 (*obj)->path_packfile = strdup(pack->path_packfile);
184 if ((*obj)->path_packfile == NULL) {
185 err = got_error_from_errno();
188 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
193 const struct got_error *
194 got_object_packed_read_privsep(struct got_object **obj,
195 struct got_repository *repo, struct got_pack *pack,
196 struct got_packidx *packidx, int idx, struct got_object_id *id)
198 const struct got_error *err = NULL;
201 struct imsgbuf *ibuf;
203 if (pack->privsep_child)
204 return request_packed_object(obj, pack, idx, id);
206 ibuf = calloc(1, sizeof(*ibuf));
208 return got_error_from_errno();
210 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
211 if (pack->privsep_child == NULL) {
212 err = got_error_from_errno();
217 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
218 err = got_error_from_errno();
224 err = got_error_from_errno();
226 } else if (pid == 0) {
227 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
228 pack->path_packfile);
233 pack->privsep_child->imsg_fd = imsg_fds[0];
234 pack->privsep_child->pid = pid;
235 imsg_init(ibuf, imsg_fds[0]);
236 pack->privsep_child->ibuf = ibuf;
238 err = got_privsep_init_pack_child(ibuf, pack, packidx);
240 const struct got_error *child_err;
241 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
242 child_err = got_privsep_wait_for_child(
243 pack->privsep_child->pid);
244 if (child_err && err == NULL)
247 free(pack->privsep_child);
248 pack->privsep_child = NULL;
255 free(pack->privsep_child);
256 pack->privsep_child = NULL;
258 err = request_packed_object(obj, pack, idx, id);
262 struct got_commit_object *
263 got_object_commit_alloc_partial(void)
265 struct got_commit_object *commit;
267 commit = calloc(1, sizeof(*commit));
270 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
271 if (commit->tree_id == NULL) {
276 SIMPLEQ_INIT(&commit->parent_ids);
281 const struct got_error *
282 got_object_commit_add_parent(struct got_commit_object *commit,
285 const struct got_error *err = NULL;
286 struct got_object_qid *qid;
288 qid = malloc(sizeof(*qid));
290 return got_error_from_errno();
292 qid->id = malloc(sizeof(*qid->id));
293 if (qid->id == NULL) {
294 err = got_error_from_errno();
295 got_object_qid_free(qid);
299 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
300 err = got_error(GOT_ERR_BAD_OBJ_DATA);
306 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
312 static const struct got_error *
313 parse_gmtoff(time_t *gmtoff, const char *tzstr)
316 const char *p = tzstr;
324 return got_error(GOT_ERR_BAD_OBJ_DATA);
326 if (!isdigit(*p) && !isdigit(*(p + 1)))
327 return got_error(GOT_ERR_BAD_OBJ_DATA);
328 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
331 if (!isdigit(*p) && !isdigit(*(p + 1)))
332 return got_error(GOT_ERR_BAD_OBJ_DATA);
333 m = ((*p - '0') * 10) + (*(p + 1) - '0');
335 *gmtoff = (h * 60 * 60 + m * 60) * sign;
339 static const struct got_error *
340 parse_commit_time(struct tm *tm, char *committer)
342 const struct got_error *err = NULL;
348 /* Parse and strip off trailing timezone indicator string. */
349 space = strrchr(committer, ' ');
351 return got_error(GOT_ERR_BAD_OBJ_DATA);
352 tzstr = strdup(space + 1);
354 return got_error_from_errno();
355 err = parse_gmtoff(&gmtoff, tzstr);
361 /* Timestamp is separated from committer name + email by space. */
362 space = strrchr(committer, ' ');
364 return got_error(GOT_ERR_BAD_OBJ_DATA);
366 /* Timestamp parsed here is expressed in comitter's local time. */
367 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
369 return got_error(GOT_ERR_BAD_OBJ_DATA);
371 /* Express the time stamp in UTC. */
372 memset(tm, 0, sizeof(*tm));
374 if (localtime_r(&time, tm) == NULL)
375 return got_error_from_errno();
376 tm->tm_gmtoff = gmtoff;
378 /* Strip off parsed time information, leaving just author and email. */
385 got_object_commit_close(struct got_commit_object *commit)
387 struct got_object_qid *qid;
389 if (commit->refcnt > 0) {
391 if (commit->refcnt > 0)
395 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
396 qid = SIMPLEQ_FIRST(&commit->parent_ids);
397 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
398 got_object_qid_free(qid);
401 free(commit->tree_id);
402 free(commit->author);
403 free(commit->committer);
404 free(commit->logmsg);
408 const struct got_error *
409 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
411 const struct got_error *err = NULL;
414 ssize_t remain = (ssize_t)len;
416 *commit = got_object_commit_alloc_partial();
418 return got_error_from_errno();
420 tlen = strlen(GOT_COMMIT_TAG_TREE);
421 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
423 if (remain < SHA1_DIGEST_STRING_LENGTH) {
424 err = got_error(GOT_ERR_BAD_OBJ_DATA);
428 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
429 err = got_error(GOT_ERR_BAD_OBJ_DATA);
432 remain -= SHA1_DIGEST_STRING_LENGTH;
433 s += SHA1_DIGEST_STRING_LENGTH;
435 err = got_error(GOT_ERR_BAD_OBJ_DATA);
439 tlen = strlen(GOT_COMMIT_TAG_PARENT);
440 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
442 if (remain < SHA1_DIGEST_STRING_LENGTH) {
443 err = got_error(GOT_ERR_BAD_OBJ_DATA);
447 err = got_object_commit_add_parent(*commit, s);
451 remain -= SHA1_DIGEST_STRING_LENGTH;
452 s += SHA1_DIGEST_STRING_LENGTH;
455 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
456 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
462 err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 err = got_error(GOT_ERR_BAD_OBJ_DATA);
473 err = parse_commit_time(&(*commit)->tm_author, s);
476 (*commit)->author = strdup(s);
477 if ((*commit)->author == NULL) {
478 err = got_error_from_errno();
485 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
486 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
492 err = got_error(GOT_ERR_BAD_OBJ_DATA);
498 err = got_error(GOT_ERR_BAD_OBJ_DATA);
503 err = parse_commit_time(&(*commit)->tm_committer, s);
506 (*commit)->committer = strdup(s);
507 if ((*commit)->committer == NULL) {
508 err = got_error_from_errno();
515 (*commit)->logmsg = strndup(s, remain);
516 if ((*commit)->logmsg == NULL) {
517 err = got_error_from_errno();
522 got_object_commit_close(*commit);
529 got_object_tree_entry_close(struct got_tree_entry *te)
537 got_object_tree_close(struct got_tree_object *tree)
539 struct got_tree_entry *te;
541 if (tree->refcnt > 0) {
543 if (tree->refcnt > 0)
547 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
548 te = SIMPLEQ_FIRST(&tree->entries.head);
549 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
550 got_object_tree_entry_close(te);
556 struct got_tree_entry *
557 got_alloc_tree_entry_partial(void)
559 struct got_tree_entry *te;
561 te = calloc(1, sizeof(*te));
565 te->id = calloc(1, sizeof(*te->id));
566 if (te->id == NULL) {
573 static const struct got_error *
574 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
577 char *p = buf, *space;
578 const struct got_error *err = NULL;
580 *te = got_alloc_tree_entry_partial();
582 return got_error_from_errno();
584 *elen = strlen(buf) + 1;
585 if (*elen > maxlen) {
588 return got_error(GOT_ERR_BAD_OBJ_DATA);
591 space = strchr(buf, ' ');
593 err = got_error(GOT_ERR_BAD_OBJ_DATA);
599 if (*p < '0' && *p > '7') {
600 err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 (*te)->mode |= *p - '0';
608 (*te)->name = strdup(space + 1);
609 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
610 err = got_error(GOT_ERR_BAD_OBJ_DATA);
613 buf += strlen(buf) + 1;
614 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
615 *elen += SHA1_DIGEST_LENGTH;
618 got_object_tree_entry_close(*te);
624 const struct got_error *
625 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
627 const struct got_error *err;
630 *tree = calloc(1, sizeof(**tree));
632 return got_error_from_errno();
634 SIMPLEQ_INIT(&(*tree)->entries.head);
637 struct got_tree_entry *te;
640 err = parse_tree_entry(&te, &elen, buf, remain);
643 (*tree)->entries.nentries++;
644 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
650 got_object_tree_close(*tree);
651 return got_error(GOT_ERR_BAD_OBJ_DATA);
657 const struct got_error *
658 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
660 const struct got_error *err = NULL;
661 static const size_t blocksize = 512;
662 size_t n, total, remain;
668 buf = malloc(blocksize);
670 return got_error_from_errno();
677 newbuf = reallocarray(buf, 1, total + blocksize);
678 if (newbuf == NULL) {
679 err = got_error_from_errno();
685 n = fread(buf + total, 1, remain, f);
688 err = got_ferror(f, GOT_ERR_IO);
706 static const struct got_error *
707 request_commit(struct got_commit_object **commit, struct got_repository *repo,
708 struct got_object *obj, int fd)
710 const struct got_error *err = NULL;
711 struct imsgbuf *ibuf;
713 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
715 err = got_privsep_send_obj_req(ibuf, fd,obj);
719 return got_privsep_recv_commit(commit, ibuf);
722 const struct got_error *
723 got_object_read_commit_privsep(struct got_commit_object **commit,
724 struct got_object *obj, int obj_fd, struct got_repository *repo)
728 struct imsgbuf *ibuf;
730 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
731 return request_commit(commit, repo, obj, obj_fd);
733 ibuf = calloc(1, sizeof(*ibuf));
735 return got_error_from_errno();
737 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
738 return got_error_from_errno();
742 return got_error_from_errno();
744 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
750 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
752 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
753 imsg_init(ibuf, imsg_fds[0]);
754 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
756 return request_commit(commit, repo, obj, obj_fd);
759 static const struct got_error *
760 request_tree(struct got_tree_object **tree, struct got_repository *repo,
761 struct got_object *obj, int fd)
763 const struct got_error *err = NULL;
764 struct imsgbuf *ibuf;
766 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
768 err = got_privsep_send_obj_req(ibuf, fd,obj);
772 return got_privsep_recv_tree(tree, ibuf);
775 const struct got_error *
776 got_object_read_tree_privsep(struct got_tree_object **tree,
777 struct got_object *obj, int obj_fd, struct got_repository *repo)
781 struct imsgbuf *ibuf;
783 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
784 return request_tree(tree, repo, obj, obj_fd);
786 ibuf = calloc(1, sizeof(*ibuf));
788 return got_error_from_errno();
790 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
791 return got_error_from_errno();
795 return got_error_from_errno();
797 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
804 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
806 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
807 imsg_init(ibuf, imsg_fds[0]);
808 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
811 return request_tree(tree, repo, obj, obj_fd);
814 static const struct got_error *
815 request_blob(size_t *size, int outfd, int infd, struct got_repository *repo)
817 const struct got_error *err = NULL;
819 struct imsgbuf *ibuf;
821 outfd_child = dup(outfd);
822 if (outfd_child == -1)
823 return got_error_from_errno();
825 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
827 err = got_privsep_send_blob_req(ibuf, outfd_child, infd);
831 err = got_privsep_recv_blob(size, ibuf);
835 if (lseek(outfd, SEEK_SET, 0) == -1)
836 return got_error_from_errno();
841 const struct got_error *
842 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
843 struct got_repository *repo)
847 struct imsgbuf *ibuf;
849 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1)
850 return request_blob(size, outfd, infd, repo);
852 ibuf = calloc(1, sizeof(*ibuf));
854 return got_error_from_errno();
856 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
857 return got_error_from_errno();
861 return got_error_from_errno();
863 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
869 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
871 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
872 imsg_init(ibuf, imsg_fds[0]);
873 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
875 return request_blob(size, outfd, infd, repo);