2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
4 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
8 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 93658fb9 2020-03-18 stsp #include <sys/types.h>
18 93658fb9 2020-03-18 stsp #include <sys/uio.h>
19 93658fb9 2020-03-18 stsp #include <sys/time.h>
20 93658fb9 2020-03-18 stsp #include <sys/stat.h>
22 93658fb9 2020-03-18 stsp #include <stdint.h>
23 93658fb9 2020-03-18 stsp #include <errno.h>
24 93658fb9 2020-03-18 stsp #include <limits.h>
25 93658fb9 2020-03-18 stsp #include <signal.h>
26 93658fb9 2020-03-18 stsp #include <stdio.h>
27 93658fb9 2020-03-18 stsp #include <stdlib.h>
28 93658fb9 2020-03-18 stsp #include <string.h>
29 93658fb9 2020-03-18 stsp #include <ctype.h>
30 93658fb9 2020-03-18 stsp #include <fcntl.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 93658fb9 2020-03-18 stsp #include <zlib.h>
33 93658fb9 2020-03-18 stsp #include <err.h>
35 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
37 93658fb9 2020-03-18 stsp #include "got_error.h"
38 93658fb9 2020-03-18 stsp #include "got_object.h"
39 abe0f35f 2020-03-18 stsp #include "got_path.h"
40 8a29a085 2020-03-18 stsp #include "got_version.h"
41 f1c6967f 2020-03-19 stsp #include "got_fetch.h"
42 659e7fbd 2020-03-20 stsp #include "got_reference.h"
44 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
45 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
49 0872c0b0 2020-03-18 stsp #include "got_lib_pack.h"
50 f024663d 2021-09-05 stsp #include "got_lib_pkt.h"
51 bd3d9e54 2021-09-05 stsp #include "got_lib_gitproto.h"
52 b637eb2e 2022-02-23 thomas #include "got_lib_ratelimit.h"
54 8a29a085 2020-03-18 stsp #ifndef nitems
55 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 93658fb9 2020-03-18 stsp struct got_object *indexed;
59 858b0dfb 2020-03-20 stsp static int chattygot;
61 bd3d9e54 2021-09-05 stsp static const struct got_capability got_capabilities[] = {
62 bd3d9e54 2021-09-05 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
63 bd3d9e54 2021-09-05 stsp { GOT_CAPA_OFS_DELTA, NULL },
64 bd3d9e54 2021-09-05 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
68 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
69 7848a0e1 2020-03-19 stsp struct got_object_id *my_id, char *refname)
71 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
73 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
74 7848a0e1 2020-03-19 stsp * we should use a flag instead */
75 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
77 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
78 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
79 7848a0e1 2020-03-19 stsp if (strcmp(pe->path, refname) == 0) {
80 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
87 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
89 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
92 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
93 659e7fbd 2020-03-20 stsp wanted_branch += 11;
95 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
99 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
101 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
103 0e4002ca 2020-03-21 stsp refname += 5;
106 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
107 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
109 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
111 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
114 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
115 0e4002ca 2020-03-21 stsp wanted_ref += 5;
117 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
118 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
121 0e4002ca 2020-03-21 stsp /* Allow exact match. */
122 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
125 abe0f35f 2020-03-18 stsp static const struct got_error *
126 e70bf110 2020-03-22 stsp send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
128 e70bf110 2020-03-22 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
129 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
131 e70bf110 2020-03-22 stsp if (msglen == 0)
132 e70bf110 2020-03-22 stsp return NULL;
134 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
135 e70bf110 2020-03-22 stsp msg, msglen) == -1)
136 e70bf110 2020-03-22 stsp return got_error_from_errno(
137 e70bf110 2020-03-22 stsp "imsg_compose FETCH_SERVER_PROGRESS");
139 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
142 531c3985 2020-03-18 stsp static const struct got_error *
143 b637eb2e 2022-02-23 thomas send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
144 b637eb2e 2022-02-23 thomas struct got_ratelimit *rl)
146 b637eb2e 2022-02-23 thomas const struct got_error *err;
147 b637eb2e 2022-02-23 thomas int elapsed = 0;
149 b637eb2e 2022-02-23 thomas if (rl) {
150 b637eb2e 2022-02-23 thomas err = got_ratelimit_check(&elapsed, rl);
151 b637eb2e 2022-02-23 thomas if (err || !elapsed)
152 b637eb2e 2022-02-23 thomas return err;
155 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
156 e70bf110 2020-03-22 stsp &bytes, sizeof(bytes)) == -1)
157 e70bf110 2020-03-22 stsp return got_error_from_errno(
158 e70bf110 2020-03-22 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
160 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
163 e70bf110 2020-03-22 stsp static const struct got_error *
164 1d72a2a0 2020-03-24 stsp send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
166 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
167 1d72a2a0 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1)
168 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
169 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
172 e70bf110 2020-03-22 stsp static const struct got_error *
173 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
177 531c3985 2020-03-18 stsp if (len == 0)
178 531c3985 2020-03-18 stsp return NULL;
181 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
182 531c3985 2020-03-18 stsp * Server may send up to 64k.
184 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
185 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
187 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
188 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
189 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
190 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
192 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
193 531c3985 2020-03-18 stsp "non-printable progress message received from server");
196 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
199 8a29a085 2020-03-18 stsp static const struct got_error *
200 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
202 531c3985 2020-03-18 stsp static char msg[1024];
205 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
206 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
207 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
208 531c3985 2020-03-18 stsp "non-printable error message received from server");
209 531c3985 2020-03-18 stsp msg[i] = buf[i];
211 531c3985 2020-03-18 stsp msg[i] = '\0';
212 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
215 531c3985 2020-03-18 stsp static const struct got_error *
216 e70bf110 2020-03-22 stsp send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
218 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
219 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
220 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
221 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
223 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
224 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
225 e70bf110 2020-03-22 stsp const char *target = pe->data;
226 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
227 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
228 e70bf110 2020-03-22 stsp nsymrefs++;
231 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
232 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
234 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
235 e70bf110 2020-03-22 stsp if (wbuf == NULL)
236 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
238 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
239 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
240 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
241 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
242 e70bf110 2020-03-22 stsp return err;
245 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
246 e70bf110 2020-03-22 stsp const char *name = pe->path;
247 e70bf110 2020-03-22 stsp size_t name_len = pe->path_len;
248 e70bf110 2020-03-22 stsp const char *target = pe->data;
249 e70bf110 2020-03-22 stsp size_t target_len = strlen(target);
251 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
252 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
253 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
254 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
255 e70bf110 2020-03-22 stsp return err;
257 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
258 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
259 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
260 e70bf110 2020-03-22 stsp return err;
262 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, name, name_len) == -1) {
263 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
264 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
265 e70bf110 2020-03-22 stsp return err;
267 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, target, target_len) == -1) {
268 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
269 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
270 e70bf110 2020-03-22 stsp return err;
274 e70bf110 2020-03-22 stsp wbuf->fd = -1;
275 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
276 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
279 e70bf110 2020-03-22 stsp static const struct got_error *
280 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
281 e70bf110 2020-03-22 stsp const char *refname)
283 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
284 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
285 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
287 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
288 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
289 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
291 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
292 e70bf110 2020-03-22 stsp if (wbuf == NULL)
293 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
295 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
296 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
297 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
298 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
299 e70bf110 2020-03-22 stsp return err;
301 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
302 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
303 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
304 e70bf110 2020-03-22 stsp return err;
307 e70bf110 2020-03-22 stsp wbuf->fd = -1;
308 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
309 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
312 e70bf110 2020-03-22 stsp static const struct got_error *
313 1d72a2a0 2020-03-24 stsp fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
314 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
315 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
316 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
317 41b0de12 2020-03-21 stsp struct imsgbuf *ibuf)
319 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
320 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
321 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
322 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
323 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
324 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
325 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
326 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
327 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
328 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
329 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
330 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
331 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
332 659e7fbd 2020-03-20 stsp int found_branch = 0;
333 dc671e91 2020-03-24 stsp SHA1_CTX sha1_ctx;
334 dc671e91 2020-03-24 stsp uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
335 dc671e91 2020-03-24 stsp size_t sha1_buf_len = 0;
337 b637eb2e 2022-02-23 thomas struct got_ratelimit rl;
339 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
340 dc671e91 2020-03-24 stsp SHA1Init(&sha1_ctx);
341 b637eb2e 2022-02-23 thomas got_ratelimit_init(&rl, 0, 500);
343 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
344 9ff10419 2020-03-18 stsp if (have == NULL)
345 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
346 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
347 9ff10419 2020-03-18 stsp if (want == NULL) {
348 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
351 9ff10419 2020-03-18 stsp while (1) {
352 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
355 9ff10419 2020-03-18 stsp if (n == 0)
357 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
358 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
361 35add24a 2021-10-08 thomas free(id_str);
362 35add24a 2021-10-08 thomas free(refname);
363 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
364 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
367 8a29a085 2020-03-18 stsp if (is_firstpkt) {
368 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
369 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
370 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
371 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
372 bd3d9e54 2021-09-05 stsp &symrefs, server_capabilities,
373 bd3d9e54 2021-09-05 stsp got_capabilities, nitems(got_capabilities));
376 858b0dfb 2020-03-20 stsp if (chattygot)
377 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
378 a90356f7 2021-08-26 stsp getprogname(), my_capabilities != NULL ?
379 a90356f7 2021-08-26 stsp my_capabilities : "");
380 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
383 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
384 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
385 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
386 659e7fbd 2020-03-20 stsp const char *name = pe->path;
387 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
388 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
390 659e7fbd 2020-03-20 stsp default_branch = symref_target;
396 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
397 2690194b 2020-03-21 stsp if (chattygot) {
398 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
399 2690194b 2020-03-21 stsp getprogname(), refname);
404 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
405 41b0de12 2020-03-21 stsp if (fetch_all_branches || list_refs_only) {
406 4ba14133 2020-03-20 stsp found_branch = 1;
407 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
408 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
409 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
412 2690194b 2020-03-21 stsp if (pe == NULL) {
413 2690194b 2020-03-21 stsp if (chattygot) {
414 2690194b 2020-03-21 stsp fprintf(stderr,
415 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
416 2690194b 2020-03-21 stsp getprogname(), refname);
420 4ba14133 2020-03-20 stsp found_branch = 1;
421 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
422 2690194b 2020-03-21 stsp if (!match_branch(refname, default_branch)) {
423 2690194b 2020-03-21 stsp if (chattygot) {
424 2690194b 2020-03-21 stsp fprintf(stderr,
425 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
426 2690194b 2020-03-21 stsp getprogname(), refname);
430 4ba14133 2020-03-20 stsp found_branch = 1;
432 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
433 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(wanted_refs)) {
434 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
435 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
438 0e4002ca 2020-03-21 stsp if (pe == NULL) {
439 0e4002ca 2020-03-21 stsp if (chattygot) {
440 0e4002ca 2020-03-21 stsp fprintf(stderr,
441 0e4002ca 2020-03-21 stsp "%s: ignoring %s\n",
442 0e4002ca 2020-03-21 stsp getprogname(), refname);
446 0e4002ca 2020-03-21 stsp found_branch = 1;
447 0e4002ca 2020-03-21 stsp } else if (!list_refs_only) {
448 2690194b 2020-03-21 stsp if (chattygot) {
449 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
450 2690194b 2020-03-21 stsp getprogname(), refname);
456 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
457 93658fb9 2020-03-18 stsp refsz *= 2;
458 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
459 9ff10419 2020-03-18 stsp if (have == NULL) {
460 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
463 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
464 9ff10419 2020-03-18 stsp if (want == NULL) {
465 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
469 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
470 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
473 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
474 e70bf110 2020-03-22 stsp err = send_fetch_ref(ibuf, &want[nref], refname);
478 2690194b 2020-03-21 stsp if (chattygot)
479 2690194b 2020-03-21 stsp fprintf(stderr, "%s: %s will be fetched\n",
480 2690194b 2020-03-21 stsp getprogname(), refname);
481 2690194b 2020-03-21 stsp if (chattygot > 1) {
482 858b0dfb 2020-03-20 stsp char *theirs, *mine;
483 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
486 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
488 858b0dfb 2020-03-20 stsp free(theirs);
491 2690194b 2020-03-21 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
492 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
493 858b0dfb 2020-03-20 stsp free(theirs);
494 858b0dfb 2020-03-20 stsp free(mine);
499 41b0de12 2020-03-21 stsp if (list_refs_only)
502 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
503 659e7fbd 2020-03-20 stsp if (!found_branch) {
504 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
508 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
509 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
511 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
512 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
513 a90356f7 2021-08-26 stsp sent_my_capabilites || my_capabilities == NULL ?
514 a90356f7 2021-08-26 stsp "" : my_capabilities);
515 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
516 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
519 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
522 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
525 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
529 3c912d14 2020-03-19 stsp if (nwant == 0)
532 ea83355f 2021-10-08 thomas TAILQ_FOREACH(pe, have_refs, entry) {
533 ea83355f 2021-10-08 thomas struct got_object_id *id = pe->data;
534 ea83355f 2021-10-08 thomas got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
535 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
536 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
537 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
540 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
546 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
547 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
549 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
550 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
553 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
554 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
557 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
558 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
561 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
562 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
563 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
564 7848a0e1 2020-03-19 stsp "unexpected message from server");
567 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
568 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
569 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
575 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
576 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
580 7848a0e1 2020-03-19 stsp if (nhave == 0) {
581 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
584 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
585 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
586 7848a0e1 2020-03-19 stsp "unexpected message from server");
591 858b0dfb 2020-03-20 stsp if (chattygot)
592 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
594 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
595 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
596 531c3985 2020-03-18 stsp have_sidebands = 1;
598 9ff10419 2020-03-18 stsp while (1) {
599 dc671e91 2020-03-24 stsp ssize_t r = 0;
600 531c3985 2020-03-18 stsp int datalen = -1;
602 531c3985 2020-03-18 stsp if (have_sidebands) {
603 f024663d 2021-09-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot);
606 531c3985 2020-03-18 stsp if (datalen <= 0)
609 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
610 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
611 531c3985 2020-03-18 stsp if (r == -1) {
612 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
615 531c3985 2020-03-18 stsp if (r != 1) {
616 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
617 531c3985 2020-03-18 stsp "short packet");
620 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
621 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
622 531c3985 2020-03-18 stsp "bad packet length");
625 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
626 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
627 531c3985 2020-03-18 stsp /* Read packfile data. */
628 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
631 531c3985 2020-03-18 stsp if (r != datalen) {
632 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
633 531c3985 2020-03-18 stsp "packet too short");
636 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
637 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
640 531c3985 2020-03-18 stsp if (r != datalen) {
641 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
642 531c3985 2020-03-18 stsp "packet too short");
645 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
649 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
650 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
653 531c3985 2020-03-18 stsp if (r != datalen) {
654 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
655 531c3985 2020-03-18 stsp "packet too short");
658 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
660 98f64f14 2021-01-05 stsp } else if (buf[0] == 'A') {
661 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
664 98f64f14 2021-01-05 stsp if (r != datalen) {
665 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
666 98f64f14 2021-01-05 stsp "packet too short");
670 98f64f14 2021-01-05 stsp * Git server responds with ACK after 'done'
671 98f64f14 2021-01-05 stsp * even though multi_ack is disabled?!?
673 98f64f14 2021-01-05 stsp buf[r] = '\0';
674 98f64f14 2021-01-05 stsp if (strncmp(buf, "CK ", 3) == 0)
675 98f64f14 2021-01-05 stsp continue; /* ignore */
676 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
677 98f64f14 2021-01-05 stsp "unexpected message from server");
680 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
681 531c3985 2020-03-18 stsp "unknown side-band received from server");
685 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
686 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, sizeof buf);
689 531c3985 2020-03-18 stsp if (r <= 0)
694 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
695 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
696 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
697 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
698 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
700 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
702 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
703 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
705 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
706 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
707 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
708 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
710 dc671e91 2020-03-24 stsp while (sha1_buf_len > 0 &&
711 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
712 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, 1);
713 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + 1, 1);
714 dc671e91 2020-03-24 stsp sha1_buf_len--;
717 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
718 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
719 dc671e91 2020-03-24 stsp sha1_buf_len += r;
722 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
723 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
725 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, r);
727 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
728 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
729 dc671e91 2020-03-24 stsp sha1_buf_len - r);
730 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
733 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
734 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
736 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
737 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
739 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
740 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
741 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
742 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
745 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
746 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
747 9ff10419 2020-03-18 stsp if (w == -1) {
748 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
751 fe53745c 2020-03-18 stsp if (w != r) {
752 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
755 531c3985 2020-03-18 stsp packsz += w;
757 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
758 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
759 b637eb2e 2022-02-23 thomas err = send_fetch_download_progress(ibuf, packsz, &rl);
762 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
765 b637eb2e 2022-02-23 thomas err = send_fetch_download_progress(ibuf, packsz, NULL);
769 dc671e91 2020-03-24 stsp SHA1Final(pack_sha1, &sha1_ctx);
770 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
771 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
772 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
773 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
776 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
777 abe0f35f 2020-03-18 stsp free((void *)pe->path);
778 abe0f35f 2020-03-18 stsp free(pe->data);
780 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
781 9ff10419 2020-03-18 stsp free(have);
782 9ff10419 2020-03-18 stsp free(want);
783 00cd0e0a 2020-03-18 stsp free(id_str);
784 00cd0e0a 2020-03-18 stsp free(refname);
785 00cd0e0a 2020-03-18 stsp free(server_capabilities);
786 8f2d01a6 2020-03-18 stsp return err;
791 93658fb9 2020-03-18 stsp main(int argc, char **argv)
793 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
794 01bb5a15 2021-09-25 thomas.ad int fetchfd, packfd = -1;
795 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
796 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
797 93658fb9 2020-03-18 stsp struct imsg imsg;
798 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
799 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
800 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
801 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
802 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
803 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
804 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
805 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
806 01bb5a15 2021-09-25 thomas.ad size_t datalen, i;
808 7848a0e1 2020-03-19 stsp static int attached;
809 7848a0e1 2020-03-19 stsp while (!attached)
813 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
814 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
815 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
817 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
818 ffb5f621 2020-03-18 stsp #ifndef PROFILE
819 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
820 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
821 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
822 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
826 97799ccd 2022-02-06 thomas /* revoke fs access */
827 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
828 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
829 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
830 97799ccd 2022-02-06 thomas return 1;
833 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
835 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
836 93658fb9 2020-03-18 stsp err = NULL;
839 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
841 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
842 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
845 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
846 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
847 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
850 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
851 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
852 4ba14133 2020-03-20 stsp imsg_free(&imsg);
854 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
855 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
857 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
858 7848a0e1 2020-03-19 stsp struct got_object_id *id;
859 7848a0e1 2020-03-19 stsp char *refname;
861 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
863 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
864 4ba14133 2020-03-20 stsp err = NULL;
867 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
869 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
870 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
873 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
874 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
875 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
878 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
879 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
880 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
883 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
884 7848a0e1 2020-03-19 stsp if (refname == NULL) {
885 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
888 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
889 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
891 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
892 7848a0e1 2020-03-19 stsp if (id == NULL) {
893 7848a0e1 2020-03-19 stsp free(refname);
894 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
897 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
898 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
900 7848a0e1 2020-03-19 stsp free(refname);
905 4ba14133 2020-03-20 stsp imsg_free(&imsg);
908 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
909 4ba14133 2020-03-20 stsp char *refname;
911 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
913 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
914 4ba14133 2020-03-20 stsp err = NULL;
917 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
919 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
920 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
923 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
924 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
925 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
928 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
929 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
930 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
933 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
934 4ba14133 2020-03-20 stsp if (refname == NULL) {
935 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
938 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
939 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
941 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
943 4ba14133 2020-03-20 stsp free(refname);
947 4ba14133 2020-03-20 stsp imsg_free(&imsg);
950 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
951 0e4002ca 2020-03-21 stsp char *refname;
953 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
955 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
956 0e4002ca 2020-03-21 stsp err = NULL;
959 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
961 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
962 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
965 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
966 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
967 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
970 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
971 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
972 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
975 0e4002ca 2020-03-21 stsp refname = malloc(wref.name_len + 1);
976 0e4002ca 2020-03-21 stsp if (refname == NULL) {
977 0e4002ca 2020-03-21 stsp err = got_error_from_errno("malloc");
980 0e4002ca 2020-03-21 stsp memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
981 0e4002ca 2020-03-21 stsp refname[wref.name_len] = '\0';
983 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
985 0e4002ca 2020-03-21 stsp free(refname);
989 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
992 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
994 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
995 93658fb9 2020-03-18 stsp err = NULL;
998 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1000 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1001 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1002 93658fb9 2020-03-18 stsp goto done;
1004 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1005 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1006 93658fb9 2020-03-18 stsp goto done;
1008 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1010 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1011 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1012 0e4002ca 2020-03-21 stsp &wanted_refs, fetch_req.list_refs_only, &ibuf);
1014 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
1015 7848a0e1 2020-03-19 stsp free((char *)pe->path);
1016 7848a0e1 2020-03-19 stsp free(pe->data);
1018 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1019 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1020 4ba14133 2020-03-20 stsp free((char *)pe->path);
1021 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1022 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1023 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1024 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1025 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1026 9ff10419 2020-03-18 stsp if (err != NULL)
1027 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1029 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1030 cf875574 2020-03-18 stsp if (err != NULL) {
1031 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1032 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);