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 hassuffix(char *base, char *suf)
76 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
81 static const struct got_error *
82 dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
83 const char *direction)
85 const struct got_error *error = NULL;
92 return got_error_from_errno("pipe");
96 error = got_error_from_errno("fork");
100 } else if (pid == 0) {
105 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
106 if (n < 0 || n >= sizeof(cmd))
108 if (execl(GOT_FETCH_PATH_SSH, GOT_FETCH_PATH_SSH, "--",
109 host, cmd, path, NULL) == -1)
111 abort(); /* not reached */
119 static const struct got_error *
120 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
121 const char *direction)
123 const struct got_error *err = NULL;
124 struct addrinfo hints, *servinfo, *p;
125 char *cmd = NULL, *pkt = NULL;
126 int fd = -1, totlen, r, eaicode;
130 memset(&hints, 0, sizeof hints);
131 hints.ai_family = AF_UNSPEC;
132 hints.ai_socktype = SOCK_STREAM;
133 eaicode = getaddrinfo(host, port, &hints, &servinfo);
136 snprintf(msg, sizeof(msg), "%s: %s", host,
137 gai_strerror(eaicode));
138 return got_error_msg(GOT_ERR_ADDRINFO, msg);
141 for (p = servinfo; p != NULL; p = p->ai_next) {
142 if ((fd = socket(p->ai_family, p->ai_socktype,
143 p->ai_protocol)) == -1)
145 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
147 err = got_error_from_errno("connect");
153 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
154 err = got_error_from_errno("asprintf");
157 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
158 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
159 err = got_error_from_errno("asprintf");
162 r = write(fd, pkt, strlen(pkt) + 1);
164 err = got_error_from_errno("write");
167 if (asprintf(&pkt, "host=%s", host) == -1) {
168 err = got_error_from_errno("asprintf");
171 r = write(fd, pkt, strlen(pkt) + 1);
173 err = got_error_from_errno("write");
187 const struct got_error *
188 got_fetch_connect(int *fetchfd, const char *proto, const char *host,
189 const char *port, const char *server_path)
191 const struct got_error *err = NULL;
195 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
196 err = dial_ssh(fetchfd, host, port, server_path, "upload");
197 else if (strcmp(proto, "git") == 0)
198 err = dial_git(fetchfd, host, port, server_path, "upload");
199 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
200 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
202 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
206 const struct got_error *
207 got_fetch_parse_uri(char **proto, char **host, char **port,
208 char **server_path, char **repo_name, const char *uri)
210 const struct got_error *err = NULL;
214 *proto = *host = *port = *server_path = *repo_name = NULL;
216 p = strstr(uri, "://");
218 return got_error(GOT_ERR_PARSE_URI);
220 *proto = strndup(uri, p - uri);
222 err = got_error_from_errno("strndup");
226 hasport = (strcmp(*proto, "git") == 0 ||
227 strstr(*proto, "http") == *proto);
237 if (p == NULL || strlen(p) == 1) {
238 err = got_error(GOT_ERR_PARSE_URI);
242 q = memchr(s, ':', p - s);
244 *host = strndup(s, q - s);
246 err = got_error_from_errno("strndup");
249 *port = strndup(q + 1, p - (q + 1));
251 err = got_error_from_errno("strndup");
255 *host = strndup(s, p - s);
257 err = got_error_from_errno("strndup");
260 if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
261 err = got_error_from_errno("asprintf");
266 *server_path = strdup(p);
267 if (*server_path == NULL) {
268 err = got_error_from_errno("strdup");
272 p = strrchr(p, '/') + 1;
273 if (!p || strlen(p) == 0) {
274 //werrstr("missing repository in uri");
275 err = got_error(GOT_ERR_PARSE_URI);
279 if (hassuffix(p, ".git"))
281 *repo_name = strndup(p, (p + n) - p);
282 if (*repo_name == NULL) {
283 err = got_error_from_errno("strndup");
302 static const struct got_error *
303 check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
306 uint8_t hexpect[SHA1_DIGEST_LENGTH];
307 uint8_t buf[32 * 1024];
310 if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
311 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
315 while (n < sz - 20) {
317 if (sz - n - 20 < sizeof(buf))
319 r = read(fd, buf, nr);
321 return got_error_from_errno("read");
323 return got_error_msg(GOT_ERR_BAD_PACKFILE,
325 SHA1Update(&ctx, buf, nr);
328 SHA1Final(hcomp, &ctx);
330 r = read(fd, hexpect, sizeof(hexpect));
332 return got_error_from_errno("read");
333 if (r != sizeof(hexpect))
334 return got_error_msg(GOT_ERR_BAD_PACKFILE,
337 if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
338 return got_error_msg(GOT_ERR_BAD_PACKFILE,
339 "packfile checksum mismatch");
344 const struct got_error*
345 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
346 struct got_pathlist_head *symrefs, int fetchfd, struct got_repository *repo,
347 got_fetch_progress_cb progress_cb, void *progress_arg)
349 int imsg_fetchfds[2], imsg_idxfds[2];
350 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
352 int fetchstatus, idxstatus, done = 0;
353 const struct got_error *err;
354 struct imsgbuf fetchibuf, idxibuf;
355 pid_t fetchpid, idxpid;
356 char *tmppackpath = NULL, *tmpidxpath = NULL;
357 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
358 const char *repo_path = got_repo_get_path(repo);
359 struct got_pathlist_head have_refs;
360 struct got_pathlist_entry *pe;
361 off_t packfile_size = 0;
365 for (i = 0; i < nitems(tmpfds); i++)
368 TAILQ_INIT(&have_refs);
370 if (asprintf(&path, "%s/%s/fetching.pack",
371 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
372 err = got_error_from_errno("asprintf");
375 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
379 npackfd = dup(packfd);
381 err = got_error_from_errno("dup");
384 if (asprintf(&path, "%s/%s/fetching.idx",
385 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
386 err = got_error_from_errno("asprintf");
389 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
395 err = got_error_from_errno("dup");
399 for (i = 0; i < nitems(tmpfds); i++) {
400 tmpfds[i] = got_opentempfd();
401 if (tmpfds[i] == -1) {
402 err = got_error_from_errno("got_opentempfd");
407 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
408 err = got_error_from_errno("socketpair");
413 if (fetchpid == -1) {
414 err = got_error_from_errno("fork");
416 } else if (fetchpid == 0){
417 got_privsep_exec_child(imsg_fetchfds,
418 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
421 if (close(imsg_fetchfds[1]) != 0) {
422 err = got_error_from_errno("close");
425 imsg_init(&fetchibuf, imsg_fetchfds[0]);
426 nfetchfd = dup(fetchfd);
427 if (nfetchfd == -1) {
428 err = got_error_from_errno("dup");
431 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs);
435 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
438 npackfd = dup(packfd);
440 err = got_error_from_errno("dup");
446 struct got_object_id *id = NULL;
447 char *refname = NULL;
448 char *server_progress = NULL;
449 off_t packfile_size_cur;
451 err = got_privsep_recv_fetch_progress(&done,
452 &id, &refname, symrefs, &server_progress,
453 &packfile_size_cur, &fetchibuf);
458 else if (refname && id) {
459 err = got_pathlist_append(refs, refname, id);
462 } else if (server_progress) {
463 char *s, *s0 = server_progress;
464 while ((s = strsep(&s0, "\r")) != NULL) {
467 err = progress_cb(progress_arg, s,
468 packfile_size_cur, 0, 0, 0, 0);
472 free(server_progress);
475 } else if (packfile_size_cur != packfile_size) {
476 err = progress_cb(progress_arg, NULL,
477 packfile_size_cur, 0, 0, 0, 0);
480 packfile_size = packfile_size_cur;
483 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
484 err = got_error_from_errno("waitpid");
488 if (lseek(packfd, 0, SEEK_SET) == -1) {
489 err = got_error_from_errno("lseek");
492 err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
496 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
497 err = got_error_from_errno("socketpair");
502 err= got_error_from_errno("fork");
504 } else if (idxpid == 0)
505 got_privsep_exec_child(imsg_idxfds,
506 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
507 if (close(imsg_idxfds[1]) != 0) {
508 err = got_error_from_errno("close");
511 imsg_init(&idxibuf, imsg_idxfds[0]);
513 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
518 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
522 for (i = 0; i < nitems(tmpfds); i++) {
523 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
530 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
532 err = got_privsep_recv_index_progress(&done, &nobj_total,
533 &nobj_indexed, &nobj_loose, &nobj_resolved,
537 if (nobj_indexed != 0) {
538 err = progress_cb(progress_arg, NULL,
539 packfile_size, nobj_total,
540 nobj_indexed, nobj_loose, nobj_resolved);
544 imsg_clear(&idxibuf);
546 if (close(imsg_idxfds[0]) == -1) {
547 err = got_error_from_errno("close");
550 if (waitpid(idxpid, &idxstatus, 0) == -1) {
551 err = got_error_from_errno("waitpid");
555 err = got_object_id_str(&id_str, *pack_hash);
558 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
559 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
560 err = got_error_from_errno("asprintf");
564 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
565 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
566 err = got_error_from_errno("asprintf");
570 if (rename(tmppackpath, packpath) == -1) {
571 err = got_error_from_errno3("rename", tmppackpath, packpath);
574 if (rename(tmpidxpath, idxpath) == -1) {
575 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
580 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
581 err = got_error_from_errno("close");
582 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
583 err = got_error_from_errno("close");
584 if (packfd != -1 && close(packfd) == -1 && err == NULL)
585 err = got_error_from_errno("close");
586 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
587 err = got_error_from_errno("close");
588 for (i = 0; i < nitems(tmpfds); i++) {
589 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
590 err = got_error_from_errno("close");
600 TAILQ_FOREACH(pe, refs, entry) {
601 free((void *)pe->path);
604 got_pathlist_free(refs);
605 TAILQ_FOREACH(pe, symrefs, entry) {
606 free((void *)pe->path);
609 got_pathlist_free(symrefs);