2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3efd8e31 2022-10-23 thomas #include <sys/types.h>
18 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
19 3efd8e31 2022-10-23 thomas #include <sys/uio.h>
21 3efd8e31 2022-10-23 thomas #include <errno.h>
22 3efd8e31 2022-10-23 thomas #include <event.h>
23 3efd8e31 2022-10-23 thomas #include <poll.h>
24 3efd8e31 2022-10-23 thomas #include <limits.h>
25 3efd8e31 2022-10-23 thomas #include <sha1.h>
26 3efd8e31 2022-10-23 thomas #include <stdio.h>
27 3efd8e31 2022-10-23 thomas #include <stdint.h>
28 3efd8e31 2022-10-23 thomas #include <stdlib.h>
29 3efd8e31 2022-10-23 thomas #include <string.h>
30 3efd8e31 2022-10-23 thomas #include <imsg.h>
31 3efd8e31 2022-10-23 thomas #include <unistd.h>
33 3efd8e31 2022-10-23 thomas #include "got_error.h"
34 3efd8e31 2022-10-23 thomas #include "got_serve.h"
35 3efd8e31 2022-10-23 thomas #include "got_path.h"
36 3efd8e31 2022-10-23 thomas #include "got_version.h"
37 3efd8e31 2022-10-23 thomas #include "got_reference.h"
39 3efd8e31 2022-10-23 thomas #include "got_lib_pkt.h"
40 3efd8e31 2022-10-23 thomas #include "got_lib_dial.h"
41 3efd8e31 2022-10-23 thomas #include "got_lib_gitproto.h"
42 3efd8e31 2022-10-23 thomas #include "got_lib_sha1.h"
43 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
45 3efd8e31 2022-10-23 thomas #include "gotd.h"
47 3efd8e31 2022-10-23 thomas #ifndef nitems
48 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 3efd8e31 2022-10-23 thomas static const struct got_capability read_capabilities[] = {
52 3efd8e31 2022-10-23 thomas { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
53 3efd8e31 2022-10-23 thomas { GOT_CAPA_OFS_DELTA, NULL },
54 3efd8e31 2022-10-23 thomas { GOT_CAPA_SIDE_BAND_64K, NULL },
57 3efd8e31 2022-10-23 thomas static const struct got_capability write_capabilities[] = {
58 3efd8e31 2022-10-23 thomas { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
59 3efd8e31 2022-10-23 thomas { GOT_CAPA_OFS_DELTA, NULL },
60 3efd8e31 2022-10-23 thomas { GOT_CAPA_REPORT_STATUS, NULL },
61 3efd8e31 2022-10-23 thomas { GOT_CAPA_NO_THIN, NULL },
62 3efd8e31 2022-10-23 thomas { GOT_CAPA_DELETE_REFS, NULL },
65 214d733a 2023-01-23 thomas const struct got_error *
66 214d733a 2023-01-23 thomas got_serve_parse_command(char **command, char **repo_path, const char *gitcmd)
68 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
69 3efd8e31 2022-10-23 thomas size_t len, cmdlen, pathlen;
70 3efd8e31 2022-10-23 thomas char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
71 3efd8e31 2022-10-23 thomas const char *relpath;
73 3efd8e31 2022-10-23 thomas *command = NULL;
74 3efd8e31 2022-10-23 thomas *repo_path = NULL;
76 3efd8e31 2022-10-23 thomas len = strlen(gitcmd);
78 3efd8e31 2022-10-23 thomas if (len >= strlen(GOT_SERVE_CMD_SEND) &&
79 3efd8e31 2022-10-23 thomas strncmp(gitcmd, GOT_SERVE_CMD_SEND,
80 3efd8e31 2022-10-23 thomas strlen(GOT_SERVE_CMD_SEND)) == 0)
81 3efd8e31 2022-10-23 thomas cmdlen = strlen(GOT_SERVE_CMD_SEND);
82 3efd8e31 2022-10-23 thomas else if (len >= strlen(GOT_SERVE_CMD_FETCH) &&
83 3efd8e31 2022-10-23 thomas strncmp(gitcmd, GOT_SERVE_CMD_FETCH,
84 3efd8e31 2022-10-23 thomas strlen(GOT_SERVE_CMD_FETCH)) == 0)
85 3efd8e31 2022-10-23 thomas cmdlen = strlen(GOT_SERVE_CMD_FETCH);
87 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_PACKET);
89 3efd8e31 2022-10-23 thomas if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
90 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_PACKET);
92 3efd8e31 2022-10-23 thomas if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
93 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_PATH);
95 3efd8e31 2022-10-23 thomas /* Forbid linefeeds in paths, like Git does. */
96 3efd8e31 2022-10-23 thomas if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
97 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_PATH);
99 3efd8e31 2022-10-23 thomas path0 = strdup(&gitcmd[cmdlen + 1]);
100 3efd8e31 2022-10-23 thomas if (path0 == NULL)
101 3efd8e31 2022-10-23 thomas return got_error_from_errno("strdup");
102 3efd8e31 2022-10-23 thomas path = path0;
103 3efd8e31 2022-10-23 thomas pathlen = strlen(path);
106 3efd8e31 2022-10-23 thomas * Git clients send a shell command.
107 3efd8e31 2022-10-23 thomas * Trim spaces and quotes around the path.
109 3efd8e31 2022-10-23 thomas while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
111 3efd8e31 2022-10-23 thomas pathlen--;
113 3efd8e31 2022-10-23 thomas while (pathlen > 0 &&
114 3efd8e31 2022-10-23 thomas (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
115 3efd8e31 2022-10-23 thomas path[pathlen - 1] == ' ')) {
116 3efd8e31 2022-10-23 thomas path[pathlen - 1] = '\0';
117 3efd8e31 2022-10-23 thomas pathlen--;
120 3efd8e31 2022-10-23 thomas /* Deny an empty repository path. */
121 3efd8e31 2022-10-23 thomas if (path[0] == '\0' || got_path_is_root_dir(path)) {
122 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NOT_GIT_REPO);
123 3efd8e31 2022-10-23 thomas goto done;
126 3efd8e31 2022-10-23 thomas if (asprintf(&abspath, "/%s", path) == -1) {
127 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
128 3efd8e31 2022-10-23 thomas goto done;
130 3efd8e31 2022-10-23 thomas pathlen = strlen(abspath);
131 3efd8e31 2022-10-23 thomas canonpath = malloc(pathlen);
132 3efd8e31 2022-10-23 thomas if (canonpath == NULL) {
133 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
134 3efd8e31 2022-10-23 thomas goto done;
136 3efd8e31 2022-10-23 thomas err = got_canonpath(abspath, canonpath, pathlen);
138 3efd8e31 2022-10-23 thomas goto done;
140 3efd8e31 2022-10-23 thomas relpath = canonpath;
141 3efd8e31 2022-10-23 thomas while (relpath[0] == '/')
142 3efd8e31 2022-10-23 thomas relpath++;
143 3efd8e31 2022-10-23 thomas *repo_path = strdup(relpath);
144 3efd8e31 2022-10-23 thomas if (*repo_path == NULL) {
145 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
146 3efd8e31 2022-10-23 thomas goto done;
148 3efd8e31 2022-10-23 thomas *command = strndup(gitcmd, cmdlen);
149 3efd8e31 2022-10-23 thomas if (*command == NULL)
150 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strndup");
152 3efd8e31 2022-10-23 thomas free(path0);
153 3efd8e31 2022-10-23 thomas free(abspath);
154 3efd8e31 2022-10-23 thomas free(canonpath);
155 3efd8e31 2022-10-23 thomas if (err) {
156 3efd8e31 2022-10-23 thomas free(*repo_path);
157 3efd8e31 2022-10-23 thomas *repo_path = NULL;
159 3efd8e31 2022-10-23 thomas return err;
162 3efd8e31 2022-10-23 thomas static const struct got_error *
163 3efd8e31 2022-10-23 thomas append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
164 3efd8e31 2022-10-23 thomas uint8_t *buf, size_t bufsize)
166 3efd8e31 2022-10-23 thomas struct got_capability capa[nitems(read_capabilities) + 1];
167 3efd8e31 2022-10-23 thomas size_t ncapa;
169 3efd8e31 2022-10-23 thomas memcpy(&capa, read_capabilities, sizeof(read_capabilities));
170 3efd8e31 2022-10-23 thomas if (symrefstr) {
171 3efd8e31 2022-10-23 thomas capa[nitems(read_capabilities)].key = "symref";
172 3efd8e31 2022-10-23 thomas capa[nitems(read_capabilities)].value = symrefstr;
173 3efd8e31 2022-10-23 thomas ncapa = nitems(capa);
175 3efd8e31 2022-10-23 thomas ncapa = nitems(read_capabilities);
177 3efd8e31 2022-10-23 thomas return got_gitproto_append_capabilities(capalen, buf, len,
178 3efd8e31 2022-10-23 thomas bufsize, capa, ncapa);
181 3efd8e31 2022-10-23 thomas static const struct got_error *
182 3efd8e31 2022-10-23 thomas send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
183 3efd8e31 2022-10-23 thomas int client_is_reading, const char *symrefstr, int chattygot)
185 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
186 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
187 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
188 3efd8e31 2022-10-23 thomas size_t len, capalen = 0;
190 946e0798 2022-11-06 thomas if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
191 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
193 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
194 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf))
195 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
197 3efd8e31 2022-10-23 thomas if (send_capabilities) {
198 3efd8e31 2022-10-23 thomas if (client_is_reading) {
199 3efd8e31 2022-10-23 thomas err = append_read_capabilities(&capalen, len,
200 3efd8e31 2022-10-23 thomas symrefstr, buf, sizeof(buf));
202 3efd8e31 2022-10-23 thomas err = got_gitproto_append_capabilities(&capalen,
203 3efd8e31 2022-10-23 thomas buf, len, sizeof(buf), write_capabilities,
204 3efd8e31 2022-10-23 thomas nitems(write_capabilities));
207 3efd8e31 2022-10-23 thomas return err;
208 3efd8e31 2022-10-23 thomas len += capalen;
211 3efd8e31 2022-10-23 thomas if (len + 1 >= sizeof(buf))
212 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
213 3efd8e31 2022-10-23 thomas buf[len] = '\n';
215 3efd8e31 2022-10-23 thomas buf[len] = '\0';
217 3efd8e31 2022-10-23 thomas return got_pkt_writepkt(outfd, buf, len, chattygot);
220 3efd8e31 2022-10-23 thomas static const struct got_error *
221 2a0fb198 2022-11-08 thomas send_zero_refs(int outfd, int client_is_reading, int chattygot)
223 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
224 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
225 3efd8e31 2022-10-23 thomas uint8_t zero[SHA1_DIGEST_LENGTH];
226 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
227 3efd8e31 2022-10-23 thomas size_t len, capalen = 0;
229 3efd8e31 2022-10-23 thomas memset(&zero, 0, sizeof(zero));
231 946e0798 2022-11-06 thomas if (got_sha1_digest_to_str(zero, hex, sizeof(hex)) == NULL)
232 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
234 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "%s capabilities^{}", hex);
235 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf))
236 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
238 2a0fb198 2022-11-08 thomas if (client_is_reading) {
239 2a0fb198 2022-11-08 thomas err = got_gitproto_append_capabilities(&capalen, buf, len,
240 2a0fb198 2022-11-08 thomas sizeof(buf), read_capabilities, nitems(read_capabilities));
242 2a0fb198 2022-11-08 thomas return err;
244 2a0fb198 2022-11-08 thomas err = got_gitproto_append_capabilities(&capalen, buf, len,
245 2a0fb198 2022-11-08 thomas sizeof(buf), write_capabilities,
246 2a0fb198 2022-11-08 thomas nitems(write_capabilities));
248 2a0fb198 2022-11-08 thomas return err;
251 ea1f0585 2022-11-07 thomas return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
254 3efd8e31 2022-10-23 thomas static void
255 3efd8e31 2022-10-23 thomas echo_error(const struct got_error *err, int outfd, int chattygot)
257 3efd8e31 2022-10-23 thomas char buf[4 + GOT_ERR_MAX_MSG_SIZE];
258 3efd8e31 2022-10-23 thomas size_t len;
261 3efd8e31 2022-10-23 thomas * Echo the error to the client on a pkt-line.
262 3efd8e31 2022-10-23 thomas * The client should then terminate its session.
264 3efd8e31 2022-10-23 thomas buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
265 3efd8e31 2022-10-23 thomas len = strlcat(buf, err->msg, sizeof(buf));
266 911f5cd5 2022-12-08 thomas got_pkt_writepkt(outfd, buf, len, chattygot);
269 3efd8e31 2022-10-23 thomas static const struct got_error *
270 3efd8e31 2022-10-23 thomas announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
271 3efd8e31 2022-10-23 thomas const char *repo_path, int chattygot)
273 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
274 3efd8e31 2022-10-23 thomas struct imsg imsg;
275 3efd8e31 2022-10-23 thomas size_t datalen;
276 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs lsref;
277 3efd8e31 2022-10-23 thomas struct gotd_imsg_reflist ireflist;
278 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref iref;
279 3efd8e31 2022-10-23 thomas struct gotd_imsg_symref isymref;
280 3efd8e31 2022-10-23 thomas size_t nrefs = 0;
281 3efd8e31 2022-10-23 thomas int have_nrefs = 0, sent_capabilities = 0;
282 3efd8e31 2022-10-23 thomas char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
283 3efd8e31 2022-10-23 thomas char *refname = NULL;
285 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
286 3efd8e31 2022-10-23 thomas memset(&lsref, 0, sizeof(lsref));
288 3efd8e31 2022-10-23 thomas if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
289 3efd8e31 2022-10-23 thomas sizeof(lsref.repo_name))
290 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
291 3efd8e31 2022-10-23 thomas lsref.client_is_reading = client_is_reading;
293 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
294 3efd8e31 2022-10-23 thomas &lsref, sizeof(lsref)) == -1)
295 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose LIST_REFS");
297 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
299 3efd8e31 2022-10-23 thomas return err;
301 3efd8e31 2022-10-23 thomas while (!have_nrefs || nrefs > 0) {
302 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
304 3efd8e31 2022-10-23 thomas goto done;
305 3efd8e31 2022-10-23 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
306 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
307 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
308 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
309 3efd8e31 2022-10-23 thomas goto done;
310 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REFLIST:
311 3efd8e31 2022-10-23 thomas if (have_nrefs || nrefs > 0) {
312 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
313 3efd8e31 2022-10-23 thomas goto done;
315 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireflist)) {
316 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
317 3efd8e31 2022-10-23 thomas goto done;
319 3efd8e31 2022-10-23 thomas memcpy(&ireflist, imsg.data, sizeof(ireflist));
320 3efd8e31 2022-10-23 thomas nrefs = ireflist.nrefs;
321 3efd8e31 2022-10-23 thomas have_nrefs = 1;
322 3efd8e31 2022-10-23 thomas if (nrefs == 0)
323 2a0fb198 2022-11-08 thomas err = send_zero_refs(outfd, client_is_reading,
324 2a0fb198 2022-11-08 thomas chattygot);
326 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF:
327 3efd8e31 2022-10-23 thomas if (!have_nrefs || nrefs == 0) {
328 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
329 3efd8e31 2022-10-23 thomas goto done;
331 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref)) {
332 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
333 3efd8e31 2022-10-23 thomas goto done;
335 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg.data, sizeof(iref));
336 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iref) + iref.name_len) {
337 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
338 3efd8e31 2022-10-23 thomas goto done;
340 fcbb06bf 2023-01-14 thomas refname = strndup(imsg.data + sizeof(iref),
341 fcbb06bf 2023-01-14 thomas iref.name_len);
342 3efd8e31 2022-10-23 thomas if (refname == NULL) {
343 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
344 3efd8e31 2022-10-23 thomas goto done;
346 3efd8e31 2022-10-23 thomas err = send_ref(outfd, iref.id, refname,
347 3efd8e31 2022-10-23 thomas !sent_capabilities, client_is_reading,
348 3efd8e31 2022-10-23 thomas NULL, chattygot);
349 3efd8e31 2022-10-23 thomas free(refname);
350 3efd8e31 2022-10-23 thomas refname = NULL;
352 3efd8e31 2022-10-23 thomas goto done;
353 3efd8e31 2022-10-23 thomas sent_capabilities = 1;
354 3efd8e31 2022-10-23 thomas if (nrefs > 0)
357 3efd8e31 2022-10-23 thomas case GOTD_IMSG_SYMREF:
358 3efd8e31 2022-10-23 thomas if (!have_nrefs || nrefs == 0) {
359 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
360 3efd8e31 2022-10-23 thomas goto done;
362 3efd8e31 2022-10-23 thomas if (datalen < sizeof(isymref)) {
363 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
364 3efd8e31 2022-10-23 thomas goto done;
366 3efd8e31 2022-10-23 thomas memcpy(&isymref, imsg.data, sizeof(isymref));
367 3efd8e31 2022-10-23 thomas if (datalen != sizeof(isymref) + isymref.name_len +
368 3efd8e31 2022-10-23 thomas isymref.target_len) {
369 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
370 3efd8e31 2022-10-23 thomas goto done;
374 3efd8e31 2022-10-23 thomas * For now, we only announce one symbolic ref,
375 3efd8e31 2022-10-23 thomas * as part of our capability advertisement.
377 3efd8e31 2022-10-23 thomas if (sent_capabilities || symrefstr != NULL ||
378 3efd8e31 2022-10-23 thomas symrefname != NULL || symreftarget != NULL)
381 fcbb06bf 2023-01-14 thomas symrefname = strndup(imsg.data + sizeof(isymref),
382 fcbb06bf 2023-01-14 thomas isymref.name_len);
383 3efd8e31 2022-10-23 thomas if (symrefname == NULL) {
384 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
385 3efd8e31 2022-10-23 thomas goto done;
388 fcbb06bf 2023-01-14 thomas symreftarget = strndup(
389 fcbb06bf 2023-01-14 thomas imsg.data + sizeof(isymref) + isymref.name_len,
390 fcbb06bf 2023-01-14 thomas isymref.target_len);
391 3efd8e31 2022-10-23 thomas if (symreftarget == NULL) {
392 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
393 3efd8e31 2022-10-23 thomas goto done;
396 3efd8e31 2022-10-23 thomas if (asprintf(&symrefstr, "%s:%s", symrefname,
397 3efd8e31 2022-10-23 thomas symreftarget) == -1) {
398 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
399 3efd8e31 2022-10-23 thomas goto done;
401 3efd8e31 2022-10-23 thomas err = send_ref(outfd, isymref.target_id, symrefname,
402 3efd8e31 2022-10-23 thomas !sent_capabilities, client_is_reading, symrefstr,
403 3efd8e31 2022-10-23 thomas chattygot);
404 3efd8e31 2022-10-23 thomas free(refname);
405 3efd8e31 2022-10-23 thomas refname = NULL;
407 3efd8e31 2022-10-23 thomas goto done;
408 3efd8e31 2022-10-23 thomas sent_capabilities = 1;
409 3efd8e31 2022-10-23 thomas if (nrefs > 0)
413 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
417 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
420 3efd8e31 2022-10-23 thomas err = got_pkt_flushpkt(outfd, chattygot);
422 3efd8e31 2022-10-23 thomas goto done;
424 3efd8e31 2022-10-23 thomas free(symrefstr);
425 3efd8e31 2022-10-23 thomas free(symrefname);
426 3efd8e31 2022-10-23 thomas free(symreftarget);
427 3efd8e31 2022-10-23 thomas return err;
430 3efd8e31 2022-10-23 thomas static const struct got_error *
431 3efd8e31 2022-10-23 thomas parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
433 3efd8e31 2022-10-23 thomas const struct got_error *err;
434 3efd8e31 2022-10-23 thomas char *id_str = NULL, *client_capabilities = NULL;
436 3efd8e31 2022-10-23 thomas err = got_gitproto_parse_want_line(&id_str,
437 3efd8e31 2022-10-23 thomas &client_capabilities, buf, len);
439 3efd8e31 2022-10-23 thomas return err;
441 3efd8e31 2022-10-23 thomas if (!got_parse_sha1_digest(id, id_str)) {
442 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
443 3efd8e31 2022-10-23 thomas "want-line with bad object ID");
444 3efd8e31 2022-10-23 thomas goto done;
447 3efd8e31 2022-10-23 thomas if (client_capabilities) {
448 3efd8e31 2022-10-23 thomas err = got_gitproto_match_capabilities(common_capabilities,
449 3efd8e31 2022-10-23 thomas NULL, client_capabilities, read_capabilities,
450 3efd8e31 2022-10-23 thomas nitems(read_capabilities));
452 3efd8e31 2022-10-23 thomas goto done;
455 3efd8e31 2022-10-23 thomas free(id_str);
456 3efd8e31 2022-10-23 thomas free(client_capabilities);
457 3efd8e31 2022-10-23 thomas return err;
460 3efd8e31 2022-10-23 thomas static const struct got_error *
461 3efd8e31 2022-10-23 thomas parse_have_line(uint8_t *id, char *buf, size_t len)
463 3efd8e31 2022-10-23 thomas const struct got_error *err;
464 3efd8e31 2022-10-23 thomas char *id_str = NULL;
466 3efd8e31 2022-10-23 thomas err = got_gitproto_parse_have_line(&id_str, buf, len);
468 3efd8e31 2022-10-23 thomas return err;
470 3efd8e31 2022-10-23 thomas if (!got_parse_sha1_digest(id, id_str)) {
471 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
472 3efd8e31 2022-10-23 thomas "have-line with bad object ID");
473 3efd8e31 2022-10-23 thomas goto done;
476 3efd8e31 2022-10-23 thomas free(id_str);
477 3efd8e31 2022-10-23 thomas return err;
480 3efd8e31 2022-10-23 thomas static const struct got_error *
481 3efd8e31 2022-10-23 thomas send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
483 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
484 3efd8e31 2022-10-23 thomas struct gotd_imsg_capability icapa;
485 3efd8e31 2022-10-23 thomas size_t len;
486 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
488 3efd8e31 2022-10-23 thomas memset(&icapa, 0, sizeof(icapa));
490 3efd8e31 2022-10-23 thomas icapa.key_len = strlen(capa->key);
491 3efd8e31 2022-10-23 thomas len = sizeof(icapa) + icapa.key_len;
492 3efd8e31 2022-10-23 thomas if (capa->value) {
493 3efd8e31 2022-10-23 thomas icapa.value_len = strlen(capa->value);
494 3efd8e31 2022-10-23 thomas len += icapa.value_len;
497 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
498 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
499 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create CAPABILITY");
500 3efd8e31 2022-10-23 thomas return err;
503 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
504 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add CAPABILITY");
505 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
506 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add CAPABILITY");
507 3efd8e31 2022-10-23 thomas if (capa->value) {
508 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
509 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add CAPABILITY");
512 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
513 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
515 3efd8e31 2022-10-23 thomas return NULL;
518 3efd8e31 2022-10-23 thomas static const struct got_error *
519 3efd8e31 2022-10-23 thomas send_capabilities(int *use_sidebands, int *report_status,
520 3efd8e31 2022-10-23 thomas char *capabilities_str, struct imsgbuf *ibuf)
522 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
523 3efd8e31 2022-10-23 thomas struct gotd_imsg_capabilities icapas;
524 3efd8e31 2022-10-23 thomas struct got_capability *capa = NULL;
525 3efd8e31 2022-10-23 thomas size_t ncapa, i;
527 3efd8e31 2022-10-23 thomas err = got_gitproto_split_capabilities_str(&capa, &ncapa,
528 3efd8e31 2022-10-23 thomas capabilities_str);
530 3efd8e31 2022-10-23 thomas return err;
532 3efd8e31 2022-10-23 thomas icapas.ncapabilities = ncapa;
533 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
534 3efd8e31 2022-10-23 thomas &icapas, sizeof(icapas)) == -1) {
535 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
536 3efd8e31 2022-10-23 thomas goto done;
539 3efd8e31 2022-10-23 thomas for (i = 0; i < ncapa; i++) {
540 3efd8e31 2022-10-23 thomas err = send_capability(&capa[i], ibuf);
542 3efd8e31 2022-10-23 thomas goto done;
543 3efd8e31 2022-10-23 thomas if (use_sidebands &&
544 3efd8e31 2022-10-23 thomas strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
545 3efd8e31 2022-10-23 thomas *use_sidebands = 1;
546 3efd8e31 2022-10-23 thomas if (report_status &&
547 3efd8e31 2022-10-23 thomas strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
548 3efd8e31 2022-10-23 thomas *report_status = 1;
551 3efd8e31 2022-10-23 thomas free(capa);
552 3efd8e31 2022-10-23 thomas return err;
555 3efd8e31 2022-10-23 thomas static const struct got_error *
556 3efd8e31 2022-10-23 thomas forward_flushpkt(struct imsgbuf *ibuf)
558 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
559 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose FLUSH");
561 3efd8e31 2022-10-23 thomas return gotd_imsg_flush(ibuf);
564 3efd8e31 2022-10-23 thomas static const struct got_error *
565 3efd8e31 2022-10-23 thomas recv_ack(struct imsg *imsg, uint8_t *expected_id)
567 3efd8e31 2022-10-23 thomas struct gotd_imsg_ack iack;
568 3efd8e31 2022-10-23 thomas size_t datalen;
570 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
571 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iack))
572 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
574 3efd8e31 2022-10-23 thomas memcpy(&iack, imsg->data, sizeof(iack));
575 3efd8e31 2022-10-23 thomas if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
576 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
578 3efd8e31 2022-10-23 thomas return NULL;
581 3efd8e31 2022-10-23 thomas static const struct got_error *
582 3efd8e31 2022-10-23 thomas recv_nak(struct imsg *imsg, uint8_t *expected_id)
584 3efd8e31 2022-10-23 thomas struct gotd_imsg_ack inak;
585 3efd8e31 2022-10-23 thomas size_t datalen;
587 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
588 3efd8e31 2022-10-23 thomas if (datalen != sizeof(inak))
589 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
591 3efd8e31 2022-10-23 thomas memcpy(&inak, imsg->data, sizeof(inak));
592 3efd8e31 2022-10-23 thomas if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
593 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
595 3efd8e31 2022-10-23 thomas return NULL;
599 3efd8e31 2022-10-23 thomas static const struct got_error *
600 3efd8e31 2022-10-23 thomas recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
601 3efd8e31 2022-10-23 thomas char *buf, size_t len, int expect_capabilities, int chattygot)
603 3efd8e31 2022-10-23 thomas const struct got_error *err;
604 3efd8e31 2022-10-23 thomas struct gotd_imsg_want iwant;
605 3efd8e31 2022-10-23 thomas char *capabilities_str;
606 3efd8e31 2022-10-23 thomas int done = 0;
607 3efd8e31 2022-10-23 thomas struct imsg imsg;
609 3efd8e31 2022-10-23 thomas memset(&iwant, 0, sizeof(iwant));
610 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
612 3efd8e31 2022-10-23 thomas err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
614 3efd8e31 2022-10-23 thomas return err;
616 3efd8e31 2022-10-23 thomas if (capabilities_str) {
617 3efd8e31 2022-10-23 thomas if (!expect_capabilities) {
618 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
619 3efd8e31 2022-10-23 thomas "unexpected capability announcement received");
620 3efd8e31 2022-10-23 thomas goto done;
622 3efd8e31 2022-10-23 thomas err = send_capabilities(use_sidebands, NULL, capabilities_str,
625 3efd8e31 2022-10-23 thomas goto done;
629 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
630 3efd8e31 2022-10-23 thomas &iwant, sizeof(iwant)) == -1) {
631 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose WANT");
632 3efd8e31 2022-10-23 thomas goto done;
635 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
637 3efd8e31 2022-10-23 thomas goto done;
640 3efd8e31 2022-10-23 thomas * Wait for an ACK, or an error in case the desired object
641 3efd8e31 2022-10-23 thomas * does not exist.
643 3efd8e31 2022-10-23 thomas while (!done && err == NULL) {
644 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
647 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
648 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
649 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
651 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ACK:
652 3efd8e31 2022-10-23 thomas err = recv_ack(&imsg, iwant.object_id);
655 3efd8e31 2022-10-23 thomas done = 1;
658 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
662 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
665 3efd8e31 2022-10-23 thomas free(capabilities_str);
666 3efd8e31 2022-10-23 thomas return err;
669 3efd8e31 2022-10-23 thomas static const struct got_error *
670 3efd8e31 2022-10-23 thomas send_ack(int outfd, uint8_t *id, int chattygot)
672 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
673 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
676 946e0798 2022-11-06 thomas if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
677 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
679 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
680 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf))
681 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
683 3efd8e31 2022-10-23 thomas return got_pkt_writepkt(outfd, buf, len, chattygot);
686 3efd8e31 2022-10-23 thomas static const struct got_error *
687 3efd8e31 2022-10-23 thomas send_nak(int outfd, int chattygot)
689 3efd8e31 2022-10-23 thomas char buf[5];
692 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "NAK\n");
693 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf))
694 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
696 3efd8e31 2022-10-23 thomas return got_pkt_writepkt(outfd, buf, len, chattygot);
699 3efd8e31 2022-10-23 thomas static const struct got_error *
700 3efd8e31 2022-10-23 thomas recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
701 3efd8e31 2022-10-23 thomas size_t len, int chattygot)
703 3efd8e31 2022-10-23 thomas const struct got_error *err;
704 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ihave;
705 3efd8e31 2022-10-23 thomas int done = 0;
706 3efd8e31 2022-10-23 thomas struct imsg imsg;
708 3efd8e31 2022-10-23 thomas memset(&ihave, 0, sizeof(ihave));
709 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
711 3efd8e31 2022-10-23 thomas err = parse_have_line(ihave.object_id, buf, len);
713 3efd8e31 2022-10-23 thomas return err;
715 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
716 3efd8e31 2022-10-23 thomas &ihave, sizeof(ihave)) == -1)
717 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose HAVE");
719 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
721 3efd8e31 2022-10-23 thomas return err;
724 3efd8e31 2022-10-23 thomas * Wait for an ACK or a NAK, indicating whether a common
725 3efd8e31 2022-10-23 thomas * commit object has been found.
727 3efd8e31 2022-10-23 thomas while (!done && err == NULL) {
728 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
730 3efd8e31 2022-10-23 thomas return err;
731 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
732 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
733 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
735 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ACK:
736 3efd8e31 2022-10-23 thomas err = recv_ack(&imsg, ihave.object_id);
739 3efd8e31 2022-10-23 thomas if (!*have_ack) {
740 3efd8e31 2022-10-23 thomas err = send_ack(outfd, ihave.object_id,
741 3efd8e31 2022-10-23 thomas chattygot);
743 3efd8e31 2022-10-23 thomas return err;
744 3efd8e31 2022-10-23 thomas *have_ack = 1;
746 3efd8e31 2022-10-23 thomas done = 1;
748 3efd8e31 2022-10-23 thomas case GOTD_IMSG_NAK:
749 3efd8e31 2022-10-23 thomas err = recv_nak(&imsg, ihave.object_id);
752 3efd8e31 2022-10-23 thomas done = 1;
755 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
759 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
762 3efd8e31 2022-10-23 thomas return err;
765 3efd8e31 2022-10-23 thomas static const struct got_error *
766 3efd8e31 2022-10-23 thomas recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
768 3efd8e31 2022-10-23 thomas const struct got_error *err;
769 3efd8e31 2022-10-23 thomas struct imsg imsg;
771 3efd8e31 2022-10-23 thomas *packfd = -1;
773 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
774 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose DONE");
776 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
778 3efd8e31 2022-10-23 thomas return err;
780 cc4cc677 2022-10-28 thomas while (*packfd == -1 && err == NULL) {
781 cc4cc677 2022-10-28 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
785 cc4cc677 2022-10-28 thomas switch (imsg.hdr.type) {
786 cc4cc677 2022-10-28 thomas case GOTD_IMSG_ERROR:
787 cc4cc677 2022-10-28 thomas err = gotd_imsg_recv_error(NULL, &imsg);
789 cc4cc677 2022-10-28 thomas case GOTD_IMSG_PACKFILE_PIPE:
790 cc4cc677 2022-10-28 thomas if (imsg.fd != -1)
791 cc4cc677 2022-10-28 thomas *packfd = imsg.fd;
793 cc4cc677 2022-10-28 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
796 cc4cc677 2022-10-28 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
800 cc4cc677 2022-10-28 thomas imsg_free(&imsg);
803 3efd8e31 2022-10-23 thomas return err;
806 3efd8e31 2022-10-23 thomas static const struct got_error *
807 3efd8e31 2022-10-23 thomas relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
809 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
810 3efd8e31 2022-10-23 thomas int pack_starting = 0;
811 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_progress iprog;
812 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
813 3efd8e31 2022-10-23 thomas struct imsg imsg;
814 3efd8e31 2022-10-23 thomas size_t datalen;
815 3efd8e31 2022-10-23 thomas int p_deltify = 0, n;
816 3efd8e31 2022-10-23 thomas const char *eol = "\r";
818 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
820 3efd8e31 2022-10-23 thomas while (!pack_starting && err == NULL) {
821 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
825 3efd8e31 2022-10-23 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
826 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
827 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
828 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
830 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_READY:
831 3efd8e31 2022-10-23 thomas eol = "\n";
832 3efd8e31 2022-10-23 thomas pack_starting = 1;
833 3efd8e31 2022-10-23 thomas /* fallthrough */
834 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_PROGRESS:
835 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iprog)) {
836 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
839 3efd8e31 2022-10-23 thomas memcpy(&iprog, imsg.data, sizeof(iprog));
840 3efd8e31 2022-10-23 thomas if (iprog.nobj_total > 0) {
841 3efd8e31 2022-10-23 thomas p_deltify = (iprog.nobj_deltify * 100) /
842 3efd8e31 2022-10-23 thomas iprog.nobj_total;
844 3efd8e31 2022-10-23 thomas buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
845 3efd8e31 2022-10-23 thomas n = snprintf(&buf[1], sizeof(buf) - 1,
846 3efd8e31 2022-10-23 thomas "%d commits colored, "
847 3efd8e31 2022-10-23 thomas "%d objects found, "
848 3efd8e31 2022-10-23 thomas "deltify %d%%%s",
849 946e0798 2022-11-06 thomas iprog.ncolored,
850 946e0798 2022-11-06 thomas iprog.nfound,
851 3efd8e31 2022-10-23 thomas p_deltify, eol);
852 3efd8e31 2022-10-23 thomas if (n >= sizeof(buf) - 1)
854 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
857 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
861 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
864 3efd8e31 2022-10-23 thomas return err;
867 3efd8e31 2022-10-23 thomas static const struct got_error *
868 3efd8e31 2022-10-23 thomas serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
869 3efd8e31 2022-10-23 thomas int chattygot)
871 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
872 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
873 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
874 3efd8e31 2022-10-23 thomas enum protostate {
875 3efd8e31 2022-10-23 thomas STATE_EXPECT_WANT,
876 3efd8e31 2022-10-23 thomas STATE_EXPECT_MORE_WANT,
877 3efd8e31 2022-10-23 thomas STATE_EXPECT_HAVE,
878 3efd8e31 2022-10-23 thomas STATE_EXPECT_DONE,
879 3efd8e31 2022-10-23 thomas STATE_DONE,
881 3efd8e31 2022-10-23 thomas enum protostate curstate = STATE_EXPECT_WANT;
882 3efd8e31 2022-10-23 thomas int have_ack = 0, use_sidebands = 0, seen_have = 0;
883 3efd8e31 2022-10-23 thomas int packfd = -1;
884 3efd8e31 2022-10-23 thomas size_t pack_chunksize;
886 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, gotd_sock);
888 3efd8e31 2022-10-23 thomas err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
890 3efd8e31 2022-10-23 thomas goto done;
892 3efd8e31 2022-10-23 thomas while (curstate != STATE_DONE) {
894 3efd8e31 2022-10-23 thomas buf[0] = '\0';
895 3efd8e31 2022-10-23 thomas err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
897 113392cf 2023-01-23 thomas goto done;
898 3efd8e31 2022-10-23 thomas if (n == 0) {
899 895484c8 2023-01-19 thomas if (curstate != STATE_EXPECT_WANT &&
900 895484c8 2023-01-19 thomas curstate != STATE_EXPECT_MORE_WANT &&
901 6110f5ef 2023-01-19 thomas curstate != STATE_EXPECT_HAVE &&
902 6110f5ef 2023-01-19 thomas curstate != STATE_EXPECT_DONE) {
903 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
904 3efd8e31 2022-10-23 thomas "unexpected flush packet received");
905 3efd8e31 2022-10-23 thomas goto done;
908 895484c8 2023-01-19 thomas if (curstate == STATE_EXPECT_WANT) {
909 895484c8 2023-01-19 thomas ssize_t r;
911 895484c8 2023-01-19 thomas * If the client does not want to fetch
912 895484c8 2023-01-19 thomas * anything we should receive a flush
913 895484c8 2023-01-19 thomas * packet followed by EOF.
915 895484c8 2023-01-19 thomas r = read(infd, buf, sizeof(buf));
916 895484c8 2023-01-19 thomas if (r == -1) {
917 895484c8 2023-01-19 thomas err = got_error_from_errno("read");
918 895484c8 2023-01-19 thomas goto done;
920 895484c8 2023-01-19 thomas if (r == 0) /* EOF */
921 895484c8 2023-01-19 thomas goto done;
923 895484c8 2023-01-19 thomas /* Zero-length field followed by payload. */
924 895484c8 2023-01-19 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
925 895484c8 2023-01-19 thomas "unexpected flush packet received");
926 895484c8 2023-01-19 thomas goto done;
929 94a71055 2023-01-23 thomas if (curstate == STATE_EXPECT_WANT ||
930 94a71055 2023-01-23 thomas curstate == STATE_EXPECT_MORE_WANT ||
931 94a71055 2023-01-23 thomas curstate == STATE_EXPECT_HAVE) {
932 94a71055 2023-01-23 thomas err = forward_flushpkt(&ibuf);
934 94a71055 2023-01-23 thomas goto done;
936 3efd8e31 2022-10-23 thomas if (curstate == STATE_EXPECT_HAVE && !have_ack) {
937 3efd8e31 2022-10-23 thomas err = send_nak(outfd, chattygot);
939 3efd8e31 2022-10-23 thomas goto done;
941 3efd8e31 2022-10-23 thomas if (curstate == STATE_EXPECT_MORE_WANT)
942 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_HAVE;
944 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_DONE;
945 3efd8e31 2022-10-23 thomas } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
946 3efd8e31 2022-10-23 thomas if (curstate != STATE_EXPECT_WANT &&
947 3efd8e31 2022-10-23 thomas curstate != STATE_EXPECT_MORE_WANT) {
948 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
949 3efd8e31 2022-10-23 thomas "unexpected 'want' packet");
950 3efd8e31 2022-10-23 thomas goto done;
952 3efd8e31 2022-10-23 thomas err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
953 3efd8e31 2022-10-23 thomas curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
955 3efd8e31 2022-10-23 thomas goto done;
956 3efd8e31 2022-10-23 thomas if (curstate == STATE_EXPECT_WANT)
957 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_MORE_WANT;
958 3efd8e31 2022-10-23 thomas } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
959 6110f5ef 2023-01-19 thomas if (curstate != STATE_EXPECT_HAVE &&
960 6110f5ef 2023-01-19 thomas curstate != STATE_EXPECT_DONE) {
961 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
962 3efd8e31 2022-10-23 thomas "unexpected 'have' packet");
963 3efd8e31 2022-10-23 thomas goto done;
965 6110f5ef 2023-01-19 thomas if (curstate == STATE_EXPECT_HAVE) {
966 6110f5ef 2023-01-19 thomas err = recv_have(&have_ack, outfd, &ibuf,
967 6110f5ef 2023-01-19 thomas buf, n, chattygot);
969 6110f5ef 2023-01-19 thomas goto done;
970 6110f5ef 2023-01-19 thomas seen_have = 1;
971 6110f5ef 2023-01-19 thomas if (have_ack)
972 6110f5ef 2023-01-19 thomas curstate = STATE_EXPECT_DONE;
974 3efd8e31 2022-10-23 thomas } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
975 3efd8e31 2022-10-23 thomas if (curstate != STATE_EXPECT_HAVE &&
976 3efd8e31 2022-10-23 thomas curstate != STATE_EXPECT_DONE) {
977 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
978 3efd8e31 2022-10-23 thomas "unexpected 'done' packet");
979 3efd8e31 2022-10-23 thomas goto done;
981 3efd8e31 2022-10-23 thomas err = recv_done(&packfd, outfd, &ibuf, chattygot);
983 3efd8e31 2022-10-23 thomas goto done;
984 3efd8e31 2022-10-23 thomas curstate = STATE_DONE;
987 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_PACKET);
988 3efd8e31 2022-10-23 thomas goto done;
992 3efd8e31 2022-10-23 thomas if (!seen_have) {
993 3efd8e31 2022-10-23 thomas err = send_nak(outfd, chattygot);
995 3efd8e31 2022-10-23 thomas goto done;
998 3efd8e31 2022-10-23 thomas if (use_sidebands) {
999 3efd8e31 2022-10-23 thomas err = relay_progress_reports(&ibuf, outfd, chattygot);
1000 3efd8e31 2022-10-23 thomas if (err)
1001 3efd8e31 2022-10-23 thomas goto done;
1002 3efd8e31 2022-10-23 thomas pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
1004 3efd8e31 2022-10-23 thomas pack_chunksize = sizeof(buf);
1006 3efd8e31 2022-10-23 thomas for (;;) {
1007 89077ea1 2022-10-31 thomas ssize_t r;
1009 3efd8e31 2022-10-23 thomas r = read(packfd, use_sidebands ? &buf[1] : buf,
1010 3efd8e31 2022-10-23 thomas pack_chunksize);
1011 3efd8e31 2022-10-23 thomas if (r == -1) {
1012 3efd8e31 2022-10-23 thomas err = got_error_from_errno("read");
1014 3efd8e31 2022-10-23 thomas } else if (r == 0) {
1015 3efd8e31 2022-10-23 thomas err = got_pkt_flushpkt(outfd, chattygot);
1019 3efd8e31 2022-10-23 thomas if (use_sidebands) {
1020 3efd8e31 2022-10-23 thomas buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
1021 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
1022 3efd8e31 2022-10-23 thomas if (err)
1024 3efd8e31 2022-10-23 thomas } else {
1025 89077ea1 2022-10-31 thomas err = got_poll_write_full(outfd, buf, r);
1026 89077ea1 2022-10-31 thomas if (err) {
1027 89077ea1 2022-10-31 thomas if (err->code == GOT_ERR_EOF)
1028 89077ea1 2022-10-31 thomas err = NULL;
1034 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
1035 3efd8e31 2022-10-23 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
1036 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1037 3efd8e31 2022-10-23 thomas if (err)
1038 3efd8e31 2022-10-23 thomas echo_error(err, outfd, chattygot);
1039 3efd8e31 2022-10-23 thomas return err;
1042 3efd8e31 2022-10-23 thomas static const struct got_error *
1043 3efd8e31 2022-10-23 thomas parse_ref_update_line(char **common_capabilities, char **refname,
1044 3efd8e31 2022-10-23 thomas uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
1046 3efd8e31 2022-10-23 thomas const struct got_error *err;
1047 3efd8e31 2022-10-23 thomas char *old_id_str = NULL, *new_id_str = NULL;
1048 3efd8e31 2022-10-23 thomas char *client_capabilities = NULL;
1050 3efd8e31 2022-10-23 thomas *refname = NULL;
1052 3efd8e31 2022-10-23 thomas err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
1053 3efd8e31 2022-10-23 thomas refname, &client_capabilities, buf, len);
1054 3efd8e31 2022-10-23 thomas if (err)
1055 3efd8e31 2022-10-23 thomas return err;
1057 3efd8e31 2022-10-23 thomas if (!got_parse_sha1_digest(old_id, old_id_str) ||
1058 3efd8e31 2022-10-23 thomas !got_parse_sha1_digest(new_id, new_id_str)) {
1059 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
1060 3efd8e31 2022-10-23 thomas "ref-update with bad object ID");
1061 3efd8e31 2022-10-23 thomas goto done;
1063 3efd8e31 2022-10-23 thomas if (!got_ref_name_is_valid(*refname)) {
1064 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
1065 3efd8e31 2022-10-23 thomas "ref-update with bad reference name");
1066 3efd8e31 2022-10-23 thomas goto done;
1069 3efd8e31 2022-10-23 thomas if (client_capabilities) {
1070 3efd8e31 2022-10-23 thomas err = got_gitproto_match_capabilities(common_capabilities,
1071 3efd8e31 2022-10-23 thomas NULL, client_capabilities, write_capabilities,
1072 3efd8e31 2022-10-23 thomas nitems(write_capabilities));
1073 3efd8e31 2022-10-23 thomas if (err)
1074 3efd8e31 2022-10-23 thomas goto done;
1077 3efd8e31 2022-10-23 thomas free(old_id_str);
1078 3efd8e31 2022-10-23 thomas free(new_id_str);
1079 3efd8e31 2022-10-23 thomas free(client_capabilities);
1080 3efd8e31 2022-10-23 thomas if (err) {
1081 3efd8e31 2022-10-23 thomas free(*refname);
1082 3efd8e31 2022-10-23 thomas *refname = NULL;
1084 3efd8e31 2022-10-23 thomas return err;
1087 3efd8e31 2022-10-23 thomas static const struct got_error *
1088 3efd8e31 2022-10-23 thomas recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
1089 3efd8e31 2022-10-23 thomas char *buf, size_t len, int expect_capabilities, int chattygot)
1091 3efd8e31 2022-10-23 thomas const struct got_error *err;
1092 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
1093 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1094 3efd8e31 2022-10-23 thomas char *capabilities_str = NULL, *refname = NULL;
1095 3efd8e31 2022-10-23 thomas int done = 0;
1096 3efd8e31 2022-10-23 thomas struct imsg imsg;
1098 3efd8e31 2022-10-23 thomas memset(&iref, 0, sizeof(iref));
1099 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
1101 3efd8e31 2022-10-23 thomas err = parse_ref_update_line(&capabilities_str, &refname,
1102 3efd8e31 2022-10-23 thomas iref.old_id, iref.new_id, buf, len);
1103 3efd8e31 2022-10-23 thomas if (err)
1104 3efd8e31 2022-10-23 thomas return err;
1106 3efd8e31 2022-10-23 thomas if (capabilities_str) {
1107 3efd8e31 2022-10-23 thomas if (!expect_capabilities) {
1108 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
1109 3efd8e31 2022-10-23 thomas "unexpected capability announcement received");
1110 3efd8e31 2022-10-23 thomas goto done;
1112 3efd8e31 2022-10-23 thomas err = send_capabilities(NULL, report_status, capabilities_str,
1114 3efd8e31 2022-10-23 thomas if (err)
1115 3efd8e31 2022-10-23 thomas goto done;
1118 3efd8e31 2022-10-23 thomas iref.name_len = strlen(refname);
1119 3efd8e31 2022-10-23 thomas len = sizeof(iref) + iref.name_len;
1120 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1121 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
1122 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF_UPDATE");
1123 3efd8e31 2022-10-23 thomas goto done;
1126 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1127 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1128 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, iref.name_len) == -1)
1129 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1130 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1131 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
1133 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
1134 3efd8e31 2022-10-23 thomas if (err)
1135 3efd8e31 2022-10-23 thomas goto done;
1137 3efd8e31 2022-10-23 thomas /* Wait for ACK or an error. */
1138 3efd8e31 2022-10-23 thomas while (!done && err == NULL) {
1139 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1140 3efd8e31 2022-10-23 thomas if (err)
1142 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1143 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1144 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
1146 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ACK:
1147 3efd8e31 2022-10-23 thomas err = recv_ack(&imsg, iref.new_id);
1148 3efd8e31 2022-10-23 thomas if (err)
1150 3efd8e31 2022-10-23 thomas done = 1;
1152 3efd8e31 2022-10-23 thomas default:
1153 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1157 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1160 3efd8e31 2022-10-23 thomas free(capabilities_str);
1161 3efd8e31 2022-10-23 thomas free(refname);
1162 3efd8e31 2022-10-23 thomas return err;
1165 3efd8e31 2022-10-23 thomas static const struct got_error *
1166 3efd8e31 2022-10-23 thomas recv_packfile(struct imsg *imsg, int infd)
1168 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1169 3efd8e31 2022-10-23 thomas size_t datalen;
1170 3efd8e31 2022-10-23 thomas int packfd;
1171 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1172 3efd8e31 2022-10-23 thomas int pack_done = 0;
1174 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1175 3efd8e31 2022-10-23 thomas if (datalen != 0)
1176 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1178 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
1179 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1181 3efd8e31 2022-10-23 thomas packfd = imsg->fd;
1182 3efd8e31 2022-10-23 thomas while (!pack_done) {
1183 3efd8e31 2022-10-23 thomas ssize_t r = 0;
1185 3efd8e31 2022-10-23 thomas err = got_poll_fd(infd, POLLIN, 1);
1186 3efd8e31 2022-10-23 thomas if (err) {
1187 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_TIMEOUT)
1189 3efd8e31 2022-10-23 thomas err = NULL;
1190 3efd8e31 2022-10-23 thomas } else {
1191 3efd8e31 2022-10-23 thomas r = read(infd, buf, sizeof(buf));
1192 3efd8e31 2022-10-23 thomas if (r == -1) {
1193 3efd8e31 2022-10-23 thomas err = got_error_from_errno("read");
1196 3efd8e31 2022-10-23 thomas if (r == 0) {
1198 3efd8e31 2022-10-23 thomas * Git clients hang up their side of the
1199 3efd8e31 2022-10-23 thomas * connection after sending the pack file.
1201 3efd8e31 2022-10-23 thomas err = NULL;
1202 3efd8e31 2022-10-23 thomas pack_done = 1;
1207 3efd8e31 2022-10-23 thomas if (r == 0) {
1208 3efd8e31 2022-10-23 thomas /* Detect gotd(8) closing the pack pipe when done. */
1209 3efd8e31 2022-10-23 thomas err = got_poll_fd(packfd, POLLOUT, 1);
1210 3efd8e31 2022-10-23 thomas if (err) {
1211 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_EOF)
1213 3efd8e31 2022-10-23 thomas err = NULL;
1214 3efd8e31 2022-10-23 thomas pack_done = 1;
1216 3efd8e31 2022-10-23 thomas } else {
1217 3efd8e31 2022-10-23 thomas /* Write pack data and/or detect pipe being closed. */
1218 3efd8e31 2022-10-23 thomas err = got_poll_write_full(packfd, buf, r);
1219 3efd8e31 2022-10-23 thomas if (err) {
1220 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_EOF)
1221 3efd8e31 2022-10-23 thomas err = NULL;
1227 3efd8e31 2022-10-23 thomas close(packfd);
1228 3efd8e31 2022-10-23 thomas return err;
1231 3efd8e31 2022-10-23 thomas static const struct got_error *
1232 3efd8e31 2022-10-23 thomas report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1234 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1235 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_status istatus;
1236 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1237 3efd8e31 2022-10-23 thomas size_t datalen, len;
1238 3efd8e31 2022-10-23 thomas char *reason = NULL;
1240 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1241 3efd8e31 2022-10-23 thomas if (datalen < sizeof(istatus))
1242 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1243 3efd8e31 2022-10-23 thomas memcpy(&istatus, imsg->data, sizeof(istatus));
1244 3efd8e31 2022-10-23 thomas if (datalen != sizeof(istatus) + istatus.reason_len)
1245 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1247 fcbb06bf 2023-01-14 thomas reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1248 3efd8e31 2022-10-23 thomas if (reason == NULL) {
1249 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
1250 3efd8e31 2022-10-23 thomas goto done;
1253 3efd8e31 2022-10-23 thomas if (err == NULL)
1254 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "unpack ok\n");
1256 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1257 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf)) {
1258 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
1259 3efd8e31 2022-10-23 thomas goto done;
1262 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, len, chattygot);
1264 3efd8e31 2022-10-23 thomas free(reason);
1265 3efd8e31 2022-10-23 thomas return err;
1268 3efd8e31 2022-10-23 thomas static const struct got_error *
1269 3efd8e31 2022-10-23 thomas recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1271 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1272 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update_ok iok;
1273 3efd8e31 2022-10-23 thomas size_t datalen, len;
1274 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1275 3efd8e31 2022-10-23 thomas char *refname = NULL;
1277 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1278 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iok))
1279 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1280 3efd8e31 2022-10-23 thomas memcpy(&iok, imsg->data, sizeof(iok));
1281 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iok) + iok.name_len)
1282 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1284 3efd8e31 2022-10-23 thomas memcpy(&iok, imsg->data, sizeof(iok));
1286 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1287 3efd8e31 2022-10-23 thomas if (refname == NULL)
1288 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
1290 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1291 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf)) {
1292 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
1293 3efd8e31 2022-10-23 thomas goto done;
1296 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, len, chattygot);
1298 3efd8e31 2022-10-23 thomas free(refname);
1299 3efd8e31 2022-10-23 thomas return err;
1302 3efd8e31 2022-10-23 thomas static const struct got_error *
1303 3efd8e31 2022-10-23 thomas recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1305 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1306 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update_ng ing;
1307 3efd8e31 2022-10-23 thomas size_t datalen, len;
1308 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1309 3efd8e31 2022-10-23 thomas char *refname = NULL, *reason = NULL;
1311 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1312 3efd8e31 2022-10-23 thomas if (datalen < sizeof(ing))
1313 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1314 3efd8e31 2022-10-23 thomas memcpy(&ing, imsg->data, sizeof(ing));
1315 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1316 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1318 3efd8e31 2022-10-23 thomas memcpy(&ing, imsg->data, sizeof(ing));
1320 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1321 3efd8e31 2022-10-23 thomas if (refname == NULL)
1322 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
1324 fcbb06bf 2023-01-14 thomas reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1325 fcbb06bf 2023-01-14 thomas ing.reason_len);
1326 3efd8e31 2022-10-23 thomas if (reason == NULL) {
1327 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
1328 3efd8e31 2022-10-23 thomas goto done;
1331 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1332 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf)) {
1333 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
1334 3efd8e31 2022-10-23 thomas goto done;
1337 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, len, chattygot);
1339 3efd8e31 2022-10-23 thomas free(refname);
1340 3efd8e31 2022-10-23 thomas free(reason);
1341 3efd8e31 2022-10-23 thomas return err;
1344 3efd8e31 2022-10-23 thomas static const struct got_error *
1345 3efd8e31 2022-10-23 thomas serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1346 3efd8e31 2022-10-23 thomas int chattygot)
1348 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1349 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1350 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
1351 3efd8e31 2022-10-23 thomas enum protostate {
1352 3efd8e31 2022-10-23 thomas STATE_EXPECT_REF_UPDATE,
1353 3efd8e31 2022-10-23 thomas STATE_EXPECT_MORE_REF_UPDATES,
1354 3efd8e31 2022-10-23 thomas STATE_EXPECT_PACKFILE,
1355 3efd8e31 2022-10-23 thomas STATE_PACKFILE_RECEIVED,
1356 3efd8e31 2022-10-23 thomas STATE_REFS_UPDATED,
1358 3efd8e31 2022-10-23 thomas enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1359 3efd8e31 2022-10-23 thomas struct imsg imsg;
1360 3efd8e31 2022-10-23 thomas int report_status = 0;
1362 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, gotd_sock);
1363 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
1365 3efd8e31 2022-10-23 thomas err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1366 3efd8e31 2022-10-23 thomas if (err)
1367 3efd8e31 2022-10-23 thomas goto done;
1369 3efd8e31 2022-10-23 thomas while (curstate != STATE_EXPECT_PACKFILE) {
1371 3efd8e31 2022-10-23 thomas buf[0] = '\0';
1372 3efd8e31 2022-10-23 thomas err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1373 3efd8e31 2022-10-23 thomas if (err)
1374 8b56c8f4 2023-01-23 thomas goto done;
1375 3efd8e31 2022-10-23 thomas if (n == 0) {
1376 3efd8e31 2022-10-23 thomas if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1377 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
1378 3efd8e31 2022-10-23 thomas "unexpected flush packet received");
1379 3efd8e31 2022-10-23 thomas goto done;
1381 3efd8e31 2022-10-23 thomas err = forward_flushpkt(&ibuf);
1382 3efd8e31 2022-10-23 thomas if (err)
1383 3efd8e31 2022-10-23 thomas goto done;
1384 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_PACKFILE;
1385 3efd8e31 2022-10-23 thomas } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1386 3efd8e31 2022-10-23 thomas if (curstate != STATE_EXPECT_REF_UPDATE &&
1387 3efd8e31 2022-10-23 thomas curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1388 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
1389 3efd8e31 2022-10-23 thomas "unexpected ref-update packet");
1390 3efd8e31 2022-10-23 thomas goto done;
1392 3efd8e31 2022-10-23 thomas if (curstate == STATE_EXPECT_REF_UPDATE) {
1393 3efd8e31 2022-10-23 thomas err = recv_ref_update(&report_status,
1394 3efd8e31 2022-10-23 thomas outfd, &ibuf, buf, n, 1, chattygot);
1395 3efd8e31 2022-10-23 thomas } else {
1396 3efd8e31 2022-10-23 thomas err = recv_ref_update(NULL, outfd, &ibuf,
1397 3efd8e31 2022-10-23 thomas buf, n, 0, chattygot);
1399 3efd8e31 2022-10-23 thomas if (err)
1400 3efd8e31 2022-10-23 thomas goto done;
1401 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_MORE_REF_UPDATES;
1402 3efd8e31 2022-10-23 thomas } else {
1403 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_PACKET);
1404 3efd8e31 2022-10-23 thomas goto done;
1408 3efd8e31 2022-10-23 thomas while (curstate != STATE_PACKFILE_RECEIVED) {
1409 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1410 3efd8e31 2022-10-23 thomas if (err)
1411 3efd8e31 2022-10-23 thomas goto done;
1412 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1413 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1414 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
1415 3efd8e31 2022-10-23 thomas goto done;
1416 3efd8e31 2022-10-23 thomas case GOTD_IMSG_RECV_PACKFILE:
1417 3efd8e31 2022-10-23 thomas err = recv_packfile(&imsg, infd);
1418 3efd8e31 2022-10-23 thomas if (err) {
1419 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_EOF)
1420 3efd8e31 2022-10-23 thomas goto done;
1422 3efd8e31 2022-10-23 thomas * EOF is reported when the client hangs up,
1423 3efd8e31 2022-10-23 thomas * which can happen with Git clients.
1424 3efd8e31 2022-10-23 thomas * The socket should stay half-open so we
1425 3efd8e31 2022-10-23 thomas * can still send our reports if requested.
1427 3efd8e31 2022-10-23 thomas err = NULL;
1429 3efd8e31 2022-10-23 thomas curstate = STATE_PACKFILE_RECEIVED;
1431 3efd8e31 2022-10-23 thomas default:
1432 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1436 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1437 3efd8e31 2022-10-23 thomas if (err)
1438 3efd8e31 2022-10-23 thomas goto done;
1441 3efd8e31 2022-10-23 thomas while (curstate != STATE_REFS_UPDATED && err == NULL) {
1442 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1443 3efd8e31 2022-10-23 thomas if (err)
1445 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1446 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1447 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
1449 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_STATUS:
1450 3efd8e31 2022-10-23 thomas if (!report_status)
1452 3efd8e31 2022-10-23 thomas err = report_unpack_status(&imsg, outfd, chattygot);
1454 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE_OK:
1455 3efd8e31 2022-10-23 thomas if (!report_status)
1457 3efd8e31 2022-10-23 thomas err = recv_ref_update_ok(&imsg, outfd, chattygot);
1459 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE_NG:
1460 3efd8e31 2022-10-23 thomas if (!report_status)
1462 3efd8e31 2022-10-23 thomas err = recv_ref_update_ng(&imsg, outfd, chattygot);
1464 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REFS_UPDATED:
1465 3efd8e31 2022-10-23 thomas curstate = STATE_REFS_UPDATED;
1466 3efd8e31 2022-10-23 thomas err = got_pkt_flushpkt(outfd, chattygot);
1468 3efd8e31 2022-10-23 thomas default:
1469 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1473 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1476 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
1477 3efd8e31 2022-10-23 thomas if (err)
1478 3efd8e31 2022-10-23 thomas echo_error(err, outfd, chattygot);
1479 3efd8e31 2022-10-23 thomas return err;
1482 3efd8e31 2022-10-23 thomas const struct got_error *
1483 214d733a 2023-01-23 thomas got_serve(int infd, int outfd, const char *command, const char *repo_path,
1484 214d733a 2023-01-23 thomas int gotd_sock, int chattygot)
1486 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1488 3efd8e31 2022-10-23 thomas if (strcmp(command, GOT_SERVE_CMD_FETCH) == 0)
1489 3efd8e31 2022-10-23 thomas err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1490 3efd8e31 2022-10-23 thomas else if (strcmp(command, GOT_SERVE_CMD_SEND) == 0)
1491 8efb3dc0 2023-01-23 thomas err = serve_write(infd, outfd, gotd_sock, repo_path,
1492 8efb3dc0 2023-01-23 thomas chattygot);
1494 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_PACKET);
1496 3efd8e31 2022-10-23 thomas return err;