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"
40 #include "got_lib_pkt.h"
41 #include "got_lib_dial.h"
42 #include "got_lib_gitproto.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_poll.h"
46 #include "gotd.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 #endif
52 static const struct got_capability read_capabilities[] = {
53 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
54 { GOT_CAPA_OFS_DELTA, NULL },
55 { GOT_CAPA_SIDE_BAND_64K, NULL },
56 };
58 static const struct got_capability write_capabilities[] = {
59 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
60 { GOT_CAPA_OFS_DELTA, NULL },
61 { GOT_CAPA_REPORT_STATUS, NULL },
62 { GOT_CAPA_NO_THIN, NULL },
63 { GOT_CAPA_DELETE_REFS, NULL },
64 };
66 const struct got_error *
67 got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
68 {
69 const struct got_error *err = NULL;
70 size_t len, cmdlen, pathlen;
71 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
72 const char *relpath;
74 *command = NULL;
75 *repo_path = NULL;
77 len = strlen(gitcmd);
79 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
80 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
81 strlen(GOT_SERVE_CMD_SEND)) == 0)
82 cmdlen = strlen(GOT_SERVE_CMD_SEND);
83 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
84 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
85 strlen(GOT_SERVE_CMD_FETCH)) == 0)
86 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
87 else
88 return got_error(GOT_ERR_BAD_PACKET);
90 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
91 return got_error(GOT_ERR_BAD_PACKET);
93 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
94 return got_error(GOT_ERR_BAD_PATH);
96 /* Forbid linefeeds in paths, like Git does. */
97 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
98 return got_error(GOT_ERR_BAD_PATH);
100 path0 = strdup(&gitcmd[cmdlen + 1]);
101 if (path0 == NULL)
102 return got_error_from_errno("strdup");
103 path = path0;
104 pathlen = strlen(path);
106 /*
107 * Git clients send a shell command.
108 * Trim spaces and quotes around the path.
109 */
110 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
111 path++;
112 pathlen--;
114 while (pathlen > 0 &&
115 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
116 path[pathlen - 1] == ' ')) {
117 path[pathlen - 1] = '\0';
118 pathlen--;
121 /* Deny an empty repository path. */
122 if (path[0] == '\0' || got_path_is_root_dir(path)) {
123 err = got_error(GOT_ERR_NOT_GIT_REPO);
124 goto done;
127 if (asprintf(&abspath, "/%s", path) == -1) {
128 err = got_error_from_errno("asprintf");
129 goto done;
131 pathlen = strlen(abspath);
132 canonpath = malloc(pathlen);
133 if (canonpath == NULL) {
134 err = got_error_from_errno("malloc");
135 goto done;
137 err = got_canonpath(abspath, canonpath, pathlen);
138 if (err)
139 goto done;
141 relpath = canonpath;
142 while (relpath[0] == '/')
143 relpath++;
144 *repo_path = strdup(relpath);
145 if (*repo_path == NULL) {
146 err = got_error_from_errno("strdup");
147 goto done;
149 *command = strndup(gitcmd, cmdlen);
150 if (*command == NULL)
151 err = got_error_from_errno("strndup");
152 done:
153 free(path0);
154 free(abspath);
155 free(canonpath);
156 if (err) {
157 free(*repo_path);
158 *repo_path = NULL;
160 return err;
163 static const struct got_error *
164 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
165 uint8_t *buf, size_t bufsize)
167 struct got_capability capa[nitems(read_capabilities) + 1];
168 size_t ncapa;
170 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
171 if (symrefstr) {
172 capa[nitems(read_capabilities)].key = "symref";
173 capa[nitems(read_capabilities)].value = symrefstr;
174 ncapa = nitems(capa);
175 } else
176 ncapa = nitems(read_capabilities);
178 return got_gitproto_append_capabilities(capalen, buf, len,
179 bufsize, capa, ncapa);
182 static const struct got_error *
183 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
184 int client_is_reading, const char *symrefstr, int chattygot)
186 const struct got_error *err = NULL;
187 char hex[SHA1_DIGEST_STRING_LENGTH];
188 char buf[GOT_PKT_MAX];
189 size_t len, capalen = 0;
191 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
192 return got_error(GOT_ERR_BAD_OBJ_ID);
194 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
195 if (len >= sizeof(buf))
196 return got_error(GOT_ERR_NO_SPACE);
198 if (send_capabilities) {
199 if (client_is_reading) {
200 err = append_read_capabilities(&capalen, len,
201 symrefstr, buf, sizeof(buf));
202 } else {
203 err = got_gitproto_append_capabilities(&capalen,
204 buf, len, sizeof(buf), write_capabilities,
205 nitems(write_capabilities));
207 if (err)
208 return err;
209 len += capalen;
212 if (len + 1 >= sizeof(buf))
213 return got_error(GOT_ERR_NO_SPACE);
214 buf[len] = '\n';
215 len++;
216 buf[len] = '\0';
218 return got_pkt_writepkt(outfd, buf, len, chattygot);
221 static const struct got_error *
222 send_zero_refs(int outfd, int client_is_reading, int chattygot)
224 const struct got_error *err = NULL;
225 char buf[GOT_PKT_MAX];
226 uint8_t zero[SHA1_DIGEST_LENGTH];
227 char hex[SHA1_DIGEST_STRING_LENGTH];
228 size_t len, capalen = 0;
230 memset(&zero, 0, sizeof(zero));
232 if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
233 return got_error(GOT_ERR_BAD_OBJ_ID);
235 len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
236 if (len >= sizeof(buf))
237 return got_error(GOT_ERR_NO_SPACE);
239 if (client_is_reading) {
240 err = got_gitproto_append_capabilities(&capalen, buf, len,
241 sizeof(buf), read_capabilities, nitems(read_capabilities));
242 if (err)
243 return err;
244 } else {
245 err = got_gitproto_append_capabilities(&capalen, buf, len,
246 sizeof(buf), write_capabilities,
247 nitems(write_capabilities));
248 if (err)
249 return err;
252 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
255 static void
256 echo_error(const struct got_error *err, int outfd, int chattygot)
258 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
259 size_t len;
261 /*
262 * Echo the error to the client on a pkt-line.
263 * The client should then terminate its session.
264 */
265 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
266 len = strlcat(buf, err->msg, sizeof(buf));
267 got_pkt_writepkt(outfd, buf, len, chattygot);
270 static const struct got_error *
271 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
272 const char *repo_path, int chattygot)
274 const struct got_error *err = NULL;
275 struct imsg imsg;
276 size_t datalen;
277 struct gotd_imsg_list_refs lsref;
278 struct gotd_imsg_reflist ireflist;
279 struct gotd_imsg_ref iref;
280 struct gotd_imsg_symref isymref;
281 size_t nrefs = 0;
282 int have_nrefs = 0, sent_capabilities = 0;
283 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
284 char *refname = NULL;
286 memset(&imsg, 0, sizeof(imsg));
287 memset(&lsref, 0, sizeof(lsref));
289 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
290 sizeof(lsref.repo_name))
291 return got_error(GOT_ERR_NO_SPACE);
292 lsref.client_is_reading = client_is_reading;
294 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
295 &lsref, sizeof(lsref)) == -1)
296 return got_error_from_errno("imsg_compose LIST_REFS");
298 err = gotd_imsg_flush(ibuf);
299 if (err)
300 return err;
302 while (!have_nrefs || nrefs > 0) {
303 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
304 if (err)
305 goto done;
306 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
307 switch (imsg.hdr.type) {
308 case GOTD_IMSG_ERROR:
309 err = gotd_imsg_recv_error(NULL, &imsg);
310 goto done;
311 case GOTD_IMSG_REFLIST:
312 if (have_nrefs || nrefs > 0) {
313 err = got_error(GOT_ERR_PRIVSEP_MSG);
314 goto done;
316 if (datalen != sizeof(ireflist)) {
317 err = got_error(GOT_ERR_PRIVSEP_MSG);
318 goto done;
320 memcpy(&ireflist, imsg.data, sizeof(ireflist));
321 nrefs = ireflist.nrefs;
322 have_nrefs = 1;
323 if (nrefs == 0)
324 err = send_zero_refs(outfd, client_is_reading,
325 chattygot);
326 break;
327 case GOTD_IMSG_REF:
328 if (!have_nrefs || nrefs == 0) {
329 err = got_error(GOT_ERR_PRIVSEP_MSG);
330 goto done;
332 if (datalen < sizeof(iref)) {
333 err = got_error(GOT_ERR_PRIVSEP_MSG);
334 goto done;
336 memcpy(&iref, imsg.data, sizeof(iref));
337 if (datalen != sizeof(iref) + iref.name_len) {
338 err = got_error(GOT_ERR_PRIVSEP_LEN);
339 goto done;
341 refname = strndup(imsg.data + sizeof(iref),
342 iref.name_len);
343 if (refname == NULL) {
344 err = got_error_from_errno("strndup");
345 goto done;
347 err = send_ref(outfd, iref.id, refname,
348 !sent_capabilities, client_is_reading,
349 NULL, chattygot);
350 free(refname);
351 refname = NULL;
352 if (err)
353 goto done;
354 sent_capabilities = 1;
355 if (nrefs > 0)
356 nrefs--;
357 break;
358 case GOTD_IMSG_SYMREF:
359 if (!have_nrefs || nrefs == 0) {
360 err = got_error(GOT_ERR_PRIVSEP_MSG);
361 goto done;
363 if (datalen < sizeof(isymref)) {
364 err = got_error(GOT_ERR_PRIVSEP_LEN);
365 goto done;
367 memcpy(&isymref, imsg.data, sizeof(isymref));
368 if (datalen != sizeof(isymref) + isymref.name_len +
369 isymref.target_len) {
370 err = got_error(GOT_ERR_PRIVSEP_LEN);
371 goto done;
374 /*
375 * For now, we only announce one symbolic ref,
376 * as part of our capability advertisement.
377 */
378 if (sent_capabilities || symrefstr != NULL ||
379 symrefname != NULL || symreftarget != NULL)
380 break;
382 symrefname = strndup(imsg.data + sizeof(isymref),
383 isymref.name_len);
384 if (symrefname == NULL) {
385 err = got_error_from_errno("malloc");
386 goto done;
389 symreftarget = strndup(
390 imsg.data + sizeof(isymref) + isymref.name_len,
391 isymref.target_len);
392 if (symreftarget == NULL) {
393 err = got_error_from_errno("strndup");
394 goto done;
397 if (asprintf(&symrefstr, "%s:%s", symrefname,
398 symreftarget) == -1) {
399 err = got_error_from_errno("asprintf");
400 goto done;
402 err = send_ref(outfd, isymref.target_id, symrefname,
403 !sent_capabilities, client_is_reading, symrefstr,
404 chattygot);
405 free(refname);
406 refname = NULL;
407 if (err)
408 goto done;
409 sent_capabilities = 1;
410 if (nrefs > 0)
411 nrefs--;
412 break;
413 default:
414 err = got_error(GOT_ERR_PRIVSEP_MSG);
415 break;
418 imsg_free(&imsg);
421 err = got_pkt_flushpkt(outfd, chattygot);
422 if (err)
423 goto done;
424 done:
425 free(symrefstr);
426 free(symrefname);
427 free(symreftarget);
428 return err;
431 static const struct got_error *
432 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
434 const struct got_error *err;
435 char *id_str = NULL, *client_capabilities = NULL;
437 err = got_gitproto_parse_want_line(&id_str,
438 &client_capabilities, buf, len);
439 if (err)
440 return err;
442 if (!got_parse_sha1_digest(id, id_str)) {
443 err = got_error_msg(GOT_ERR_BAD_PACKET,
444 "want-line with bad object ID");
445 goto done;
448 if (client_capabilities) {
449 err = got_gitproto_match_capabilities(common_capabilities,
450 NULL, client_capabilities, read_capabilities,
451 nitems(read_capabilities));
452 if (err)
453 goto done;
455 done:
456 free(id_str);
457 free(client_capabilities);
458 return err;
461 static const struct got_error *
462 parse_have_line(uint8_t *id, char *buf, size_t len)
464 const struct got_error *err;
465 char *id_str = NULL;
467 err = got_gitproto_parse_have_line(&id_str, buf, len);
468 if (err)
469 return err;
471 if (!got_parse_sha1_digest(id, id_str)) {
472 err = got_error_msg(GOT_ERR_BAD_PACKET,
473 "have-line with bad object ID");
474 goto done;
476 done:
477 free(id_str);
478 return err;
481 static const struct got_error *
482 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
484 const struct got_error *err = NULL;
485 struct gotd_imsg_capability icapa;
486 size_t len;
487 struct ibuf *wbuf;
489 memset(&icapa, 0, sizeof(icapa));
491 icapa.key_len = strlen(capa->key);
492 len = sizeof(icapa) + icapa.key_len;
493 if (capa->value) {
494 icapa.value_len = strlen(capa->value);
495 len += icapa.value_len;
498 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
499 if (wbuf == NULL) {
500 err = got_error_from_errno("imsg_create CAPABILITY");
501 return err;
504 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
505 return got_error_from_errno("imsg_add CAPABILITY");
506 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
507 return got_error_from_errno("imsg_add CAPABILITY");
508 if (capa->value) {
509 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
510 return got_error_from_errno("imsg_add CAPABILITY");
513 wbuf->fd = -1;
514 imsg_close(ibuf, wbuf);
516 return NULL;
519 static const struct got_error *
520 send_capabilities(int *use_sidebands, int *report_status,
521 char *capabilities_str, struct imsgbuf *ibuf)
523 const struct got_error *err = NULL;
524 struct gotd_imsg_capabilities icapas;
525 struct got_capability *capa = NULL;
526 size_t ncapa, i;
528 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
529 capabilities_str);
530 if (err)
531 return err;
533 icapas.ncapabilities = ncapa;
534 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
535 &icapas, sizeof(icapas)) == -1) {
536 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
537 goto done;
540 for (i = 0; i < ncapa; i++) {
541 err = send_capability(&capa[i], ibuf);
542 if (err)
543 goto done;
544 if (use_sidebands &&
545 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
546 *use_sidebands = 1;
547 if (report_status &&
548 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
549 *report_status = 1;
551 done:
552 free(capa);
553 return err;
556 static const struct got_error *
557 forward_flushpkt(struct imsgbuf *ibuf)
559 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
560 return got_error_from_errno("imsg_compose FLUSH");
562 return gotd_imsg_flush(ibuf);
565 static const struct got_error *
566 recv_ack(struct imsg *imsg, uint8_t *expected_id)
568 struct gotd_imsg_ack iack;
569 size_t datalen;
571 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
572 if (datalen != sizeof(iack))
573 return got_error(GOT_ERR_PRIVSEP_LEN);
575 memcpy(&iack, imsg->data, sizeof(iack));
576 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
577 return got_error(GOT_ERR_BAD_OBJ_ID);
579 return NULL;
582 static const struct got_error *
583 recv_nak(struct imsg *imsg, uint8_t *expected_id)
585 struct gotd_imsg_ack inak;
586 size_t datalen;
588 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
589 if (datalen != sizeof(inak))
590 return got_error(GOT_ERR_PRIVSEP_LEN);
592 memcpy(&inak, imsg->data, sizeof(inak));
593 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
594 return got_error(GOT_ERR_BAD_OBJ_ID);
596 return NULL;
600 static const struct got_error *
601 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
602 char *buf, size_t len, int expect_capabilities, int chattygot)
604 const struct got_error *err;
605 struct gotd_imsg_want iwant;
606 char *capabilities_str;
607 int done = 0;
608 struct imsg imsg;
610 memset(&iwant, 0, sizeof(iwant));
611 memset(&imsg, 0, sizeof(imsg));
613 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
614 if (err)
615 return err;
617 if (capabilities_str) {
618 if (!expect_capabilities) {
619 err = got_error_msg(GOT_ERR_BAD_PACKET,
620 "unexpected capability announcement received");
621 goto done;
623 err = send_capabilities(use_sidebands, NULL, capabilities_str,
624 ibuf);
625 if (err)
626 goto done;
630 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
631 &iwant, sizeof(iwant)) == -1) {
632 err = got_error_from_errno("imsg_compose WANT");
633 goto done;
636 err = gotd_imsg_flush(ibuf);
637 if (err)
638 goto done;
640 /*
641 * Wait for an ACK, or an error in case the desired object
642 * does not exist.
643 */
644 while (!done && err == NULL) {
645 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
646 if (err)
647 break;
648 switch (imsg.hdr.type) {
649 case GOTD_IMSG_ERROR:
650 err = gotd_imsg_recv_error(NULL, &imsg);
651 break;
652 case GOTD_IMSG_ACK:
653 err = recv_ack(&imsg, iwant.object_id);
654 if (err)
655 break;
656 done = 1;
657 break;
658 default:
659 err = got_error(GOT_ERR_PRIVSEP_MSG);
660 break;
663 imsg_free(&imsg);
665 done:
666 free(capabilities_str);
667 return err;
670 static const struct got_error *
671 send_ack(int outfd, uint8_t *id, int chattygot)
673 char hex[SHA1_DIGEST_STRING_LENGTH];
674 char buf[GOT_PKT_MAX];
675 int len;
677 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
678 return got_error(GOT_ERR_BAD_OBJ_ID);
680 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
681 if (len >= sizeof(buf))
682 return got_error(GOT_ERR_NO_SPACE);
684 return got_pkt_writepkt(outfd, buf, len, chattygot);
687 static const struct got_error *
688 send_nak(int outfd, int chattygot)
690 char buf[5];
691 int len;
693 len = snprintf(buf, sizeof(buf), "NAK\n");
694 if (len >= sizeof(buf))
695 return got_error(GOT_ERR_NO_SPACE);
697 return got_pkt_writepkt(outfd, buf, len, chattygot);
700 static const struct got_error *
701 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
702 size_t len, int chattygot)
704 const struct got_error *err;
705 struct gotd_imsg_have ihave;
706 int done = 0;
707 struct imsg imsg;
709 memset(&ihave, 0, sizeof(ihave));
710 memset(&imsg, 0, sizeof(imsg));
712 err = parse_have_line(ihave.object_id, buf, len);
713 if (err)
714 return err;
716 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
717 &ihave, sizeof(ihave)) == -1)
718 return got_error_from_errno("imsg_compose HAVE");
720 err = gotd_imsg_flush(ibuf);
721 if (err)
722 return err;
724 /*
725 * Wait for an ACK or a NAK, indicating whether a common
726 * commit object has been found.
727 */
728 while (!done && err == NULL) {
729 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
730 if (err)
731 return err;
732 switch (imsg.hdr.type) {
733 case GOTD_IMSG_ERROR:
734 err = gotd_imsg_recv_error(NULL, &imsg);
735 break;
736 case GOTD_IMSG_ACK:
737 err = recv_ack(&imsg, ihave.object_id);
738 if (err)
739 break;
740 if (!*have_ack) {
741 err = send_ack(outfd, ihave.object_id,
742 chattygot);
743 if (err)
744 return err;
745 *have_ack = 1;
747 done = 1;
748 break;
749 case GOTD_IMSG_NAK:
750 err = recv_nak(&imsg, ihave.object_id);
751 if (err)
752 break;
753 done = 1;
754 break;
755 default:
756 err = got_error(GOT_ERR_PRIVSEP_MSG);
757 break;
760 imsg_free(&imsg);
763 return err;
766 static const struct got_error *
767 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
769 const struct got_error *err;
770 struct imsg imsg;
772 *packfd = -1;
774 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
775 return got_error_from_errno("imsg_compose DONE");
777 err = gotd_imsg_flush(ibuf);
778 if (err)
779 return err;
781 while (*packfd == -1 && err == NULL) {
782 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
783 if (err)
784 break;
786 switch (imsg.hdr.type) {
787 case GOTD_IMSG_ERROR:
788 err = gotd_imsg_recv_error(NULL, &imsg);
789 break;
790 case GOTD_IMSG_PACKFILE_PIPE:
791 if (imsg.fd != -1)
792 *packfd = imsg.fd;
793 else
794 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
795 break;
796 default:
797 err = got_error(GOT_ERR_PRIVSEP_MSG);
798 break;
801 imsg_free(&imsg);
804 return err;
807 static const struct got_error *
808 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
810 const struct got_error *err = NULL;
811 int pack_starting = 0;
812 struct gotd_imsg_packfile_progress iprog;
813 char buf[GOT_PKT_MAX];
814 struct imsg imsg;
815 size_t datalen;
816 int p_deltify = 0, n;
817 const char *eol = "\r";
819 memset(&imsg, 0, sizeof(imsg));
821 while (!pack_starting && err == NULL) {
822 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
823 if (err)
824 break;
826 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
827 switch (imsg.hdr.type) {
828 case GOTD_IMSG_ERROR:
829 err = gotd_imsg_recv_error(NULL, &imsg);
830 break;
831 case GOTD_IMSG_PACKFILE_READY:
832 eol = "\n";
833 pack_starting = 1;
834 /* fallthrough */
835 case GOTD_IMSG_PACKFILE_PROGRESS:
836 if (datalen != sizeof(iprog)) {
837 err = got_error(GOT_ERR_PRIVSEP_LEN);
838 break;
840 memcpy(&iprog, imsg.data, sizeof(iprog));
841 if (iprog.nobj_total > 0) {
842 p_deltify = (iprog.nobj_deltify * 100) /
843 iprog.nobj_total;
845 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
846 n = snprintf(&buf[1], sizeof(buf) - 1,
847 "%d commits colored, "
848 "%d objects found, "
849 "deltify %d%%%s",
850 iprog.ncolored,
851 iprog.nfound,
852 p_deltify, eol);
853 if (n >= sizeof(buf) - 1)
854 break;
855 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
856 break;
857 default:
858 err = got_error(GOT_ERR_PRIVSEP_MSG);
859 break;
862 imsg_free(&imsg);
865 return err;
868 static const struct got_error *
869 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
870 int chattygot)
872 const struct got_error *err = NULL;
873 char buf[GOT_PKT_MAX];
874 struct imsgbuf ibuf;
875 enum protostate {
876 STATE_EXPECT_WANT,
877 STATE_EXPECT_MORE_WANT,
878 STATE_EXPECT_HAVE,
879 STATE_EXPECT_DONE,
880 STATE_DONE,
881 };
882 enum protostate curstate = STATE_EXPECT_WANT;
883 int have_ack = 0, use_sidebands = 0, seen_have = 0;
884 int packfd = -1;
885 size_t pack_chunksize;
887 imsg_init(&ibuf, gotd_sock);
889 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
890 if (err)
891 goto done;
893 while (curstate != STATE_DONE) {
894 int n;
895 buf[0] = '\0';
896 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
897 if (err)
898 goto done;
899 if (n == 0) {
900 if (curstate != STATE_EXPECT_WANT &&
901 curstate != STATE_EXPECT_MORE_WANT &&
902 curstate != STATE_EXPECT_HAVE &&
903 curstate != STATE_EXPECT_DONE) {
904 err = got_error_msg(GOT_ERR_BAD_PACKET,
905 "unexpected flush packet received");
906 goto done;
909 if (curstate == STATE_EXPECT_WANT) {
910 ssize_t r;
911 /*
912 * If the client does not want to fetch
913 * anything we should receive a flush
914 * packet followed by EOF.
915 */
916 r = read(infd, buf, sizeof(buf));
917 if (r == -1) {
918 err = got_error_from_errno("read");
919 goto done;
921 if (r == 0) /* EOF */
922 goto done;
924 /* Zero-length field followed by payload. */
925 err = got_error_msg(GOT_ERR_BAD_PACKET,
926 "unexpected flush packet received");
927 goto done;
930 if (curstate == STATE_EXPECT_WANT ||
931 curstate == STATE_EXPECT_MORE_WANT ||
932 curstate == STATE_EXPECT_HAVE) {
933 err = forward_flushpkt(&ibuf);
934 if (err)
935 goto done;
937 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
938 err = send_nak(outfd, chattygot);
939 if (err)
940 goto done;
942 if (curstate == STATE_EXPECT_MORE_WANT)
943 curstate = STATE_EXPECT_HAVE;
944 else
945 curstate = STATE_EXPECT_DONE;
946 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
947 if (curstate != STATE_EXPECT_WANT &&
948 curstate != STATE_EXPECT_MORE_WANT) {
949 err = got_error_msg(GOT_ERR_BAD_PACKET,
950 "unexpected 'want' packet");
951 goto done;
953 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
954 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
955 if (err)
956 goto done;
957 if (curstate == STATE_EXPECT_WANT)
958 curstate = STATE_EXPECT_MORE_WANT;
959 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
960 if (curstate != STATE_EXPECT_HAVE &&
961 curstate != STATE_EXPECT_DONE) {
962 err = got_error_msg(GOT_ERR_BAD_PACKET,
963 "unexpected 'have' packet");
964 goto done;
966 if (curstate == STATE_EXPECT_HAVE) {
967 err = recv_have(&have_ack, outfd, &ibuf,
968 buf, n, chattygot);
969 if (err)
970 goto done;
971 seen_have = 1;
972 if (have_ack)
973 curstate = STATE_EXPECT_DONE;
975 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
976 if (curstate != STATE_EXPECT_HAVE &&
977 curstate != STATE_EXPECT_DONE) {
978 err = got_error_msg(GOT_ERR_BAD_PACKET,
979 "unexpected 'done' packet");
980 goto done;
982 err = recv_done(&packfd, outfd, &ibuf, chattygot);
983 if (err)
984 goto done;
985 curstate = STATE_DONE;
986 break;
987 } else {
988 err = got_error(GOT_ERR_BAD_PACKET);
989 goto done;
993 if (!seen_have) {
994 err = send_nak(outfd, chattygot);
995 if (err)
996 goto done;
999 if (use_sidebands) {
1000 err = relay_progress_reports(&ibuf, outfd, chattygot);
1001 if (err)
1002 goto done;
1003 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
1004 } else
1005 pack_chunksize = sizeof(buf);
1007 for (;;) {
1008 ssize_t r;
1010 r = read(packfd, use_sidebands ? &buf[1] : buf,
1011 pack_chunksize);
1012 if (r == -1) {
1013 err = got_error_from_errno("read");
1014 break;
1015 } else if (r == 0) {
1016 err = got_pkt_flushpkt(outfd, chattygot);
1017 break;
1020 if (use_sidebands) {
1021 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1022 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1023 if (err)
1024 break;
1025 } else {
1026 err = got_poll_write_full(outfd, buf, r);
1027 if (err) {
1028 if (err->code == GOT_ERR_EOF)
1029 err = NULL;
1030 break;
1034 done:
1035 imsg_clear(&ibuf);
1036 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1037 err = got_error_from_errno("close");
1038 if (err)
1039 echo_error(err, outfd, chattygot);
1040 return err;
1043 static const struct got_error *
1044 parse_ref_update_line(char **common_capabilities, char **refname,
1045 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1047 const struct got_error *err;
1048 char *old_id_str = NULL, *new_id_str = NULL;
1049 char *client_capabilities = NULL;
1051 *refname = NULL;
1053 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1054 refname, &client_capabilities, buf, len);
1055 if (err)
1056 return err;
1058 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1059 !got_parse_sha1_digest(new_id, new_id_str)) {
1060 err = got_error_msg(GOT_ERR_BAD_PACKET,
1061 "ref-update with bad object ID");
1062 goto done;
1064 if (!got_ref_name_is_valid(*refname)) {
1065 err = got_error_msg(GOT_ERR_BAD_PACKET,
1066 "ref-update with bad reference name");
1067 goto done;
1070 if (client_capabilities) {
1071 err = got_gitproto_match_capabilities(common_capabilities,
1072 NULL, client_capabilities, write_capabilities,
1073 nitems(write_capabilities));
1074 if (err)
1075 goto done;
1077 done:
1078 free(old_id_str);
1079 free(new_id_str);
1080 free(client_capabilities);
1081 if (err) {
1082 free(*refname);
1083 *refname = NULL;
1085 return err;
1088 static const struct got_error *
1089 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1090 char *buf, size_t len, int expect_capabilities, int chattygot)
1092 const struct got_error *err;
1093 struct gotd_imsg_ref_update iref;
1094 struct ibuf *wbuf;
1095 char *capabilities_str = NULL, *refname = NULL;
1096 int done = 0;
1097 struct imsg imsg;
1099 memset(&iref, 0, sizeof(iref));
1100 memset(&imsg, 0, sizeof(imsg));
1102 err = parse_ref_update_line(&capabilities_str, &refname,
1103 iref.old_id, iref.new_id, buf, len);
1104 if (err)
1105 return err;
1107 if (capabilities_str) {
1108 if (!expect_capabilities) {
1109 err = got_error_msg(GOT_ERR_BAD_PACKET,
1110 "unexpected capability announcement received");
1111 goto done;
1113 err = send_capabilities(NULL, report_status, capabilities_str,
1114 ibuf);
1115 if (err)
1116 goto done;
1119 iref.name_len = strlen(refname);
1120 len = sizeof(iref) + iref.name_len;
1121 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1122 if (wbuf == NULL) {
1123 err = got_error_from_errno("imsg_create REF_UPDATE");
1124 goto done;
1127 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1128 return got_error_from_errno("imsg_add REF_UPDATE");
1129 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1130 return got_error_from_errno("imsg_add REF_UPDATE");
1131 wbuf->fd = -1;
1132 imsg_close(ibuf, wbuf);
1134 err = gotd_imsg_flush(ibuf);
1135 if (err)
1136 goto done;
1138 /* Wait for ACK or an error. */
1139 while (!done && err == NULL) {
1140 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1141 if (err)
1142 break;
1143 switch (imsg.hdr.type) {
1144 case GOTD_IMSG_ERROR:
1145 err = gotd_imsg_recv_error(NULL, &imsg);
1146 break;
1147 case GOTD_IMSG_ACK:
1148 err = recv_ack(&imsg, iref.new_id);
1149 if (err)
1150 break;
1151 done = 1;
1152 break;
1153 default:
1154 err = got_error(GOT_ERR_PRIVSEP_MSG);
1155 break;
1158 imsg_free(&imsg);
1160 done:
1161 free(capabilities_str);
1162 free(refname);
1163 return err;
1166 static const struct got_error *
1167 recv_packfile(struct imsg *imsg, int infd)
1169 const struct got_error *err = NULL;
1170 size_t datalen;
1171 int packfd;
1172 char buf[GOT_PKT_MAX];
1173 int pack_done = 0;
1175 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1176 if (datalen != 0)
1177 return got_error(GOT_ERR_PRIVSEP_MSG);
1179 if (imsg->fd == -1)
1180 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1182 packfd = imsg->fd;
1183 while (!pack_done) {
1184 ssize_t r = 0;
1186 err = got_poll_fd(infd, POLLIN, 1);
1187 if (err) {
1188 if (err->code != GOT_ERR_TIMEOUT)
1189 break;
1190 err = NULL;
1191 } else {
1192 r = read(infd, buf, sizeof(buf));
1193 if (r == -1) {
1194 err = got_error_from_errno("read");
1195 break;
1197 if (r == 0) {
1199 * Git clients hang up their side of the
1200 * connection after sending the pack file.
1202 err = NULL;
1203 pack_done = 1;
1204 break;
1208 if (r == 0) {
1209 /* Detect gotd(8) closing the pack pipe when done. */
1210 err = got_poll_fd(packfd, POLLOUT, 1);
1211 if (err) {
1212 if (err->code != GOT_ERR_EOF)
1213 break;
1214 err = NULL;
1215 pack_done = 1;
1217 } else {
1218 /* Write pack data and/or detect pipe being closed. */
1219 err = got_poll_write_full(packfd, buf, r);
1220 if (err) {
1221 if (err->code == GOT_ERR_EOF)
1222 err = NULL;
1223 break;
1228 close(packfd);
1229 return err;
1232 static const struct got_error *
1233 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1235 const struct got_error *err = NULL;
1236 struct gotd_imsg_packfile_status istatus;
1237 char buf[GOT_PKT_MAX];
1238 size_t datalen, len;
1239 char *reason = NULL;
1241 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1242 if (datalen < sizeof(istatus))
1243 return got_error(GOT_ERR_PRIVSEP_LEN);
1244 memcpy(&istatus, imsg->data, sizeof(istatus));
1245 if (datalen != sizeof(istatus) + istatus.reason_len)
1246 return got_error(GOT_ERR_PRIVSEP_LEN);
1248 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1249 if (reason == NULL) {
1250 err = got_error_from_errno("strndup");
1251 goto done;
1254 if (err == NULL)
1255 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1256 else
1257 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1258 if (len >= sizeof(buf)) {
1259 err = got_error(GOT_ERR_NO_SPACE);
1260 goto done;
1263 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1264 done:
1265 free(reason);
1266 return err;
1269 static const struct got_error *
1270 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1272 const struct got_error *err = NULL;
1273 struct gotd_imsg_ref_update_ok iok;
1274 size_t datalen, len;
1275 char buf[GOT_PKT_MAX];
1276 char *refname = NULL;
1278 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1279 if (datalen < sizeof(iok))
1280 return got_error(GOT_ERR_PRIVSEP_LEN);
1281 memcpy(&iok, imsg->data, sizeof(iok));
1282 if (datalen != sizeof(iok) + iok.name_len)
1283 return got_error(GOT_ERR_PRIVSEP_LEN);
1285 memcpy(&iok, imsg->data, sizeof(iok));
1287 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1288 if (refname == NULL)
1289 return got_error_from_errno("strndup");
1291 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1292 if (len >= sizeof(buf)) {
1293 err = got_error(GOT_ERR_NO_SPACE);
1294 goto done;
1297 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1298 done:
1299 free(refname);
1300 return err;
1303 static const struct got_error *
1304 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1306 const struct got_error *err = NULL;
1307 struct gotd_imsg_ref_update_ng ing;
1308 size_t datalen, len;
1309 char buf[GOT_PKT_MAX];
1310 char *refname = NULL, *reason = NULL;
1312 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1313 if (datalen < sizeof(ing))
1314 return got_error(GOT_ERR_PRIVSEP_LEN);
1315 memcpy(&ing, imsg->data, sizeof(ing));
1316 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1317 return got_error(GOT_ERR_PRIVSEP_LEN);
1319 memcpy(&ing, imsg->data, sizeof(ing));
1321 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1322 if (refname == NULL)
1323 return got_error_from_errno("strndup");
1325 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1326 ing.reason_len);
1327 if (reason == NULL) {
1328 err = got_error_from_errno("strndup");
1329 goto done;
1332 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1333 if (len >= sizeof(buf)) {
1334 err = got_error(GOT_ERR_NO_SPACE);
1335 goto done;
1338 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1339 done:
1340 free(refname);
1341 free(reason);
1342 return err;
1345 static const struct got_error *
1346 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1347 int chattygot)
1349 const struct got_error *err = NULL;
1350 char buf[GOT_PKT_MAX];
1351 struct imsgbuf ibuf;
1352 enum protostate {
1353 STATE_EXPECT_REF_UPDATE,
1354 STATE_EXPECT_MORE_REF_UPDATES,
1355 STATE_EXPECT_PACKFILE,
1356 STATE_PACKFILE_RECEIVED,
1357 STATE_REFS_UPDATED,
1359 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1360 struct imsg imsg;
1361 int report_status = 0;
1363 imsg_init(&ibuf, gotd_sock);
1364 memset(&imsg, 0, sizeof(imsg));
1366 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1367 if (err)
1368 goto done;
1370 while (curstate != STATE_EXPECT_PACKFILE) {
1371 int n;
1372 buf[0] = '\0';
1373 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1374 if (err)
1375 goto done;
1376 if (n == 0) {
1377 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1378 err = got_error_msg(GOT_ERR_BAD_PACKET,
1379 "unexpected flush packet received");
1380 goto done;
1382 err = forward_flushpkt(&ibuf);
1383 if (err)
1384 goto done;
1385 curstate = STATE_EXPECT_PACKFILE;
1386 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1387 if (curstate != STATE_EXPECT_REF_UPDATE &&
1388 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1389 err = got_error_msg(GOT_ERR_BAD_PACKET,
1390 "unexpected ref-update packet");
1391 goto done;
1393 if (curstate == STATE_EXPECT_REF_UPDATE) {
1394 err = recv_ref_update(&report_status,
1395 outfd, &ibuf, buf, n, 1, chattygot);
1396 } else {
1397 err = recv_ref_update(NULL, outfd, &ibuf,
1398 buf, n, 0, chattygot);
1400 if (err)
1401 goto done;
1402 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1403 } else {
1404 err = got_error(GOT_ERR_BAD_PACKET);
1405 goto done;
1409 while (curstate != STATE_PACKFILE_RECEIVED) {
1410 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1411 if (err)
1412 goto done;
1413 switch (imsg.hdr.type) {
1414 case GOTD_IMSG_ERROR:
1415 err = gotd_imsg_recv_error(NULL, &imsg);
1416 goto done;
1417 case GOTD_IMSG_PACKFILE_PIPE:
1418 err = recv_packfile(&imsg, infd);
1419 if (err) {
1420 if (err->code != GOT_ERR_EOF)
1421 goto done;
1423 * EOF is reported when the client hangs up,
1424 * which can happen with Git clients.
1425 * The socket should stay half-open so we
1426 * can still send our reports if requested.
1428 err = NULL;
1430 curstate = STATE_PACKFILE_RECEIVED;
1431 break;
1432 default:
1433 err = got_error(GOT_ERR_PRIVSEP_MSG);
1434 break;
1437 imsg_free(&imsg);
1438 if (err)
1439 goto done;
1442 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1443 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1444 if (err)
1445 break;
1446 switch (imsg.hdr.type) {
1447 case GOTD_IMSG_ERROR:
1448 err = gotd_imsg_recv_error(NULL, &imsg);
1449 break;
1450 case GOTD_IMSG_PACKFILE_STATUS:
1451 if (!report_status)
1452 break;
1453 err = report_unpack_status(&imsg, outfd, chattygot);
1454 break;
1455 case GOTD_IMSG_REF_UPDATE_OK:
1456 if (!report_status)
1457 break;
1458 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1459 break;
1460 case GOTD_IMSG_REF_UPDATE_NG:
1461 if (!report_status)
1462 break;
1463 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1464 break;
1465 case GOTD_IMSG_REFS_UPDATED:
1466 curstate = STATE_REFS_UPDATED;
1467 err = got_pkt_flushpkt(outfd, chattygot);
1468 break;
1469 default:
1470 err = got_error(GOT_ERR_PRIVSEP_MSG);
1471 break;
1474 imsg_free(&imsg);
1476 done:
1477 imsg_clear(&ibuf);
1478 if (err)
1479 echo_error(err, outfd, chattygot);
1480 return err;
1483 const struct got_error *
1484 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1485 int gotd_sock, int chattygot)
1487 const struct got_error *err = NULL;
1489 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1490 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1491 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1492 err = serve_write(infd, outfd, gotd_sock, repo_path,
1493 chattygot);
1494 else
1495 err = got_error(GOT_ERR_BAD_PACKET);
1497 return err;