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"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
62 #define GOT_PROTOMAX 64
63 #define GOT_HOSTMAX 256
64 #define GOT_PATHMAX 512
65 #define GOT_REPOMAX 256
66 #define GOT_PORTMAX 16
67 #define GOT_URIMAX 1024
69 static int
70 mkpath(char *path)
71 {
72 char *p, namebuf[PATH_MAX];
73 struct stat sb;
74 int done;
76 while (*path == '/')
77 path++;
78 if (strlcpy(namebuf, path, sizeof(namebuf)) >= sizeof(namebuf)) {
79 errno = ENAMETOOLONG;
80 return -1;
81 }
83 p = namebuf;
84 for (;;) {
85 p += strspn(p, "/");
86 p += strcspn(p, "/");
87 done = (*p == '\0');
88 *p = '\0';
90 if (mkdir(namebuf, 0755) != 0) {
91 int mkdir_errno = errno;
92 if (stat(path, &sb) == -1) {
93 /* Not there; use mkdir()s errno */
94 errno = mkdir_errno;
95 return -1;
96 }
97 if (!S_ISDIR(sb.st_mode)) {
98 /* Is there, but isn't a directory */
99 errno = ENOTDIR;
100 return -1;
104 if (done)
105 break;
106 *p = '/';
109 return 0;
112 static int
113 hassuffix(char *base, char *suf)
115 int nb, ns;
117 nb = strlen(base);
118 ns = strlen(suf);
119 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
120 return 1;
121 return 0;
124 static int
125 grab(char *dst, int n, char *p, char *e)
127 int l;
129 l = e - p;
130 if (l >= n) {
131 errno = ENAMETOOLONG;
132 return -1;
134 return strlcpy(dst, p, l + 1);
137 static int
138 got_dial_ssh(char *host, char *port, char *path, char *direction)
140 int pid, pfd[2];
141 char cmd[64];
143 if (pipe(pfd) == -1)
144 return -1;
145 pid = fork();
146 if (pid == -1)
147 return -1;
148 if (pid == 0) {
149 close(pfd[1]);
150 dup2(pfd[0], 0);
151 dup2(pfd[0], 1);
152 snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
153 execlp("ssh", "ssh", host, cmd, path, NULL);
154 abort();
155 }else{
156 close(pfd[0]);
157 return pfd[1];
161 static int
162 got_dial_git(char *host, char *port, char *path, char *direction)
164 struct addrinfo hints, *servinfo, *p;
165 char *cmd, *pkt;
166 int fd, l, r;
168 memset(&hints, 0, sizeof hints);
169 hints.ai_family = AF_UNSPEC;
170 hints.ai_socktype = SOCK_STREAM;
171 if (getaddrinfo(host, port, &hints, &servinfo) != 0)
172 return -1;
174 for (p = servinfo; p != NULL; p = p->ai_next) {
175 if ((fd = socket(p->ai_family, p->ai_socktype,
176 p->ai_protocol)) == -1)
177 continue;
178 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
179 break;
180 close(fd);
182 if (p == NULL)
183 return -1;
185 if ((l = asprintf(&cmd, "git-%s-pack %s\n", direction, path)) == -1)
186 return -1;
187 if ((l = asprintf(&pkt, "%04x%s", l+4, cmd)) == -1)
188 return -1;
189 r = write(fd, pkt, l);
190 free(cmd);
191 free(pkt);
192 if (r == -1) {
193 close(fd);
194 return -1;
196 return fd;
199 int
200 got_parse_uri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
202 char *s, *p, *q;
203 int n, hasport;
205 p = strstr(uri, "://");
206 if (!p) {
207 //werrstr("missing protocol");
208 return -1;
210 if (grab(proto, GOT_PROTOMAX, uri, p) == -1)
211 return -1;
212 hasport = (strcmp(proto, "git") == 0 || strstr(proto, "http") == proto);
213 s = p + 3;
214 p = NULL;
215 if (!hasport) {
216 p = strstr(s, ":");
217 if (p != NULL)
218 p++;
220 if (p == NULL)
221 p = strstr(s, "/");
222 if (p == NULL || strlen(p) == 1) {
223 //werrstr("missing path");
224 return -1;
227 q = memchr(s, ':', p - s);
228 if (q) {
229 grab(host, GOT_HOSTMAX, s, q);
230 grab(port, GOT_PORTMAX, q + 1, p);
231 }else{
232 grab(host, GOT_HOSTMAX, s, p);
233 snprintf(port, GOT_PORTMAX, "9418");
236 snprintf(path, GOT_PATHMAX, "%s", p);
237 p = strrchr(p, '/') + 1;
238 if (!p || strlen(p) == 0) {
239 //werrstr("missing repository in uri");
240 return -1;
242 n = strlen(p);
243 if (hassuffix(p, ".git"))
244 n -= 4;
245 grab(repo, GOT_REPOMAX, p, p + n);
246 return 0;
249 const struct got_error*
250 got_clone(char *uri, char *branch_filter, char *dirname)
252 char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
253 char repo[GOT_REPOMAX], path[GOT_PATHMAX];
254 int imsg_fetchfds[2], imsg_idxfds[2], fetchfd;
255 int packfd, npackfd, idxfd, nidxfd, status;
256 struct got_object_id packhash;
257 const struct got_error *err;
258 struct imsgbuf ibuf;
259 pid_t pid;
261 fetchfd = -1;
262 if (got_parse_uri(uri, proto, host, port, path, repo) == -1)
263 return got_error(GOT_ERR_PARSE_URI);
264 if (dirname == NULL)
265 dirname = repo;
266 err = got_repo_init(dirname);
267 if (err != NULL)
268 return err;
269 if (chdir(dirname))
270 return got_error_from_errno("enter new repo");
271 if (mkpath(".git/objects/pack") == -1)
272 return got_error_from_errno("mkpath");
273 packfd = open(".git/objects/pack/fetching.pack", O_CREAT|O_RDWR, 0644);
274 if (packfd == -1)
275 return got_error_from_errno("open pack");
276 npackfd = dup(packfd);
277 if (npackfd == -1)
278 return got_error_from_errno("dup");
279 idxfd = open(".git/objects/pack/fetching.idx", O_CREAT|O_RDWR, 0644);
280 if (idxfd == -1)
281 return got_error_from_errno("open pack");
282 nidxfd = dup(idxfd);
283 if (nidxfd == -1)
284 return got_error_from_errno("dup");
286 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
287 fetchfd = got_dial_ssh(host, port, path, "upload");
288 else if (strcmp(proto, "git") == 0)
289 fetchfd = got_dial_git(host, port, path, "upload");
290 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
291 err = got_error(GOT_ERR_BAD_PROTO);
292 else
293 err = got_error(GOT_ERR_BAD_PROTO);
295 if (fetchfd == -1)
296 err = got_error_from_errno("dial uri");
297 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1)
298 return got_error_from_errno("socketpair");
300 pid = fork();
301 if (pid == -1)
302 return got_error_from_errno("fork");
303 else if (pid == 0){
304 got_privsep_exec_child(imsg_fetchfds, GOT_PATH_PROG_FETCH_PACK, ".");
307 if (close(imsg_fetchfds[1]) != 0)
308 return got_error_from_errno("close");
309 imsg_init(&ibuf, imsg_fetchfds[0]);
310 err = got_privsep_send_fetch_req(&ibuf, fetchfd);
311 if (err != NULL)
312 return err;
313 err = got_privsep_wait_ack(&ibuf);
314 if (err != NULL)
315 return err;
316 err = got_privsep_send_tmpfd(&ibuf, npackfd);
317 if (err != NULL)
318 return err;
319 npackfd = dup(packfd);
320 if (npackfd == -1)
321 return got_error_from_errno("dup");
322 err = got_privsep_wait_fetch_done(&ibuf, &packhash);
323 if (err != NULL)
324 return err;
325 if (waitpid(pid, &status, 0) == -1)
326 return got_error_from_errno("child exit");
328 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1)
329 return got_error_from_errno("socketpair");
330 pid = fork();
331 if (pid == -1)
332 return got_error_from_errno("fork");
333 else if (pid == 0)
334 got_privsep_exec_child(imsg_idxfds, GOT_PATH_PROG_INDEX_PACK, ".");
335 if (close(imsg_idxfds[1]) != 0)
336 return got_error_from_errno("close");
337 imsg_init(&ibuf, imsg_idxfds[0]);
339 err = got_privsep_send_index_pack_req(&ibuf, npackfd, packhash);
340 if (err != NULL)
341 return err;
342 err = got_privsep_wait_ack(&ibuf);
343 if (err != NULL)
344 return err;
345 err = got_privsep_send_tmpfd(&ibuf, nidxfd);
346 if (err != NULL)
347 return err;
348 err = got_privsep_wait_index_pack_done(&ibuf);
349 if (err != NULL)
350 return err;
351 imsg_clear(&ibuf);
352 if (close(imsg_idxfds[0]) == -1)
353 return got_error_from_errno("close child");
354 if (waitpid(pid, &status, 0) == -1)
355 return got_error_from_errno("child exit");
358 return NULL;