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 = strndup(imsg.data + sizeof(iref),
344 if (refname == NULL) {
345 err = got_error_from_errno("strndup");
348 err = send_ref(outfd, iref.id, refname,
349 !sent_capabilities, client_is_reading,
355 sent_capabilities = 1;
359 case GOTD_IMSG_SYMREF:
360 if (!have_nrefs || nrefs == 0) {
361 err = got_error(GOT_ERR_PRIVSEP_MSG);
364 if (datalen < sizeof(isymref)) {
365 err = got_error(GOT_ERR_PRIVSEP_LEN);
368 memcpy(&isymref, imsg.data, sizeof(isymref));
369 if (datalen != sizeof(isymref) + isymref.name_len +
370 isymref.target_len) {
371 err = got_error(GOT_ERR_PRIVSEP_LEN);
376 * For now, we only announce one symbolic ref,
377 * as part of our capability advertisement.
379 if (sent_capabilities || symrefstr != NULL ||
380 symrefname != NULL || symreftarget != NULL)
383 symrefname = strndup(imsg.data + sizeof(isymref),
385 if (symrefname == NULL) {
386 err = got_error_from_errno("malloc");
390 symreftarget = strndup(
391 imsg.data + sizeof(isymref) + isymref.name_len,
393 if (symreftarget == NULL) {
394 err = got_error_from_errno("strndup");
398 if (asprintf(&symrefstr, "%s:%s", symrefname,
399 symreftarget) == -1) {
400 err = got_error_from_errno("asprintf");
403 err = send_ref(outfd, isymref.target_id, symrefname,
404 !sent_capabilities, client_is_reading, symrefstr,
410 sent_capabilities = 1;
415 err = got_error(GOT_ERR_PRIVSEP_MSG);
422 err = got_pkt_flushpkt(outfd, chattygot);
432 static const struct got_error *
433 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
435 const struct got_error *err;
436 char *id_str = NULL, *client_capabilities = NULL;
438 err = got_gitproto_parse_want_line(&id_str,
439 &client_capabilities, buf, len);
443 if (!got_parse_sha1_digest(id, id_str)) {
444 err = got_error_msg(GOT_ERR_BAD_PACKET,
445 "want-line with bad object ID");
449 if (client_capabilities) {
450 err = got_gitproto_match_capabilities(common_capabilities,
451 NULL, client_capabilities, read_capabilities,
452 nitems(read_capabilities));
458 free(client_capabilities);
462 static const struct got_error *
463 parse_have_line(uint8_t *id, char *buf, size_t len)
465 const struct got_error *err;
468 err = got_gitproto_parse_have_line(&id_str, buf, len);
472 if (!got_parse_sha1_digest(id, id_str)) {
473 err = got_error_msg(GOT_ERR_BAD_PACKET,
474 "have-line with bad object ID");
482 static const struct got_error *
483 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
485 const struct got_error *err = NULL;
486 struct gotd_imsg_capability icapa;
490 memset(&icapa, 0, sizeof(icapa));
492 icapa.key_len = strlen(capa->key);
493 len = sizeof(icapa) + icapa.key_len;
495 icapa.value_len = strlen(capa->value);
496 len += icapa.value_len;
499 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
501 err = got_error_from_errno("imsg_create CAPABILITY");
505 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
506 return got_error_from_errno("imsg_add CAPABILITY");
507 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
508 return got_error_from_errno("imsg_add CAPABILITY");
510 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
511 return got_error_from_errno("imsg_add CAPABILITY");
515 imsg_close(ibuf, wbuf);
520 static const struct got_error *
521 send_capabilities(int *use_sidebands, int *report_status,
522 char *capabilities_str, struct imsgbuf *ibuf)
524 const struct got_error *err = NULL;
525 struct gotd_imsg_capabilities icapas;
526 struct got_capability *capa = NULL;
529 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
534 icapas.ncapabilities = ncapa;
535 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
536 &icapas, sizeof(icapas)) == -1) {
537 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
541 for (i = 0; i < ncapa; i++) {
542 err = send_capability(&capa[i], ibuf);
546 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
549 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
557 static const struct got_error *
558 forward_flushpkt(struct imsgbuf *ibuf)
560 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
561 return got_error_from_errno("imsg_compose FLUSH");
563 return gotd_imsg_flush(ibuf);
566 static const struct got_error *
567 recv_ack(struct imsg *imsg, uint8_t *expected_id)
569 struct gotd_imsg_ack iack;
572 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
573 if (datalen != sizeof(iack))
574 return got_error(GOT_ERR_PRIVSEP_LEN);
576 memcpy(&iack, imsg->data, sizeof(iack));
577 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
578 return got_error(GOT_ERR_BAD_OBJ_ID);
583 static const struct got_error *
584 recv_nak(struct imsg *imsg, uint8_t *expected_id)
586 struct gotd_imsg_ack inak;
589 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
590 if (datalen != sizeof(inak))
591 return got_error(GOT_ERR_PRIVSEP_LEN);
593 memcpy(&inak, imsg->data, sizeof(inak));
594 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
595 return got_error(GOT_ERR_BAD_OBJ_ID);
601 static const struct got_error *
602 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
603 char *buf, size_t len, int expect_capabilities, int chattygot)
605 const struct got_error *err;
606 struct gotd_imsg_want iwant;
607 char *capabilities_str;
611 memset(&iwant, 0, sizeof(iwant));
612 memset(&imsg, 0, sizeof(imsg));
614 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
618 if (capabilities_str) {
619 if (!expect_capabilities) {
620 err = got_error_msg(GOT_ERR_BAD_PACKET,
621 "unexpected capability announcement received");
624 err = send_capabilities(use_sidebands, NULL, capabilities_str,
631 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
632 &iwant, sizeof(iwant)) == -1) {
633 err = got_error_from_errno("imsg_compose WANT");
637 err = gotd_imsg_flush(ibuf);
642 * Wait for an ACK, or an error in case the desired object
645 while (!done && err == NULL) {
646 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
649 switch (imsg.hdr.type) {
650 case GOTD_IMSG_ERROR:
651 err = gotd_imsg_recv_error(NULL, &imsg);
654 err = recv_ack(&imsg, iwant.object_id);
660 err = got_error(GOT_ERR_PRIVSEP_MSG);
667 free(capabilities_str);
671 static const struct got_error *
672 send_ack(int outfd, uint8_t *id, int chattygot)
674 char hex[SHA1_DIGEST_STRING_LENGTH];
675 char buf[GOT_PKT_MAX];
678 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
679 return got_error(GOT_ERR_BAD_OBJ_ID);
681 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
682 if (len >= sizeof(buf))
683 return got_error(GOT_ERR_NO_SPACE);
685 return got_pkt_writepkt(outfd, buf, len, chattygot);
688 static const struct got_error *
689 send_nak(int outfd, int chattygot)
694 len = snprintf(buf, sizeof(buf), "NAK\n");
695 if (len >= sizeof(buf))
696 return got_error(GOT_ERR_NO_SPACE);
698 return got_pkt_writepkt(outfd, buf, len, chattygot);
701 static const struct got_error *
702 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
703 size_t len, int chattygot)
705 const struct got_error *err;
706 struct gotd_imsg_have ihave;
710 memset(&ihave, 0, sizeof(ihave));
711 memset(&imsg, 0, sizeof(imsg));
713 err = parse_have_line(ihave.object_id, buf, len);
717 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
718 &ihave, sizeof(ihave)) == -1)
719 return got_error_from_errno("imsg_compose HAVE");
721 err = gotd_imsg_flush(ibuf);
726 * Wait for an ACK or a NAK, indicating whether a common
727 * commit object has been found.
729 while (!done && err == NULL) {
730 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
733 switch (imsg.hdr.type) {
734 case GOTD_IMSG_ERROR:
735 err = gotd_imsg_recv_error(NULL, &imsg);
738 err = recv_ack(&imsg, ihave.object_id);
742 err = send_ack(outfd, ihave.object_id,
751 err = recv_nak(&imsg, ihave.object_id);
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
767 static const struct got_error *
768 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
770 const struct got_error *err;
775 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
776 return got_error_from_errno("imsg_compose DONE");
778 err = gotd_imsg_flush(ibuf);
782 while (*packfd == -1 && err == NULL) {
783 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
787 switch (imsg.hdr.type) {
788 case GOTD_IMSG_ERROR:
789 err = gotd_imsg_recv_error(NULL, &imsg);
791 case GOTD_IMSG_PACKFILE_PIPE:
795 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
798 err = got_error(GOT_ERR_PRIVSEP_MSG);
808 static const struct got_error *
809 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
811 const struct got_error *err = NULL;
812 int pack_starting = 0;
813 struct gotd_imsg_packfile_progress iprog;
814 char buf[GOT_PKT_MAX];
817 int p_deltify = 0, n;
818 const char *eol = "\r";
820 memset(&imsg, 0, sizeof(imsg));
822 while (!pack_starting && err == NULL) {
823 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
827 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
828 switch (imsg.hdr.type) {
829 case GOTD_IMSG_ERROR:
830 err = gotd_imsg_recv_error(NULL, &imsg);
832 case GOTD_IMSG_PACKFILE_READY:
836 case GOTD_IMSG_PACKFILE_PROGRESS:
837 if (datalen != sizeof(iprog)) {
838 err = got_error(GOT_ERR_PRIVSEP_LEN);
841 memcpy(&iprog, imsg.data, sizeof(iprog));
842 if (iprog.nobj_total > 0) {
843 p_deltify = (iprog.nobj_deltify * 100) /
846 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
847 n = snprintf(&buf[1], sizeof(buf) - 1,
848 "%d commits colored, "
854 if (n >= sizeof(buf) - 1)
856 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
859 err = got_error(GOT_ERR_PRIVSEP_MSG);
869 static const struct got_error *
870 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
873 const struct got_error *err = NULL;
874 char buf[GOT_PKT_MAX];
878 STATE_EXPECT_MORE_WANT,
883 enum protostate curstate = STATE_EXPECT_WANT;
884 int have_ack = 0, use_sidebands = 0, seen_have = 0;
886 size_t pack_chunksize;
888 imsg_init(&ibuf, gotd_sock);
890 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
894 while (curstate != STATE_DONE) {
897 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
901 if (curstate != STATE_EXPECT_MORE_WANT &&
902 curstate != STATE_EXPECT_HAVE) {
903 err = got_error_msg(GOT_ERR_BAD_PACKET,
904 "unexpected flush packet received");
907 err = forward_flushpkt(&ibuf);
910 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
911 err = send_nak(outfd, chattygot);
915 if (curstate == STATE_EXPECT_MORE_WANT)
916 curstate = STATE_EXPECT_HAVE;
918 curstate = STATE_EXPECT_DONE;
919 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
920 if (curstate != STATE_EXPECT_WANT &&
921 curstate != STATE_EXPECT_MORE_WANT) {
922 err = got_error_msg(GOT_ERR_BAD_PACKET,
923 "unexpected 'want' packet");
926 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
927 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
930 if (curstate == STATE_EXPECT_WANT)
931 curstate = STATE_EXPECT_MORE_WANT;
932 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
933 if (curstate != STATE_EXPECT_HAVE) {
934 err = got_error_msg(GOT_ERR_BAD_PACKET,
935 "unexpected 'have' packet");
938 err = recv_have(&have_ack, outfd, &ibuf, buf, n,
943 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
944 if (curstate != STATE_EXPECT_HAVE &&
945 curstate != STATE_EXPECT_DONE) {
946 err = got_error_msg(GOT_ERR_BAD_PACKET,
947 "unexpected 'done' packet");
950 err = recv_done(&packfd, outfd, &ibuf, chattygot);
953 curstate = STATE_DONE;
956 err = got_error(GOT_ERR_BAD_PACKET);
962 err = send_nak(outfd, chattygot);
968 err = relay_progress_reports(&ibuf, outfd, chattygot);
971 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
973 pack_chunksize = sizeof(buf);
978 r = read(packfd, use_sidebands ? &buf[1] : buf,
981 err = got_error_from_errno("read");
984 err = got_pkt_flushpkt(outfd, chattygot);
989 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
990 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
994 err = got_poll_write_full(outfd, buf, r);
996 if (err->code == GOT_ERR_EOF)
1004 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1005 err = got_error_from_errno("close");
1007 echo_error(err, outfd, chattygot);
1011 static const struct got_error *
1012 parse_ref_update_line(char **common_capabilities, char **refname,
1013 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1015 const struct got_error *err;
1016 char *old_id_str = NULL, *new_id_str = NULL;
1017 char *client_capabilities = NULL;
1021 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1022 refname, &client_capabilities, buf, len);
1026 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1027 !got_parse_sha1_digest(new_id, new_id_str)) {
1028 err = got_error_msg(GOT_ERR_BAD_PACKET,
1029 "ref-update with bad object ID");
1032 if (!got_ref_name_is_valid(*refname)) {
1033 err = got_error_msg(GOT_ERR_BAD_PACKET,
1034 "ref-update with bad reference name");
1038 if (client_capabilities) {
1039 err = got_gitproto_match_capabilities(common_capabilities,
1040 NULL, client_capabilities, write_capabilities,
1041 nitems(write_capabilities));
1048 free(client_capabilities);
1056 static const struct got_error *
1057 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1058 char *buf, size_t len, int expect_capabilities, int chattygot)
1060 const struct got_error *err;
1061 struct gotd_imsg_ref_update iref;
1063 char *capabilities_str = NULL, *refname = NULL;
1067 memset(&iref, 0, sizeof(iref));
1068 memset(&imsg, 0, sizeof(imsg));
1070 err = parse_ref_update_line(&capabilities_str, &refname,
1071 iref.old_id, iref.new_id, buf, len);
1075 if (capabilities_str) {
1076 if (!expect_capabilities) {
1077 err = got_error_msg(GOT_ERR_BAD_PACKET,
1078 "unexpected capability announcement received");
1081 err = send_capabilities(NULL, report_status, capabilities_str,
1087 iref.name_len = strlen(refname);
1088 len = sizeof(iref) + iref.name_len;
1089 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1091 err = got_error_from_errno("imsg_create REF_UPDATE");
1095 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1096 return got_error_from_errno("imsg_add REF_UPDATE");
1097 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1098 return got_error_from_errno("imsg_add REF_UPDATE");
1100 imsg_close(ibuf, wbuf);
1102 err = gotd_imsg_flush(ibuf);
1106 /* Wait for ACK or an error. */
1107 while (!done && err == NULL) {
1108 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1111 switch (imsg.hdr.type) {
1112 case GOTD_IMSG_ERROR:
1113 err = gotd_imsg_recv_error(NULL, &imsg);
1116 err = recv_ack(&imsg, iref.new_id);
1122 err = got_error(GOT_ERR_PRIVSEP_MSG);
1129 free(capabilities_str);
1134 static const struct got_error *
1135 recv_packfile(struct imsg *imsg, int infd)
1137 const struct got_error *err = NULL;
1140 char buf[GOT_PKT_MAX];
1143 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1145 return got_error(GOT_ERR_PRIVSEP_MSG);
1148 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1151 while (!pack_done) {
1154 err = got_poll_fd(infd, POLLIN, 1);
1156 if (err->code != GOT_ERR_TIMEOUT)
1160 r = read(infd, buf, sizeof(buf));
1162 err = got_error_from_errno("read");
1167 * Git clients hang up their side of the
1168 * connection after sending the pack file.
1177 /* Detect gotd(8) closing the pack pipe when done. */
1178 err = got_poll_fd(packfd, POLLOUT, 1);
1180 if (err->code != GOT_ERR_EOF)
1186 /* Write pack data and/or detect pipe being closed. */
1187 err = got_poll_write_full(packfd, buf, r);
1189 if (err->code == GOT_ERR_EOF)
1200 static const struct got_error *
1201 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1203 const struct got_error *err = NULL;
1204 struct gotd_imsg_packfile_status istatus;
1205 char buf[GOT_PKT_MAX];
1206 size_t datalen, len;
1207 char *reason = NULL;
1209 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1210 if (datalen < sizeof(istatus))
1211 return got_error(GOT_ERR_PRIVSEP_LEN);
1212 memcpy(&istatus, imsg->data, sizeof(istatus));
1213 if (datalen != sizeof(istatus) + istatus.reason_len)
1214 return got_error(GOT_ERR_PRIVSEP_LEN);
1216 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1217 if (reason == NULL) {
1218 err = got_error_from_errno("strndup");
1223 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1225 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1226 if (len >= sizeof(buf)) {
1227 err = got_error(GOT_ERR_NO_SPACE);
1231 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1237 static const struct got_error *
1238 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1240 const struct got_error *err = NULL;
1241 struct gotd_imsg_ref_update_ok iok;
1242 size_t datalen, len;
1243 char buf[GOT_PKT_MAX];
1244 char *refname = NULL;
1246 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1247 if (datalen < sizeof(iok))
1248 return got_error(GOT_ERR_PRIVSEP_LEN);
1249 memcpy(&iok, imsg->data, sizeof(iok));
1250 if (datalen != sizeof(iok) + iok.name_len)
1251 return got_error(GOT_ERR_PRIVSEP_LEN);
1253 memcpy(&iok, imsg->data, sizeof(iok));
1255 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1256 if (refname == NULL)
1257 return got_error_from_errno("strndup");
1259 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1260 if (len >= sizeof(buf)) {
1261 err = got_error(GOT_ERR_NO_SPACE);
1265 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1271 static const struct got_error *
1272 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1274 const struct got_error *err = NULL;
1275 struct gotd_imsg_ref_update_ng ing;
1276 size_t datalen, len;
1277 char buf[GOT_PKT_MAX];
1278 char *refname = NULL, *reason = NULL;
1280 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1281 if (datalen < sizeof(ing))
1282 return got_error(GOT_ERR_PRIVSEP_LEN);
1283 memcpy(&ing, imsg->data, sizeof(ing));
1284 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1285 return got_error(GOT_ERR_PRIVSEP_LEN);
1287 memcpy(&ing, imsg->data, sizeof(ing));
1289 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1290 if (refname == NULL)
1291 return got_error_from_errno("strndup");
1293 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1295 if (reason == NULL) {
1296 err = got_error_from_errno("strndup");
1300 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1301 if (len >= sizeof(buf)) {
1302 err = got_error(GOT_ERR_NO_SPACE);
1306 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1313 static const struct got_error *
1314 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1317 const struct got_error *err = NULL;
1318 char buf[GOT_PKT_MAX];
1319 struct imsgbuf ibuf;
1321 STATE_EXPECT_REF_UPDATE,
1322 STATE_EXPECT_MORE_REF_UPDATES,
1323 STATE_EXPECT_PACKFILE,
1324 STATE_PACKFILE_RECEIVED,
1327 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1329 int report_status = 0;
1331 imsg_init(&ibuf, gotd_sock);
1332 memset(&imsg, 0, sizeof(imsg));
1334 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1338 while (curstate != STATE_EXPECT_PACKFILE) {
1341 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1345 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1346 err = got_error_msg(GOT_ERR_BAD_PACKET,
1347 "unexpected flush packet received");
1350 err = forward_flushpkt(&ibuf);
1353 curstate = STATE_EXPECT_PACKFILE;
1354 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1355 if (curstate != STATE_EXPECT_REF_UPDATE &&
1356 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1357 err = got_error_msg(GOT_ERR_BAD_PACKET,
1358 "unexpected ref-update packet");
1361 if (curstate == STATE_EXPECT_REF_UPDATE) {
1362 err = recv_ref_update(&report_status,
1363 outfd, &ibuf, buf, n, 1, chattygot);
1365 err = recv_ref_update(NULL, outfd, &ibuf,
1366 buf, n, 0, chattygot);
1370 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1372 err = got_error(GOT_ERR_BAD_PACKET);
1377 while (curstate != STATE_PACKFILE_RECEIVED) {
1378 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1381 switch (imsg.hdr.type) {
1382 case GOTD_IMSG_ERROR:
1383 err = gotd_imsg_recv_error(NULL, &imsg);
1385 case GOTD_IMSG_RECV_PACKFILE:
1386 err = recv_packfile(&imsg, infd);
1388 if (err->code != GOT_ERR_EOF)
1391 * EOF is reported when the client hangs up,
1392 * which can happen with Git clients.
1393 * The socket should stay half-open so we
1394 * can still send our reports if requested.
1398 curstate = STATE_PACKFILE_RECEIVED;
1401 err = got_error(GOT_ERR_PRIVSEP_MSG);
1410 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1411 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1414 switch (imsg.hdr.type) {
1415 case GOTD_IMSG_ERROR:
1416 err = gotd_imsg_recv_error(NULL, &imsg);
1418 case GOTD_IMSG_PACKFILE_STATUS:
1421 err = report_unpack_status(&imsg, outfd, chattygot);
1423 case GOTD_IMSG_REF_UPDATE_OK:
1426 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1428 case GOTD_IMSG_REF_UPDATE_NG:
1431 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1433 case GOTD_IMSG_REFS_UPDATED:
1434 curstate = STATE_REFS_UPDATED;
1435 err = got_pkt_flushpkt(outfd, chattygot);
1438 err = got_error(GOT_ERR_PRIVSEP_MSG);
1447 echo_error(err, outfd, chattygot);
1451 const struct got_error *
1452 got_serve(int infd, int outfd, const char *gitcmd, int gotd_sock, int chattygot)
1454 const struct got_error *err = NULL;
1455 char *command = NULL, *repo_path = NULL;
1457 err = parse_command(&command, &repo_path, gitcmd);
1461 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1462 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1463 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1464 err = serve_write(infd, outfd, gotd_sock, repo_path, chattygot);
1466 err = got_error(GOT_ERR_BAD_PACKET);