Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
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.
7 3efd8e31 2022-10-23 thomas *
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.
15 3efd8e31 2022-10-23 thomas */
16 4efc8dcb 2023-08-29 thomas
17 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
18 3efd8e31 2022-10-23 thomas
19 3efd8e31 2022-10-23 thomas #include <sys/types.h>
20 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
21 3efd8e31 2022-10-23 thomas #include <sys/uio.h>
22 3efd8e31 2022-10-23 thomas
23 3efd8e31 2022-10-23 thomas #include <errno.h>
24 3efd8e31 2022-10-23 thomas #include <event.h>
25 3efd8e31 2022-10-23 thomas #include <poll.h>
26 3efd8e31 2022-10-23 thomas #include <limits.h>
27 3efd8e31 2022-10-23 thomas #include <stdio.h>
28 3efd8e31 2022-10-23 thomas #include <stdint.h>
29 3efd8e31 2022-10-23 thomas #include <stdlib.h>
30 3efd8e31 2022-10-23 thomas #include <string.h>
31 3efd8e31 2022-10-23 thomas #include <imsg.h>
32 3efd8e31 2022-10-23 thomas #include <unistd.h>
33 3efd8e31 2022-10-23 thomas
34 3efd8e31 2022-10-23 thomas #include "got_error.h"
35 3efd8e31 2022-10-23 thomas #include "got_serve.h"
36 3efd8e31 2022-10-23 thomas #include "got_path.h"
37 3efd8e31 2022-10-23 thomas #include "got_version.h"
38 3efd8e31 2022-10-23 thomas #include "got_reference.h"
39 c8ae092d 2023-02-23 thomas #include "got_object.h"
40 3efd8e31 2022-10-23 thomas
41 3efd8e31 2022-10-23 thomas #include "got_lib_pkt.h"
42 3efd8e31 2022-10-23 thomas #include "got_lib_dial.h"
43 3efd8e31 2022-10-23 thomas #include "got_lib_gitproto.h"
44 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
45 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
46 3efd8e31 2022-10-23 thomas
47 3efd8e31 2022-10-23 thomas #include "gotd.h"
48 3efd8e31 2022-10-23 thomas
49 3efd8e31 2022-10-23 thomas #ifndef nitems
50 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 3efd8e31 2022-10-23 thomas #endif
52 3efd8e31 2022-10-23 thomas
53 3efd8e31 2022-10-23 thomas static const struct got_capability read_capabilities[] = {
54 3efd8e31 2022-10-23 thomas { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
55 3efd8e31 2022-10-23 thomas { GOT_CAPA_OFS_DELTA, NULL },
56 3efd8e31 2022-10-23 thomas { GOT_CAPA_SIDE_BAND_64K, NULL },
57 3efd8e31 2022-10-23 thomas };
58 3efd8e31 2022-10-23 thomas
59 3efd8e31 2022-10-23 thomas static const struct got_capability write_capabilities[] = {
60 3efd8e31 2022-10-23 thomas { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
61 3efd8e31 2022-10-23 thomas { GOT_CAPA_OFS_DELTA, NULL },
62 3efd8e31 2022-10-23 thomas { GOT_CAPA_REPORT_STATUS, NULL },
63 3efd8e31 2022-10-23 thomas { GOT_CAPA_NO_THIN, NULL },
64 3efd8e31 2022-10-23 thomas { GOT_CAPA_DELETE_REFS, NULL },
65 3efd8e31 2022-10-23 thomas };
66 3efd8e31 2022-10-23 thomas
67 3efd8e31 2022-10-23 thomas static const struct got_error *
68 3efd8e31 2022-10-23 thomas append_read_capabilities(size_t *capalen, size_t len, const char *symrefstr,
69 3efd8e31 2022-10-23 thomas uint8_t *buf, size_t bufsize)
70 3efd8e31 2022-10-23 thomas {
71 3efd8e31 2022-10-23 thomas struct got_capability capa[nitems(read_capabilities) + 1];
72 3efd8e31 2022-10-23 thomas size_t ncapa;
73 3efd8e31 2022-10-23 thomas
74 3efd8e31 2022-10-23 thomas memcpy(&capa, read_capabilities, sizeof(read_capabilities));
75 3efd8e31 2022-10-23 thomas if (symrefstr) {
76 3efd8e31 2022-10-23 thomas capa[nitems(read_capabilities)].key = "symref";
77 3efd8e31 2022-10-23 thomas capa[nitems(read_capabilities)].value = symrefstr;
78 3efd8e31 2022-10-23 thomas ncapa = nitems(capa);
79 3efd8e31 2022-10-23 thomas } else
80 3efd8e31 2022-10-23 thomas ncapa = nitems(read_capabilities);
81 3efd8e31 2022-10-23 thomas
82 3efd8e31 2022-10-23 thomas return got_gitproto_append_capabilities(capalen, buf, len,
83 3efd8e31 2022-10-23 thomas bufsize, capa, ncapa);
84 3efd8e31 2022-10-23 thomas }
85 3efd8e31 2022-10-23 thomas
86 3efd8e31 2022-10-23 thomas static const struct got_error *
87 3efd8e31 2022-10-23 thomas send_ref(int outfd, uint8_t *id, const char *refname, int send_capabilities,
88 3efd8e31 2022-10-23 thomas int client_is_reading, const char *symrefstr, int chattygot)
89 3efd8e31 2022-10-23 thomas {
90 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
91 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
92 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
93 3efd8e31 2022-10-23 thomas size_t len, capalen = 0;
94 3efd8e31 2022-10-23 thomas
95 946e0798 2022-11-06 thomas if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
96 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
97 3efd8e31 2022-10-23 thomas
98 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "%s %s", hex, refname);
99 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf))
100 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
101 3efd8e31 2022-10-23 thomas
102 3efd8e31 2022-10-23 thomas if (send_capabilities) {
103 3efd8e31 2022-10-23 thomas if (client_is_reading) {
104 3efd8e31 2022-10-23 thomas err = append_read_capabilities(&capalen, len,
105 3efd8e31 2022-10-23 thomas symrefstr, buf, sizeof(buf));
106 3efd8e31 2022-10-23 thomas } else {
107 3efd8e31 2022-10-23 thomas err = got_gitproto_append_capabilities(&capalen,
108 3efd8e31 2022-10-23 thomas buf, len, sizeof(buf), write_capabilities,
109 3efd8e31 2022-10-23 thomas nitems(write_capabilities));
110 3efd8e31 2022-10-23 thomas }
111 3efd8e31 2022-10-23 thomas if (err)
112 3efd8e31 2022-10-23 thomas return err;
113 3efd8e31 2022-10-23 thomas len += capalen;
114 3efd8e31 2022-10-23 thomas }
115 3efd8e31 2022-10-23 thomas
116 3efd8e31 2022-10-23 thomas if (len + 1 >= sizeof(buf))
117 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
118 3efd8e31 2022-10-23 thomas buf[len] = '\n';
119 3efd8e31 2022-10-23 thomas len++;
120 3efd8e31 2022-10-23 thomas buf[len] = '\0';
121 3efd8e31 2022-10-23 thomas
122 3efd8e31 2022-10-23 thomas return got_pkt_writepkt(outfd, buf, len, chattygot);
123 3efd8e31 2022-10-23 thomas }
124 3efd8e31 2022-10-23 thomas
125 3efd8e31 2022-10-23 thomas static const struct got_error *
126 2a0fb198 2022-11-08 thomas send_zero_refs(int outfd, int client_is_reading, int chattygot)
127 3efd8e31 2022-10-23 thomas {
128 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
129 4c4f6843 2023-02-20 thomas const char *line = GOT_SHA1_STRING_ZERO " capabilities^{}";
130 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
131 3efd8e31 2022-10-23 thomas size_t len, capalen = 0;
132 3efd8e31 2022-10-23 thomas
133 4c4f6843 2023-02-20 thomas len = strlcpy(buf, line, sizeof(buf));
134 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf))
135 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
136 3efd8e31 2022-10-23 thomas
137 2a0fb198 2022-11-08 thomas if (client_is_reading) {
138 2a0fb198 2022-11-08 thomas err = got_gitproto_append_capabilities(&capalen, buf, len,
139 2a0fb198 2022-11-08 thomas sizeof(buf), read_capabilities, nitems(read_capabilities));
140 2a0fb198 2022-11-08 thomas if (err)
141 2a0fb198 2022-11-08 thomas return err;
142 2a0fb198 2022-11-08 thomas } else {
143 2a0fb198 2022-11-08 thomas err = got_gitproto_append_capabilities(&capalen, buf, len,
144 2a0fb198 2022-11-08 thomas sizeof(buf), write_capabilities,
145 2a0fb198 2022-11-08 thomas nitems(write_capabilities));
146 2a0fb198 2022-11-08 thomas if (err)
147 2a0fb198 2022-11-08 thomas return err;
148 2a0fb198 2022-11-08 thomas }
149 3efd8e31 2022-10-23 thomas
150 ea1f0585 2022-11-07 thomas return got_pkt_writepkt(outfd, buf, len + capalen, chattygot);
151 3efd8e31 2022-10-23 thomas }
152 3efd8e31 2022-10-23 thomas
153 3efd8e31 2022-10-23 thomas static void
154 3efd8e31 2022-10-23 thomas echo_error(const struct got_error *err, int outfd, int chattygot)
155 3efd8e31 2022-10-23 thomas {
156 3efd8e31 2022-10-23 thomas char buf[4 + GOT_ERR_MAX_MSG_SIZE];
157 3efd8e31 2022-10-23 thomas size_t len;
158 3efd8e31 2022-10-23 thomas
159 3efd8e31 2022-10-23 thomas /*
160 3efd8e31 2022-10-23 thomas * Echo the error to the client on a pkt-line.
161 3efd8e31 2022-10-23 thomas * The client should then terminate its session.
162 3efd8e31 2022-10-23 thomas */
163 3efd8e31 2022-10-23 thomas buf[0] = 'E'; buf[1] = 'R'; buf[2] = 'R'; buf[3] = ' '; buf[4] = '\0';
164 3efd8e31 2022-10-23 thomas len = strlcat(buf, err->msg, sizeof(buf));
165 911f5cd5 2022-12-08 thomas got_pkt_writepkt(outfd, buf, len, chattygot);
166 3efd8e31 2022-10-23 thomas }
167 3efd8e31 2022-10-23 thomas
168 3efd8e31 2022-10-23 thomas static const struct got_error *
169 3efd8e31 2022-10-23 thomas announce_refs(int outfd, struct imsgbuf *ibuf, int client_is_reading,
170 3efd8e31 2022-10-23 thomas const char *repo_path, int chattygot)
171 3efd8e31 2022-10-23 thomas {
172 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
173 3efd8e31 2022-10-23 thomas struct imsg imsg;
174 3efd8e31 2022-10-23 thomas size_t datalen;
175 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs lsref;
176 3efd8e31 2022-10-23 thomas struct gotd_imsg_reflist ireflist;
177 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref iref;
178 3efd8e31 2022-10-23 thomas struct gotd_imsg_symref isymref;
179 3efd8e31 2022-10-23 thomas size_t nrefs = 0;
180 3efd8e31 2022-10-23 thomas int have_nrefs = 0, sent_capabilities = 0;
181 3efd8e31 2022-10-23 thomas char *symrefname = NULL, *symreftarget = NULL, *symrefstr = NULL;
182 3efd8e31 2022-10-23 thomas char *refname = NULL;
183 3efd8e31 2022-10-23 thomas
184 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
185 3efd8e31 2022-10-23 thomas memset(&lsref, 0, sizeof(lsref));
186 3efd8e31 2022-10-23 thomas
187 3efd8e31 2022-10-23 thomas if (strlcpy(lsref.repo_name, repo_path, sizeof(lsref.repo_name)) >=
188 3efd8e31 2022-10-23 thomas sizeof(lsref.repo_name))
189 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
190 3efd8e31 2022-10-23 thomas lsref.client_is_reading = client_is_reading;
191 0df2f4da 2023-01-27 thomas
192 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_LIST_REFS, 0, 0, -1,
193 3efd8e31 2022-10-23 thomas &lsref, sizeof(lsref)) == -1)
194 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose LIST_REFS");
195 3efd8e31 2022-10-23 thomas
196 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
197 3efd8e31 2022-10-23 thomas if (err)
198 3efd8e31 2022-10-23 thomas return err;
199 3efd8e31 2022-10-23 thomas
200 3efd8e31 2022-10-23 thomas while (!have_nrefs || nrefs > 0) {
201 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
202 3efd8e31 2022-10-23 thomas if (err)
203 3efd8e31 2022-10-23 thomas goto done;
204 3efd8e31 2022-10-23 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
205 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
206 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
207 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
208 3efd8e31 2022-10-23 thomas goto done;
209 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REFLIST:
210 3efd8e31 2022-10-23 thomas if (have_nrefs || nrefs > 0) {
211 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
212 3efd8e31 2022-10-23 thomas goto done;
213 3efd8e31 2022-10-23 thomas }
214 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireflist)) {
215 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
216 3efd8e31 2022-10-23 thomas goto done;
217 3efd8e31 2022-10-23 thomas }
218 3efd8e31 2022-10-23 thomas memcpy(&ireflist, imsg.data, sizeof(ireflist));
219 3efd8e31 2022-10-23 thomas nrefs = ireflist.nrefs;
220 3efd8e31 2022-10-23 thomas have_nrefs = 1;
221 3efd8e31 2022-10-23 thomas if (nrefs == 0)
222 2a0fb198 2022-11-08 thomas err = send_zero_refs(outfd, client_is_reading,
223 2a0fb198 2022-11-08 thomas chattygot);
224 3efd8e31 2022-10-23 thomas break;
225 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF:
226 3efd8e31 2022-10-23 thomas if (!have_nrefs || nrefs == 0) {
227 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
228 3efd8e31 2022-10-23 thomas goto done;
229 3efd8e31 2022-10-23 thomas }
230 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref)) {
231 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
232 3efd8e31 2022-10-23 thomas goto done;
233 3efd8e31 2022-10-23 thomas }
234 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg.data, sizeof(iref));
235 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iref) + iref.name_len) {
236 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
237 3efd8e31 2022-10-23 thomas goto done;
238 3efd8e31 2022-10-23 thomas }
239 fcbb06bf 2023-01-14 thomas refname = strndup(imsg.data + sizeof(iref),
240 fcbb06bf 2023-01-14 thomas iref.name_len);
241 3efd8e31 2022-10-23 thomas if (refname == NULL) {
242 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
243 3efd8e31 2022-10-23 thomas goto done;
244 3efd8e31 2022-10-23 thomas }
245 3efd8e31 2022-10-23 thomas err = send_ref(outfd, iref.id, refname,
246 3efd8e31 2022-10-23 thomas !sent_capabilities, client_is_reading,
247 3efd8e31 2022-10-23 thomas NULL, chattygot);
248 3efd8e31 2022-10-23 thomas free(refname);
249 3efd8e31 2022-10-23 thomas refname = NULL;
250 3efd8e31 2022-10-23 thomas if (err)
251 3efd8e31 2022-10-23 thomas goto done;
252 3efd8e31 2022-10-23 thomas sent_capabilities = 1;
253 3efd8e31 2022-10-23 thomas if (nrefs > 0)
254 3efd8e31 2022-10-23 thomas nrefs--;
255 3efd8e31 2022-10-23 thomas break;
256 3efd8e31 2022-10-23 thomas case GOTD_IMSG_SYMREF:
257 3efd8e31 2022-10-23 thomas if (!have_nrefs || nrefs == 0) {
258 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
259 3efd8e31 2022-10-23 thomas goto done;
260 3efd8e31 2022-10-23 thomas }
261 3efd8e31 2022-10-23 thomas if (datalen < sizeof(isymref)) {
262 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
263 3efd8e31 2022-10-23 thomas goto done;
264 3efd8e31 2022-10-23 thomas }
265 3efd8e31 2022-10-23 thomas memcpy(&isymref, imsg.data, sizeof(isymref));
266 3efd8e31 2022-10-23 thomas if (datalen != sizeof(isymref) + isymref.name_len +
267 3efd8e31 2022-10-23 thomas isymref.target_len) {
268 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
269 3efd8e31 2022-10-23 thomas goto done;
270 3efd8e31 2022-10-23 thomas }
271 3efd8e31 2022-10-23 thomas
272 3efd8e31 2022-10-23 thomas /*
273 3efd8e31 2022-10-23 thomas * For now, we only announce one symbolic ref,
274 3efd8e31 2022-10-23 thomas * as part of our capability advertisement.
275 3efd8e31 2022-10-23 thomas */
276 3efd8e31 2022-10-23 thomas if (sent_capabilities || symrefstr != NULL ||
277 3efd8e31 2022-10-23 thomas symrefname != NULL || symreftarget != NULL)
278 3efd8e31 2022-10-23 thomas break;
279 3efd8e31 2022-10-23 thomas
280 fcbb06bf 2023-01-14 thomas symrefname = strndup(imsg.data + sizeof(isymref),
281 fcbb06bf 2023-01-14 thomas isymref.name_len);
282 3efd8e31 2022-10-23 thomas if (symrefname == NULL) {
283 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
284 3efd8e31 2022-10-23 thomas goto done;
285 3efd8e31 2022-10-23 thomas }
286 3efd8e31 2022-10-23 thomas
287 fcbb06bf 2023-01-14 thomas symreftarget = strndup(
288 fcbb06bf 2023-01-14 thomas imsg.data + sizeof(isymref) + isymref.name_len,
289 fcbb06bf 2023-01-14 thomas isymref.target_len);
290 3efd8e31 2022-10-23 thomas if (symreftarget == NULL) {
291 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
292 3efd8e31 2022-10-23 thomas goto done;
293 3efd8e31 2022-10-23 thomas }
294 3efd8e31 2022-10-23 thomas
295 3efd8e31 2022-10-23 thomas if (asprintf(&symrefstr, "%s:%s", symrefname,
296 3efd8e31 2022-10-23 thomas symreftarget) == -1) {
297 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
298 3efd8e31 2022-10-23 thomas goto done;
299 3efd8e31 2022-10-23 thomas }
300 3efd8e31 2022-10-23 thomas err = send_ref(outfd, isymref.target_id, symrefname,
301 3efd8e31 2022-10-23 thomas !sent_capabilities, client_is_reading, symrefstr,
302 3efd8e31 2022-10-23 thomas chattygot);
303 3efd8e31 2022-10-23 thomas free(refname);
304 3efd8e31 2022-10-23 thomas refname = NULL;
305 3efd8e31 2022-10-23 thomas if (err)
306 3efd8e31 2022-10-23 thomas goto done;
307 3efd8e31 2022-10-23 thomas sent_capabilities = 1;
308 3efd8e31 2022-10-23 thomas if (nrefs > 0)
309 3efd8e31 2022-10-23 thomas nrefs--;
310 3efd8e31 2022-10-23 thomas break;
311 3efd8e31 2022-10-23 thomas default:
312 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
313 3efd8e31 2022-10-23 thomas break;
314 3efd8e31 2022-10-23 thomas }
315 3efd8e31 2022-10-23 thomas
316 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
317 3efd8e31 2022-10-23 thomas }
318 3efd8e31 2022-10-23 thomas
319 3efd8e31 2022-10-23 thomas err = got_pkt_flushpkt(outfd, chattygot);
320 3efd8e31 2022-10-23 thomas if (err)
321 3efd8e31 2022-10-23 thomas goto done;
322 3efd8e31 2022-10-23 thomas done:
323 3efd8e31 2022-10-23 thomas free(symrefstr);
324 3efd8e31 2022-10-23 thomas free(symrefname);
325 3efd8e31 2022-10-23 thomas free(symreftarget);
326 3efd8e31 2022-10-23 thomas return err;
327 3efd8e31 2022-10-23 thomas }
328 3efd8e31 2022-10-23 thomas
329 3efd8e31 2022-10-23 thomas static const struct got_error *
330 3efd8e31 2022-10-23 thomas parse_want_line(char **common_capabilities, uint8_t *id, char *buf, size_t len)
331 3efd8e31 2022-10-23 thomas {
332 3efd8e31 2022-10-23 thomas const struct got_error *err;
333 3efd8e31 2022-10-23 thomas char *id_str = NULL, *client_capabilities = NULL;
334 3efd8e31 2022-10-23 thomas
335 3efd8e31 2022-10-23 thomas err = got_gitproto_parse_want_line(&id_str,
336 3efd8e31 2022-10-23 thomas &client_capabilities, buf, len);
337 3efd8e31 2022-10-23 thomas if (err)
338 3efd8e31 2022-10-23 thomas return err;
339 3efd8e31 2022-10-23 thomas
340 c8ae092d 2023-02-23 thomas if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
341 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
342 3efd8e31 2022-10-23 thomas "want-line with bad object ID");
343 3efd8e31 2022-10-23 thomas goto done;
344 3efd8e31 2022-10-23 thomas }
345 3efd8e31 2022-10-23 thomas
346 3efd8e31 2022-10-23 thomas if (client_capabilities) {
347 3efd8e31 2022-10-23 thomas err = got_gitproto_match_capabilities(common_capabilities,
348 3efd8e31 2022-10-23 thomas NULL, client_capabilities, read_capabilities,
349 3efd8e31 2022-10-23 thomas nitems(read_capabilities));
350 3efd8e31 2022-10-23 thomas if (err)
351 3efd8e31 2022-10-23 thomas goto done;
352 3efd8e31 2022-10-23 thomas }
353 3efd8e31 2022-10-23 thomas done:
354 3efd8e31 2022-10-23 thomas free(id_str);
355 3efd8e31 2022-10-23 thomas free(client_capabilities);
356 3efd8e31 2022-10-23 thomas return err;
357 3efd8e31 2022-10-23 thomas }
358 3efd8e31 2022-10-23 thomas
359 3efd8e31 2022-10-23 thomas static const struct got_error *
360 3efd8e31 2022-10-23 thomas parse_have_line(uint8_t *id, char *buf, size_t len)
361 3efd8e31 2022-10-23 thomas {
362 3efd8e31 2022-10-23 thomas const struct got_error *err;
363 3efd8e31 2022-10-23 thomas char *id_str = NULL;
364 3efd8e31 2022-10-23 thomas
365 3efd8e31 2022-10-23 thomas err = got_gitproto_parse_have_line(&id_str, buf, len);
366 3efd8e31 2022-10-23 thomas if (err)
367 3efd8e31 2022-10-23 thomas return err;
368 3efd8e31 2022-10-23 thomas
369 c8ae092d 2023-02-23 thomas if (!got_parse_hash_digest(id, id_str, GOT_HASH_SHA1)) {
370 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
371 3efd8e31 2022-10-23 thomas "have-line with bad object ID");
372 3efd8e31 2022-10-23 thomas goto done;
373 3efd8e31 2022-10-23 thomas }
374 3efd8e31 2022-10-23 thomas done:
375 3efd8e31 2022-10-23 thomas free(id_str);
376 3efd8e31 2022-10-23 thomas return err;
377 3efd8e31 2022-10-23 thomas }
378 3efd8e31 2022-10-23 thomas
379 3efd8e31 2022-10-23 thomas static const struct got_error *
380 3efd8e31 2022-10-23 thomas send_capability(struct got_capability *capa, struct imsgbuf *ibuf)
381 3efd8e31 2022-10-23 thomas {
382 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
383 3efd8e31 2022-10-23 thomas struct gotd_imsg_capability icapa;
384 3efd8e31 2022-10-23 thomas size_t len;
385 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
386 3efd8e31 2022-10-23 thomas
387 3efd8e31 2022-10-23 thomas memset(&icapa, 0, sizeof(icapa));
388 3efd8e31 2022-10-23 thomas
389 3efd8e31 2022-10-23 thomas icapa.key_len = strlen(capa->key);
390 3efd8e31 2022-10-23 thomas len = sizeof(icapa) + icapa.key_len;
391 3efd8e31 2022-10-23 thomas if (capa->value) {
392 3efd8e31 2022-10-23 thomas icapa.value_len = strlen(capa->value);
393 3efd8e31 2022-10-23 thomas len += icapa.value_len;
394 3efd8e31 2022-10-23 thomas }
395 3efd8e31 2022-10-23 thomas
396 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
397 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
398 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create CAPABILITY");
399 3efd8e31 2022-10-23 thomas return err;
400 3efd8e31 2022-10-23 thomas }
401 3efd8e31 2022-10-23 thomas
402 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
403 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add CAPABILITY");
404 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
405 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add CAPABILITY");
406 3efd8e31 2022-10-23 thomas if (capa->value) {
407 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
408 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add CAPABILITY");
409 3efd8e31 2022-10-23 thomas }
410 3efd8e31 2022-10-23 thomas
411 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
412 3efd8e31 2022-10-23 thomas
413 3efd8e31 2022-10-23 thomas return NULL;
414 3efd8e31 2022-10-23 thomas }
415 3efd8e31 2022-10-23 thomas
416 3efd8e31 2022-10-23 thomas static const struct got_error *
417 3efd8e31 2022-10-23 thomas send_capabilities(int *use_sidebands, int *report_status,
418 3efd8e31 2022-10-23 thomas char *capabilities_str, struct imsgbuf *ibuf)
419 3efd8e31 2022-10-23 thomas {
420 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
421 3efd8e31 2022-10-23 thomas struct gotd_imsg_capabilities icapas;
422 3efd8e31 2022-10-23 thomas struct got_capability *capa = NULL;
423 3efd8e31 2022-10-23 thomas size_t ncapa, i;
424 3efd8e31 2022-10-23 thomas
425 3efd8e31 2022-10-23 thomas err = got_gitproto_split_capabilities_str(&capa, &ncapa,
426 3efd8e31 2022-10-23 thomas capabilities_str);
427 3efd8e31 2022-10-23 thomas if (err)
428 3efd8e31 2022-10-23 thomas return err;
429 3efd8e31 2022-10-23 thomas
430 3efd8e31 2022-10-23 thomas icapas.ncapabilities = ncapa;
431 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_CAPABILITIES, 0, 0, -1,
432 3efd8e31 2022-10-23 thomas &icapas, sizeof(icapas)) == -1) {
433 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose IMSG_CAPABILITIES");
434 3efd8e31 2022-10-23 thomas goto done;
435 3efd8e31 2022-10-23 thomas }
436 3efd8e31 2022-10-23 thomas
437 3efd8e31 2022-10-23 thomas for (i = 0; i < ncapa; i++) {
438 3efd8e31 2022-10-23 thomas err = send_capability(&capa[i], ibuf);
439 3efd8e31 2022-10-23 thomas if (err)
440 3efd8e31 2022-10-23 thomas goto done;
441 3efd8e31 2022-10-23 thomas if (use_sidebands &&
442 3efd8e31 2022-10-23 thomas strcmp(capa[i].key, GOT_CAPA_SIDE_BAND_64K) == 0)
443 3efd8e31 2022-10-23 thomas *use_sidebands = 1;
444 3efd8e31 2022-10-23 thomas if (report_status &&
445 3efd8e31 2022-10-23 thomas strcmp(capa[i].key, GOT_CAPA_REPORT_STATUS) == 0)
446 3efd8e31 2022-10-23 thomas *report_status = 1;
447 3efd8e31 2022-10-23 thomas }
448 3efd8e31 2022-10-23 thomas done:
449 3efd8e31 2022-10-23 thomas free(capa);
450 3efd8e31 2022-10-23 thomas return err;
451 3efd8e31 2022-10-23 thomas }
452 3efd8e31 2022-10-23 thomas
453 3efd8e31 2022-10-23 thomas static const struct got_error *
454 3efd8e31 2022-10-23 thomas forward_flushpkt(struct imsgbuf *ibuf)
455 3efd8e31 2022-10-23 thomas {
456 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_FLUSH, 0, 0, -1, NULL, 0) == -1)
457 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose FLUSH");
458 3efd8e31 2022-10-23 thomas
459 3efd8e31 2022-10-23 thomas return gotd_imsg_flush(ibuf);
460 3efd8e31 2022-10-23 thomas }
461 3efd8e31 2022-10-23 thomas
462 3efd8e31 2022-10-23 thomas static const struct got_error *
463 3efd8e31 2022-10-23 thomas recv_ack(struct imsg *imsg, uint8_t *expected_id)
464 3efd8e31 2022-10-23 thomas {
465 3efd8e31 2022-10-23 thomas struct gotd_imsg_ack iack;
466 3efd8e31 2022-10-23 thomas size_t datalen;
467 3efd8e31 2022-10-23 thomas
468 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
469 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iack))
470 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
471 3efd8e31 2022-10-23 thomas
472 3efd8e31 2022-10-23 thomas memcpy(&iack, imsg->data, sizeof(iack));
473 3efd8e31 2022-10-23 thomas if (memcmp(iack.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
474 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
475 3efd8e31 2022-10-23 thomas
476 3efd8e31 2022-10-23 thomas return NULL;
477 3efd8e31 2022-10-23 thomas }
478 3efd8e31 2022-10-23 thomas
479 3efd8e31 2022-10-23 thomas static const struct got_error *
480 3efd8e31 2022-10-23 thomas recv_nak(struct imsg *imsg, uint8_t *expected_id)
481 3efd8e31 2022-10-23 thomas {
482 3efd8e31 2022-10-23 thomas struct gotd_imsg_ack inak;
483 3efd8e31 2022-10-23 thomas size_t datalen;
484 3efd8e31 2022-10-23 thomas
485 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
486 3efd8e31 2022-10-23 thomas if (datalen != sizeof(inak))
487 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
488 3efd8e31 2022-10-23 thomas
489 3efd8e31 2022-10-23 thomas memcpy(&inak, imsg->data, sizeof(inak));
490 3efd8e31 2022-10-23 thomas if (memcmp(inak.object_id, expected_id, SHA1_DIGEST_LENGTH) != 0)
491 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
492 3efd8e31 2022-10-23 thomas
493 3efd8e31 2022-10-23 thomas return NULL;
494 3efd8e31 2022-10-23 thomas }
495 3efd8e31 2022-10-23 thomas
496 3efd8e31 2022-10-23 thomas
497 3efd8e31 2022-10-23 thomas static const struct got_error *
498 3efd8e31 2022-10-23 thomas recv_want(int *use_sidebands, int outfd, struct imsgbuf *ibuf,
499 3efd8e31 2022-10-23 thomas char *buf, size_t len, int expect_capabilities, int chattygot)
500 3efd8e31 2022-10-23 thomas {
501 3efd8e31 2022-10-23 thomas const struct got_error *err;
502 3efd8e31 2022-10-23 thomas struct gotd_imsg_want iwant;
503 3efd8e31 2022-10-23 thomas char *capabilities_str;
504 3efd8e31 2022-10-23 thomas int done = 0;
505 3efd8e31 2022-10-23 thomas struct imsg imsg;
506 3efd8e31 2022-10-23 thomas
507 3efd8e31 2022-10-23 thomas memset(&iwant, 0, sizeof(iwant));
508 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
509 3efd8e31 2022-10-23 thomas
510 3efd8e31 2022-10-23 thomas err = parse_want_line(&capabilities_str, iwant.object_id, buf, len);
511 3efd8e31 2022-10-23 thomas if (err)
512 3efd8e31 2022-10-23 thomas return err;
513 3efd8e31 2022-10-23 thomas
514 3efd8e31 2022-10-23 thomas if (capabilities_str) {
515 3efd8e31 2022-10-23 thomas if (!expect_capabilities) {
516 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
517 3efd8e31 2022-10-23 thomas "unexpected capability announcement received");
518 3efd8e31 2022-10-23 thomas goto done;
519 3efd8e31 2022-10-23 thomas }
520 3efd8e31 2022-10-23 thomas err = send_capabilities(use_sidebands, NULL, capabilities_str,
521 3efd8e31 2022-10-23 thomas ibuf);
522 3efd8e31 2022-10-23 thomas if (err)
523 3efd8e31 2022-10-23 thomas goto done;
524 3efd8e31 2022-10-23 thomas
525 3efd8e31 2022-10-23 thomas }
526 3efd8e31 2022-10-23 thomas
527 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_WANT, 0, 0, -1,
528 3efd8e31 2022-10-23 thomas &iwant, sizeof(iwant)) == -1) {
529 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose WANT");
530 3efd8e31 2022-10-23 thomas goto done;
531 3efd8e31 2022-10-23 thomas }
532 3efd8e31 2022-10-23 thomas
533 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
534 3efd8e31 2022-10-23 thomas if (err)
535 3efd8e31 2022-10-23 thomas goto done;
536 3efd8e31 2022-10-23 thomas
537 3efd8e31 2022-10-23 thomas /*
538 3efd8e31 2022-10-23 thomas * Wait for an ACK, or an error in case the desired object
539 3efd8e31 2022-10-23 thomas * does not exist.
540 3efd8e31 2022-10-23 thomas */
541 3efd8e31 2022-10-23 thomas while (!done && err == NULL) {
542 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
543 3efd8e31 2022-10-23 thomas if (err)
544 3efd8e31 2022-10-23 thomas break;
545 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
546 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
547 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
548 3efd8e31 2022-10-23 thomas break;
549 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ACK:
550 3efd8e31 2022-10-23 thomas err = recv_ack(&imsg, iwant.object_id);
551 3efd8e31 2022-10-23 thomas if (err)
552 3efd8e31 2022-10-23 thomas break;
553 3efd8e31 2022-10-23 thomas done = 1;
554 3efd8e31 2022-10-23 thomas break;
555 3efd8e31 2022-10-23 thomas default:
556 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
557 3efd8e31 2022-10-23 thomas break;
558 3efd8e31 2022-10-23 thomas }
559 3efd8e31 2022-10-23 thomas
560 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
561 3efd8e31 2022-10-23 thomas }
562 3efd8e31 2022-10-23 thomas done:
563 3efd8e31 2022-10-23 thomas free(capabilities_str);
564 3efd8e31 2022-10-23 thomas return err;
565 3efd8e31 2022-10-23 thomas }
566 3efd8e31 2022-10-23 thomas
567 3efd8e31 2022-10-23 thomas static const struct got_error *
568 3efd8e31 2022-10-23 thomas send_ack(int outfd, uint8_t *id, int chattygot)
569 3efd8e31 2022-10-23 thomas {
570 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
571 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
572 3efd8e31 2022-10-23 thomas int len;
573 3efd8e31 2022-10-23 thomas
574 946e0798 2022-11-06 thomas if (got_sha1_digest_to_str(id, hex, sizeof(hex)) == NULL)
575 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_BAD_OBJ_ID);
576 3efd8e31 2022-10-23 thomas
577 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "ACK %s\n", hex);
578 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf))
579 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
580 3efd8e31 2022-10-23 thomas
581 3efd8e31 2022-10-23 thomas return got_pkt_writepkt(outfd, buf, len, chattygot);
582 3efd8e31 2022-10-23 thomas }
583 3efd8e31 2022-10-23 thomas
584 3efd8e31 2022-10-23 thomas static const struct got_error *
585 3efd8e31 2022-10-23 thomas send_nak(int outfd, int chattygot)
586 3efd8e31 2022-10-23 thomas {
587 3efd8e31 2022-10-23 thomas char buf[5];
588 3efd8e31 2022-10-23 thomas int len;
589 3efd8e31 2022-10-23 thomas
590 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "NAK\n");
591 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf))
592 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
593 3efd8e31 2022-10-23 thomas
594 3efd8e31 2022-10-23 thomas return got_pkt_writepkt(outfd, buf, len, chattygot);
595 3efd8e31 2022-10-23 thomas }
596 3efd8e31 2022-10-23 thomas
597 3efd8e31 2022-10-23 thomas static const struct got_error *
598 3efd8e31 2022-10-23 thomas recv_have(int *have_ack, int outfd, struct imsgbuf *ibuf, char *buf,
599 3efd8e31 2022-10-23 thomas size_t len, int chattygot)
600 3efd8e31 2022-10-23 thomas {
601 3efd8e31 2022-10-23 thomas const struct got_error *err;
602 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ihave;
603 3efd8e31 2022-10-23 thomas int done = 0;
604 3efd8e31 2022-10-23 thomas struct imsg imsg;
605 3efd8e31 2022-10-23 thomas
606 3efd8e31 2022-10-23 thomas memset(&ihave, 0, sizeof(ihave));
607 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
608 3efd8e31 2022-10-23 thomas
609 3efd8e31 2022-10-23 thomas err = parse_have_line(ihave.object_id, buf, len);
610 3efd8e31 2022-10-23 thomas if (err)
611 3efd8e31 2022-10-23 thomas return err;
612 3efd8e31 2022-10-23 thomas
613 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_HAVE, 0, 0, -1,
614 3efd8e31 2022-10-23 thomas &ihave, sizeof(ihave)) == -1)
615 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose HAVE");
616 3efd8e31 2022-10-23 thomas
617 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
618 3efd8e31 2022-10-23 thomas if (err)
619 3efd8e31 2022-10-23 thomas return err;
620 3efd8e31 2022-10-23 thomas
621 3efd8e31 2022-10-23 thomas /*
622 3efd8e31 2022-10-23 thomas * Wait for an ACK or a NAK, indicating whether a common
623 3efd8e31 2022-10-23 thomas * commit object has been found.
624 3efd8e31 2022-10-23 thomas */
625 3efd8e31 2022-10-23 thomas while (!done && err == NULL) {
626 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
627 3efd8e31 2022-10-23 thomas if (err)
628 3efd8e31 2022-10-23 thomas return err;
629 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
630 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
631 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
632 3efd8e31 2022-10-23 thomas break;
633 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ACK:
634 3efd8e31 2022-10-23 thomas err = recv_ack(&imsg, ihave.object_id);
635 3efd8e31 2022-10-23 thomas if (err)
636 3efd8e31 2022-10-23 thomas break;
637 3efd8e31 2022-10-23 thomas if (!*have_ack) {
638 3efd8e31 2022-10-23 thomas err = send_ack(outfd, ihave.object_id,
639 3efd8e31 2022-10-23 thomas chattygot);
640 3efd8e31 2022-10-23 thomas if (err)
641 3efd8e31 2022-10-23 thomas return err;
642 3efd8e31 2022-10-23 thomas *have_ack = 1;
643 3efd8e31 2022-10-23 thomas }
644 3efd8e31 2022-10-23 thomas done = 1;
645 3efd8e31 2022-10-23 thomas break;
646 3efd8e31 2022-10-23 thomas case GOTD_IMSG_NAK:
647 3efd8e31 2022-10-23 thomas err = recv_nak(&imsg, ihave.object_id);
648 3efd8e31 2022-10-23 thomas if (err)
649 3efd8e31 2022-10-23 thomas break;
650 3efd8e31 2022-10-23 thomas done = 1;
651 3efd8e31 2022-10-23 thomas break;
652 3efd8e31 2022-10-23 thomas default:
653 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
654 3efd8e31 2022-10-23 thomas break;
655 3efd8e31 2022-10-23 thomas }
656 3efd8e31 2022-10-23 thomas
657 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
658 3efd8e31 2022-10-23 thomas }
659 3efd8e31 2022-10-23 thomas
660 3efd8e31 2022-10-23 thomas return err;
661 3efd8e31 2022-10-23 thomas }
662 3efd8e31 2022-10-23 thomas
663 3efd8e31 2022-10-23 thomas static const struct got_error *
664 3efd8e31 2022-10-23 thomas recv_done(int *packfd, int outfd, struct imsgbuf *ibuf, int chattygot)
665 3efd8e31 2022-10-23 thomas {
666 3efd8e31 2022-10-23 thomas const struct got_error *err;
667 3efd8e31 2022-10-23 thomas struct imsg imsg;
668 3d97effa 2024-01-31 thomas int fd;
669 3efd8e31 2022-10-23 thomas
670 3efd8e31 2022-10-23 thomas *packfd = -1;
671 3efd8e31 2022-10-23 thomas
672 3efd8e31 2022-10-23 thomas if (imsg_compose(ibuf, GOTD_IMSG_DONE, 0, 0, -1, NULL, 0) == -1)
673 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose DONE");
674 3efd8e31 2022-10-23 thomas
675 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
676 3efd8e31 2022-10-23 thomas if (err)
677 3efd8e31 2022-10-23 thomas return err;
678 3efd8e31 2022-10-23 thomas
679 cc4cc677 2022-10-28 thomas while (*packfd == -1 && err == NULL) {
680 cc4cc677 2022-10-28 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
681 cc4cc677 2022-10-28 thomas if (err)
682 cc4cc677 2022-10-28 thomas break;
683 3efd8e31 2022-10-23 thomas
684 cc4cc677 2022-10-28 thomas switch (imsg.hdr.type) {
685 cc4cc677 2022-10-28 thomas case GOTD_IMSG_ERROR:
686 cc4cc677 2022-10-28 thomas err = gotd_imsg_recv_error(NULL, &imsg);
687 cc4cc677 2022-10-28 thomas break;
688 cc4cc677 2022-10-28 thomas case GOTD_IMSG_PACKFILE_PIPE:
689 3d97effa 2024-01-31 thomas fd = imsg_get_fd(&imsg);
690 3d97effa 2024-01-31 thomas if (fd != -1)
691 3d97effa 2024-01-31 thomas *packfd = fd;
692 cc4cc677 2022-10-28 thomas else
693 cc4cc677 2022-10-28 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
694 cc4cc677 2022-10-28 thomas break;
695 cc4cc677 2022-10-28 thomas default:
696 cc4cc677 2022-10-28 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
697 cc4cc677 2022-10-28 thomas break;
698 cc4cc677 2022-10-28 thomas }
699 3efd8e31 2022-10-23 thomas
700 cc4cc677 2022-10-28 thomas imsg_free(&imsg);
701 cc4cc677 2022-10-28 thomas }
702 cc4cc677 2022-10-28 thomas
703 3efd8e31 2022-10-23 thomas return err;
704 3efd8e31 2022-10-23 thomas }
705 3efd8e31 2022-10-23 thomas
706 3efd8e31 2022-10-23 thomas static const struct got_error *
707 3efd8e31 2022-10-23 thomas relay_progress_reports(struct imsgbuf *ibuf, int outfd, int chattygot)
708 3efd8e31 2022-10-23 thomas {
709 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
710 3efd8e31 2022-10-23 thomas int pack_starting = 0;
711 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_progress iprog;
712 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
713 3efd8e31 2022-10-23 thomas struct imsg imsg;
714 3efd8e31 2022-10-23 thomas size_t datalen;
715 3efd8e31 2022-10-23 thomas int p_deltify = 0, n;
716 3efd8e31 2022-10-23 thomas const char *eol = "\r";
717 3efd8e31 2022-10-23 thomas
718 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
719 3efd8e31 2022-10-23 thomas
720 3efd8e31 2022-10-23 thomas while (!pack_starting && err == NULL) {
721 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
722 3efd8e31 2022-10-23 thomas if (err)
723 3efd8e31 2022-10-23 thomas break;
724 3efd8e31 2022-10-23 thomas
725 3efd8e31 2022-10-23 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
726 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
727 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
728 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
729 3efd8e31 2022-10-23 thomas break;
730 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_READY:
731 3efd8e31 2022-10-23 thomas eol = "\n";
732 3efd8e31 2022-10-23 thomas pack_starting = 1;
733 3efd8e31 2022-10-23 thomas /* fallthrough */
734 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_PROGRESS:
735 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iprog)) {
736 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
737 3efd8e31 2022-10-23 thomas break;
738 3efd8e31 2022-10-23 thomas }
739 3efd8e31 2022-10-23 thomas memcpy(&iprog, imsg.data, sizeof(iprog));
740 3efd8e31 2022-10-23 thomas if (iprog.nobj_total > 0) {
741 3efd8e31 2022-10-23 thomas p_deltify = (iprog.nobj_deltify * 100) /
742 3efd8e31 2022-10-23 thomas iprog.nobj_total;
743 3efd8e31 2022-10-23 thomas }
744 3efd8e31 2022-10-23 thomas buf[0] = GOT_SIDEBAND_PROGRESS_INFO;
745 3efd8e31 2022-10-23 thomas n = snprintf(&buf[1], sizeof(buf) - 1,
746 3efd8e31 2022-10-23 thomas "%d commits colored, "
747 3efd8e31 2022-10-23 thomas "%d objects found, "
748 3efd8e31 2022-10-23 thomas "deltify %d%%%s",
749 946e0798 2022-11-06 thomas iprog.ncolored,
750 946e0798 2022-11-06 thomas iprog.nfound,
751 3efd8e31 2022-10-23 thomas p_deltify, eol);
752 3efd8e31 2022-10-23 thomas if (n >= sizeof(buf) - 1)
753 3efd8e31 2022-10-23 thomas break;
754 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, 1 + n, chattygot);
755 3efd8e31 2022-10-23 thomas break;
756 3efd8e31 2022-10-23 thomas default:
757 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
758 3efd8e31 2022-10-23 thomas break;
759 3efd8e31 2022-10-23 thomas }
760 3efd8e31 2022-10-23 thomas
761 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
762 3efd8e31 2022-10-23 thomas }
763 3efd8e31 2022-10-23 thomas
764 3efd8e31 2022-10-23 thomas return err;
765 3efd8e31 2022-10-23 thomas }
766 3efd8e31 2022-10-23 thomas
767 3efd8e31 2022-10-23 thomas static const struct got_error *
768 3efd8e31 2022-10-23 thomas serve_read(int infd, int outfd, int gotd_sock, const char *repo_path,
769 3efd8e31 2022-10-23 thomas int chattygot)
770 3efd8e31 2022-10-23 thomas {
771 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
772 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
773 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
774 3efd8e31 2022-10-23 thomas enum protostate {
775 3efd8e31 2022-10-23 thomas STATE_EXPECT_WANT,
776 3efd8e31 2022-10-23 thomas STATE_EXPECT_MORE_WANT,
777 3efd8e31 2022-10-23 thomas STATE_EXPECT_HAVE,
778 3efd8e31 2022-10-23 thomas STATE_EXPECT_DONE,
779 3efd8e31 2022-10-23 thomas STATE_DONE,
780 3efd8e31 2022-10-23 thomas };
781 3efd8e31 2022-10-23 thomas enum protostate curstate = STATE_EXPECT_WANT;
782 3efd8e31 2022-10-23 thomas int have_ack = 0, use_sidebands = 0, seen_have = 0;
783 3efd8e31 2022-10-23 thomas int packfd = -1;
784 3efd8e31 2022-10-23 thomas size_t pack_chunksize;
785 3efd8e31 2022-10-23 thomas
786 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, gotd_sock);
787 3efd8e31 2022-10-23 thomas
788 3efd8e31 2022-10-23 thomas err = announce_refs(outfd, &ibuf, 1, repo_path, chattygot);
789 3efd8e31 2022-10-23 thomas if (err)
790 3efd8e31 2022-10-23 thomas goto done;
791 3efd8e31 2022-10-23 thomas
792 3efd8e31 2022-10-23 thomas while (curstate != STATE_DONE) {
793 3efd8e31 2022-10-23 thomas int n;
794 3efd8e31 2022-10-23 thomas buf[0] = '\0';
795 3efd8e31 2022-10-23 thomas err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
796 3efd8e31 2022-10-23 thomas if (err)
797 113392cf 2023-01-23 thomas goto done;
798 3efd8e31 2022-10-23 thomas if (n == 0) {
799 895484c8 2023-01-19 thomas if (curstate != STATE_EXPECT_WANT &&
800 895484c8 2023-01-19 thomas curstate != STATE_EXPECT_MORE_WANT &&
801 6110f5ef 2023-01-19 thomas curstate != STATE_EXPECT_HAVE &&
802 6110f5ef 2023-01-19 thomas curstate != STATE_EXPECT_DONE) {
803 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
804 3efd8e31 2022-10-23 thomas "unexpected flush packet received");
805 3efd8e31 2022-10-23 thomas goto done;
806 3efd8e31 2022-10-23 thomas }
807 895484c8 2023-01-19 thomas
808 895484c8 2023-01-19 thomas if (curstate == STATE_EXPECT_WANT) {
809 895484c8 2023-01-19 thomas ssize_t r;
810 895484c8 2023-01-19 thomas /*
811 895484c8 2023-01-19 thomas * If the client does not want to fetch
812 895484c8 2023-01-19 thomas * anything we should receive a flush
813 895484c8 2023-01-19 thomas * packet followed by EOF.
814 895484c8 2023-01-19 thomas */
815 895484c8 2023-01-19 thomas r = read(infd, buf, sizeof(buf));
816 895484c8 2023-01-19 thomas if (r == -1) {
817 895484c8 2023-01-19 thomas err = got_error_from_errno("read");
818 895484c8 2023-01-19 thomas goto done;
819 895484c8 2023-01-19 thomas }
820 895484c8 2023-01-19 thomas if (r == 0) /* EOF */
821 895484c8 2023-01-19 thomas goto done;
822 895484c8 2023-01-19 thomas
823 895484c8 2023-01-19 thomas /* Zero-length field followed by payload. */
824 895484c8 2023-01-19 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
825 895484c8 2023-01-19 thomas "unexpected flush packet received");
826 895484c8 2023-01-19 thomas goto done;
827 895484c8 2023-01-19 thomas }
828 895484c8 2023-01-19 thomas
829 94a71055 2023-01-23 thomas if (curstate == STATE_EXPECT_WANT ||
830 94a71055 2023-01-23 thomas curstate == STATE_EXPECT_MORE_WANT ||
831 94a71055 2023-01-23 thomas curstate == STATE_EXPECT_HAVE) {
832 94a71055 2023-01-23 thomas err = forward_flushpkt(&ibuf);
833 94a71055 2023-01-23 thomas if (err)
834 94a71055 2023-01-23 thomas goto done;
835 94a71055 2023-01-23 thomas }
836 3efd8e31 2022-10-23 thomas if (curstate == STATE_EXPECT_HAVE && !have_ack) {
837 3efd8e31 2022-10-23 thomas err = send_nak(outfd, chattygot);
838 3efd8e31 2022-10-23 thomas if (err)
839 3efd8e31 2022-10-23 thomas goto done;
840 3efd8e31 2022-10-23 thomas }
841 3efd8e31 2022-10-23 thomas if (curstate == STATE_EXPECT_MORE_WANT)
842 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_HAVE;
843 3efd8e31 2022-10-23 thomas else
844 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_DONE;
845 3efd8e31 2022-10-23 thomas } else if (n >= 5 && strncmp(buf, "want ", 5) == 0) {
846 3efd8e31 2022-10-23 thomas if (curstate != STATE_EXPECT_WANT &&
847 3efd8e31 2022-10-23 thomas curstate != STATE_EXPECT_MORE_WANT) {
848 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
849 3efd8e31 2022-10-23 thomas "unexpected 'want' packet");
850 3efd8e31 2022-10-23 thomas goto done;
851 3efd8e31 2022-10-23 thomas }
852 3efd8e31 2022-10-23 thomas err = recv_want(&use_sidebands, outfd, &ibuf, buf, n,
853 3efd8e31 2022-10-23 thomas curstate == STATE_EXPECT_WANT ? 1 : 0, chattygot);
854 3efd8e31 2022-10-23 thomas if (err)
855 3efd8e31 2022-10-23 thomas goto done;
856 3efd8e31 2022-10-23 thomas if (curstate == STATE_EXPECT_WANT)
857 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_MORE_WANT;
858 3efd8e31 2022-10-23 thomas } else if (n >= 5 && strncmp(buf, "have ", 5) == 0) {
859 6110f5ef 2023-01-19 thomas if (curstate != STATE_EXPECT_HAVE &&
860 6110f5ef 2023-01-19 thomas curstate != STATE_EXPECT_DONE) {
861 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
862 3efd8e31 2022-10-23 thomas "unexpected 'have' packet");
863 3efd8e31 2022-10-23 thomas goto done;
864 3efd8e31 2022-10-23 thomas }
865 6110f5ef 2023-01-19 thomas if (curstate == STATE_EXPECT_HAVE) {
866 6110f5ef 2023-01-19 thomas err = recv_have(&have_ack, outfd, &ibuf,
867 6110f5ef 2023-01-19 thomas buf, n, chattygot);
868 6110f5ef 2023-01-19 thomas if (err)
869 6110f5ef 2023-01-19 thomas goto done;
870 6110f5ef 2023-01-19 thomas seen_have = 1;
871 6110f5ef 2023-01-19 thomas }
872 3efd8e31 2022-10-23 thomas } else if (n == 5 && strncmp(buf, "done\n", 5) == 0) {
873 3efd8e31 2022-10-23 thomas if (curstate != STATE_EXPECT_HAVE &&
874 3efd8e31 2022-10-23 thomas curstate != STATE_EXPECT_DONE) {
875 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
876 3efd8e31 2022-10-23 thomas "unexpected 'done' packet");
877 3efd8e31 2022-10-23 thomas goto done;
878 3efd8e31 2022-10-23 thomas }
879 3efd8e31 2022-10-23 thomas err = recv_done(&packfd, outfd, &ibuf, chattygot);
880 3efd8e31 2022-10-23 thomas if (err)
881 3efd8e31 2022-10-23 thomas goto done;
882 3efd8e31 2022-10-23 thomas curstate = STATE_DONE;
883 3efd8e31 2022-10-23 thomas break;
884 3efd8e31 2022-10-23 thomas } else {
885 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_PACKET);
886 3efd8e31 2022-10-23 thomas goto done;
887 3efd8e31 2022-10-23 thomas }
888 3efd8e31 2022-10-23 thomas }
889 3efd8e31 2022-10-23 thomas
890 3efd8e31 2022-10-23 thomas if (!seen_have) {
891 3efd8e31 2022-10-23 thomas err = send_nak(outfd, chattygot);
892 3efd8e31 2022-10-23 thomas if (err)
893 3efd8e31 2022-10-23 thomas goto done;
894 3efd8e31 2022-10-23 thomas }
895 3efd8e31 2022-10-23 thomas
896 3efd8e31 2022-10-23 thomas if (use_sidebands) {
897 3efd8e31 2022-10-23 thomas err = relay_progress_reports(&ibuf, outfd, chattygot);
898 3efd8e31 2022-10-23 thomas if (err)
899 3efd8e31 2022-10-23 thomas goto done;
900 3efd8e31 2022-10-23 thomas pack_chunksize = GOT_SIDEBAND_64K_PACKFILE_DATA_MAX;
901 3efd8e31 2022-10-23 thomas } else
902 3efd8e31 2022-10-23 thomas pack_chunksize = sizeof(buf);
903 3efd8e31 2022-10-23 thomas
904 3efd8e31 2022-10-23 thomas for (;;) {
905 89077ea1 2022-10-31 thomas ssize_t r;
906 3efd8e31 2022-10-23 thomas
907 3efd8e31 2022-10-23 thomas r = read(packfd, use_sidebands ? &buf[1] : buf,
908 3efd8e31 2022-10-23 thomas pack_chunksize);
909 3efd8e31 2022-10-23 thomas if (r == -1) {
910 3efd8e31 2022-10-23 thomas err = got_error_from_errno("read");
911 3efd8e31 2022-10-23 thomas break;
912 3efd8e31 2022-10-23 thomas } else if (r == 0) {
913 3efd8e31 2022-10-23 thomas err = got_pkt_flushpkt(outfd, chattygot);
914 3efd8e31 2022-10-23 thomas break;
915 3efd8e31 2022-10-23 thomas }
916 3efd8e31 2022-10-23 thomas
917 3efd8e31 2022-10-23 thomas if (use_sidebands) {
918 3efd8e31 2022-10-23 thomas buf[0] = GOT_SIDEBAND_PACKFILE_DATA;
919 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, 1 + r, chattygot);
920 3efd8e31 2022-10-23 thomas if (err)
921 3efd8e31 2022-10-23 thomas break;
922 3efd8e31 2022-10-23 thomas } else {
923 89077ea1 2022-10-31 thomas err = got_poll_write_full(outfd, buf, r);
924 89077ea1 2022-10-31 thomas if (err) {
925 89077ea1 2022-10-31 thomas if (err->code == GOT_ERR_EOF)
926 89077ea1 2022-10-31 thomas err = NULL;
927 3efd8e31 2022-10-23 thomas break;
928 3efd8e31 2022-10-23 thomas }
929 3efd8e31 2022-10-23 thomas }
930 3efd8e31 2022-10-23 thomas }
931 3efd8e31 2022-10-23 thomas done:
932 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
933 3efd8e31 2022-10-23 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
934 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
935 3efd8e31 2022-10-23 thomas if (err)
936 3efd8e31 2022-10-23 thomas echo_error(err, outfd, chattygot);
937 3efd8e31 2022-10-23 thomas return err;
938 3efd8e31 2022-10-23 thomas }
939 3efd8e31 2022-10-23 thomas
940 3efd8e31 2022-10-23 thomas static const struct got_error *
941 3efd8e31 2022-10-23 thomas parse_ref_update_line(char **common_capabilities, char **refname,
942 3efd8e31 2022-10-23 thomas uint8_t *old_id, uint8_t *new_id, char *buf, size_t len)
943 3efd8e31 2022-10-23 thomas {
944 3efd8e31 2022-10-23 thomas const struct got_error *err;
945 3efd8e31 2022-10-23 thomas char *old_id_str = NULL, *new_id_str = NULL;
946 3efd8e31 2022-10-23 thomas char *client_capabilities = NULL;
947 3efd8e31 2022-10-23 thomas
948 3efd8e31 2022-10-23 thomas *refname = NULL;
949 3efd8e31 2022-10-23 thomas
950 3efd8e31 2022-10-23 thomas err = got_gitproto_parse_ref_update_line(&old_id_str, &new_id_str,
951 3efd8e31 2022-10-23 thomas refname, &client_capabilities, buf, len);
952 3efd8e31 2022-10-23 thomas if (err)
953 3efd8e31 2022-10-23 thomas return err;
954 3efd8e31 2022-10-23 thomas
955 c8ae092d 2023-02-23 thomas if (!got_parse_hash_digest(old_id, old_id_str, GOT_HASH_SHA1) ||
956 c8ae092d 2023-02-23 thomas !got_parse_hash_digest(new_id, new_id_str, GOT_HASH_SHA1)) {
957 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
958 3efd8e31 2022-10-23 thomas "ref-update with bad object ID");
959 3efd8e31 2022-10-23 thomas goto done;
960 3efd8e31 2022-10-23 thomas }
961 3efd8e31 2022-10-23 thomas if (!got_ref_name_is_valid(*refname)) {
962 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
963 3efd8e31 2022-10-23 thomas "ref-update with bad reference name");
964 3efd8e31 2022-10-23 thomas goto done;
965 3efd8e31 2022-10-23 thomas }
966 3efd8e31 2022-10-23 thomas
967 3efd8e31 2022-10-23 thomas if (client_capabilities) {
968 3efd8e31 2022-10-23 thomas err = got_gitproto_match_capabilities(common_capabilities,
969 3efd8e31 2022-10-23 thomas NULL, client_capabilities, write_capabilities,
970 3efd8e31 2022-10-23 thomas nitems(write_capabilities));
971 3efd8e31 2022-10-23 thomas if (err)
972 3efd8e31 2022-10-23 thomas goto done;
973 3efd8e31 2022-10-23 thomas }
974 3efd8e31 2022-10-23 thomas done:
975 3efd8e31 2022-10-23 thomas free(old_id_str);
976 3efd8e31 2022-10-23 thomas free(new_id_str);
977 3efd8e31 2022-10-23 thomas free(client_capabilities);
978 3efd8e31 2022-10-23 thomas if (err) {
979 3efd8e31 2022-10-23 thomas free(*refname);
980 3efd8e31 2022-10-23 thomas *refname = NULL;
981 3efd8e31 2022-10-23 thomas }
982 3efd8e31 2022-10-23 thomas return err;
983 3efd8e31 2022-10-23 thomas }
984 3efd8e31 2022-10-23 thomas
985 3efd8e31 2022-10-23 thomas static const struct got_error *
986 3efd8e31 2022-10-23 thomas recv_ref_update(int *report_status, int outfd, struct imsgbuf *ibuf,
987 3efd8e31 2022-10-23 thomas char *buf, size_t len, int expect_capabilities, int chattygot)
988 3efd8e31 2022-10-23 thomas {
989 3efd8e31 2022-10-23 thomas const struct got_error *err;
990 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
991 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
992 3efd8e31 2022-10-23 thomas char *capabilities_str = NULL, *refname = NULL;
993 3efd8e31 2022-10-23 thomas int done = 0;
994 3efd8e31 2022-10-23 thomas struct imsg imsg;
995 3efd8e31 2022-10-23 thomas
996 3efd8e31 2022-10-23 thomas memset(&iref, 0, sizeof(iref));
997 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
998 3efd8e31 2022-10-23 thomas
999 3efd8e31 2022-10-23 thomas err = parse_ref_update_line(&capabilities_str, &refname,
1000 3efd8e31 2022-10-23 thomas iref.old_id, iref.new_id, buf, len);
1001 3efd8e31 2022-10-23 thomas if (err)
1002 3efd8e31 2022-10-23 thomas return err;
1003 3efd8e31 2022-10-23 thomas
1004 3efd8e31 2022-10-23 thomas if (capabilities_str) {
1005 3efd8e31 2022-10-23 thomas if (!expect_capabilities) {
1006 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
1007 3efd8e31 2022-10-23 thomas "unexpected capability announcement received");
1008 3efd8e31 2022-10-23 thomas goto done;
1009 3efd8e31 2022-10-23 thomas }
1010 3efd8e31 2022-10-23 thomas err = send_capabilities(NULL, report_status, capabilities_str,
1011 3efd8e31 2022-10-23 thomas ibuf);
1012 3efd8e31 2022-10-23 thomas if (err)
1013 3efd8e31 2022-10-23 thomas goto done;
1014 3efd8e31 2022-10-23 thomas }
1015 3efd8e31 2022-10-23 thomas
1016 3efd8e31 2022-10-23 thomas iref.name_len = strlen(refname);
1017 3efd8e31 2022-10-23 thomas len = sizeof(iref) + iref.name_len;
1018 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF_UPDATE, 0, 0, len);
1019 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
1020 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF_UPDATE");
1021 3efd8e31 2022-10-23 thomas goto done;
1022 3efd8e31 2022-10-23 thomas }
1023 3efd8e31 2022-10-23 thomas
1024 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1025 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1026 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, iref.name_len) == -1)
1027 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1028 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
1029 3efd8e31 2022-10-23 thomas
1030 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(ibuf);
1031 3efd8e31 2022-10-23 thomas if (err)
1032 3efd8e31 2022-10-23 thomas goto done;
1033 3efd8e31 2022-10-23 thomas
1034 3efd8e31 2022-10-23 thomas /* Wait for ACK or an error. */
1035 3efd8e31 2022-10-23 thomas while (!done && err == NULL) {
1036 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, ibuf, 0);
1037 3efd8e31 2022-10-23 thomas if (err)
1038 3efd8e31 2022-10-23 thomas break;
1039 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1040 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1041 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
1042 3efd8e31 2022-10-23 thomas break;
1043 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ACK:
1044 3efd8e31 2022-10-23 thomas err = recv_ack(&imsg, iref.new_id);
1045 3efd8e31 2022-10-23 thomas if (err)
1046 3efd8e31 2022-10-23 thomas break;
1047 3efd8e31 2022-10-23 thomas done = 1;
1048 3efd8e31 2022-10-23 thomas break;
1049 3efd8e31 2022-10-23 thomas default:
1050 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1051 3efd8e31 2022-10-23 thomas break;
1052 3efd8e31 2022-10-23 thomas }
1053 3efd8e31 2022-10-23 thomas
1054 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1055 3efd8e31 2022-10-23 thomas }
1056 3efd8e31 2022-10-23 thomas done:
1057 3efd8e31 2022-10-23 thomas free(capabilities_str);
1058 3efd8e31 2022-10-23 thomas free(refname);
1059 3efd8e31 2022-10-23 thomas return err;
1060 3efd8e31 2022-10-23 thomas }
1061 3efd8e31 2022-10-23 thomas
1062 3efd8e31 2022-10-23 thomas static const struct got_error *
1063 3efd8e31 2022-10-23 thomas recv_packfile(struct imsg *imsg, int infd)
1064 3efd8e31 2022-10-23 thomas {
1065 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1066 3efd8e31 2022-10-23 thomas size_t datalen;
1067 3efd8e31 2022-10-23 thomas int packfd;
1068 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1069 3efd8e31 2022-10-23 thomas int pack_done = 0;
1070 3efd8e31 2022-10-23 thomas
1071 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1072 3efd8e31 2022-10-23 thomas if (datalen != 0)
1073 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1074 3efd8e31 2022-10-23 thomas
1075 3d97effa 2024-01-31 thomas packfd = imsg_get_fd(imsg);
1076 3d97effa 2024-01-31 thomas if (packfd == -1)
1077 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1078 0df2f4da 2023-01-27 thomas
1079 3efd8e31 2022-10-23 thomas while (!pack_done) {
1080 3efd8e31 2022-10-23 thomas ssize_t r = 0;
1081 3efd8e31 2022-10-23 thomas
1082 3efd8e31 2022-10-23 thomas err = got_poll_fd(infd, POLLIN, 1);
1083 3efd8e31 2022-10-23 thomas if (err) {
1084 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_TIMEOUT)
1085 3efd8e31 2022-10-23 thomas break;
1086 3efd8e31 2022-10-23 thomas err = NULL;
1087 3efd8e31 2022-10-23 thomas } else {
1088 3efd8e31 2022-10-23 thomas r = read(infd, buf, sizeof(buf));
1089 3efd8e31 2022-10-23 thomas if (r == -1) {
1090 3efd8e31 2022-10-23 thomas err = got_error_from_errno("read");
1091 3efd8e31 2022-10-23 thomas break;
1092 3efd8e31 2022-10-23 thomas }
1093 3efd8e31 2022-10-23 thomas if (r == 0) {
1094 3efd8e31 2022-10-23 thomas /*
1095 3efd8e31 2022-10-23 thomas * Git clients hang up their side of the
1096 3efd8e31 2022-10-23 thomas * connection after sending the pack file.
1097 3efd8e31 2022-10-23 thomas */
1098 3efd8e31 2022-10-23 thomas err = NULL;
1099 3efd8e31 2022-10-23 thomas pack_done = 1;
1100 3efd8e31 2022-10-23 thomas break;
1101 3efd8e31 2022-10-23 thomas }
1102 3efd8e31 2022-10-23 thomas }
1103 3efd8e31 2022-10-23 thomas
1104 3efd8e31 2022-10-23 thomas if (r == 0) {
1105 3efd8e31 2022-10-23 thomas /* Detect gotd(8) closing the pack pipe when done. */
1106 d95d6907 2023-08-23 thomas err = got_poll_fd(packfd, 0, 1);
1107 3efd8e31 2022-10-23 thomas if (err) {
1108 34cd2d95 2023-09-18 thomas if (err->code != GOT_ERR_TIMEOUT &&
1109 34cd2d95 2023-09-18 thomas err->code != GOT_ERR_EOF)
1110 3efd8e31 2022-10-23 thomas break;
1111 34cd2d95 2023-09-18 thomas if (err->code == GOT_ERR_EOF)
1112 34cd2d95 2023-09-18 thomas pack_done = 1;
1113 3efd8e31 2022-10-23 thomas err = NULL;
1114 3efd8e31 2022-10-23 thomas }
1115 3efd8e31 2022-10-23 thomas } else {
1116 3efd8e31 2022-10-23 thomas /* Write pack data and/or detect pipe being closed. */
1117 3efd8e31 2022-10-23 thomas err = got_poll_write_full(packfd, buf, r);
1118 3efd8e31 2022-10-23 thomas if (err) {
1119 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_EOF)
1120 3efd8e31 2022-10-23 thomas err = NULL;
1121 3efd8e31 2022-10-23 thomas break;
1122 3efd8e31 2022-10-23 thomas }
1123 3efd8e31 2022-10-23 thomas }
1124 3efd8e31 2022-10-23 thomas }
1125 3efd8e31 2022-10-23 thomas
1126 3efd8e31 2022-10-23 thomas close(packfd);
1127 3efd8e31 2022-10-23 thomas return err;
1128 3efd8e31 2022-10-23 thomas }
1129 3efd8e31 2022-10-23 thomas
1130 3efd8e31 2022-10-23 thomas static const struct got_error *
1131 3efd8e31 2022-10-23 thomas report_unpack_status(struct imsg *imsg, int outfd, int chattygot)
1132 3efd8e31 2022-10-23 thomas {
1133 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1134 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_status istatus;
1135 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1136 3efd8e31 2022-10-23 thomas size_t datalen, len;
1137 3efd8e31 2022-10-23 thomas char *reason = NULL;
1138 3efd8e31 2022-10-23 thomas
1139 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1140 3efd8e31 2022-10-23 thomas if (datalen < sizeof(istatus))
1141 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1142 3efd8e31 2022-10-23 thomas memcpy(&istatus, imsg->data, sizeof(istatus));
1143 3efd8e31 2022-10-23 thomas if (datalen != sizeof(istatus) + istatus.reason_len)
1144 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1145 3efd8e31 2022-10-23 thomas
1146 fcbb06bf 2023-01-14 thomas reason = strndup(imsg->data + sizeof(istatus), istatus.reason_len);
1147 3efd8e31 2022-10-23 thomas if (reason == NULL) {
1148 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
1149 3efd8e31 2022-10-23 thomas goto done;
1150 3efd8e31 2022-10-23 thomas }
1151 3efd8e31 2022-10-23 thomas
1152 3efd8e31 2022-10-23 thomas if (err == NULL)
1153 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "unpack ok\n");
1154 3efd8e31 2022-10-23 thomas else
1155 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "unpack %s\n", reason);
1156 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf)) {
1157 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
1158 3efd8e31 2022-10-23 thomas goto done;
1159 3efd8e31 2022-10-23 thomas }
1160 3efd8e31 2022-10-23 thomas
1161 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, len, chattygot);
1162 3efd8e31 2022-10-23 thomas done:
1163 3efd8e31 2022-10-23 thomas free(reason);
1164 3efd8e31 2022-10-23 thomas return err;
1165 3efd8e31 2022-10-23 thomas }
1166 3efd8e31 2022-10-23 thomas
1167 3efd8e31 2022-10-23 thomas static const struct got_error *
1168 3efd8e31 2022-10-23 thomas recv_ref_update_ok(struct imsg *imsg, int outfd, int chattygot)
1169 3efd8e31 2022-10-23 thomas {
1170 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1171 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update_ok iok;
1172 3efd8e31 2022-10-23 thomas size_t datalen, len;
1173 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1174 3efd8e31 2022-10-23 thomas char *refname = NULL;
1175 3efd8e31 2022-10-23 thomas
1176 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1177 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iok))
1178 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1179 3efd8e31 2022-10-23 thomas memcpy(&iok, imsg->data, sizeof(iok));
1180 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iok) + iok.name_len)
1181 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1182 3efd8e31 2022-10-23 thomas
1183 3efd8e31 2022-10-23 thomas memcpy(&iok, imsg->data, sizeof(iok));
1184 3efd8e31 2022-10-23 thomas
1185 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iok), iok.name_len);
1186 3efd8e31 2022-10-23 thomas if (refname == NULL)
1187 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
1188 3efd8e31 2022-10-23 thomas
1189 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "ok %s\n", refname);
1190 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf)) {
1191 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
1192 3efd8e31 2022-10-23 thomas goto done;
1193 3efd8e31 2022-10-23 thomas }
1194 3efd8e31 2022-10-23 thomas
1195 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, len, chattygot);
1196 3efd8e31 2022-10-23 thomas done:
1197 3efd8e31 2022-10-23 thomas free(refname);
1198 3efd8e31 2022-10-23 thomas return err;
1199 3efd8e31 2022-10-23 thomas }
1200 3efd8e31 2022-10-23 thomas
1201 3efd8e31 2022-10-23 thomas static const struct got_error *
1202 3efd8e31 2022-10-23 thomas recv_ref_update_ng(struct imsg *imsg, int outfd, int chattygot)
1203 3efd8e31 2022-10-23 thomas {
1204 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1205 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update_ng ing;
1206 3efd8e31 2022-10-23 thomas size_t datalen, len;
1207 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1208 3efd8e31 2022-10-23 thomas char *refname = NULL, *reason = NULL;
1209 3efd8e31 2022-10-23 thomas
1210 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1211 3efd8e31 2022-10-23 thomas if (datalen < sizeof(ing))
1212 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1213 3efd8e31 2022-10-23 thomas memcpy(&ing, imsg->data, sizeof(ing));
1214 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ing) + ing.name_len + ing.reason_len)
1215 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1216 3efd8e31 2022-10-23 thomas
1217 3efd8e31 2022-10-23 thomas memcpy(&ing, imsg->data, sizeof(ing));
1218 3efd8e31 2022-10-23 thomas
1219 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(ing), ing.name_len);
1220 3efd8e31 2022-10-23 thomas if (refname == NULL)
1221 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
1222 3efd8e31 2022-10-23 thomas
1223 fcbb06bf 2023-01-14 thomas reason = strndup(imsg->data + sizeof(ing) + ing.name_len,
1224 fcbb06bf 2023-01-14 thomas ing.reason_len);
1225 3efd8e31 2022-10-23 thomas if (reason == NULL) {
1226 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
1227 3efd8e31 2022-10-23 thomas goto done;
1228 3efd8e31 2022-10-23 thomas }
1229 3efd8e31 2022-10-23 thomas
1230 3efd8e31 2022-10-23 thomas len = snprintf(buf, sizeof(buf), "ng %s %s\n", refname, reason);
1231 3efd8e31 2022-10-23 thomas if (len >= sizeof(buf)) {
1232 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
1233 3efd8e31 2022-10-23 thomas goto done;
1234 3efd8e31 2022-10-23 thomas }
1235 3efd8e31 2022-10-23 thomas
1236 3efd8e31 2022-10-23 thomas err = got_pkt_writepkt(outfd, buf, len, chattygot);
1237 3efd8e31 2022-10-23 thomas done:
1238 3efd8e31 2022-10-23 thomas free(refname);
1239 3efd8e31 2022-10-23 thomas free(reason);
1240 3efd8e31 2022-10-23 thomas return err;
1241 3efd8e31 2022-10-23 thomas }
1242 3efd8e31 2022-10-23 thomas
1243 3efd8e31 2022-10-23 thomas static const struct got_error *
1244 3efd8e31 2022-10-23 thomas serve_write(int infd, int outfd, int gotd_sock, const char *repo_path,
1245 3efd8e31 2022-10-23 thomas int chattygot)
1246 3efd8e31 2022-10-23 thomas {
1247 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1248 3efd8e31 2022-10-23 thomas char buf[GOT_PKT_MAX];
1249 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
1250 3efd8e31 2022-10-23 thomas enum protostate {
1251 3efd8e31 2022-10-23 thomas STATE_EXPECT_REF_UPDATE,
1252 3efd8e31 2022-10-23 thomas STATE_EXPECT_MORE_REF_UPDATES,
1253 3efd8e31 2022-10-23 thomas STATE_EXPECT_PACKFILE,
1254 3efd8e31 2022-10-23 thomas STATE_PACKFILE_RECEIVED,
1255 3efd8e31 2022-10-23 thomas STATE_REFS_UPDATED,
1256 3efd8e31 2022-10-23 thomas };
1257 3efd8e31 2022-10-23 thomas enum protostate curstate = STATE_EXPECT_REF_UPDATE;
1258 3efd8e31 2022-10-23 thomas struct imsg imsg;
1259 3efd8e31 2022-10-23 thomas int report_status = 0;
1260 3efd8e31 2022-10-23 thomas
1261 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, gotd_sock);
1262 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
1263 3efd8e31 2022-10-23 thomas
1264 3efd8e31 2022-10-23 thomas err = announce_refs(outfd, &ibuf, 0, repo_path, chattygot);
1265 3efd8e31 2022-10-23 thomas if (err)
1266 3efd8e31 2022-10-23 thomas goto done;
1267 3efd8e31 2022-10-23 thomas
1268 3efd8e31 2022-10-23 thomas while (curstate != STATE_EXPECT_PACKFILE) {
1269 3efd8e31 2022-10-23 thomas int n;
1270 3efd8e31 2022-10-23 thomas buf[0] = '\0';
1271 3efd8e31 2022-10-23 thomas err = got_pkt_readpkt(&n, infd, buf, sizeof(buf), chattygot);
1272 3efd8e31 2022-10-23 thomas if (err)
1273 8b56c8f4 2023-01-23 thomas goto done;
1274 3efd8e31 2022-10-23 thomas if (n == 0) {
1275 8e92c55c 2023-06-08 thomas if (curstate == STATE_EXPECT_REF_UPDATE) {
1276 8e92c55c 2023-06-08 thomas /* The client will not send us anything. */
1277 8e92c55c 2023-06-08 thomas goto done;
1278 8e92c55c 2023-06-08 thomas } else if (curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1279 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
1280 3efd8e31 2022-10-23 thomas "unexpected flush packet received");
1281 3efd8e31 2022-10-23 thomas goto done;
1282 3efd8e31 2022-10-23 thomas }
1283 3efd8e31 2022-10-23 thomas err = forward_flushpkt(&ibuf);
1284 3efd8e31 2022-10-23 thomas if (err)
1285 3efd8e31 2022-10-23 thomas goto done;
1286 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_PACKFILE;
1287 3efd8e31 2022-10-23 thomas } else if (n >= (SHA1_DIGEST_STRING_LENGTH * 2) + 2) {
1288 3efd8e31 2022-10-23 thomas if (curstate != STATE_EXPECT_REF_UPDATE &&
1289 3efd8e31 2022-10-23 thomas curstate != STATE_EXPECT_MORE_REF_UPDATES) {
1290 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
1291 3efd8e31 2022-10-23 thomas "unexpected ref-update packet");
1292 3efd8e31 2022-10-23 thomas goto done;
1293 3efd8e31 2022-10-23 thomas }
1294 3efd8e31 2022-10-23 thomas if (curstate == STATE_EXPECT_REF_UPDATE) {
1295 3efd8e31 2022-10-23 thomas err = recv_ref_update(&report_status,
1296 3efd8e31 2022-10-23 thomas outfd, &ibuf, buf, n, 1, chattygot);
1297 3efd8e31 2022-10-23 thomas } else {
1298 3efd8e31 2022-10-23 thomas err = recv_ref_update(NULL, outfd, &ibuf,
1299 3efd8e31 2022-10-23 thomas buf, n, 0, chattygot);
1300 3efd8e31 2022-10-23 thomas }
1301 3efd8e31 2022-10-23 thomas if (err)
1302 3efd8e31 2022-10-23 thomas goto done;
1303 3efd8e31 2022-10-23 thomas curstate = STATE_EXPECT_MORE_REF_UPDATES;
1304 3efd8e31 2022-10-23 thomas } else {
1305 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_PACKET);
1306 3efd8e31 2022-10-23 thomas goto done;
1307 3efd8e31 2022-10-23 thomas }
1308 3efd8e31 2022-10-23 thomas }
1309 3efd8e31 2022-10-23 thomas
1310 3efd8e31 2022-10-23 thomas while (curstate != STATE_PACKFILE_RECEIVED) {
1311 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1312 3efd8e31 2022-10-23 thomas if (err)
1313 3efd8e31 2022-10-23 thomas goto done;
1314 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1315 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1316 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
1317 3efd8e31 2022-10-23 thomas goto done;
1318 e0b6d366 2023-08-29 thomas case GOTD_IMSG_PACKFILE_PIPE:
1319 3efd8e31 2022-10-23 thomas err = recv_packfile(&imsg, infd);
1320 3efd8e31 2022-10-23 thomas if (err) {
1321 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_EOF)
1322 3efd8e31 2022-10-23 thomas goto done;
1323 3efd8e31 2022-10-23 thomas /*
1324 3efd8e31 2022-10-23 thomas * EOF is reported when the client hangs up,
1325 3efd8e31 2022-10-23 thomas * which can happen with Git clients.
1326 3efd8e31 2022-10-23 thomas * The socket should stay half-open so we
1327 3efd8e31 2022-10-23 thomas * can still send our reports if requested.
1328 3efd8e31 2022-10-23 thomas */
1329 3efd8e31 2022-10-23 thomas err = NULL;
1330 3efd8e31 2022-10-23 thomas }
1331 3efd8e31 2022-10-23 thomas curstate = STATE_PACKFILE_RECEIVED;
1332 3efd8e31 2022-10-23 thomas break;
1333 3efd8e31 2022-10-23 thomas default:
1334 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1335 3efd8e31 2022-10-23 thomas break;
1336 3efd8e31 2022-10-23 thomas }
1337 3efd8e31 2022-10-23 thomas
1338 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1339 3efd8e31 2022-10-23 thomas if (err)
1340 3efd8e31 2022-10-23 thomas goto done;
1341 3efd8e31 2022-10-23 thomas }
1342 3efd8e31 2022-10-23 thomas
1343 3efd8e31 2022-10-23 thomas while (curstate != STATE_REFS_UPDATED && err == NULL) {
1344 3efd8e31 2022-10-23 thomas err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
1345 3efd8e31 2022-10-23 thomas if (err)
1346 3efd8e31 2022-10-23 thomas break;
1347 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1348 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1349 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(NULL, &imsg);
1350 3efd8e31 2022-10-23 thomas break;
1351 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_STATUS:
1352 3efd8e31 2022-10-23 thomas if (!report_status)
1353 3efd8e31 2022-10-23 thomas break;
1354 3efd8e31 2022-10-23 thomas err = report_unpack_status(&imsg, outfd, chattygot);
1355 3efd8e31 2022-10-23 thomas break;
1356 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE_OK:
1357 3efd8e31 2022-10-23 thomas if (!report_status)
1358 3efd8e31 2022-10-23 thomas break;
1359 3efd8e31 2022-10-23 thomas err = recv_ref_update_ok(&imsg, outfd, chattygot);
1360 3efd8e31 2022-10-23 thomas break;
1361 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE_NG:
1362 3efd8e31 2022-10-23 thomas if (!report_status)
1363 3efd8e31 2022-10-23 thomas break;
1364 3efd8e31 2022-10-23 thomas err = recv_ref_update_ng(&imsg, outfd, chattygot);
1365 3efd8e31 2022-10-23 thomas break;
1366 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REFS_UPDATED:
1367 3efd8e31 2022-10-23 thomas curstate = STATE_REFS_UPDATED;
1368 3efd8e31 2022-10-23 thomas err = got_pkt_flushpkt(outfd, chattygot);
1369 3efd8e31 2022-10-23 thomas break;
1370 3efd8e31 2022-10-23 thomas default:
1371 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1372 3efd8e31 2022-10-23 thomas break;
1373 3efd8e31 2022-10-23 thomas }
1374 3efd8e31 2022-10-23 thomas
1375 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1376 3efd8e31 2022-10-23 thomas }
1377 3efd8e31 2022-10-23 thomas done:
1378 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
1379 3efd8e31 2022-10-23 thomas if (err)
1380 3efd8e31 2022-10-23 thomas echo_error(err, outfd, chattygot);
1381 3efd8e31 2022-10-23 thomas return err;
1382 3efd8e31 2022-10-23 thomas }
1383 3efd8e31 2022-10-23 thomas
1384 3efd8e31 2022-10-23 thomas const struct got_error *
1385 214d733a 2023-01-23 thomas got_serve(int infd, int outfd, const char *command, const char *repo_path,
1386 214d733a 2023-01-23 thomas int gotd_sock, int chattygot)
1387 3efd8e31 2022-10-23 thomas {
1388 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1389 3efd8e31 2022-10-23 thomas
1390 5769f9a0 2023-04-22 thomas if (strcmp(command, GOT_DIAL_CMD_FETCH) == 0)
1391 3efd8e31 2022-10-23 thomas err = serve_read(infd, outfd, gotd_sock, repo_path, chattygot);
1392 5769f9a0 2023-04-22 thomas else if (strcmp(command, GOT_DIAL_CMD_SEND) == 0)
1393 8efb3dc0 2023-01-23 thomas err = serve_write(infd, outfd, gotd_sock, repo_path,
1394 8efb3dc0 2023-01-23 thomas chattygot);
1395 3efd8e31 2022-10-23 thomas else
1396 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_PACKET);
1397 3efd8e31 2022-10-23 thomas
1398 3efd8e31 2022-10-23 thomas return err;
1399 3efd8e31 2022-10-23 thomas }