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 wbuf->fd = -1;
412 imsg_close(ibuf, wbuf);
414 return NULL;
417 static const struct got_error *
418 send_capabilities(int *use_sidebands, int *report_status,
419 char *capabilities_str, struct imsgbuf *ibuf)
421 const struct got_error *err = NULL;
422 struct gotd_imsg_capabilities icapas;
423 struct got_capability *capa = NULL;
424 size_t ncapa, i;
426 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
427 capabilities_str);
428 if (err)
429 return err;
431 icapas.ncapabilities = ncapa;
432 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
433 &icapas, sizeof(icapas)) == -1) {
434 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
435 goto done;
438 for (i = 0; i < ncapa; i++) {
439 err = send_capability(&capa[i], ibuf);
440 if (err)
441 goto done;
442 if (use_sidebands &&
443 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
444 *use_sidebands = 1;
445 if (report_status &&
446 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
447 *report_status = 1;
449 done:
450 free(capa);
451 return err;
454 static const struct got_error *
455 forward_flushpkt(struct imsgbuf *ibuf)
457 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
458 return got_error_from_errno("imsg_compose FLUSH");
460 return gotd_imsg_flush(ibuf);
463 static const struct got_error *
464 recv_ack(struct imsg *imsg, uint8_t *expected_id)
466 struct gotd_imsg_ack iack;
467 size_t datalen;
469 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
470 if (datalen != sizeof(iack))
471 return got_error(GOT_ERR_PRIVSEP_LEN);
473 memcpy(&iack, imsg->data, sizeof(iack));
474 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
475 return got_error(GOT_ERR_BAD_OBJ_ID);
477 return NULL;
480 static const struct got_error *
481 recv_nak(struct imsg *imsg, uint8_t *expected_id)
483 struct gotd_imsg_ack inak;
484 size_t datalen;
486 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
487 if (datalen != sizeof(inak))
488 return got_error(GOT_ERR_PRIVSEP_LEN);
490 memcpy(&inak, imsg->data, sizeof(inak));
491 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
492 return got_error(GOT_ERR_BAD_OBJ_ID);
494 return NULL;
498 static const struct got_error *
499 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
500 char *buf, size_t len, int expect_capabilities, int chattygot)
502 const struct got_error *err;
503 struct gotd_imsg_want iwant;
504 char *capabilities_str;
505 int done = 0;
506 struct imsg imsg;
508 memset(&iwant, 0, sizeof(iwant));
509 memset(&imsg, 0, sizeof(imsg));
511 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
512 if (err)
513 return err;
515 if (capabilities_str) {
516 if (!expect_capabilities) {
517 err = got_error_msg(GOT_ERR_BAD_PACKET,
518 "unexpected capability announcement received");
519 goto done;
521 err = send_capabilities(use_sidebands, NULL, capabilities_str,
522 ibuf);
523 if (err)
524 goto done;
528 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
529 &iwant, sizeof(iwant)) == -1) {
530 err = got_error_from_errno("imsg_compose WANT");
531 goto done;
534 err = gotd_imsg_flush(ibuf);
535 if (err)
536 goto done;
538 /*
539 * Wait for an ACK, or an error in case the desired object
540 * does not exist.
541 */
542 while (!done && err == NULL) {
543 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
544 if (err)
545 break;
546 switch (imsg.hdr.type) {
547 case GOTD_IMSG_ERROR:
548 err = gotd_imsg_recv_error(NULL, &imsg);
549 break;
550 case GOTD_IMSG_ACK:
551 err = recv_ack(&imsg, iwant.object_id);
552 if (err)
553 break;
554 done = 1;
555 break;
556 default:
557 err = got_error(GOT_ERR_PRIVSEP_MSG);
558 break;
561 imsg_free(&imsg);
563 done:
564 free(capabilities_str);
565 return err;
568 static const struct got_error *
569 send_ack(int outfd, uint8_t *id, int chattygot)
571 char hex[SHA1_DIGEST_STRING_LENGTH];
572 char buf[GOT_PKT_MAX];
573 int len;
575 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
576 return got_error(GOT_ERR_BAD_OBJ_ID);
578 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
579 if (len >= sizeof(buf))
580 return got_error(GOT_ERR_NO_SPACE);
582 return got_pkt_writepkt(outfd, buf, len, chattygot);
585 static const struct got_error *
586 send_nak(int outfd, int chattygot)
588 char buf[5];
589 int len;
591 len = snprintf(buf, sizeof(buf), "NAK\n");
592 if (len >= sizeof(buf))
593 return got_error(GOT_ERR_NO_SPACE);
595 return got_pkt_writepkt(outfd, buf, len, chattygot);
598 static const struct got_error *
599 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
600 size_t len, int chattygot)
602 const struct got_error *err;
603 struct gotd_imsg_have ihave;
604 int done = 0;
605 struct imsg imsg;
607 memset(&ihave, 0, sizeof(ihave));
608 memset(&imsg, 0, sizeof(imsg));
610 err = parse_have_line(ihave.object_id, buf, len);
611 if (err)
612 return err;
614 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
615 &ihave, sizeof(ihave)) == -1)
616 return got_error_from_errno("imsg_compose HAVE");
618 err = gotd_imsg_flush(ibuf);
619 if (err)
620 return err;
622 /*
623 * Wait for an ACK or a NAK, indicating whether a common
624 * commit object has been found.
625 */
626 while (!done && err == NULL) {
627 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
628 if (err)
629 return err;
630 switch (imsg.hdr.type) {
631 case GOTD_IMSG_ERROR:
632 err = gotd_imsg_recv_error(NULL, &imsg);
633 break;
634 case GOTD_IMSG_ACK:
635 err = recv_ack(&imsg, ihave.object_id);
636 if (err)
637 break;
638 if (!*have_ack) {
639 err = send_ack(outfd, ihave.object_id,
640 chattygot);
641 if (err)
642 return err;
643 *have_ack = 1;
645 done = 1;
646 break;
647 case GOTD_IMSG_NAK:
648 err = recv_nak(&imsg, ihave.object_id);
649 if (err)
650 break;
651 done = 1;
652 break;
653 default:
654 err = got_error(GOT_ERR_PRIVSEP_MSG);
655 break;
658 imsg_free(&imsg);
661 return err;
664 static const struct got_error *
665 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
667 const struct got_error *err;
668 struct imsg imsg;
669 int fd;
671 *packfd = -1;
673 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
674 return got_error_from_errno("imsg_compose DONE");
676 err = gotd_imsg_flush(ibuf);
677 if (err)
678 return err;
680 while (*packfd == -1 && err == NULL) {
681 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
682 if (err)
683 break;
685 switch (imsg.hdr.type) {
686 case GOTD_IMSG_ERROR:
687 err = gotd_imsg_recv_error(NULL, &imsg);
688 break;
689 case GOTD_IMSG_PACKFILE_PIPE:
690 fd = imsg_get_fd(&imsg);
691 if (fd != -1)
692 *packfd = fd;
693 else
694 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
695 break;
696 default:
697 err = got_error(GOT_ERR_PRIVSEP_MSG);
698 break;
701 imsg_free(&imsg);
704 return err;
707 static const struct got_error *
708 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
710 const struct got_error *err = NULL;
711 int pack_starting = 0;
712 struct gotd_imsg_packfile_progress iprog;
713 char buf[GOT_PKT_MAX];
714 struct imsg imsg;
715 size_t datalen;
716 int p_deltify = 0, n;
717 const char *eol = "\r";
719 memset(&imsg, 0, sizeof(imsg));
721 while (!pack_starting && err == NULL) {
722 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
723 if (err)
724 break;
726 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
727 switch (imsg.hdr.type) {
728 case GOTD_IMSG_ERROR:
729 err = gotd_imsg_recv_error(NULL, &imsg);
730 break;
731 case GOTD_IMSG_PACKFILE_READY:
732 eol = "\n";
733 pack_starting = 1;
734 /* fallthrough */
735 case GOTD_IMSG_PACKFILE_PROGRESS:
736 if (datalen != sizeof(iprog)) {
737 err = got_error(GOT_ERR_PRIVSEP_LEN);
738 break;
740 memcpy(&iprog, imsg.data, sizeof(iprog));
741 if (iprog.nobj_total > 0) {
742 p_deltify = (iprog.nobj_deltify * 100) /
743 iprog.nobj_total;
745 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
746 n = snprintf(&buf[1], sizeof(buf) - 1,
747 "%d commits colored, "
748 "%d objects found, "
749 "deltify %d%%%s",
750 iprog.ncolored,
751 iprog.nfound,
752 p_deltify, eol);
753 if (n >= sizeof(buf) - 1)
754 break;
755 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
756 break;
757 default:
758 err = got_error(GOT_ERR_PRIVSEP_MSG);
759 break;
762 imsg_free(&imsg);
765 return err;
768 static const struct got_error *
769 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
770 int chattygot)
772 const struct got_error *err = NULL;
773 char buf[GOT_PKT_MAX];
774 struct imsgbuf ibuf;
775 enum protostate {
776 STATE_EXPECT_WANT,
777 STATE_EXPECT_MORE_WANT,
778 STATE_EXPECT_HAVE,
779 STATE_EXPECT_DONE,
780 STATE_DONE,
781 };
782 enum protostate curstate = STATE_EXPECT_WANT;
783 int have_ack = 0, use_sidebands = 0, seen_have = 0;
784 int packfd = -1;
785 size_t pack_chunksize;
787 imsg_init(&ibuf, gotd_sock);
789 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
790 if (err)
791 goto done;
793 while (curstate != STATE_DONE) {
794 int n;
795 buf[0] = '\0';
796 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
797 if (err)
798 goto done;
799 if (n == 0) {
800 if (curstate != STATE_EXPECT_WANT &&
801 curstate != STATE_EXPECT_MORE_WANT &&
802 curstate != STATE_EXPECT_HAVE &&
803 curstate != STATE_EXPECT_DONE) {
804 err = got_error_msg(GOT_ERR_BAD_PACKET,
805 "unexpected flush packet received");
806 goto done;
809 if (curstate == STATE_EXPECT_WANT) {
810 ssize_t r;
811 /*
812 * If the client does not want to fetch
813 * anything we should receive a flush
814 * packet followed by EOF.
815 */
816 r = read(infd, buf, sizeof(buf));
817 if (r == -1) {
818 err = got_error_from_errno("read");
819 goto done;
821 if (r == 0) /* EOF */
822 goto done;
824 /* Zero-length field followed by payload. */
825 err = got_error_msg(GOT_ERR_BAD_PACKET,
826 "unexpected flush packet received");
827 goto done;
830 if (curstate == STATE_EXPECT_WANT ||
831 curstate == STATE_EXPECT_MORE_WANT ||
832 curstate == STATE_EXPECT_HAVE) {
833 err = forward_flushpkt(&ibuf);
834 if (err)
835 goto done;
837 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
838 err = send_nak(outfd, chattygot);
839 if (err)
840 goto done;
842 if (curstate == STATE_EXPECT_MORE_WANT)
843 curstate = STATE_EXPECT_HAVE;
844 else
845 curstate = STATE_EXPECT_DONE;
846 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
847 if (curstate != STATE_EXPECT_WANT &&
848 curstate != STATE_EXPECT_MORE_WANT) {
849 err = got_error_msg(GOT_ERR_BAD_PACKET,
850 "unexpected 'want' packet");
851 goto done;
853 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
854 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
855 if (err)
856 goto done;
857 if (curstate == STATE_EXPECT_WANT)
858 curstate = STATE_EXPECT_MORE_WANT;
859 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
860 if (curstate != STATE_EXPECT_HAVE &&
861 curstate != STATE_EXPECT_DONE) {
862 err = got_error_msg(GOT_ERR_BAD_PACKET,
863 "unexpected 'have' packet");
864 goto done;
866 if (curstate == STATE_EXPECT_HAVE) {
867 err = recv_have(&have_ack, outfd, &ibuf,
868 buf, n, chattygot);
869 if (err)
870 goto done;
871 seen_have = 1;
873 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
874 if (curstate != STATE_EXPECT_HAVE &&
875 curstate != STATE_EXPECT_DONE) {
876 err = got_error_msg(GOT_ERR_BAD_PACKET,
877 "unexpected 'done' packet");
878 goto done;
880 err = recv_done(&packfd, outfd, &ibuf, chattygot);
881 if (err)
882 goto done;
883 curstate = STATE_DONE;
884 break;
885 } else {
886 err = got_error(GOT_ERR_BAD_PACKET);
887 goto done;
891 if (!seen_have) {
892 err = send_nak(outfd, chattygot);
893 if (err)
894 goto done;
897 if (use_sidebands) {
898 err = relay_progress_reports(&ibuf, outfd, chattygot);
899 if (err)
900 goto done;
901 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
902 } else
903 pack_chunksize = sizeof(buf);
905 for (;;) {
906 ssize_t r;
908 r = read(packfd, use_sidebands ? &buf[1] : buf,
909 pack_chunksize);
910 if (r == -1) {
911 err = got_error_from_errno("read");
912 break;
913 } else if (r == 0) {
914 err = got_pkt_flushpkt(outfd, chattygot);
915 break;
918 if (use_sidebands) {
919 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
920 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
921 if (err)
922 break;
923 } else {
924 err = got_poll_write_full(outfd, buf, r);
925 if (err) {
926 if (err->code == GOT_ERR_EOF)
927 err = NULL;
928 break;
932 done:
933 imsg_clear(&ibuf);
934 if (packfd != -1 && close(packfd) == -1 && err == NULL)
935 err = got_error_from_errno("close");
936 if (err)
937 echo_error(err, outfd, chattygot);
938 return err;
941 static const struct got_error *
942 parse_ref_update_line(char **common_capabilities, char **refname,
943 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
945 const struct got_error *err;
946 char *old_id_str = NULL, *new_id_str = NULL;
947 char *client_capabilities = NULL;
949 *refname = NULL;
951 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
952 refname, &client_capabilities, buf, len);
953 if (err)
954 return err;
956 if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
957 !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
958 err = got_error_msg(GOT_ERR_BAD_PACKET,
959 "ref-update with bad object ID");
960 goto done;
962 if (!got_ref_name_is_valid(*refname)) {
963 err = got_error_msg(GOT_ERR_BAD_PACKET,
964 "ref-update with bad reference name");
965 goto done;
968 if (client_capabilities) {
969 err = got_gitproto_match_capabilities(common_capabilities,
970 NULL, client_capabilities, write_capabilities,
971 nitems(write_capabilities));
972 if (err)
973 goto done;
975 done:
976 free(old_id_str);
977 free(new_id_str);
978 free(client_capabilities);
979 if (err) {
980 free(*refname);
981 *refname = NULL;
983 return err;
986 static const struct got_error *
987 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
988 char *buf, size_t len, int expect_capabilities, int chattygot)
990 const struct got_error *err;
991 struct gotd_imsg_ref_update iref;
992 struct ibuf *wbuf;
993 char *capabilities_str = NULL, *refname = NULL;
994 int done = 0;
995 struct imsg imsg;
997 memset(&iref, 0, sizeof(iref));
998 memset(&imsg, 0, sizeof(imsg));
1000 err = parse_ref_update_line(&capabilities_str, &refname,
1001 iref.old_id, iref.new_id, buf, len);
1002 if (err)
1003 return err;
1005 if (capabilities_str) {
1006 if (!expect_capabilities) {
1007 err = got_error_msg(GOT_ERR_BAD_PACKET,
1008 "unexpected capability announcement received");
1009 goto done;
1011 err = send_capabilities(NULL, report_status, capabilities_str,
1012 ibuf);
1013 if (err)
1014 goto done;
1017 iref.name_len = strlen(refname);
1018 len = sizeof(iref) + iref.name_len;
1019 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1020 if (wbuf == NULL) {
1021 err = got_error_from_errno("imsg_create REF_UPDATE");
1022 goto done;
1025 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1026 return got_error_from_errno("imsg_add REF_UPDATE");
1027 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1028 return got_error_from_errno("imsg_add REF_UPDATE");
1029 wbuf->fd = -1;
1030 imsg_close(ibuf, wbuf);
1032 err = gotd_imsg_flush(ibuf);
1033 if (err)
1034 goto done;
1036 /* Wait for ACK or an error. */
1037 while (!done && err == NULL) {
1038 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1039 if (err)
1040 break;
1041 switch (imsg.hdr.type) {
1042 case GOTD_IMSG_ERROR:
1043 err = gotd_imsg_recv_error(NULL, &imsg);
1044 break;
1045 case GOTD_IMSG_ACK:
1046 err = recv_ack(&imsg, iref.new_id);
1047 if (err)
1048 break;
1049 done = 1;
1050 break;
1051 default:
1052 err = got_error(GOT_ERR_PRIVSEP_MSG);
1053 break;
1056 imsg_free(&imsg);
1058 done:
1059 free(capabilities_str);
1060 free(refname);
1061 return err;
1064 static const struct got_error *
1065 recv_packfile(struct imsg *imsg, int infd)
1067 const struct got_error *err = NULL;
1068 size_t datalen;
1069 int packfd;
1070 char buf[GOT_PKT_MAX];
1071 int pack_done = 0;
1073 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1074 if (datalen != 0)
1075 return got_error(GOT_ERR_PRIVSEP_MSG);
1077 packfd = imsg_get_fd(imsg);
1078 if (packfd == -1)
1079 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1081 while (!pack_done) {
1082 ssize_t r = 0;
1084 err = got_poll_fd(infd, POLLIN, 1);
1085 if (err) {
1086 if (err->code != GOT_ERR_TIMEOUT)
1087 break;
1088 err = NULL;
1089 } else {
1090 r = read(infd, buf, sizeof(buf));
1091 if (r == -1) {
1092 err = got_error_from_errno("read");
1093 break;
1095 if (r == 0) {
1097 * Git clients hang up their side of the
1098 * connection after sending the pack file.
1100 err = NULL;
1101 pack_done = 1;
1102 break;
1106 if (r == 0) {
1107 /* Detect gotd(8) closing the pack pipe when done. */
1108 err = got_poll_fd(packfd, 0, 1);
1109 if (err) {
1110 if (err->code != GOT_ERR_TIMEOUT &&
1111 err->code != GOT_ERR_EOF)
1112 break;
1113 if (err->code == GOT_ERR_EOF)
1114 pack_done = 1;
1115 err = NULL;
1117 } else {
1118 /* Write pack data and/or detect pipe being closed. */
1119 err = got_poll_write_full(packfd, buf, r);
1120 if (err) {
1121 if (err->code == GOT_ERR_EOF)
1122 err = NULL;
1123 break;
1128 close(packfd);
1129 return err;
1132 static const struct got_error *
1133 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1135 const struct got_error *err = NULL;
1136 struct gotd_imsg_packfile_status istatus;
1137 char buf[GOT_PKT_MAX];
1138 size_t datalen, len;
1139 char *reason = NULL;
1141 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1142 if (datalen < sizeof(istatus))
1143 return got_error(GOT_ERR_PRIVSEP_LEN);
1144 memcpy(&istatus, imsg->data, sizeof(istatus));
1145 if (datalen != sizeof(istatus) + istatus.reason_len)
1146 return got_error(GOT_ERR_PRIVSEP_LEN);
1148 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1149 if (reason == NULL) {
1150 err = got_error_from_errno("strndup");
1151 goto done;
1154 if (err == NULL)
1155 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1156 else
1157 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1158 if (len >= sizeof(buf)) {
1159 err = got_error(GOT_ERR_NO_SPACE);
1160 goto done;
1163 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1164 done:
1165 free(reason);
1166 return err;
1169 static const struct got_error *
1170 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1172 const struct got_error *err = NULL;
1173 struct gotd_imsg_ref_update_ok iok;
1174 size_t datalen, len;
1175 char buf[GOT_PKT_MAX];
1176 char *refname = NULL;
1178 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1179 if (datalen < sizeof(iok))
1180 return got_error(GOT_ERR_PRIVSEP_LEN);
1181 memcpy(&iok, imsg->data, sizeof(iok));
1182 if (datalen != sizeof(iok) + iok.name_len)
1183 return got_error(GOT_ERR_PRIVSEP_LEN);
1185 memcpy(&iok, imsg->data, sizeof(iok));
1187 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1188 if (refname == NULL)
1189 return got_error_from_errno("strndup");
1191 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1192 if (len >= sizeof(buf)) {
1193 err = got_error(GOT_ERR_NO_SPACE);
1194 goto done;
1197 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1198 done:
1199 free(refname);
1200 return err;
1203 static const struct got_error *
1204 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1206 const struct got_error *err = NULL;
1207 struct gotd_imsg_ref_update_ng ing;
1208 size_t datalen, len;
1209 char buf[GOT_PKT_MAX];
1210 char *refname = NULL, *reason = NULL;
1212 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1213 if (datalen < sizeof(ing))
1214 return got_error(GOT_ERR_PRIVSEP_LEN);
1215 memcpy(&ing, imsg->data, sizeof(ing));
1216 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1217 return got_error(GOT_ERR_PRIVSEP_LEN);
1219 memcpy(&ing, imsg->data, sizeof(ing));
1221 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1222 if (refname == NULL)
1223 return got_error_from_errno("strndup");
1225 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1226 ing.reason_len);
1227 if (reason == NULL) {
1228 err = got_error_from_errno("strndup");
1229 goto done;
1232 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1233 if (len >= sizeof(buf)) {
1234 err = got_error(GOT_ERR_NO_SPACE);
1235 goto done;
1238 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1239 done:
1240 free(refname);
1241 free(reason);
1242 return err;
1245 static const struct got_error *
1246 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1247 int chattygot)
1249 const struct got_error *err = NULL;
1250 char buf[GOT_PKT_MAX];
1251 struct imsgbuf ibuf;
1252 enum protostate {
1253 STATE_EXPECT_REF_UPDATE,
1254 STATE_EXPECT_MORE_REF_UPDATES,
1255 STATE_EXPECT_PACKFILE,
1256 STATE_PACKFILE_RECEIVED,
1257 STATE_REFS_UPDATED,
1259 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1260 struct imsg imsg;
1261 int report_status = 0;
1263 imsg_init(&ibuf, gotd_sock);
1264 memset(&imsg, 0, sizeof(imsg));
1266 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1267 if (err)
1268 goto done;
1270 while (curstate != STATE_EXPECT_PACKFILE) {
1271 int n;
1272 buf[0] = '\0';
1273 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1274 if (err)
1275 goto done;
1276 if (n == 0) {
1277 if (curstate == STATE_EXPECT_REF_UPDATE) {
1278 /* The client will not send us anything. */
1279 goto done;
1280 } else if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1281 err = got_error_msg(GOT_ERR_BAD_PACKET,
1282 "unexpected flush packet received");
1283 goto done;
1285 err = forward_flushpkt(&ibuf);
1286 if (err)
1287 goto done;
1288 curstate = STATE_EXPECT_PACKFILE;
1289 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1290 if (curstate != STATE_EXPECT_REF_UPDATE &&
1291 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1292 err = got_error_msg(GOT_ERR_BAD_PACKET,
1293 "unexpected ref-update packet");
1294 goto done;
1296 if (curstate == STATE_EXPECT_REF_UPDATE) {
1297 err = recv_ref_update(&report_status,
1298 outfd, &ibuf, buf, n, 1, chattygot);
1299 } else {
1300 err = recv_ref_update(NULL, outfd, &ibuf,
1301 buf, n, 0, chattygot);
1303 if (err)
1304 goto done;
1305 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1306 } else {
1307 err = got_error(GOT_ERR_BAD_PACKET);
1308 goto done;
1312 while (curstate != STATE_PACKFILE_RECEIVED) {
1313 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1314 if (err)
1315 goto done;
1316 switch (imsg.hdr.type) {
1317 case GOTD_IMSG_ERROR:
1318 err = gotd_imsg_recv_error(NULL, &imsg);
1319 goto done;
1320 case GOTD_IMSG_PACKFILE_PIPE:
1321 err = recv_packfile(&imsg, infd);
1322 if (err) {
1323 if (err->code != GOT_ERR_EOF)
1324 goto done;
1326 * EOF is reported when the client hangs up,
1327 * which can happen with Git clients.
1328 * The socket should stay half-open so we
1329 * can still send our reports if requested.
1331 err = NULL;
1333 curstate = STATE_PACKFILE_RECEIVED;
1334 break;
1335 default:
1336 err = got_error(GOT_ERR_PRIVSEP_MSG);
1337 break;
1340 imsg_free(&imsg);
1341 if (err)
1342 goto done;
1345 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1346 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1347 if (err)
1348 break;
1349 switch (imsg.hdr.type) {
1350 case GOTD_IMSG_ERROR:
1351 err = gotd_imsg_recv_error(NULL, &imsg);
1352 break;
1353 case GOTD_IMSG_PACKFILE_STATUS:
1354 if (!report_status)
1355 break;
1356 err = report_unpack_status(&imsg, outfd, chattygot);
1357 break;
1358 case GOTD_IMSG_REF_UPDATE_OK:
1359 if (!report_status)
1360 break;
1361 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1362 break;
1363 case GOTD_IMSG_REF_UPDATE_NG:
1364 if (!report_status)
1365 break;
1366 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1367 break;
1368 case GOTD_IMSG_REFS_UPDATED:
1369 curstate = STATE_REFS_UPDATED;
1370 err = got_pkt_flushpkt(outfd, chattygot);
1371 break;
1372 default:
1373 err = got_error(GOT_ERR_PRIVSEP_MSG);
1374 break;
1377 imsg_free(&imsg);
1379 done:
1380 imsg_clear(&ibuf);
1381 if (err)
1382 echo_error(err, outfd, chattygot);
1383 return err;
1386 const struct got_error *
1387 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1388 int gotd_sock, int chattygot)
1390 const struct got_error *err = NULL;
1392 if (strcmp(command, GOT_DIAL_CMD_FETCH) == 0)
1393 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1394 else if (strcmp(command, GOT_DIAL_CMD_SEND) == 0)
1395 err = serve_write(infd, outfd, gotd_sock, repo_path,
1396 chattygot);
1397 else
1398 err = got_error(GOT_ERR_BAD_PACKET);
1400 return err;