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_parse_uri(char **proto, char **host, char **port,
187 char **server_path, char **repo_name, const char *uri)
189 const struct got_error *err = NULL;
190 char *s, *p, *q;
191 int n, hasport;
193 *proto = *host = *port = *server_path = *repo_name = NULL;
195 p = strstr(uri, "://");
196 if (!p) {
197 return got_error(GOT_ERR_PARSE_URI);
199 *proto = strndup(uri, p - uri);
200 if (proto == NULL) {
201 err = got_error_from_errno("strndup");
202 goto done;
205 hasport = (strcmp(*proto, "git") == 0 ||
206 strstr(*proto, "http") == *proto);
207 s = p + 3;
208 p = NULL;
209 if (!hasport) {
210 p = strstr(s, ":");
211 if (p != NULL)
212 p++;
214 if (p == NULL)
215 p = strstr(s, "/");
216 if (p == NULL || strlen(p) == 1) {
217 err = got_error(GOT_ERR_PARSE_URI);
218 goto done;
221 q = memchr(s, ':', p - s);
222 if (q) {
223 *host = strndup(s, q - s);
224 if (*host == NULL) {
225 err = got_error_from_errno("strndup");
226 goto done;
228 *port = strndup(q + 1, p - (q + 1));
229 if (*port == NULL) {
230 err = got_error_from_errno("strndup");
231 goto done;
233 } else {
234 *host = strndup(s, p - s);
235 if (*host == NULL) {
236 err = got_error_from_errno("strndup");
237 goto done;
239 if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
240 err = got_error_from_errno("asprintf");
241 goto done;
245 *server_path = strdup(p);
246 if (*server_path == NULL) {
247 err = got_error_from_errno("strdup");
248 goto done;
251 p = strrchr(p, '/') + 1;
252 if (!p || strlen(p) == 0) {
253 //werrstr("missing repository in uri");
254 err = got_error(GOT_ERR_PARSE_URI);
255 goto done;
257 n = strlen(p);
258 if (hassuffix(p, ".git"))
259 n -= 4;
260 *repo_name = strndup(p, (p + n) - p);
261 if (*repo_name == NULL) {
262 err = got_error_from_errno("strndup");
263 goto done;
265 done:
266 if (err) {
267 free(*proto);
268 *proto = NULL;
269 free(*host);
270 *host = NULL;
271 free(*port);
272 *port = NULL;
273 free(*server_path);
274 *server_path = NULL;
275 free(*repo_name);
276 *repo_name = NULL;
278 return err;
281 const struct got_error*
282 got_fetch(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
283 struct got_pathlist_head *symrefs, const char *proto, const char *host,
284 const char *port, const char *server_path, const char *repo_name,
285 const char *branch_filter, struct got_repository *repo)
287 int imsg_fetchfds[2], imsg_idxfds[2], fetchfd = -1;
288 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1;
289 int status, done = 0;
290 const struct got_error *err;
291 struct imsgbuf ibuf;
292 pid_t pid;
293 char *tmppackpath = NULL, *tmpidxpath = NULL;
294 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
295 const char *repo_path = got_repo_get_path(repo);
296 struct got_pathlist_entry *pe;
297 char *path;
299 *pack_hash = NULL;
301 fetchfd = -1;
303 if (asprintf(&path, "%s/%s/fetching.pack",
304 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
309 free(path);
310 if (err)
311 goto done;
312 npackfd = dup(packfd);
313 if (npackfd == -1) {
314 err = got_error_from_errno("dup");
315 goto done;
317 if (asprintf(&path, "%s/%s/fetching.idx",
318 repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
319 err = got_error_from_errno("asprintf");
320 goto done;
322 err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
323 free(path);
324 if (err)
325 goto done;
326 nidxfd = dup(idxfd);
327 if (nidxfd == -1) {
328 err = got_error_from_errno("dup");
329 goto done;
332 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
333 err = dial_ssh(&fetchfd, host, port, server_path, "upload");
334 else if (strcmp(proto, "git") == 0)
335 err = dial_git(&fetchfd, host, port, server_path, "upload");
336 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
337 err = got_error(GOT_ERR_BAD_PROTO);
338 else
339 err = got_error(GOT_ERR_BAD_PROTO);
340 if (err)
341 goto done;
343 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
344 err = got_error_from_errno("socketpair");
345 goto done;
348 pid = fork();
349 if (pid == -1) {
350 err = got_error_from_errno("fork");
351 goto done;
352 } else if (pid == 0){
353 got_privsep_exec_child(imsg_fetchfds,
354 GOT_PATH_PROG_FETCH_PACK, ".");
357 if (close(imsg_fetchfds[1]) != 0) {
358 err = got_error_from_errno("close");
359 goto done;
361 imsg_init(&ibuf, imsg_fetchfds[0]);
362 err = got_privsep_send_fetch_req(&ibuf, fetchfd);
363 if (err != NULL)
364 goto done;
365 fetchfd = -1;
366 err = got_privsep_send_tmpfd(&ibuf, npackfd);
367 if (err != NULL)
368 goto done;
369 npackfd = dup(packfd);
370 if (npackfd == -1) {
371 err = got_error_from_errno("dup");
372 goto done;
375 while (!done) {
376 struct got_object_id *id = NULL;
377 char *refname = NULL;
379 err = got_privsep_recv_fetch_progress(&done,
380 &id, &refname, symrefs, &ibuf);
381 if (err != NULL)
382 goto done;
383 if (done)
384 *pack_hash = id;
385 else if (refname && id) {
386 err = got_pathlist_append(refs, refname, id);
387 if (err)
388 goto done;
390 /* TODO remote status / download progress callback */
392 if (waitpid(pid, &status, 0) == -1) {
393 err = got_error_from_errno("waitpid");
394 goto done;
397 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
398 err = got_error_from_errno("socketpair");
399 goto done;
401 pid = fork();
402 if (pid == -1) {
403 err= got_error_from_errno("fork");
404 goto done;
405 } else if (pid == 0)
406 got_privsep_exec_child(imsg_idxfds,
407 GOT_PATH_PROG_INDEX_PACK, ".");
408 if (close(imsg_idxfds[1]) != 0) {
409 err = got_error_from_errno("close");
410 goto done;
412 imsg_init(&ibuf, imsg_idxfds[0]);
414 err = got_privsep_send_index_pack_req(&ibuf, npackfd, *pack_hash);
415 if (err != NULL)
416 goto done;
417 npackfd = -1;
418 err = got_privsep_send_tmpfd(&ibuf, nidxfd);
419 if (err != NULL)
420 goto done;
421 nidxfd = -1;
422 err = got_privsep_wait_index_pack_done(&ibuf);
423 if (err != NULL)
424 goto done;
425 imsg_clear(&ibuf);
426 if (close(imsg_idxfds[0]) == -1) {
427 err = got_error_from_errno("close");
428 goto done;
430 if (waitpid(pid, &status, 0) == -1) {
431 err = got_error_from_errno("waitpid");
432 goto done;
435 err = got_object_id_str(&id_str, *pack_hash);
436 if (err)
437 goto done;
438 if (asprintf(&packpath, "%s/%s/pack-%s.pack",
439 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
440 err = got_error_from_errno("asprintf");
441 goto done;
444 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
445 repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
446 err = got_error_from_errno("asprintf");
447 goto done;
450 if (rename(tmppackpath, packpath) == -1) {
451 err = got_error_from_errno3("rename", tmppackpath, packpath);
452 goto done;
454 if (rename(tmpidxpath, idxpath) == -1) {
455 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
456 goto done;
459 done:
460 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
461 err = got_error_from_errno("close");
462 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
463 err = got_error_from_errno("close");
464 if (packfd != -1 && close(packfd) == -1 && err == NULL)
465 err = got_error_from_errno("close");
466 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
467 err = got_error_from_errno("close");
468 free(tmppackpath);
469 free(tmpidxpath);
470 free(idxpath);
471 free(packpath);
473 if (err) {
474 free(*pack_hash);
475 *pack_hash = NULL;
476 TAILQ_FOREACH(pe, refs, entry) {
477 free((void *)pe->path);
478 free(pe->data);
480 got_pathlist_free(refs);
481 TAILQ_FOREACH(pe, symrefs, entry) {
482 free((void *)pe->path);
483 free(pe->data);
485 got_pathlist_free(symrefs);
487 return err;