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