Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <errno.h>
22 #include <event.h>
23 #include <poll.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <sha2.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <imsg.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_serve.h"
36 #include "got_path.h"
37 #include "got_version.h"
38 #include "got_reference.h"
40 #include "got_lib_pkt.h"
41 #include "got_lib_dial.h"
42 #include "got_lib_gitproto.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_poll.h"
46 #include "gotd.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 #endif
52 static const struct got_capability read_capabilities[] = {
53 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
54 { GOT_CAPA_OFS_DELTA, NULL },
55 { GOT_CAPA_SIDE_BAND_64K, NULL },
56 };
58 static const struct got_capability write_capabilities[] = {
59 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
60 { GOT_CAPA_OFS_DELTA, NULL },
61 { GOT_CAPA_REPORT_STATUS, NULL },
62 { GOT_CAPA_NO_THIN, NULL },
63 { GOT_CAPA_DELETE_REFS, NULL },
64 };
66 const struct got_error *
67 got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
68 {
69 const struct got_error *err = NULL;
70 size_t len, cmdlen, pathlen;
71 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
72 const char *relpath;
74 *command = NULL;
75 *repo_path = NULL;
77 len = strlen(gitcmd);
79 if (len >= strlen(GOT_SERVE_CMD_SEND) &&
80 strncmp(gitcmd, GOT_SERVE_CMD_SEND,
81 strlen(GOT_SERVE_CMD_SEND)) == 0)
82 cmdlen = strlen(GOT_SERVE_CMD_SEND);
83 else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
84 strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
85 strlen(GOT_SERVE_CMD_FETCH)) == 0)
86 cmdlen = strlen(GOT_SERVE_CMD_FETCH);
87 else
88 return got_error(GOT_ERR_BAD_PACKET);
90 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
91 return got_error(GOT_ERR_BAD_PACKET);
93 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
94 return got_error(GOT_ERR_BAD_PATH);
96 /* Forbid linefeeds in paths, like Git does. */
97 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
98 return got_error(GOT_ERR_BAD_PATH);
100 path0 = strdup(&gitcmd[cmdlen + 1]);
101 if (path0 == NULL)
102 return got_error_from_errno("strdup");
103 path = path0;
104 pathlen = strlen(path);
106 /*
107 * Git clients send a shell command.
108 * Trim spaces and quotes around the path.
109 */
110 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
111 path++;
112 pathlen--;
114 while (pathlen > 0 &&
115 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
116 path[pathlen - 1] == ' ')) {
117 path[pathlen - 1] = '\0';
118 pathlen--;
121 /* Deny an empty repository path. */
122 if (path[0] == '\0' || got_path_is_root_dir(path)) {
123 err = got_error(GOT_ERR_NOT_GIT_REPO);
124 goto done;
127 if (asprintf(&abspath, "/%s", path) == -1) {
128 err = got_error_from_errno("asprintf");
129 goto done;
131 pathlen = strlen(abspath);
132 canonpath = malloc(pathlen);
133 if (canonpath == NULL) {
134 err = got_error_from_errno("malloc");
135 goto done;
137 err = got_canonpath(abspath, canonpath, pathlen);
138 if (err)
139 goto done;
141 relpath = canonpath;
142 while (relpath[0] == '/')
143 relpath++;
144 *repo_path = strdup(relpath);
145 if (*repo_path == NULL) {
146 err = got_error_from_errno("strdup");
147 goto done;
149 *command = strndup(gitcmd, cmdlen);
150 if (*command == NULL)
151 err = got_error_from_errno("strndup");
152 done:
153 free(path0);
154 free(abspath);
155 free(canonpath);
156 if (err) {
157 free(*repo_path);
158 *repo_path = NULL;
160 return err;
163 static const struct got_error *
164 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
165 uint8_t *buf, size_t bufsize)
167 struct got_capability capa[nitems(read_capabilities) + 1];
168 size_t ncapa;
170 memcpy(&capa, read_capabilities, sizeof(read_capabilities));
171 if (symrefstr) {
172 capa[nitems(read_capabilities)].key = "symref";
173 capa[nitems(read_capabilities)].value = symrefstr;
174 ncapa = nitems(capa);
175 } else
176 ncapa = nitems(read_capabilities);
178 return got_gitproto_append_capabilities(capalen, buf, len,
179 bufsize, capa, ncapa);
182 static const struct got_error *
183 send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
184 int client_is_reading, const char *symrefstr, int chattygot)
186 const struct got_error *err = NULL;
187 char hex[SHA1_DIGEST_STRING_LENGTH];
188 char buf[GOT_PKT_MAX];
189 size_t len, capalen = 0;
191 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
192 return got_error(GOT_ERR_BAD_OBJ_ID);
194 len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
195 if (len >= sizeof(buf))
196 return got_error(GOT_ERR_NO_SPACE);
198 if (send_capabilities) {
199 if (client_is_reading) {
200 err = append_read_capabilities(&capalen, len,
201 symrefstr, buf, sizeof(buf));
202 } else {
203 err = got_gitproto_append_capabilities(&capalen,
204 buf, len, sizeof(buf), write_capabilities,
205 nitems(write_capabilities));
207 if (err)
208 return err;
209 len += capalen;
212 if (len + 1 >= sizeof(buf))
213 return got_error(GOT_ERR_NO_SPACE);
214 buf[len] = '\n';
215 len++;
216 buf[len] = '\0';
218 return got_pkt_writepkt(outfd, buf, len, chattygot);
221 static const struct got_error *
222 send_zero_refs(int outfd, int client_is_reading, int chattygot)
224 const struct got_error *err = NULL;
225 const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
226 char buf[GOT_PKT_MAX];
227 size_t len, capalen = 0;
229 len = strlcpy(buf, line, sizeof(buf));
230 if (len >= sizeof(buf))
231 return got_error(GOT_ERR_NO_SPACE);
233 if (client_is_reading) {
234 err = got_gitproto_append_capabilities(&capalen, buf, len,
235 sizeof(buf), read_capabilities, nitems(read_capabilities));
236 if (err)
237 return err;
238 } else {
239 err = got_gitproto_append_capabilities(&capalen, buf, len,
240 sizeof(buf), write_capabilities,
241 nitems(write_capabilities));
242 if (err)
243 return err;
246 return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
249 static void
250 echo_error(const struct got_error *err, int outfd, int chattygot)
252 char buf[4 + GOT_ERR_MAX_MSG_SIZE];
253 size_t len;
255 /*
256 * Echo the error to the client on a pkt-line.
257 * The client should then terminate its session.
258 */
259 buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
260 len = strlcat(buf, err->msg, sizeof(buf));
261 got_pkt_writepkt(outfd, buf, len, chattygot);
264 static const struct got_error *
265 announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
266 const char *repo_path, int chattygot)
268 const struct got_error *err = NULL;
269 struct imsg imsg;
270 size_t datalen;
271 struct gotd_imsg_list_refs lsref;
272 struct gotd_imsg_reflist ireflist;
273 struct gotd_imsg_ref iref;
274 struct gotd_imsg_symref isymref;
275 size_t nrefs = 0;
276 int have_nrefs = 0, sent_capabilities = 0;
277 char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
278 char *refname = NULL;
280 memset(&imsg, 0, sizeof(imsg));
281 memset(&lsref, 0, sizeof(lsref));
283 if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
284 sizeof(lsref.repo_name))
285 return got_error(GOT_ERR_NO_SPACE);
286 lsref.client_is_reading = client_is_reading;
288 if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
289 &lsref, sizeof(lsref)) == -1)
290 return got_error_from_errno("imsg_compose LIST_REFS");
292 err = gotd_imsg_flush(ibuf);
293 if (err)
294 return err;
296 while (!have_nrefs || nrefs > 0) {
297 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
298 if (err)
299 goto done;
300 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
301 switch (imsg.hdr.type) {
302 case GOTD_IMSG_ERROR:
303 err = gotd_imsg_recv_error(NULL, &imsg);
304 goto done;
305 case GOTD_IMSG_REFLIST:
306 if (have_nrefs || nrefs > 0) {
307 err = got_error(GOT_ERR_PRIVSEP_MSG);
308 goto done;
310 if (datalen != sizeof(ireflist)) {
311 err = got_error(GOT_ERR_PRIVSEP_MSG);
312 goto done;
314 memcpy(&ireflist, imsg.data, sizeof(ireflist));
315 nrefs = ireflist.nrefs;
316 have_nrefs = 1;
317 if (nrefs == 0)
318 err = send_zero_refs(outfd, client_is_reading,
319 chattygot);
320 break;
321 case GOTD_IMSG_REF:
322 if (!have_nrefs || nrefs == 0) {
323 err = got_error(GOT_ERR_PRIVSEP_MSG);
324 goto done;
326 if (datalen < sizeof(iref)) {
327 err = got_error(GOT_ERR_PRIVSEP_MSG);
328 goto done;
330 memcpy(&iref, imsg.data, sizeof(iref));
331 if (datalen != sizeof(iref) + iref.name_len) {
332 err = got_error(GOT_ERR_PRIVSEP_LEN);
333 goto done;
335 refname = strndup(imsg.data + sizeof(iref),
336 iref.name_len);
337 if (refname == NULL) {
338 err = got_error_from_errno("strndup");
339 goto done;
341 err = send_ref(outfd, iref.id, refname,
342 !sent_capabilities, client_is_reading,
343 NULL, chattygot);
344 free(refname);
345 refname = NULL;
346 if (err)
347 goto done;
348 sent_capabilities = 1;
349 if (nrefs > 0)
350 nrefs--;
351 break;
352 case GOTD_IMSG_SYMREF:
353 if (!have_nrefs || nrefs == 0) {
354 err = got_error(GOT_ERR_PRIVSEP_MSG);
355 goto done;
357 if (datalen < sizeof(isymref)) {
358 err = got_error(GOT_ERR_PRIVSEP_LEN);
359 goto done;
361 memcpy(&isymref, imsg.data, sizeof(isymref));
362 if (datalen != sizeof(isymref) + isymref.name_len +
363 isymref.target_len) {
364 err = got_error(GOT_ERR_PRIVSEP_LEN);
365 goto done;
368 /*
369 * For now, we only announce one symbolic ref,
370 * as part of our capability advertisement.
371 */
372 if (sent_capabilities || symrefstr != NULL ||
373 symrefname != NULL || symreftarget != NULL)
374 break;
376 symrefname = strndup(imsg.data + sizeof(isymref),
377 isymref.name_len);
378 if (symrefname == NULL) {
379 err = got_error_from_errno("malloc");
380 goto done;
383 symreftarget = strndup(
384 imsg.data + sizeof(isymref) + isymref.name_len,
385 isymref.target_len);
386 if (symreftarget == NULL) {
387 err = got_error_from_errno("strndup");
388 goto done;
391 if (asprintf(&symrefstr, "%s:%s", symrefname,
392 symreftarget) == -1) {
393 err = got_error_from_errno("asprintf");
394 goto done;
396 err = send_ref(outfd, isymref.target_id, symrefname,
397 !sent_capabilities, client_is_reading, symrefstr,
398 chattygot);
399 free(refname);
400 refname = NULL;
401 if (err)
402 goto done;
403 sent_capabilities = 1;
404 if (nrefs > 0)
405 nrefs--;
406 break;
407 default:
408 err = got_error(GOT_ERR_PRIVSEP_MSG);
409 break;
412 imsg_free(&imsg);
415 err = got_pkt_flushpkt(outfd, chattygot);
416 if (err)
417 goto done;
418 done:
419 free(symrefstr);
420 free(symrefname);
421 free(symreftarget);
422 return err;
425 static const struct got_error *
426 parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
428 const struct got_error *err;
429 char *id_str = NULL, *client_capabilities = NULL;
431 err = got_gitproto_parse_want_line(&id_str,
432 &client_capabilities, buf, len);
433 if (err)
434 return err;
436 if (!got_parse_sha1_digest(id, id_str)) {
437 err = got_error_msg(GOT_ERR_BAD_PACKET,
438 "want-line with bad object ID");
439 goto done;
442 if (client_capabilities) {
443 err = got_gitproto_match_capabilities(common_capabilities,
444 NULL, client_capabilities, read_capabilities,
445 nitems(read_capabilities));
446 if (err)
447 goto done;
449 done:
450 free(id_str);
451 free(client_capabilities);
452 return err;
455 static const struct got_error *
456 parse_have_line(uint8_t *id, char *buf, size_t len)
458 const struct got_error *err;
459 char *id_str = NULL;
461 err = got_gitproto_parse_have_line(&id_str, buf, len);
462 if (err)
463 return err;
465 if (!got_parse_sha1_digest(id, id_str)) {
466 err = got_error_msg(GOT_ERR_BAD_PACKET,
467 "have-line with bad object ID");
468 goto done;
470 done:
471 free(id_str);
472 return err;
475 static const struct got_error *
476 send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
478 const struct got_error *err = NULL;
479 struct gotd_imsg_capability icapa;
480 size_t len;
481 struct ibuf *wbuf;
483 memset(&icapa, 0, sizeof(icapa));
485 icapa.key_len = strlen(capa->key);
486 len = sizeof(icapa) + icapa.key_len;
487 if (capa->value) {
488 icapa.value_len = strlen(capa->value);
489 len += icapa.value_len;
492 wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
493 if (wbuf == NULL) {
494 err = got_error_from_errno("imsg_create CAPABILITY");
495 return err;
498 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
499 return got_error_from_errno("imsg_add CAPABILITY");
500 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
501 return got_error_from_errno("imsg_add CAPABILITY");
502 if (capa->value) {
503 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
504 return got_error_from_errno("imsg_add CAPABILITY");
507 wbuf->fd = -1;
508 imsg_close(ibuf, wbuf);
510 return NULL;
513 static const struct got_error *
514 send_capabilities(int *use_sidebands, int *report_status,
515 char *capabilities_str, struct imsgbuf *ibuf)
517 const struct got_error *err = NULL;
518 struct gotd_imsg_capabilities icapas;
519 struct got_capability *capa = NULL;
520 size_t ncapa, i;
522 err = got_gitproto_split_capabilities_str(&capa, &ncapa,
523 capabilities_str);
524 if (err)
525 return err;
527 icapas.ncapabilities = ncapa;
528 if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
529 &icapas, sizeof(icapas)) == -1) {
530 err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
531 goto done;
534 for (i = 0; i < ncapa; i++) {
535 err = send_capability(&capa[i], ibuf);
536 if (err)
537 goto done;
538 if (use_sidebands &&
539 strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
540 *use_sidebands = 1;
541 if (report_status &&
542 strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
543 *report_status = 1;
545 done:
546 free(capa);
547 return err;
550 static const struct got_error *
551 forward_flushpkt(struct imsgbuf *ibuf)
553 if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
554 return got_error_from_errno("imsg_compose FLUSH");
556 return gotd_imsg_flush(ibuf);
559 static const struct got_error *
560 recv_ack(struct imsg *imsg, uint8_t *expected_id)
562 struct gotd_imsg_ack iack;
563 size_t datalen;
565 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
566 if (datalen != sizeof(iack))
567 return got_error(GOT_ERR_PRIVSEP_LEN);
569 memcpy(&iack, imsg->data, sizeof(iack));
570 if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
571 return got_error(GOT_ERR_BAD_OBJ_ID);
573 return NULL;
576 static const struct got_error *
577 recv_nak(struct imsg *imsg, uint8_t *expected_id)
579 struct gotd_imsg_ack inak;
580 size_t datalen;
582 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
583 if (datalen != sizeof(inak))
584 return got_error(GOT_ERR_PRIVSEP_LEN);
586 memcpy(&inak, imsg->data, sizeof(inak));
587 if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
588 return got_error(GOT_ERR_BAD_OBJ_ID);
590 return NULL;
594 static const struct got_error *
595 recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
596 char *buf, size_t len, int expect_capabilities, int chattygot)
598 const struct got_error *err;
599 struct gotd_imsg_want iwant;
600 char *capabilities_str;
601 int done = 0;
602 struct imsg imsg;
604 memset(&iwant, 0, sizeof(iwant));
605 memset(&imsg, 0, sizeof(imsg));
607 err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
608 if (err)
609 return err;
611 if (capabilities_str) {
612 if (!expect_capabilities) {
613 err = got_error_msg(GOT_ERR_BAD_PACKET,
614 "unexpected capability announcement received");
615 goto done;
617 err = send_capabilities(use_sidebands, NULL, capabilities_str,
618 ibuf);
619 if (err)
620 goto done;
624 if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
625 &iwant, sizeof(iwant)) == -1) {
626 err = got_error_from_errno("imsg_compose WANT");
627 goto done;
630 err = gotd_imsg_flush(ibuf);
631 if (err)
632 goto done;
634 /*
635 * Wait for an ACK, or an error in case the desired object
636 * does not exist.
637 */
638 while (!done && err == NULL) {
639 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
640 if (err)
641 break;
642 switch (imsg.hdr.type) {
643 case GOTD_IMSG_ERROR:
644 err = gotd_imsg_recv_error(NULL, &imsg);
645 break;
646 case GOTD_IMSG_ACK:
647 err = recv_ack(&imsg, iwant.object_id);
648 if (err)
649 break;
650 done = 1;
651 break;
652 default:
653 err = got_error(GOT_ERR_PRIVSEP_MSG);
654 break;
657 imsg_free(&imsg);
659 done:
660 free(capabilities_str);
661 return err;
664 static const struct got_error *
665 send_ack(int outfd, uint8_t *id, int chattygot)
667 char hex[SHA1_DIGEST_STRING_LENGTH];
668 char buf[GOT_PKT_MAX];
669 int len;
671 if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
672 return got_error(GOT_ERR_BAD_OBJ_ID);
674 len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
675 if (len >= sizeof(buf))
676 return got_error(GOT_ERR_NO_SPACE);
678 return got_pkt_writepkt(outfd, buf, len, chattygot);
681 static const struct got_error *
682 send_nak(int outfd, int chattygot)
684 char buf[5];
685 int len;
687 len = snprintf(buf, sizeof(buf), "NAK\n");
688 if (len >= sizeof(buf))
689 return got_error(GOT_ERR_NO_SPACE);
691 return got_pkt_writepkt(outfd, buf, len, chattygot);
694 static const struct got_error *
695 recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
696 size_t len, int chattygot)
698 const struct got_error *err;
699 struct gotd_imsg_have ihave;
700 int done = 0;
701 struct imsg imsg;
703 memset(&ihave, 0, sizeof(ihave));
704 memset(&imsg, 0, sizeof(imsg));
706 err = parse_have_line(ihave.object_id, buf, len);
707 if (err)
708 return err;
710 if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
711 &ihave, sizeof(ihave)) == -1)
712 return got_error_from_errno("imsg_compose HAVE");
714 err = gotd_imsg_flush(ibuf);
715 if (err)
716 return err;
718 /*
719 * Wait for an ACK or a NAK, indicating whether a common
720 * commit object has been found.
721 */
722 while (!done && err == NULL) {
723 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
724 if (err)
725 return err;
726 switch (imsg.hdr.type) {
727 case GOTD_IMSG_ERROR:
728 err = gotd_imsg_recv_error(NULL, &imsg);
729 break;
730 case GOTD_IMSG_ACK:
731 err = recv_ack(&imsg, ihave.object_id);
732 if (err)
733 break;
734 if (!*have_ack) {
735 err = send_ack(outfd, ihave.object_id,
736 chattygot);
737 if (err)
738 return err;
739 *have_ack = 1;
741 done = 1;
742 break;
743 case GOTD_IMSG_NAK:
744 err = recv_nak(&imsg, ihave.object_id);
745 if (err)
746 break;
747 done = 1;
748 break;
749 default:
750 err = got_error(GOT_ERR_PRIVSEP_MSG);
751 break;
754 imsg_free(&imsg);
757 return err;
760 static const struct got_error *
761 recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
763 const struct got_error *err;
764 struct imsg imsg;
766 *packfd = -1;
768 if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
769 return got_error_from_errno("imsg_compose DONE");
771 err = gotd_imsg_flush(ibuf);
772 if (err)
773 return err;
775 while (*packfd == -1 && err == NULL) {
776 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
777 if (err)
778 break;
780 switch (imsg.hdr.type) {
781 case GOTD_IMSG_ERROR:
782 err = gotd_imsg_recv_error(NULL, &imsg);
783 break;
784 case GOTD_IMSG_PACKFILE_PIPE:
785 if (imsg.fd != -1)
786 *packfd = imsg.fd;
787 else
788 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
789 break;
790 default:
791 err = got_error(GOT_ERR_PRIVSEP_MSG);
792 break;
795 imsg_free(&imsg);
798 return err;
801 static const struct got_error *
802 relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
804 const struct got_error *err = NULL;
805 int pack_starting = 0;
806 struct gotd_imsg_packfile_progress iprog;
807 char buf[GOT_PKT_MAX];
808 struct imsg imsg;
809 size_t datalen;
810 int p_deltify = 0, n;
811 const char *eol = "\r";
813 memset(&imsg, 0, sizeof(imsg));
815 while (!pack_starting && err == NULL) {
816 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
817 if (err)
818 break;
820 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
821 switch (imsg.hdr.type) {
822 case GOTD_IMSG_ERROR:
823 err = gotd_imsg_recv_error(NULL, &imsg);
824 break;
825 case GOTD_IMSG_PACKFILE_READY:
826 eol = "\n";
827 pack_starting = 1;
828 /* fallthrough */
829 case GOTD_IMSG_PACKFILE_PROGRESS:
830 if (datalen != sizeof(iprog)) {
831 err = got_error(GOT_ERR_PRIVSEP_LEN);
832 break;
834 memcpy(&iprog, imsg.data, sizeof(iprog));
835 if (iprog.nobj_total > 0) {
836 p_deltify = (iprog.nobj_deltify * 100) /
837 iprog.nobj_total;
839 buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
840 n = snprintf(&buf[1], sizeof(buf) - 1,
841 "%d commits colored, "
842 "%d objects found, "
843 "deltify %d%%%s",
844 iprog.ncolored,
845 iprog.nfound,
846 p_deltify, eol);
847 if (n >= sizeof(buf) - 1)
848 break;
849 err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
850 break;
851 default:
852 err = got_error(GOT_ERR_PRIVSEP_MSG);
853 break;
856 imsg_free(&imsg);
859 return err;
862 static const struct got_error *
863 serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
864 int chattygot)
866 const struct got_error *err = NULL;
867 char buf[GOT_PKT_MAX];
868 struct imsgbuf ibuf;
869 enum protostate {
870 STATE_EXPECT_WANT,
871 STATE_EXPECT_MORE_WANT,
872 STATE_EXPECT_HAVE,
873 STATE_EXPECT_DONE,
874 STATE_DONE,
875 };
876 enum protostate curstate = STATE_EXPECT_WANT;
877 int have_ack = 0, use_sidebands = 0, seen_have = 0;
878 int packfd = -1;
879 size_t pack_chunksize;
881 imsg_init(&ibuf, gotd_sock);
883 err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
884 if (err)
885 goto done;
887 while (curstate != STATE_DONE) {
888 int n;
889 buf[0] = '\0';
890 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
891 if (err)
892 goto done;
893 if (n == 0) {
894 if (curstate != STATE_EXPECT_WANT &&
895 curstate != STATE_EXPECT_MORE_WANT &&
896 curstate != STATE_EXPECT_HAVE &&
897 curstate != STATE_EXPECT_DONE) {
898 err = got_error_msg(GOT_ERR_BAD_PACKET,
899 "unexpected flush packet received");
900 goto done;
903 if (curstate == STATE_EXPECT_WANT) {
904 ssize_t r;
905 /*
906 * If the client does not want to fetch
907 * anything we should receive a flush
908 * packet followed by EOF.
909 */
910 r = read(infd, buf, sizeof(buf));
911 if (r == -1) {
912 err = got_error_from_errno("read");
913 goto done;
915 if (r == 0) /* EOF */
916 goto done;
918 /* Zero-length field followed by payload. */
919 err = got_error_msg(GOT_ERR_BAD_PACKET,
920 "unexpected flush packet received");
921 goto done;
924 if (curstate == STATE_EXPECT_WANT ||
925 curstate == STATE_EXPECT_MORE_WANT ||
926 curstate == STATE_EXPECT_HAVE) {
927 err = forward_flushpkt(&ibuf);
928 if (err)
929 goto done;
931 if (curstate == STATE_EXPECT_HAVE && !have_ack) {
932 err = send_nak(outfd, chattygot);
933 if (err)
934 goto done;
936 if (curstate == STATE_EXPECT_MORE_WANT)
937 curstate = STATE_EXPECT_HAVE;
938 else
939 curstate = STATE_EXPECT_DONE;
940 } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
941 if (curstate != STATE_EXPECT_WANT &&
942 curstate != STATE_EXPECT_MORE_WANT) {
943 err = got_error_msg(GOT_ERR_BAD_PACKET,
944 "unexpected 'want' packet");
945 goto done;
947 err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
948 curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
949 if (err)
950 goto done;
951 if (curstate == STATE_EXPECT_WANT)
952 curstate = STATE_EXPECT_MORE_WANT;
953 } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
954 if (curstate != STATE_EXPECT_HAVE &&
955 curstate != STATE_EXPECT_DONE) {
956 err = got_error_msg(GOT_ERR_BAD_PACKET,
957 "unexpected 'have' packet");
958 goto done;
960 if (curstate == STATE_EXPECT_HAVE) {
961 err = recv_have(&have_ack, outfd, &ibuf,
962 buf, n, chattygot);
963 if (err)
964 goto done;
965 seen_have = 1;
966 if (have_ack)
967 curstate = STATE_EXPECT_DONE;
969 } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
970 if (curstate != STATE_EXPECT_HAVE &&
971 curstate != STATE_EXPECT_DONE) {
972 err = got_error_msg(GOT_ERR_BAD_PACKET,
973 "unexpected 'done' packet");
974 goto done;
976 err = recv_done(&packfd, outfd, &ibuf, chattygot);
977 if (err)
978 goto done;
979 curstate = STATE_DONE;
980 break;
981 } else {
982 err = got_error(GOT_ERR_BAD_PACKET);
983 goto done;
987 if (!seen_have) {
988 err = send_nak(outfd, chattygot);
989 if (err)
990 goto done;
993 if (use_sidebands) {
994 err = relay_progress_reports(&ibuf, outfd, chattygot);
995 if (err)
996 goto done;
997 pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
998 } else
999 pack_chunksize = sizeof(buf);
1001 for (;;) {
1002 ssize_t r;
1004 r = read(packfd, use_sidebands ? &buf[1] : buf,
1005 pack_chunksize);
1006 if (r == -1) {
1007 err = got_error_from_errno("read");
1008 break;
1009 } else if (r == 0) {
1010 err = got_pkt_flushpkt(outfd, chattygot);
1011 break;
1014 if (use_sidebands) {
1015 buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1016 err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1017 if (err)
1018 break;
1019 } else {
1020 err = got_poll_write_full(outfd, buf, r);
1021 if (err) {
1022 if (err->code == GOT_ERR_EOF)
1023 err = NULL;
1024 break;
1028 done:
1029 imsg_clear(&ibuf);
1030 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1031 err = got_error_from_errno("close");
1032 if (err)
1033 echo_error(err, outfd, chattygot);
1034 return err;
1037 static const struct got_error *
1038 parse_ref_update_line(char **common_capabilities, char **refname,
1039 uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1041 const struct got_error *err;
1042 char *old_id_str = NULL, *new_id_str = NULL;
1043 char *client_capabilities = NULL;
1045 *refname = NULL;
1047 err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1048 refname, &client_capabilities, buf, len);
1049 if (err)
1050 return err;
1052 if (!got_parse_sha1_digest(old_id, old_id_str) ||
1053 !got_parse_sha1_digest(new_id, new_id_str)) {
1054 err = got_error_msg(GOT_ERR_BAD_PACKET,
1055 "ref-update with bad object ID");
1056 goto done;
1058 if (!got_ref_name_is_valid(*refname)) {
1059 err = got_error_msg(GOT_ERR_BAD_PACKET,
1060 "ref-update with bad reference name");
1061 goto done;
1064 if (client_capabilities) {
1065 err = got_gitproto_match_capabilities(common_capabilities,
1066 NULL, client_capabilities, write_capabilities,
1067 nitems(write_capabilities));
1068 if (err)
1069 goto done;
1071 done:
1072 free(old_id_str);
1073 free(new_id_str);
1074 free(client_capabilities);
1075 if (err) {
1076 free(*refname);
1077 *refname = NULL;
1079 return err;
1082 static const struct got_error *
1083 recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1084 char *buf, size_t len, int expect_capabilities, int chattygot)
1086 const struct got_error *err;
1087 struct gotd_imsg_ref_update iref;
1088 struct ibuf *wbuf;
1089 char *capabilities_str = NULL, *refname = NULL;
1090 int done = 0;
1091 struct imsg imsg;
1093 memset(&iref, 0, sizeof(iref));
1094 memset(&imsg, 0, sizeof(imsg));
1096 err = parse_ref_update_line(&capabilities_str, &refname,
1097 iref.old_id, iref.new_id, buf, len);
1098 if (err)
1099 return err;
1101 if (capabilities_str) {
1102 if (!expect_capabilities) {
1103 err = got_error_msg(GOT_ERR_BAD_PACKET,
1104 "unexpected capability announcement received");
1105 goto done;
1107 err = send_capabilities(NULL, report_status, capabilities_str,
1108 ibuf);
1109 if (err)
1110 goto done;
1113 iref.name_len = strlen(refname);
1114 len = sizeof(iref) + iref.name_len;
1115 wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1116 if (wbuf == NULL) {
1117 err = got_error_from_errno("imsg_create REF_UPDATE");
1118 goto done;
1121 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1122 return got_error_from_errno("imsg_add REF_UPDATE");
1123 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1124 return got_error_from_errno("imsg_add REF_UPDATE");
1125 wbuf->fd = -1;
1126 imsg_close(ibuf, wbuf);
1128 err = gotd_imsg_flush(ibuf);
1129 if (err)
1130 goto done;
1132 /* Wait for ACK or an error. */
1133 while (!done && err == NULL) {
1134 err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1135 if (err)
1136 break;
1137 switch (imsg.hdr.type) {
1138 case GOTD_IMSG_ERROR:
1139 err = gotd_imsg_recv_error(NULL, &imsg);
1140 break;
1141 case GOTD_IMSG_ACK:
1142 err = recv_ack(&imsg, iref.new_id);
1143 if (err)
1144 break;
1145 done = 1;
1146 break;
1147 default:
1148 err = got_error(GOT_ERR_PRIVSEP_MSG);
1149 break;
1152 imsg_free(&imsg);
1154 done:
1155 free(capabilities_str);
1156 free(refname);
1157 return err;
1160 static const struct got_error *
1161 recv_packfile(struct imsg *imsg, int infd)
1163 const struct got_error *err = NULL;
1164 size_t datalen;
1165 int packfd;
1166 char buf[GOT_PKT_MAX];
1167 int pack_done = 0;
1169 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1170 if (datalen != 0)
1171 return got_error(GOT_ERR_PRIVSEP_MSG);
1173 if (imsg->fd == -1)
1174 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1176 packfd = imsg->fd;
1177 while (!pack_done) {
1178 ssize_t r = 0;
1180 err = got_poll_fd(infd, POLLIN, 1);
1181 if (err) {
1182 if (err->code != GOT_ERR_TIMEOUT)
1183 break;
1184 err = NULL;
1185 } else {
1186 r = read(infd, buf, sizeof(buf));
1187 if (r == -1) {
1188 err = got_error_from_errno("read");
1189 break;
1191 if (r == 0) {
1193 * Git clients hang up their side of the
1194 * connection after sending the pack file.
1196 err = NULL;
1197 pack_done = 1;
1198 break;
1202 if (r == 0) {
1203 /* Detect gotd(8) closing the pack pipe when done. */
1204 err = got_poll_fd(packfd, POLLOUT, 1);
1205 if (err) {
1206 if (err->code != GOT_ERR_EOF)
1207 break;
1208 err = NULL;
1209 pack_done = 1;
1211 } else {
1212 /* Write pack data and/or detect pipe being closed. */
1213 err = got_poll_write_full(packfd, buf, r);
1214 if (err) {
1215 if (err->code == GOT_ERR_EOF)
1216 err = NULL;
1217 break;
1222 close(packfd);
1223 return err;
1226 static const struct got_error *
1227 report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1229 const struct got_error *err = NULL;
1230 struct gotd_imsg_packfile_status istatus;
1231 char buf[GOT_PKT_MAX];
1232 size_t datalen, len;
1233 char *reason = NULL;
1235 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1236 if (datalen < sizeof(istatus))
1237 return got_error(GOT_ERR_PRIVSEP_LEN);
1238 memcpy(&istatus, imsg->data, sizeof(istatus));
1239 if (datalen != sizeof(istatus) + istatus.reason_len)
1240 return got_error(GOT_ERR_PRIVSEP_LEN);
1242 reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1243 if (reason == NULL) {
1244 err = got_error_from_errno("strndup");
1245 goto done;
1248 if (err == NULL)
1249 len = snprintf(buf, sizeof(buf), "unpack ok\n");
1250 else
1251 len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1252 if (len >= sizeof(buf)) {
1253 err = got_error(GOT_ERR_NO_SPACE);
1254 goto done;
1257 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1258 done:
1259 free(reason);
1260 return err;
1263 static const struct got_error *
1264 recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1266 const struct got_error *err = NULL;
1267 struct gotd_imsg_ref_update_ok iok;
1268 size_t datalen, len;
1269 char buf[GOT_PKT_MAX];
1270 char *refname = NULL;
1272 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1273 if (datalen < sizeof(iok))
1274 return got_error(GOT_ERR_PRIVSEP_LEN);
1275 memcpy(&iok, imsg->data, sizeof(iok));
1276 if (datalen != sizeof(iok) + iok.name_len)
1277 return got_error(GOT_ERR_PRIVSEP_LEN);
1279 memcpy(&iok, imsg->data, sizeof(iok));
1281 refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1282 if (refname == NULL)
1283 return got_error_from_errno("strndup");
1285 len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1286 if (len >= sizeof(buf)) {
1287 err = got_error(GOT_ERR_NO_SPACE);
1288 goto done;
1291 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1292 done:
1293 free(refname);
1294 return err;
1297 static const struct got_error *
1298 recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1300 const struct got_error *err = NULL;
1301 struct gotd_imsg_ref_update_ng ing;
1302 size_t datalen, len;
1303 char buf[GOT_PKT_MAX];
1304 char *refname = NULL, *reason = NULL;
1306 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1307 if (datalen < sizeof(ing))
1308 return got_error(GOT_ERR_PRIVSEP_LEN);
1309 memcpy(&ing, imsg->data, sizeof(ing));
1310 if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1311 return got_error(GOT_ERR_PRIVSEP_LEN);
1313 memcpy(&ing, imsg->data, sizeof(ing));
1315 refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1316 if (refname == NULL)
1317 return got_error_from_errno("strndup");
1319 reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1320 ing.reason_len);
1321 if (reason == NULL) {
1322 err = got_error_from_errno("strndup");
1323 goto done;
1326 len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1327 if (len >= sizeof(buf)) {
1328 err = got_error(GOT_ERR_NO_SPACE);
1329 goto done;
1332 err = got_pkt_writepkt(outfd, buf, len, chattygot);
1333 done:
1334 free(refname);
1335 free(reason);
1336 return err;
1339 static const struct got_error *
1340 serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1341 int chattygot)
1343 const struct got_error *err = NULL;
1344 char buf[GOT_PKT_MAX];
1345 struct imsgbuf ibuf;
1346 enum protostate {
1347 STATE_EXPECT_REF_UPDATE,
1348 STATE_EXPECT_MORE_REF_UPDATES,
1349 STATE_EXPECT_PACKFILE,
1350 STATE_PACKFILE_RECEIVED,
1351 STATE_REFS_UPDATED,
1353 enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1354 struct imsg imsg;
1355 int report_status = 0;
1357 imsg_init(&ibuf, gotd_sock);
1358 memset(&imsg, 0, sizeof(imsg));
1360 err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1361 if (err)
1362 goto done;
1364 while (curstate != STATE_EXPECT_PACKFILE) {
1365 int n;
1366 buf[0] = '\0';
1367 err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1368 if (err)
1369 goto done;
1370 if (n == 0) {
1371 if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1372 err = got_error_msg(GOT_ERR_BAD_PACKET,
1373 "unexpected flush packet received");
1374 goto done;
1376 err = forward_flushpkt(&ibuf);
1377 if (err)
1378 goto done;
1379 curstate = STATE_EXPECT_PACKFILE;
1380 } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1381 if (curstate != STATE_EXPECT_REF_UPDATE &&
1382 curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1383 err = got_error_msg(GOT_ERR_BAD_PACKET,
1384 "unexpected ref-update packet");
1385 goto done;
1387 if (curstate == STATE_EXPECT_REF_UPDATE) {
1388 err = recv_ref_update(&report_status,
1389 outfd, &ibuf, buf, n, 1, chattygot);
1390 } else {
1391 err = recv_ref_update(NULL, outfd, &ibuf,
1392 buf, n, 0, chattygot);
1394 if (err)
1395 goto done;
1396 curstate = STATE_EXPECT_MORE_REF_UPDATES;
1397 } else {
1398 err = got_error(GOT_ERR_BAD_PACKET);
1399 goto done;
1403 while (curstate != STATE_PACKFILE_RECEIVED) {
1404 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1405 if (err)
1406 goto done;
1407 switch (imsg.hdr.type) {
1408 case GOTD_IMSG_ERROR:
1409 err = gotd_imsg_recv_error(NULL, &imsg);
1410 goto done;
1411 case GOTD_IMSG_PACKFILE_PIPE:
1412 err = recv_packfile(&imsg, infd);
1413 if (err) {
1414 if (err->code != GOT_ERR_EOF)
1415 goto done;
1417 * EOF is reported when the client hangs up,
1418 * which can happen with Git clients.
1419 * The socket should stay half-open so we
1420 * can still send our reports if requested.
1422 err = NULL;
1424 curstate = STATE_PACKFILE_RECEIVED;
1425 break;
1426 default:
1427 err = got_error(GOT_ERR_PRIVSEP_MSG);
1428 break;
1431 imsg_free(&imsg);
1432 if (err)
1433 goto done;
1436 while (curstate != STATE_REFS_UPDATED && err == NULL) {
1437 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1438 if (err)
1439 break;
1440 switch (imsg.hdr.type) {
1441 case GOTD_IMSG_ERROR:
1442 err = gotd_imsg_recv_error(NULL, &imsg);
1443 break;
1444 case GOTD_IMSG_PACKFILE_STATUS:
1445 if (!report_status)
1446 break;
1447 err = report_unpack_status(&imsg, outfd, chattygot);
1448 break;
1449 case GOTD_IMSG_REF_UPDATE_OK:
1450 if (!report_status)
1451 break;
1452 err = recv_ref_update_ok(&imsg, outfd, chattygot);
1453 break;
1454 case GOTD_IMSG_REF_UPDATE_NG:
1455 if (!report_status)
1456 break;
1457 err = recv_ref_update_ng(&imsg, outfd, chattygot);
1458 break;
1459 case GOTD_IMSG_REFS_UPDATED:
1460 curstate = STATE_REFS_UPDATED;
1461 err = got_pkt_flushpkt(outfd, chattygot);
1462 break;
1463 default:
1464 err = got_error(GOT_ERR_PRIVSEP_MSG);
1465 break;
1468 imsg_free(&imsg);
1470 done:
1471 imsg_clear(&ibuf);
1472 if (err)
1473 echo_error(err, outfd, chattygot);
1474 return err;
1477 const struct got_error *
1478 got_serve(int infd, int outfd, const char *command, const char *repo_path,
1479 int gotd_sock, int chattygot)
1481 const struct got_error *err = NULL;
1483 if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1484 err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1485 else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1486 err = serve_write(infd, outfd, gotd_sock, repo_path,
1487 chattygot);
1488 else
1489 err = got_error(GOT_ERR_BAD_PACKET);
1491 return err;