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