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 <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <imsg.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_serve.h"
35 #include "got_path.h"
36 #include "got_version.h"
37 #include "got_reference.h"
39 #include "got_lib_pkt.h"
40 #include "got_lib_dial.h"
41 #include "got_lib_gitproto.h"
42 #include "got_lib_sha1.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 #if 0
63 { GOT_CAPA_DELETE_REFS, NULL },
64 #endif
65 };
67 static const struct got_error *
68 parse_command(char **command, char **repo_path, const char *gitcmd)
69 {
70 const struct got_error *err = NULL;
71 size_t len, cmdlen, pathlen;
72 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
73 const char *relpath;
75 *command = NULL;
76 *repo_path = NULL;
78 len = strlen(gitcmd);
80 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
81 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
82 strlen(GOT_SERVE_CMD_SEND)) == 0)
83 cmdlen = strlen(GOT_SERVE_CMD_SEND);
84 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
85 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
86 strlen(GOT_SERVE_CMD_FETCH)) == 0)
87 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
88 else
89 return got_error(GOT_ERR_BAD_PACKET);
91 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
92 return got_error(GOT_ERR_BAD_PACKET);
94 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
95 return got_error(GOT_ERR_BAD_PATH);
97 /* Forbid linefeeds in paths, like Git does. */
98 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
99 return got_error(GOT_ERR_BAD_PATH);
101 path0 = strdup(&gitcmd[cmdlen + 1]);
102 if (path0 == NULL)
103 return got_error_from_errno("strdup");
104 path = path0;
105 pathlen = strlen(path);
107 /*
108 * Git clients send a shell command.
109 * Trim spaces and quotes around the path.
110 */
111 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
112 path++;
113 pathlen--;
115 while (pathlen > 0 &&
116 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
117 path[pathlen - 1] == ' ')) {
118 path[pathlen - 1] = '\0';
119 pathlen--;
122 /* Deny an empty repository path. */
123 if (path[0] == '\0' || got_path_is_root_dir(path)) {
124 err = got_error(GOT_ERR_NOT_GIT_REPO);
125 goto done;
128 if (asprintf(&abspath, "/%s", path) == -1) {
129 err = got_error_from_errno("asprintf");
130 goto done;
132 pathlen = strlen(abspath);
133 canonpath = malloc(pathlen);
134 if (canonpath == NULL) {
135 err = got_error_from_errno("malloc");
136 goto done;
138 err = got_canonpath(abspath, canonpath, pathlen);
139 if (err)
140 goto done;
142 relpath = canonpath;
143 while (relpath[0] == '/')
144 relpath++;
145 *repo_path = strdup(relpath);
146 if (*repo_path == NULL) {
147 err = got_error_from_errno("strdup");
148 goto done;
150 *command = strndup(gitcmd, cmdlen);
151 if (*command == NULL)
152 err = got_error_from_errno("strndup");
153 done:
154 free(path0);
155 free(abspath);
156 free(canonpath);
157 if (err) {
158 free(*repo_path);
159 *repo_path = NULL;
161 return err;
164 static const struct got_error *
165 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
166 uint8_t *buf, size_t bufsize)
168 struct got_capability capa[nitems(read_capabilities) + 1];
169 size_t ncapa;
171 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
172 if (symrefstr) {
173 capa[nitems(read_capabilities)].key = "symref";
174 capa[nitems(read_capabilities)].value = symrefstr;
175 ncapa = nitems(capa);
176 } else
177 ncapa = nitems(read_capabilities);
179 return got_gitproto_append_capabilities(capalen, buf, len,
180 bufsize, capa, ncapa);
183 static const struct got_error *
184 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
185 int client_is_reading, const char *symrefstr, int chattygot)
187 const struct got_error *err = NULL;
188 char hex[SHA1_DIGEST_STRING_LENGTH];
189 char buf[GOT_PKT_MAX];
190 size_t len, capalen = 0;
192 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
193 return got_error(GOT_ERR_BAD_OBJ_ID);
195 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
196 if (len >= sizeof(buf))
197 return got_error(GOT_ERR_NO_SPACE);
199 if (send_capabilities) {
200 if (client_is_reading) {
201 err = append_read_capabilities(&capalen, len,
202 symrefstr, buf, sizeof(buf));
203 } else {
204 err = got_gitproto_append_capabilities(&capalen,
205 buf, len, sizeof(buf), write_capabilities,
206 nitems(write_capabilities));
208 if (err)
209 return err;
210 len += capalen;
213 if (len + 1 >= sizeof(buf))
214 return got_error(GOT_ERR_NO_SPACE);
215 buf[len] = '\n';
216 len++;
217 buf[len] = '\0';
219 return got_pkt_writepkt(outfd, buf, len, chattygot);
222 static const struct got_error *
223 send_zero_refs(int outfd, int chattygot)
225 const struct got_error *err = NULL;
226 char buf[GOT_PKT_MAX];
227 uint8_t zero[SHA1_DIGEST_LENGTH];
228 char hex[SHA1_DIGEST_STRING_LENGTH];
229 size_t len, capalen = 0;
231 memset(&zero, 0, sizeof(zero));
233 if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
234 return got_error(GOT_ERR_BAD_OBJ_ID);
236 len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
237 if (len >= sizeof(buf))
238 return got_error(GOT_ERR_NO_SPACE);
240 err = got_gitproto_append_capabilities(&capalen, buf, len,
241 sizeof(buf), read_capabilities, nitems(read_capabilities));
242 if (err)
243 return err;
245 return got_pkt_writepkt(outfd, buf, len, chattygot);
248 static void
249 echo_error(const struct got_error *err, int outfd, int chattygot)
251 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
252 size_t len;
254 /*
255 * Echo the error to the client on a pkt-line.
256 * The client should then terminate its session.
257 */
258 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
259 len = strlcat(buf, err->msg, sizeof(buf));
260 err = got_pkt_writepkt(outfd, buf, len, chattygot);
261 abort();
264 static const struct got_error *
265 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
266 const char *repo_path, int chattygot)
268 const struct got_error *err = NULL;
269 struct imsg imsg;
270 size_t datalen;
271 struct gotd_imsg_list_refs lsref;
272 struct gotd_imsg_reflist ireflist;
273 struct gotd_imsg_ref iref;
274 struct gotd_imsg_symref isymref;
275 size_t nrefs = 0;
276 int have_nrefs = 0, sent_capabilities = 0;
277 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
278 char *refname = NULL;
280 memset(&imsg, 0, sizeof(imsg));
281 memset(&lsref, 0, sizeof(lsref));
283 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
284 sizeof(lsref.repo_name))
285 return got_error(GOT_ERR_NO_SPACE);
286 lsref.client_is_reading = client_is_reading;
288 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
289 &lsref, sizeof(lsref)) == -1)
290 return got_error_from_errno("imsg_compose LIST_REFS");
292 err = gotd_imsg_flush(ibuf);
293 if (err)
294 return err;
296 while (!have_nrefs || nrefs > 0) {
297 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
298 if (err)
299 goto done;
300 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
301 switch (imsg.hdr.type) {
302 case GOTD_IMSG_ERROR:
303 err = gotd_imsg_recv_error(NULL, &imsg);
304 goto done;
305 case GOTD_IMSG_REFLIST:
306 if (have_nrefs || nrefs > 0) {
307 err = got_error(GOT_ERR_PRIVSEP_MSG);
308 goto done;
310 if (datalen != sizeof(ireflist)) {
311 err = got_error(GOT_ERR_PRIVSEP_MSG);
312 goto done;
314 memcpy(&ireflist, imsg.data, sizeof(ireflist));
315 nrefs = ireflist.nrefs;
316 have_nrefs = 1;
317 if (nrefs == 0)
318 err = send_zero_refs(outfd, chattygot);
319 break;
320 case GOTD_IMSG_REF:
321 if (!have_nrefs || nrefs == 0) {
322 err = got_error(GOT_ERR_PRIVSEP_MSG);
323 goto done;
325 if (datalen < sizeof(iref)) {
326 err = got_error(GOT_ERR_PRIVSEP_MSG);
327 goto done;
329 memcpy(&iref, imsg.data, sizeof(iref));
330 if (datalen != sizeof(iref) + iref.name_len) {
331 err = got_error(GOT_ERR_PRIVSEP_LEN);
332 goto done;
334 refname = malloc(iref.name_len + 1);
335 if (refname == NULL) {
336 err = got_error_from_errno("malloc");
337 goto done;
339 memcpy(refname, imsg.data + sizeof(iref),
340 iref.name_len);
341 refname[iref.name_len] = '\0';
342 err = send_ref(outfd, iref.id, refname,
343 !sent_capabilities, client_is_reading,
344 NULL, chattygot);
345 free(refname);
346 refname = NULL;
347 if (err)
348 goto done;
349 sent_capabilities = 1;
350 if (nrefs > 0)
351 nrefs--;
352 break;
353 case GOTD_IMSG_SYMREF:
354 if (!have_nrefs || nrefs == 0) {
355 err = got_error(GOT_ERR_PRIVSEP_MSG);
356 goto done;
358 if (datalen < sizeof(isymref)) {
359 err = got_error(GOT_ERR_PRIVSEP_LEN);
360 goto done;
362 memcpy(&isymref, imsg.data, sizeof(isymref));
363 if (datalen != sizeof(isymref) + isymref.name_len +
364 isymref.target_len) {
365 err = got_error(GOT_ERR_PRIVSEP_LEN);
366 goto done;
369 /*
370 * For now, we only announce one symbolic ref,
371 * as part of our capability advertisement.
372 */
373 if (sent_capabilities || symrefstr != NULL ||
374 symrefname != NULL || symreftarget != NULL)
375 break;
377 symrefname = malloc(isymref.name_len + 1);
378 if (symrefname == NULL) {
379 err = got_error_from_errno("malloc");
380 goto done;
382 memcpy(symrefname, imsg.data + sizeof(isymref),
383 isymref.name_len);
384 symrefname[isymref.name_len] = '\0';
386 symreftarget = malloc(isymref.target_len + 1);
387 if (symreftarget == NULL) {
388 err = got_error_from_errno("malloc");
389 goto done;
391 memcpy(symreftarget,
392 imsg.data + sizeof(isymref) + isymref.name_len,
393 isymref.target_len);
394 symreftarget[isymref.target_len] = '\0';
396 if (asprintf(&symrefstr, "%s:%s", symrefname,
397 symreftarget) == -1) {
398 err = got_error_from_errno("asprintf");
399 goto done;
401 err = send_ref(outfd, isymref.target_id, symrefname,
402 !sent_capabilities, client_is_reading, symrefstr,
403 chattygot);
404 free(refname);
405 refname = NULL;
406 if (err)
407 goto done;
408 sent_capabilities = 1;
409 if (nrefs > 0)
410 nrefs--;
411 break;
412 default:
413 err = got_error(GOT_ERR_PRIVSEP_MSG);
414 break;
417 imsg_free(&imsg);
420 err = got_pkt_flushpkt(outfd, chattygot);
421 if (err)
422 goto done;
423 done:
424 free(symrefstr);
425 free(symrefname);
426 free(symreftarget);
427 return err;
430 static const struct got_error *
431 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
433 const struct got_error *err;
434 char *id_str = NULL, *client_capabilities = NULL;
436 err = got_gitproto_parse_want_line(&id_str,
437 &client_capabilities, buf, len);
438 if (err)
439 return err;
441 if (!got_parse_sha1_digest(id, id_str)) {
442 err = got_error_msg(GOT_ERR_BAD_PACKET,
443 "want-line with bad object ID");
444 goto done;
447 if (client_capabilities) {
448 err = got_gitproto_match_capabilities(common_capabilities,
449 NULL, client_capabilities, read_capabilities,
450 nitems(read_capabilities));
451 if (err)
452 goto done;
454 done:
455 free(id_str);
456 free(client_capabilities);
457 return err;
460 static const struct got_error *
461 parse_have_line(uint8_t *id, char *buf, size_t len)
463 const struct got_error *err;
464 char *id_str = NULL;
466 err = got_gitproto_parse_have_line(&id_str, buf, len);
467 if (err)
468 return err;
470 if (!got_parse_sha1_digest(id, id_str)) {
471 err = got_error_msg(GOT_ERR_BAD_PACKET,
472 "have-line with bad object ID");
473 goto done;
475 done:
476 free(id_str);
477 return err;
480 static const struct got_error *
481 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
483 const struct got_error *err = NULL;
484 struct gotd_imsg_capability icapa;
485 size_t len;
486 struct ibuf *wbuf;
488 memset(&icapa, 0, sizeof(icapa));
490 icapa.key_len = strlen(capa->key);
491 len = sizeof(icapa) + icapa.key_len;
492 if (capa->value) {
493 icapa.value_len = strlen(capa->value);
494 len += icapa.value_len;
497 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
498 if (wbuf == NULL) {
499 err = got_error_from_errno("imsg_create CAPABILITY");
500 return err;
503 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
504 return got_error_from_errno("imsg_add CAPABILITY");
505 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
506 return got_error_from_errno("imsg_add CAPABILITY");
507 if (capa->value) {
508 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
509 return got_error_from_errno("imsg_add CAPABILITY");
512 wbuf->fd = -1;
513 imsg_close(ibuf, wbuf);
515 return NULL;
518 static const struct got_error *
519 send_capabilities(int *use_sidebands, int *report_status,
520 char *capabilities_str, struct imsgbuf *ibuf)
522 const struct got_error *err = NULL;
523 struct gotd_imsg_capabilities icapas;
524 struct got_capability *capa = NULL;
525 size_t ncapa, i;
527 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
528 capabilities_str);
529 if (err)
530 return err;
532 icapas.ncapabilities = ncapa;
533 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
534 &icapas, sizeof(icapas)) == -1) {
535 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
536 goto done;
539 for (i = 0; i < ncapa; i++) {
540 err = send_capability(&capa[i], ibuf);
541 if (err)
542 goto done;
543 if (use_sidebands &&
544 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
545 *use_sidebands = 1;
546 if (report_status &&
547 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
548 *report_status = 1;
550 done:
551 free(capa);
552 return err;
555 static const struct got_error *
556 forward_flushpkt(struct imsgbuf *ibuf)
558 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
559 return got_error_from_errno("imsg_compose FLUSH");
561 return gotd_imsg_flush(ibuf);
564 static const struct got_error *
565 recv_ack(struct imsg *imsg, uint8_t *expected_id)
567 struct gotd_imsg_ack iack;
568 size_t datalen;
570 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
571 if (datalen != sizeof(iack))
572 return got_error(GOT_ERR_PRIVSEP_LEN);
574 memcpy(&iack, imsg->data, sizeof(iack));
575 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
576 return got_error(GOT_ERR_BAD_OBJ_ID);
578 return NULL;
581 static const struct got_error *
582 recv_nak(struct imsg *imsg, uint8_t *expected_id)
584 struct gotd_imsg_ack inak;
585 size_t datalen;
587 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
588 if (datalen != sizeof(inak))
589 return got_error(GOT_ERR_PRIVSEP_LEN);
591 memcpy(&inak, imsg->data, sizeof(inak));
592 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
593 return got_error(GOT_ERR_BAD_OBJ_ID);
595 return NULL;
599 static const struct got_error *
600 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
601 char *buf, size_t len, int expect_capabilities, int chattygot)
603 const struct got_error *err;
604 struct gotd_imsg_want iwant;
605 char *capabilities_str;
606 int done = 0;
607 struct imsg imsg;
609 memset(&iwant, 0, sizeof(iwant));
610 memset(&imsg, 0, sizeof(imsg));
612 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
613 if (err)
614 return err;
616 if (capabilities_str) {
617 if (!expect_capabilities) {
618 err = got_error_msg(GOT_ERR_BAD_PACKET,
619 "unexpected capability announcement received");
620 goto done;
622 err = send_capabilities(use_sidebands, NULL, capabilities_str,
623 ibuf);
624 if (err)
625 goto done;
629 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
630 &iwant, sizeof(iwant)) == -1) {
631 err = got_error_from_errno("imsg_compose WANT");
632 goto done;
635 err = gotd_imsg_flush(ibuf);
636 if (err)
637 goto done;
639 /*
640 * Wait for an ACK, or an error in case the desired object
641 * does not exist.
642 */
643 while (!done && err == NULL) {
644 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
645 if (err)
646 break;
647 switch (imsg.hdr.type) {
648 case GOTD_IMSG_ERROR:
649 err = gotd_imsg_recv_error(NULL, &imsg);
650 break;
651 case GOTD_IMSG_ACK:
652 err = recv_ack(&imsg, iwant.object_id);
653 if (err)
654 break;
655 done = 1;
656 break;
657 default:
658 err = got_error(GOT_ERR_PRIVSEP_MSG);
659 break;
662 imsg_free(&imsg);
664 done:
665 free(capabilities_str);
666 return err;
669 static const struct got_error *
670 send_ack(int outfd, uint8_t *id, int chattygot)
672 char hex[SHA1_DIGEST_STRING_LENGTH];
673 char buf[GOT_PKT_MAX];
674 int len;
676 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
677 return got_error(GOT_ERR_BAD_OBJ_ID);
679 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
680 if (len >= sizeof(buf))
681 return got_error(GOT_ERR_NO_SPACE);
683 return got_pkt_writepkt(outfd, buf, len, chattygot);
686 static const struct got_error *
687 send_nak(int outfd, int chattygot)
689 char buf[5];
690 int len;
692 len = snprintf(buf, sizeof(buf), "NAK\n");
693 if (len >= sizeof(buf))
694 return got_error(GOT_ERR_NO_SPACE);
696 return got_pkt_writepkt(outfd, buf, len, chattygot);
699 static const struct got_error *
700 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
701 size_t len, int chattygot)
703 const struct got_error *err;
704 struct gotd_imsg_have ihave;
705 int done = 0;
706 struct imsg imsg;
708 memset(&ihave, 0, sizeof(ihave));
709 memset(&imsg, 0, sizeof(imsg));
711 err = parse_have_line(ihave.object_id, buf, len);
712 if (err)
713 return err;
715 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
716 &ihave, sizeof(ihave)) == -1)
717 return got_error_from_errno("imsg_compose HAVE");
719 err = gotd_imsg_flush(ibuf);
720 if (err)
721 return err;
723 /*
724 * Wait for an ACK or a NAK, indicating whether a common
725 * commit object has been found.
726 */
727 while (!done && err == NULL) {
728 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
729 if (err)
730 return err;
731 switch (imsg.hdr.type) {
732 case GOTD_IMSG_ERROR:
733 err = gotd_imsg_recv_error(NULL, &imsg);
734 break;
735 case GOTD_IMSG_ACK:
736 err = recv_ack(&imsg, ihave.object_id);
737 if (err)
738 break;
739 if (!*have_ack) {
740 err = send_ack(outfd, ihave.object_id,
741 chattygot);
742 if (err)
743 return err;
744 *have_ack = 1;
746 done = 1;
747 break;
748 case GOTD_IMSG_NAK:
749 err = recv_nak(&imsg, ihave.object_id);
750 if (err)
751 break;
752 done = 1;
753 break;
754 default:
755 err = got_error(GOT_ERR_PRIVSEP_MSG);
756 break;
759 imsg_free(&imsg);
762 return err;
765 static const struct got_error *
766 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
768 const struct got_error *err;
769 struct imsg imsg;
771 *packfd = -1;
773 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
774 return got_error_from_errno("imsg_compose DONE");
776 err = gotd_imsg_flush(ibuf);
777 if (err)
778 return err;
780 while (*packfd == -1 && err == NULL) {
781 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
782 if (err)
783 break;
785 switch (imsg.hdr.type) {
786 case GOTD_IMSG_ERROR:
787 err = gotd_imsg_recv_error(NULL, &imsg);
788 break;
789 case GOTD_IMSG_PACKFILE_PIPE:
790 if (imsg.fd != -1)
791 *packfd = imsg.fd;
792 else
793 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
794 break;
795 default:
796 err = got_error(GOT_ERR_PRIVSEP_MSG);
797 break;
800 imsg_free(&imsg);
803 return err;
806 static const struct got_error *
807 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
809 const struct got_error *err = NULL;
810 int pack_starting = 0;
811 struct gotd_imsg_packfile_progress iprog;
812 char buf[GOT_PKT_MAX];
813 struct imsg imsg;
814 size_t datalen;
815 int p_deltify = 0, n;
816 const char *eol = "\r";
818 memset(&imsg, 0, sizeof(imsg));
820 while (!pack_starting && err == NULL) {
821 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
822 if (err)
823 break;
825 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
826 switch (imsg.hdr.type) {
827 case GOTD_IMSG_ERROR:
828 err = gotd_imsg_recv_error(NULL, &imsg);
829 break;
830 case GOTD_IMSG_PACKFILE_READY:
831 eol = "\n";
832 pack_starting = 1;
833 /* fallthrough */
834 case GOTD_IMSG_PACKFILE_PROGRESS:
835 if (datalen != sizeof(iprog)) {
836 err = got_error(GOT_ERR_PRIVSEP_LEN);
837 break;
839 memcpy(&iprog, imsg.data, sizeof(iprog));
840 if (iprog.nobj_total > 0) {
841 p_deltify = (iprog.nobj_deltify * 100) /
842 iprog.nobj_total;
844 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
845 n = snprintf(&buf[1], sizeof(buf) - 1,
846 "%d commits colored, "
847 "%d objects found, "
848 "deltify %d%%%s",
849 iprog.ncolored,
850 iprog.nfound,
851 p_deltify, eol);
852 if (n >= sizeof(buf) - 1)
853 break;
854 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
855 break;
856 default:
857 err = got_error(GOT_ERR_PRIVSEP_MSG);
858 break;
861 imsg_free(&imsg);
864 return err;
867 static const struct got_error *
868 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
869 int chattygot)
871 const struct got_error *err = NULL;
872 char buf[GOT_PKT_MAX];
873 struct imsgbuf ibuf;
874 enum protostate {
875 STATE_EXPECT_WANT,
876 STATE_EXPECT_MORE_WANT,
877 STATE_EXPECT_HAVE,
878 STATE_EXPECT_DONE,
879 STATE_DONE,
880 };
881 enum protostate curstate = STATE_EXPECT_WANT;
882 int have_ack = 0, use_sidebands = 0, seen_have = 0;
883 int packfd = -1;
884 size_t pack_chunksize;
886 imsg_init(&ibuf, gotd_sock);
888 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
889 if (err)
890 goto done;
892 while (curstate != STATE_DONE) {
893 int n;
894 buf[0] = '\0';
895 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
896 if (err)
897 break;
898 if (n == 0) {
899 if (curstate != STATE_EXPECT_MORE_WANT &&
900 curstate != STATE_EXPECT_HAVE) {
901 err = got_error_msg(GOT_ERR_BAD_PACKET,
902 "unexpected flush packet received");
903 goto done;
905 err = forward_flushpkt(&ibuf);
906 if (err)
907 goto done;
908 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
909 err = send_nak(outfd, chattygot);
910 if (err)
911 goto done;
913 if (curstate == STATE_EXPECT_MORE_WANT)
914 curstate = STATE_EXPECT_HAVE;
915 else
916 curstate = STATE_EXPECT_DONE;
917 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
918 if (curstate != STATE_EXPECT_WANT &&
919 curstate != STATE_EXPECT_MORE_WANT) {
920 err = got_error_msg(GOT_ERR_BAD_PACKET,
921 "unexpected 'want' packet");
922 goto done;
924 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
925 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
926 if (err)
927 goto done;
928 if (curstate == STATE_EXPECT_WANT)
929 curstate = STATE_EXPECT_MORE_WANT;
930 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
931 if (curstate != STATE_EXPECT_HAVE) {
932 err = got_error_msg(GOT_ERR_BAD_PACKET,
933 "unexpected 'have' packet");
934 goto done;
936 err = recv_have(&have_ack, outfd, &ibuf, buf, n,
937 chattygot);
938 if (err)
939 goto done;
940 seen_have = 1;
941 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
942 if (curstate != STATE_EXPECT_HAVE &&
943 curstate != STATE_EXPECT_DONE) {
944 err = got_error_msg(GOT_ERR_BAD_PACKET,
945 "unexpected 'done' packet");
946 goto done;
948 err = recv_done(&packfd, outfd, &ibuf, chattygot);
949 if (err)
950 goto done;
951 curstate = STATE_DONE;
952 break;
953 } else {
954 err = got_error(GOT_ERR_BAD_PACKET);
955 goto done;
959 if (!seen_have) {
960 err = send_nak(outfd, chattygot);
961 if (err)
962 goto done;
965 if (use_sidebands) {
966 err = relay_progress_reports(&ibuf, outfd, chattygot);
967 if (err)
968 goto done;
969 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
970 } else
971 pack_chunksize = sizeof(buf);
973 for (;;) {
974 ssize_t r;
976 r = read(packfd, use_sidebands ? &buf[1] : buf,
977 pack_chunksize);
978 if (r == -1) {
979 err = got_error_from_errno("read");
980 break;
981 } else if (r == 0) {
982 err = got_pkt_flushpkt(outfd, chattygot);
983 break;
986 if (use_sidebands) {
987 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
988 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
989 if (err)
990 break;
991 } else {
992 err = got_poll_write_full(outfd, buf, r);
993 if (err) {
994 if (err->code == GOT_ERR_EOF)
995 err = NULL;
996 break;
1000 done:
1001 imsg_clear(&ibuf);
1002 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1003 err = got_error_from_errno("close");
1004 if (err)
1005 echo_error(err, outfd, chattygot);
1006 return err;
1009 static const struct got_error *
1010 parse_ref_update_line(char **common_capabilities, char **refname,
1011 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1013 const struct got_error *err;
1014 char *old_id_str = NULL, *new_id_str = NULL;
1015 char *client_capabilities = NULL;
1017 *refname = NULL;
1019 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1020 refname, &client_capabilities, buf, len);
1021 if (err)
1022 return err;
1024 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1025 !got_parse_sha1_digest(new_id, new_id_str)) {
1026 err = got_error_msg(GOT_ERR_BAD_PACKET,
1027 "ref-update with bad object ID");
1028 goto done;
1030 if (!got_ref_name_is_valid(*refname)) {
1031 err = got_error_msg(GOT_ERR_BAD_PACKET,
1032 "ref-update with bad reference name");
1033 goto done;
1036 if (client_capabilities) {
1037 err = got_gitproto_match_capabilities(common_capabilities,
1038 NULL, client_capabilities, write_capabilities,
1039 nitems(write_capabilities));
1040 if (err)
1041 goto done;
1043 done:
1044 free(old_id_str);
1045 free(new_id_str);
1046 free(client_capabilities);
1047 if (err) {
1048 free(*refname);
1049 *refname = NULL;
1051 return err;
1054 static const struct got_error *
1055 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1056 char *buf, size_t len, int expect_capabilities, int chattygot)
1058 const struct got_error *err;
1059 struct gotd_imsg_ref_update iref;
1060 struct ibuf *wbuf;
1061 char *capabilities_str = NULL, *refname = NULL;
1062 int done = 0;
1063 struct imsg imsg;
1065 memset(&iref, 0, sizeof(iref));
1066 memset(&imsg, 0, sizeof(imsg));
1068 err = parse_ref_update_line(&capabilities_str, &refname,
1069 iref.old_id, iref.new_id, buf, len);
1070 if (err)
1071 return err;
1073 if (capabilities_str) {
1074 if (!expect_capabilities) {
1075 err = got_error_msg(GOT_ERR_BAD_PACKET,
1076 "unexpected capability announcement received");
1077 goto done;
1079 err = send_capabilities(NULL, report_status, capabilities_str,
1080 ibuf);
1081 if (err)
1082 goto done;
1085 iref.name_len = strlen(refname);
1086 len = sizeof(iref) + iref.name_len;
1087 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1088 if (wbuf == NULL) {
1089 err = got_error_from_errno("imsg_create REF_UPDATE");
1090 goto done;
1093 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1094 return got_error_from_errno("imsg_add REF_UPDATE");
1095 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1096 return got_error_from_errno("imsg_add REF_UPDATE");
1097 wbuf->fd = -1;
1098 imsg_close(ibuf, wbuf);
1100 err = gotd_imsg_flush(ibuf);
1101 if (err)
1102 goto done;
1104 /* Wait for ACK or an error. */
1105 while (!done && err == NULL) {
1106 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1107 if (err)
1108 break;
1109 switch (imsg.hdr.type) {
1110 case GOTD_IMSG_ERROR:
1111 err = gotd_imsg_recv_error(NULL, &imsg);
1112 break;
1113 case GOTD_IMSG_ACK:
1114 err = recv_ack(&imsg, iref.new_id);
1115 if (err)
1116 break;
1117 done = 1;
1118 break;
1119 default:
1120 err = got_error(GOT_ERR_PRIVSEP_MSG);
1121 break;
1124 imsg_free(&imsg);
1126 done:
1127 free(capabilities_str);
1128 free(refname);
1129 return err;
1132 static const struct got_error *
1133 recv_packfile(struct imsg *imsg, int infd)
1135 const struct got_error *err = NULL;
1136 size_t datalen;
1137 int packfd;
1138 char buf[GOT_PKT_MAX];
1139 int pack_done = 0;
1141 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1142 if (datalen != 0)
1143 return got_error(GOT_ERR_PRIVSEP_MSG);
1145 if (imsg->fd == -1)
1146 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1148 packfd = imsg->fd;
1149 while (!pack_done) {
1150 ssize_t r = 0;
1152 err = got_poll_fd(infd, POLLIN, 1);
1153 if (err) {
1154 if (err->code != GOT_ERR_TIMEOUT)
1155 break;
1156 err = NULL;
1157 } else {
1158 r = read(infd, buf, sizeof(buf));
1159 if (r == -1) {
1160 err = got_error_from_errno("read");
1161 break;
1163 if (r == 0) {
1165 * Git clients hang up their side of the
1166 * connection after sending the pack file.
1168 err = NULL;
1169 pack_done = 1;
1170 break;
1174 if (r == 0) {
1175 /* Detect gotd(8) closing the pack pipe when done. */
1176 err = got_poll_fd(packfd, POLLOUT, 1);
1177 if (err) {
1178 if (err->code != GOT_ERR_EOF)
1179 break;
1180 err = NULL;
1181 pack_done = 1;
1183 } else {
1184 /* Write pack data and/or detect pipe being closed. */
1185 err = got_poll_write_full(packfd, buf, r);
1186 if (err) {
1187 if (err->code == GOT_ERR_EOF)
1188 err = NULL;
1189 break;
1194 close(packfd);
1195 return err;
1198 static const struct got_error *
1199 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1201 const struct got_error *err = NULL;
1202 struct gotd_imsg_packfile_status istatus;
1203 char buf[GOT_PKT_MAX];
1204 size_t datalen, len;
1205 char *reason = NULL;
1207 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1208 if (datalen < sizeof(istatus))
1209 return got_error(GOT_ERR_PRIVSEP_LEN);
1210 memcpy(&istatus, imsg->data, sizeof(istatus));
1211 if (datalen != sizeof(istatus) + istatus.reason_len)
1212 return got_error(GOT_ERR_PRIVSEP_LEN);
1214 reason = malloc(istatus.reason_len + 1);
1215 if (reason == NULL) {
1216 err = got_error_from_errno("malloc");
1217 goto done;
1219 memcpy(reason, imsg->data + sizeof(istatus), istatus.reason_len);
1220 reason[istatus.reason_len] = '\0';
1222 if (err == NULL)
1223 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1224 else
1225 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1226 if (len >= sizeof(buf)) {
1227 err = got_error(GOT_ERR_NO_SPACE);
1228 goto done;
1231 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1232 done:
1233 free(reason);
1234 return err;
1237 static const struct got_error *
1238 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1240 const struct got_error *err = NULL;
1241 struct gotd_imsg_ref_update_ok iok;
1242 size_t datalen, len;
1243 char buf[GOT_PKT_MAX];
1244 char *refname = NULL;
1246 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1247 if (datalen < sizeof(iok))
1248 return got_error(GOT_ERR_PRIVSEP_LEN);
1249 memcpy(&iok, imsg->data, sizeof(iok));
1250 if (datalen != sizeof(iok) + iok.name_len)
1251 return got_error(GOT_ERR_PRIVSEP_LEN);
1253 memcpy(&iok, imsg->data, sizeof(iok));
1255 refname = malloc(iok.name_len + 1);
1256 if (refname == NULL)
1257 return got_error_from_errno("malloc");
1258 memcpy(refname, imsg->data + sizeof(iok), iok.name_len);
1259 refname[iok.name_len] = '\0';
1261 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1262 if (len >= sizeof(buf)) {
1263 err = got_error(GOT_ERR_NO_SPACE);
1264 goto done;
1267 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1268 done:
1269 free(refname);
1270 return err;
1273 static const struct got_error *
1274 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1276 const struct got_error *err = NULL;
1277 struct gotd_imsg_ref_update_ng ing;
1278 size_t datalen, len;
1279 char buf[GOT_PKT_MAX];
1280 char *refname = NULL, *reason = NULL;
1282 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1283 if (datalen < sizeof(ing))
1284 return got_error(GOT_ERR_PRIVSEP_LEN);
1285 memcpy(&ing, imsg->data, sizeof(ing));
1286 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1287 return got_error(GOT_ERR_PRIVSEP_LEN);
1289 memcpy(&ing, imsg->data, sizeof(ing));
1291 refname = malloc(ing.name_len + 1);
1292 if (refname == NULL)
1293 return got_error_from_errno("malloc");
1294 memcpy(refname, imsg->data + sizeof(ing), ing.name_len);
1295 refname[ing.name_len] = '\0';
1297 reason = malloc(ing.reason_len + 1);
1298 if (reason == NULL) {
1299 err = got_error_from_errno("malloc");
1300 goto done;
1302 memcpy(refname, imsg->data + sizeof(ing) + ing.name_len,
1303 ing.reason_len);
1304 refname[ing.reason_len] = '\0';
1306 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1307 if (len >= sizeof(buf)) {
1308 err = got_error(GOT_ERR_NO_SPACE);
1309 goto done;
1312 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1313 done:
1314 free(refname);
1315 free(reason);
1316 return err;
1319 static const struct got_error *
1320 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1321 int chattygot)
1323 const struct got_error *err = NULL;
1324 char buf[GOT_PKT_MAX];
1325 struct imsgbuf ibuf;
1326 enum protostate {
1327 STATE_EXPECT_REF_UPDATE,
1328 STATE_EXPECT_MORE_REF_UPDATES,
1329 STATE_EXPECT_PACKFILE,
1330 STATE_PACKFILE_RECEIVED,
1331 STATE_REFS_UPDATED,
1333 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1334 struct imsg imsg;
1335 int report_status = 0;
1337 imsg_init(&ibuf, gotd_sock);
1338 memset(&imsg, 0, sizeof(imsg));
1340 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1341 if (err)
1342 goto done;
1344 while (curstate != STATE_EXPECT_PACKFILE) {
1345 int n;
1346 buf[0] = '\0';
1347 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1348 if (err)
1349 break;
1350 if (n == 0) {
1351 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1352 err = got_error_msg(GOT_ERR_BAD_PACKET,
1353 "unexpected flush packet received");
1354 goto done;
1356 err = forward_flushpkt(&ibuf);
1357 if (err)
1358 goto done;
1359 curstate = STATE_EXPECT_PACKFILE;
1360 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1361 if (curstate != STATE_EXPECT_REF_UPDATE &&
1362 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1363 err = got_error_msg(GOT_ERR_BAD_PACKET,
1364 "unexpected ref-update packet");
1365 goto done;
1367 if (curstate == STATE_EXPECT_REF_UPDATE) {
1368 err = recv_ref_update(&report_status,
1369 outfd, &ibuf, buf, n, 1, chattygot);
1370 } else {
1371 err = recv_ref_update(NULL, outfd, &ibuf,
1372 buf, n, 0, chattygot);
1374 if (err)
1375 goto done;
1376 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1377 } else {
1378 err = got_error(GOT_ERR_BAD_PACKET);
1379 goto done;
1383 while (curstate != STATE_PACKFILE_RECEIVED) {
1384 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1385 if (err)
1386 goto done;
1387 switch (imsg.hdr.type) {
1388 case GOTD_IMSG_ERROR:
1389 err = gotd_imsg_recv_error(NULL, &imsg);
1390 goto done;
1391 case GOTD_IMSG_PACKFILE_PIPE:
1392 err = recv_packfile(&imsg, infd);
1393 if (err) {
1394 if (err->code != GOT_ERR_EOF)
1395 goto done;
1397 * EOF is reported when the client hangs up,
1398 * which can happen with Git clients.
1399 * The socket should stay half-open so we
1400 * can still send our reports if requested.
1402 err = NULL;
1404 curstate = STATE_PACKFILE_RECEIVED;
1405 break;
1406 default:
1407 err = got_error(GOT_ERR_PRIVSEP_MSG);
1408 break;
1411 imsg_free(&imsg);
1412 if (err)
1413 goto done;
1416 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1417 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1418 if (err)
1419 break;
1420 switch (imsg.hdr.type) {
1421 case GOTD_IMSG_ERROR:
1422 err = gotd_imsg_recv_error(NULL, &imsg);
1423 break;
1424 case GOTD_IMSG_PACKFILE_STATUS:
1425 if (!report_status)
1426 break;
1427 err = report_unpack_status(&imsg, outfd, chattygot);
1428 break;
1429 case GOTD_IMSG_REF_UPDATE_OK:
1430 if (!report_status)
1431 break;
1432 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1433 break;
1434 case GOTD_IMSG_REF_UPDATE_NG:
1435 if (!report_status)
1436 break;
1437 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1438 break;
1439 case GOTD_IMSG_REFS_UPDATED:
1440 curstate = STATE_REFS_UPDATED;
1441 err = got_pkt_flushpkt(outfd, chattygot);
1442 break;
1443 default:
1444 err = got_error(GOT_ERR_PRIVSEP_MSG);
1445 break;
1448 imsg_free(&imsg);
1450 done:
1451 imsg_clear(&ibuf);
1452 if (err)
1453 echo_error(err, outfd, chattygot);
1454 return err;
1457 const struct got_error *
1458 got_serve(int infd, int outfd, const char *gitcmd, int gotd_sock, int chattygot)
1460 const struct got_error *err = NULL;
1461 char *command = NULL, *repo_path = NULL;
1463 err = parse_command(&command, &repo_path, gitcmd);
1464 if (err)
1465 return err;
1467 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1468 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1469 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1470 err = serve_write(infd, outfd, gotd_sock, repo_path, chattygot);
1471 else
1472 err = got_error(GOT_ERR_BAD_PACKET);
1474 free(command);
1475 free(repo_path);
1476 return err;