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/uio.h>
19 #include <sys/time.h>
20 #include <sys/stat.h>
22 #include <stdint.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <err.h>
35 #include "got_compat.h"
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_version.h"
41 #include "got_fetch.h"
42 #include "got_reference.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_pkt.h"
51 #include "got_lib_gitproto.h"
52 #include "got_lib_ratelimit.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 #endif
58 struct got_object *indexed;
59 static int chattygot;
61 static const struct got_capability got_capabilities[] = {
62 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
63 { GOT_CAPA_OFS_DELTA, NULL },
64 { GOT_CAPA_SIDE_BAND_64K, NULL },
65 };
67 static void
68 match_remote_ref(struct got_pathlist_head *have_refs,
69 struct got_object_id *my_id, char *refname)
70 {
71 struct got_pathlist_entry *pe;
73 /* XXX zero-hash signifies we don't have this ref;
74 * we should use a flag instead */
75 memset(my_id, 0, sizeof(*my_id));
77 TAILQ_FOREACH(pe, have_refs, entry) {
78 struct got_object_id *id = pe->data;
79 if (strcmp(pe->path, refname) == 0) {
80 memcpy(my_id, id, sizeof(*my_id));
81 break;
82 }
83 }
84 }
86 static int
87 match_branch(const char *branch, const char *wanted_branch)
88 {
89 if (strncmp(branch, "refs/heads/", 11) != 0)
90 return 0;
92 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
93 wanted_branch += 11;
95 return (strcmp(branch + 11, wanted_branch) == 0);
96 }
98 static int
99 match_wanted_ref(const char *refname, const char *wanted_ref)
101 if (strncmp(refname, "refs/", 5) != 0)
102 return 0;
103 refname += 5;
105 /*
106 * Prevent fetching of references that won't make any
107 * sense outside of the remote repository's context.
108 */
109 if (strncmp(refname, "got/", 4) == 0)
110 return 0;
111 if (strncmp(refname, "remotes/", 8) == 0)
112 return 0;
114 if (strncmp(wanted_ref, "refs/", 5) == 0)
115 wanted_ref += 5;
117 /* Allow prefix match. */
118 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
119 return 1;
121 /* Allow exact match. */
122 return (strcmp(refname, wanted_ref) == 0);
125 static const struct got_error *
126 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
128 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
129 return got_error(GOT_ERR_NO_SPACE);
131 if (msglen == 0)
132 return NULL;
134 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
135 msg, msglen) == -1)
136 return got_error_from_errno(
137 "imsg_compose FETCH_SERVER_PROGRESS");
139 return got_privsep_flush_imsg(ibuf);
142 static const struct got_error *
143 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
144 struct got_ratelimit *rl)
146 const struct got_error *err;
147 int elapsed = 0;
149 if (rl) {
150 err = got_ratelimit_check(&elapsed, rl);
151 if (err || !elapsed)
152 return err;
155 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
156 &bytes, sizeof(bytes)) == -1)
157 return got_error_from_errno(
158 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
160 return got_privsep_flush_imsg(ibuf);
163 static const struct got_error *
164 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
166 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
167 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
168 return got_error_from_errno("imsg_compose FETCH");
169 return got_privsep_flush_imsg(ibuf);
172 static const struct got_error *
173 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
175 size_t i;
177 if (len == 0)
178 return NULL;
180 /*
181 * Truncate messages which exceed the maximum imsg payload size.
182 * Server may send up to 64k.
183 */
184 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
185 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
187 /* Only allow printable ASCII. */
188 for (i = 0; i < len; i++) {
189 if (isprint((unsigned char)buf[i]) ||
190 isspace((unsigned char)buf[i]))
191 continue;
192 return got_error_msg(GOT_ERR_BAD_PACKET,
193 "non-printable progress message received from server");
196 return send_fetch_server_progress(ibuf, buf, len);
199 static const struct got_error *
200 fetch_error(const char *buf, size_t len)
202 static char msg[1024];
203 size_t i;
205 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
206 if (!isprint(buf[i]))
207 return got_error_msg(GOT_ERR_BAD_PACKET,
208 "non-printable error message received from server");
209 msg[i] = buf[i];
211 msg[i] = '\0';
212 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
215 static const struct got_error *
216 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
218 const struct got_error *err = NULL;
219 struct ibuf *wbuf;
220 size_t len, nsymrefs = 0;
221 struct got_pathlist_entry *pe;
223 len = sizeof(struct got_imsg_fetch_symrefs);
224 TAILQ_FOREACH(pe, symrefs, entry) {
225 const char *target = pe->data;
226 len += sizeof(struct got_imsg_fetch_symref) +
227 pe->path_len + strlen(target);
228 nsymrefs++;
231 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
232 return got_error(GOT_ERR_NO_SPACE);
234 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
235 if (wbuf == NULL)
236 return got_error_from_errno("imsg_create FETCH_SYMREFS");
238 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
239 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
240 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
241 ibuf_free(wbuf);
242 return err;
245 TAILQ_FOREACH(pe, symrefs, entry) {
246 const char *name = pe->path;
247 size_t name_len = pe->path_len;
248 const char *target = pe->data;
249 size_t target_len = strlen(target);
251 /* Keep in sync with struct got_imsg_fetch_symref definition! */
252 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
253 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
254 ibuf_free(wbuf);
255 return err;
257 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
258 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
259 ibuf_free(wbuf);
260 return err;
262 if (imsg_add(wbuf, name, name_len) == -1) {
263 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
264 ibuf_free(wbuf);
265 return err;
267 if (imsg_add(wbuf, target, target_len) == -1) {
268 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
269 ibuf_free(wbuf);
270 return err;
274 wbuf->fd = -1;
275 imsg_close(ibuf, wbuf);
276 return got_privsep_flush_imsg(ibuf);
279 static const struct got_error *
280 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
281 const char *refname)
283 const struct got_error *err = NULL;
284 struct ibuf *wbuf;
285 size_t len, reflen = strlen(refname);
287 len = sizeof(struct got_imsg_fetch_ref) + reflen;
288 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
289 return got_error(GOT_ERR_NO_SPACE);
291 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
292 if (wbuf == NULL)
293 return got_error_from_errno("imsg_create FETCH_REF");
295 /* Keep in sync with struct got_imsg_fetch_ref definition! */
296 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
297 err = got_error_from_errno("imsg_add FETCH_REF");
298 ibuf_free(wbuf);
299 return err;
301 if (imsg_add(wbuf, refname, reflen) == -1) {
302 err = got_error_from_errno("imsg_add FETCH_REF");
303 ibuf_free(wbuf);
304 return err;
307 wbuf->fd = -1;
308 imsg_close(ibuf, wbuf);
309 return got_privsep_flush_imsg(ibuf);
312 static const struct got_error *
313 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
314 struct got_pathlist_head *have_refs, int fetch_all_branches,
315 struct got_pathlist_head *wanted_branches,
316 struct got_pathlist_head *wanted_refs, int list_refs_only,
317 struct imsgbuf *ibuf)
319 const struct got_error *err = NULL;
320 char buf[GOT_PKT_MAX];
321 char hashstr[SHA1_DIGEST_STRING_LENGTH];
322 struct got_object_id *have, *want;
323 int is_firstpkt = 1, nref = 0, refsz = 16;
324 int i, n, nwant = 0, nhave = 0, acked = 0;
325 off_t packsz = 0, last_reported_packsz = 0;
326 char *id_str = NULL, *refname = NULL;
327 char *server_capabilities = NULL, *my_capabilities = NULL;
328 const char *default_branch = NULL;
329 struct got_pathlist_head symrefs;
330 struct got_pathlist_entry *pe;
331 int sent_my_capabilites = 0, have_sidebands = 0;
332 int found_branch = 0;
333 SHA1_CTX sha1_ctx;
334 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
335 size_t sha1_buf_len = 0;
336 ssize_t w;
337 struct got_ratelimit rl;
339 TAILQ_INIT(&symrefs);
340 SHA1Init(&sha1_ctx);
341 got_ratelimit_init(&rl, 0, 500);
343 have = malloc(refsz * sizeof(have[0]));
344 if (have == NULL)
345 return got_error_from_errno("malloc");
346 want = malloc(refsz * sizeof(want[0]));
347 if (want == NULL) {
348 err = got_error_from_errno("malloc");
349 goto done;
351 while (1) {
352 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
353 if (err)
354 goto done;
355 if (n == 0)
356 break;
357 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
358 err = fetch_error(&buf[4], n - 4);
359 goto done;
361 free(id_str);
362 free(refname);
363 err = got_gitproto_parse_refline(&id_str, &refname,
364 &server_capabilities, buf, n);
365 if (err)
366 goto done;
367 if (is_firstpkt) {
368 if (chattygot && server_capabilities[0] != '\0')
369 fprintf(stderr, "%s: server capabilities: %s\n",
370 getprogname(), server_capabilities);
371 err = got_gitproto_match_capabilities(&my_capabilities,
372 &symrefs, server_capabilities,
373 got_capabilities, nitems(got_capabilities));
374 if (err)
375 goto done;
376 if (chattygot)
377 fprintf(stderr, "%s: my capabilities:%s\n",
378 getprogname(), my_capabilities != NULL ?
379 my_capabilities : "");
380 err = send_fetch_symrefs(ibuf, &symrefs);
381 if (err)
382 goto done;
383 is_firstpkt = 0;
384 if (!fetch_all_branches) {
385 TAILQ_FOREACH(pe, &symrefs, entry) {
386 const char *name = pe->path;
387 const char *symref_target = pe->data;
388 if (strcmp(name, GOT_REF_HEAD) != 0)
389 continue;
390 default_branch = symref_target;
391 break;
394 continue;
396 if (strstr(refname, "^{}")) {
397 if (chattygot) {
398 fprintf(stderr, "%s: ignoring %s\n",
399 getprogname(), refname);
401 continue;
404 if (strncmp(refname, "refs/heads/", 11) == 0) {
405 if (fetch_all_branches || list_refs_only) {
406 found_branch = 1;
407 } else if (!TAILQ_EMPTY(wanted_branches)) {
408 TAILQ_FOREACH(pe, wanted_branches, entry) {
409 if (match_branch(refname, pe->path))
410 break;
412 if (pe == NULL) {
413 if (chattygot) {
414 fprintf(stderr,
415 "%s: ignoring %s\n",
416 getprogname(), refname);
418 continue;
420 found_branch = 1;
421 } else if (default_branch != NULL) {
422 if (!match_branch(refname, default_branch)) {
423 if (chattygot) {
424 fprintf(stderr,
425 "%s: ignoring %s\n",
426 getprogname(), refname);
428 continue;
430 found_branch = 1;
432 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
433 if (!TAILQ_EMPTY(wanted_refs)) {
434 TAILQ_FOREACH(pe, wanted_refs, entry) {
435 if (match_wanted_ref(refname, pe->path))
436 break;
438 if (pe == NULL) {
439 if (chattygot) {
440 fprintf(stderr,
441 "%s: ignoring %s\n",
442 getprogname(), refname);
444 continue;
446 found_branch = 1;
447 } else if (!list_refs_only) {
448 if (chattygot) {
449 fprintf(stderr, "%s: ignoring %s\n",
450 getprogname(), refname);
452 continue;
456 if (refsz == nref + 1) {
457 refsz *= 2;
458 have = reallocarray(have, refsz, sizeof(have[0]));
459 if (have == NULL) {
460 err = got_error_from_errno("reallocarray");
461 goto done;
463 want = reallocarray(want, refsz, sizeof(want[0]));
464 if (want == NULL) {
465 err = got_error_from_errno("reallocarray");
466 goto done;
469 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
470 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
471 goto done;
473 match_remote_ref(have_refs, &have[nref], refname);
474 err = send_fetch_ref(ibuf, &want[nref], refname);
475 if (err)
476 goto done;
478 if (chattygot)
479 fprintf(stderr, "%s: %s will be fetched\n",
480 getprogname(), refname);
481 if (chattygot > 1) {
482 char *theirs, *mine;
483 err = got_object_id_str(&theirs, &want[nref]);
484 if (err)
485 goto done;
486 err = got_object_id_str(&mine, &have[nref]);
487 if (err) {
488 free(theirs);
489 goto done;
491 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
492 getprogname(), theirs, getprogname(), mine);
493 free(theirs);
494 free(mine);
496 nref++;
499 if (list_refs_only)
500 goto done;
502 /* Abort if we haven't found any branch to fetch. */
503 if (!found_branch) {
504 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
505 goto done;
508 for (i = 0; i < nref; i++) {
509 if (got_object_id_cmp(&have[i], &want[i]) == 0)
510 continue;
511 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
512 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
513 sent_my_capabilites || my_capabilities == NULL ?
514 "" : my_capabilities);
515 if (n >= sizeof(buf)) {
516 err = got_error(GOT_ERR_NO_SPACE);
517 goto done;
519 err = got_pkt_writepkt(fd, buf, n, chattygot);
520 if (err)
521 goto done;
522 sent_my_capabilites = 1;
523 nwant++;
525 err = got_pkt_flushpkt(fd, chattygot);
526 if (err)
527 goto done;
529 if (nwant == 0)
530 goto done;
532 TAILQ_FOREACH(pe, have_refs, entry) {
533 struct got_object_id *id = pe->data;
534 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
535 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
536 if (n >= sizeof(buf)) {
537 err = got_error(GOT_ERR_NO_SPACE);
538 goto done;
540 err = got_pkt_writepkt(fd, buf, n, chattygot);
541 if (err)
542 goto done;
543 nhave++;
546 while (nhave > 0 && !acked) {
547 struct got_object_id common_id;
549 /* The server should ACK the object IDs we need. */
550 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
551 if (err)
552 goto done;
553 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
554 err = fetch_error(&buf[4], n - 4);
555 goto done;
557 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
558 /* Server has not located our objects yet. */
559 continue;
561 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
562 strncmp(buf, "ACK ", 4) != 0) {
563 err = got_error_msg(GOT_ERR_BAD_PACKET,
564 "unexpected message from server");
565 goto done;
567 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
568 err = got_error_msg(GOT_ERR_BAD_PACKET,
569 "bad object ID in ACK packet from server");
570 goto done;
572 acked++;
575 n = snprintf(buf, sizeof(buf), "done\n");
576 err = got_pkt_writepkt(fd, buf, n, chattygot);
577 if (err)
578 goto done;
580 if (nhave == 0) {
581 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
582 if (err)
583 goto done;
584 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
585 err = got_error_msg(GOT_ERR_BAD_PACKET,
586 "unexpected message from server");
587 goto done;
591 if (chattygot)
592 fprintf(stderr, "%s: fetching...\n", getprogname());
594 if (my_capabilities != NULL &&
595 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
596 have_sidebands = 1;
598 while (1) {
599 ssize_t r = 0;
600 int datalen = -1;
602 if (have_sidebands) {
603 err = got_pkt_readhdr(&datalen, fd, chattygot);
604 if (err)
605 goto done;
606 if (datalen <= 0)
607 break;
609 /* Read sideband channel ID (one byte). */
610 r = read(fd, buf, 1);
611 if (r == -1) {
612 err = got_error_from_errno("read");
613 goto done;
615 if (r != 1) {
616 err = got_error_msg(GOT_ERR_BAD_PACKET,
617 "short packet");
618 goto done;
620 if (datalen > sizeof(buf) - 5) {
621 err = got_error_msg(GOT_ERR_BAD_PACKET,
622 "bad packet length");
623 goto done;
625 datalen--; /* sideband ID has been read */
626 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
627 /* Read packfile data. */
628 err = got_pkt_readn(&r, fd, buf, datalen);
629 if (err)
630 goto done;
631 if (r != datalen) {
632 err = got_error_msg(GOT_ERR_BAD_PACKET,
633 "packet too short");
634 goto done;
636 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
637 err = got_pkt_readn(&r, fd, buf, datalen);
638 if (err)
639 goto done;
640 if (r != datalen) {
641 err = got_error_msg(GOT_ERR_BAD_PACKET,
642 "packet too short");
643 goto done;
645 err = fetch_progress(ibuf, buf, r);
646 if (err)
647 goto done;
648 continue;
649 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
650 err = got_pkt_readn(&r, fd, buf, datalen);
651 if (err)
652 goto done;
653 if (r != datalen) {
654 err = got_error_msg(GOT_ERR_BAD_PACKET,
655 "packet too short");
656 goto done;
658 err = fetch_error(buf, r);
659 goto done;
660 } else if (buf[0] == 'A') {
661 err = got_pkt_readn(&r, fd, buf, datalen);
662 if (err)
663 goto done;
664 if (r != datalen) {
665 err = got_error_msg(GOT_ERR_BAD_PACKET,
666 "packet too short");
667 goto done;
669 /*
670 * Git server responds with ACK after 'done'
671 * even though multi_ack is disabled?!?
672 */
673 buf[r] = '\0';
674 if (strncmp(buf, "CK ", 3) == 0)
675 continue; /* ignore */
676 err = got_error_msg(GOT_ERR_BAD_PACKET,
677 "unexpected message from server");
678 goto done;
679 } else {
680 err = got_error_msg(GOT_ERR_BAD_PACKET,
681 "unknown side-band received from server");
682 goto done;
684 } else {
685 /* No sideband channel. Every byte is packfile data. */
686 err = got_pkt_readn(&r, fd, buf, sizeof buf);
687 if (err)
688 goto done;
689 if (r <= 0)
690 break;
693 /*
694 * An expected SHA1 checksum sits at the end of the pack file.
695 * Since we don't know the file size ahead of time we have to
696 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
697 * those bytes into our SHA1 checksum computation until we
698 * know for sure that additional pack file data bytes follow.
700 * We can assume r > 0 since otherwise the loop would exit.
701 */
702 if (r < SHA1_DIGEST_LENGTH) {
703 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
704 /*
705 * If there's enough buffered + read data to
706 * fill up the buffer then shift a sufficient
707 * amount of bytes out at the front to make
708 * room, mixing those bytes into the checksum.
709 */
710 while (sha1_buf_len > 0 &&
711 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
712 SHA1Update(&sha1_ctx, sha1_buf, 1);
713 memmove(sha1_buf, sha1_buf + 1, 1);
714 sha1_buf_len--;
717 /* Buffer potential checksum bytes. */
718 memcpy(sha1_buf + sha1_buf_len, buf, r);
719 sha1_buf_len += r;
720 } else {
721 /*
722 * Mix in previously buffered bytes which
723 * are not part of the checksum after all.
724 */
725 SHA1Update(&sha1_ctx, sha1_buf, r);
727 /* Update potential checksum buffer. */
728 memmove(sha1_buf, sha1_buf + r,
729 sha1_buf_len - r);
730 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
732 } else {
733 /* Mix in any previously buffered bytes. */
734 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
736 /* Mix in bytes read minus potential checksum bytes. */
737 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
739 /* Buffer potential checksum bytes. */
740 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
741 SHA1_DIGEST_LENGTH);
742 sha1_buf_len = SHA1_DIGEST_LENGTH;
745 /* Write packfile data to temporary pack file. */
746 w = write(packfd, buf, r);
747 if (w == -1) {
748 err = got_error_from_errno("write");
749 goto done;
751 if (w != r) {
752 err = got_error(GOT_ERR_IO);
753 goto done;
755 packsz += w;
757 /* Don't send too many progress privsep messages. */
758 if (packsz > last_reported_packsz + 1024) {
759 err = send_fetch_download_progress(ibuf, packsz, &rl);
760 if (err)
761 goto done;
762 last_reported_packsz = packsz;
765 err = send_fetch_download_progress(ibuf, packsz, NULL);
766 if (err)
767 goto done;
769 SHA1Final(pack_sha1, &sha1_ctx);
770 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
771 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
772 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
773 "pack file checksum mismatch");
775 done:
776 TAILQ_FOREACH(pe, &symrefs, entry) {
777 free((void *)pe->path);
778 free(pe->data);
780 got_pathlist_free(&symrefs);
781 free(have);
782 free(want);
783 free(id_str);
784 free(refname);
785 free(server_capabilities);
786 return err;
790 int
791 main(int argc, char **argv)
793 const struct got_error *err = NULL;
794 int fetchfd, packfd = -1;
795 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
796 struct imsgbuf ibuf;
797 struct imsg imsg;
798 struct got_pathlist_head have_refs;
799 struct got_pathlist_head wanted_branches;
800 struct got_pathlist_head wanted_refs;
801 struct got_pathlist_entry *pe;
802 struct got_imsg_fetch_request fetch_req;
803 struct got_imsg_fetch_have_ref href;
804 struct got_imsg_fetch_wanted_branch wbranch;
805 struct got_imsg_fetch_wanted_ref wref;
806 size_t datalen, i;
807 #if 0
808 static int attached;
809 while (!attached)
810 sleep (1);
811 #endif
813 TAILQ_INIT(&have_refs);
814 TAILQ_INIT(&wanted_branches);
815 TAILQ_INIT(&wanted_refs);
817 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
818 #ifndef PROFILE
819 /* revoke access to most system calls */
820 if (pledge("stdio recvfd", NULL) == -1) {
821 err = got_error_from_errno("pledge");
822 got_privsep_send_error(&ibuf, err);
823 return 1;
826 /* revoke fs access */
827 if (landlock_no_fs() == -1) {
828 err = got_error_from_errno("landlock_no_fs");
829 got_privsep_send_error(&ibuf, err);
830 return 1;
832 #endif
833 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
834 if (err) {
835 if (err->code == GOT_ERR_PRIVSEP_PIPE)
836 err = NULL;
837 goto done;
839 if (imsg.hdr.type == GOT_IMSG_STOP)
840 goto done;
841 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
842 err = got_error(GOT_ERR_PRIVSEP_MSG);
843 goto done;
845 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
846 if (datalen < sizeof(fetch_req)) {
847 err = got_error(GOT_ERR_PRIVSEP_LEN);
848 goto done;
850 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
851 fetchfd = imsg.fd;
852 imsg_free(&imsg);
854 if (fetch_req.verbosity > 0)
855 chattygot += fetch_req.verbosity;
857 for (i = 0; i < fetch_req.n_have_refs; i++) {
858 struct got_object_id *id;
859 char *refname;
861 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
862 if (err) {
863 if (err->code == GOT_ERR_PRIVSEP_PIPE)
864 err = NULL;
865 goto done;
867 if (imsg.hdr.type == GOT_IMSG_STOP)
868 goto done;
869 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
870 err = got_error(GOT_ERR_PRIVSEP_MSG);
871 goto done;
873 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
874 if (datalen < sizeof(href)) {
875 err = got_error(GOT_ERR_PRIVSEP_LEN);
876 goto done;
878 memcpy(&href, imsg.data, sizeof(href));
879 if (datalen - sizeof(href) < href.name_len) {
880 err = got_error(GOT_ERR_PRIVSEP_LEN);
881 goto done;
883 refname = malloc(href.name_len + 1);
884 if (refname == NULL) {
885 err = got_error_from_errno("malloc");
886 goto done;
888 memcpy(refname, imsg.data + sizeof(href), href.name_len);
889 refname[href.name_len] = '\0';
891 id = malloc(sizeof(*id));
892 if (id == NULL) {
893 free(refname);
894 err = got_error_from_errno("malloc");
895 goto done;
897 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
898 err = got_pathlist_append(&have_refs, refname, id);
899 if (err) {
900 free(refname);
901 free(id);
902 goto done;
905 imsg_free(&imsg);
908 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
909 char *refname;
911 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
912 if (err) {
913 if (err->code == GOT_ERR_PRIVSEP_PIPE)
914 err = NULL;
915 goto done;
917 if (imsg.hdr.type == GOT_IMSG_STOP)
918 goto done;
919 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
920 err = got_error(GOT_ERR_PRIVSEP_MSG);
921 goto done;
923 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
924 if (datalen < sizeof(wbranch)) {
925 err = got_error(GOT_ERR_PRIVSEP_LEN);
926 goto done;
928 memcpy(&wbranch, imsg.data, sizeof(wbranch));
929 if (datalen - sizeof(wbranch) < wbranch.name_len) {
930 err = got_error(GOT_ERR_PRIVSEP_LEN);
931 goto done;
933 refname = malloc(wbranch.name_len + 1);
934 if (refname == NULL) {
935 err = got_error_from_errno("malloc");
936 goto done;
938 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
939 refname[wbranch.name_len] = '\0';
941 err = got_pathlist_append(&wanted_branches, refname, NULL);
942 if (err) {
943 free(refname);
944 goto done;
947 imsg_free(&imsg);
950 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
951 char *refname;
953 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
954 if (err) {
955 if (err->code == GOT_ERR_PRIVSEP_PIPE)
956 err = NULL;
957 goto done;
959 if (imsg.hdr.type == GOT_IMSG_STOP)
960 goto done;
961 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
962 err = got_error(GOT_ERR_PRIVSEP_MSG);
963 goto done;
965 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
966 if (datalen < sizeof(wref)) {
967 err = got_error(GOT_ERR_PRIVSEP_LEN);
968 goto done;
970 memcpy(&wref, imsg.data, sizeof(wref));
971 if (datalen - sizeof(wref) < wref.name_len) {
972 err = got_error(GOT_ERR_PRIVSEP_LEN);
973 goto done;
975 refname = malloc(wref.name_len + 1);
976 if (refname == NULL) {
977 err = got_error_from_errno("malloc");
978 goto done;
980 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
981 refname[wref.name_len] = '\0';
983 err = got_pathlist_append(&wanted_refs, refname, NULL);
984 if (err) {
985 free(refname);
986 goto done;
989 imsg_free(&imsg);
992 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
993 if (err) {
994 if (err->code == GOT_ERR_PRIVSEP_PIPE)
995 err = NULL;
996 goto done;
998 if (imsg.hdr.type == GOT_IMSG_STOP)
999 goto done;
1000 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1001 err = got_error(GOT_ERR_PRIVSEP_MSG);
1002 goto done;
1004 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1005 err = got_error(GOT_ERR_PRIVSEP_LEN);
1006 goto done;
1008 packfd = imsg.fd;
1010 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1011 fetch_req.fetch_all_branches, &wanted_branches,
1012 &wanted_refs, fetch_req.list_refs_only, &ibuf);
1013 done:
1014 TAILQ_FOREACH(pe, &have_refs, entry) {
1015 free((char *)pe->path);
1016 free(pe->data);
1018 got_pathlist_free(&have_refs);
1019 TAILQ_FOREACH(pe, &wanted_branches, entry)
1020 free((char *)pe->path);
1021 got_pathlist_free(&wanted_branches);
1022 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1023 err = got_error_from_errno("close");
1024 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1025 err = got_error_from_errno("close");
1026 if (err != NULL)
1027 got_privsep_send_error(&ibuf, err);
1028 else
1029 err = send_fetch_done(&ibuf, pack_sha1);
1030 if (err != NULL) {
1031 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1032 got_privsep_send_error(&ibuf, err);
1035 exit(0);