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>
32 #include "got_error.h"
33 #include "got_serve.h"
35 #include "got_version.h"
36 #include "got_reference.h"
37 #include "got_object.h"
39 #include "got_lib_pkt.h"
40 #include "got_lib_dial.h"
41 #include "got_lib_gitproto.h"
42 #include "got_lib_hash.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 },
62 { GOT_CAPA_DELETE_REFS, NULL },
65 const struct got_error *
66 got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
68 const struct got_error *err = NULL;
69 size_t len, cmdlen, pathlen;
70 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
78 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
79 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
80 strlen(GOT_SERVE_CMD_SEND)) == 0)
81 cmdlen = strlen(GOT_SERVE_CMD_SEND);
82 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
83 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
84 strlen(GOT_SERVE_CMD_FETCH)) == 0)
85 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
87 return got_error(GOT_ERR_BAD_PACKET);
89 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
90 return got_error(GOT_ERR_BAD_PACKET);
92 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
93 return got_error(GOT_ERR_BAD_PATH);
95 /* Forbid linefeeds in paths, like Git does. */
96 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
97 return got_error(GOT_ERR_BAD_PATH);
99 path0 = strdup(&gitcmd[cmdlen + 1]);
101 return got_error_from_errno("strdup");
103 pathlen = strlen(path);
106 * Git clients send a shell command.
107 * Trim spaces and quotes around the path.
109 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
113 while (pathlen > 0 &&
114 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
115 path[pathlen - 1] == ' ')) {
116 path[pathlen - 1] = '\0';
120 /* Deny an empty repository path. */
121 if (path[0] == '\0' || got_path_is_root_dir(path)) {
122 err = got_error(GOT_ERR_NOT_GIT_REPO);
126 if (asprintf(&abspath, "/%s", path) == -1) {
127 err = got_error_from_errno("asprintf");
130 pathlen = strlen(abspath);
131 canonpath = malloc(pathlen + 1);
132 if (canonpath == NULL) {
133 err = got_error_from_errno("malloc");
136 err = got_canonpath(abspath, canonpath, pathlen + 1);
141 while (relpath[0] == '/')
143 *repo_path = strdup(relpath);
144 if (*repo_path == NULL) {
145 err = got_error_from_errno("strdup");
148 *command = strndup(gitcmd, cmdlen);
149 if (*command == NULL)
150 err = got_error_from_errno("strndup");
162 static const struct got_error *
163 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
164 uint8_t *buf, size_t bufsize)
166 struct got_capability capa[nitems(read_capabilities) + 1];
169 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
171 capa[nitems(read_capabilities)].key = "symref";
172 capa[nitems(read_capabilities)].value = symrefstr;
173 ncapa = nitems(capa);
175 ncapa = nitems(read_capabilities);
177 return got_gitproto_append_capabilities(capalen, buf, len,
178 bufsize, capa, ncapa);
181 static const struct got_error *
182 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
183 int client_is_reading, const char *symrefstr, int chattygot)
185 const struct got_error *err = NULL;
186 char hex[SHA1_DIGEST_STRING_LENGTH];
187 char buf[GOT_PKT_MAX];
188 size_t len, capalen = 0;
190 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
191 return got_error(GOT_ERR_BAD_OBJ_ID);
193 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
194 if (len >= sizeof(buf))
195 return got_error(GOT_ERR_NO_SPACE);
197 if (send_capabilities) {
198 if (client_is_reading) {
199 err = append_read_capabilities(&capalen, len,
200 symrefstr, buf, sizeof(buf));
202 err = got_gitproto_append_capabilities(&capalen,
203 buf, len, sizeof(buf), write_capabilities,
204 nitems(write_capabilities));
211 if (len + 1 >= sizeof(buf))
212 return got_error(GOT_ERR_NO_SPACE);
217 return got_pkt_writepkt(outfd, buf, len, chattygot);
220 static const struct got_error *
221 send_zero_refs(int outfd, int client_is_reading, int chattygot)
223 const struct got_error *err = NULL;
224 const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
225 char buf[GOT_PKT_MAX];
226 size_t len, capalen = 0;
228 len = strlcpy(buf, line, sizeof(buf));
229 if (len >= sizeof(buf))
230 return got_error(GOT_ERR_NO_SPACE);
232 if (client_is_reading) {
233 err = got_gitproto_append_capabilities(&capalen, buf, len,
234 sizeof(buf), read_capabilities, nitems(read_capabilities));
238 err = got_gitproto_append_capabilities(&capalen, buf, len,
239 sizeof(buf), write_capabilities,
240 nitems(write_capabilities));
245 return got_pkt_writepkt(outfd, buf, len + capalen, 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 got_pkt_writepkt(outfd, buf, len, chattygot);
263 static const struct got_error *
264 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
265 const char *repo_path, int chattygot)
267 const struct got_error *err = NULL;
270 struct gotd_imsg_list_refs lsref;
271 struct gotd_imsg_reflist ireflist;
272 struct gotd_imsg_ref iref;
273 struct gotd_imsg_symref isymref;
275 int have_nrefs = 0, sent_capabilities = 0;
276 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
277 char *refname = NULL;
279 memset(&imsg, 0, sizeof(imsg));
280 memset(&lsref, 0, sizeof(lsref));
282 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
283 sizeof(lsref.repo_name))
284 return got_error(GOT_ERR_NO_SPACE);
285 lsref.client_is_reading = client_is_reading;
287 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
288 &lsref, sizeof(lsref)) == -1)
289 return got_error_from_errno("imsg_compose LIST_REFS");
291 err = gotd_imsg_flush(ibuf);
295 while (!have_nrefs || nrefs > 0) {
296 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
299 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
300 switch (imsg.hdr.type) {
301 case GOTD_IMSG_ERROR:
302 err = gotd_imsg_recv_error(NULL, &imsg);
304 case GOTD_IMSG_REFLIST:
305 if (have_nrefs || nrefs > 0) {
306 err = got_error(GOT_ERR_PRIVSEP_MSG);
309 if (datalen != sizeof(ireflist)) {
310 err = got_error(GOT_ERR_PRIVSEP_MSG);
313 memcpy(&ireflist, imsg.data, sizeof(ireflist));
314 nrefs = ireflist.nrefs;
317 err = send_zero_refs(outfd, client_is_reading,
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 = strndup(imsg.data + sizeof(iref),
336 if (refname == NULL) {
337 err = got_error_from_errno("strndup");
340 err = send_ref(outfd, iref.id, refname,
341 !sent_capabilities, client_is_reading,
347 sent_capabilities = 1;
351 case GOTD_IMSG_SYMREF:
352 if (!have_nrefs || nrefs == 0) {
353 err = got_error(GOT_ERR_PRIVSEP_MSG);
356 if (datalen < sizeof(isymref)) {
357 err = got_error(GOT_ERR_PRIVSEP_LEN);
360 memcpy(&isymref, imsg.data, sizeof(isymref));
361 if (datalen != sizeof(isymref) + isymref.name_len +
362 isymref.target_len) {
363 err = got_error(GOT_ERR_PRIVSEP_LEN);
368 * For now, we only announce one symbolic ref,
369 * as part of our capability advertisement.
371 if (sent_capabilities || symrefstr != NULL ||
372 symrefname != NULL || symreftarget != NULL)
375 symrefname = strndup(imsg.data + sizeof(isymref),
377 if (symrefname == NULL) {
378 err = got_error_from_errno("malloc");
382 symreftarget = strndup(
383 imsg.data + sizeof(isymref) + isymref.name_len,
385 if (symreftarget == NULL) {
386 err = got_error_from_errno("strndup");
390 if (asprintf(&symrefstr, "%s:%s", symrefname,
391 symreftarget) == -1) {
392 err = got_error_from_errno("asprintf");
395 err = send_ref(outfd, isymref.target_id, symrefname,
396 !sent_capabilities, client_is_reading, symrefstr,
402 sent_capabilities = 1;
407 err = got_error(GOT_ERR_PRIVSEP_MSG);
414 err = got_pkt_flushpkt(outfd, chattygot);
424 static const struct got_error *
425 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
427 const struct got_error *err;
428 char *id_str = NULL, *client_capabilities = NULL;
430 err = got_gitproto_parse_want_line(&id_str,
431 &client_capabilities, buf, len);
435 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
436 err = got_error_msg(GOT_ERR_BAD_PACKET,
437 "want-line with bad object ID");
441 if (client_capabilities) {
442 err = got_gitproto_match_capabilities(common_capabilities,
443 NULL, client_capabilities, read_capabilities,
444 nitems(read_capabilities));
450 free(client_capabilities);
454 static const struct got_error *
455 parse_have_line(uint8_t *id, char *buf, size_t len)
457 const struct got_error *err;
460 err = got_gitproto_parse_have_line(&id_str, buf, len);
464 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
465 err = got_error_msg(GOT_ERR_BAD_PACKET,
466 "have-line with bad object ID");
474 static const struct got_error *
475 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
477 const struct got_error *err = NULL;
478 struct gotd_imsg_capability icapa;
482 memset(&icapa, 0, sizeof(icapa));
484 icapa.key_len = strlen(capa->key);
485 len = sizeof(icapa) + icapa.key_len;
487 icapa.value_len = strlen(capa->value);
488 len += icapa.value_len;
491 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
493 err = got_error_from_errno("imsg_create CAPABILITY");
497 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
498 return got_error_from_errno("imsg_add CAPABILITY");
499 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
500 return got_error_from_errno("imsg_add CAPABILITY");
502 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
503 return got_error_from_errno("imsg_add CAPABILITY");
507 imsg_close(ibuf, wbuf);
512 static const struct got_error *
513 send_capabilities(int *use_sidebands, int *report_status,
514 char *capabilities_str, struct imsgbuf *ibuf)
516 const struct got_error *err = NULL;
517 struct gotd_imsg_capabilities icapas;
518 struct got_capability *capa = NULL;
521 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
526 icapas.ncapabilities = ncapa;
527 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
528 &icapas, sizeof(icapas)) == -1) {
529 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
533 for (i = 0; i < ncapa; i++) {
534 err = send_capability(&capa[i], ibuf);
538 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
541 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
549 static const struct got_error *
550 forward_flushpkt(struct imsgbuf *ibuf)
552 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
553 return got_error_from_errno("imsg_compose FLUSH");
555 return gotd_imsg_flush(ibuf);
558 static const struct got_error *
559 recv_ack(struct imsg *imsg, uint8_t *expected_id)
561 struct gotd_imsg_ack iack;
564 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
565 if (datalen != sizeof(iack))
566 return got_error(GOT_ERR_PRIVSEP_LEN);
568 memcpy(&iack, imsg->data, sizeof(iack));
569 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
570 return got_error(GOT_ERR_BAD_OBJ_ID);
575 static const struct got_error *
576 recv_nak(struct imsg *imsg, uint8_t *expected_id)
578 struct gotd_imsg_ack inak;
581 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
582 if (datalen != sizeof(inak))
583 return got_error(GOT_ERR_PRIVSEP_LEN);
585 memcpy(&inak, imsg->data, sizeof(inak));
586 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
587 return got_error(GOT_ERR_BAD_OBJ_ID);
593 static const struct got_error *
594 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
595 char *buf, size_t len, int expect_capabilities, int chattygot)
597 const struct got_error *err;
598 struct gotd_imsg_want iwant;
599 char *capabilities_str;
603 memset(&iwant, 0, sizeof(iwant));
604 memset(&imsg, 0, sizeof(imsg));
606 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
610 if (capabilities_str) {
611 if (!expect_capabilities) {
612 err = got_error_msg(GOT_ERR_BAD_PACKET,
613 "unexpected capability announcement received");
616 err = send_capabilities(use_sidebands, NULL, capabilities_str,
623 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
624 &iwant, sizeof(iwant)) == -1) {
625 err = got_error_from_errno("imsg_compose WANT");
629 err = gotd_imsg_flush(ibuf);
634 * Wait for an ACK, or an error in case the desired object
637 while (!done && err == NULL) {
638 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
641 switch (imsg.hdr.type) {
642 case GOTD_IMSG_ERROR:
643 err = gotd_imsg_recv_error(NULL, &imsg);
646 err = recv_ack(&imsg, iwant.object_id);
652 err = got_error(GOT_ERR_PRIVSEP_MSG);
659 free(capabilities_str);
663 static const struct got_error *
664 send_ack(int outfd, uint8_t *id, int chattygot)
666 char hex[SHA1_DIGEST_STRING_LENGTH];
667 char buf[GOT_PKT_MAX];
670 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
671 return got_error(GOT_ERR_BAD_OBJ_ID);
673 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
674 if (len >= sizeof(buf))
675 return got_error(GOT_ERR_NO_SPACE);
677 return got_pkt_writepkt(outfd, buf, len, chattygot);
680 static const struct got_error *
681 send_nak(int outfd, int chattygot)
686 len = snprintf(buf, sizeof(buf), "NAK\n");
687 if (len >= sizeof(buf))
688 return got_error(GOT_ERR_NO_SPACE);
690 return got_pkt_writepkt(outfd, buf, len, chattygot);
693 static const struct got_error *
694 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
695 size_t len, int chattygot)
697 const struct got_error *err;
698 struct gotd_imsg_have ihave;
702 memset(&ihave, 0, sizeof(ihave));
703 memset(&imsg, 0, sizeof(imsg));
705 err = parse_have_line(ihave.object_id, buf, len);
709 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
710 &ihave, sizeof(ihave)) == -1)
711 return got_error_from_errno("imsg_compose HAVE");
713 err = gotd_imsg_flush(ibuf);
718 * Wait for an ACK or a NAK, indicating whether a common
719 * commit object has been found.
721 while (!done && err == NULL) {
722 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
725 switch (imsg.hdr.type) {
726 case GOTD_IMSG_ERROR:
727 err = gotd_imsg_recv_error(NULL, &imsg);
730 err = recv_ack(&imsg, ihave.object_id);
734 err = send_ack(outfd, ihave.object_id,
743 err = recv_nak(&imsg, ihave.object_id);
749 err = got_error(GOT_ERR_PRIVSEP_MSG);
759 static const struct got_error *
760 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
762 const struct got_error *err;
767 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
768 return got_error_from_errno("imsg_compose DONE");
770 err = gotd_imsg_flush(ibuf);
774 while (*packfd == -1 && err == NULL) {
775 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
779 switch (imsg.hdr.type) {
780 case GOTD_IMSG_ERROR:
781 err = gotd_imsg_recv_error(NULL, &imsg);
783 case GOTD_IMSG_PACKFILE_PIPE:
787 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
790 err = got_error(GOT_ERR_PRIVSEP_MSG);
800 static const struct got_error *
801 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
803 const struct got_error *err = NULL;
804 int pack_starting = 0;
805 struct gotd_imsg_packfile_progress iprog;
806 char buf[GOT_PKT_MAX];
809 int p_deltify = 0, n;
810 const char *eol = "\r";
812 memset(&imsg, 0, sizeof(imsg));
814 while (!pack_starting && err == NULL) {
815 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
819 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
820 switch (imsg.hdr.type) {
821 case GOTD_IMSG_ERROR:
822 err = gotd_imsg_recv_error(NULL, &imsg);
824 case GOTD_IMSG_PACKFILE_READY:
828 case GOTD_IMSG_PACKFILE_PROGRESS:
829 if (datalen != sizeof(iprog)) {
830 err = got_error(GOT_ERR_PRIVSEP_LEN);
833 memcpy(&iprog, imsg.data, sizeof(iprog));
834 if (iprog.nobj_total > 0) {
835 p_deltify = (iprog.nobj_deltify * 100) /
838 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
839 n = snprintf(&buf[1], sizeof(buf) - 1,
840 "%d commits colored, "
846 if (n >= sizeof(buf) - 1)
848 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
851 err = got_error(GOT_ERR_PRIVSEP_MSG);
861 static const struct got_error *
862 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
865 const struct got_error *err = NULL;
866 char buf[GOT_PKT_MAX];
870 STATE_EXPECT_MORE_WANT,
875 enum protostate curstate = STATE_EXPECT_WANT;
876 int have_ack = 0, use_sidebands = 0, seen_have = 0;
878 size_t pack_chunksize;
880 imsg_init(&ibuf, gotd_sock);
882 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
886 while (curstate != STATE_DONE) {
889 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
893 if (curstate != STATE_EXPECT_WANT &&
894 curstate != STATE_EXPECT_MORE_WANT &&
895 curstate != STATE_EXPECT_HAVE &&
896 curstate != STATE_EXPECT_DONE) {
897 err = got_error_msg(GOT_ERR_BAD_PACKET,
898 "unexpected flush packet received");
902 if (curstate == STATE_EXPECT_WANT) {
905 * If the client does not want to fetch
906 * anything we should receive a flush
907 * packet followed by EOF.
909 r = read(infd, buf, sizeof(buf));
911 err = got_error_from_errno("read");
914 if (r == 0) /* EOF */
917 /* Zero-length field followed by payload. */
918 err = got_error_msg(GOT_ERR_BAD_PACKET,
919 "unexpected flush packet received");
923 if (curstate == STATE_EXPECT_WANT ||
924 curstate == STATE_EXPECT_MORE_WANT ||
925 curstate == STATE_EXPECT_HAVE) {
926 err = forward_flushpkt(&ibuf);
930 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
931 err = send_nak(outfd, chattygot);
935 if (curstate == STATE_EXPECT_MORE_WANT)
936 curstate = STATE_EXPECT_HAVE;
938 curstate = STATE_EXPECT_DONE;
939 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
940 if (curstate != STATE_EXPECT_WANT &&
941 curstate != STATE_EXPECT_MORE_WANT) {
942 err = got_error_msg(GOT_ERR_BAD_PACKET,
943 "unexpected 'want' packet");
946 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
947 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
950 if (curstate == STATE_EXPECT_WANT)
951 curstate = STATE_EXPECT_MORE_WANT;
952 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
953 if (curstate != STATE_EXPECT_HAVE &&
954 curstate != STATE_EXPECT_DONE) {
955 err = got_error_msg(GOT_ERR_BAD_PACKET,
956 "unexpected 'have' packet");
959 if (curstate == STATE_EXPECT_HAVE) {
960 err = recv_have(&have_ack, outfd, &ibuf,
966 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
967 if (curstate != STATE_EXPECT_HAVE &&
968 curstate != STATE_EXPECT_DONE) {
969 err = got_error_msg(GOT_ERR_BAD_PACKET,
970 "unexpected 'done' packet");
973 err = recv_done(&packfd, outfd, &ibuf, chattygot);
976 curstate = STATE_DONE;
979 err = got_error(GOT_ERR_BAD_PACKET);
985 err = send_nak(outfd, chattygot);
991 err = relay_progress_reports(&ibuf, outfd, chattygot);
994 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
996 pack_chunksize = sizeof(buf);
1001 r = read(packfd, use_sidebands ? &buf[1] : buf,
1004 err = got_error_from_errno("read");
1006 } else if (r == 0) {
1007 err = got_pkt_flushpkt(outfd, chattygot);
1011 if (use_sidebands) {
1012 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1013 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1017 err = got_poll_write_full(outfd, buf, r);
1019 if (err->code == GOT_ERR_EOF)
1027 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1028 err = got_error_from_errno("close");
1030 echo_error(err, outfd, chattygot);
1034 static const struct got_error *
1035 parse_ref_update_line(char **common_capabilities, char **refname,
1036 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1038 const struct got_error *err;
1039 char *old_id_str = NULL, *new_id_str = NULL;
1040 char *client_capabilities = NULL;
1044 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1045 refname, &client_capabilities, buf, len);
1049 if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
1050 !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
1051 err = got_error_msg(GOT_ERR_BAD_PACKET,
1052 "ref-update with bad object ID");
1055 if (!got_ref_name_is_valid(*refname)) {
1056 err = got_error_msg(GOT_ERR_BAD_PACKET,
1057 "ref-update with bad reference name");
1061 if (client_capabilities) {
1062 err = got_gitproto_match_capabilities(common_capabilities,
1063 NULL, client_capabilities, write_capabilities,
1064 nitems(write_capabilities));
1071 free(client_capabilities);
1079 static const struct got_error *
1080 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1081 char *buf, size_t len, int expect_capabilities, int chattygot)
1083 const struct got_error *err;
1084 struct gotd_imsg_ref_update iref;
1086 char *capabilities_str = NULL, *refname = NULL;
1090 memset(&iref, 0, sizeof(iref));
1091 memset(&imsg, 0, sizeof(imsg));
1093 err = parse_ref_update_line(&capabilities_str, &refname,
1094 iref.old_id, iref.new_id, buf, len);
1098 if (capabilities_str) {
1099 if (!expect_capabilities) {
1100 err = got_error_msg(GOT_ERR_BAD_PACKET,
1101 "unexpected capability announcement received");
1104 err = send_capabilities(NULL, report_status, capabilities_str,
1110 iref.name_len = strlen(refname);
1111 len = sizeof(iref) + iref.name_len;
1112 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1114 err = got_error_from_errno("imsg_create REF_UPDATE");
1118 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1119 return got_error_from_errno("imsg_add REF_UPDATE");
1120 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1121 return got_error_from_errno("imsg_add REF_UPDATE");
1123 imsg_close(ibuf, wbuf);
1125 err = gotd_imsg_flush(ibuf);
1129 /* Wait for ACK or an error. */
1130 while (!done && err == NULL) {
1131 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1134 switch (imsg.hdr.type) {
1135 case GOTD_IMSG_ERROR:
1136 err = gotd_imsg_recv_error(NULL, &imsg);
1139 err = recv_ack(&imsg, iref.new_id);
1145 err = got_error(GOT_ERR_PRIVSEP_MSG);
1152 free(capabilities_str);
1157 static const struct got_error *
1158 recv_packfile(struct imsg *imsg, int infd)
1160 const struct got_error *err = NULL;
1163 char buf[GOT_PKT_MAX];
1166 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1168 return got_error(GOT_ERR_PRIVSEP_MSG);
1171 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1174 while (!pack_done) {
1177 err = got_poll_fd(infd, POLLIN, 1);
1179 if (err->code != GOT_ERR_TIMEOUT)
1183 r = read(infd, buf, sizeof(buf));
1185 err = got_error_from_errno("read");
1190 * Git clients hang up their side of the
1191 * connection after sending the pack file.
1200 /* Detect gotd(8) closing the pack pipe when done. */
1201 err = got_poll_fd(packfd, POLLOUT, 1);
1203 if (err->code != GOT_ERR_EOF)
1209 /* Write pack data and/or detect pipe being closed. */
1210 err = got_poll_write_full(packfd, buf, r);
1212 if (err->code == GOT_ERR_EOF)
1223 static const struct got_error *
1224 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1226 const struct got_error *err = NULL;
1227 struct gotd_imsg_packfile_status istatus;
1228 char buf[GOT_PKT_MAX];
1229 size_t datalen, len;
1230 char *reason = NULL;
1232 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1233 if (datalen < sizeof(istatus))
1234 return got_error(GOT_ERR_PRIVSEP_LEN);
1235 memcpy(&istatus, imsg->data, sizeof(istatus));
1236 if (datalen != sizeof(istatus) + istatus.reason_len)
1237 return got_error(GOT_ERR_PRIVSEP_LEN);
1239 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1240 if (reason == NULL) {
1241 err = got_error_from_errno("strndup");
1246 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1248 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1249 if (len >= sizeof(buf)) {
1250 err = got_error(GOT_ERR_NO_SPACE);
1254 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1260 static const struct got_error *
1261 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1263 const struct got_error *err = NULL;
1264 struct gotd_imsg_ref_update_ok iok;
1265 size_t datalen, len;
1266 char buf[GOT_PKT_MAX];
1267 char *refname = NULL;
1269 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1270 if (datalen < sizeof(iok))
1271 return got_error(GOT_ERR_PRIVSEP_LEN);
1272 memcpy(&iok, imsg->data, sizeof(iok));
1273 if (datalen != sizeof(iok) + iok.name_len)
1274 return got_error(GOT_ERR_PRIVSEP_LEN);
1276 memcpy(&iok, imsg->data, sizeof(iok));
1278 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1279 if (refname == NULL)
1280 return got_error_from_errno("strndup");
1282 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1283 if (len >= sizeof(buf)) {
1284 err = got_error(GOT_ERR_NO_SPACE);
1288 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1294 static const struct got_error *
1295 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1297 const struct got_error *err = NULL;
1298 struct gotd_imsg_ref_update_ng ing;
1299 size_t datalen, len;
1300 char buf[GOT_PKT_MAX];
1301 char *refname = NULL, *reason = NULL;
1303 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1304 if (datalen < sizeof(ing))
1305 return got_error(GOT_ERR_PRIVSEP_LEN);
1306 memcpy(&ing, imsg->data, sizeof(ing));
1307 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1308 return got_error(GOT_ERR_PRIVSEP_LEN);
1310 memcpy(&ing, imsg->data, sizeof(ing));
1312 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1313 if (refname == NULL)
1314 return got_error_from_errno("strndup");
1316 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1318 if (reason == NULL) {
1319 err = got_error_from_errno("strndup");
1323 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1324 if (len >= sizeof(buf)) {
1325 err = got_error(GOT_ERR_NO_SPACE);
1329 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1336 static const struct got_error *
1337 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1340 const struct got_error *err = NULL;
1341 char buf[GOT_PKT_MAX];
1342 struct imsgbuf ibuf;
1344 STATE_EXPECT_REF_UPDATE,
1345 STATE_EXPECT_MORE_REF_UPDATES,
1346 STATE_EXPECT_PACKFILE,
1347 STATE_PACKFILE_RECEIVED,
1350 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1352 int report_status = 0;
1354 imsg_init(&ibuf, gotd_sock);
1355 memset(&imsg, 0, sizeof(imsg));
1357 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1361 while (curstate != STATE_EXPECT_PACKFILE) {
1364 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1368 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1369 err = got_error_msg(GOT_ERR_BAD_PACKET,
1370 "unexpected flush packet received");
1373 err = forward_flushpkt(&ibuf);
1376 curstate = STATE_EXPECT_PACKFILE;
1377 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1378 if (curstate != STATE_EXPECT_REF_UPDATE &&
1379 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1380 err = got_error_msg(GOT_ERR_BAD_PACKET,
1381 "unexpected ref-update packet");
1384 if (curstate == STATE_EXPECT_REF_UPDATE) {
1385 err = recv_ref_update(&report_status,
1386 outfd, &ibuf, buf, n, 1, chattygot);
1388 err = recv_ref_update(NULL, outfd, &ibuf,
1389 buf, n, 0, chattygot);
1393 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1395 err = got_error(GOT_ERR_BAD_PACKET);
1400 while (curstate != STATE_PACKFILE_RECEIVED) {
1401 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1404 switch (imsg.hdr.type) {
1405 case GOTD_IMSG_ERROR:
1406 err = gotd_imsg_recv_error(NULL, &imsg);
1408 case GOTD_IMSG_RECV_PACKFILE:
1409 err = recv_packfile(&imsg, infd);
1411 if (err->code != GOT_ERR_EOF)
1414 * EOF is reported when the client hangs up,
1415 * which can happen with Git clients.
1416 * The socket should stay half-open so we
1417 * can still send our reports if requested.
1421 curstate = STATE_PACKFILE_RECEIVED;
1424 err = got_error(GOT_ERR_PRIVSEP_MSG);
1433 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1434 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1437 switch (imsg.hdr.type) {
1438 case GOTD_IMSG_ERROR:
1439 err = gotd_imsg_recv_error(NULL, &imsg);
1441 case GOTD_IMSG_PACKFILE_STATUS:
1444 err = report_unpack_status(&imsg, outfd, chattygot);
1446 case GOTD_IMSG_REF_UPDATE_OK:
1449 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1451 case GOTD_IMSG_REF_UPDATE_NG:
1454 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1456 case GOTD_IMSG_REFS_UPDATED:
1457 curstate = STATE_REFS_UPDATED;
1458 err = got_pkt_flushpkt(outfd, chattygot);
1461 err = got_error(GOT_ERR_PRIVSEP_MSG);
1470 echo_error(err, outfd, chattygot);
1474 const struct got_error *
1475 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1476 int gotd_sock, int chattygot)
1478 const struct got_error *err = NULL;
1480 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1481 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1482 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1483 err = serve_write(infd, outfd, gotd_sock, repo_path,
1486 err = got_error(GOT_ERR_BAD_PACKET);