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"
53 #include "got_lib_delta.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_sha1.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
64 #define GOT_PROTOMAX 64
65 #define GOT_HOSTMAX 256
66 #define GOT_PATHMAX 512
67 #define GOT_REPOMAX 256
68 #define GOT_PORTMAX 16
69 #define GOT_URIMAX 1024
71 static int
72 mkpath(char *path)
73 {
74 char *p, namebuf[PATH_MAX];
75 struct stat sb;
76 int done;
78 while (*path == '/')
79 path++;
80 if (strlcpy(namebuf, path, sizeof(namebuf)) >= sizeof(namebuf)) {
81 errno = ENAMETOOLONG;
82 return -1;
83 }
85 p = namebuf;
86 for (;;) {
87 p += strspn(p, "/");
88 p += strcspn(p, "/");
89 done = (*p == '\0');
90 *p = '\0';
92 if (mkdir(namebuf, 0755) != 0) {
93 int mkdir_errno = errno;
94 if (stat(path, &sb) == -1) {
95 /* Not there; use mkdir()s errno */
96 errno = mkdir_errno;
97 return -1;
98 }
99 if (!S_ISDIR(sb.st_mode)) {
100 /* Is there, but isn't a directory */
101 errno = ENOTDIR;
102 return -1;
106 if (done)
107 break;
108 *p = '/';
111 return 0;
114 static int
115 hassuffix(char *base, char *suf)
117 int nb, ns;
119 nb = strlen(base);
120 ns = strlen(suf);
121 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
122 return 1;
123 return 0;
126 static int
127 grab(char *dst, int n, char *p, char *e)
129 int l;
131 l = e - p;
132 if (l >= n) {
133 errno = ENAMETOOLONG;
134 return -1;
136 return strlcpy(dst, p, l + 1);
139 static const struct got_error *
140 dial_ssh(int *fetchfd, char *host, char *port, char *path, char *direction)
142 const struct got_error *error = NULL;
143 int pid, pfd[2];
144 char cmd[64];
146 *fetchfd = -1;
148 if (pipe(pfd) == -1)
149 return got_error_from_errno("pipe");
151 pid = fork();
152 if (pid == -1) {
153 error = got_error_from_errno("fork");
154 close(pfd[0]);
155 close(pfd[1]);
156 return error;
157 } else if (pid == 0) {
158 int n;
159 close(pfd[1]);
160 dup2(pfd[0], 0);
161 dup2(pfd[0], 1);
162 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
163 if (n < 0 || n >= sizeof(cmd))
164 err(1, "snprintf");
165 if (execlp("ssh", "ssh", host, cmd, path, NULL) == -1)
166 err(1, "execlp");
167 abort(); /* not reached */
168 } else {
169 close(pfd[0]);
170 *fetchfd = pfd[1];
171 return NULL;
175 static const struct got_error *
176 dial_git(int *fetchfd, char *host, char *port, char *path, char *direction)
178 const struct got_error *err = NULL;
179 struct addrinfo hints, *servinfo, *p;
180 char *cmd = NULL, *pkt = NULL;
181 int fd = -1, totlen, r, eaicode;
183 *fetchfd = -1;
185 memset(&hints, 0, sizeof hints);
186 hints.ai_family = AF_UNSPEC;
187 hints.ai_socktype = SOCK_STREAM;
188 eaicode = getaddrinfo(host, port, &hints, &servinfo);
189 if (eaicode)
190 return got_error_msg(GOT_ERR_ADDRINFO, gai_strerror(eaicode));
192 for (p = servinfo; p != NULL; p = p->ai_next) {
193 if ((fd = socket(p->ai_family, p->ai_socktype,
194 p->ai_protocol)) == -1)
195 continue;
196 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
197 break;
198 err = got_error_from_errno("connect");
199 close(fd);
201 if (p == NULL)
202 goto done;
204 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
205 err = got_error_from_errno("asprintf");
206 goto done;
208 totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
209 if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
210 err = got_error_from_errno("asprintf");
211 goto done;
213 r = write(fd, pkt, strlen(pkt) + 1);
214 if (r == -1) {
215 err = got_error_from_errno("write");
216 goto done;
218 if (asprintf(&pkt, "host=%s", host) == -1) {
219 err = got_error_from_errno("asprintf");
220 goto done;
222 r = write(fd, pkt, strlen(pkt) + 1);
223 if (r == -1) {
224 err = got_error_from_errno("write");
225 goto done;
227 done:
228 free(cmd);
229 free(pkt);
230 if (err) {
231 if (fd != -1)
232 close(fd);
233 } else
234 *fetchfd = fd;
235 return err;
238 int
239 got_parse_uri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
241 char *s, *p, *q;
242 int n, hasport;
244 p = strstr(uri, "://");
245 if (!p) {
246 //werrstr("missing protocol");
247 return -1;
249 if (grab(proto, GOT_PROTOMAX, uri, p) == -1)
250 return -1;
251 hasport = (strcmp(proto, "git") == 0 || strstr(proto, "http") == proto);
252 s = p + 3;
253 p = NULL;
254 if (!hasport) {
255 p = strstr(s, ":");
256 if (p != NULL)
257 p++;
259 if (p == NULL)
260 p = strstr(s, "/");
261 if (p == NULL || strlen(p) == 1) {
262 //werrstr("missing path");
263 return -1;
266 q = memchr(s, ':', p - s);
267 if (q) {
268 grab(host, GOT_HOSTMAX, s, q);
269 grab(port, GOT_PORTMAX, q + 1, p);
270 }else{
271 grab(host, GOT_HOSTMAX, s, p);
272 snprintf(port, GOT_PORTMAX, "9418");
275 snprintf(path, GOT_PATHMAX, "%s", p);
276 p = strrchr(p, '/') + 1;
277 if (!p || strlen(p) == 0) {
278 //werrstr("missing repository in uri");
279 return -1;
281 n = strlen(p);
282 if (hassuffix(p, ".git"))
283 n -= 4;
284 grab(repo, GOT_REPOMAX, p, p + n);
285 return 0;
288 const struct got_error*
289 got_fetch(char *uri, char *branch_filter, char *destdir)
291 char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
292 char repo[GOT_REPOMAX], path[GOT_PATHMAX];
293 int imsg_fetchfds[2], imsg_idxfds[2], fetchfd = -1;
294 int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1;
295 int status, done = 0;
296 struct got_object_id *packhash = NULL;
297 const struct got_error *err;
298 struct imsgbuf ibuf;
299 pid_t pid;
300 char *tmppackpath = NULL, *tmpidxpath = NULL, *default_destdir = NULL;
301 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
302 struct got_pathlist_head symrefs;
303 struct got_pathlist_entry *pe;
305 TAILQ_INIT(&symrefs);
307 fetchfd = -1;
308 if (got_parse_uri(uri, proto, host, port, path, repo) == -1)
309 return got_error(GOT_ERR_PARSE_URI);
310 if (destdir == NULL) {
311 if (asprintf(&default_destdir, "%s.git", repo) == -1)
312 return got_error_from_errno("asprintf");
314 err = got_repo_init(destdir ? destdir : default_destdir);
315 if (err != NULL)
316 goto done;
317 if (chdir(destdir ? destdir : default_destdir)) {
318 err = got_error_from_errno("enter new repo");
319 goto done;
321 if (mkpath("objects/pack") == -1) {
322 err = got_error_from_errno("mkpath");
323 goto done;
325 err = got_opentemp_named_fd(&tmppackpath, &packfd,
326 "objects/pack/fetching.pack");
327 if (err)
328 goto done;
329 npackfd = dup(packfd);
330 if (npackfd == -1) {
331 err = got_error_from_errno("dup");
332 goto done;
334 err = got_opentemp_named_fd(&tmpidxpath, &idxfd,
335 "objects/pack/fetching.idx");
336 if (err)
337 goto done;
338 nidxfd = dup(idxfd);
339 if (nidxfd == -1) {
340 err = got_error_from_errno("dup");
341 goto done;
344 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
345 err = dial_ssh(&fetchfd, host, port, path, "upload");
346 else if (strcmp(proto, "git") == 0)
347 err = dial_git(&fetchfd, host, port, path, "upload");
348 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
349 err = got_error(GOT_ERR_BAD_PROTO);
350 else
351 err = got_error(GOT_ERR_BAD_PROTO);
352 if (err)
353 goto done;
355 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
356 err = got_error_from_errno("socketpair");
357 goto done;
360 pid = fork();
361 if (pid == -1) {
362 err = got_error_from_errno("fork");
363 goto done;
364 } else if (pid == 0){
365 got_privsep_exec_child(imsg_fetchfds,
366 GOT_PATH_PROG_FETCH_PACK, ".");
369 if (close(imsg_fetchfds[1]) != 0) {
370 err = got_error_from_errno("close");
371 goto done;
373 imsg_init(&ibuf, imsg_fetchfds[0]);
374 err = got_privsep_send_fetch_req(&ibuf, fetchfd);
375 if (err != NULL)
376 goto done;
377 fetchfd = -1;
378 err = got_privsep_send_tmpfd(&ibuf, npackfd);
379 if (err != NULL)
380 goto done;
381 npackfd = dup(packfd);
382 if (npackfd == -1) {
383 err = got_error_from_errno("dup");
384 goto done;
387 while (!done) {
388 struct got_object_id *id;
389 char *refname;
391 err = got_privsep_recv_fetch_progress(&done,
392 &id, &refname, &symrefs, &ibuf);
393 if (err != NULL)
394 goto done;
395 if (done) {
396 packhash = got_object_id_dup(id);
397 if (packhash == NULL) {
398 err = got_error_from_errno(
399 "got_object_id_dup");
400 goto done;
402 printf("symrefs:");
403 TAILQ_FOREACH(pe, &symrefs, entry) {
404 printf(" %s:%s", pe->path,
405 (const char *)pe->data);
407 printf("\n");
408 } else if (id) {
409 char *id_str;
410 /* TODO Use a progress callback */
411 err = got_object_id_str(&id_str, id);
412 if (err)
413 goto done;
414 printf( "%.12s %s\n", id_str, refname);
417 if (waitpid(pid, &status, 0) == -1) {
418 err = got_error_from_errno("waitpid");
419 goto done;
422 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
423 err = got_error_from_errno("socketpair");
424 goto done;
426 pid = fork();
427 if (pid == -1) {
428 err= got_error_from_errno("fork");
429 goto done;
430 } else if (pid == 0)
431 got_privsep_exec_child(imsg_idxfds,
432 GOT_PATH_PROG_INDEX_PACK, ".");
433 if (close(imsg_idxfds[1]) != 0) {
434 err = got_error_from_errno("close");
435 goto done;
437 imsg_init(&ibuf, imsg_idxfds[0]);
439 err = got_privsep_send_index_pack_req(&ibuf, npackfd, packhash);
440 if (err != NULL)
441 goto done;
442 npackfd = -1;
443 err = got_privsep_send_tmpfd(&ibuf, nidxfd);
444 if (err != NULL)
445 goto done;
446 nidxfd = -1;
447 err = got_privsep_wait_index_pack_done(&ibuf);
448 if (err != NULL)
449 goto done;
450 imsg_clear(&ibuf);
451 if (close(imsg_idxfds[0]) == -1) {
452 err = got_error_from_errno("close");
453 goto done;
455 if (waitpid(pid, &status, 0) == -1) {
456 err = got_error_from_errno("waitpid");
457 goto done;
460 err = got_object_id_str(&id_str, packhash);
461 if (err)
462 goto done;
463 if (asprintf(&packpath, "objects/pack/pack-%s.pack", id_str) == -1) {
464 err = got_error_from_errno("asprintf");
465 goto done;
468 if (asprintf(&idxpath, "objects/pack/pack-%s.idx", id_str) == -1) {
469 err = got_error_from_errno("asprintf");
470 goto done;
473 if (rename(tmppackpath, packpath) == -1) {
474 err = got_error_from_errno3("rename", tmppackpath, packpath);
475 goto done;
477 if (rename(tmpidxpath, idxpath) == -1) {
478 err = got_error_from_errno3("rename", tmpidxpath, idxpath);
479 goto done;
481 done:
482 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
483 err = got_error_from_errno("close");
484 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
485 err = got_error_from_errno("close");
486 if (packfd != -1 && close(packfd) == -1 && err == NULL)
487 err = got_error_from_errno("close");
488 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
489 err = got_error_from_errno("close");
490 free(tmppackpath);
491 free(tmpidxpath);
492 free(idxpath);
493 free(packpath);
494 free(default_destdir);
495 free(packhash);
496 TAILQ_FOREACH(pe, &symrefs, entry) {
497 free((void *)pe->path);
498 free(pe->data);
500 got_pathlist_free(&symrefs);
502 return err;