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>
37 #include "got_error.h"
38 #include "got_object.h"
40 #include "got_version.h"
41 #include "got_fetch.h"
42 #include "got_reference.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_pkt.h"
51 #include "got_lib_gitproto.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 struct got_object *indexed;
60 static const struct got_capability got_capabilities[] = {
61 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
62 { GOT_CAPA_OFS_DELTA, NULL },
64 { GOT_CAPA_SIDE_BAND_64K, NULL },
66 { GOT_CAPA_REPORT_STATUS, NULL },
67 { GOT_CAPA_DELETE_REFS, NULL },
70 static const struct got_error *
71 send_upload_progress(struct imsgbuf *ibuf, off_t bytes)
73 if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
74 &bytes, sizeof(bytes)) == -1)
75 return got_error_from_errno(
76 "imsg_compose SEND_UPLOAD_PROGRESS");
78 return got_privsep_flush_imsg(ibuf);
81 static const struct got_error *
82 send_pack_request(struct imsgbuf *ibuf)
84 if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
86 return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
87 return got_privsep_flush_imsg(ibuf);
90 static const struct got_error *
91 send_done(struct imsgbuf *ibuf)
93 if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
94 return got_error_from_errno("imsg_compose SEND_DONE");
95 return got_privsep_flush_imsg(ibuf);
98 static const struct got_error *
99 recv_packfd(int *packfd, struct imsgbuf *ibuf)
101 const struct got_error *err;
106 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
110 if (imsg.hdr.type == GOT_IMSG_STOP) {
111 err = got_error(GOT_ERR_CANCELLED);
115 if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
116 err = got_error(GOT_ERR_PRIVSEP_MSG);
120 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
121 err = got_error(GOT_ERR_PRIVSEP_LEN);
131 static const struct got_error *
132 send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
134 const struct got_error *err;
135 unsigned char buf[8192];
139 if (lseek(packfd, 0L, SEEK_SET) == -1)
140 return got_error_from_errno("lseek");
143 r = read(packfd, buf, sizeof(buf));
145 return got_error_from_errno("read");
148 w = write(sendfd, buf, r);
150 return got_error_from_errno("write");
152 return got_error(GOT_ERR_IO);
154 err = send_upload_progress(ibuf, wtotal);
162 static const struct got_error *
163 send_error(const char *buf, size_t len)
165 static char msg[1024];
168 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
169 if (!isprint(buf[i]))
170 return got_error_msg(GOT_ERR_BAD_PACKET,
171 "non-printable error message received from server");
175 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
178 static const struct got_error *
179 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
182 const struct got_error *err = NULL;
184 size_t len, reflen = strlen(refname);
186 len = sizeof(struct got_imsg_send_remote_ref) + reflen;
187 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
188 return got_error(GOT_ERR_NO_SPACE);
190 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
192 return got_error_from_errno("imsg_create SEND_REMOTE_REF");
194 /* Keep in sync with struct got_imsg_send_remote_ref definition! */
195 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
196 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
200 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
201 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
205 if (imsg_add(wbuf, refname, reflen) == -1) {
206 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
212 imsg_close(ibuf, wbuf);
213 return got_privsep_flush_imsg(ibuf);
216 static const struct got_error *
217 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
218 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
221 const struct got_error *err = NULL;
223 size_t len, reflen = strlen(refname);
224 struct got_pathlist_entry *pe;
228 eol = strchr(refname, '\n');
230 return got_error_msg(GOT_ERR_BAD_PACKET,
231 "unexpected message from server");
235 TAILQ_FOREACH(pe, refs, entry) {
236 if (strcmp(refname, pe->path) == 0) {
242 TAILQ_FOREACH(pe, delete_refs, entry) {
243 if (strcmp(refname, pe->path) == 0) {
250 return got_error_msg(GOT_ERR_BAD_PACKET,
251 "unexpected message from server");
254 len = sizeof(struct got_imsg_send_ref_status) + reflen;
255 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
256 return got_error(GOT_ERR_NO_SPACE);
258 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
261 return got_error_from_errno("imsg_create SEND_REF_STATUS");
263 /* Keep in sync with struct got_imsg_send_ref_status definition! */
264 if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
265 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
269 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
270 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
274 if (imsg_add(wbuf, refname, reflen) == -1) {
275 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
281 imsg_close(ibuf, wbuf);
282 return got_privsep_flush_imsg(ibuf);
285 static const struct got_error *
286 describe_refchange(int *n, int *sent_my_capabilites,
287 const char *my_capabilities, char *buf, size_t bufsize,
288 const char *refname, const char *old_hashstr, const char *new_hashstr)
290 *n = snprintf(buf, bufsize, "%s %s %s",
291 old_hashstr, new_hashstr, refname);
293 return got_error(GOT_ERR_NO_SPACE);
296 * We must announce our capabilities along with the first
297 * reference. Unfortunately, the protocol requires an embedded
298 * NUL as a separator between reference name and capabilities,
299 * which we have to deal with here.
300 * It also requires a linefeed for terminating packet data.
302 if (!*sent_my_capabilites && my_capabilities != NULL) {
304 if (*n >= bufsize - 1)
305 return got_error(GOT_ERR_NO_SPACE);
306 m = snprintf(buf + *n + 1, /* offset after '\0' */
307 bufsize - (*n + 1), "%s\n", my_capabilities);
308 if (*n + m >= bufsize)
309 return got_error(GOT_ERR_NO_SPACE);
311 *sent_my_capabilites = 1;
313 *n = strlcat(buf, "\n", bufsize);
315 return got_error(GOT_ERR_NO_SPACE);
321 static const struct got_error *
322 send_pack(int fd, struct got_pathlist_head *refs,
323 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
325 const struct got_error *err = NULL;
326 char buf[GOT_PKT_MAX];
327 unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
328 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
329 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
330 struct got_pathlist_head their_refs;
334 char *id_str = NULL, *refname = NULL;
335 struct got_object_id *id = NULL;
336 char *server_capabilities = NULL, *my_capabilities = NULL;
337 struct got_pathlist_entry *pe;
338 int sent_my_capabilites = 0;
340 TAILQ_INIT(&their_refs);
342 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
343 return got_error(GOT_ERR_SEND_EMPTY);
346 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
351 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
352 err = send_error(&buf[4], n - 4);
355 err = got_gitproto_parse_refline(&id_str, &refname,
356 &server_capabilities, buf, n);
360 if (chattygot && server_capabilities[0] != '\0')
361 fprintf(stderr, "%s: server capabilities: %s\n",
362 getprogname(), server_capabilities);
363 err = got_gitproto_match_capabilities(&my_capabilities,
364 NULL, server_capabilities, got_capabilities,
365 nitems(got_capabilities));
369 fprintf(stderr, "%s: my capabilities:%s\n",
370 getprogname(), my_capabilities);
373 if (strstr(refname, "^{}")) {
375 fprintf(stderr, "%s: ignoring %s\n",
376 getprogname(), refname);
381 id = malloc(sizeof(*id));
383 err = got_error_from_errno("malloc");
386 if (!got_parse_sha1_digest(id->sha1, id_str)) {
387 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
390 err = send_their_ref(ibuf, id, refname);
394 err = got_pathlist_append(&their_refs, refname, id);
396 fprintf(stderr, "%s: remote has %s %s\n",
397 getprogname(), refname, id_str);
400 refname = NULL; /* do not free; owned by their_refs */
401 id = NULL; /* do not free; owned by their_refs */
404 if (!TAILQ_EMPTY(delete_refs)) {
405 if (my_capabilities == NULL ||
406 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
407 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
412 TAILQ_FOREACH(pe, delete_refs, entry) {
413 const char *refname = pe->path;
414 struct got_pathlist_entry *their_pe;
415 struct got_object_id *their_id = NULL;
417 TAILQ_FOREACH(their_pe, &their_refs, entry) {
418 const char *their_refname = their_pe->path;
419 if (got_path_cmp(refname, their_refname,
420 strlen(refname), strlen(their_refname)) == 0) {
421 their_id = their_pe->data;
425 if (their_id == NULL) {
426 err = got_error_fmt(GOT_ERR_NOT_REF,
427 "%s does not exist in remote repository",
432 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
433 sizeof(old_hashstr));
434 got_sha1_digest_to_str(zero_id, new_hashstr,
435 sizeof(new_hashstr));
436 err = describe_refchange(&n, &sent_my_capabilites,
437 my_capabilities, buf, sizeof(buf), refname,
438 old_hashstr, new_hashstr);
441 err = got_pkt_writepkt(fd, buf, n, chattygot);
445 fprintf(stderr, "%s: deleting %s %s\n",
446 getprogname(), refname, old_hashstr);
451 TAILQ_FOREACH(pe, refs, entry) {
452 const char *refname = pe->path;
453 struct got_object_id *id = pe->data;
454 struct got_object_id *their_id = NULL;
455 struct got_pathlist_entry *their_pe;
457 TAILQ_FOREACH(their_pe, &their_refs, entry) {
458 const char *their_refname = their_pe->path;
459 if (got_path_cmp(refname, their_refname,
460 strlen(refname), strlen(their_refname)) == 0) {
461 their_id = their_pe->data;
466 if (got_object_id_cmp(id, their_id) == 0) {
469 "%s: no change for %s\n",
470 getprogname(), refname);
474 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
475 sizeof(old_hashstr));
477 got_sha1_digest_to_str(zero_id, old_hashstr,
478 sizeof(old_hashstr));
480 got_sha1_digest_to_str(id->sha1, new_hashstr,
481 sizeof(new_hashstr));
482 err = describe_refchange(&n, &sent_my_capabilites,
483 my_capabilities, buf, sizeof(buf), refname,
484 old_hashstr, new_hashstr);
487 err = got_pkt_writepkt(fd, buf, n, chattygot);
492 fprintf(stderr, "%s: updating %s %s -> %s\n",
493 getprogname(), refname, old_hashstr,
496 fprintf(stderr, "%s: creating %s %s\n",
497 getprogname(), refname, new_hashstr);
502 err = got_pkt_flushpkt(fd, chattygot);
506 err = send_pack_request(ibuf);
510 err = recv_packfd(&packfd, ibuf);
515 err = send_pack_file(fd, packfd, ibuf);
520 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
523 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
524 err = send_error(&buf[4], n - 4);
526 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
527 err = got_error_msg(GOT_ERR_BAD_PACKET,
528 "unexpected message from server");
533 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
537 err = got_error_msg(GOT_ERR_BAD_PACKET,
538 "unexpected message from server");
540 } else if (strncmp(buf, "ok ", 3) == 0) {
541 err = send_ref_status(ibuf, buf + 3, 1,
545 } else if (strncmp(buf, "ng ", 3) == 0) {
546 err = send_ref_status(ibuf, buf + 3, 0,
551 err = got_error_msg(GOT_ERR_BAD_PACKET,
552 "unexpected message from server");
558 err = send_done(ibuf);
560 TAILQ_FOREACH(pe, &their_refs, entry) {
561 free((void *)pe->path);
564 got_pathlist_free(&their_refs);
568 free(server_capabilities);
573 main(int argc, char **argv)
575 const struct got_error *err = NULL;
579 struct got_pathlist_head refs;
580 struct got_pathlist_head delete_refs;
581 struct got_pathlist_entry *pe;
582 struct got_imsg_send_request send_req;
583 struct got_imsg_send_ref href;
592 TAILQ_INIT(&delete_refs);
594 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
596 /* revoke access to most system calls */
597 if (pledge("stdio recvfd", NULL) == -1) {
598 err = got_error_from_errno("pledge");
599 got_privsep_send_error(&ibuf, err);
603 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
604 if (err->code == GOT_ERR_PRIVSEP_PIPE)
608 if (imsg.hdr.type == GOT_IMSG_STOP)
610 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
611 err = got_error(GOT_ERR_PRIVSEP_MSG);
614 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
615 if (datalen < sizeof(send_req)) {
616 err = got_error(GOT_ERR_PRIVSEP_LEN);
619 memcpy(&send_req, imsg.data, sizeof(send_req));
623 if (send_req.verbosity > 0)
624 chattygot += send_req.verbosity;
626 for (i = 0; i < send_req.nrefs; i++) {
627 struct got_object_id *id;
630 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
631 if (err->code == GOT_ERR_PRIVSEP_PIPE)
635 if (imsg.hdr.type == GOT_IMSG_STOP)
637 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
638 err = got_error(GOT_ERR_PRIVSEP_MSG);
641 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
642 if (datalen < sizeof(href)) {
643 err = got_error(GOT_ERR_PRIVSEP_LEN);
646 memcpy(&href, imsg.data, sizeof(href));
647 if (datalen - sizeof(href) < href.name_len) {
648 err = got_error(GOT_ERR_PRIVSEP_LEN);
651 refname = malloc(href.name_len + 1);
652 if (refname == NULL) {
653 err = got_error_from_errno("malloc");
656 memcpy(refname, imsg.data + sizeof(href), href.name_len);
657 refname[href.name_len] = '\0';
660 * Prevent sending of references that won't make any
661 * sense outside the local repository's context.
663 if (strncmp(refname, "refs/got/", 9) == 0 ||
664 strncmp(refname, "refs/remotes/", 13) == 0) {
665 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
670 id = malloc(sizeof(*id));
673 err = got_error_from_errno("malloc");
676 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
678 err = got_pathlist_append(&delete_refs, refname, id);
680 err = got_pathlist_append(&refs, refname, id);
690 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
692 TAILQ_FOREACH(pe, &refs, entry) {
693 free((char *)pe->path);
696 got_pathlist_free(&refs);
697 TAILQ_FOREACH(pe, &delete_refs, entry) {
698 free((char *)pe->path);
701 got_pathlist_free(&delete_refs);
702 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
703 err = got_error_from_errno("close");
704 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
705 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
706 got_privsep_send_error(&ibuf, err);