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>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_repository.h"
51 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #define GOT_OBJ_TAG_COMMIT "commit"
55 #define GOT_OBJ_TAG_TREE "tree"
56 #define GOT_OBJ_TAG_BLOB "blob"
58 #define GOT_COMMIT_TAG_TREE "tree "
59 #define GOT_COMMIT_TAG_PARENT "parent "
60 #define GOT_COMMIT_TAG_AUTHOR "author "
61 #define GOT_COMMIT_TAG_COMMITTER "committer "
64 got_object_close(struct got_object *obj)
66 if (obj->refcnt > 0) {
72 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
73 struct got_delta *delta;
74 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
75 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
76 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
77 got_delta_close(delta);
80 if (obj->flags & GOT_OBJ_FLAG_PACKED)
81 free(obj->path_packfile);
86 got_object_qid_free(struct got_object_qid *qid)
92 static const struct got_error *
93 request_object(struct got_object **obj, struct got_repository *repo, int fd)
95 const struct got_error *err = NULL;
98 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
100 err = got_privsep_send_obj_req(ibuf, fd, NULL);
104 return got_privsep_recv_obj(obj, ibuf);
108 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
112 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
113 fprintf(stderr, "%s: %s\n", getprogname(),
117 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
118 fprintf(stderr, "%s: %s\n", getprogname(),
123 if (execl(path, path, repo_path, (char *)NULL) == -1) {
124 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
130 const struct got_error *
131 got_object_read_header_privsep(struct got_object **obj,
132 struct got_repository *repo, int obj_fd)
136 struct imsgbuf *ibuf;
138 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
139 return request_object(obj, repo, obj_fd);
141 ibuf = calloc(1, sizeof(*ibuf));
143 return got_error_from_errno();
145 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
146 return got_error_from_errno();
150 return got_error_from_errno();
152 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
158 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
160 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
161 imsg_init(ibuf, imsg_fds[0]);
162 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
164 return request_object(obj, repo, obj_fd);
167 struct got_commit_object *
168 got_object_commit_alloc_partial(void)
170 struct got_commit_object *commit;
172 commit = calloc(1, sizeof(*commit));
175 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
176 if (commit->tree_id == NULL) {
181 SIMPLEQ_INIT(&commit->parent_ids);
186 const struct got_error *
187 got_object_commit_add_parent(struct got_commit_object *commit,
190 const struct got_error *err = NULL;
191 struct got_object_qid *qid;
193 qid = malloc(sizeof(*qid));
195 return got_error_from_errno();
197 qid->id = malloc(sizeof(*qid->id));
198 if (qid->id == NULL) {
199 err = got_error_from_errno();
200 got_object_qid_free(qid);
204 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
205 err = got_error(GOT_ERR_BAD_OBJ_DATA);
211 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
217 static const struct got_error *
218 parse_gmtoff(time_t *gmtoff, const char *tzstr)
221 const char *p = tzstr;
229 return got_error(GOT_ERR_BAD_OBJ_DATA);
231 if (!isdigit(*p) && !isdigit(*(p + 1)))
232 return got_error(GOT_ERR_BAD_OBJ_DATA);
233 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
236 if (!isdigit(*p) && !isdigit(*(p + 1)))
237 return got_error(GOT_ERR_BAD_OBJ_DATA);
238 m = ((*p - '0') * 10) + (*(p + 1) - '0');
240 *gmtoff = (h * 60 * 60 + m * 60) * sign;
244 static const struct got_error *
245 parse_commit_time(struct tm *tm, char *committer)
247 const struct got_error *err = NULL;
253 /* Parse and strip off trailing timezone indicator string. */
254 space = strrchr(committer, ' ');
256 return got_error(GOT_ERR_BAD_OBJ_DATA);
257 tzstr = strdup(space + 1);
259 return got_error_from_errno();
260 err = parse_gmtoff(&gmtoff, tzstr);
266 /* Timestamp is separated from committer name + email by space. */
267 space = strrchr(committer, ' ');
269 return got_error(GOT_ERR_BAD_OBJ_DATA);
271 /* Timestamp parsed here is expressed in comitter's local time. */
272 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
274 return got_error(GOT_ERR_BAD_OBJ_DATA);
276 /* Express the time stamp in UTC. */
277 memset(tm, 0, sizeof(*tm));
279 if (localtime_r(&time, tm) == NULL)
280 return got_error_from_errno();
281 tm->tm_gmtoff = gmtoff;
283 /* Strip off parsed time information, leaving just author and email. */
290 got_object_commit_close(struct got_commit_object *commit)
292 struct got_object_qid *qid;
294 if (commit->refcnt > 0) {
296 if (commit->refcnt > 0)
300 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
301 qid = SIMPLEQ_FIRST(&commit->parent_ids);
302 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
303 got_object_qid_free(qid);
306 free(commit->tree_id);
307 free(commit->author);
308 free(commit->committer);
309 free(commit->logmsg);
313 const struct got_error *
314 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
316 const struct got_error *err = NULL;
319 ssize_t remain = (ssize_t)len;
321 *commit = got_object_commit_alloc_partial();
323 return got_error_from_errno();
325 tlen = strlen(GOT_COMMIT_TAG_TREE);
326 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
328 if (remain < SHA1_DIGEST_STRING_LENGTH) {
329 err = got_error(GOT_ERR_BAD_OBJ_DATA);
333 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
334 err = got_error(GOT_ERR_BAD_OBJ_DATA);
337 remain -= SHA1_DIGEST_STRING_LENGTH;
338 s += SHA1_DIGEST_STRING_LENGTH;
340 err = got_error(GOT_ERR_BAD_OBJ_DATA);
344 tlen = strlen(GOT_COMMIT_TAG_PARENT);
345 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
347 if (remain < SHA1_DIGEST_STRING_LENGTH) {
348 err = got_error(GOT_ERR_BAD_OBJ_DATA);
352 err = got_object_commit_add_parent(*commit, s);
356 remain -= SHA1_DIGEST_STRING_LENGTH;
357 s += SHA1_DIGEST_STRING_LENGTH;
360 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
361 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
367 err = got_error(GOT_ERR_BAD_OBJ_DATA);
373 err = got_error(GOT_ERR_BAD_OBJ_DATA);
378 err = parse_commit_time(&(*commit)->tm_author, s);
381 (*commit)->author = strdup(s);
382 if ((*commit)->author == NULL) {
383 err = got_error_from_errno();
390 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
391 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
397 err = got_error(GOT_ERR_BAD_OBJ_DATA);
403 err = got_error(GOT_ERR_BAD_OBJ_DATA);
408 err = parse_commit_time(&(*commit)->tm_committer, s);
411 (*commit)->committer = strdup(s);
412 if ((*commit)->committer == NULL) {
413 err = got_error_from_errno();
420 (*commit)->logmsg = strndup(s, remain);
421 if ((*commit)->logmsg == NULL) {
422 err = got_error_from_errno();
427 got_object_commit_close(*commit);
434 got_object_tree_entry_close(struct got_tree_entry *te)
442 got_object_tree_close(struct got_tree_object *tree)
444 struct got_tree_entry *te;
446 if (tree->refcnt > 0) {
448 if (tree->refcnt > 0)
452 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
453 te = SIMPLEQ_FIRST(&tree->entries.head);
454 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
455 got_object_tree_entry_close(te);
461 struct got_tree_entry *
462 got_alloc_tree_entry_partial(void)
464 struct got_tree_entry *te;
466 te = calloc(1, sizeof(*te));
470 te->id = calloc(1, sizeof(*te->id));
471 if (te->id == NULL) {
478 static const struct got_error *
479 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
482 char *p = buf, *space;
483 const struct got_error *err = NULL;
485 *te = got_alloc_tree_entry_partial();
487 return got_error_from_errno();
489 *elen = strlen(buf) + 1;
490 if (*elen > maxlen) {
493 return got_error(GOT_ERR_BAD_OBJ_DATA);
496 space = strchr(buf, ' ');
498 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 if (*p < '0' && *p > '7') {
505 err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 (*te)->mode |= *p - '0';
513 (*te)->name = strdup(space + 1);
514 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
515 err = got_error(GOT_ERR_BAD_OBJ_DATA);
518 buf += strlen(buf) + 1;
519 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
520 *elen += SHA1_DIGEST_LENGTH;
523 got_object_tree_entry_close(*te);
529 const struct got_error *
530 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
532 const struct got_error *err;
535 *tree = calloc(1, sizeof(**tree));
537 return got_error_from_errno();
539 SIMPLEQ_INIT(&(*tree)->entries.head);
542 struct got_tree_entry *te;
545 err = parse_tree_entry(&te, &elen, buf, remain);
548 (*tree)->entries.nentries++;
549 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
555 got_object_tree_close(*tree);
556 return got_error(GOT_ERR_BAD_OBJ_DATA);
562 const struct got_error *
563 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
565 const struct got_error *err = NULL;
566 static const size_t blocksize = 512;
567 size_t n, total, remain;
573 buf = malloc(blocksize);
575 return got_error_from_errno();
582 newbuf = reallocarray(buf, 1, total + blocksize);
583 if (newbuf == NULL) {
584 err = got_error_from_errno();
590 n = fread(buf + total, 1, remain, f);
593 err = got_ferror(f, GOT_ERR_IO);
611 static const struct got_error *
612 request_commit(struct got_commit_object **commit, struct got_repository *repo,
613 struct got_object *obj, int fd)
615 const struct got_error *err = NULL;
616 struct imsgbuf *ibuf;
618 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
620 err = got_privsep_send_obj_req(ibuf, fd,obj);
624 return got_privsep_recv_commit(commit, ibuf);
627 const struct got_error *
628 got_object_read_commit_privsep(struct got_commit_object **commit,
629 struct got_object *obj, int obj_fd, struct got_repository *repo)
633 struct imsgbuf *ibuf;
635 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
636 return request_commit(commit, repo, obj, obj_fd);
638 ibuf = calloc(1, sizeof(*ibuf));
640 return got_error_from_errno();
642 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
643 return got_error_from_errno();
647 return got_error_from_errno();
649 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
655 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
657 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
658 imsg_init(ibuf, imsg_fds[0]);
659 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
661 return request_commit(commit, repo, obj, obj_fd);
664 static const struct got_error *
665 request_tree(struct got_tree_object **tree, struct got_repository *repo,
666 struct got_object *obj, int fd)
668 const struct got_error *err = NULL;
669 struct imsgbuf *ibuf;
671 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
673 err = got_privsep_send_obj_req(ibuf, fd,obj);
677 return got_privsep_recv_tree(tree, ibuf);
680 const struct got_error *
681 got_object_read_tree_privsep(struct got_tree_object **tree,
682 struct got_object *obj, int obj_fd, struct got_repository *repo)
686 struct imsgbuf *ibuf;
688 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
689 return request_tree(tree, repo, obj, obj_fd);
691 ibuf = calloc(1, sizeof(*ibuf));
693 return got_error_from_errno();
695 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
696 return got_error_from_errno();
700 return got_error_from_errno();
702 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
709 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
711 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
712 imsg_init(ibuf, imsg_fds[0]);
713 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
716 return request_tree(tree, repo, obj, obj_fd);
719 static const struct got_error *
720 request_blob(size_t *size, int outfd, int infd, struct got_repository *repo)
722 const struct got_error *err = NULL;
724 struct imsgbuf *ibuf;
726 outfd_child = dup(outfd);
727 if (outfd_child == -1)
728 return got_error_from_errno();
730 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
732 err = got_privsep_send_blob_req(ibuf, outfd_child, infd);
736 err = got_privsep_recv_blob(size, ibuf);
740 if (lseek(outfd, SEEK_SET, 0) == -1)
741 return got_error_from_errno();
746 const struct got_error *
747 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
748 struct got_repository *repo)
752 struct imsgbuf *ibuf;
754 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1)
755 return request_blob(size, outfd, infd, repo);
757 ibuf = calloc(1, sizeof(*ibuf));
759 return got_error_from_errno();
761 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
762 return got_error_from_errno();
766 return got_error_from_errno();
768 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
774 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
776 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
777 imsg_init(ibuf, imsg_fds[0]);
778 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
780 return request_blob(size, outfd, infd, repo);