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 },
63 { GOT_CAPA_DELETE_REFS, NULL },
67 static const struct got_error *
68 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 char buf[GOT_PKT_MAX];
227 uint8_t zero[SHA1_DIGEST_LENGTH];
228 char hex[SHA1_DIGEST_STRING_LENGTH];
229 size_t len, capalen = 0;
231 memset(&zero, 0, sizeof(zero));
233 if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
234 return got_error(GOT_ERR_BAD_OBJ_ID);
236 len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
237 if (len >= sizeof(buf))
238 return got_error(GOT_ERR_NO_SPACE);
240 if (client_is_reading) {
241 err = got_gitproto_append_capabilities(&capalen, buf, len,
242 sizeof(buf), read_capabilities, nitems(read_capabilities));
246 err = got_gitproto_append_capabilities(&capalen, buf, len,
247 sizeof(buf), write_capabilities,
248 nitems(write_capabilities));
253 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
257 echo_error(const struct got_error *err, int outfd, int chattygot)
259 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
263 * Echo the error to the client on a pkt-line.
264 * The client should then terminate its session.
266 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
267 len = strlcat(buf, err->msg, sizeof(buf));
268 got_pkt_writepkt(outfd, buf, len, chattygot);
271 static const struct got_error *
272 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
273 const char *repo_path, int chattygot)
275 const struct got_error *err = NULL;
278 struct gotd_imsg_list_refs lsref;
279 struct gotd_imsg_reflist ireflist;
280 struct gotd_imsg_ref iref;
281 struct gotd_imsg_symref isymref;
283 int have_nrefs = 0, sent_capabilities = 0;
284 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
285 char *refname = NULL;
287 memset(&imsg, 0, sizeof(imsg));
288 memset(&lsref, 0, sizeof(lsref));
290 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
291 sizeof(lsref.repo_name))
292 return got_error(GOT_ERR_NO_SPACE);
293 lsref.client_is_reading = client_is_reading;
295 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
296 &lsref, sizeof(lsref)) == -1)
297 return got_error_from_errno("imsg_compose LIST_REFS");
299 err = gotd_imsg_flush(ibuf);
303 while (!have_nrefs || nrefs > 0) {
304 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
307 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
308 switch (imsg.hdr.type) {
309 case GOTD_IMSG_ERROR:
310 err = gotd_imsg_recv_error(NULL, &imsg);
312 case GOTD_IMSG_REFLIST:
313 if (have_nrefs || nrefs > 0) {
314 err = got_error(GOT_ERR_PRIVSEP_MSG);
317 if (datalen != sizeof(ireflist)) {
318 err = got_error(GOT_ERR_PRIVSEP_MSG);
321 memcpy(&ireflist, imsg.data, sizeof(ireflist));
322 nrefs = ireflist.nrefs;
325 err = send_zero_refs(outfd, client_is_reading,
329 if (!have_nrefs || nrefs == 0) {
330 err = got_error(GOT_ERR_PRIVSEP_MSG);
333 if (datalen < sizeof(iref)) {
334 err = got_error(GOT_ERR_PRIVSEP_MSG);
337 memcpy(&iref, imsg.data, sizeof(iref));
338 if (datalen != sizeof(iref) + iref.name_len) {
339 err = got_error(GOT_ERR_PRIVSEP_LEN);
342 refname = malloc(iref.name_len + 1);
343 if (refname == NULL) {
344 err = got_error_from_errno("malloc");
347 memcpy(refname, imsg.data + sizeof(iref),
349 refname[iref.name_len] = '\0';
350 err = send_ref(outfd, iref.id, refname,
351 !sent_capabilities, client_is_reading,
357 sent_capabilities = 1;
361 case GOTD_IMSG_SYMREF:
362 if (!have_nrefs || nrefs == 0) {
363 err = got_error(GOT_ERR_PRIVSEP_MSG);
366 if (datalen < sizeof(isymref)) {
367 err = got_error(GOT_ERR_PRIVSEP_LEN);
370 memcpy(&isymref, imsg.data, sizeof(isymref));
371 if (datalen != sizeof(isymref) + isymref.name_len +
372 isymref.target_len) {
373 err = got_error(GOT_ERR_PRIVSEP_LEN);
378 * For now, we only announce one symbolic ref,
379 * as part of our capability advertisement.
381 if (sent_capabilities || symrefstr != NULL ||
382 symrefname != NULL || symreftarget != NULL)
385 symrefname = malloc(isymref.name_len + 1);
386 if (symrefname == NULL) {
387 err = got_error_from_errno("malloc");
390 memcpy(symrefname, imsg.data + sizeof(isymref),
392 symrefname[isymref.name_len] = '\0';
394 symreftarget = malloc(isymref.target_len + 1);
395 if (symreftarget == NULL) {
396 err = got_error_from_errno("malloc");
400 imsg.data + sizeof(isymref) + isymref.name_len,
402 symreftarget[isymref.target_len] = '\0';
404 if (asprintf(&symrefstr, "%s:%s", symrefname,
405 symreftarget) == -1) {
406 err = got_error_from_errno("asprintf");
409 err = send_ref(outfd, isymref.target_id, symrefname,
410 !sent_capabilities, client_is_reading, symrefstr,
416 sent_capabilities = 1;
421 err = got_error(GOT_ERR_PRIVSEP_MSG);
428 err = got_pkt_flushpkt(outfd, chattygot);
438 static const struct got_error *
439 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
441 const struct got_error *err;
442 char *id_str = NULL, *client_capabilities = NULL;
444 err = got_gitproto_parse_want_line(&id_str,
445 &client_capabilities, buf, len);
449 if (!got_parse_sha1_digest(id, id_str)) {
450 err = got_error_msg(GOT_ERR_BAD_PACKET,
451 "want-line with bad object ID");
455 if (client_capabilities) {
456 err = got_gitproto_match_capabilities(common_capabilities,
457 NULL, client_capabilities, read_capabilities,
458 nitems(read_capabilities));
464 free(client_capabilities);
468 static const struct got_error *
469 parse_have_line(uint8_t *id, char *buf, size_t len)
471 const struct got_error *err;
474 err = got_gitproto_parse_have_line(&id_str, buf, len);
478 if (!got_parse_sha1_digest(id, id_str)) {
479 err = got_error_msg(GOT_ERR_BAD_PACKET,
480 "have-line with bad object ID");
488 static const struct got_error *
489 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
491 const struct got_error *err = NULL;
492 struct gotd_imsg_capability icapa;
496 memset(&icapa, 0, sizeof(icapa));
498 icapa.key_len = strlen(capa->key);
499 len = sizeof(icapa) + icapa.key_len;
501 icapa.value_len = strlen(capa->value);
502 len += icapa.value_len;
505 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
507 err = got_error_from_errno("imsg_create CAPABILITY");
511 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
512 return got_error_from_errno("imsg_add CAPABILITY");
513 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
514 return got_error_from_errno("imsg_add CAPABILITY");
516 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
517 return got_error_from_errno("imsg_add CAPABILITY");
521 imsg_close(ibuf, wbuf);
526 static const struct got_error *
527 send_capabilities(int *use_sidebands, int *report_status,
528 char *capabilities_str, struct imsgbuf *ibuf)
530 const struct got_error *err = NULL;
531 struct gotd_imsg_capabilities icapas;
532 struct got_capability *capa = NULL;
535 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
540 icapas.ncapabilities = ncapa;
541 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
542 &icapas, sizeof(icapas)) == -1) {
543 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
547 for (i = 0; i < ncapa; i++) {
548 err = send_capability(&capa[i], ibuf);
552 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
555 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
563 static const struct got_error *
564 forward_flushpkt(struct imsgbuf *ibuf)
566 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
567 return got_error_from_errno("imsg_compose FLUSH");
569 return gotd_imsg_flush(ibuf);
572 static const struct got_error *
573 recv_ack(struct imsg *imsg, uint8_t *expected_id)
575 struct gotd_imsg_ack iack;
578 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
579 if (datalen != sizeof(iack))
580 return got_error(GOT_ERR_PRIVSEP_LEN);
582 memcpy(&iack, imsg->data, sizeof(iack));
583 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
584 return got_error(GOT_ERR_BAD_OBJ_ID);
589 static const struct got_error *
590 recv_nak(struct imsg *imsg, uint8_t *expected_id)
592 struct gotd_imsg_ack inak;
595 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
596 if (datalen != sizeof(inak))
597 return got_error(GOT_ERR_PRIVSEP_LEN);
599 memcpy(&inak, imsg->data, sizeof(inak));
600 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
601 return got_error(GOT_ERR_BAD_OBJ_ID);
607 static const struct got_error *
608 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
609 char *buf, size_t len, int expect_capabilities, int chattygot)
611 const struct got_error *err;
612 struct gotd_imsg_want iwant;
613 char *capabilities_str;
617 memset(&iwant, 0, sizeof(iwant));
618 memset(&imsg, 0, sizeof(imsg));
620 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
624 if (capabilities_str) {
625 if (!expect_capabilities) {
626 err = got_error_msg(GOT_ERR_BAD_PACKET,
627 "unexpected capability announcement received");
630 err = send_capabilities(use_sidebands, NULL, capabilities_str,
637 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
638 &iwant, sizeof(iwant)) == -1) {
639 err = got_error_from_errno("imsg_compose WANT");
643 err = gotd_imsg_flush(ibuf);
648 * Wait for an ACK, or an error in case the desired object
651 while (!done && err == NULL) {
652 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
655 switch (imsg.hdr.type) {
656 case GOTD_IMSG_ERROR:
657 err = gotd_imsg_recv_error(NULL, &imsg);
660 err = recv_ack(&imsg, iwant.object_id);
666 err = got_error(GOT_ERR_PRIVSEP_MSG);
673 free(capabilities_str);
677 static const struct got_error *
678 send_ack(int outfd, uint8_t *id, int chattygot)
680 char hex[SHA1_DIGEST_STRING_LENGTH];
681 char buf[GOT_PKT_MAX];
684 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
685 return got_error(GOT_ERR_BAD_OBJ_ID);
687 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
688 if (len >= sizeof(buf))
689 return got_error(GOT_ERR_NO_SPACE);
691 return got_pkt_writepkt(outfd, buf, len, chattygot);
694 static const struct got_error *
695 send_nak(int outfd, int chattygot)
700 len = snprintf(buf, sizeof(buf), "NAK\n");
701 if (len >= sizeof(buf))
702 return got_error(GOT_ERR_NO_SPACE);
704 return got_pkt_writepkt(outfd, buf, len, chattygot);
707 static const struct got_error *
708 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
709 size_t len, int chattygot)
711 const struct got_error *err;
712 struct gotd_imsg_have ihave;
716 memset(&ihave, 0, sizeof(ihave));
717 memset(&imsg, 0, sizeof(imsg));
719 err = parse_have_line(ihave.object_id, buf, len);
723 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
724 &ihave, sizeof(ihave)) == -1)
725 return got_error_from_errno("imsg_compose HAVE");
727 err = gotd_imsg_flush(ibuf);
732 * Wait for an ACK or a NAK, indicating whether a common
733 * commit object has been found.
735 while (!done && err == NULL) {
736 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
739 switch (imsg.hdr.type) {
740 case GOTD_IMSG_ERROR:
741 err = gotd_imsg_recv_error(NULL, &imsg);
744 err = recv_ack(&imsg, ihave.object_id);
748 err = send_ack(outfd, ihave.object_id,
757 err = recv_nak(&imsg, ihave.object_id);
763 err = got_error(GOT_ERR_PRIVSEP_MSG);
773 static const struct got_error *
774 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
776 const struct got_error *err;
781 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
782 return got_error_from_errno("imsg_compose DONE");
784 err = gotd_imsg_flush(ibuf);
788 while (*packfd == -1 && err == NULL) {
789 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
793 switch (imsg.hdr.type) {
794 case GOTD_IMSG_ERROR:
795 err = gotd_imsg_recv_error(NULL, &imsg);
797 case GOTD_IMSG_PACKFILE_PIPE:
801 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
804 err = got_error(GOT_ERR_PRIVSEP_MSG);
814 static const struct got_error *
815 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
817 const struct got_error *err = NULL;
818 int pack_starting = 0;
819 struct gotd_imsg_packfile_progress iprog;
820 char buf[GOT_PKT_MAX];
823 int p_deltify = 0, n;
824 const char *eol = "\r";
826 memset(&imsg, 0, sizeof(imsg));
828 while (!pack_starting && err == NULL) {
829 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
833 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
834 switch (imsg.hdr.type) {
835 case GOTD_IMSG_ERROR:
836 err = gotd_imsg_recv_error(NULL, &imsg);
838 case GOTD_IMSG_PACKFILE_READY:
842 case GOTD_IMSG_PACKFILE_PROGRESS:
843 if (datalen != sizeof(iprog)) {
844 err = got_error(GOT_ERR_PRIVSEP_LEN);
847 memcpy(&iprog, imsg.data, sizeof(iprog));
848 if (iprog.nobj_total > 0) {
849 p_deltify = (iprog.nobj_deltify * 100) /
852 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
853 n = snprintf(&buf[1], sizeof(buf) - 1,
854 "%d commits colored, "
860 if (n >= sizeof(buf) - 1)
862 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
865 err = got_error(GOT_ERR_PRIVSEP_MSG);
875 static const struct got_error *
876 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
879 const struct got_error *err = NULL;
880 char buf[GOT_PKT_MAX];
884 STATE_EXPECT_MORE_WANT,
889 enum protostate curstate = STATE_EXPECT_WANT;
890 int have_ack = 0, use_sidebands = 0, seen_have = 0;
892 size_t pack_chunksize;
894 imsg_init(&ibuf, gotd_sock);
896 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
900 while (curstate != STATE_DONE) {
903 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
907 if (curstate != STATE_EXPECT_MORE_WANT &&
908 curstate != STATE_EXPECT_HAVE) {
909 err = got_error_msg(GOT_ERR_BAD_PACKET,
910 "unexpected flush packet received");
913 err = forward_flushpkt(&ibuf);
916 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
917 err = send_nak(outfd, chattygot);
921 if (curstate == STATE_EXPECT_MORE_WANT)
922 curstate = STATE_EXPECT_HAVE;
924 curstate = STATE_EXPECT_DONE;
925 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
926 if (curstate != STATE_EXPECT_WANT &&
927 curstate != STATE_EXPECT_MORE_WANT) {
928 err = got_error_msg(GOT_ERR_BAD_PACKET,
929 "unexpected 'want' packet");
932 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
933 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
936 if (curstate == STATE_EXPECT_WANT)
937 curstate = STATE_EXPECT_MORE_WANT;
938 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
939 if (curstate != STATE_EXPECT_HAVE) {
940 err = got_error_msg(GOT_ERR_BAD_PACKET,
941 "unexpected 'have' packet");
944 err = recv_have(&have_ack, outfd, &ibuf, buf, n,
949 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
950 if (curstate != STATE_EXPECT_HAVE &&
951 curstate != STATE_EXPECT_DONE) {
952 err = got_error_msg(GOT_ERR_BAD_PACKET,
953 "unexpected 'done' packet");
956 err = recv_done(&packfd, outfd, &ibuf, chattygot);
959 curstate = STATE_DONE;
962 err = got_error(GOT_ERR_BAD_PACKET);
968 err = send_nak(outfd, chattygot);
974 err = relay_progress_reports(&ibuf, outfd, chattygot);
977 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
979 pack_chunksize = sizeof(buf);
984 r = read(packfd, use_sidebands ? &buf[1] : buf,
987 err = got_error_from_errno("read");
990 err = got_pkt_flushpkt(outfd, chattygot);
995 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
996 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1000 err = got_poll_write_full(outfd, buf, r);
1002 if (err->code == GOT_ERR_EOF)
1010 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1011 err = got_error_from_errno("close");
1013 echo_error(err, outfd, chattygot);
1017 static const struct got_error *
1018 parse_ref_update_line(char **common_capabilities, char **refname,
1019 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1021 const struct got_error *err;
1022 char *old_id_str = NULL, *new_id_str = NULL;
1023 char *client_capabilities = NULL;
1027 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1028 refname, &client_capabilities, buf, len);
1032 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1033 !got_parse_sha1_digest(new_id, new_id_str)) {
1034 err = got_error_msg(GOT_ERR_BAD_PACKET,
1035 "ref-update with bad object ID");
1038 if (!got_ref_name_is_valid(*refname)) {
1039 err = got_error_msg(GOT_ERR_BAD_PACKET,
1040 "ref-update with bad reference name");
1044 if (client_capabilities) {
1045 err = got_gitproto_match_capabilities(common_capabilities,
1046 NULL, client_capabilities, write_capabilities,
1047 nitems(write_capabilities));
1054 free(client_capabilities);
1062 static const struct got_error *
1063 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1064 char *buf, size_t len, int expect_capabilities, int chattygot)
1066 const struct got_error *err;
1067 struct gotd_imsg_ref_update iref;
1069 char *capabilities_str = NULL, *refname = NULL;
1073 memset(&iref, 0, sizeof(iref));
1074 memset(&imsg, 0, sizeof(imsg));
1076 err = parse_ref_update_line(&capabilities_str, &refname,
1077 iref.old_id, iref.new_id, buf, len);
1081 if (capabilities_str) {
1082 if (!expect_capabilities) {
1083 err = got_error_msg(GOT_ERR_BAD_PACKET,
1084 "unexpected capability announcement received");
1087 err = send_capabilities(NULL, report_status, capabilities_str,
1093 iref.name_len = strlen(refname);
1094 len = sizeof(iref) + iref.name_len;
1095 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1097 err = got_error_from_errno("imsg_create REF_UPDATE");
1101 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1102 return got_error_from_errno("imsg_add REF_UPDATE");
1103 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1104 return got_error_from_errno("imsg_add REF_UPDATE");
1106 imsg_close(ibuf, wbuf);
1108 err = gotd_imsg_flush(ibuf);
1112 /* Wait for ACK or an error. */
1113 while (!done && err == NULL) {
1114 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1117 switch (imsg.hdr.type) {
1118 case GOTD_IMSG_ERROR:
1119 err = gotd_imsg_recv_error(NULL, &imsg);
1122 err = recv_ack(&imsg, iref.new_id);
1128 err = got_error(GOT_ERR_PRIVSEP_MSG);
1135 free(capabilities_str);
1140 static const struct got_error *
1141 recv_packfile(struct imsg *imsg, int infd)
1143 const struct got_error *err = NULL;
1146 char buf[GOT_PKT_MAX];
1149 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1151 return got_error(GOT_ERR_PRIVSEP_MSG);
1154 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1157 while (!pack_done) {
1160 err = got_poll_fd(infd, POLLIN, 1);
1162 if (err->code != GOT_ERR_TIMEOUT)
1166 r = read(infd, buf, sizeof(buf));
1168 err = got_error_from_errno("read");
1173 * Git clients hang up their side of the
1174 * connection after sending the pack file.
1183 /* Detect gotd(8) closing the pack pipe when done. */
1184 err = got_poll_fd(packfd, POLLOUT, 1);
1186 if (err->code != GOT_ERR_EOF)
1192 /* Write pack data and/or detect pipe being closed. */
1193 err = got_poll_write_full(packfd, buf, r);
1195 if (err->code == GOT_ERR_EOF)
1206 static const struct got_error *
1207 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1209 const struct got_error *err = NULL;
1210 struct gotd_imsg_packfile_status istatus;
1211 char buf[GOT_PKT_MAX];
1212 size_t datalen, len;
1213 char *reason = NULL;
1215 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1216 if (datalen < sizeof(istatus))
1217 return got_error(GOT_ERR_PRIVSEP_LEN);
1218 memcpy(&istatus, imsg->data, sizeof(istatus));
1219 if (datalen != sizeof(istatus) + istatus.reason_len)
1220 return got_error(GOT_ERR_PRIVSEP_LEN);
1222 reason = malloc(istatus.reason_len + 1);
1223 if (reason == NULL) {
1224 err = got_error_from_errno("malloc");
1227 memcpy(reason, imsg->data + sizeof(istatus), istatus.reason_len);
1228 reason[istatus.reason_len] = '\0';
1231 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1233 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1234 if (len >= sizeof(buf)) {
1235 err = got_error(GOT_ERR_NO_SPACE);
1239 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1245 static const struct got_error *
1246 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1248 const struct got_error *err = NULL;
1249 struct gotd_imsg_ref_update_ok iok;
1250 size_t datalen, len;
1251 char buf[GOT_PKT_MAX];
1252 char *refname = NULL;
1254 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1255 if (datalen < sizeof(iok))
1256 return got_error(GOT_ERR_PRIVSEP_LEN);
1257 memcpy(&iok, imsg->data, sizeof(iok));
1258 if (datalen != sizeof(iok) + iok.name_len)
1259 return got_error(GOT_ERR_PRIVSEP_LEN);
1261 memcpy(&iok, imsg->data, sizeof(iok));
1263 refname = malloc(iok.name_len + 1);
1264 if (refname == NULL)
1265 return got_error_from_errno("malloc");
1266 memcpy(refname, imsg->data + sizeof(iok), iok.name_len);
1267 refname[iok.name_len] = '\0';
1269 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1270 if (len >= sizeof(buf)) {
1271 err = got_error(GOT_ERR_NO_SPACE);
1275 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1281 static const struct got_error *
1282 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1284 const struct got_error *err = NULL;
1285 struct gotd_imsg_ref_update_ng ing;
1286 size_t datalen, len;
1287 char buf[GOT_PKT_MAX];
1288 char *refname = NULL, *reason = NULL;
1290 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1291 if (datalen < sizeof(ing))
1292 return got_error(GOT_ERR_PRIVSEP_LEN);
1293 memcpy(&ing, imsg->data, sizeof(ing));
1294 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1295 return got_error(GOT_ERR_PRIVSEP_LEN);
1297 memcpy(&ing, imsg->data, sizeof(ing));
1299 refname = malloc(ing.name_len + 1);
1300 if (refname == NULL)
1301 return got_error_from_errno("malloc");
1302 memcpy(refname, imsg->data + sizeof(ing), ing.name_len);
1303 refname[ing.name_len] = '\0';
1305 reason = malloc(ing.reason_len + 1);
1306 if (reason == NULL) {
1307 err = got_error_from_errno("malloc");
1310 memcpy(refname, imsg->data + sizeof(ing) + ing.name_len,
1312 refname[ing.reason_len] = '\0';
1314 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1315 if (len >= sizeof(buf)) {
1316 err = got_error(GOT_ERR_NO_SPACE);
1320 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1327 static const struct got_error *
1328 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1331 const struct got_error *err = NULL;
1332 char buf[GOT_PKT_MAX];
1333 struct imsgbuf ibuf;
1335 STATE_EXPECT_REF_UPDATE,
1336 STATE_EXPECT_MORE_REF_UPDATES,
1337 STATE_EXPECT_PACKFILE,
1338 STATE_PACKFILE_RECEIVED,
1341 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1343 int report_status = 0;
1345 imsg_init(&ibuf, gotd_sock);
1346 memset(&imsg, 0, sizeof(imsg));
1348 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1352 while (curstate != STATE_EXPECT_PACKFILE) {
1355 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1359 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1360 err = got_error_msg(GOT_ERR_BAD_PACKET,
1361 "unexpected flush packet received");
1364 err = forward_flushpkt(&ibuf);
1367 curstate = STATE_EXPECT_PACKFILE;
1368 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1369 if (curstate != STATE_EXPECT_REF_UPDATE &&
1370 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1371 err = got_error_msg(GOT_ERR_BAD_PACKET,
1372 "unexpected ref-update packet");
1375 if (curstate == STATE_EXPECT_REF_UPDATE) {
1376 err = recv_ref_update(&report_status,
1377 outfd, &ibuf, buf, n, 1, chattygot);
1379 err = recv_ref_update(NULL, outfd, &ibuf,
1380 buf, n, 0, chattygot);
1384 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1386 err = got_error(GOT_ERR_BAD_PACKET);
1391 while (curstate != STATE_PACKFILE_RECEIVED) {
1392 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1395 switch (imsg.hdr.type) {
1396 case GOTD_IMSG_ERROR:
1397 err = gotd_imsg_recv_error(NULL, &imsg);
1399 case GOTD_IMSG_PACKFILE_PIPE:
1400 err = recv_packfile(&imsg, infd);
1402 if (err->code != GOT_ERR_EOF)
1405 * EOF is reported when the client hangs up,
1406 * which can happen with Git clients.
1407 * The socket should stay half-open so we
1408 * can still send our reports if requested.
1412 curstate = STATE_PACKFILE_RECEIVED;
1415 err = got_error(GOT_ERR_PRIVSEP_MSG);
1424 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1425 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1428 switch (imsg.hdr.type) {
1429 case GOTD_IMSG_ERROR:
1430 err = gotd_imsg_recv_error(NULL, &imsg);
1432 case GOTD_IMSG_PACKFILE_STATUS:
1435 err = report_unpack_status(&imsg, outfd, chattygot);
1437 case GOTD_IMSG_REF_UPDATE_OK:
1440 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1442 case GOTD_IMSG_REF_UPDATE_NG:
1445 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1447 case GOTD_IMSG_REFS_UPDATED:
1448 curstate = STATE_REFS_UPDATED;
1449 err = got_pkt_flushpkt(outfd, chattygot);
1452 err = got_error(GOT_ERR_PRIVSEP_MSG);
1461 echo_error(err, outfd, chattygot);
1465 const struct got_error *
1466 got_serve(int infd, int outfd, const char *gitcmd, int gotd_sock, int chattygot)
1468 const struct got_error *err = NULL;
1469 char *command = NULL, *repo_path = NULL;
1471 err = parse_command(&command, &repo_path, gitcmd);
1475 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1476 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1477 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1478 err = serve_write(infd, outfd, gotd_sock, repo_path, chattygot);
1480 err = got_error(GOT_ERR_BAD_PACKET);