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;
99 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd);
101 err = got_privsep_send_obj_req(&ibuf, fd, NULL);
104 err = got_privsep_recv_obj(obj, &ibuf);
111 exec_privsep_child(int imsg_fds[2], const char *path)
115 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
116 fprintf(stderr, "%s: %s\n", getprogname(),
120 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
121 fprintf(stderr, "%s: %s\n", getprogname(),
126 if (execl(path, path, (char *)NULL) == -1) {
127 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
133 const struct got_error *
134 got_object_read_header_privsep(struct got_object **obj,
135 struct got_repository *repo, int obj_fd)
140 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
141 return request_object(obj, repo, obj_fd);
143 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
144 return got_error_from_errno();
148 return got_error_from_errno();
150 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT);
155 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
157 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
159 return request_object(obj, repo, obj_fd);
162 struct got_commit_object *
163 got_object_commit_alloc_partial(void)
165 struct got_commit_object *commit;
167 commit = calloc(1, sizeof(*commit));
170 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
171 if (commit->tree_id == NULL) {
176 SIMPLEQ_INIT(&commit->parent_ids);
181 const struct got_error *
182 got_object_commit_add_parent(struct got_commit_object *commit,
185 const struct got_error *err = NULL;
186 struct got_object_qid *qid;
188 qid = malloc(sizeof(*qid));
190 return got_error_from_errno();
192 qid->id = malloc(sizeof(*qid->id));
193 if (qid->id == NULL) {
194 err = got_error_from_errno();
195 got_object_qid_free(qid);
199 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
200 err = got_error(GOT_ERR_BAD_OBJ_DATA);
206 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
212 static const struct got_error *
213 parse_gmtoff(time_t *gmtoff, const char *tzstr)
216 const char *p = tzstr;
224 return got_error(GOT_ERR_BAD_OBJ_DATA);
226 if (!isdigit(*p) && !isdigit(*(p + 1)))
227 return got_error(GOT_ERR_BAD_OBJ_DATA);
228 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
231 if (!isdigit(*p) && !isdigit(*(p + 1)))
232 return got_error(GOT_ERR_BAD_OBJ_DATA);
233 m = ((*p - '0') * 10) + (*(p + 1) - '0');
235 *gmtoff = (h * 60 * 60 + m * 60) * sign;
239 static const struct got_error *
240 parse_commit_time(struct tm *tm, char *committer)
242 const struct got_error *err = NULL;
248 /* Parse and strip off trailing timezone indicator string. */
249 space = strrchr(committer, ' ');
251 return got_error(GOT_ERR_BAD_OBJ_DATA);
252 tzstr = strdup(space + 1);
254 return got_error_from_errno();
255 err = parse_gmtoff(&gmtoff, tzstr);
261 /* Timestamp is separated from committer name + email by space. */
262 space = strrchr(committer, ' ');
264 return got_error(GOT_ERR_BAD_OBJ_DATA);
266 /* Timestamp parsed here is expressed in comitter's local time. */
267 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
269 return got_error(GOT_ERR_BAD_OBJ_DATA);
271 /* Express the time stamp in UTC. */
272 memset(tm, 0, sizeof(*tm));
274 if (localtime_r(&time, tm) == NULL)
275 return got_error_from_errno();
276 tm->tm_gmtoff = gmtoff;
278 /* Strip off parsed time information, leaving just author and email. */
285 got_object_commit_close(struct got_commit_object *commit)
287 struct got_object_qid *qid;
289 if (commit->refcnt > 0) {
291 if (commit->refcnt > 0)
295 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
296 qid = SIMPLEQ_FIRST(&commit->parent_ids);
297 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
298 got_object_qid_free(qid);
301 free(commit->tree_id);
302 free(commit->author);
303 free(commit->committer);
304 free(commit->logmsg);
308 const struct got_error *
309 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
311 const struct got_error *err = NULL;
314 ssize_t remain = (ssize_t)len;
316 *commit = got_object_commit_alloc_partial();
318 return got_error_from_errno();
320 tlen = strlen(GOT_COMMIT_TAG_TREE);
321 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
323 if (remain < SHA1_DIGEST_STRING_LENGTH) {
324 err = got_error(GOT_ERR_BAD_OBJ_DATA);
328 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
329 err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 remain -= SHA1_DIGEST_STRING_LENGTH;
333 s += SHA1_DIGEST_STRING_LENGTH;
335 err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 tlen = strlen(GOT_COMMIT_TAG_PARENT);
340 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
342 if (remain < SHA1_DIGEST_STRING_LENGTH) {
343 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 err = got_object_commit_add_parent(*commit, s);
351 remain -= SHA1_DIGEST_STRING_LENGTH;
352 s += SHA1_DIGEST_STRING_LENGTH;
355 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
356 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
362 err = got_error(GOT_ERR_BAD_OBJ_DATA);
368 err = got_error(GOT_ERR_BAD_OBJ_DATA);
373 err = parse_commit_time(&(*commit)->tm_author, s);
376 (*commit)->author = strdup(s);
377 if ((*commit)->author == NULL) {
378 err = got_error_from_errno();
385 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
386 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
392 err = got_error(GOT_ERR_BAD_OBJ_DATA);
398 err = got_error(GOT_ERR_BAD_OBJ_DATA);
403 err = parse_commit_time(&(*commit)->tm_committer, s);
406 (*commit)->committer = strdup(s);
407 if ((*commit)->committer == NULL) {
408 err = got_error_from_errno();
415 (*commit)->logmsg = strndup(s, remain);
416 if ((*commit)->logmsg == NULL) {
417 err = got_error_from_errno();
422 got_object_commit_close(*commit);
429 got_object_tree_entry_close(struct got_tree_entry *te)
437 got_object_tree_close(struct got_tree_object *tree)
439 struct got_tree_entry *te;
441 if (tree->refcnt > 0) {
443 if (tree->refcnt > 0)
447 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
448 te = SIMPLEQ_FIRST(&tree->entries.head);
449 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
450 got_object_tree_entry_close(te);
456 struct got_tree_entry *
457 got_alloc_tree_entry_partial(void)
459 struct got_tree_entry *te;
461 te = calloc(1, sizeof(*te));
465 te->id = calloc(1, sizeof(*te->id));
466 if (te->id == NULL) {
473 static const struct got_error *
474 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
477 char *p = buf, *space;
478 const struct got_error *err = NULL;
480 *te = got_alloc_tree_entry_partial();
482 return got_error_from_errno();
484 *elen = strlen(buf) + 1;
485 if (*elen > maxlen) {
488 return got_error(GOT_ERR_BAD_OBJ_DATA);
491 space = strchr(buf, ' ');
493 err = got_error(GOT_ERR_BAD_OBJ_DATA);
499 if (*p < '0' && *p > '7') {
500 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 (*te)->mode |= *p - '0';
508 (*te)->name = strdup(space + 1);
509 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
510 err = got_error(GOT_ERR_BAD_OBJ_DATA);
513 buf += strlen(buf) + 1;
514 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
515 *elen += SHA1_DIGEST_LENGTH;
518 got_object_tree_entry_close(*te);
524 const struct got_error *
525 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
527 const struct got_error *err;
530 *tree = calloc(1, sizeof(**tree));
532 return got_error_from_errno();
534 SIMPLEQ_INIT(&(*tree)->entries.head);
537 struct got_tree_entry *te;
540 err = parse_tree_entry(&te, &elen, buf, remain);
543 (*tree)->entries.nentries++;
544 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
550 got_object_tree_close(*tree);
551 return got_error(GOT_ERR_BAD_OBJ_DATA);
557 const struct got_error *
558 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
560 const struct got_error *err = NULL;
561 static const size_t blocksize = 512;
562 size_t n, total, remain;
568 buf = malloc(blocksize);
570 return got_error_from_errno();
577 newbuf = reallocarray(buf, 1, total + blocksize);
578 if (newbuf == NULL) {
579 err = got_error_from_errno();
585 n = fread(buf + total, 1, remain, f);
588 err = got_ferror(f, GOT_ERR_IO);
606 static const struct got_error *
607 request_commit(struct got_commit_object **commit, struct got_repository *repo,
608 struct got_object *obj, int fd)
610 const struct got_error *err = NULL;
614 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd);
616 err = got_privsep_send_obj_req(&ibuf, fd,obj);
620 err = got_privsep_recv_commit(commit, &ibuf);
626 const struct got_error *
627 got_object_read_commit_privsep(struct got_commit_object **commit,
628 struct got_object *obj, int obj_fd, struct got_repository *repo)
633 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
634 return request_commit(commit, repo, obj, obj_fd);
636 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
637 return got_error_from_errno();
641 return got_error_from_errno();
643 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT);
648 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
650 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
652 return request_commit(commit, repo, obj, obj_fd);
655 static const struct got_error *
656 request_tree(struct got_tree_object **tree, struct got_repository *repo,
657 struct got_object *obj, int fd)
659 const struct got_error *err = NULL;
663 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd);
665 err = got_privsep_send_obj_req(&ibuf, fd,obj);
669 err = got_privsep_recv_tree(tree, &ibuf);
675 const struct got_error *
676 got_object_read_tree_privsep(struct got_tree_object **tree,
677 struct got_object *obj, int obj_fd, struct got_repository *repo)
682 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
683 return request_tree(tree, repo, obj, obj_fd);
685 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
686 return got_error_from_errno();
690 return got_error_from_errno();
692 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE);
697 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
699 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
701 return request_tree(tree, repo, obj, obj_fd);
704 static const struct got_error *
705 request_blob(size_t *size, int outfd, int infd, struct got_repository *repo)
707 const struct got_error *err = NULL;
711 outfd_child = dup(outfd);
712 if (outfd_child == -1)
713 return got_error_from_errno();
716 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd);
718 err = got_privsep_send_blob_req(&ibuf, outfd_child, infd);
722 err = got_privsep_recv_blob(size, &ibuf);
726 if (lseek(outfd, SEEK_SET, 0) == -1)
727 err = got_error_from_errno();
733 const struct got_error *
734 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
735 struct got_repository *repo)
740 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1)
741 return request_blob(size, outfd, infd, repo);
743 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
744 return got_error_from_errno();
748 return got_error_from_errno();
750 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB);
755 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
757 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
759 return request_blob(size, outfd, infd, repo);