Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <errno.h>
22 #include <event.h>
23 #include <poll.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <sha2.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <imsg.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_serve.h"
36 #include "got_path.h"
37 #include "got_version.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_lib_pkt.h"
42 #include "got_lib_dial.h"
43 #include "got_lib_gitproto.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_poll.h"
47 #include "gotd.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static const struct got_capability read_capabilities[] = {
54 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
55 { GOT_CAPA_OFS_DELTA, NULL },
56 { GOT_CAPA_SIDE_BAND_64K, NULL },
57 };
59 static const struct got_capability write_capabilities[] = {
60 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
61 { GOT_CAPA_OFS_DELTA, NULL },
62 { GOT_CAPA_REPORT_STATUS, NULL },
63 { GOT_CAPA_NO_THIN, NULL },
64 { GOT_CAPA_DELETE_REFS, NULL },
65 };
67 const struct got_error *
68 got_serve_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 client_is_reading, 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 if (client_is_reading) {
241 err = got_gitproto_append_capabilities(&capalen, buf, len,
242 sizeof(buf), read_capabilities, nitems(read_capabilities));
243 if (err)
244 return err;
245 } else {
246 err = got_gitproto_append_capabilities(&capalen, buf, len,
247 sizeof(buf), write_capabilities,
248 nitems(write_capabilities));
249 if (err)
250 return err;
253 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
256 static void
257 echo_error(const struct got_error *err, int outfd, int chattygot)
259 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
260 size_t len;
262 /*
263 * Echo the error to the client on a pkt-line.
264 * The client should then terminate its session.
265 */
266 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
267 len = strlcat(buf, err->msg, sizeof(buf));
268 got_pkt_writepkt(outfd, buf, len, chattygot);
271 static const struct got_error *
272 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
273 const char *repo_path, int chattygot)
275 const struct got_error *err = NULL;
276 struct imsg imsg;
277 size_t datalen;
278 struct gotd_imsg_list_refs lsref;
279 struct gotd_imsg_reflist ireflist;
280 struct gotd_imsg_ref iref;
281 struct gotd_imsg_symref isymref;
282 size_t nrefs = 0;
283 int have_nrefs = 0, sent_capabilities = 0;
284 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
285 char *refname = NULL;
287 memset(&imsg, 0, sizeof(imsg));
288 memset(&lsref, 0, sizeof(lsref));
290 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
291 sizeof(lsref.repo_name))
292 return got_error(GOT_ERR_NO_SPACE);
293 lsref.client_is_reading = client_is_reading;
295 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
296 &lsref, sizeof(lsref)) == -1)
297 return got_error_from_errno("imsg_compose LIST_REFS");
299 err = gotd_imsg_flush(ibuf);
300 if (err)
301 return err;
303 while (!have_nrefs || nrefs > 0) {
304 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
305 if (err)
306 goto done;
307 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
308 switch (imsg.hdr.type) {
309 case GOTD_IMSG_ERROR:
310 err = gotd_imsg_recv_error(NULL, &imsg);
311 goto done;
312 case GOTD_IMSG_REFLIST:
313 if (have_nrefs || nrefs > 0) {
314 err = got_error(GOT_ERR_PRIVSEP_MSG);
315 goto done;
317 if (datalen != sizeof(ireflist)) {
318 err = got_error(GOT_ERR_PRIVSEP_MSG);
319 goto done;
321 memcpy(&ireflist, imsg.data, sizeof(ireflist));
322 nrefs = ireflist.nrefs;
323 have_nrefs = 1;
324 if (nrefs == 0)
325 err = send_zero_refs(outfd, client_is_reading,
326 chattygot);
327 break;
328 case GOTD_IMSG_REF:
329 if (!have_nrefs || nrefs == 0) {
330 err = got_error(GOT_ERR_PRIVSEP_MSG);
331 goto done;
333 if (datalen < sizeof(iref)) {
334 err = got_error(GOT_ERR_PRIVSEP_MSG);
335 goto done;
337 memcpy(&iref, imsg.data, sizeof(iref));
338 if (datalen != sizeof(iref) + iref.name_len) {
339 err = got_error(GOT_ERR_PRIVSEP_LEN);
340 goto done;
342 refname = strndup(imsg.data + sizeof(iref),
343 iref.name_len);
344 if (refname == NULL) {
345 err = got_error_from_errno("strndup");
346 goto done;
348 err = send_ref(outfd, iref.id, refname,
349 !sent_capabilities, client_is_reading,
350 NULL, chattygot);
351 free(refname);
352 refname = NULL;
353 if (err)
354 goto done;
355 sent_capabilities = 1;
356 if (nrefs > 0)
357 nrefs--;
358 break;
359 case GOTD_IMSG_SYMREF:
360 if (!have_nrefs || nrefs == 0) {
361 err = got_error(GOT_ERR_PRIVSEP_MSG);
362 goto done;
364 if (datalen < sizeof(isymref)) {
365 err = got_error(GOT_ERR_PRIVSEP_LEN);
366 goto done;
368 memcpy(&isymref, imsg.data, sizeof(isymref));
369 if (datalen != sizeof(isymref) + isymref.name_len +
370 isymref.target_len) {
371 err = got_error(GOT_ERR_PRIVSEP_LEN);
372 goto done;
375 /*
376 * For now, we only announce one symbolic ref,
377 * as part of our capability advertisement.
378 */
379 if (sent_capabilities || symrefstr != NULL ||
380 symrefname != NULL || symreftarget != NULL)
381 break;
383 symrefname = strndup(imsg.data + sizeof(isymref),
384 isymref.name_len);
385 if (symrefname == NULL) {
386 err = got_error_from_errno("malloc");
387 goto done;
390 symreftarget = strndup(
391 imsg.data + sizeof(isymref) + isymref.name_len,
392 isymref.target_len);
393 if (symreftarget == NULL) {
394 err = got_error_from_errno("strndup");
395 goto done;
398 if (asprintf(&symrefstr, "%s:%s", symrefname,
399 symreftarget) == -1) {
400 err = got_error_from_errno("asprintf");
401 goto done;
403 err = send_ref(outfd, isymref.target_id, symrefname,
404 !sent_capabilities, client_is_reading, symrefstr,
405 chattygot);
406 free(refname);
407 refname = NULL;
408 if (err)
409 goto done;
410 sent_capabilities = 1;
411 if (nrefs > 0)
412 nrefs--;
413 break;
414 default:
415 err = got_error(GOT_ERR_PRIVSEP_MSG);
416 break;
419 imsg_free(&imsg);
422 err = got_pkt_flushpkt(outfd, chattygot);
423 if (err)
424 goto done;
425 done:
426 free(symrefstr);
427 free(symrefname);
428 free(symreftarget);
429 return err;
432 static const struct got_error *
433 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
435 const struct got_error *err;
436 char *id_str = NULL, *client_capabilities = NULL;
438 err = got_gitproto_parse_want_line(&id_str,
439 &client_capabilities, buf, len);
440 if (err)
441 return err;
443 if (!got_parse_sha1_digest(id, id_str)) {
444 err = got_error_msg(GOT_ERR_BAD_PACKET,
445 "want-line with bad object ID");
446 goto done;
449 if (client_capabilities) {
450 err = got_gitproto_match_capabilities(common_capabilities,
451 NULL, client_capabilities, read_capabilities,
452 nitems(read_capabilities));
453 if (err)
454 goto done;
456 done:
457 free(id_str);
458 free(client_capabilities);
459 return err;
462 static const struct got_error *
463 parse_have_line(uint8_t *id, char *buf, size_t len)
465 const struct got_error *err;
466 char *id_str = NULL;
468 err = got_gitproto_parse_have_line(&id_str, buf, len);
469 if (err)
470 return err;
472 if (!got_parse_sha1_digest(id, id_str)) {
473 err = got_error_msg(GOT_ERR_BAD_PACKET,
474 "have-line with bad object ID");
475 goto done;
477 done:
478 free(id_str);
479 return err;
482 static const struct got_error *
483 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
485 const struct got_error *err = NULL;
486 struct gotd_imsg_capability icapa;
487 size_t len;
488 struct ibuf *wbuf;
490 memset(&icapa, 0, sizeof(icapa));
492 icapa.key_len = strlen(capa->key);
493 len = sizeof(icapa) + icapa.key_len;
494 if (capa->value) {
495 icapa.value_len = strlen(capa->value);
496 len += icapa.value_len;
499 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
500 if (wbuf == NULL) {
501 err = got_error_from_errno("imsg_create CAPABILITY");
502 return err;
505 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
506 return got_error_from_errno("imsg_add CAPABILITY");
507 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
508 return got_error_from_errno("imsg_add CAPABILITY");
509 if (capa->value) {
510 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
511 return got_error_from_errno("imsg_add CAPABILITY");
514 wbuf->fd = -1;
515 imsg_close(ibuf, wbuf);
517 return NULL;
520 static const struct got_error *
521 send_capabilities(int *use_sidebands, int *report_status,
522 char *capabilities_str, struct imsgbuf *ibuf)
524 const struct got_error *err = NULL;
525 struct gotd_imsg_capabilities icapas;
526 struct got_capability *capa = NULL;
527 size_t ncapa, i;
529 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
530 capabilities_str);
531 if (err)
532 return err;
534 icapas.ncapabilities = ncapa;
535 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
536 &icapas, sizeof(icapas)) == -1) {
537 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
538 goto done;
541 for (i = 0; i < ncapa; i++) {
542 err = send_capability(&capa[i], ibuf);
543 if (err)
544 goto done;
545 if (use_sidebands &&
546 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
547 *use_sidebands = 1;
548 if (report_status &&
549 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
550 *report_status = 1;
552 done:
553 free(capa);
554 return err;
557 static const struct got_error *
558 forward_flushpkt(struct imsgbuf *ibuf)
560 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
561 return got_error_from_errno("imsg_compose FLUSH");
563 return gotd_imsg_flush(ibuf);
566 static const struct got_error *
567 recv_ack(struct imsg *imsg, uint8_t *expected_id)
569 struct gotd_imsg_ack iack;
570 size_t datalen;
572 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
573 if (datalen != sizeof(iack))
574 return got_error(GOT_ERR_PRIVSEP_LEN);
576 memcpy(&iack, imsg->data, sizeof(iack));
577 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
578 return got_error(GOT_ERR_BAD_OBJ_ID);
580 return NULL;
583 static const struct got_error *
584 recv_nak(struct imsg *imsg, uint8_t *expected_id)
586 struct gotd_imsg_ack inak;
587 size_t datalen;
589 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
590 if (datalen != sizeof(inak))
591 return got_error(GOT_ERR_PRIVSEP_LEN);
593 memcpy(&inak, imsg->data, sizeof(inak));
594 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
595 return got_error(GOT_ERR_BAD_OBJ_ID);
597 return NULL;
601 static const struct got_error *
602 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
603 char *buf, size_t len, int expect_capabilities, int chattygot)
605 const struct got_error *err;
606 struct gotd_imsg_want iwant;
607 char *capabilities_str;
608 int done = 0;
609 struct imsg imsg;
611 memset(&iwant, 0, sizeof(iwant));
612 memset(&imsg, 0, sizeof(imsg));
614 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
615 if (err)
616 return err;
618 if (capabilities_str) {
619 if (!expect_capabilities) {
620 err = got_error_msg(GOT_ERR_BAD_PACKET,
621 "unexpected capability announcement received");
622 goto done;
624 err = send_capabilities(use_sidebands, NULL, capabilities_str,
625 ibuf);
626 if (err)
627 goto done;
631 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
632 &iwant, sizeof(iwant)) == -1) {
633 err = got_error_from_errno("imsg_compose WANT");
634 goto done;
637 err = gotd_imsg_flush(ibuf);
638 if (err)
639 goto done;
641 /*
642 * Wait for an ACK, or an error in case the desired object
643 * does not exist.
644 */
645 while (!done && err == NULL) {
646 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
647 if (err)
648 break;
649 switch (imsg.hdr.type) {
650 case GOTD_IMSG_ERROR:
651 err = gotd_imsg_recv_error(NULL, &imsg);
652 break;
653 case GOTD_IMSG_ACK:
654 err = recv_ack(&imsg, iwant.object_id);
655 if (err)
656 break;
657 done = 1;
658 break;
659 default:
660 err = got_error(GOT_ERR_PRIVSEP_MSG);
661 break;
664 imsg_free(&imsg);
666 done:
667 free(capabilities_str);
668 return err;
671 static const struct got_error *
672 send_ack(int outfd, uint8_t *id, int chattygot)
674 char hex[SHA1_DIGEST_STRING_LENGTH];
675 char buf[GOT_PKT_MAX];
676 int len;
678 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
679 return got_error(GOT_ERR_BAD_OBJ_ID);
681 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
682 if (len >= sizeof(buf))
683 return got_error(GOT_ERR_NO_SPACE);
685 return got_pkt_writepkt(outfd, buf, len, chattygot);
688 static const struct got_error *
689 send_nak(int outfd, int chattygot)
691 char buf[5];
692 int len;
694 len = snprintf(buf, sizeof(buf), "NAK\n");
695 if (len >= sizeof(buf))
696 return got_error(GOT_ERR_NO_SPACE);
698 return got_pkt_writepkt(outfd, buf, len, chattygot);
701 static const struct got_error *
702 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
703 size_t len, int chattygot)
705 const struct got_error *err;
706 struct gotd_imsg_have ihave;
707 int done = 0;
708 struct imsg imsg;
710 memset(&ihave, 0, sizeof(ihave));
711 memset(&imsg, 0, sizeof(imsg));
713 err = parse_have_line(ihave.object_id, buf, len);
714 if (err)
715 return err;
717 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
718 &ihave, sizeof(ihave)) == -1)
719 return got_error_from_errno("imsg_compose HAVE");
721 err = gotd_imsg_flush(ibuf);
722 if (err)
723 return err;
725 /*
726 * Wait for an ACK or a NAK, indicating whether a common
727 * commit object has been found.
728 */
729 while (!done && err == NULL) {
730 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
731 if (err)
732 return err;
733 switch (imsg.hdr.type) {
734 case GOTD_IMSG_ERROR:
735 err = gotd_imsg_recv_error(NULL, &imsg);
736 break;
737 case GOTD_IMSG_ACK:
738 err = recv_ack(&imsg, ihave.object_id);
739 if (err)
740 break;
741 if (!*have_ack) {
742 err = send_ack(outfd, ihave.object_id,
743 chattygot);
744 if (err)
745 return err;
746 *have_ack = 1;
748 done = 1;
749 break;
750 case GOTD_IMSG_NAK:
751 err = recv_nak(&imsg, ihave.object_id);
752 if (err)
753 break;
754 done = 1;
755 break;
756 default:
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
758 break;
761 imsg_free(&imsg);
764 return err;
767 static const struct got_error *
768 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
770 const struct got_error *err;
771 struct imsg imsg;
773 *packfd = -1;
775 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
776 return got_error_from_errno("imsg_compose DONE");
778 err = gotd_imsg_flush(ibuf);
779 if (err)
780 return err;
782 while (*packfd == -1 && err == NULL) {
783 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
784 if (err)
785 break;
787 switch (imsg.hdr.type) {
788 case GOTD_IMSG_ERROR:
789 err = gotd_imsg_recv_error(NULL, &imsg);
790 break;
791 case GOTD_IMSG_PACKFILE_PIPE:
792 if (imsg.fd != -1)
793 *packfd = imsg.fd;
794 else
795 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
796 break;
797 default:
798 err = got_error(GOT_ERR_PRIVSEP_MSG);
799 break;
802 imsg_free(&imsg);
805 return err;
808 static const struct got_error *
809 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
811 const struct got_error *err = NULL;
812 int pack_starting = 0;
813 struct gotd_imsg_packfile_progress iprog;
814 char buf[GOT_PKT_MAX];
815 struct imsg imsg;
816 size_t datalen;
817 int p_deltify = 0, n;
818 const char *eol = "\r";
820 memset(&imsg, 0, sizeof(imsg));
822 while (!pack_starting && err == NULL) {
823 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
824 if (err)
825 break;
827 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
828 switch (imsg.hdr.type) {
829 case GOTD_IMSG_ERROR:
830 err = gotd_imsg_recv_error(NULL, &imsg);
831 break;
832 case GOTD_IMSG_PACKFILE_READY:
833 eol = "\n";
834 pack_starting = 1;
835 /* fallthrough */
836 case GOTD_IMSG_PACKFILE_PROGRESS:
837 if (datalen != sizeof(iprog)) {
838 err = got_error(GOT_ERR_PRIVSEP_LEN);
839 break;
841 memcpy(&iprog, imsg.data, sizeof(iprog));
842 if (iprog.nobj_total > 0) {
843 p_deltify = (iprog.nobj_deltify * 100) /
844 iprog.nobj_total;
846 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
847 n = snprintf(&buf[1], sizeof(buf) - 1,
848 "%d commits colored, "
849 "%d objects found, "
850 "deltify %d%%%s",
851 iprog.ncolored,
852 iprog.nfound,
853 p_deltify, eol);
854 if (n >= sizeof(buf) - 1)
855 break;
856 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
857 break;
858 default:
859 err = got_error(GOT_ERR_PRIVSEP_MSG);
860 break;
863 imsg_free(&imsg);
866 return err;
869 static const struct got_error *
870 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
871 int chattygot)
873 const struct got_error *err = NULL;
874 char buf[GOT_PKT_MAX];
875 struct imsgbuf ibuf;
876 enum protostate {
877 STATE_EXPECT_WANT,
878 STATE_EXPECT_MORE_WANT,
879 STATE_EXPECT_HAVE,
880 STATE_EXPECT_DONE,
881 STATE_DONE,
882 };
883 enum protostate curstate = STATE_EXPECT_WANT;
884 int have_ack = 0, use_sidebands = 0, seen_have = 0;
885 int packfd = -1;
886 size_t pack_chunksize;
888 imsg_init(&ibuf, gotd_sock);
890 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
891 if (err)
892 goto done;
894 while (curstate != STATE_DONE) {
895 int n;
896 buf[0] = '\0';
897 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
898 if (err)
899 goto done;
900 if (n == 0) {
901 if (curstate != STATE_EXPECT_WANT &&
902 curstate != STATE_EXPECT_MORE_WANT &&
903 curstate != STATE_EXPECT_HAVE &&
904 curstate != STATE_EXPECT_DONE) {
905 err = got_error_msg(GOT_ERR_BAD_PACKET,
906 "unexpected flush packet received");
907 goto done;
910 if (curstate == STATE_EXPECT_WANT) {
911 ssize_t r;
912 /*
913 * If the client does not want to fetch
914 * anything we should receive a flush
915 * packet followed by EOF.
916 */
917 r = read(infd, buf, sizeof(buf));
918 if (r == -1) {
919 err = got_error_from_errno("read");
920 goto done;
922 if (r == 0) /* EOF */
923 goto done;
925 /* Zero-length field followed by payload. */
926 err = got_error_msg(GOT_ERR_BAD_PACKET,
927 "unexpected flush packet received");
928 goto done;
931 if (curstate == STATE_EXPECT_WANT ||
932 curstate == STATE_EXPECT_MORE_WANT ||
933 curstate == STATE_EXPECT_HAVE) {
934 err = forward_flushpkt(&ibuf);
935 if (err)
936 goto done;
938 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
939 err = send_nak(outfd, chattygot);
940 if (err)
941 goto done;
943 if (curstate == STATE_EXPECT_MORE_WANT)
944 curstate = STATE_EXPECT_HAVE;
945 else
946 curstate = STATE_EXPECT_DONE;
947 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
948 if (curstate != STATE_EXPECT_WANT &&
949 curstate != STATE_EXPECT_MORE_WANT) {
950 err = got_error_msg(GOT_ERR_BAD_PACKET,
951 "unexpected 'want' packet");
952 goto done;
954 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
955 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
956 if (err)
957 goto done;
958 if (curstate == STATE_EXPECT_WANT)
959 curstate = STATE_EXPECT_MORE_WANT;
960 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
961 if (curstate != STATE_EXPECT_HAVE &&
962 curstate != STATE_EXPECT_DONE) {
963 err = got_error_msg(GOT_ERR_BAD_PACKET,
964 "unexpected 'have' packet");
965 goto done;
967 if (curstate == STATE_EXPECT_HAVE) {
968 err = recv_have(&have_ack, outfd, &ibuf,
969 buf, n, chattygot);
970 if (err)
971 goto done;
972 seen_have = 1;
973 if (have_ack)
974 curstate = STATE_EXPECT_DONE;
976 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
977 if (curstate != STATE_EXPECT_HAVE &&
978 curstate != STATE_EXPECT_DONE) {
979 err = got_error_msg(GOT_ERR_BAD_PACKET,
980 "unexpected 'done' packet");
981 goto done;
983 err = recv_done(&packfd, outfd, &ibuf, chattygot);
984 if (err)
985 goto done;
986 curstate = STATE_DONE;
987 break;
988 } else {
989 err = got_error(GOT_ERR_BAD_PACKET);
990 goto done;
994 if (!seen_have) {
995 err = send_nak(outfd, chattygot);
996 if (err)
997 goto done;
1000 if (use_sidebands) {
1001 err = relay_progress_reports(&ibuf, outfd, chattygot);
1002 if (err)
1003 goto done;
1004 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
1005 } else
1006 pack_chunksize = sizeof(buf);
1008 for (;;) {
1009 ssize_t r;
1011 r = read(packfd, use_sidebands ? &buf[1] : buf,
1012 pack_chunksize);
1013 if (r == -1) {
1014 err = got_error_from_errno("read");
1015 break;
1016 } else if (r == 0) {
1017 err = got_pkt_flushpkt(outfd, chattygot);
1018 break;
1021 if (use_sidebands) {
1022 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1023 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1024 if (err)
1025 break;
1026 } else {
1027 err = got_poll_write_full(outfd, buf, r);
1028 if (err) {
1029 if (err->code == GOT_ERR_EOF)
1030 err = NULL;
1031 break;
1035 done:
1036 imsg_clear(&ibuf);
1037 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1038 err = got_error_from_errno("close");
1039 if (err)
1040 echo_error(err, outfd, chattygot);
1041 return err;
1044 static const struct got_error *
1045 parse_ref_update_line(char **common_capabilities, char **refname,
1046 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1048 const struct got_error *err;
1049 char *old_id_str = NULL, *new_id_str = NULL;
1050 char *client_capabilities = NULL;
1052 *refname = NULL;
1054 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1055 refname, &client_capabilities, buf, len);
1056 if (err)
1057 return err;
1059 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1060 !got_parse_sha1_digest(new_id, new_id_str)) {
1061 err = got_error_msg(GOT_ERR_BAD_PACKET,
1062 "ref-update with bad object ID");
1063 goto done;
1065 if (!got_ref_name_is_valid(*refname)) {
1066 err = got_error_msg(GOT_ERR_BAD_PACKET,
1067 "ref-update with bad reference name");
1068 goto done;
1071 if (client_capabilities) {
1072 err = got_gitproto_match_capabilities(common_capabilities,
1073 NULL, client_capabilities, write_capabilities,
1074 nitems(write_capabilities));
1075 if (err)
1076 goto done;
1078 done:
1079 free(old_id_str);
1080 free(new_id_str);
1081 free(client_capabilities);
1082 if (err) {
1083 free(*refname);
1084 *refname = NULL;
1086 return err;
1089 static const struct got_error *
1090 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1091 char *buf, size_t len, int expect_capabilities, int chattygot)
1093 const struct got_error *err;
1094 struct gotd_imsg_ref_update iref;
1095 struct ibuf *wbuf;
1096 char *capabilities_str = NULL, *refname = NULL;
1097 int done = 0;
1098 struct imsg imsg;
1100 memset(&iref, 0, sizeof(iref));
1101 memset(&imsg, 0, sizeof(imsg));
1103 err = parse_ref_update_line(&capabilities_str, &refname,
1104 iref.old_id, iref.new_id, buf, len);
1105 if (err)
1106 return err;
1108 if (capabilities_str) {
1109 if (!expect_capabilities) {
1110 err = got_error_msg(GOT_ERR_BAD_PACKET,
1111 "unexpected capability announcement received");
1112 goto done;
1114 err = send_capabilities(NULL, report_status, capabilities_str,
1115 ibuf);
1116 if (err)
1117 goto done;
1120 iref.name_len = strlen(refname);
1121 len = sizeof(iref) + iref.name_len;
1122 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1123 if (wbuf == NULL) {
1124 err = got_error_from_errno("imsg_create REF_UPDATE");
1125 goto done;
1128 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1129 return got_error_from_errno("imsg_add REF_UPDATE");
1130 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1131 return got_error_from_errno("imsg_add REF_UPDATE");
1132 wbuf->fd = -1;
1133 imsg_close(ibuf, wbuf);
1135 err = gotd_imsg_flush(ibuf);
1136 if (err)
1137 goto done;
1139 /* Wait for ACK or an error. */
1140 while (!done && err == NULL) {
1141 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1142 if (err)
1143 break;
1144 switch (imsg.hdr.type) {
1145 case GOTD_IMSG_ERROR:
1146 err = gotd_imsg_recv_error(NULL, &imsg);
1147 break;
1148 case GOTD_IMSG_ACK:
1149 err = recv_ack(&imsg, iref.new_id);
1150 if (err)
1151 break;
1152 done = 1;
1153 break;
1154 default:
1155 err = got_error(GOT_ERR_PRIVSEP_MSG);
1156 break;
1159 imsg_free(&imsg);
1161 done:
1162 free(capabilities_str);
1163 free(refname);
1164 return err;
1167 static const struct got_error *
1168 recv_packfile(struct imsg *imsg, int infd)
1170 const struct got_error *err = NULL;
1171 size_t datalen;
1172 int packfd;
1173 char buf[GOT_PKT_MAX];
1174 int pack_done = 0;
1176 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1177 if (datalen != 0)
1178 return got_error(GOT_ERR_PRIVSEP_MSG);
1180 if (imsg->fd == -1)
1181 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1183 packfd = imsg->fd;
1184 while (!pack_done) {
1185 ssize_t r = 0;
1187 err = got_poll_fd(infd, POLLIN, 1);
1188 if (err) {
1189 if (err->code != GOT_ERR_TIMEOUT)
1190 break;
1191 err = NULL;
1192 } else {
1193 r = read(infd, buf, sizeof(buf));
1194 if (r == -1) {
1195 err = got_error_from_errno("read");
1196 break;
1198 if (r == 0) {
1200 * Git clients hang up their side of the
1201 * connection after sending the pack file.
1203 err = NULL;
1204 pack_done = 1;
1205 break;
1209 if (r == 0) {
1210 /* Detect gotd(8) closing the pack pipe when done. */
1211 err = got_poll_fd(packfd, POLLOUT, 1);
1212 if (err) {
1213 if (err->code != GOT_ERR_EOF)
1214 break;
1215 err = NULL;
1216 pack_done = 1;
1218 } else {
1219 /* Write pack data and/or detect pipe being closed. */
1220 err = got_poll_write_full(packfd, buf, r);
1221 if (err) {
1222 if (err->code == GOT_ERR_EOF)
1223 err = NULL;
1224 break;
1229 close(packfd);
1230 return err;
1233 static const struct got_error *
1234 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1236 const struct got_error *err = NULL;
1237 struct gotd_imsg_packfile_status istatus;
1238 char buf[GOT_PKT_MAX];
1239 size_t datalen, len;
1240 char *reason = NULL;
1242 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1243 if (datalen < sizeof(istatus))
1244 return got_error(GOT_ERR_PRIVSEP_LEN);
1245 memcpy(&istatus, imsg->data, sizeof(istatus));
1246 if (datalen != sizeof(istatus) + istatus.reason_len)
1247 return got_error(GOT_ERR_PRIVSEP_LEN);
1249 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1250 if (reason == NULL) {
1251 err = got_error_from_errno("strndup");
1252 goto done;
1255 if (err == NULL)
1256 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1257 else
1258 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1259 if (len >= sizeof(buf)) {
1260 err = got_error(GOT_ERR_NO_SPACE);
1261 goto done;
1264 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1265 done:
1266 free(reason);
1267 return err;
1270 static const struct got_error *
1271 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1273 const struct got_error *err = NULL;
1274 struct gotd_imsg_ref_update_ok iok;
1275 size_t datalen, len;
1276 char buf[GOT_PKT_MAX];
1277 char *refname = NULL;
1279 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1280 if (datalen < sizeof(iok))
1281 return got_error(GOT_ERR_PRIVSEP_LEN);
1282 memcpy(&iok, imsg->data, sizeof(iok));
1283 if (datalen != sizeof(iok) + iok.name_len)
1284 return got_error(GOT_ERR_PRIVSEP_LEN);
1286 memcpy(&iok, imsg->data, sizeof(iok));
1288 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1289 if (refname == NULL)
1290 return got_error_from_errno("strndup");
1292 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1293 if (len >= sizeof(buf)) {
1294 err = got_error(GOT_ERR_NO_SPACE);
1295 goto done;
1298 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1299 done:
1300 free(refname);
1301 return err;
1304 static const struct got_error *
1305 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1307 const struct got_error *err = NULL;
1308 struct gotd_imsg_ref_update_ng ing;
1309 size_t datalen, len;
1310 char buf[GOT_PKT_MAX];
1311 char *refname = NULL, *reason = NULL;
1313 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1314 if (datalen < sizeof(ing))
1315 return got_error(GOT_ERR_PRIVSEP_LEN);
1316 memcpy(&ing, imsg->data, sizeof(ing));
1317 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1318 return got_error(GOT_ERR_PRIVSEP_LEN);
1320 memcpy(&ing, imsg->data, sizeof(ing));
1322 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1323 if (refname == NULL)
1324 return got_error_from_errno("strndup");
1326 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1327 ing.reason_len);
1328 if (reason == NULL) {
1329 err = got_error_from_errno("strndup");
1330 goto done;
1333 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1334 if (len >= sizeof(buf)) {
1335 err = got_error(GOT_ERR_NO_SPACE);
1336 goto done;
1339 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1340 done:
1341 free(refname);
1342 free(reason);
1343 return err;
1346 static const struct got_error *
1347 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1348 int chattygot)
1350 const struct got_error *err = NULL;
1351 char buf[GOT_PKT_MAX];
1352 struct imsgbuf ibuf;
1353 enum protostate {
1354 STATE_EXPECT_REF_UPDATE,
1355 STATE_EXPECT_MORE_REF_UPDATES,
1356 STATE_EXPECT_PACKFILE,
1357 STATE_PACKFILE_RECEIVED,
1358 STATE_REFS_UPDATED,
1360 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1361 struct imsg imsg;
1362 int report_status = 0;
1364 imsg_init(&ibuf, gotd_sock);
1365 memset(&imsg, 0, sizeof(imsg));
1367 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1368 if (err)
1369 goto done;
1371 while (curstate != STATE_EXPECT_PACKFILE) {
1372 int n;
1373 buf[0] = '\0';
1374 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1375 if (err)
1376 goto done;
1377 if (n == 0) {
1378 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1379 err = got_error_msg(GOT_ERR_BAD_PACKET,
1380 "unexpected flush packet received");
1381 goto done;
1383 err = forward_flushpkt(&ibuf);
1384 if (err)
1385 goto done;
1386 curstate = STATE_EXPECT_PACKFILE;
1387 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1388 if (curstate != STATE_EXPECT_REF_UPDATE &&
1389 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1390 err = got_error_msg(GOT_ERR_BAD_PACKET,
1391 "unexpected ref-update packet");
1392 goto done;
1394 if (curstate == STATE_EXPECT_REF_UPDATE) {
1395 err = recv_ref_update(&report_status,
1396 outfd, &ibuf, buf, n, 1, chattygot);
1397 } else {
1398 err = recv_ref_update(NULL, outfd, &ibuf,
1399 buf, n, 0, chattygot);
1401 if (err)
1402 goto done;
1403 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1404 } else {
1405 err = got_error(GOT_ERR_BAD_PACKET);
1406 goto done;
1410 while (curstate != STATE_PACKFILE_RECEIVED) {
1411 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1412 if (err)
1413 goto done;
1414 switch (imsg.hdr.type) {
1415 case GOTD_IMSG_ERROR:
1416 err = gotd_imsg_recv_error(NULL, &imsg);
1417 goto done;
1418 case GOTD_IMSG_PACKFILE_PIPE:
1419 err = recv_packfile(&imsg, infd);
1420 if (err) {
1421 if (err->code != GOT_ERR_EOF)
1422 goto done;
1424 * EOF is reported when the client hangs up,
1425 * which can happen with Git clients.
1426 * The socket should stay half-open so we
1427 * can still send our reports if requested.
1429 err = NULL;
1431 curstate = STATE_PACKFILE_RECEIVED;
1432 break;
1433 default:
1434 err = got_error(GOT_ERR_PRIVSEP_MSG);
1435 break;
1438 imsg_free(&imsg);
1439 if (err)
1440 goto done;
1443 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1444 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1445 if (err)
1446 break;
1447 switch (imsg.hdr.type) {
1448 case GOTD_IMSG_ERROR:
1449 err = gotd_imsg_recv_error(NULL, &imsg);
1450 break;
1451 case GOTD_IMSG_PACKFILE_STATUS:
1452 if (!report_status)
1453 break;
1454 err = report_unpack_status(&imsg, outfd, chattygot);
1455 break;
1456 case GOTD_IMSG_REF_UPDATE_OK:
1457 if (!report_status)
1458 break;
1459 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1460 break;
1461 case GOTD_IMSG_REF_UPDATE_NG:
1462 if (!report_status)
1463 break;
1464 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1465 break;
1466 case GOTD_IMSG_REFS_UPDATED:
1467 curstate = STATE_REFS_UPDATED;
1468 err = got_pkt_flushpkt(outfd, chattygot);
1469 break;
1470 default:
1471 err = got_error(GOT_ERR_PRIVSEP_MSG);
1472 break;
1475 imsg_free(&imsg);
1477 done:
1478 imsg_clear(&ibuf);
1479 if (err)
1480 echo_error(err, outfd, chattygot);
1481 return err;
1484 const struct got_error *
1485 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1486 int gotd_sock, int chattygot)
1488 const struct got_error *err = NULL;
1490 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1491 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1492 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1493 err = serve_write(infd, outfd, gotd_sock, repo_path,
1494 chattygot);
1495 else
1496 err = got_error(GOT_ERR_BAD_PACKET);
1498 return err;