Blame


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