Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <errno.h>
22 #include <event.h>
23 #include <poll.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <sha2.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <imsg.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_serve.h"
36 #include "got_path.h"
37 #include "got_version.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_lib_pkt.h"
42 #include "got_lib_dial.h"
43 #include "got_lib_gitproto.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_poll.h"
47 #include "gotd.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static const struct got_capability read_capabilities[] = {
54 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
55 { GOT_CAPA_OFS_DELTA, NULL },
56 { GOT_CAPA_SIDE_BAND_64K, NULL },
57 };
59 static const struct got_capability write_capabilities[] = {
60 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
61 { GOT_CAPA_OFS_DELTA, NULL },
62 { GOT_CAPA_REPORT_STATUS, NULL },
63 { GOT_CAPA_NO_THIN, NULL },
64 { GOT_CAPA_DELETE_REFS, NULL },
65 };
67 static const struct got_error *
68 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
69 uint8_t *buf, size_t bufsize)
70 {
71 struct got_capability capa[nitems(read_capabilities) + 1];
72 size_t ncapa;
74 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
75 if (symrefstr) {
76 capa[nitems(read_capabilities)].key = "symref";
77 capa[nitems(read_capabilities)].value = symrefstr;
78 ncapa = nitems(capa);
79 } else
80 ncapa = nitems(read_capabilities);
82 return got_gitproto_append_capabilities(capalen, buf, len,
83 bufsize, capa, ncapa);
84 }
86 static const struct got_error *
87 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
88 int client_is_reading, const char *symrefstr, int chattygot)
89 {
90 const struct got_error *err = NULL;
91 char hex[SHA1_DIGEST_STRING_LENGTH];
92 char buf[GOT_PKT_MAX];
93 size_t len, capalen = 0;
95 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
96 return got_error(GOT_ERR_BAD_OBJ_ID);
98 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
99 if (len >= sizeof(buf))
100 return got_error(GOT_ERR_NO_SPACE);
102 if (send_capabilities) {
103 if (client_is_reading) {
104 err = append_read_capabilities(&capalen, len,
105 symrefstr, buf, sizeof(buf));
106 } else {
107 err = got_gitproto_append_capabilities(&capalen,
108 buf, len, sizeof(buf), write_capabilities,
109 nitems(write_capabilities));
111 if (err)
112 return err;
113 len += capalen;
116 if (len + 1 >= sizeof(buf))
117 return got_error(GOT_ERR_NO_SPACE);
118 buf[len] = '\n';
119 len++;
120 buf[len] = '\0';
122 return got_pkt_writepkt(outfd, buf, len, chattygot);
125 static const struct got_error *
126 send_zero_refs(int outfd, int client_is_reading, int chattygot)
128 const struct got_error *err = NULL;
129 const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
130 char buf[GOT_PKT_MAX];
131 size_t len, capalen = 0;
133 len = strlcpy(buf, line, sizeof(buf));
134 if (len >= sizeof(buf))
135 return got_error(GOT_ERR_NO_SPACE);
137 if (client_is_reading) {
138 err = got_gitproto_append_capabilities(&capalen, buf, len,
139 sizeof(buf), read_capabilities, nitems(read_capabilities));
140 if (err)
141 return err;
142 } else {
143 err = got_gitproto_append_capabilities(&capalen, buf, len,
144 sizeof(buf), write_capabilities,
145 nitems(write_capabilities));
146 if (err)
147 return err;
150 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
153 static void
154 echo_error(const struct got_error *err, int outfd, int chattygot)
156 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
157 size_t len;
159 /*
160 * Echo the error to the client on a pkt-line.
161 * The client should then terminate its session.
162 */
163 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
164 len = strlcat(buf, err->msg, sizeof(buf));
165 got_pkt_writepkt(outfd, buf, len, chattygot);
168 static const struct got_error *
169 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
170 const char *repo_path, int chattygot)
172 const struct got_error *err = NULL;
173 struct imsg imsg;
174 size_t datalen;
175 struct gotd_imsg_list_refs lsref;
176 struct gotd_imsg_reflist ireflist;
177 struct gotd_imsg_ref iref;
178 struct gotd_imsg_symref isymref;
179 size_t nrefs = 0;
180 int have_nrefs = 0, sent_capabilities = 0;
181 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
182 char *refname = NULL;
184 memset(&imsg, 0, sizeof(imsg));
185 memset(&lsref, 0, sizeof(lsref));
187 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
188 sizeof(lsref.repo_name))
189 return got_error(GOT_ERR_NO_SPACE);
190 lsref.client_is_reading = client_is_reading;
192 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
193 &lsref, sizeof(lsref)) == -1)
194 return got_error_from_errno("imsg_compose LIST_REFS");
196 err = gotd_imsg_flush(ibuf);
197 if (err)
198 return err;
200 while (!have_nrefs || nrefs > 0) {
201 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
202 if (err)
203 goto done;
204 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
205 switch (imsg.hdr.type) {
206 case GOTD_IMSG_ERROR:
207 err = gotd_imsg_recv_error(NULL, &imsg);
208 goto done;
209 case GOTD_IMSG_REFLIST:
210 if (have_nrefs || nrefs > 0) {
211 err = got_error(GOT_ERR_PRIVSEP_MSG);
212 goto done;
214 if (datalen != sizeof(ireflist)) {
215 err = got_error(GOT_ERR_PRIVSEP_MSG);
216 goto done;
218 memcpy(&ireflist, imsg.data, sizeof(ireflist));
219 nrefs = ireflist.nrefs;
220 have_nrefs = 1;
221 if (nrefs == 0)
222 err = send_zero_refs(outfd, client_is_reading,
223 chattygot);
224 break;
225 case GOTD_IMSG_REF:
226 if (!have_nrefs || nrefs == 0) {
227 err = got_error(GOT_ERR_PRIVSEP_MSG);
228 goto done;
230 if (datalen < sizeof(iref)) {
231 err = got_error(GOT_ERR_PRIVSEP_MSG);
232 goto done;
234 memcpy(&iref, imsg.data, sizeof(iref));
235 if (datalen != sizeof(iref) + iref.name_len) {
236 err = got_error(GOT_ERR_PRIVSEP_LEN);
237 goto done;
239 refname = strndup(imsg.data + sizeof(iref),
240 iref.name_len);
241 if (refname == NULL) {
242 err = got_error_from_errno("strndup");
243 goto done;
245 err = send_ref(outfd, iref.id, refname,
246 !sent_capabilities, client_is_reading,
247 NULL, chattygot);
248 free(refname);
249 refname = NULL;
250 if (err)
251 goto done;
252 sent_capabilities = 1;
253 if (nrefs > 0)
254 nrefs--;
255 break;
256 case GOTD_IMSG_SYMREF:
257 if (!have_nrefs || nrefs == 0) {
258 err = got_error(GOT_ERR_PRIVSEP_MSG);
259 goto done;
261 if (datalen < sizeof(isymref)) {
262 err = got_error(GOT_ERR_PRIVSEP_LEN);
263 goto done;
265 memcpy(&isymref, imsg.data, sizeof(isymref));
266 if (datalen != sizeof(isymref) + isymref.name_len +
267 isymref.target_len) {
268 err = got_error(GOT_ERR_PRIVSEP_LEN);
269 goto done;
272 /*
273 * For now, we only announce one symbolic ref,
274 * as part of our capability advertisement.
275 */
276 if (sent_capabilities || symrefstr != NULL ||
277 symrefname != NULL || symreftarget != NULL)
278 break;
280 symrefname = strndup(imsg.data + sizeof(isymref),
281 isymref.name_len);
282 if (symrefname == NULL) {
283 err = got_error_from_errno("malloc");
284 goto done;
287 symreftarget = strndup(
288 imsg.data + sizeof(isymref) + isymref.name_len,
289 isymref.target_len);
290 if (symreftarget == NULL) {
291 err = got_error_from_errno("strndup");
292 goto done;
295 if (asprintf(&symrefstr, "%s:%s", symrefname,
296 symreftarget) == -1) {
297 err = got_error_from_errno("asprintf");
298 goto done;
300 err = send_ref(outfd, isymref.target_id, symrefname,
301 !sent_capabilities, client_is_reading, symrefstr,
302 chattygot);
303 free(refname);
304 refname = NULL;
305 if (err)
306 goto done;
307 sent_capabilities = 1;
308 if (nrefs > 0)
309 nrefs--;
310 break;
311 default:
312 err = got_error(GOT_ERR_PRIVSEP_MSG);
313 break;
316 imsg_free(&imsg);
319 err = got_pkt_flushpkt(outfd, chattygot);
320 if (err)
321 goto done;
322 done:
323 free(symrefstr);
324 free(symrefname);
325 free(symreftarget);
326 return err;
329 static const struct got_error *
330 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
332 const struct got_error *err;
333 char *id_str = NULL, *client_capabilities = NULL;
335 err = got_gitproto_parse_want_line(&id_str,
336 &client_capabilities, buf, len);
337 if (err)
338 return err;
340 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
341 err = got_error_msg(GOT_ERR_BAD_PACKET,
342 "want-line with bad object ID");
343 goto done;
346 if (client_capabilities) {
347 err = got_gitproto_match_capabilities(common_capabilities,
348 NULL, client_capabilities, read_capabilities,
349 nitems(read_capabilities));
350 if (err)
351 goto done;
353 done:
354 free(id_str);
355 free(client_capabilities);
356 return err;
359 static const struct got_error *
360 parse_have_line(uint8_t *id, char *buf, size_t len)
362 const struct got_error *err;
363 char *id_str = NULL;
365 err = got_gitproto_parse_have_line(&id_str, buf, len);
366 if (err)
367 return err;
369 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
370 err = got_error_msg(GOT_ERR_BAD_PACKET,
371 "have-line with bad object ID");
372 goto done;
374 done:
375 free(id_str);
376 return err;
379 static const struct got_error *
380 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
382 const struct got_error *err = NULL;
383 struct gotd_imsg_capability icapa;
384 size_t len;
385 struct ibuf *wbuf;
387 memset(&icapa, 0, sizeof(icapa));
389 icapa.key_len = strlen(capa->key);
390 len = sizeof(icapa) + icapa.key_len;
391 if (capa->value) {
392 icapa.value_len = strlen(capa->value);
393 len += icapa.value_len;
396 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
397 if (wbuf == NULL) {
398 err = got_error_from_errno("imsg_create CAPABILITY");
399 return err;
402 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
403 return got_error_from_errno("imsg_add CAPABILITY");
404 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
405 return got_error_from_errno("imsg_add CAPABILITY");
406 if (capa->value) {
407 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
408 return got_error_from_errno("imsg_add CAPABILITY");
411 imsg_close(ibuf, wbuf);
413 return NULL;
416 static const struct got_error *
417 send_capabilities(int *use_sidebands, int *report_status,
418 char *capabilities_str, struct imsgbuf *ibuf)
420 const struct got_error *err = NULL;
421 struct gotd_imsg_capabilities icapas;
422 struct got_capability *capa = NULL;
423 size_t ncapa, i;
425 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
426 capabilities_str);
427 if (err)
428 return err;
430 icapas.ncapabilities = ncapa;
431 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
432 &icapas, sizeof(icapas)) == -1) {
433 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
434 goto done;
437 for (i = 0; i < ncapa; i++) {
438 err = send_capability(&capa[i], ibuf);
439 if (err)
440 goto done;
441 if (use_sidebands &&
442 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
443 *use_sidebands = 1;
444 if (report_status &&
445 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
446 *report_status = 1;
448 done:
449 free(capa);
450 return err;
453 static const struct got_error *
454 forward_flushpkt(struct imsgbuf *ibuf)
456 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
457 return got_error_from_errno("imsg_compose FLUSH");
459 return gotd_imsg_flush(ibuf);
462 static const struct got_error *
463 recv_ack(struct imsg *imsg, uint8_t *expected_id)
465 struct gotd_imsg_ack iack;
466 size_t datalen;
468 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
469 if (datalen != sizeof(iack))
470 return got_error(GOT_ERR_PRIVSEP_LEN);
472 memcpy(&iack, imsg->data, sizeof(iack));
473 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
474 return got_error(GOT_ERR_BAD_OBJ_ID);
476 return NULL;
479 static const struct got_error *
480 recv_nak(struct imsg *imsg, uint8_t *expected_id)
482 struct gotd_imsg_ack inak;
483 size_t datalen;
485 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
486 if (datalen != sizeof(inak))
487 return got_error(GOT_ERR_PRIVSEP_LEN);
489 memcpy(&inak, imsg->data, sizeof(inak));
490 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
491 return got_error(GOT_ERR_BAD_OBJ_ID);
493 return NULL;
497 static const struct got_error *
498 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
499 char *buf, size_t len, int expect_capabilities, int chattygot)
501 const struct got_error *err;
502 struct gotd_imsg_want iwant;
503 char *capabilities_str;
504 int done = 0;
505 struct imsg imsg;
507 memset(&iwant, 0, sizeof(iwant));
508 memset(&imsg, 0, sizeof(imsg));
510 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
511 if (err)
512 return err;
514 if (capabilities_str) {
515 if (!expect_capabilities) {
516 err = got_error_msg(GOT_ERR_BAD_PACKET,
517 "unexpected capability announcement received");
518 goto done;
520 err = send_capabilities(use_sidebands, NULL, capabilities_str,
521 ibuf);
522 if (err)
523 goto done;
527 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
528 &iwant, sizeof(iwant)) == -1) {
529 err = got_error_from_errno("imsg_compose WANT");
530 goto done;
533 err = gotd_imsg_flush(ibuf);
534 if (err)
535 goto done;
537 /*
538 * Wait for an ACK, or an error in case the desired object
539 * does not exist.
540 */
541 while (!done && err == NULL) {
542 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
543 if (err)
544 break;
545 switch (imsg.hdr.type) {
546 case GOTD_IMSG_ERROR:
547 err = gotd_imsg_recv_error(NULL, &imsg);
548 break;
549 case GOTD_IMSG_ACK:
550 err = recv_ack(&imsg, iwant.object_id);
551 if (err)
552 break;
553 done = 1;
554 break;
555 default:
556 err = got_error(GOT_ERR_PRIVSEP_MSG);
557 break;
560 imsg_free(&imsg);
562 done:
563 free(capabilities_str);
564 return err;
567 static const struct got_error *
568 send_ack(int outfd, uint8_t *id, int chattygot)
570 char hex[SHA1_DIGEST_STRING_LENGTH];
571 char buf[GOT_PKT_MAX];
572 int len;
574 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
575 return got_error(GOT_ERR_BAD_OBJ_ID);
577 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
578 if (len >= sizeof(buf))
579 return got_error(GOT_ERR_NO_SPACE);
581 return got_pkt_writepkt(outfd, buf, len, chattygot);
584 static const struct got_error *
585 send_nak(int outfd, int chattygot)
587 char buf[5];
588 int len;
590 len = snprintf(buf, sizeof(buf), "NAK\n");
591 if (len >= sizeof(buf))
592 return got_error(GOT_ERR_NO_SPACE);
594 return got_pkt_writepkt(outfd, buf, len, chattygot);
597 static const struct got_error *
598 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
599 size_t len, int chattygot)
601 const struct got_error *err;
602 struct gotd_imsg_have ihave;
603 int done = 0;
604 struct imsg imsg;
606 memset(&ihave, 0, sizeof(ihave));
607 memset(&imsg, 0, sizeof(imsg));
609 err = parse_have_line(ihave.object_id, buf, len);
610 if (err)
611 return err;
613 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
614 &ihave, sizeof(ihave)) == -1)
615 return got_error_from_errno("imsg_compose HAVE");
617 err = gotd_imsg_flush(ibuf);
618 if (err)
619 return err;
621 /*
622 * Wait for an ACK or a NAK, indicating whether a common
623 * commit object has been found.
624 */
625 while (!done && err == NULL) {
626 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
627 if (err)
628 return err;
629 switch (imsg.hdr.type) {
630 case GOTD_IMSG_ERROR:
631 err = gotd_imsg_recv_error(NULL, &imsg);
632 break;
633 case GOTD_IMSG_ACK:
634 err = recv_ack(&imsg, ihave.object_id);
635 if (err)
636 break;
637 if (!*have_ack) {
638 err = send_ack(outfd, ihave.object_id,
639 chattygot);
640 if (err)
641 return err;
642 *have_ack = 1;
644 done = 1;
645 break;
646 case GOTD_IMSG_NAK:
647 err = recv_nak(&imsg, ihave.object_id);
648 if (err)
649 break;
650 done = 1;
651 break;
652 default:
653 err = got_error(GOT_ERR_PRIVSEP_MSG);
654 break;
657 imsg_free(&imsg);
660 return err;
663 static const struct got_error *
664 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
666 const struct got_error *err;
667 struct imsg imsg;
668 int fd;
670 *packfd = -1;
672 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
673 return got_error_from_errno("imsg_compose DONE");
675 err = gotd_imsg_flush(ibuf);
676 if (err)
677 return err;
679 while (*packfd == -1 && err == NULL) {
680 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
681 if (err)
682 break;
684 switch (imsg.hdr.type) {
685 case GOTD_IMSG_ERROR:
686 err = gotd_imsg_recv_error(NULL, &imsg);
687 break;
688 case GOTD_IMSG_PACKFILE_PIPE:
689 fd = imsg_get_fd(&imsg);
690 if (fd != -1)
691 *packfd = fd;
692 else
693 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
694 break;
695 default:
696 err = got_error(GOT_ERR_PRIVSEP_MSG);
697 break;
700 imsg_free(&imsg);
703 return err;
706 static const struct got_error *
707 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
709 const struct got_error *err = NULL;
710 int pack_starting = 0;
711 struct gotd_imsg_packfile_progress iprog;
712 char buf[GOT_PKT_MAX];
713 struct imsg imsg;
714 size_t datalen;
715 int p_deltify = 0, n;
716 const char *eol = "\r";
718 memset(&imsg, 0, sizeof(imsg));
720 while (!pack_starting && err == NULL) {
721 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
722 if (err)
723 break;
725 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
726 switch (imsg.hdr.type) {
727 case GOTD_IMSG_ERROR:
728 err = gotd_imsg_recv_error(NULL, &imsg);
729 break;
730 case GOTD_IMSG_PACKFILE_READY:
731 eol = "\n";
732 pack_starting = 1;
733 /* fallthrough */
734 case GOTD_IMSG_PACKFILE_PROGRESS:
735 if (datalen != sizeof(iprog)) {
736 err = got_error(GOT_ERR_PRIVSEP_LEN);
737 break;
739 memcpy(&iprog, imsg.data, sizeof(iprog));
740 if (iprog.nobj_total > 0) {
741 p_deltify = (iprog.nobj_deltify * 100) /
742 iprog.nobj_total;
744 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
745 n = snprintf(&buf[1], sizeof(buf) - 1,
746 "%d commits colored, "
747 "%d objects found, "
748 "deltify %d%%%s",
749 iprog.ncolored,
750 iprog.nfound,
751 p_deltify, eol);
752 if (n >= sizeof(buf) - 1)
753 break;
754 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
755 break;
756 default:
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
758 break;
761 imsg_free(&imsg);
764 return err;
767 static const struct got_error *
768 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
769 int chattygot)
771 const struct got_error *err = NULL;
772 char buf[GOT_PKT_MAX];
773 struct imsgbuf ibuf;
774 enum protostate {
775 STATE_EXPECT_WANT,
776 STATE_EXPECT_MORE_WANT,
777 STATE_EXPECT_HAVE_OR_DONE,
778 STATE_DONE,
779 };
780 enum protostate curstate = STATE_EXPECT_WANT;
781 int have_ack = 0, use_sidebands = 0, seen_have = 0;
782 int packfd = -1;
783 size_t pack_chunksize;
785 imsg_init(&ibuf, gotd_sock);
787 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
788 if (err)
789 goto done;
791 while (curstate != STATE_DONE) {
792 int n;
793 buf[0] = '\0';
794 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
795 if (err)
796 goto done;
797 if (n == 0) {
798 if (curstate != STATE_EXPECT_WANT &&
799 curstate != STATE_EXPECT_MORE_WANT &&
800 curstate != STATE_EXPECT_HAVE_OR_DONE) {
801 err = got_error_msg(GOT_ERR_BAD_PACKET,
802 "unexpected flush packet received");
803 goto done;
806 if (curstate == STATE_EXPECT_WANT) {
807 ssize_t r;
808 /*
809 * If the client does not want to fetch
810 * anything we should receive a flush
811 * packet followed by EOF.
812 */
813 r = read(infd, buf, sizeof(buf));
814 if (r == -1) {
815 err = got_error_from_errno("read");
816 goto done;
818 if (r == 0) /* EOF */
819 goto done;
821 /* Zero-length field followed by payload. */
822 err = got_error_msg(GOT_ERR_BAD_PACKET,
823 "unexpected flush packet received");
824 goto done;
827 if (curstate == STATE_EXPECT_WANT ||
828 curstate == STATE_EXPECT_MORE_WANT ||
829 curstate == STATE_EXPECT_HAVE_OR_DONE) {
830 err = forward_flushpkt(&ibuf);
831 if (err)
832 goto done;
834 if (curstate == STATE_EXPECT_HAVE_OR_DONE &&
835 !have_ack) {
836 err = send_nak(outfd, chattygot);
837 if (err)
838 goto done;
840 if (curstate == STATE_EXPECT_MORE_WANT)
841 curstate = STATE_EXPECT_HAVE_OR_DONE;
842 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
843 if (curstate != STATE_EXPECT_WANT &&
844 curstate != STATE_EXPECT_MORE_WANT) {
845 err = got_error_msg(GOT_ERR_BAD_PACKET,
846 "unexpected 'want' packet");
847 goto done;
849 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
850 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
851 if (err)
852 goto done;
853 if (curstate == STATE_EXPECT_WANT)
854 curstate = STATE_EXPECT_MORE_WANT;
855 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
856 if (curstate != STATE_EXPECT_HAVE_OR_DONE) {
857 err = got_error_msg(GOT_ERR_BAD_PACKET,
858 "unexpected 'have' packet");
859 goto done;
861 err = recv_have(&have_ack, outfd, &ibuf,
862 buf, n, chattygot);
863 if (err)
864 goto done;
865 seen_have = 1;
866 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
867 if (curstate != STATE_EXPECT_HAVE_OR_DONE) {
868 err = got_error_msg(GOT_ERR_BAD_PACKET,
869 "unexpected 'done' packet");
870 goto done;
872 err = recv_done(&packfd, outfd, &ibuf, chattygot);
873 if (err)
874 goto done;
875 curstate = STATE_DONE;
876 break;
877 } else {
878 err = got_error(GOT_ERR_BAD_PACKET);
879 goto done;
883 if (!seen_have) {
884 err = send_nak(outfd, chattygot);
885 if (err)
886 goto done;
889 if (use_sidebands) {
890 err = relay_progress_reports(&ibuf, outfd, chattygot);
891 if (err)
892 goto done;
893 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
894 } else
895 pack_chunksize = sizeof(buf);
897 for (;;) {
898 ssize_t r;
900 r = read(packfd, use_sidebands ? &buf[1] : buf,
901 pack_chunksize);
902 if (r == -1) {
903 err = got_error_from_errno("read");
904 break;
905 } else if (r == 0) {
906 err = got_pkt_flushpkt(outfd, chattygot);
907 break;
910 if (use_sidebands) {
911 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
912 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
913 if (err)
914 break;
915 } else {
916 err = got_poll_write_full(outfd, buf, r);
917 if (err) {
918 if (err->code == GOT_ERR_EOF)
919 err = NULL;
920 break;
924 done:
925 imsg_clear(&ibuf);
926 if (packfd != -1 && close(packfd) == -1 && err == NULL)
927 err = got_error_from_errno("close");
928 if (err)
929 echo_error(err, outfd, chattygot);
930 return err;
933 static const struct got_error *
934 parse_ref_update_line(char **common_capabilities, char **refname,
935 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
937 const struct got_error *err;
938 char *old_id_str = NULL, *new_id_str = NULL;
939 char *client_capabilities = NULL;
941 *refname = NULL;
943 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
944 refname, &client_capabilities, buf, len);
945 if (err)
946 return err;
948 if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
949 !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
950 err = got_error_msg(GOT_ERR_BAD_PACKET,
951 "ref-update with bad object ID");
952 goto done;
954 if (!got_ref_name_is_valid(*refname)) {
955 err = got_error_msg(GOT_ERR_BAD_PACKET,
956 "ref-update with bad reference name");
957 goto done;
960 if (client_capabilities) {
961 err = got_gitproto_match_capabilities(common_capabilities,
962 NULL, client_capabilities, write_capabilities,
963 nitems(write_capabilities));
964 if (err)
965 goto done;
967 done:
968 free(old_id_str);
969 free(new_id_str);
970 free(client_capabilities);
971 if (err) {
972 free(*refname);
973 *refname = NULL;
975 return err;
978 static const struct got_error *
979 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
980 char *buf, size_t len, int expect_capabilities, int chattygot)
982 const struct got_error *err;
983 struct gotd_imsg_ref_update iref;
984 struct ibuf *wbuf;
985 char *capabilities_str = NULL, *refname = NULL;
986 int done = 0;
987 struct imsg imsg;
989 memset(&iref, 0, sizeof(iref));
990 memset(&imsg, 0, sizeof(imsg));
992 err = parse_ref_update_line(&capabilities_str, &refname,
993 iref.old_id, iref.new_id, buf, len);
994 if (err)
995 return err;
997 if (capabilities_str) {
998 if (!expect_capabilities) {
999 err = got_error_msg(GOT_ERR_BAD_PACKET,
1000 "unexpected capability announcement received");
1001 goto done;
1003 err = send_capabilities(NULL, report_status, capabilities_str,
1004 ibuf);
1005 if (err)
1006 goto done;
1009 iref.name_len = strlen(refname);
1010 len = sizeof(iref) + iref.name_len;
1011 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1012 if (wbuf == NULL) {
1013 err = got_error_from_errno("imsg_create REF_UPDATE");
1014 goto done;
1017 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1018 return got_error_from_errno("imsg_add REF_UPDATE");
1019 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1020 return got_error_from_errno("imsg_add REF_UPDATE");
1021 imsg_close(ibuf, wbuf);
1023 err = gotd_imsg_flush(ibuf);
1024 if (err)
1025 goto done;
1027 /* Wait for ACK or an error. */
1028 while (!done && err == NULL) {
1029 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1030 if (err)
1031 break;
1032 switch (imsg.hdr.type) {
1033 case GOTD_IMSG_ERROR:
1034 err = gotd_imsg_recv_error(NULL, &imsg);
1035 break;
1036 case GOTD_IMSG_ACK:
1037 err = recv_ack(&imsg, iref.new_id);
1038 if (err)
1039 break;
1040 done = 1;
1041 break;
1042 default:
1043 err = got_error(GOT_ERR_PRIVSEP_MSG);
1044 break;
1047 imsg_free(&imsg);
1049 done:
1050 free(capabilities_str);
1051 free(refname);
1052 return err;
1055 static const struct got_error *
1056 recv_packfile(struct imsg *imsg, int infd)
1058 const struct got_error *err = NULL;
1059 size_t datalen;
1060 int packfd;
1061 char buf[GOT_PKT_MAX];
1062 int pack_done = 0;
1064 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1065 if (datalen != 0)
1066 return got_error(GOT_ERR_PRIVSEP_MSG);
1068 packfd = imsg_get_fd(imsg);
1069 if (packfd == -1)
1070 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1072 while (!pack_done) {
1073 ssize_t r = 0;
1075 err = got_poll_fd(infd, POLLIN, 1);
1076 if (err) {
1077 if (err->code != GOT_ERR_TIMEOUT)
1078 break;
1079 err = NULL;
1080 } else {
1081 r = read(infd, buf, sizeof(buf));
1082 if (r == -1) {
1083 err = got_error_from_errno("read");
1084 break;
1086 if (r == 0) {
1088 * Git clients hang up their side of the
1089 * connection after sending the pack file.
1091 err = NULL;
1092 pack_done = 1;
1093 break;
1097 if (r == 0) {
1098 /* Detect gotd(8) closing the pack pipe when done. */
1099 err = got_poll_fd(packfd, 0, 1);
1100 if (err) {
1101 if (err->code != GOT_ERR_TIMEOUT &&
1102 err->code != GOT_ERR_EOF)
1103 break;
1104 if (err->code == GOT_ERR_EOF)
1105 pack_done = 1;
1106 err = NULL;
1108 } else {
1109 /* Write pack data and/or detect pipe being closed. */
1110 err = got_poll_write_full(packfd, buf, r);
1111 if (err) {
1112 if (err->code == GOT_ERR_EOF)
1113 err = NULL;
1114 break;
1119 close(packfd);
1120 return err;
1123 static const struct got_error *
1124 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1126 const struct got_error *err = NULL;
1127 struct gotd_imsg_packfile_status istatus;
1128 char buf[GOT_PKT_MAX];
1129 size_t datalen, len;
1130 char *reason = NULL;
1132 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1133 if (datalen < sizeof(istatus))
1134 return got_error(GOT_ERR_PRIVSEP_LEN);
1135 memcpy(&istatus, imsg->data, sizeof(istatus));
1136 if (datalen != sizeof(istatus) + istatus.reason_len)
1137 return got_error(GOT_ERR_PRIVSEP_LEN);
1139 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1140 if (reason == NULL) {
1141 err = got_error_from_errno("strndup");
1142 goto done;
1145 if (err == NULL)
1146 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1147 else
1148 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1149 if (len >= sizeof(buf)) {
1150 err = got_error(GOT_ERR_NO_SPACE);
1151 goto done;
1154 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1155 done:
1156 free(reason);
1157 return err;
1160 static const struct got_error *
1161 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1163 const struct got_error *err = NULL;
1164 struct gotd_imsg_ref_update_ok iok;
1165 size_t datalen, len;
1166 char buf[GOT_PKT_MAX];
1167 char *refname = NULL;
1169 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1170 if (datalen < sizeof(iok))
1171 return got_error(GOT_ERR_PRIVSEP_LEN);
1172 memcpy(&iok, imsg->data, sizeof(iok));
1173 if (datalen != sizeof(iok) + iok.name_len)
1174 return got_error(GOT_ERR_PRIVSEP_LEN);
1176 memcpy(&iok, imsg->data, sizeof(iok));
1178 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1179 if (refname == NULL)
1180 return got_error_from_errno("strndup");
1182 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1183 if (len >= sizeof(buf)) {
1184 err = got_error(GOT_ERR_NO_SPACE);
1185 goto done;
1188 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1189 done:
1190 free(refname);
1191 return err;
1194 static const struct got_error *
1195 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1197 const struct got_error *err = NULL;
1198 struct gotd_imsg_ref_update_ng ing;
1199 size_t datalen, len;
1200 char buf[GOT_PKT_MAX];
1201 char *refname = NULL, *reason = NULL;
1203 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1204 if (datalen < sizeof(ing))
1205 return got_error(GOT_ERR_PRIVSEP_LEN);
1206 memcpy(&ing, imsg->data, sizeof(ing));
1207 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1208 return got_error(GOT_ERR_PRIVSEP_LEN);
1210 memcpy(&ing, imsg->data, sizeof(ing));
1212 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1213 if (refname == NULL)
1214 return got_error_from_errno("strndup");
1216 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1217 ing.reason_len);
1218 if (reason == NULL) {
1219 err = got_error_from_errno("strndup");
1220 goto done;
1223 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1224 if (len >= sizeof(buf)) {
1225 err = got_error(GOT_ERR_NO_SPACE);
1226 goto done;
1229 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1230 done:
1231 free(refname);
1232 free(reason);
1233 return err;
1236 static const struct got_error *
1237 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1238 int chattygot)
1240 const struct got_error *err = NULL;
1241 char buf[GOT_PKT_MAX];
1242 struct imsgbuf ibuf;
1243 enum protostate {
1244 STATE_EXPECT_REF_UPDATE,
1245 STATE_EXPECT_MORE_REF_UPDATES,
1246 STATE_EXPECT_PACKFILE,
1247 STATE_PACKFILE_RECEIVED,
1248 STATE_REFS_UPDATED,
1250 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1251 struct imsg imsg;
1252 int report_status = 0;
1254 imsg_init(&ibuf, gotd_sock);
1255 memset(&imsg, 0, sizeof(imsg));
1257 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1258 if (err)
1259 goto done;
1261 while (curstate != STATE_EXPECT_PACKFILE) {
1262 int n;
1263 buf[0] = '\0';
1264 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1265 if (err)
1266 goto done;
1267 if (n == 0) {
1268 if (curstate == STATE_EXPECT_REF_UPDATE) {
1269 /* The client will not send us anything. */
1270 goto done;
1271 } else if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1272 err = got_error_msg(GOT_ERR_BAD_PACKET,
1273 "unexpected flush packet received");
1274 goto done;
1276 err = forward_flushpkt(&ibuf);
1277 if (err)
1278 goto done;
1279 curstate = STATE_EXPECT_PACKFILE;
1280 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1281 if (curstate != STATE_EXPECT_REF_UPDATE &&
1282 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1283 err = got_error_msg(GOT_ERR_BAD_PACKET,
1284 "unexpected ref-update packet");
1285 goto done;
1287 if (curstate == STATE_EXPECT_REF_UPDATE) {
1288 err = recv_ref_update(&report_status,
1289 outfd, &ibuf, buf, n, 1, chattygot);
1290 } else {
1291 err = recv_ref_update(NULL, outfd, &ibuf,
1292 buf, n, 0, chattygot);
1294 if (err)
1295 goto done;
1296 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1297 } else {
1298 err = got_error(GOT_ERR_BAD_PACKET);
1299 goto done;
1303 while (curstate != STATE_PACKFILE_RECEIVED) {
1304 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1305 if (err)
1306 goto done;
1307 switch (imsg.hdr.type) {
1308 case GOTD_IMSG_ERROR:
1309 err = gotd_imsg_recv_error(NULL, &imsg);
1310 goto done;
1311 case GOTD_IMSG_PACKFILE_PIPE:
1312 err = recv_packfile(&imsg, infd);
1313 if (err) {
1314 if (err->code != GOT_ERR_EOF)
1315 goto done;
1317 * EOF is reported when the client hangs up,
1318 * which can happen with Git clients.
1319 * The socket should stay half-open so we
1320 * can still send our reports if requested.
1322 err = NULL;
1324 curstate = STATE_PACKFILE_RECEIVED;
1325 break;
1326 default:
1327 err = got_error(GOT_ERR_PRIVSEP_MSG);
1328 break;
1331 imsg_free(&imsg);
1332 if (err)
1333 goto done;
1336 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1337 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1338 if (err)
1339 break;
1340 switch (imsg.hdr.type) {
1341 case GOTD_IMSG_ERROR:
1342 err = gotd_imsg_recv_error(NULL, &imsg);
1343 break;
1344 case GOTD_IMSG_PACKFILE_STATUS:
1345 if (!report_status)
1346 break;
1347 err = report_unpack_status(&imsg, outfd, chattygot);
1348 break;
1349 case GOTD_IMSG_REF_UPDATE_OK:
1350 if (!report_status)
1351 break;
1352 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1353 break;
1354 case GOTD_IMSG_REF_UPDATE_NG:
1355 if (!report_status)
1356 break;
1357 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1358 break;
1359 case GOTD_IMSG_REFS_UPDATED:
1360 curstate = STATE_REFS_UPDATED;
1361 err = got_pkt_flushpkt(outfd, chattygot);
1362 break;
1363 default:
1364 err = got_error(GOT_ERR_PRIVSEP_MSG);
1365 break;
1368 imsg_free(&imsg);
1370 done:
1371 imsg_clear(&ibuf);
1372 if (err)
1373 echo_error(err, outfd, chattygot);
1374 return err;
1377 const struct got_error *
1378 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1379 int gotd_sock, int chattygot)
1381 const struct got_error *err = NULL;
1383 if (strcmp(command, GOT_DIAL_CMD_FETCH) == 0)
1384 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1385 else if (strcmp(command, GOT_DIAL_CMD_SEND) == 0)
1386 err = serve_write(infd, outfd, gotd_sock, repo_path,
1387 chattygot);
1388 else
1389 err = got_error(GOT_ERR_BAD_PACKET);
1391 return err;