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"
55 #include "got_lib_poll.h"
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 struct got_object *indexed;
64 static const struct got_capability got_capabilities[] = {
65 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
66 { GOT_CAPA_OFS_DELTA, NULL },
68 { GOT_CAPA_SIDE_BAND_64K, NULL },
70 { GOT_CAPA_REPORT_STATUS, NULL },
71 { GOT_CAPA_DELETE_REFS, NULL },
74 static const struct got_error *
75 send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
76 struct got_ratelimit *rl)
78 const struct got_error *err = NULL;
82 err = got_ratelimit_check(&elapsed, rl);
87 if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
88 &bytes, sizeof(bytes)) == -1)
89 return got_error_from_errno(
90 "imsg_compose SEND_UPLOAD_PROGRESS");
92 return got_privsep_flush_imsg(ibuf);
95 static const struct got_error *
96 send_pack_request(struct imsgbuf *ibuf)
98 if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
100 return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
101 return got_privsep_flush_imsg(ibuf);
104 static const struct got_error *
105 send_done(struct imsgbuf *ibuf)
107 if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
108 return got_error_from_errno("imsg_compose SEND_DONE");
109 return got_privsep_flush_imsg(ibuf);
112 static const struct got_error *
113 recv_packfd(int *packfd, struct imsgbuf *ibuf)
115 const struct got_error *err;
120 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
124 if (imsg.hdr.type == GOT_IMSG_STOP) {
125 err = got_error(GOT_ERR_CANCELLED);
129 if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
130 err = got_error(GOT_ERR_PRIVSEP_MSG);
134 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
135 err = got_error(GOT_ERR_PRIVSEP_LEN);
145 static const struct got_error *
146 send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
148 const struct got_error *err;
149 unsigned char buf[8192];
152 struct got_ratelimit rl;
154 if (lseek(packfd, 0L, SEEK_SET) == -1)
155 return got_error_from_errno("lseek");
157 got_ratelimit_init(&rl, 0, 500);
160 r = read(packfd, buf, sizeof(buf));
162 return got_error_from_errno("read");
165 err = got_poll_write_full(sendfd, buf, r);
169 err = send_upload_progress(ibuf, wtotal, &rl);
174 return send_upload_progress(ibuf, wtotal, NULL);
177 static const struct got_error *
178 send_error(const char *buf, size_t len)
180 static char msg[1024];
183 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
184 if (!isprint((unsigned char)buf[i]))
185 return got_error_msg(GOT_ERR_BAD_PACKET,
186 "non-printable error message received from server");
190 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
193 static const struct got_error *
194 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
198 size_t len, reflen = strlen(refname);
200 len = sizeof(struct got_imsg_send_remote_ref) + reflen;
201 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
202 return got_error(GOT_ERR_NO_SPACE);
204 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
206 return got_error_from_errno("imsg_create SEND_REMOTE_REF");
208 /* Keep in sync with struct got_imsg_send_remote_ref definition! */
209 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
210 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
211 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
212 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
213 if (imsg_add(wbuf, refname, reflen) == -1)
214 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
217 imsg_close(ibuf, wbuf);
218 return got_privsep_flush_imsg(ibuf);
221 static const struct got_error *
222 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
223 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
226 size_t i, len, reflen, errmsglen = 0;
227 struct got_pathlist_entry *pe;
230 const char *errmsg = "";
232 eol = strchr(refname, '\n');
234 return got_error_msg(GOT_ERR_BAD_PACKET,
235 "unexpected message from server");
239 sp = strchr(refname, ' ');
243 errmsglen = strlen(errmsg);
245 for (i = 0; i < errmsglen; ++i) {
246 if (!isprint((unsigned char)errmsg[i])) {
247 return got_error_msg(GOT_ERR_BAD_PACKET,
248 "non-printable error message received "
254 reflen = strlen(refname);
255 if (!got_ref_name_is_valid(refname)) {
256 return got_error_msg(GOT_ERR_BAD_PACKET,
257 "unexpected message from server");
260 TAILQ_FOREACH(pe, refs, entry) {
261 if (strcmp(refname, pe->path) == 0) {
267 TAILQ_FOREACH(pe, delete_refs, entry) {
268 if (strcmp(refname, pe->path) == 0) {
275 return got_error_msg(GOT_ERR_BAD_PACKET,
276 "unexpected message from server");
279 len = sizeof(struct got_imsg_send_ref_status) + reflen + errmsglen;
280 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
281 return got_error(GOT_ERR_NO_SPACE);
283 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
286 return got_error_from_errno("imsg_create SEND_REF_STATUS");
288 /* Keep in sync with struct got_imsg_send_ref_status definition! */
289 if (imsg_add(wbuf, &success, sizeof(success)) == -1)
290 return got_error_from_errno("imsg_add SEND_REF_STATUS");
291 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
292 return got_error_from_errno("imsg_add SEND_REF_STATUS");
293 if (imsg_add(wbuf, &errmsglen, sizeof(errmsglen)) == -1)
294 return got_error_from_errno("imsg_add SEND_REF_STATUS");
295 if (imsg_add(wbuf, refname, reflen) == -1)
296 return got_error_from_errno("imsg_add SEND_REF_STATUS");
297 if (imsg_add(wbuf, errmsg, errmsglen) == -1)
298 return got_error_from_errno("imsg_add SEND_REF_STATUS");
301 imsg_close(ibuf, wbuf);
302 return got_privsep_flush_imsg(ibuf);
305 static const struct got_error *
306 describe_refchange(int *n, int *sent_my_capabilites,
307 const char *my_capabilities, char *buf, size_t bufsize,
308 const char *refname, const char *old_hashstr, const char *new_hashstr)
310 *n = snprintf(buf, bufsize, "%s %s %s",
311 old_hashstr, new_hashstr, refname);
312 if (*n < 0 || (size_t)*n >= bufsize)
313 return got_error(GOT_ERR_NO_SPACE);
316 * We must announce our capabilities along with the first
317 * reference. Unfortunately, the protocol requires an embedded
318 * NUL as a separator between reference name and capabilities,
319 * which we have to deal with here.
320 * It also requires a linefeed for terminating packet data.
322 if (!*sent_my_capabilites && my_capabilities != NULL) {
324 if (*n >= bufsize - 1)
325 return got_error(GOT_ERR_NO_SPACE);
326 m = snprintf(buf + *n + 1, /* offset after '\0' */
327 bufsize - (*n + 1), "%s\n", my_capabilities);
328 if (m < 0 || *n + m >= bufsize)
329 return got_error(GOT_ERR_NO_SPACE);
331 *sent_my_capabilites = 1;
333 *n = strlcat(buf, "\n", bufsize);
335 return got_error(GOT_ERR_NO_SPACE);
341 static const struct got_error *
342 send_pack(int fd, struct got_pathlist_head *refs,
343 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
345 const struct got_error *err = NULL;
346 char buf[GOT_PKT_MAX];
347 const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
348 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
349 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
350 struct got_pathlist_head their_refs;
354 char *id_str = NULL, *refname = NULL;
355 struct got_object_id *id = NULL;
356 char *server_capabilities = NULL, *my_capabilities = NULL;
357 struct got_pathlist_entry *pe;
358 int sent_my_capabilites = 0;
360 TAILQ_INIT(&their_refs);
362 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
363 return got_error(GOT_ERR_SEND_EMPTY);
366 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
371 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
372 err = send_error(&buf[4], n - 4);
377 err = got_gitproto_parse_refline(&id_str, &refname,
378 &server_capabilities, buf, n);
382 if (server_capabilities == NULL) {
383 server_capabilities = strdup("");
384 if (server_capabilities == NULL) {
385 err = got_error_from_errno("strdup");
389 if (chattygot && server_capabilities[0] != '\0')
390 fprintf(stderr, "%s: server capabilities: %s\n",
391 getprogname(), server_capabilities);
392 err = got_gitproto_match_capabilities(&my_capabilities,
393 NULL, server_capabilities, got_capabilities,
394 nitems(got_capabilities));
398 fprintf(stderr, "%s: my capabilities:%s\n",
400 my_capabilities ? my_capabilities : "");
403 if (strstr(refname, "^{}")) {
405 fprintf(stderr, "%s: ignoring %s\n",
406 getprogname(), refname);
411 id = malloc(sizeof(*id));
413 err = got_error_from_errno("malloc");
416 if (!got_parse_sha1_digest(id->sha1, id_str)) {
417 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
420 err = send_their_ref(ibuf, id, refname);
424 err = got_pathlist_append(&their_refs, refname, id);
429 fprintf(stderr, "%s: remote has %s %s\n",
430 getprogname(), refname, id_str);
433 refname = NULL; /* do not free; owned by their_refs */
434 id = NULL; /* do not free; owned by their_refs */
437 if (!TAILQ_EMPTY(delete_refs)) {
438 if (my_capabilities == NULL ||
439 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
440 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
445 TAILQ_FOREACH(pe, delete_refs, entry) {
446 const char *refname = pe->path;
447 struct got_pathlist_entry *their_pe;
448 struct got_object_id *their_id = NULL;
450 TAILQ_FOREACH(their_pe, &their_refs, entry) {
451 const char *their_refname = their_pe->path;
452 if (got_path_cmp(refname, their_refname,
453 strlen(refname), strlen(their_refname)) == 0) {
454 their_id = their_pe->data;
458 if (their_id == NULL) {
459 err = got_error_fmt(GOT_ERR_NOT_REF,
460 "%s does not exist in remote repository",
465 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
466 sizeof(old_hashstr));
467 got_sha1_digest_to_str(zero_id, new_hashstr,
468 sizeof(new_hashstr));
469 err = describe_refchange(&n, &sent_my_capabilites,
470 my_capabilities, buf, sizeof(buf), refname,
471 old_hashstr, new_hashstr);
474 err = got_pkt_writepkt(fd, buf, n, chattygot);
478 fprintf(stderr, "%s: deleting %s %s\n",
479 getprogname(), refname, old_hashstr);
484 TAILQ_FOREACH(pe, refs, entry) {
485 const char *refname = pe->path;
486 struct got_object_id *id = pe->data;
487 struct got_object_id *their_id = NULL;
488 struct got_pathlist_entry *their_pe;
490 TAILQ_FOREACH(their_pe, &their_refs, entry) {
491 const char *their_refname = their_pe->path;
492 if (got_path_cmp(refname, their_refname,
493 strlen(refname), strlen(their_refname)) == 0) {
494 their_id = their_pe->data;
499 if (got_object_id_cmp(id, their_id) == 0) {
502 "%s: no change for %s\n",
503 getprogname(), refname);
507 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
508 sizeof(old_hashstr));
510 got_sha1_digest_to_str(zero_id, old_hashstr,
511 sizeof(old_hashstr));
513 got_sha1_digest_to_str(id->sha1, new_hashstr,
514 sizeof(new_hashstr));
515 err = describe_refchange(&n, &sent_my_capabilites,
516 my_capabilities, buf, sizeof(buf), refname,
517 old_hashstr, new_hashstr);
520 err = got_pkt_writepkt(fd, buf, n, chattygot);
525 fprintf(stderr, "%s: updating %s %s -> %s\n",
526 getprogname(), refname, old_hashstr,
529 fprintf(stderr, "%s: creating %s %s\n",
530 getprogname(), refname, new_hashstr);
535 err = got_pkt_flushpkt(fd, chattygot);
539 err = send_pack_request(ibuf);
543 err = recv_packfd(&packfd, ibuf);
548 err = send_pack_file(fd, packfd, ibuf);
553 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
556 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
557 err = send_error(&buf[4], n - 4);
559 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
560 err = got_error_msg(GOT_ERR_BAD_PACKET,
561 "unexpected message from server");
566 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
570 err = got_error_msg(GOT_ERR_BAD_PACKET,
571 "unexpected message from server");
573 } else if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
574 err = send_error(&buf[4], n - 4);
576 } else if (strncmp(buf, "ok ", 3) == 0) {
577 err = send_ref_status(ibuf, buf + 3, 1,
581 } else if (strncmp(buf, "ng ", 3) == 0) {
582 err = send_ref_status(ibuf, buf + 3, 0,
587 err = got_error_msg(GOT_ERR_BAD_PACKET,
588 "unexpected message from server");
594 err = send_done(ibuf);
596 TAILQ_FOREACH(pe, &their_refs, entry) {
597 free((void *)pe->path);
600 got_pathlist_free(&their_refs);
604 free(server_capabilities);
609 main(int argc, char **argv)
611 const struct got_error *err = NULL;
615 struct got_pathlist_head refs;
616 struct got_pathlist_head delete_refs;
617 struct got_pathlist_entry *pe;
618 struct got_imsg_send_request send_req;
619 struct got_imsg_send_ref href;
628 TAILQ_INIT(&delete_refs);
630 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
632 /* revoke access to most system calls */
633 if (pledge("stdio recvfd", NULL) == -1) {
634 err = got_error_from_errno("pledge");
635 got_privsep_send_error(&ibuf, err);
639 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
640 if (err->code == GOT_ERR_PRIVSEP_PIPE)
644 if (imsg.hdr.type == GOT_IMSG_STOP)
646 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
647 err = got_error(GOT_ERR_PRIVSEP_MSG);
650 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
651 if (datalen < sizeof(send_req)) {
652 err = got_error(GOT_ERR_PRIVSEP_LEN);
655 memcpy(&send_req, imsg.data, sizeof(send_req));
659 if (send_req.verbosity > 0)
660 chattygot += send_req.verbosity;
662 for (i = 0; i < send_req.nrefs; i++) {
663 struct got_object_id *id;
666 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
667 if (err->code == GOT_ERR_PRIVSEP_PIPE)
671 if (imsg.hdr.type == GOT_IMSG_STOP)
673 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
674 err = got_error(GOT_ERR_PRIVSEP_MSG);
677 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
678 if (datalen < sizeof(href)) {
679 err = got_error(GOT_ERR_PRIVSEP_LEN);
682 memcpy(&href, imsg.data, sizeof(href));
683 if (datalen - sizeof(href) < href.name_len) {
684 err = got_error(GOT_ERR_PRIVSEP_LEN);
687 refname = malloc(href.name_len + 1);
688 if (refname == NULL) {
689 err = got_error_from_errno("malloc");
692 memcpy(refname, imsg.data + sizeof(href), href.name_len);
693 refname[href.name_len] = '\0';
696 * Prevent sending of references that won't make any
697 * sense outside the local repository's context.
699 if (strncmp(refname, "refs/got/", 9) == 0 ||
700 strncmp(refname, "refs/remotes/", 13) == 0) {
701 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
706 id = malloc(sizeof(*id));
709 err = got_error_from_errno("malloc");
712 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
714 err = got_pathlist_append(&delete_refs, refname, id);
716 err = got_pathlist_append(&refs, refname, id);
726 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
728 TAILQ_FOREACH(pe, &refs, entry) {
729 free((char *)pe->path);
732 got_pathlist_free(&refs);
733 TAILQ_FOREACH(pe, &delete_refs, entry) {
734 free((char *)pe->path);
737 got_pathlist_free(&delete_refs);
738 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
739 err = got_error_from_errno("close");
740 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
741 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
742 got_privsep_send_error(&ibuf, err);