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>
41 #include "got_error.h"
42 #include "got_object.h"
44 #include "got_version.h"
45 #include "got_fetch.h"
46 #include "got_reference.h"
48 #include "got_lib_hash.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_pkt.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_ratelimit.h"
57 #include "got_lib_poll.h"
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 struct got_object *indexed;
66 static const struct got_capability got_capabilities[] = {
67 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
68 { GOT_CAPA_OFS_DELTA, NULL },
70 { GOT_CAPA_SIDE_BAND_64K, NULL },
72 { GOT_CAPA_REPORT_STATUS, NULL },
73 { GOT_CAPA_DELETE_REFS, NULL },
76 static const struct got_error *
77 send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
78 struct got_ratelimit *rl)
80 const struct got_error *err = NULL;
84 err = got_ratelimit_check(&elapsed, rl);
89 if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
90 &bytes, sizeof(bytes)) == -1)
91 return got_error_from_errno(
92 "imsg_compose SEND_UPLOAD_PROGRESS");
94 return got_privsep_flush_imsg(ibuf);
97 static const struct got_error *
98 send_pack_request(struct imsgbuf *ibuf)
100 if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
102 return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
103 return got_privsep_flush_imsg(ibuf);
106 static const struct got_error *
107 send_done(struct imsgbuf *ibuf)
109 if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
110 return got_error_from_errno("imsg_compose SEND_DONE");
111 return got_privsep_flush_imsg(ibuf);
114 static const struct got_error *
115 recv_packfd(int *packfd, struct imsgbuf *ibuf)
117 const struct got_error *err;
122 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
126 if (imsg.hdr.type == GOT_IMSG_STOP) {
127 err = got_error(GOT_ERR_CANCELLED);
131 if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
132 err = got_error(GOT_ERR_PRIVSEP_MSG);
136 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
137 err = got_error(GOT_ERR_PRIVSEP_LEN);
141 *packfd = imsg_get_fd(&imsg);
147 static const struct got_error *
148 send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
150 const struct got_error *err;
151 unsigned char buf[8192];
154 struct got_ratelimit rl;
156 if (lseek(packfd, 0L, SEEK_SET) == -1)
157 return got_error_from_errno("lseek");
159 got_ratelimit_init(&rl, 0, 500);
162 r = read(packfd, buf, sizeof(buf));
164 return got_error_from_errno("read");
167 err = got_poll_write_full(sendfd, buf, r);
171 err = send_upload_progress(ibuf, wtotal, &rl);
176 return send_upload_progress(ibuf, wtotal, NULL);
179 static const struct got_error *
180 send_error(const char *buf, size_t len)
182 static char msg[1024];
185 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
186 if (!isprint((unsigned char)buf[i]))
187 return got_error_msg(GOT_ERR_BAD_PACKET,
188 "non-printable error message received from server");
192 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
195 static const struct got_error *
196 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
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, sizeof(*refid)) == -1)
212 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
213 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
214 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
215 if (imsg_add(wbuf, refname, reflen) == -1)
216 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
218 imsg_close(ibuf, wbuf);
219 return got_privsep_flush_imsg(ibuf);
222 static const struct got_error *
223 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
224 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
227 size_t i, len, reflen, errmsglen = 0;
228 struct got_pathlist_entry *pe;
231 const char *errmsg = "";
233 eol = strchr(refname, '\n');
235 return got_error_msg(GOT_ERR_BAD_PACKET,
236 "unexpected message from server");
240 sp = strchr(refname, ' ');
244 errmsglen = strlen(errmsg);
246 for (i = 0; i < errmsglen; ++i) {
247 if (!isprint((unsigned char)errmsg[i])) {
248 return got_error_msg(GOT_ERR_BAD_PACKET,
249 "non-printable error message received "
255 reflen = strlen(refname);
256 if (!got_ref_name_is_valid(refname)) {
257 return got_error_msg(GOT_ERR_BAD_PACKET,
258 "unexpected message from server");
261 TAILQ_FOREACH(pe, refs, entry) {
262 if (strcmp(refname, pe->path) == 0) {
268 TAILQ_FOREACH(pe, delete_refs, entry) {
269 if (strcmp(refname, pe->path) == 0) {
276 return got_error_msg(GOT_ERR_BAD_PACKET,
277 "unexpected message from server");
280 len = sizeof(struct got_imsg_send_ref_status) + reflen + errmsglen;
281 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
282 return got_error(GOT_ERR_NO_SPACE);
284 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
287 return got_error_from_errno("imsg_create SEND_REF_STATUS");
289 /* Keep in sync with struct got_imsg_send_ref_status definition! */
290 if (imsg_add(wbuf, &success, sizeof(success)) == -1)
291 return got_error_from_errno("imsg_add SEND_REF_STATUS");
292 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
293 return got_error_from_errno("imsg_add SEND_REF_STATUS");
294 if (imsg_add(wbuf, &errmsglen, sizeof(errmsglen)) == -1)
295 return got_error_from_errno("imsg_add SEND_REF_STATUS");
296 if (imsg_add(wbuf, refname, reflen) == -1)
297 return got_error_from_errno("imsg_add SEND_REF_STATUS");
298 if (imsg_add(wbuf, errmsg, errmsglen) == -1)
299 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,
372 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
373 err = send_error(&buf[4], n - 4);
378 err = got_gitproto_parse_refline(&id_str, &refname,
379 &server_capabilities, buf, n);
383 if (server_capabilities == NULL) {
384 server_capabilities = strdup("");
385 if (server_capabilities == NULL) {
386 err = got_error_from_errno("strdup");
390 if (chattygot && server_capabilities[0] != '\0')
391 fprintf(stderr, "%s: server capabilities: %s\n",
392 getprogname(), server_capabilities);
393 err = got_gitproto_match_capabilities(&my_capabilities,
394 NULL, server_capabilities, got_capabilities,
395 nitems(got_capabilities));
399 fprintf(stderr, "%s: my capabilities:%s\n",
401 my_capabilities ? my_capabilities : "");
404 if (strstr(refname, "^{}")) {
406 fprintf(stderr, "%s: ignoring %s\n",
407 getprogname(), refname);
412 id = malloc(sizeof(*id));
414 err = got_error_from_errno("malloc");
417 if (!got_parse_object_id(id, id_str, GOT_HASH_SHA1)) {
418 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
421 err = send_their_ref(ibuf, id, refname);
425 err = got_pathlist_append(&their_refs, refname, id);
430 fprintf(stderr, "%s: remote has %s %s\n",
431 getprogname(), refname, id_str);
434 refname = NULL; /* do not free; owned by their_refs */
435 id = NULL; /* do not free; owned by their_refs */
438 if (!TAILQ_EMPTY(delete_refs)) {
439 if (my_capabilities == NULL ||
440 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
441 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
446 TAILQ_FOREACH(pe, delete_refs, entry) {
447 const char *refname = pe->path;
448 struct got_pathlist_entry *their_pe;
449 struct got_object_id *their_id = NULL;
451 TAILQ_FOREACH(their_pe, &their_refs, entry) {
452 const char *their_refname = their_pe->path;
453 if (got_path_cmp(refname, their_refname,
454 strlen(refname), strlen(their_refname)) == 0) {
455 their_id = their_pe->data;
459 if (their_id == NULL) {
460 err = got_error_fmt(GOT_ERR_NOT_REF,
461 "%s does not exist in remote repository",
466 got_object_id_hex(their_id, old_hashstr,
467 sizeof(old_hashstr));
468 got_sha1_digest_to_str(zero_id, new_hashstr,
469 sizeof(new_hashstr));
470 err = describe_refchange(&n, &sent_my_capabilites,
471 my_capabilities, buf, sizeof(buf), refname,
472 old_hashstr, new_hashstr);
475 err = got_pkt_writepkt(fd, buf, n, chattygot);
479 fprintf(stderr, "%s: deleting %s %s\n",
480 getprogname(), refname, old_hashstr);
485 TAILQ_FOREACH(pe, refs, entry) {
486 const char *refname = pe->path;
487 struct got_object_id *id = pe->data;
488 struct got_object_id *their_id = NULL;
489 struct got_pathlist_entry *their_pe;
491 TAILQ_FOREACH(their_pe, &their_refs, entry) {
492 const char *their_refname = their_pe->path;
493 if (got_path_cmp(refname, their_refname,
494 strlen(refname), strlen(their_refname)) == 0) {
495 their_id = their_pe->data;
500 if (got_object_id_cmp(id, their_id) == 0) {
503 "%s: no change for %s\n",
504 getprogname(), refname);
508 got_object_id_hex(their_id, old_hashstr,
509 sizeof(old_hashstr));
511 got_sha1_digest_to_str(zero_id, old_hashstr,
512 sizeof(old_hashstr));
514 got_object_id_hex(id, new_hashstr, 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, INFTIM);
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,
571 err = got_error_msg(GOT_ERR_BAD_PACKET,
572 "unexpected message from server");
574 } else if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
575 err = send_error(&buf[4], n - 4);
577 } else if (strncmp(buf, "ok ", 3) == 0) {
578 err = send_ref_status(ibuf, buf + 3, 1,
582 } else if (strncmp(buf, "ng ", 3) == 0) {
583 err = send_ref_status(ibuf, buf + 3, 0,
588 err = got_error_msg(GOT_ERR_BAD_PACKET,
589 "unexpected message from server");
595 err = send_done(ibuf);
597 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
601 free(server_capabilities);
606 main(int argc, char **argv)
608 const struct got_error *err = NULL;
612 struct got_pathlist_head refs;
613 struct got_pathlist_head delete_refs;
614 struct got_imsg_send_request send_req;
615 struct got_imsg_send_ref href;
624 TAILQ_INIT(&delete_refs);
626 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
628 /* revoke access to most system calls */
629 if (pledge("stdio recvfd", NULL) == -1) {
630 err = got_error_from_errno("pledge");
631 got_privsep_send_error(&ibuf, err);
635 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
636 if (err->code == GOT_ERR_PRIVSEP_PIPE)
640 if (imsg.hdr.type == GOT_IMSG_STOP)
642 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
643 err = got_error(GOT_ERR_PRIVSEP_MSG);
646 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
647 if (datalen < sizeof(send_req)) {
648 err = got_error(GOT_ERR_PRIVSEP_LEN);
651 memcpy(&send_req, imsg.data, sizeof(send_req));
652 sendfd = imsg_get_fd(&imsg);
655 if (send_req.verbosity > 0)
656 chattygot += send_req.verbosity;
658 for (i = 0; i < send_req.nrefs; i++) {
659 struct got_object_id *id;
662 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
663 if (err->code == GOT_ERR_PRIVSEP_PIPE)
667 if (imsg.hdr.type == GOT_IMSG_STOP)
669 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
670 err = got_error(GOT_ERR_PRIVSEP_MSG);
673 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
674 if (datalen < sizeof(href)) {
675 err = got_error(GOT_ERR_PRIVSEP_LEN);
678 memcpy(&href, imsg.data, sizeof(href));
679 if (datalen - sizeof(href) < href.name_len) {
680 err = got_error(GOT_ERR_PRIVSEP_LEN);
683 refname = malloc(href.name_len + 1);
684 if (refname == NULL) {
685 err = got_error_from_errno("malloc");
688 memcpy(refname, imsg.data + sizeof(href), href.name_len);
689 refname[href.name_len] = '\0';
692 * Prevent sending of references that won't make any
693 * sense outside the local repository's context.
695 if (strncmp(refname, "refs/got/", 9) == 0 ||
696 strncmp(refname, "refs/remotes/", 13) == 0) {
697 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
702 id = malloc(sizeof(*id));
705 err = got_error_from_errno("malloc");
708 memcpy(id, &href.id, sizeof(*id));
710 err = got_pathlist_append(&delete_refs, refname, id);
712 err = got_pathlist_append(&refs, refname, id);
722 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
724 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
725 got_pathlist_free(&delete_refs, GOT_PATHLIST_FREE_ALL);
726 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
727 err = got_error_from_errno("close");
728 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
729 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
730 got_privsep_send_error(&ibuf, err);