2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_object.h"
42 #include "got_version.h"
43 #include "got_fetch.h"
44 #include "got_reference.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_pkt.h"
53 #include "got_lib_gitproto.h"
54 #include "got_lib_ratelimit.h"
57 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 struct got_object *indexed;
63 static const struct got_capability got_capabilities[] = {
64 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
65 { GOT_CAPA_OFS_DELTA, NULL },
67 { GOT_CAPA_SIDE_BAND_64K, NULL },
69 { GOT_CAPA_REPORT_STATUS, NULL },
70 { GOT_CAPA_DELETE_REFS, NULL },
73 static const struct got_error *
74 send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
75 struct got_ratelimit *rl)
77 const struct got_error *err = NULL;
81 err = got_ratelimit_check(&elapsed, rl);
86 if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
87 &bytes, sizeof(bytes)) == -1)
88 return got_error_from_errno(
89 "imsg_compose SEND_UPLOAD_PROGRESS");
91 return got_privsep_flush_imsg(ibuf);
94 static const struct got_error *
95 send_pack_request(struct imsgbuf *ibuf)
97 if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
99 return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
100 return got_privsep_flush_imsg(ibuf);
103 static const struct got_error *
104 send_done(struct imsgbuf *ibuf)
106 if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
107 return got_error_from_errno("imsg_compose SEND_DONE");
108 return got_privsep_flush_imsg(ibuf);
111 static const struct got_error *
112 recv_packfd(int *packfd, struct imsgbuf *ibuf)
114 const struct got_error *err;
119 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
123 if (imsg.hdr.type == GOT_IMSG_STOP) {
124 err = got_error(GOT_ERR_CANCELLED);
128 if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
129 err = got_error(GOT_ERR_PRIVSEP_MSG);
133 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
134 err = got_error(GOT_ERR_PRIVSEP_LEN);
144 static const struct got_error *
145 send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
147 const struct got_error *err;
148 unsigned char buf[8192];
151 struct got_ratelimit rl;
153 if (lseek(packfd, 0L, SEEK_SET) == -1)
154 return got_error_from_errno("lseek");
156 got_ratelimit_init(&rl, 0, 500);
159 r = read(packfd, buf, sizeof(buf));
161 return got_error_from_errno("read");
164 w = write(sendfd, buf, r);
166 return got_error_from_errno("write");
168 return got_error(GOT_ERR_IO);
170 err = send_upload_progress(ibuf, wtotal, &rl);
175 return send_upload_progress(ibuf, wtotal, NULL);
178 static const struct got_error *
179 send_error(const char *buf, size_t len)
181 static char msg[1024];
184 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
185 if (!isprint(buf[i]))
186 return got_error_msg(GOT_ERR_BAD_PACKET,
187 "non-printable error message received from server");
191 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
194 static const struct got_error *
195 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
198 const struct got_error *err = NULL;
200 size_t len, reflen = strlen(refname);
202 len = sizeof(struct got_imsg_send_remote_ref) + reflen;
203 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
204 return got_error(GOT_ERR_NO_SPACE);
206 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
208 return got_error_from_errno("imsg_create SEND_REMOTE_REF");
210 /* Keep in sync with struct got_imsg_send_remote_ref definition! */
211 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
212 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
216 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
217 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
221 if (imsg_add(wbuf, refname, reflen) == -1) {
222 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
228 imsg_close(ibuf, wbuf);
229 return got_privsep_flush_imsg(ibuf);
232 static const struct got_error *
233 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
234 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
236 const struct got_error *err = NULL;
238 size_t len, reflen = strlen(refname);
239 struct got_pathlist_entry *pe;
243 eol = strchr(refname, '\n');
245 return got_error_msg(GOT_ERR_BAD_PACKET,
246 "unexpected message from server");
250 TAILQ_FOREACH(pe, refs, entry) {
251 if (strcmp(refname, pe->path) == 0) {
257 TAILQ_FOREACH(pe, delete_refs, entry) {
258 if (strcmp(refname, pe->path) == 0) {
265 return got_error_msg(GOT_ERR_BAD_PACKET,
266 "unexpected message from server");
269 len = sizeof(struct got_imsg_send_ref_status) + reflen;
270 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
271 return got_error(GOT_ERR_NO_SPACE);
273 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
276 return got_error_from_errno("imsg_create SEND_REF_STATUS");
278 /* Keep in sync with struct got_imsg_send_ref_status definition! */
279 if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
280 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
284 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
285 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
289 if (imsg_add(wbuf, refname, reflen) == -1) {
290 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
296 imsg_close(ibuf, wbuf);
297 return got_privsep_flush_imsg(ibuf);
300 static const struct got_error *
301 describe_refchange(int *n, int *sent_my_capabilites,
302 const char *my_capabilities, char *buf, size_t bufsize,
303 const char *refname, const char *old_hashstr, const char *new_hashstr)
305 *n = snprintf(buf, bufsize, "%s %s %s",
306 old_hashstr, new_hashstr, refname);
308 return got_error(GOT_ERR_NO_SPACE);
311 * We must announce our capabilities along with the first
312 * reference. Unfortunately, the protocol requires an embedded
313 * NUL as a separator between reference name and capabilities,
314 * which we have to deal with here.
315 * It also requires a linefeed for terminating packet data.
317 if (!*sent_my_capabilites && my_capabilities != NULL) {
319 if (*n >= bufsize - 1)
320 return got_error(GOT_ERR_NO_SPACE);
321 m = snprintf(buf + *n + 1, /* offset after '\0' */
322 bufsize - (*n + 1), "%s\n", my_capabilities);
323 if (*n + m >= bufsize)
324 return got_error(GOT_ERR_NO_SPACE);
326 *sent_my_capabilites = 1;
328 *n = strlcat(buf, "\n", bufsize);
330 return got_error(GOT_ERR_NO_SPACE);
336 static const struct got_error *
337 send_pack(int fd, struct got_pathlist_head *refs,
338 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
340 const struct got_error *err = NULL;
341 char buf[GOT_PKT_MAX];
342 const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
343 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
344 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
345 struct got_pathlist_head their_refs;
349 char *id_str = NULL, *refname = NULL;
350 struct got_object_id *id = NULL;
351 char *server_capabilities = NULL, *my_capabilities = NULL;
352 struct got_pathlist_entry *pe;
353 int sent_my_capabilites = 0;
355 TAILQ_INIT(&their_refs);
357 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
358 return got_error(GOT_ERR_SEND_EMPTY);
361 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
366 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
367 err = send_error(&buf[4], n - 4);
372 err = got_gitproto_parse_refline(&id_str, &refname,
373 &server_capabilities, buf, n);
377 if (chattygot && server_capabilities[0] != '\0')
378 fprintf(stderr, "%s: server capabilities: %s\n",
379 getprogname(), server_capabilities);
380 err = got_gitproto_match_capabilities(&my_capabilities,
381 NULL, server_capabilities, got_capabilities,
382 nitems(got_capabilities));
386 fprintf(stderr, "%s: my capabilities:%s\n",
387 getprogname(), my_capabilities);
390 if (strstr(refname, "^{}")) {
392 fprintf(stderr, "%s: ignoring %s\n",
393 getprogname(), refname);
398 id = malloc(sizeof(*id));
400 err = got_error_from_errno("malloc");
403 if (!got_parse_sha1_digest(id->sha1, id_str)) {
404 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
407 err = send_their_ref(ibuf, id, refname);
411 err = got_pathlist_append(&their_refs, refname, id);
413 fprintf(stderr, "%s: remote has %s %s\n",
414 getprogname(), refname, id_str);
417 refname = NULL; /* do not free; owned by their_refs */
418 id = NULL; /* do not free; owned by their_refs */
421 if (!TAILQ_EMPTY(delete_refs)) {
422 if (my_capabilities == NULL ||
423 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
424 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
429 TAILQ_FOREACH(pe, delete_refs, entry) {
430 const char *refname = pe->path;
431 struct got_pathlist_entry *their_pe;
432 struct got_object_id *their_id = NULL;
434 TAILQ_FOREACH(their_pe, &their_refs, entry) {
435 const char *their_refname = their_pe->path;
436 if (got_path_cmp(refname, their_refname,
437 strlen(refname), strlen(their_refname)) == 0) {
438 their_id = their_pe->data;
442 if (their_id == NULL) {
443 err = got_error_fmt(GOT_ERR_NOT_REF,
444 "%s does not exist in remote repository",
449 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
450 sizeof(old_hashstr));
451 got_sha1_digest_to_str(zero_id, new_hashstr,
452 sizeof(new_hashstr));
453 err = describe_refchange(&n, &sent_my_capabilites,
454 my_capabilities, buf, sizeof(buf), refname,
455 old_hashstr, new_hashstr);
458 err = got_pkt_writepkt(fd, buf, n, chattygot);
462 fprintf(stderr, "%s: deleting %s %s\n",
463 getprogname(), refname, old_hashstr);
468 TAILQ_FOREACH(pe, refs, entry) {
469 const char *refname = pe->path;
470 struct got_object_id *id = pe->data;
471 struct got_object_id *their_id = NULL;
472 struct got_pathlist_entry *their_pe;
474 TAILQ_FOREACH(their_pe, &their_refs, entry) {
475 const char *their_refname = their_pe->path;
476 if (got_path_cmp(refname, their_refname,
477 strlen(refname), strlen(their_refname)) == 0) {
478 their_id = their_pe->data;
483 if (got_object_id_cmp(id, their_id) == 0) {
486 "%s: no change for %s\n",
487 getprogname(), refname);
491 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
492 sizeof(old_hashstr));
494 got_sha1_digest_to_str(zero_id, old_hashstr,
495 sizeof(old_hashstr));
497 got_sha1_digest_to_str(id->sha1, new_hashstr,
498 sizeof(new_hashstr));
499 err = describe_refchange(&n, &sent_my_capabilites,
500 my_capabilities, buf, sizeof(buf), refname,
501 old_hashstr, new_hashstr);
504 err = got_pkt_writepkt(fd, buf, n, chattygot);
509 fprintf(stderr, "%s: updating %s %s -> %s\n",
510 getprogname(), refname, old_hashstr,
513 fprintf(stderr, "%s: creating %s %s\n",
514 getprogname(), refname, new_hashstr);
519 err = got_pkt_flushpkt(fd, chattygot);
523 err = send_pack_request(ibuf);
527 err = recv_packfd(&packfd, ibuf);
532 err = send_pack_file(fd, packfd, ibuf);
537 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
540 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
541 err = send_error(&buf[4], n - 4);
543 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
544 err = got_error_msg(GOT_ERR_BAD_PACKET,
545 "unexpected message from server");
550 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
554 err = got_error_msg(GOT_ERR_BAD_PACKET,
555 "unexpected message from server");
557 } else if (strncmp(buf, "ok ", 3) == 0) {
558 err = send_ref_status(ibuf, buf + 3, 1,
562 } else if (strncmp(buf, "ng ", 3) == 0) {
563 err = send_ref_status(ibuf, buf + 3, 0,
568 err = got_error_msg(GOT_ERR_BAD_PACKET,
569 "unexpected message from server");
575 err = send_done(ibuf);
577 TAILQ_FOREACH(pe, &their_refs, entry) {
578 free((void *)pe->path);
581 got_pathlist_free(&their_refs);
585 free(server_capabilities);
590 main(int argc, char **argv)
592 const struct got_error *err = NULL;
596 struct got_pathlist_head refs;
597 struct got_pathlist_head delete_refs;
598 struct got_pathlist_entry *pe;
599 struct got_imsg_send_request send_req;
600 struct got_imsg_send_ref href;
609 TAILQ_INIT(&delete_refs);
611 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
613 /* revoke access to most system calls */
614 if (pledge("stdio recvfd", NULL) == -1) {
615 err = got_error_from_errno("pledge");
616 got_privsep_send_error(&ibuf, err);
620 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
621 if (err->code == GOT_ERR_PRIVSEP_PIPE)
625 if (imsg.hdr.type == GOT_IMSG_STOP)
627 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
628 err = got_error(GOT_ERR_PRIVSEP_MSG);
631 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
632 if (datalen < sizeof(send_req)) {
633 err = got_error(GOT_ERR_PRIVSEP_LEN);
636 memcpy(&send_req, imsg.data, sizeof(send_req));
640 if (send_req.verbosity > 0)
641 chattygot += send_req.verbosity;
643 for (i = 0; i < send_req.nrefs; i++) {
644 struct got_object_id *id;
647 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
648 if (err->code == GOT_ERR_PRIVSEP_PIPE)
652 if (imsg.hdr.type == GOT_IMSG_STOP)
654 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
655 err = got_error(GOT_ERR_PRIVSEP_MSG);
658 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
659 if (datalen < sizeof(href)) {
660 err = got_error(GOT_ERR_PRIVSEP_LEN);
663 memcpy(&href, imsg.data, sizeof(href));
664 if (datalen - sizeof(href) < href.name_len) {
665 err = got_error(GOT_ERR_PRIVSEP_LEN);
668 refname = malloc(href.name_len + 1);
669 if (refname == NULL) {
670 err = got_error_from_errno("malloc");
673 memcpy(refname, imsg.data + sizeof(href), href.name_len);
674 refname[href.name_len] = '\0';
677 * Prevent sending of references that won't make any
678 * sense outside the local repository's context.
680 if (strncmp(refname, "refs/got/", 9) == 0 ||
681 strncmp(refname, "refs/remotes/", 13) == 0) {
682 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
687 id = malloc(sizeof(*id));
690 err = got_error_from_errno("malloc");
693 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
695 err = got_pathlist_append(&delete_refs, refname, id);
697 err = got_pathlist_append(&refs, refname, id);
707 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
709 TAILQ_FOREACH(pe, &refs, entry) {
710 free((char *)pe->path);
713 got_pathlist_free(&refs);
714 TAILQ_FOREACH(pe, &delete_refs, entry) {
715 free((char *)pe->path);
718 got_pathlist_free(&delete_refs);
719 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
720 err = got_error_from_errno("close");
721 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
722 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
723 got_privsep_send_error(&ibuf, err);