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>
37 #include "got_compat.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_hash.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"
51 #include "got_lib_pkt.h"
52 #include "got_lib_gitproto.h"
53 #include "got_lib_ratelimit.h"
54 #include "got_lib_poll.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);
138 *packfd = imsg_get_fd(&imsg);
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 err = got_poll_write_full(sendfd, buf, r);
168 err = send_upload_progress(ibuf, wtotal, &rl);
173 return send_upload_progress(ibuf, wtotal, NULL);
176 static const struct got_error *
177 send_error(const char *buf, size_t len)
179 static char msg[1024];
182 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
183 if (!isprint((unsigned char)buf[i]))
184 return got_error_msg(GOT_ERR_BAD_PACKET,
185 "non-printable error message received from server");
189 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
192 static const struct got_error *
193 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
197 size_t len, reflen = strlen(refname);
199 len = sizeof(struct got_imsg_send_remote_ref) + reflen;
200 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
201 return got_error(GOT_ERR_NO_SPACE);
203 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
205 return got_error_from_errno("imsg_create SEND_REMOTE_REF");
207 /* Keep in sync with struct got_imsg_send_remote_ref definition! */
208 if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
209 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
210 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
211 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
212 if (imsg_add(wbuf, refname, reflen) == -1)
213 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
215 imsg_close(ibuf, wbuf);
216 return got_privsep_flush_imsg(ibuf);
219 static const struct got_error *
220 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
221 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
224 size_t i, len, reflen, errmsglen = 0;
225 struct got_pathlist_entry *pe;
228 const char *errmsg = "";
230 eol = strchr(refname, '\n');
232 return got_error_msg(GOT_ERR_BAD_PACKET,
233 "unexpected message from server");
237 sp = strchr(refname, ' ');
241 errmsglen = strlen(errmsg);
243 for (i = 0; i < errmsglen; ++i) {
244 if (!isprint((unsigned char)errmsg[i])) {
245 return got_error_msg(GOT_ERR_BAD_PACKET,
246 "non-printable error message received "
252 reflen = strlen(refname);
253 if (!got_ref_name_is_valid(refname)) {
254 return got_error_msg(GOT_ERR_BAD_PACKET,
255 "unexpected message from server");
258 TAILQ_FOREACH(pe, refs, entry) {
259 if (strcmp(refname, pe->path) == 0) {
265 TAILQ_FOREACH(pe, delete_refs, entry) {
266 if (strcmp(refname, pe->path) == 0) {
273 return got_error_msg(GOT_ERR_BAD_PACKET,
274 "unexpected message from server");
277 len = sizeof(struct got_imsg_send_ref_status) + reflen + errmsglen;
278 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
279 return got_error(GOT_ERR_NO_SPACE);
281 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
284 return got_error_from_errno("imsg_create SEND_REF_STATUS");
286 /* Keep in sync with struct got_imsg_send_ref_status definition! */
287 if (imsg_add(wbuf, &success, sizeof(success)) == -1)
288 return got_error_from_errno("imsg_add SEND_REF_STATUS");
289 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
290 return got_error_from_errno("imsg_add SEND_REF_STATUS");
291 if (imsg_add(wbuf, &errmsglen, sizeof(errmsglen)) == -1)
292 return got_error_from_errno("imsg_add SEND_REF_STATUS");
293 if (imsg_add(wbuf, refname, reflen) == -1)
294 return got_error_from_errno("imsg_add SEND_REF_STATUS");
295 if (imsg_add(wbuf, errmsg, errmsglen) == -1)
296 return got_error_from_errno("imsg_add SEND_REF_STATUS");
298 imsg_close(ibuf, wbuf);
299 return got_privsep_flush_imsg(ibuf);
302 static const struct got_error *
303 describe_refchange(int *n, int *sent_my_capabilites,
304 const char *my_capabilities, char *buf, size_t bufsize,
305 const char *refname, const char *old_hashstr, const char *new_hashstr)
307 *n = snprintf(buf, bufsize, "%s %s %s",
308 old_hashstr, new_hashstr, refname);
309 if (*n < 0 || (size_t)*n >= bufsize)
310 return got_error(GOT_ERR_NO_SPACE);
313 * We must announce our capabilities along with the first
314 * reference. Unfortunately, the protocol requires an embedded
315 * NUL as a separator between reference name and capabilities,
316 * which we have to deal with here.
317 * It also requires a linefeed for terminating packet data.
319 if (!*sent_my_capabilites && my_capabilities != NULL) {
321 if (*n >= bufsize - 1)
322 return got_error(GOT_ERR_NO_SPACE);
323 m = snprintf(buf + *n + 1, /* offset after '\0' */
324 bufsize - (*n + 1), "%s\n", my_capabilities);
325 if (m < 0 || *n + m >= bufsize)
326 return got_error(GOT_ERR_NO_SPACE);
328 *sent_my_capabilites = 1;
330 *n = strlcat(buf, "\n", bufsize);
332 return got_error(GOT_ERR_NO_SPACE);
338 static const struct got_error *
339 send_pack(int fd, struct got_pathlist_head *refs,
340 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
342 const struct got_error *err = NULL;
343 char buf[GOT_PKT_MAX];
344 const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
345 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
346 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
347 struct got_pathlist_head their_refs;
351 char *id_str = NULL, *refname = NULL;
352 struct got_object_id *id = NULL;
353 char *server_capabilities = NULL, *my_capabilities = NULL;
354 struct got_pathlist_entry *pe;
355 int sent_my_capabilites = 0;
357 TAILQ_INIT(&their_refs);
359 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
360 return got_error(GOT_ERR_SEND_EMPTY);
363 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
368 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
369 err = send_error(&buf[4], n - 4);
374 err = got_gitproto_parse_refline(&id_str, &refname,
375 &server_capabilities, buf, n);
379 if (server_capabilities == NULL) {
380 server_capabilities = strdup("");
381 if (server_capabilities == NULL) {
382 err = got_error_from_errno("strdup");
386 if (chattygot && server_capabilities[0] != '\0')
387 fprintf(stderr, "%s: server capabilities: %s\n",
388 getprogname(), server_capabilities);
389 err = got_gitproto_match_capabilities(&my_capabilities,
390 NULL, server_capabilities, got_capabilities,
391 nitems(got_capabilities));
395 fprintf(stderr, "%s: my capabilities:%s\n",
397 my_capabilities ? my_capabilities : "");
400 if (strstr(refname, "^{}")) {
402 fprintf(stderr, "%s: ignoring %s\n",
403 getprogname(), refname);
408 id = malloc(sizeof(*id));
410 err = got_error_from_errno("malloc");
413 if (!got_parse_object_id(id, id_str, GOT_HASH_SHA1)) {
414 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
417 err = send_their_ref(ibuf, id, refname);
421 err = got_pathlist_append(&their_refs, refname, id);
426 fprintf(stderr, "%s: remote has %s %s\n",
427 getprogname(), refname, id_str);
430 refname = NULL; /* do not free; owned by their_refs */
431 id = NULL; /* do not free; owned by their_refs */
434 if (!TAILQ_EMPTY(delete_refs)) {
435 if (my_capabilities == NULL ||
436 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
437 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
442 TAILQ_FOREACH(pe, delete_refs, entry) {
443 const char *refname = pe->path;
444 struct got_pathlist_entry *their_pe;
445 struct got_object_id *their_id = NULL;
447 TAILQ_FOREACH(their_pe, &their_refs, entry) {
448 const char *their_refname = their_pe->path;
449 if (got_path_cmp(refname, their_refname,
450 strlen(refname), strlen(their_refname)) == 0) {
451 their_id = their_pe->data;
455 if (their_id == NULL) {
456 err = got_error_fmt(GOT_ERR_NOT_REF,
457 "%s does not exist in remote repository",
462 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
463 sizeof(old_hashstr));
464 got_sha1_digest_to_str(zero_id, new_hashstr,
465 sizeof(new_hashstr));
466 err = describe_refchange(&n, &sent_my_capabilites,
467 my_capabilities, buf, sizeof(buf), refname,
468 old_hashstr, new_hashstr);
471 err = got_pkt_writepkt(fd, buf, n, chattygot);
475 fprintf(stderr, "%s: deleting %s %s\n",
476 getprogname(), refname, old_hashstr);
481 TAILQ_FOREACH(pe, refs, entry) {
482 const char *refname = pe->path;
483 struct got_object_id *id = pe->data;
484 struct got_object_id *their_id = NULL;
485 struct got_pathlist_entry *their_pe;
487 TAILQ_FOREACH(their_pe, &their_refs, entry) {
488 const char *their_refname = their_pe->path;
489 if (got_path_cmp(refname, their_refname,
490 strlen(refname), strlen(their_refname)) == 0) {
491 their_id = their_pe->data;
496 if (got_object_id_cmp(id, their_id) == 0) {
499 "%s: no change for %s\n",
500 getprogname(), refname);
504 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
505 sizeof(old_hashstr));
507 got_sha1_digest_to_str(zero_id, old_hashstr,
508 sizeof(old_hashstr));
510 got_sha1_digest_to_str(id->sha1, new_hashstr,
511 sizeof(new_hashstr));
512 err = describe_refchange(&n, &sent_my_capabilites,
513 my_capabilities, buf, sizeof(buf), refname,
514 old_hashstr, new_hashstr);
517 err = got_pkt_writepkt(fd, buf, n, chattygot);
522 fprintf(stderr, "%s: updating %s %s -> %s\n",
523 getprogname(), refname, old_hashstr,
526 fprintf(stderr, "%s: creating %s %s\n",
527 getprogname(), refname, new_hashstr);
532 err = got_pkt_flushpkt(fd, chattygot);
536 err = send_pack_request(ibuf);
540 err = recv_packfd(&packfd, ibuf);
545 err = send_pack_file(fd, packfd, ibuf);
550 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
553 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
554 err = send_error(&buf[4], n - 4);
556 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
557 err = got_error_msg(GOT_ERR_BAD_PACKET,
558 "unexpected message from server");
563 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
567 err = got_error_msg(GOT_ERR_BAD_PACKET,
568 "unexpected message from server");
570 } else if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
571 err = send_error(&buf[4], n - 4);
573 } else if (strncmp(buf, "ok ", 3) == 0) {
574 err = send_ref_status(ibuf, buf + 3, 1,
578 } else if (strncmp(buf, "ng ", 3) == 0) {
579 err = send_ref_status(ibuf, buf + 3, 0,
584 err = got_error_msg(GOT_ERR_BAD_PACKET,
585 "unexpected message from server");
591 err = send_done(ibuf);
593 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
597 free(server_capabilities);
602 main(int argc, char **argv)
604 const struct got_error *err = NULL;
608 struct got_pathlist_head refs;
609 struct got_pathlist_head delete_refs;
610 struct got_imsg_send_request send_req;
611 struct got_imsg_send_ref href;
620 TAILQ_INIT(&delete_refs);
622 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
624 /* revoke access to most system calls */
625 if (pledge("stdio recvfd", NULL) == -1) {
626 err = got_error_from_errno("pledge");
627 got_privsep_send_error(&ibuf, err);
631 /* revoke fs access */
632 if (landlock_no_fs() == -1) {
633 err = got_error_from_errno("landlock_no_fs");
634 got_privsep_send_error(&ibuf, err);
637 if (cap_enter() == -1) {
638 err = got_error_from_errno("cap_enter");
639 got_privsep_send_error(&ibuf, err);
643 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
644 if (err->code == GOT_ERR_PRIVSEP_PIPE)
648 if (imsg.hdr.type == GOT_IMSG_STOP)
650 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
651 err = got_error(GOT_ERR_PRIVSEP_MSG);
654 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
655 if (datalen < sizeof(send_req)) {
656 err = got_error(GOT_ERR_PRIVSEP_LEN);
659 memcpy(&send_req, imsg.data, sizeof(send_req));
660 sendfd = imsg_get_fd(&imsg);
663 if (send_req.verbosity > 0)
664 chattygot += send_req.verbosity;
666 for (i = 0; i < send_req.nrefs; i++) {
667 struct got_object_id *id;
670 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
671 if (err->code == GOT_ERR_PRIVSEP_PIPE)
675 if (imsg.hdr.type == GOT_IMSG_STOP)
677 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
678 err = got_error(GOT_ERR_PRIVSEP_MSG);
681 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
682 if (datalen < sizeof(href)) {
683 err = got_error(GOT_ERR_PRIVSEP_LEN);
686 memcpy(&href, imsg.data, sizeof(href));
687 if (datalen - sizeof(href) < href.name_len) {
688 err = got_error(GOT_ERR_PRIVSEP_LEN);
691 refname = malloc(href.name_len + 1);
692 if (refname == NULL) {
693 err = got_error_from_errno("malloc");
696 memcpy(refname, imsg.data + sizeof(href), href.name_len);
697 refname[href.name_len] = '\0';
700 * Prevent sending of references that won't make any
701 * sense outside the local repository's context.
703 if (strncmp(refname, "refs/got/", 9) == 0 ||
704 strncmp(refname, "refs/remotes/", 13) == 0) {
705 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
710 id = malloc(sizeof(*id));
713 err = got_error_from_errno("malloc");
716 memcpy(id, &href.id, sizeof(*id));
718 err = got_pathlist_append(&delete_refs, refname, id);
720 err = got_pathlist_append(&refs, refname, id);
730 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
732 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
733 got_pathlist_free(&delete_refs, GOT_PATHLIST_FREE_ALL);
734 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
735 err = got_error_from_errno("close");
736 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
737 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
738 got_privsep_send_error(&ibuf, err);