2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 13b2bc37 2022-10-23 stsp #include <sys/types.h>
18 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
19 13b2bc37 2022-10-23 stsp #include <sys/uio.h>
21 13b2bc37 2022-10-23 stsp #include <errno.h>
22 13b2bc37 2022-10-23 stsp #include <event.h>
23 13b2bc37 2022-10-23 stsp #include <poll.h>
24 13b2bc37 2022-10-23 stsp #include <limits.h>
25 13b2bc37 2022-10-23 stsp #include <sha1.h>
26 13b2bc37 2022-10-23 stsp #include <stdio.h>
27 13b2bc37 2022-10-23 stsp #include <stdint.h>
28 13b2bc37 2022-10-23 stsp #include <stdlib.h>
29 13b2bc37 2022-10-23 stsp #include <string.h>
30 13b2bc37 2022-10-23 stsp #include <imsg.h>
31 13b2bc37 2022-10-23 stsp #include <unistd.h>
33 13b2bc37 2022-10-23 stsp #include "got_error.h"
34 13b2bc37 2022-10-23 stsp #include "got_serve.h"
35 13b2bc37 2022-10-23 stsp #include "got_path.h"
36 13b2bc37 2022-10-23 stsp #include "got_version.h"
37 13b2bc37 2022-10-23 stsp #include "got_reference.h"
39 13b2bc37 2022-10-23 stsp #include "got_lib_pkt.h"
40 13b2bc37 2022-10-23 stsp #include "got_lib_dial.h"
41 13b2bc37 2022-10-23 stsp #include "got_lib_gitproto.h"
42 13b2bc37 2022-10-23 stsp #include "got_lib_sha1.h"
43 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
45 13b2bc37 2022-10-23 stsp #include "gotd.h"
47 13b2bc37 2022-10-23 stsp #ifndef nitems
48 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 13b2bc37 2022-10-23 stsp static const struct got_capability read_capabilities[] = {
52 13b2bc37 2022-10-23 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
53 13b2bc37 2022-10-23 stsp { GOT_CAPA_OFS_DELTA, NULL },
54 13b2bc37 2022-10-23 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
57 13b2bc37 2022-10-23 stsp static const struct got_capability write_capabilities[] = {
58 13b2bc37 2022-10-23 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
59 13b2bc37 2022-10-23 stsp { GOT_CAPA_OFS_DELTA, NULL },
60 13b2bc37 2022-10-23 stsp { GOT_CAPA_REPORT_STATUS, NULL },
61 13b2bc37 2022-10-23 stsp { GOT_CAPA_NO_THIN, NULL },
63 13b2bc37 2022-10-23 stsp { GOT_CAPA_DELETE_REFS, NULL },
67 13b2bc37 2022-10-23 stsp static const struct got_error *
68 13b2bc37 2022-10-23 stsp parse_command(char **command, char **repo_path, const char *gitcmd)
70 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
71 13b2bc37 2022-10-23 stsp size_t len, cmdlen, pathlen;
72 13b2bc37 2022-10-23 stsp char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
73 13b2bc37 2022-10-23 stsp const char *relpath;
75 13b2bc37 2022-10-23 stsp *command = NULL;
76 13b2bc37 2022-10-23 stsp *repo_path = NULL;
78 13b2bc37 2022-10-23 stsp len = strlen(gitcmd);
80 13b2bc37 2022-10-23 stsp if (len >= strlen(GOT_SERVE_CMD_SEND) &&
81 13b2bc37 2022-10-23 stsp strncmp(gitcmd, GOT_SERVE_CMD_SEND,
82 13b2bc37 2022-10-23 stsp strlen(GOT_SERVE_CMD_SEND)) == 0)
83 13b2bc37 2022-10-23 stsp cmdlen = strlen(GOT_SERVE_CMD_SEND);
84 13b2bc37 2022-10-23 stsp else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
85 13b2bc37 2022-10-23 stsp strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
86 13b2bc37 2022-10-23 stsp strlen(GOT_SERVE_CMD_FETCH)) == 0)
87 13b2bc37 2022-10-23 stsp cmdlen = strlen(GOT_SERVE_CMD_FETCH);
89 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_PACKET);
91 13b2bc37 2022-10-23 stsp if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
92 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_PACKET);
94 13b2bc37 2022-10-23 stsp if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
95 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_PATH);
97 13b2bc37 2022-10-23 stsp /* Forbid linefeeds in paths, like Git does. */
98 13b2bc37 2022-10-23 stsp if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
99 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_PATH);
101 13b2bc37 2022-10-23 stsp path0 = strdup(&gitcmd[cmdlen + 1]);
102 13b2bc37 2022-10-23 stsp if (path0 == NULL)
103 13b2bc37 2022-10-23 stsp return got_error_from_errno("strdup");
104 13b2bc37 2022-10-23 stsp path = path0;
105 13b2bc37 2022-10-23 stsp pathlen = strlen(path);
108 13b2bc37 2022-10-23 stsp * Git clients send a shell command.
109 13b2bc37 2022-10-23 stsp * Trim spaces and quotes around the path.
111 13b2bc37 2022-10-23 stsp while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
115 13b2bc37 2022-10-23 stsp while (pathlen > 0 &&
116 13b2bc37 2022-10-23 stsp (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
117 13b2bc37 2022-10-23 stsp path[pathlen - 1] == ' ')) {
118 13b2bc37 2022-10-23 stsp path[pathlen - 1] = '\0';
122 13b2bc37 2022-10-23 stsp /* Deny an empty repository path. */
123 13b2bc37 2022-10-23 stsp if (path[0] == '\0' || got_path_is_root_dir(path)) {
124 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
128 13b2bc37 2022-10-23 stsp if (asprintf(&abspath, "/%s", path) == -1) {
129 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
132 13b2bc37 2022-10-23 stsp pathlen = strlen(abspath);
133 13b2bc37 2022-10-23 stsp canonpath = malloc(pathlen);
134 13b2bc37 2022-10-23 stsp if (canonpath == NULL) {
135 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
138 13b2bc37 2022-10-23 stsp err = got_canonpath(abspath, canonpath, pathlen);
142 13b2bc37 2022-10-23 stsp relpath = canonpath;
143 13b2bc37 2022-10-23 stsp while (relpath[0] == '/')
145 13b2bc37 2022-10-23 stsp *repo_path = strdup(relpath);
146 13b2bc37 2022-10-23 stsp if (*repo_path == NULL) {
147 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strdup");
150 13b2bc37 2022-10-23 stsp *command = strndup(gitcmd, cmdlen);
151 13b2bc37 2022-10-23 stsp if (*command == NULL)
152 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strndup");
154 13b2bc37 2022-10-23 stsp free(path0);
155 13b2bc37 2022-10-23 stsp free(abspath);
156 13b2bc37 2022-10-23 stsp free(canonpath);
158 13b2bc37 2022-10-23 stsp free(*repo_path);
159 13b2bc37 2022-10-23 stsp *repo_path = NULL;
161 13b2bc37 2022-10-23 stsp return err;
164 13b2bc37 2022-10-23 stsp static const struct got_error *
165 13b2bc37 2022-10-23 stsp append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
166 13b2bc37 2022-10-23 stsp uint8_t *buf, size_t bufsize)
168 13b2bc37 2022-10-23 stsp struct got_capability capa[nitems(read_capabilities) + 1];
169 13b2bc37 2022-10-23 stsp size_t ncapa;
171 13b2bc37 2022-10-23 stsp memcpy(&capa, read_capabilities, sizeof(read_capabilities));
172 13b2bc37 2022-10-23 stsp if (symrefstr) {
173 13b2bc37 2022-10-23 stsp capa[nitems(read_capabilities)].key = "symref";
174 13b2bc37 2022-10-23 stsp capa[nitems(read_capabilities)].value = symrefstr;
175 13b2bc37 2022-10-23 stsp ncapa = nitems(capa);
177 13b2bc37 2022-10-23 stsp ncapa = nitems(read_capabilities);
179 13b2bc37 2022-10-23 stsp return got_gitproto_append_capabilities(capalen, buf, len,
180 13b2bc37 2022-10-23 stsp bufsize, capa, ncapa);
183 13b2bc37 2022-10-23 stsp static const struct got_error *
184 13b2bc37 2022-10-23 stsp send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
185 13b2bc37 2022-10-23 stsp int client_is_reading, const char *symrefstr, int chattygot)
187 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
188 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
189 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
190 13b2bc37 2022-10-23 stsp size_t len, capalen = 0;
192 13b2bc37 2022-10-23 stsp if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
193 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
195 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
196 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf))
197 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
199 13b2bc37 2022-10-23 stsp if (send_capabilities) {
200 13b2bc37 2022-10-23 stsp if (client_is_reading) {
201 13b2bc37 2022-10-23 stsp err = append_read_capabilities(&capalen, len,
202 13b2bc37 2022-10-23 stsp symrefstr, buf, sizeof(buf));
204 13b2bc37 2022-10-23 stsp err = got_gitproto_append_capabilities(&capalen,
205 13b2bc37 2022-10-23 stsp buf, len, sizeof(buf), write_capabilities,
206 13b2bc37 2022-10-23 stsp nitems(write_capabilities));
209 13b2bc37 2022-10-23 stsp return err;
210 13b2bc37 2022-10-23 stsp len += capalen;
213 13b2bc37 2022-10-23 stsp if (len + 1 >= sizeof(buf))
214 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
215 13b2bc37 2022-10-23 stsp buf[len] = '\n';
217 13b2bc37 2022-10-23 stsp buf[len] = '\0';
219 13b2bc37 2022-10-23 stsp return got_pkt_writepkt(outfd, buf, len, chattygot);
222 13b2bc37 2022-10-23 stsp static const struct got_error *
223 13b2bc37 2022-10-23 stsp send_zero_refs(int outfd, int chattygot)
225 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
226 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
227 13b2bc37 2022-10-23 stsp uint8_t zero[SHA1_DIGEST_LENGTH];
228 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
229 13b2bc37 2022-10-23 stsp size_t len, capalen = 0;
231 13b2bc37 2022-10-23 stsp memset(&zero, 0, sizeof(zero));
233 13b2bc37 2022-10-23 stsp if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
234 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
236 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
237 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf))
238 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
240 13b2bc37 2022-10-23 stsp err = got_gitproto_append_capabilities(&capalen, buf, len,
241 13b2bc37 2022-10-23 stsp sizeof(buf), read_capabilities, nitems(read_capabilities));
243 13b2bc37 2022-10-23 stsp return err;
245 13b2bc37 2022-10-23 stsp return got_pkt_writepkt(outfd, buf, len, chattygot);
248 13b2bc37 2022-10-23 stsp static void
249 13b2bc37 2022-10-23 stsp echo_error(const struct got_error *err, int outfd, int chattygot)
251 13b2bc37 2022-10-23 stsp char buf[4 + GOT_ERR_MAX_MSG_SIZE];
252 13b2bc37 2022-10-23 stsp size_t len;
255 13b2bc37 2022-10-23 stsp * Echo the error to the client on a pkt-line.
256 13b2bc37 2022-10-23 stsp * The client should then terminate its session.
258 13b2bc37 2022-10-23 stsp buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
259 13b2bc37 2022-10-23 stsp len = strlcat(buf, err->msg, sizeof(buf));
260 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, len, chattygot);
264 13b2bc37 2022-10-23 stsp static const struct got_error *
265 13b2bc37 2022-10-23 stsp announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
266 13b2bc37 2022-10-23 stsp const char *repo_path, int chattygot)
268 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
269 13b2bc37 2022-10-23 stsp struct imsg imsg;
270 13b2bc37 2022-10-23 stsp size_t datalen;
271 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs lsref;
272 13b2bc37 2022-10-23 stsp struct gotd_imsg_reflist ireflist;
273 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref iref;
274 13b2bc37 2022-10-23 stsp struct gotd_imsg_symref isymref;
275 13b2bc37 2022-10-23 stsp size_t nrefs = 0;
276 13b2bc37 2022-10-23 stsp int have_nrefs = 0, sent_capabilities = 0;
277 13b2bc37 2022-10-23 stsp char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
278 13b2bc37 2022-10-23 stsp char *refname = NULL;
280 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
281 13b2bc37 2022-10-23 stsp memset(&lsref, 0, sizeof(lsref));
283 13b2bc37 2022-10-23 stsp if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
284 13b2bc37 2022-10-23 stsp sizeof(lsref.repo_name))
285 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
286 13b2bc37 2022-10-23 stsp lsref.client_is_reading = client_is_reading;
288 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
289 13b2bc37 2022-10-23 stsp &lsref, sizeof(lsref)) == -1)
290 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose LIST_REFS");
292 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
294 13b2bc37 2022-10-23 stsp return err;
296 13b2bc37 2022-10-23 stsp while (!have_nrefs || nrefs > 0) {
297 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
300 13b2bc37 2022-10-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
301 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
302 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
303 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
305 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REFLIST:
306 13b2bc37 2022-10-23 stsp if (have_nrefs || nrefs > 0) {
307 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
310 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireflist)) {
311 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
314 13b2bc37 2022-10-23 stsp memcpy(&ireflist, imsg.data, sizeof(ireflist));
315 13b2bc37 2022-10-23 stsp nrefs = ireflist.nrefs;
316 13b2bc37 2022-10-23 stsp have_nrefs = 1;
317 13b2bc37 2022-10-23 stsp if (nrefs == 0)
318 13b2bc37 2022-10-23 stsp err = send_zero_refs(outfd, chattygot);
320 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF:
321 13b2bc37 2022-10-23 stsp if (!have_nrefs || nrefs == 0) {
322 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
325 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref)) {
326 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
329 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg.data, sizeof(iref));
330 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len) {
331 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
334 13b2bc37 2022-10-23 stsp refname = malloc(iref.name_len + 1);
335 13b2bc37 2022-10-23 stsp if (refname == NULL) {
336 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
339 13b2bc37 2022-10-23 stsp memcpy(refname, imsg.data + sizeof(iref),
340 13b2bc37 2022-10-23 stsp iref.name_len);
341 13b2bc37 2022-10-23 stsp refname[iref.name_len] = '\0';
342 13b2bc37 2022-10-23 stsp err = send_ref(outfd, iref.id, refname,
343 13b2bc37 2022-10-23 stsp !sent_capabilities, client_is_reading,
344 13b2bc37 2022-10-23 stsp NULL, chattygot);
345 13b2bc37 2022-10-23 stsp free(refname);
346 13b2bc37 2022-10-23 stsp refname = NULL;
349 13b2bc37 2022-10-23 stsp sent_capabilities = 1;
350 13b2bc37 2022-10-23 stsp if (nrefs > 0)
353 13b2bc37 2022-10-23 stsp case GOTD_IMSG_SYMREF:
354 13b2bc37 2022-10-23 stsp if (!have_nrefs || nrefs == 0) {
355 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
358 13b2bc37 2022-10-23 stsp if (datalen < sizeof(isymref)) {
359 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
362 13b2bc37 2022-10-23 stsp memcpy(&isymref, imsg.data, sizeof(isymref));
363 13b2bc37 2022-10-23 stsp if (datalen != sizeof(isymref) + isymref.name_len +
364 13b2bc37 2022-10-23 stsp isymref.target_len) {
365 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
370 13b2bc37 2022-10-23 stsp * For now, we only announce one symbolic ref,
371 13b2bc37 2022-10-23 stsp * as part of our capability advertisement.
373 13b2bc37 2022-10-23 stsp if (sent_capabilities || symrefstr != NULL ||
374 13b2bc37 2022-10-23 stsp symrefname != NULL || symreftarget != NULL)
377 13b2bc37 2022-10-23 stsp symrefname = malloc(isymref.name_len + 1);
378 13b2bc37 2022-10-23 stsp if (symrefname == NULL) {
379 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
382 13b2bc37 2022-10-23 stsp memcpy(symrefname, imsg.data + sizeof(isymref),
383 13b2bc37 2022-10-23 stsp isymref.name_len);
384 13b2bc37 2022-10-23 stsp symrefname[isymref.name_len] = '\0';
386 13b2bc37 2022-10-23 stsp symreftarget = malloc(isymref.target_len + 1);
387 13b2bc37 2022-10-23 stsp if (symreftarget == NULL) {
388 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
391 13b2bc37 2022-10-23 stsp memcpy(symreftarget,
392 13b2bc37 2022-10-23 stsp imsg.data + sizeof(isymref) + isymref.name_len,
393 13b2bc37 2022-10-23 stsp isymref.target_len);
394 13b2bc37 2022-10-23 stsp symreftarget[isymref.target_len] = '\0';
396 13b2bc37 2022-10-23 stsp if (asprintf(&symrefstr, "%s:%s", symrefname,
397 13b2bc37 2022-10-23 stsp symreftarget) == -1) {
398 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
401 13b2bc37 2022-10-23 stsp err = send_ref(outfd, isymref.target_id, symrefname,
402 13b2bc37 2022-10-23 stsp !sent_capabilities, client_is_reading, symrefstr,
403 13b2bc37 2022-10-23 stsp chattygot);
404 13b2bc37 2022-10-23 stsp free(refname);
405 13b2bc37 2022-10-23 stsp refname = NULL;
408 13b2bc37 2022-10-23 stsp sent_capabilities = 1;
409 13b2bc37 2022-10-23 stsp if (nrefs > 0)
413 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
417 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
420 13b2bc37 2022-10-23 stsp err = got_pkt_flushpkt(outfd, chattygot);
424 13b2bc37 2022-10-23 stsp free(symrefstr);
425 13b2bc37 2022-10-23 stsp free(symrefname);
426 13b2bc37 2022-10-23 stsp free(symreftarget);
427 13b2bc37 2022-10-23 stsp return err;
430 13b2bc37 2022-10-23 stsp static const struct got_error *
431 13b2bc37 2022-10-23 stsp parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
433 13b2bc37 2022-10-23 stsp const struct got_error *err;
434 13b2bc37 2022-10-23 stsp char *id_str = NULL, *client_capabilities = NULL;
436 13b2bc37 2022-10-23 stsp err = got_gitproto_parse_want_line(&id_str,
437 13b2bc37 2022-10-23 stsp &client_capabilities, buf, len);
439 13b2bc37 2022-10-23 stsp return err;
441 13b2bc37 2022-10-23 stsp if (!got_parse_sha1_digest(id, id_str)) {
442 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
443 13b2bc37 2022-10-23 stsp "want-line with bad object ID");
447 13b2bc37 2022-10-23 stsp if (client_capabilities) {
448 13b2bc37 2022-10-23 stsp err = got_gitproto_match_capabilities(common_capabilities,
449 13b2bc37 2022-10-23 stsp NULL, client_capabilities, read_capabilities,
450 13b2bc37 2022-10-23 stsp nitems(read_capabilities));
455 13b2bc37 2022-10-23 stsp free(id_str);
456 13b2bc37 2022-10-23 stsp free(client_capabilities);
457 13b2bc37 2022-10-23 stsp return err;
460 13b2bc37 2022-10-23 stsp static const struct got_error *
461 13b2bc37 2022-10-23 stsp parse_have_line(uint8_t *id, char *buf, size_t len)
463 13b2bc37 2022-10-23 stsp const struct got_error *err;
464 13b2bc37 2022-10-23 stsp char *id_str = NULL;
466 13b2bc37 2022-10-23 stsp err = got_gitproto_parse_have_line(&id_str, buf, len);
468 13b2bc37 2022-10-23 stsp return err;
470 13b2bc37 2022-10-23 stsp if (!got_parse_sha1_digest(id, id_str)) {
471 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
472 13b2bc37 2022-10-23 stsp "have-line with bad object ID");
476 13b2bc37 2022-10-23 stsp free(id_str);
477 13b2bc37 2022-10-23 stsp return err;
480 13b2bc37 2022-10-23 stsp static const struct got_error *
481 13b2bc37 2022-10-23 stsp send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
483 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
484 13b2bc37 2022-10-23 stsp struct gotd_imsg_capability icapa;
485 13b2bc37 2022-10-23 stsp size_t len;
486 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
488 13b2bc37 2022-10-23 stsp memset(&icapa, 0, sizeof(icapa));
490 13b2bc37 2022-10-23 stsp icapa.key_len = strlen(capa->key);
491 13b2bc37 2022-10-23 stsp len = sizeof(icapa) + icapa.key_len;
492 13b2bc37 2022-10-23 stsp if (capa->value) {
493 13b2bc37 2022-10-23 stsp icapa.value_len = strlen(capa->value);
494 13b2bc37 2022-10-23 stsp len += icapa.value_len;
497 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
498 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
499 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create CAPABILITY");
500 13b2bc37 2022-10-23 stsp return err;
503 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
504 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add CAPABILITY");
505 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
506 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add CAPABILITY");
507 13b2bc37 2022-10-23 stsp if (capa->value) {
508 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
509 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add CAPABILITY");
512 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
513 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
515 13b2bc37 2022-10-23 stsp return NULL;
518 13b2bc37 2022-10-23 stsp static const struct got_error *
519 13b2bc37 2022-10-23 stsp send_capabilities(int *use_sidebands, int *report_status,
520 13b2bc37 2022-10-23 stsp char *capabilities_str, struct imsgbuf *ibuf)
522 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
523 13b2bc37 2022-10-23 stsp struct gotd_imsg_capabilities icapas;
524 13b2bc37 2022-10-23 stsp struct got_capability *capa = NULL;
525 13b2bc37 2022-10-23 stsp size_t ncapa, i;
527 13b2bc37 2022-10-23 stsp err = got_gitproto_split_capabilities_str(&capa, &ncapa,
528 13b2bc37 2022-10-23 stsp capabilities_str);
530 13b2bc37 2022-10-23 stsp return err;
532 13b2bc37 2022-10-23 stsp icapas.ncapabilities = ncapa;
533 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
534 13b2bc37 2022-10-23 stsp &icapas, sizeof(icapas)) == -1) {
535 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
539 13b2bc37 2022-10-23 stsp for (i = 0; i < ncapa; i++) {
540 13b2bc37 2022-10-23 stsp err = send_capability(&capa[i], ibuf);
543 13b2bc37 2022-10-23 stsp if (use_sidebands &&
544 13b2bc37 2022-10-23 stsp strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
545 13b2bc37 2022-10-23 stsp *use_sidebands = 1;
546 13b2bc37 2022-10-23 stsp if (report_status &&
547 13b2bc37 2022-10-23 stsp strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
548 13b2bc37 2022-10-23 stsp *report_status = 1;
551 13b2bc37 2022-10-23 stsp free(capa);
552 13b2bc37 2022-10-23 stsp return err;
555 13b2bc37 2022-10-23 stsp static const struct got_error *
556 13b2bc37 2022-10-23 stsp forward_flushpkt(struct imsgbuf *ibuf)
558 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
559 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose FLUSH");
561 13b2bc37 2022-10-23 stsp return gotd_imsg_flush(ibuf);
564 13b2bc37 2022-10-23 stsp static const struct got_error *
565 13b2bc37 2022-10-23 stsp recv_ack(struct imsg *imsg, uint8_t *expected_id)
567 13b2bc37 2022-10-23 stsp struct gotd_imsg_ack iack;
568 13b2bc37 2022-10-23 stsp size_t datalen;
570 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
571 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iack))
572 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
574 13b2bc37 2022-10-23 stsp memcpy(&iack, imsg->data, sizeof(iack));
575 13b2bc37 2022-10-23 stsp if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
576 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
578 13b2bc37 2022-10-23 stsp return NULL;
581 13b2bc37 2022-10-23 stsp static const struct got_error *
582 13b2bc37 2022-10-23 stsp recv_nak(struct imsg *imsg, uint8_t *expected_id)
584 13b2bc37 2022-10-23 stsp struct gotd_imsg_ack inak;
585 13b2bc37 2022-10-23 stsp size_t datalen;
587 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
588 13b2bc37 2022-10-23 stsp if (datalen != sizeof(inak))
589 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
591 13b2bc37 2022-10-23 stsp memcpy(&inak, imsg->data, sizeof(inak));
592 13b2bc37 2022-10-23 stsp if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
593 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
595 13b2bc37 2022-10-23 stsp return NULL;
599 13b2bc37 2022-10-23 stsp static const struct got_error *
600 13b2bc37 2022-10-23 stsp recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
601 13b2bc37 2022-10-23 stsp char *buf, size_t len, int expect_capabilities, int chattygot)
603 13b2bc37 2022-10-23 stsp const struct got_error *err;
604 13b2bc37 2022-10-23 stsp struct gotd_imsg_want iwant;
605 13b2bc37 2022-10-23 stsp char *capabilities_str;
606 13b2bc37 2022-10-23 stsp int done = 0;
607 13b2bc37 2022-10-23 stsp struct imsg imsg;
609 13b2bc37 2022-10-23 stsp memset(&iwant, 0, sizeof(iwant));
610 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
612 13b2bc37 2022-10-23 stsp err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
614 13b2bc37 2022-10-23 stsp return err;
616 13b2bc37 2022-10-23 stsp if (capabilities_str) {
617 13b2bc37 2022-10-23 stsp if (!expect_capabilities) {
618 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
619 13b2bc37 2022-10-23 stsp "unexpected capability announcement received");
622 13b2bc37 2022-10-23 stsp err = send_capabilities(use_sidebands, NULL, capabilities_str,
629 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
630 13b2bc37 2022-10-23 stsp &iwant, sizeof(iwant)) == -1) {
631 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose WANT");
635 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
640 13b2bc37 2022-10-23 stsp * Wait for an ACK, or an error in case the desired object
641 13b2bc37 2022-10-23 stsp * does not exist.
643 13b2bc37 2022-10-23 stsp while (!done && err == NULL) {
644 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
647 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
648 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
649 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
651 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ACK:
652 13b2bc37 2022-10-23 stsp err = recv_ack(&imsg, iwant.object_id);
658 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
662 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
665 13b2bc37 2022-10-23 stsp free(capabilities_str);
666 13b2bc37 2022-10-23 stsp return err;
669 13b2bc37 2022-10-23 stsp static const struct got_error *
670 13b2bc37 2022-10-23 stsp send_ack(int outfd, uint8_t *id, int chattygot)
672 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
673 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
676 13b2bc37 2022-10-23 stsp if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
677 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
679 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
680 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf))
681 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
683 13b2bc37 2022-10-23 stsp return got_pkt_writepkt(outfd, buf, len, chattygot);
686 13b2bc37 2022-10-23 stsp static const struct got_error *
687 13b2bc37 2022-10-23 stsp send_nak(int outfd, int chattygot)
689 13b2bc37 2022-10-23 stsp char buf[5];
692 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "NAK\n");
693 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf))
694 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
696 13b2bc37 2022-10-23 stsp return got_pkt_writepkt(outfd, buf, len, chattygot);
699 13b2bc37 2022-10-23 stsp static const struct got_error *
700 13b2bc37 2022-10-23 stsp recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
701 13b2bc37 2022-10-23 stsp size_t len, int chattygot)
703 13b2bc37 2022-10-23 stsp const struct got_error *err;
704 13b2bc37 2022-10-23 stsp struct gotd_imsg_have ihave;
705 13b2bc37 2022-10-23 stsp int done = 0;
706 13b2bc37 2022-10-23 stsp struct imsg imsg;
708 13b2bc37 2022-10-23 stsp memset(&ihave, 0, sizeof(ihave));
709 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
711 13b2bc37 2022-10-23 stsp err = parse_have_line(ihave.object_id, buf, len);
713 13b2bc37 2022-10-23 stsp return err;
715 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
716 13b2bc37 2022-10-23 stsp &ihave, sizeof(ihave)) == -1)
717 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose HAVE");
719 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
721 13b2bc37 2022-10-23 stsp return err;
724 13b2bc37 2022-10-23 stsp * Wait for an ACK or a NAK, indicating whether a common
725 13b2bc37 2022-10-23 stsp * commit object has been found.
727 13b2bc37 2022-10-23 stsp while (!done && err == NULL) {
728 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
730 13b2bc37 2022-10-23 stsp return err;
731 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
732 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
733 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
735 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ACK:
736 13b2bc37 2022-10-23 stsp err = recv_ack(&imsg, ihave.object_id);
739 13b2bc37 2022-10-23 stsp if (!*have_ack) {
740 13b2bc37 2022-10-23 stsp err = send_ack(outfd, ihave.object_id,
741 13b2bc37 2022-10-23 stsp chattygot);
743 13b2bc37 2022-10-23 stsp return err;
744 13b2bc37 2022-10-23 stsp *have_ack = 1;
748 13b2bc37 2022-10-23 stsp case GOTD_IMSG_NAK:
749 13b2bc37 2022-10-23 stsp err = recv_nak(&imsg, ihave.object_id);
755 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
759 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
762 13b2bc37 2022-10-23 stsp return err;
765 13b2bc37 2022-10-23 stsp static const struct got_error *
766 13b2bc37 2022-10-23 stsp recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
768 13b2bc37 2022-10-23 stsp const struct got_error *err;
769 13b2bc37 2022-10-23 stsp struct imsg imsg;
771 13b2bc37 2022-10-23 stsp *packfd = -1;
773 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
774 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose DONE");
776 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
778 13b2bc37 2022-10-23 stsp return err;
780 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
782 13b2bc37 2022-10-23 stsp return err;
784 13b2bc37 2022-10-23 stsp if (imsg.hdr.type == GOTD_IMSG_ERROR)
785 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
786 13b2bc37 2022-10-23 stsp else if (imsg.hdr.type != GOTD_IMSG_PACKFILE_PIPE)
787 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
788 13b2bc37 2022-10-23 stsp else if (imsg.fd == -1)
789 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
790 13b2bc37 2022-10-23 stsp if (err == NULL)
791 13b2bc37 2022-10-23 stsp *packfd = imsg.fd;
793 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
794 13b2bc37 2022-10-23 stsp return err;
797 13b2bc37 2022-10-23 stsp static const struct got_error *
798 13b2bc37 2022-10-23 stsp relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
800 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
801 13b2bc37 2022-10-23 stsp int pack_starting = 0;
802 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_progress iprog;
803 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
804 13b2bc37 2022-10-23 stsp struct imsg imsg;
805 13b2bc37 2022-10-23 stsp size_t datalen;
806 13b2bc37 2022-10-23 stsp int p_deltify = 0, n;
807 13b2bc37 2022-10-23 stsp const char *eol = "\r";
809 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
811 13b2bc37 2022-10-23 stsp while (!pack_starting && err == NULL) {
812 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
816 13b2bc37 2022-10-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
817 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
818 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
819 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
821 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_READY:
822 13b2bc37 2022-10-23 stsp eol = "\n";
823 13b2bc37 2022-10-23 stsp pack_starting = 1;
824 13b2bc37 2022-10-23 stsp /* fallthrough */
825 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PROGRESS:
826 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iprog)) {
827 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
830 13b2bc37 2022-10-23 stsp memcpy(&iprog, imsg.data, sizeof(iprog));
831 13b2bc37 2022-10-23 stsp if (iprog.nobj_total > 0) {
832 13b2bc37 2022-10-23 stsp p_deltify = (iprog.nobj_deltify * 100) /
833 13b2bc37 2022-10-23 stsp iprog.nobj_total;
835 13b2bc37 2022-10-23 stsp buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
836 13b2bc37 2022-10-23 stsp n = snprintf(&buf[1], sizeof(buf) - 1,
837 13b2bc37 2022-10-23 stsp "%d commits colored, "
838 13b2bc37 2022-10-23 stsp "%d objects found, "
839 13b2bc37 2022-10-23 stsp "deltify %d%%%s",
840 13b2bc37 2022-10-23 stsp iprog.ncolored,
841 13b2bc37 2022-10-23 stsp iprog.nfound,
842 13b2bc37 2022-10-23 stsp p_deltify, eol);
843 13b2bc37 2022-10-23 stsp if (n >= sizeof(buf) - 1)
845 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
848 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
852 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
855 13b2bc37 2022-10-23 stsp return err;
858 13b2bc37 2022-10-23 stsp static const struct got_error *
859 13b2bc37 2022-10-23 stsp serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
860 13b2bc37 2022-10-23 stsp int chattygot)
862 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
863 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
864 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
865 13b2bc37 2022-10-23 stsp enum protostate {
866 13b2bc37 2022-10-23 stsp STATE_EXPECT_WANT,
867 13b2bc37 2022-10-23 stsp STATE_EXPECT_MORE_WANT,
868 13b2bc37 2022-10-23 stsp STATE_EXPECT_HAVE,
869 13b2bc37 2022-10-23 stsp STATE_EXPECT_DONE,
870 13b2bc37 2022-10-23 stsp STATE_DONE,
872 13b2bc37 2022-10-23 stsp enum protostate curstate = STATE_EXPECT_WANT;
873 13b2bc37 2022-10-23 stsp int have_ack = 0, use_sidebands = 0, seen_have = 0;
874 13b2bc37 2022-10-23 stsp int packfd = -1;
875 13b2bc37 2022-10-23 stsp size_t pack_chunksize;
877 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, gotd_sock);
879 13b2bc37 2022-10-23 stsp err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
883 13b2bc37 2022-10-23 stsp while (curstate != STATE_DONE) {
885 13b2bc37 2022-10-23 stsp buf[0] = '\0';
886 13b2bc37 2022-10-23 stsp err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
889 13b2bc37 2022-10-23 stsp if (n == 0) {
890 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_MORE_WANT &&
891 13b2bc37 2022-10-23 stsp curstate != STATE_EXPECT_HAVE) {
892 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
893 13b2bc37 2022-10-23 stsp "unexpected flush packet received");
896 13b2bc37 2022-10-23 stsp err = forward_flushpkt(&ibuf);
899 13b2bc37 2022-10-23 stsp if (curstate == STATE_EXPECT_HAVE && !have_ack) {
900 13b2bc37 2022-10-23 stsp err = send_nak(outfd, chattygot);
904 13b2bc37 2022-10-23 stsp if (curstate == STATE_EXPECT_MORE_WANT)
905 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_HAVE;
907 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_DONE;
908 13b2bc37 2022-10-23 stsp } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
909 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_WANT &&
910 13b2bc37 2022-10-23 stsp curstate != STATE_EXPECT_MORE_WANT) {
911 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
912 13b2bc37 2022-10-23 stsp "unexpected 'want' packet");
915 13b2bc37 2022-10-23 stsp err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
916 13b2bc37 2022-10-23 stsp curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
919 13b2bc37 2022-10-23 stsp if (curstate == STATE_EXPECT_WANT)
920 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_MORE_WANT;
921 13b2bc37 2022-10-23 stsp } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
922 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_HAVE) {
923 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
924 13b2bc37 2022-10-23 stsp "unexpected 'have' packet");
927 13b2bc37 2022-10-23 stsp err = recv_have(&have_ack, outfd, &ibuf, buf, n,
928 13b2bc37 2022-10-23 stsp chattygot);
931 13b2bc37 2022-10-23 stsp seen_have = 1;
932 13b2bc37 2022-10-23 stsp } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
933 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_HAVE &&
934 13b2bc37 2022-10-23 stsp curstate != STATE_EXPECT_DONE) {
935 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
936 13b2bc37 2022-10-23 stsp "unexpected 'done' packet");
939 13b2bc37 2022-10-23 stsp err = recv_done(&packfd, outfd, &ibuf, chattygot);
942 13b2bc37 2022-10-23 stsp curstate = STATE_DONE;
945 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_PACKET);
950 13b2bc37 2022-10-23 stsp if (!seen_have) {
951 13b2bc37 2022-10-23 stsp err = send_nak(outfd, chattygot);
956 13b2bc37 2022-10-23 stsp if (use_sidebands) {
957 13b2bc37 2022-10-23 stsp err = relay_progress_reports(&ibuf, outfd, chattygot);
960 13b2bc37 2022-10-23 stsp pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
962 13b2bc37 2022-10-23 stsp pack_chunksize = sizeof(buf);
965 13b2bc37 2022-10-23 stsp ssize_t r, w;
967 13b2bc37 2022-10-23 stsp r = read(packfd, use_sidebands ? &buf[1] : buf,
968 13b2bc37 2022-10-23 stsp pack_chunksize);
969 13b2bc37 2022-10-23 stsp if (r == -1) {
970 13b2bc37 2022-10-23 stsp err = got_error_from_errno("read");
972 13b2bc37 2022-10-23 stsp } else if (r == 0) {
973 13b2bc37 2022-10-23 stsp err = got_pkt_flushpkt(outfd, chattygot);
977 13b2bc37 2022-10-23 stsp if (use_sidebands) {
978 13b2bc37 2022-10-23 stsp buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
979 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
983 13b2bc37 2022-10-23 stsp w = write(outfd, buf, r);
984 13b2bc37 2022-10-23 stsp if (w == -1) {
985 13b2bc37 2022-10-23 stsp err = got_error_from_errno("write");
987 13b2bc37 2022-10-23 stsp } else if (w != r) {
988 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_IO);
994 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
995 13b2bc37 2022-10-23 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
996 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
998 13b2bc37 2022-10-23 stsp echo_error(err, outfd, chattygot);
999 13b2bc37 2022-10-23 stsp return err;
1002 13b2bc37 2022-10-23 stsp static const struct got_error *
1003 13b2bc37 2022-10-23 stsp parse_ref_update_line(char **common_capabilities, char **refname,
1004 13b2bc37 2022-10-23 stsp uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1006 13b2bc37 2022-10-23 stsp const struct got_error *err;
1007 13b2bc37 2022-10-23 stsp char *old_id_str = NULL, *new_id_str = NULL;
1008 13b2bc37 2022-10-23 stsp char *client_capabilities = NULL;
1010 13b2bc37 2022-10-23 stsp *refname = NULL;
1012 13b2bc37 2022-10-23 stsp err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1013 13b2bc37 2022-10-23 stsp refname, &client_capabilities, buf, len);
1015 13b2bc37 2022-10-23 stsp return err;
1017 13b2bc37 2022-10-23 stsp if (!got_parse_sha1_digest(old_id, old_id_str) ||
1018 13b2bc37 2022-10-23 stsp !got_parse_sha1_digest(new_id, new_id_str)) {
1019 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1020 13b2bc37 2022-10-23 stsp "ref-update with bad object ID");
1021 13b2bc37 2022-10-23 stsp goto done;
1023 13b2bc37 2022-10-23 stsp if (!got_ref_name_is_valid(*refname)) {
1024 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1025 13b2bc37 2022-10-23 stsp "ref-update with bad reference name");
1026 13b2bc37 2022-10-23 stsp goto done;
1029 13b2bc37 2022-10-23 stsp if (client_capabilities) {
1030 13b2bc37 2022-10-23 stsp err = got_gitproto_match_capabilities(common_capabilities,
1031 13b2bc37 2022-10-23 stsp NULL, client_capabilities, write_capabilities,
1032 13b2bc37 2022-10-23 stsp nitems(write_capabilities));
1034 13b2bc37 2022-10-23 stsp goto done;
1037 13b2bc37 2022-10-23 stsp free(old_id_str);
1038 13b2bc37 2022-10-23 stsp free(new_id_str);
1039 13b2bc37 2022-10-23 stsp free(client_capabilities);
1040 13b2bc37 2022-10-23 stsp if (err) {
1041 13b2bc37 2022-10-23 stsp free(*refname);
1042 13b2bc37 2022-10-23 stsp *refname = NULL;
1044 13b2bc37 2022-10-23 stsp return err;
1047 13b2bc37 2022-10-23 stsp static const struct got_error *
1048 13b2bc37 2022-10-23 stsp recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1049 13b2bc37 2022-10-23 stsp char *buf, size_t len, int expect_capabilities, int chattygot)
1051 13b2bc37 2022-10-23 stsp const struct got_error *err;
1052 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1053 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1054 13b2bc37 2022-10-23 stsp char *capabilities_str = NULL, *refname = NULL;
1055 13b2bc37 2022-10-23 stsp int done = 0;
1056 13b2bc37 2022-10-23 stsp struct imsg imsg;
1058 13b2bc37 2022-10-23 stsp memset(&iref, 0, sizeof(iref));
1059 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
1061 13b2bc37 2022-10-23 stsp err = parse_ref_update_line(&capabilities_str, &refname,
1062 13b2bc37 2022-10-23 stsp iref.old_id, iref.new_id, buf, len);
1064 13b2bc37 2022-10-23 stsp return err;
1066 13b2bc37 2022-10-23 stsp if (capabilities_str) {
1067 13b2bc37 2022-10-23 stsp if (!expect_capabilities) {
1068 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1069 13b2bc37 2022-10-23 stsp "unexpected capability announcement received");
1070 13b2bc37 2022-10-23 stsp goto done;
1072 13b2bc37 2022-10-23 stsp err = send_capabilities(NULL, report_status, capabilities_str,
1075 13b2bc37 2022-10-23 stsp goto done;
1078 13b2bc37 2022-10-23 stsp iref.name_len = strlen(refname);
1079 13b2bc37 2022-10-23 stsp len = sizeof(iref) + iref.name_len;
1080 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1081 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
1082 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF_UPDATE");
1083 13b2bc37 2022-10-23 stsp goto done;
1086 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1087 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1088 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iref.name_len) == -1)
1089 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1090 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
1091 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
1093 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
1095 13b2bc37 2022-10-23 stsp goto done;
1097 13b2bc37 2022-10-23 stsp /* Wait for ACK or an error. */
1098 13b2bc37 2022-10-23 stsp while (!done && err == NULL) {
1099 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1102 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1103 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1104 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
1106 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ACK:
1107 13b2bc37 2022-10-23 stsp err = recv_ack(&imsg, iref.new_id);
1113 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1117 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1120 13b2bc37 2022-10-23 stsp free(capabilities_str);
1121 13b2bc37 2022-10-23 stsp free(refname);
1122 13b2bc37 2022-10-23 stsp return err;
1125 13b2bc37 2022-10-23 stsp static const struct got_error *
1126 13b2bc37 2022-10-23 stsp recv_packfile(struct imsg *imsg, int infd)
1128 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1129 13b2bc37 2022-10-23 stsp size_t datalen;
1130 13b2bc37 2022-10-23 stsp int packfd;
1131 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1132 13b2bc37 2022-10-23 stsp int pack_done = 0;
1134 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1135 13b2bc37 2022-10-23 stsp if (datalen != 0)
1136 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1138 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
1139 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1141 13b2bc37 2022-10-23 stsp packfd = imsg->fd;
1142 13b2bc37 2022-10-23 stsp while (!pack_done) {
1143 13b2bc37 2022-10-23 stsp ssize_t r = 0;
1145 13b2bc37 2022-10-23 stsp err = got_poll_fd(infd, POLLIN, 1);
1146 13b2bc37 2022-10-23 stsp if (err) {
1147 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_TIMEOUT)
1149 13b2bc37 2022-10-23 stsp err = NULL;
1151 13b2bc37 2022-10-23 stsp r = read(infd, buf, sizeof(buf));
1152 13b2bc37 2022-10-23 stsp if (r == -1) {
1153 13b2bc37 2022-10-23 stsp err = got_error_from_errno("read");
1156 13b2bc37 2022-10-23 stsp if (r == 0) {
1158 13b2bc37 2022-10-23 stsp * Git clients hang up their side of the
1159 13b2bc37 2022-10-23 stsp * connection after sending the pack file.
1161 13b2bc37 2022-10-23 stsp err = NULL;
1162 13b2bc37 2022-10-23 stsp pack_done = 1;
1167 13b2bc37 2022-10-23 stsp if (r == 0) {
1168 13b2bc37 2022-10-23 stsp /* Detect gotd(8) closing the pack pipe when done. */
1169 13b2bc37 2022-10-23 stsp err = got_poll_fd(packfd, POLLOUT, 1);
1170 13b2bc37 2022-10-23 stsp if (err) {
1171 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF)
1173 13b2bc37 2022-10-23 stsp err = NULL;
1174 13b2bc37 2022-10-23 stsp pack_done = 1;
1177 13b2bc37 2022-10-23 stsp /* Write pack data and/or detect pipe being closed. */
1178 13b2bc37 2022-10-23 stsp err = got_poll_write_full(packfd, buf, r);
1179 13b2bc37 2022-10-23 stsp if (err) {
1180 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF)
1181 13b2bc37 2022-10-23 stsp err = NULL;
1187 13b2bc37 2022-10-23 stsp close(packfd);
1188 13b2bc37 2022-10-23 stsp return err;
1191 13b2bc37 2022-10-23 stsp static const struct got_error *
1192 13b2bc37 2022-10-23 stsp report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1194 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1195 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_status istatus;
1196 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1197 13b2bc37 2022-10-23 stsp size_t datalen, len;
1198 13b2bc37 2022-10-23 stsp char *reason = NULL;
1200 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1201 13b2bc37 2022-10-23 stsp if (datalen < sizeof(istatus))
1202 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1203 13b2bc37 2022-10-23 stsp memcpy(&istatus, imsg->data, sizeof(istatus));
1204 13b2bc37 2022-10-23 stsp if (datalen != sizeof(istatus) + istatus.reason_len)
1205 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1207 13b2bc37 2022-10-23 stsp reason = malloc(istatus.reason_len + 1);
1208 13b2bc37 2022-10-23 stsp if (reason == NULL) {
1209 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
1210 13b2bc37 2022-10-23 stsp goto done;
1212 13b2bc37 2022-10-23 stsp memcpy(reason, imsg->data + sizeof(istatus), istatus.reason_len);
1213 13b2bc37 2022-10-23 stsp reason[istatus.reason_len] = '\0';
1215 13b2bc37 2022-10-23 stsp if (err == NULL)
1216 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "unpack ok\n");
1218 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1219 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf)) {
1220 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1221 13b2bc37 2022-10-23 stsp goto done;
1224 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, len, chattygot);
1226 13b2bc37 2022-10-23 stsp free(reason);
1227 13b2bc37 2022-10-23 stsp return err;
1230 13b2bc37 2022-10-23 stsp static const struct got_error *
1231 13b2bc37 2022-10-23 stsp recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1233 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1234 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update_ok iok;
1235 13b2bc37 2022-10-23 stsp size_t datalen, len;
1236 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1237 13b2bc37 2022-10-23 stsp char *refname = NULL;
1239 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1240 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iok))
1241 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1242 13b2bc37 2022-10-23 stsp memcpy(&iok, imsg->data, sizeof(iok));
1243 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iok) + iok.name_len)
1244 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1246 13b2bc37 2022-10-23 stsp memcpy(&iok, imsg->data, sizeof(iok));
1248 13b2bc37 2022-10-23 stsp refname = malloc(iok.name_len + 1);
1249 13b2bc37 2022-10-23 stsp if (refname == NULL)
1250 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
1251 13b2bc37 2022-10-23 stsp memcpy(refname, imsg->data + sizeof(iok), iok.name_len);
1252 13b2bc37 2022-10-23 stsp refname[iok.name_len] = '\0';
1254 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1255 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf)) {
1256 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1257 13b2bc37 2022-10-23 stsp goto done;
1260 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, len, chattygot);
1262 13b2bc37 2022-10-23 stsp free(refname);
1263 13b2bc37 2022-10-23 stsp return err;
1266 13b2bc37 2022-10-23 stsp static const struct got_error *
1267 13b2bc37 2022-10-23 stsp recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1269 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1270 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update_ng ing;
1271 13b2bc37 2022-10-23 stsp size_t datalen, len;
1272 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1273 13b2bc37 2022-10-23 stsp char *refname = NULL, *reason = NULL;
1275 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1276 13b2bc37 2022-10-23 stsp if (datalen < sizeof(ing))
1277 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1278 13b2bc37 2022-10-23 stsp memcpy(&ing, imsg->data, sizeof(ing));
1279 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1280 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1282 13b2bc37 2022-10-23 stsp memcpy(&ing, imsg->data, sizeof(ing));
1284 13b2bc37 2022-10-23 stsp refname = malloc(ing.name_len + 1);
1285 13b2bc37 2022-10-23 stsp if (refname == NULL)
1286 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
1287 13b2bc37 2022-10-23 stsp memcpy(refname, imsg->data + sizeof(ing), ing.name_len);
1288 13b2bc37 2022-10-23 stsp refname[ing.name_len] = '\0';
1290 13b2bc37 2022-10-23 stsp reason = malloc(ing.reason_len + 1);
1291 13b2bc37 2022-10-23 stsp if (reason == NULL) {
1292 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
1293 13b2bc37 2022-10-23 stsp goto done;
1295 13b2bc37 2022-10-23 stsp memcpy(refname, imsg->data + sizeof(ing) + ing.name_len,
1296 13b2bc37 2022-10-23 stsp ing.reason_len);
1297 13b2bc37 2022-10-23 stsp refname[ing.reason_len] = '\0';
1299 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1300 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf)) {
1301 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1302 13b2bc37 2022-10-23 stsp goto done;
1305 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, len, chattygot);
1307 13b2bc37 2022-10-23 stsp free(refname);
1308 13b2bc37 2022-10-23 stsp free(reason);
1309 13b2bc37 2022-10-23 stsp return err;
1312 13b2bc37 2022-10-23 stsp static const struct got_error *
1313 13b2bc37 2022-10-23 stsp serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1314 13b2bc37 2022-10-23 stsp int chattygot)
1316 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1317 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1318 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
1319 13b2bc37 2022-10-23 stsp enum protostate {
1320 13b2bc37 2022-10-23 stsp STATE_EXPECT_REF_UPDATE,
1321 13b2bc37 2022-10-23 stsp STATE_EXPECT_MORE_REF_UPDATES,
1322 13b2bc37 2022-10-23 stsp STATE_EXPECT_PACKFILE,
1323 13b2bc37 2022-10-23 stsp STATE_PACKFILE_RECEIVED,
1324 13b2bc37 2022-10-23 stsp STATE_REFS_UPDATED,
1326 13b2bc37 2022-10-23 stsp enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1327 13b2bc37 2022-10-23 stsp struct imsg imsg;
1328 13b2bc37 2022-10-23 stsp int report_status = 0;
1330 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, gotd_sock);
1331 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
1333 13b2bc37 2022-10-23 stsp err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1335 13b2bc37 2022-10-23 stsp goto done;
1337 13b2bc37 2022-10-23 stsp while (curstate != STATE_EXPECT_PACKFILE) {
1339 13b2bc37 2022-10-23 stsp buf[0] = '\0';
1340 13b2bc37 2022-10-23 stsp err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1343 13b2bc37 2022-10-23 stsp if (n == 0) {
1344 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1345 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1346 13b2bc37 2022-10-23 stsp "unexpected flush packet received");
1347 13b2bc37 2022-10-23 stsp goto done;
1349 13b2bc37 2022-10-23 stsp err = forward_flushpkt(&ibuf);
1351 13b2bc37 2022-10-23 stsp goto done;
1352 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_PACKFILE;
1353 13b2bc37 2022-10-23 stsp } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1354 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_REF_UPDATE &&
1355 13b2bc37 2022-10-23 stsp curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1356 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1357 13b2bc37 2022-10-23 stsp "unexpected ref-update packet");
1358 13b2bc37 2022-10-23 stsp goto done;
1360 13b2bc37 2022-10-23 stsp if (curstate == STATE_EXPECT_REF_UPDATE) {
1361 13b2bc37 2022-10-23 stsp err = recv_ref_update(&report_status,
1362 13b2bc37 2022-10-23 stsp outfd, &ibuf, buf, n, 1, chattygot);
1364 13b2bc37 2022-10-23 stsp err = recv_ref_update(NULL, outfd, &ibuf,
1365 13b2bc37 2022-10-23 stsp buf, n, 0, chattygot);
1368 13b2bc37 2022-10-23 stsp goto done;
1369 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_MORE_REF_UPDATES;
1371 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_PACKET);
1372 13b2bc37 2022-10-23 stsp goto done;
1376 13b2bc37 2022-10-23 stsp while (curstate != STATE_PACKFILE_RECEIVED) {
1377 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1379 13b2bc37 2022-10-23 stsp goto done;
1380 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1381 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1382 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
1383 13b2bc37 2022-10-23 stsp goto done;
1384 13b2bc37 2022-10-23 stsp case GOTD_IMSG_RECV_PACKFILE:
1385 13b2bc37 2022-10-23 stsp err = recv_packfile(&imsg, infd);
1386 13b2bc37 2022-10-23 stsp if (err) {
1387 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF)
1388 13b2bc37 2022-10-23 stsp goto done;
1390 13b2bc37 2022-10-23 stsp * EOF is reported when the client hangs up,
1391 13b2bc37 2022-10-23 stsp * which can happen with Git clients.
1392 13b2bc37 2022-10-23 stsp * The socket should stay half-open so we
1393 13b2bc37 2022-10-23 stsp * can still send our reports if requested.
1395 13b2bc37 2022-10-23 stsp err = NULL;
1397 13b2bc37 2022-10-23 stsp curstate = STATE_PACKFILE_RECEIVED;
1400 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1404 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1406 13b2bc37 2022-10-23 stsp goto done;
1409 13b2bc37 2022-10-23 stsp while (curstate != STATE_REFS_UPDATED && err == NULL) {
1410 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1413 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1414 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1415 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
1417 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_STATUS:
1418 13b2bc37 2022-10-23 stsp if (!report_status)
1420 13b2bc37 2022-10-23 stsp err = report_unpack_status(&imsg, outfd, chattygot);
1422 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE_OK:
1423 13b2bc37 2022-10-23 stsp if (!report_status)
1425 13b2bc37 2022-10-23 stsp err = recv_ref_update_ok(&imsg, outfd, chattygot);
1427 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE_NG:
1428 13b2bc37 2022-10-23 stsp if (!report_status)
1430 13b2bc37 2022-10-23 stsp err = recv_ref_update_ng(&imsg, outfd, chattygot);
1432 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REFS_UPDATED:
1433 13b2bc37 2022-10-23 stsp curstate = STATE_REFS_UPDATED;
1434 13b2bc37 2022-10-23 stsp err = got_pkt_flushpkt(outfd, chattygot);
1437 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1441 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1444 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1446 13b2bc37 2022-10-23 stsp echo_error(err, outfd, chattygot);
1447 13b2bc37 2022-10-23 stsp return err;
1450 13b2bc37 2022-10-23 stsp const struct got_error *
1451 13b2bc37 2022-10-23 stsp got_serve(int infd, int outfd, const char *gitcmd, int gotd_sock, int chattygot)
1453 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1454 13b2bc37 2022-10-23 stsp char *command = NULL, *repo_path = NULL;
1456 13b2bc37 2022-10-23 stsp err = parse_command(&command, &repo_path, gitcmd);
1458 13b2bc37 2022-10-23 stsp return err;
1460 13b2bc37 2022-10-23 stsp if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1461 13b2bc37 2022-10-23 stsp err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1462 13b2bc37 2022-10-23 stsp else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1463 13b2bc37 2022-10-23 stsp err = serve_write(infd, outfd, gotd_sock, repo_path, chattygot);
1465 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_PACKET);
1467 13b2bc37 2022-10-23 stsp free(command);
1468 13b2bc37 2022-10-23 stsp free(repo_path);
1469 13b2bc37 2022-10-23 stsp return err;