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"
39 #include "got_object.h"
41 #include "got_lib_pkt.h"
42 #include "got_lib_dial.h"
43 #include "got_lib_gitproto.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_poll.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static const struct got_capability read_capabilities[] = {
54 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
55 { GOT_CAPA_OFS_DELTA, NULL },
56 { GOT_CAPA_SIDE_BAND_64K, NULL },
59 static const struct got_capability write_capabilities[] = {
60 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
61 { GOT_CAPA_OFS_DELTA, NULL },
62 { GOT_CAPA_REPORT_STATUS, NULL },
63 { GOT_CAPA_NO_THIN, NULL },
64 { GOT_CAPA_DELETE_REFS, NULL },
67 const struct got_error *
68 got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
70 const struct got_error *err = NULL;
71 size_t len, cmdlen, pathlen;
72 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
80 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
81 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
82 strlen(GOT_SERVE_CMD_SEND)) == 0)
83 cmdlen = strlen(GOT_SERVE_CMD_SEND);
84 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
85 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
86 strlen(GOT_SERVE_CMD_FETCH)) == 0)
87 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
89 return got_error(GOT_ERR_BAD_PACKET);
91 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
92 return got_error(GOT_ERR_BAD_PACKET);
94 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
95 return got_error(GOT_ERR_BAD_PATH);
97 /* Forbid linefeeds in paths, like Git does. */
98 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
99 return got_error(GOT_ERR_BAD_PATH);
101 path0 = strdup(&gitcmd[cmdlen + 1]);
103 return got_error_from_errno("strdup");
105 pathlen = strlen(path);
108 * Git clients send a shell command.
109 * Trim spaces and quotes around the path.
111 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
115 while (pathlen > 0 &&
116 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
117 path[pathlen - 1] == ' ')) {
118 path[pathlen - 1] = '\0';
122 /* Deny an empty repository path. */
123 if (path[0] == '\0' || got_path_is_root_dir(path)) {
124 err = got_error(GOT_ERR_NOT_GIT_REPO);
128 if (asprintf(&abspath, "/%s", path) == -1) {
129 err = got_error_from_errno("asprintf");
132 pathlen = strlen(abspath);
133 canonpath = malloc(pathlen);
134 if (canonpath == NULL) {
135 err = got_error_from_errno("malloc");
138 err = got_canonpath(abspath, canonpath, pathlen);
143 while (relpath[0] == '/')
145 *repo_path = strdup(relpath);
146 if (*repo_path == NULL) {
147 err = got_error_from_errno("strdup");
150 *command = strndup(gitcmd, cmdlen);
151 if (*command == NULL)
152 err = got_error_from_errno("strndup");
164 static const struct got_error *
165 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
166 uint8_t *buf, size_t bufsize)
168 struct got_capability capa[nitems(read_capabilities) + 1];
171 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
173 capa[nitems(read_capabilities)].key = "symref";
174 capa[nitems(read_capabilities)].value = symrefstr;
175 ncapa = nitems(capa);
177 ncapa = nitems(read_capabilities);
179 return got_gitproto_append_capabilities(capalen, buf, len,
180 bufsize, capa, ncapa);
183 static const struct got_error *
184 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
185 int client_is_reading, const char *symrefstr, int chattygot)
187 const struct got_error *err = NULL;
188 char hex[SHA1_DIGEST_STRING_LENGTH];
189 char buf[GOT_PKT_MAX];
190 size_t len, capalen = 0;
192 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
193 return got_error(GOT_ERR_BAD_OBJ_ID);
195 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
196 if (len >= sizeof(buf))
197 return got_error(GOT_ERR_NO_SPACE);
199 if (send_capabilities) {
200 if (client_is_reading) {
201 err = append_read_capabilities(&capalen, len,
202 symrefstr, buf, sizeof(buf));
204 err = got_gitproto_append_capabilities(&capalen,
205 buf, len, sizeof(buf), write_capabilities,
206 nitems(write_capabilities));
213 if (len + 1 >= sizeof(buf))
214 return got_error(GOT_ERR_NO_SPACE);
219 return got_pkt_writepkt(outfd, buf, len, chattygot);
222 static const struct got_error *
223 send_zero_refs(int outfd, int client_is_reading, int chattygot)
225 const struct got_error *err = NULL;
226 const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
227 char buf[GOT_PKT_MAX];
228 size_t len, capalen = 0;
230 len = strlcpy(buf, line, sizeof(buf));
231 if (len >= sizeof(buf))
232 return got_error(GOT_ERR_NO_SPACE);
234 if (client_is_reading) {
235 err = got_gitproto_append_capabilities(&capalen, buf, len,
236 sizeof(buf), read_capabilities, nitems(read_capabilities));
240 err = got_gitproto_append_capabilities(&capalen, buf, len,
241 sizeof(buf), write_capabilities,
242 nitems(write_capabilities));
247 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
251 echo_error(const struct got_error *err, int outfd, int chattygot)
253 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
257 * Echo the error to the client on a pkt-line.
258 * The client should then terminate its session.
260 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
261 len = strlcat(buf, err->msg, sizeof(buf));
262 got_pkt_writepkt(outfd, buf, len, chattygot);
265 static const struct got_error *
266 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
267 const char *repo_path, int chattygot)
269 const struct got_error *err = NULL;
272 struct gotd_imsg_list_refs lsref;
273 struct gotd_imsg_reflist ireflist;
274 struct gotd_imsg_ref iref;
275 struct gotd_imsg_symref isymref;
277 int have_nrefs = 0, sent_capabilities = 0;
278 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
279 char *refname = NULL;
281 memset(&imsg, 0, sizeof(imsg));
282 memset(&lsref, 0, sizeof(lsref));
284 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
285 sizeof(lsref.repo_name))
286 return got_error(GOT_ERR_NO_SPACE);
287 lsref.client_is_reading = client_is_reading;
289 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
290 &lsref, sizeof(lsref)) == -1)
291 return got_error_from_errno("imsg_compose LIST_REFS");
293 err = gotd_imsg_flush(ibuf);
297 while (!have_nrefs || nrefs > 0) {
298 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
301 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
302 switch (imsg.hdr.type) {
303 case GOTD_IMSG_ERROR:
304 err = gotd_imsg_recv_error(NULL, &imsg);
306 case GOTD_IMSG_REFLIST:
307 if (have_nrefs || nrefs > 0) {
308 err = got_error(GOT_ERR_PRIVSEP_MSG);
311 if (datalen != sizeof(ireflist)) {
312 err = got_error(GOT_ERR_PRIVSEP_MSG);
315 memcpy(&ireflist, imsg.data, sizeof(ireflist));
316 nrefs = ireflist.nrefs;
319 err = send_zero_refs(outfd, client_is_reading,
323 if (!have_nrefs || nrefs == 0) {
324 err = got_error(GOT_ERR_PRIVSEP_MSG);
327 if (datalen < sizeof(iref)) {
328 err = got_error(GOT_ERR_PRIVSEP_MSG);
331 memcpy(&iref, imsg.data, sizeof(iref));
332 if (datalen != sizeof(iref) + iref.name_len) {
333 err = got_error(GOT_ERR_PRIVSEP_LEN);
336 refname = strndup(imsg.data + sizeof(iref),
338 if (refname == NULL) {
339 err = got_error_from_errno("strndup");
342 err = send_ref(outfd, iref.id, refname,
343 !sent_capabilities, client_is_reading,
349 sent_capabilities = 1;
353 case GOTD_IMSG_SYMREF:
354 if (!have_nrefs || nrefs == 0) {
355 err = got_error(GOT_ERR_PRIVSEP_MSG);
358 if (datalen < sizeof(isymref)) {
359 err = got_error(GOT_ERR_PRIVSEP_LEN);
362 memcpy(&isymref, imsg.data, sizeof(isymref));
363 if (datalen != sizeof(isymref) + isymref.name_len +
364 isymref.target_len) {
365 err = got_error(GOT_ERR_PRIVSEP_LEN);
370 * For now, we only announce one symbolic ref,
371 * as part of our capability advertisement.
373 if (sent_capabilities || symrefstr != NULL ||
374 symrefname != NULL || symreftarget != NULL)
377 symrefname = strndup(imsg.data + sizeof(isymref),
379 if (symrefname == NULL) {
380 err = got_error_from_errno("malloc");
384 symreftarget = strndup(
385 imsg.data + sizeof(isymref) + isymref.name_len,
387 if (symreftarget == NULL) {
388 err = got_error_from_errno("strndup");
392 if (asprintf(&symrefstr, "%s:%s", symrefname,
393 symreftarget) == -1) {
394 err = got_error_from_errno("asprintf");
397 err = send_ref(outfd, isymref.target_id, symrefname,
398 !sent_capabilities, client_is_reading, symrefstr,
404 sent_capabilities = 1;
409 err = got_error(GOT_ERR_PRIVSEP_MSG);
416 err = got_pkt_flushpkt(outfd, chattygot);
426 static const struct got_error *
427 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
429 const struct got_error *err;
430 char *id_str = NULL, *client_capabilities = NULL;
432 err = got_gitproto_parse_want_line(&id_str,
433 &client_capabilities, buf, len);
437 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
438 err = got_error_msg(GOT_ERR_BAD_PACKET,
439 "want-line with bad object ID");
443 if (client_capabilities) {
444 err = got_gitproto_match_capabilities(common_capabilities,
445 NULL, client_capabilities, read_capabilities,
446 nitems(read_capabilities));
452 free(client_capabilities);
456 static const struct got_error *
457 parse_have_line(uint8_t *id, char *buf, size_t len)
459 const struct got_error *err;
462 err = got_gitproto_parse_have_line(&id_str, buf, len);
466 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
467 err = got_error_msg(GOT_ERR_BAD_PACKET,
468 "have-line with bad object ID");
476 static const struct got_error *
477 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
479 const struct got_error *err = NULL;
480 struct gotd_imsg_capability icapa;
484 memset(&icapa, 0, sizeof(icapa));
486 icapa.key_len = strlen(capa->key);
487 len = sizeof(icapa) + icapa.key_len;
489 icapa.value_len = strlen(capa->value);
490 len += icapa.value_len;
493 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
495 err = got_error_from_errno("imsg_create CAPABILITY");
499 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
500 return got_error_from_errno("imsg_add CAPABILITY");
501 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
502 return got_error_from_errno("imsg_add CAPABILITY");
504 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
505 return got_error_from_errno("imsg_add CAPABILITY");
509 imsg_close(ibuf, wbuf);
514 static const struct got_error *
515 send_capabilities(int *use_sidebands, int *report_status,
516 char *capabilities_str, struct imsgbuf *ibuf)
518 const struct got_error *err = NULL;
519 struct gotd_imsg_capabilities icapas;
520 struct got_capability *capa = NULL;
523 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
528 icapas.ncapabilities = ncapa;
529 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
530 &icapas, sizeof(icapas)) == -1) {
531 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
535 for (i = 0; i < ncapa; i++) {
536 err = send_capability(&capa[i], ibuf);
540 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
543 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
551 static const struct got_error *
552 forward_flushpkt(struct imsgbuf *ibuf)
554 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
555 return got_error_from_errno("imsg_compose FLUSH");
557 return gotd_imsg_flush(ibuf);
560 static const struct got_error *
561 recv_ack(struct imsg *imsg, uint8_t *expected_id)
563 struct gotd_imsg_ack iack;
566 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
567 if (datalen != sizeof(iack))
568 return got_error(GOT_ERR_PRIVSEP_LEN);
570 memcpy(&iack, imsg->data, sizeof(iack));
571 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
572 return got_error(GOT_ERR_BAD_OBJ_ID);
577 static const struct got_error *
578 recv_nak(struct imsg *imsg, uint8_t *expected_id)
580 struct gotd_imsg_ack inak;
583 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
584 if (datalen != sizeof(inak))
585 return got_error(GOT_ERR_PRIVSEP_LEN);
587 memcpy(&inak, imsg->data, sizeof(inak));
588 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
589 return got_error(GOT_ERR_BAD_OBJ_ID);
595 static const struct got_error *
596 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
597 char *buf, size_t len, int expect_capabilities, int chattygot)
599 const struct got_error *err;
600 struct gotd_imsg_want iwant;
601 char *capabilities_str;
605 memset(&iwant, 0, sizeof(iwant));
606 memset(&imsg, 0, sizeof(imsg));
608 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
612 if (capabilities_str) {
613 if (!expect_capabilities) {
614 err = got_error_msg(GOT_ERR_BAD_PACKET,
615 "unexpected capability announcement received");
618 err = send_capabilities(use_sidebands, NULL, capabilities_str,
625 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
626 &iwant, sizeof(iwant)) == -1) {
627 err = got_error_from_errno("imsg_compose WANT");
631 err = gotd_imsg_flush(ibuf);
636 * Wait for an ACK, or an error in case the desired object
639 while (!done && err == NULL) {
640 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
643 switch (imsg.hdr.type) {
644 case GOTD_IMSG_ERROR:
645 err = gotd_imsg_recv_error(NULL, &imsg);
648 err = recv_ack(&imsg, iwant.object_id);
654 err = got_error(GOT_ERR_PRIVSEP_MSG);
661 free(capabilities_str);
665 static const struct got_error *
666 send_ack(int outfd, uint8_t *id, int chattygot)
668 char hex[SHA1_DIGEST_STRING_LENGTH];
669 char buf[GOT_PKT_MAX];
672 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
673 return got_error(GOT_ERR_BAD_OBJ_ID);
675 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
676 if (len >= sizeof(buf))
677 return got_error(GOT_ERR_NO_SPACE);
679 return got_pkt_writepkt(outfd, buf, len, chattygot);
682 static const struct got_error *
683 send_nak(int outfd, int chattygot)
688 len = snprintf(buf, sizeof(buf), "NAK\n");
689 if (len >= sizeof(buf))
690 return got_error(GOT_ERR_NO_SPACE);
692 return got_pkt_writepkt(outfd, buf, len, chattygot);
695 static const struct got_error *
696 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
697 size_t len, int chattygot)
699 const struct got_error *err;
700 struct gotd_imsg_have ihave;
704 memset(&ihave, 0, sizeof(ihave));
705 memset(&imsg, 0, sizeof(imsg));
707 err = parse_have_line(ihave.object_id, buf, len);
711 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
712 &ihave, sizeof(ihave)) == -1)
713 return got_error_from_errno("imsg_compose HAVE");
715 err = gotd_imsg_flush(ibuf);
720 * Wait for an ACK or a NAK, indicating whether a common
721 * commit object has been found.
723 while (!done && err == NULL) {
724 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
727 switch (imsg.hdr.type) {
728 case GOTD_IMSG_ERROR:
729 err = gotd_imsg_recv_error(NULL, &imsg);
732 err = recv_ack(&imsg, ihave.object_id);
736 err = send_ack(outfd, ihave.object_id,
745 err = recv_nak(&imsg, ihave.object_id);
751 err = got_error(GOT_ERR_PRIVSEP_MSG);
761 static const struct got_error *
762 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
764 const struct got_error *err;
769 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
770 return got_error_from_errno("imsg_compose DONE");
772 err = gotd_imsg_flush(ibuf);
776 while (*packfd == -1 && err == NULL) {
777 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
781 switch (imsg.hdr.type) {
782 case GOTD_IMSG_ERROR:
783 err = gotd_imsg_recv_error(NULL, &imsg);
785 case GOTD_IMSG_PACKFILE_PIPE:
789 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
802 static const struct got_error *
803 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
805 const struct got_error *err = NULL;
806 int pack_starting = 0;
807 struct gotd_imsg_packfile_progress iprog;
808 char buf[GOT_PKT_MAX];
811 int p_deltify = 0, n;
812 const char *eol = "\r";
814 memset(&imsg, 0, sizeof(imsg));
816 while (!pack_starting && err == NULL) {
817 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
821 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
822 switch (imsg.hdr.type) {
823 case GOTD_IMSG_ERROR:
824 err = gotd_imsg_recv_error(NULL, &imsg);
826 case GOTD_IMSG_PACKFILE_READY:
830 case GOTD_IMSG_PACKFILE_PROGRESS:
831 if (datalen != sizeof(iprog)) {
832 err = got_error(GOT_ERR_PRIVSEP_LEN);
835 memcpy(&iprog, imsg.data, sizeof(iprog));
836 if (iprog.nobj_total > 0) {
837 p_deltify = (iprog.nobj_deltify * 100) /
840 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
841 n = snprintf(&buf[1], sizeof(buf) - 1,
842 "%d commits colored, "
848 if (n >= sizeof(buf) - 1)
850 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
853 err = got_error(GOT_ERR_PRIVSEP_MSG);
863 static const struct got_error *
864 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
867 const struct got_error *err = NULL;
868 char buf[GOT_PKT_MAX];
872 STATE_EXPECT_MORE_WANT,
877 enum protostate curstate = STATE_EXPECT_WANT;
878 int have_ack = 0, use_sidebands = 0, seen_have = 0;
880 size_t pack_chunksize;
882 imsg_init(&ibuf, gotd_sock);
884 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
888 while (curstate != STATE_DONE) {
891 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
895 if (curstate != STATE_EXPECT_WANT &&
896 curstate != STATE_EXPECT_MORE_WANT &&
897 curstate != STATE_EXPECT_HAVE &&
898 curstate != STATE_EXPECT_DONE) {
899 err = got_error_msg(GOT_ERR_BAD_PACKET,
900 "unexpected flush packet received");
904 if (curstate == STATE_EXPECT_WANT) {
907 * If the client does not want to fetch
908 * anything we should receive a flush
909 * packet followed by EOF.
911 r = read(infd, buf, sizeof(buf));
913 err = got_error_from_errno("read");
916 if (r == 0) /* EOF */
919 /* Zero-length field followed by payload. */
920 err = got_error_msg(GOT_ERR_BAD_PACKET,
921 "unexpected flush packet received");
925 if (curstate == STATE_EXPECT_WANT ||
926 curstate == STATE_EXPECT_MORE_WANT ||
927 curstate == STATE_EXPECT_HAVE) {
928 err = forward_flushpkt(&ibuf);
932 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
933 err = send_nak(outfd, chattygot);
937 if (curstate == STATE_EXPECT_MORE_WANT)
938 curstate = STATE_EXPECT_HAVE;
940 curstate = STATE_EXPECT_DONE;
941 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
942 if (curstate != STATE_EXPECT_WANT &&
943 curstate != STATE_EXPECT_MORE_WANT) {
944 err = got_error_msg(GOT_ERR_BAD_PACKET,
945 "unexpected 'want' packet");
948 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
949 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
952 if (curstate == STATE_EXPECT_WANT)
953 curstate = STATE_EXPECT_MORE_WANT;
954 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
955 if (curstate != STATE_EXPECT_HAVE &&
956 curstate != STATE_EXPECT_DONE) {
957 err = got_error_msg(GOT_ERR_BAD_PACKET,
958 "unexpected 'have' packet");
961 if (curstate == STATE_EXPECT_HAVE) {
962 err = recv_have(&have_ack, outfd, &ibuf,
968 curstate = STATE_EXPECT_DONE;
970 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
971 if (curstate != STATE_EXPECT_HAVE &&
972 curstate != STATE_EXPECT_DONE) {
973 err = got_error_msg(GOT_ERR_BAD_PACKET,
974 "unexpected 'done' packet");
977 err = recv_done(&packfd, outfd, &ibuf, chattygot);
980 curstate = STATE_DONE;
983 err = got_error(GOT_ERR_BAD_PACKET);
989 err = send_nak(outfd, chattygot);
995 err = relay_progress_reports(&ibuf, outfd, chattygot);
998 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
1000 pack_chunksize = sizeof(buf);
1005 r = read(packfd, use_sidebands ? &buf[1] : buf,
1008 err = got_error_from_errno("read");
1010 } else if (r == 0) {
1011 err = got_pkt_flushpkt(outfd, chattygot);
1015 if (use_sidebands) {
1016 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1017 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1021 err = got_poll_write_full(outfd, buf, r);
1023 if (err->code == GOT_ERR_EOF)
1031 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1032 err = got_error_from_errno("close");
1034 echo_error(err, outfd, chattygot);
1038 static const struct got_error *
1039 parse_ref_update_line(char **common_capabilities, char **refname,
1040 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1042 const struct got_error *err;
1043 char *old_id_str = NULL, *new_id_str = NULL;
1044 char *client_capabilities = NULL;
1048 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1049 refname, &client_capabilities, buf, len);
1053 if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
1054 !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
1055 err = got_error_msg(GOT_ERR_BAD_PACKET,
1056 "ref-update with bad object ID");
1059 if (!got_ref_name_is_valid(*refname)) {
1060 err = got_error_msg(GOT_ERR_BAD_PACKET,
1061 "ref-update with bad reference name");
1065 if (client_capabilities) {
1066 err = got_gitproto_match_capabilities(common_capabilities,
1067 NULL, client_capabilities, write_capabilities,
1068 nitems(write_capabilities));
1075 free(client_capabilities);
1083 static const struct got_error *
1084 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1085 char *buf, size_t len, int expect_capabilities, int chattygot)
1087 const struct got_error *err;
1088 struct gotd_imsg_ref_update iref;
1090 char *capabilities_str = NULL, *refname = NULL;
1094 memset(&iref, 0, sizeof(iref));
1095 memset(&imsg, 0, sizeof(imsg));
1097 err = parse_ref_update_line(&capabilities_str, &refname,
1098 iref.old_id, iref.new_id, buf, len);
1102 if (capabilities_str) {
1103 if (!expect_capabilities) {
1104 err = got_error_msg(GOT_ERR_BAD_PACKET,
1105 "unexpected capability announcement received");
1108 err = send_capabilities(NULL, report_status, capabilities_str,
1114 iref.name_len = strlen(refname);
1115 len = sizeof(iref) + iref.name_len;
1116 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1118 err = got_error_from_errno("imsg_create REF_UPDATE");
1122 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1123 return got_error_from_errno("imsg_add REF_UPDATE");
1124 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1125 return got_error_from_errno("imsg_add REF_UPDATE");
1127 imsg_close(ibuf, wbuf);
1129 err = gotd_imsg_flush(ibuf);
1133 /* Wait for ACK or an error. */
1134 while (!done && err == NULL) {
1135 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1138 switch (imsg.hdr.type) {
1139 case GOTD_IMSG_ERROR:
1140 err = gotd_imsg_recv_error(NULL, &imsg);
1143 err = recv_ack(&imsg, iref.new_id);
1149 err = got_error(GOT_ERR_PRIVSEP_MSG);
1156 free(capabilities_str);
1161 static const struct got_error *
1162 recv_packfile(struct imsg *imsg, int infd)
1164 const struct got_error *err = NULL;
1167 char buf[GOT_PKT_MAX];
1170 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1172 return got_error(GOT_ERR_PRIVSEP_MSG);
1175 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1178 while (!pack_done) {
1181 err = got_poll_fd(infd, POLLIN, 1);
1183 if (err->code != GOT_ERR_TIMEOUT)
1187 r = read(infd, buf, sizeof(buf));
1189 err = got_error_from_errno("read");
1194 * Git clients hang up their side of the
1195 * connection after sending the pack file.
1204 /* Detect gotd(8) closing the pack pipe when done. */
1205 err = got_poll_fd(packfd, POLLOUT, 1);
1207 if (err->code != GOT_ERR_EOF)
1213 /* Write pack data and/or detect pipe being closed. */
1214 err = got_poll_write_full(packfd, buf, r);
1216 if (err->code == GOT_ERR_EOF)
1227 static const struct got_error *
1228 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1230 const struct got_error *err = NULL;
1231 struct gotd_imsg_packfile_status istatus;
1232 char buf[GOT_PKT_MAX];
1233 size_t datalen, len;
1234 char *reason = NULL;
1236 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1237 if (datalen < sizeof(istatus))
1238 return got_error(GOT_ERR_PRIVSEP_LEN);
1239 memcpy(&istatus, imsg->data, sizeof(istatus));
1240 if (datalen != sizeof(istatus) + istatus.reason_len)
1241 return got_error(GOT_ERR_PRIVSEP_LEN);
1243 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1244 if (reason == NULL) {
1245 err = got_error_from_errno("strndup");
1250 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1252 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1253 if (len >= sizeof(buf)) {
1254 err = got_error(GOT_ERR_NO_SPACE);
1258 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1264 static const struct got_error *
1265 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1267 const struct got_error *err = NULL;
1268 struct gotd_imsg_ref_update_ok iok;
1269 size_t datalen, len;
1270 char buf[GOT_PKT_MAX];
1271 char *refname = NULL;
1273 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1274 if (datalen < sizeof(iok))
1275 return got_error(GOT_ERR_PRIVSEP_LEN);
1276 memcpy(&iok, imsg->data, sizeof(iok));
1277 if (datalen != sizeof(iok) + iok.name_len)
1278 return got_error(GOT_ERR_PRIVSEP_LEN);
1280 memcpy(&iok, imsg->data, sizeof(iok));
1282 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1283 if (refname == NULL)
1284 return got_error_from_errno("strndup");
1286 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1287 if (len >= sizeof(buf)) {
1288 err = got_error(GOT_ERR_NO_SPACE);
1292 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1298 static const struct got_error *
1299 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1301 const struct got_error *err = NULL;
1302 struct gotd_imsg_ref_update_ng ing;
1303 size_t datalen, len;
1304 char buf[GOT_PKT_MAX];
1305 char *refname = NULL, *reason = NULL;
1307 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1308 if (datalen < sizeof(ing))
1309 return got_error(GOT_ERR_PRIVSEP_LEN);
1310 memcpy(&ing, imsg->data, sizeof(ing));
1311 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1312 return got_error(GOT_ERR_PRIVSEP_LEN);
1314 memcpy(&ing, imsg->data, sizeof(ing));
1316 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1317 if (refname == NULL)
1318 return got_error_from_errno("strndup");
1320 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1322 if (reason == NULL) {
1323 err = got_error_from_errno("strndup");
1327 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1328 if (len >= sizeof(buf)) {
1329 err = got_error(GOT_ERR_NO_SPACE);
1333 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1340 static const struct got_error *
1341 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1344 const struct got_error *err = NULL;
1345 char buf[GOT_PKT_MAX];
1346 struct imsgbuf ibuf;
1348 STATE_EXPECT_REF_UPDATE,
1349 STATE_EXPECT_MORE_REF_UPDATES,
1350 STATE_EXPECT_PACKFILE,
1351 STATE_PACKFILE_RECEIVED,
1354 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1356 int report_status = 0;
1358 imsg_init(&ibuf, gotd_sock);
1359 memset(&imsg, 0, sizeof(imsg));
1361 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1365 while (curstate != STATE_EXPECT_PACKFILE) {
1368 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1372 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1373 err = got_error_msg(GOT_ERR_BAD_PACKET,
1374 "unexpected flush packet received");
1377 err = forward_flushpkt(&ibuf);
1380 curstate = STATE_EXPECT_PACKFILE;
1381 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1382 if (curstate != STATE_EXPECT_REF_UPDATE &&
1383 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1384 err = got_error_msg(GOT_ERR_BAD_PACKET,
1385 "unexpected ref-update packet");
1388 if (curstate == STATE_EXPECT_REF_UPDATE) {
1389 err = recv_ref_update(&report_status,
1390 outfd, &ibuf, buf, n, 1, chattygot);
1392 err = recv_ref_update(NULL, outfd, &ibuf,
1393 buf, n, 0, chattygot);
1397 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1399 err = got_error(GOT_ERR_BAD_PACKET);
1404 while (curstate != STATE_PACKFILE_RECEIVED) {
1405 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1408 switch (imsg.hdr.type) {
1409 case GOTD_IMSG_ERROR:
1410 err = gotd_imsg_recv_error(NULL, &imsg);
1412 case GOTD_IMSG_PACKFILE_PIPE:
1413 err = recv_packfile(&imsg, infd);
1415 if (err->code != GOT_ERR_EOF)
1418 * EOF is reported when the client hangs up,
1419 * which can happen with Git clients.
1420 * The socket should stay half-open so we
1421 * can still send our reports if requested.
1425 curstate = STATE_PACKFILE_RECEIVED;
1428 err = got_error(GOT_ERR_PRIVSEP_MSG);
1437 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1438 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1441 switch (imsg.hdr.type) {
1442 case GOTD_IMSG_ERROR:
1443 err = gotd_imsg_recv_error(NULL, &imsg);
1445 case GOTD_IMSG_PACKFILE_STATUS:
1448 err = report_unpack_status(&imsg, outfd, chattygot);
1450 case GOTD_IMSG_REF_UPDATE_OK:
1453 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1455 case GOTD_IMSG_REF_UPDATE_NG:
1458 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1460 case GOTD_IMSG_REFS_UPDATED:
1461 curstate = STATE_REFS_UPDATED;
1462 err = got_pkt_flushpkt(outfd, chattygot);
1465 err = got_error(GOT_ERR_PRIVSEP_MSG);
1474 echo_error(err, outfd, chattygot);
1478 const struct got_error *
1479 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1480 int gotd_sock, int chattygot)
1482 const struct got_error *err = NULL;
1484 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1485 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1486 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1487 err = serve_write(infd, outfd, gotd_sock, repo_path,
1490 err = got_error(GOT_ERR_BAD_PACKET);