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 const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
227 char buf[GOT_PKT_MAX];
228 size_t len, capalen = 0;
230 len = strlcpy(buf, line, sizeof(buf));
231 if (len >= sizeof(buf))
232 return got_error(GOT_ERR_NO_SPACE);
234 if (client_is_reading) {
235 err = got_gitproto_append_capabilities(&capalen, buf, len,
236 sizeof(buf), read_capabilities, nitems(read_capabilities));
237 if (err)
238 return err;
239 } else {
240 err = got_gitproto_append_capabilities(&capalen, buf, len,
241 sizeof(buf), write_capabilities,
242 nitems(write_capabilities));
243 if (err)
244 return err;
247 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
250 static void
251 echo_error(const struct got_error *err, int outfd, int chattygot)
253 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
254 size_t len;
256 /*
257 * Echo the error to the client on a pkt-line.
258 * The client should then terminate its session.
259 */
260 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
261 len = strlcat(buf, err->msg, sizeof(buf));
262 got_pkt_writepkt(outfd, buf, len, chattygot);
265 static const struct got_error *
266 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
267 const char *repo_path, int chattygot)
269 const struct got_error *err = NULL;
270 struct imsg imsg;
271 size_t datalen;
272 struct gotd_imsg_list_refs lsref;
273 struct gotd_imsg_reflist ireflist;
274 struct gotd_imsg_ref iref;
275 struct gotd_imsg_symref isymref;
276 size_t nrefs = 0;
277 int have_nrefs = 0, sent_capabilities = 0;
278 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
279 char *refname = NULL;
281 memset(&imsg, 0, sizeof(imsg));
282 memset(&lsref, 0, sizeof(lsref));
284 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
285 sizeof(lsref.repo_name))
286 return got_error(GOT_ERR_NO_SPACE);
287 lsref.client_is_reading = client_is_reading;
289 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
290 &lsref, sizeof(lsref)) == -1)
291 return got_error_from_errno("imsg_compose LIST_REFS");
293 err = gotd_imsg_flush(ibuf);
294 if (err)
295 return err;
297 while (!have_nrefs || nrefs > 0) {
298 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
299 if (err)
300 goto done;
301 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
302 switch (imsg.hdr.type) {
303 case GOTD_IMSG_ERROR:
304 err = gotd_imsg_recv_error(NULL, &imsg);
305 goto done;
306 case GOTD_IMSG_REFLIST:
307 if (have_nrefs || nrefs > 0) {
308 err = got_error(GOT_ERR_PRIVSEP_MSG);
309 goto done;
311 if (datalen != sizeof(ireflist)) {
312 err = got_error(GOT_ERR_PRIVSEP_MSG);
313 goto done;
315 memcpy(&ireflist, imsg.data, sizeof(ireflist));
316 nrefs = ireflist.nrefs;
317 have_nrefs = 1;
318 if (nrefs == 0)
319 err = send_zero_refs(outfd, client_is_reading,
320 chattygot);
321 break;
322 case GOTD_IMSG_REF:
323 if (!have_nrefs || nrefs == 0) {
324 err = got_error(GOT_ERR_PRIVSEP_MSG);
325 goto done;
327 if (datalen < sizeof(iref)) {
328 err = got_error(GOT_ERR_PRIVSEP_MSG);
329 goto done;
331 memcpy(&iref, imsg.data, sizeof(iref));
332 if (datalen != sizeof(iref) + iref.name_len) {
333 err = got_error(GOT_ERR_PRIVSEP_LEN);
334 goto done;
336 refname = strndup(imsg.data + sizeof(iref),
337 iref.name_len);
338 if (refname == NULL) {
339 err = got_error_from_errno("strndup");
340 goto done;
342 err = send_ref(outfd, iref.id, refname,
343 !sent_capabilities, client_is_reading,
344 NULL, chattygot);
345 free(refname);
346 refname = NULL;
347 if (err)
348 goto done;
349 sent_capabilities = 1;
350 if (nrefs > 0)
351 nrefs--;
352 break;
353 case GOTD_IMSG_SYMREF:
354 if (!have_nrefs || nrefs == 0) {
355 err = got_error(GOT_ERR_PRIVSEP_MSG);
356 goto done;
358 if (datalen < sizeof(isymref)) {
359 err = got_error(GOT_ERR_PRIVSEP_LEN);
360 goto done;
362 memcpy(&isymref, imsg.data, sizeof(isymref));
363 if (datalen != sizeof(isymref) + isymref.name_len +
364 isymref.target_len) {
365 err = got_error(GOT_ERR_PRIVSEP_LEN);
366 goto done;
369 /*
370 * For now, we only announce one symbolic ref,
371 * as part of our capability advertisement.
372 */
373 if (sent_capabilities || symrefstr != NULL ||
374 symrefname != NULL || symreftarget != NULL)
375 break;
377 symrefname = strndup(imsg.data + sizeof(isymref),
378 isymref.name_len);
379 if (symrefname == NULL) {
380 err = got_error_from_errno("malloc");
381 goto done;
384 symreftarget = strndup(
385 imsg.data + sizeof(isymref) + isymref.name_len,
386 isymref.target_len);
387 if (symreftarget == NULL) {
388 err = got_error_from_errno("strndup");
389 goto done;
392 if (asprintf(&symrefstr, "%s:%s", symrefname,
393 symreftarget) == -1) {
394 err = got_error_from_errno("asprintf");
395 goto done;
397 err = send_ref(outfd, isymref.target_id, symrefname,
398 !sent_capabilities, client_is_reading, symrefstr,
399 chattygot);
400 free(refname);
401 refname = NULL;
402 if (err)
403 goto done;
404 sent_capabilities = 1;
405 if (nrefs > 0)
406 nrefs--;
407 break;
408 default:
409 err = got_error(GOT_ERR_PRIVSEP_MSG);
410 break;
413 imsg_free(&imsg);
416 err = got_pkt_flushpkt(outfd, chattygot);
417 if (err)
418 goto done;
419 done:
420 free(symrefstr);
421 free(symrefname);
422 free(symreftarget);
423 return err;
426 static const struct got_error *
427 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
429 const struct got_error *err;
430 char *id_str = NULL, *client_capabilities = NULL;
432 err = got_gitproto_parse_want_line(&id_str,
433 &client_capabilities, buf, len);
434 if (err)
435 return err;
437 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
438 err = got_error_msg(GOT_ERR_BAD_PACKET,
439 "want-line with bad object ID");
440 goto done;
443 if (client_capabilities) {
444 err = got_gitproto_match_capabilities(common_capabilities,
445 NULL, client_capabilities, read_capabilities,
446 nitems(read_capabilities));
447 if (err)
448 goto done;
450 done:
451 free(id_str);
452 free(client_capabilities);
453 return err;
456 static const struct got_error *
457 parse_have_line(uint8_t *id, char *buf, size_t len)
459 const struct got_error *err;
460 char *id_str = NULL;
462 err = got_gitproto_parse_have_line(&id_str, buf, len);
463 if (err)
464 return err;
466 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
467 err = got_error_msg(GOT_ERR_BAD_PACKET,
468 "have-line with bad object ID");
469 goto done;
471 done:
472 free(id_str);
473 return err;
476 static const struct got_error *
477 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
479 const struct got_error *err = NULL;
480 struct gotd_imsg_capability icapa;
481 size_t len;
482 struct ibuf *wbuf;
484 memset(&icapa, 0, sizeof(icapa));
486 icapa.key_len = strlen(capa->key);
487 len = sizeof(icapa) + icapa.key_len;
488 if (capa->value) {
489 icapa.value_len = strlen(capa->value);
490 len += icapa.value_len;
493 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
494 if (wbuf == NULL) {
495 err = got_error_from_errno("imsg_create CAPABILITY");
496 return err;
499 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
500 return got_error_from_errno("imsg_add CAPABILITY");
501 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
502 return got_error_from_errno("imsg_add CAPABILITY");
503 if (capa->value) {
504 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
505 return got_error_from_errno("imsg_add CAPABILITY");
508 wbuf->fd = -1;
509 imsg_close(ibuf, wbuf);
511 return NULL;
514 static const struct got_error *
515 send_capabilities(int *use_sidebands, int *report_status,
516 char *capabilities_str, struct imsgbuf *ibuf)
518 const struct got_error *err = NULL;
519 struct gotd_imsg_capabilities icapas;
520 struct got_capability *capa = NULL;
521 size_t ncapa, i;
523 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
524 capabilities_str);
525 if (err)
526 return err;
528 icapas.ncapabilities = ncapa;
529 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
530 &icapas, sizeof(icapas)) == -1) {
531 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
532 goto done;
535 for (i = 0; i < ncapa; i++) {
536 err = send_capability(&capa[i], ibuf);
537 if (err)
538 goto done;
539 if (use_sidebands &&
540 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
541 *use_sidebands = 1;
542 if (report_status &&
543 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
544 *report_status = 1;
546 done:
547 free(capa);
548 return err;
551 static const struct got_error *
552 forward_flushpkt(struct imsgbuf *ibuf)
554 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
555 return got_error_from_errno("imsg_compose FLUSH");
557 return gotd_imsg_flush(ibuf);
560 static const struct got_error *
561 recv_ack(struct imsg *imsg, uint8_t *expected_id)
563 struct gotd_imsg_ack iack;
564 size_t datalen;
566 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
567 if (datalen != sizeof(iack))
568 return got_error(GOT_ERR_PRIVSEP_LEN);
570 memcpy(&iack, imsg->data, sizeof(iack));
571 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
572 return got_error(GOT_ERR_BAD_OBJ_ID);
574 return NULL;
577 static const struct got_error *
578 recv_nak(struct imsg *imsg, uint8_t *expected_id)
580 struct gotd_imsg_ack inak;
581 size_t datalen;
583 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
584 if (datalen != sizeof(inak))
585 return got_error(GOT_ERR_PRIVSEP_LEN);
587 memcpy(&inak, imsg->data, sizeof(inak));
588 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
589 return got_error(GOT_ERR_BAD_OBJ_ID);
591 return NULL;
595 static const struct got_error *
596 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
597 char *buf, size_t len, int expect_capabilities, int chattygot)
599 const struct got_error *err;
600 struct gotd_imsg_want iwant;
601 char *capabilities_str;
602 int done = 0;
603 struct imsg imsg;
605 memset(&iwant, 0, sizeof(iwant));
606 memset(&imsg, 0, sizeof(imsg));
608 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
609 if (err)
610 return err;
612 if (capabilities_str) {
613 if (!expect_capabilities) {
614 err = got_error_msg(GOT_ERR_BAD_PACKET,
615 "unexpected capability announcement received");
616 goto done;
618 err = send_capabilities(use_sidebands, NULL, capabilities_str,
619 ibuf);
620 if (err)
621 goto done;
625 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
626 &iwant, sizeof(iwant)) == -1) {
627 err = got_error_from_errno("imsg_compose WANT");
628 goto done;
631 err = gotd_imsg_flush(ibuf);
632 if (err)
633 goto done;
635 /*
636 * Wait for an ACK, or an error in case the desired object
637 * does not exist.
638 */
639 while (!done && err == NULL) {
640 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
641 if (err)
642 break;
643 switch (imsg.hdr.type) {
644 case GOTD_IMSG_ERROR:
645 err = gotd_imsg_recv_error(NULL, &imsg);
646 break;
647 case GOTD_IMSG_ACK:
648 err = recv_ack(&imsg, iwant.object_id);
649 if (err)
650 break;
651 done = 1;
652 break;
653 default:
654 err = got_error(GOT_ERR_PRIVSEP_MSG);
655 break;
658 imsg_free(&imsg);
660 done:
661 free(capabilities_str);
662 return err;
665 static const struct got_error *
666 send_ack(int outfd, uint8_t *id, int chattygot)
668 char hex[SHA1_DIGEST_STRING_LENGTH];
669 char buf[GOT_PKT_MAX];
670 int len;
672 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
673 return got_error(GOT_ERR_BAD_OBJ_ID);
675 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
676 if (len >= sizeof(buf))
677 return got_error(GOT_ERR_NO_SPACE);
679 return got_pkt_writepkt(outfd, buf, len, chattygot);
682 static const struct got_error *
683 send_nak(int outfd, int chattygot)
685 char buf[5];
686 int len;
688 len = snprintf(buf, sizeof(buf), "NAK\n");
689 if (len >= sizeof(buf))
690 return got_error(GOT_ERR_NO_SPACE);
692 return got_pkt_writepkt(outfd, buf, len, chattygot);
695 static const struct got_error *
696 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
697 size_t len, int chattygot)
699 const struct got_error *err;
700 struct gotd_imsg_have ihave;
701 int done = 0;
702 struct imsg imsg;
704 memset(&ihave, 0, sizeof(ihave));
705 memset(&imsg, 0, sizeof(imsg));
707 err = parse_have_line(ihave.object_id, buf, len);
708 if (err)
709 return err;
711 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
712 &ihave, sizeof(ihave)) == -1)
713 return got_error_from_errno("imsg_compose HAVE");
715 err = gotd_imsg_flush(ibuf);
716 if (err)
717 return err;
719 /*
720 * Wait for an ACK or a NAK, indicating whether a common
721 * commit object has been found.
722 */
723 while (!done && err == NULL) {
724 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
725 if (err)
726 return err;
727 switch (imsg.hdr.type) {
728 case GOTD_IMSG_ERROR:
729 err = gotd_imsg_recv_error(NULL, &imsg);
730 break;
731 case GOTD_IMSG_ACK:
732 err = recv_ack(&imsg, ihave.object_id);
733 if (err)
734 break;
735 if (!*have_ack) {
736 err = send_ack(outfd, ihave.object_id,
737 chattygot);
738 if (err)
739 return err;
740 *have_ack = 1;
742 done = 1;
743 break;
744 case GOTD_IMSG_NAK:
745 err = recv_nak(&imsg, ihave.object_id);
746 if (err)
747 break;
748 done = 1;
749 break;
750 default:
751 err = got_error(GOT_ERR_PRIVSEP_MSG);
752 break;
755 imsg_free(&imsg);
758 return err;
761 static const struct got_error *
762 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
764 const struct got_error *err;
765 struct imsg imsg;
767 *packfd = -1;
769 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
770 return got_error_from_errno("imsg_compose DONE");
772 err = gotd_imsg_flush(ibuf);
773 if (err)
774 return err;
776 while (*packfd == -1 && err == NULL) {
777 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
778 if (err)
779 break;
781 switch (imsg.hdr.type) {
782 case GOTD_IMSG_ERROR:
783 err = gotd_imsg_recv_error(NULL, &imsg);
784 break;
785 case GOTD_IMSG_PACKFILE_PIPE:
786 if (imsg.fd != -1)
787 *packfd = imsg.fd;
788 else
789 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
790 break;
791 default:
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
793 break;
796 imsg_free(&imsg);
799 return err;
802 static const struct got_error *
803 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
805 const struct got_error *err = NULL;
806 int pack_starting = 0;
807 struct gotd_imsg_packfile_progress iprog;
808 char buf[GOT_PKT_MAX];
809 struct imsg imsg;
810 size_t datalen;
811 int p_deltify = 0, n;
812 const char *eol = "\r";
814 memset(&imsg, 0, sizeof(imsg));
816 while (!pack_starting && err == NULL) {
817 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
818 if (err)
819 break;
821 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
822 switch (imsg.hdr.type) {
823 case GOTD_IMSG_ERROR:
824 err = gotd_imsg_recv_error(NULL, &imsg);
825 break;
826 case GOTD_IMSG_PACKFILE_READY:
827 eol = "\n";
828 pack_starting = 1;
829 /* fallthrough */
830 case GOTD_IMSG_PACKFILE_PROGRESS:
831 if (datalen != sizeof(iprog)) {
832 err = got_error(GOT_ERR_PRIVSEP_LEN);
833 break;
835 memcpy(&iprog, imsg.data, sizeof(iprog));
836 if (iprog.nobj_total > 0) {
837 p_deltify = (iprog.nobj_deltify * 100) /
838 iprog.nobj_total;
840 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
841 n = snprintf(&buf[1], sizeof(buf) - 1,
842 "%d commits colored, "
843 "%d objects found, "
844 "deltify %d%%%s",
845 iprog.ncolored,
846 iprog.nfound,
847 p_deltify, eol);
848 if (n >= sizeof(buf) - 1)
849 break;
850 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
851 break;
852 default:
853 err = got_error(GOT_ERR_PRIVSEP_MSG);
854 break;
857 imsg_free(&imsg);
860 return err;
863 static const struct got_error *
864 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
865 int chattygot)
867 const struct got_error *err = NULL;
868 char buf[GOT_PKT_MAX];
869 struct imsgbuf ibuf;
870 enum protostate {
871 STATE_EXPECT_WANT,
872 STATE_EXPECT_MORE_WANT,
873 STATE_EXPECT_HAVE,
874 STATE_EXPECT_DONE,
875 STATE_DONE,
876 };
877 enum protostate curstate = STATE_EXPECT_WANT;
878 int have_ack = 0, use_sidebands = 0, seen_have = 0;
879 int packfd = -1;
880 size_t pack_chunksize;
882 imsg_init(&ibuf, gotd_sock);
884 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
885 if (err)
886 goto done;
888 while (curstate != STATE_DONE) {
889 int n;
890 buf[0] = '\0';
891 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
892 if (err)
893 goto done;
894 if (n == 0) {
895 if (curstate != STATE_EXPECT_WANT &&
896 curstate != STATE_EXPECT_MORE_WANT &&
897 curstate != STATE_EXPECT_HAVE &&
898 curstate != STATE_EXPECT_DONE) {
899 err = got_error_msg(GOT_ERR_BAD_PACKET,
900 "unexpected flush packet received");
901 goto done;
904 if (curstate == STATE_EXPECT_WANT) {
905 ssize_t r;
906 /*
907 * If the client does not want to fetch
908 * anything we should receive a flush
909 * packet followed by EOF.
910 */
911 r = read(infd, buf, sizeof(buf));
912 if (r == -1) {
913 err = got_error_from_errno("read");
914 goto done;
916 if (r == 0) /* EOF */
917 goto done;
919 /* Zero-length field followed by payload. */
920 err = got_error_msg(GOT_ERR_BAD_PACKET,
921 "unexpected flush packet received");
922 goto done;
925 if (curstate == STATE_EXPECT_WANT ||
926 curstate == STATE_EXPECT_MORE_WANT ||
927 curstate == STATE_EXPECT_HAVE) {
928 err = forward_flushpkt(&ibuf);
929 if (err)
930 goto done;
932 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
933 err = send_nak(outfd, chattygot);
934 if (err)
935 goto done;
937 if (curstate == STATE_EXPECT_MORE_WANT)
938 curstate = STATE_EXPECT_HAVE;
939 else
940 curstate = STATE_EXPECT_DONE;
941 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
942 if (curstate != STATE_EXPECT_WANT &&
943 curstate != STATE_EXPECT_MORE_WANT) {
944 err = got_error_msg(GOT_ERR_BAD_PACKET,
945 "unexpected 'want' packet");
946 goto done;
948 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
949 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
950 if (err)
951 goto done;
952 if (curstate == STATE_EXPECT_WANT)
953 curstate = STATE_EXPECT_MORE_WANT;
954 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
955 if (curstate != STATE_EXPECT_HAVE &&
956 curstate != STATE_EXPECT_DONE) {
957 err = got_error_msg(GOT_ERR_BAD_PACKET,
958 "unexpected 'have' packet");
959 goto done;
961 if (curstate == STATE_EXPECT_HAVE) {
962 err = recv_have(&have_ack, outfd, &ibuf,
963 buf, n, chattygot);
964 if (err)
965 goto done;
966 seen_have = 1;
967 if (have_ack)
968 curstate = STATE_EXPECT_DONE;
970 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
971 if (curstate != STATE_EXPECT_HAVE &&
972 curstate != STATE_EXPECT_DONE) {
973 err = got_error_msg(GOT_ERR_BAD_PACKET,
974 "unexpected 'done' packet");
975 goto done;
977 err = recv_done(&packfd, outfd, &ibuf, chattygot);
978 if (err)
979 goto done;
980 curstate = STATE_DONE;
981 break;
982 } else {
983 err = got_error(GOT_ERR_BAD_PACKET);
984 goto done;
988 if (!seen_have) {
989 err = send_nak(outfd, chattygot);
990 if (err)
991 goto done;
994 if (use_sidebands) {
995 err = relay_progress_reports(&ibuf, outfd, chattygot);
996 if (err)
997 goto done;
998 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
999 } else
1000 pack_chunksize = sizeof(buf);
1002 for (;;) {
1003 ssize_t r;
1005 r = read(packfd, use_sidebands ? &buf[1] : buf,
1006 pack_chunksize);
1007 if (r == -1) {
1008 err = got_error_from_errno("read");
1009 break;
1010 } else if (r == 0) {
1011 err = got_pkt_flushpkt(outfd, chattygot);
1012 break;
1015 if (use_sidebands) {
1016 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1017 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1018 if (err)
1019 break;
1020 } else {
1021 err = got_poll_write_full(outfd, buf, r);
1022 if (err) {
1023 if (err->code == GOT_ERR_EOF)
1024 err = NULL;
1025 break;
1029 done:
1030 imsg_clear(&ibuf);
1031 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1032 err = got_error_from_errno("close");
1033 if (err)
1034 echo_error(err, outfd, chattygot);
1035 return err;
1038 static const struct got_error *
1039 parse_ref_update_line(char **common_capabilities, char **refname,
1040 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1042 const struct got_error *err;
1043 char *old_id_str = NULL, *new_id_str = NULL;
1044 char *client_capabilities = NULL;
1046 *refname = NULL;
1048 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1049 refname, &client_capabilities, buf, len);
1050 if (err)
1051 return err;
1053 if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
1054 !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
1055 err = got_error_msg(GOT_ERR_BAD_PACKET,
1056 "ref-update with bad object ID");
1057 goto done;
1059 if (!got_ref_name_is_valid(*refname)) {
1060 err = got_error_msg(GOT_ERR_BAD_PACKET,
1061 "ref-update with bad reference name");
1062 goto done;
1065 if (client_capabilities) {
1066 err = got_gitproto_match_capabilities(common_capabilities,
1067 NULL, client_capabilities, write_capabilities,
1068 nitems(write_capabilities));
1069 if (err)
1070 goto done;
1072 done:
1073 free(old_id_str);
1074 free(new_id_str);
1075 free(client_capabilities);
1076 if (err) {
1077 free(*refname);
1078 *refname = NULL;
1080 return err;
1083 static const struct got_error *
1084 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1085 char *buf, size_t len, int expect_capabilities, int chattygot)
1087 const struct got_error *err;
1088 struct gotd_imsg_ref_update iref;
1089 struct ibuf *wbuf;
1090 char *capabilities_str = NULL, *refname = NULL;
1091 int done = 0;
1092 struct imsg imsg;
1094 memset(&iref, 0, sizeof(iref));
1095 memset(&imsg, 0, sizeof(imsg));
1097 err = parse_ref_update_line(&capabilities_str, &refname,
1098 iref.old_id, iref.new_id, buf, len);
1099 if (err)
1100 return err;
1102 if (capabilities_str) {
1103 if (!expect_capabilities) {
1104 err = got_error_msg(GOT_ERR_BAD_PACKET,
1105 "unexpected capability announcement received");
1106 goto done;
1108 err = send_capabilities(NULL, report_status, capabilities_str,
1109 ibuf);
1110 if (err)
1111 goto done;
1114 iref.name_len = strlen(refname);
1115 len = sizeof(iref) + iref.name_len;
1116 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1117 if (wbuf == NULL) {
1118 err = got_error_from_errno("imsg_create REF_UPDATE");
1119 goto done;
1122 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1123 return got_error_from_errno("imsg_add REF_UPDATE");
1124 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1125 return got_error_from_errno("imsg_add REF_UPDATE");
1126 wbuf->fd = -1;
1127 imsg_close(ibuf, wbuf);
1129 err = gotd_imsg_flush(ibuf);
1130 if (err)
1131 goto done;
1133 /* Wait for ACK or an error. */
1134 while (!done && err == NULL) {
1135 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1136 if (err)
1137 break;
1138 switch (imsg.hdr.type) {
1139 case GOTD_IMSG_ERROR:
1140 err = gotd_imsg_recv_error(NULL, &imsg);
1141 break;
1142 case GOTD_IMSG_ACK:
1143 err = recv_ack(&imsg, iref.new_id);
1144 if (err)
1145 break;
1146 done = 1;
1147 break;
1148 default:
1149 err = got_error(GOT_ERR_PRIVSEP_MSG);
1150 break;
1153 imsg_free(&imsg);
1155 done:
1156 free(capabilities_str);
1157 free(refname);
1158 return err;
1161 static const struct got_error *
1162 recv_packfile(struct imsg *imsg, int infd)
1164 const struct got_error *err = NULL;
1165 size_t datalen;
1166 int packfd;
1167 char buf[GOT_PKT_MAX];
1168 int pack_done = 0;
1170 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1171 if (datalen != 0)
1172 return got_error(GOT_ERR_PRIVSEP_MSG);
1174 if (imsg->fd == -1)
1175 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1177 packfd = imsg->fd;
1178 while (!pack_done) {
1179 ssize_t r = 0;
1181 err = got_poll_fd(infd, POLLIN, 1);
1182 if (err) {
1183 if (err->code != GOT_ERR_TIMEOUT)
1184 break;
1185 err = NULL;
1186 } else {
1187 r = read(infd, buf, sizeof(buf));
1188 if (r == -1) {
1189 err = got_error_from_errno("read");
1190 break;
1192 if (r == 0) {
1194 * Git clients hang up their side of the
1195 * connection after sending the pack file.
1197 err = NULL;
1198 pack_done = 1;
1199 break;
1203 if (r == 0) {
1204 /* Detect gotd(8) closing the pack pipe when done. */
1205 err = got_poll_fd(packfd, POLLOUT, 1);
1206 if (err) {
1207 if (err->code != GOT_ERR_EOF)
1208 break;
1209 err = NULL;
1210 pack_done = 1;
1212 } else {
1213 /* Write pack data and/or detect pipe being closed. */
1214 err = got_poll_write_full(packfd, buf, r);
1215 if (err) {
1216 if (err->code == GOT_ERR_EOF)
1217 err = NULL;
1218 break;
1223 close(packfd);
1224 return err;
1227 static const struct got_error *
1228 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1230 const struct got_error *err = NULL;
1231 struct gotd_imsg_packfile_status istatus;
1232 char buf[GOT_PKT_MAX];
1233 size_t datalen, len;
1234 char *reason = NULL;
1236 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1237 if (datalen < sizeof(istatus))
1238 return got_error(GOT_ERR_PRIVSEP_LEN);
1239 memcpy(&istatus, imsg->data, sizeof(istatus));
1240 if (datalen != sizeof(istatus) + istatus.reason_len)
1241 return got_error(GOT_ERR_PRIVSEP_LEN);
1243 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1244 if (reason == NULL) {
1245 err = got_error_from_errno("strndup");
1246 goto done;
1249 if (err == NULL)
1250 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1251 else
1252 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1253 if (len >= sizeof(buf)) {
1254 err = got_error(GOT_ERR_NO_SPACE);
1255 goto done;
1258 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1259 done:
1260 free(reason);
1261 return err;
1264 static const struct got_error *
1265 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1267 const struct got_error *err = NULL;
1268 struct gotd_imsg_ref_update_ok iok;
1269 size_t datalen, len;
1270 char buf[GOT_PKT_MAX];
1271 char *refname = NULL;
1273 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1274 if (datalen < sizeof(iok))
1275 return got_error(GOT_ERR_PRIVSEP_LEN);
1276 memcpy(&iok, imsg->data, sizeof(iok));
1277 if (datalen != sizeof(iok) + iok.name_len)
1278 return got_error(GOT_ERR_PRIVSEP_LEN);
1280 memcpy(&iok, imsg->data, sizeof(iok));
1282 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1283 if (refname == NULL)
1284 return got_error_from_errno("strndup");
1286 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1287 if (len >= sizeof(buf)) {
1288 err = got_error(GOT_ERR_NO_SPACE);
1289 goto done;
1292 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1293 done:
1294 free(refname);
1295 return err;
1298 static const struct got_error *
1299 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1301 const struct got_error *err = NULL;
1302 struct gotd_imsg_ref_update_ng ing;
1303 size_t datalen, len;
1304 char buf[GOT_PKT_MAX];
1305 char *refname = NULL, *reason = NULL;
1307 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1308 if (datalen < sizeof(ing))
1309 return got_error(GOT_ERR_PRIVSEP_LEN);
1310 memcpy(&ing, imsg->data, sizeof(ing));
1311 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1312 return got_error(GOT_ERR_PRIVSEP_LEN);
1314 memcpy(&ing, imsg->data, sizeof(ing));
1316 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1317 if (refname == NULL)
1318 return got_error_from_errno("strndup");
1320 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1321 ing.reason_len);
1322 if (reason == NULL) {
1323 err = got_error_from_errno("strndup");
1324 goto done;
1327 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1328 if (len >= sizeof(buf)) {
1329 err = got_error(GOT_ERR_NO_SPACE);
1330 goto done;
1333 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1334 done:
1335 free(refname);
1336 free(reason);
1337 return err;
1340 static const struct got_error *
1341 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1342 int chattygot)
1344 const struct got_error *err = NULL;
1345 char buf[GOT_PKT_MAX];
1346 struct imsgbuf ibuf;
1347 enum protostate {
1348 STATE_EXPECT_REF_UPDATE,
1349 STATE_EXPECT_MORE_REF_UPDATES,
1350 STATE_EXPECT_PACKFILE,
1351 STATE_PACKFILE_RECEIVED,
1352 STATE_REFS_UPDATED,
1354 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1355 struct imsg imsg;
1356 int report_status = 0;
1358 imsg_init(&ibuf, gotd_sock);
1359 memset(&imsg, 0, sizeof(imsg));
1361 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1362 if (err)
1363 goto done;
1365 while (curstate != STATE_EXPECT_PACKFILE) {
1366 int n;
1367 buf[0] = '\0';
1368 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1369 if (err)
1370 goto done;
1371 if (n == 0) {
1372 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1373 err = got_error_msg(GOT_ERR_BAD_PACKET,
1374 "unexpected flush packet received");
1375 goto done;
1377 err = forward_flushpkt(&ibuf);
1378 if (err)
1379 goto done;
1380 curstate = STATE_EXPECT_PACKFILE;
1381 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1382 if (curstate != STATE_EXPECT_REF_UPDATE &&
1383 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1384 err = got_error_msg(GOT_ERR_BAD_PACKET,
1385 "unexpected ref-update packet");
1386 goto done;
1388 if (curstate == STATE_EXPECT_REF_UPDATE) {
1389 err = recv_ref_update(&report_status,
1390 outfd, &ibuf, buf, n, 1, chattygot);
1391 } else {
1392 err = recv_ref_update(NULL, outfd, &ibuf,
1393 buf, n, 0, chattygot);
1395 if (err)
1396 goto done;
1397 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1398 } else {
1399 err = got_error(GOT_ERR_BAD_PACKET);
1400 goto done;
1404 while (curstate != STATE_PACKFILE_RECEIVED) {
1405 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1406 if (err)
1407 goto done;
1408 switch (imsg.hdr.type) {
1409 case GOTD_IMSG_ERROR:
1410 err = gotd_imsg_recv_error(NULL, &imsg);
1411 goto done;
1412 case GOTD_IMSG_PACKFILE_PIPE:
1413 err = recv_packfile(&imsg, infd);
1414 if (err) {
1415 if (err->code != GOT_ERR_EOF)
1416 goto done;
1418 * EOF is reported when the client hangs up,
1419 * which can happen with Git clients.
1420 * The socket should stay half-open so we
1421 * can still send our reports if requested.
1423 err = NULL;
1425 curstate = STATE_PACKFILE_RECEIVED;
1426 break;
1427 default:
1428 err = got_error(GOT_ERR_PRIVSEP_MSG);
1429 break;
1432 imsg_free(&imsg);
1433 if (err)
1434 goto done;
1437 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1438 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1439 if (err)
1440 break;
1441 switch (imsg.hdr.type) {
1442 case GOTD_IMSG_ERROR:
1443 err = gotd_imsg_recv_error(NULL, &imsg);
1444 break;
1445 case GOTD_IMSG_PACKFILE_STATUS:
1446 if (!report_status)
1447 break;
1448 err = report_unpack_status(&imsg, outfd, chattygot);
1449 break;
1450 case GOTD_IMSG_REF_UPDATE_OK:
1451 if (!report_status)
1452 break;
1453 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1454 break;
1455 case GOTD_IMSG_REF_UPDATE_NG:
1456 if (!report_status)
1457 break;
1458 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1459 break;
1460 case GOTD_IMSG_REFS_UPDATED:
1461 curstate = STATE_REFS_UPDATED;
1462 err = got_pkt_flushpkt(outfd, chattygot);
1463 break;
1464 default:
1465 err = got_error(GOT_ERR_PRIVSEP_MSG);
1466 break;
1469 imsg_free(&imsg);
1471 done:
1472 imsg_clear(&ibuf);
1473 if (err)
1474 echo_error(err, outfd, chattygot);
1475 return err;
1478 const struct got_error *
1479 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1480 int gotd_sock, int chattygot)
1482 const struct got_error *err = NULL;
1484 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1485 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1486 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1487 err = serve_write(infd, outfd, gotd_sock, repo_path,
1488 chattygot);
1489 else
1490 err = got_error(GOT_ERR_BAD_PACKET);
1492 return err;