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 <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <imsg.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_serve.h"
34 #include "got_path.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"
45 #include "gotd.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
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 },
55 };
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 },
63 };
65 static const struct got_error *
66 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
67 uint8_t *buf, size_t bufsize)
68 {
69 struct got_capability capa[nitems(read_capabilities) + 1];
70 size_t ncapa;
72 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
73 if (symrefstr) {
74 capa[nitems(read_capabilities)].key = "symref";
75 capa[nitems(read_capabilities)].value = symrefstr;
76 ncapa = nitems(capa);
77 } else
78 ncapa = nitems(read_capabilities);
80 return got_gitproto_append_capabilities(capalen, buf, len,
81 bufsize, capa, ncapa);
82 }
84 static const struct got_error *
85 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
86 int client_is_reading, const char *symrefstr, int chattygot)
87 {
88 const struct got_error *err = NULL;
89 char hex[SHA1_DIGEST_STRING_LENGTH];
90 char buf[GOT_PKT_MAX];
91 size_t len, capalen = 0;
93 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
94 return got_error(GOT_ERR_BAD_OBJ_ID);
96 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
97 if (len >= sizeof(buf))
98 return got_error(GOT_ERR_NO_SPACE);
100 if (send_capabilities) {
101 if (client_is_reading) {
102 err = append_read_capabilities(&capalen, len,
103 symrefstr, buf, sizeof(buf));
104 } else {
105 err = got_gitproto_append_capabilities(&capalen,
106 buf, len, sizeof(buf), write_capabilities,
107 nitems(write_capabilities));
109 if (err)
110 return err;
111 len += capalen;
114 if (len + 1 >= sizeof(buf))
115 return got_error(GOT_ERR_NO_SPACE);
116 buf[len] = '\n';
117 len++;
118 buf[len] = '\0';
120 return got_pkt_writepkt(outfd, buf, len, chattygot);
123 static const struct got_error *
124 send_zero_refs(int outfd, int client_is_reading, int chattygot)
126 const struct got_error *err = NULL;
127 const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
128 char buf[GOT_PKT_MAX];
129 size_t len, capalen = 0;
131 len = strlcpy(buf, line, sizeof(buf));
132 if (len >= sizeof(buf))
133 return got_error(GOT_ERR_NO_SPACE);
135 if (client_is_reading) {
136 err = got_gitproto_append_capabilities(&capalen, buf, len,
137 sizeof(buf), read_capabilities, nitems(read_capabilities));
138 if (err)
139 return err;
140 } else {
141 err = got_gitproto_append_capabilities(&capalen, buf, len,
142 sizeof(buf), write_capabilities,
143 nitems(write_capabilities));
144 if (err)
145 return err;
148 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
151 static void
152 echo_error(const struct got_error *err, int outfd, int chattygot)
154 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
155 size_t len;
157 /*
158 * Echo the error to the client on a pkt-line.
159 * The client should then terminate its session.
160 */
161 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
162 len = strlcat(buf, err->msg, sizeof(buf));
163 got_pkt_writepkt(outfd, buf, len, chattygot);
166 static const struct got_error *
167 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
168 const char *repo_path, int chattygot)
170 const struct got_error *err = NULL;
171 struct imsg imsg;
172 size_t datalen;
173 struct gotd_imsg_list_refs lsref;
174 struct gotd_imsg_reflist ireflist;
175 struct gotd_imsg_ref iref;
176 struct gotd_imsg_symref isymref;
177 size_t nrefs = 0;
178 int have_nrefs = 0, sent_capabilities = 0;
179 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
180 char *refname = NULL;
182 memset(&imsg, 0, sizeof(imsg));
183 memset(&lsref, 0, sizeof(lsref));
185 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
186 sizeof(lsref.repo_name))
187 return got_error(GOT_ERR_NO_SPACE);
188 lsref.client_is_reading = client_is_reading;
190 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
191 &lsref, sizeof(lsref)) == -1)
192 return got_error_from_errno("imsg_compose LIST_REFS");
194 err = gotd_imsg_flush(ibuf);
195 if (err)
196 return err;
198 while (!have_nrefs || nrefs > 0) {
199 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
200 if (err)
201 goto done;
202 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
203 switch (imsg.hdr.type) {
204 case GOTD_IMSG_ERROR:
205 err = gotd_imsg_recv_error(NULL, &imsg);
206 goto done;
207 case GOTD_IMSG_REFLIST:
208 if (have_nrefs || nrefs > 0) {
209 err = got_error(GOT_ERR_PRIVSEP_MSG);
210 goto done;
212 if (datalen != sizeof(ireflist)) {
213 err = got_error(GOT_ERR_PRIVSEP_MSG);
214 goto done;
216 memcpy(&ireflist, imsg.data, sizeof(ireflist));
217 nrefs = ireflist.nrefs;
218 have_nrefs = 1;
219 if (nrefs == 0)
220 err = send_zero_refs(outfd, client_is_reading,
221 chattygot);
222 break;
223 case GOTD_IMSG_REF:
224 if (!have_nrefs || nrefs == 0) {
225 err = got_error(GOT_ERR_PRIVSEP_MSG);
226 goto done;
228 if (datalen < sizeof(iref)) {
229 err = got_error(GOT_ERR_PRIVSEP_MSG);
230 goto done;
232 memcpy(&iref, imsg.data, sizeof(iref));
233 if (datalen != sizeof(iref) + iref.name_len) {
234 err = got_error(GOT_ERR_PRIVSEP_LEN);
235 goto done;
237 refname = strndup(imsg.data + sizeof(iref),
238 iref.name_len);
239 if (refname == NULL) {
240 err = got_error_from_errno("strndup");
241 goto done;
243 err = send_ref(outfd, iref.id, refname,
244 !sent_capabilities, client_is_reading,
245 NULL, chattygot);
246 free(refname);
247 refname = NULL;
248 if (err)
249 goto done;
250 sent_capabilities = 1;
251 if (nrefs > 0)
252 nrefs--;
253 break;
254 case GOTD_IMSG_SYMREF:
255 if (!have_nrefs || nrefs == 0) {
256 err = got_error(GOT_ERR_PRIVSEP_MSG);
257 goto done;
259 if (datalen < sizeof(isymref)) {
260 err = got_error(GOT_ERR_PRIVSEP_LEN);
261 goto done;
263 memcpy(&isymref, imsg.data, sizeof(isymref));
264 if (datalen != sizeof(isymref) + isymref.name_len +
265 isymref.target_len) {
266 err = got_error(GOT_ERR_PRIVSEP_LEN);
267 goto done;
270 /*
271 * For now, we only announce one symbolic ref,
272 * as part of our capability advertisement.
273 */
274 if (sent_capabilities || symrefstr != NULL ||
275 symrefname != NULL || symreftarget != NULL)
276 break;
278 symrefname = strndup(imsg.data + sizeof(isymref),
279 isymref.name_len);
280 if (symrefname == NULL) {
281 err = got_error_from_errno("malloc");
282 goto done;
285 symreftarget = strndup(
286 imsg.data + sizeof(isymref) + isymref.name_len,
287 isymref.target_len);
288 if (symreftarget == NULL) {
289 err = got_error_from_errno("strndup");
290 goto done;
293 if (asprintf(&symrefstr, "%s:%s", symrefname,
294 symreftarget) == -1) {
295 err = got_error_from_errno("asprintf");
296 goto done;
298 err = send_ref(outfd, isymref.target_id, symrefname,
299 !sent_capabilities, client_is_reading, symrefstr,
300 chattygot);
301 free(refname);
302 refname = NULL;
303 if (err)
304 goto done;
305 sent_capabilities = 1;
306 if (nrefs > 0)
307 nrefs--;
308 break;
309 default:
310 err = got_error(GOT_ERR_PRIVSEP_MSG);
311 break;
314 imsg_free(&imsg);
317 err = got_pkt_flushpkt(outfd, chattygot);
318 if (err)
319 goto done;
320 done:
321 free(symrefstr);
322 free(symrefname);
323 free(symreftarget);
324 return err;
327 static const struct got_error *
328 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
330 const struct got_error *err;
331 char *id_str = NULL, *client_capabilities = NULL;
333 err = got_gitproto_parse_want_line(&id_str,
334 &client_capabilities, buf, len);
335 if (err)
336 return err;
338 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
339 err = got_error_msg(GOT_ERR_BAD_PACKET,
340 "want-line with bad object ID");
341 goto done;
344 if (client_capabilities) {
345 err = got_gitproto_match_capabilities(common_capabilities,
346 NULL, client_capabilities, read_capabilities,
347 nitems(read_capabilities));
348 if (err)
349 goto done;
351 done:
352 free(id_str);
353 free(client_capabilities);
354 return err;
357 static const struct got_error *
358 parse_have_line(uint8_t *id, char *buf, size_t len)
360 const struct got_error *err;
361 char *id_str = NULL;
363 err = got_gitproto_parse_have_line(&id_str, buf, len);
364 if (err)
365 return err;
367 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
368 err = got_error_msg(GOT_ERR_BAD_PACKET,
369 "have-line with bad object ID");
370 goto done;
372 done:
373 free(id_str);
374 return err;
377 static const struct got_error *
378 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
380 const struct got_error *err = NULL;
381 struct gotd_imsg_capability icapa;
382 size_t len;
383 struct ibuf *wbuf;
385 memset(&icapa, 0, sizeof(icapa));
387 icapa.key_len = strlen(capa->key);
388 len = sizeof(icapa) + icapa.key_len;
389 if (capa->value) {
390 icapa.value_len = strlen(capa->value);
391 len += icapa.value_len;
394 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
395 if (wbuf == NULL) {
396 err = got_error_from_errno("imsg_create CAPABILITY");
397 return err;
400 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
401 return got_error_from_errno("imsg_add CAPABILITY");
402 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
403 return got_error_from_errno("imsg_add CAPABILITY");
404 if (capa->value) {
405 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
406 return got_error_from_errno("imsg_add CAPABILITY");
409 wbuf->fd = -1;
410 imsg_close(ibuf, wbuf);
412 return NULL;
415 static const struct got_error *
416 send_capabilities(int *use_sidebands, int *report_status,
417 char *capabilities_str, struct imsgbuf *ibuf)
419 const struct got_error *err = NULL;
420 struct gotd_imsg_capabilities icapas;
421 struct got_capability *capa = NULL;
422 size_t ncapa, i;
424 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
425 capabilities_str);
426 if (err)
427 return err;
429 icapas.ncapabilities = ncapa;
430 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
431 &icapas, sizeof(icapas)) == -1) {
432 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
433 goto done;
436 for (i = 0; i < ncapa; i++) {
437 err = send_capability(&capa[i], ibuf);
438 if (err)
439 goto done;
440 if (use_sidebands &&
441 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
442 *use_sidebands = 1;
443 if (report_status &&
444 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
445 *report_status = 1;
447 done:
448 free(capa);
449 return err;
452 static const struct got_error *
453 forward_flushpkt(struct imsgbuf *ibuf)
455 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
456 return got_error_from_errno("imsg_compose FLUSH");
458 return gotd_imsg_flush(ibuf);
461 static const struct got_error *
462 recv_ack(struct imsg *imsg, uint8_t *expected_id)
464 struct gotd_imsg_ack iack;
465 size_t datalen;
467 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
468 if (datalen != sizeof(iack))
469 return got_error(GOT_ERR_PRIVSEP_LEN);
471 memcpy(&iack, imsg->data, sizeof(iack));
472 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
473 return got_error(GOT_ERR_BAD_OBJ_ID);
475 return NULL;
478 static const struct got_error *
479 recv_nak(struct imsg *imsg, uint8_t *expected_id)
481 struct gotd_imsg_ack inak;
482 size_t datalen;
484 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
485 if (datalen != sizeof(inak))
486 return got_error(GOT_ERR_PRIVSEP_LEN);
488 memcpy(&inak, imsg->data, sizeof(inak));
489 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
490 return got_error(GOT_ERR_BAD_OBJ_ID);
492 return NULL;
496 static const struct got_error *
497 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
498 char *buf, size_t len, int expect_capabilities, int chattygot)
500 const struct got_error *err;
501 struct gotd_imsg_want iwant;
502 char *capabilities_str;
503 int done = 0;
504 struct imsg imsg;
506 memset(&iwant, 0, sizeof(iwant));
507 memset(&imsg, 0, sizeof(imsg));
509 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
510 if (err)
511 return err;
513 if (capabilities_str) {
514 if (!expect_capabilities) {
515 err = got_error_msg(GOT_ERR_BAD_PACKET,
516 "unexpected capability announcement received");
517 goto done;
519 err = send_capabilities(use_sidebands, NULL, capabilities_str,
520 ibuf);
521 if (err)
522 goto done;
526 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
527 &iwant, sizeof(iwant)) == -1) {
528 err = got_error_from_errno("imsg_compose WANT");
529 goto done;
532 err = gotd_imsg_flush(ibuf);
533 if (err)
534 goto done;
536 /*
537 * Wait for an ACK, or an error in case the desired object
538 * does not exist.
539 */
540 while (!done && err == NULL) {
541 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
542 if (err)
543 break;
544 switch (imsg.hdr.type) {
545 case GOTD_IMSG_ERROR:
546 err = gotd_imsg_recv_error(NULL, &imsg);
547 break;
548 case GOTD_IMSG_ACK:
549 err = recv_ack(&imsg, iwant.object_id);
550 if (err)
551 break;
552 done = 1;
553 break;
554 default:
555 err = got_error(GOT_ERR_PRIVSEP_MSG);
556 break;
559 imsg_free(&imsg);
561 done:
562 free(capabilities_str);
563 return err;
566 static const struct got_error *
567 send_ack(int outfd, uint8_t *id, int chattygot)
569 char hex[SHA1_DIGEST_STRING_LENGTH];
570 char buf[GOT_PKT_MAX];
571 int len;
573 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
574 return got_error(GOT_ERR_BAD_OBJ_ID);
576 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
577 if (len >= sizeof(buf))
578 return got_error(GOT_ERR_NO_SPACE);
580 return got_pkt_writepkt(outfd, buf, len, chattygot);
583 static const struct got_error *
584 send_nak(int outfd, int chattygot)
586 char buf[5];
587 int len;
589 len = snprintf(buf, sizeof(buf), "NAK\n");
590 if (len >= sizeof(buf))
591 return got_error(GOT_ERR_NO_SPACE);
593 return got_pkt_writepkt(outfd, buf, len, chattygot);
596 static const struct got_error *
597 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
598 size_t len, int chattygot)
600 const struct got_error *err;
601 struct gotd_imsg_have ihave;
602 int done = 0;
603 struct imsg imsg;
605 memset(&ihave, 0, sizeof(ihave));
606 memset(&imsg, 0, sizeof(imsg));
608 err = parse_have_line(ihave.object_id, buf, len);
609 if (err)
610 return err;
612 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
613 &ihave, sizeof(ihave)) == -1)
614 return got_error_from_errno("imsg_compose HAVE");
616 err = gotd_imsg_flush(ibuf);
617 if (err)
618 return err;
620 /*
621 * Wait for an ACK or a NAK, indicating whether a common
622 * commit object has been found.
623 */
624 while (!done && err == NULL) {
625 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
626 if (err)
627 return err;
628 switch (imsg.hdr.type) {
629 case GOTD_IMSG_ERROR:
630 err = gotd_imsg_recv_error(NULL, &imsg);
631 break;
632 case GOTD_IMSG_ACK:
633 err = recv_ack(&imsg, ihave.object_id);
634 if (err)
635 break;
636 if (!*have_ack) {
637 err = send_ack(outfd, ihave.object_id,
638 chattygot);
639 if (err)
640 return err;
641 *have_ack = 1;
643 done = 1;
644 break;
645 case GOTD_IMSG_NAK:
646 err = recv_nak(&imsg, ihave.object_id);
647 if (err)
648 break;
649 done = 1;
650 break;
651 default:
652 err = got_error(GOT_ERR_PRIVSEP_MSG);
653 break;
656 imsg_free(&imsg);
659 return err;
662 static const struct got_error *
663 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
665 const struct got_error *err;
666 struct imsg imsg;
668 *packfd = -1;
670 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
671 return got_error_from_errno("imsg_compose DONE");
673 err = gotd_imsg_flush(ibuf);
674 if (err)
675 return err;
677 while (*packfd == -1 && err == NULL) {
678 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
679 if (err)
680 break;
682 switch (imsg.hdr.type) {
683 case GOTD_IMSG_ERROR:
684 err = gotd_imsg_recv_error(NULL, &imsg);
685 break;
686 case GOTD_IMSG_PACKFILE_PIPE:
687 if (imsg.fd != -1)
688 *packfd = imsg.fd;
689 else
690 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
691 break;
692 default:
693 err = got_error(GOT_ERR_PRIVSEP_MSG);
694 break;
697 imsg_free(&imsg);
700 return err;
703 static const struct got_error *
704 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
706 const struct got_error *err = NULL;
707 int pack_starting = 0;
708 struct gotd_imsg_packfile_progress iprog;
709 char buf[GOT_PKT_MAX];
710 struct imsg imsg;
711 size_t datalen;
712 int p_deltify = 0, n;
713 const char *eol = "\r";
715 memset(&imsg, 0, sizeof(imsg));
717 while (!pack_starting && err == NULL) {
718 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
719 if (err)
720 break;
722 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
723 switch (imsg.hdr.type) {
724 case GOTD_IMSG_ERROR:
725 err = gotd_imsg_recv_error(NULL, &imsg);
726 break;
727 case GOTD_IMSG_PACKFILE_READY:
728 eol = "\n";
729 pack_starting = 1;
730 /* fallthrough */
731 case GOTD_IMSG_PACKFILE_PROGRESS:
732 if (datalen != sizeof(iprog)) {
733 err = got_error(GOT_ERR_PRIVSEP_LEN);
734 break;
736 memcpy(&iprog, imsg.data, sizeof(iprog));
737 if (iprog.nobj_total > 0) {
738 p_deltify = (iprog.nobj_deltify * 100) /
739 iprog.nobj_total;
741 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
742 n = snprintf(&buf[1], sizeof(buf) - 1,
743 "%d commits colored, "
744 "%d objects found, "
745 "deltify %d%%%s",
746 iprog.ncolored,
747 iprog.nfound,
748 p_deltify, eol);
749 if (n >= sizeof(buf) - 1)
750 break;
751 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
752 break;
753 default:
754 err = got_error(GOT_ERR_PRIVSEP_MSG);
755 break;
758 imsg_free(&imsg);
761 return err;
764 static const struct got_error *
765 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
766 int chattygot)
768 const struct got_error *err = NULL;
769 char buf[GOT_PKT_MAX];
770 struct imsgbuf ibuf;
771 enum protostate {
772 STATE_EXPECT_WANT,
773 STATE_EXPECT_MORE_WANT,
774 STATE_EXPECT_HAVE,
775 STATE_EXPECT_DONE,
776 STATE_DONE,
777 };
778 enum protostate curstate = STATE_EXPECT_WANT;
779 int have_ack = 0, use_sidebands = 0, seen_have = 0;
780 int packfd = -1;
781 size_t pack_chunksize;
783 imsg_init(&ibuf, gotd_sock);
785 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
786 if (err)
787 goto done;
789 while (curstate != STATE_DONE) {
790 int n;
791 buf[0] = '\0';
792 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
793 if (err)
794 goto done;
795 if (n == 0) {
796 if (curstate != STATE_EXPECT_WANT &&
797 curstate != STATE_EXPECT_MORE_WANT &&
798 curstate != STATE_EXPECT_HAVE &&
799 curstate != STATE_EXPECT_DONE) {
800 err = got_error_msg(GOT_ERR_BAD_PACKET,
801 "unexpected flush packet received");
802 goto done;
805 if (curstate == STATE_EXPECT_WANT) {
806 ssize_t r;
807 /*
808 * If the client does not want to fetch
809 * anything we should receive a flush
810 * packet followed by EOF.
811 */
812 r = read(infd, buf, sizeof(buf));
813 if (r == -1) {
814 err = got_error_from_errno("read");
815 goto done;
817 if (r == 0) /* EOF */
818 goto done;
820 /* Zero-length field followed by payload. */
821 err = got_error_msg(GOT_ERR_BAD_PACKET,
822 "unexpected flush packet received");
823 goto done;
826 if (curstate == STATE_EXPECT_WANT ||
827 curstate == STATE_EXPECT_MORE_WANT ||
828 curstate == STATE_EXPECT_HAVE) {
829 err = forward_flushpkt(&ibuf);
830 if (err)
831 goto done;
833 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
834 err = send_nak(outfd, chattygot);
835 if (err)
836 goto done;
838 if (curstate == STATE_EXPECT_MORE_WANT)
839 curstate = STATE_EXPECT_HAVE;
840 else
841 curstate = STATE_EXPECT_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 &&
857 curstate != STATE_EXPECT_DONE) {
858 err = got_error_msg(GOT_ERR_BAD_PACKET,
859 "unexpected 'have' packet");
860 goto done;
862 if (curstate == STATE_EXPECT_HAVE) {
863 err = recv_have(&have_ack, outfd, &ibuf,
864 buf, n, chattygot);
865 if (err)
866 goto done;
867 seen_have = 1;
869 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
870 if (curstate != STATE_EXPECT_HAVE &&
871 curstate != STATE_EXPECT_DONE) {
872 err = got_error_msg(GOT_ERR_BAD_PACKET,
873 "unexpected 'done' packet");
874 goto done;
876 err = recv_done(&packfd, outfd, &ibuf, chattygot);
877 if (err)
878 goto done;
879 curstate = STATE_DONE;
880 break;
881 } else {
882 err = got_error(GOT_ERR_BAD_PACKET);
883 goto done;
887 if (!seen_have) {
888 err = send_nak(outfd, chattygot);
889 if (err)
890 goto done;
893 if (use_sidebands) {
894 err = relay_progress_reports(&ibuf, outfd, chattygot);
895 if (err)
896 goto done;
897 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
898 } else
899 pack_chunksize = sizeof(buf);
901 for (;;) {
902 ssize_t r;
904 r = read(packfd, use_sidebands ? &buf[1] : buf,
905 pack_chunksize);
906 if (r == -1) {
907 err = got_error_from_errno("read");
908 break;
909 } else if (r == 0) {
910 err = got_pkt_flushpkt(outfd, chattygot);
911 break;
914 if (use_sidebands) {
915 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
916 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
917 if (err)
918 break;
919 } else {
920 err = got_poll_write_full(outfd, buf, r);
921 if (err) {
922 if (err->code == GOT_ERR_EOF)
923 err = NULL;
924 break;
928 done:
929 imsg_clear(&ibuf);
930 if (packfd != -1 && close(packfd) == -1 && err == NULL)
931 err = got_error_from_errno("close");
932 if (err)
933 echo_error(err, outfd, chattygot);
934 return err;
937 static const struct got_error *
938 parse_ref_update_line(char **common_capabilities, char **refname,
939 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
941 const struct got_error *err;
942 char *old_id_str = NULL, *new_id_str = NULL;
943 char *client_capabilities = NULL;
945 *refname = NULL;
947 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
948 refname, &client_capabilities, buf, len);
949 if (err)
950 return err;
952 if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
953 !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
954 err = got_error_msg(GOT_ERR_BAD_PACKET,
955 "ref-update with bad object ID");
956 goto done;
958 if (!got_ref_name_is_valid(*refname)) {
959 err = got_error_msg(GOT_ERR_BAD_PACKET,
960 "ref-update with bad reference name");
961 goto done;
964 if (client_capabilities) {
965 err = got_gitproto_match_capabilities(common_capabilities,
966 NULL, client_capabilities, write_capabilities,
967 nitems(write_capabilities));
968 if (err)
969 goto done;
971 done:
972 free(old_id_str);
973 free(new_id_str);
974 free(client_capabilities);
975 if (err) {
976 free(*refname);
977 *refname = NULL;
979 return err;
982 static const struct got_error *
983 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
984 char *buf, size_t len, int expect_capabilities, int chattygot)
986 const struct got_error *err;
987 struct gotd_imsg_ref_update iref;
988 struct ibuf *wbuf;
989 char *capabilities_str = NULL, *refname = NULL;
990 int done = 0;
991 struct imsg imsg;
993 memset(&iref, 0, sizeof(iref));
994 memset(&imsg, 0, sizeof(imsg));
996 err = parse_ref_update_line(&capabilities_str, &refname,
997 iref.old_id, iref.new_id, buf, len);
998 if (err)
999 return err;
1001 if (capabilities_str) {
1002 if (!expect_capabilities) {
1003 err = got_error_msg(GOT_ERR_BAD_PACKET,
1004 "unexpected capability announcement received");
1005 goto done;
1007 err = send_capabilities(NULL, report_status, capabilities_str,
1008 ibuf);
1009 if (err)
1010 goto done;
1013 iref.name_len = strlen(refname);
1014 len = sizeof(iref) + iref.name_len;
1015 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1016 if (wbuf == NULL) {
1017 err = got_error_from_errno("imsg_create REF_UPDATE");
1018 goto done;
1021 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1022 return got_error_from_errno("imsg_add REF_UPDATE");
1023 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1024 return got_error_from_errno("imsg_add REF_UPDATE");
1025 wbuf->fd = -1;
1026 imsg_close(ibuf, wbuf);
1028 err = gotd_imsg_flush(ibuf);
1029 if (err)
1030 goto done;
1032 /* Wait for ACK or an error. */
1033 while (!done && err == NULL) {
1034 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1035 if (err)
1036 break;
1037 switch (imsg.hdr.type) {
1038 case GOTD_IMSG_ERROR:
1039 err = gotd_imsg_recv_error(NULL, &imsg);
1040 break;
1041 case GOTD_IMSG_ACK:
1042 err = recv_ack(&imsg, iref.new_id);
1043 if (err)
1044 break;
1045 done = 1;
1046 break;
1047 default:
1048 err = got_error(GOT_ERR_PRIVSEP_MSG);
1049 break;
1052 imsg_free(&imsg);
1054 done:
1055 free(capabilities_str);
1056 free(refname);
1057 return err;
1060 static const struct got_error *
1061 recv_packfile(struct imsg *imsg, int infd)
1063 const struct got_error *err = NULL;
1064 size_t datalen;
1065 int packfd;
1066 char buf[GOT_PKT_MAX];
1067 int pack_done = 0;
1069 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1070 if (datalen != 0)
1071 return got_error(GOT_ERR_PRIVSEP_MSG);
1073 if (imsg->fd == -1)
1074 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1076 packfd = imsg->fd;
1077 while (!pack_done) {
1078 ssize_t r = 0;
1080 err = got_poll_fd(infd, POLLIN, 1);
1081 if (err) {
1082 if (err->code != GOT_ERR_TIMEOUT)
1083 break;
1084 err = NULL;
1085 } else {
1086 r = read(infd, buf, sizeof(buf));
1087 if (r == -1) {
1088 err = got_error_from_errno("read");
1089 break;
1091 if (r == 0) {
1093 * Git clients hang up their side of the
1094 * connection after sending the pack file.
1096 err = NULL;
1097 pack_done = 1;
1098 break;
1102 if (r == 0) {
1103 /* Detect gotd(8) closing the pack pipe when done. */
1104 err = got_poll_fd(packfd, POLLOUT, 1);
1105 if (err) {
1106 if (err->code != GOT_ERR_EOF)
1107 break;
1108 err = NULL;
1109 pack_done = 1;
1111 } else {
1112 /* Write pack data and/or detect pipe being closed. */
1113 err = got_poll_write_full(packfd, buf, r);
1114 if (err) {
1115 if (err->code == GOT_ERR_EOF)
1116 err = NULL;
1117 break;
1122 close(packfd);
1123 return err;
1126 static const struct got_error *
1127 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1129 const struct got_error *err = NULL;
1130 struct gotd_imsg_packfile_status istatus;
1131 char buf[GOT_PKT_MAX];
1132 size_t datalen, len;
1133 char *reason = NULL;
1135 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1136 if (datalen < sizeof(istatus))
1137 return got_error(GOT_ERR_PRIVSEP_LEN);
1138 memcpy(&istatus, imsg->data, sizeof(istatus));
1139 if (datalen != sizeof(istatus) + istatus.reason_len)
1140 return got_error(GOT_ERR_PRIVSEP_LEN);
1142 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1143 if (reason == NULL) {
1144 err = got_error_from_errno("strndup");
1145 goto done;
1148 if (err == NULL)
1149 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1150 else
1151 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1152 if (len >= sizeof(buf)) {
1153 err = got_error(GOT_ERR_NO_SPACE);
1154 goto done;
1157 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1158 done:
1159 free(reason);
1160 return err;
1163 static const struct got_error *
1164 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1166 const struct got_error *err = NULL;
1167 struct gotd_imsg_ref_update_ok iok;
1168 size_t datalen, len;
1169 char buf[GOT_PKT_MAX];
1170 char *refname = NULL;
1172 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1173 if (datalen < sizeof(iok))
1174 return got_error(GOT_ERR_PRIVSEP_LEN);
1175 memcpy(&iok, imsg->data, sizeof(iok));
1176 if (datalen != sizeof(iok) + iok.name_len)
1177 return got_error(GOT_ERR_PRIVSEP_LEN);
1179 memcpy(&iok, imsg->data, sizeof(iok));
1181 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1182 if (refname == NULL)
1183 return got_error_from_errno("strndup");
1185 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1186 if (len >= sizeof(buf)) {
1187 err = got_error(GOT_ERR_NO_SPACE);
1188 goto done;
1191 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1192 done:
1193 free(refname);
1194 return err;
1197 static const struct got_error *
1198 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1200 const struct got_error *err = NULL;
1201 struct gotd_imsg_ref_update_ng ing;
1202 size_t datalen, len;
1203 char buf[GOT_PKT_MAX];
1204 char *refname = NULL, *reason = NULL;
1206 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1207 if (datalen < sizeof(ing))
1208 return got_error(GOT_ERR_PRIVSEP_LEN);
1209 memcpy(&ing, imsg->data, sizeof(ing));
1210 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1211 return got_error(GOT_ERR_PRIVSEP_LEN);
1213 memcpy(&ing, imsg->data, sizeof(ing));
1215 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1216 if (refname == NULL)
1217 return got_error_from_errno("strndup");
1219 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1220 ing.reason_len);
1221 if (reason == NULL) {
1222 err = got_error_from_errno("strndup");
1223 goto done;
1226 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1227 if (len >= sizeof(buf)) {
1228 err = got_error(GOT_ERR_NO_SPACE);
1229 goto done;
1232 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1233 done:
1234 free(refname);
1235 free(reason);
1236 return err;
1239 static const struct got_error *
1240 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1241 int chattygot)
1243 const struct got_error *err = NULL;
1244 char buf[GOT_PKT_MAX];
1245 struct imsgbuf ibuf;
1246 enum protostate {
1247 STATE_EXPECT_REF_UPDATE,
1248 STATE_EXPECT_MORE_REF_UPDATES,
1249 STATE_EXPECT_PACKFILE,
1250 STATE_PACKFILE_RECEIVED,
1251 STATE_REFS_UPDATED,
1253 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1254 struct imsg imsg;
1255 int report_status = 0;
1257 imsg_init(&ibuf, gotd_sock);
1258 memset(&imsg, 0, sizeof(imsg));
1260 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1261 if (err)
1262 goto done;
1264 while (curstate != STATE_EXPECT_PACKFILE) {
1265 int n;
1266 buf[0] = '\0';
1267 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1268 if (err)
1269 goto done;
1270 if (n == 0) {
1271 if (curstate == STATE_EXPECT_REF_UPDATE) {
1272 /* The client will not send us anything. */
1273 goto done;
1274 } else if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1275 err = got_error_msg(GOT_ERR_BAD_PACKET,
1276 "unexpected flush packet received");
1277 goto done;
1279 err = forward_flushpkt(&ibuf);
1280 if (err)
1281 goto done;
1282 curstate = STATE_EXPECT_PACKFILE;
1283 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1284 if (curstate != STATE_EXPECT_REF_UPDATE &&
1285 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1286 err = got_error_msg(GOT_ERR_BAD_PACKET,
1287 "unexpected ref-update packet");
1288 goto done;
1290 if (curstate == STATE_EXPECT_REF_UPDATE) {
1291 err = recv_ref_update(&report_status,
1292 outfd, &ibuf, buf, n, 1, chattygot);
1293 } else {
1294 err = recv_ref_update(NULL, outfd, &ibuf,
1295 buf, n, 0, chattygot);
1297 if (err)
1298 goto done;
1299 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1300 } else {
1301 err = got_error(GOT_ERR_BAD_PACKET);
1302 goto done;
1306 while (curstate != STATE_PACKFILE_RECEIVED) {
1307 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1308 if (err)
1309 goto done;
1310 switch (imsg.hdr.type) {
1311 case GOTD_IMSG_ERROR:
1312 err = gotd_imsg_recv_error(NULL, &imsg);
1313 goto done;
1314 case GOTD_IMSG_RECV_PACKFILE:
1315 err = recv_packfile(&imsg, infd);
1316 if (err) {
1317 if (err->code != GOT_ERR_EOF)
1318 goto done;
1320 * EOF is reported when the client hangs up,
1321 * which can happen with Git clients.
1322 * The socket should stay half-open so we
1323 * can still send our reports if requested.
1325 err = NULL;
1327 curstate = STATE_PACKFILE_RECEIVED;
1328 break;
1329 default:
1330 err = got_error(GOT_ERR_PRIVSEP_MSG);
1331 break;
1334 imsg_free(&imsg);
1335 if (err)
1336 goto done;
1339 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1340 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1341 if (err)
1342 break;
1343 switch (imsg.hdr.type) {
1344 case GOTD_IMSG_ERROR:
1345 err = gotd_imsg_recv_error(NULL, &imsg);
1346 break;
1347 case GOTD_IMSG_PACKFILE_STATUS:
1348 if (!report_status)
1349 break;
1350 err = report_unpack_status(&imsg, outfd, chattygot);
1351 break;
1352 case GOTD_IMSG_REF_UPDATE_OK:
1353 if (!report_status)
1354 break;
1355 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1356 break;
1357 case GOTD_IMSG_REF_UPDATE_NG:
1358 if (!report_status)
1359 break;
1360 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1361 break;
1362 case GOTD_IMSG_REFS_UPDATED:
1363 curstate = STATE_REFS_UPDATED;
1364 err = got_pkt_flushpkt(outfd, chattygot);
1365 break;
1366 default:
1367 err = got_error(GOT_ERR_PRIVSEP_MSG);
1368 break;
1371 imsg_free(&imsg);
1373 done:
1374 imsg_clear(&ibuf);
1375 if (err)
1376 echo_error(err, outfd, chattygot);
1377 return err;
1380 const struct got_error *
1381 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1382 int gotd_sock, int chattygot)
1384 const struct got_error *err = NULL;
1386 if (strcmp(command, GOT_DIAL_CMD_FETCH) == 0)
1387 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1388 else if (strcmp(command, GOT_DIAL_CMD_SEND) == 0)
1389 err = serve_write(infd, outfd, gotd_sock, repo_path,
1390 chattygot);
1391 else
1392 err = got_error(GOT_ERR_BAD_PACKET);
1394 return err;