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_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 #define GOT_PKTMAX 65536
56 struct got_object *indexed;
57 static int chattygit;
58 static char *fetchbranch;
59 static char *upstream = "origin";
60 static struct got_object_id zhash = {.sha1={0}};
62 static char*
63 strip(char *ref)
64 {
65 return ref;
66 }
68 int
69 readn(int fd, void *buf, size_t n)
70 {
71 ssize_t r, off;
73 off = 0;
74 while (off != n) {
75 r = read(fd, buf + off, n - off);
76 if (r < 0)
77 return -1;
78 if (r == 0)
79 return off;
80 off += r;
81 }
82 return off;
83 }
85 int
86 flushpkt(int fd)
87 {
88 if(chattygit)
89 fprintf(stderr, "writepkt: 0000\n");
90 return write(fd, "0000", 4);
91 }
94 int
95 readpkt(int fd, char *buf, int nbuf)
96 {
97 char len[5];
98 char *e;
99 int n, r;
101 if(readn(fd, len, 4) == -1){
102 return -1;
104 len[4] = 0;
105 n = strtol(len, &e, 16);
106 if(n == 0){
107 if(chattygit)
108 fprintf(stderr, "readpkt: 0000\n");
109 return 0;
111 if(e != len + 4 || n <= 4)
112 err(1, "invalid packet line length");
113 n -= 4;
114 if(n >= nbuf)
115 err(1, "buffer too small");
116 if((r = readn(fd, buf, n)) != n)
117 return -1;
118 buf[n] = 0;
119 if(chattygit)
120 fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
121 return n;
124 int
125 writepkt(int fd, char *buf, int nbuf)
127 char len[5];
128 int i;
130 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
131 return -1;
132 if(write(fd, len, 4) != 4)
133 return -1;
134 if(write(fd, buf, nbuf) != nbuf)
135 return -1;
136 if(chattygit){
137 fprintf(stderr, "writepkt: %s:\t", len);
138 fwrite(buf, 1, nbuf, stderr);
139 for(i = 0; i < nbuf; i++){
140 if(isprint(buf[i]))
141 fputc(buf[i], stderr);
144 return 0;
148 int
149 got_resolve_remote_ref(struct got_object_id *id, char *ref)
151 char buf[128], *s;
152 int r, f;
154 ref = strip(ref);
155 if(!got_parse_sha1_digest(id->sha1, ref))
156 return 0;
158 /* Slightly special handling: translate remote refs to local ones. */
159 if (strcmp(ref, "HEAD") == 0) {
160 if(snprintf(buf, sizeof(buf), ".git/HEAD") >= sizeof(buf))
161 return -1;
162 } else if(strstr(ref, "refs/heads") == ref) {
163 ref += strlen("refs/heads");
164 if(snprintf(buf, sizeof(buf),
165 ".git/refs/remotes/%s/%s", upstream, ref) >= sizeof(buf))
166 return -1;
167 } else if(strstr(ref, "refs/tags") == ref) {
168 ref += strlen("refs/tags");
169 if(snprintf(buf, sizeof(buf),
170 ".git/refs/tags/%s/%s", upstream, ref) >= sizeof(buf))
171 return -1;
172 } else {
173 return -1;
176 r = -1;
177 s = strip(buf);
178 if((f = open(s, O_RDONLY)) == -1)
179 goto err;
180 if(readn(f, buf, sizeof(buf)) < 40)
181 goto err;
182 if(!got_parse_sha1_digest(id->sha1, buf))
183 goto err;
184 err:
185 close(f);
186 if(r == -1 && strstr(buf, "ref:") == buf)
187 return got_resolve_remote_ref(id, buf + strlen("ref:"));
188 return r;
191 static int
192 got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
194 SHA1_CTX ctx;
195 uint8_t hexpect[SHA1_DIGEST_LENGTH];
196 char s1[SHA1_DIGEST_STRING_LENGTH + 1];
197 char s2[SHA1_DIGEST_STRING_LENGTH + 1];
198 uint8_t buf[32*1024];
199 ssize_t n, r, nr;
201 if(sz < 28)
202 return -1;
204 n = 0;
205 SHA1Init(&ctx);
206 while(n < sz - 20){
207 nr = sizeof(buf);
208 if(sz - n - 20 < sizeof(buf))
209 nr = sz - n - 20;
210 r = readn(fd, buf, nr);
211 if(r != nr)
212 return -1;
213 SHA1Update(&ctx, buf, nr);
214 n += r;
216 SHA1Final(hcomp, &ctx);
218 if(readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
219 errx(1, "truncated packfile");
220 if(memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0){
221 got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
222 got_sha1_digest_to_str(hexpect, s2, sizeof(s2));
223 printf("hash mismatch %s != %s\n", s1, s2);
224 return -1;
226 return 0;
229 int
230 got_has_object(struct got_object_id *obj)
232 return 0;
235 /*static */int
236 got_make_pack_dir(char *path)
238 char s[128];
239 char *p;
241 if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
242 return -1;
243 for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
244 *p = 0;
245 if (mkdir(s, 0755) == -1)
246 if(errno != EEXIST)
247 return -1;
248 *p = '/';
250 return 0;
253 static int
254 got_match_branch(char *br, char *pat)
256 char name[128];
258 if(strstr(pat, "refs/heads") == pat) {
259 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
260 return -1;
261 } else if(strstr(pat, "heads")) {
262 if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
263 return -1;
264 } else {
265 if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
266 return -1;
268 return strcmp(br, name) == 0;
271 static const struct got_error *
272 got_tokenize_refline(char **tokens, char *line, int len)
274 const struct got_error *err = NULL;
275 char *p;
276 size_t i, n = 0;
277 const int maxtokens = 3;
279 for (i = 0; i < maxtokens; i++)
280 tokens[i] = NULL;
282 for (i = 0; n < len && i < maxtokens; i++) {
283 while (isspace(*line)) {
284 line++;
285 n++;
287 p = line;
288 while (*line != '\0' &&
289 (!isspace(*line) || i == maxtokens - 1)) {
290 line++;
291 n++;
293 tokens[i] = strndup(p, line - p);
294 if (tokens[i] == NULL) {
295 err = got_error_from_errno("strndup");
296 goto done;
298 /* Skip \0 field-delimiter at end of token. */
299 while (line[0] == '\0' && n < len) {
300 line++;
301 n++;
304 if (i <= 2)
305 err = got_error(GOT_ERR_NOT_REF);
306 done:
307 if (err) {
308 int j;
309 for (j = 0; j < i; j++)
310 free(tokens[j]);
311 tokens[j] = NULL;
313 return err;
316 struct got_capability {
317 const char *key;
318 const char *value;
319 };
320 static const struct got_capability got_capabilities[] = {
321 #if 0 /* got-index-pack is not ready for this */
322 { "ofs-delta", NULL },
323 #endif
324 { "agent", "got/" GOT_VERSION_STR },
325 };
327 static const struct got_error *
328 match_capability(char **my_capabilities, const char *capa,
329 const struct got_capability *mycapa)
331 char *equalsign;
332 char *s;
334 equalsign = strchr(capa, '=');
335 if (equalsign) {
336 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
337 return NULL;
338 } else {
339 if (strcmp(capa, mycapa->key) != 0)
340 return NULL;
343 if (asprintf(&s, "%s%s%s%s%s",
344 *my_capabilities != NULL ? *my_capabilities : "",
345 *my_capabilities != NULL ? " " : "",
346 mycapa->key,
347 mycapa->value != NULL ? "=" : "",
348 mycapa->value != NULL? mycapa->value : "") == -1)
349 return got_error_from_errno("asprintf");
351 free(*my_capabilities);
352 *my_capabilities = s;
353 return NULL;
356 static const struct got_error *
357 add_symref(struct got_pathlist_head *symrefs, const char *capa)
359 const struct got_error *err = NULL;
360 char *colon, *name = NULL, *target = NULL;
362 /* Need at least "A:B" */
363 if (strlen(capa) < 3)
364 return NULL;
366 colon = strchr(capa, ':');
367 if (colon == NULL)
368 return NULL;
370 *colon = '\0';
371 name = strdup(capa);
372 if (name == NULL)
373 return got_error_from_errno("strdup");
375 target = strdup(colon + 1);
376 if (target == NULL) {
377 err = got_error_from_errno("strdup");
378 goto done;
381 /* We can't validate the ref itself here. The main process will. */
382 err = got_pathlist_append(symrefs, name, target);
383 done:
384 if (err) {
385 free(name);
386 free(target);
388 return err;
391 static const struct got_error *
392 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
393 char *server_capabilities)
395 const struct got_error *err = NULL;
396 char *capa, *equalsign;
397 int i;
399 *my_capabilities = NULL;
400 do {
401 capa = strsep(&server_capabilities, " ");
402 if (capa == NULL)
403 return NULL;
405 equalsign = strchr(capa, '=');
406 if (equalsign != NULL &&
407 strncmp(capa, "symref", equalsign - capa) == 0) {
408 err = add_symref(symrefs, equalsign + 1);
409 if (err)
410 break;
411 continue;
414 for (i = 0; i < nitems(got_capabilities); i++) {
415 err = match_capability(my_capabilities,
416 capa, &got_capabilities[i]);
417 if (err)
418 break;
420 } while (capa);
422 return err;
425 static const struct got_error *
426 fetch_pack(int fd, int packfd, struct got_object_id *packid,
427 struct imsgbuf *ibuf)
429 const struct got_error *err = NULL;
430 char buf[GOT_PKTMAX], *sp[3];
431 char hashstr[SHA1_DIGEST_STRING_LENGTH];
432 struct got_object_id *have, *want;
433 int is_firstpkt = 1, nref = 0, refsz = 16;
434 int i, n, req;
435 off_t packsz;
436 char *my_capabilities = NULL;
437 struct got_pathlist_head symrefs;
438 struct got_pathlist_entry *pe;
440 TAILQ_INIT(&symrefs);
442 have = malloc(refsz * sizeof(have[0]));
443 if (have == NULL)
444 return got_error_from_errno("malloc");
445 want = malloc(refsz * sizeof(want[0]));
446 if (want == NULL) {
447 err = got_error_from_errno("malloc");
448 goto done;
450 if (chattygit)
451 fprintf(stderr, "starting fetch\n");
452 while (1) {
453 n = readpkt(fd, buf, sizeof(buf));
454 if (n == -1){
455 err = got_error_from_errno("readpkt");
456 goto done;
458 if (n == 0)
459 break;
460 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
461 static char msg[1024];
462 for (i = 0; i < n && i < sizeof(msg) - 1; i++) {
463 if (!isprint(buf[i])) {
464 err = got_error(GOT_ERR_FETCH_FAILED);
465 goto done;
467 msg[i] = buf[i];
469 msg[i] = '\0';
470 err = got_error_msg(GOT_ERR_FETCH_FAILED, msg);
471 goto done;
473 err = got_tokenize_refline(sp, buf, n);
474 if (err)
475 goto done;
476 if (chattygit && sp[2][0] != '\0')
477 fprintf(stderr, "server capabilities: %s\n", sp[2]);
478 if (is_firstpkt) {
479 err = match_capabilities(&my_capabilities, &symrefs,
480 sp[2]);
481 if (err)
482 goto done;
483 if (chattygit && my_capabilities)
484 fprintf(stderr, "my matched capabilities: %s\n",
485 my_capabilities);
486 err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
487 if (err)
488 goto done;
490 is_firstpkt = 0;
491 if (strstr(sp[1], "^{}"))
492 continue;
493 if (fetchbranch && !got_match_branch(sp[1], fetchbranch))
494 continue;
495 if (refsz == nref + 1){
496 refsz *= 2;
497 have = realloc(have, refsz * sizeof(have[0]));
498 if (have == NULL) {
499 err = got_error_from_errno("realloc");
500 goto done;
502 want = realloc(want, refsz * sizeof(want[0]));
503 if (want == NULL) {
504 err = got_error_from_errno("realloc");
505 goto done;
508 if (!got_parse_sha1_digest(want[nref].sha1, sp[0])) {
509 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
510 goto done;
513 if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
514 memset(&have[nref], 0, sizeof(have[nref]));
515 err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
516 if (err)
517 goto done;
518 if (chattygit)
519 fprintf(stderr, "remote %s\n", sp[1]);
520 nref++;
523 req = 0;
524 for (i = 0; i < nref; i++){
525 if (got_object_id_cmp(&have[i], &want[i]) == 0)
526 continue;
527 if (got_has_object(&want[i]))
528 continue;
529 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
530 n = snprintf(buf, sizeof(buf), "want %s%s%s\n", hashstr,
531 i == 0 && my_capabilities ? " " : "",
532 i == 0 && my_capabilities ? my_capabilities : "");
533 if (n >= sizeof(buf)) {
534 err = got_error(GOT_ERR_NO_SPACE);
535 goto done;
537 if (writepkt(fd, buf, n) == -1) {
538 err = got_error_from_errno("writepkt");
539 goto done;
541 req = 1;
543 flushpkt(fd);
544 for (i = 0; i < nref; i++){
545 if (got_object_id_cmp(&have[i], &zhash) == 0)
546 continue;
547 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
548 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
549 if (n >= sizeof(buf)) {
550 err = got_error(GOT_ERR_NO_SPACE);
551 goto done;
553 if (writepkt(fd, buf, n + 1) == -1) {
554 err = got_error_from_errno("writepkt");
555 goto done;
558 if (!req){
559 fprintf(stderr, "up to date\n");
560 flushpkt(fd);
562 n = snprintf(buf, sizeof(buf), "done\n");
563 if (writepkt(fd, buf, n) == -1) {
564 err = got_error_from_errno("writepkt");
565 goto done;
567 if (!req)
568 return 0;
570 if ((n = readpkt(fd, buf, sizeof(buf))) == -1) {
571 err = got_error_from_errno("readpkt");
572 goto done;
574 buf[n] = 0;
576 if (chattygit)
577 fprintf(stderr, "fetching...\n");
578 packsz = 0;
579 while (1) {
580 ssize_t w;
581 n = readn(fd, buf, sizeof buf);
582 if (n == 0)
583 break;
584 if (n == -1) {
585 err = got_error_from_errno("readn");
586 goto done;
588 w = write(packfd, buf, n);
589 if (w == -1) {
590 err = got_error_from_errno("write");
591 goto done;
593 if (w != n) {
594 err = got_error(GOT_ERR_IO);
595 goto done;
597 packsz += n;
599 if (lseek(packfd, 0, SEEK_SET) == -1) {
600 err = got_error_from_errno("lseek");
601 goto done;
603 if (got_check_pack_hash(packfd, packsz, packid->sha1) == -1)
604 err = got_error(GOT_ERR_BAD_PACKFILE);
605 done:
606 TAILQ_FOREACH(pe, &symrefs, entry) {
607 free((void *)pe->path);
608 free(pe->data);
610 got_pathlist_free(&symrefs);
611 free(have);
612 free(want);
613 return err;
617 int
618 main(int argc, char **argv)
620 const struct got_error *err = NULL;
621 int fetchfd, packfd = -1;
622 struct got_object_id packid;
623 struct imsgbuf ibuf;
624 struct imsg imsg;
626 if(getenv("GOT_DEBUG") != NULL){
627 fprintf(stderr, "fetch-pack being chatty!\n");
628 chattygit = 1;
631 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
632 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
633 if (err->code == GOT_ERR_PRIVSEP_PIPE)
634 err = NULL;
635 goto done;
637 if (imsg.hdr.type == GOT_IMSG_STOP)
638 goto done;
639 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
640 err = got_error(GOT_ERR_PRIVSEP_MSG);
641 goto done;
643 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
644 err = got_error(GOT_ERR_PRIVSEP_LEN);
645 goto done;
647 fetchfd = imsg.fd;
649 if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
650 if (err->code == GOT_ERR_PRIVSEP_PIPE)
651 err = NULL;
652 goto done;
654 if (imsg.hdr.type == GOT_IMSG_STOP)
655 goto done;
656 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
657 err = got_error(GOT_ERR_PRIVSEP_MSG);
658 goto done;
660 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
661 err = got_error(GOT_ERR_PRIVSEP_LEN);
662 goto done;
664 packfd = imsg.fd;
666 err = fetch_pack(fetchfd, packfd, &packid, &ibuf);
667 if (err)
668 goto done;
669 done:
670 if (packfd != -1 && close(packfd) == -1 && err == NULL)
671 err = got_error_from_errno("close");
672 if (err != NULL)
673 got_privsep_send_error(&ibuf, err);
674 else
675 err = got_privsep_send_fetch_done(&ibuf, packid);
676 if(err != NULL) {
677 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
678 got_privsep_send_error(&ibuf, err);
681 exit(0);