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>
23 #include <stdint.h>
24 #include <errno.h>
25 #include <imsg.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <sha1.h>
33 #include <fcntl.h>
34 #include <unistd.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"
42 #include "got_fetch.h"
43 #include "got_reference.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_pkt.h"
52 #include "got_lib_gitproto.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)
145 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
146 &bytes, sizeof(bytes)) == -1)
147 return got_error_from_errno(
148 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
150 return got_privsep_flush_imsg(ibuf);
153 static const struct got_error *
154 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
156 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
157 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
158 return got_error_from_errno("imsg_compose FETCH");
159 return got_privsep_flush_imsg(ibuf);
162 static const struct got_error *
163 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
165 size_t i;
167 if (len == 0)
168 return NULL;
170 /*
171 * Truncate messages which exceed the maximum imsg payload size.
172 * Server may send up to 64k.
173 */
174 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
175 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
177 /* Only allow printable ASCII. */
178 for (i = 0; i < len; i++) {
179 if (isprint((unsigned char)buf[i]) ||
180 isspace((unsigned char)buf[i]))
181 continue;
182 return got_error_msg(GOT_ERR_BAD_PACKET,
183 "non-printable progress message received from server");
186 return send_fetch_server_progress(ibuf, buf, len);
189 static const struct got_error *
190 fetch_error(const char *buf, size_t len)
192 static char msg[1024];
193 size_t i;
195 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
196 if (!isprint(buf[i]))
197 return got_error_msg(GOT_ERR_BAD_PACKET,
198 "non-printable error message received from server");
199 msg[i] = buf[i];
201 msg[i] = '\0';
202 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
205 static const struct got_error *
206 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
208 const struct got_error *err = NULL;
209 struct ibuf *wbuf;
210 size_t len, nsymrefs = 0;
211 struct got_pathlist_entry *pe;
213 len = sizeof(struct got_imsg_fetch_symrefs);
214 TAILQ_FOREACH(pe, symrefs, entry) {
215 const char *target = pe->data;
216 len += sizeof(struct got_imsg_fetch_symref) +
217 pe->path_len + strlen(target);
218 nsymrefs++;
221 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
222 return got_error(GOT_ERR_NO_SPACE);
224 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
225 if (wbuf == NULL)
226 return got_error_from_errno("imsg_create FETCH_SYMREFS");
228 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
229 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
230 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
231 ibuf_free(wbuf);
232 return err;
235 TAILQ_FOREACH(pe, symrefs, entry) {
236 const char *name = pe->path;
237 size_t name_len = pe->path_len;
238 const char *target = pe->data;
239 size_t target_len = strlen(target);
241 /* Keep in sync with struct got_imsg_fetch_symref definition! */
242 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
243 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
244 ibuf_free(wbuf);
245 return err;
247 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
248 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
249 ibuf_free(wbuf);
250 return err;
252 if (imsg_add(wbuf, name, 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, target_len) == -1) {
258 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
259 ibuf_free(wbuf);
260 return err;
264 wbuf->fd = -1;
265 imsg_close(ibuf, wbuf);
266 return got_privsep_flush_imsg(ibuf);
269 static const struct got_error *
270 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
271 const char *refname)
273 const struct got_error *err = NULL;
274 struct ibuf *wbuf;
275 size_t len, reflen = strlen(refname);
277 len = sizeof(struct got_imsg_fetch_ref) + reflen;
278 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
279 return got_error(GOT_ERR_NO_SPACE);
281 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
282 if (wbuf == NULL)
283 return got_error_from_errno("imsg_create FETCH_REF");
285 /* Keep in sync with struct got_imsg_fetch_ref definition! */
286 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
287 err = got_error_from_errno("imsg_add FETCH_REF");
288 ibuf_free(wbuf);
289 return err;
291 if (imsg_add(wbuf, refname, reflen) == -1) {
292 err = got_error_from_errno("imsg_add FETCH_REF");
293 ibuf_free(wbuf);
294 return err;
297 wbuf->fd = -1;
298 imsg_close(ibuf, wbuf);
299 return got_privsep_flush_imsg(ibuf);
302 static const struct got_error *
303 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
304 struct got_pathlist_head *have_refs, int fetch_all_branches,
305 struct got_pathlist_head *wanted_branches,
306 struct got_pathlist_head *wanted_refs, int list_refs_only,
307 struct imsgbuf *ibuf)
309 const struct got_error *err = NULL;
310 char buf[GOT_PKT_MAX];
311 char hashstr[SHA1_DIGEST_STRING_LENGTH];
312 struct got_object_id *have, *want;
313 int is_firstpkt = 1, nref = 0, refsz = 16;
314 int i, n, nwant = 0, nhave = 0, acked = 0;
315 off_t packsz = 0, last_reported_packsz = 0;
316 char *id_str = NULL, *refname = NULL;
317 char *server_capabilities = NULL, *my_capabilities = NULL;
318 const char *default_branch = NULL;
319 struct got_pathlist_head symrefs;
320 struct got_pathlist_entry *pe;
321 int sent_my_capabilites = 0, have_sidebands = 0;
322 int found_branch = 0;
323 SHA1_CTX sha1_ctx;
324 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
325 size_t sha1_buf_len = 0;
326 ssize_t w;
328 TAILQ_INIT(&symrefs);
329 SHA1Init(&sha1_ctx);
331 have = malloc(refsz * sizeof(have[0]));
332 if (have == NULL)
333 return got_error_from_errno("malloc");
334 want = malloc(refsz * sizeof(want[0]));
335 if (want == NULL) {
336 err = got_error_from_errno("malloc");
337 goto done;
339 while (1) {
340 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
341 if (err)
342 goto done;
343 if (n == 0)
344 break;
345 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
346 err = fetch_error(&buf[4], n - 4);
347 goto done;
349 err = got_gitproto_parse_refline(&id_str, &refname,
350 &server_capabilities, buf, n);
351 if (err)
352 goto done;
353 if (is_firstpkt) {
354 if (chattygot && server_capabilities[0] != '\0')
355 fprintf(stderr, "%s: server capabilities: %s\n",
356 getprogname(), server_capabilities);
357 err = got_gitproto_match_capabilities(&my_capabilities,
358 &symrefs, server_capabilities,
359 got_capabilities, nitems(got_capabilities));
360 if (err)
361 goto done;
362 if (chattygot)
363 fprintf(stderr, "%s: my capabilities:%s\n",
364 getprogname(), my_capabilities != NULL ?
365 my_capabilities : "");
366 err = send_fetch_symrefs(ibuf, &symrefs);
367 if (err)
368 goto done;
369 is_firstpkt = 0;
370 if (!fetch_all_branches) {
371 TAILQ_FOREACH(pe, &symrefs, entry) {
372 const char *name = pe->path;
373 const char *symref_target = pe->data;
374 if (strcmp(name, GOT_REF_HEAD) != 0)
375 continue;
376 default_branch = symref_target;
377 break;
380 continue;
382 if (strstr(refname, "^{}")) {
383 if (chattygot) {
384 fprintf(stderr, "%s: ignoring %s\n",
385 getprogname(), refname);
387 continue;
390 if (strncmp(refname, "refs/heads/", 11) == 0) {
391 if (fetch_all_branches || list_refs_only) {
392 found_branch = 1;
393 } else if (!TAILQ_EMPTY(wanted_branches)) {
394 TAILQ_FOREACH(pe, wanted_branches, entry) {
395 if (match_branch(refname, pe->path))
396 break;
398 if (pe == NULL) {
399 if (chattygot) {
400 fprintf(stderr,
401 "%s: ignoring %s\n",
402 getprogname(), refname);
404 continue;
406 found_branch = 1;
407 } else if (default_branch != NULL) {
408 if (!match_branch(refname, default_branch)) {
409 if (chattygot) {
410 fprintf(stderr,
411 "%s: ignoring %s\n",
412 getprogname(), refname);
414 continue;
416 found_branch = 1;
418 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
419 if (!TAILQ_EMPTY(wanted_refs)) {
420 TAILQ_FOREACH(pe, wanted_refs, entry) {
421 if (match_wanted_ref(refname, pe->path))
422 break;
424 if (pe == NULL) {
425 if (chattygot) {
426 fprintf(stderr,
427 "%s: ignoring %s\n",
428 getprogname(), refname);
430 continue;
432 found_branch = 1;
433 } else if (!list_refs_only) {
434 if (chattygot) {
435 fprintf(stderr, "%s: ignoring %s\n",
436 getprogname(), refname);
438 continue;
442 if (refsz == nref + 1) {
443 refsz *= 2;
444 have = reallocarray(have, refsz, sizeof(have[0]));
445 if (have == NULL) {
446 err = got_error_from_errno("reallocarray");
447 goto done;
449 want = reallocarray(want, refsz, sizeof(want[0]));
450 if (want == NULL) {
451 err = got_error_from_errno("reallocarray");
452 goto done;
455 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
456 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
457 goto done;
459 match_remote_ref(have_refs, &have[nref], refname);
460 err = send_fetch_ref(ibuf, &want[nref], refname);
461 if (err)
462 goto done;
464 if (chattygot)
465 fprintf(stderr, "%s: %s will be fetched\n",
466 getprogname(), refname);
467 if (chattygot > 1) {
468 char *theirs, *mine;
469 err = got_object_id_str(&theirs, &want[nref]);
470 if (err)
471 goto done;
472 err = got_object_id_str(&mine, &have[nref]);
473 if (err) {
474 free(theirs);
475 goto done;
477 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
478 getprogname(), theirs, getprogname(), mine);
479 free(theirs);
480 free(mine);
482 nref++;
485 if (list_refs_only)
486 goto done;
488 /* Abort if we haven't found any branch to fetch. */
489 if (!found_branch) {
490 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
491 goto done;
494 for (i = 0; i < nref; i++) {
495 if (got_object_id_cmp(&have[i], &want[i]) == 0)
496 continue;
497 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
498 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
499 sent_my_capabilites || my_capabilities == NULL ?
500 "" : my_capabilities);
501 if (n >= sizeof(buf)) {
502 err = got_error(GOT_ERR_NO_SPACE);
503 goto done;
505 err = got_pkt_writepkt(fd, buf, n, chattygot);
506 if (err)
507 goto done;
508 sent_my_capabilites = 1;
509 nwant++;
511 err = got_pkt_flushpkt(fd, chattygot);
512 if (err)
513 goto done;
515 if (nwant == 0)
516 goto done;
518 TAILQ_FOREACH(pe, have_refs, entry) {
519 struct got_object_id *id = pe->data;
520 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
521 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
522 if (n >= sizeof(buf)) {
523 err = got_error(GOT_ERR_NO_SPACE);
524 goto done;
526 err = got_pkt_writepkt(fd, buf, n, chattygot);
527 if (err)
528 goto done;
529 nhave++;
532 while (nhave > 0 && !acked) {
533 struct got_object_id common_id;
535 /* The server should ACK the object IDs we need. */
536 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
537 if (err)
538 goto done;
539 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
540 err = fetch_error(&buf[4], n - 4);
541 goto done;
543 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
544 /* Server has not located our objects yet. */
545 continue;
547 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
548 strncmp(buf, "ACK ", 4) != 0) {
549 err = got_error_msg(GOT_ERR_BAD_PACKET,
550 "unexpected message from server");
551 goto done;
553 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
554 err = got_error_msg(GOT_ERR_BAD_PACKET,
555 "bad object ID in ACK packet from server");
556 goto done;
558 acked++;
561 n = snprintf(buf, sizeof(buf), "done\n");
562 err = got_pkt_writepkt(fd, buf, n, chattygot);
563 if (err)
564 goto done;
566 if (nhave == 0) {
567 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
568 if (err)
569 goto done;
570 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
571 err = got_error_msg(GOT_ERR_BAD_PACKET,
572 "unexpected message from server");
573 goto done;
577 if (chattygot)
578 fprintf(stderr, "%s: fetching...\n", getprogname());
580 if (my_capabilities != NULL &&
581 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
582 have_sidebands = 1;
584 while (1) {
585 ssize_t r = 0;
586 int datalen = -1;
588 if (have_sidebands) {
589 err = got_pkt_readhdr(&datalen, fd, chattygot);
590 if (err)
591 goto done;
592 if (datalen <= 0)
593 break;
595 /* Read sideband channel ID (one byte). */
596 r = read(fd, buf, 1);
597 if (r == -1) {
598 err = got_error_from_errno("read");
599 goto done;
601 if (r != 1) {
602 err = got_error_msg(GOT_ERR_BAD_PACKET,
603 "short packet");
604 goto done;
606 if (datalen > sizeof(buf) - 5) {
607 err = got_error_msg(GOT_ERR_BAD_PACKET,
608 "bad packet length");
609 goto done;
611 datalen--; /* sideband ID has been read */
612 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
613 /* Read packfile data. */
614 err = got_pkt_readn(&r, fd, buf, datalen);
615 if (err)
616 goto done;
617 if (r != datalen) {
618 err = got_error_msg(GOT_ERR_BAD_PACKET,
619 "packet too short");
620 goto done;
622 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
623 err = got_pkt_readn(&r, fd, buf, datalen);
624 if (err)
625 goto done;
626 if (r != datalen) {
627 err = got_error_msg(GOT_ERR_BAD_PACKET,
628 "packet too short");
629 goto done;
631 err = fetch_progress(ibuf, buf, r);
632 if (err)
633 goto done;
634 continue;
635 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
636 err = got_pkt_readn(&r, fd, buf, datalen);
637 if (err)
638 goto done;
639 if (r != datalen) {
640 err = got_error_msg(GOT_ERR_BAD_PACKET,
641 "packet too short");
642 goto done;
644 err = fetch_error(buf, r);
645 goto done;
646 } else if (buf[0] == 'A') {
647 err = got_pkt_readn(&r, fd, buf, datalen);
648 if (err)
649 goto done;
650 if (r != datalen) {
651 err = got_error_msg(GOT_ERR_BAD_PACKET,
652 "packet too short");
653 goto done;
655 /*
656 * Git server responds with ACK after 'done'
657 * even though multi_ack is disabled?!?
658 */
659 buf[r] = '\0';
660 if (strncmp(buf, "CK ", 3) == 0)
661 continue; /* ignore */
662 err = got_error_msg(GOT_ERR_BAD_PACKET,
663 "unexpected message from server");
664 goto done;
665 } else {
666 err = got_error_msg(GOT_ERR_BAD_PACKET,
667 "unknown side-band received from server");
668 goto done;
670 } else {
671 /* No sideband channel. Every byte is packfile data. */
672 err = got_pkt_readn(&r, fd, buf, sizeof buf);
673 if (err)
674 goto done;
675 if (r <= 0)
676 break;
679 /*
680 * An expected SHA1 checksum sits at the end of the pack file.
681 * Since we don't know the file size ahead of time we have to
682 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
683 * those bytes into our SHA1 checksum computation until we
684 * know for sure that additional pack file data bytes follow.
686 * We can assume r > 0 since otherwise the loop would exit.
687 */
688 if (r < SHA1_DIGEST_LENGTH) {
689 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
690 /*
691 * If there's enough buffered + read data to
692 * fill up the buffer then shift a sufficient
693 * amount of bytes out at the front to make
694 * room, mixing those bytes into the checksum.
695 */
696 while (sha1_buf_len > 0 &&
697 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
698 SHA1Update(&sha1_ctx, sha1_buf, 1);
699 memmove(sha1_buf, sha1_buf + 1, 1);
700 sha1_buf_len--;
703 /* Buffer potential checksum bytes. */
704 memcpy(sha1_buf + sha1_buf_len, buf, r);
705 sha1_buf_len += r;
706 } else {
707 /*
708 * Mix in previously buffered bytes which
709 * are not part of the checksum after all.
710 */
711 SHA1Update(&sha1_ctx, sha1_buf, r);
713 /* Update potential checksum buffer. */
714 memmove(sha1_buf, sha1_buf + r,
715 sha1_buf_len - r);
716 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
718 } else {
719 /* Mix in any previously buffered bytes. */
720 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
722 /* Mix in bytes read minus potential checksum bytes. */
723 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
725 /* Buffer potential checksum bytes. */
726 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
727 SHA1_DIGEST_LENGTH);
728 sha1_buf_len = SHA1_DIGEST_LENGTH;
731 /* Write packfile data to temporary pack file. */
732 w = write(packfd, buf, r);
733 if (w == -1) {
734 err = got_error_from_errno("write");
735 goto done;
737 if (w != r) {
738 err = got_error(GOT_ERR_IO);
739 goto done;
741 packsz += w;
743 /* Don't send too many progress privsep messages. */
744 if (packsz > last_reported_packsz + 1024) {
745 err = send_fetch_download_progress(ibuf, packsz);
746 if (err)
747 goto done;
748 last_reported_packsz = packsz;
751 err = send_fetch_download_progress(ibuf, packsz);
752 if (err)
753 goto done;
755 SHA1Final(pack_sha1, &sha1_ctx);
756 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
757 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
758 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
759 "pack file checksum mismatch");
761 done:
762 TAILQ_FOREACH(pe, &symrefs, entry) {
763 free((void *)pe->path);
764 free(pe->data);
766 got_pathlist_free(&symrefs);
767 free(have);
768 free(want);
769 free(id_str);
770 free(refname);
771 free(server_capabilities);
772 return err;
776 int
777 main(int argc, char **argv)
779 const struct got_error *err = NULL;
780 int fetchfd, packfd = -1;
781 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
782 struct imsgbuf ibuf;
783 struct imsg imsg;
784 struct got_pathlist_head have_refs;
785 struct got_pathlist_head wanted_branches;
786 struct got_pathlist_head wanted_refs;
787 struct got_pathlist_entry *pe;
788 struct got_imsg_fetch_request fetch_req;
789 struct got_imsg_fetch_have_ref href;
790 struct got_imsg_fetch_wanted_branch wbranch;
791 struct got_imsg_fetch_wanted_ref wref;
792 size_t datalen, i;
793 #if 0
794 static int attached;
795 while (!attached)
796 sleep (1);
797 #endif
799 TAILQ_INIT(&have_refs);
800 TAILQ_INIT(&wanted_branches);
801 TAILQ_INIT(&wanted_refs);
803 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
804 #ifndef PROFILE
805 /* revoke access to most system calls */
806 if (pledge("stdio recvfd", NULL) == -1) {
807 err = got_error_from_errno("pledge");
808 got_privsep_send_error(&ibuf, err);
809 return 1;
811 #endif
812 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
813 if (err) {
814 if (err->code == GOT_ERR_PRIVSEP_PIPE)
815 err = NULL;
816 goto done;
818 if (imsg.hdr.type == GOT_IMSG_STOP)
819 goto done;
820 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
821 err = got_error(GOT_ERR_PRIVSEP_MSG);
822 goto done;
824 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
825 if (datalen < sizeof(fetch_req)) {
826 err = got_error(GOT_ERR_PRIVSEP_LEN);
827 goto done;
829 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
830 fetchfd = imsg.fd;
831 imsg_free(&imsg);
833 if (fetch_req.verbosity > 0)
834 chattygot += fetch_req.verbosity;
836 for (i = 0; i < fetch_req.n_have_refs; i++) {
837 struct got_object_id *id;
838 char *refname;
840 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
841 if (err) {
842 if (err->code == GOT_ERR_PRIVSEP_PIPE)
843 err = NULL;
844 goto done;
846 if (imsg.hdr.type == GOT_IMSG_STOP)
847 goto done;
848 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
849 err = got_error(GOT_ERR_PRIVSEP_MSG);
850 goto done;
852 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
853 if (datalen < sizeof(href)) {
854 err = got_error(GOT_ERR_PRIVSEP_LEN);
855 goto done;
857 memcpy(&href, imsg.data, sizeof(href));
858 if (datalen - sizeof(href) < href.name_len) {
859 err = got_error(GOT_ERR_PRIVSEP_LEN);
860 goto done;
862 refname = malloc(href.name_len + 1);
863 if (refname == NULL) {
864 err = got_error_from_errno("malloc");
865 goto done;
867 memcpy(refname, imsg.data + sizeof(href), href.name_len);
868 refname[href.name_len] = '\0';
870 id = malloc(sizeof(*id));
871 if (id == NULL) {
872 free(refname);
873 err = got_error_from_errno("malloc");
874 goto done;
876 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
877 err = got_pathlist_append(&have_refs, refname, id);
878 if (err) {
879 free(refname);
880 free(id);
881 goto done;
884 imsg_free(&imsg);
887 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
888 char *refname;
890 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
891 if (err) {
892 if (err->code == GOT_ERR_PRIVSEP_PIPE)
893 err = NULL;
894 goto done;
896 if (imsg.hdr.type == GOT_IMSG_STOP)
897 goto done;
898 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
899 err = got_error(GOT_ERR_PRIVSEP_MSG);
900 goto done;
902 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
903 if (datalen < sizeof(wbranch)) {
904 err = got_error(GOT_ERR_PRIVSEP_LEN);
905 goto done;
907 memcpy(&wbranch, imsg.data, sizeof(wbranch));
908 if (datalen - sizeof(wbranch) < wbranch.name_len) {
909 err = got_error(GOT_ERR_PRIVSEP_LEN);
910 goto done;
912 refname = malloc(wbranch.name_len + 1);
913 if (refname == NULL) {
914 err = got_error_from_errno("malloc");
915 goto done;
917 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
918 refname[wbranch.name_len] = '\0';
920 err = got_pathlist_append(&wanted_branches, refname, NULL);
921 if (err) {
922 free(refname);
923 goto done;
926 imsg_free(&imsg);
929 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
930 char *refname;
932 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
933 if (err) {
934 if (err->code == GOT_ERR_PRIVSEP_PIPE)
935 err = NULL;
936 goto done;
938 if (imsg.hdr.type == GOT_IMSG_STOP)
939 goto done;
940 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
941 err = got_error(GOT_ERR_PRIVSEP_MSG);
942 goto done;
944 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
945 if (datalen < sizeof(wref)) {
946 err = got_error(GOT_ERR_PRIVSEP_LEN);
947 goto done;
949 memcpy(&wref, imsg.data, sizeof(wref));
950 if (datalen - sizeof(wref) < wref.name_len) {
951 err = got_error(GOT_ERR_PRIVSEP_LEN);
952 goto done;
954 refname = malloc(wref.name_len + 1);
955 if (refname == NULL) {
956 err = got_error_from_errno("malloc");
957 goto done;
959 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
960 refname[wref.name_len] = '\0';
962 err = got_pathlist_append(&wanted_refs, refname, NULL);
963 if (err) {
964 free(refname);
965 goto done;
968 imsg_free(&imsg);
971 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
972 if (err) {
973 if (err->code == GOT_ERR_PRIVSEP_PIPE)
974 err = NULL;
975 goto done;
977 if (imsg.hdr.type == GOT_IMSG_STOP)
978 goto done;
979 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
980 err = got_error(GOT_ERR_PRIVSEP_MSG);
981 goto done;
983 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
984 err = got_error(GOT_ERR_PRIVSEP_LEN);
985 goto done;
987 packfd = imsg.fd;
989 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
990 fetch_req.fetch_all_branches, &wanted_branches,
991 &wanted_refs, fetch_req.list_refs_only, &ibuf);
992 done:
993 TAILQ_FOREACH(pe, &have_refs, entry) {
994 free((char *)pe->path);
995 free(pe->data);
997 got_pathlist_free(&have_refs);
998 TAILQ_FOREACH(pe, &wanted_branches, entry)
999 free((char *)pe->path);
1000 got_pathlist_free(&wanted_branches);
1001 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1002 err = got_error_from_errno("close");
1003 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1004 err = got_error_from_errno("close");
1005 if (err != NULL)
1006 got_privsep_send_error(&ibuf, err);
1007 else
1008 err = send_fetch_done(&ibuf, pack_sha1);
1009 if (err != NULL) {
1010 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1011 got_privsep_send_error(&ibuf, err);
1014 exit(0);