free gotwebd servers in gotwebd_shutdown()
[got-portable.git] / lib / serve.c
blob4a83c3046bc674b4bd18cbe3b46d1a82cd292035
1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <poll.h>
26 #include <limits.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
54 * Number of seconds until we give up trying to read more
55 * data from the client connection.
57 static const int timeout = 60;
59 static const struct got_capability read_capabilities[] = {
60 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
61 { GOT_CAPA_OFS_DELTA, NULL },
62 { GOT_CAPA_SIDE_BAND_64K, NULL },
65 static const struct got_capability write_capabilities[] = {
66 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
67 { GOT_CAPA_OFS_DELTA, NULL },
68 { GOT_CAPA_REPORT_STATUS, NULL },
69 { GOT_CAPA_NO_THIN, NULL },
70 { GOT_CAPA_DELETE_REFS, NULL },
73 static const struct got_error *
74 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
75 uint8_t *buf, size_t bufsize)
77 struct got_capability capa[nitems(read_capabilities) + 1];
78 size_t ncapa;
80 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
81 if (symrefstr) {
82 capa[nitems(read_capabilities)].key = "symref";
83 capa[nitems(read_capabilities)].value = symrefstr;
84 ncapa = nitems(capa);
85 } else
86 ncapa = nitems(read_capabilities);
88 return got_gitproto_append_capabilities(capalen, buf, len,
89 bufsize, capa, ncapa);
92 static const struct got_error *
93 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
94 int client_is_reading, const char *symrefstr, int chattygot)
96 const struct got_error *err = NULL;
97 char hex[SHA1_DIGEST_STRING_LENGTH];
98 char buf[GOT_PKT_MAX];
99 size_t len, capalen = 0;
101 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
102 return got_error(GOT_ERR_BAD_OBJ_ID);
104 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
105 if (len >= sizeof(buf))
106 return got_error(GOT_ERR_NO_SPACE);
108 if (send_capabilities) {
109 if (client_is_reading) {
110 err = append_read_capabilities(&capalen, len,
111 symrefstr, buf, sizeof(buf));
112 } else {
113 err = got_gitproto_append_capabilities(&capalen,
114 buf, len, sizeof(buf), write_capabilities,
115 nitems(write_capabilities));
117 if (err)
118 return err;
119 len += capalen;
122 if (len + 1 >= sizeof(buf))
123 return got_error(GOT_ERR_NO_SPACE);
124 buf[len] = '\n';
125 len++;
126 buf[len] = '\0';
128 return got_pkt_writepkt(outfd, buf, len, chattygot);
131 static const struct got_error *
132 send_zero_refs(int outfd, int client_is_reading, int chattygot)
134 const struct got_error *err = NULL;
135 const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
136 char buf[GOT_PKT_MAX];
137 size_t len, capalen = 0;
139 len = strlcpy(buf, line, sizeof(buf));
140 if (len >= sizeof(buf))
141 return got_error(GOT_ERR_NO_SPACE);
143 if (client_is_reading) {
144 err = got_gitproto_append_capabilities(&capalen, buf, len,
145 sizeof(buf), read_capabilities, nitems(read_capabilities));
146 if (err)
147 return err;
148 } else {
149 err = got_gitproto_append_capabilities(&capalen, buf, len,
150 sizeof(buf), write_capabilities,
151 nitems(write_capabilities));
152 if (err)
153 return err;
156 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
159 static void
160 echo_error(const struct got_error *err, int outfd, int chattygot)
162 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
163 size_t len;
166 * Echo the error to the client on a pkt-line.
167 * The client should then terminate its session.
169 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
170 len = strlcat(buf, err->msg, sizeof(buf));
171 got_pkt_writepkt(outfd, buf, len, chattygot);
174 static const struct got_error *
175 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
176 const char *repo_path, int chattygot)
178 const struct got_error *err = NULL;
179 struct imsg imsg;
180 size_t datalen;
181 struct gotd_imsg_list_refs lsref;
182 struct gotd_imsg_reflist ireflist;
183 struct gotd_imsg_ref iref;
184 struct gotd_imsg_symref isymref;
185 size_t nrefs = 0;
186 int have_nrefs = 0, sent_capabilities = 0;
187 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
188 char *refname = NULL;
190 memset(&imsg, 0, sizeof(imsg));
191 memset(&lsref, 0, sizeof(lsref));
193 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
194 sizeof(lsref.repo_name))
195 return got_error(GOT_ERR_NO_SPACE);
196 lsref.client_is_reading = client_is_reading;
198 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
199 &lsref, sizeof(lsref)) == -1)
200 return got_error_from_errno("imsg_compose LIST_REFS");
202 err = gotd_imsg_flush(ibuf);
203 if (err)
204 return err;
206 while (!have_nrefs || nrefs > 0) {
207 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
208 if (err)
209 goto done;
210 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
211 switch (imsg.hdr.type) {
212 case GOTD_IMSG_ERROR:
213 err = gotd_imsg_recv_error(NULL, &imsg);
214 goto done;
215 case GOTD_IMSG_REFLIST:
216 if (have_nrefs || nrefs > 0) {
217 err = got_error(GOT_ERR_PRIVSEP_MSG);
218 goto done;
220 if (datalen != sizeof(ireflist)) {
221 err = got_error(GOT_ERR_PRIVSEP_MSG);
222 goto done;
224 memcpy(&ireflist, imsg.data, sizeof(ireflist));
225 nrefs = ireflist.nrefs;
226 have_nrefs = 1;
227 if (nrefs == 0)
228 err = send_zero_refs(outfd, client_is_reading,
229 chattygot);
230 break;
231 case GOTD_IMSG_REF:
232 if (!have_nrefs || nrefs == 0) {
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
234 goto done;
236 if (datalen < sizeof(iref)) {
237 err = got_error(GOT_ERR_PRIVSEP_MSG);
238 goto done;
240 memcpy(&iref, imsg.data, sizeof(iref));
241 if (datalen != sizeof(iref) + iref.name_len) {
242 err = got_error(GOT_ERR_PRIVSEP_LEN);
243 goto done;
245 refname = strndup(imsg.data + sizeof(iref),
246 iref.name_len);
247 if (refname == NULL) {
248 err = got_error_from_errno("strndup");
249 goto done;
251 err = send_ref(outfd, iref.id, refname,
252 !sent_capabilities, client_is_reading,
253 NULL, chattygot);
254 free(refname);
255 refname = NULL;
256 if (err)
257 goto done;
258 sent_capabilities = 1;
259 if (nrefs > 0)
260 nrefs--;
261 break;
262 case GOTD_IMSG_SYMREF:
263 if (!have_nrefs || nrefs == 0) {
264 err = got_error(GOT_ERR_PRIVSEP_MSG);
265 goto done;
267 if (datalen < sizeof(isymref)) {
268 err = got_error(GOT_ERR_PRIVSEP_LEN);
269 goto done;
271 memcpy(&isymref, imsg.data, sizeof(isymref));
272 if (datalen != sizeof(isymref) + isymref.name_len +
273 isymref.target_len) {
274 err = got_error(GOT_ERR_PRIVSEP_LEN);
275 goto done;
279 * For now, we only announce one symbolic ref,
280 * as part of our capability advertisement.
282 if (sent_capabilities || symrefstr != NULL ||
283 symrefname != NULL || symreftarget != NULL)
284 break;
286 symrefname = strndup(imsg.data + sizeof(isymref),
287 isymref.name_len);
288 if (symrefname == NULL) {
289 err = got_error_from_errno("malloc");
290 goto done;
293 symreftarget = strndup(
294 imsg.data + sizeof(isymref) + isymref.name_len,
295 isymref.target_len);
296 if (symreftarget == NULL) {
297 err = got_error_from_errno("strndup");
298 goto done;
301 if (asprintf(&symrefstr, "%s:%s", symrefname,
302 symreftarget) == -1) {
303 err = got_error_from_errno("asprintf");
304 goto done;
306 err = send_ref(outfd, isymref.target_id, symrefname,
307 !sent_capabilities, client_is_reading, symrefstr,
308 chattygot);
309 free(refname);
310 refname = NULL;
311 if (err)
312 goto done;
313 sent_capabilities = 1;
314 if (nrefs > 0)
315 nrefs--;
316 break;
317 default:
318 err = got_error(GOT_ERR_PRIVSEP_MSG);
319 break;
322 imsg_free(&imsg);
325 err = got_pkt_flushpkt(outfd, chattygot);
326 if (err)
327 goto done;
328 done:
329 free(symrefstr);
330 free(symrefname);
331 free(symreftarget);
332 return err;
335 static const struct got_error *
336 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
338 const struct got_error *err;
339 char *id_str = NULL, *client_capabilities = NULL;
341 err = got_gitproto_parse_want_line(&id_str,
342 &client_capabilities, buf, len);
343 if (err)
344 return err;
346 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
347 err = got_error_msg(GOT_ERR_BAD_PACKET,
348 "want-line with bad object ID");
349 goto done;
352 if (client_capabilities) {
353 err = got_gitproto_match_capabilities(common_capabilities,
354 NULL, client_capabilities, read_capabilities,
355 nitems(read_capabilities));
356 if (err)
357 goto done;
359 done:
360 free(id_str);
361 free(client_capabilities);
362 return err;
365 static const struct got_error *
366 parse_have_line(uint8_t *id, char *buf, size_t len)
368 const struct got_error *err;
369 char *id_str = NULL;
371 err = got_gitproto_parse_have_line(&id_str, buf, len);
372 if (err)
373 return err;
375 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
376 err = got_error_msg(GOT_ERR_BAD_PACKET,
377 "have-line with bad object ID");
378 goto done;
380 done:
381 free(id_str);
382 return err;
385 static const struct got_error *
386 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
388 const struct got_error *err = NULL;
389 struct gotd_imsg_capability icapa;
390 size_t len;
391 struct ibuf *wbuf;
393 memset(&icapa, 0, sizeof(icapa));
395 icapa.key_len = strlen(capa->key);
396 len = sizeof(icapa) + icapa.key_len;
397 if (capa->value) {
398 icapa.value_len = strlen(capa->value);
399 len += icapa.value_len;
402 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
403 if (wbuf == NULL) {
404 err = got_error_from_errno("imsg_create CAPABILITY");
405 return err;
408 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
409 return got_error_from_errno("imsg_add CAPABILITY");
410 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
411 return got_error_from_errno("imsg_add CAPABILITY");
412 if (capa->value) {
413 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
414 return got_error_from_errno("imsg_add CAPABILITY");
417 imsg_close(ibuf, wbuf);
419 return NULL;
422 static const struct got_error *
423 send_capabilities(int *use_sidebands, int *report_status,
424 char *capabilities_str, struct imsgbuf *ibuf)
426 const struct got_error *err = NULL;
427 struct gotd_imsg_capabilities icapas;
428 struct got_capability *capa = NULL;
429 size_t ncapa, i;
431 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
432 capabilities_str);
433 if (err)
434 return err;
436 icapas.ncapabilities = ncapa;
437 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
438 &icapas, sizeof(icapas)) == -1) {
439 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
440 goto done;
443 for (i = 0; i < ncapa; i++) {
444 err = send_capability(&capa[i], ibuf);
445 if (err)
446 goto done;
447 if (use_sidebands &&
448 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
449 *use_sidebands = 1;
450 if (report_status &&
451 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
452 *report_status = 1;
454 done:
455 free(capa);
456 return err;
459 static const struct got_error *
460 forward_flushpkt(struct imsgbuf *ibuf)
462 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
463 return got_error_from_errno("imsg_compose FLUSH");
465 return gotd_imsg_flush(ibuf);
468 static const struct got_error *
469 recv_ack(struct imsg *imsg, uint8_t *expected_id)
471 struct gotd_imsg_ack iack;
472 size_t datalen;
474 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
475 if (datalen != sizeof(iack))
476 return got_error(GOT_ERR_PRIVSEP_LEN);
478 memcpy(&iack, imsg->data, sizeof(iack));
479 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
480 return got_error(GOT_ERR_BAD_OBJ_ID);
482 return NULL;
485 static const struct got_error *
486 recv_nak(struct imsg *imsg, uint8_t *expected_id)
488 struct gotd_imsg_ack inak;
489 size_t datalen;
491 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
492 if (datalen != sizeof(inak))
493 return got_error(GOT_ERR_PRIVSEP_LEN);
495 memcpy(&inak, imsg->data, sizeof(inak));
496 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
497 return got_error(GOT_ERR_BAD_OBJ_ID);
499 return NULL;
503 static const struct got_error *
504 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
505 char *buf, size_t len, int expect_capabilities, int chattygot)
507 const struct got_error *err;
508 struct gotd_imsg_want iwant;
509 char *capabilities_str;
510 int done = 0;
511 struct imsg imsg;
513 memset(&iwant, 0, sizeof(iwant));
514 memset(&imsg, 0, sizeof(imsg));
516 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
517 if (err)
518 return err;
520 if (capabilities_str) {
521 if (!expect_capabilities) {
522 err = got_error_msg(GOT_ERR_BAD_PACKET,
523 "unexpected capability announcement received");
524 goto done;
526 err = send_capabilities(use_sidebands, NULL, capabilities_str,
527 ibuf);
528 if (err)
529 goto done;
533 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
534 &iwant, sizeof(iwant)) == -1) {
535 err = got_error_from_errno("imsg_compose WANT");
536 goto done;
539 err = gotd_imsg_flush(ibuf);
540 if (err)
541 goto done;
544 * Wait for an ACK, or an error in case the desired object
545 * does not exist.
547 while (!done && err == NULL) {
548 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
549 if (err)
550 break;
551 switch (imsg.hdr.type) {
552 case GOTD_IMSG_ERROR:
553 err = gotd_imsg_recv_error(NULL, &imsg);
554 break;
555 case GOTD_IMSG_ACK:
556 err = recv_ack(&imsg, iwant.object_id);
557 if (err)
558 break;
559 done = 1;
560 break;
561 default:
562 err = got_error(GOT_ERR_PRIVSEP_MSG);
563 break;
566 imsg_free(&imsg);
568 done:
569 free(capabilities_str);
570 return err;
573 static const struct got_error *
574 send_ack(int outfd, uint8_t *id, int chattygot)
576 char hex[SHA1_DIGEST_STRING_LENGTH];
577 char buf[GOT_PKT_MAX];
578 int len;
580 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
581 return got_error(GOT_ERR_BAD_OBJ_ID);
583 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
584 if (len >= sizeof(buf))
585 return got_error(GOT_ERR_NO_SPACE);
587 return got_pkt_writepkt(outfd, buf, len, chattygot);
590 static const struct got_error *
591 send_nak(int outfd, int chattygot)
593 char buf[5];
594 int len;
596 len = snprintf(buf, sizeof(buf), "NAK\n");
597 if (len >= sizeof(buf))
598 return got_error(GOT_ERR_NO_SPACE);
600 return got_pkt_writepkt(outfd, buf, len, chattygot);
603 static const struct got_error *
604 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
605 size_t len, int chattygot)
607 const struct got_error *err;
608 struct gotd_imsg_have ihave;
609 int done = 0;
610 struct imsg imsg;
612 memset(&ihave, 0, sizeof(ihave));
613 memset(&imsg, 0, sizeof(imsg));
615 err = parse_have_line(ihave.object_id, buf, len);
616 if (err)
617 return err;
619 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
620 &ihave, sizeof(ihave)) == -1)
621 return got_error_from_errno("imsg_compose HAVE");
623 err = gotd_imsg_flush(ibuf);
624 if (err)
625 return err;
628 * Wait for an ACK or a NAK, indicating whether a common
629 * commit object has been found.
631 while (!done && err == NULL) {
632 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
633 if (err)
634 return err;
635 switch (imsg.hdr.type) {
636 case GOTD_IMSG_ERROR:
637 err = gotd_imsg_recv_error(NULL, &imsg);
638 break;
639 case GOTD_IMSG_ACK:
640 err = recv_ack(&imsg, ihave.object_id);
641 if (err)
642 break;
643 if (!*have_ack) {
644 err = send_ack(outfd, ihave.object_id,
645 chattygot);
646 if (err)
647 return err;
648 *have_ack = 1;
650 done = 1;
651 break;
652 case GOTD_IMSG_NAK:
653 err = recv_nak(&imsg, ihave.object_id);
654 if (err)
655 break;
656 done = 1;
657 break;
658 default:
659 err = got_error(GOT_ERR_PRIVSEP_MSG);
660 break;
663 imsg_free(&imsg);
666 return err;
669 static const struct got_error *
670 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
672 const struct got_error *err;
673 struct imsg imsg;
674 int fd;
676 *packfd = -1;
678 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
679 return got_error_from_errno("imsg_compose DONE");
681 err = gotd_imsg_flush(ibuf);
682 if (err)
683 return err;
685 while (*packfd == -1 && err == NULL) {
686 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
687 if (err)
688 break;
690 switch (imsg.hdr.type) {
691 case GOTD_IMSG_ERROR:
692 err = gotd_imsg_recv_error(NULL, &imsg);
693 break;
694 case GOTD_IMSG_PACKFILE_PIPE:
695 fd = imsg_get_fd(&imsg);
696 if (fd != -1)
697 *packfd = fd;
698 else
699 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
700 break;
701 default:
702 err = got_error(GOT_ERR_PRIVSEP_MSG);
703 break;
706 imsg_free(&imsg);
709 return err;
712 static const struct got_error *
713 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
715 const struct got_error *err = NULL;
716 int pack_starting = 0;
717 struct gotd_imsg_packfile_progress iprog;
718 char buf[GOT_PKT_MAX];
719 struct imsg imsg;
720 size_t datalen;
721 int p_deltify = 0, n;
722 const char *eol = "\r";
724 memset(&imsg, 0, sizeof(imsg));
726 while (!pack_starting && err == NULL) {
727 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
728 if (err)
729 break;
731 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
732 switch (imsg.hdr.type) {
733 case GOTD_IMSG_ERROR:
734 err = gotd_imsg_recv_error(NULL, &imsg);
735 break;
736 case GOTD_IMSG_PACKFILE_READY:
737 eol = "\n";
738 pack_starting = 1;
739 /* fallthrough */
740 case GOTD_IMSG_PACKFILE_PROGRESS:
741 if (datalen != sizeof(iprog)) {
742 err = got_error(GOT_ERR_PRIVSEP_LEN);
743 break;
745 memcpy(&iprog, imsg.data, sizeof(iprog));
746 if (iprog.nobj_total > 0) {
747 p_deltify = (iprog.nobj_deltify * 100) /
748 iprog.nobj_total;
750 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
751 n = snprintf(&buf[1], sizeof(buf) - 1,
752 "%d commits colored, "
753 "%d objects found, "
754 "deltify %d%%%s",
755 iprog.ncolored,
756 iprog.nfound,
757 p_deltify, eol);
758 if (n >= sizeof(buf) - 1)
759 break;
760 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
761 break;
762 default:
763 err = got_error(GOT_ERR_PRIVSEP_MSG);
764 break;
767 imsg_free(&imsg);
770 return err;
773 static const struct got_error *
774 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
775 int chattygot)
777 const struct got_error *err = NULL;
778 char buf[GOT_PKT_MAX];
779 struct imsgbuf ibuf;
780 enum protostate {
781 STATE_EXPECT_WANT,
782 STATE_EXPECT_MORE_WANT,
783 STATE_EXPECT_HAVE_OR_DONE,
784 STATE_DONE,
786 enum protostate curstate = STATE_EXPECT_WANT;
787 int have_ack = 0, use_sidebands = 0, seen_have = 0;
788 int packfd = -1;
789 size_t pack_chunksize;
791 if (imsgbuf_init(&ibuf, gotd_sock) == -1)
792 return got_error_from_errno("imsgbuf_init");
793 imsgbuf_allow_fdpass(&ibuf);
795 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
796 if (err)
797 goto done;
799 while (curstate != STATE_DONE) {
800 int n;
801 buf[0] = '\0';
802 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot,
803 timeout);
804 if (err)
805 goto done;
806 if (n == 0) {
807 if (curstate != STATE_EXPECT_WANT &&
808 curstate != STATE_EXPECT_MORE_WANT &&
809 curstate != STATE_EXPECT_HAVE_OR_DONE) {
810 err = got_error_msg(GOT_ERR_BAD_PACKET,
811 "unexpected flush packet received");
812 goto done;
815 if (curstate == STATE_EXPECT_WANT) {
816 ssize_t r;
818 * If the client does not want to fetch
819 * anything we should receive a flush
820 * packet followed by EOF.
822 r = read(infd, buf, sizeof(buf));
823 if (r == -1) {
824 err = got_error_from_errno("read");
825 goto done;
827 if (r == 0) /* EOF */
828 goto done;
830 /* Zero-length field followed by payload. */
831 err = got_error_msg(GOT_ERR_BAD_PACKET,
832 "unexpected flush packet received");
833 goto done;
836 if (curstate == STATE_EXPECT_WANT ||
837 curstate == STATE_EXPECT_MORE_WANT ||
838 curstate == STATE_EXPECT_HAVE_OR_DONE) {
839 err = forward_flushpkt(&ibuf);
840 if (err)
841 goto done;
843 if (curstate == STATE_EXPECT_HAVE_OR_DONE &&
844 !have_ack) {
845 err = send_nak(outfd, chattygot);
846 if (err)
847 goto done;
849 if (curstate == STATE_EXPECT_MORE_WANT)
850 curstate = STATE_EXPECT_HAVE_OR_DONE;
851 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
852 if (curstate != STATE_EXPECT_WANT &&
853 curstate != STATE_EXPECT_MORE_WANT) {
854 err = got_error_msg(GOT_ERR_BAD_PACKET,
855 "unexpected 'want' packet");
856 goto done;
858 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
859 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
860 if (err)
861 goto done;
862 if (curstate == STATE_EXPECT_WANT)
863 curstate = STATE_EXPECT_MORE_WANT;
864 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
865 if (curstate != STATE_EXPECT_HAVE_OR_DONE) {
866 err = got_error_msg(GOT_ERR_BAD_PACKET,
867 "unexpected 'have' packet");
868 goto done;
870 err = recv_have(&have_ack, outfd, &ibuf,
871 buf, n, chattygot);
872 if (err)
873 goto done;
874 seen_have = 1;
875 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
876 if (curstate != STATE_EXPECT_HAVE_OR_DONE) {
877 err = got_error_msg(GOT_ERR_BAD_PACKET,
878 "unexpected 'done' packet");
879 goto done;
881 err = recv_done(&packfd, outfd, &ibuf, chattygot);
882 if (err)
883 goto done;
884 curstate = STATE_DONE;
885 break;
886 } else {
887 err = got_error(GOT_ERR_BAD_PACKET);
888 goto done;
892 if (!seen_have) {
893 err = send_nak(outfd, chattygot);
894 if (err)
895 goto done;
898 if (use_sidebands) {
899 err = relay_progress_reports(&ibuf, outfd, chattygot);
900 if (err)
901 goto done;
902 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
903 } else
904 pack_chunksize = sizeof(buf);
906 for (;;) {
907 ssize_t r;
909 r = read(packfd, use_sidebands ? &buf[1] : buf,
910 pack_chunksize);
911 if (r == -1) {
912 err = got_error_from_errno("read");
913 break;
914 } else if (r == 0) {
915 err = got_pkt_flushpkt(outfd, chattygot);
916 break;
919 if (use_sidebands) {
920 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
921 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
922 if (err)
923 break;
924 } else {
925 err = got_poll_write_full(outfd, buf, r);
926 if (err) {
927 if (err->code == GOT_ERR_EOF)
928 err = NULL;
929 break;
933 done:
934 imsgbuf_clear(&ibuf);
935 if (packfd != -1 && close(packfd) == -1 && err == NULL)
936 err = got_error_from_errno("close");
937 if (err)
938 echo_error(err, outfd, chattygot);
939 return err;
942 static const struct got_error *
943 parse_ref_update_line(char **common_capabilities, char **refname,
944 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
946 const struct got_error *err;
947 char *old_id_str = NULL, *new_id_str = NULL;
948 char *client_capabilities = NULL;
950 *refname = NULL;
952 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
953 refname, &client_capabilities, buf, len);
954 if (err)
955 return err;
957 if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
958 !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
959 err = got_error_msg(GOT_ERR_BAD_PACKET,
960 "ref-update with bad object ID");
961 goto done;
963 if (!got_ref_name_is_valid(*refname)) {
964 err = got_error_msg(GOT_ERR_BAD_PACKET,
965 "ref-update with bad reference name");
966 goto done;
969 if (client_capabilities) {
970 err = got_gitproto_match_capabilities(common_capabilities,
971 NULL, client_capabilities, write_capabilities,
972 nitems(write_capabilities));
973 if (err)
974 goto done;
976 done:
977 free(old_id_str);
978 free(new_id_str);
979 free(client_capabilities);
980 if (err) {
981 free(*refname);
982 *refname = NULL;
984 return err;
987 static const struct got_error *
988 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
989 char *buf, size_t len, int expect_capabilities, int chattygot)
991 const struct got_error *err;
992 struct gotd_imsg_ref_update iref;
993 struct ibuf *wbuf;
994 char *capabilities_str = NULL, *refname = NULL;
995 int done = 0;
996 struct imsg imsg;
998 memset(&iref, 0, sizeof(iref));
999 memset(&imsg, 0, sizeof(imsg));
1001 err = parse_ref_update_line(&capabilities_str, &refname,
1002 iref.old_id, iref.new_id, buf, len);
1003 if (err)
1004 return err;
1006 if (capabilities_str) {
1007 if (!expect_capabilities) {
1008 err = got_error_msg(GOT_ERR_BAD_PACKET,
1009 "unexpected capability announcement received");
1010 goto done;
1012 err = send_capabilities(NULL, report_status, capabilities_str,
1013 ibuf);
1014 if (err)
1015 goto done;
1018 iref.name_len = strlen(refname);
1019 len = sizeof(iref) + iref.name_len;
1020 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1021 if (wbuf == NULL) {
1022 err = got_error_from_errno("imsg_create REF_UPDATE");
1023 goto done;
1026 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1027 return got_error_from_errno("imsg_add REF_UPDATE");
1028 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1029 return got_error_from_errno("imsg_add REF_UPDATE");
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 if (imsgbuf_init(&ibuf, gotd_sock) == -1)
1264 return got_error_from_errno("imsgbuf_init");
1265 imsgbuf_allow_fdpass(&ibuf);
1267 memset(&imsg, 0, sizeof(imsg));
1269 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1270 if (err)
1271 goto done;
1273 while (curstate != STATE_EXPECT_PACKFILE) {
1274 int n;
1275 buf[0] = '\0';
1276 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot,
1277 timeout);
1278 if (err)
1279 goto done;
1280 if (n == 0) {
1281 if (curstate == STATE_EXPECT_REF_UPDATE) {
1282 /* The client will not send us anything. */
1283 goto done;
1284 } else if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1285 err = got_error_msg(GOT_ERR_BAD_PACKET,
1286 "unexpected flush packet received");
1287 goto done;
1289 err = forward_flushpkt(&ibuf);
1290 if (err)
1291 goto done;
1292 curstate = STATE_EXPECT_PACKFILE;
1293 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1294 if (curstate != STATE_EXPECT_REF_UPDATE &&
1295 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1296 err = got_error_msg(GOT_ERR_BAD_PACKET,
1297 "unexpected ref-update packet");
1298 goto done;
1300 if (curstate == STATE_EXPECT_REF_UPDATE) {
1301 err = recv_ref_update(&report_status,
1302 outfd, &ibuf, buf, n, 1, chattygot);
1303 } else {
1304 err = recv_ref_update(NULL, outfd, &ibuf,
1305 buf, n, 0, chattygot);
1307 if (err)
1308 goto done;
1309 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1310 } else {
1311 err = got_error(GOT_ERR_BAD_PACKET);
1312 goto done;
1316 while (curstate != STATE_PACKFILE_RECEIVED) {
1317 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1318 if (err)
1319 goto done;
1320 switch (imsg.hdr.type) {
1321 case GOTD_IMSG_ERROR:
1322 err = gotd_imsg_recv_error(NULL, &imsg);
1323 goto done;
1324 case GOTD_IMSG_PACKFILE_PIPE:
1325 err = recv_packfile(&imsg, infd);
1326 if (err) {
1327 if (err->code != GOT_ERR_EOF)
1328 goto done;
1330 * EOF is reported when the client hangs up,
1331 * which can happen with Git clients.
1332 * The socket should stay half-open so we
1333 * can still send our reports if requested.
1335 err = NULL;
1337 curstate = STATE_PACKFILE_RECEIVED;
1338 break;
1339 default:
1340 err = got_error(GOT_ERR_PRIVSEP_MSG);
1341 break;
1344 imsg_free(&imsg);
1345 if (err)
1346 goto done;
1349 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1350 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1351 if (err)
1352 break;
1353 switch (imsg.hdr.type) {
1354 case GOTD_IMSG_ERROR:
1355 err = gotd_imsg_recv_error(NULL, &imsg);
1356 break;
1357 case GOTD_IMSG_PACKFILE_STATUS:
1358 if (!report_status)
1359 break;
1360 err = report_unpack_status(&imsg, outfd, chattygot);
1361 break;
1362 case GOTD_IMSG_REF_UPDATE_OK:
1363 if (!report_status)
1364 break;
1365 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1366 break;
1367 case GOTD_IMSG_REF_UPDATE_NG:
1368 if (!report_status)
1369 break;
1370 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1371 break;
1372 case GOTD_IMSG_REFS_UPDATED:
1373 curstate = STATE_REFS_UPDATED;
1374 err = got_pkt_flushpkt(outfd, chattygot);
1375 break;
1376 default:
1377 err = got_error(GOT_ERR_PRIVSEP_MSG);
1378 break;
1381 imsg_free(&imsg);
1383 done:
1384 imsgbuf_clear(&ibuf);
1385 if (err)
1386 echo_error(err, outfd, chattygot);
1387 return err;
1390 const struct got_error *
1391 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1392 int gotd_sock, int chattygot)
1394 const struct got_error *err = NULL;
1396 if (strcmp(command, GOT_DIAL_CMD_FETCH) == 0)
1397 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1398 else if (strcmp(command, GOT_DIAL_CMD_SEND) == 0)
1399 err = serve_write(infd, outfd, gotd_sock, repo_path,
1400 chattygot);
1401 else
1402 err = got_error(GOT_ERR_BAD_PACKET);
1404 return err;