Blob


1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/syslimits.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
27 #include <errno.h>
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <sha1.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <limits.h>
38 #include <imsg.h>
39 #include <time.h>
40 #include <uuid.h>
41 #include <netdb.h>
42 #include <netinet/in.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.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"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 #ifndef MIN
70 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
71 #endif
73 static int
74 hassuffix(char *base, char *suf)
75 {
76 int nb, ns;
78 nb = strlen(base);
79 ns = strlen(suf);
80 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
81 return 1;
82 return 0;
83 }
85 static const struct got_error *
86 dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
87 const char *direction, int verbosity)
88 {
89 const struct got_error *error = NULL;
90 int pid, pfd[2];
91 char cmd[64];
92 char *argv[9];
93 int i = 0;
95 *fetchfd = -1;
97 argv[0] = GOT_FETCH_PATH_SSH;
98 if (verbosity == -1) {
99 argv[1 + i++] = "-q";
100 } else {
101 /* ssh(1) allows up to 3 "-v" options. */
102 for (i = 0; i < MIN(3, verbosity); i++)
103 argv[1 + i] = "-v";
105 argv[1 + i] = "--";
106 argv[2 + i] = (char *)host;
107 argv[3 + i] = (char *)cmd;
108 argv[4 + i] = (char *)path;
109 argv[5 + i] = NULL;
111 if (pipe(pfd) == -1)
112 return got_error_from_errno("pipe");
114 pid = fork();
115 if (pid == -1) {
116 error = got_error_from_errno("fork");
117 close(pfd[0]);
118 close(pfd[1]);
119 return error;
120 } else if (pid == 0) {
121 int n;
122 close(pfd[1]);
123 dup2(pfd[0], 0);
124 dup2(pfd[0], 1);
125 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
126 if (n < 0 || n >= sizeof(cmd))
127 err(1, "snprintf");
128 if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
129 err(1, "execl");
130 abort(); /* not reached */
131 } else {
132 close(pfd[0]);
133 *fetchfd = pfd[1];
134 return NULL;
138 static const struct got_error *
139 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
140 const char *direction)
142 const struct got_error *err = NULL;
143 struct addrinfo hints, *servinfo, *p;
144 char *cmd = NULL, *pkt = NULL;
145 int fd = -1, totlen, r, eaicode;
147 *fetchfd = -1;
149 memset(&hints, 0, sizeof hints);
150 hints.ai_family = AF_UNSPEC;
151 hints.ai_socktype = SOCK_STREAM;
152 eaicode = getaddrinfo(host, port, &hints, &servinfo);
153 if (eaicode) {
154 char msg[512];
155 snprintf(msg, sizeof(msg), "%s: %s", host,
156 gai_strerror(eaicode));
157 return got_error_msg(GOT_ERR_ADDRINFO, msg);
160 for (p = servinfo; p != NULL; p = p->ai_next) {
161 if ((fd = socket(p->ai_family, p->ai_socktype,
162 p->ai_protocol)) == -1)
163 continue;
164 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
165 break;
166 err = got_error_from_errno("connect");
167 close(fd);
169 if (p == NULL)
170 goto done;
172 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
173 err = got_error_from_errno("asprintf");
174 goto done;
176 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
177 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
178 err = got_error_from_errno("asprintf");
179 goto done;
181 r = write(fd, pkt, strlen(pkt) + 1);
182 if (r == -1) {
183 err = got_error_from_errno("write");
184 goto done;
186 if (asprintf(&pkt, "host=%s", host) == -1) {
187 err = got_error_from_errno("asprintf");
188 goto done;
190 r = write(fd, pkt, strlen(pkt) + 1);
191 if (r == -1) {
192 err = got_error_from_errno("write");
193 goto done;
195 done:
196 free(cmd);
197 free(pkt);
198 if (err) {
199 if (fd != -1)
200 close(fd);
201 } else
202 *fetchfd = fd;
203 return err;
206 const struct got_error *
207 got_fetch_connect(int *fetchfd, const char *proto, const char *host,
208 const char *port, const char *server_path, int verbosity)
210 const struct got_error *err = NULL;
212 *fetchfd = -1;
214 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
215 err = dial_ssh(fetchfd, host, port, server_path, "upload",
216 verbosity);
217 else if (strcmp(proto, "git") == 0)
218 err = dial_git(fetchfd, host, port, server_path, "upload");
219 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
220 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
221 else
222 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
223 return err;
226 const struct got_error *
227 got_fetch_parse_uri(char **proto, char **host, char **port,
228 char **server_path, char **repo_name, const char *uri)
230 const struct got_error *err = NULL;
231 char *s, *p, *q;
232 int n, hasport;
234 *proto = *host = *port = *server_path = *repo_name = NULL;
236 p = strstr(uri, "://");
237 if (!p) {
238 return got_error(GOT_ERR_PARSE_URI);
240 *proto = strndup(uri, p - uri);
241 if (proto == NULL) {
242 err = got_error_from_errno("strndup");
243 goto done;
246 hasport = (strcmp(*proto, "git") == 0 ||
247 strstr(*proto, "http") == *proto);
248 s = p + 3;
249 p = NULL;
250 if (!hasport) {
251 p = strstr(s, ":");
252 if (p != NULL)
253 p++;
255 if (p == NULL)
256 p = strstr(s, "/");
257 if (p == NULL || strlen(p) == 1) {
258 err = got_error(GOT_ERR_PARSE_URI);
259 goto done;
262 q = memchr(s, ':', p - s);
263 if (q) {
264 *host = strndup(s, q - s);
265 if (*host == NULL) {
266 err = got_error_from_errno("strndup");
267 goto done;
269 *port = strndup(q + 1, p - (q + 1));
270 if (*port == NULL) {
271 err = got_error_from_errno("strndup");
272 goto done;
274 } else {
275 *host = strndup(s, p - s);
276 if (*host == NULL) {
277 err = got_error_from_errno("strndup");
278 goto done;
280 if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
281 err = got_error_from_errno("asprintf");
282 goto done;
286 *server_path = strdup(p);
287 if (*server_path == NULL) {
288 err = got_error_from_errno("strdup");
289 goto done;
292 p = strrchr(p, '/') + 1;
293 if (!p || strlen(p) == 0) {
294 //werrstr("missing repository in uri");
295 err = got_error(GOT_ERR_PARSE_URI);
296 goto done;
298 n = strlen(p);
299 if (hassuffix(p, ".git"))
300 n -= 4;
301 *repo_name = strndup(p, (p + n) - p);
302 if (*repo_name == NULL) {
303 err = got_error_from_errno("strndup");
304 goto done;
306 done:
307 if (err) {
308 free(*proto);
309 *proto = NULL;
310 free(*host);
311 *host = NULL;
312 free(*port);
313 *port = NULL;
314 free(*server_path);
315 *server_path = NULL;
316 free(*repo_name);
317 *repo_name = NULL;
319 return err;
322 static const struct got_error *
323 check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
325 SHA1_CTX ctx;
326 uint8_t hexpect[SHA1_DIGEST_LENGTH];
327 uint8_t buf[32 * 1024];
328 ssize_t n, r, nr;
330 if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
331 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
333 n = 0;
334 SHA1Init(&ctx);
335 while (n < sz - 20) {
336 nr = sizeof(buf);
337 if (sz - n - 20 < sizeof(buf))
338 nr = sz - n - 20;
339 r = read(fd, buf, nr);
340 if (r == -1)
341 return got_error_from_errno("read");
342 if (r != nr)
343 return got_error_msg(GOT_ERR_BAD_PACKFILE,
344 "short pack file");
345 SHA1Update(&ctx, buf, nr);
346 n += r;
348 SHA1Final(hcomp, &ctx);
350 r = read(fd, hexpect, sizeof(hexpect));
351 if (r == -1)
352 return got_error_from_errno("read");
353 if (r != sizeof(hexpect))
354 return got_error_msg(GOT_ERR_BAD_PACKFILE,
355 "short pack file");
357 if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
358 return got_error_msg(GOT_ERR_BAD_PACKFILE,
359 "packfile checksum mismatch");
361 return NULL;
364 const struct got_error*
365 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
366 struct got_pathlist_head *symrefs, int fetchfd, struct got_repository *repo,
367 got_fetch_progress_cb progress_cb, void *progress_arg)
369 int imsg_fetchfds[2], imsg_idxfds[2];
370 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
371 int tmpfds[3], i;
372 int fetchstatus, idxstatus, done = 0;
373 const struct got_error *err;
374 struct imsgbuf fetchibuf, idxibuf;
375 pid_t fetchpid, idxpid;
376 char *tmppackpath = NULL, *tmpidxpath = NULL;
377 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
378 const char *repo_path = got_repo_get_path(repo);
379 struct got_pathlist_head have_refs;
380 struct got_pathlist_entry *pe;
381 off_t packfile_size = 0;
382 char *path;
384 *pack_hash = NULL;
385 for (i = 0; i < nitems(tmpfds); i++)
386 tmpfds[i] = -1;
388 TAILQ_INIT(&have_refs);
390 if (asprintf(&path, "%s/%s/fetching.pack",
391 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
392 err = got_error_from_errno("asprintf");
393 goto done;
395 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
396 free(path);
397 if (err)
398 goto done;
399 npackfd = dup(packfd);
400 if (npackfd == -1) {
401 err = got_error_from_errno("dup");
402 goto done;
404 if (asprintf(&path, "%s/%s/fetching.idx",
405 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
406 err = got_error_from_errno("asprintf");
407 goto done;
409 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
410 free(path);
411 if (err)
412 goto done;
413 nidxfd = dup(idxfd);
414 if (nidxfd == -1) {
415 err = got_error_from_errno("dup");
416 goto done;
419 for (i = 0; i < nitems(tmpfds); i++) {
420 tmpfds[i] = got_opentempfd();
421 if (tmpfds[i] == -1) {
422 err = got_error_from_errno("got_opentempfd");
423 goto done;
427 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
428 err = got_error_from_errno("socketpair");
429 goto done;
432 fetchpid = fork();
433 if (fetchpid == -1) {
434 err = got_error_from_errno("fork");
435 goto done;
436 } else if (fetchpid == 0){
437 got_privsep_exec_child(imsg_fetchfds,
438 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
441 if (close(imsg_fetchfds[1]) != 0) {
442 err = got_error_from_errno("close");
443 goto done;
445 imsg_init(&fetchibuf, imsg_fetchfds[0]);
446 nfetchfd = dup(fetchfd);
447 if (nfetchfd == -1) {
448 err = got_error_from_errno("dup");
449 goto done;
451 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs);
452 if (err != NULL)
453 goto done;
454 nfetchfd = -1;
455 err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
456 if (err != NULL)
457 goto done;
458 npackfd = dup(packfd);
459 if (npackfd == -1) {
460 err = got_error_from_errno("dup");
461 goto done;
464 packfile_size = 0;
465 while (!done) {
466 struct got_object_id *id = NULL;
467 char *refname = NULL;
468 char *server_progress = NULL;
469 off_t packfile_size_cur;
471 err = got_privsep_recv_fetch_progress(&done,
472 &id, &refname, symrefs, &server_progress,
473 &packfile_size_cur, &fetchibuf);
474 if (err != NULL)
475 goto done;
476 if (done)
477 *pack_hash = id;
478 else if (refname && id) {
479 err = got_pathlist_append(refs, refname, id);
480 if (err)
481 goto done;
482 } else if (server_progress) {
483 char *s, *s0 = server_progress;
484 while ((s = strsep(&s0, "\r")) != NULL) {
485 if (*s == '\0')
486 continue;
487 err = progress_cb(progress_arg, s,
488 packfile_size_cur, 0, 0, 0, 0);
489 if (err)
490 break;
492 free(server_progress);
493 if (err)
494 goto done;
495 } else if (packfile_size_cur != packfile_size) {
496 err = progress_cb(progress_arg, NULL,
497 packfile_size_cur, 0, 0, 0, 0);
498 if (err)
499 break;
500 packfile_size = packfile_size_cur;
503 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
504 err = got_error_from_errno("waitpid");
505 goto done;
508 if (lseek(packfd, 0, SEEK_SET) == -1) {
509 err = got_error_from_errno("lseek");
510 goto done;
512 err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
513 if (err)
514 goto done;
516 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
517 err = got_error_from_errno("socketpair");
518 goto done;
520 idxpid = fork();
521 if (idxpid == -1) {
522 err= got_error_from_errno("fork");
523 goto done;
524 } else if (idxpid == 0)
525 got_privsep_exec_child(imsg_idxfds,
526 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
527 if (close(imsg_idxfds[1]) != 0) {
528 err = got_error_from_errno("close");
529 goto done;
531 imsg_init(&idxibuf, imsg_idxfds[0]);
533 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
534 npackfd);
535 if (err != NULL)
536 goto done;
537 npackfd = -1;
538 err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
539 if (err != NULL)
540 goto done;
541 nidxfd = -1;
542 for (i = 0; i < nitems(tmpfds); i++) {
543 err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
544 if (err != NULL)
545 goto done;
546 tmpfds[i] = -1;
548 done = 0;
549 while (!done) {
550 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
552 err = got_privsep_recv_index_progress(&done, &nobj_total,
553 &nobj_indexed, &nobj_loose, &nobj_resolved,
554 &idxibuf);
555 if (err != NULL)
556 goto done;
557 if (nobj_indexed != 0) {
558 err = progress_cb(progress_arg, NULL,
559 packfile_size, nobj_total,
560 nobj_indexed, nobj_loose, nobj_resolved);
561 if (err)
562 break;
564 imsg_clear(&idxibuf);
566 if (close(imsg_idxfds[0]) == -1) {
567 err = got_error_from_errno("close");
568 goto done;
570 if (waitpid(idxpid, &idxstatus, 0) == -1) {
571 err = got_error_from_errno("waitpid");
572 goto done;
575 err = got_object_id_str(&id_str, *pack_hash);
576 if (err)
577 goto done;
578 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
579 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
580 err = got_error_from_errno("asprintf");
581 goto done;
584 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
585 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
586 err = got_error_from_errno("asprintf");
587 goto done;
590 if (rename(tmppackpath, packpath) == -1) {
591 err = got_error_from_errno3("rename", tmppackpath, packpath);
592 goto done;
594 if (rename(tmpidxpath, idxpath) == -1) {
595 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
596 goto done;
599 done:
600 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
601 err = got_error_from_errno("close");
602 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
603 err = got_error_from_errno("close");
604 if (packfd != -1 && close(packfd) == -1 && err == NULL)
605 err = got_error_from_errno("close");
606 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
607 err = got_error_from_errno("close");
608 for (i = 0; i < nitems(tmpfds); i++) {
609 if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
610 err = got_error_from_errno("close");
612 free(tmppackpath);
613 free(tmpidxpath);
614 free(idxpath);
615 free(packpath);
617 if (err) {
618 free(*pack_hash);
619 *pack_hash = NULL;
620 TAILQ_FOREACH(pe, refs, entry) {
621 free((void *)pe->path);
622 free(pe->data);
624 got_pathlist_free(refs);
625 TAILQ_FOREACH(pe, symrefs, entry) {
626 free((void *)pe->path);
627 free(pe->data);
629 got_pathlist_free(symrefs);
631 return err;