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 <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <sha1.h>
34 #include <zlib.h>
35 #include <ctype.h>
36 #include <limits.h>
37 #include <imsg.h>
38 #include <time.h>
39 #include <uuid.h>
40 #include <netdb.h>
41 #include <netinet/in.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_object_cache.h"
61 #include "got_lib_repository.h"
63 #define GOT_PROTOMAX 64
64 #define GOT_HOSTMAX 256
65 #define GOT_PATHMAX 512
66 #define GOT_REPOMAX 256
67 #define GOT_PORTMAX 16
68 #define GOT_URIMAX 1024
70 static int
71 mkpath(char *path)
72 {
73 char *p, namebuf[PATH_MAX];
74 struct stat sb;
75 int done;
77 while (*path == '/')
78 path++;
79 if (strlcpy(namebuf, path, sizeof(namebuf)) >= sizeof(namebuf)) {
80 errno = ENAMETOOLONG;
81 return -1;
82 }
84 p = namebuf;
85 for (;;) {
86 p += strspn(p, "/");
87 p += strcspn(p, "/");
88 done = (*p == '\0');
89 *p = '\0';
91 if (mkdir(namebuf, 0755) != 0) {
92 int mkdir_errno = errno;
93 if (stat(path, &sb) == -1) {
94 /* Not there; use mkdir()s errno */
95 errno = mkdir_errno;
96 return -1;
97 }
98 if (!S_ISDIR(sb.st_mode)) {
99 /* Is there, but isn't a directory */
100 errno = ENOTDIR;
101 return -1;
105 if (done)
106 break;
107 *p = '/';
110 return 0;
113 static int
114 hassuffix(char *base, char *suf)
116 int nb, ns;
118 nb = strlen(base);
119 ns = strlen(suf);
120 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
121 return 1;
122 return 0;
125 static int
126 grab(char *dst, int n, char *p, char *e)
128 int l;
130 l = e - p;
131 if (l >= n) {
132 errno = ENAMETOOLONG;
133 return -1;
135 return strlcpy(dst, p, l + 1);
138 static int
139 got_dial_ssh(char *host, char *port, char *path, char *direction)
141 int pid, pfd[2];
142 char cmd[64];
144 if (pipe(pfd) == -1)
145 return -1;
146 pid = fork();
147 if (pid == -1)
148 return -1;
149 if (pid == 0) {
150 close(pfd[1]);
151 dup2(pfd[0], 0);
152 dup2(pfd[0], 1);
153 snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
154 execlp("ssh", "ssh", host, cmd, path, NULL);
155 abort();
156 }else{
157 close(pfd[0]);
158 return pfd[1];
162 static int
163 got_dial_git(char *host, char *port, char *path, char *direction)
165 struct addrinfo hints, *servinfo, *p;
166 char *cmd, *pkt;
167 int fd, l, r;
169 memset(&hints, 0, sizeof hints);
170 hints.ai_family = AF_UNSPEC;
171 hints.ai_socktype = SOCK_STREAM;
172 if (getaddrinfo(host, port, &hints, &servinfo) != 0)
173 return -1;
175 for (p = servinfo; p != NULL; p = p->ai_next) {
176 if ((fd = socket(p->ai_family, p->ai_socktype,
177 p->ai_protocol)) == -1)
178 continue;
179 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
180 break;
181 close(fd);
183 if (p == NULL)
184 return -1;
186 if ((l = asprintf(&cmd, "git-%s-pack %s\n", direction, path)) == -1)
187 return -1;
188 if ((l = asprintf(&pkt, "%04x%s", l+4, cmd)) == -1)
189 return -1;
190 r = write(fd, pkt, l);
191 free(cmd);
192 free(pkt);
193 if (r == -1) {
194 close(fd);
195 return -1;
197 return fd;
200 int
201 got_parse_uri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
203 char *s, *p, *q;
204 int n, hasport;
206 p = strstr(uri, "://");
207 if (!p) {
208 //werrstr("missing protocol");
209 return -1;
211 if (grab(proto, GOT_PROTOMAX, uri, p) == -1)
212 return -1;
213 hasport = (strcmp(proto, "git") == 0 || strstr(proto, "http") == proto);
214 s = p + 3;
215 p = NULL;
216 if (!hasport) {
217 p = strstr(s, ":");
218 if (p != NULL)
219 p++;
221 if (p == NULL)
222 p = strstr(s, "/");
223 if (p == NULL || strlen(p) == 1) {
224 //werrstr("missing path");
225 return -1;
228 q = memchr(s, ':', p - s);
229 if (q) {
230 grab(host, GOT_HOSTMAX, s, q);
231 grab(port, GOT_PORTMAX, q + 1, p);
232 }else{
233 grab(host, GOT_HOSTMAX, s, p);
234 snprintf(port, GOT_PORTMAX, "9418");
237 snprintf(path, GOT_PATHMAX, "%s", p);
238 p = strrchr(p, '/') + 1;
239 if (!p || strlen(p) == 0) {
240 //werrstr("missing repository in uri");
241 return -1;
243 n = strlen(p);
244 if (hassuffix(p, ".git"))
245 n -= 4;
246 grab(repo, GOT_REPOMAX, p, p + n);
247 return 0;
250 const struct got_error*
251 got_fetch(char *uri, char *branch_filter, char *destdir)
253 char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
254 char repo[GOT_REPOMAX], path[GOT_PATHMAX];
255 int imsg_fetchfds[2], imsg_idxfds[2], fetchfd;
256 int packfd = -1, npackfd, idxfd = -1, nidxfd, status;
257 struct got_object_id packhash;
258 const struct got_error *err;
259 struct imsgbuf ibuf;
260 pid_t pid;
261 char *tmppackpath = NULL, *tmpidxpath = NULL, *default_destdir = NULL;
262 char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
264 fetchfd = -1;
265 if (got_parse_uri(uri, proto, host, port, path, repo) == -1)
266 return got_error(GOT_ERR_PARSE_URI);
267 if (destdir == NULL) {
268 if (asprintf(&default_destdir, "%s.git", repo) == -1)
269 return got_error_from_errno("asprintf");
271 err = got_repo_init(destdir ? destdir : default_destdir);
272 if (err != NULL)
273 return err;
274 if (chdir(destdir ? destdir : default_destdir))
275 return got_error_from_errno("enter new repo");
276 if (mkpath("objects/pack") == -1)
277 return got_error_from_errno("mkpath");
278 err = got_opentemp_named_fd(&tmppackpath, &packfd,
279 "objects/pack/fetching.pack");
280 if (err)
281 return err;
282 npackfd = dup(packfd);
283 if (npackfd == -1)
284 return got_error_from_errno("dup");
285 err = got_opentemp_named_fd(&tmpidxpath, &idxfd,
286 "objects/pack/fetching.idx");
287 if (err)
288 return err;
289 nidxfd = dup(idxfd);
290 if (nidxfd == -1)
291 return got_error_from_errno("dup");
293 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
294 fetchfd = got_dial_ssh(host, port, path, "upload");
295 else if (strcmp(proto, "git") == 0)
296 fetchfd = got_dial_git(host, port, path, "upload");
297 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
298 err = got_error(GOT_ERR_BAD_PROTO);
299 else
300 err = got_error(GOT_ERR_BAD_PROTO);
302 if (fetchfd == -1)
303 err = got_error_from_errno("dial uri");
304 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1)
305 return got_error_from_errno("socketpair");
307 pid = fork();
308 if (pid == -1)
309 return got_error_from_errno("fork");
310 else if (pid == 0){
311 got_privsep_exec_child(imsg_fetchfds, GOT_PATH_PROG_FETCH_PACK, ".");
314 if (close(imsg_fetchfds[1]) != 0)
315 return got_error_from_errno("close");
316 imsg_init(&ibuf, imsg_fetchfds[0]);
317 err = got_privsep_send_fetch_req(&ibuf, fetchfd);
318 if (err != NULL)
319 return err;
320 err = got_privsep_send_tmpfd(&ibuf, npackfd);
321 if (err != NULL)
322 return err;
323 npackfd = dup(packfd);
324 if (npackfd == -1)
325 return got_error_from_errno("dup");
326 err = got_privsep_wait_fetch_done(&ibuf, &packhash);
327 if (err != NULL)
328 return err;
329 if (waitpid(pid, &status, 0) == -1)
330 return got_error_from_errno("child exit");
332 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1)
333 return got_error_from_errno("socketpair");
334 pid = fork();
335 if (pid == -1)
336 return got_error_from_errno("fork");
337 else if (pid == 0)
338 got_privsep_exec_child(imsg_idxfds, GOT_PATH_PROG_INDEX_PACK, ".");
339 if (close(imsg_idxfds[1]) != 0)
340 return got_error_from_errno("close");
341 imsg_init(&ibuf, imsg_idxfds[0]);
343 err = got_privsep_send_index_pack_req(&ibuf, npackfd, &packhash);
344 if (err != NULL)
345 return err;
346 err = got_privsep_send_tmpfd(&ibuf, nidxfd);
347 if (err != NULL)
348 return err;
349 err = got_privsep_wait_index_pack_done(&ibuf);
350 if (err != NULL)
351 return err;
352 imsg_clear(&ibuf);
353 if (close(imsg_idxfds[0]) == -1)
354 return got_error_from_errno("close child");
355 if (waitpid(pid, &status, 0) == -1)
356 return got_error_from_errno("child exit");
358 err = got_object_id_str(&id_str, &packhash);
359 if (err)
360 return err;
361 if (asprintf(&packpath, "objects/pack/pack-%s.pack", id_str) == -1)
362 return got_error_from_errno("asprintf");
364 if (asprintf(&idxpath, "objects/pack/pack-%s.idx", id_str) == -1)
365 return got_error_from_errno("asprintf");
367 if (rename(tmppackpath, packpath) == -1)
368 return got_error_from_errno3("rename", tmppackpath, packpath);
369 if (rename(tmpidxpath, idxpath) == -1)
370 return got_error_from_errno3("rename", tmpidxpath, idxpath);
372 free(tmppackpath);
373 free(tmpidxpath);
374 free(idxpath);
375 free(packpath);
376 free(default_destdir);
378 return NULL;