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 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 err = got_gitproto_append_capabilities(&capalen, buf, len,
241 sizeof(buf), read_capabilities, nitems(read_capabilities));
245 return got_pkt_writepkt(outfd, buf, len, chattygot);
249 echo_error(const struct got_error *err, int outfd, int chattygot)
251 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
255 * Echo the error to the client on a pkt-line.
256 * The client should then terminate its session.
258 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
259 len = strlcat(buf, err->msg, sizeof(buf));
260 err = got_pkt_writepkt(outfd, buf, len, chattygot);
264 static const struct got_error *
265 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
266 const char *repo_path, int chattygot)
268 const struct got_error *err = NULL;
271 struct gotd_imsg_list_refs lsref;
272 struct gotd_imsg_reflist ireflist;
273 struct gotd_imsg_ref iref;
274 struct gotd_imsg_symref isymref;
276 int have_nrefs = 0, sent_capabilities = 0;
277 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
278 char *refname = NULL;
280 memset(&imsg, 0, sizeof(imsg));
281 memset(&lsref, 0, sizeof(lsref));
283 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
284 sizeof(lsref.repo_name))
285 return got_error(GOT_ERR_NO_SPACE);
286 lsref.client_is_reading = client_is_reading;
288 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
289 &lsref, sizeof(lsref)) == -1)
290 return got_error_from_errno("imsg_compose LIST_REFS");
292 err = gotd_imsg_flush(ibuf);
296 while (!have_nrefs || nrefs > 0) {
297 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
300 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
301 switch (imsg.hdr.type) {
302 case GOTD_IMSG_ERROR:
303 err = gotd_imsg_recv_error(NULL, &imsg);
305 case GOTD_IMSG_REFLIST:
306 if (have_nrefs || nrefs > 0) {
307 err = got_error(GOT_ERR_PRIVSEP_MSG);
310 if (datalen != sizeof(ireflist)) {
311 err = got_error(GOT_ERR_PRIVSEP_MSG);
314 memcpy(&ireflist, imsg.data, sizeof(ireflist));
315 nrefs = ireflist.nrefs;
318 err = send_zero_refs(outfd, chattygot);
321 if (!have_nrefs || nrefs == 0) {
322 err = got_error(GOT_ERR_PRIVSEP_MSG);
325 if (datalen < sizeof(iref)) {
326 err = got_error(GOT_ERR_PRIVSEP_MSG);
329 memcpy(&iref, imsg.data, sizeof(iref));
330 if (datalen != sizeof(iref) + iref.name_len) {
331 err = got_error(GOT_ERR_PRIVSEP_LEN);
334 refname = malloc(iref.name_len + 1);
335 if (refname == NULL) {
336 err = got_error_from_errno("malloc");
339 memcpy(refname, imsg.data + sizeof(iref),
341 refname[iref.name_len] = '\0';
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 = malloc(isymref.name_len + 1);
378 if (symrefname == NULL) {
379 err = got_error_from_errno("malloc");
382 memcpy(symrefname, imsg.data + sizeof(isymref),
384 symrefname[isymref.name_len] = '\0';
386 symreftarget = malloc(isymref.target_len + 1);
387 if (symreftarget == NULL) {
388 err = got_error_from_errno("malloc");
392 imsg.data + sizeof(isymref) + isymref.name_len,
394 symreftarget[isymref.target_len] = '\0';
396 if (asprintf(&symrefstr, "%s:%s", symrefname,
397 symreftarget) == -1) {
398 err = got_error_from_errno("asprintf");
401 err = send_ref(outfd, isymref.target_id, symrefname,
402 !sent_capabilities, client_is_reading, symrefstr,
408 sent_capabilities = 1;
413 err = got_error(GOT_ERR_PRIVSEP_MSG);
420 err = got_pkt_flushpkt(outfd, chattygot);
430 static const struct got_error *
431 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
433 const struct got_error *err;
434 char *id_str = NULL, *client_capabilities = NULL;
436 err = got_gitproto_parse_want_line(&id_str,
437 &client_capabilities, buf, len);
441 if (!got_parse_sha1_digest(id, id_str)) {
442 err = got_error_msg(GOT_ERR_BAD_PACKET,
443 "want-line with bad object ID");
447 if (client_capabilities) {
448 err = got_gitproto_match_capabilities(common_capabilities,
449 NULL, client_capabilities, read_capabilities,
450 nitems(read_capabilities));
456 free(client_capabilities);
460 static const struct got_error *
461 parse_have_line(uint8_t *id, char *buf, size_t len)
463 const struct got_error *err;
466 err = got_gitproto_parse_have_line(&id_str, buf, len);
470 if (!got_parse_sha1_digest(id, id_str)) {
471 err = got_error_msg(GOT_ERR_BAD_PACKET,
472 "have-line with bad object ID");
480 static const struct got_error *
481 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
483 const struct got_error *err = NULL;
484 struct gotd_imsg_capability icapa;
488 memset(&icapa, 0, sizeof(icapa));
490 icapa.key_len = strlen(capa->key);
491 len = sizeof(icapa) + icapa.key_len;
493 icapa.value_len = strlen(capa->value);
494 len += icapa.value_len;
497 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
499 err = got_error_from_errno("imsg_create CAPABILITY");
503 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
504 return got_error_from_errno("imsg_add CAPABILITY");
505 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
506 return got_error_from_errno("imsg_add CAPABILITY");
508 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
509 return got_error_from_errno("imsg_add CAPABILITY");
513 imsg_close(ibuf, wbuf);
518 static const struct got_error *
519 send_capabilities(int *use_sidebands, int *report_status,
520 char *capabilities_str, struct imsgbuf *ibuf)
522 const struct got_error *err = NULL;
523 struct gotd_imsg_capabilities icapas;
524 struct got_capability *capa = NULL;
527 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
532 icapas.ncapabilities = ncapa;
533 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
534 &icapas, sizeof(icapas)) == -1) {
535 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
539 for (i = 0; i < ncapa; i++) {
540 err = send_capability(&capa[i], ibuf);
544 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
547 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
555 static const struct got_error *
556 forward_flushpkt(struct imsgbuf *ibuf)
558 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
559 return got_error_from_errno("imsg_compose FLUSH");
561 return gotd_imsg_flush(ibuf);
564 static const struct got_error *
565 recv_ack(struct imsg *imsg, uint8_t *expected_id)
567 struct gotd_imsg_ack iack;
570 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
571 if (datalen != sizeof(iack))
572 return got_error(GOT_ERR_PRIVSEP_LEN);
574 memcpy(&iack, imsg->data, sizeof(iack));
575 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
576 return got_error(GOT_ERR_BAD_OBJ_ID);
581 static const struct got_error *
582 recv_nak(struct imsg *imsg, uint8_t *expected_id)
584 struct gotd_imsg_ack inak;
587 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
588 if (datalen != sizeof(inak))
589 return got_error(GOT_ERR_PRIVSEP_LEN);
591 memcpy(&inak, imsg->data, sizeof(inak));
592 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
593 return got_error(GOT_ERR_BAD_OBJ_ID);
599 static const struct got_error *
600 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
601 char *buf, size_t len, int expect_capabilities, int chattygot)
603 const struct got_error *err;
604 struct gotd_imsg_want iwant;
605 char *capabilities_str;
609 memset(&iwant, 0, sizeof(iwant));
610 memset(&imsg, 0, sizeof(imsg));
612 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
616 if (capabilities_str) {
617 if (!expect_capabilities) {
618 err = got_error_msg(GOT_ERR_BAD_PACKET,
619 "unexpected capability announcement received");
622 err = send_capabilities(use_sidebands, NULL, capabilities_str,
629 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
630 &iwant, sizeof(iwant)) == -1) {
631 err = got_error_from_errno("imsg_compose WANT");
635 err = gotd_imsg_flush(ibuf);
640 * Wait for an ACK, or an error in case the desired object
643 while (!done && err == NULL) {
644 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
647 switch (imsg.hdr.type) {
648 case GOTD_IMSG_ERROR:
649 err = gotd_imsg_recv_error(NULL, &imsg);
652 err = recv_ack(&imsg, iwant.object_id);
658 err = got_error(GOT_ERR_PRIVSEP_MSG);
665 free(capabilities_str);
669 static const struct got_error *
670 send_ack(int outfd, uint8_t *id, int chattygot)
672 char hex[SHA1_DIGEST_STRING_LENGTH];
673 char buf[GOT_PKT_MAX];
676 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
677 return got_error(GOT_ERR_BAD_OBJ_ID);
679 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
680 if (len >= sizeof(buf))
681 return got_error(GOT_ERR_NO_SPACE);
683 return got_pkt_writepkt(outfd, buf, len, chattygot);
686 static const struct got_error *
687 send_nak(int outfd, int chattygot)
692 len = snprintf(buf, sizeof(buf), "NAK\n");
693 if (len >= sizeof(buf))
694 return got_error(GOT_ERR_NO_SPACE);
696 return got_pkt_writepkt(outfd, buf, len, chattygot);
699 static const struct got_error *
700 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
701 size_t len, int chattygot)
703 const struct got_error *err;
704 struct gotd_imsg_have ihave;
708 memset(&ihave, 0, sizeof(ihave));
709 memset(&imsg, 0, sizeof(imsg));
711 err = parse_have_line(ihave.object_id, buf, len);
715 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
716 &ihave, sizeof(ihave)) == -1)
717 return got_error_from_errno("imsg_compose HAVE");
719 err = gotd_imsg_flush(ibuf);
724 * Wait for an ACK or a NAK, indicating whether a common
725 * commit object has been found.
727 while (!done && err == NULL) {
728 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
731 switch (imsg.hdr.type) {
732 case GOTD_IMSG_ERROR:
733 err = gotd_imsg_recv_error(NULL, &imsg);
736 err = recv_ack(&imsg, ihave.object_id);
740 err = send_ack(outfd, ihave.object_id,
749 err = recv_nak(&imsg, ihave.object_id);
755 err = got_error(GOT_ERR_PRIVSEP_MSG);
765 static const struct got_error *
766 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
768 const struct got_error *err;
773 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
774 return got_error_from_errno("imsg_compose DONE");
776 err = gotd_imsg_flush(ibuf);
780 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
784 if (imsg.hdr.type == GOTD_IMSG_ERROR)
785 err = gotd_imsg_recv_error(NULL, &imsg);
786 else if (imsg.hdr.type != GOTD_IMSG_PACKFILE_PIPE)
787 err = got_error(GOT_ERR_PRIVSEP_MSG);
788 else if (imsg.fd == -1)
789 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
797 static const struct got_error *
798 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
800 const struct got_error *err = NULL;
801 int pack_starting = 0;
802 struct gotd_imsg_packfile_progress iprog;
803 char buf[GOT_PKT_MAX];
806 int p_deltify = 0, n;
807 const char *eol = "\r";
809 memset(&imsg, 0, sizeof(imsg));
811 while (!pack_starting && err == NULL) {
812 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
816 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
817 switch (imsg.hdr.type) {
818 case GOTD_IMSG_ERROR:
819 err = gotd_imsg_recv_error(NULL, &imsg);
821 case GOTD_IMSG_PACKFILE_READY:
825 case GOTD_IMSG_PACKFILE_PROGRESS:
826 if (datalen != sizeof(iprog)) {
827 err = got_error(GOT_ERR_PRIVSEP_LEN);
830 memcpy(&iprog, imsg.data, sizeof(iprog));
831 if (iprog.nobj_total > 0) {
832 p_deltify = (iprog.nobj_deltify * 100) /
835 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
836 n = snprintf(&buf[1], sizeof(buf) - 1,
837 "%d commits colored, "
843 if (n >= sizeof(buf) - 1)
845 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
848 err = got_error(GOT_ERR_PRIVSEP_MSG);
858 static const struct got_error *
859 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
862 const struct got_error *err = NULL;
863 char buf[GOT_PKT_MAX];
867 STATE_EXPECT_MORE_WANT,
872 enum protostate curstate = STATE_EXPECT_WANT;
873 int have_ack = 0, use_sidebands = 0, seen_have = 0;
875 size_t pack_chunksize;
877 imsg_init(&ibuf, gotd_sock);
879 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
883 while (curstate != STATE_DONE) {
886 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
890 if (curstate != STATE_EXPECT_MORE_WANT &&
891 curstate != STATE_EXPECT_HAVE) {
892 err = got_error_msg(GOT_ERR_BAD_PACKET,
893 "unexpected flush packet received");
896 err = forward_flushpkt(&ibuf);
899 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
900 err = send_nak(outfd, chattygot);
904 if (curstate == STATE_EXPECT_MORE_WANT)
905 curstate = STATE_EXPECT_HAVE;
907 curstate = STATE_EXPECT_DONE;
908 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
909 if (curstate != STATE_EXPECT_WANT &&
910 curstate != STATE_EXPECT_MORE_WANT) {
911 err = got_error_msg(GOT_ERR_BAD_PACKET,
912 "unexpected 'want' packet");
915 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
916 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
919 if (curstate == STATE_EXPECT_WANT)
920 curstate = STATE_EXPECT_MORE_WANT;
921 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
922 if (curstate != STATE_EXPECT_HAVE) {
923 err = got_error_msg(GOT_ERR_BAD_PACKET,
924 "unexpected 'have' packet");
927 err = recv_have(&have_ack, outfd, &ibuf, buf, n,
932 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
933 if (curstate != STATE_EXPECT_HAVE &&
934 curstate != STATE_EXPECT_DONE) {
935 err = got_error_msg(GOT_ERR_BAD_PACKET,
936 "unexpected 'done' packet");
939 err = recv_done(&packfd, outfd, &ibuf, chattygot);
942 curstate = STATE_DONE;
945 err = got_error(GOT_ERR_BAD_PACKET);
951 err = send_nak(outfd, chattygot);
957 err = relay_progress_reports(&ibuf, outfd, chattygot);
960 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
962 pack_chunksize = sizeof(buf);
967 r = read(packfd, use_sidebands ? &buf[1] : buf,
970 err = got_error_from_errno("read");
973 err = got_pkt_flushpkt(outfd, chattygot);
978 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
979 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
983 w = write(outfd, buf, r);
985 err = got_error_from_errno("write");
988 err = got_error(GOT_ERR_IO);
995 if (packfd != -1 && close(packfd) == -1 && err == NULL)
996 err = got_error_from_errno("close");
998 echo_error(err, outfd, chattygot);
1002 static const struct got_error *
1003 parse_ref_update_line(char **common_capabilities, char **refname,
1004 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1006 const struct got_error *err;
1007 char *old_id_str = NULL, *new_id_str = NULL;
1008 char *client_capabilities = NULL;
1012 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1013 refname, &client_capabilities, buf, len);
1017 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1018 !got_parse_sha1_digest(new_id, new_id_str)) {
1019 err = got_error_msg(GOT_ERR_BAD_PACKET,
1020 "ref-update with bad object ID");
1023 if (!got_ref_name_is_valid(*refname)) {
1024 err = got_error_msg(GOT_ERR_BAD_PACKET,
1025 "ref-update with bad reference name");
1029 if (client_capabilities) {
1030 err = got_gitproto_match_capabilities(common_capabilities,
1031 NULL, client_capabilities, write_capabilities,
1032 nitems(write_capabilities));
1039 free(client_capabilities);
1047 static const struct got_error *
1048 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1049 char *buf, size_t len, int expect_capabilities, int chattygot)
1051 const struct got_error *err;
1052 struct gotd_imsg_ref_update iref;
1054 char *capabilities_str = NULL, *refname = NULL;
1058 memset(&iref, 0, sizeof(iref));
1059 memset(&imsg, 0, sizeof(imsg));
1061 err = parse_ref_update_line(&capabilities_str, &refname,
1062 iref.old_id, iref.new_id, buf, len);
1066 if (capabilities_str) {
1067 if (!expect_capabilities) {
1068 err = got_error_msg(GOT_ERR_BAD_PACKET,
1069 "unexpected capability announcement received");
1072 err = send_capabilities(NULL, report_status, capabilities_str,
1078 iref.name_len = strlen(refname);
1079 len = sizeof(iref) + iref.name_len;
1080 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1082 err = got_error_from_errno("imsg_create REF_UPDATE");
1086 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1087 return got_error_from_errno("imsg_add REF_UPDATE");
1088 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1089 return got_error_from_errno("imsg_add REF_UPDATE");
1091 imsg_close(ibuf, wbuf);
1093 err = gotd_imsg_flush(ibuf);
1097 /* Wait for ACK or an error. */
1098 while (!done && err == NULL) {
1099 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1102 switch (imsg.hdr.type) {
1103 case GOTD_IMSG_ERROR:
1104 err = gotd_imsg_recv_error(NULL, &imsg);
1107 err = recv_ack(&imsg, iref.new_id);
1113 err = got_error(GOT_ERR_PRIVSEP_MSG);
1120 free(capabilities_str);
1125 static const struct got_error *
1126 recv_packfile(struct imsg *imsg, int infd)
1128 const struct got_error *err = NULL;
1131 char buf[GOT_PKT_MAX];
1134 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1136 return got_error(GOT_ERR_PRIVSEP_MSG);
1139 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1142 while (!pack_done) {
1145 err = got_poll_fd(infd, POLLIN, 1);
1147 if (err->code != GOT_ERR_TIMEOUT)
1151 r = read(infd, buf, sizeof(buf));
1153 err = got_error_from_errno("read");
1158 * Git clients hang up their side of the
1159 * connection after sending the pack file.
1168 /* Detect gotd(8) closing the pack pipe when done. */
1169 err = got_poll_fd(packfd, POLLOUT, 1);
1171 if (err->code != GOT_ERR_EOF)
1177 /* Write pack data and/or detect pipe being closed. */
1178 err = got_poll_write_full(packfd, buf, r);
1180 if (err->code == GOT_ERR_EOF)
1191 static const struct got_error *
1192 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1194 const struct got_error *err = NULL;
1195 struct gotd_imsg_packfile_status istatus;
1196 char buf[GOT_PKT_MAX];
1197 size_t datalen, len;
1198 char *reason = NULL;
1200 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1201 if (datalen < sizeof(istatus))
1202 return got_error(GOT_ERR_PRIVSEP_LEN);
1203 memcpy(&istatus, imsg->data, sizeof(istatus));
1204 if (datalen != sizeof(istatus) + istatus.reason_len)
1205 return got_error(GOT_ERR_PRIVSEP_LEN);
1207 reason = malloc(istatus.reason_len + 1);
1208 if (reason == NULL) {
1209 err = got_error_from_errno("malloc");
1212 memcpy(reason, imsg->data + sizeof(istatus), istatus.reason_len);
1213 reason[istatus.reason_len] = '\0';
1216 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1218 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1219 if (len >= sizeof(buf)) {
1220 err = got_error(GOT_ERR_NO_SPACE);
1224 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1230 static const struct got_error *
1231 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1233 const struct got_error *err = NULL;
1234 struct gotd_imsg_ref_update_ok iok;
1235 size_t datalen, len;
1236 char buf[GOT_PKT_MAX];
1237 char *refname = NULL;
1239 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1240 if (datalen < sizeof(iok))
1241 return got_error(GOT_ERR_PRIVSEP_LEN);
1242 memcpy(&iok, imsg->data, sizeof(iok));
1243 if (datalen != sizeof(iok) + iok.name_len)
1244 return got_error(GOT_ERR_PRIVSEP_LEN);
1246 memcpy(&iok, imsg->data, sizeof(iok));
1248 refname = malloc(iok.name_len + 1);
1249 if (refname == NULL)
1250 return got_error_from_errno("malloc");
1251 memcpy(refname, imsg->data + sizeof(iok), iok.name_len);
1252 refname[iok.name_len] = '\0';
1254 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1255 if (len >= sizeof(buf)) {
1256 err = got_error(GOT_ERR_NO_SPACE);
1260 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1266 static const struct got_error *
1267 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1269 const struct got_error *err = NULL;
1270 struct gotd_imsg_ref_update_ng ing;
1271 size_t datalen, len;
1272 char buf[GOT_PKT_MAX];
1273 char *refname = NULL, *reason = NULL;
1275 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1276 if (datalen < sizeof(ing))
1277 return got_error(GOT_ERR_PRIVSEP_LEN);
1278 memcpy(&ing, imsg->data, sizeof(ing));
1279 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1280 return got_error(GOT_ERR_PRIVSEP_LEN);
1282 memcpy(&ing, imsg->data, sizeof(ing));
1284 refname = malloc(ing.name_len + 1);
1285 if (refname == NULL)
1286 return got_error_from_errno("malloc");
1287 memcpy(refname, imsg->data + sizeof(ing), ing.name_len);
1288 refname[ing.name_len] = '\0';
1290 reason = malloc(ing.reason_len + 1);
1291 if (reason == NULL) {
1292 err = got_error_from_errno("malloc");
1295 memcpy(refname, imsg->data + sizeof(ing) + ing.name_len,
1297 refname[ing.reason_len] = '\0';
1299 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1300 if (len >= sizeof(buf)) {
1301 err = got_error(GOT_ERR_NO_SPACE);
1305 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1312 static const struct got_error *
1313 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1316 const struct got_error *err = NULL;
1317 char buf[GOT_PKT_MAX];
1318 struct imsgbuf ibuf;
1320 STATE_EXPECT_REF_UPDATE,
1321 STATE_EXPECT_MORE_REF_UPDATES,
1322 STATE_EXPECT_PACKFILE,
1323 STATE_PACKFILE_RECEIVED,
1326 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1328 int report_status = 0;
1330 imsg_init(&ibuf, gotd_sock);
1331 memset(&imsg, 0, sizeof(imsg));
1333 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1337 while (curstate != STATE_EXPECT_PACKFILE) {
1340 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1344 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1345 err = got_error_msg(GOT_ERR_BAD_PACKET,
1346 "unexpected flush packet received");
1349 err = forward_flushpkt(&ibuf);
1352 curstate = STATE_EXPECT_PACKFILE;
1353 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1354 if (curstate != STATE_EXPECT_REF_UPDATE &&
1355 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1356 err = got_error_msg(GOT_ERR_BAD_PACKET,
1357 "unexpected ref-update packet");
1360 if (curstate == STATE_EXPECT_REF_UPDATE) {
1361 err = recv_ref_update(&report_status,
1362 outfd, &ibuf, buf, n, 1, chattygot);
1364 err = recv_ref_update(NULL, outfd, &ibuf,
1365 buf, n, 0, chattygot);
1369 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1371 err = got_error(GOT_ERR_BAD_PACKET);
1376 while (curstate != STATE_PACKFILE_RECEIVED) {
1377 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1380 switch (imsg.hdr.type) {
1381 case GOTD_IMSG_ERROR:
1382 err = gotd_imsg_recv_error(NULL, &imsg);
1384 case GOTD_IMSG_RECV_PACKFILE:
1385 err = recv_packfile(&imsg, infd);
1387 if (err->code != GOT_ERR_EOF)
1390 * EOF is reported when the client hangs up,
1391 * which can happen with Git clients.
1392 * The socket should stay half-open so we
1393 * can still send our reports if requested.
1397 curstate = STATE_PACKFILE_RECEIVED;
1400 err = got_error(GOT_ERR_PRIVSEP_MSG);
1409 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1410 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1413 switch (imsg.hdr.type) {
1414 case GOTD_IMSG_ERROR:
1415 err = gotd_imsg_recv_error(NULL, &imsg);
1417 case GOTD_IMSG_PACKFILE_STATUS:
1420 err = report_unpack_status(&imsg, outfd, chattygot);
1422 case GOTD_IMSG_REF_UPDATE_OK:
1425 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1427 case GOTD_IMSG_REF_UPDATE_NG:
1430 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1432 case GOTD_IMSG_REFS_UPDATED:
1433 curstate = STATE_REFS_UPDATED;
1434 err = got_pkt_flushpkt(outfd, chattygot);
1437 err = got_error(GOT_ERR_PRIVSEP_MSG);
1446 echo_error(err, outfd, chattygot);
1450 const struct got_error *
1451 got_serve(int infd, int outfd, const char *gitcmd, int gotd_sock, int chattygot)
1453 const struct got_error *err = NULL;
1454 char *command = NULL, *repo_path = NULL;
1456 err = parse_command(&command, &repo_path, gitcmd);
1460 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1461 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1462 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1463 err = serve_write(infd, outfd, gotd_sock, repo_path, chattygot);
1465 err = got_error(GOT_ERR_BAD_PACKET);