Blob


1 /*
2 * Copyright (c) 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/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <sys/syslimits.h>
24 #include <stdint.h>
25 #include <errno.h>
26 #include <imsg.h>
27 #include <limits.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sha1.h>
34 #include <fcntl.h>
35 #include <zlib.h>
36 #include <err.h>
38 #include "got_error.h"
39 #include "got_object.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_privsep.h"
48 #define GOT_PKTMAX 65536
50 struct got_object *indexed;
51 static int chattygit;
52 static char *fetchbranch;
53 static char *upstream = "origin";
54 static struct got_object_id zhash = {.sha1={0}};
56 static char*
57 strip(char *ref)
58 {
59 return ref;
60 }
62 int
63 readn(int fd, void *buf, size_t n)
64 {
65 ssize_t r, off;
67 off = 0;
68 while (off != n) {
69 r = read(fd, buf + off, n - off);
70 if (r < 0)
71 return -1;
72 if (r == 0)
73 return off;
74 off += r;
75 }
76 return off;
77 }
79 int
80 flushpkt(int fd)
81 {
82 if(chattygit)
83 fprintf(stderr, "writepkt: 0000\n");
84 return write(fd, "0000", 4);
85 }
88 int
89 readpkt(int fd, char *buf, int nbuf)
90 {
91 char len[5];
92 char *e;
93 int n, r;
95 if(readn(fd, len, 4) == -1){
96 return -1;
97 }
98 len[4] = 0;
99 n = strtol(len, &e, 16);
100 if(n == 0){
101 if(chattygit)
102 fprintf(stderr, "readpkt: 0000\n");
103 return 0;
105 if(e != len + 4 || n <= 4)
106 err(1, "invalid packet line length");
107 n -= 4;
108 if(n >= nbuf)
109 err(1, "buffer too small");
110 if((r = readn(fd, buf, n)) != n)
111 return -1;
112 buf[n] = 0;
113 if(chattygit)
114 fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
115 return n;
118 int
119 writepkt(int fd, char *buf, int nbuf)
121 char len[5];
122 int i;
124 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
125 return -1;
126 if(write(fd, len, 4) != 4)
127 return -1;
128 if(write(fd, buf, nbuf) != nbuf)
129 return -1;
130 if(chattygit){
131 fprintf(stderr, "writepkt: %s:\t", len);
132 fwrite(buf, 1, nbuf, stderr);
133 for(i = 0; i < nbuf; i++){
134 if(isprint(buf[i]))
135 fputc(buf[i], stderr);
138 return 0;
142 int
143 got_resolve_remote_ref(struct got_object_id *id, char *ref)
145 char buf[128], *s;
146 int r, f;
148 ref = strip(ref);
149 if(!got_parse_sha1_digest(id->sha1, ref))
150 return 0;
152 /* Slightly special handling: translate remote refs to local ones. */
153 if (strcmp(ref, "HEAD") == 0) {
154 if(snprintf(buf, sizeof(buf), ".git/HEAD") >= sizeof(buf))
155 return -1;
156 } else if(strstr(ref, "refs/heads") == ref) {
157 ref += strlen("refs/heads");
158 if(snprintf(buf, sizeof(buf),
159 ".git/refs/remotes/%s/%s", upstream, ref) >= sizeof(buf))
160 return -1;
161 } else if(strstr(ref, "refs/tags") == ref) {
162 ref += strlen("refs/tags");
163 if(snprintf(buf, sizeof(buf),
164 ".git/refs/tags/%s/%s", upstream, ref) >= sizeof(buf))
165 return -1;
166 } else {
167 return -1;
170 r = -1;
171 s = strip(buf);
172 if((f = open(s, O_RDONLY)) == -1)
173 goto err;
174 if(readn(f, buf, sizeof(buf)) < 40)
175 goto err;
176 if(!got_parse_sha1_digest(id->sha1, buf))
177 goto err;
178 err:
179 close(f);
180 if(r == -1 && strstr(buf, "ref:") == buf)
181 return got_resolve_remote_ref(id, buf + strlen("ref:"));
182 return r;
185 static int
186 got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
188 SHA1_CTX ctx;
189 uint8_t hexpect[SHA1_DIGEST_LENGTH];
190 char s1[SHA1_DIGEST_STRING_LENGTH + 1];
191 char s2[SHA1_DIGEST_STRING_LENGTH + 1];
192 uint8_t buf[32*1024];
193 ssize_t n, r, nr;
195 if(sz < 28)
196 return -1;
198 n = 0;
199 SHA1Init(&ctx);
200 while(n < sz - 20){
201 nr = sizeof(buf);
202 if(sz - n - 20 < sizeof(buf))
203 nr = sz - n - 20;
204 r = readn(fd, buf, nr);
205 if(r != nr)
206 return -1;
207 SHA1Update(&ctx, buf, nr);
208 n += r;
210 SHA1Final(hcomp, &ctx);
212 if(readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
213 errx(1, "truncated packfile");
214 if(memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0){
215 got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
216 got_sha1_digest_to_str(hexpect, s2, sizeof(s2));
217 printf("hash mismatch %s != %s\n", s1, s2);
218 return -1;
220 return 0;
223 int
224 got_has_object(struct got_object_id *obj)
226 return 0;
229 /*static */int
230 got_make_pack_dir(char *path)
232 char s[128];
233 char *p;
235 if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
236 return -1;
237 for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
238 *p = 0;
239 if (mkdir(s, 0755) == -1)
240 if(errno != EEXIST)
241 return -1;
242 *p = '/';
244 return 0;
247 static int
248 got_match_branch(char *br, char *pat)
250 char name[128];
252 if(strstr(pat, "refs/heads") == pat) {
253 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
254 return -1;
255 } else if(strstr(pat, "heads")) {
256 if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
257 return -1;
258 } else {
259 if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
260 return -1;
262 return strcmp(br, name) == 0;
265 static int
266 got_tokenize_refline(char *line, char **sp, size_t nsp)
268 char *p;
269 size_t i, j;
271 for (i = 0; i < nsp; i++) {
272 while (isspace(*line))
273 line++;
274 p = line;
275 while (*line != '\0' && (!isspace(*line) || i == nsp - 1))
276 line++;
277 sp[i] = strndup(p, line - p);
279 for (j = i; j < nsp; j++)
280 sp[j] = NULL;
281 return i;
284 static const struct got_error *
285 fetch_pack(int fd, int packfd, struct got_object_id *packid,
286 struct imsgbuf *ibuf)
288 const struct got_error *err = NULL;
289 char buf[GOT_PKTMAX], *sp[3];
290 char hashstr[SHA1_DIGEST_STRING_LENGTH];
291 struct got_object_id *have, *want;
292 int nref, refsz;
293 int i, n, req;
294 off_t packsz;
296 nref = 0;
297 refsz = 16;
298 have = malloc(refsz * sizeof(have[0]));
299 if (have == NULL)
300 return got_error_from_errno("malloc");
301 want = malloc(refsz * sizeof(want[0]));
302 if (want == NULL) {
303 err = got_error_from_errno("malloc");
304 goto done;
306 if (chattygit)
307 fprintf(stderr, "starting fetch\n");
308 while (1) {
309 n = readpkt(fd, buf, sizeof(buf));
310 if (n == -1){
311 err = got_error_from_errno("readpkt:");
312 goto done;
314 if (n == 0)
315 break;
316 if (strncmp(buf, "ERR ", 4) == 0) {
317 static char msg[1024];
318 strlcpy(msg, buf + 4, sizeof(msg));
319 err = got_error_msg(GOT_ERR_FETCH_FAILED, msg);
320 goto done;
322 if (got_tokenize_refline(buf, sp, 3) <= 2) {
323 err = got_error(GOT_ERR_NOT_REF);
324 goto done;
326 if (strstr(sp[1], "^{}"))
327 continue;
328 if (fetchbranch && !got_match_branch(sp[1], fetchbranch))
329 continue;
330 if (refsz == nref + 1){
331 refsz *= 2;
332 have = realloc(have, refsz * sizeof(have[0]));
333 if (have == NULL) {
334 err = got_error_from_errno("realloc");
335 goto done;
337 want = realloc(want, refsz * sizeof(want[0]));
338 if (want == NULL) {
339 err = got_error_from_errno("realloc");
340 goto done;
343 if (!got_parse_sha1_digest(want[nref].sha1, sp[0])) {
344 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
345 goto done;
348 if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
349 memset(&have[nref], 0, sizeof(have[nref]));
350 err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
351 if (err)
352 goto done;
353 if (chattygit)
354 fprintf(stderr, "remote %s\n", sp[1]);
355 nref++;
358 req = 0;
359 for (i = 0; i < nref; i++){
360 if (got_object_id_cmp(&have[i], &want[i]) == 0)
361 continue;
362 if (got_has_object(&want[i]))
363 continue;
364 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
365 n = snprintf(buf, sizeof(buf), "want %s\n", hashstr);
366 if (n >= sizeof(buf)) {
367 err = got_error(GOT_ERR_NO_SPACE);
368 goto done;
370 if (writepkt(fd, buf, n) == -1) {
371 err = got_error_from_errno("writepkt");
372 goto done;
374 req = 1;
376 flushpkt(fd);
377 for (i = 0; i < nref; i++){
378 if (got_object_id_cmp(&have[i], &zhash) == 0)
379 continue;
380 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
381 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
382 if (n >= sizeof(buf)) {
383 err = got_error(GOT_ERR_NO_SPACE);
384 goto done;
386 if (writepkt(fd, buf, n + 1) == -1) {
387 err = got_error_from_errno("writepkt");
388 goto done;
391 if (!req){
392 fprintf(stderr, "up to date\n");
393 flushpkt(fd);
395 n = snprintf(buf, sizeof(buf), "done\n");
396 if (writepkt(fd, buf, n) == -1) {
397 err = got_error_from_errno("writepkt");
398 goto done;
400 if (!req)
401 return 0;
403 if ((n = readpkt(fd, buf, sizeof(buf))) == -1) {
404 err = got_error_from_errno("readpkt");
405 goto done;
407 buf[n] = 0;
409 if (chattygit)
410 fprintf(stderr, "fetching...\n");
411 packsz = 0;
412 while (1) {
413 ssize_t w;
414 n = readn(fd, buf, sizeof buf);
415 if (n == 0)
416 break;
417 if (n == -1) {
418 err = got_error_from_errno("readn");
419 goto done;
421 w = write(packfd, buf, n);
422 if (w == -1) {
423 err = got_error_from_errno("write");
424 goto done;
426 if (w != n) {
427 err = got_error(GOT_ERR_IO);
428 goto done;
430 packsz += n;
432 if (lseek(packfd, 0, SEEK_SET) == -1) {
433 err = got_error_from_errno("lseek");
434 goto done;
436 if (got_check_pack_hash(packfd, packsz, packid->sha1) == -1)
437 err = got_error(GOT_ERR_BAD_PACKFILE);
438 done:
439 free(have);
440 free(want);
441 return err;
445 int
446 main(int argc, char **argv)
448 const struct got_error *err = NULL;
449 int fetchfd, packfd = -1;
450 struct got_object_id packid;
451 struct imsgbuf ibuf;
452 struct imsg imsg;
454 if(getenv("GOT_DEBUG") != NULL){
455 fprintf(stderr, "fetch-pack being chatty!\n");
456 chattygit = 1;
459 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
460 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
461 if (err->code == GOT_ERR_PRIVSEP_PIPE)
462 err = NULL;
463 goto done;
465 if (imsg.hdr.type == GOT_IMSG_STOP)
466 goto done;
467 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
468 err = got_error(GOT_ERR_PRIVSEP_MSG);
469 goto done;
471 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
472 err = got_error(GOT_ERR_PRIVSEP_LEN);
473 goto done;
475 fetchfd = imsg.fd;
477 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
478 if (err->code == GOT_ERR_PRIVSEP_PIPE)
479 err = NULL;
480 goto done;
482 if (imsg.hdr.type == GOT_IMSG_STOP)
483 goto done;
484 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
485 err = got_error(GOT_ERR_PRIVSEP_MSG);
486 goto done;
488 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
489 err = got_error(GOT_ERR_PRIVSEP_LEN);
490 goto done;
492 packfd = imsg.fd;
494 err = fetch_pack(fetchfd, packfd, &packid, &ibuf);
495 if (err)
496 goto done;
497 done:
498 if (packfd != -1 && close(packfd) == -1 && err == NULL)
499 err = got_error_from_errno("close");
500 if (err != NULL)
501 got_privsep_send_error(&ibuf, err);
502 else
503 err = got_privsep_send_fetch_done(&ibuf, packid);
504 if(err != NULL) {
505 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
506 got_privsep_send_error(&ibuf, err);
509 exit(0);