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>
34 #include "got_error.h"
35 #include "got_serve.h"
37 #include "got_version.h"
38 #include "got_reference.h"
40 #include "got_lib_pkt.h"
41 #include "got_lib_dial.h"
42 #include "got_lib_gitproto.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_poll.h"
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 static const struct got_capability read_capabilities[] = {
53 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
54 { GOT_CAPA_OFS_DELTA, NULL },
55 { GOT_CAPA_SIDE_BAND_64K, NULL },
58 static const struct got_capability write_capabilities[] = {
59 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
60 { GOT_CAPA_OFS_DELTA, NULL },
61 { GOT_CAPA_REPORT_STATUS, NULL },
62 { GOT_CAPA_NO_THIN, NULL },
63 { GOT_CAPA_DELETE_REFS, NULL },
66 const struct got_error *
67 got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
69 const struct got_error *err = NULL;
70 size_t len, cmdlen, pathlen;
71 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
79 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
80 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
81 strlen(GOT_SERVE_CMD_SEND)) == 0)
82 cmdlen = strlen(GOT_SERVE_CMD_SEND);
83 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
84 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
85 strlen(GOT_SERVE_CMD_FETCH)) == 0)
86 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
88 return got_error(GOT_ERR_BAD_PACKET);
90 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
91 return got_error(GOT_ERR_BAD_PACKET);
93 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
94 return got_error(GOT_ERR_BAD_PATH);
96 /* Forbid linefeeds in paths, like Git does. */
97 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
98 return got_error(GOT_ERR_BAD_PATH);
100 path0 = strdup(&gitcmd[cmdlen + 1]);
102 return got_error_from_errno("strdup");
104 pathlen = strlen(path);
107 * Git clients send a shell command.
108 * Trim spaces and quotes around the path.
110 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
114 while (pathlen > 0 &&
115 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
116 path[pathlen - 1] == ' ')) {
117 path[pathlen - 1] = '\0';
121 /* Deny an empty repository path. */
122 if (path[0] == '\0' || got_path_is_root_dir(path)) {
123 err = got_error(GOT_ERR_NOT_GIT_REPO);
127 if (asprintf(&abspath, "/%s", path) == -1) {
128 err = got_error_from_errno("asprintf");
131 pathlen = strlen(abspath);
132 canonpath = malloc(pathlen);
133 if (canonpath == NULL) {
134 err = got_error_from_errno("malloc");
137 err = got_canonpath(abspath, canonpath, pathlen);
142 while (relpath[0] == '/')
144 *repo_path = strdup(relpath);
145 if (*repo_path == NULL) {
146 err = got_error_from_errno("strdup");
149 *command = strndup(gitcmd, cmdlen);
150 if (*command == NULL)
151 err = got_error_from_errno("strndup");
163 static const struct got_error *
164 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
165 uint8_t *buf, size_t bufsize)
167 struct got_capability capa[nitems(read_capabilities) + 1];
170 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
172 capa[nitems(read_capabilities)].key = "symref";
173 capa[nitems(read_capabilities)].value = symrefstr;
174 ncapa = nitems(capa);
176 ncapa = nitems(read_capabilities);
178 return got_gitproto_append_capabilities(capalen, buf, len,
179 bufsize, capa, ncapa);
182 static const struct got_error *
183 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
184 int client_is_reading, const char *symrefstr, int chattygot)
186 const struct got_error *err = NULL;
187 char hex[SHA1_DIGEST_STRING_LENGTH];
188 char buf[GOT_PKT_MAX];
189 size_t len, capalen = 0;
191 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
192 return got_error(GOT_ERR_BAD_OBJ_ID);
194 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
195 if (len >= sizeof(buf))
196 return got_error(GOT_ERR_NO_SPACE);
198 if (send_capabilities) {
199 if (client_is_reading) {
200 err = append_read_capabilities(&capalen, len,
201 symrefstr, buf, sizeof(buf));
203 err = got_gitproto_append_capabilities(&capalen,
204 buf, len, sizeof(buf), write_capabilities,
205 nitems(write_capabilities));
212 if (len + 1 >= sizeof(buf))
213 return got_error(GOT_ERR_NO_SPACE);
218 return got_pkt_writepkt(outfd, buf, len, chattygot);
221 static const struct got_error *
222 send_zero_refs(int outfd, int client_is_reading, int chattygot)
224 const struct got_error *err = NULL;
225 char buf[GOT_PKT_MAX];
226 uint8_t zero[SHA1_DIGEST_LENGTH];
227 char hex[SHA1_DIGEST_STRING_LENGTH];
228 size_t len, capalen = 0;
230 memset(&zero, 0, sizeof(zero));
232 if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
233 return got_error(GOT_ERR_BAD_OBJ_ID);
235 len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
236 if (len >= sizeof(buf))
237 return got_error(GOT_ERR_NO_SPACE);
239 if (client_is_reading) {
240 err = got_gitproto_append_capabilities(&capalen, buf, len,
241 sizeof(buf), read_capabilities, nitems(read_capabilities));
245 err = got_gitproto_append_capabilities(&capalen, buf, len,
246 sizeof(buf), write_capabilities,
247 nitems(write_capabilities));
252 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
256 echo_error(const struct got_error *err, int outfd, int chattygot)
258 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
262 * Echo the error to the client on a pkt-line.
263 * The client should then terminate its session.
265 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
266 len = strlcat(buf, err->msg, sizeof(buf));
267 got_pkt_writepkt(outfd, buf, len, chattygot);
270 static const struct got_error *
271 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
272 const char *repo_path, int chattygot)
274 const struct got_error *err = NULL;
277 struct gotd_imsg_list_refs lsref;
278 struct gotd_imsg_reflist ireflist;
279 struct gotd_imsg_ref iref;
280 struct gotd_imsg_symref isymref;
282 int have_nrefs = 0, sent_capabilities = 0;
283 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
284 char *refname = NULL;
286 memset(&imsg, 0, sizeof(imsg));
287 memset(&lsref, 0, sizeof(lsref));
289 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
290 sizeof(lsref.repo_name))
291 return got_error(GOT_ERR_NO_SPACE);
292 lsref.client_is_reading = client_is_reading;
294 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
295 &lsref, sizeof(lsref)) == -1)
296 return got_error_from_errno("imsg_compose LIST_REFS");
298 err = gotd_imsg_flush(ibuf);
302 while (!have_nrefs || nrefs > 0) {
303 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
306 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
307 switch (imsg.hdr.type) {
308 case GOTD_IMSG_ERROR:
309 err = gotd_imsg_recv_error(NULL, &imsg);
311 case GOTD_IMSG_REFLIST:
312 if (have_nrefs || nrefs > 0) {
313 err = got_error(GOT_ERR_PRIVSEP_MSG);
316 if (datalen != sizeof(ireflist)) {
317 err = got_error(GOT_ERR_PRIVSEP_MSG);
320 memcpy(&ireflist, imsg.data, sizeof(ireflist));
321 nrefs = ireflist.nrefs;
324 err = send_zero_refs(outfd, client_is_reading,
328 if (!have_nrefs || nrefs == 0) {
329 err = got_error(GOT_ERR_PRIVSEP_MSG);
332 if (datalen < sizeof(iref)) {
333 err = got_error(GOT_ERR_PRIVSEP_MSG);
336 memcpy(&iref, imsg.data, sizeof(iref));
337 if (datalen != sizeof(iref) + iref.name_len) {
338 err = got_error(GOT_ERR_PRIVSEP_LEN);
341 refname = strndup(imsg.data + sizeof(iref),
343 if (refname == NULL) {
344 err = got_error_from_errno("strndup");
347 err = send_ref(outfd, iref.id, refname,
348 !sent_capabilities, client_is_reading,
354 sent_capabilities = 1;
358 case GOTD_IMSG_SYMREF:
359 if (!have_nrefs || nrefs == 0) {
360 err = got_error(GOT_ERR_PRIVSEP_MSG);
363 if (datalen < sizeof(isymref)) {
364 err = got_error(GOT_ERR_PRIVSEP_LEN);
367 memcpy(&isymref, imsg.data, sizeof(isymref));
368 if (datalen != sizeof(isymref) + isymref.name_len +
369 isymref.target_len) {
370 err = got_error(GOT_ERR_PRIVSEP_LEN);
375 * For now, we only announce one symbolic ref,
376 * as part of our capability advertisement.
378 if (sent_capabilities || symrefstr != NULL ||
379 symrefname != NULL || symreftarget != NULL)
382 symrefname = strndup(imsg.data + sizeof(isymref),
384 if (symrefname == NULL) {
385 err = got_error_from_errno("malloc");
389 symreftarget = strndup(
390 imsg.data + sizeof(isymref) + isymref.name_len,
392 if (symreftarget == NULL) {
393 err = got_error_from_errno("strndup");
397 if (asprintf(&symrefstr, "%s:%s", symrefname,
398 symreftarget) == -1) {
399 err = got_error_from_errno("asprintf");
402 err = send_ref(outfd, isymref.target_id, symrefname,
403 !sent_capabilities, client_is_reading, symrefstr,
409 sent_capabilities = 1;
414 err = got_error(GOT_ERR_PRIVSEP_MSG);
421 err = got_pkt_flushpkt(outfd, chattygot);
431 static const struct got_error *
432 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
434 const struct got_error *err;
435 char *id_str = NULL, *client_capabilities = NULL;
437 err = got_gitproto_parse_want_line(&id_str,
438 &client_capabilities, buf, len);
442 if (!got_parse_sha1_digest(id, id_str)) {
443 err = got_error_msg(GOT_ERR_BAD_PACKET,
444 "want-line with bad object ID");
448 if (client_capabilities) {
449 err = got_gitproto_match_capabilities(common_capabilities,
450 NULL, client_capabilities, read_capabilities,
451 nitems(read_capabilities));
457 free(client_capabilities);
461 static const struct got_error *
462 parse_have_line(uint8_t *id, char *buf, size_t len)
464 const struct got_error *err;
467 err = got_gitproto_parse_have_line(&id_str, buf, len);
471 if (!got_parse_sha1_digest(id, id_str)) {
472 err = got_error_msg(GOT_ERR_BAD_PACKET,
473 "have-line with bad object ID");
481 static const struct got_error *
482 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
484 const struct got_error *err = NULL;
485 struct gotd_imsg_capability icapa;
489 memset(&icapa, 0, sizeof(icapa));
491 icapa.key_len = strlen(capa->key);
492 len = sizeof(icapa) + icapa.key_len;
494 icapa.value_len = strlen(capa->value);
495 len += icapa.value_len;
498 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
500 err = got_error_from_errno("imsg_create CAPABILITY");
504 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
505 return got_error_from_errno("imsg_add CAPABILITY");
506 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
507 return got_error_from_errno("imsg_add CAPABILITY");
509 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
510 return got_error_from_errno("imsg_add CAPABILITY");
514 imsg_close(ibuf, wbuf);
519 static const struct got_error *
520 send_capabilities(int *use_sidebands, int *report_status,
521 char *capabilities_str, struct imsgbuf *ibuf)
523 const struct got_error *err = NULL;
524 struct gotd_imsg_capabilities icapas;
525 struct got_capability *capa = NULL;
528 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
533 icapas.ncapabilities = ncapa;
534 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
535 &icapas, sizeof(icapas)) == -1) {
536 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
540 for (i = 0; i < ncapa; i++) {
541 err = send_capability(&capa[i], ibuf);
545 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
548 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
556 static const struct got_error *
557 forward_flushpkt(struct imsgbuf *ibuf)
559 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
560 return got_error_from_errno("imsg_compose FLUSH");
562 return gotd_imsg_flush(ibuf);
565 static const struct got_error *
566 recv_ack(struct imsg *imsg, uint8_t *expected_id)
568 struct gotd_imsg_ack iack;
571 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
572 if (datalen != sizeof(iack))
573 return got_error(GOT_ERR_PRIVSEP_LEN);
575 memcpy(&iack, imsg->data, sizeof(iack));
576 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
577 return got_error(GOT_ERR_BAD_OBJ_ID);
582 static const struct got_error *
583 recv_nak(struct imsg *imsg, uint8_t *expected_id)
585 struct gotd_imsg_ack inak;
588 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
589 if (datalen != sizeof(inak))
590 return got_error(GOT_ERR_PRIVSEP_LEN);
592 memcpy(&inak, imsg->data, sizeof(inak));
593 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
594 return got_error(GOT_ERR_BAD_OBJ_ID);
600 static const struct got_error *
601 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
602 char *buf, size_t len, int expect_capabilities, int chattygot)
604 const struct got_error *err;
605 struct gotd_imsg_want iwant;
606 char *capabilities_str;
610 memset(&iwant, 0, sizeof(iwant));
611 memset(&imsg, 0, sizeof(imsg));
613 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
617 if (capabilities_str) {
618 if (!expect_capabilities) {
619 err = got_error_msg(GOT_ERR_BAD_PACKET,
620 "unexpected capability announcement received");
623 err = send_capabilities(use_sidebands, NULL, capabilities_str,
630 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
631 &iwant, sizeof(iwant)) == -1) {
632 err = got_error_from_errno("imsg_compose WANT");
636 err = gotd_imsg_flush(ibuf);
641 * Wait for an ACK, or an error in case the desired object
644 while (!done && err == NULL) {
645 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
648 switch (imsg.hdr.type) {
649 case GOTD_IMSG_ERROR:
650 err = gotd_imsg_recv_error(NULL, &imsg);
653 err = recv_ack(&imsg, iwant.object_id);
659 err = got_error(GOT_ERR_PRIVSEP_MSG);
666 free(capabilities_str);
670 static const struct got_error *
671 send_ack(int outfd, uint8_t *id, int chattygot)
673 char hex[SHA1_DIGEST_STRING_LENGTH];
674 char buf[GOT_PKT_MAX];
677 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
678 return got_error(GOT_ERR_BAD_OBJ_ID);
680 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
681 if (len >= sizeof(buf))
682 return got_error(GOT_ERR_NO_SPACE);
684 return got_pkt_writepkt(outfd, buf, len, chattygot);
687 static const struct got_error *
688 send_nak(int outfd, int chattygot)
693 len = snprintf(buf, sizeof(buf), "NAK\n");
694 if (len >= sizeof(buf))
695 return got_error(GOT_ERR_NO_SPACE);
697 return got_pkt_writepkt(outfd, buf, len, chattygot);
700 static const struct got_error *
701 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
702 size_t len, int chattygot)
704 const struct got_error *err;
705 struct gotd_imsg_have ihave;
709 memset(&ihave, 0, sizeof(ihave));
710 memset(&imsg, 0, sizeof(imsg));
712 err = parse_have_line(ihave.object_id, buf, len);
716 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
717 &ihave, sizeof(ihave)) == -1)
718 return got_error_from_errno("imsg_compose HAVE");
720 err = gotd_imsg_flush(ibuf);
725 * Wait for an ACK or a NAK, indicating whether a common
726 * commit object has been found.
728 while (!done && err == NULL) {
729 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
732 switch (imsg.hdr.type) {
733 case GOTD_IMSG_ERROR:
734 err = gotd_imsg_recv_error(NULL, &imsg);
737 err = recv_ack(&imsg, ihave.object_id);
741 err = send_ack(outfd, ihave.object_id,
750 err = recv_nak(&imsg, ihave.object_id);
756 err = got_error(GOT_ERR_PRIVSEP_MSG);
766 static const struct got_error *
767 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
769 const struct got_error *err;
774 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
775 return got_error_from_errno("imsg_compose DONE");
777 err = gotd_imsg_flush(ibuf);
781 while (*packfd == -1 && err == NULL) {
782 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
786 switch (imsg.hdr.type) {
787 case GOTD_IMSG_ERROR:
788 err = gotd_imsg_recv_error(NULL, &imsg);
790 case GOTD_IMSG_PACKFILE_PIPE:
794 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
797 err = got_error(GOT_ERR_PRIVSEP_MSG);
807 static const struct got_error *
808 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
810 const struct got_error *err = NULL;
811 int pack_starting = 0;
812 struct gotd_imsg_packfile_progress iprog;
813 char buf[GOT_PKT_MAX];
816 int p_deltify = 0, n;
817 const char *eol = "\r";
819 memset(&imsg, 0, sizeof(imsg));
821 while (!pack_starting && err == NULL) {
822 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
826 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
827 switch (imsg.hdr.type) {
828 case GOTD_IMSG_ERROR:
829 err = gotd_imsg_recv_error(NULL, &imsg);
831 case GOTD_IMSG_PACKFILE_READY:
835 case GOTD_IMSG_PACKFILE_PROGRESS:
836 if (datalen != sizeof(iprog)) {
837 err = got_error(GOT_ERR_PRIVSEP_LEN);
840 memcpy(&iprog, imsg.data, sizeof(iprog));
841 if (iprog.nobj_total > 0) {
842 p_deltify = (iprog.nobj_deltify * 100) /
845 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
846 n = snprintf(&buf[1], sizeof(buf) - 1,
847 "%d commits colored, "
853 if (n >= sizeof(buf) - 1)
855 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
858 err = got_error(GOT_ERR_PRIVSEP_MSG);
868 static const struct got_error *
869 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
872 const struct got_error *err = NULL;
873 char buf[GOT_PKT_MAX];
877 STATE_EXPECT_MORE_WANT,
882 enum protostate curstate = STATE_EXPECT_WANT;
883 int have_ack = 0, use_sidebands = 0, seen_have = 0;
885 size_t pack_chunksize;
887 imsg_init(&ibuf, gotd_sock);
889 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
893 while (curstate != STATE_DONE) {
896 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
900 if (curstate != STATE_EXPECT_WANT &&
901 curstate != STATE_EXPECT_MORE_WANT &&
902 curstate != STATE_EXPECT_HAVE &&
903 curstate != STATE_EXPECT_DONE) {
904 err = got_error_msg(GOT_ERR_BAD_PACKET,
905 "unexpected flush packet received");
909 if (curstate == STATE_EXPECT_WANT) {
912 * If the client does not want to fetch
913 * anything we should receive a flush
914 * packet followed by EOF.
916 r = read(infd, buf, sizeof(buf));
918 err = got_error_from_errno("read");
921 if (r == 0) /* EOF */
924 /* Zero-length field followed by payload. */
925 err = got_error_msg(GOT_ERR_BAD_PACKET,
926 "unexpected flush packet received");
930 if (curstate == STATE_EXPECT_WANT ||
931 curstate == STATE_EXPECT_MORE_WANT ||
932 curstate == STATE_EXPECT_HAVE) {
933 err = forward_flushpkt(&ibuf);
937 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
938 err = send_nak(outfd, chattygot);
942 if (curstate == STATE_EXPECT_MORE_WANT)
943 curstate = STATE_EXPECT_HAVE;
945 curstate = STATE_EXPECT_DONE;
946 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
947 if (curstate != STATE_EXPECT_WANT &&
948 curstate != STATE_EXPECT_MORE_WANT) {
949 err = got_error_msg(GOT_ERR_BAD_PACKET,
950 "unexpected 'want' packet");
953 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
954 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
957 if (curstate == STATE_EXPECT_WANT)
958 curstate = STATE_EXPECT_MORE_WANT;
959 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
960 if (curstate != STATE_EXPECT_HAVE &&
961 curstate != STATE_EXPECT_DONE) {
962 err = got_error_msg(GOT_ERR_BAD_PACKET,
963 "unexpected 'have' packet");
966 if (curstate == STATE_EXPECT_HAVE) {
967 err = recv_have(&have_ack, outfd, &ibuf,
973 curstate = STATE_EXPECT_DONE;
975 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
976 if (curstate != STATE_EXPECT_HAVE &&
977 curstate != STATE_EXPECT_DONE) {
978 err = got_error_msg(GOT_ERR_BAD_PACKET,
979 "unexpected 'done' packet");
982 err = recv_done(&packfd, outfd, &ibuf, chattygot);
985 curstate = STATE_DONE;
988 err = got_error(GOT_ERR_BAD_PACKET);
994 err = send_nak(outfd, chattygot);
1000 err = relay_progress_reports(&ibuf, outfd, chattygot);
1003 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
1005 pack_chunksize = sizeof(buf);
1010 r = read(packfd, use_sidebands ? &buf[1] : buf,
1013 err = got_error_from_errno("read");
1015 } else if (r == 0) {
1016 err = got_pkt_flushpkt(outfd, chattygot);
1020 if (use_sidebands) {
1021 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1022 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1026 err = got_poll_write_full(outfd, buf, r);
1028 if (err->code == GOT_ERR_EOF)
1036 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1037 err = got_error_from_errno("close");
1039 echo_error(err, outfd, chattygot);
1043 static const struct got_error *
1044 parse_ref_update_line(char **common_capabilities, char **refname,
1045 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1047 const struct got_error *err;
1048 char *old_id_str = NULL, *new_id_str = NULL;
1049 char *client_capabilities = NULL;
1053 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1054 refname, &client_capabilities, buf, len);
1058 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1059 !got_parse_sha1_digest(new_id, new_id_str)) {
1060 err = got_error_msg(GOT_ERR_BAD_PACKET,
1061 "ref-update with bad object ID");
1064 if (!got_ref_name_is_valid(*refname)) {
1065 err = got_error_msg(GOT_ERR_BAD_PACKET,
1066 "ref-update with bad reference name");
1070 if (client_capabilities) {
1071 err = got_gitproto_match_capabilities(common_capabilities,
1072 NULL, client_capabilities, write_capabilities,
1073 nitems(write_capabilities));
1080 free(client_capabilities);
1088 static const struct got_error *
1089 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1090 char *buf, size_t len, int expect_capabilities, int chattygot)
1092 const struct got_error *err;
1093 struct gotd_imsg_ref_update iref;
1095 char *capabilities_str = NULL, *refname = NULL;
1099 memset(&iref, 0, sizeof(iref));
1100 memset(&imsg, 0, sizeof(imsg));
1102 err = parse_ref_update_line(&capabilities_str, &refname,
1103 iref.old_id, iref.new_id, buf, len);
1107 if (capabilities_str) {
1108 if (!expect_capabilities) {
1109 err = got_error_msg(GOT_ERR_BAD_PACKET,
1110 "unexpected capability announcement received");
1113 err = send_capabilities(NULL, report_status, capabilities_str,
1119 iref.name_len = strlen(refname);
1120 len = sizeof(iref) + iref.name_len;
1121 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1123 err = got_error_from_errno("imsg_create REF_UPDATE");
1127 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1128 return got_error_from_errno("imsg_add REF_UPDATE");
1129 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1130 return got_error_from_errno("imsg_add REF_UPDATE");
1132 imsg_close(ibuf, wbuf);
1134 err = gotd_imsg_flush(ibuf);
1138 /* Wait for ACK or an error. */
1139 while (!done && err == NULL) {
1140 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1143 switch (imsg.hdr.type) {
1144 case GOTD_IMSG_ERROR:
1145 err = gotd_imsg_recv_error(NULL, &imsg);
1148 err = recv_ack(&imsg, iref.new_id);
1154 err = got_error(GOT_ERR_PRIVSEP_MSG);
1161 free(capabilities_str);
1166 static const struct got_error *
1167 recv_packfile(struct imsg *imsg, int infd)
1169 const struct got_error *err = NULL;
1172 char buf[GOT_PKT_MAX];
1175 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1177 return got_error(GOT_ERR_PRIVSEP_MSG);
1180 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1183 while (!pack_done) {
1186 err = got_poll_fd(infd, POLLIN, 1);
1188 if (err->code != GOT_ERR_TIMEOUT)
1192 r = read(infd, buf, sizeof(buf));
1194 err = got_error_from_errno("read");
1199 * Git clients hang up their side of the
1200 * connection after sending the pack file.
1209 /* Detect gotd(8) closing the pack pipe when done. */
1210 err = got_poll_fd(packfd, POLLOUT, 1);
1212 if (err->code != GOT_ERR_EOF)
1218 /* Write pack data and/or detect pipe being closed. */
1219 err = got_poll_write_full(packfd, buf, r);
1221 if (err->code == GOT_ERR_EOF)
1232 static const struct got_error *
1233 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1235 const struct got_error *err = NULL;
1236 struct gotd_imsg_packfile_status istatus;
1237 char buf[GOT_PKT_MAX];
1238 size_t datalen, len;
1239 char *reason = NULL;
1241 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1242 if (datalen < sizeof(istatus))
1243 return got_error(GOT_ERR_PRIVSEP_LEN);
1244 memcpy(&istatus, imsg->data, sizeof(istatus));
1245 if (datalen != sizeof(istatus) + istatus.reason_len)
1246 return got_error(GOT_ERR_PRIVSEP_LEN);
1248 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1249 if (reason == NULL) {
1250 err = got_error_from_errno("strndup");
1255 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1257 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1258 if (len >= sizeof(buf)) {
1259 err = got_error(GOT_ERR_NO_SPACE);
1263 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1269 static const struct got_error *
1270 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1272 const struct got_error *err = NULL;
1273 struct gotd_imsg_ref_update_ok iok;
1274 size_t datalen, len;
1275 char buf[GOT_PKT_MAX];
1276 char *refname = NULL;
1278 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1279 if (datalen < sizeof(iok))
1280 return got_error(GOT_ERR_PRIVSEP_LEN);
1281 memcpy(&iok, imsg->data, sizeof(iok));
1282 if (datalen != sizeof(iok) + iok.name_len)
1283 return got_error(GOT_ERR_PRIVSEP_LEN);
1285 memcpy(&iok, imsg->data, sizeof(iok));
1287 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1288 if (refname == NULL)
1289 return got_error_from_errno("strndup");
1291 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1292 if (len >= sizeof(buf)) {
1293 err = got_error(GOT_ERR_NO_SPACE);
1297 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1303 static const struct got_error *
1304 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1306 const struct got_error *err = NULL;
1307 struct gotd_imsg_ref_update_ng ing;
1308 size_t datalen, len;
1309 char buf[GOT_PKT_MAX];
1310 char *refname = NULL, *reason = NULL;
1312 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1313 if (datalen < sizeof(ing))
1314 return got_error(GOT_ERR_PRIVSEP_LEN);
1315 memcpy(&ing, imsg->data, sizeof(ing));
1316 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1317 return got_error(GOT_ERR_PRIVSEP_LEN);
1319 memcpy(&ing, imsg->data, sizeof(ing));
1321 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1322 if (refname == NULL)
1323 return got_error_from_errno("strndup");
1325 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1327 if (reason == NULL) {
1328 err = got_error_from_errno("strndup");
1332 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1333 if (len >= sizeof(buf)) {
1334 err = got_error(GOT_ERR_NO_SPACE);
1338 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1345 static const struct got_error *
1346 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1349 const struct got_error *err = NULL;
1350 char buf[GOT_PKT_MAX];
1351 struct imsgbuf ibuf;
1353 STATE_EXPECT_REF_UPDATE,
1354 STATE_EXPECT_MORE_REF_UPDATES,
1355 STATE_EXPECT_PACKFILE,
1356 STATE_PACKFILE_RECEIVED,
1359 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1361 int report_status = 0;
1363 imsg_init(&ibuf, gotd_sock);
1364 memset(&imsg, 0, sizeof(imsg));
1366 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1370 while (curstate != STATE_EXPECT_PACKFILE) {
1373 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1377 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1378 err = got_error_msg(GOT_ERR_BAD_PACKET,
1379 "unexpected flush packet received");
1382 err = forward_flushpkt(&ibuf);
1385 curstate = STATE_EXPECT_PACKFILE;
1386 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1387 if (curstate != STATE_EXPECT_REF_UPDATE &&
1388 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1389 err = got_error_msg(GOT_ERR_BAD_PACKET,
1390 "unexpected ref-update packet");
1393 if (curstate == STATE_EXPECT_REF_UPDATE) {
1394 err = recv_ref_update(&report_status,
1395 outfd, &ibuf, buf, n, 1, chattygot);
1397 err = recv_ref_update(NULL, outfd, &ibuf,
1398 buf, n, 0, chattygot);
1402 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1404 err = got_error(GOT_ERR_BAD_PACKET);
1409 while (curstate != STATE_PACKFILE_RECEIVED) {
1410 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1413 switch (imsg.hdr.type) {
1414 case GOTD_IMSG_ERROR:
1415 err = gotd_imsg_recv_error(NULL, &imsg);
1417 case GOTD_IMSG_PACKFILE_PIPE:
1418 err = recv_packfile(&imsg, infd);
1420 if (err->code != GOT_ERR_EOF)
1423 * EOF is reported when the client hangs up,
1424 * which can happen with Git clients.
1425 * The socket should stay half-open so we
1426 * can still send our reports if requested.
1430 curstate = STATE_PACKFILE_RECEIVED;
1433 err = got_error(GOT_ERR_PRIVSEP_MSG);
1442 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1443 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1446 switch (imsg.hdr.type) {
1447 case GOTD_IMSG_ERROR:
1448 err = gotd_imsg_recv_error(NULL, &imsg);
1450 case GOTD_IMSG_PACKFILE_STATUS:
1453 err = report_unpack_status(&imsg, outfd, chattygot);
1455 case GOTD_IMSG_REF_UPDATE_OK:
1458 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1460 case GOTD_IMSG_REF_UPDATE_NG:
1463 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1465 case GOTD_IMSG_REFS_UPDATED:
1466 curstate = STATE_REFS_UPDATED;
1467 err = got_pkt_flushpkt(outfd, chattygot);
1470 err = got_error(GOT_ERR_PRIVSEP_MSG);
1479 echo_error(err, outfd, chattygot);
1483 const struct got_error *
1484 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1485 int gotd_sock, int chattygot)
1487 const struct got_error *err = NULL;
1489 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1490 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1491 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1492 err = serve_write(infd, outfd, gotd_sock, repo_path,
1495 err = got_error(GOT_ERR_BAD_PACKET);