2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@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>
23 #include <sys/syslimits.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
42 #include <netinet/in.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
51 #include "got_opentemp.h"
52 #include "got_fetch.h"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
74 hassuffix(char *base, char *suf)
80 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
85 static const struct got_error *
86 dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
87 const char *path, const char *direction, int verbosity)
89 const struct got_error *error = NULL;
101 argv[0] = GOT_FETCH_PATH_SSH;
103 argv[2] = (char *)port;
104 if (verbosity == -1) {
105 argv[3 + i++] = "-q";
107 /* ssh(1) allows up to 3 "-v" options. */
108 for (i = 0; i < MIN(3, verbosity); i++)
112 argv[4 + i] = (char *)host;
113 argv[5 + i] = (char *)cmd;
114 argv[6 + i] = (char *)path;
118 return got_error_from_errno("pipe");
122 error = got_error_from_errno("fork");
126 } else if (pid == 0) {
131 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
132 if (n < 0 || n >= sizeof(cmd))
134 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
136 abort(); /* not reached */
145 static const struct got_error *
146 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
147 const char *direction)
149 const struct got_error *err = NULL;
150 struct addrinfo hints, *servinfo, *p;
151 char *cmd = NULL, *pkt = NULL;
152 int fd = -1, totlen, r, eaicode;
157 port = GOT_DEFAULT_GIT_PORT_STR;
159 memset(&hints, 0, sizeof hints);
160 hints.ai_family = AF_UNSPEC;
161 hints.ai_socktype = SOCK_STREAM;
162 eaicode = getaddrinfo(host, port, &hints, &servinfo);
165 snprintf(msg, sizeof(msg), "%s: %s", host,
166 gai_strerror(eaicode));
167 return got_error_msg(GOT_ERR_ADDRINFO, msg);
170 for (p = servinfo; p != NULL; p = p->ai_next) {
171 if ((fd = socket(p->ai_family, p->ai_socktype,
172 p->ai_protocol)) == -1)
174 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
176 err = got_error_from_errno("connect");
182 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
183 err = got_error_from_errno("asprintf");
186 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
187 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
188 err = got_error_from_errno("asprintf");
191 r = write(fd, pkt, strlen(pkt) + 1);
193 err = got_error_from_errno("write");
196 if (asprintf(&pkt, "host=%s", host) == -1) {
197 err = got_error_from_errno("asprintf");
200 r = write(fd, pkt, strlen(pkt) + 1);
202 err = got_error_from_errno("write");
216 const struct got_error *
217 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
218 const char *host, const char *port, const char *server_path, int verbosity)
220 const struct got_error *err = NULL;
225 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
226 err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
227 "upload", verbosity);
228 else if (strcmp(proto, "git") == 0)
229 err = dial_git(fetchfd, host, port, server_path, "upload");
230 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
231 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
233 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
237 const struct got_error *
238 got_fetch_parse_uri(char **proto, char **host, char **port,
239 char **server_path, char **repo_name, const char *uri)
241 const struct got_error *err = NULL;
245 *proto = *host = *port = *server_path = *repo_name = NULL;
247 p = strstr(uri, "://");
249 /* Try parsing Git's "scp" style URL syntax. */
250 *proto = strdup("ssh");
252 err = got_error_from_errno("strdup");
258 err = got_error(GOT_ERR_PARSE_URI);
261 /* No slashes allowed before first colon. */
264 err = got_error(GOT_ERR_PARSE_URI);
267 *host = strndup(s, q - s);
269 err = got_error_from_errno("strndup");
274 *proto = strndup(uri, p - uri);
276 err = got_error_from_errno("strndup");
282 if (p == NULL || strlen(p) == 1) {
283 err = got_error(GOT_ERR_PARSE_URI);
287 q = memchr(s, ':', p - s);
289 *host = strndup(s, q - s);
291 err = got_error_from_errno("strndup");
294 *port = strndup(q + 1, p - (q + 1));
296 err = got_error_from_errno("strndup");
300 *host = strndup(s, p - s);
302 err = got_error_from_errno("strndup");
308 *server_path = strdup(p);
309 if (*server_path == NULL) {
310 err = got_error_from_errno("strdup");
315 if (!p || strlen(p) <= 1) {
316 err = got_error(GOT_ERR_PARSE_URI);
322 err = got_error(GOT_ERR_PARSE_URI);
325 if (hassuffix(p, ".git"))
327 *repo_name = strndup(p, (p + n) - p);
328 if (*repo_name == NULL) {
329 err = got_error_from_errno("strndup");
348 static const struct got_error *
349 check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
352 uint8_t hexpect[SHA1_DIGEST_LENGTH];
353 uint8_t buf[32 * 1024];
356 if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
357 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
361 while (n < sz - 20) {
363 if (sz - n - 20 < sizeof(buf))
365 r = read(fd, buf, nr);
367 return got_error_from_errno("read");
369 return got_error_msg(GOT_ERR_BAD_PACKFILE,
371 SHA1Update(&ctx, buf, nr);
374 SHA1Final(hcomp, &ctx);
376 r = read(fd, hexpect, sizeof(hexpect));
378 return got_error_from_errno("read");
379 if (r != sizeof(hexpect))
380 return got_error_msg(GOT_ERR_BAD_PACKFILE,
383 if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
384 return got_error_msg(GOT_ERR_BAD_PACKFILE,
385 "packfile checksum mismatch");
390 const struct got_error*
391 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
392 struct got_pathlist_head *symrefs, const char *remote_name,
393 int mirror_references, int fetch_all_branches,
394 struct got_pathlist_head *wanted_branches,
395 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
396 int fetchfd, struct got_repository *repo,
397 got_fetch_progress_cb progress_cb, void *progress_arg)
399 int imsg_fetchfds[2], imsg_idxfds[2];
400 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
402 int fetchstatus, idxstatus, done = 0;
403 const struct got_error *err;
404 struct imsgbuf fetchibuf, idxibuf;
405 pid_t fetchpid, idxpid;
406 char *tmppackpath = NULL, *tmpidxpath = NULL;
407 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
408 const char *repo_path = NULL;
409 struct got_pathlist_head have_refs;
410 struct got_pathlist_entry *pe;
411 struct got_reflist_head my_refs;
412 struct got_reflist_entry *re;
413 off_t packfile_size = 0;
414 struct got_packfile_hdr pack_hdr;
416 char *ref_prefix = NULL;
417 size_t ref_prefixlen = 0;
419 char *progress = NULL;
422 * Prevent fetching of references that won't make any
423 * sense outside of the remote repository's context.
425 TAILQ_FOREACH(pe, wanted_refs, entry) {
426 const char *refname = pe->path;
427 if (strncmp(refname, "refs/got/", 9) == 0 ||
428 strncmp(refname, "got/", 4) == 0 ||
429 strncmp(refname, "refs/remotes/", 13) == 0 ||
430 strncmp(refname, "remotes/", 8) == 0)
431 return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
435 repo_path = got_repo_get_path_git_dir(repo);
438 for (i = 0; i < nitems(tmpfds); i++)
441 TAILQ_INIT(&have_refs);
442 SIMPLEQ_INIT(&my_refs);
444 if (!mirror_references) {
445 if (asprintf(&ref_prefix, "refs/remotes/%s/",
447 return got_error_from_errno("asprintf");
448 ref_prefixlen = strlen(ref_prefix);
451 if (!list_refs_only) {
452 err = got_ref_list(&my_refs, repo, NULL,
453 got_ref_cmp_by_name, NULL);
458 SIMPLEQ_FOREACH(re, &my_refs, entry) {
459 struct got_object_id *id;
462 if (got_ref_is_symbolic(re->ref))
465 refname = got_ref_get_name(re->ref);
467 if (mirror_references) {
469 err = got_ref_resolve(&id, repo, re->ref);
472 name = strdup(refname);
474 err = got_error_from_errno("strdup");
477 err = got_pathlist_append(&have_refs, name, id);
483 if (strncmp("refs/tags/", refname, 10) == 0) {
486 err = got_ref_resolve(&id, repo, re->ref);
489 tagname = strdup(refname);
490 if (tagname == NULL) {
491 err = got_error_from_errno("strdup");
494 err = got_pathlist_append(&have_refs, tagname, id);
501 if (strncmp(ref_prefix, refname, ref_prefixlen) == 0) {
504 err = got_ref_resolve(&id, repo, re->ref);
508 if (asprintf(&branchname, "refs/heads/%s",
509 refname + ref_prefixlen) == -1) {
510 err = got_error_from_errno("asprintf");
513 err = got_pathlist_append(&have_refs, branchname, id);
521 if (list_refs_only) {
522 packfd = got_opentempfd();
524 err = got_error_from_errno("got_opentempfd");
528 if (asprintf(&path, "%s/%s/fetching.pack",
529 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
530 err = got_error_from_errno("asprintf");
533 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
538 if (list_refs_only) {
539 idxfd = got_opentempfd();
541 err = got_error_from_errno("got_opentempfd");
545 if (asprintf(&path, "%s/%s/fetching.idx",
546 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
547 err = got_error_from_errno("asprintf");
550 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
557 err = got_error_from_errno("dup");
561 for (i = 0; i < nitems(tmpfds); i++) {
562 tmpfds[i] = got_opentempfd();
563 if (tmpfds[i] == -1) {
564 err = got_error_from_errno("got_opentempfd");
569 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
570 err = got_error_from_errno("socketpair");
575 if (fetchpid == -1) {
576 err = got_error_from_errno("fork");
578 } else if (fetchpid == 0){
579 got_privsep_exec_child(imsg_fetchfds,
580 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
583 if (close(imsg_fetchfds[1]) != 0) {
584 err = got_error_from_errno("close");
587 imsg_init(&fetchibuf, imsg_fetchfds[0]);
588 nfetchfd = dup(fetchfd);
589 if (nfetchfd == -1) {
590 err = got_error_from_errno("dup");
593 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
594 fetch_all_branches, wanted_branches, wanted_refs,
595 list_refs_only, verbosity);
599 npackfd = dup(packfd);
601 err = got_error_from_errno("dup");
604 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
610 progress = calloc(GOT_FETCH_PKTMAX, 1);
611 if (progress == NULL) {
612 err = got_error_from_errno("calloc");
616 struct got_object_id *id = NULL;
617 char *refname = NULL;
618 char *server_progress = NULL;
619 off_t packfile_size_cur = 0;
621 err = got_privsep_recv_fetch_progress(&done,
622 &id, &refname, symrefs, &server_progress,
623 &packfile_size_cur, &fetchibuf);
627 if (packfile_size > 0)
631 } else if (refname && id) {
632 err = got_pathlist_insert(NULL, refs, refname, id);
635 } else if (server_progress) {
638 * XXX git-daemon tends to send batched output with
639 * lines spanning separate packets. Buffer progress
640 * output until we see a CR or LF to avoid giving
641 * partial lines of progress output to the callback.
643 if (strlcat(progress, server_progress,
644 GOT_FETCH_PKTMAX) >= GOT_FETCH_PKTMAX) {
645 progress[0] = '\0'; /* discard */
648 while ((p = strchr(progress, '\r')) != NULL ||
649 (p = strchr(progress, '\n')) != NULL) {
654 if (asprintf(&s, "%s%s", progress,
655 c == '\n' ? "\n" : "") == -1) {
656 err = got_error_from_errno("asprintf");
659 err = progress_cb(progress_arg, s,
660 packfile_size_cur, 0, 0, 0, 0);
664 n = strlen(progress);
665 if (n < GOT_FETCH_PKTMAX - 1) {
666 memmove(progress, &progress[n + 1],
667 GOT_FETCH_PKTMAX - n - 1);
671 free(server_progress);
674 } else if (packfile_size_cur != packfile_size) {
675 err = progress_cb(progress_arg, NULL,
676 packfile_size_cur, 0, 0, 0, 0);
679 packfile_size = packfile_size_cur;
682 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
683 err = got_error_from_errno("waitpid");
687 if (lseek(packfd, 0, SEEK_SET) == -1) {
688 err = got_error_from_errno("lseek");
692 /* If zero data was fetched without error we are already up-to-date. */
693 if (packfile_size == 0)
695 else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
696 err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
701 n = read(packfd, &pack_hdr, sizeof(pack_hdr));
703 err = got_error_from_errno("read");
706 if (n != sizeof(pack_hdr)) {
707 err = got_error(GOT_ERR_IO);
710 if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
711 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
712 "bad pack file signature");
715 if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
716 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
717 "bad pack file version");
720 nobj = betoh32(pack_hdr.nobjects);
722 packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
723 return got_error_msg(GOT_ERR_BAD_PACKFILE,
724 "bad pack file with zero objects");
726 packfile_size <= sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
727 return got_error_msg(GOT_ERR_BAD_PACKFILE,
728 "empty pack file with non-zero object count");
732 * If the pack file contains no objects, we may only need to update
733 * references in our repository. The caller will take care of that.
738 if (lseek(packfd, 0, SEEK_SET) == -1) {
739 err = got_error_from_errno("lseek");
743 err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
747 if (lseek(packfd, 0, SEEK_SET) == -1) {
748 err = got_error_from_errno("lseek");
752 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
753 err = got_error_from_errno("socketpair");
758 err= got_error_from_errno("fork");
760 } else if (idxpid == 0)
761 got_privsep_exec_child(imsg_idxfds,
762 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
763 if (close(imsg_idxfds[1]) != 0) {
764 err = got_error_from_errno("close");
767 imsg_init(&idxibuf, imsg_idxfds[0]);
769 npackfd = dup(packfd);
771 err = got_error_from_errno("dup");
774 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
779 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
783 for (i = 0; i < nitems(tmpfds); i++) {
784 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
791 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
793 err = got_privsep_recv_index_progress(&done, &nobj_total,
794 &nobj_indexed, &nobj_loose, &nobj_resolved,
798 if (nobj_indexed != 0) {
799 err = progress_cb(progress_arg, NULL,
800 packfile_size, nobj_total,
801 nobj_indexed, nobj_loose, nobj_resolved);
805 imsg_clear(&idxibuf);
807 if (close(imsg_idxfds[0]) == -1) {
808 err = got_error_from_errno("close");
811 if (waitpid(idxpid, &idxstatus, 0) == -1) {
812 err = got_error_from_errno("waitpid");
816 err = got_object_id_str(&id_str, *pack_hash);
819 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
820 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
821 err = got_error_from_errno("asprintf");
825 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
826 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
827 err = got_error_from_errno("asprintf");
831 if (rename(tmppackpath, packpath) == -1) {
832 err = got_error_from_errno3("rename", tmppackpath, packpath);
837 if (rename(tmpidxpath, idxpath) == -1) {
838 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
845 if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
846 err = got_error_from_errno2("unlink", tmppackpath);
847 if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
848 err = got_error_from_errno2("unlink", tmpidxpath);
849 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
850 err = got_error_from_errno("close");
851 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
852 err = got_error_from_errno("close");
853 if (packfd != -1 && close(packfd) == -1 && err == NULL)
854 err = got_error_from_errno("close");
855 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
856 err = got_error_from_errno("close");
857 for (i = 0; i < nitems(tmpfds); i++) {
858 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
859 err = got_error_from_errno("close");
868 TAILQ_FOREACH(pe, &have_refs, entry) {
869 free((char *)pe->path);
872 got_pathlist_free(&have_refs);
873 got_ref_list_free(&my_refs);
877 TAILQ_FOREACH(pe, refs, entry) {
878 free((void *)pe->path);
881 got_pathlist_free(refs);
882 TAILQ_FOREACH(pe, symrefs, entry) {
883 free((void *)pe->path);
886 got_pathlist_free(symrefs);