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"
40 #include "got_path.h"
41 #include "got_version.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 #define GOT_PKTMAX 65536
55 struct got_object *indexed;
56 static int chattygit;
57 static char *fetchbranch;
58 static char *upstream = "origin";
59 static struct got_object_id zhash = {.sha1={0}};
61 static char*
62 strip(char *ref)
63 {
64 return ref;
65 }
67 int
68 readn(int fd, void *buf, size_t n)
69 {
70 ssize_t r, off;
72 off = 0;
73 while (off != n) {
74 r = read(fd, buf + off, n - off);
75 if (r < 0)
76 return -1;
77 if (r == 0)
78 return off;
79 off += r;
80 }
81 return off;
82 }
84 int
85 flushpkt(int fd)
86 {
87 if(chattygit)
88 fprintf(stderr, "writepkt: 0000\n");
89 return write(fd, "0000", 4);
90 }
93 int
94 readpkt(int fd, char *buf, int nbuf)
95 {
96 char len[5];
97 char *e;
98 int n, r;
100 if(readn(fd, len, 4) == -1){
101 return -1;
103 len[4] = 0;
104 n = strtol(len, &e, 16);
105 if(n == 0){
106 if(chattygit)
107 fprintf(stderr, "readpkt: 0000\n");
108 return 0;
110 if(e != len + 4 || n <= 4)
111 err(1, "invalid packet line length");
112 n -= 4;
113 if(n >= nbuf)
114 err(1, "buffer too small");
115 if((r = readn(fd, buf, n)) != n)
116 return -1;
117 buf[n] = 0;
118 if(chattygit)
119 fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
120 return n;
123 int
124 writepkt(int fd, char *buf, int nbuf)
126 char len[5];
127 int i;
129 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
130 return -1;
131 if(write(fd, len, 4) != 4)
132 return -1;
133 if(write(fd, buf, nbuf) != nbuf)
134 return -1;
135 if(chattygit){
136 fprintf(stderr, "writepkt: %s:\t", len);
137 fwrite(buf, 1, nbuf, stderr);
138 for(i = 0; i < nbuf; i++){
139 if(isprint(buf[i]))
140 fputc(buf[i], stderr);
143 return 0;
147 int
148 got_resolve_remote_ref(struct got_object_id *id, char *ref)
150 char buf[128], *s;
151 int r, f;
153 ref = strip(ref);
154 if(!got_parse_sha1_digest(id->sha1, ref))
155 return 0;
157 /* Slightly special handling: translate remote refs to local ones. */
158 if (strcmp(ref, "HEAD") == 0) {
159 if(snprintf(buf, sizeof(buf), ".git/HEAD") >= sizeof(buf))
160 return -1;
161 } else if(strstr(ref, "refs/heads") == ref) {
162 ref += strlen("refs/heads");
163 if(snprintf(buf, sizeof(buf),
164 ".git/refs/remotes/%s/%s", upstream, ref) >= sizeof(buf))
165 return -1;
166 } else if(strstr(ref, "refs/tags") == ref) {
167 ref += strlen("refs/tags");
168 if(snprintf(buf, sizeof(buf),
169 ".git/refs/tags/%s/%s", upstream, ref) >= sizeof(buf))
170 return -1;
171 } else {
172 return -1;
175 r = -1;
176 s = strip(buf);
177 if((f = open(s, O_RDONLY)) == -1)
178 goto err;
179 if(readn(f, buf, sizeof(buf)) < 40)
180 goto err;
181 if(!got_parse_sha1_digest(id->sha1, buf))
182 goto err;
183 err:
184 close(f);
185 if(r == -1 && strstr(buf, "ref:") == buf)
186 return got_resolve_remote_ref(id, buf + strlen("ref:"));
187 return r;
190 static int
191 got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
193 SHA1_CTX ctx;
194 uint8_t hexpect[SHA1_DIGEST_LENGTH];
195 char s1[SHA1_DIGEST_STRING_LENGTH + 1];
196 char s2[SHA1_DIGEST_STRING_LENGTH + 1];
197 uint8_t buf[32*1024];
198 ssize_t n, r, nr;
200 if(sz < 28)
201 return -1;
203 n = 0;
204 SHA1Init(&ctx);
205 while(n < sz - 20){
206 nr = sizeof(buf);
207 if(sz - n - 20 < sizeof(buf))
208 nr = sz - n - 20;
209 r = readn(fd, buf, nr);
210 if(r != nr)
211 return -1;
212 SHA1Update(&ctx, buf, nr);
213 n += r;
215 SHA1Final(hcomp, &ctx);
217 if(readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
218 errx(1, "truncated packfile");
219 if(memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0){
220 got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
221 got_sha1_digest_to_str(hexpect, s2, sizeof(s2));
222 printf("hash mismatch %s != %s\n", s1, s2);
223 return -1;
225 return 0;
228 int
229 got_has_object(struct got_object_id *obj)
231 return 0;
234 /*static */int
235 got_make_pack_dir(char *path)
237 char s[128];
238 char *p;
240 if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
241 return -1;
242 for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
243 *p = 0;
244 if (mkdir(s, 0755) == -1)
245 if(errno != EEXIST)
246 return -1;
247 *p = '/';
249 return 0;
252 static int
253 got_match_branch(char *br, char *pat)
255 char name[128];
257 if(strstr(pat, "refs/heads") == pat) {
258 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
259 return -1;
260 } else if(strstr(pat, "heads")) {
261 if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
262 return -1;
263 } else {
264 if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
265 return -1;
267 return strcmp(br, name) == 0;
270 static const struct got_error *
271 got_tokenize_refline(char **tokens, char *line, int len)
273 const struct got_error *err = NULL;
274 char *p;
275 size_t i, n = 0;
276 const int maxtokens = 3;
278 for (i = 0; i < maxtokens; i++)
279 tokens[i] = NULL;
281 for (i = 0; n < len && i < maxtokens; i++) {
282 while (isspace(*line)) {
283 line++;
284 n++;
286 p = line;
287 while (*line != '\0' &&
288 (!isspace(*line) || i == maxtokens - 1)) {
289 line++;
290 n++;
292 tokens[i] = strndup(p, line - p);
293 if (tokens[i] == NULL) {
294 err = got_error_from_errno("strndup");
295 goto done;
297 /* Skip \0 field-delimiter at end of token. */
298 while (line[0] == '\0' && n < len) {
299 line++;
300 n++;
303 if (i <= 2)
304 err = got_error(GOT_ERR_NOT_REF);
305 done:
306 if (err) {
307 int j;
308 for (j = 0; j < i; j++)
309 free(tokens[j]);
310 tokens[j] = NULL;
312 return err;
315 struct got_capability {
316 const char *key;
317 const char *value;
318 };
319 static const struct got_capability got_capabilities[] = {
320 #if 0 /* got-index-pack is not ready for this */
321 { "ofs-delta", NULL },
322 #endif
323 { "agent", "got/" GOT_VERSION_STR },
324 };
326 static const struct got_error *
327 match_capability(char **my_capabilities, const char *capa,
328 const struct got_capability *mycapa)
330 char *equalsign;
331 char *s;
333 equalsign = strchr(capa, '=');
334 if (equalsign) {
335 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
336 return NULL;
337 } else {
338 if (strcmp(capa, mycapa->key) != 0)
339 return NULL;
342 if (asprintf(&s, "%s%s%s%s%s",
343 *my_capabilities != NULL ? *my_capabilities : "",
344 *my_capabilities != NULL ? " " : "",
345 mycapa->key,
346 mycapa->value != NULL ? "=" : "",
347 mycapa->value != NULL? mycapa->value : "") == -1)
348 return got_error_from_errno("asprintf");
350 free(*my_capabilities);
351 *my_capabilities = s;
352 return NULL;
355 static const struct got_error *
356 add_symref(struct got_pathlist_head *symrefs, const char *capa)
358 const struct got_error *err = NULL;
359 char *colon, *name = NULL, *target = NULL;
361 /* Need at least "A:B" */
362 if (strlen(capa) < 3)
363 return NULL;
365 colon = strchr(capa, ':');
366 if (colon == NULL)
367 return NULL;
369 *colon = '\0';
370 name = strdup(capa);
371 if (name == NULL)
372 return got_error_from_errno("strdup");
374 target = strdup(colon + 1);
375 if (target == NULL) {
376 err = got_error_from_errno("strdup");
377 goto done;
380 /* We can't validate the ref itself here. The main process will. */
381 err = got_pathlist_append(symrefs, name, target);
382 done:
383 if (err) {
384 free(name);
385 free(target);
387 return err;
390 static const struct got_error *
391 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
392 char *server_capabilities)
394 const struct got_error *err = NULL;
395 char *capa, *equalsign;
396 int i;
398 *my_capabilities = NULL;
399 do {
400 capa = strsep(&server_capabilities, " ");
401 if (capa == NULL)
402 return NULL;
404 equalsign = strchr(capa, '=');
405 if (equalsign != NULL &&
406 strncmp(capa, "symref", equalsign - capa) == 0) {
407 err = add_symref(symrefs, equalsign + 1);
408 if (err)
409 break;
410 continue;
413 for (i = 0; i < nitems(got_capabilities); i++) {
414 err = match_capability(my_capabilities,
415 capa, &got_capabilities[i]);
416 if (err)
417 break;
419 } while (capa);
421 return err;
424 static const struct got_error *
425 fetch_pack(int fd, int packfd, struct got_object_id *packid,
426 struct imsgbuf *ibuf)
428 const struct got_error *err = NULL;
429 char buf[GOT_PKTMAX], *sp[3];
430 char hashstr[SHA1_DIGEST_STRING_LENGTH];
431 struct got_object_id *have, *want;
432 int is_firstpkt = 1, nref = 0, refsz = 16;
433 int i, n, req;
434 off_t packsz;
435 char *my_capabilities = NULL;
436 struct got_pathlist_head symrefs;
437 struct got_pathlist_entry *pe;
439 TAILQ_INIT(&symrefs);
441 have = malloc(refsz * sizeof(have[0]));
442 if (have == NULL)
443 return got_error_from_errno("malloc");
444 want = malloc(refsz * sizeof(want[0]));
445 if (want == NULL) {
446 err = got_error_from_errno("malloc");
447 goto done;
449 if (chattygit)
450 fprintf(stderr, "starting fetch\n");
451 while (1) {
452 n = readpkt(fd, buf, sizeof(buf));
453 if (n == -1){
454 err = got_error_from_errno("readpkt");
455 goto done;
457 if (n == 0)
458 break;
459 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
460 static char msg[1024];
461 for (i = 0; i < n && i < sizeof(msg) - 1; i++) {
462 if (!isprint(buf[i])) {
463 err = got_error(GOT_ERR_FETCH_FAILED);
464 goto done;
466 msg[i] = buf[i];
468 msg[i] = '\0';
469 err = got_error_msg(GOT_ERR_FETCH_FAILED, msg);
470 goto done;
472 err = got_tokenize_refline(sp, buf, n);
473 if (err)
474 goto done;
475 if (chattygit && sp[2][0] != '\0')
476 fprintf(stderr, "server capabilities: %s\n", sp[2]);
477 if (is_firstpkt) {
478 err = match_capabilities(&my_capabilities, &symrefs,
479 sp[2]);
480 if (err)
481 goto done;
482 if (chattygit && my_capabilities)
483 fprintf(stderr, "my matched capabilities: %s\n",
484 my_capabilities);
485 err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
486 if (err)
487 goto done;
489 is_firstpkt = 0;
490 if (strstr(sp[1], "^{}"))
491 continue;
492 if (fetchbranch && !got_match_branch(sp[1], fetchbranch))
493 continue;
494 if (refsz == nref + 1){
495 refsz *= 2;
496 have = realloc(have, refsz * sizeof(have[0]));
497 if (have == NULL) {
498 err = got_error_from_errno("realloc");
499 goto done;
501 want = realloc(want, refsz * sizeof(want[0]));
502 if (want == NULL) {
503 err = got_error_from_errno("realloc");
504 goto done;
507 if (!got_parse_sha1_digest(want[nref].sha1, sp[0])) {
508 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
509 goto done;
512 if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
513 memset(&have[nref], 0, sizeof(have[nref]));
514 err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
515 if (err)
516 goto done;
517 if (chattygit)
518 fprintf(stderr, "remote %s\n", sp[1]);
519 nref++;
522 req = 0;
523 for (i = 0; i < nref; i++){
524 if (got_object_id_cmp(&have[i], &want[i]) == 0)
525 continue;
526 if (got_has_object(&want[i]))
527 continue;
528 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
529 n = snprintf(buf, sizeof(buf), "want %s%s%s\n", hashstr,
530 i == 0 && my_capabilities ? " " : "",
531 i == 0 && my_capabilities ? my_capabilities : "");
532 if (n >= sizeof(buf)) {
533 err = got_error(GOT_ERR_NO_SPACE);
534 goto done;
536 if (writepkt(fd, buf, n) == -1) {
537 err = got_error_from_errno("writepkt");
538 goto done;
540 req = 1;
542 flushpkt(fd);
543 for (i = 0; i < nref; i++){
544 if (got_object_id_cmp(&have[i], &zhash) == 0)
545 continue;
546 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
547 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
548 if (n >= sizeof(buf)) {
549 err = got_error(GOT_ERR_NO_SPACE);
550 goto done;
552 if (writepkt(fd, buf, n + 1) == -1) {
553 err = got_error_from_errno("writepkt");
554 goto done;
557 if (!req){
558 fprintf(stderr, "up to date\n");
559 flushpkt(fd);
561 n = snprintf(buf, sizeof(buf), "done\n");
562 if (writepkt(fd, buf, n) == -1) {
563 err = got_error_from_errno("writepkt");
564 goto done;
566 if (!req)
567 return 0;
569 if ((n = readpkt(fd, buf, sizeof(buf))) == -1) {
570 err = got_error_from_errno("readpkt");
571 goto done;
573 buf[n] = 0;
575 if (chattygit)
576 fprintf(stderr, "fetching...\n");
577 packsz = 0;
578 while (1) {
579 ssize_t w;
580 n = readn(fd, buf, sizeof buf);
581 if (n == 0)
582 break;
583 if (n == -1) {
584 err = got_error_from_errno("readn");
585 goto done;
587 w = write(packfd, buf, n);
588 if (w == -1) {
589 err = got_error_from_errno("write");
590 goto done;
592 if (w != n) {
593 err = got_error(GOT_ERR_IO);
594 goto done;
596 packsz += n;
598 if (lseek(packfd, 0, SEEK_SET) == -1) {
599 err = got_error_from_errno("lseek");
600 goto done;
602 if (got_check_pack_hash(packfd, packsz, packid->sha1) == -1)
603 err = got_error(GOT_ERR_BAD_PACKFILE);
604 done:
605 TAILQ_FOREACH(pe, &symrefs, entry) {
606 free((void *)pe->path);
607 free(pe->data);
609 got_pathlist_free(&symrefs);
610 free(have);
611 free(want);
612 return err;
616 int
617 main(int argc, char **argv)
619 const struct got_error *err = NULL;
620 int fetchfd, packfd = -1;
621 struct got_object_id packid;
622 struct imsgbuf ibuf;
623 struct imsg imsg;
625 if(getenv("GOT_DEBUG") != NULL){
626 fprintf(stderr, "fetch-pack being chatty!\n");
627 chattygit = 1;
630 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
631 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
632 if (err->code == GOT_ERR_PRIVSEP_PIPE)
633 err = NULL;
634 goto done;
636 if (imsg.hdr.type == GOT_IMSG_STOP)
637 goto done;
638 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
639 err = got_error(GOT_ERR_PRIVSEP_MSG);
640 goto done;
642 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
643 err = got_error(GOT_ERR_PRIVSEP_LEN);
644 goto done;
646 fetchfd = imsg.fd;
648 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
649 if (err->code == GOT_ERR_PRIVSEP_PIPE)
650 err = NULL;
651 goto done;
653 if (imsg.hdr.type == GOT_IMSG_STOP)
654 goto done;
655 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
656 err = got_error(GOT_ERR_PRIVSEP_MSG);
657 goto done;
659 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
660 err = got_error(GOT_ERR_PRIVSEP_LEN);
661 goto done;
663 packfd = imsg.fd;
665 err = fetch_pack(fetchfd, packfd, &packid, &ibuf);
666 if (err)
667 goto done;
668 done:
669 if (packfd != -1 && close(packfd) == -1 && err == NULL)
670 err = got_error_from_errno("close");
671 if (err != NULL)
672 got_privsep_send_error(&ibuf, err);
673 else
674 err = got_privsep_send_fetch_done(&ibuf, packid);
675 if(err != NULL) {
676 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
677 got_privsep_send_error(&ibuf, err);
680 exit(0);