2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
22 #include <sys/syslimits.h>
38 #include "got_error.h"
39 #include "got_object.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"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 struct got_object *indexed;
58 static struct got_object_id zhash = {.sha1={0}};
60 static const struct got_error *
61 readn(ssize_t *off, int fd, void *buf, size_t n)
67 r = read(fd, buf + *off, n - *off);
69 return got_error_from_errno("read");
77 static const struct got_error *
83 fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
85 w = write(fd, "0000", 4);
87 return got_error_from_errno("write");
89 return got_error(GOT_ERR_IO);
94 * Packet header contains a 4-byte hexstring which specifies the length
95 * of data which follows.
97 static const struct got_error *
98 read_pkthdr(int *datalen, int fd)
100 static const struct got_error *err = NULL;
109 err = readn(&r, fd, lenstr, 4);
113 /* implicit "0000" */
115 fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
119 return got_error_msg(GOT_ERR_BAD_PACKET,
120 "wrong packet header length");
123 for (i = 0; i < 4; i++) {
124 if (!isprint((unsigned char)lenstr[i]))
125 return got_error_msg(GOT_ERR_BAD_PACKET,
126 "unprintable character in packet length field");
128 for (i = 0; i < 4; i++) {
129 if (!isxdigit((unsigned char)lenstr[i])) {
131 fprintf(stderr, "%s: bad length: '%s'\n",
132 getprogname(), lenstr);
133 return got_error_msg(GOT_ERR_BAD_PACKET,
134 "packet length not specified in hex");
138 len = strtol(lenstr, &e, 16);
139 if (lenstr[0] == '\0' || *e != '\0')
140 return got_error(GOT_ERR_BAD_PACKET);
141 if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
142 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
143 if (len > INT_MAX || len < INT_MIN)
144 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
149 return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
156 static const struct got_error *
157 readpkt(int *outlen, int fd, char *buf, int buflen)
159 const struct got_error *err = NULL;
163 err = read_pkthdr(&datalen, fd);
167 if (datalen > buflen)
168 return got_error(GOT_ERR_NO_SPACE);
170 err = readn(&n, fd, buf, datalen);
174 return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
177 fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
178 for (i = 0; i < n; i++) {
180 fputc(buf[i], stderr);
182 fprintf(stderr, "[0x%.2x]", buf[i]);
191 static const struct got_error *
192 writepkt(int fd, char *buf, int nbuf)
198 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
199 return got_error(GOT_ERR_NO_SPACE);
200 w = write(fd, len, 4);
202 return got_error_from_errno("write");
204 return got_error(GOT_ERR_IO);
205 w = write(fd, buf, nbuf);
207 return got_error_from_errno("write");
209 return got_error(GOT_ERR_IO);
211 fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
212 for (i = 0; i < nbuf; i++) {
214 fputc(buf[i], stderr);
216 fprintf(stderr, "[0x%.2x]", buf[i]);
224 match_remote_ref(struct got_pathlist_head *have_refs,
225 struct got_object_id *my_id, char *refname)
227 struct got_pathlist_entry *pe;
229 /* XXX zero-hash signifies we don't have this ref;
230 * we should use a flag instead */
231 memset(my_id, 0, sizeof(*my_id));
233 TAILQ_FOREACH(pe, have_refs, entry) {
234 struct got_object_id *id = pe->data;
235 if (strcmp(pe->path, refname) == 0) {
236 memcpy(my_id, id, sizeof(*my_id));
243 match_branch(const char *branch, const char *wanted_branch)
245 if (strncmp(branch, "refs/heads/", 11) != 0)
248 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
251 return (strcmp(branch + 11, wanted_branch) == 0);
254 static const struct got_error *
255 tokenize_refline(char **tokens, char *line, int len, int maxtokens)
257 const struct got_error *err = NULL;
261 for (i = 0; i < maxtokens; i++)
264 for (i = 0; n < len && i < maxtokens; i++) {
265 while (isspace(*line)) {
270 while (*line != '\0' &&
271 (!isspace(*line) || i == maxtokens - 1)) {
275 tokens[i] = strndup(p, line - p);
276 if (tokens[i] == NULL) {
277 err = got_error_from_errno("strndup");
280 /* Skip \0 field-delimiter at end of token. */
281 while (line[0] == '\0' && n < len) {
287 err = got_error(GOT_ERR_NOT_REF);
291 for (j = 0; j < i; j++)
298 static const struct got_error *
299 parse_refline(char **id_str, char **refname, char **server_capabilities,
302 const struct got_error *err = NULL;
305 err = tokenize_refline(tokens, line, len, nitems(tokens));
312 *refname = tokens[1];
314 *server_capabilities = tokens[2];
319 #define GOT_CAPA_AGENT "agent"
320 #define GOT_CAPA_OFS_DELTA "ofs-delta"
321 #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
323 #define GOT_SIDEBAND_PACKFILE_DATA 1
324 #define GOT_SIDEBAND_PROGRESS_INFO 2
325 #define GOT_SIDEBAND_ERROR_INFO 3
328 struct got_capability {
332 static const struct got_capability got_capabilities[] = {
333 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
334 { GOT_CAPA_OFS_DELTA, NULL },
335 { GOT_CAPA_SIDE_BAND_64K, NULL },
338 static const struct got_error *
339 match_capability(char **my_capabilities, const char *capa,
340 const struct got_capability *mycapa)
345 equalsign = strchr(capa, '=');
347 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
350 if (strcmp(capa, mycapa->key) != 0)
354 if (asprintf(&s, "%s %s%s%s",
355 *my_capabilities != NULL ? *my_capabilities : "",
357 mycapa->value != NULL ? "=" : "",
358 mycapa->value != NULL? mycapa->value : "") == -1)
359 return got_error_from_errno("asprintf");
361 free(*my_capabilities);
362 *my_capabilities = s;
366 static const struct got_error *
367 add_symref(struct got_pathlist_head *symrefs, char *capa)
369 const struct got_error *err = NULL;
370 char *colon, *name = NULL, *target = NULL;
372 /* Need at least "A:B" */
373 if (strlen(capa) < 3)
376 colon = strchr(capa, ':');
383 return got_error_from_errno("strdup");
385 target = strdup(colon + 1);
386 if (target == NULL) {
387 err = got_error_from_errno("strdup");
391 /* We can't validate the ref itself here. The main process will. */
392 err = got_pathlist_append(symrefs, name, target);
401 static const struct got_error *
402 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
403 char *server_capabilities)
405 const struct got_error *err = NULL;
406 char *capa, *equalsign;
409 *my_capabilities = NULL;
411 capa = strsep(&server_capabilities, " ");
415 equalsign = strchr(capa, '=');
416 if (equalsign != NULL &&
417 strncmp(capa, "symref", equalsign - capa) == 0) {
418 err = add_symref(symrefs, equalsign + 1);
424 for (i = 0; i < nitems(got_capabilities); i++) {
425 err = match_capability(my_capabilities,
426 capa, &got_capabilities[i]);
432 if (*my_capabilities == NULL) {
433 *my_capabilities = strdup("");
434 if (*my_capabilities == NULL)
435 err = got_error_from_errno("strdup");
440 static const struct got_error *
441 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
449 * Truncate messages which exceed the maximum imsg payload size.
450 * Server may send up to 64k.
452 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
453 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
455 /* Only allow printable ASCII. */
456 for (i = 0; i < len; i++) {
457 if (isprint((unsigned char)buf[i]) ||
458 isspace((unsigned char)buf[i]))
460 return got_error_msg(GOT_ERR_BAD_PACKET,
461 "non-printable progress message received from server");
464 return got_privsep_send_fetch_server_progress(ibuf, buf, len);
467 static const struct got_error *
468 fetch_error(const char *buf, size_t len)
470 static char msg[1024];
473 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
474 if (!isprint(buf[i]))
475 return got_error_msg(GOT_ERR_BAD_PACKET,
476 "non-printable error message received from server");
480 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
483 static const struct got_error *
484 fetch_pack(int fd, int packfd, struct got_object_id *packid,
485 struct got_pathlist_head *have_refs, int fetch_all_branches,
486 struct imsgbuf *ibuf)
488 const struct got_error *err = NULL;
489 char buf[GOT_FETCH_PKTMAX];
490 char hashstr[SHA1_DIGEST_STRING_LENGTH];
491 struct got_object_id *have, *want;
492 int is_firstpkt = 1, nref = 0, refsz = 16;
493 int i, n, nwant = 0, nhave = 0, acked = 0;
494 off_t packsz = 0, last_reported_packsz = 0;
495 char *id_str = NULL, *refname = NULL;
496 char *server_capabilities = NULL, *my_capabilities = NULL;
497 const char *default_branch = NULL;
498 struct got_pathlist_head symrefs;
499 struct got_pathlist_entry *pe;
500 int sent_my_capabilites = 0, have_sidebands = 0;
501 int found_branch = 0;
503 TAILQ_INIT(&symrefs);
505 have = malloc(refsz * sizeof(have[0]));
507 return got_error_from_errno("malloc");
508 want = malloc(refsz * sizeof(want[0]));
510 err = got_error_from_errno("malloc");
514 fprintf(stderr, "%s: starting fetch\n", getprogname());
516 err = readpkt(&n, fd, buf, sizeof(buf));
521 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
522 err = fetch_error(&buf[4], n - 4);
525 err = parse_refline(&id_str, &refname, &server_capabilities,
530 if (chattygot && server_capabilities[0] != '\0')
531 fprintf(stderr, "%s: server capabilities: %s\n",
532 getprogname(), server_capabilities);
533 err = match_capabilities(&my_capabilities, &symrefs,
534 server_capabilities);
538 fprintf(stderr, "%s: my capabilities: %s\n",
539 getprogname(), my_capabilities);
540 err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
544 if (!fetch_all_branches) {
545 TAILQ_FOREACH(pe, &symrefs, entry) {
546 const char *name = pe->path;
547 const char *symref_target = pe->data;
548 if (strcmp(name, GOT_REF_HEAD) != 0)
550 default_branch = symref_target;
556 if (strstr(refname, "^{}"))
560 fprintf(stderr, "%s: discovered remote ref %s\n",
561 getprogname(), refname);
563 if (strncmp(refname, "refs/heads/", 11) == 0) {
564 if (default_branch != NULL &&
565 !match_branch(refname, default_branch))
568 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
570 fprintf(stderr, "%s: ignoring '%s' which is "
571 "neither a branch nor a tag\n",
572 getprogname(), refname);
577 if (refsz == nref + 1) {
579 have = reallocarray(have, refsz, sizeof(have[0]));
581 err = got_error_from_errno("reallocarray");
584 want = reallocarray(want, refsz, sizeof(want[0]));
586 err = got_error_from_errno("reallocarray");
590 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
591 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
594 match_remote_ref(have_refs, &have[nref], refname);
595 err = got_privsep_send_fetch_ref(ibuf, &want[nref],
602 err = got_object_id_str(&theirs, &want[nref]);
605 err = got_object_id_str(&mine, &have[nref]);
610 fprintf(stderr, "%s: %s will be fetched\n",
611 getprogname(), refname);
612 fprintf(stderr, "%s: theirs=%s\n%s: mine=%s\n",
613 getprogname(), theirs, getprogname(), mine);
620 /* Abort if we haven't found any branch to fetch. */
622 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
626 for (i = 0; i < nref; i++) {
627 if (got_object_id_cmp(&have[i], &want[i]) == 0)
629 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
630 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
631 sent_my_capabilites ? "" : my_capabilities);
632 if (n >= sizeof(buf)) {
633 err = got_error(GOT_ERR_NO_SPACE);
636 err = writepkt(fd, buf, n);
639 sent_my_capabilites = 1;
649 for (i = 0; i < nref; i++) {
650 if (got_object_id_cmp(&have[i], &zhash) == 0)
652 got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
653 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
654 if (n >= sizeof(buf)) {
655 err = got_error(GOT_ERR_NO_SPACE);
658 err = writepkt(fd, buf, n);
664 while (nhave > 0 && !acked) {
665 struct got_object_id common_id;
667 /* The server should ACK the object IDs we need. */
668 err = readpkt(&n, fd, buf, sizeof(buf));
671 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
672 err = fetch_error(&buf[4], n - 4);
675 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
676 /* Server has not located our objects yet. */
679 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
680 strncmp(buf, "ACK ", 4) != 0) {
681 err = got_error_msg(GOT_ERR_BAD_PACKET,
682 "unexpected message from server");
685 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
686 err = got_error_msg(GOT_ERR_BAD_PACKET,
687 "bad object ID in ACK packet from server");
693 n = snprintf(buf, sizeof(buf), "done\n");
694 err = writepkt(fd, buf, n);
699 err = readpkt(&n, fd, buf, sizeof(buf));
702 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
703 err = got_error_msg(GOT_ERR_BAD_PACKET,
704 "unexpected message from server");
710 fprintf(stderr, "%s: fetching...\n", getprogname());
712 if (my_capabilities != NULL &&
713 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
720 if (have_sidebands) {
721 err = read_pkthdr(&datalen, fd);
727 /* Read sideband channel ID (one byte). */
728 r = read(fd, buf, 1);
730 err = got_error_from_errno("read");
734 err = got_error_msg(GOT_ERR_BAD_PACKET,
738 if (datalen > sizeof(buf) - 5) {
739 err = got_error_msg(GOT_ERR_BAD_PACKET,
740 "bad packet length");
743 datalen--; /* sideband ID has been read */
744 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
745 /* Read packfile data. */
746 err = readn(&r, fd, buf, datalen);
750 err = got_error_msg(GOT_ERR_BAD_PACKET,
754 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
755 err = readn(&r, fd, buf, datalen);
759 err = got_error_msg(GOT_ERR_BAD_PACKET,
763 err = fetch_progress(ibuf, buf, r);
767 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
768 err = readn(&r, fd, buf, datalen);
772 err = got_error_msg(GOT_ERR_BAD_PACKET,
776 err = fetch_error(buf, r);
779 err = got_error_msg(GOT_ERR_BAD_PACKET,
780 "unknown side-band received from server");
784 /* No sideband channel. Every byte is packfile data. */
785 err = readn(&r, fd, buf, sizeof buf);
792 /* Write packfile data to temporary pack file. */
793 w = write(packfd, buf, r);
795 err = got_error_from_errno("write");
799 err = got_error(GOT_ERR_IO);
804 /* Don't send too many progress privsep messages. */
805 if (packsz > last_reported_packsz + 1024) {
806 err = got_privsep_send_fetch_download_progress(ibuf,
810 last_reported_packsz = packsz;
813 err = got_privsep_send_fetch_download_progress(ibuf, packsz);
817 TAILQ_FOREACH(pe, &symrefs, entry) {
818 free((void *)pe->path);
821 got_pathlist_free(&symrefs);
826 free(server_capabilities);
832 main(int argc, char **argv)
834 const struct got_error *err = NULL;
835 int fetchfd, packfd = -1, i;
836 struct got_object_id packid;
839 struct got_pathlist_head have_refs;
840 struct got_pathlist_entry *pe;
841 struct got_imsg_fetch_request *fetch_req = NULL;
842 struct got_imsg_fetch_have_ref href;
843 size_t datalen, remain;
851 TAILQ_INIT(&have_refs);
853 if (getenv("GOT_FETCH_DEBUG") != NULL) {
854 fprintf(stderr, "%s being chatty!\n", getprogname());
858 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
860 /* revoke access to most system calls */
861 if (pledge("stdio recvfd", NULL) == -1) {
862 err = got_error_from_errno("pledge");
863 got_privsep_send_error(&ibuf, err);
867 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
868 if (err->code == GOT_ERR_PRIVSEP_PIPE)
872 if (imsg.hdr.type == GOT_IMSG_STOP)
874 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
875 err = got_error(GOT_ERR_PRIVSEP_MSG);
878 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
879 if (datalen < sizeof(struct got_imsg_fetch_request)) {
880 err = got_error(GOT_ERR_PRIVSEP_LEN);
883 fetch_req = (struct got_imsg_fetch_request *)imsg.data;
884 if (datalen < sizeof(*fetch_req) +
885 sizeof(struct got_imsg_fetch_have_ref) *
886 fetch_req->n_have_refs) {
887 err = got_error(GOT_ERR_PRIVSEP_LEN);
890 offset = sizeof(*fetch_req);
892 for (i = 0; i < fetch_req->n_have_refs; i++) {
893 struct got_object_id *id;
896 if (remain < sizeof(href) || offset > datalen) {
897 err = got_error(GOT_ERR_PRIVSEP_LEN);
900 memcpy(&href, imsg.data + offset, sizeof(href));
901 remain -= sizeof(href);
902 if (remain < href.name_len) {
903 err = got_error(GOT_ERR_PRIVSEP_LEN);
906 remain -= href.name_len;
907 refname = malloc(href.name_len + 1);
908 if (refname == NULL) {
909 err = got_error_from_errno("malloc");
912 offset += sizeof(href);
913 memcpy(refname, imsg.data + offset, href.name_len);
914 refname[href.name_len] = '\0';
915 offset += href.name_len;
917 id = malloc(sizeof(*id));
920 err = got_error_from_errno("malloc");
923 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
924 err = got_pathlist_append(&have_refs, refname, id);
933 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
934 if (err->code == GOT_ERR_PRIVSEP_PIPE)
938 if (imsg.hdr.type == GOT_IMSG_STOP)
940 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
941 err = got_error(GOT_ERR_PRIVSEP_MSG);
944 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
945 err = got_error(GOT_ERR_PRIVSEP_LEN);
950 err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
951 fetch_req->fetch_all_branches, &ibuf);
953 TAILQ_FOREACH(pe, &have_refs, entry) {
954 free((char *)pe->path);
957 got_pathlist_free(&have_refs);
958 if (packfd != -1 && close(packfd) == -1 && err == NULL)
959 err = got_error_from_errno("close");
961 got_privsep_send_error(&ibuf, err);
963 err = got_privsep_send_fetch_done(&ibuf, packid);
965 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
966 got_privsep_send_error(&ibuf, err);