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, l, 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 ((l = asprintf(&cmd, "git-%s-pack %s\n", direction, path)) == -1) {
205 err = got_error_from_errno("asprintf");
206 goto done;
208 if ((l = asprintf(&pkt, "%04x%s", l + 4, cmd)) == -1) {
209 err = got_error_from_errno("asprintf");
210 goto done;
212 r = write(fd, pkt, l);
213 if (r == -1)
214 err = got_error_from_errno("write");
215 done:
216 free(cmd);
217 free(pkt);
218 if (err) {
219 if (fd != -1)
220 close(fd);
221 } else
222 *fetchfd = fd;
223 return err;
226 int
227 got_parse_uri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
229 char *s, *p, *q;
230 int n, hasport;
232 p = strstr(uri, "://");
233 if (!p) {
234 //werrstr("missing protocol");
235 return -1;
237 if (grab(proto, GOT_PROTOMAX, uri, p) == -1)
238 return -1;
239 hasport = (strcmp(proto, "git") == 0 || strstr(proto, "http") == proto);
240 s = p + 3;
241 p = NULL;
242 if (!hasport) {
243 p = strstr(s, ":");
244 if (p != NULL)
245 p++;
247 if (p == NULL)
248 p = strstr(s, "/");
249 if (p == NULL || strlen(p) == 1) {
250 //werrstr("missing path");
251 return -1;
254 q = memchr(s, ':', p - s);
255 if (q) {
256 grab(host, GOT_HOSTMAX, s, q);
257 grab(port, GOT_PORTMAX, q + 1, p);
258 }else{
259 grab(host, GOT_HOSTMAX, s, p);
260 snprintf(port, GOT_PORTMAX, "9418");
263 snprintf(path, GOT_PATHMAX, "%s", p);
264 p = strrchr(p, '/') + 1;
265 if (!p || strlen(p) == 0) {
266 //werrstr("missing repository in uri");
267 return -1;
269 n = strlen(p);
270 if (hassuffix(p, ".git"))
271 n -= 4;
272 grab(repo, GOT_REPOMAX, p, p + n);
273 return 0;
276 const struct got_error*
277 got_fetch(char *uri, char *branch_filter, char *destdir)
279 char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
280 char repo[GOT_REPOMAX], path[GOT_PATHMAX];
281 int imsg_fetchfds[2], imsg_idxfds[2], fetchfd;
282 int packfd = -1, npackfd, idxfd = -1, nidxfd, status, done = 0;
283 struct got_object_id *packhash = NULL;
284 const struct got_error *err;
285 struct imsgbuf ibuf;
286 pid_t pid;
287 char *tmppackpath = NULL, *tmpidxpath = NULL, *default_destdir = NULL;
288 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
290 fetchfd = -1;
291 if (got_parse_uri(uri, proto, host, port, path, repo) == -1)
292 return got_error(GOT_ERR_PARSE_URI);
293 if (destdir == NULL) {
294 if (asprintf(&default_destdir, "%s.git", repo) == -1)
295 return got_error_from_errno("asprintf");
297 err = got_repo_init(destdir ? destdir : default_destdir);
298 if (err != NULL)
299 return err;
300 if (chdir(destdir ? destdir : default_destdir))
301 return got_error_from_errno("enter new repo");
302 if (mkpath("objects/pack") == -1)
303 return got_error_from_errno("mkpath");
304 err = got_opentemp_named_fd(&tmppackpath, &packfd,
305 "objects/pack/fetching.pack");
306 if (err)
307 return err;
308 npackfd = dup(packfd);
309 if (npackfd == -1)
310 return got_error_from_errno("dup");
311 err = got_opentemp_named_fd(&tmpidxpath, &idxfd,
312 "objects/pack/fetching.idx");
313 if (err)
314 return err;
315 nidxfd = dup(idxfd);
316 if (nidxfd == -1)
317 return got_error_from_errno("dup");
319 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
320 err = dial_ssh(&fetchfd, host, port, path, "upload");
321 else if (strcmp(proto, "git") == 0)
322 err = dial_git(&fetchfd, host, port, path, "upload");
323 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
324 err = got_error(GOT_ERR_BAD_PROTO);
325 else
326 err = got_error(GOT_ERR_BAD_PROTO);
328 if (fetchfd == -1)
329 err = got_error_from_errno("dial uri");
330 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1)
331 return got_error_from_errno("socketpair");
333 pid = fork();
334 if (pid == -1)
335 return got_error_from_errno("fork");
336 else if (pid == 0){
337 got_privsep_exec_child(imsg_fetchfds, GOT_PATH_PROG_FETCH_PACK, ".");
340 if (close(imsg_fetchfds[1]) != 0)
341 return got_error_from_errno("close");
342 imsg_init(&ibuf, imsg_fetchfds[0]);
343 err = got_privsep_send_fetch_req(&ibuf, fetchfd);
344 if (err != NULL)
345 return err;
346 err = got_privsep_send_tmpfd(&ibuf, npackfd);
347 if (err != NULL)
348 return err;
349 npackfd = dup(packfd);
350 if (npackfd == -1)
351 return got_error_from_errno("dup");
352 while (!done) {
353 struct got_object_id *id;
354 char *refname;
355 err = got_privsep_recv_fetch_progress(&done,
356 &id, &refname, &ibuf);
357 if (err != NULL)
358 return err;
359 if (done) {
360 packhash = got_object_id_dup(id);
361 if (packhash == NULL)
362 return got_error_from_errno(
363 "got_object_id_dup");
364 } else {
365 char *id_str;
366 /* TODO Use a progress callback */
367 err = got_object_id_str(&id_str, id);
368 if (err)
369 return err;
370 printf( "%.12s %s\n", id_str, refname);
373 if (waitpid(pid, &status, 0) == -1)
374 return got_error_from_errno("child exit");
376 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1)
377 return got_error_from_errno("socketpair");
378 pid = fork();
379 if (pid == -1)
380 return got_error_from_errno("fork");
381 else if (pid == 0)
382 got_privsep_exec_child(imsg_idxfds, GOT_PATH_PROG_INDEX_PACK, ".");
383 if (close(imsg_idxfds[1]) != 0)
384 return got_error_from_errno("close");
385 imsg_init(&ibuf, imsg_idxfds[0]);
387 err = got_privsep_send_index_pack_req(&ibuf, npackfd, packhash);
388 if (err != NULL)
389 return err;
390 err = got_privsep_send_tmpfd(&ibuf, nidxfd);
391 if (err != NULL)
392 return err;
393 err = got_privsep_wait_index_pack_done(&ibuf);
394 if (err != NULL)
395 return err;
396 imsg_clear(&ibuf);
397 if (close(imsg_idxfds[0]) == -1)
398 return got_error_from_errno("close child");
399 if (waitpid(pid, &status, 0) == -1)
400 return got_error_from_errno("child exit");
402 err = got_object_id_str(&id_str, packhash);
403 if (err)
404 return err;
405 if (asprintf(&packpath, "objects/pack/pack-%s.pack", id_str) == -1)
406 return got_error_from_errno("asprintf");
408 if (asprintf(&idxpath, "objects/pack/pack-%s.idx", id_str) == -1)
409 return got_error_from_errno("asprintf");
411 if (rename(tmppackpath, packpath) == -1)
412 return got_error_from_errno3("rename", tmppackpath, packpath);
413 if (rename(tmpidxpath, idxpath) == -1)
414 return got_error_from_errno3("rename", tmpidxpath, idxpath);
416 free(tmppackpath);
417 free(tmpidxpath);
418 free(idxpath);
419 free(packpath);
420 free(default_destdir);
421 free(packhash);
423 return NULL;