Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <errno.h>
22 #include <event.h>
23 #include <poll.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <imsg.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_serve.h"
35 #include "got_path.h"
36 #include "got_version.h"
37 #include "got_reference.h"
39 #include "got_lib_pkt.h"
40 #include "got_lib_dial.h"
41 #include "got_lib_gitproto.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_poll.h"
45 #include "gotd.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
51 static const struct got_capability read_capabilities[] = {
52 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
53 { GOT_CAPA_OFS_DELTA, NULL },
54 { GOT_CAPA_SIDE_BAND_64K, NULL },
55 };
57 static const struct got_capability write_capabilities[] = {
58 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
59 { GOT_CAPA_OFS_DELTA, NULL },
60 { GOT_CAPA_REPORT_STATUS, NULL },
61 { GOT_CAPA_NO_THIN, NULL },
62 #if 0
63 { GOT_CAPA_DELETE_REFS, NULL },
64 #endif
65 };
67 static const struct got_error *
68 parse_command(char **command, char **repo_path, const char *gitcmd)
69 {
70 const struct got_error *err = NULL;
71 size_t len, cmdlen, pathlen;
72 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
73 const char *relpath;
75 *command = NULL;
76 *repo_path = NULL;
78 len = strlen(gitcmd);
80 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
81 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
82 strlen(GOT_SERVE_CMD_SEND)) == 0)
83 cmdlen = strlen(GOT_SERVE_CMD_SEND);
84 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
85 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
86 strlen(GOT_SERVE_CMD_FETCH)) == 0)
87 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
88 else
89 return got_error(GOT_ERR_BAD_PACKET);
91 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
92 return got_error(GOT_ERR_BAD_PACKET);
94 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
95 return got_error(GOT_ERR_BAD_PATH);
97 /* Forbid linefeeds in paths, like Git does. */
98 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
99 return got_error(GOT_ERR_BAD_PATH);
101 path0 = strdup(&gitcmd[cmdlen + 1]);
102 if (path0 == NULL)
103 return got_error_from_errno("strdup");
104 path = path0;
105 pathlen = strlen(path);
107 /*
108 * Git clients send a shell command.
109 * Trim spaces and quotes around the path.
110 */
111 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
112 path++;
113 pathlen--;
115 while (pathlen > 0 &&
116 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
117 path[pathlen - 1] == ' ')) {
118 path[pathlen - 1] = '\0';
119 pathlen--;
122 /* Deny an empty repository path. */
123 if (path[0] == '\0' || got_path_is_root_dir(path)) {
124 err = got_error(GOT_ERR_NOT_GIT_REPO);
125 goto done;
128 if (asprintf(&abspath, "/%s", path) == -1) {
129 err = got_error_from_errno("asprintf");
130 goto done;
132 pathlen = strlen(abspath);
133 canonpath = malloc(pathlen);
134 if (canonpath == NULL) {
135 err = got_error_from_errno("malloc");
136 goto done;
138 err = got_canonpath(abspath, canonpath, pathlen);
139 if (err)
140 goto done;
142 relpath = canonpath;
143 while (relpath[0] == '/')
144 relpath++;
145 *repo_path = strdup(relpath);
146 if (*repo_path == NULL) {
147 err = got_error_from_errno("strdup");
148 goto done;
150 *command = strndup(gitcmd, cmdlen);
151 if (*command == NULL)
152 err = got_error_from_errno("strndup");
153 done:
154 free(path0);
155 free(abspath);
156 free(canonpath);
157 if (err) {
158 free(*repo_path);
159 *repo_path = NULL;
161 return err;
164 static const struct got_error *
165 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
166 uint8_t *buf, size_t bufsize)
168 struct got_capability capa[nitems(read_capabilities) + 1];
169 size_t ncapa;
171 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
172 if (symrefstr) {
173 capa[nitems(read_capabilities)].key = "symref";
174 capa[nitems(read_capabilities)].value = symrefstr;
175 ncapa = nitems(capa);
176 } else
177 ncapa = nitems(read_capabilities);
179 return got_gitproto_append_capabilities(capalen, buf, len,
180 bufsize, capa, ncapa);
183 static const struct got_error *
184 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
185 int client_is_reading, const char *symrefstr, int chattygot)
187 const struct got_error *err = NULL;
188 char hex[SHA1_DIGEST_STRING_LENGTH];
189 char buf[GOT_PKT_MAX];
190 size_t len, capalen = 0;
192 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
193 return got_error(GOT_ERR_BAD_OBJ_ID);
195 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
196 if (len >= sizeof(buf))
197 return got_error(GOT_ERR_NO_SPACE);
199 if (send_capabilities) {
200 if (client_is_reading) {
201 err = append_read_capabilities(&capalen, len,
202 symrefstr, buf, sizeof(buf));
203 } else {
204 err = got_gitproto_append_capabilities(&capalen,
205 buf, len, sizeof(buf), write_capabilities,
206 nitems(write_capabilities));
208 if (err)
209 return err;
210 len += capalen;
213 if (len + 1 >= sizeof(buf))
214 return got_error(GOT_ERR_NO_SPACE);
215 buf[len] = '\n';
216 len++;
217 buf[len] = '\0';
219 return got_pkt_writepkt(outfd, buf, len, chattygot);
222 static const struct got_error *
223 send_zero_refs(int outfd, int client_is_reading, int chattygot)
225 const struct got_error *err = NULL;
226 char buf[GOT_PKT_MAX];
227 uint8_t zero[SHA1_DIGEST_LENGTH];
228 char hex[SHA1_DIGEST_STRING_LENGTH];
229 size_t len, capalen = 0;
231 memset(&zero, 0, sizeof(zero));
233 if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
234 return got_error(GOT_ERR_BAD_OBJ_ID);
236 len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
237 if (len >= sizeof(buf))
238 return got_error(GOT_ERR_NO_SPACE);
240 if (client_is_reading) {
241 err = got_gitproto_append_capabilities(&capalen, buf, len,
242 sizeof(buf), read_capabilities, nitems(read_capabilities));
243 if (err)
244 return err;
245 } else {
246 err = got_gitproto_append_capabilities(&capalen, buf, len,
247 sizeof(buf), write_capabilities,
248 nitems(write_capabilities));
249 if (err)
250 return err;
253 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
256 static void
257 echo_error(const struct got_error *err, int outfd, int chattygot)
259 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
260 size_t len;
262 /*
263 * Echo the error to the client on a pkt-line.
264 * The client should then terminate its session.
265 */
266 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
267 len = strlcat(buf, err->msg, sizeof(buf));
268 err = got_pkt_writepkt(outfd, buf, len, chattygot);
269 abort();
272 static const struct got_error *
273 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
274 const char *repo_path, int chattygot)
276 const struct got_error *err = NULL;
277 struct imsg imsg;
278 size_t datalen;
279 struct gotd_imsg_list_refs lsref;
280 struct gotd_imsg_reflist ireflist;
281 struct gotd_imsg_ref iref;
282 struct gotd_imsg_symref isymref;
283 size_t nrefs = 0;
284 int have_nrefs = 0, sent_capabilities = 0;
285 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
286 char *refname = NULL;
288 memset(&imsg, 0, sizeof(imsg));
289 memset(&lsref, 0, sizeof(lsref));
291 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
292 sizeof(lsref.repo_name))
293 return got_error(GOT_ERR_NO_SPACE);
294 lsref.client_is_reading = client_is_reading;
296 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
297 &lsref, sizeof(lsref)) == -1)
298 return got_error_from_errno("imsg_compose LIST_REFS");
300 err = gotd_imsg_flush(ibuf);
301 if (err)
302 return err;
304 while (!have_nrefs || nrefs > 0) {
305 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
306 if (err)
307 goto done;
308 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
309 switch (imsg.hdr.type) {
310 case GOTD_IMSG_ERROR:
311 err = gotd_imsg_recv_error(NULL, &imsg);
312 goto done;
313 case GOTD_IMSG_REFLIST:
314 if (have_nrefs || nrefs > 0) {
315 err = got_error(GOT_ERR_PRIVSEP_MSG);
316 goto done;
318 if (datalen != sizeof(ireflist)) {
319 err = got_error(GOT_ERR_PRIVSEP_MSG);
320 goto done;
322 memcpy(&ireflist, imsg.data, sizeof(ireflist));
323 nrefs = ireflist.nrefs;
324 have_nrefs = 1;
325 if (nrefs == 0)
326 err = send_zero_refs(outfd, client_is_reading,
327 chattygot);
328 break;
329 case GOTD_IMSG_REF:
330 if (!have_nrefs || nrefs == 0) {
331 err = got_error(GOT_ERR_PRIVSEP_MSG);
332 goto done;
334 if (datalen < sizeof(iref)) {
335 err = got_error(GOT_ERR_PRIVSEP_MSG);
336 goto done;
338 memcpy(&iref, imsg.data, sizeof(iref));
339 if (datalen != sizeof(iref) + iref.name_len) {
340 err = got_error(GOT_ERR_PRIVSEP_LEN);
341 goto done;
343 refname = malloc(iref.name_len + 1);
344 if (refname == NULL) {
345 err = got_error_from_errno("malloc");
346 goto done;
348 memcpy(refname, imsg.data + sizeof(iref),
349 iref.name_len);
350 refname[iref.name_len] = '\0';
351 err = send_ref(outfd, iref.id, refname,
352 !sent_capabilities, client_is_reading,
353 NULL, chattygot);
354 free(refname);
355 refname = NULL;
356 if (err)
357 goto done;
358 sent_capabilities = 1;
359 if (nrefs > 0)
360 nrefs--;
361 break;
362 case GOTD_IMSG_SYMREF:
363 if (!have_nrefs || nrefs == 0) {
364 err = got_error(GOT_ERR_PRIVSEP_MSG);
365 goto done;
367 if (datalen < sizeof(isymref)) {
368 err = got_error(GOT_ERR_PRIVSEP_LEN);
369 goto done;
371 memcpy(&isymref, imsg.data, sizeof(isymref));
372 if (datalen != sizeof(isymref) + isymref.name_len +
373 isymref.target_len) {
374 err = got_error(GOT_ERR_PRIVSEP_LEN);
375 goto done;
378 /*
379 * For now, we only announce one symbolic ref,
380 * as part of our capability advertisement.
381 */
382 if (sent_capabilities || symrefstr != NULL ||
383 symrefname != NULL || symreftarget != NULL)
384 break;
386 symrefname = malloc(isymref.name_len + 1);
387 if (symrefname == NULL) {
388 err = got_error_from_errno("malloc");
389 goto done;
391 memcpy(symrefname, imsg.data + sizeof(isymref),
392 isymref.name_len);
393 symrefname[isymref.name_len] = '\0';
395 symreftarget = malloc(isymref.target_len + 1);
396 if (symreftarget == NULL) {
397 err = got_error_from_errno("malloc");
398 goto done;
400 memcpy(symreftarget,
401 imsg.data + sizeof(isymref) + isymref.name_len,
402 isymref.target_len);
403 symreftarget[isymref.target_len] = '\0';
405 if (asprintf(&symrefstr, "%s:%s", symrefname,
406 symreftarget) == -1) {
407 err = got_error_from_errno("asprintf");
408 goto done;
410 err = send_ref(outfd, isymref.target_id, symrefname,
411 !sent_capabilities, client_is_reading, symrefstr,
412 chattygot);
413 free(refname);
414 refname = NULL;
415 if (err)
416 goto done;
417 sent_capabilities = 1;
418 if (nrefs > 0)
419 nrefs--;
420 break;
421 default:
422 err = got_error(GOT_ERR_PRIVSEP_MSG);
423 break;
426 imsg_free(&imsg);
429 err = got_pkt_flushpkt(outfd, chattygot);
430 if (err)
431 goto done;
432 done:
433 free(symrefstr);
434 free(symrefname);
435 free(symreftarget);
436 return err;
439 static const struct got_error *
440 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
442 const struct got_error *err;
443 char *id_str = NULL, *client_capabilities = NULL;
445 err = got_gitproto_parse_want_line(&id_str,
446 &client_capabilities, buf, len);
447 if (err)
448 return err;
450 if (!got_parse_sha1_digest(id, id_str)) {
451 err = got_error_msg(GOT_ERR_BAD_PACKET,
452 "want-line with bad object ID");
453 goto done;
456 if (client_capabilities) {
457 err = got_gitproto_match_capabilities(common_capabilities,
458 NULL, client_capabilities, read_capabilities,
459 nitems(read_capabilities));
460 if (err)
461 goto done;
463 done:
464 free(id_str);
465 free(client_capabilities);
466 return err;
469 static const struct got_error *
470 parse_have_line(uint8_t *id, char *buf, size_t len)
472 const struct got_error *err;
473 char *id_str = NULL;
475 err = got_gitproto_parse_have_line(&id_str, buf, len);
476 if (err)
477 return err;
479 if (!got_parse_sha1_digest(id, id_str)) {
480 err = got_error_msg(GOT_ERR_BAD_PACKET,
481 "have-line with bad object ID");
482 goto done;
484 done:
485 free(id_str);
486 return err;
489 static const struct got_error *
490 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
492 const struct got_error *err = NULL;
493 struct gotd_imsg_capability icapa;
494 size_t len;
495 struct ibuf *wbuf;
497 memset(&icapa, 0, sizeof(icapa));
499 icapa.key_len = strlen(capa->key);
500 len = sizeof(icapa) + icapa.key_len;
501 if (capa->value) {
502 icapa.value_len = strlen(capa->value);
503 len += icapa.value_len;
506 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
507 if (wbuf == NULL) {
508 err = got_error_from_errno("imsg_create CAPABILITY");
509 return err;
512 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
513 return got_error_from_errno("imsg_add CAPABILITY");
514 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
515 return got_error_from_errno("imsg_add CAPABILITY");
516 if (capa->value) {
517 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
518 return got_error_from_errno("imsg_add CAPABILITY");
521 wbuf->fd = -1;
522 imsg_close(ibuf, wbuf);
524 return NULL;
527 static const struct got_error *
528 send_capabilities(int *use_sidebands, int *report_status,
529 char *capabilities_str, struct imsgbuf *ibuf)
531 const struct got_error *err = NULL;
532 struct gotd_imsg_capabilities icapas;
533 struct got_capability *capa = NULL;
534 size_t ncapa, i;
536 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
537 capabilities_str);
538 if (err)
539 return err;
541 icapas.ncapabilities = ncapa;
542 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
543 &icapas, sizeof(icapas)) == -1) {
544 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
545 goto done;
548 for (i = 0; i < ncapa; i++) {
549 err = send_capability(&capa[i], ibuf);
550 if (err)
551 goto done;
552 if (use_sidebands &&
553 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
554 *use_sidebands = 1;
555 if (report_status &&
556 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
557 *report_status = 1;
559 done:
560 free(capa);
561 return err;
564 static const struct got_error *
565 forward_flushpkt(struct imsgbuf *ibuf)
567 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
568 return got_error_from_errno("imsg_compose FLUSH");
570 return gotd_imsg_flush(ibuf);
573 static const struct got_error *
574 recv_ack(struct imsg *imsg, uint8_t *expected_id)
576 struct gotd_imsg_ack iack;
577 size_t datalen;
579 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
580 if (datalen != sizeof(iack))
581 return got_error(GOT_ERR_PRIVSEP_LEN);
583 memcpy(&iack, imsg->data, sizeof(iack));
584 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
585 return got_error(GOT_ERR_BAD_OBJ_ID);
587 return NULL;
590 static const struct got_error *
591 recv_nak(struct imsg *imsg, uint8_t *expected_id)
593 struct gotd_imsg_ack inak;
594 size_t datalen;
596 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
597 if (datalen != sizeof(inak))
598 return got_error(GOT_ERR_PRIVSEP_LEN);
600 memcpy(&inak, imsg->data, sizeof(inak));
601 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
602 return got_error(GOT_ERR_BAD_OBJ_ID);
604 return NULL;
608 static const struct got_error *
609 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
610 char *buf, size_t len, int expect_capabilities, int chattygot)
612 const struct got_error *err;
613 struct gotd_imsg_want iwant;
614 char *capabilities_str;
615 int done = 0;
616 struct imsg imsg;
618 memset(&iwant, 0, sizeof(iwant));
619 memset(&imsg, 0, sizeof(imsg));
621 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
622 if (err)
623 return err;
625 if (capabilities_str) {
626 if (!expect_capabilities) {
627 err = got_error_msg(GOT_ERR_BAD_PACKET,
628 "unexpected capability announcement received");
629 goto done;
631 err = send_capabilities(use_sidebands, NULL, capabilities_str,
632 ibuf);
633 if (err)
634 goto done;
638 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
639 &iwant, sizeof(iwant)) == -1) {
640 err = got_error_from_errno("imsg_compose WANT");
641 goto done;
644 err = gotd_imsg_flush(ibuf);
645 if (err)
646 goto done;
648 /*
649 * Wait for an ACK, or an error in case the desired object
650 * does not exist.
651 */
652 while (!done && err == NULL) {
653 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
654 if (err)
655 break;
656 switch (imsg.hdr.type) {
657 case GOTD_IMSG_ERROR:
658 err = gotd_imsg_recv_error(NULL, &imsg);
659 break;
660 case GOTD_IMSG_ACK:
661 err = recv_ack(&imsg, iwant.object_id);
662 if (err)
663 break;
664 done = 1;
665 break;
666 default:
667 err = got_error(GOT_ERR_PRIVSEP_MSG);
668 break;
671 imsg_free(&imsg);
673 done:
674 free(capabilities_str);
675 return err;
678 static const struct got_error *
679 send_ack(int outfd, uint8_t *id, int chattygot)
681 char hex[SHA1_DIGEST_STRING_LENGTH];
682 char buf[GOT_PKT_MAX];
683 int len;
685 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
686 return got_error(GOT_ERR_BAD_OBJ_ID);
688 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
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 send_nak(int outfd, int chattygot)
698 char buf[5];
699 int len;
701 len = snprintf(buf, sizeof(buf), "NAK\n");
702 if (len >= sizeof(buf))
703 return got_error(GOT_ERR_NO_SPACE);
705 return got_pkt_writepkt(outfd, buf, len, chattygot);
708 static const struct got_error *
709 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
710 size_t len, int chattygot)
712 const struct got_error *err;
713 struct gotd_imsg_have ihave;
714 int done = 0;
715 struct imsg imsg;
717 memset(&ihave, 0, sizeof(ihave));
718 memset(&imsg, 0, sizeof(imsg));
720 err = parse_have_line(ihave.object_id, buf, len);
721 if (err)
722 return err;
724 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
725 &ihave, sizeof(ihave)) == -1)
726 return got_error_from_errno("imsg_compose HAVE");
728 err = gotd_imsg_flush(ibuf);
729 if (err)
730 return err;
732 /*
733 * Wait for an ACK or a NAK, indicating whether a common
734 * commit object has been found.
735 */
736 while (!done && err == NULL) {
737 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
738 if (err)
739 return err;
740 switch (imsg.hdr.type) {
741 case GOTD_IMSG_ERROR:
742 err = gotd_imsg_recv_error(NULL, &imsg);
743 break;
744 case GOTD_IMSG_ACK:
745 err = recv_ack(&imsg, ihave.object_id);
746 if (err)
747 break;
748 if (!*have_ack) {
749 err = send_ack(outfd, ihave.object_id,
750 chattygot);
751 if (err)
752 return err;
753 *have_ack = 1;
755 done = 1;
756 break;
757 case GOTD_IMSG_NAK:
758 err = recv_nak(&imsg, ihave.object_id);
759 if (err)
760 break;
761 done = 1;
762 break;
763 default:
764 err = got_error(GOT_ERR_PRIVSEP_MSG);
765 break;
768 imsg_free(&imsg);
771 return err;
774 static const struct got_error *
775 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
777 const struct got_error *err;
778 struct imsg imsg;
780 *packfd = -1;
782 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
783 return got_error_from_errno("imsg_compose DONE");
785 err = gotd_imsg_flush(ibuf);
786 if (err)
787 return err;
789 while (*packfd == -1 && err == NULL) {
790 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
791 if (err)
792 break;
794 switch (imsg.hdr.type) {
795 case GOTD_IMSG_ERROR:
796 err = gotd_imsg_recv_error(NULL, &imsg);
797 break;
798 case GOTD_IMSG_PACKFILE_PIPE:
799 if (imsg.fd != -1)
800 *packfd = imsg.fd;
801 else
802 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
803 break;
804 default:
805 err = got_error(GOT_ERR_PRIVSEP_MSG);
806 break;
809 imsg_free(&imsg);
812 return err;
815 static const struct got_error *
816 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
818 const struct got_error *err = NULL;
819 int pack_starting = 0;
820 struct gotd_imsg_packfile_progress iprog;
821 char buf[GOT_PKT_MAX];
822 struct imsg imsg;
823 size_t datalen;
824 int p_deltify = 0, n;
825 const char *eol = "\r";
827 memset(&imsg, 0, sizeof(imsg));
829 while (!pack_starting && err == NULL) {
830 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
831 if (err)
832 break;
834 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
835 switch (imsg.hdr.type) {
836 case GOTD_IMSG_ERROR:
837 err = gotd_imsg_recv_error(NULL, &imsg);
838 break;
839 case GOTD_IMSG_PACKFILE_READY:
840 eol = "\n";
841 pack_starting = 1;
842 /* fallthrough */
843 case GOTD_IMSG_PACKFILE_PROGRESS:
844 if (datalen != sizeof(iprog)) {
845 err = got_error(GOT_ERR_PRIVSEP_LEN);
846 break;
848 memcpy(&iprog, imsg.data, sizeof(iprog));
849 if (iprog.nobj_total > 0) {
850 p_deltify = (iprog.nobj_deltify * 100) /
851 iprog.nobj_total;
853 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
854 n = snprintf(&buf[1], sizeof(buf) - 1,
855 "%d commits colored, "
856 "%d objects found, "
857 "deltify %d%%%s",
858 iprog.ncolored,
859 iprog.nfound,
860 p_deltify, eol);
861 if (n >= sizeof(buf) - 1)
862 break;
863 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
864 break;
865 default:
866 err = got_error(GOT_ERR_PRIVSEP_MSG);
867 break;
870 imsg_free(&imsg);
873 return err;
876 static const struct got_error *
877 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
878 int chattygot)
880 const struct got_error *err = NULL;
881 char buf[GOT_PKT_MAX];
882 struct imsgbuf ibuf;
883 enum protostate {
884 STATE_EXPECT_WANT,
885 STATE_EXPECT_MORE_WANT,
886 STATE_EXPECT_HAVE,
887 STATE_EXPECT_DONE,
888 STATE_DONE,
889 };
890 enum protostate curstate = STATE_EXPECT_WANT;
891 int have_ack = 0, use_sidebands = 0, seen_have = 0;
892 int packfd = -1;
893 size_t pack_chunksize;
895 imsg_init(&ibuf, gotd_sock);
897 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
898 if (err)
899 goto done;
901 while (curstate != STATE_DONE) {
902 int n;
903 buf[0] = '\0';
904 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
905 if (err)
906 break;
907 if (n == 0) {
908 if (curstate != STATE_EXPECT_MORE_WANT &&
909 curstate != STATE_EXPECT_HAVE) {
910 err = got_error_msg(GOT_ERR_BAD_PACKET,
911 "unexpected flush packet received");
912 goto done;
914 err = forward_flushpkt(&ibuf);
915 if (err)
916 goto done;
917 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
918 err = send_nak(outfd, chattygot);
919 if (err)
920 goto done;
922 if (curstate == STATE_EXPECT_MORE_WANT)
923 curstate = STATE_EXPECT_HAVE;
924 else
925 curstate = STATE_EXPECT_DONE;
926 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
927 if (curstate != STATE_EXPECT_WANT &&
928 curstate != STATE_EXPECT_MORE_WANT) {
929 err = got_error_msg(GOT_ERR_BAD_PACKET,
930 "unexpected 'want' packet");
931 goto done;
933 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
934 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
935 if (err)
936 goto done;
937 if (curstate == STATE_EXPECT_WANT)
938 curstate = STATE_EXPECT_MORE_WANT;
939 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
940 if (curstate != STATE_EXPECT_HAVE) {
941 err = got_error_msg(GOT_ERR_BAD_PACKET,
942 "unexpected 'have' packet");
943 goto done;
945 err = recv_have(&have_ack, outfd, &ibuf, buf, n,
946 chattygot);
947 if (err)
948 goto done;
949 seen_have = 1;
950 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
951 if (curstate != STATE_EXPECT_HAVE &&
952 curstate != STATE_EXPECT_DONE) {
953 err = got_error_msg(GOT_ERR_BAD_PACKET,
954 "unexpected 'done' packet");
955 goto done;
957 err = recv_done(&packfd, outfd, &ibuf, chattygot);
958 if (err)
959 goto done;
960 curstate = STATE_DONE;
961 break;
962 } else {
963 err = got_error(GOT_ERR_BAD_PACKET);
964 goto done;
968 if (!seen_have) {
969 err = send_nak(outfd, chattygot);
970 if (err)
971 goto done;
974 if (use_sidebands) {
975 err = relay_progress_reports(&ibuf, outfd, chattygot);
976 if (err)
977 goto done;
978 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
979 } else
980 pack_chunksize = sizeof(buf);
982 for (;;) {
983 ssize_t r;
985 r = read(packfd, use_sidebands ? &buf[1] : buf,
986 pack_chunksize);
987 if (r == -1) {
988 err = got_error_from_errno("read");
989 break;
990 } else if (r == 0) {
991 err = got_pkt_flushpkt(outfd, chattygot);
992 break;
995 if (use_sidebands) {
996 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
997 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
998 if (err)
999 break;
1000 } else {
1001 err = got_poll_write_full(outfd, buf, r);
1002 if (err) {
1003 if (err->code == GOT_ERR_EOF)
1004 err = NULL;
1005 break;
1009 done:
1010 imsg_clear(&ibuf);
1011 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1012 err = got_error_from_errno("close");
1013 if (err)
1014 echo_error(err, outfd, chattygot);
1015 return err;
1018 static const struct got_error *
1019 parse_ref_update_line(char **common_capabilities, char **refname,
1020 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1022 const struct got_error *err;
1023 char *old_id_str = NULL, *new_id_str = NULL;
1024 char *client_capabilities = NULL;
1026 *refname = NULL;
1028 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1029 refname, &client_capabilities, buf, len);
1030 if (err)
1031 return err;
1033 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1034 !got_parse_sha1_digest(new_id, new_id_str)) {
1035 err = got_error_msg(GOT_ERR_BAD_PACKET,
1036 "ref-update with bad object ID");
1037 goto done;
1039 if (!got_ref_name_is_valid(*refname)) {
1040 err = got_error_msg(GOT_ERR_BAD_PACKET,
1041 "ref-update with bad reference name");
1042 goto done;
1045 if (client_capabilities) {
1046 err = got_gitproto_match_capabilities(common_capabilities,
1047 NULL, client_capabilities, write_capabilities,
1048 nitems(write_capabilities));
1049 if (err)
1050 goto done;
1052 done:
1053 free(old_id_str);
1054 free(new_id_str);
1055 free(client_capabilities);
1056 if (err) {
1057 free(*refname);
1058 *refname = NULL;
1060 return err;
1063 static const struct got_error *
1064 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1065 char *buf, size_t len, int expect_capabilities, int chattygot)
1067 const struct got_error *err;
1068 struct gotd_imsg_ref_update iref;
1069 struct ibuf *wbuf;
1070 char *capabilities_str = NULL, *refname = NULL;
1071 int done = 0;
1072 struct imsg imsg;
1074 memset(&iref, 0, sizeof(iref));
1075 memset(&imsg, 0, sizeof(imsg));
1077 err = parse_ref_update_line(&capabilities_str, &refname,
1078 iref.old_id, iref.new_id, buf, len);
1079 if (err)
1080 return err;
1082 if (capabilities_str) {
1083 if (!expect_capabilities) {
1084 err = got_error_msg(GOT_ERR_BAD_PACKET,
1085 "unexpected capability announcement received");
1086 goto done;
1088 err = send_capabilities(NULL, report_status, capabilities_str,
1089 ibuf);
1090 if (err)
1091 goto done;
1094 iref.name_len = strlen(refname);
1095 len = sizeof(iref) + iref.name_len;
1096 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1097 if (wbuf == NULL) {
1098 err = got_error_from_errno("imsg_create REF_UPDATE");
1099 goto done;
1102 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1103 return got_error_from_errno("imsg_add REF_UPDATE");
1104 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1105 return got_error_from_errno("imsg_add REF_UPDATE");
1106 wbuf->fd = -1;
1107 imsg_close(ibuf, wbuf);
1109 err = gotd_imsg_flush(ibuf);
1110 if (err)
1111 goto done;
1113 /* Wait for ACK or an error. */
1114 while (!done && err == NULL) {
1115 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1116 if (err)
1117 break;
1118 switch (imsg.hdr.type) {
1119 case GOTD_IMSG_ERROR:
1120 err = gotd_imsg_recv_error(NULL, &imsg);
1121 break;
1122 case GOTD_IMSG_ACK:
1123 err = recv_ack(&imsg, iref.new_id);
1124 if (err)
1125 break;
1126 done = 1;
1127 break;
1128 default:
1129 err = got_error(GOT_ERR_PRIVSEP_MSG);
1130 break;
1133 imsg_free(&imsg);
1135 done:
1136 free(capabilities_str);
1137 free(refname);
1138 return err;
1141 static const struct got_error *
1142 recv_packfile(struct imsg *imsg, int infd)
1144 const struct got_error *err = NULL;
1145 size_t datalen;
1146 int packfd;
1147 char buf[GOT_PKT_MAX];
1148 int pack_done = 0;
1150 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1151 if (datalen != 0)
1152 return got_error(GOT_ERR_PRIVSEP_MSG);
1154 if (imsg->fd == -1)
1155 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1157 packfd = imsg->fd;
1158 while (!pack_done) {
1159 ssize_t r = 0;
1161 err = got_poll_fd(infd, POLLIN, 1);
1162 if (err) {
1163 if (err->code != GOT_ERR_TIMEOUT)
1164 break;
1165 err = NULL;
1166 } else {
1167 r = read(infd, buf, sizeof(buf));
1168 if (r == -1) {
1169 err = got_error_from_errno("read");
1170 break;
1172 if (r == 0) {
1174 * Git clients hang up their side of the
1175 * connection after sending the pack file.
1177 err = NULL;
1178 pack_done = 1;
1179 break;
1183 if (r == 0) {
1184 /* Detect gotd(8) closing the pack pipe when done. */
1185 err = got_poll_fd(packfd, POLLOUT, 1);
1186 if (err) {
1187 if (err->code != GOT_ERR_EOF)
1188 break;
1189 err = NULL;
1190 pack_done = 1;
1192 } else {
1193 /* Write pack data and/or detect pipe being closed. */
1194 err = got_poll_write_full(packfd, buf, r);
1195 if (err) {
1196 if (err->code == GOT_ERR_EOF)
1197 err = NULL;
1198 break;
1203 close(packfd);
1204 return err;
1207 static const struct got_error *
1208 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1210 const struct got_error *err = NULL;
1211 struct gotd_imsg_packfile_status istatus;
1212 char buf[GOT_PKT_MAX];
1213 size_t datalen, len;
1214 char *reason = NULL;
1216 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1217 if (datalen < sizeof(istatus))
1218 return got_error(GOT_ERR_PRIVSEP_LEN);
1219 memcpy(&istatus, imsg->data, sizeof(istatus));
1220 if (datalen != sizeof(istatus) + istatus.reason_len)
1221 return got_error(GOT_ERR_PRIVSEP_LEN);
1223 reason = malloc(istatus.reason_len + 1);
1224 if (reason == NULL) {
1225 err = got_error_from_errno("malloc");
1226 goto done;
1228 memcpy(reason, imsg->data + sizeof(istatus), istatus.reason_len);
1229 reason[istatus.reason_len] = '\0';
1231 if (err == NULL)
1232 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1233 else
1234 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1235 if (len >= sizeof(buf)) {
1236 err = got_error(GOT_ERR_NO_SPACE);
1237 goto done;
1240 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1241 done:
1242 free(reason);
1243 return err;
1246 static const struct got_error *
1247 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1249 const struct got_error *err = NULL;
1250 struct gotd_imsg_ref_update_ok iok;
1251 size_t datalen, len;
1252 char buf[GOT_PKT_MAX];
1253 char *refname = NULL;
1255 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1256 if (datalen < sizeof(iok))
1257 return got_error(GOT_ERR_PRIVSEP_LEN);
1258 memcpy(&iok, imsg->data, sizeof(iok));
1259 if (datalen != sizeof(iok) + iok.name_len)
1260 return got_error(GOT_ERR_PRIVSEP_LEN);
1262 memcpy(&iok, imsg->data, sizeof(iok));
1264 refname = malloc(iok.name_len + 1);
1265 if (refname == NULL)
1266 return got_error_from_errno("malloc");
1267 memcpy(refname, imsg->data + sizeof(iok), iok.name_len);
1268 refname[iok.name_len] = '\0';
1270 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1271 if (len >= sizeof(buf)) {
1272 err = got_error(GOT_ERR_NO_SPACE);
1273 goto done;
1276 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1277 done:
1278 free(refname);
1279 return err;
1282 static const struct got_error *
1283 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1285 const struct got_error *err = NULL;
1286 struct gotd_imsg_ref_update_ng ing;
1287 size_t datalen, len;
1288 char buf[GOT_PKT_MAX];
1289 char *refname = NULL, *reason = NULL;
1291 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1292 if (datalen < sizeof(ing))
1293 return got_error(GOT_ERR_PRIVSEP_LEN);
1294 memcpy(&ing, imsg->data, sizeof(ing));
1295 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1296 return got_error(GOT_ERR_PRIVSEP_LEN);
1298 memcpy(&ing, imsg->data, sizeof(ing));
1300 refname = malloc(ing.name_len + 1);
1301 if (refname == NULL)
1302 return got_error_from_errno("malloc");
1303 memcpy(refname, imsg->data + sizeof(ing), ing.name_len);
1304 refname[ing.name_len] = '\0';
1306 reason = malloc(ing.reason_len + 1);
1307 if (reason == NULL) {
1308 err = got_error_from_errno("malloc");
1309 goto done;
1311 memcpy(refname, imsg->data + sizeof(ing) + ing.name_len,
1312 ing.reason_len);
1313 refname[ing.reason_len] = '\0';
1315 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1316 if (len >= sizeof(buf)) {
1317 err = got_error(GOT_ERR_NO_SPACE);
1318 goto done;
1321 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1322 done:
1323 free(refname);
1324 free(reason);
1325 return err;
1328 static const struct got_error *
1329 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1330 int chattygot)
1332 const struct got_error *err = NULL;
1333 char buf[GOT_PKT_MAX];
1334 struct imsgbuf ibuf;
1335 enum protostate {
1336 STATE_EXPECT_REF_UPDATE,
1337 STATE_EXPECT_MORE_REF_UPDATES,
1338 STATE_EXPECT_PACKFILE,
1339 STATE_PACKFILE_RECEIVED,
1340 STATE_REFS_UPDATED,
1342 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1343 struct imsg imsg;
1344 int report_status = 0;
1346 imsg_init(&ibuf, gotd_sock);
1347 memset(&imsg, 0, sizeof(imsg));
1349 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1350 if (err)
1351 goto done;
1353 while (curstate != STATE_EXPECT_PACKFILE) {
1354 int n;
1355 buf[0] = '\0';
1356 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1357 if (err)
1358 break;
1359 if (n == 0) {
1360 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1361 err = got_error_msg(GOT_ERR_BAD_PACKET,
1362 "unexpected flush packet received");
1363 goto done;
1365 err = forward_flushpkt(&ibuf);
1366 if (err)
1367 goto done;
1368 curstate = STATE_EXPECT_PACKFILE;
1369 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1370 if (curstate != STATE_EXPECT_REF_UPDATE &&
1371 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1372 err = got_error_msg(GOT_ERR_BAD_PACKET,
1373 "unexpected ref-update packet");
1374 goto done;
1376 if (curstate == STATE_EXPECT_REF_UPDATE) {
1377 err = recv_ref_update(&report_status,
1378 outfd, &ibuf, buf, n, 1, chattygot);
1379 } else {
1380 err = recv_ref_update(NULL, outfd, &ibuf,
1381 buf, n, 0, chattygot);
1383 if (err)
1384 goto done;
1385 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1386 } else {
1387 err = got_error(GOT_ERR_BAD_PACKET);
1388 goto done;
1392 while (curstate != STATE_PACKFILE_RECEIVED) {
1393 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1394 if (err)
1395 goto done;
1396 switch (imsg.hdr.type) {
1397 case GOTD_IMSG_ERROR:
1398 err = gotd_imsg_recv_error(NULL, &imsg);
1399 goto done;
1400 case GOTD_IMSG_PACKFILE_PIPE:
1401 err = recv_packfile(&imsg, infd);
1402 if (err) {
1403 if (err->code != GOT_ERR_EOF)
1404 goto done;
1406 * EOF is reported when the client hangs up,
1407 * which can happen with Git clients.
1408 * The socket should stay half-open so we
1409 * can still send our reports if requested.
1411 err = NULL;
1413 curstate = STATE_PACKFILE_RECEIVED;
1414 break;
1415 default:
1416 err = got_error(GOT_ERR_PRIVSEP_MSG);
1417 break;
1420 imsg_free(&imsg);
1421 if (err)
1422 goto done;
1425 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1426 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1427 if (err)
1428 break;
1429 switch (imsg.hdr.type) {
1430 case GOTD_IMSG_ERROR:
1431 err = gotd_imsg_recv_error(NULL, &imsg);
1432 break;
1433 case GOTD_IMSG_PACKFILE_STATUS:
1434 if (!report_status)
1435 break;
1436 err = report_unpack_status(&imsg, outfd, chattygot);
1437 break;
1438 case GOTD_IMSG_REF_UPDATE_OK:
1439 if (!report_status)
1440 break;
1441 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1442 break;
1443 case GOTD_IMSG_REF_UPDATE_NG:
1444 if (!report_status)
1445 break;
1446 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1447 break;
1448 case GOTD_IMSG_REFS_UPDATED:
1449 curstate = STATE_REFS_UPDATED;
1450 err = got_pkt_flushpkt(outfd, chattygot);
1451 break;
1452 default:
1453 err = got_error(GOT_ERR_PRIVSEP_MSG);
1454 break;
1457 imsg_free(&imsg);
1459 done:
1460 imsg_clear(&ibuf);
1461 if (err)
1462 echo_error(err, outfd, chattygot);
1463 return err;
1466 const struct got_error *
1467 got_serve(int infd, int outfd, const char *gitcmd, int gotd_sock, int chattygot)
1469 const struct got_error *err = NULL;
1470 char *command = NULL, *repo_path = NULL;
1472 err = parse_command(&command, &repo_path, gitcmd);
1473 if (err)
1474 return err;
1476 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1477 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1478 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1479 err = serve_write(infd, outfd, gotd_sock, repo_path, chattygot);
1480 else
1481 err = got_error(GOT_ERR_BAD_PACKET);
1483 free(command);
1484 free(repo_path);
1485 return err;