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"
53 #include "got_lib_ratelimit.h"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 #ifndef nitems
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 #endif
63 struct got_object *indexed;
64 static int chattygot;
66 static const struct got_capability got_capabilities[] = {
67 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
68 { GOT_CAPA_OFS_DELTA, NULL },
69 { GOT_CAPA_SIDE_BAND_64K, NULL },
70 };
72 static void
73 match_remote_ref(struct got_pathlist_head *have_refs,
74 struct got_object_id *my_id, char *refname)
75 {
76 struct got_pathlist_entry *pe;
78 /* XXX zero-hash signifies we don't have this ref;
79 * we should use a flag instead */
80 memset(my_id, 0, sizeof(*my_id));
82 TAILQ_FOREACH(pe, have_refs, entry) {
83 struct got_object_id *id = pe->data;
84 if (strcmp(pe->path, refname) == 0) {
85 memcpy(my_id, id, sizeof(*my_id));
86 break;
87 }
88 }
89 }
91 static int
92 match_branch(const char *branch, const char *wanted_branch)
93 {
94 if (strncmp(branch, "refs/heads/", 11) != 0)
95 return 0;
97 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
98 wanted_branch += 11;
100 return (strcmp(branch + 11, wanted_branch) == 0);
103 static int
104 match_wanted_ref(const char *refname, const char *wanted_ref)
106 if (strncmp(refname, "refs/", 5) != 0)
107 return 0;
108 refname += 5;
110 /*
111 * Prevent fetching of references that won't make any
112 * sense outside of the remote repository's context.
113 */
114 if (strncmp(refname, "got/", 4) == 0)
115 return 0;
116 if (strncmp(refname, "remotes/", 8) == 0)
117 return 0;
119 if (strncmp(wanted_ref, "refs/", 5) == 0)
120 wanted_ref += 5;
122 /* Allow prefix match. */
123 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
124 return 1;
126 /* Allow exact match. */
127 return (strcmp(refname, wanted_ref) == 0);
130 static const struct got_error *
131 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
133 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
134 return got_error(GOT_ERR_NO_SPACE);
136 if (msglen == 0)
137 return NULL;
139 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
140 msg, msglen) == -1)
141 return got_error_from_errno(
142 "imsg_compose FETCH_SERVER_PROGRESS");
144 return got_privsep_flush_imsg(ibuf);
147 static const struct got_error *
148 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
149 struct got_ratelimit *rl)
151 const struct got_error *err;
152 int elapsed = 0;
154 if (rl) {
155 err = got_ratelimit_check(&elapsed, rl);
156 if (err || !elapsed)
157 return err;
160 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
161 &bytes, sizeof(bytes)) == -1)
162 return got_error_from_errno(
163 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
165 return got_privsep_flush_imsg(ibuf);
168 static const struct got_error *
169 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
171 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
172 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
173 return got_error_from_errno("imsg_compose FETCH");
174 return got_privsep_flush_imsg(ibuf);
177 static const struct got_error *
178 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
180 size_t i;
182 if (len == 0)
183 return NULL;
185 /*
186 * Truncate messages which exceed the maximum imsg payload size.
187 * Server may send up to 64k.
188 */
189 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
192 /* Only allow printable ASCII. */
193 for (i = 0; i < len; i++) {
194 if (isprint((unsigned char)buf[i]) ||
195 isspace((unsigned char)buf[i]))
196 continue;
197 return got_error_msg(GOT_ERR_BAD_PACKET,
198 "non-printable progress message received from server");
201 return send_fetch_server_progress(ibuf, buf, len);
204 static const struct got_error *
205 fetch_error(const char *buf, size_t len)
207 static char msg[1024];
208 size_t i;
210 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
211 if (!isprint((unsigned char)buf[i]))
212 return got_error_msg(GOT_ERR_BAD_PACKET,
213 "non-printable error message received from server");
214 msg[i] = buf[i];
216 msg[i] = '\0';
217 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
220 static const struct got_error *
221 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
223 struct ibuf *wbuf;
224 size_t len, nsymrefs = 0;
225 struct got_pathlist_entry *pe;
227 len = sizeof(struct got_imsg_fetch_symrefs);
228 TAILQ_FOREACH(pe, symrefs, entry) {
229 const char *target = pe->data;
230 len += sizeof(struct got_imsg_fetch_symref) +
231 pe->path_len + strlen(target);
232 nsymrefs++;
235 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
236 return got_error(GOT_ERR_NO_SPACE);
238 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
239 if (wbuf == NULL)
240 return got_error_from_errno("imsg_create FETCH_SYMREFS");
242 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
243 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
244 return got_error_from_errno("imsg_add FETCH_SYMREFS");
246 TAILQ_FOREACH(pe, symrefs, entry) {
247 const char *name = pe->path;
248 size_t name_len = pe->path_len;
249 const char *target = pe->data;
250 size_t target_len = strlen(target);
252 /* Keep in sync with struct got_imsg_fetch_symref definition! */
253 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
254 return got_error_from_errno("imsg_add FETCH_SYMREFS");
255 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
256 return got_error_from_errno("imsg_add FETCH_SYMREFS");
257 if (imsg_add(wbuf, name, name_len) == -1)
258 return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 if (imsg_add(wbuf, target, target_len) == -1)
260 return got_error_from_errno("imsg_add FETCH_SYMREFS");
263 wbuf->fd = -1;
264 imsg_close(ibuf, wbuf);
265 return got_privsep_flush_imsg(ibuf);
268 static const struct got_error *
269 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
270 const char *refname)
272 struct ibuf *wbuf;
273 size_t len, reflen = strlen(refname);
275 len = sizeof(struct got_imsg_fetch_ref) + reflen;
276 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
277 return got_error(GOT_ERR_NO_SPACE);
279 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
280 if (wbuf == NULL)
281 return got_error_from_errno("imsg_create FETCH_REF");
283 /* Keep in sync with struct got_imsg_fetch_ref definition! */
284 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
285 return got_error_from_errno("imsg_add FETCH_REF");
286 if (imsg_add(wbuf, refname, reflen) == -1)
287 return got_error_from_errno("imsg_add FETCH_REF");
289 wbuf->fd = -1;
290 imsg_close(ibuf, wbuf);
291 return got_privsep_flush_imsg(ibuf);
294 static const struct got_error *
295 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
296 struct got_pathlist_head *have_refs, int fetch_all_branches,
297 struct got_pathlist_head *wanted_branches,
298 struct got_pathlist_head *wanted_refs, int list_refs_only,
299 struct imsgbuf *ibuf)
301 const struct got_error *err = NULL;
302 char buf[GOT_PKT_MAX];
303 char hashstr[SHA1_DIGEST_STRING_LENGTH];
304 struct got_object_id *have, *want;
305 int is_firstpkt = 1, nref = 0, refsz = 16;
306 int i, n, nwant = 0, nhave = 0, acked = 0;
307 off_t packsz = 0, last_reported_packsz = 0;
308 char *id_str = NULL, *refname = NULL;
309 char *server_capabilities = NULL, *my_capabilities = NULL;
310 const char *default_branch = NULL;
311 struct got_pathlist_head symrefs;
312 struct got_pathlist_entry *pe;
313 int sent_my_capabilites = 0, have_sidebands = 0;
314 int found_branch = 0;
315 SHA1_CTX sha1_ctx;
316 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
317 size_t sha1_buf_len = 0;
318 ssize_t w;
319 struct got_ratelimit rl;
321 TAILQ_INIT(&symrefs);
322 SHA1Init(&sha1_ctx);
323 got_ratelimit_init(&rl, 0, 500);
325 have = malloc(refsz * sizeof(have[0]));
326 if (have == NULL)
327 return got_error_from_errno("malloc");
328 want = malloc(refsz * sizeof(want[0]));
329 if (want == NULL) {
330 err = got_error_from_errno("malloc");
331 goto done;
333 while (1) {
334 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
335 if (err)
336 goto done;
337 if (n == 0)
338 break;
339 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
340 err = fetch_error(&buf[4], n - 4);
341 goto done;
343 free(id_str);
344 free(refname);
345 err = got_gitproto_parse_refline(&id_str, &refname,
346 &server_capabilities, buf, n);
347 if (err)
348 goto done;
349 if (is_firstpkt) {
350 if (chattygot && server_capabilities[0] != '\0')
351 fprintf(stderr, "%s: server capabilities: %s\n",
352 getprogname(), server_capabilities);
353 err = got_gitproto_match_capabilities(&my_capabilities,
354 &symrefs, server_capabilities,
355 got_capabilities, nitems(got_capabilities));
356 if (err)
357 goto done;
358 if (chattygot)
359 fprintf(stderr, "%s: my capabilities:%s\n",
360 getprogname(), my_capabilities != NULL ?
361 my_capabilities : "");
362 err = send_fetch_symrefs(ibuf, &symrefs);
363 if (err)
364 goto done;
365 is_firstpkt = 0;
366 if (!fetch_all_branches) {
367 TAILQ_FOREACH(pe, &symrefs, entry) {
368 const char *name = pe->path;
369 const char *symref_target = pe->data;
370 if (strcmp(name, GOT_REF_HEAD) != 0)
371 continue;
372 default_branch = symref_target;
373 break;
376 if (default_branch)
377 continue;
379 if (strstr(refname, "^{}")) {
380 if (chattygot) {
381 fprintf(stderr, "%s: ignoring %s\n",
382 getprogname(), refname);
384 continue;
387 if (strncmp(refname, "refs/heads/", 11) == 0) {
388 if (fetch_all_branches || list_refs_only) {
389 found_branch = 1;
390 } else if (!TAILQ_EMPTY(wanted_branches)) {
391 TAILQ_FOREACH(pe, wanted_branches, entry) {
392 if (match_branch(refname, pe->path))
393 break;
395 if (pe == NULL) {
396 if (chattygot) {
397 fprintf(stderr,
398 "%s: ignoring %s\n",
399 getprogname(), refname);
401 continue;
403 found_branch = 1;
404 } else if (default_branch != NULL) {
405 if (!match_branch(refname, default_branch)) {
406 if (chattygot) {
407 fprintf(stderr,
408 "%s: ignoring %s\n",
409 getprogname(), refname);
411 continue;
413 found_branch = 1;
415 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
416 if (!TAILQ_EMPTY(wanted_refs)) {
417 TAILQ_FOREACH(pe, wanted_refs, entry) {
418 if (match_wanted_ref(refname, pe->path))
419 break;
421 if (pe == NULL) {
422 if (chattygot) {
423 fprintf(stderr,
424 "%s: ignoring %s\n",
425 getprogname(), refname);
427 continue;
429 found_branch = 1;
430 } else if (!list_refs_only) {
431 if (chattygot) {
432 fprintf(stderr, "%s: ignoring %s\n",
433 getprogname(), refname);
435 continue;
439 if (refsz == nref + 1) {
440 refsz *= 2;
441 have = reallocarray(have, refsz, sizeof(have[0]));
442 if (have == NULL) {
443 err = got_error_from_errno("reallocarray");
444 goto done;
446 want = reallocarray(want, refsz, sizeof(want[0]));
447 if (want == NULL) {
448 err = got_error_from_errno("reallocarray");
449 goto done;
452 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
453 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
454 goto done;
456 match_remote_ref(have_refs, &have[nref], refname);
457 err = send_fetch_ref(ibuf, &want[nref], refname);
458 if (err)
459 goto done;
461 if (chattygot)
462 fprintf(stderr, "%s: %s will be fetched\n",
463 getprogname(), refname);
464 if (chattygot > 1) {
465 char *theirs, *mine;
466 err = got_object_id_str(&theirs, &want[nref]);
467 if (err)
468 goto done;
469 err = got_object_id_str(&mine, &have[nref]);
470 if (err) {
471 free(theirs);
472 goto done;
474 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
475 getprogname(), theirs, getprogname(), mine);
476 free(theirs);
477 free(mine);
479 nref++;
482 if (list_refs_only)
483 goto done;
485 /* Abort if we haven't found any branch to fetch. */
486 if (!found_branch) {
487 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
488 goto done;
491 for (i = 0; i < nref; i++) {
492 if (got_object_id_cmp(&have[i], &want[i]) == 0)
493 continue;
494 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
495 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
496 sent_my_capabilites || my_capabilities == NULL ?
497 "" : my_capabilities);
498 if (n < 0 || (size_t)n >= sizeof(buf)) {
499 err = got_error(GOT_ERR_NO_SPACE);
500 goto done;
502 err = got_pkt_writepkt(fd, buf, n, chattygot);
503 if (err)
504 goto done;
505 sent_my_capabilites = 1;
506 nwant++;
508 err = got_pkt_flushpkt(fd, chattygot);
509 if (err)
510 goto done;
512 if (nwant == 0)
513 goto done;
515 TAILQ_FOREACH(pe, have_refs, entry) {
516 struct got_object_id *id = pe->data;
517 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
518 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
519 if (n < 0 || (size_t)n >= sizeof(buf)) {
520 err = got_error(GOT_ERR_NO_SPACE);
521 goto done;
523 err = got_pkt_writepkt(fd, buf, n, chattygot);
524 if (err)
525 goto done;
526 nhave++;
529 while (nhave > 0 && !acked) {
530 struct got_object_id common_id;
532 /* The server should ACK the object IDs we need. */
533 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
534 if (err)
535 goto done;
536 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
537 err = fetch_error(&buf[4], n - 4);
538 goto done;
540 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
541 /* Server has not located our objects yet. */
542 continue;
544 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
545 strncmp(buf, "ACK ", 4) != 0) {
546 err = got_error_msg(GOT_ERR_BAD_PACKET,
547 "unexpected message from server");
548 goto done;
550 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
551 err = got_error_msg(GOT_ERR_BAD_PACKET,
552 "bad object ID in ACK packet from server");
553 goto done;
555 acked++;
558 n = strlcpy(buf, "done\n", sizeof(buf));
559 err = got_pkt_writepkt(fd, buf, n, chattygot);
560 if (err)
561 goto done;
563 if (nhave == 0) {
564 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
565 if (err)
566 goto done;
567 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
568 err = got_error_msg(GOT_ERR_BAD_PACKET,
569 "unexpected message from server");
570 goto done;
574 if (chattygot)
575 fprintf(stderr, "%s: fetching...\n", getprogname());
577 if (my_capabilities != NULL &&
578 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
579 have_sidebands = 1;
581 while (1) {
582 ssize_t r = 0;
583 int datalen = -1;
585 if (have_sidebands) {
586 err = got_pkt_readhdr(&datalen, fd, chattygot);
587 if (err)
588 goto done;
589 if (datalen <= 0)
590 break;
592 /* Read sideband channel ID (one byte). */
593 r = read(fd, buf, 1);
594 if (r == -1) {
595 err = got_error_from_errno("read");
596 goto done;
598 if (r != 1) {
599 err = got_error_msg(GOT_ERR_BAD_PACKET,
600 "short packet");
601 goto done;
603 if (datalen > sizeof(buf) - 5) {
604 err = got_error_msg(GOT_ERR_BAD_PACKET,
605 "bad packet length");
606 goto done;
608 datalen--; /* sideband ID has been read */
609 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
610 /* Read packfile data. */
611 err = got_pkt_readn(&r, fd, buf, datalen);
612 if (err)
613 goto done;
614 if (r != datalen) {
615 err = got_error_msg(GOT_ERR_BAD_PACKET,
616 "packet too short");
617 goto done;
619 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
620 err = got_pkt_readn(&r, fd, buf, datalen);
621 if (err)
622 goto done;
623 if (r != datalen) {
624 err = got_error_msg(GOT_ERR_BAD_PACKET,
625 "packet too short");
626 goto done;
628 err = fetch_progress(ibuf, buf, r);
629 if (err)
630 goto done;
631 continue;
632 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
633 err = got_pkt_readn(&r, fd, buf, datalen);
634 if (err)
635 goto done;
636 if (r != datalen) {
637 err = got_error_msg(GOT_ERR_BAD_PACKET,
638 "packet too short");
639 goto done;
641 err = fetch_error(buf, r);
642 goto done;
643 } else if (buf[0] == 'A') {
644 err = got_pkt_readn(&r, fd, buf, datalen);
645 if (err)
646 goto done;
647 if (r != datalen) {
648 err = got_error_msg(GOT_ERR_BAD_PACKET,
649 "packet too short");
650 goto done;
652 /*
653 * Git server responds with ACK after 'done'
654 * even though multi_ack is disabled?!?
655 */
656 buf[r] = '\0';
657 if (strncmp(buf, "CK ", 3) == 0)
658 continue; /* ignore */
659 err = got_error_msg(GOT_ERR_BAD_PACKET,
660 "unexpected message from server");
661 goto done;
662 } else {
663 err = got_error_msg(GOT_ERR_BAD_PACKET,
664 "unknown side-band received from server");
665 goto done;
667 } else {
668 /* No sideband channel. Every byte is packfile data. */
669 err = got_pkt_readn(&r, fd, buf, sizeof buf);
670 if (err)
671 goto done;
672 if (r <= 0)
673 break;
676 /*
677 * An expected SHA1 checksum sits at the end of the pack file.
678 * Since we don't know the file size ahead of time we have to
679 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
680 * those bytes into our SHA1 checksum computation until we
681 * know for sure that additional pack file data bytes follow.
683 * We can assume r > 0 since otherwise the loop would exit.
684 */
685 if (r < SHA1_DIGEST_LENGTH) {
686 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
687 /*
688 * If there's enough buffered + read data to
689 * fill up the buffer then shift a sufficient
690 * amount of bytes out at the front to make
691 * room, mixing those bytes into the checksum.
692 */
693 if (sha1_buf_len > 0 &&
694 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
695 size_t nshift = MIN(sha1_buf_len + r -
696 SHA1_DIGEST_LENGTH, sha1_buf_len);
697 SHA1Update(&sha1_ctx, sha1_buf, nshift);
698 memmove(sha1_buf, sha1_buf + nshift,
699 sha1_buf_len - nshift);
700 sha1_buf_len -= nshift;
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, &rl);
746 if (err)
747 goto done;
748 last_reported_packsz = packsz;
751 err = send_fetch_download_progress(ibuf, packsz, NULL);
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 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
763 free(have);
764 free(want);
765 free(id_str);
766 free(refname);
767 free(server_capabilities);
768 return err;
772 int
773 main(int argc, char **argv)
775 const struct got_error *err = NULL;
776 int fetchfd = -1, packfd = -1;
777 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
778 struct imsgbuf ibuf;
779 struct imsg imsg;
780 struct got_pathlist_head have_refs;
781 struct got_pathlist_head wanted_branches;
782 struct got_pathlist_head wanted_refs;
783 struct got_imsg_fetch_request fetch_req;
784 struct got_imsg_fetch_have_ref href;
785 struct got_imsg_fetch_wanted_branch wbranch;
786 struct got_imsg_fetch_wanted_ref wref;
787 size_t datalen, i;
788 #if 0
789 static int attached;
790 while (!attached)
791 sleep (1);
792 #endif
794 TAILQ_INIT(&have_refs);
795 TAILQ_INIT(&wanted_branches);
796 TAILQ_INIT(&wanted_refs);
798 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
799 #ifndef PROFILE
800 /* revoke access to most system calls */
801 if (pledge("stdio recvfd", NULL) == -1) {
802 err = got_error_from_errno("pledge");
803 got_privsep_send_error(&ibuf, err);
804 return 1;
806 #endif
807 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
808 if (err) {
809 if (err->code == GOT_ERR_PRIVSEP_PIPE)
810 err = NULL;
811 goto done;
813 if (imsg.hdr.type == GOT_IMSG_STOP)
814 goto done;
815 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
816 err = got_error(GOT_ERR_PRIVSEP_MSG);
817 goto done;
819 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
820 if (datalen < sizeof(fetch_req)) {
821 err = got_error(GOT_ERR_PRIVSEP_LEN);
822 goto done;
824 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
825 fetchfd = imsg.fd;
826 imsg_free(&imsg);
828 if (fetch_req.verbosity > 0)
829 chattygot += fetch_req.verbosity;
831 for (i = 0; i < fetch_req.n_have_refs; i++) {
832 struct got_object_id *id;
833 char *refname;
835 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
836 if (err) {
837 if (err->code == GOT_ERR_PRIVSEP_PIPE)
838 err = NULL;
839 goto done;
841 if (imsg.hdr.type == GOT_IMSG_STOP)
842 goto done;
843 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
844 err = got_error(GOT_ERR_PRIVSEP_MSG);
845 goto done;
847 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
848 if (datalen < sizeof(href)) {
849 err = got_error(GOT_ERR_PRIVSEP_LEN);
850 goto done;
852 memcpy(&href, imsg.data, sizeof(href));
853 if (datalen - sizeof(href) < href.name_len) {
854 err = got_error(GOT_ERR_PRIVSEP_LEN);
855 goto done;
857 refname = strndup(imsg.data + sizeof(href), href.name_len);
858 if (refname == NULL) {
859 err = got_error_from_errno("strndup");
860 goto done;
863 id = malloc(sizeof(*id));
864 if (id == NULL) {
865 free(refname);
866 err = got_error_from_errno("malloc");
867 goto done;
869 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
870 err = got_pathlist_append(&have_refs, refname, id);
871 if (err) {
872 free(refname);
873 free(id);
874 goto done;
877 imsg_free(&imsg);
880 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
881 char *refname;
883 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
884 if (err) {
885 if (err->code == GOT_ERR_PRIVSEP_PIPE)
886 err = NULL;
887 goto done;
889 if (imsg.hdr.type == GOT_IMSG_STOP)
890 goto done;
891 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
892 err = got_error(GOT_ERR_PRIVSEP_MSG);
893 goto done;
895 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
896 if (datalen < sizeof(wbranch)) {
897 err = got_error(GOT_ERR_PRIVSEP_LEN);
898 goto done;
900 memcpy(&wbranch, imsg.data, sizeof(wbranch));
901 if (datalen - sizeof(wbranch) < wbranch.name_len) {
902 err = got_error(GOT_ERR_PRIVSEP_LEN);
903 goto done;
905 refname = strndup(imsg.data + sizeof(wbranch),
906 wbranch.name_len);
907 if (refname == NULL) {
908 err = got_error_from_errno("strndup");
909 goto done;
912 err = got_pathlist_append(&wanted_branches, refname, NULL);
913 if (err) {
914 free(refname);
915 goto done;
918 imsg_free(&imsg);
921 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
922 char *refname;
924 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
925 if (err) {
926 if (err->code == GOT_ERR_PRIVSEP_PIPE)
927 err = NULL;
928 goto done;
930 if (imsg.hdr.type == GOT_IMSG_STOP)
931 goto done;
932 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
933 err = got_error(GOT_ERR_PRIVSEP_MSG);
934 goto done;
936 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
937 if (datalen < sizeof(wref)) {
938 err = got_error(GOT_ERR_PRIVSEP_LEN);
939 goto done;
941 memcpy(&wref, imsg.data, sizeof(wref));
942 if (datalen - sizeof(wref) < wref.name_len) {
943 err = got_error(GOT_ERR_PRIVSEP_LEN);
944 goto done;
946 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
947 if (refname == NULL) {
948 err = got_error_from_errno("strndup");
949 goto done;
952 err = got_pathlist_append(&wanted_refs, refname, NULL);
953 if (err) {
954 free(refname);
955 goto done;
958 imsg_free(&imsg);
961 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
962 if (err) {
963 if (err->code == GOT_ERR_PRIVSEP_PIPE)
964 err = NULL;
965 goto done;
967 if (imsg.hdr.type == GOT_IMSG_STOP)
968 goto done;
969 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
970 err = got_error(GOT_ERR_PRIVSEP_MSG);
971 goto done;
973 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
974 err = got_error(GOT_ERR_PRIVSEP_LEN);
975 goto done;
977 packfd = imsg.fd;
979 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
980 fetch_req.fetch_all_branches, &wanted_branches,
981 &wanted_refs, fetch_req.list_refs_only, &ibuf);
982 done:
983 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
984 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
985 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
986 err = got_error_from_errno("close");
987 if (packfd != -1 && close(packfd) == -1 && err == NULL)
988 err = got_error_from_errno("close");
989 if (err != NULL)
990 got_privsep_send_error(&ibuf, err);
991 else
992 err = send_fetch_done(&ibuf, pack_sha1);
993 if (err != NULL) {
994 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
995 got_privsep_send_error(&ibuf, err);
998 exit(0);