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 <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <imsg.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_serve.h"
34 #include "got_path.h"
35 #include "got_version.h"
36 #include "got_reference.h"
37 #include "got_object.h"
39 #include "got_lib_pkt.h"
40 #include "got_lib_dial.h"
41 #include "got_lib_gitproto.h"
42 #include "got_lib_hash.h"
43 #include "got_lib_poll.h"
45 #include "gotd.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
51 static const struct got_capability read_capabilities[] = {
52 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
53 { GOT_CAPA_OFS_DELTA, NULL },
54 { GOT_CAPA_SIDE_BAND_64K, NULL },
55 };
57 static const struct got_capability write_capabilities[] = {
58 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
59 { GOT_CAPA_OFS_DELTA, NULL },
60 { GOT_CAPA_REPORT_STATUS, NULL },
61 { GOT_CAPA_NO_THIN, NULL },
62 { GOT_CAPA_DELETE_REFS, NULL },
63 };
65 const struct got_error *
66 got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
67 {
68 const struct got_error *err = NULL;
69 size_t len, cmdlen, pathlen;
70 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
71 const char *relpath;
73 *command = NULL;
74 *repo_path = NULL;
76 len = strlen(gitcmd);
78 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
79 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
80 strlen(GOT_SERVE_CMD_SEND)) == 0)
81 cmdlen = strlen(GOT_SERVE_CMD_SEND);
82 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
83 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
84 strlen(GOT_SERVE_CMD_FETCH)) == 0)
85 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
86 else
87 return got_error(GOT_ERR_BAD_PACKET);
89 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
90 return got_error(GOT_ERR_BAD_PACKET);
92 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
93 return got_error(GOT_ERR_BAD_PATH);
95 /* Forbid linefeeds in paths, like Git does. */
96 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
97 return got_error(GOT_ERR_BAD_PATH);
99 path0 = strdup(&gitcmd[cmdlen + 1]);
100 if (path0 == NULL)
101 return got_error_from_errno("strdup");
102 path = path0;
103 pathlen = strlen(path);
105 /*
106 * Git clients send a shell command.
107 * Trim spaces and quotes around the path.
108 */
109 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
110 path++;
111 pathlen--;
113 while (pathlen > 0 &&
114 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
115 path[pathlen - 1] == ' ')) {
116 path[pathlen - 1] = '\0';
117 pathlen--;
120 /* Deny an empty repository path. */
121 if (path[0] == '\0' || got_path_is_root_dir(path)) {
122 err = got_error(GOT_ERR_NOT_GIT_REPO);
123 goto done;
126 if (asprintf(&abspath, "/%s", path) == -1) {
127 err = got_error_from_errno("asprintf");
128 goto done;
130 pathlen = strlen(abspath);
131 canonpath = malloc(pathlen);
132 if (canonpath == NULL) {
133 err = got_error_from_errno("malloc");
134 goto done;
136 err = got_canonpath(abspath, canonpath, pathlen);
137 if (err)
138 goto done;
140 relpath = canonpath;
141 while (relpath[0] == '/')
142 relpath++;
143 *repo_path = strdup(relpath);
144 if (*repo_path == NULL) {
145 err = got_error_from_errno("strdup");
146 goto done;
148 *command = strndup(gitcmd, cmdlen);
149 if (*command == NULL)
150 err = got_error_from_errno("strndup");
151 done:
152 free(path0);
153 free(abspath);
154 free(canonpath);
155 if (err) {
156 free(*repo_path);
157 *repo_path = NULL;
159 return err;
162 static const struct got_error *
163 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
164 uint8_t *buf, size_t bufsize)
166 struct got_capability capa[nitems(read_capabilities) + 1];
167 size_t ncapa;
169 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
170 if (symrefstr) {
171 capa[nitems(read_capabilities)].key = "symref";
172 capa[nitems(read_capabilities)].value = symrefstr;
173 ncapa = nitems(capa);
174 } else
175 ncapa = nitems(read_capabilities);
177 return got_gitproto_append_capabilities(capalen, buf, len,
178 bufsize, capa, ncapa);
181 static const struct got_error *
182 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
183 int client_is_reading, const char *symrefstr, int chattygot)
185 const struct got_error *err = NULL;
186 char hex[SHA1_DIGEST_STRING_LENGTH];
187 char buf[GOT_PKT_MAX];
188 size_t len, capalen = 0;
190 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
191 return got_error(GOT_ERR_BAD_OBJ_ID);
193 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
194 if (len >= sizeof(buf))
195 return got_error(GOT_ERR_NO_SPACE);
197 if (send_capabilities) {
198 if (client_is_reading) {
199 err = append_read_capabilities(&capalen, len,
200 symrefstr, buf, sizeof(buf));
201 } else {
202 err = got_gitproto_append_capabilities(&capalen,
203 buf, len, sizeof(buf), write_capabilities,
204 nitems(write_capabilities));
206 if (err)
207 return err;
208 len += capalen;
211 if (len + 1 >= sizeof(buf))
212 return got_error(GOT_ERR_NO_SPACE);
213 buf[len] = '\n';
214 len++;
215 buf[len] = '\0';
217 return got_pkt_writepkt(outfd, buf, len, chattygot);
220 static const struct got_error *
221 send_zero_refs(int outfd, int client_is_reading, int chattygot)
223 const struct got_error *err = NULL;
224 const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
225 char buf[GOT_PKT_MAX];
226 size_t len, capalen = 0;
228 len = strlcpy(buf, line, sizeof(buf));
229 if (len >= sizeof(buf))
230 return got_error(GOT_ERR_NO_SPACE);
232 if (client_is_reading) {
233 err = got_gitproto_append_capabilities(&capalen, buf, len,
234 sizeof(buf), read_capabilities, nitems(read_capabilities));
235 if (err)
236 return err;
237 } else {
238 err = got_gitproto_append_capabilities(&capalen, buf, len,
239 sizeof(buf), write_capabilities,
240 nitems(write_capabilities));
241 if (err)
242 return err;
245 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
248 static void
249 echo_error(const struct got_error *err, int outfd, int chattygot)
251 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
252 size_t len;
254 /*
255 * Echo the error to the client on a pkt-line.
256 * The client should then terminate its session.
257 */
258 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
259 len = strlcat(buf, err->msg, sizeof(buf));
260 got_pkt_writepkt(outfd, buf, len, chattygot);
263 static const struct got_error *
264 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
265 const char *repo_path, int chattygot)
267 const struct got_error *err = NULL;
268 struct imsg imsg;
269 size_t datalen;
270 struct gotd_imsg_list_refs lsref;
271 struct gotd_imsg_reflist ireflist;
272 struct gotd_imsg_ref iref;
273 struct gotd_imsg_symref isymref;
274 size_t nrefs = 0;
275 int have_nrefs = 0, sent_capabilities = 0;
276 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
277 char *refname = NULL;
279 memset(&imsg, 0, sizeof(imsg));
280 memset(&lsref, 0, sizeof(lsref));
282 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
283 sizeof(lsref.repo_name))
284 return got_error(GOT_ERR_NO_SPACE);
285 lsref.client_is_reading = client_is_reading;
287 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
288 &lsref, sizeof(lsref)) == -1)
289 return got_error_from_errno("imsg_compose LIST_REFS");
291 err = gotd_imsg_flush(ibuf);
292 if (err)
293 return err;
295 while (!have_nrefs || nrefs > 0) {
296 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
297 if (err)
298 goto done;
299 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
300 switch (imsg.hdr.type) {
301 case GOTD_IMSG_ERROR:
302 err = gotd_imsg_recv_error(NULL, &imsg);
303 goto done;
304 case GOTD_IMSG_REFLIST:
305 if (have_nrefs || nrefs > 0) {
306 err = got_error(GOT_ERR_PRIVSEP_MSG);
307 goto done;
309 if (datalen != sizeof(ireflist)) {
310 err = got_error(GOT_ERR_PRIVSEP_MSG);
311 goto done;
313 memcpy(&ireflist, imsg.data, sizeof(ireflist));
314 nrefs = ireflist.nrefs;
315 have_nrefs = 1;
316 if (nrefs == 0)
317 err = send_zero_refs(outfd, client_is_reading,
318 chattygot);
319 break;
320 case GOTD_IMSG_REF:
321 if (!have_nrefs || nrefs == 0) {
322 err = got_error(GOT_ERR_PRIVSEP_MSG);
323 goto done;
325 if (datalen < sizeof(iref)) {
326 err = got_error(GOT_ERR_PRIVSEP_MSG);
327 goto done;
329 memcpy(&iref, imsg.data, sizeof(iref));
330 if (datalen != sizeof(iref) + iref.name_len) {
331 err = got_error(GOT_ERR_PRIVSEP_LEN);
332 goto done;
334 refname = strndup(imsg.data + sizeof(iref),
335 iref.name_len);
336 if (refname == NULL) {
337 err = got_error_from_errno("strndup");
338 goto done;
340 err = send_ref(outfd, iref.id, refname,
341 !sent_capabilities, client_is_reading,
342 NULL, chattygot);
343 free(refname);
344 refname = NULL;
345 if (err)
346 goto done;
347 sent_capabilities = 1;
348 if (nrefs > 0)
349 nrefs--;
350 break;
351 case GOTD_IMSG_SYMREF:
352 if (!have_nrefs || nrefs == 0) {
353 err = got_error(GOT_ERR_PRIVSEP_MSG);
354 goto done;
356 if (datalen < sizeof(isymref)) {
357 err = got_error(GOT_ERR_PRIVSEP_LEN);
358 goto done;
360 memcpy(&isymref, imsg.data, sizeof(isymref));
361 if (datalen != sizeof(isymref) + isymref.name_len +
362 isymref.target_len) {
363 err = got_error(GOT_ERR_PRIVSEP_LEN);
364 goto done;
367 /*
368 * For now, we only announce one symbolic ref,
369 * as part of our capability advertisement.
370 */
371 if (sent_capabilities || symrefstr != NULL ||
372 symrefname != NULL || symreftarget != NULL)
373 break;
375 symrefname = strndup(imsg.data + sizeof(isymref),
376 isymref.name_len);
377 if (symrefname == NULL) {
378 err = got_error_from_errno("malloc");
379 goto done;
382 symreftarget = strndup(
383 imsg.data + sizeof(isymref) + isymref.name_len,
384 isymref.target_len);
385 if (symreftarget == NULL) {
386 err = got_error_from_errno("strndup");
387 goto done;
390 if (asprintf(&symrefstr, "%s:%s", symrefname,
391 symreftarget) == -1) {
392 err = got_error_from_errno("asprintf");
393 goto done;
395 err = send_ref(outfd, isymref.target_id, symrefname,
396 !sent_capabilities, client_is_reading, symrefstr,
397 chattygot);
398 free(refname);
399 refname = NULL;
400 if (err)
401 goto done;
402 sent_capabilities = 1;
403 if (nrefs > 0)
404 nrefs--;
405 break;
406 default:
407 err = got_error(GOT_ERR_PRIVSEP_MSG);
408 break;
411 imsg_free(&imsg);
414 err = got_pkt_flushpkt(outfd, chattygot);
415 if (err)
416 goto done;
417 done:
418 free(symrefstr);
419 free(symrefname);
420 free(symreftarget);
421 return err;
424 static const struct got_error *
425 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
427 const struct got_error *err;
428 char *id_str = NULL, *client_capabilities = NULL;
430 err = got_gitproto_parse_want_line(&id_str,
431 &client_capabilities, buf, len);
432 if (err)
433 return err;
435 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
436 err = got_error_msg(GOT_ERR_BAD_PACKET,
437 "want-line with bad object ID");
438 goto done;
441 if (client_capabilities) {
442 err = got_gitproto_match_capabilities(common_capabilities,
443 NULL, client_capabilities, read_capabilities,
444 nitems(read_capabilities));
445 if (err)
446 goto done;
448 done:
449 free(id_str);
450 free(client_capabilities);
451 return err;
454 static const struct got_error *
455 parse_have_line(uint8_t *id, char *buf, size_t len)
457 const struct got_error *err;
458 char *id_str = NULL;
460 err = got_gitproto_parse_have_line(&id_str, buf, len);
461 if (err)
462 return err;
464 if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
465 err = got_error_msg(GOT_ERR_BAD_PACKET,
466 "have-line with bad object ID");
467 goto done;
469 done:
470 free(id_str);
471 return err;
474 static const struct got_error *
475 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
477 const struct got_error *err = NULL;
478 struct gotd_imsg_capability icapa;
479 size_t len;
480 struct ibuf *wbuf;
482 memset(&icapa, 0, sizeof(icapa));
484 icapa.key_len = strlen(capa->key);
485 len = sizeof(icapa) + icapa.key_len;
486 if (capa->value) {
487 icapa.value_len = strlen(capa->value);
488 len += icapa.value_len;
491 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
492 if (wbuf == NULL) {
493 err = got_error_from_errno("imsg_create CAPABILITY");
494 return err;
497 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
498 return got_error_from_errno("imsg_add CAPABILITY");
499 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
500 return got_error_from_errno("imsg_add CAPABILITY");
501 if (capa->value) {
502 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
503 return got_error_from_errno("imsg_add CAPABILITY");
506 wbuf->fd = -1;
507 imsg_close(ibuf, wbuf);
509 return NULL;
512 static const struct got_error *
513 send_capabilities(int *use_sidebands, int *report_status,
514 char *capabilities_str, struct imsgbuf *ibuf)
516 const struct got_error *err = NULL;
517 struct gotd_imsg_capabilities icapas;
518 struct got_capability *capa = NULL;
519 size_t ncapa, i;
521 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
522 capabilities_str);
523 if (err)
524 return err;
526 icapas.ncapabilities = ncapa;
527 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
528 &icapas, sizeof(icapas)) == -1) {
529 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
530 goto done;
533 for (i = 0; i < ncapa; i++) {
534 err = send_capability(&capa[i], ibuf);
535 if (err)
536 goto done;
537 if (use_sidebands &&
538 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
539 *use_sidebands = 1;
540 if (report_status &&
541 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
542 *report_status = 1;
544 done:
545 free(capa);
546 return err;
549 static const struct got_error *
550 forward_flushpkt(struct imsgbuf *ibuf)
552 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
553 return got_error_from_errno("imsg_compose FLUSH");
555 return gotd_imsg_flush(ibuf);
558 static const struct got_error *
559 recv_ack(struct imsg *imsg, uint8_t *expected_id)
561 struct gotd_imsg_ack iack;
562 size_t datalen;
564 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
565 if (datalen != sizeof(iack))
566 return got_error(GOT_ERR_PRIVSEP_LEN);
568 memcpy(&iack, imsg->data, sizeof(iack));
569 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
570 return got_error(GOT_ERR_BAD_OBJ_ID);
572 return NULL;
575 static const struct got_error *
576 recv_nak(struct imsg *imsg, uint8_t *expected_id)
578 struct gotd_imsg_ack inak;
579 size_t datalen;
581 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
582 if (datalen != sizeof(inak))
583 return got_error(GOT_ERR_PRIVSEP_LEN);
585 memcpy(&inak, imsg->data, sizeof(inak));
586 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
587 return got_error(GOT_ERR_BAD_OBJ_ID);
589 return NULL;
593 static const struct got_error *
594 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
595 char *buf, size_t len, int expect_capabilities, int chattygot)
597 const struct got_error *err;
598 struct gotd_imsg_want iwant;
599 char *capabilities_str;
600 int done = 0;
601 struct imsg imsg;
603 memset(&iwant, 0, sizeof(iwant));
604 memset(&imsg, 0, sizeof(imsg));
606 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
607 if (err)
608 return err;
610 if (capabilities_str) {
611 if (!expect_capabilities) {
612 err = got_error_msg(GOT_ERR_BAD_PACKET,
613 "unexpected capability announcement received");
614 goto done;
616 err = send_capabilities(use_sidebands, NULL, capabilities_str,
617 ibuf);
618 if (err)
619 goto done;
623 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
624 &iwant, sizeof(iwant)) == -1) {
625 err = got_error_from_errno("imsg_compose WANT");
626 goto done;
629 err = gotd_imsg_flush(ibuf);
630 if (err)
631 goto done;
633 /*
634 * Wait for an ACK, or an error in case the desired object
635 * does not exist.
636 */
637 while (!done && err == NULL) {
638 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
639 if (err)
640 break;
641 switch (imsg.hdr.type) {
642 case GOTD_IMSG_ERROR:
643 err = gotd_imsg_recv_error(NULL, &imsg);
644 break;
645 case GOTD_IMSG_ACK:
646 err = recv_ack(&imsg, iwant.object_id);
647 if (err)
648 break;
649 done = 1;
650 break;
651 default:
652 err = got_error(GOT_ERR_PRIVSEP_MSG);
653 break;
656 imsg_free(&imsg);
658 done:
659 free(capabilities_str);
660 return err;
663 static const struct got_error *
664 send_ack(int outfd, uint8_t *id, int chattygot)
666 char hex[SHA1_DIGEST_STRING_LENGTH];
667 char buf[GOT_PKT_MAX];
668 int len;
670 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
671 return got_error(GOT_ERR_BAD_OBJ_ID);
673 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
674 if (len >= sizeof(buf))
675 return got_error(GOT_ERR_NO_SPACE);
677 return got_pkt_writepkt(outfd, buf, len, chattygot);
680 static const struct got_error *
681 send_nak(int outfd, int chattygot)
683 char buf[5];
684 int len;
686 len = snprintf(buf, sizeof(buf), "NAK\n");
687 if (len >= sizeof(buf))
688 return got_error(GOT_ERR_NO_SPACE);
690 return got_pkt_writepkt(outfd, buf, len, chattygot);
693 static const struct got_error *
694 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
695 size_t len, int chattygot)
697 const struct got_error *err;
698 struct gotd_imsg_have ihave;
699 int done = 0;
700 struct imsg imsg;
702 memset(&ihave, 0, sizeof(ihave));
703 memset(&imsg, 0, sizeof(imsg));
705 err = parse_have_line(ihave.object_id, buf, len);
706 if (err)
707 return err;
709 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
710 &ihave, sizeof(ihave)) == -1)
711 return got_error_from_errno("imsg_compose HAVE");
713 err = gotd_imsg_flush(ibuf);
714 if (err)
715 return err;
717 /*
718 * Wait for an ACK or a NAK, indicating whether a common
719 * commit object has been found.
720 */
721 while (!done && err == NULL) {
722 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
723 if (err)
724 return err;
725 switch (imsg.hdr.type) {
726 case GOTD_IMSG_ERROR:
727 err = gotd_imsg_recv_error(NULL, &imsg);
728 break;
729 case GOTD_IMSG_ACK:
730 err = recv_ack(&imsg, ihave.object_id);
731 if (err)
732 break;
733 if (!*have_ack) {
734 err = send_ack(outfd, ihave.object_id,
735 chattygot);
736 if (err)
737 return err;
738 *have_ack = 1;
740 done = 1;
741 break;
742 case GOTD_IMSG_NAK:
743 err = recv_nak(&imsg, ihave.object_id);
744 if (err)
745 break;
746 done = 1;
747 break;
748 default:
749 err = got_error(GOT_ERR_PRIVSEP_MSG);
750 break;
753 imsg_free(&imsg);
756 return err;
759 static const struct got_error *
760 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
762 const struct got_error *err;
763 struct imsg imsg;
765 *packfd = -1;
767 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
768 return got_error_from_errno("imsg_compose DONE");
770 err = gotd_imsg_flush(ibuf);
771 if (err)
772 return err;
774 while (*packfd == -1 && err == NULL) {
775 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
776 if (err)
777 break;
779 switch (imsg.hdr.type) {
780 case GOTD_IMSG_ERROR:
781 err = gotd_imsg_recv_error(NULL, &imsg);
782 break;
783 case GOTD_IMSG_PACKFILE_PIPE:
784 if (imsg.fd != -1)
785 *packfd = imsg.fd;
786 else
787 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
788 break;
789 default:
790 err = got_error(GOT_ERR_PRIVSEP_MSG);
791 break;
794 imsg_free(&imsg);
797 return err;
800 static const struct got_error *
801 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
803 const struct got_error *err = NULL;
804 int pack_starting = 0;
805 struct gotd_imsg_packfile_progress iprog;
806 char buf[GOT_PKT_MAX];
807 struct imsg imsg;
808 size_t datalen;
809 int p_deltify = 0, n;
810 const char *eol = "\r";
812 memset(&imsg, 0, sizeof(imsg));
814 while (!pack_starting && err == NULL) {
815 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
816 if (err)
817 break;
819 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
820 switch (imsg.hdr.type) {
821 case GOTD_IMSG_ERROR:
822 err = gotd_imsg_recv_error(NULL, &imsg);
823 break;
824 case GOTD_IMSG_PACKFILE_READY:
825 eol = "\n";
826 pack_starting = 1;
827 /* fallthrough */
828 case GOTD_IMSG_PACKFILE_PROGRESS:
829 if (datalen != sizeof(iprog)) {
830 err = got_error(GOT_ERR_PRIVSEP_LEN);
831 break;
833 memcpy(&iprog, imsg.data, sizeof(iprog));
834 if (iprog.nobj_total > 0) {
835 p_deltify = (iprog.nobj_deltify * 100) /
836 iprog.nobj_total;
838 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
839 n = snprintf(&buf[1], sizeof(buf) - 1,
840 "%d commits colored, "
841 "%d objects found, "
842 "deltify %d%%%s",
843 iprog.ncolored,
844 iprog.nfound,
845 p_deltify, eol);
846 if (n >= sizeof(buf) - 1)
847 break;
848 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
849 break;
850 default:
851 err = got_error(GOT_ERR_PRIVSEP_MSG);
852 break;
855 imsg_free(&imsg);
858 return err;
861 static const struct got_error *
862 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
863 int chattygot)
865 const struct got_error *err = NULL;
866 char buf[GOT_PKT_MAX];
867 struct imsgbuf ibuf;
868 enum protostate {
869 STATE_EXPECT_WANT,
870 STATE_EXPECT_MORE_WANT,
871 STATE_EXPECT_HAVE,
872 STATE_EXPECT_DONE,
873 STATE_DONE,
874 };
875 enum protostate curstate = STATE_EXPECT_WANT;
876 int have_ack = 0, use_sidebands = 0, seen_have = 0;
877 int packfd = -1;
878 size_t pack_chunksize;
880 imsg_init(&ibuf, gotd_sock);
882 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
883 if (err)
884 goto done;
886 while (curstate != STATE_DONE) {
887 int n;
888 buf[0] = '\0';
889 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
890 if (err)
891 goto done;
892 if (n == 0) {
893 if (curstate != STATE_EXPECT_WANT &&
894 curstate != STATE_EXPECT_MORE_WANT &&
895 curstate != STATE_EXPECT_HAVE &&
896 curstate != STATE_EXPECT_DONE) {
897 err = got_error_msg(GOT_ERR_BAD_PACKET,
898 "unexpected flush packet received");
899 goto done;
902 if (curstate == STATE_EXPECT_WANT) {
903 ssize_t r;
904 /*
905 * If the client does not want to fetch
906 * anything we should receive a flush
907 * packet followed by EOF.
908 */
909 r = read(infd, buf, sizeof(buf));
910 if (r == -1) {
911 err = got_error_from_errno("read");
912 goto done;
914 if (r == 0) /* EOF */
915 goto done;
917 /* Zero-length field followed by payload. */
918 err = got_error_msg(GOT_ERR_BAD_PACKET,
919 "unexpected flush packet received");
920 goto done;
923 if (curstate == STATE_EXPECT_WANT ||
924 curstate == STATE_EXPECT_MORE_WANT ||
925 curstate == STATE_EXPECT_HAVE) {
926 err = forward_flushpkt(&ibuf);
927 if (err)
928 goto done;
930 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
931 err = send_nak(outfd, chattygot);
932 if (err)
933 goto done;
935 if (curstate == STATE_EXPECT_MORE_WANT)
936 curstate = STATE_EXPECT_HAVE;
937 else
938 curstate = STATE_EXPECT_DONE;
939 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
940 if (curstate != STATE_EXPECT_WANT &&
941 curstate != STATE_EXPECT_MORE_WANT) {
942 err = got_error_msg(GOT_ERR_BAD_PACKET,
943 "unexpected 'want' packet");
944 goto done;
946 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
947 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
948 if (err)
949 goto done;
950 if (curstate == STATE_EXPECT_WANT)
951 curstate = STATE_EXPECT_MORE_WANT;
952 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
953 if (curstate != STATE_EXPECT_HAVE &&
954 curstate != STATE_EXPECT_DONE) {
955 err = got_error_msg(GOT_ERR_BAD_PACKET,
956 "unexpected 'have' packet");
957 goto done;
959 if (curstate == STATE_EXPECT_HAVE) {
960 err = recv_have(&have_ack, outfd, &ibuf,
961 buf, n, chattygot);
962 if (err)
963 goto done;
964 seen_have = 1;
965 if (have_ack)
966 curstate = STATE_EXPECT_DONE;
968 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
969 if (curstate != STATE_EXPECT_HAVE &&
970 curstate != STATE_EXPECT_DONE) {
971 err = got_error_msg(GOT_ERR_BAD_PACKET,
972 "unexpected 'done' packet");
973 goto done;
975 err = recv_done(&packfd, outfd, &ibuf, chattygot);
976 if (err)
977 goto done;
978 curstate = STATE_DONE;
979 break;
980 } else {
981 err = got_error(GOT_ERR_BAD_PACKET);
982 goto done;
986 if (!seen_have) {
987 err = send_nak(outfd, chattygot);
988 if (err)
989 goto done;
992 if (use_sidebands) {
993 err = relay_progress_reports(&ibuf, outfd, chattygot);
994 if (err)
995 goto done;
996 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
997 } else
998 pack_chunksize = sizeof(buf);
1000 for (;;) {
1001 ssize_t r;
1003 r = read(packfd, use_sidebands ? &buf[1] : buf,
1004 pack_chunksize);
1005 if (r == -1) {
1006 err = got_error_from_errno("read");
1007 break;
1008 } else if (r == 0) {
1009 err = got_pkt_flushpkt(outfd, chattygot);
1010 break;
1013 if (use_sidebands) {
1014 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1015 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1016 if (err)
1017 break;
1018 } else {
1019 err = got_poll_write_full(outfd, buf, r);
1020 if (err) {
1021 if (err->code == GOT_ERR_EOF)
1022 err = NULL;
1023 break;
1027 done:
1028 imsg_clear(&ibuf);
1029 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1030 err = got_error_from_errno("close");
1031 if (err)
1032 echo_error(err, outfd, chattygot);
1033 return err;
1036 static const struct got_error *
1037 parse_ref_update_line(char **common_capabilities, char **refname,
1038 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1040 const struct got_error *err;
1041 char *old_id_str = NULL, *new_id_str = NULL;
1042 char *client_capabilities = NULL;
1044 *refname = NULL;
1046 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1047 refname, &client_capabilities, buf, len);
1048 if (err)
1049 return err;
1051 if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
1052 !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
1053 err = got_error_msg(GOT_ERR_BAD_PACKET,
1054 "ref-update with bad object ID");
1055 goto done;
1057 if (!got_ref_name_is_valid(*refname)) {
1058 err = got_error_msg(GOT_ERR_BAD_PACKET,
1059 "ref-update with bad reference name");
1060 goto done;
1063 if (client_capabilities) {
1064 err = got_gitproto_match_capabilities(common_capabilities,
1065 NULL, client_capabilities, write_capabilities,
1066 nitems(write_capabilities));
1067 if (err)
1068 goto done;
1070 done:
1071 free(old_id_str);
1072 free(new_id_str);
1073 free(client_capabilities);
1074 if (err) {
1075 free(*refname);
1076 *refname = NULL;
1078 return err;
1081 static const struct got_error *
1082 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1083 char *buf, size_t len, int expect_capabilities, int chattygot)
1085 const struct got_error *err;
1086 struct gotd_imsg_ref_update iref;
1087 struct ibuf *wbuf;
1088 char *capabilities_str = NULL, *refname = NULL;
1089 int done = 0;
1090 struct imsg imsg;
1092 memset(&iref, 0, sizeof(iref));
1093 memset(&imsg, 0, sizeof(imsg));
1095 err = parse_ref_update_line(&capabilities_str, &refname,
1096 iref.old_id, iref.new_id, buf, len);
1097 if (err)
1098 return err;
1100 if (capabilities_str) {
1101 if (!expect_capabilities) {
1102 err = got_error_msg(GOT_ERR_BAD_PACKET,
1103 "unexpected capability announcement received");
1104 goto done;
1106 err = send_capabilities(NULL, report_status, capabilities_str,
1107 ibuf);
1108 if (err)
1109 goto done;
1112 iref.name_len = strlen(refname);
1113 len = sizeof(iref) + iref.name_len;
1114 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1115 if (wbuf == NULL) {
1116 err = got_error_from_errno("imsg_create REF_UPDATE");
1117 goto done;
1120 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1121 return got_error_from_errno("imsg_add REF_UPDATE");
1122 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1123 return got_error_from_errno("imsg_add REF_UPDATE");
1124 wbuf->fd = -1;
1125 imsg_close(ibuf, wbuf);
1127 err = gotd_imsg_flush(ibuf);
1128 if (err)
1129 goto done;
1131 /* Wait for ACK or an error. */
1132 while (!done && err == NULL) {
1133 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1134 if (err)
1135 break;
1136 switch (imsg.hdr.type) {
1137 case GOTD_IMSG_ERROR:
1138 err = gotd_imsg_recv_error(NULL, &imsg);
1139 break;
1140 case GOTD_IMSG_ACK:
1141 err = recv_ack(&imsg, iref.new_id);
1142 if (err)
1143 break;
1144 done = 1;
1145 break;
1146 default:
1147 err = got_error(GOT_ERR_PRIVSEP_MSG);
1148 break;
1151 imsg_free(&imsg);
1153 done:
1154 free(capabilities_str);
1155 free(refname);
1156 return err;
1159 static const struct got_error *
1160 recv_packfile(struct imsg *imsg, int infd)
1162 const struct got_error *err = NULL;
1163 size_t datalen;
1164 int packfd;
1165 char buf[GOT_PKT_MAX];
1166 int pack_done = 0;
1168 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1169 if (datalen != 0)
1170 return got_error(GOT_ERR_PRIVSEP_MSG);
1172 if (imsg->fd == -1)
1173 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1175 packfd = imsg->fd;
1176 while (!pack_done) {
1177 ssize_t r = 0;
1179 err = got_poll_fd(infd, POLLIN, 1);
1180 if (err) {
1181 if (err->code != GOT_ERR_TIMEOUT)
1182 break;
1183 err = NULL;
1184 } else {
1185 r = read(infd, buf, sizeof(buf));
1186 if (r == -1) {
1187 err = got_error_from_errno("read");
1188 break;
1190 if (r == 0) {
1192 * Git clients hang up their side of the
1193 * connection after sending the pack file.
1195 err = NULL;
1196 pack_done = 1;
1197 break;
1201 if (r == 0) {
1202 /* Detect gotd(8) closing the pack pipe when done. */
1203 err = got_poll_fd(packfd, POLLOUT, 1);
1204 if (err) {
1205 if (err->code != GOT_ERR_EOF)
1206 break;
1207 err = NULL;
1208 pack_done = 1;
1210 } else {
1211 /* Write pack data and/or detect pipe being closed. */
1212 err = got_poll_write_full(packfd, buf, r);
1213 if (err) {
1214 if (err->code == GOT_ERR_EOF)
1215 err = NULL;
1216 break;
1221 close(packfd);
1222 return err;
1225 static const struct got_error *
1226 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1228 const struct got_error *err = NULL;
1229 struct gotd_imsg_packfile_status istatus;
1230 char buf[GOT_PKT_MAX];
1231 size_t datalen, len;
1232 char *reason = NULL;
1234 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1235 if (datalen < sizeof(istatus))
1236 return got_error(GOT_ERR_PRIVSEP_LEN);
1237 memcpy(&istatus, imsg->data, sizeof(istatus));
1238 if (datalen != sizeof(istatus) + istatus.reason_len)
1239 return got_error(GOT_ERR_PRIVSEP_LEN);
1241 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1242 if (reason == NULL) {
1243 err = got_error_from_errno("strndup");
1244 goto done;
1247 if (err == NULL)
1248 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1249 else
1250 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1251 if (len >= sizeof(buf)) {
1252 err = got_error(GOT_ERR_NO_SPACE);
1253 goto done;
1256 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1257 done:
1258 free(reason);
1259 return err;
1262 static const struct got_error *
1263 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1265 const struct got_error *err = NULL;
1266 struct gotd_imsg_ref_update_ok iok;
1267 size_t datalen, len;
1268 char buf[GOT_PKT_MAX];
1269 char *refname = NULL;
1271 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1272 if (datalen < sizeof(iok))
1273 return got_error(GOT_ERR_PRIVSEP_LEN);
1274 memcpy(&iok, imsg->data, sizeof(iok));
1275 if (datalen != sizeof(iok) + iok.name_len)
1276 return got_error(GOT_ERR_PRIVSEP_LEN);
1278 memcpy(&iok, imsg->data, sizeof(iok));
1280 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1281 if (refname == NULL)
1282 return got_error_from_errno("strndup");
1284 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1285 if (len >= sizeof(buf)) {
1286 err = got_error(GOT_ERR_NO_SPACE);
1287 goto done;
1290 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1291 done:
1292 free(refname);
1293 return err;
1296 static const struct got_error *
1297 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1299 const struct got_error *err = NULL;
1300 struct gotd_imsg_ref_update_ng ing;
1301 size_t datalen, len;
1302 char buf[GOT_PKT_MAX];
1303 char *refname = NULL, *reason = NULL;
1305 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1306 if (datalen < sizeof(ing))
1307 return got_error(GOT_ERR_PRIVSEP_LEN);
1308 memcpy(&ing, imsg->data, sizeof(ing));
1309 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1310 return got_error(GOT_ERR_PRIVSEP_LEN);
1312 memcpy(&ing, imsg->data, sizeof(ing));
1314 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1315 if (refname == NULL)
1316 return got_error_from_errno("strndup");
1318 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1319 ing.reason_len);
1320 if (reason == NULL) {
1321 err = got_error_from_errno("strndup");
1322 goto done;
1325 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1326 if (len >= sizeof(buf)) {
1327 err = got_error(GOT_ERR_NO_SPACE);
1328 goto done;
1331 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1332 done:
1333 free(refname);
1334 free(reason);
1335 return err;
1338 static const struct got_error *
1339 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1340 int chattygot)
1342 const struct got_error *err = NULL;
1343 char buf[GOT_PKT_MAX];
1344 struct imsgbuf ibuf;
1345 enum protostate {
1346 STATE_EXPECT_REF_UPDATE,
1347 STATE_EXPECT_MORE_REF_UPDATES,
1348 STATE_EXPECT_PACKFILE,
1349 STATE_PACKFILE_RECEIVED,
1350 STATE_REFS_UPDATED,
1352 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1353 struct imsg imsg;
1354 int report_status = 0;
1356 imsg_init(&ibuf, gotd_sock);
1357 memset(&imsg, 0, sizeof(imsg));
1359 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1360 if (err)
1361 goto done;
1363 while (curstate != STATE_EXPECT_PACKFILE) {
1364 int n;
1365 buf[0] = '\0';
1366 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1367 if (err)
1368 goto done;
1369 if (n == 0) {
1370 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1371 err = got_error_msg(GOT_ERR_BAD_PACKET,
1372 "unexpected flush packet received");
1373 goto done;
1375 err = forward_flushpkt(&ibuf);
1376 if (err)
1377 goto done;
1378 curstate = STATE_EXPECT_PACKFILE;
1379 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1380 if (curstate != STATE_EXPECT_REF_UPDATE &&
1381 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1382 err = got_error_msg(GOT_ERR_BAD_PACKET,
1383 "unexpected ref-update packet");
1384 goto done;
1386 if (curstate == STATE_EXPECT_REF_UPDATE) {
1387 err = recv_ref_update(&report_status,
1388 outfd, &ibuf, buf, n, 1, chattygot);
1389 } else {
1390 err = recv_ref_update(NULL, outfd, &ibuf,
1391 buf, n, 0, chattygot);
1393 if (err)
1394 goto done;
1395 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1396 } else {
1397 err = got_error(GOT_ERR_BAD_PACKET);
1398 goto done;
1402 while (curstate != STATE_PACKFILE_RECEIVED) {
1403 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1404 if (err)
1405 goto done;
1406 switch (imsg.hdr.type) {
1407 case GOTD_IMSG_ERROR:
1408 err = gotd_imsg_recv_error(NULL, &imsg);
1409 goto done;
1410 case GOTD_IMSG_RECV_PACKFILE:
1411 err = recv_packfile(&imsg, infd);
1412 if (err) {
1413 if (err->code != GOT_ERR_EOF)
1414 goto done;
1416 * EOF is reported when the client hangs up,
1417 * which can happen with Git clients.
1418 * The socket should stay half-open so we
1419 * can still send our reports if requested.
1421 err = NULL;
1423 curstate = STATE_PACKFILE_RECEIVED;
1424 break;
1425 default:
1426 err = got_error(GOT_ERR_PRIVSEP_MSG);
1427 break;
1430 imsg_free(&imsg);
1431 if (err)
1432 goto done;
1435 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1436 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1437 if (err)
1438 break;
1439 switch (imsg.hdr.type) {
1440 case GOTD_IMSG_ERROR:
1441 err = gotd_imsg_recv_error(NULL, &imsg);
1442 break;
1443 case GOTD_IMSG_PACKFILE_STATUS:
1444 if (!report_status)
1445 break;
1446 err = report_unpack_status(&imsg, outfd, chattygot);
1447 break;
1448 case GOTD_IMSG_REF_UPDATE_OK:
1449 if (!report_status)
1450 break;
1451 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1452 break;
1453 case GOTD_IMSG_REF_UPDATE_NG:
1454 if (!report_status)
1455 break;
1456 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1457 break;
1458 case GOTD_IMSG_REFS_UPDATED:
1459 curstate = STATE_REFS_UPDATED;
1460 err = got_pkt_flushpkt(outfd, chattygot);
1461 break;
1462 default:
1463 err = got_error(GOT_ERR_PRIVSEP_MSG);
1464 break;
1467 imsg_free(&imsg);
1469 done:
1470 imsg_clear(&ibuf);
1471 if (err)
1472 echo_error(err, outfd, chattygot);
1473 return err;
1476 const struct got_error *
1477 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1478 int gotd_sock, int chattygot)
1480 const struct got_error *err = NULL;
1482 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1483 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1484 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1485 err = serve_write(infd, outfd, gotd_sock, repo_path,
1486 chattygot);
1487 else
1488 err = got_error(GOT_ERR_BAD_PACKET);
1490 return err;