Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 93658fb9 2020-03-18 stsp *
4 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
7 93658fb9 2020-03-18 stsp *
8 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 93658fb9 2020-03-18 stsp */
16 93658fb9 2020-03-18 stsp
17 93658fb9 2020-03-18 stsp #include <sys/types.h>
18 93658fb9 2020-03-18 stsp #include <sys/stat.h>
19 93658fb9 2020-03-18 stsp #include <sys/queue.h>
20 93658fb9 2020-03-18 stsp #include <sys/uio.h>
21 93658fb9 2020-03-18 stsp #include <sys/socket.h>
22 93658fb9 2020-03-18 stsp #include <sys/wait.h>
23 93658fb9 2020-03-18 stsp #include <sys/syslimits.h>
24 93658fb9 2020-03-18 stsp #include <sys/resource.h>
25 93658fb9 2020-03-18 stsp #include <sys/socket.h>
26 93658fb9 2020-03-18 stsp
27 93658fb9 2020-03-18 stsp #include <errno.h>
28 5cc27ede 2020-03-18 stsp #include <err.h>
29 93658fb9 2020-03-18 stsp #include <fcntl.h>
30 93658fb9 2020-03-18 stsp #include <stdio.h>
31 93658fb9 2020-03-18 stsp #include <stdlib.h>
32 93658fb9 2020-03-18 stsp #include <string.h>
33 93658fb9 2020-03-18 stsp #include <stdint.h>
34 93658fb9 2020-03-18 stsp #include <sha1.h>
35 93658fb9 2020-03-18 stsp #include <zlib.h>
36 93658fb9 2020-03-18 stsp #include <ctype.h>
37 93658fb9 2020-03-18 stsp #include <limits.h>
38 93658fb9 2020-03-18 stsp #include <imsg.h>
39 93658fb9 2020-03-18 stsp #include <time.h>
40 93658fb9 2020-03-18 stsp #include <uuid.h>
41 93658fb9 2020-03-18 stsp #include <netdb.h>
42 93658fb9 2020-03-18 stsp #include <netinet/in.h>
43 93658fb9 2020-03-18 stsp
44 93658fb9 2020-03-18 stsp #include "got_error.h"
45 93658fb9 2020-03-18 stsp #include "got_reference.h"
46 93658fb9 2020-03-18 stsp #include "got_repository.h"
47 93658fb9 2020-03-18 stsp #include "got_path.h"
48 93658fb9 2020-03-18 stsp #include "got_cancel.h"
49 93658fb9 2020-03-18 stsp #include "got_worktree.h"
50 93658fb9 2020-03-18 stsp #include "got_object.h"
51 fe4e1501 2020-03-18 stsp #include "got_opentemp.h"
52 82ebf666 2020-03-18 stsp #include "got_fetch.h"
53 93658fb9 2020-03-18 stsp
54 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
55 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
56 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
57 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
58 93658fb9 2020-03-18 stsp #include "got_lib_object_create.h"
59 93658fb9 2020-03-18 stsp #include "got_lib_pack.h"
60 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
61 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
62 93658fb9 2020-03-18 stsp #include "got_lib_object_cache.h"
63 93658fb9 2020-03-18 stsp #include "got_lib_repository.h"
64 d582f26c 2020-03-18 stsp
65 d582f26c 2020-03-18 stsp #ifndef nitems
66 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 d582f26c 2020-03-18 stsp #endif
68 93658fb9 2020-03-18 stsp
69 68999b92 2020-03-18 stsp #ifndef MIN
70 68999b92 2020-03-18 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
71 68999b92 2020-03-18 stsp #endif
72 68999b92 2020-03-18 stsp
73 93658fb9 2020-03-18 stsp static int
74 93658fb9 2020-03-18 stsp hassuffix(char *base, char *suf)
75 93658fb9 2020-03-18 stsp {
76 93658fb9 2020-03-18 stsp int nb, ns;
77 93658fb9 2020-03-18 stsp
78 93658fb9 2020-03-18 stsp nb = strlen(base);
79 93658fb9 2020-03-18 stsp ns = strlen(suf);
80 93658fb9 2020-03-18 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
81 93658fb9 2020-03-18 stsp return 1;
82 93658fb9 2020-03-18 stsp return 0;
83 93658fb9 2020-03-18 stsp }
84 93658fb9 2020-03-18 stsp
85 5cc27ede 2020-03-18 stsp static const struct got_error *
86 09838ffc 2020-03-18 stsp dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
87 68999b92 2020-03-18 stsp const char *direction, int verbosity)
88 93658fb9 2020-03-18 stsp {
89 5cc27ede 2020-03-18 stsp const struct got_error *error = NULL;
90 93658fb9 2020-03-18 stsp int pid, pfd[2];
91 93658fb9 2020-03-18 stsp char cmd[64];
92 68999b92 2020-03-18 stsp char *argv[9];
93 68999b92 2020-03-18 stsp int i = 0;
94 93658fb9 2020-03-18 stsp
95 5cc27ede 2020-03-18 stsp *fetchfd = -1;
96 68999b92 2020-03-18 stsp
97 68999b92 2020-03-18 stsp argv[0] = GOT_FETCH_PATH_SSH;
98 68999b92 2020-03-18 stsp if (verbosity == -1) {
99 68999b92 2020-03-18 stsp argv[1 + i++] = "-q";
100 68999b92 2020-03-18 stsp } else {
101 68999b92 2020-03-18 stsp /* ssh(1) allows up to 3 "-v" options. */
102 68999b92 2020-03-18 stsp for (i = 0; i < MIN(3, verbosity); i++)
103 68999b92 2020-03-18 stsp argv[1 + i] = "-v";
104 68999b92 2020-03-18 stsp }
105 68999b92 2020-03-18 stsp argv[1 + i] = "--";
106 68999b92 2020-03-18 stsp argv[2 + i] = (char *)host;
107 68999b92 2020-03-18 stsp argv[3 + i] = (char *)cmd;
108 68999b92 2020-03-18 stsp argv[4 + i] = (char *)path;
109 68999b92 2020-03-18 stsp argv[5 + i] = NULL;
110 5cc27ede 2020-03-18 stsp
111 93658fb9 2020-03-18 stsp if (pipe(pfd) == -1)
112 5cc27ede 2020-03-18 stsp return got_error_from_errno("pipe");
113 5cc27ede 2020-03-18 stsp
114 93658fb9 2020-03-18 stsp pid = fork();
115 5cc27ede 2020-03-18 stsp if (pid == -1) {
116 5cc27ede 2020-03-18 stsp error = got_error_from_errno("fork");
117 5cc27ede 2020-03-18 stsp close(pfd[0]);
118 93658fb9 2020-03-18 stsp close(pfd[1]);
119 5cc27ede 2020-03-18 stsp return error;
120 5cc27ede 2020-03-18 stsp } else if (pid == 0) {
121 5cc27ede 2020-03-18 stsp int n;
122 5cc27ede 2020-03-18 stsp close(pfd[1]);
123 93658fb9 2020-03-18 stsp dup2(pfd[0], 0);
124 93658fb9 2020-03-18 stsp dup2(pfd[0], 1);
125 5cc27ede 2020-03-18 stsp n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
126 5cc27ede 2020-03-18 stsp if (n < 0 || n >= sizeof(cmd))
127 5cc27ede 2020-03-18 stsp err(1, "snprintf");
128 68999b92 2020-03-18 stsp if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
129 ee448f5f 2020-03-18 stsp err(1, "execl");
130 5cc27ede 2020-03-18 stsp abort(); /* not reached */
131 5cc27ede 2020-03-18 stsp } else {
132 93658fb9 2020-03-18 stsp close(pfd[0]);
133 5cc27ede 2020-03-18 stsp *fetchfd = pfd[1];
134 5cc27ede 2020-03-18 stsp return NULL;
135 93658fb9 2020-03-18 stsp }
136 93658fb9 2020-03-18 stsp }
137 93658fb9 2020-03-18 stsp
138 5cc27ede 2020-03-18 stsp static const struct got_error *
139 09838ffc 2020-03-18 stsp dial_git(int *fetchfd, const char *host, const char *port, const char *path,
140 09838ffc 2020-03-18 stsp const char *direction)
141 93658fb9 2020-03-18 stsp {
142 5cc27ede 2020-03-18 stsp const struct got_error *err = NULL;
143 93658fb9 2020-03-18 stsp struct addrinfo hints, *servinfo, *p;
144 5cc27ede 2020-03-18 stsp char *cmd = NULL, *pkt = NULL;
145 4312a498 2020-03-18 stsp int fd = -1, totlen, r, eaicode;
146 5cc27ede 2020-03-18 stsp
147 5cc27ede 2020-03-18 stsp *fetchfd = -1;
148 93658fb9 2020-03-18 stsp
149 93658fb9 2020-03-18 stsp memset(&hints, 0, sizeof hints);
150 93658fb9 2020-03-18 stsp hints.ai_family = AF_UNSPEC;
151 93658fb9 2020-03-18 stsp hints.ai_socktype = SOCK_STREAM;
152 5cc27ede 2020-03-18 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
153 a117fd10 2020-03-18 stsp if (eaicode) {
154 a117fd10 2020-03-18 stsp char msg[512];
155 a117fd10 2020-03-18 stsp snprintf(msg, sizeof(msg), "%s: %s", host,
156 a117fd10 2020-03-18 stsp gai_strerror(eaicode));
157 a117fd10 2020-03-18 stsp return got_error_msg(GOT_ERR_ADDRINFO, msg);
158 a117fd10 2020-03-18 stsp }
159 93658fb9 2020-03-18 stsp
160 93658fb9 2020-03-18 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
161 93658fb9 2020-03-18 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
162 93658fb9 2020-03-18 stsp p->ai_protocol)) == -1)
163 93658fb9 2020-03-18 stsp continue;
164 93658fb9 2020-03-18 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0)
165 93658fb9 2020-03-18 stsp break;
166 5cc27ede 2020-03-18 stsp err = got_error_from_errno("connect");
167 93658fb9 2020-03-18 stsp close(fd);
168 93658fb9 2020-03-18 stsp }
169 93658fb9 2020-03-18 stsp if (p == NULL)
170 5cc27ede 2020-03-18 stsp goto done;
171 93658fb9 2020-03-18 stsp
172 4312a498 2020-03-18 stsp if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
173 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
174 5cc27ede 2020-03-18 stsp goto done;
175 5cc27ede 2020-03-18 stsp }
176 4312a498 2020-03-18 stsp totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
177 4312a498 2020-03-18 stsp if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
178 5cc27ede 2020-03-18 stsp err = got_error_from_errno("asprintf");
179 5cc27ede 2020-03-18 stsp goto done;
180 5cc27ede 2020-03-18 stsp }
181 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
182 4312a498 2020-03-18 stsp if (r == -1) {
183 4312a498 2020-03-18 stsp err = got_error_from_errno("write");
184 4312a498 2020-03-18 stsp goto done;
185 4312a498 2020-03-18 stsp }
186 4312a498 2020-03-18 stsp if (asprintf(&pkt, "host=%s", host) == -1) {
187 4312a498 2020-03-18 stsp err = got_error_from_errno("asprintf");
188 4312a498 2020-03-18 stsp goto done;
189 4312a498 2020-03-18 stsp }
190 4312a498 2020-03-18 stsp r = write(fd, pkt, strlen(pkt) + 1);
191 4312a498 2020-03-18 stsp if (r == -1) {
192 5cc27ede 2020-03-18 stsp err = got_error_from_errno("write");
193 4312a498 2020-03-18 stsp goto done;
194 4312a498 2020-03-18 stsp }
195 5cc27ede 2020-03-18 stsp done:
196 93658fb9 2020-03-18 stsp free(cmd);
197 93658fb9 2020-03-18 stsp free(pkt);
198 5cc27ede 2020-03-18 stsp if (err) {
199 5cc27ede 2020-03-18 stsp if (fd != -1)
200 5cc27ede 2020-03-18 stsp close(fd);
201 5cc27ede 2020-03-18 stsp } else
202 5cc27ede 2020-03-18 stsp *fetchfd = fd;
203 20eb36d0 2020-03-18 stsp return err;
204 20eb36d0 2020-03-18 stsp }
205 20eb36d0 2020-03-18 stsp
206 20eb36d0 2020-03-18 stsp const struct got_error *
207 20eb36d0 2020-03-18 stsp got_fetch_connect(int *fetchfd, const char *proto, const char *host,
208 68999b92 2020-03-18 stsp const char *port, const char *server_path, int verbosity)
209 20eb36d0 2020-03-18 stsp {
210 20eb36d0 2020-03-18 stsp const struct got_error *err = NULL;
211 20eb36d0 2020-03-18 stsp
212 20eb36d0 2020-03-18 stsp *fetchfd = -1;
213 20eb36d0 2020-03-18 stsp
214 20eb36d0 2020-03-18 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
215 68999b92 2020-03-18 stsp err = dial_ssh(fetchfd, host, port, server_path, "upload",
216 68999b92 2020-03-18 stsp verbosity);
217 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "git") == 0)
218 20eb36d0 2020-03-18 stsp err = dial_git(fetchfd, host, port, server_path, "upload");
219 20eb36d0 2020-03-18 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
220 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
221 20eb36d0 2020-03-18 stsp else
222 20eb36d0 2020-03-18 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
223 5cc27ede 2020-03-18 stsp return err;
224 93658fb9 2020-03-18 stsp }
225 93658fb9 2020-03-18 stsp
226 82ebf666 2020-03-18 stsp const struct got_error *
227 82ebf666 2020-03-18 stsp got_fetch_parse_uri(char **proto, char **host, char **port,
228 82ebf666 2020-03-18 stsp char **server_path, char **repo_name, const char *uri)
229 93658fb9 2020-03-18 stsp {
230 82ebf666 2020-03-18 stsp const struct got_error *err = NULL;
231 93658fb9 2020-03-18 stsp char *s, *p, *q;
232 93658fb9 2020-03-18 stsp int n, hasport;
233 93658fb9 2020-03-18 stsp
234 82ebf666 2020-03-18 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
235 82ebf666 2020-03-18 stsp
236 93658fb9 2020-03-18 stsp p = strstr(uri, "://");
237 93658fb9 2020-03-18 stsp if (!p) {
238 82ebf666 2020-03-18 stsp return got_error(GOT_ERR_PARSE_URI);
239 93658fb9 2020-03-18 stsp }
240 82ebf666 2020-03-18 stsp *proto = strndup(uri, p - uri);
241 82ebf666 2020-03-18 stsp if (proto == NULL) {
242 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
243 82ebf666 2020-03-18 stsp goto done;
244 82ebf666 2020-03-18 stsp }
245 82ebf666 2020-03-18 stsp
246 82ebf666 2020-03-18 stsp hasport = (strcmp(*proto, "git") == 0 ||
247 82ebf666 2020-03-18 stsp strstr(*proto, "http") == *proto);
248 93658fb9 2020-03-18 stsp s = p + 3;
249 93658fb9 2020-03-18 stsp p = NULL;
250 93658fb9 2020-03-18 stsp if (!hasport) {
251 93658fb9 2020-03-18 stsp p = strstr(s, ":");
252 93658fb9 2020-03-18 stsp if (p != NULL)
253 93658fb9 2020-03-18 stsp p++;
254 93658fb9 2020-03-18 stsp }
255 93658fb9 2020-03-18 stsp if (p == NULL)
256 93658fb9 2020-03-18 stsp p = strstr(s, "/");
257 93658fb9 2020-03-18 stsp if (p == NULL || strlen(p) == 1) {
258 82ebf666 2020-03-18 stsp err = got_error(GOT_ERR_PARSE_URI);
259 82ebf666 2020-03-18 stsp goto done;
260 93658fb9 2020-03-18 stsp }
261 93658fb9 2020-03-18 stsp
262 93658fb9 2020-03-18 stsp q = memchr(s, ':', p - s);
263 93658fb9 2020-03-18 stsp if (q) {
264 82ebf666 2020-03-18 stsp *host = strndup(s, q - s);
265 82ebf666 2020-03-18 stsp if (*host == NULL) {
266 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
267 82ebf666 2020-03-18 stsp goto done;
268 82ebf666 2020-03-18 stsp }
269 82ebf666 2020-03-18 stsp *port = strndup(q + 1, p - (q + 1));
270 82ebf666 2020-03-18 stsp if (*port == NULL) {
271 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
272 82ebf666 2020-03-18 stsp goto done;
273 82ebf666 2020-03-18 stsp }
274 82ebf666 2020-03-18 stsp } else {
275 82ebf666 2020-03-18 stsp *host = strndup(s, p - s);
276 82ebf666 2020-03-18 stsp if (*host == NULL) {
277 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
278 82ebf666 2020-03-18 stsp goto done;
279 82ebf666 2020-03-18 stsp }
280 82ebf666 2020-03-18 stsp if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
281 82ebf666 2020-03-18 stsp err = got_error_from_errno("asprintf");
282 82ebf666 2020-03-18 stsp goto done;
283 82ebf666 2020-03-18 stsp }
284 93658fb9 2020-03-18 stsp }
285 93658fb9 2020-03-18 stsp
286 82ebf666 2020-03-18 stsp *server_path = strdup(p);
287 82ebf666 2020-03-18 stsp if (*server_path == NULL) {
288 82ebf666 2020-03-18 stsp err = got_error_from_errno("strdup");
289 82ebf666 2020-03-18 stsp goto done;
290 82ebf666 2020-03-18 stsp }
291 82ebf666 2020-03-18 stsp
292 93658fb9 2020-03-18 stsp p = strrchr(p, '/') + 1;
293 93658fb9 2020-03-18 stsp if (!p || strlen(p) == 0) {
294 93658fb9 2020-03-18 stsp //werrstr("missing repository in uri");
295 82ebf666 2020-03-18 stsp err = got_error(GOT_ERR_PARSE_URI);
296 82ebf666 2020-03-18 stsp goto done;
297 93658fb9 2020-03-18 stsp }
298 93658fb9 2020-03-18 stsp n = strlen(p);
299 93658fb9 2020-03-18 stsp if (hassuffix(p, ".git"))
300 93658fb9 2020-03-18 stsp n -= 4;
301 82ebf666 2020-03-18 stsp *repo_name = strndup(p, (p + n) - p);
302 82ebf666 2020-03-18 stsp if (*repo_name == NULL) {
303 82ebf666 2020-03-18 stsp err = got_error_from_errno("strndup");
304 82ebf666 2020-03-18 stsp goto done;
305 82ebf666 2020-03-18 stsp }
306 82ebf666 2020-03-18 stsp done:
307 82ebf666 2020-03-18 stsp if (err) {
308 82ebf666 2020-03-18 stsp free(*proto);
309 82ebf666 2020-03-18 stsp *proto = NULL;
310 82ebf666 2020-03-18 stsp free(*host);
311 82ebf666 2020-03-18 stsp *host = NULL;
312 82ebf666 2020-03-18 stsp free(*port);
313 82ebf666 2020-03-18 stsp *port = NULL;
314 82ebf666 2020-03-18 stsp free(*server_path);
315 82ebf666 2020-03-18 stsp *server_path = NULL;
316 82ebf666 2020-03-18 stsp free(*repo_name);
317 82ebf666 2020-03-18 stsp *repo_name = NULL;
318 82ebf666 2020-03-18 stsp }
319 82ebf666 2020-03-18 stsp return err;
320 849f7557 2020-03-18 stsp }
321 849f7557 2020-03-18 stsp
322 849f7557 2020-03-18 stsp static const struct got_error *
323 849f7557 2020-03-18 stsp check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
324 849f7557 2020-03-18 stsp {
325 849f7557 2020-03-18 stsp SHA1_CTX ctx;
326 849f7557 2020-03-18 stsp uint8_t hexpect[SHA1_DIGEST_LENGTH];
327 849f7557 2020-03-18 stsp uint8_t buf[32 * 1024];
328 849f7557 2020-03-18 stsp ssize_t n, r, nr;
329 849f7557 2020-03-18 stsp
330 849f7557 2020-03-18 stsp if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
331 849f7557 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE, "short packfile");
332 849f7557 2020-03-18 stsp
333 849f7557 2020-03-18 stsp n = 0;
334 849f7557 2020-03-18 stsp SHA1Init(&ctx);
335 849f7557 2020-03-18 stsp while (n < sz - 20) {
336 849f7557 2020-03-18 stsp nr = sizeof(buf);
337 849f7557 2020-03-18 stsp if (sz - n - 20 < sizeof(buf))
338 849f7557 2020-03-18 stsp nr = sz - n - 20;
339 849f7557 2020-03-18 stsp r = read(fd, buf, nr);
340 849f7557 2020-03-18 stsp if (r == -1)
341 849f7557 2020-03-18 stsp return got_error_from_errno("read");
342 849f7557 2020-03-18 stsp if (r != nr)
343 849f7557 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
344 849f7557 2020-03-18 stsp "short pack file");
345 849f7557 2020-03-18 stsp SHA1Update(&ctx, buf, nr);
346 849f7557 2020-03-18 stsp n += r;
347 849f7557 2020-03-18 stsp }
348 849f7557 2020-03-18 stsp SHA1Final(hcomp, &ctx);
349 849f7557 2020-03-18 stsp
350 849f7557 2020-03-18 stsp r = read(fd, hexpect, sizeof(hexpect));
351 849f7557 2020-03-18 stsp if (r == -1)
352 849f7557 2020-03-18 stsp return got_error_from_errno("read");
353 849f7557 2020-03-18 stsp if (r != sizeof(hexpect))
354 849f7557 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
355 849f7557 2020-03-18 stsp "short pack file");
356 849f7557 2020-03-18 stsp
357 849f7557 2020-03-18 stsp if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
358 849f7557 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
359 849f7557 2020-03-18 stsp "packfile checksum mismatch");
360 849f7557 2020-03-18 stsp
361 849f7557 2020-03-18 stsp return NULL;
362 93658fb9 2020-03-18 stsp }
363 93658fb9 2020-03-18 stsp
364 93658fb9 2020-03-18 stsp const struct got_error*
365 07e52fce 2020-03-18 stsp got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
366 531c3985 2020-03-18 stsp struct got_pathlist_head *symrefs, int fetchfd, struct got_repository *repo,
367 531c3985 2020-03-18 stsp got_fetch_progress_cb progress_cb, void *progress_arg)
368 93658fb9 2020-03-18 stsp {
369 20eb36d0 2020-03-18 stsp int imsg_fetchfds[2], imsg_idxfds[2];
370 20eb36d0 2020-03-18 stsp int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
371 d582f26c 2020-03-18 stsp int tmpfds[3], i;
372 85e8591f 2020-03-18 stsp int fetchstatus, idxstatus, done = 0;
373 93658fb9 2020-03-18 stsp const struct got_error *err;
374 85e8591f 2020-03-18 stsp struct imsgbuf fetchibuf, idxibuf;
375 85e8591f 2020-03-18 stsp pid_t fetchpid, idxpid;
376 bb64b798 2020-03-18 stsp char *tmppackpath = NULL, *tmpidxpath = NULL;
377 afa77e03 2020-03-18 stsp char *packpath = NULL, *idxpath = NULL, *id_str = NULL;
378 bb64b798 2020-03-18 stsp const char *repo_path = got_repo_get_path(repo);
379 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
380 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
381 d2cdc636 2020-03-18 stsp off_t packfile_size = 0;
382 ee61b6d3 2020-03-18 stsp char *path;
383 abe0f35f 2020-03-18 stsp
384 d9b4d0c0 2020-03-18 stsp *pack_hash = NULL;
385 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++)
386 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
387 93658fb9 2020-03-18 stsp
388 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
389 33501562 2020-03-18 stsp
390 66cba96f 2020-03-18 stsp if (asprintf(&path, "%s/%s/fetching.pack",
391 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
392 ee61b6d3 2020-03-18 stsp err = got_error_from_errno("asprintf");
393 ee61b6d3 2020-03-18 stsp goto done;
394 8e278d17 2020-03-18 stsp }
395 ee61b6d3 2020-03-18 stsp err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
396 ee61b6d3 2020-03-18 stsp free(path);
397 fe4e1501 2020-03-18 stsp if (err)
398 8e278d17 2020-03-18 stsp goto done;
399 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
400 8e278d17 2020-03-18 stsp if (npackfd == -1) {
401 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
402 8e278d17 2020-03-18 stsp goto done;
403 8e278d17 2020-03-18 stsp }
404 66cba96f 2020-03-18 stsp if (asprintf(&path, "%s/%s/fetching.idx",
405 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
406 ee61b6d3 2020-03-18 stsp err = got_error_from_errno("asprintf");
407 ee61b6d3 2020-03-18 stsp goto done;
408 ee61b6d3 2020-03-18 stsp }
409 ee61b6d3 2020-03-18 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
410 ee61b6d3 2020-03-18 stsp free(path);
411 fe4e1501 2020-03-18 stsp if (err)
412 8e278d17 2020-03-18 stsp goto done;
413 93658fb9 2020-03-18 stsp nidxfd = dup(idxfd);
414 8e278d17 2020-03-18 stsp if (nidxfd == -1) {
415 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
416 8e278d17 2020-03-18 stsp goto done;
417 8e278d17 2020-03-18 stsp }
418 93658fb9 2020-03-18 stsp
419 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
420 d582f26c 2020-03-18 stsp tmpfds[i] = got_opentempfd();
421 d582f26c 2020-03-18 stsp if (tmpfds[i] == -1) {
422 d582f26c 2020-03-18 stsp err = got_error_from_errno("got_opentempfd");
423 d582f26c 2020-03-18 stsp goto done;
424 d582f26c 2020-03-18 stsp }
425 4788f1ce 2020-03-18 stsp }
426 4788f1ce 2020-03-18 stsp
427 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fetchfds) == -1) {
428 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
429 8e278d17 2020-03-18 stsp goto done;
430 8e278d17 2020-03-18 stsp }
431 93658fb9 2020-03-18 stsp
432 85e8591f 2020-03-18 stsp fetchpid = fork();
433 85e8591f 2020-03-18 stsp if (fetchpid == -1) {
434 8e278d17 2020-03-18 stsp err = got_error_from_errno("fork");
435 8e278d17 2020-03-18 stsp goto done;
436 85e8591f 2020-03-18 stsp } else if (fetchpid == 0){
437 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_fetchfds,
438 12491971 2020-03-18 stsp GOT_PATH_PROG_FETCH_PACK, tmppackpath);
439 93658fb9 2020-03-18 stsp }
440 93658fb9 2020-03-18 stsp
441 8e278d17 2020-03-18 stsp if (close(imsg_fetchfds[1]) != 0) {
442 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
443 8e278d17 2020-03-18 stsp goto done;
444 8e278d17 2020-03-18 stsp }
445 85e8591f 2020-03-18 stsp imsg_init(&fetchibuf, imsg_fetchfds[0]);
446 20eb36d0 2020-03-18 stsp nfetchfd = dup(fetchfd);
447 20eb36d0 2020-03-18 stsp if (nfetchfd == -1) {
448 20eb36d0 2020-03-18 stsp err = got_error_from_errno("dup");
449 20eb36d0 2020-03-18 stsp goto done;
450 20eb36d0 2020-03-18 stsp }
451 85e8591f 2020-03-18 stsp err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs);
452 93658fb9 2020-03-18 stsp if (err != NULL)
453 8e278d17 2020-03-18 stsp goto done;
454 20eb36d0 2020-03-18 stsp nfetchfd = -1;
455 f826addf 2020-03-18 stsp err = got_privsep_send_fetch_outfd(&fetchibuf, npackfd);
456 93658fb9 2020-03-18 stsp if (err != NULL)
457 8e278d17 2020-03-18 stsp goto done;
458 93658fb9 2020-03-18 stsp npackfd = dup(packfd);
459 8e278d17 2020-03-18 stsp if (npackfd == -1) {
460 8e278d17 2020-03-18 stsp err = got_error_from_errno("dup");
461 8e278d17 2020-03-18 stsp goto done;
462 8e278d17 2020-03-18 stsp }
463 8e278d17 2020-03-18 stsp
464 d2cdc636 2020-03-18 stsp packfile_size = 0;
465 8f2d01a6 2020-03-18 stsp while (!done) {
466 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = NULL;
467 d9b4d0c0 2020-03-18 stsp char *refname = NULL;
468 531c3985 2020-03-18 stsp char *server_progress = NULL;
469 d2cdc636 2020-03-18 stsp off_t packfile_size_cur;
470 8e278d17 2020-03-18 stsp
471 8f2d01a6 2020-03-18 stsp err = got_privsep_recv_fetch_progress(&done,
472 d2cdc636 2020-03-18 stsp &id, &refname, symrefs, &server_progress,
473 85e8591f 2020-03-18 stsp &packfile_size_cur, &fetchibuf);
474 8f2d01a6 2020-03-18 stsp if (err != NULL)
475 8e278d17 2020-03-18 stsp goto done;
476 d9b4d0c0 2020-03-18 stsp if (done)
477 d9b4d0c0 2020-03-18 stsp *pack_hash = id;
478 d9b4d0c0 2020-03-18 stsp else if (refname && id) {
479 d9b4d0c0 2020-03-18 stsp err = got_pathlist_append(refs, refname, id);
480 8f2d01a6 2020-03-18 stsp if (err)
481 8e278d17 2020-03-18 stsp goto done;
482 531c3985 2020-03-18 stsp } else if (server_progress) {
483 531c3985 2020-03-18 stsp char *s, *s0 = server_progress;
484 531c3985 2020-03-18 stsp while ((s = strsep(&s0, "\r")) != NULL) {
485 531c3985 2020-03-18 stsp if (*s == '\0')
486 531c3985 2020-03-18 stsp continue;
487 668a20f6 2020-03-18 stsp err = progress_cb(progress_arg, s,
488 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
489 531c3985 2020-03-18 stsp if (err)
490 531c3985 2020-03-18 stsp break;
491 531c3985 2020-03-18 stsp }
492 531c3985 2020-03-18 stsp free(server_progress);
493 531c3985 2020-03-18 stsp if (err)
494 531c3985 2020-03-18 stsp goto done;
495 d2cdc636 2020-03-18 stsp } else if (packfile_size_cur != packfile_size) {
496 d2cdc636 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
497 668a20f6 2020-03-18 stsp packfile_size_cur, 0, 0, 0, 0);
498 d2cdc636 2020-03-18 stsp if (err)
499 d2cdc636 2020-03-18 stsp break;
500 d2cdc636 2020-03-18 stsp packfile_size = packfile_size_cur;
501 8f2d01a6 2020-03-18 stsp }
502 8f2d01a6 2020-03-18 stsp }
503 85e8591f 2020-03-18 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1) {
504 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
505 8e278d17 2020-03-18 stsp goto done;
506 8e278d17 2020-03-18 stsp }
507 849f7557 2020-03-18 stsp
508 849f7557 2020-03-18 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
509 849f7557 2020-03-18 stsp err = got_error_from_errno("lseek");
510 849f7557 2020-03-18 stsp goto done;
511 849f7557 2020-03-18 stsp }
512 849f7557 2020-03-18 stsp err = check_pack_hash(packfd, packfile_size, (*pack_hash)->sha1);
513 849f7557 2020-03-18 stsp if (err)
514 849f7557 2020-03-18 stsp goto done;
515 531c3985 2020-03-18 stsp
516 8e278d17 2020-03-18 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
517 8e278d17 2020-03-18 stsp err = got_error_from_errno("socketpair");
518 8e278d17 2020-03-18 stsp goto done;
519 8e278d17 2020-03-18 stsp }
520 85e8591f 2020-03-18 stsp idxpid = fork();
521 85e8591f 2020-03-18 stsp if (idxpid == -1) {
522 8e278d17 2020-03-18 stsp err= got_error_from_errno("fork");
523 8e278d17 2020-03-18 stsp goto done;
524 85e8591f 2020-03-18 stsp } else if (idxpid == 0)
525 8e278d17 2020-03-18 stsp got_privsep_exec_child(imsg_idxfds,
526 12491971 2020-03-18 stsp GOT_PATH_PROG_INDEX_PACK, tmppackpath);
527 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[1]) != 0) {
528 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
529 8e278d17 2020-03-18 stsp goto done;
530 8e278d17 2020-03-18 stsp }
531 85e8591f 2020-03-18 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
532 93658fb9 2020-03-18 stsp
533 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
534 668a20f6 2020-03-18 stsp npackfd);
535 93658fb9 2020-03-18 stsp if (err != NULL)
536 8e278d17 2020-03-18 stsp goto done;
537 8e278d17 2020-03-18 stsp npackfd = -1;
538 73ab1060 2020-03-18 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
539 93658fb9 2020-03-18 stsp if (err != NULL)
540 8e278d17 2020-03-18 stsp goto done;
541 8e278d17 2020-03-18 stsp nidxfd = -1;
542 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
543 d582f26c 2020-03-18 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
544 d582f26c 2020-03-18 stsp if (err != NULL)
545 d582f26c 2020-03-18 stsp goto done;
546 d582f26c 2020-03-18 stsp tmpfds[i] = -1;
547 d582f26c 2020-03-18 stsp }
548 baa9fea0 2020-03-18 stsp done = 0;
549 baa9fea0 2020-03-18 stsp while (!done) {
550 668a20f6 2020-03-18 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
551 668a20f6 2020-03-18 stsp
552 668a20f6 2020-03-18 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
553 668a20f6 2020-03-18 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
554 668a20f6 2020-03-18 stsp &idxibuf);
555 baa9fea0 2020-03-18 stsp if (err != NULL)
556 baa9fea0 2020-03-18 stsp goto done;
557 668a20f6 2020-03-18 stsp if (nobj_indexed != 0) {
558 baa9fea0 2020-03-18 stsp err = progress_cb(progress_arg, NULL,
559 668a20f6 2020-03-18 stsp packfile_size, nobj_total,
560 668a20f6 2020-03-18 stsp nobj_indexed, nobj_loose, nobj_resolved);
561 baa9fea0 2020-03-18 stsp if (err)
562 baa9fea0 2020-03-18 stsp break;
563 baa9fea0 2020-03-18 stsp }
564 baa9fea0 2020-03-18 stsp imsg_clear(&idxibuf);
565 baa9fea0 2020-03-18 stsp }
566 8e278d17 2020-03-18 stsp if (close(imsg_idxfds[0]) == -1) {
567 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
568 8e278d17 2020-03-18 stsp goto done;
569 8e278d17 2020-03-18 stsp }
570 85e8591f 2020-03-18 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
571 8e278d17 2020-03-18 stsp err = got_error_from_errno("waitpid");
572 8e278d17 2020-03-18 stsp goto done;
573 8e278d17 2020-03-18 stsp }
574 93658fb9 2020-03-18 stsp
575 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, *pack_hash);
576 afa77e03 2020-03-18 stsp if (err)
577 8e278d17 2020-03-18 stsp goto done;
578 66cba96f 2020-03-18 stsp if (asprintf(&packpath, "%s/%s/pack-%s.pack",
579 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
580 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
581 8e278d17 2020-03-18 stsp goto done;
582 8e278d17 2020-03-18 stsp }
583 93658fb9 2020-03-18 stsp
584 66cba96f 2020-03-18 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
585 66cba96f 2020-03-18 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
586 8e278d17 2020-03-18 stsp err = got_error_from_errno("asprintf");
587 8e278d17 2020-03-18 stsp goto done;
588 8e278d17 2020-03-18 stsp }
589 afa77e03 2020-03-18 stsp
590 8e278d17 2020-03-18 stsp if (rename(tmppackpath, packpath) == -1) {
591 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmppackpath, packpath);
592 8e278d17 2020-03-18 stsp goto done;
593 8e278d17 2020-03-18 stsp }
594 8e278d17 2020-03-18 stsp if (rename(tmpidxpath, idxpath) == -1) {
595 8e278d17 2020-03-18 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
596 8e278d17 2020-03-18 stsp goto done;
597 8e278d17 2020-03-18 stsp }
598 ee61b6d3 2020-03-18 stsp
599 8e278d17 2020-03-18 stsp done:
600 20eb36d0 2020-03-18 stsp if (nfetchfd != -1 && close(nfetchfd) == -1 && err == NULL)
601 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
602 8e278d17 2020-03-18 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
603 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
604 8e278d17 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
605 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
606 8e278d17 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
607 8e278d17 2020-03-18 stsp err = got_error_from_errno("close");
608 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfds); i++) {
609 d582f26c 2020-03-18 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
610 d582f26c 2020-03-18 stsp err = got_error_from_errno("close");
611 d582f26c 2020-03-18 stsp }
612 afa77e03 2020-03-18 stsp free(tmppackpath);
613 afa77e03 2020-03-18 stsp free(tmpidxpath);
614 fe4e1501 2020-03-18 stsp free(idxpath);
615 afa77e03 2020-03-18 stsp free(packpath);
616 fe4e1501 2020-03-18 stsp
617 d9b4d0c0 2020-03-18 stsp if (err) {
618 d9b4d0c0 2020-03-18 stsp free(*pack_hash);
619 d9b4d0c0 2020-03-18 stsp *pack_hash = NULL;
620 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, refs, entry) {
621 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
622 d9b4d0c0 2020-03-18 stsp free(pe->data);
623 d9b4d0c0 2020-03-18 stsp }
624 d9b4d0c0 2020-03-18 stsp got_pathlist_free(refs);
625 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, symrefs, entry) {
626 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
627 d9b4d0c0 2020-03-18 stsp free(pe->data);
628 d9b4d0c0 2020-03-18 stsp }
629 d9b4d0c0 2020-03-18 stsp got_pathlist_free(symrefs);
630 d9b4d0c0 2020-03-18 stsp }
631 8e278d17 2020-03-18 stsp return err;
632 93658fb9 2020-03-18 stsp }