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 #define GOT_PROTOMAX 64
66 #define GOT_HOSTMAX 256
67 #define GOT_PATHMAX 512
68 #define GOT_REPOMAX 256
69 #define GOT_PORTMAX 16
70 #define GOT_URIMAX 1024
72 static int
73 hassuffix(char *base, char *suf)
74 {
75 int nb, ns;
77 nb = strlen(base);
78 ns = strlen(suf);
79 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
80 return 1;
81 return 0;
82 }
84 static const struct got_error *
85 dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
86 const char *direction)
87 {
88 const struct got_error *error = NULL;
89 int pid, pfd[2];
90 char cmd[64];
92 *fetchfd = -1;
94 if (pipe(pfd) == -1)
95 return got_error_from_errno("pipe");
97 pid = fork();
98 if (pid == -1) {
99 error = got_error_from_errno("fork");
100 close(pfd[0]);
101 close(pfd[1]);
102 return error;
103 } else if (pid == 0) {
104 int n;
105 close(pfd[1]);
106 dup2(pfd[0], 0);
107 dup2(pfd[0], 1);
108 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
109 if (n < 0 || n >= sizeof(cmd))
110 err(1, "snprintf");
111 if (execlp("ssh", "ssh", host, cmd, path, NULL) == -1)
112 err(1, "execlp");
113 abort(); /* not reached */
114 } else {
115 close(pfd[0]);
116 *fetchfd = pfd[1];
117 return NULL;
121 static const struct got_error *
122 dial_git(int *fetchfd, const char *host, const char *port, const char *path,
123 const char *direction)
125 const struct got_error *err = NULL;
126 struct addrinfo hints, *servinfo, *p;
127 char *cmd = NULL, *pkt = NULL;
128 int fd = -1, totlen, r, eaicode;
130 *fetchfd = -1;
132 memset(&hints, 0, sizeof hints);
133 hints.ai_family = AF_UNSPEC;
134 hints.ai_socktype = SOCK_STREAM;
135 eaicode = getaddrinfo(host, port, &hints, &servinfo);
136 if (eaicode)
137 return got_error_msg(GOT_ERR_ADDRINFO, gai_strerror(eaicode));
139 for (p = servinfo; p != NULL; p = p->ai_next) {
140 if ((fd = socket(p->ai_family, p->ai_socktype,
141 p->ai_protocol)) == -1)
142 continue;
143 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
144 break;
145 err = got_error_from_errno("connect");
146 close(fd);
148 if (p == NULL)
149 goto done;
151 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
152 err = got_error_from_errno("asprintf");
153 goto done;
155 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
156 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
157 err = got_error_from_errno("asprintf");
158 goto done;
160 r = write(fd, pkt, strlen(pkt) + 1);
161 if (r == -1) {
162 err = got_error_from_errno("write");
163 goto done;
165 if (asprintf(&pkt, "host=%s", host) == -1) {
166 err = got_error_from_errno("asprintf");
167 goto done;
169 r = write(fd, pkt, strlen(pkt) + 1);
170 if (r == -1) {
171 err = got_error_from_errno("write");
172 goto done;
174 done:
175 free(cmd);
176 free(pkt);
177 if (err) {
178 if (fd != -1)
179 close(fd);
180 } else
181 *fetchfd = fd;
182 return err;
185 const struct got_error *
186 got_fetch_connect(int *fetchfd, const char *proto, const char *host,
187 const char *port, const char *server_path)
189 const struct got_error *err = NULL;
191 *fetchfd = -1;
193 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
194 err = dial_ssh(fetchfd, host, port, server_path, "upload");
195 else if (strcmp(proto, "git") == 0)
196 err = dial_git(fetchfd, host, port, server_path, "upload");
197 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
198 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
199 else
200 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
201 return err;
204 const struct got_error *
205 got_fetch_parse_uri(char **proto, char **host, char **port,
206 char **server_path, char **repo_name, const char *uri)
208 const struct got_error *err = NULL;
209 char *s, *p, *q;
210 int n, hasport;
212 *proto = *host = *port = *server_path = *repo_name = NULL;
214 p = strstr(uri, "://");
215 if (!p) {
216 return got_error(GOT_ERR_PARSE_URI);
218 *proto = strndup(uri, p - uri);
219 if (proto == NULL) {
220 err = got_error_from_errno("strndup");
221 goto done;
224 hasport = (strcmp(*proto, "git") == 0 ||
225 strstr(*proto, "http") == *proto);
226 s = p + 3;
227 p = NULL;
228 if (!hasport) {
229 p = strstr(s, ":");
230 if (p != NULL)
231 p++;
233 if (p == NULL)
234 p = strstr(s, "/");
235 if (p == NULL || strlen(p) == 1) {
236 err = got_error(GOT_ERR_PARSE_URI);
237 goto done;
240 q = memchr(s, ':', p - s);
241 if (q) {
242 *host = strndup(s, q - s);
243 if (*host == NULL) {
244 err = got_error_from_errno("strndup");
245 goto done;
247 *port = strndup(q + 1, p - (q + 1));
248 if (*port == NULL) {
249 err = got_error_from_errno("strndup");
250 goto done;
252 } else {
253 *host = strndup(s, p - s);
254 if (*host == NULL) {
255 err = got_error_from_errno("strndup");
256 goto done;
258 if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
259 err = got_error_from_errno("asprintf");
260 goto done;
264 *server_path = strdup(p);
265 if (*server_path == NULL) {
266 err = got_error_from_errno("strdup");
267 goto done;
270 p = strrchr(p, '/') + 1;
271 if (!p || strlen(p) == 0) {
272 //werrstr("missing repository in uri");
273 err = got_error(GOT_ERR_PARSE_URI);
274 goto done;
276 n = strlen(p);
277 if (hassuffix(p, ".git"))
278 n -= 4;
279 *repo_name = strndup(p, (p + n) - p);
280 if (*repo_name == NULL) {
281 err = got_error_from_errno("strndup");
282 goto done;
284 done:
285 if (err) {
286 free(*proto);
287 *proto = NULL;
288 free(*host);
289 *host = NULL;
290 free(*port);
291 *port = NULL;
292 free(*server_path);
293 *server_path = NULL;
294 free(*repo_name);
295 *repo_name = NULL;
297 return err;
300 static const struct got_error *
301 check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
303 SHA1_CTX ctx;
304 uint8_t hexpect[SHA1_DIGEST_LENGTH];
305 uint8_t buf[32 * 1024];
306 ssize_t n, r, nr;
308 if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
309 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
311 n = 0;
312 SHA1Init(&ctx);
313 while (n < sz - 20) {
314 nr = sizeof(buf);
315 if (sz - n - 20 < sizeof(buf))
316 nr = sz - n - 20;
317 r = read(fd, buf, nr);
318 if (r == -1)
319 return got_error_from_errno("read");
320 if (r != nr)
321 return got_error_msg(GOT_ERR_BAD_PACKFILE,
322 "short pack file");
323 SHA1Update(&ctx, buf, nr);
324 n += r;
326 SHA1Final(hcomp, &ctx);
328 r = read(fd, hexpect, sizeof(hexpect));
329 if (r == -1)
330 return got_error_from_errno("read");
331 if (r != sizeof(hexpect))
332 return got_error_msg(GOT_ERR_BAD_PACKFILE,
333 "short pack file");
335 if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
336 return got_error_msg(GOT_ERR_BAD_PACKFILE,
337 "packfile checksum mismatch");
339 return NULL;
342 const struct got_error*
343 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
344 struct got_pathlist_head *symrefs, int fetchfd, struct got_repository *repo,
345 got_fetch_progress_cb progress_cb, void *progress_arg)
347 int imsg_fetchfds[2], imsg_idxfds[2];
348 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
349 int fetchstatus, idxstatus, done = 0;
350 const struct got_error *err;
351 struct imsgbuf fetchibuf, idxibuf;
352 pid_t fetchpid, idxpid;
353 char *tmppackpath = NULL, *tmpidxpath = NULL;
354 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
355 const char *repo_path = got_repo_get_path(repo);
356 struct got_pathlist_head have_refs;
357 struct got_pathlist_entry *pe;
358 off_t packfile_size = 0;
359 char *path;
361 *pack_hash = NULL;
363 TAILQ_INIT(&have_refs);
365 if (asprintf(&path, "%s/%s/fetching.pack",
366 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
367 err = got_error_from_errno("asprintf");
368 goto done;
370 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
371 free(path);
372 if (err)
373 goto done;
374 npackfd = dup(packfd);
375 if (npackfd == -1) {
376 err = got_error_from_errno("dup");
377 goto done;
379 if (asprintf(&path, "%s/%s/fetching.idx",
380 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
381 err = got_error_from_errno("asprintf");
382 goto done;
384 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
385 free(path);
386 if (err)
387 goto done;
388 nidxfd = dup(idxfd);
389 if (nidxfd == -1) {
390 err = got_error_from_errno("dup");
391 goto done;
394 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
395 err = got_error_from_errno("socketpair");
396 goto done;
399 fetchpid = fork();
400 if (fetchpid == -1) {
401 err = got_error_from_errno("fork");
402 goto done;
403 } else if (fetchpid == 0){
404 got_privsep_exec_child(imsg_fetchfds,
405 GOT_PATH_PROG_FETCH_PACK, tmppackpath);
408 if (close(imsg_fetchfds[1]) != 0) {
409 err = got_error_from_errno("close");
410 goto done;
412 imsg_init(&fetchibuf, imsg_fetchfds[0]);
413 nfetchfd = dup(fetchfd);
414 if (nfetchfd == -1) {
415 err = got_error_from_errno("dup");
416 goto done;
418 err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs);
419 if (err != NULL)
420 goto done;
421 nfetchfd = -1;
422 err = got_privsep_send_tmpfd(&fetchibuf, npackfd);
423 if (err != NULL)
424 goto done;
425 npackfd = dup(packfd);
426 if (npackfd == -1) {
427 err = got_error_from_errno("dup");
428 goto done;
431 packfile_size = 0;
432 while (!done) {
433 struct got_object_id *id = NULL;
434 char *refname = NULL;
435 char *server_progress = NULL;
436 off_t packfile_size_cur;
438 err = got_privsep_recv_fetch_progress(&done,
439 &id, &refname, symrefs, &server_progress,
440 &packfile_size_cur, &fetchibuf);
441 if (err != NULL)
442 goto done;
443 if (done)
444 *pack_hash = id;
445 else if (refname && id) {
446 err = got_pathlist_append(refs, refname, id);
447 if (err)
448 goto done;
449 } else if (server_progress) {
450 char *s, *s0 = server_progress;
451 while ((s = strsep(&s0, "\r")) != NULL) {
452 if (*s == '\0')
453 continue;
454 err = progress_cb(progress_arg, s,
455 packfile_size_cur, 0, 0, 0, 0);
456 if (err)
457 break;
459 free(server_progress);
460 if (err)
461 goto done;
462 } else if (packfile_size_cur != packfile_size) {
463 err = progress_cb(progress_arg, NULL,
464 packfile_size_cur, 0, 0, 0, 0);
465 if (err)
466 break;
467 packfile_size = packfile_size_cur;
470 if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
471 err = got_error_from_errno("waitpid");
472 goto done;
475 if (lseek(packfd, 0, SEEK_SET) == -1) {
476 err = got_error_from_errno("lseek");
477 goto done;
479 err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
480 if (err)
481 goto done;
483 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
484 err = got_error_from_errno("socketpair");
485 goto done;
487 idxpid = fork();
488 if (idxpid == -1) {
489 err= got_error_from_errno("fork");
490 goto done;
491 } else if (idxpid == 0)
492 got_privsep_exec_child(imsg_idxfds,
493 GOT_PATH_PROG_INDEX_PACK, tmppackpath);
494 if (close(imsg_idxfds[1]) != 0) {
495 err = got_error_from_errno("close");
496 goto done;
498 imsg_init(&idxibuf, imsg_idxfds[0]);
500 err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
501 npackfd);
502 if (err != NULL)
503 goto done;
504 npackfd = -1;
505 err = got_privsep_send_tmpfd(&idxibuf, nidxfd);
506 if (err != NULL)
507 goto done;
508 nidxfd = -1;
509 done = 0;
510 while (!done) {
511 int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
513 err = got_privsep_recv_index_progress(&done, &nobj_total,
514 &nobj_indexed, &nobj_loose, &nobj_resolved,
515 &idxibuf);
516 if (err != NULL)
517 goto done;
518 if (nobj_indexed != 0) {
519 err = progress_cb(progress_arg, NULL,
520 packfile_size, nobj_total,
521 nobj_indexed, nobj_loose, nobj_resolved);
522 if (err)
523 break;
525 imsg_clear(&idxibuf);
527 if (close(imsg_idxfds[0]) == -1) {
528 err = got_error_from_errno("close");
529 goto done;
531 if (waitpid(idxpid, &idxstatus, 0) == -1) {
532 err = got_error_from_errno("waitpid");
533 goto done;
536 err = got_object_id_str(&id_str, *pack_hash);
537 if (err)
538 goto done;
539 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
540 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
541 err = got_error_from_errno("asprintf");
542 goto done;
545 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
546 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
547 err = got_error_from_errno("asprintf");
548 goto done;
551 if (rename(tmppackpath, packpath) == -1) {
552 err = got_error_from_errno3("rename", tmppackpath, packpath);
553 goto done;
555 if (rename(tmpidxpath, idxpath) == -1) {
556 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
557 goto done;
560 done:
561 if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
562 err = got_error_from_errno("close");
563 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
564 err = got_error_from_errno("close");
565 if (packfd != -1 && close(packfd) == -1 && err == NULL)
566 err = got_error_from_errno("close");
567 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
568 err = got_error_from_errno("close");
569 free(tmppackpath);
570 free(tmpidxpath);
571 free(idxpath);
572 free(packpath);
574 if (err) {
575 free(*pack_hash);
576 *pack_hash = NULL;
577 TAILQ_FOREACH(pe, refs, entry) {
578 free((void *)pe->path);
579 free(pe->data);
581 got_pathlist_free(refs);
582 TAILQ_FOREACH(pe, symrefs, entry) {
583 free((void *)pe->path);
584 free(pe->data);
586 got_pathlist_free(symrefs);
588 return err;