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 5822e79e 2023-02-23 op #include <sha2.h>
27 13b2bc37 2022-10-23 stsp #include <stdio.h>
28 13b2bc37 2022-10-23 stsp #include <stdint.h>
29 13b2bc37 2022-10-23 stsp #include <stdlib.h>
30 13b2bc37 2022-10-23 stsp #include <string.h>
31 13b2bc37 2022-10-23 stsp #include <imsg.h>
32 13b2bc37 2022-10-23 stsp #include <unistd.h>
34 13b2bc37 2022-10-23 stsp #include "got_error.h"
35 13b2bc37 2022-10-23 stsp #include "got_serve.h"
36 13b2bc37 2022-10-23 stsp #include "got_path.h"
37 13b2bc37 2022-10-23 stsp #include "got_version.h"
38 13b2bc37 2022-10-23 stsp #include "got_reference.h"
39 87a3ab84 2023-02-23 op #include "got_object.h"
41 13b2bc37 2022-10-23 stsp #include "got_lib_pkt.h"
42 13b2bc37 2022-10-23 stsp #include "got_lib_dial.h"
43 13b2bc37 2022-10-23 stsp #include "got_lib_gitproto.h"
44 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
45 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
47 13b2bc37 2022-10-23 stsp #include "gotd.h"
49 13b2bc37 2022-10-23 stsp #ifndef nitems
50 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 13b2bc37 2022-10-23 stsp static const struct got_capability read_capabilities[] = {
54 13b2bc37 2022-10-23 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
55 13b2bc37 2022-10-23 stsp { GOT_CAPA_OFS_DELTA, NULL },
56 13b2bc37 2022-10-23 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
59 13b2bc37 2022-10-23 stsp static const struct got_capability write_capabilities[] = {
60 13b2bc37 2022-10-23 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
61 13b2bc37 2022-10-23 stsp { GOT_CAPA_OFS_DELTA, NULL },
62 13b2bc37 2022-10-23 stsp { GOT_CAPA_REPORT_STATUS, NULL },
63 13b2bc37 2022-10-23 stsp { GOT_CAPA_NO_THIN, NULL },
64 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 append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
69 13b2bc37 2022-10-23 stsp uint8_t *buf, size_t bufsize)
71 13b2bc37 2022-10-23 stsp struct got_capability capa[nitems(read_capabilities) + 1];
72 13b2bc37 2022-10-23 stsp size_t ncapa;
74 13b2bc37 2022-10-23 stsp memcpy(&capa, read_capabilities, sizeof(read_capabilities));
75 13b2bc37 2022-10-23 stsp if (symrefstr) {
76 13b2bc37 2022-10-23 stsp capa[nitems(read_capabilities)].key = "symref";
77 13b2bc37 2022-10-23 stsp capa[nitems(read_capabilities)].value = symrefstr;
78 13b2bc37 2022-10-23 stsp ncapa = nitems(capa);
80 13b2bc37 2022-10-23 stsp ncapa = nitems(read_capabilities);
82 13b2bc37 2022-10-23 stsp return got_gitproto_append_capabilities(capalen, buf, len,
83 13b2bc37 2022-10-23 stsp bufsize, capa, ncapa);
86 13b2bc37 2022-10-23 stsp static const struct got_error *
87 13b2bc37 2022-10-23 stsp send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
88 13b2bc37 2022-10-23 stsp int client_is_reading, const char *symrefstr, int chattygot)
90 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
91 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
92 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
93 13b2bc37 2022-10-23 stsp size_t len, capalen = 0;
95 758dc042 2022-11-06 stsp if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
96 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
98 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
99 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf))
100 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
102 13b2bc37 2022-10-23 stsp if (send_capabilities) {
103 13b2bc37 2022-10-23 stsp if (client_is_reading) {
104 13b2bc37 2022-10-23 stsp err = append_read_capabilities(&capalen, len,
105 13b2bc37 2022-10-23 stsp symrefstr, buf, sizeof(buf));
107 13b2bc37 2022-10-23 stsp err = got_gitproto_append_capabilities(&capalen,
108 13b2bc37 2022-10-23 stsp buf, len, sizeof(buf), write_capabilities,
109 13b2bc37 2022-10-23 stsp nitems(write_capabilities));
112 13b2bc37 2022-10-23 stsp return err;
113 13b2bc37 2022-10-23 stsp len += capalen;
116 13b2bc37 2022-10-23 stsp if (len + 1 >= sizeof(buf))
117 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
118 13b2bc37 2022-10-23 stsp buf[len] = '\n';
120 13b2bc37 2022-10-23 stsp buf[len] = '\0';
122 13b2bc37 2022-10-23 stsp return got_pkt_writepkt(outfd, buf, len, chattygot);
125 13b2bc37 2022-10-23 stsp static const struct got_error *
126 2ff9f081 2022-11-08 stsp send_zero_refs(int outfd, int client_is_reading, int chattygot)
128 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
129 53e553e8 2023-02-19 op const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
130 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
131 13b2bc37 2022-10-23 stsp size_t len, capalen = 0;
133 53e553e8 2023-02-19 op len = strlcpy(buf, line, sizeof(buf));
134 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf))
135 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
137 2ff9f081 2022-11-08 stsp if (client_is_reading) {
138 2ff9f081 2022-11-08 stsp err = got_gitproto_append_capabilities(&capalen, buf, len,
139 2ff9f081 2022-11-08 stsp sizeof(buf), read_capabilities, nitems(read_capabilities));
141 2ff9f081 2022-11-08 stsp return err;
143 2ff9f081 2022-11-08 stsp err = got_gitproto_append_capabilities(&capalen, buf, len,
144 2ff9f081 2022-11-08 stsp sizeof(buf), write_capabilities,
145 2ff9f081 2022-11-08 stsp nitems(write_capabilities));
147 2ff9f081 2022-11-08 stsp return err;
150 c6b5adb8 2022-11-07 stsp return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
153 13b2bc37 2022-10-23 stsp static void
154 13b2bc37 2022-10-23 stsp echo_error(const struct got_error *err, int outfd, int chattygot)
156 13b2bc37 2022-10-23 stsp char buf[4 + GOT_ERR_MAX_MSG_SIZE];
157 13b2bc37 2022-10-23 stsp size_t len;
160 13b2bc37 2022-10-23 stsp * Echo the error to the client on a pkt-line.
161 13b2bc37 2022-10-23 stsp * The client should then terminate its session.
163 13b2bc37 2022-10-23 stsp buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
164 13b2bc37 2022-10-23 stsp len = strlcat(buf, err->msg, sizeof(buf));
165 d10629e6 2022-12-08 stsp got_pkt_writepkt(outfd, buf, len, chattygot);
168 13b2bc37 2022-10-23 stsp static const struct got_error *
169 13b2bc37 2022-10-23 stsp announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
170 13b2bc37 2022-10-23 stsp const char *repo_path, int chattygot)
172 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
173 13b2bc37 2022-10-23 stsp struct imsg imsg;
174 13b2bc37 2022-10-23 stsp size_t datalen;
175 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs lsref;
176 13b2bc37 2022-10-23 stsp struct gotd_imsg_reflist ireflist;
177 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref iref;
178 13b2bc37 2022-10-23 stsp struct gotd_imsg_symref isymref;
179 13b2bc37 2022-10-23 stsp size_t nrefs = 0;
180 13b2bc37 2022-10-23 stsp int have_nrefs = 0, sent_capabilities = 0;
181 13b2bc37 2022-10-23 stsp char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
182 13b2bc37 2022-10-23 stsp char *refname = NULL;
184 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
185 13b2bc37 2022-10-23 stsp memset(&lsref, 0, sizeof(lsref));
187 13b2bc37 2022-10-23 stsp if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
188 13b2bc37 2022-10-23 stsp sizeof(lsref.repo_name))
189 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
190 13b2bc37 2022-10-23 stsp lsref.client_is_reading = client_is_reading;
192 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
193 13b2bc37 2022-10-23 stsp &lsref, sizeof(lsref)) == -1)
194 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose LIST_REFS");
196 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
198 13b2bc37 2022-10-23 stsp return err;
200 13b2bc37 2022-10-23 stsp while (!have_nrefs || nrefs > 0) {
201 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
204 13b2bc37 2022-10-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
205 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
206 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
207 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
209 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REFLIST:
210 13b2bc37 2022-10-23 stsp if (have_nrefs || nrefs > 0) {
211 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
214 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireflist)) {
215 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
218 13b2bc37 2022-10-23 stsp memcpy(&ireflist, imsg.data, sizeof(ireflist));
219 13b2bc37 2022-10-23 stsp nrefs = ireflist.nrefs;
220 13b2bc37 2022-10-23 stsp have_nrefs = 1;
221 13b2bc37 2022-10-23 stsp if (nrefs == 0)
222 2ff9f081 2022-11-08 stsp err = send_zero_refs(outfd, client_is_reading,
223 2ff9f081 2022-11-08 stsp chattygot);
225 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF:
226 13b2bc37 2022-10-23 stsp if (!have_nrefs || nrefs == 0) {
227 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
230 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref)) {
231 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
234 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg.data, sizeof(iref));
235 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len) {
236 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
239 00b3e9ae 2023-01-11 op refname = strndup(imsg.data + sizeof(iref),
240 00b3e9ae 2023-01-11 op iref.name_len);
241 13b2bc37 2022-10-23 stsp if (refname == NULL) {
242 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
245 13b2bc37 2022-10-23 stsp err = send_ref(outfd, iref.id, refname,
246 13b2bc37 2022-10-23 stsp !sent_capabilities, client_is_reading,
247 13b2bc37 2022-10-23 stsp NULL, chattygot);
248 13b2bc37 2022-10-23 stsp free(refname);
249 13b2bc37 2022-10-23 stsp refname = NULL;
252 13b2bc37 2022-10-23 stsp sent_capabilities = 1;
253 13b2bc37 2022-10-23 stsp if (nrefs > 0)
256 13b2bc37 2022-10-23 stsp case GOTD_IMSG_SYMREF:
257 13b2bc37 2022-10-23 stsp if (!have_nrefs || nrefs == 0) {
258 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
261 13b2bc37 2022-10-23 stsp if (datalen < sizeof(isymref)) {
262 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
265 13b2bc37 2022-10-23 stsp memcpy(&isymref, imsg.data, sizeof(isymref));
266 13b2bc37 2022-10-23 stsp if (datalen != sizeof(isymref) + isymref.name_len +
267 13b2bc37 2022-10-23 stsp isymref.target_len) {
268 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
273 13b2bc37 2022-10-23 stsp * For now, we only announce one symbolic ref,
274 13b2bc37 2022-10-23 stsp * as part of our capability advertisement.
276 13b2bc37 2022-10-23 stsp if (sent_capabilities || symrefstr != NULL ||
277 13b2bc37 2022-10-23 stsp symrefname != NULL || symreftarget != NULL)
280 00b3e9ae 2023-01-11 op symrefname = strndup(imsg.data + sizeof(isymref),
281 00b3e9ae 2023-01-11 op isymref.name_len);
282 13b2bc37 2022-10-23 stsp if (symrefname == NULL) {
283 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
287 00b3e9ae 2023-01-11 op symreftarget = strndup(
288 00b3e9ae 2023-01-11 op imsg.data + sizeof(isymref) + isymref.name_len,
289 00b3e9ae 2023-01-11 op isymref.target_len);
290 13b2bc37 2022-10-23 stsp if (symreftarget == NULL) {
291 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
295 13b2bc37 2022-10-23 stsp if (asprintf(&symrefstr, "%s:%s", symrefname,
296 13b2bc37 2022-10-23 stsp symreftarget) == -1) {
297 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
300 13b2bc37 2022-10-23 stsp err = send_ref(outfd, isymref.target_id, symrefname,
301 13b2bc37 2022-10-23 stsp !sent_capabilities, client_is_reading, symrefstr,
302 13b2bc37 2022-10-23 stsp chattygot);
303 13b2bc37 2022-10-23 stsp free(refname);
304 13b2bc37 2022-10-23 stsp refname = NULL;
307 13b2bc37 2022-10-23 stsp sent_capabilities = 1;
308 13b2bc37 2022-10-23 stsp if (nrefs > 0)
312 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
316 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
319 13b2bc37 2022-10-23 stsp err = got_pkt_flushpkt(outfd, chattygot);
323 13b2bc37 2022-10-23 stsp free(symrefstr);
324 13b2bc37 2022-10-23 stsp free(symrefname);
325 13b2bc37 2022-10-23 stsp free(symreftarget);
326 13b2bc37 2022-10-23 stsp return err;
329 13b2bc37 2022-10-23 stsp static const struct got_error *
330 13b2bc37 2022-10-23 stsp parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
332 13b2bc37 2022-10-23 stsp const struct got_error *err;
333 13b2bc37 2022-10-23 stsp char *id_str = NULL, *client_capabilities = NULL;
335 13b2bc37 2022-10-23 stsp err = got_gitproto_parse_want_line(&id_str,
336 13b2bc37 2022-10-23 stsp &client_capabilities, buf, len);
338 13b2bc37 2022-10-23 stsp return err;
340 87a3ab84 2023-02-23 op if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
341 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
342 13b2bc37 2022-10-23 stsp "want-line with bad object ID");
346 13b2bc37 2022-10-23 stsp if (client_capabilities) {
347 13b2bc37 2022-10-23 stsp err = got_gitproto_match_capabilities(common_capabilities,
348 13b2bc37 2022-10-23 stsp NULL, client_capabilities, read_capabilities,
349 13b2bc37 2022-10-23 stsp nitems(read_capabilities));
354 13b2bc37 2022-10-23 stsp free(id_str);
355 13b2bc37 2022-10-23 stsp free(client_capabilities);
356 13b2bc37 2022-10-23 stsp return err;
359 13b2bc37 2022-10-23 stsp static const struct got_error *
360 13b2bc37 2022-10-23 stsp parse_have_line(uint8_t *id, char *buf, size_t len)
362 13b2bc37 2022-10-23 stsp const struct got_error *err;
363 13b2bc37 2022-10-23 stsp char *id_str = NULL;
365 13b2bc37 2022-10-23 stsp err = got_gitproto_parse_have_line(&id_str, buf, len);
367 13b2bc37 2022-10-23 stsp return err;
369 87a3ab84 2023-02-23 op if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
370 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
371 13b2bc37 2022-10-23 stsp "have-line with bad object ID");
375 13b2bc37 2022-10-23 stsp free(id_str);
376 13b2bc37 2022-10-23 stsp return err;
379 13b2bc37 2022-10-23 stsp static const struct got_error *
380 13b2bc37 2022-10-23 stsp send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
382 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
383 13b2bc37 2022-10-23 stsp struct gotd_imsg_capability icapa;
384 13b2bc37 2022-10-23 stsp size_t len;
385 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
387 13b2bc37 2022-10-23 stsp memset(&icapa, 0, sizeof(icapa));
389 13b2bc37 2022-10-23 stsp icapa.key_len = strlen(capa->key);
390 13b2bc37 2022-10-23 stsp len = sizeof(icapa) + icapa.key_len;
391 13b2bc37 2022-10-23 stsp if (capa->value) {
392 13b2bc37 2022-10-23 stsp icapa.value_len = strlen(capa->value);
393 13b2bc37 2022-10-23 stsp len += icapa.value_len;
396 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
397 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
398 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create CAPABILITY");
399 13b2bc37 2022-10-23 stsp return err;
402 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
403 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add CAPABILITY");
404 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
405 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add CAPABILITY");
406 13b2bc37 2022-10-23 stsp if (capa->value) {
407 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
408 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add CAPABILITY");
411 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
412 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
414 13b2bc37 2022-10-23 stsp return NULL;
417 13b2bc37 2022-10-23 stsp static const struct got_error *
418 13b2bc37 2022-10-23 stsp send_capabilities(int *use_sidebands, int *report_status,
419 13b2bc37 2022-10-23 stsp char *capabilities_str, struct imsgbuf *ibuf)
421 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
422 13b2bc37 2022-10-23 stsp struct gotd_imsg_capabilities icapas;
423 13b2bc37 2022-10-23 stsp struct got_capability *capa = NULL;
424 13b2bc37 2022-10-23 stsp size_t ncapa, i;
426 13b2bc37 2022-10-23 stsp err = got_gitproto_split_capabilities_str(&capa, &ncapa,
427 13b2bc37 2022-10-23 stsp capabilities_str);
429 13b2bc37 2022-10-23 stsp return err;
431 13b2bc37 2022-10-23 stsp icapas.ncapabilities = ncapa;
432 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
433 13b2bc37 2022-10-23 stsp &icapas, sizeof(icapas)) == -1) {
434 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
438 13b2bc37 2022-10-23 stsp for (i = 0; i < ncapa; i++) {
439 13b2bc37 2022-10-23 stsp err = send_capability(&capa[i], ibuf);
442 13b2bc37 2022-10-23 stsp if (use_sidebands &&
443 13b2bc37 2022-10-23 stsp strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
444 13b2bc37 2022-10-23 stsp *use_sidebands = 1;
445 13b2bc37 2022-10-23 stsp if (report_status &&
446 13b2bc37 2022-10-23 stsp strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
447 13b2bc37 2022-10-23 stsp *report_status = 1;
450 13b2bc37 2022-10-23 stsp free(capa);
451 13b2bc37 2022-10-23 stsp return err;
454 13b2bc37 2022-10-23 stsp static const struct got_error *
455 13b2bc37 2022-10-23 stsp forward_flushpkt(struct imsgbuf *ibuf)
457 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
458 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose FLUSH");
460 13b2bc37 2022-10-23 stsp return gotd_imsg_flush(ibuf);
463 13b2bc37 2022-10-23 stsp static const struct got_error *
464 13b2bc37 2022-10-23 stsp recv_ack(struct imsg *imsg, uint8_t *expected_id)
466 13b2bc37 2022-10-23 stsp struct gotd_imsg_ack iack;
467 13b2bc37 2022-10-23 stsp size_t datalen;
469 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
470 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iack))
471 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
473 13b2bc37 2022-10-23 stsp memcpy(&iack, imsg->data, sizeof(iack));
474 13b2bc37 2022-10-23 stsp if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
475 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
477 13b2bc37 2022-10-23 stsp return NULL;
480 13b2bc37 2022-10-23 stsp static const struct got_error *
481 13b2bc37 2022-10-23 stsp recv_nak(struct imsg *imsg, uint8_t *expected_id)
483 13b2bc37 2022-10-23 stsp struct gotd_imsg_ack inak;
484 13b2bc37 2022-10-23 stsp size_t datalen;
486 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
487 13b2bc37 2022-10-23 stsp if (datalen != sizeof(inak))
488 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
490 13b2bc37 2022-10-23 stsp memcpy(&inak, imsg->data, sizeof(inak));
491 13b2bc37 2022-10-23 stsp if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
492 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
494 13b2bc37 2022-10-23 stsp return NULL;
498 13b2bc37 2022-10-23 stsp static const struct got_error *
499 13b2bc37 2022-10-23 stsp recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
500 13b2bc37 2022-10-23 stsp char *buf, size_t len, int expect_capabilities, int chattygot)
502 13b2bc37 2022-10-23 stsp const struct got_error *err;
503 13b2bc37 2022-10-23 stsp struct gotd_imsg_want iwant;
504 13b2bc37 2022-10-23 stsp char *capabilities_str;
505 13b2bc37 2022-10-23 stsp int done = 0;
506 13b2bc37 2022-10-23 stsp struct imsg imsg;
508 13b2bc37 2022-10-23 stsp memset(&iwant, 0, sizeof(iwant));
509 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
511 13b2bc37 2022-10-23 stsp err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
513 13b2bc37 2022-10-23 stsp return err;
515 13b2bc37 2022-10-23 stsp if (capabilities_str) {
516 13b2bc37 2022-10-23 stsp if (!expect_capabilities) {
517 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
518 13b2bc37 2022-10-23 stsp "unexpected capability announcement received");
521 13b2bc37 2022-10-23 stsp err = send_capabilities(use_sidebands, NULL, capabilities_str,
528 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
529 13b2bc37 2022-10-23 stsp &iwant, sizeof(iwant)) == -1) {
530 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose WANT");
534 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
539 13b2bc37 2022-10-23 stsp * Wait for an ACK, or an error in case the desired object
540 13b2bc37 2022-10-23 stsp * does not exist.
542 13b2bc37 2022-10-23 stsp while (!done && err == NULL) {
543 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
546 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
547 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
548 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
550 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ACK:
551 13b2bc37 2022-10-23 stsp err = recv_ack(&imsg, iwant.object_id);
557 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
561 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
564 13b2bc37 2022-10-23 stsp free(capabilities_str);
565 13b2bc37 2022-10-23 stsp return err;
568 13b2bc37 2022-10-23 stsp static const struct got_error *
569 13b2bc37 2022-10-23 stsp send_ack(int outfd, uint8_t *id, int chattygot)
571 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
572 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
575 758dc042 2022-11-06 stsp if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
576 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID);
578 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
579 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf))
580 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
582 13b2bc37 2022-10-23 stsp return got_pkt_writepkt(outfd, buf, len, chattygot);
585 13b2bc37 2022-10-23 stsp static const struct got_error *
586 13b2bc37 2022-10-23 stsp send_nak(int outfd, int chattygot)
588 13b2bc37 2022-10-23 stsp char buf[5];
591 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "NAK\n");
592 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf))
593 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
595 13b2bc37 2022-10-23 stsp return got_pkt_writepkt(outfd, buf, len, chattygot);
598 13b2bc37 2022-10-23 stsp static const struct got_error *
599 13b2bc37 2022-10-23 stsp recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
600 13b2bc37 2022-10-23 stsp size_t len, int chattygot)
602 13b2bc37 2022-10-23 stsp const struct got_error *err;
603 13b2bc37 2022-10-23 stsp struct gotd_imsg_have ihave;
604 13b2bc37 2022-10-23 stsp int done = 0;
605 13b2bc37 2022-10-23 stsp struct imsg imsg;
607 13b2bc37 2022-10-23 stsp memset(&ihave, 0, sizeof(ihave));
608 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
610 13b2bc37 2022-10-23 stsp err = parse_have_line(ihave.object_id, buf, len);
612 13b2bc37 2022-10-23 stsp return err;
614 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
615 13b2bc37 2022-10-23 stsp &ihave, sizeof(ihave)) == -1)
616 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose HAVE");
618 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
620 13b2bc37 2022-10-23 stsp return err;
623 13b2bc37 2022-10-23 stsp * Wait for an ACK or a NAK, indicating whether a common
624 13b2bc37 2022-10-23 stsp * commit object has been found.
626 13b2bc37 2022-10-23 stsp while (!done && err == NULL) {
627 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
629 13b2bc37 2022-10-23 stsp return err;
630 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
631 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
632 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
634 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ACK:
635 13b2bc37 2022-10-23 stsp err = recv_ack(&imsg, ihave.object_id);
638 13b2bc37 2022-10-23 stsp if (!*have_ack) {
639 13b2bc37 2022-10-23 stsp err = send_ack(outfd, ihave.object_id,
640 13b2bc37 2022-10-23 stsp chattygot);
642 13b2bc37 2022-10-23 stsp return err;
643 13b2bc37 2022-10-23 stsp *have_ack = 1;
647 13b2bc37 2022-10-23 stsp case GOTD_IMSG_NAK:
648 13b2bc37 2022-10-23 stsp err = recv_nak(&imsg, ihave.object_id);
654 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
658 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
661 13b2bc37 2022-10-23 stsp return err;
664 13b2bc37 2022-10-23 stsp static const struct got_error *
665 13b2bc37 2022-10-23 stsp recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
667 13b2bc37 2022-10-23 stsp const struct got_error *err;
668 13b2bc37 2022-10-23 stsp struct imsg imsg;
670 13b2bc37 2022-10-23 stsp *packfd = -1;
672 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
673 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose DONE");
675 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
677 13b2bc37 2022-10-23 stsp return err;
679 86769de8 2022-10-28 stsp while (*packfd == -1 && err == NULL) {
680 86769de8 2022-10-28 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
684 86769de8 2022-10-28 stsp switch (imsg.hdr.type) {
685 86769de8 2022-10-28 stsp case GOTD_IMSG_ERROR:
686 86769de8 2022-10-28 stsp err = gotd_imsg_recv_error(NULL, &imsg);
688 86769de8 2022-10-28 stsp case GOTD_IMSG_PACKFILE_PIPE:
689 86769de8 2022-10-28 stsp if (imsg.fd != -1)
690 86769de8 2022-10-28 stsp *packfd = imsg.fd;
692 86769de8 2022-10-28 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
695 86769de8 2022-10-28 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
699 86769de8 2022-10-28 stsp imsg_free(&imsg);
702 13b2bc37 2022-10-23 stsp return err;
705 13b2bc37 2022-10-23 stsp static const struct got_error *
706 13b2bc37 2022-10-23 stsp relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
708 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
709 13b2bc37 2022-10-23 stsp int pack_starting = 0;
710 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_progress iprog;
711 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
712 13b2bc37 2022-10-23 stsp struct imsg imsg;
713 13b2bc37 2022-10-23 stsp size_t datalen;
714 13b2bc37 2022-10-23 stsp int p_deltify = 0, n;
715 13b2bc37 2022-10-23 stsp const char *eol = "\r";
717 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
719 13b2bc37 2022-10-23 stsp while (!pack_starting && err == NULL) {
720 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
724 13b2bc37 2022-10-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
725 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
726 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
727 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
729 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_READY:
730 13b2bc37 2022-10-23 stsp eol = "\n";
731 13b2bc37 2022-10-23 stsp pack_starting = 1;
732 13b2bc37 2022-10-23 stsp /* fallthrough */
733 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PROGRESS:
734 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iprog)) {
735 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
738 13b2bc37 2022-10-23 stsp memcpy(&iprog, imsg.data, sizeof(iprog));
739 13b2bc37 2022-10-23 stsp if (iprog.nobj_total > 0) {
740 13b2bc37 2022-10-23 stsp p_deltify = (iprog.nobj_deltify * 100) /
741 13b2bc37 2022-10-23 stsp iprog.nobj_total;
743 13b2bc37 2022-10-23 stsp buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
744 13b2bc37 2022-10-23 stsp n = snprintf(&buf[1], sizeof(buf) - 1,
745 13b2bc37 2022-10-23 stsp "%d commits colored, "
746 13b2bc37 2022-10-23 stsp "%d objects found, "
747 13b2bc37 2022-10-23 stsp "deltify %d%%%s",
748 758dc042 2022-11-06 stsp iprog.ncolored,
749 758dc042 2022-11-06 stsp iprog.nfound,
750 13b2bc37 2022-10-23 stsp p_deltify, eol);
751 13b2bc37 2022-10-23 stsp if (n >= sizeof(buf) - 1)
753 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
756 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
760 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
763 13b2bc37 2022-10-23 stsp return err;
766 13b2bc37 2022-10-23 stsp static const struct got_error *
767 13b2bc37 2022-10-23 stsp serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
768 13b2bc37 2022-10-23 stsp int chattygot)
770 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
771 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
772 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
773 13b2bc37 2022-10-23 stsp enum protostate {
774 13b2bc37 2022-10-23 stsp STATE_EXPECT_WANT,
775 13b2bc37 2022-10-23 stsp STATE_EXPECT_MORE_WANT,
776 13b2bc37 2022-10-23 stsp STATE_EXPECT_HAVE,
777 13b2bc37 2022-10-23 stsp STATE_EXPECT_DONE,
778 13b2bc37 2022-10-23 stsp STATE_DONE,
780 13b2bc37 2022-10-23 stsp enum protostate curstate = STATE_EXPECT_WANT;
781 13b2bc37 2022-10-23 stsp int have_ack = 0, use_sidebands = 0, seen_have = 0;
782 13b2bc37 2022-10-23 stsp int packfd = -1;
783 13b2bc37 2022-10-23 stsp size_t pack_chunksize;
785 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, gotd_sock);
787 13b2bc37 2022-10-23 stsp err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
791 13b2bc37 2022-10-23 stsp while (curstate != STATE_DONE) {
793 13b2bc37 2022-10-23 stsp buf[0] = '\0';
794 13b2bc37 2022-10-23 stsp err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
797 13b2bc37 2022-10-23 stsp if (n == 0) {
798 fecfd5bc 2023-01-18 stsp if (curstate != STATE_EXPECT_WANT &&
799 fecfd5bc 2023-01-18 stsp curstate != STATE_EXPECT_MORE_WANT &&
800 f9550d47 2023-01-18 stsp curstate != STATE_EXPECT_HAVE &&
801 f9550d47 2023-01-18 stsp curstate != STATE_EXPECT_DONE) {
802 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
803 13b2bc37 2022-10-23 stsp "unexpected flush packet received");
807 fecfd5bc 2023-01-18 stsp if (curstate == STATE_EXPECT_WANT) {
810 fecfd5bc 2023-01-18 stsp * If the client does not want to fetch
811 fecfd5bc 2023-01-18 stsp * anything we should receive a flush
812 fecfd5bc 2023-01-18 stsp * packet followed by EOF.
814 fecfd5bc 2023-01-18 stsp r = read(infd, buf, sizeof(buf));
815 fecfd5bc 2023-01-18 stsp if (r == -1) {
816 fecfd5bc 2023-01-18 stsp err = got_error_from_errno("read");
819 fecfd5bc 2023-01-18 stsp if (r == 0) /* EOF */
822 fecfd5bc 2023-01-18 stsp /* Zero-length field followed by payload. */
823 fecfd5bc 2023-01-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
824 fecfd5bc 2023-01-18 stsp "unexpected flush packet received");
828 f91b5c43 2023-01-21 stsp if (curstate == STATE_EXPECT_WANT ||
829 f91b5c43 2023-01-21 stsp curstate == STATE_EXPECT_MORE_WANT ||
830 f91b5c43 2023-01-21 stsp curstate == STATE_EXPECT_HAVE) {
831 f91b5c43 2023-01-21 stsp err = forward_flushpkt(&ibuf);
835 13b2bc37 2022-10-23 stsp if (curstate == STATE_EXPECT_HAVE && !have_ack) {
836 13b2bc37 2022-10-23 stsp err = send_nak(outfd, chattygot);
840 13b2bc37 2022-10-23 stsp if (curstate == STATE_EXPECT_MORE_WANT)
841 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_HAVE;
843 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_DONE;
844 13b2bc37 2022-10-23 stsp } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
845 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_WANT &&
846 13b2bc37 2022-10-23 stsp curstate != STATE_EXPECT_MORE_WANT) {
847 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
848 13b2bc37 2022-10-23 stsp "unexpected 'want' packet");
851 13b2bc37 2022-10-23 stsp err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
852 13b2bc37 2022-10-23 stsp curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
855 13b2bc37 2022-10-23 stsp if (curstate == STATE_EXPECT_WANT)
856 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_MORE_WANT;
857 13b2bc37 2022-10-23 stsp } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
858 f9550d47 2023-01-18 stsp if (curstate != STATE_EXPECT_HAVE &&
859 f9550d47 2023-01-18 stsp curstate != STATE_EXPECT_DONE) {
860 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
861 13b2bc37 2022-10-23 stsp "unexpected 'have' packet");
864 f9550d47 2023-01-18 stsp if (curstate == STATE_EXPECT_HAVE) {
865 f9550d47 2023-01-18 stsp err = recv_have(&have_ack, outfd, &ibuf,
866 f9550d47 2023-01-18 stsp buf, n, chattygot);
869 f9550d47 2023-01-18 stsp seen_have = 1;
871 13b2bc37 2022-10-23 stsp } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
872 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_HAVE &&
873 13b2bc37 2022-10-23 stsp curstate != STATE_EXPECT_DONE) {
874 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
875 13b2bc37 2022-10-23 stsp "unexpected 'done' packet");
878 13b2bc37 2022-10-23 stsp err = recv_done(&packfd, outfd, &ibuf, chattygot);
881 13b2bc37 2022-10-23 stsp curstate = STATE_DONE;
884 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_PACKET);
889 13b2bc37 2022-10-23 stsp if (!seen_have) {
890 13b2bc37 2022-10-23 stsp err = send_nak(outfd, chattygot);
895 13b2bc37 2022-10-23 stsp if (use_sidebands) {
896 13b2bc37 2022-10-23 stsp err = relay_progress_reports(&ibuf, outfd, chattygot);
899 13b2bc37 2022-10-23 stsp pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
901 13b2bc37 2022-10-23 stsp pack_chunksize = sizeof(buf);
906 13b2bc37 2022-10-23 stsp r = read(packfd, use_sidebands ? &buf[1] : buf,
907 13b2bc37 2022-10-23 stsp pack_chunksize);
908 13b2bc37 2022-10-23 stsp if (r == -1) {
909 13b2bc37 2022-10-23 stsp err = got_error_from_errno("read");
911 13b2bc37 2022-10-23 stsp } else if (r == 0) {
912 13b2bc37 2022-10-23 stsp err = got_pkt_flushpkt(outfd, chattygot);
916 13b2bc37 2022-10-23 stsp if (use_sidebands) {
917 13b2bc37 2022-10-23 stsp buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
918 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
922 1c9d898d 2022-10-31 stsp err = got_poll_write_full(outfd, buf, r);
924 1c9d898d 2022-10-31 stsp if (err->code == GOT_ERR_EOF)
925 1c9d898d 2022-10-31 stsp err = NULL;
931 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
932 13b2bc37 2022-10-23 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
933 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
935 13b2bc37 2022-10-23 stsp echo_error(err, outfd, chattygot);
936 13b2bc37 2022-10-23 stsp return err;
939 13b2bc37 2022-10-23 stsp static const struct got_error *
940 13b2bc37 2022-10-23 stsp parse_ref_update_line(char **common_capabilities, char **refname,
941 13b2bc37 2022-10-23 stsp uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
943 13b2bc37 2022-10-23 stsp const struct got_error *err;
944 13b2bc37 2022-10-23 stsp char *old_id_str = NULL, *new_id_str = NULL;
945 13b2bc37 2022-10-23 stsp char *client_capabilities = NULL;
947 13b2bc37 2022-10-23 stsp *refname = NULL;
949 13b2bc37 2022-10-23 stsp err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
950 13b2bc37 2022-10-23 stsp refname, &client_capabilities, buf, len);
952 13b2bc37 2022-10-23 stsp return err;
954 87a3ab84 2023-02-23 op if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
955 87a3ab84 2023-02-23 op !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
956 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
957 13b2bc37 2022-10-23 stsp "ref-update with bad object ID");
960 13b2bc37 2022-10-23 stsp if (!got_ref_name_is_valid(*refname)) {
961 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
962 13b2bc37 2022-10-23 stsp "ref-update with bad reference name");
966 13b2bc37 2022-10-23 stsp if (client_capabilities) {
967 13b2bc37 2022-10-23 stsp err = got_gitproto_match_capabilities(common_capabilities,
968 13b2bc37 2022-10-23 stsp NULL, client_capabilities, write_capabilities,
969 13b2bc37 2022-10-23 stsp nitems(write_capabilities));
974 13b2bc37 2022-10-23 stsp free(old_id_str);
975 13b2bc37 2022-10-23 stsp free(new_id_str);
976 13b2bc37 2022-10-23 stsp free(client_capabilities);
978 13b2bc37 2022-10-23 stsp free(*refname);
979 13b2bc37 2022-10-23 stsp *refname = NULL;
981 13b2bc37 2022-10-23 stsp return err;
984 13b2bc37 2022-10-23 stsp static const struct got_error *
985 13b2bc37 2022-10-23 stsp recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
986 13b2bc37 2022-10-23 stsp char *buf, size_t len, int expect_capabilities, int chattygot)
988 13b2bc37 2022-10-23 stsp const struct got_error *err;
989 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
990 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
991 13b2bc37 2022-10-23 stsp char *capabilities_str = NULL, *refname = NULL;
992 13b2bc37 2022-10-23 stsp int done = 0;
993 13b2bc37 2022-10-23 stsp struct imsg imsg;
995 13b2bc37 2022-10-23 stsp memset(&iref, 0, sizeof(iref));
996 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
998 13b2bc37 2022-10-23 stsp err = parse_ref_update_line(&capabilities_str, &refname,
999 13b2bc37 2022-10-23 stsp iref.old_id, iref.new_id, buf, len);
1001 13b2bc37 2022-10-23 stsp return err;
1003 13b2bc37 2022-10-23 stsp if (capabilities_str) {
1004 13b2bc37 2022-10-23 stsp if (!expect_capabilities) {
1005 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1006 13b2bc37 2022-10-23 stsp "unexpected capability announcement received");
1007 13b2bc37 2022-10-23 stsp goto done;
1009 13b2bc37 2022-10-23 stsp err = send_capabilities(NULL, report_status, capabilities_str,
1012 13b2bc37 2022-10-23 stsp goto done;
1015 13b2bc37 2022-10-23 stsp iref.name_len = strlen(refname);
1016 13b2bc37 2022-10-23 stsp len = sizeof(iref) + iref.name_len;
1017 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1018 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
1019 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF_UPDATE");
1020 13b2bc37 2022-10-23 stsp goto done;
1023 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1024 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1025 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iref.name_len) == -1)
1026 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1027 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
1028 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
1030 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
1032 13b2bc37 2022-10-23 stsp goto done;
1034 13b2bc37 2022-10-23 stsp /* Wait for ACK or an error. */
1035 13b2bc37 2022-10-23 stsp while (!done && err == NULL) {
1036 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1039 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1040 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1041 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
1043 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ACK:
1044 13b2bc37 2022-10-23 stsp err = recv_ack(&imsg, iref.new_id);
1050 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1054 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1057 13b2bc37 2022-10-23 stsp free(capabilities_str);
1058 13b2bc37 2022-10-23 stsp free(refname);
1059 13b2bc37 2022-10-23 stsp return err;
1062 13b2bc37 2022-10-23 stsp static const struct got_error *
1063 13b2bc37 2022-10-23 stsp recv_packfile(struct imsg *imsg, int infd)
1065 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1066 13b2bc37 2022-10-23 stsp size_t datalen;
1067 13b2bc37 2022-10-23 stsp int packfd;
1068 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1069 13b2bc37 2022-10-23 stsp int pack_done = 0;
1071 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1072 13b2bc37 2022-10-23 stsp if (datalen != 0)
1073 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1075 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
1076 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1078 13b2bc37 2022-10-23 stsp packfd = imsg->fd;
1079 13b2bc37 2022-10-23 stsp while (!pack_done) {
1080 13b2bc37 2022-10-23 stsp ssize_t r = 0;
1082 13b2bc37 2022-10-23 stsp err = got_poll_fd(infd, POLLIN, 1);
1083 13b2bc37 2022-10-23 stsp if (err) {
1084 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_TIMEOUT)
1086 13b2bc37 2022-10-23 stsp err = NULL;
1088 13b2bc37 2022-10-23 stsp r = read(infd, buf, sizeof(buf));
1089 13b2bc37 2022-10-23 stsp if (r == -1) {
1090 13b2bc37 2022-10-23 stsp err = got_error_from_errno("read");
1093 13b2bc37 2022-10-23 stsp if (r == 0) {
1095 13b2bc37 2022-10-23 stsp * Git clients hang up their side of the
1096 13b2bc37 2022-10-23 stsp * connection after sending the pack file.
1098 13b2bc37 2022-10-23 stsp err = NULL;
1099 13b2bc37 2022-10-23 stsp pack_done = 1;
1104 13b2bc37 2022-10-23 stsp if (r == 0) {
1105 13b2bc37 2022-10-23 stsp /* Detect gotd(8) closing the pack pipe when done. */
1106 13b2bc37 2022-10-23 stsp err = got_poll_fd(packfd, POLLOUT, 1);
1107 13b2bc37 2022-10-23 stsp if (err) {
1108 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF)
1110 13b2bc37 2022-10-23 stsp err = NULL;
1111 13b2bc37 2022-10-23 stsp pack_done = 1;
1114 13b2bc37 2022-10-23 stsp /* Write pack data and/or detect pipe being closed. */
1115 13b2bc37 2022-10-23 stsp err = got_poll_write_full(packfd, buf, r);
1116 13b2bc37 2022-10-23 stsp if (err) {
1117 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF)
1118 13b2bc37 2022-10-23 stsp err = NULL;
1124 13b2bc37 2022-10-23 stsp close(packfd);
1125 13b2bc37 2022-10-23 stsp return err;
1128 13b2bc37 2022-10-23 stsp static const struct got_error *
1129 13b2bc37 2022-10-23 stsp report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1131 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1132 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_status istatus;
1133 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1134 13b2bc37 2022-10-23 stsp size_t datalen, len;
1135 13b2bc37 2022-10-23 stsp char *reason = NULL;
1137 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1138 13b2bc37 2022-10-23 stsp if (datalen < sizeof(istatus))
1139 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1140 13b2bc37 2022-10-23 stsp memcpy(&istatus, imsg->data, sizeof(istatus));
1141 13b2bc37 2022-10-23 stsp if (datalen != sizeof(istatus) + istatus.reason_len)
1142 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1144 00b3e9ae 2023-01-11 op reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1145 13b2bc37 2022-10-23 stsp if (reason == NULL) {
1146 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
1147 13b2bc37 2022-10-23 stsp goto done;
1150 13b2bc37 2022-10-23 stsp if (err == NULL)
1151 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "unpack ok\n");
1153 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1154 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf)) {
1155 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1156 13b2bc37 2022-10-23 stsp goto done;
1159 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, len, chattygot);
1161 13b2bc37 2022-10-23 stsp free(reason);
1162 13b2bc37 2022-10-23 stsp return err;
1165 13b2bc37 2022-10-23 stsp static const struct got_error *
1166 13b2bc37 2022-10-23 stsp recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1168 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1169 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update_ok iok;
1170 13b2bc37 2022-10-23 stsp size_t datalen, len;
1171 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1172 13b2bc37 2022-10-23 stsp char *refname = NULL;
1174 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1175 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iok))
1176 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1177 13b2bc37 2022-10-23 stsp memcpy(&iok, imsg->data, sizeof(iok));
1178 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iok) + iok.name_len)
1179 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1181 13b2bc37 2022-10-23 stsp memcpy(&iok, imsg->data, sizeof(iok));
1183 00b3e9ae 2023-01-11 op refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1184 13b2bc37 2022-10-23 stsp if (refname == NULL)
1185 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
1187 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1188 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf)) {
1189 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1190 13b2bc37 2022-10-23 stsp goto done;
1193 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, len, chattygot);
1195 13b2bc37 2022-10-23 stsp free(refname);
1196 13b2bc37 2022-10-23 stsp return err;
1199 13b2bc37 2022-10-23 stsp static const struct got_error *
1200 13b2bc37 2022-10-23 stsp recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1202 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1203 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update_ng ing;
1204 13b2bc37 2022-10-23 stsp size_t datalen, len;
1205 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1206 13b2bc37 2022-10-23 stsp char *refname = NULL, *reason = NULL;
1208 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1209 13b2bc37 2022-10-23 stsp if (datalen < sizeof(ing))
1210 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1211 13b2bc37 2022-10-23 stsp memcpy(&ing, imsg->data, sizeof(ing));
1212 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1213 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1215 13b2bc37 2022-10-23 stsp memcpy(&ing, imsg->data, sizeof(ing));
1217 00b3e9ae 2023-01-11 op refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1218 13b2bc37 2022-10-23 stsp if (refname == NULL)
1219 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
1221 00b3e9ae 2023-01-11 op reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1222 00b3e9ae 2023-01-11 op ing.reason_len);
1223 13b2bc37 2022-10-23 stsp if (reason == NULL) {
1224 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
1225 13b2bc37 2022-10-23 stsp goto done;
1228 13b2bc37 2022-10-23 stsp len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1229 13b2bc37 2022-10-23 stsp if (len >= sizeof(buf)) {
1230 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
1231 13b2bc37 2022-10-23 stsp goto done;
1234 13b2bc37 2022-10-23 stsp err = got_pkt_writepkt(outfd, buf, len, chattygot);
1236 13b2bc37 2022-10-23 stsp free(refname);
1237 13b2bc37 2022-10-23 stsp free(reason);
1238 13b2bc37 2022-10-23 stsp return err;
1241 13b2bc37 2022-10-23 stsp static const struct got_error *
1242 13b2bc37 2022-10-23 stsp serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1243 13b2bc37 2022-10-23 stsp int chattygot)
1245 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1246 13b2bc37 2022-10-23 stsp char buf[GOT_PKT_MAX];
1247 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
1248 13b2bc37 2022-10-23 stsp enum protostate {
1249 13b2bc37 2022-10-23 stsp STATE_EXPECT_REF_UPDATE,
1250 13b2bc37 2022-10-23 stsp STATE_EXPECT_MORE_REF_UPDATES,
1251 13b2bc37 2022-10-23 stsp STATE_EXPECT_PACKFILE,
1252 13b2bc37 2022-10-23 stsp STATE_PACKFILE_RECEIVED,
1253 13b2bc37 2022-10-23 stsp STATE_REFS_UPDATED,
1255 13b2bc37 2022-10-23 stsp enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1256 13b2bc37 2022-10-23 stsp struct imsg imsg;
1257 13b2bc37 2022-10-23 stsp int report_status = 0;
1259 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, gotd_sock);
1260 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
1262 13b2bc37 2022-10-23 stsp err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1264 13b2bc37 2022-10-23 stsp goto done;
1266 13b2bc37 2022-10-23 stsp while (curstate != STATE_EXPECT_PACKFILE) {
1268 13b2bc37 2022-10-23 stsp buf[0] = '\0';
1269 13b2bc37 2022-10-23 stsp err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1271 f3dfebfc 2023-01-23 stsp goto done;
1272 13b2bc37 2022-10-23 stsp if (n == 0) {
1273 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1274 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1275 13b2bc37 2022-10-23 stsp "unexpected flush packet received");
1276 13b2bc37 2022-10-23 stsp goto done;
1278 13b2bc37 2022-10-23 stsp err = forward_flushpkt(&ibuf);
1280 13b2bc37 2022-10-23 stsp goto done;
1281 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_PACKFILE;
1282 13b2bc37 2022-10-23 stsp } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1283 13b2bc37 2022-10-23 stsp if (curstate != STATE_EXPECT_REF_UPDATE &&
1284 13b2bc37 2022-10-23 stsp curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1285 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1286 13b2bc37 2022-10-23 stsp "unexpected ref-update packet");
1287 13b2bc37 2022-10-23 stsp goto done;
1289 13b2bc37 2022-10-23 stsp if (curstate == STATE_EXPECT_REF_UPDATE) {
1290 13b2bc37 2022-10-23 stsp err = recv_ref_update(&report_status,
1291 13b2bc37 2022-10-23 stsp outfd, &ibuf, buf, n, 1, chattygot);
1293 13b2bc37 2022-10-23 stsp err = recv_ref_update(NULL, outfd, &ibuf,
1294 13b2bc37 2022-10-23 stsp buf, n, 0, chattygot);
1297 13b2bc37 2022-10-23 stsp goto done;
1298 13b2bc37 2022-10-23 stsp curstate = STATE_EXPECT_MORE_REF_UPDATES;
1300 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_PACKET);
1301 13b2bc37 2022-10-23 stsp goto done;
1305 13b2bc37 2022-10-23 stsp while (curstate != STATE_PACKFILE_RECEIVED) {
1306 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1308 13b2bc37 2022-10-23 stsp goto done;
1309 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1310 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1311 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
1312 13b2bc37 2022-10-23 stsp goto done;
1313 7fec5f4a 2022-10-28 stsp case GOTD_IMSG_PACKFILE_PIPE:
1314 13b2bc37 2022-10-23 stsp err = recv_packfile(&imsg, infd);
1315 13b2bc37 2022-10-23 stsp if (err) {
1316 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF)
1317 13b2bc37 2022-10-23 stsp goto done;
1319 13b2bc37 2022-10-23 stsp * EOF is reported when the client hangs up,
1320 13b2bc37 2022-10-23 stsp * which can happen with Git clients.
1321 13b2bc37 2022-10-23 stsp * The socket should stay half-open so we
1322 13b2bc37 2022-10-23 stsp * can still send our reports if requested.
1324 13b2bc37 2022-10-23 stsp err = NULL;
1326 13b2bc37 2022-10-23 stsp curstate = STATE_PACKFILE_RECEIVED;
1329 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1333 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1335 13b2bc37 2022-10-23 stsp goto done;
1338 13b2bc37 2022-10-23 stsp while (curstate != STATE_REFS_UPDATED && err == NULL) {
1339 13b2bc37 2022-10-23 stsp err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1342 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1343 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1344 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(NULL, &imsg);
1346 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_STATUS:
1347 13b2bc37 2022-10-23 stsp if (!report_status)
1349 13b2bc37 2022-10-23 stsp err = report_unpack_status(&imsg, outfd, chattygot);
1351 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE_OK:
1352 13b2bc37 2022-10-23 stsp if (!report_status)
1354 13b2bc37 2022-10-23 stsp err = recv_ref_update_ok(&imsg, outfd, chattygot);
1356 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE_NG:
1357 13b2bc37 2022-10-23 stsp if (!report_status)
1359 13b2bc37 2022-10-23 stsp err = recv_ref_update_ng(&imsg, outfd, chattygot);
1361 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REFS_UPDATED:
1362 13b2bc37 2022-10-23 stsp curstate = STATE_REFS_UPDATED;
1363 13b2bc37 2022-10-23 stsp err = got_pkt_flushpkt(outfd, chattygot);
1366 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1370 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1373 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1375 13b2bc37 2022-10-23 stsp echo_error(err, outfd, chattygot);
1376 13b2bc37 2022-10-23 stsp return err;
1379 13b2bc37 2022-10-23 stsp const struct got_error *
1380 9aeaf23a 2023-01-22 op got_serve(int infd, int outfd, const char *command, const char *repo_path,
1381 9aeaf23a 2023-01-22 op int gotd_sock, int chattygot)
1383 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1385 1eb38992 2023-04-14 stsp if (strcmp(command, GOT_DIAL_CMD_FETCH) == 0)
1386 13b2bc37 2022-10-23 stsp err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1387 1eb38992 2023-04-14 stsp else if (strcmp(command, GOT_DIAL_CMD_SEND) == 0)
1388 c808f450 2023-01-22 op err = serve_write(infd, outfd, gotd_sock, repo_path,
1391 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_PACKET);
1393 13b2bc37 2022-10-23 stsp return err;