2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
33 #include "got_error.h"
34 #include "got_serve.h"
36 #include "got_version.h"
37 #include "got_reference.h"
39 #include "got_lib_pkt.h"
40 #include "got_lib_dial.h"
41 #include "got_lib_gitproto.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_poll.h"
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 static const struct got_capability read_capabilities[] = {
52 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
53 { GOT_CAPA_OFS_DELTA, NULL },
54 { GOT_CAPA_SIDE_BAND_64K, NULL },
57 static const struct got_capability write_capabilities[] = {
58 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
59 { GOT_CAPA_OFS_DELTA, NULL },
60 { GOT_CAPA_REPORT_STATUS, NULL },
61 { GOT_CAPA_NO_THIN, NULL },
62 { GOT_CAPA_DELETE_REFS, NULL },
65 const struct got_error *
66 got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
68 const struct got_error *err = NULL;
69 size_t len, cmdlen, pathlen;
70 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
78 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
79 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
80 strlen(GOT_SERVE_CMD_SEND)) == 0)
81 cmdlen = strlen(GOT_SERVE_CMD_SEND);
82 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
83 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
84 strlen(GOT_SERVE_CMD_FETCH)) == 0)
85 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
87 return got_error(GOT_ERR_BAD_PACKET);
89 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
90 return got_error(GOT_ERR_BAD_PACKET);
92 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
93 return got_error(GOT_ERR_BAD_PATH);
95 /* Forbid linefeeds in paths, like Git does. */
96 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
97 return got_error(GOT_ERR_BAD_PATH);
99 path0 = strdup(&gitcmd[cmdlen + 1]);
101 return got_error_from_errno("strdup");
103 pathlen = strlen(path);
106 * Git clients send a shell command.
107 * Trim spaces and quotes around the path.
109 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
113 while (pathlen > 0 &&
114 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
115 path[pathlen - 1] == ' ')) {
116 path[pathlen - 1] = '\0';
120 /* Deny an empty repository path. */
121 if (path[0] == '\0' || got_path_is_root_dir(path)) {
122 err = got_error(GOT_ERR_NOT_GIT_REPO);
126 if (asprintf(&abspath, "/%s", path) == -1) {
127 err = got_error_from_errno("asprintf");
130 pathlen = strlen(abspath);
131 canonpath = malloc(pathlen);
132 if (canonpath == NULL) {
133 err = got_error_from_errno("malloc");
136 err = got_canonpath(abspath, canonpath, pathlen);
141 while (relpath[0] == '/')
143 *repo_path = strdup(relpath);
144 if (*repo_path == NULL) {
145 err = got_error_from_errno("strdup");
148 *command = strndup(gitcmd, cmdlen);
149 if (*command == NULL)
150 err = got_error_from_errno("strndup");
162 static const struct got_error *
163 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
164 uint8_t *buf, size_t bufsize)
166 struct got_capability capa[nitems(read_capabilities) + 1];
169 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
171 capa[nitems(read_capabilities)].key = "symref";
172 capa[nitems(read_capabilities)].value = symrefstr;
173 ncapa = nitems(capa);
175 ncapa = nitems(read_capabilities);
177 return got_gitproto_append_capabilities(capalen, buf, len,
178 bufsize, capa, ncapa);
181 static const struct got_error *
182 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
183 int client_is_reading, const char *symrefstr, int chattygot)
185 const struct got_error *err = NULL;
186 char hex[SHA1_DIGEST_STRING_LENGTH];
187 char buf[GOT_PKT_MAX];
188 size_t len, capalen = 0;
190 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
191 return got_error(GOT_ERR_BAD_OBJ_ID);
193 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
194 if (len >= sizeof(buf))
195 return got_error(GOT_ERR_NO_SPACE);
197 if (send_capabilities) {
198 if (client_is_reading) {
199 err = append_read_capabilities(&capalen, len,
200 symrefstr, buf, sizeof(buf));
202 err = got_gitproto_append_capabilities(&capalen,
203 buf, len, sizeof(buf), write_capabilities,
204 nitems(write_capabilities));
211 if (len + 1 >= sizeof(buf))
212 return got_error(GOT_ERR_NO_SPACE);
217 return got_pkt_writepkt(outfd, buf, len, chattygot);
220 static const struct got_error *
221 send_zero_refs(int outfd, int client_is_reading, int chattygot)
223 const struct got_error *err = NULL;
224 char buf[GOT_PKT_MAX];
225 uint8_t zero[SHA1_DIGEST_LENGTH];
226 char hex[SHA1_DIGEST_STRING_LENGTH];
227 size_t len, capalen = 0;
229 memset(&zero, 0, sizeof(zero));
231 if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
232 return got_error(GOT_ERR_BAD_OBJ_ID);
234 len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
235 if (len >= sizeof(buf))
236 return got_error(GOT_ERR_NO_SPACE);
238 if (client_is_reading) {
239 err = got_gitproto_append_capabilities(&capalen, buf, len,
240 sizeof(buf), read_capabilities, nitems(read_capabilities));
244 err = got_gitproto_append_capabilities(&capalen, buf, len,
245 sizeof(buf), write_capabilities,
246 nitems(write_capabilities));
251 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
255 echo_error(const struct got_error *err, int outfd, int chattygot)
257 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
261 * Echo the error to the client on a pkt-line.
262 * The client should then terminate its session.
264 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
265 len = strlcat(buf, err->msg, sizeof(buf));
266 got_pkt_writepkt(outfd, buf, len, chattygot);
269 static const struct got_error *
270 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
271 const char *repo_path, int chattygot)
273 const struct got_error *err = NULL;
276 struct gotd_imsg_list_refs lsref;
277 struct gotd_imsg_reflist ireflist;
278 struct gotd_imsg_ref iref;
279 struct gotd_imsg_symref isymref;
281 int have_nrefs = 0, sent_capabilities = 0;
282 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
283 char *refname = NULL;
285 memset(&imsg, 0, sizeof(imsg));
286 memset(&lsref, 0, sizeof(lsref));
288 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
289 sizeof(lsref.repo_name))
290 return got_error(GOT_ERR_NO_SPACE);
291 lsref.client_is_reading = client_is_reading;
293 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
294 &lsref, sizeof(lsref)) == -1)
295 return got_error_from_errno("imsg_compose LIST_REFS");
297 err = gotd_imsg_flush(ibuf);
301 while (!have_nrefs || nrefs > 0) {
302 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
305 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
306 switch (imsg.hdr.type) {
307 case GOTD_IMSG_ERROR:
308 err = gotd_imsg_recv_error(NULL, &imsg);
310 case GOTD_IMSG_REFLIST:
311 if (have_nrefs || nrefs > 0) {
312 err = got_error(GOT_ERR_PRIVSEP_MSG);
315 if (datalen != sizeof(ireflist)) {
316 err = got_error(GOT_ERR_PRIVSEP_MSG);
319 memcpy(&ireflist, imsg.data, sizeof(ireflist));
320 nrefs = ireflist.nrefs;
323 err = send_zero_refs(outfd, client_is_reading,
327 if (!have_nrefs || nrefs == 0) {
328 err = got_error(GOT_ERR_PRIVSEP_MSG);
331 if (datalen < sizeof(iref)) {
332 err = got_error(GOT_ERR_PRIVSEP_MSG);
335 memcpy(&iref, imsg.data, sizeof(iref));
336 if (datalen != sizeof(iref) + iref.name_len) {
337 err = got_error(GOT_ERR_PRIVSEP_LEN);
340 refname = strndup(imsg.data + sizeof(iref),
342 if (refname == NULL) {
343 err = got_error_from_errno("strndup");
346 err = send_ref(outfd, iref.id, refname,
347 !sent_capabilities, client_is_reading,
353 sent_capabilities = 1;
357 case GOTD_IMSG_SYMREF:
358 if (!have_nrefs || nrefs == 0) {
359 err = got_error(GOT_ERR_PRIVSEP_MSG);
362 if (datalen < sizeof(isymref)) {
363 err = got_error(GOT_ERR_PRIVSEP_LEN);
366 memcpy(&isymref, imsg.data, sizeof(isymref));
367 if (datalen != sizeof(isymref) + isymref.name_len +
368 isymref.target_len) {
369 err = got_error(GOT_ERR_PRIVSEP_LEN);
374 * For now, we only announce one symbolic ref,
375 * as part of our capability advertisement.
377 if (sent_capabilities || symrefstr != NULL ||
378 symrefname != NULL || symreftarget != NULL)
381 symrefname = strndup(imsg.data + sizeof(isymref),
383 if (symrefname == NULL) {
384 err = got_error_from_errno("malloc");
388 symreftarget = strndup(
389 imsg.data + sizeof(isymref) + isymref.name_len,
391 if (symreftarget == NULL) {
392 err = got_error_from_errno("strndup");
396 if (asprintf(&symrefstr, "%s:%s", symrefname,
397 symreftarget) == -1) {
398 err = got_error_from_errno("asprintf");
401 err = send_ref(outfd, isymref.target_id, symrefname,
402 !sent_capabilities, client_is_reading, symrefstr,
408 sent_capabilities = 1;
413 err = got_error(GOT_ERR_PRIVSEP_MSG);
420 err = got_pkt_flushpkt(outfd, chattygot);
430 static const struct got_error *
431 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
433 const struct got_error *err;
434 char *id_str = NULL, *client_capabilities = NULL;
436 err = got_gitproto_parse_want_line(&id_str,
437 &client_capabilities, buf, len);
441 if (!got_parse_sha1_digest(id, id_str)) {
442 err = got_error_msg(GOT_ERR_BAD_PACKET,
443 "want-line with bad object ID");
447 if (client_capabilities) {
448 err = got_gitproto_match_capabilities(common_capabilities,
449 NULL, client_capabilities, read_capabilities,
450 nitems(read_capabilities));
456 free(client_capabilities);
460 static const struct got_error *
461 parse_have_line(uint8_t *id, char *buf, size_t len)
463 const struct got_error *err;
466 err = got_gitproto_parse_have_line(&id_str, buf, len);
470 if (!got_parse_sha1_digest(id, id_str)) {
471 err = got_error_msg(GOT_ERR_BAD_PACKET,
472 "have-line with bad object ID");
480 static const struct got_error *
481 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
483 const struct got_error *err = NULL;
484 struct gotd_imsg_capability icapa;
488 memset(&icapa, 0, sizeof(icapa));
490 icapa.key_len = strlen(capa->key);
491 len = sizeof(icapa) + icapa.key_len;
493 icapa.value_len = strlen(capa->value);
494 len += icapa.value_len;
497 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
499 err = got_error_from_errno("imsg_create CAPABILITY");
503 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
504 return got_error_from_errno("imsg_add CAPABILITY");
505 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
506 return got_error_from_errno("imsg_add CAPABILITY");
508 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
509 return got_error_from_errno("imsg_add CAPABILITY");
513 imsg_close(ibuf, wbuf);
518 static const struct got_error *
519 send_capabilities(int *use_sidebands, int *report_status,
520 char *capabilities_str, struct imsgbuf *ibuf)
522 const struct got_error *err = NULL;
523 struct gotd_imsg_capabilities icapas;
524 struct got_capability *capa = NULL;
527 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
532 icapas.ncapabilities = ncapa;
533 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
534 &icapas, sizeof(icapas)) == -1) {
535 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
539 for (i = 0; i < ncapa; i++) {
540 err = send_capability(&capa[i], ibuf);
544 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
547 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
555 static const struct got_error *
556 forward_flushpkt(struct imsgbuf *ibuf)
558 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
559 return got_error_from_errno("imsg_compose FLUSH");
561 return gotd_imsg_flush(ibuf);
564 static const struct got_error *
565 recv_ack(struct imsg *imsg, uint8_t *expected_id)
567 struct gotd_imsg_ack iack;
570 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
571 if (datalen != sizeof(iack))
572 return got_error(GOT_ERR_PRIVSEP_LEN);
574 memcpy(&iack, imsg->data, sizeof(iack));
575 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
576 return got_error(GOT_ERR_BAD_OBJ_ID);
581 static const struct got_error *
582 recv_nak(struct imsg *imsg, uint8_t *expected_id)
584 struct gotd_imsg_ack inak;
587 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
588 if (datalen != sizeof(inak))
589 return got_error(GOT_ERR_PRIVSEP_LEN);
591 memcpy(&inak, imsg->data, sizeof(inak));
592 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
593 return got_error(GOT_ERR_BAD_OBJ_ID);
599 static const struct got_error *
600 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
601 char *buf, size_t len, int expect_capabilities, int chattygot)
603 const struct got_error *err;
604 struct gotd_imsg_want iwant;
605 char *capabilities_str;
609 memset(&iwant, 0, sizeof(iwant));
610 memset(&imsg, 0, sizeof(imsg));
612 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
616 if (capabilities_str) {
617 if (!expect_capabilities) {
618 err = got_error_msg(GOT_ERR_BAD_PACKET,
619 "unexpected capability announcement received");
622 err = send_capabilities(use_sidebands, NULL, capabilities_str,
629 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
630 &iwant, sizeof(iwant)) == -1) {
631 err = got_error_from_errno("imsg_compose WANT");
635 err = gotd_imsg_flush(ibuf);
640 * Wait for an ACK, or an error in case the desired object
643 while (!done && err == NULL) {
644 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
647 switch (imsg.hdr.type) {
648 case GOTD_IMSG_ERROR:
649 err = gotd_imsg_recv_error(NULL, &imsg);
652 err = recv_ack(&imsg, iwant.object_id);
658 err = got_error(GOT_ERR_PRIVSEP_MSG);
665 free(capabilities_str);
669 static const struct got_error *
670 send_ack(int outfd, uint8_t *id, int chattygot)
672 char hex[SHA1_DIGEST_STRING_LENGTH];
673 char buf[GOT_PKT_MAX];
676 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
677 return got_error(GOT_ERR_BAD_OBJ_ID);
679 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
680 if (len >= sizeof(buf))
681 return got_error(GOT_ERR_NO_SPACE);
683 return got_pkt_writepkt(outfd, buf, len, chattygot);
686 static const struct got_error *
687 send_nak(int outfd, int chattygot)
692 len = snprintf(buf, sizeof(buf), "NAK\n");
693 if (len >= sizeof(buf))
694 return got_error(GOT_ERR_NO_SPACE);
696 return got_pkt_writepkt(outfd, buf, len, chattygot);
699 static const struct got_error *
700 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
701 size_t len, int chattygot)
703 const struct got_error *err;
704 struct gotd_imsg_have ihave;
708 memset(&ihave, 0, sizeof(ihave));
709 memset(&imsg, 0, sizeof(imsg));
711 err = parse_have_line(ihave.object_id, buf, len);
715 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
716 &ihave, sizeof(ihave)) == -1)
717 return got_error_from_errno("imsg_compose HAVE");
719 err = gotd_imsg_flush(ibuf);
724 * Wait for an ACK or a NAK, indicating whether a common
725 * commit object has been found.
727 while (!done && err == NULL) {
728 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
731 switch (imsg.hdr.type) {
732 case GOTD_IMSG_ERROR:
733 err = gotd_imsg_recv_error(NULL, &imsg);
736 err = recv_ack(&imsg, ihave.object_id);
740 err = send_ack(outfd, ihave.object_id,
749 err = recv_nak(&imsg, ihave.object_id);
755 err = got_error(GOT_ERR_PRIVSEP_MSG);
765 static const struct got_error *
766 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
768 const struct got_error *err;
773 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
774 return got_error_from_errno("imsg_compose DONE");
776 err = gotd_imsg_flush(ibuf);
780 while (*packfd == -1 && err == NULL) {
781 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
785 switch (imsg.hdr.type) {
786 case GOTD_IMSG_ERROR:
787 err = gotd_imsg_recv_error(NULL, &imsg);
789 case GOTD_IMSG_PACKFILE_PIPE:
793 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
796 err = got_error(GOT_ERR_PRIVSEP_MSG);
806 static const struct got_error *
807 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
809 const struct got_error *err = NULL;
810 int pack_starting = 0;
811 struct gotd_imsg_packfile_progress iprog;
812 char buf[GOT_PKT_MAX];
815 int p_deltify = 0, n;
816 const char *eol = "\r";
818 memset(&imsg, 0, sizeof(imsg));
820 while (!pack_starting && err == NULL) {
821 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
825 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
826 switch (imsg.hdr.type) {
827 case GOTD_IMSG_ERROR:
828 err = gotd_imsg_recv_error(NULL, &imsg);
830 case GOTD_IMSG_PACKFILE_READY:
834 case GOTD_IMSG_PACKFILE_PROGRESS:
835 if (datalen != sizeof(iprog)) {
836 err = got_error(GOT_ERR_PRIVSEP_LEN);
839 memcpy(&iprog, imsg.data, sizeof(iprog));
840 if (iprog.nobj_total > 0) {
841 p_deltify = (iprog.nobj_deltify * 100) /
844 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
845 n = snprintf(&buf[1], sizeof(buf) - 1,
846 "%d commits colored, "
852 if (n >= sizeof(buf) - 1)
854 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
857 err = got_error(GOT_ERR_PRIVSEP_MSG);
867 static const struct got_error *
868 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
871 const struct got_error *err = NULL;
872 char buf[GOT_PKT_MAX];
876 STATE_EXPECT_MORE_WANT,
881 enum protostate curstate = STATE_EXPECT_WANT;
882 int have_ack = 0, use_sidebands = 0, seen_have = 0;
884 size_t pack_chunksize;
886 imsg_init(&ibuf, gotd_sock);
888 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
892 while (curstate != STATE_DONE) {
895 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
899 if (curstate != STATE_EXPECT_WANT &&
900 curstate != STATE_EXPECT_MORE_WANT &&
901 curstate != STATE_EXPECT_HAVE &&
902 curstate != STATE_EXPECT_DONE) {
903 err = got_error_msg(GOT_ERR_BAD_PACKET,
904 "unexpected flush packet received");
908 if (curstate == STATE_EXPECT_WANT) {
911 * If the client does not want to fetch
912 * anything we should receive a flush
913 * packet followed by EOF.
915 r = read(infd, buf, sizeof(buf));
917 err = got_error_from_errno("read");
920 if (r == 0) /* EOF */
923 /* Zero-length field followed by payload. */
924 err = got_error_msg(GOT_ERR_BAD_PACKET,
925 "unexpected flush packet received");
929 if (curstate == STATE_EXPECT_WANT ||
930 curstate == STATE_EXPECT_MORE_WANT ||
931 curstate == STATE_EXPECT_HAVE) {
932 err = forward_flushpkt(&ibuf);
936 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
937 err = send_nak(outfd, chattygot);
941 if (curstate == STATE_EXPECT_MORE_WANT)
942 curstate = STATE_EXPECT_HAVE;
944 curstate = STATE_EXPECT_DONE;
945 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
946 if (curstate != STATE_EXPECT_WANT &&
947 curstate != STATE_EXPECT_MORE_WANT) {
948 err = got_error_msg(GOT_ERR_BAD_PACKET,
949 "unexpected 'want' packet");
952 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
953 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
956 if (curstate == STATE_EXPECT_WANT)
957 curstate = STATE_EXPECT_MORE_WANT;
958 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
959 if (curstate != STATE_EXPECT_HAVE &&
960 curstate != STATE_EXPECT_DONE) {
961 err = got_error_msg(GOT_ERR_BAD_PACKET,
962 "unexpected 'have' packet");
965 if (curstate == STATE_EXPECT_HAVE) {
966 err = recv_have(&have_ack, outfd, &ibuf,
972 curstate = STATE_EXPECT_DONE;
974 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
975 if (curstate != STATE_EXPECT_HAVE &&
976 curstate != STATE_EXPECT_DONE) {
977 err = got_error_msg(GOT_ERR_BAD_PACKET,
978 "unexpected 'done' packet");
981 err = recv_done(&packfd, outfd, &ibuf, chattygot);
984 curstate = STATE_DONE;
987 err = got_error(GOT_ERR_BAD_PACKET);
993 err = send_nak(outfd, chattygot);
999 err = relay_progress_reports(&ibuf, outfd, chattygot);
1002 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
1004 pack_chunksize = sizeof(buf);
1009 r = read(packfd, use_sidebands ? &buf[1] : buf,
1012 err = got_error_from_errno("read");
1014 } else if (r == 0) {
1015 err = got_pkt_flushpkt(outfd, chattygot);
1019 if (use_sidebands) {
1020 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1021 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1025 err = got_poll_write_full(outfd, buf, r);
1027 if (err->code == GOT_ERR_EOF)
1035 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1036 err = got_error_from_errno("close");
1038 echo_error(err, outfd, chattygot);
1042 static const struct got_error *
1043 parse_ref_update_line(char **common_capabilities, char **refname,
1044 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1046 const struct got_error *err;
1047 char *old_id_str = NULL, *new_id_str = NULL;
1048 char *client_capabilities = NULL;
1052 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1053 refname, &client_capabilities, buf, len);
1057 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1058 !got_parse_sha1_digest(new_id, new_id_str)) {
1059 err = got_error_msg(GOT_ERR_BAD_PACKET,
1060 "ref-update with bad object ID");
1063 if (!got_ref_name_is_valid(*refname)) {
1064 err = got_error_msg(GOT_ERR_BAD_PACKET,
1065 "ref-update with bad reference name");
1069 if (client_capabilities) {
1070 err = got_gitproto_match_capabilities(common_capabilities,
1071 NULL, client_capabilities, write_capabilities,
1072 nitems(write_capabilities));
1079 free(client_capabilities);
1087 static const struct got_error *
1088 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1089 char *buf, size_t len, int expect_capabilities, int chattygot)
1091 const struct got_error *err;
1092 struct gotd_imsg_ref_update iref;
1094 char *capabilities_str = NULL, *refname = NULL;
1098 memset(&iref, 0, sizeof(iref));
1099 memset(&imsg, 0, sizeof(imsg));
1101 err = parse_ref_update_line(&capabilities_str, &refname,
1102 iref.old_id, iref.new_id, buf, len);
1106 if (capabilities_str) {
1107 if (!expect_capabilities) {
1108 err = got_error_msg(GOT_ERR_BAD_PACKET,
1109 "unexpected capability announcement received");
1112 err = send_capabilities(NULL, report_status, capabilities_str,
1118 iref.name_len = strlen(refname);
1119 len = sizeof(iref) + iref.name_len;
1120 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1122 err = got_error_from_errno("imsg_create REF_UPDATE");
1126 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1127 return got_error_from_errno("imsg_add REF_UPDATE");
1128 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1129 return got_error_from_errno("imsg_add REF_UPDATE");
1131 imsg_close(ibuf, wbuf);
1133 err = gotd_imsg_flush(ibuf);
1137 /* Wait for ACK or an error. */
1138 while (!done && err == NULL) {
1139 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1142 switch (imsg.hdr.type) {
1143 case GOTD_IMSG_ERROR:
1144 err = gotd_imsg_recv_error(NULL, &imsg);
1147 err = recv_ack(&imsg, iref.new_id);
1153 err = got_error(GOT_ERR_PRIVSEP_MSG);
1160 free(capabilities_str);
1165 static const struct got_error *
1166 recv_packfile(struct imsg *imsg, int infd)
1168 const struct got_error *err = NULL;
1171 char buf[GOT_PKT_MAX];
1174 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1176 return got_error(GOT_ERR_PRIVSEP_MSG);
1179 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1182 while (!pack_done) {
1185 err = got_poll_fd(infd, POLLIN, 1);
1187 if (err->code != GOT_ERR_TIMEOUT)
1191 r = read(infd, buf, sizeof(buf));
1193 err = got_error_from_errno("read");
1198 * Git clients hang up their side of the
1199 * connection after sending the pack file.
1208 /* Detect gotd(8) closing the pack pipe when done. */
1209 err = got_poll_fd(packfd, POLLOUT, 1);
1211 if (err->code != GOT_ERR_EOF)
1217 /* Write pack data and/or detect pipe being closed. */
1218 err = got_poll_write_full(packfd, buf, r);
1220 if (err->code == GOT_ERR_EOF)
1231 static const struct got_error *
1232 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1234 const struct got_error *err = NULL;
1235 struct gotd_imsg_packfile_status istatus;
1236 char buf[GOT_PKT_MAX];
1237 size_t datalen, len;
1238 char *reason = NULL;
1240 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1241 if (datalen < sizeof(istatus))
1242 return got_error(GOT_ERR_PRIVSEP_LEN);
1243 memcpy(&istatus, imsg->data, sizeof(istatus));
1244 if (datalen != sizeof(istatus) + istatus.reason_len)
1245 return got_error(GOT_ERR_PRIVSEP_LEN);
1247 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1248 if (reason == NULL) {
1249 err = got_error_from_errno("strndup");
1254 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1256 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1257 if (len >= sizeof(buf)) {
1258 err = got_error(GOT_ERR_NO_SPACE);
1262 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1268 static const struct got_error *
1269 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1271 const struct got_error *err = NULL;
1272 struct gotd_imsg_ref_update_ok iok;
1273 size_t datalen, len;
1274 char buf[GOT_PKT_MAX];
1275 char *refname = NULL;
1277 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1278 if (datalen < sizeof(iok))
1279 return got_error(GOT_ERR_PRIVSEP_LEN);
1280 memcpy(&iok, imsg->data, sizeof(iok));
1281 if (datalen != sizeof(iok) + iok.name_len)
1282 return got_error(GOT_ERR_PRIVSEP_LEN);
1284 memcpy(&iok, imsg->data, sizeof(iok));
1286 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1287 if (refname == NULL)
1288 return got_error_from_errno("strndup");
1290 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1291 if (len >= sizeof(buf)) {
1292 err = got_error(GOT_ERR_NO_SPACE);
1296 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1302 static const struct got_error *
1303 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1305 const struct got_error *err = NULL;
1306 struct gotd_imsg_ref_update_ng ing;
1307 size_t datalen, len;
1308 char buf[GOT_PKT_MAX];
1309 char *refname = NULL, *reason = NULL;
1311 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1312 if (datalen < sizeof(ing))
1313 return got_error(GOT_ERR_PRIVSEP_LEN);
1314 memcpy(&ing, imsg->data, sizeof(ing));
1315 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1316 return got_error(GOT_ERR_PRIVSEP_LEN);
1318 memcpy(&ing, imsg->data, sizeof(ing));
1320 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1321 if (refname == NULL)
1322 return got_error_from_errno("strndup");
1324 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1326 if (reason == NULL) {
1327 err = got_error_from_errno("strndup");
1331 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1332 if (len >= sizeof(buf)) {
1333 err = got_error(GOT_ERR_NO_SPACE);
1337 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1344 static const struct got_error *
1345 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1348 const struct got_error *err = NULL;
1349 char buf[GOT_PKT_MAX];
1350 struct imsgbuf ibuf;
1352 STATE_EXPECT_REF_UPDATE,
1353 STATE_EXPECT_MORE_REF_UPDATES,
1354 STATE_EXPECT_PACKFILE,
1355 STATE_PACKFILE_RECEIVED,
1358 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1360 int report_status = 0;
1362 imsg_init(&ibuf, gotd_sock);
1363 memset(&imsg, 0, sizeof(imsg));
1365 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1369 while (curstate != STATE_EXPECT_PACKFILE) {
1372 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1376 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1377 err = got_error_msg(GOT_ERR_BAD_PACKET,
1378 "unexpected flush packet received");
1381 err = forward_flushpkt(&ibuf);
1384 curstate = STATE_EXPECT_PACKFILE;
1385 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1386 if (curstate != STATE_EXPECT_REF_UPDATE &&
1387 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1388 err = got_error_msg(GOT_ERR_BAD_PACKET,
1389 "unexpected ref-update packet");
1392 if (curstate == STATE_EXPECT_REF_UPDATE) {
1393 err = recv_ref_update(&report_status,
1394 outfd, &ibuf, buf, n, 1, chattygot);
1396 err = recv_ref_update(NULL, outfd, &ibuf,
1397 buf, n, 0, chattygot);
1401 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1403 err = got_error(GOT_ERR_BAD_PACKET);
1408 while (curstate != STATE_PACKFILE_RECEIVED) {
1409 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1412 switch (imsg.hdr.type) {
1413 case GOTD_IMSG_ERROR:
1414 err = gotd_imsg_recv_error(NULL, &imsg);
1416 case GOTD_IMSG_PACKFILE_PIPE:
1417 err = recv_packfile(&imsg, infd);
1419 if (err->code != GOT_ERR_EOF)
1422 * EOF is reported when the client hangs up,
1423 * which can happen with Git clients.
1424 * The socket should stay half-open so we
1425 * can still send our reports if requested.
1429 curstate = STATE_PACKFILE_RECEIVED;
1432 err = got_error(GOT_ERR_PRIVSEP_MSG);
1441 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1442 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1445 switch (imsg.hdr.type) {
1446 case GOTD_IMSG_ERROR:
1447 err = gotd_imsg_recv_error(NULL, &imsg);
1449 case GOTD_IMSG_PACKFILE_STATUS:
1452 err = report_unpack_status(&imsg, outfd, chattygot);
1454 case GOTD_IMSG_REF_UPDATE_OK:
1457 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1459 case GOTD_IMSG_REF_UPDATE_NG:
1462 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1464 case GOTD_IMSG_REFS_UPDATED:
1465 curstate = STATE_REFS_UPDATED;
1466 err = got_pkt_flushpkt(outfd, chattygot);
1469 err = got_error(GOT_ERR_PRIVSEP_MSG);
1478 echo_error(err, outfd, chattygot);
1482 const struct got_error *
1483 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1484 int gotd_sock, int chattygot)
1486 const struct got_error *err = NULL;
1488 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1489 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1490 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1491 err = serve_write(infd, outfd, gotd_sock, repo_path,
1494 err = got_error(GOT_ERR_BAD_PACKET);