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 <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <zlib.h>
34 #include <err.h>
36 #include "got_compat.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, const 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, sizeof(*refid)) == -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_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
296 struct got_object_id *have, struct got_object_id *want,
297 const char *refname, const char *id_str)
299 const struct got_error *err;
300 char *theirs = NULL, *mine = NULL;
302 if (!got_parse_sha1_digest(want->sha1, id_str)) {
303 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
304 goto done;
307 match_remote_ref(have_refs, have, refname);
308 err = send_fetch_ref(ibuf, want, refname);
309 if (err)
310 goto done;
312 if (chattygot)
313 fprintf(stderr, "%s: %s will be fetched\n",
314 getprogname(), refname);
315 if (chattygot > 1) {
316 err = got_object_id_str(&theirs, want);
317 if (err)
318 goto done;
319 err = got_object_id_str(&mine, have);
320 if (err)
321 goto done;
322 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
323 getprogname(), theirs, getprogname(), mine);
325 done:
326 free(theirs);
327 free(mine);
328 return err;
331 static const struct got_error *
332 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
333 struct got_pathlist_head *have_refs, int fetch_all_branches,
334 struct got_pathlist_head *wanted_branches,
335 struct got_pathlist_head *wanted_refs, int list_refs_only,
336 const char *worktree_branch, int no_head, struct imsgbuf *ibuf)
338 const struct got_error *err = NULL;
339 char buf[GOT_PKT_MAX];
340 char hashstr[SHA1_DIGEST_STRING_LENGTH];
341 struct got_object_id *have, *want;
342 int is_firstpkt = 1, nref = 0, refsz = 16;
343 int i, n, nwant = 0, nhave = 0, acked = 0;
344 off_t packsz = 0, last_reported_packsz = 0;
345 char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
346 char *server_capabilities = NULL, *my_capabilities = NULL;
347 const char *default_branch = NULL;
348 struct got_pathlist_head symrefs;
349 struct got_pathlist_entry *pe;
350 int sent_my_capabilites = 0, have_sidebands = 0;
351 int found_branch = 0;
352 SHA1_CTX sha1_ctx;
353 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
354 size_t sha1_buf_len = 0;
355 ssize_t w;
356 struct got_ratelimit rl;
358 TAILQ_INIT(&symrefs);
359 SHA1Init(&sha1_ctx);
360 got_ratelimit_init(&rl, 0, 500);
362 have = malloc(refsz * sizeof(have[0]));
363 if (have == NULL)
364 return got_error_from_errno("malloc");
365 want = malloc(refsz * sizeof(want[0]));
366 if (want == NULL) {
367 err = got_error_from_errno("malloc");
368 goto done;
370 while (1) {
371 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
372 if (err)
373 goto done;
374 if (n == 0)
375 break;
376 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
377 err = fetch_error(&buf[4], n - 4);
378 goto done;
380 free(id_str);
381 free(refname);
382 err = got_gitproto_parse_refline(&id_str, &refname,
383 &server_capabilities, buf, n);
384 if (err)
385 goto done;
387 if (refsz == nref + 1) {
388 struct got_object_id *h, *w;
390 refsz *= 2;
391 h = reallocarray(have, refsz, sizeof(have[0]));
392 if (h == NULL) {
393 err = got_error_from_errno("reallocarray");
394 goto done;
396 have = h;
397 w = reallocarray(want, refsz, sizeof(want[0]));
398 if (w == NULL) {
399 err = got_error_from_errno("reallocarray");
400 goto done;
402 want = w;
405 if (is_firstpkt) {
406 if (chattygot && server_capabilities[0] != '\0')
407 fprintf(stderr, "%s: server capabilities: %s\n",
408 getprogname(), server_capabilities);
409 err = got_gitproto_match_capabilities(&my_capabilities,
410 &symrefs, server_capabilities,
411 got_capabilities, nitems(got_capabilities));
412 if (err)
413 goto done;
414 if (chattygot)
415 fprintf(stderr, "%s: my capabilities:%s\n",
416 getprogname(), my_capabilities != NULL ?
417 my_capabilities : "");
418 err = send_fetch_symrefs(ibuf, &symrefs);
419 if (err)
420 goto done;
421 is_firstpkt = 0;
422 if (!fetch_all_branches) {
423 TAILQ_FOREACH(pe, &symrefs, entry) {
424 const char *name = pe->path;
425 const char *symref_target = pe->data;
426 if (strcmp(name, GOT_REF_HEAD) != 0)
427 continue;
428 default_branch = symref_target;
429 break;
432 if (default_branch)
433 continue;
435 if (strstr(refname, "^{}")) {
436 if (chattygot) {
437 fprintf(stderr, "%s: ignoring %s\n",
438 getprogname(), refname);
440 continue;
442 if (default_branch && default_id_str == NULL &&
443 strcmp(refname, default_branch) == 0) {
444 default_id_str = strdup(id_str);
445 if (default_id_str == NULL) {
446 err = got_error_from_errno("strdup");
447 goto done;
451 if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
452 err = fetch_ref(ibuf, have_refs, &have[nref],
453 &want[nref], refname, id_str);
454 if (err)
455 goto done;
456 nref++;
457 } else if (strncmp(refname, "refs/heads/", 11) == 0) {
458 if (fetch_all_branches) {
459 err = fetch_ref(ibuf, have_refs, &have[nref],
460 &want[nref], refname, id_str);
461 if (err)
462 goto done;
463 nref++;
464 found_branch = 1;
465 continue;
467 TAILQ_FOREACH(pe, wanted_branches, entry) {
468 if (match_branch(refname, pe->path))
469 break;
471 if (pe != NULL || (worktree_branch != NULL &&
472 match_branch(refname, worktree_branch))) {
473 err = fetch_ref(ibuf, have_refs, &have[nref],
474 &want[nref], refname, id_str);
475 if (err)
476 goto done;
477 nref++;
478 found_branch = 1;
479 } else if (chattygot) {
480 fprintf(stderr, "%s: ignoring %s\n",
481 getprogname(), refname);
483 } else {
484 TAILQ_FOREACH(pe, wanted_refs, entry) {
485 if (match_wanted_ref(refname, pe->path))
486 break;
488 if (pe != NULL) {
489 err = fetch_ref(ibuf, have_refs, &have[nref],
490 &want[nref], refname, id_str);
491 if (err)
492 goto done;
493 nref++;
494 } else if (chattygot) {
495 fprintf(stderr, "%s: ignoring %s\n",
496 getprogname(), refname);
501 if (list_refs_only)
502 goto done;
504 if (!found_branch && !no_head && default_branch && default_id_str &&
505 strncmp(default_branch, "refs/heads/", 11) == 0) {
506 err = fetch_ref(ibuf, have_refs, &have[nref],
507 &want[nref], default_branch, default_id_str);
508 if (err)
509 goto done;
510 nref++;
511 found_branch = 1;
514 /* Abort if we haven't found anything to fetch. */
515 if (nref == 0) {
516 struct got_pathlist_entry *pe;
517 static char msg[PATH_MAX + 33];
519 pe = TAILQ_FIRST(wanted_branches);
520 if (pe) {
521 snprintf(msg, sizeof(msg),
522 "branch \"%s\" not found on server", pe->path);
523 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
524 goto done;
527 pe = TAILQ_FIRST(wanted_refs);
528 if (pe) {
529 snprintf(msg, sizeof(msg),
530 "reference \"%s\" not found on server", pe->path);
531 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
532 goto done;
535 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
536 goto done;
539 for (i = 0; i < nref; i++) {
540 if (got_object_id_cmp(&have[i], &want[i]) == 0)
541 continue;
542 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
543 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
544 sent_my_capabilites || my_capabilities == NULL ?
545 "" : my_capabilities);
546 if (n < 0 || (size_t)n >= sizeof(buf)) {
547 err = got_error(GOT_ERR_NO_SPACE);
548 goto done;
550 err = got_pkt_writepkt(fd, buf, n, chattygot);
551 if (err)
552 goto done;
553 sent_my_capabilites = 1;
554 nwant++;
556 err = got_pkt_flushpkt(fd, chattygot);
557 if (err)
558 goto done;
560 if (nwant == 0)
561 goto done;
563 TAILQ_FOREACH(pe, have_refs, entry) {
564 struct got_object_id *id = pe->data;
565 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
566 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
567 if (n < 0 || (size_t)n >= sizeof(buf)) {
568 err = got_error(GOT_ERR_NO_SPACE);
569 goto done;
571 err = got_pkt_writepkt(fd, buf, n, chattygot);
572 if (err)
573 goto done;
574 nhave++;
577 while (nhave > 0 && !acked) {
578 struct got_object_id common_id;
580 /* The server should ACK the object IDs we need. */
581 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
582 if (err)
583 goto done;
584 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
585 err = fetch_error(&buf[4], n - 4);
586 goto done;
588 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
589 /* Server has not located our objects yet. */
590 continue;
592 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
593 strncmp(buf, "ACK ", 4) != 0) {
594 err = got_error_msg(GOT_ERR_BAD_PACKET,
595 "unexpected message from server");
596 goto done;
598 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
599 err = got_error_msg(GOT_ERR_BAD_PACKET,
600 "bad object ID in ACK packet from server");
601 goto done;
603 acked++;
606 n = strlcpy(buf, "done\n", sizeof(buf));
607 err = got_pkt_writepkt(fd, buf, n, chattygot);
608 if (err)
609 goto done;
611 if (nhave == 0) {
612 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
613 if (err)
614 goto done;
615 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
616 err = got_error_msg(GOT_ERR_BAD_PACKET,
617 "unexpected message from server");
618 goto done;
622 if (chattygot)
623 fprintf(stderr, "%s: fetching...\n", getprogname());
625 if (my_capabilities != NULL &&
626 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
627 have_sidebands = 1;
629 while (1) {
630 ssize_t r = 0;
631 int datalen = -1;
633 if (have_sidebands) {
634 err = got_pkt_readhdr(&datalen, fd, chattygot);
635 if (err)
636 goto done;
637 if (datalen <= 0)
638 break;
640 /* Read sideband channel ID (one byte). */
641 r = read(fd, buf, 1);
642 if (r == -1) {
643 err = got_error_from_errno("read");
644 goto done;
646 if (r != 1) {
647 err = got_error_msg(GOT_ERR_BAD_PACKET,
648 "short packet");
649 goto done;
651 if (datalen > sizeof(buf) - 5) {
652 err = got_error_msg(GOT_ERR_BAD_PACKET,
653 "bad packet length");
654 goto done;
656 datalen--; /* sideband ID has been read */
657 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
658 /* Read packfile data. */
659 err = got_pkt_readn(&r, fd, buf, datalen);
660 if (err)
661 goto done;
662 if (r != datalen) {
663 err = got_error_msg(GOT_ERR_BAD_PACKET,
664 "packet too short");
665 goto done;
667 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
668 err = got_pkt_readn(&r, fd, buf, datalen);
669 if (err)
670 goto done;
671 if (r != datalen) {
672 err = got_error_msg(GOT_ERR_BAD_PACKET,
673 "packet too short");
674 goto done;
676 err = fetch_progress(ibuf, buf, r);
677 if (err)
678 goto done;
679 continue;
680 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
681 err = got_pkt_readn(&r, fd, buf, datalen);
682 if (err)
683 goto done;
684 if (r != datalen) {
685 err = got_error_msg(GOT_ERR_BAD_PACKET,
686 "packet too short");
687 goto done;
689 err = fetch_error(buf, r);
690 goto done;
691 } else if (buf[0] == 'A') {
692 err = got_pkt_readn(&r, fd, buf, datalen);
693 if (err)
694 goto done;
695 if (r != datalen) {
696 err = got_error_msg(GOT_ERR_BAD_PACKET,
697 "packet too short");
698 goto done;
700 /*
701 * Git server responds with ACK after 'done'
702 * even though multi_ack is disabled?!?
703 */
704 buf[r] = '\0';
705 if (strncmp(buf, "CK ", 3) == 0)
706 continue; /* ignore */
707 err = got_error_msg(GOT_ERR_BAD_PACKET,
708 "unexpected message from server");
709 goto done;
710 } else {
711 err = got_error_msg(GOT_ERR_BAD_PACKET,
712 "unknown side-band received from server");
713 goto done;
715 } else {
716 /* No sideband channel. Every byte is packfile data. */
717 err = got_pkt_readn(&r, fd, buf, sizeof buf);
718 if (err)
719 goto done;
720 if (r <= 0)
721 break;
724 /*
725 * An expected SHA1 checksum sits at the end of the pack file.
726 * Since we don't know the file size ahead of time we have to
727 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
728 * those bytes into our SHA1 checksum computation until we
729 * know for sure that additional pack file data bytes follow.
731 * We can assume r > 0 since otherwise the loop would exit.
732 */
733 if (r < SHA1_DIGEST_LENGTH) {
734 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
735 /*
736 * If there's enough buffered + read data to
737 * fill up the buffer then shift a sufficient
738 * amount of bytes out at the front to make
739 * room, mixing those bytes into the checksum.
740 */
741 if (sha1_buf_len > 0 &&
742 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
743 size_t nshift = MIN(sha1_buf_len + r -
744 SHA1_DIGEST_LENGTH, sha1_buf_len);
745 SHA1Update(&sha1_ctx, sha1_buf, nshift);
746 memmove(sha1_buf, sha1_buf + nshift,
747 sha1_buf_len - nshift);
748 sha1_buf_len -= nshift;
751 /* Buffer potential checksum bytes. */
752 memcpy(sha1_buf + sha1_buf_len, buf, r);
753 sha1_buf_len += r;
754 } else {
755 /*
756 * Mix in previously buffered bytes which
757 * are not part of the checksum after all.
758 */
759 SHA1Update(&sha1_ctx, sha1_buf, r);
761 /* Update potential checksum buffer. */
762 memmove(sha1_buf, sha1_buf + r,
763 sha1_buf_len - r);
764 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
766 } else {
767 /* Mix in any previously buffered bytes. */
768 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
770 /* Mix in bytes read minus potential checksum bytes. */
771 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
773 /* Buffer potential checksum bytes. */
774 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
775 SHA1_DIGEST_LENGTH);
776 sha1_buf_len = SHA1_DIGEST_LENGTH;
779 /* Write packfile data to temporary pack file. */
780 w = write(packfd, buf, r);
781 if (w == -1) {
782 err = got_error_from_errno("write");
783 goto done;
785 if (w != r) {
786 err = got_error(GOT_ERR_IO);
787 goto done;
789 packsz += w;
791 /* Don't send too many progress privsep messages. */
792 if (packsz > last_reported_packsz + 1024) {
793 err = send_fetch_download_progress(ibuf, packsz, &rl);
794 if (err)
795 goto done;
796 last_reported_packsz = packsz;
799 err = send_fetch_download_progress(ibuf, packsz, NULL);
800 if (err)
801 goto done;
803 SHA1Final(pack_sha1, &sha1_ctx);
804 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
805 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
806 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
807 "pack file checksum mismatch");
809 done:
810 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
811 free(have);
812 free(want);
813 free(id_str);
814 free(default_id_str);
815 free(refname);
816 free(server_capabilities);
817 return err;
821 int
822 main(int argc, char **argv)
824 const struct got_error *err = NULL;
825 int fetchfd = -1, packfd = -1;
826 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
827 struct imsgbuf ibuf;
828 struct imsg imsg;
829 struct got_pathlist_head have_refs;
830 struct got_pathlist_head wanted_branches;
831 struct got_pathlist_head wanted_refs;
832 struct got_imsg_fetch_request fetch_req;
833 struct got_imsg_fetch_have_ref href;
834 struct got_imsg_fetch_wanted_branch wbranch;
835 struct got_imsg_fetch_wanted_ref wref;
836 size_t datalen, i;
837 char *worktree_branch = NULL;
838 #if 0
839 static int attached;
840 while (!attached)
841 sleep (1);
842 #endif
844 TAILQ_INIT(&have_refs);
845 TAILQ_INIT(&wanted_branches);
846 TAILQ_INIT(&wanted_refs);
848 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
849 #ifndef PROFILE
850 /* revoke access to most system calls */
851 if (pledge("stdio recvfd", NULL) == -1) {
852 err = got_error_from_errno("pledge");
853 got_privsep_send_error(&ibuf, err);
854 return 1;
857 /* revoke fs access */
858 if (landlock_no_fs() == -1) {
859 err = got_error_from_errno("landlock_no_fs");
860 got_privsep_send_error(&ibuf, err);
861 return 1;
863 if (cap_enter() == -1) {
864 err = got_error_from_errno("cap_enter");
865 got_privsep_send_error(&ibuf, err);
866 return 1;
868 #endif
869 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
870 if (err) {
871 if (err->code == GOT_ERR_PRIVSEP_PIPE)
872 err = NULL;
873 goto done;
875 if (imsg.hdr.type == GOT_IMSG_STOP)
876 goto done;
877 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
878 err = got_error(GOT_ERR_PRIVSEP_MSG);
879 goto done;
881 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
882 if (datalen < sizeof(fetch_req)) {
883 err = got_error(GOT_ERR_PRIVSEP_LEN);
884 goto done;
886 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
887 fetchfd = imsg.fd;
889 if (datalen != sizeof(fetch_req) +
890 fetch_req.worktree_branch_len) {
891 err = got_error(GOT_ERR_PRIVSEP_LEN);
892 goto done;
895 if (fetch_req.worktree_branch_len != 0) {
896 worktree_branch = strndup(imsg.data +
897 sizeof(fetch_req), fetch_req.worktree_branch_len);
898 if (worktree_branch == NULL) {
899 err = got_error_from_errno("strndup");
900 goto done;
904 imsg_free(&imsg);
906 if (fetch_req.verbosity > 0)
907 chattygot += fetch_req.verbosity;
909 for (i = 0; i < fetch_req.n_have_refs; i++) {
910 struct got_object_id *id;
911 char *refname;
913 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
914 if (err) {
915 if (err->code == GOT_ERR_PRIVSEP_PIPE)
916 err = NULL;
917 goto done;
919 if (imsg.hdr.type == GOT_IMSG_STOP)
920 goto done;
921 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
922 err = got_error(GOT_ERR_PRIVSEP_MSG);
923 goto done;
925 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
926 if (datalen < sizeof(href)) {
927 err = got_error(GOT_ERR_PRIVSEP_LEN);
928 goto done;
930 memcpy(&href, imsg.data, sizeof(href));
931 if (datalen - sizeof(href) < href.name_len) {
932 err = got_error(GOT_ERR_PRIVSEP_LEN);
933 goto done;
935 refname = strndup(imsg.data + sizeof(href), href.name_len);
936 if (refname == NULL) {
937 err = got_error_from_errno("strndup");
938 goto done;
941 id = malloc(sizeof(*id));
942 if (id == NULL) {
943 free(refname);
944 err = got_error_from_errno("malloc");
945 goto done;
947 memcpy(id, &href.id, sizeof(*id));
948 err = got_pathlist_append(&have_refs, refname, id);
949 if (err) {
950 free(refname);
951 free(id);
952 goto done;
955 imsg_free(&imsg);
958 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
959 char *refname;
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_WANTED_BRANCH) {
970 err = got_error(GOT_ERR_PRIVSEP_MSG);
971 goto done;
973 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
974 if (datalen < sizeof(wbranch)) {
975 err = got_error(GOT_ERR_PRIVSEP_LEN);
976 goto done;
978 memcpy(&wbranch, imsg.data, sizeof(wbranch));
979 if (datalen - sizeof(wbranch) < wbranch.name_len) {
980 err = got_error(GOT_ERR_PRIVSEP_LEN);
981 goto done;
983 refname = strndup(imsg.data + sizeof(wbranch),
984 wbranch.name_len);
985 if (refname == NULL) {
986 err = got_error_from_errno("strndup");
987 goto done;
990 err = got_pathlist_append(&wanted_branches, refname, NULL);
991 if (err) {
992 free(refname);
993 goto done;
996 imsg_free(&imsg);
999 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1000 char *refname;
1002 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1003 if (err) {
1004 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1005 err = NULL;
1006 goto done;
1008 if (imsg.hdr.type == GOT_IMSG_STOP)
1009 goto done;
1010 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1011 err = got_error(GOT_ERR_PRIVSEP_MSG);
1012 goto done;
1014 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1015 if (datalen < sizeof(wref)) {
1016 err = got_error(GOT_ERR_PRIVSEP_LEN);
1017 goto done;
1019 memcpy(&wref, imsg.data, sizeof(wref));
1020 if (datalen - sizeof(wref) < wref.name_len) {
1021 err = got_error(GOT_ERR_PRIVSEP_LEN);
1022 goto done;
1024 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1025 if (refname == NULL) {
1026 err = got_error_from_errno("strndup");
1027 goto done;
1030 err = got_pathlist_append(&wanted_refs, refname, NULL);
1031 if (err) {
1032 free(refname);
1033 goto done;
1036 imsg_free(&imsg);
1039 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1040 if (err) {
1041 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1042 err = NULL;
1043 goto done;
1045 if (imsg.hdr.type == GOT_IMSG_STOP)
1046 goto done;
1047 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1048 err = got_error(GOT_ERR_PRIVSEP_MSG);
1049 goto done;
1051 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1052 err = got_error(GOT_ERR_PRIVSEP_LEN);
1053 goto done;
1055 packfd = imsg.fd;
1057 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1058 fetch_req.fetch_all_branches, &wanted_branches,
1059 &wanted_refs, fetch_req.list_refs_only,
1060 worktree_branch, fetch_req.no_head, &ibuf);
1061 done:
1062 free(worktree_branch);
1063 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1064 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1065 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1066 err = got_error_from_errno("close");
1067 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1068 err = got_error_from_errno("close");
1069 if (err != NULL)
1070 got_privsep_send_error(&ibuf, err);
1071 else
1072 err = send_fetch_done(&ibuf, pack_sha1);
1073 if (err != NULL) {
1074 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1075 got_privsep_send_error(&ibuf, err);
1078 exit(0);