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 err = got_pkt_writepkt(outfd, buf, len, chattygot);
272 static const struct got_error *
273 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
274 const char *repo_path, int chattygot)
276 const struct got_error *err = NULL;
279 struct gotd_imsg_list_refs lsref;
280 struct gotd_imsg_reflist ireflist;
281 struct gotd_imsg_ref iref;
282 struct gotd_imsg_symref isymref;
284 int have_nrefs = 0, sent_capabilities = 0;
285 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
286 char *refname = NULL;
288 memset(&imsg, 0, sizeof(imsg));
289 memset(&lsref, 0, sizeof(lsref));
291 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
292 sizeof(lsref.repo_name))
293 return got_error(GOT_ERR_NO_SPACE);
294 lsref.client_is_reading = client_is_reading;
296 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
297 &lsref, sizeof(lsref)) == -1)
298 return got_error_from_errno("imsg_compose LIST_REFS");
300 err = gotd_imsg_flush(ibuf);
304 while (!have_nrefs || nrefs > 0) {
305 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
308 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
309 switch (imsg.hdr.type) {
310 case GOTD_IMSG_ERROR:
311 err = gotd_imsg_recv_error(NULL, &imsg);
313 case GOTD_IMSG_REFLIST:
314 if (have_nrefs || nrefs > 0) {
315 err = got_error(GOT_ERR_PRIVSEP_MSG);
318 if (datalen != sizeof(ireflist)) {
319 err = got_error(GOT_ERR_PRIVSEP_MSG);
322 memcpy(&ireflist, imsg.data, sizeof(ireflist));
323 nrefs = ireflist.nrefs;
326 err = send_zero_refs(outfd, client_is_reading,
330 if (!have_nrefs || nrefs == 0) {
331 err = got_error(GOT_ERR_PRIVSEP_MSG);
334 if (datalen < sizeof(iref)) {
335 err = got_error(GOT_ERR_PRIVSEP_MSG);
338 memcpy(&iref, imsg.data, sizeof(iref));
339 if (datalen != sizeof(iref) + iref.name_len) {
340 err = got_error(GOT_ERR_PRIVSEP_LEN);
343 refname = malloc(iref.name_len + 1);
344 if (refname == NULL) {
345 err = got_error_from_errno("malloc");
348 memcpy(refname, imsg.data + sizeof(iref),
350 refname[iref.name_len] = '\0';
351 err = send_ref(outfd, iref.id, refname,
352 !sent_capabilities, client_is_reading,
358 sent_capabilities = 1;
362 case GOTD_IMSG_SYMREF:
363 if (!have_nrefs || nrefs == 0) {
364 err = got_error(GOT_ERR_PRIVSEP_MSG);
367 if (datalen < sizeof(isymref)) {
368 err = got_error(GOT_ERR_PRIVSEP_LEN);
371 memcpy(&isymref, imsg.data, sizeof(isymref));
372 if (datalen != sizeof(isymref) + isymref.name_len +
373 isymref.target_len) {
374 err = got_error(GOT_ERR_PRIVSEP_LEN);
379 * For now, we only announce one symbolic ref,
380 * as part of our capability advertisement.
382 if (sent_capabilities || symrefstr != NULL ||
383 symrefname != NULL || symreftarget != NULL)
386 symrefname = malloc(isymref.name_len + 1);
387 if (symrefname == NULL) {
388 err = got_error_from_errno("malloc");
391 memcpy(symrefname, imsg.data + sizeof(isymref),
393 symrefname[isymref.name_len] = '\0';
395 symreftarget = malloc(isymref.target_len + 1);
396 if (symreftarget == NULL) {
397 err = got_error_from_errno("malloc");
401 imsg.data + sizeof(isymref) + isymref.name_len,
403 symreftarget[isymref.target_len] = '\0';
405 if (asprintf(&symrefstr, "%s:%s", symrefname,
406 symreftarget) == -1) {
407 err = got_error_from_errno("asprintf");
410 err = send_ref(outfd, isymref.target_id, symrefname,
411 !sent_capabilities, client_is_reading, symrefstr,
417 sent_capabilities = 1;
422 err = got_error(GOT_ERR_PRIVSEP_MSG);
429 err = got_pkt_flushpkt(outfd, chattygot);
439 static const struct got_error *
440 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
442 const struct got_error *err;
443 char *id_str = NULL, *client_capabilities = NULL;
445 err = got_gitproto_parse_want_line(&id_str,
446 &client_capabilities, buf, len);
450 if (!got_parse_sha1_digest(id, id_str)) {
451 err = got_error_msg(GOT_ERR_BAD_PACKET,
452 "want-line with bad object ID");
456 if (client_capabilities) {
457 err = got_gitproto_match_capabilities(common_capabilities,
458 NULL, client_capabilities, read_capabilities,
459 nitems(read_capabilities));
465 free(client_capabilities);
469 static const struct got_error *
470 parse_have_line(uint8_t *id, char *buf, size_t len)
472 const struct got_error *err;
475 err = got_gitproto_parse_have_line(&id_str, buf, len);
479 if (!got_parse_sha1_digest(id, id_str)) {
480 err = got_error_msg(GOT_ERR_BAD_PACKET,
481 "have-line with bad object ID");
489 static const struct got_error *
490 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
492 const struct got_error *err = NULL;
493 struct gotd_imsg_capability icapa;
497 memset(&icapa, 0, sizeof(icapa));
499 icapa.key_len = strlen(capa->key);
500 len = sizeof(icapa) + icapa.key_len;
502 icapa.value_len = strlen(capa->value);
503 len += icapa.value_len;
506 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
508 err = got_error_from_errno("imsg_create CAPABILITY");
512 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
513 return got_error_from_errno("imsg_add CAPABILITY");
514 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
515 return got_error_from_errno("imsg_add CAPABILITY");
517 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
518 return got_error_from_errno("imsg_add CAPABILITY");
522 imsg_close(ibuf, wbuf);
527 static const struct got_error *
528 send_capabilities(int *use_sidebands, int *report_status,
529 char *capabilities_str, struct imsgbuf *ibuf)
531 const struct got_error *err = NULL;
532 struct gotd_imsg_capabilities icapas;
533 struct got_capability *capa = NULL;
536 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
541 icapas.ncapabilities = ncapa;
542 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
543 &icapas, sizeof(icapas)) == -1) {
544 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
548 for (i = 0; i < ncapa; i++) {
549 err = send_capability(&capa[i], ibuf);
553 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
556 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
564 static const struct got_error *
565 forward_flushpkt(struct imsgbuf *ibuf)
567 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
568 return got_error_from_errno("imsg_compose FLUSH");
570 return gotd_imsg_flush(ibuf);
573 static const struct got_error *
574 recv_ack(struct imsg *imsg, uint8_t *expected_id)
576 struct gotd_imsg_ack iack;
579 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
580 if (datalen != sizeof(iack))
581 return got_error(GOT_ERR_PRIVSEP_LEN);
583 memcpy(&iack, imsg->data, sizeof(iack));
584 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
585 return got_error(GOT_ERR_BAD_OBJ_ID);
590 static const struct got_error *
591 recv_nak(struct imsg *imsg, uint8_t *expected_id)
593 struct gotd_imsg_ack inak;
596 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
597 if (datalen != sizeof(inak))
598 return got_error(GOT_ERR_PRIVSEP_LEN);
600 memcpy(&inak, imsg->data, sizeof(inak));
601 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
602 return got_error(GOT_ERR_BAD_OBJ_ID);
608 static const struct got_error *
609 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
610 char *buf, size_t len, int expect_capabilities, int chattygot)
612 const struct got_error *err;
613 struct gotd_imsg_want iwant;
614 char *capabilities_str;
618 memset(&iwant, 0, sizeof(iwant));
619 memset(&imsg, 0, sizeof(imsg));
621 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
625 if (capabilities_str) {
626 if (!expect_capabilities) {
627 err = got_error_msg(GOT_ERR_BAD_PACKET,
628 "unexpected capability announcement received");
631 err = send_capabilities(use_sidebands, NULL, capabilities_str,
638 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
639 &iwant, sizeof(iwant)) == -1) {
640 err = got_error_from_errno("imsg_compose WANT");
644 err = gotd_imsg_flush(ibuf);
649 * Wait for an ACK, or an error in case the desired object
652 while (!done && err == NULL) {
653 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
656 switch (imsg.hdr.type) {
657 case GOTD_IMSG_ERROR:
658 err = gotd_imsg_recv_error(NULL, &imsg);
661 err = recv_ack(&imsg, iwant.object_id);
667 err = got_error(GOT_ERR_PRIVSEP_MSG);
674 free(capabilities_str);
678 static const struct got_error *
679 send_ack(int outfd, uint8_t *id, int chattygot)
681 char hex[SHA1_DIGEST_STRING_LENGTH];
682 char buf[GOT_PKT_MAX];
685 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
686 return got_error(GOT_ERR_BAD_OBJ_ID);
688 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
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 send_nak(int outfd, int chattygot)
701 len = snprintf(buf, sizeof(buf), "NAK\n");
702 if (len >= sizeof(buf))
703 return got_error(GOT_ERR_NO_SPACE);
705 return got_pkt_writepkt(outfd, buf, len, chattygot);
708 static const struct got_error *
709 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
710 size_t len, int chattygot)
712 const struct got_error *err;
713 struct gotd_imsg_have ihave;
717 memset(&ihave, 0, sizeof(ihave));
718 memset(&imsg, 0, sizeof(imsg));
720 err = parse_have_line(ihave.object_id, buf, len);
724 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
725 &ihave, sizeof(ihave)) == -1)
726 return got_error_from_errno("imsg_compose HAVE");
728 err = gotd_imsg_flush(ibuf);
733 * Wait for an ACK or a NAK, indicating whether a common
734 * commit object has been found.
736 while (!done && err == NULL) {
737 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
740 switch (imsg.hdr.type) {
741 case GOTD_IMSG_ERROR:
742 err = gotd_imsg_recv_error(NULL, &imsg);
745 err = recv_ack(&imsg, ihave.object_id);
749 err = send_ack(outfd, ihave.object_id,
758 err = recv_nak(&imsg, ihave.object_id);
764 err = got_error(GOT_ERR_PRIVSEP_MSG);
774 static const struct got_error *
775 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
777 const struct got_error *err;
782 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
783 return got_error_from_errno("imsg_compose DONE");
785 err = gotd_imsg_flush(ibuf);
789 while (*packfd == -1 && err == NULL) {
790 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
794 switch (imsg.hdr.type) {
795 case GOTD_IMSG_ERROR:
796 err = gotd_imsg_recv_error(NULL, &imsg);
798 case GOTD_IMSG_PACKFILE_PIPE:
802 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
805 err = got_error(GOT_ERR_PRIVSEP_MSG);
815 static const struct got_error *
816 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
818 const struct got_error *err = NULL;
819 int pack_starting = 0;
820 struct gotd_imsg_packfile_progress iprog;
821 char buf[GOT_PKT_MAX];
824 int p_deltify = 0, n;
825 const char *eol = "\r";
827 memset(&imsg, 0, sizeof(imsg));
829 while (!pack_starting && err == NULL) {
830 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
834 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
835 switch (imsg.hdr.type) {
836 case GOTD_IMSG_ERROR:
837 err = gotd_imsg_recv_error(NULL, &imsg);
839 case GOTD_IMSG_PACKFILE_READY:
843 case GOTD_IMSG_PACKFILE_PROGRESS:
844 if (datalen != sizeof(iprog)) {
845 err = got_error(GOT_ERR_PRIVSEP_LEN);
848 memcpy(&iprog, imsg.data, sizeof(iprog));
849 if (iprog.nobj_total > 0) {
850 p_deltify = (iprog.nobj_deltify * 100) /
853 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
854 n = snprintf(&buf[1], sizeof(buf) - 1,
855 "%d commits colored, "
861 if (n >= sizeof(buf) - 1)
863 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
866 err = got_error(GOT_ERR_PRIVSEP_MSG);
876 static const struct got_error *
877 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
880 const struct got_error *err = NULL;
881 char buf[GOT_PKT_MAX];
885 STATE_EXPECT_MORE_WANT,
890 enum protostate curstate = STATE_EXPECT_WANT;
891 int have_ack = 0, use_sidebands = 0, seen_have = 0;
893 size_t pack_chunksize;
895 imsg_init(&ibuf, gotd_sock);
897 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
901 while (curstate != STATE_DONE) {
904 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
908 if (curstate != STATE_EXPECT_MORE_WANT &&
909 curstate != STATE_EXPECT_HAVE) {
910 err = got_error_msg(GOT_ERR_BAD_PACKET,
911 "unexpected flush packet received");
914 err = forward_flushpkt(&ibuf);
917 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
918 err = send_nak(outfd, chattygot);
922 if (curstate == STATE_EXPECT_MORE_WANT)
923 curstate = STATE_EXPECT_HAVE;
925 curstate = STATE_EXPECT_DONE;
926 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
927 if (curstate != STATE_EXPECT_WANT &&
928 curstate != STATE_EXPECT_MORE_WANT) {
929 err = got_error_msg(GOT_ERR_BAD_PACKET,
930 "unexpected 'want' packet");
933 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
934 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
937 if (curstate == STATE_EXPECT_WANT)
938 curstate = STATE_EXPECT_MORE_WANT;
939 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
940 if (curstate != STATE_EXPECT_HAVE) {
941 err = got_error_msg(GOT_ERR_BAD_PACKET,
942 "unexpected 'have' packet");
945 err = recv_have(&have_ack, outfd, &ibuf, buf, n,
950 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
951 if (curstate != STATE_EXPECT_HAVE &&
952 curstate != STATE_EXPECT_DONE) {
953 err = got_error_msg(GOT_ERR_BAD_PACKET,
954 "unexpected 'done' packet");
957 err = recv_done(&packfd, outfd, &ibuf, chattygot);
960 curstate = STATE_DONE;
963 err = got_error(GOT_ERR_BAD_PACKET);
969 err = send_nak(outfd, chattygot);
975 err = relay_progress_reports(&ibuf, outfd, chattygot);
978 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
980 pack_chunksize = sizeof(buf);
985 r = read(packfd, use_sidebands ? &buf[1] : buf,
988 err = got_error_from_errno("read");
991 err = got_pkt_flushpkt(outfd, chattygot);
996 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
997 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1001 err = got_poll_write_full(outfd, buf, r);
1003 if (err->code == GOT_ERR_EOF)
1011 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1012 err = got_error_from_errno("close");
1014 echo_error(err, outfd, chattygot);
1018 static const struct got_error *
1019 parse_ref_update_line(char **common_capabilities, char **refname,
1020 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1022 const struct got_error *err;
1023 char *old_id_str = NULL, *new_id_str = NULL;
1024 char *client_capabilities = NULL;
1028 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1029 refname, &client_capabilities, buf, len);
1033 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1034 !got_parse_sha1_digest(new_id, new_id_str)) {
1035 err = got_error_msg(GOT_ERR_BAD_PACKET,
1036 "ref-update with bad object ID");
1039 if (!got_ref_name_is_valid(*refname)) {
1040 err = got_error_msg(GOT_ERR_BAD_PACKET,
1041 "ref-update with bad reference name");
1045 if (client_capabilities) {
1046 err = got_gitproto_match_capabilities(common_capabilities,
1047 NULL, client_capabilities, write_capabilities,
1048 nitems(write_capabilities));
1055 free(client_capabilities);
1063 static const struct got_error *
1064 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1065 char *buf, size_t len, int expect_capabilities, int chattygot)
1067 const struct got_error *err;
1068 struct gotd_imsg_ref_update iref;
1070 char *capabilities_str = NULL, *refname = NULL;
1074 memset(&iref, 0, sizeof(iref));
1075 memset(&imsg, 0, sizeof(imsg));
1077 err = parse_ref_update_line(&capabilities_str, &refname,
1078 iref.old_id, iref.new_id, buf, len);
1082 if (capabilities_str) {
1083 if (!expect_capabilities) {
1084 err = got_error_msg(GOT_ERR_BAD_PACKET,
1085 "unexpected capability announcement received");
1088 err = send_capabilities(NULL, report_status, capabilities_str,
1094 iref.name_len = strlen(refname);
1095 len = sizeof(iref) + iref.name_len;
1096 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1098 err = got_error_from_errno("imsg_create REF_UPDATE");
1102 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1103 return got_error_from_errno("imsg_add REF_UPDATE");
1104 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1105 return got_error_from_errno("imsg_add REF_UPDATE");
1107 imsg_close(ibuf, wbuf);
1109 err = gotd_imsg_flush(ibuf);
1113 /* Wait for ACK or an error. */
1114 while (!done && err == NULL) {
1115 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1118 switch (imsg.hdr.type) {
1119 case GOTD_IMSG_ERROR:
1120 err = gotd_imsg_recv_error(NULL, &imsg);
1123 err = recv_ack(&imsg, iref.new_id);
1129 err = got_error(GOT_ERR_PRIVSEP_MSG);
1136 free(capabilities_str);
1141 static const struct got_error *
1142 recv_packfile(struct imsg *imsg, int infd)
1144 const struct got_error *err = NULL;
1147 char buf[GOT_PKT_MAX];
1150 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1152 return got_error(GOT_ERR_PRIVSEP_MSG);
1155 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1158 while (!pack_done) {
1161 err = got_poll_fd(infd, POLLIN, 1);
1163 if (err->code != GOT_ERR_TIMEOUT)
1167 r = read(infd, buf, sizeof(buf));
1169 err = got_error_from_errno("read");
1174 * Git clients hang up their side of the
1175 * connection after sending the pack file.
1184 /* Detect gotd(8) closing the pack pipe when done. */
1185 err = got_poll_fd(packfd, POLLOUT, 1);
1187 if (err->code != GOT_ERR_EOF)
1193 /* Write pack data and/or detect pipe being closed. */
1194 err = got_poll_write_full(packfd, buf, r);
1196 if (err->code == GOT_ERR_EOF)
1207 static const struct got_error *
1208 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1210 const struct got_error *err = NULL;
1211 struct gotd_imsg_packfile_status istatus;
1212 char buf[GOT_PKT_MAX];
1213 size_t datalen, len;
1214 char *reason = NULL;
1216 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1217 if (datalen < sizeof(istatus))
1218 return got_error(GOT_ERR_PRIVSEP_LEN);
1219 memcpy(&istatus, imsg->data, sizeof(istatus));
1220 if (datalen != sizeof(istatus) + istatus.reason_len)
1221 return got_error(GOT_ERR_PRIVSEP_LEN);
1223 reason = malloc(istatus.reason_len + 1);
1224 if (reason == NULL) {
1225 err = got_error_from_errno("malloc");
1228 memcpy(reason, imsg->data + sizeof(istatus), istatus.reason_len);
1229 reason[istatus.reason_len] = '\0';
1232 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1234 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1235 if (len >= sizeof(buf)) {
1236 err = got_error(GOT_ERR_NO_SPACE);
1240 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1246 static const struct got_error *
1247 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1249 const struct got_error *err = NULL;
1250 struct gotd_imsg_ref_update_ok iok;
1251 size_t datalen, len;
1252 char buf[GOT_PKT_MAX];
1253 char *refname = NULL;
1255 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1256 if (datalen < sizeof(iok))
1257 return got_error(GOT_ERR_PRIVSEP_LEN);
1258 memcpy(&iok, imsg->data, sizeof(iok));
1259 if (datalen != sizeof(iok) + iok.name_len)
1260 return got_error(GOT_ERR_PRIVSEP_LEN);
1262 memcpy(&iok, imsg->data, sizeof(iok));
1264 refname = malloc(iok.name_len + 1);
1265 if (refname == NULL)
1266 return got_error_from_errno("malloc");
1267 memcpy(refname, imsg->data + sizeof(iok), iok.name_len);
1268 refname[iok.name_len] = '\0';
1270 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1271 if (len >= sizeof(buf)) {
1272 err = got_error(GOT_ERR_NO_SPACE);
1276 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1282 static const struct got_error *
1283 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1285 const struct got_error *err = NULL;
1286 struct gotd_imsg_ref_update_ng ing;
1287 size_t datalen, len;
1288 char buf[GOT_PKT_MAX];
1289 char *refname = NULL, *reason = NULL;
1291 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1292 if (datalen < sizeof(ing))
1293 return got_error(GOT_ERR_PRIVSEP_LEN);
1294 memcpy(&ing, imsg->data, sizeof(ing));
1295 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1296 return got_error(GOT_ERR_PRIVSEP_LEN);
1298 memcpy(&ing, imsg->data, sizeof(ing));
1300 refname = malloc(ing.name_len + 1);
1301 if (refname == NULL)
1302 return got_error_from_errno("malloc");
1303 memcpy(refname, imsg->data + sizeof(ing), ing.name_len);
1304 refname[ing.name_len] = '\0';
1306 reason = malloc(ing.reason_len + 1);
1307 if (reason == NULL) {
1308 err = got_error_from_errno("malloc");
1311 memcpy(refname, imsg->data + sizeof(ing) + ing.name_len,
1313 refname[ing.reason_len] = '\0';
1315 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1316 if (len >= sizeof(buf)) {
1317 err = got_error(GOT_ERR_NO_SPACE);
1321 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1328 static const struct got_error *
1329 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1332 const struct got_error *err = NULL;
1333 char buf[GOT_PKT_MAX];
1334 struct imsgbuf ibuf;
1336 STATE_EXPECT_REF_UPDATE,
1337 STATE_EXPECT_MORE_REF_UPDATES,
1338 STATE_EXPECT_PACKFILE,
1339 STATE_PACKFILE_RECEIVED,
1342 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1344 int report_status = 0;
1346 imsg_init(&ibuf, gotd_sock);
1347 memset(&imsg, 0, sizeof(imsg));
1349 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1353 while (curstate != STATE_EXPECT_PACKFILE) {
1356 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1360 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1361 err = got_error_msg(GOT_ERR_BAD_PACKET,
1362 "unexpected flush packet received");
1365 err = forward_flushpkt(&ibuf);
1368 curstate = STATE_EXPECT_PACKFILE;
1369 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1370 if (curstate != STATE_EXPECT_REF_UPDATE &&
1371 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1372 err = got_error_msg(GOT_ERR_BAD_PACKET,
1373 "unexpected ref-update packet");
1376 if (curstate == STATE_EXPECT_REF_UPDATE) {
1377 err = recv_ref_update(&report_status,
1378 outfd, &ibuf, buf, n, 1, chattygot);
1380 err = recv_ref_update(NULL, outfd, &ibuf,
1381 buf, n, 0, chattygot);
1385 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1387 err = got_error(GOT_ERR_BAD_PACKET);
1392 while (curstate != STATE_PACKFILE_RECEIVED) {
1393 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1396 switch (imsg.hdr.type) {
1397 case GOTD_IMSG_ERROR:
1398 err = gotd_imsg_recv_error(NULL, &imsg);
1400 case GOTD_IMSG_PACKFILE_PIPE:
1401 err = recv_packfile(&imsg, infd);
1403 if (err->code != GOT_ERR_EOF)
1406 * EOF is reported when the client hangs up,
1407 * which can happen with Git clients.
1408 * The socket should stay half-open so we
1409 * can still send our reports if requested.
1413 curstate = STATE_PACKFILE_RECEIVED;
1416 err = got_error(GOT_ERR_PRIVSEP_MSG);
1425 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1426 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1429 switch (imsg.hdr.type) {
1430 case GOTD_IMSG_ERROR:
1431 err = gotd_imsg_recv_error(NULL, &imsg);
1433 case GOTD_IMSG_PACKFILE_STATUS:
1436 err = report_unpack_status(&imsg, outfd, chattygot);
1438 case GOTD_IMSG_REF_UPDATE_OK:
1441 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1443 case GOTD_IMSG_REF_UPDATE_NG:
1446 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1448 case GOTD_IMSG_REFS_UPDATED:
1449 curstate = STATE_REFS_UPDATED;
1450 err = got_pkt_flushpkt(outfd, chattygot);
1453 err = got_error(GOT_ERR_PRIVSEP_MSG);
1462 echo_error(err, outfd, chattygot);
1466 const struct got_error *
1467 got_serve(int infd, int outfd, const char *gitcmd, int gotd_sock, int chattygot)
1469 const struct got_error *err = NULL;
1470 char *command = NULL, *repo_path = NULL;
1472 err = parse_command(&command, &repo_path, gitcmd);
1476 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1477 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1478 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1479 err = serve_write(infd, outfd, gotd_sock, repo_path, chattygot);
1481 err = got_error(GOT_ERR_BAD_PACKET);