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 { GOT_CAPA_DELETE_REFS, NULL },
63 };
65 const struct got_error *
66 got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
67 {
68 const struct got_error *err = NULL;
69 size_t len, cmdlen, pathlen;
70 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
71 const char *relpath;
73 *command = NULL;
74 *repo_path = NULL;
76 len = strlen(gitcmd);
78 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
79 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
80 strlen(GOT_SERVE_CMD_SEND)) == 0)
81 cmdlen = strlen(GOT_SERVE_CMD_SEND);
82 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
83 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
84 strlen(GOT_SERVE_CMD_FETCH)) == 0)
85 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
86 else
87 return got_error(GOT_ERR_BAD_PACKET);
89 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
90 return got_error(GOT_ERR_BAD_PACKET);
92 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
93 return got_error(GOT_ERR_BAD_PATH);
95 /* Forbid linefeeds in paths, like Git does. */
96 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
97 return got_error(GOT_ERR_BAD_PATH);
99 path0 = strdup(&gitcmd[cmdlen + 1]);
100 if (path0 == NULL)
101 return got_error_from_errno("strdup");
102 path = path0;
103 pathlen = strlen(path);
105 /*
106 * Git clients send a shell command.
107 * Trim spaces and quotes around the path.
108 */
109 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
110 path++;
111 pathlen--;
113 while (pathlen > 0 &&
114 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
115 path[pathlen - 1] == ' ')) {
116 path[pathlen - 1] = '\0';
117 pathlen--;
120 /* Deny an empty repository path. */
121 if (path[0] == '\0' || got_path_is_root_dir(path)) {
122 err = got_error(GOT_ERR_NOT_GIT_REPO);
123 goto done;
126 if (asprintf(&abspath, "/%s", path) == -1) {
127 err = got_error_from_errno("asprintf");
128 goto done;
130 pathlen = strlen(abspath);
131 canonpath = malloc(pathlen);
132 if (canonpath == NULL) {
133 err = got_error_from_errno("malloc");
134 goto done;
136 err = got_canonpath(abspath, canonpath, pathlen);
137 if (err)
138 goto done;
140 relpath = canonpath;
141 while (relpath[0] == '/')
142 relpath++;
143 *repo_path = strdup(relpath);
144 if (*repo_path == NULL) {
145 err = got_error_from_errno("strdup");
146 goto done;
148 *command = strndup(gitcmd, cmdlen);
149 if (*command == NULL)
150 err = got_error_from_errno("strndup");
151 done:
152 free(path0);
153 free(abspath);
154 free(canonpath);
155 if (err) {
156 free(*repo_path);
157 *repo_path = NULL;
159 return err;
162 static const struct got_error *
163 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
164 uint8_t *buf, size_t bufsize)
166 struct got_capability capa[nitems(read_capabilities) + 1];
167 size_t ncapa;
169 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
170 if (symrefstr) {
171 capa[nitems(read_capabilities)].key = "symref";
172 capa[nitems(read_capabilities)].value = symrefstr;
173 ncapa = nitems(capa);
174 } else
175 ncapa = nitems(read_capabilities);
177 return got_gitproto_append_capabilities(capalen, buf, len,
178 bufsize, capa, ncapa);
181 static const struct got_error *
182 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
183 int client_is_reading, const char *symrefstr, int chattygot)
185 const struct got_error *err = NULL;
186 char hex[SHA1_DIGEST_STRING_LENGTH];
187 char buf[GOT_PKT_MAX];
188 size_t len, capalen = 0;
190 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
191 return got_error(GOT_ERR_BAD_OBJ_ID);
193 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
194 if (len >= sizeof(buf))
195 return got_error(GOT_ERR_NO_SPACE);
197 if (send_capabilities) {
198 if (client_is_reading) {
199 err = append_read_capabilities(&capalen, len,
200 symrefstr, buf, sizeof(buf));
201 } else {
202 err = got_gitproto_append_capabilities(&capalen,
203 buf, len, sizeof(buf), write_capabilities,
204 nitems(write_capabilities));
206 if (err)
207 return err;
208 len += capalen;
211 if (len + 1 >= sizeof(buf))
212 return got_error(GOT_ERR_NO_SPACE);
213 buf[len] = '\n';
214 len++;
215 buf[len] = '\0';
217 return got_pkt_writepkt(outfd, buf, len, chattygot);
220 static const struct got_error *
221 send_zero_refs(int outfd, int client_is_reading, int chattygot)
223 const struct got_error *err = NULL;
224 char buf[GOT_PKT_MAX];
225 uint8_t zero[SHA1_DIGEST_LENGTH];
226 char hex[SHA1_DIGEST_STRING_LENGTH];
227 size_t len, capalen = 0;
229 memset(&zero, 0, sizeof(zero));
231 if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
232 return got_error(GOT_ERR_BAD_OBJ_ID);
234 len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
235 if (len >= sizeof(buf))
236 return got_error(GOT_ERR_NO_SPACE);
238 if (client_is_reading) {
239 err = got_gitproto_append_capabilities(&capalen, buf, len,
240 sizeof(buf), read_capabilities, nitems(read_capabilities));
241 if (err)
242 return err;
243 } else {
244 err = got_gitproto_append_capabilities(&capalen, buf, len,
245 sizeof(buf), write_capabilities,
246 nitems(write_capabilities));
247 if (err)
248 return err;
251 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
254 static void
255 echo_error(const struct got_error *err, int outfd, int chattygot)
257 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
258 size_t len;
260 /*
261 * Echo the error to the client on a pkt-line.
262 * The client should then terminate its session.
263 */
264 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
265 len = strlcat(buf, err->msg, sizeof(buf));
266 got_pkt_writepkt(outfd, buf, len, chattygot);
269 static const struct got_error *
270 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
271 const char *repo_path, int chattygot)
273 const struct got_error *err = NULL;
274 struct imsg imsg;
275 size_t datalen;
276 struct gotd_imsg_list_refs lsref;
277 struct gotd_imsg_reflist ireflist;
278 struct gotd_imsg_ref iref;
279 struct gotd_imsg_symref isymref;
280 size_t nrefs = 0;
281 int have_nrefs = 0, sent_capabilities = 0;
282 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
283 char *refname = NULL;
285 memset(&imsg, 0, sizeof(imsg));
286 memset(&lsref, 0, sizeof(lsref));
288 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
289 sizeof(lsref.repo_name))
290 return got_error(GOT_ERR_NO_SPACE);
291 lsref.client_is_reading = client_is_reading;
293 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
294 &lsref, sizeof(lsref)) == -1)
295 return got_error_from_errno("imsg_compose LIST_REFS");
297 err = gotd_imsg_flush(ibuf);
298 if (err)
299 return err;
301 while (!have_nrefs || nrefs > 0) {
302 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
303 if (err)
304 goto done;
305 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
306 switch (imsg.hdr.type) {
307 case GOTD_IMSG_ERROR:
308 err = gotd_imsg_recv_error(NULL, &imsg);
309 goto done;
310 case GOTD_IMSG_REFLIST:
311 if (have_nrefs || nrefs > 0) {
312 err = got_error(GOT_ERR_PRIVSEP_MSG);
313 goto done;
315 if (datalen != sizeof(ireflist)) {
316 err = got_error(GOT_ERR_PRIVSEP_MSG);
317 goto done;
319 memcpy(&ireflist, imsg.data, sizeof(ireflist));
320 nrefs = ireflist.nrefs;
321 have_nrefs = 1;
322 if (nrefs == 0)
323 err = send_zero_refs(outfd, client_is_reading,
324 chattygot);
325 break;
326 case GOTD_IMSG_REF:
327 if (!have_nrefs || nrefs == 0) {
328 err = got_error(GOT_ERR_PRIVSEP_MSG);
329 goto done;
331 if (datalen < sizeof(iref)) {
332 err = got_error(GOT_ERR_PRIVSEP_MSG);
333 goto done;
335 memcpy(&iref, imsg.data, sizeof(iref));
336 if (datalen != sizeof(iref) + iref.name_len) {
337 err = got_error(GOT_ERR_PRIVSEP_LEN);
338 goto done;
340 refname = strndup(imsg.data + sizeof(iref),
341 iref.name_len);
342 if (refname == NULL) {
343 err = got_error_from_errno("strndup");
344 goto done;
346 err = send_ref(outfd, iref.id, refname,
347 !sent_capabilities, client_is_reading,
348 NULL, chattygot);
349 free(refname);
350 refname = NULL;
351 if (err)
352 goto done;
353 sent_capabilities = 1;
354 if (nrefs > 0)
355 nrefs--;
356 break;
357 case GOTD_IMSG_SYMREF:
358 if (!have_nrefs || nrefs == 0) {
359 err = got_error(GOT_ERR_PRIVSEP_MSG);
360 goto done;
362 if (datalen < sizeof(isymref)) {
363 err = got_error(GOT_ERR_PRIVSEP_LEN);
364 goto done;
366 memcpy(&isymref, imsg.data, sizeof(isymref));
367 if (datalen != sizeof(isymref) + isymref.name_len +
368 isymref.target_len) {
369 err = got_error(GOT_ERR_PRIVSEP_LEN);
370 goto done;
373 /*
374 * For now, we only announce one symbolic ref,
375 * as part of our capability advertisement.
376 */
377 if (sent_capabilities || symrefstr != NULL ||
378 symrefname != NULL || symreftarget != NULL)
379 break;
381 symrefname = strndup(imsg.data + sizeof(isymref),
382 isymref.name_len);
383 if (symrefname == NULL) {
384 err = got_error_from_errno("malloc");
385 goto done;
388 symreftarget = strndup(
389 imsg.data + sizeof(isymref) + isymref.name_len,
390 isymref.target_len);
391 if (symreftarget == NULL) {
392 err = got_error_from_errno("strndup");
393 goto done;
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 goto done;
898 if (n == 0) {
899 if (curstate != STATE_EXPECT_WANT &&
900 curstate != STATE_EXPECT_MORE_WANT &&
901 curstate != STATE_EXPECT_HAVE &&
902 curstate != STATE_EXPECT_DONE) {
903 err = got_error_msg(GOT_ERR_BAD_PACKET,
904 "unexpected flush packet received");
905 goto done;
908 if (curstate == STATE_EXPECT_WANT) {
909 ssize_t r;
910 /*
911 * If the client does not want to fetch
912 * anything we should receive a flush
913 * packet followed by EOF.
914 */
915 r = read(infd, buf, sizeof(buf));
916 if (r == -1) {
917 err = got_error_from_errno("read");
918 goto done;
920 if (r == 0) /* EOF */
921 goto done;
923 /* Zero-length field followed by payload. */
924 err = got_error_msg(GOT_ERR_BAD_PACKET,
925 "unexpected flush packet received");
926 goto done;
929 if (curstate == STATE_EXPECT_WANT ||
930 curstate == STATE_EXPECT_MORE_WANT ||
931 curstate == STATE_EXPECT_HAVE) {
932 err = forward_flushpkt(&ibuf);
933 if (err)
934 goto done;
936 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
937 err = send_nak(outfd, chattygot);
938 if (err)
939 goto done;
941 if (curstate == STATE_EXPECT_MORE_WANT)
942 curstate = STATE_EXPECT_HAVE;
943 else
944 curstate = STATE_EXPECT_DONE;
945 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
946 if (curstate != STATE_EXPECT_WANT &&
947 curstate != STATE_EXPECT_MORE_WANT) {
948 err = got_error_msg(GOT_ERR_BAD_PACKET,
949 "unexpected 'want' packet");
950 goto done;
952 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
953 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
954 if (err)
955 goto done;
956 if (curstate == STATE_EXPECT_WANT)
957 curstate = STATE_EXPECT_MORE_WANT;
958 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
959 if (curstate != STATE_EXPECT_HAVE &&
960 curstate != STATE_EXPECT_DONE) {
961 err = got_error_msg(GOT_ERR_BAD_PACKET,
962 "unexpected 'have' packet");
963 goto done;
965 if (curstate == STATE_EXPECT_HAVE) {
966 err = recv_have(&have_ack, outfd, &ibuf,
967 buf, n, chattygot);
968 if (err)
969 goto done;
970 seen_have = 1;
971 if (have_ack)
972 curstate = STATE_EXPECT_DONE;
974 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
975 if (curstate != STATE_EXPECT_HAVE &&
976 curstate != STATE_EXPECT_DONE) {
977 err = got_error_msg(GOT_ERR_BAD_PACKET,
978 "unexpected 'done' packet");
979 goto done;
981 err = recv_done(&packfd, outfd, &ibuf, chattygot);
982 if (err)
983 goto done;
984 curstate = STATE_DONE;
985 break;
986 } else {
987 err = got_error(GOT_ERR_BAD_PACKET);
988 goto done;
992 if (!seen_have) {
993 err = send_nak(outfd, chattygot);
994 if (err)
995 goto done;
998 if (use_sidebands) {
999 err = relay_progress_reports(&ibuf, outfd, chattygot);
1000 if (err)
1001 goto done;
1002 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
1003 } else
1004 pack_chunksize = sizeof(buf);
1006 for (;;) {
1007 ssize_t r;
1009 r = read(packfd, use_sidebands ? &buf[1] : buf,
1010 pack_chunksize);
1011 if (r == -1) {
1012 err = got_error_from_errno("read");
1013 break;
1014 } else if (r == 0) {
1015 err = got_pkt_flushpkt(outfd, chattygot);
1016 break;
1019 if (use_sidebands) {
1020 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1021 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1022 if (err)
1023 break;
1024 } else {
1025 err = got_poll_write_full(outfd, buf, r);
1026 if (err) {
1027 if (err->code == GOT_ERR_EOF)
1028 err = NULL;
1029 break;
1033 done:
1034 imsg_clear(&ibuf);
1035 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1036 err = got_error_from_errno("close");
1037 if (err)
1038 echo_error(err, outfd, chattygot);
1039 return err;
1042 static const struct got_error *
1043 parse_ref_update_line(char **common_capabilities, char **refname,
1044 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1046 const struct got_error *err;
1047 char *old_id_str = NULL, *new_id_str = NULL;
1048 char *client_capabilities = NULL;
1050 *refname = NULL;
1052 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1053 refname, &client_capabilities, buf, len);
1054 if (err)
1055 return err;
1057 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1058 !got_parse_sha1_digest(new_id, new_id_str)) {
1059 err = got_error_msg(GOT_ERR_BAD_PACKET,
1060 "ref-update with bad object ID");
1061 goto done;
1063 if (!got_ref_name_is_valid(*refname)) {
1064 err = got_error_msg(GOT_ERR_BAD_PACKET,
1065 "ref-update with bad reference name");
1066 goto done;
1069 if (client_capabilities) {
1070 err = got_gitproto_match_capabilities(common_capabilities,
1071 NULL, client_capabilities, write_capabilities,
1072 nitems(write_capabilities));
1073 if (err)
1074 goto done;
1076 done:
1077 free(old_id_str);
1078 free(new_id_str);
1079 free(client_capabilities);
1080 if (err) {
1081 free(*refname);
1082 *refname = NULL;
1084 return err;
1087 static const struct got_error *
1088 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1089 char *buf, size_t len, int expect_capabilities, int chattygot)
1091 const struct got_error *err;
1092 struct gotd_imsg_ref_update iref;
1093 struct ibuf *wbuf;
1094 char *capabilities_str = NULL, *refname = NULL;
1095 int done = 0;
1096 struct imsg imsg;
1098 memset(&iref, 0, sizeof(iref));
1099 memset(&imsg, 0, sizeof(imsg));
1101 err = parse_ref_update_line(&capabilities_str, &refname,
1102 iref.old_id, iref.new_id, buf, len);
1103 if (err)
1104 return err;
1106 if (capabilities_str) {
1107 if (!expect_capabilities) {
1108 err = got_error_msg(GOT_ERR_BAD_PACKET,
1109 "unexpected capability announcement received");
1110 goto done;
1112 err = send_capabilities(NULL, report_status, capabilities_str,
1113 ibuf);
1114 if (err)
1115 goto done;
1118 iref.name_len = strlen(refname);
1119 len = sizeof(iref) + iref.name_len;
1120 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1121 if (wbuf == NULL) {
1122 err = got_error_from_errno("imsg_create REF_UPDATE");
1123 goto done;
1126 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1127 return got_error_from_errno("imsg_add REF_UPDATE");
1128 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1129 return got_error_from_errno("imsg_add REF_UPDATE");
1130 wbuf->fd = -1;
1131 imsg_close(ibuf, wbuf);
1133 err = gotd_imsg_flush(ibuf);
1134 if (err)
1135 goto done;
1137 /* Wait for ACK or an error. */
1138 while (!done && err == NULL) {
1139 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1140 if (err)
1141 break;
1142 switch (imsg.hdr.type) {
1143 case GOTD_IMSG_ERROR:
1144 err = gotd_imsg_recv_error(NULL, &imsg);
1145 break;
1146 case GOTD_IMSG_ACK:
1147 err = recv_ack(&imsg, iref.new_id);
1148 if (err)
1149 break;
1150 done = 1;
1151 break;
1152 default:
1153 err = got_error(GOT_ERR_PRIVSEP_MSG);
1154 break;
1157 imsg_free(&imsg);
1159 done:
1160 free(capabilities_str);
1161 free(refname);
1162 return err;
1165 static const struct got_error *
1166 recv_packfile(struct imsg *imsg, int infd)
1168 const struct got_error *err = NULL;
1169 size_t datalen;
1170 int packfd;
1171 char buf[GOT_PKT_MAX];
1172 int pack_done = 0;
1174 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1175 if (datalen != 0)
1176 return got_error(GOT_ERR_PRIVSEP_MSG);
1178 if (imsg->fd == -1)
1179 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1181 packfd = imsg->fd;
1182 while (!pack_done) {
1183 ssize_t r = 0;
1185 err = got_poll_fd(infd, POLLIN, 1);
1186 if (err) {
1187 if (err->code != GOT_ERR_TIMEOUT)
1188 break;
1189 err = NULL;
1190 } else {
1191 r = read(infd, buf, sizeof(buf));
1192 if (r == -1) {
1193 err = got_error_from_errno("read");
1194 break;
1196 if (r == 0) {
1198 * Git clients hang up their side of the
1199 * connection after sending the pack file.
1201 err = NULL;
1202 pack_done = 1;
1203 break;
1207 if (r == 0) {
1208 /* Detect gotd(8) closing the pack pipe when done. */
1209 err = got_poll_fd(packfd, POLLOUT, 1);
1210 if (err) {
1211 if (err->code != GOT_ERR_EOF)
1212 break;
1213 err = NULL;
1214 pack_done = 1;
1216 } else {
1217 /* Write pack data and/or detect pipe being closed. */
1218 err = got_poll_write_full(packfd, buf, r);
1219 if (err) {
1220 if (err->code == GOT_ERR_EOF)
1221 err = NULL;
1222 break;
1227 close(packfd);
1228 return err;
1231 static const struct got_error *
1232 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1234 const struct got_error *err = NULL;
1235 struct gotd_imsg_packfile_status istatus;
1236 char buf[GOT_PKT_MAX];
1237 size_t datalen, len;
1238 char *reason = NULL;
1240 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1241 if (datalen < sizeof(istatus))
1242 return got_error(GOT_ERR_PRIVSEP_LEN);
1243 memcpy(&istatus, imsg->data, sizeof(istatus));
1244 if (datalen != sizeof(istatus) + istatus.reason_len)
1245 return got_error(GOT_ERR_PRIVSEP_LEN);
1247 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1248 if (reason == NULL) {
1249 err = got_error_from_errno("strndup");
1250 goto done;
1253 if (err == NULL)
1254 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1255 else
1256 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1257 if (len >= sizeof(buf)) {
1258 err = got_error(GOT_ERR_NO_SPACE);
1259 goto done;
1262 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1263 done:
1264 free(reason);
1265 return err;
1268 static const struct got_error *
1269 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1271 const struct got_error *err = NULL;
1272 struct gotd_imsg_ref_update_ok iok;
1273 size_t datalen, len;
1274 char buf[GOT_PKT_MAX];
1275 char *refname = NULL;
1277 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1278 if (datalen < sizeof(iok))
1279 return got_error(GOT_ERR_PRIVSEP_LEN);
1280 memcpy(&iok, imsg->data, sizeof(iok));
1281 if (datalen != sizeof(iok) + iok.name_len)
1282 return got_error(GOT_ERR_PRIVSEP_LEN);
1284 memcpy(&iok, imsg->data, sizeof(iok));
1286 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1287 if (refname == NULL)
1288 return got_error_from_errno("strndup");
1290 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1291 if (len >= sizeof(buf)) {
1292 err = got_error(GOT_ERR_NO_SPACE);
1293 goto done;
1296 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1297 done:
1298 free(refname);
1299 return err;
1302 static const struct got_error *
1303 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1305 const struct got_error *err = NULL;
1306 struct gotd_imsg_ref_update_ng ing;
1307 size_t datalen, len;
1308 char buf[GOT_PKT_MAX];
1309 char *refname = NULL, *reason = NULL;
1311 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1312 if (datalen < sizeof(ing))
1313 return got_error(GOT_ERR_PRIVSEP_LEN);
1314 memcpy(&ing, imsg->data, sizeof(ing));
1315 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1316 return got_error(GOT_ERR_PRIVSEP_LEN);
1318 memcpy(&ing, imsg->data, sizeof(ing));
1320 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1321 if (refname == NULL)
1322 return got_error_from_errno("strndup");
1324 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1325 ing.reason_len);
1326 if (reason == NULL) {
1327 err = got_error_from_errno("strndup");
1328 goto done;
1331 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1332 if (len >= sizeof(buf)) {
1333 err = got_error(GOT_ERR_NO_SPACE);
1334 goto done;
1337 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1338 done:
1339 free(refname);
1340 free(reason);
1341 return err;
1344 static const struct got_error *
1345 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1346 int chattygot)
1348 const struct got_error *err = NULL;
1349 char buf[GOT_PKT_MAX];
1350 struct imsgbuf ibuf;
1351 enum protostate {
1352 STATE_EXPECT_REF_UPDATE,
1353 STATE_EXPECT_MORE_REF_UPDATES,
1354 STATE_EXPECT_PACKFILE,
1355 STATE_PACKFILE_RECEIVED,
1356 STATE_REFS_UPDATED,
1358 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1359 struct imsg imsg;
1360 int report_status = 0;
1362 imsg_init(&ibuf, gotd_sock);
1363 memset(&imsg, 0, sizeof(imsg));
1365 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1366 if (err)
1367 goto done;
1369 while (curstate != STATE_EXPECT_PACKFILE) {
1370 int n;
1371 buf[0] = '\0';
1372 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1373 if (err)
1374 goto done;
1375 if (n == 0) {
1376 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1377 err = got_error_msg(GOT_ERR_BAD_PACKET,
1378 "unexpected flush packet received");
1379 goto done;
1381 err = forward_flushpkt(&ibuf);
1382 if (err)
1383 goto done;
1384 curstate = STATE_EXPECT_PACKFILE;
1385 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1386 if (curstate != STATE_EXPECT_REF_UPDATE &&
1387 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1388 err = got_error_msg(GOT_ERR_BAD_PACKET,
1389 "unexpected ref-update packet");
1390 goto done;
1392 if (curstate == STATE_EXPECT_REF_UPDATE) {
1393 err = recv_ref_update(&report_status,
1394 outfd, &ibuf, buf, n, 1, chattygot);
1395 } else {
1396 err = recv_ref_update(NULL, outfd, &ibuf,
1397 buf, n, 0, chattygot);
1399 if (err)
1400 goto done;
1401 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1402 } else {
1403 err = got_error(GOT_ERR_BAD_PACKET);
1404 goto done;
1408 while (curstate != STATE_PACKFILE_RECEIVED) {
1409 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1410 if (err)
1411 goto done;
1412 switch (imsg.hdr.type) {
1413 case GOTD_IMSG_ERROR:
1414 err = gotd_imsg_recv_error(NULL, &imsg);
1415 goto done;
1416 case GOTD_IMSG_PACKFILE_PIPE:
1417 err = recv_packfile(&imsg, infd);
1418 if (err) {
1419 if (err->code != GOT_ERR_EOF)
1420 goto done;
1422 * EOF is reported when the client hangs up,
1423 * which can happen with Git clients.
1424 * The socket should stay half-open so we
1425 * can still send our reports if requested.
1427 err = NULL;
1429 curstate = STATE_PACKFILE_RECEIVED;
1430 break;
1431 default:
1432 err = got_error(GOT_ERR_PRIVSEP_MSG);
1433 break;
1436 imsg_free(&imsg);
1437 if (err)
1438 goto done;
1441 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1442 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1443 if (err)
1444 break;
1445 switch (imsg.hdr.type) {
1446 case GOTD_IMSG_ERROR:
1447 err = gotd_imsg_recv_error(NULL, &imsg);
1448 break;
1449 case GOTD_IMSG_PACKFILE_STATUS:
1450 if (!report_status)
1451 break;
1452 err = report_unpack_status(&imsg, outfd, chattygot);
1453 break;
1454 case GOTD_IMSG_REF_UPDATE_OK:
1455 if (!report_status)
1456 break;
1457 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1458 break;
1459 case GOTD_IMSG_REF_UPDATE_NG:
1460 if (!report_status)
1461 break;
1462 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1463 break;
1464 case GOTD_IMSG_REFS_UPDATED:
1465 curstate = STATE_REFS_UPDATED;
1466 err = got_pkt_flushpkt(outfd, chattygot);
1467 break;
1468 default:
1469 err = got_error(GOT_ERR_PRIVSEP_MSG);
1470 break;
1473 imsg_free(&imsg);
1475 done:
1476 imsg_clear(&ibuf);
1477 if (err)
1478 echo_error(err, outfd, chattygot);
1479 return err;
1482 const struct got_error *
1483 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1484 int gotd_sock, int chattygot)
1486 const struct got_error *err = NULL;
1488 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1489 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1490 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1491 err = serve_write(infd, outfd, gotd_sock, repo_path,
1492 chattygot);
1493 else
1494 err = got_error(GOT_ERR_BAD_PACKET);
1496 return err;