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"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #define GOT_PKTMAX 65536
56 struct got_object *indexed;
58 static char *fetchbranch;
59 static struct got_object_id zhash = {.sha1={0}};
61 static const struct got_error *
62 readn(ssize_t *off, int fd, void *buf, size_t n)
68 r = read(fd, buf + *off, n - *off);
70 return got_error_from_errno("read");
78 static const struct got_error *
84 fprintf(stderr, "writepkt: 0000\n");
86 w = write(fd, "0000", 4);
88 return got_error_from_errno("write");
90 return got_error(GOT_ERR_IO);
95 * Packet header contains a 4-byte hexstring which specifies the length
96 * of data which follows.
98 static const struct got_error *
99 read_pkthdr(int *datalen, int fd)
101 static const struct got_error *err = NULL;
110 err = readn(&r, fd, lenstr, 4);
113 if (r == 0) /* implicit "0000" */
116 return got_error_msg(GOT_ERR_BAD_PACKET,
117 "wrong packet header length");
120 for (i = 0; i < 4; i++) {
121 if (!isxdigit(lenstr[i]))
122 return got_error_msg(GOT_ERR_BAD_PACKET,
123 "packet length not specified in hex");
126 len = strtol(lenstr, &e, 16);
127 if (lenstr[0] == '\0' || *e != '\0')
128 return got_error(GOT_ERR_BAD_PACKET);
129 if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
130 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
131 if (len > INT_MAX || len < INT_MIN)
132 return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
137 return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
144 static const struct got_error *
145 readpkt(int *outlen, int fd, char *buf, int buflen)
147 const struct got_error *err = NULL;
151 err = read_pkthdr(&datalen, fd);
155 if (datalen > buflen)
156 return got_error(GOT_ERR_NO_SPACE);
158 err = readn(&n, fd, buf, datalen);
162 return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
168 static const struct got_error *
169 writepkt(int fd, char *buf, int nbuf)
175 if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
176 return got_error(GOT_ERR_NO_SPACE);
177 w = write(fd, len, 4);
179 return got_error_from_errno("write");
181 return got_error(GOT_ERR_IO);
182 w = write(fd, buf, nbuf);
184 return got_error_from_errno("write");
186 return got_error(GOT_ERR_IO);
188 fprintf(stderr, "writepkt: %s:\t", len);
189 fwrite(buf, 1, nbuf, stderr);
190 for (i = 0; i < nbuf; i++) {
192 fputc(buf[i], stderr);
199 static const struct got_error *
200 match_remote_ref(struct got_pathlist_head *have_refs, struct got_object_id *id,
201 char *refname, char *id_str)
203 struct got_pathlist_entry *pe;
205 memset(id, 0, sizeof(*id));
207 TAILQ_FOREACH(pe, have_refs, entry) {
208 if (strcmp(pe->path, refname) == 0) {
209 if (!got_parse_sha1_digest(id->sha1, id_str))
210 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
218 match_branch(char *br, char *pat)
222 if (strstr(pat, "refs/heads") == pat) {
223 if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
225 } else if (strstr(pat, "heads")) {
226 if (snprintf(name, sizeof(name), "refs/%s", pat)
230 if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
234 return strcmp(br, name) == 0;
237 static const struct got_error *
238 tokenize_refline(char **tokens, char *line, int len, int maxtokens)
240 const struct got_error *err = NULL;
244 for (i = 0; i < maxtokens; i++)
247 for (i = 0; n < len && i < maxtokens; i++) {
248 while (isspace(*line)) {
253 while (*line != '\0' &&
254 (!isspace(*line) || i == maxtokens - 1)) {
258 tokens[i] = strndup(p, line - p);
259 if (tokens[i] == NULL) {
260 err = got_error_from_errno("strndup");
263 /* Skip \0 field-delimiter at end of token. */
264 while (line[0] == '\0' && n < len) {
270 err = got_error(GOT_ERR_NOT_REF);
274 for (j = 0; j < i; j++)
281 static const struct got_error *
282 parse_refline(char **id_str, char **refname, char **server_capabilities,
285 const struct got_error *err = NULL;
288 err = tokenize_refline(tokens, line, len, nitems(tokens));
295 *refname = tokens[1];
297 *server_capabilities = tokens[2];
302 #define GOT_CAPA_AGENT "agent"
303 #define GOT_CAPA_OFS_DELTA "ofs-delta"
304 #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
306 #define GOT_SIDEBAND_PACKFILE_DATA 1
307 #define GOT_SIDEBAND_PROGRESS_INFO 2
308 #define GOT_SIDEBAND_ERROR_INFO 3
311 struct got_capability {
315 static const struct got_capability got_capabilities[] = {
316 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
317 { GOT_CAPA_OFS_DELTA, NULL },
318 { GOT_CAPA_SIDE_BAND_64K, NULL },
321 static const struct got_error *
322 match_capability(char **my_capabilities, const char *capa,
323 const struct got_capability *mycapa)
328 equalsign = strchr(capa, '=');
330 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
333 if (strcmp(capa, mycapa->key) != 0)
337 if (asprintf(&s, "%s%s%s%s%s",
338 *my_capabilities != NULL ? *my_capabilities : "",
339 *my_capabilities != NULL ? " " : "",
341 mycapa->value != NULL ? "=" : "",
342 mycapa->value != NULL? mycapa->value : "") == -1)
343 return got_error_from_errno("asprintf");
345 free(*my_capabilities);
346 *my_capabilities = s;
350 static const struct got_error *
351 add_symref(struct got_pathlist_head *symrefs, char *capa)
353 const struct got_error *err = NULL;
354 char *colon, *name = NULL, *target = NULL;
356 /* Need at least "A:B" */
357 if (strlen(capa) < 3)
360 colon = strchr(capa, ':');
367 return got_error_from_errno("strdup");
369 target = strdup(colon + 1);
370 if (target == NULL) {
371 err = got_error_from_errno("strdup");
375 /* We can't validate the ref itself here. The main process will. */
376 err = got_pathlist_append(symrefs, name, target);
385 static const struct got_error *
386 match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
387 char *server_capabilities)
389 const struct got_error *err = NULL;
390 char *capa, *equalsign;
393 *my_capabilities = NULL;
395 capa = strsep(&server_capabilities, " ");
399 equalsign = strchr(capa, '=');
400 if (equalsign != NULL &&
401 strncmp(capa, "symref", equalsign - capa) == 0) {
402 err = add_symref(symrefs, equalsign + 1);
408 for (i = 0; i < nitems(got_capabilities); i++) {
409 err = match_capability(my_capabilities,
410 capa, &got_capabilities[i]);
419 static const struct got_error *
420 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
429 * Truncate messages which exceed the maximum imsg payload size.
430 * Server may send up to 64k.
432 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
433 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
435 /* Only allow printable ASCII. */
436 for (i = 0; i < len; i++) {
437 if (isprint((unsigned char)buf[i]) ||
438 isspace((unsigned char)buf[i]))
440 return got_error_msg(GOT_ERR_BAD_PACKET,
441 "non-printable progress message received from server");
444 return got_privsep_send_fetch_server_progress(ibuf, buf, len);
447 static const struct got_error *
448 fetch_error(const char *buf, size_t len)
450 static char msg[1024];
453 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
454 if (!isprint(buf[i]))
455 return got_error_msg(GOT_ERR_BAD_PACKET,
456 "non-printable error message received from server");
460 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
463 static const struct got_error *
464 fetch_pack(int fd, int packfd, struct got_object_id *packid,
465 struct got_pathlist_head *have_refs, struct imsgbuf *ibuf)
467 const struct got_error *err = NULL;
468 char buf[GOT_PKTMAX];
469 char hashstr[SHA1_DIGEST_STRING_LENGTH];
470 struct got_object_id *have, *want;
471 int is_firstpkt = 1, nref = 0, refsz = 16;
474 char *id_str = NULL, *refname = NULL;
475 char *server_capabilities = NULL, *my_capabilities = NULL;
476 struct got_pathlist_head symrefs;
477 struct got_pathlist_entry *pe;
478 int have_sidebands = 0;
480 TAILQ_INIT(&symrefs);
482 have = malloc(refsz * sizeof(have[0]));
484 return got_error_from_errno("malloc");
485 want = malloc(refsz * sizeof(want[0]));
487 err = got_error_from_errno("malloc");
491 fprintf(stderr, "starting fetch\n");
493 err = readpkt(&n, fd, buf, sizeof(buf));
498 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
499 err = fetch_error(&buf[4], n - 4);
502 err = parse_refline(&id_str, &refname, &server_capabilities,
506 if (chattygit && server_capabilities[0] != '\0')
507 fprintf(stderr, "server capabilities: %s\n",
508 server_capabilities);
510 err = match_capabilities(&my_capabilities, &symrefs,
511 server_capabilities);
514 if (chattygit && my_capabilities)
515 fprintf(stderr, "my matched capabilities: %s\n",
517 err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
522 if (strstr(refname, "^{}"))
524 if (fetchbranch && !match_branch(refname, fetchbranch))
526 if (refsz == nref + 1) {
528 have = reallocarray(have, refsz, sizeof(have[0]));
530 err = got_error_from_errno("reallocarray");
533 want = reallocarray(want, refsz, sizeof(want[0]));
535 err = got_error_from_errno("reallocarray");
539 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
540 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
544 err = match_remote_ref(have_refs, &have[nref], id_str, refname);
548 err = got_privsep_send_fetch_ref(ibuf, &want[nref],
553 fprintf(stderr, "remote %s\n", refname);
558 for (i = 0; i < nref; i++) {
559 if (got_object_id_cmp(&have[i], &want[i]) == 0)
561 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
562 n = snprintf(buf, sizeof(buf), "want %s%s%s\n", hashstr,
563 i == 0 && my_capabilities ? " " : "",
564 i == 0 && my_capabilities ? my_capabilities : "");
565 if (n >= sizeof(buf)) {
566 err = got_error(GOT_ERR_NO_SPACE);
569 err = writepkt(fd, buf, n);
577 for (i = 0; i < nref; i++) {
578 if (got_object_id_cmp(&have[i], &zhash) == 0)
580 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
581 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
582 if (n >= sizeof(buf)) {
583 err = got_error(GOT_ERR_NO_SPACE);
586 err = writepkt(fd, buf, n + 1);
592 fprintf(stderr, "up to date\n");
597 n = snprintf(buf, sizeof(buf), "done\n");
598 err = writepkt(fd, buf, n);
604 err = readpkt(&n, fd, buf, sizeof(buf));
608 * For now, we only support a full clone, in which case the server
609 * will now send a "NAK" (meaning no common objects were found).
611 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
612 err = got_error_msg(GOT_ERR_BAD_PACKET,
613 "unexpected message from server");
618 fprintf(stderr, "fetching...\n");
620 if (my_capabilities != NULL &&
621 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
629 if (have_sidebands) {
630 err = read_pkthdr(&datalen, fd);
636 /* Read sideband channel ID (one byte). */
637 r = read(fd, buf, 1);
639 err = got_error_from_errno("read");
643 err = got_error_msg(GOT_ERR_BAD_PACKET,
647 if (datalen > sizeof(buf) - 5) {
648 err = got_error_msg(GOT_ERR_BAD_PACKET,
649 "bad packet length");
652 datalen--; /* sideband ID has been read */
653 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
654 /* Read packfile data. */
655 err = readn(&r, fd, buf, datalen);
659 err = got_error_msg(GOT_ERR_BAD_PACKET,
663 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
664 err = readn(&r, fd, buf, datalen);
668 err = got_error_msg(GOT_ERR_BAD_PACKET,
672 err = fetch_progress(ibuf, buf, r);
676 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
677 err = readn(&r, fd, buf, datalen);
681 err = got_error_msg(GOT_ERR_BAD_PACKET,
685 err = fetch_error(buf, r);
688 err = got_error_msg(GOT_ERR_BAD_PACKET,
689 "unknown side-band received from server");
693 /* No sideband channel. Every byte is packfile data. */
694 err = readn(&r, fd, buf, sizeof buf);
701 /* Write packfile data to temporary pack file. */
702 w = write(packfd, buf, r);
704 err = got_error_from_errno("write");
708 err = got_error(GOT_ERR_IO);
712 err = got_privsep_send_fetch_download_progress(ibuf, packsz);
717 TAILQ_FOREACH(pe, &symrefs, entry) {
718 free((void *)pe->path);
721 got_pathlist_free(&symrefs);
726 free(server_capabilities);
732 main(int argc, char **argv)
734 const struct got_error *err = NULL;
735 int fetchfd, packfd = -1;
736 struct got_object_id packid;
739 struct got_pathlist_head have_refs;
740 struct got_imsg_fetch_have_refs *fetch_have_refs = NULL;
743 TAILQ_INIT(&have_refs);
745 if (getenv("GOT_DEBUG") != NULL) {
746 fprintf(stderr, "fetch-pack being chatty!\n");
750 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
752 /* revoke access to most system calls */
753 if (pledge("stdio recvfd", NULL) == -1) {
754 err = got_error_from_errno("pledge");
755 got_privsep_send_error(&ibuf, err);
759 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
760 if (err->code == GOT_ERR_PRIVSEP_PIPE)
764 if (imsg.hdr.type == GOT_IMSG_STOP)
766 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
767 err = got_error(GOT_ERR_PRIVSEP_MSG);
770 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
771 if (datalen < sizeof(struct got_imsg_fetch_have_refs)) {
772 err = got_error(GOT_ERR_PRIVSEP_LEN);
775 fetch_have_refs = (struct got_imsg_fetch_have_refs *)imsg.data;
776 if (datalen != sizeof(struct got_imsg_fetch_have_refs) +
777 sizeof(struct got_imsg_fetch_have_ref) *
778 fetch_have_refs->n_have_refs) {
779 err = got_error(GOT_ERR_PRIVSEP_LEN);
782 if (fetch_have_refs->n_have_refs != 0) {
783 /* TODO: Incremental fetch support */
784 err = got_error(GOT_ERR_NOT_IMPL);
789 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
790 if (err->code == GOT_ERR_PRIVSEP_PIPE)
794 if (imsg.hdr.type == GOT_IMSG_STOP)
796 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
797 err = got_error(GOT_ERR_PRIVSEP_MSG);
800 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
801 err = got_error(GOT_ERR_PRIVSEP_LEN);
806 err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
808 if (packfd != -1 && close(packfd) == -1 && err == NULL)
809 err = got_error_from_errno("close");
811 got_privsep_send_error(&ibuf, err);
813 err = got_privsep_send_fetch_done(&ibuf, packid);
815 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
816 got_privsep_send_error(&ibuf, err);