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