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(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 len, reflen = strlen(refname);
227 struct got_pathlist_entry *pe;
231 eol = strchr(refname, '\n');
233 return got_error_msg(GOT_ERR_BAD_PACKET,
234 "unexpected message from server");
238 TAILQ_FOREACH(pe, refs, entry) {
239 if (strcmp(refname, pe->path) == 0) {
245 TAILQ_FOREACH(pe, delete_refs, entry) {
246 if (strcmp(refname, pe->path) == 0) {
253 return got_error_msg(GOT_ERR_BAD_PACKET,
254 "unexpected message from server");
257 len = sizeof(struct got_imsg_send_ref_status) + reflen;
258 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
259 return got_error(GOT_ERR_NO_SPACE);
261 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
264 return got_error_from_errno("imsg_create SEND_REF_STATUS");
266 /* Keep in sync with struct got_imsg_send_ref_status definition! */
267 if (imsg_add(wbuf, &success, sizeof(success)) == -1)
268 return got_error_from_errno("imsg_add SEND_REF_STATUS");
269 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
270 return got_error_from_errno("imsg_add SEND_REF_STATUS");
271 if (imsg_add(wbuf, refname, reflen) == -1)
272 return got_error_from_errno("imsg_add SEND_REF_STATUS");
275 imsg_close(ibuf, wbuf);
276 return got_privsep_flush_imsg(ibuf);
279 static const struct got_error *
280 describe_refchange(int *n, int *sent_my_capabilites,
281 const char *my_capabilities, char *buf, size_t bufsize,
282 const char *refname, const char *old_hashstr, const char *new_hashstr)
284 *n = snprintf(buf, bufsize, "%s %s %s",
285 old_hashstr, new_hashstr, refname);
286 if (*n < 0 || (size_t)*n >= bufsize)
287 return got_error(GOT_ERR_NO_SPACE);
290 * We must announce our capabilities along with the first
291 * reference. Unfortunately, the protocol requires an embedded
292 * NUL as a separator between reference name and capabilities,
293 * which we have to deal with here.
294 * It also requires a linefeed for terminating packet data.
296 if (!*sent_my_capabilites && my_capabilities != NULL) {
298 if (*n >= bufsize - 1)
299 return got_error(GOT_ERR_NO_SPACE);
300 m = snprintf(buf + *n + 1, /* offset after '\0' */
301 bufsize - (*n + 1), "%s\n", my_capabilities);
302 if (m < 0 || *n + m >= bufsize)
303 return got_error(GOT_ERR_NO_SPACE);
305 *sent_my_capabilites = 1;
307 *n = strlcat(buf, "\n", bufsize);
309 return got_error(GOT_ERR_NO_SPACE);
315 static const struct got_error *
316 send_pack(int fd, struct got_pathlist_head *refs,
317 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
319 const struct got_error *err = NULL;
320 char buf[GOT_PKT_MAX];
321 const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
322 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
323 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
324 struct got_pathlist_head their_refs;
328 char *id_str = NULL, *refname = NULL;
329 struct got_object_id *id = NULL;
330 char *server_capabilities = NULL, *my_capabilities = NULL;
331 struct got_pathlist_entry *pe;
332 int sent_my_capabilites = 0;
334 TAILQ_INIT(&their_refs);
336 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
337 return got_error(GOT_ERR_SEND_EMPTY);
340 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
345 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
346 err = send_error(&buf[4], n - 4);
351 err = got_gitproto_parse_refline(&id_str, &refname,
352 &server_capabilities, buf, n);
356 if (chattygot && server_capabilities[0] != '\0')
357 fprintf(stderr, "%s: server capabilities: %s\n",
358 getprogname(), server_capabilities);
359 err = got_gitproto_match_capabilities(&my_capabilities,
360 NULL, server_capabilities, got_capabilities,
361 nitems(got_capabilities));
365 fprintf(stderr, "%s: my capabilities:%s\n",
366 getprogname(), my_capabilities);
369 if (strstr(refname, "^{}")) {
371 fprintf(stderr, "%s: ignoring %s\n",
372 getprogname(), refname);
377 id = malloc(sizeof(*id));
379 err = got_error_from_errno("malloc");
382 if (!got_parse_sha1_digest(id->sha1, id_str)) {
383 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
386 err = send_their_ref(ibuf, id, refname);
390 err = got_pathlist_append(&their_refs, refname, id);
395 fprintf(stderr, "%s: remote has %s %s\n",
396 getprogname(), refname, id_str);
399 refname = NULL; /* do not free; owned by their_refs */
400 id = NULL; /* do not free; owned by their_refs */
403 if (!TAILQ_EMPTY(delete_refs)) {
404 if (my_capabilities == NULL ||
405 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
406 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
411 TAILQ_FOREACH(pe, delete_refs, entry) {
412 const char *refname = pe->path;
413 struct got_pathlist_entry *their_pe;
414 struct got_object_id *their_id = NULL;
416 TAILQ_FOREACH(their_pe, &their_refs, entry) {
417 const char *their_refname = their_pe->path;
418 if (got_path_cmp(refname, their_refname,
419 strlen(refname), strlen(their_refname)) == 0) {
420 their_id = their_pe->data;
424 if (their_id == NULL) {
425 err = got_error_fmt(GOT_ERR_NOT_REF,
426 "%s does not exist in remote repository",
431 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
432 sizeof(old_hashstr));
433 got_sha1_digest_to_str(zero_id, new_hashstr,
434 sizeof(new_hashstr));
435 err = describe_refchange(&n, &sent_my_capabilites,
436 my_capabilities, buf, sizeof(buf), refname,
437 old_hashstr, new_hashstr);
440 err = got_pkt_writepkt(fd, buf, n, chattygot);
444 fprintf(stderr, "%s: deleting %s %s\n",
445 getprogname(), refname, old_hashstr);
450 TAILQ_FOREACH(pe, refs, entry) {
451 const char *refname = pe->path;
452 struct got_object_id *id = pe->data;
453 struct got_object_id *their_id = NULL;
454 struct got_pathlist_entry *their_pe;
456 TAILQ_FOREACH(their_pe, &their_refs, entry) {
457 const char *their_refname = their_pe->path;
458 if (got_path_cmp(refname, their_refname,
459 strlen(refname), strlen(their_refname)) == 0) {
460 their_id = their_pe->data;
465 if (got_object_id_cmp(id, their_id) == 0) {
468 "%s: no change for %s\n",
469 getprogname(), refname);
473 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
474 sizeof(old_hashstr));
476 got_sha1_digest_to_str(zero_id, old_hashstr,
477 sizeof(old_hashstr));
479 got_sha1_digest_to_str(id->sha1, new_hashstr,
480 sizeof(new_hashstr));
481 err = describe_refchange(&n, &sent_my_capabilites,
482 my_capabilities, buf, sizeof(buf), refname,
483 old_hashstr, new_hashstr);
486 err = got_pkt_writepkt(fd, buf, n, chattygot);
491 fprintf(stderr, "%s: updating %s %s -> %s\n",
492 getprogname(), refname, old_hashstr,
495 fprintf(stderr, "%s: creating %s %s\n",
496 getprogname(), refname, new_hashstr);
501 err = got_pkt_flushpkt(fd, chattygot);
505 err = send_pack_request(ibuf);
509 err = recv_packfd(&packfd, ibuf);
514 err = send_pack_file(fd, packfd, ibuf);
519 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
522 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
523 err = send_error(&buf[4], n - 4);
525 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
526 err = got_error_msg(GOT_ERR_BAD_PACKET,
527 "unexpected message from server");
532 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
536 err = got_error_msg(GOT_ERR_BAD_PACKET,
537 "unexpected message from server");
539 } else if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
540 err = send_error(&buf[4], n - 4);
542 } else if (strncmp(buf, "ok ", 3) == 0) {
543 err = send_ref_status(ibuf, buf + 3, 1,
547 } else if (strncmp(buf, "ng ", 3) == 0) {
548 err = send_ref_status(ibuf, buf + 3, 0,
553 err = got_error_msg(GOT_ERR_BAD_PACKET,
554 "unexpected message from server");
560 err = send_done(ibuf);
562 TAILQ_FOREACH(pe, &their_refs, entry) {
563 free((void *)pe->path);
566 got_pathlist_free(&their_refs);
570 free(server_capabilities);
575 main(int argc, char **argv)
577 const struct got_error *err = NULL;
581 struct got_pathlist_head refs;
582 struct got_pathlist_head delete_refs;
583 struct got_pathlist_entry *pe;
584 struct got_imsg_send_request send_req;
585 struct got_imsg_send_ref href;
594 TAILQ_INIT(&delete_refs);
596 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
598 /* revoke access to most system calls */
599 if (pledge("stdio recvfd", NULL) == -1) {
600 err = got_error_from_errno("pledge");
601 got_privsep_send_error(&ibuf, err);
605 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
606 if (err->code == GOT_ERR_PRIVSEP_PIPE)
610 if (imsg.hdr.type == GOT_IMSG_STOP)
612 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
613 err = got_error(GOT_ERR_PRIVSEP_MSG);
616 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
617 if (datalen < sizeof(send_req)) {
618 err = got_error(GOT_ERR_PRIVSEP_LEN);
621 memcpy(&send_req, imsg.data, sizeof(send_req));
625 if (send_req.verbosity > 0)
626 chattygot += send_req.verbosity;
628 for (i = 0; i < send_req.nrefs; i++) {
629 struct got_object_id *id;
632 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
633 if (err->code == GOT_ERR_PRIVSEP_PIPE)
637 if (imsg.hdr.type == GOT_IMSG_STOP)
639 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
640 err = got_error(GOT_ERR_PRIVSEP_MSG);
643 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
644 if (datalen < sizeof(href)) {
645 err = got_error(GOT_ERR_PRIVSEP_LEN);
648 memcpy(&href, imsg.data, sizeof(href));
649 if (datalen - sizeof(href) < href.name_len) {
650 err = got_error(GOT_ERR_PRIVSEP_LEN);
653 refname = malloc(href.name_len + 1);
654 if (refname == NULL) {
655 err = got_error_from_errno("malloc");
658 memcpy(refname, imsg.data + sizeof(href), href.name_len);
659 refname[href.name_len] = '\0';
662 * Prevent sending of references that won't make any
663 * sense outside the local repository's context.
665 if (strncmp(refname, "refs/got/", 9) == 0 ||
666 strncmp(refname, "refs/remotes/", 13) == 0) {
667 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
672 id = malloc(sizeof(*id));
675 err = got_error_from_errno("malloc");
678 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
680 err = got_pathlist_append(&delete_refs, refname, id);
682 err = got_pathlist_append(&refs, refname, id);
692 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
694 TAILQ_FOREACH(pe, &refs, entry) {
695 free((char *)pe->path);
698 got_pathlist_free(&refs);
699 TAILQ_FOREACH(pe, &delete_refs, entry) {
700 free((char *)pe->path);
703 got_pathlist_free(&delete_refs);
704 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
705 err = got_error_from_errno("close");
706 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
707 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
708 got_privsep_send_error(&ibuf, err);