Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 93658fb9 2020-03-18 stsp *
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.
7 93658fb9 2020-03-18 stsp *
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.
15 93658fb9 2020-03-18 stsp */
16 93658fb9 2020-03-18 stsp
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>
21 93658fb9 2020-03-18 stsp
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>
34 dd038bc6 2021-09-21 thomas.ad
35 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
36 93658fb9 2020-03-18 stsp
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"
43 93658fb9 2020-03-18 stsp
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"
53 93658fb9 2020-03-18 stsp
54 8a29a085 2020-03-18 stsp #ifndef nitems
55 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 8a29a085 2020-03-18 stsp #endif
57 8a29a085 2020-03-18 stsp
58 93658fb9 2020-03-18 stsp struct got_object *indexed;
59 858b0dfb 2020-03-20 stsp static int chattygot;
60 93658fb9 2020-03-18 stsp
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 },
65 bd3d9e54 2021-09-05 stsp };
66 bd3d9e54 2021-09-05 stsp
67 7848a0e1 2020-03-19 stsp static void
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)
70 93658fb9 2020-03-18 stsp {
71 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
72 93658fb9 2020-03-18 stsp
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));
76 93658fb9 2020-03-18 stsp
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));
81 33501562 2020-03-18 stsp break;
82 33501562 2020-03-18 stsp }
83 93658fb9 2020-03-18 stsp }
84 93658fb9 2020-03-18 stsp }
85 93658fb9 2020-03-18 stsp
86 93658fb9 2020-03-18 stsp static int
87 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
88 93658fb9 2020-03-18 stsp {
89 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
90 659e7fbd 2020-03-20 stsp return 0;
91 93658fb9 2020-03-18 stsp
92 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
93 659e7fbd 2020-03-20 stsp wanted_branch += 11;
94 659e7fbd 2020-03-20 stsp
95 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
96 0e4002ca 2020-03-21 stsp }
97 0e4002ca 2020-03-21 stsp
98 0e4002ca 2020-03-21 stsp static int
99 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
100 0e4002ca 2020-03-21 stsp {
101 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
102 0e4002ca 2020-03-21 stsp return 0;
103 0e4002ca 2020-03-21 stsp refname += 5;
104 0e4002ca 2020-03-21 stsp
105 0e4002ca 2020-03-21 stsp /*
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.
108 0e4002ca 2020-03-21 stsp */
109 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
110 0e4002ca 2020-03-21 stsp return 0;
111 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
112 0e4002ca 2020-03-21 stsp return 0;
113 0e4002ca 2020-03-21 stsp
114 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
115 0e4002ca 2020-03-21 stsp wanted_ref += 5;
116 0e4002ca 2020-03-21 stsp
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)))
119 0e4002ca 2020-03-21 stsp return 1;
120 0e4002ca 2020-03-21 stsp
121 0e4002ca 2020-03-21 stsp /* Allow exact match. */
122 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
123 abe0f35f 2020-03-18 stsp }
124 abe0f35f 2020-03-18 stsp
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)
127 e70bf110 2020-03-22 stsp {
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);
130 e70bf110 2020-03-22 stsp
131 e70bf110 2020-03-22 stsp if (msglen == 0)
132 e70bf110 2020-03-22 stsp return NULL;
133 e70bf110 2020-03-22 stsp
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");
138 e70bf110 2020-03-22 stsp
139 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
140 531c3985 2020-03-18 stsp }
141 531c3985 2020-03-18 stsp
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)
145 e70bf110 2020-03-22 stsp {
146 b637eb2e 2022-02-23 thomas const struct got_error *err;
147 b637eb2e 2022-02-23 thomas int elapsed = 0;
148 b637eb2e 2022-02-23 thomas
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;
153 b637eb2e 2022-02-23 thomas }
154 b637eb2e 2022-02-23 thomas
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");
159 e70bf110 2020-03-22 stsp
160 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
161 e70bf110 2020-03-22 stsp }
162 e70bf110 2020-03-22 stsp
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)
165 e70bf110 2020-03-22 stsp {
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);
170 e70bf110 2020-03-22 stsp }
171 e70bf110 2020-03-22 stsp
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)
174 531c3985 2020-03-18 stsp {
175 6059809a 2020-12-17 stsp size_t i;
176 531c3985 2020-03-18 stsp
177 531c3985 2020-03-18 stsp if (len == 0)
178 531c3985 2020-03-18 stsp return NULL;
179 531c3985 2020-03-18 stsp
180 531c3985 2020-03-18 stsp /*
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.
183 531c3985 2020-03-18 stsp */
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;
186 531c3985 2020-03-18 stsp
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]))
191 531c3985 2020-03-18 stsp continue;
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");
194 531c3985 2020-03-18 stsp }
195 531c3985 2020-03-18 stsp
196 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
197 8a29a085 2020-03-18 stsp }
198 abe0f35f 2020-03-18 stsp
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)
201 531c3985 2020-03-18 stsp {
202 531c3985 2020-03-18 stsp static char msg[1024];
203 6059809a 2020-12-17 stsp size_t i;
204 531c3985 2020-03-18 stsp
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];
210 531c3985 2020-03-18 stsp }
211 531c3985 2020-03-18 stsp msg[i] = '\0';
212 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
213 531c3985 2020-03-18 stsp }
214 531c3985 2020-03-18 stsp
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)
217 e70bf110 2020-03-22 stsp {
218 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
219 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
220 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
221 e70bf110 2020-03-22 stsp
222 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
223 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
224 e70bf110 2020-03-22 stsp const char *target = pe->data;
225 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
226 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
227 e70bf110 2020-03-22 stsp nsymrefs++;
228 e70bf110 2020-03-22 stsp }
229 e70bf110 2020-03-22 stsp
230 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
231 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
232 e70bf110 2020-03-22 stsp
233 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
234 e70bf110 2020-03-22 stsp if (wbuf == NULL)
235 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
236 e70bf110 2020-03-22 stsp
237 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
238 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
239 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
240 e70bf110 2020-03-22 stsp
241 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
242 e70bf110 2020-03-22 stsp const char *name = pe->path;
243 e70bf110 2020-03-22 stsp size_t name_len = pe->path_len;
244 e70bf110 2020-03-22 stsp const char *target = pe->data;
245 e70bf110 2020-03-22 stsp size_t target_len = strlen(target);
246 e70bf110 2020-03-22 stsp
247 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
248 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
249 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
250 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
251 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
252 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, name, name_len) == -1)
253 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
254 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, target, target_len) == -1)
255 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
256 e70bf110 2020-03-22 stsp }
257 e70bf110 2020-03-22 stsp
258 e70bf110 2020-03-22 stsp wbuf->fd = -1;
259 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
260 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
261 e70bf110 2020-03-22 stsp }
262 e70bf110 2020-03-22 stsp
263 e70bf110 2020-03-22 stsp static const struct got_error *
264 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
265 e70bf110 2020-03-22 stsp const char *refname)
266 e70bf110 2020-03-22 stsp {
267 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
268 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
269 e70bf110 2020-03-22 stsp
270 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
271 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
272 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
273 e70bf110 2020-03-22 stsp
274 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
275 e70bf110 2020-03-22 stsp if (wbuf == NULL)
276 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
277 e70bf110 2020-03-22 stsp
278 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
279 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
280 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_REF");
281 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refname, reflen) == -1)
282 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_REF");
283 e70bf110 2020-03-22 stsp
284 e70bf110 2020-03-22 stsp wbuf->fd = -1;
285 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
286 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
287 e70bf110 2020-03-22 stsp }
288 729743d1 2020-03-23 stsp
289 e70bf110 2020-03-22 stsp static const struct got_error *
290 1d72a2a0 2020-03-24 stsp fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
291 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
292 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
293 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
294 41b0de12 2020-03-21 stsp struct imsgbuf *ibuf)
295 93658fb9 2020-03-18 stsp {
296 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
297 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
298 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
299 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
300 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
301 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
302 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
303 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
304 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
305 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
306 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
307 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
308 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
309 659e7fbd 2020-03-20 stsp int found_branch = 0;
310 dc671e91 2020-03-24 stsp SHA1_CTX sha1_ctx;
311 dc671e91 2020-03-24 stsp uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
312 dc671e91 2020-03-24 stsp size_t sha1_buf_len = 0;
313 dc671e91 2020-03-24 stsp ssize_t w;
314 b637eb2e 2022-02-23 thomas struct got_ratelimit rl;
315 93658fb9 2020-03-18 stsp
316 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
317 dc671e91 2020-03-24 stsp SHA1Init(&sha1_ctx);
318 b637eb2e 2022-02-23 thomas got_ratelimit_init(&rl, 0, 500);
319 abe0f35f 2020-03-18 stsp
320 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
321 9ff10419 2020-03-18 stsp if (have == NULL)
322 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
323 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
324 9ff10419 2020-03-18 stsp if (want == NULL) {
325 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
326 9ff10419 2020-03-18 stsp goto done;
327 9ff10419 2020-03-18 stsp }
328 9ff10419 2020-03-18 stsp while (1) {
329 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
330 fe53745c 2020-03-18 stsp if (err)
331 9ff10419 2020-03-18 stsp goto done;
332 9ff10419 2020-03-18 stsp if (n == 0)
333 93658fb9 2020-03-18 stsp break;
334 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
335 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
336 9ff10419 2020-03-18 stsp goto done;
337 9ff10419 2020-03-18 stsp }
338 35add24a 2021-10-08 thomas free(id_str);
339 35add24a 2021-10-08 thomas free(refname);
340 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
341 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
342 0d0a341c 2020-03-18 stsp if (err)
343 9ff10419 2020-03-18 stsp goto done;
344 8a29a085 2020-03-18 stsp if (is_firstpkt) {
345 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
346 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
347 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
348 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
349 bd3d9e54 2021-09-05 stsp &symrefs, server_capabilities,
350 bd3d9e54 2021-09-05 stsp got_capabilities, nitems(got_capabilities));
351 8a29a085 2020-03-18 stsp if (err)
352 8a29a085 2020-03-18 stsp goto done;
353 858b0dfb 2020-03-20 stsp if (chattygot)
354 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
355 a90356f7 2021-08-26 stsp getprogname(), my_capabilities != NULL ?
356 a90356f7 2021-08-26 stsp my_capabilities : "");
357 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
358 abe0f35f 2020-03-18 stsp if (err)
359 abe0f35f 2020-03-18 stsp goto done;
360 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
361 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
362 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
363 659e7fbd 2020-03-20 stsp const char *name = pe->path;
364 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
365 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
366 659e7fbd 2020-03-20 stsp continue;
367 659e7fbd 2020-03-20 stsp default_branch = symref_target;
368 659e7fbd 2020-03-20 stsp break;
369 659e7fbd 2020-03-20 stsp }
370 659e7fbd 2020-03-20 stsp }
371 7848a0e1 2020-03-19 stsp continue;
372 8a29a085 2020-03-18 stsp }
373 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
374 2690194b 2020-03-21 stsp if (chattygot) {
375 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
376 2690194b 2020-03-21 stsp getprogname(), refname);
377 2690194b 2020-03-21 stsp }
378 93658fb9 2020-03-18 stsp continue;
379 2690194b 2020-03-21 stsp }
380 659e7fbd 2020-03-20 stsp
381 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
382 41b0de12 2020-03-21 stsp if (fetch_all_branches || list_refs_only) {
383 4ba14133 2020-03-20 stsp found_branch = 1;
384 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
385 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
386 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
387 4ba14133 2020-03-20 stsp break;
388 4ba14133 2020-03-20 stsp }
389 2690194b 2020-03-21 stsp if (pe == NULL) {
390 2690194b 2020-03-21 stsp if (chattygot) {
391 2690194b 2020-03-21 stsp fprintf(stderr,
392 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
393 2690194b 2020-03-21 stsp getprogname(), refname);
394 2690194b 2020-03-21 stsp }
395 4ba14133 2020-03-20 stsp continue;
396 2690194b 2020-03-21 stsp }
397 4ba14133 2020-03-20 stsp found_branch = 1;
398 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
399 2690194b 2020-03-21 stsp if (!match_branch(refname, default_branch)) {
400 2690194b 2020-03-21 stsp if (chattygot) {
401 2690194b 2020-03-21 stsp fprintf(stderr,
402 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
403 2690194b 2020-03-21 stsp getprogname(), refname);
404 2690194b 2020-03-21 stsp }
405 4ba14133 2020-03-20 stsp continue;
406 2690194b 2020-03-21 stsp }
407 4ba14133 2020-03-20 stsp found_branch = 1;
408 4ba14133 2020-03-20 stsp }
409 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
410 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(wanted_refs)) {
411 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
412 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
413 0e4002ca 2020-03-21 stsp break;
414 0e4002ca 2020-03-21 stsp }
415 0e4002ca 2020-03-21 stsp if (pe == NULL) {
416 0e4002ca 2020-03-21 stsp if (chattygot) {
417 0e4002ca 2020-03-21 stsp fprintf(stderr,
418 0e4002ca 2020-03-21 stsp "%s: ignoring %s\n",
419 0e4002ca 2020-03-21 stsp getprogname(), refname);
420 0e4002ca 2020-03-21 stsp }
421 0e4002ca 2020-03-21 stsp continue;
422 0e4002ca 2020-03-21 stsp }
423 0e4002ca 2020-03-21 stsp found_branch = 1;
424 0e4002ca 2020-03-21 stsp } else if (!list_refs_only) {
425 2690194b 2020-03-21 stsp if (chattygot) {
426 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
427 2690194b 2020-03-21 stsp getprogname(), refname);
428 2690194b 2020-03-21 stsp }
429 4515a796 2020-03-21 stsp continue;
430 2690194b 2020-03-21 stsp }
431 659e7fbd 2020-03-20 stsp }
432 659e7fbd 2020-03-20 stsp
433 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
434 93658fb9 2020-03-18 stsp refsz *= 2;
435 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
436 9ff10419 2020-03-18 stsp if (have == NULL) {
437 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
438 9ff10419 2020-03-18 stsp goto done;
439 9ff10419 2020-03-18 stsp }
440 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
441 9ff10419 2020-03-18 stsp if (want == NULL) {
442 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
443 9ff10419 2020-03-18 stsp goto done;
444 9ff10419 2020-03-18 stsp }
445 93658fb9 2020-03-18 stsp }
446 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
447 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
448 9ff10419 2020-03-18 stsp goto done;
449 9ff10419 2020-03-18 stsp }
450 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
451 e70bf110 2020-03-22 stsp err = send_fetch_ref(ibuf, &want[nref], refname);
452 8f2d01a6 2020-03-18 stsp if (err)
453 8f2d01a6 2020-03-18 stsp goto done;
454 858b0dfb 2020-03-20 stsp
455 2690194b 2020-03-21 stsp if (chattygot)
456 2690194b 2020-03-21 stsp fprintf(stderr, "%s: %s will be fetched\n",
457 2690194b 2020-03-21 stsp getprogname(), refname);
458 2690194b 2020-03-21 stsp if (chattygot > 1) {
459 858b0dfb 2020-03-20 stsp char *theirs, *mine;
460 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
461 858b0dfb 2020-03-20 stsp if (err)
462 858b0dfb 2020-03-20 stsp goto done;
463 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
464 858b0dfb 2020-03-20 stsp if (err) {
465 858b0dfb 2020-03-20 stsp free(theirs);
466 858b0dfb 2020-03-20 stsp goto done;
467 858b0dfb 2020-03-20 stsp }
468 2690194b 2020-03-21 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
469 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
470 858b0dfb 2020-03-20 stsp free(theirs);
471 858b0dfb 2020-03-20 stsp free(mine);
472 858b0dfb 2020-03-20 stsp }
473 93658fb9 2020-03-18 stsp nref++;
474 93658fb9 2020-03-18 stsp }
475 93658fb9 2020-03-18 stsp
476 41b0de12 2020-03-21 stsp if (list_refs_only)
477 41b0de12 2020-03-21 stsp goto done;
478 41b0de12 2020-03-21 stsp
479 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
480 659e7fbd 2020-03-20 stsp if (!found_branch) {
481 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
482 659e7fbd 2020-03-20 stsp goto done;
483 659e7fbd 2020-03-20 stsp }
484 659e7fbd 2020-03-20 stsp
485 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
486 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
487 93658fb9 2020-03-18 stsp continue;
488 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
489 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
490 a90356f7 2021-08-26 stsp sent_my_capabilites || my_capabilities == NULL ?
491 a90356f7 2021-08-26 stsp "" : my_capabilities);
492 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
493 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
494 9ff10419 2020-03-18 stsp goto done;
495 9ff10419 2020-03-18 stsp }
496 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
497 344e4747 2020-03-18 stsp if (err)
498 9ff10419 2020-03-18 stsp goto done;
499 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
500 7848a0e1 2020-03-19 stsp nwant++;
501 93658fb9 2020-03-18 stsp }
502 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
503 38c670f1 2020-03-18 stsp if (err)
504 38c670f1 2020-03-18 stsp goto done;
505 7848a0e1 2020-03-19 stsp
506 3c912d14 2020-03-19 stsp if (nwant == 0)
507 7848a0e1 2020-03-19 stsp goto done;
508 7848a0e1 2020-03-19 stsp
509 ea83355f 2021-10-08 thomas TAILQ_FOREACH(pe, have_refs, entry) {
510 ea83355f 2021-10-08 thomas struct got_object_id *id = pe->data;
511 ea83355f 2021-10-08 thomas got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
512 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
513 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
514 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
515 9ff10419 2020-03-18 stsp goto done;
516 9ff10419 2020-03-18 stsp }
517 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
518 344e4747 2020-03-18 stsp if (err)
519 9ff10419 2020-03-18 stsp goto done;
520 7848a0e1 2020-03-19 stsp nhave++;
521 93658fb9 2020-03-18 stsp }
522 7848a0e1 2020-03-19 stsp
523 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
524 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
525 7848a0e1 2020-03-19 stsp
526 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
527 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
528 38c670f1 2020-03-18 stsp if (err)
529 38c670f1 2020-03-18 stsp goto done;
530 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
531 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
532 7848a0e1 2020-03-19 stsp goto done;
533 7848a0e1 2020-03-19 stsp }
534 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
535 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
536 7848a0e1 2020-03-19 stsp continue;
537 7848a0e1 2020-03-19 stsp }
538 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
539 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
540 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
541 7848a0e1 2020-03-19 stsp "unexpected message from server");
542 7848a0e1 2020-03-19 stsp goto done;
543 7848a0e1 2020-03-19 stsp }
544 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
545 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
546 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
547 7848a0e1 2020-03-19 stsp goto done;
548 7848a0e1 2020-03-19 stsp }
549 7848a0e1 2020-03-19 stsp acked++;
550 93658fb9 2020-03-18 stsp }
551 7848a0e1 2020-03-19 stsp
552 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
553 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
554 344e4747 2020-03-18 stsp if (err)
555 9ff10419 2020-03-18 stsp goto done;
556 93658fb9 2020-03-18 stsp
557 7848a0e1 2020-03-19 stsp if (nhave == 0) {
558 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
559 7848a0e1 2020-03-19 stsp if (err)
560 7848a0e1 2020-03-19 stsp goto done;
561 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
562 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
563 7848a0e1 2020-03-19 stsp "unexpected message from server");
564 7848a0e1 2020-03-19 stsp goto done;
565 7848a0e1 2020-03-19 stsp }
566 04c53c18 2020-03-18 stsp }
567 93658fb9 2020-03-18 stsp
568 858b0dfb 2020-03-20 stsp if (chattygot)
569 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
570 858b0dfb 2020-03-20 stsp
571 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
572 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
573 531c3985 2020-03-18 stsp have_sidebands = 1;
574 531c3985 2020-03-18 stsp
575 9ff10419 2020-03-18 stsp while (1) {
576 dc671e91 2020-03-24 stsp ssize_t r = 0;
577 531c3985 2020-03-18 stsp int datalen = -1;
578 531c3985 2020-03-18 stsp
579 531c3985 2020-03-18 stsp if (have_sidebands) {
580 f024663d 2021-09-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot);
581 531c3985 2020-03-18 stsp if (err)
582 531c3985 2020-03-18 stsp goto done;
583 531c3985 2020-03-18 stsp if (datalen <= 0)
584 531c3985 2020-03-18 stsp break;
585 531c3985 2020-03-18 stsp
586 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
587 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
588 531c3985 2020-03-18 stsp if (r == -1) {
589 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
590 531c3985 2020-03-18 stsp goto done;
591 531c3985 2020-03-18 stsp }
592 531c3985 2020-03-18 stsp if (r != 1) {
593 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
594 531c3985 2020-03-18 stsp "short packet");
595 531c3985 2020-03-18 stsp goto done;
596 531c3985 2020-03-18 stsp }
597 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
598 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
599 531c3985 2020-03-18 stsp "bad packet length");
600 531c3985 2020-03-18 stsp goto done;
601 531c3985 2020-03-18 stsp }
602 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
603 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
604 531c3985 2020-03-18 stsp /* Read packfile data. */
605 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
606 531c3985 2020-03-18 stsp if (err)
607 531c3985 2020-03-18 stsp goto done;
608 531c3985 2020-03-18 stsp if (r != datalen) {
609 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
610 531c3985 2020-03-18 stsp "packet too short");
611 531c3985 2020-03-18 stsp goto done;
612 531c3985 2020-03-18 stsp }
613 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
614 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
615 531c3985 2020-03-18 stsp if (err)
616 531c3985 2020-03-18 stsp goto done;
617 531c3985 2020-03-18 stsp if (r != datalen) {
618 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
619 531c3985 2020-03-18 stsp "packet too short");
620 531c3985 2020-03-18 stsp goto done;
621 531c3985 2020-03-18 stsp }
622 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
623 531c3985 2020-03-18 stsp if (err)
624 531c3985 2020-03-18 stsp goto done;
625 531c3985 2020-03-18 stsp continue;
626 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
627 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
628 531c3985 2020-03-18 stsp if (err)
629 531c3985 2020-03-18 stsp goto done;
630 531c3985 2020-03-18 stsp if (r != datalen) {
631 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
632 531c3985 2020-03-18 stsp "packet too short");
633 531c3985 2020-03-18 stsp goto done;
634 531c3985 2020-03-18 stsp }
635 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
636 531c3985 2020-03-18 stsp goto done;
637 98f64f14 2021-01-05 stsp } else if (buf[0] == 'A') {
638 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
639 98f64f14 2021-01-05 stsp if (err)
640 98f64f14 2021-01-05 stsp goto done;
641 98f64f14 2021-01-05 stsp if (r != datalen) {
642 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
643 98f64f14 2021-01-05 stsp "packet too short");
644 98f64f14 2021-01-05 stsp goto done;
645 98f64f14 2021-01-05 stsp }
646 98f64f14 2021-01-05 stsp /*
647 98f64f14 2021-01-05 stsp * Git server responds with ACK after 'done'
648 98f64f14 2021-01-05 stsp * even though multi_ack is disabled?!?
649 98f64f14 2021-01-05 stsp */
650 98f64f14 2021-01-05 stsp buf[r] = '\0';
651 98f64f14 2021-01-05 stsp if (strncmp(buf, "CK ", 3) == 0)
652 98f64f14 2021-01-05 stsp continue; /* ignore */
653 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
654 98f64f14 2021-01-05 stsp "unexpected message from server");
655 98f64f14 2021-01-05 stsp goto done;
656 531c3985 2020-03-18 stsp } else {
657 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
658 531c3985 2020-03-18 stsp "unknown side-band received from server");
659 531c3985 2020-03-18 stsp goto done;
660 531c3985 2020-03-18 stsp }
661 531c3985 2020-03-18 stsp } else {
662 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
663 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, sizeof buf);
664 531c3985 2020-03-18 stsp if (err)
665 531c3985 2020-03-18 stsp goto done;
666 531c3985 2020-03-18 stsp if (r <= 0)
667 531c3985 2020-03-18 stsp break;
668 531c3985 2020-03-18 stsp }
669 dc671e91 2020-03-24 stsp
670 dc671e91 2020-03-24 stsp /*
671 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
672 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
673 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
674 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
675 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
676 dc671e91 2020-03-24 stsp *
677 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
678 dc671e91 2020-03-24 stsp */
679 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
680 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
681 dc671e91 2020-03-24 stsp /*
682 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
683 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
684 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
685 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
686 dc671e91 2020-03-24 stsp */
687 dc671e91 2020-03-24 stsp while (sha1_buf_len > 0 &&
688 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
689 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, 1);
690 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + 1, 1);
691 dc671e91 2020-03-24 stsp sha1_buf_len--;
692 dc671e91 2020-03-24 stsp }
693 dc671e91 2020-03-24 stsp
694 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
695 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
696 dc671e91 2020-03-24 stsp sha1_buf_len += r;
697 dc671e91 2020-03-24 stsp } else {
698 dc671e91 2020-03-24 stsp /*
699 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
700 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
701 dc671e91 2020-03-24 stsp */
702 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, r);
703 531c3985 2020-03-18 stsp
704 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
705 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
706 dc671e91 2020-03-24 stsp sha1_buf_len - r);
707 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
708 dc671e91 2020-03-24 stsp }
709 dc671e91 2020-03-24 stsp } else {
710 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
711 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
712 dc671e91 2020-03-24 stsp
713 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
714 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
715 dc671e91 2020-03-24 stsp
716 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
717 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
718 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
719 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
720 dc671e91 2020-03-24 stsp }
721 3168e5da 2020-09-10 stsp
722 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
723 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
724 9ff10419 2020-03-18 stsp if (w == -1) {
725 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
726 9ff10419 2020-03-18 stsp goto done;
727 9ff10419 2020-03-18 stsp }
728 fe53745c 2020-03-18 stsp if (w != r) {
729 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
730 9ff10419 2020-03-18 stsp goto done;
731 9ff10419 2020-03-18 stsp }
732 531c3985 2020-03-18 stsp packsz += w;
733 5672d305 2020-03-18 stsp
734 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
735 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
736 b637eb2e 2022-02-23 thomas err = send_fetch_download_progress(ibuf, packsz, &rl);
737 5672d305 2020-03-18 stsp if (err)
738 5672d305 2020-03-18 stsp goto done;
739 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
740 5672d305 2020-03-18 stsp }
741 93658fb9 2020-03-18 stsp }
742 b637eb2e 2022-02-23 thomas err = send_fetch_download_progress(ibuf, packsz, NULL);
743 5672d305 2020-03-18 stsp if (err)
744 5672d305 2020-03-18 stsp goto done;
745 dc671e91 2020-03-24 stsp
746 dc671e91 2020-03-24 stsp SHA1Final(pack_sha1, &sha1_ctx);
747 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
748 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
749 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
750 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
751 dc671e91 2020-03-24 stsp }
752 9ff10419 2020-03-18 stsp done:
753 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
754 abe0f35f 2020-03-18 stsp free((void *)pe->path);
755 abe0f35f 2020-03-18 stsp free(pe->data);
756 abe0f35f 2020-03-18 stsp }
757 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
758 9ff10419 2020-03-18 stsp free(have);
759 9ff10419 2020-03-18 stsp free(want);
760 00cd0e0a 2020-03-18 stsp free(id_str);
761 00cd0e0a 2020-03-18 stsp free(refname);
762 00cd0e0a 2020-03-18 stsp free(server_capabilities);
763 8f2d01a6 2020-03-18 stsp return err;
764 93658fb9 2020-03-18 stsp }
765 93658fb9 2020-03-18 stsp
766 93658fb9 2020-03-18 stsp
767 93658fb9 2020-03-18 stsp int
768 93658fb9 2020-03-18 stsp main(int argc, char **argv)
769 93658fb9 2020-03-18 stsp {
770 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
771 01bb5a15 2021-09-25 thomas.ad int fetchfd, packfd = -1;
772 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
773 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
774 93658fb9 2020-03-18 stsp struct imsg imsg;
775 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
776 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
777 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
778 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
779 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
780 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
781 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
782 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
783 01bb5a15 2021-09-25 thomas.ad size_t datalen, i;
784 7848a0e1 2020-03-19 stsp #if 0
785 7848a0e1 2020-03-19 stsp static int attached;
786 7848a0e1 2020-03-19 stsp while (!attached)
787 7848a0e1 2020-03-19 stsp sleep (1);
788 7848a0e1 2020-03-19 stsp #endif
789 33501562 2020-03-18 stsp
790 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
791 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
792 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
793 858b0dfb 2020-03-20 stsp
794 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
795 ffb5f621 2020-03-18 stsp #ifndef PROFILE
796 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
797 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
798 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
799 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
800 ffb5f621 2020-03-18 stsp return 1;
801 ffb5f621 2020-03-18 stsp }
802 97799ccd 2022-02-06 thomas
803 97799ccd 2022-02-06 thomas /* revoke fs access */
804 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
805 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
806 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
807 97799ccd 2022-02-06 thomas return 1;
808 97799ccd 2022-02-06 thomas }
809 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
810 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
811 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
812 5d120ea8 2022-06-23 op return 1;
813 5d120ea8 2022-06-23 op }
814 ffb5f621 2020-03-18 stsp #endif
815 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
816 9ca26ac3 2021-08-06 stsp if (err) {
817 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
818 93658fb9 2020-03-18 stsp err = NULL;
819 93658fb9 2020-03-18 stsp goto done;
820 93658fb9 2020-03-18 stsp }
821 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
822 93658fb9 2020-03-18 stsp goto done;
823 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
824 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
825 93658fb9 2020-03-18 stsp goto done;
826 93658fb9 2020-03-18 stsp }
827 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
828 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
829 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
830 93658fb9 2020-03-18 stsp goto done;
831 93658fb9 2020-03-18 stsp }
832 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
833 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
834 4ba14133 2020-03-20 stsp imsg_free(&imsg);
835 4ba14133 2020-03-20 stsp
836 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
837 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
838 2690194b 2020-03-21 stsp
839 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
840 7848a0e1 2020-03-19 stsp struct got_object_id *id;
841 7848a0e1 2020-03-19 stsp char *refname;
842 7848a0e1 2020-03-19 stsp
843 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
844 9ca26ac3 2021-08-06 stsp if (err) {
845 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
846 4ba14133 2020-03-20 stsp err = NULL;
847 4ba14133 2020-03-20 stsp goto done;
848 4ba14133 2020-03-20 stsp }
849 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
850 4ba14133 2020-03-20 stsp goto done;
851 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
852 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
853 4ba14133 2020-03-20 stsp goto done;
854 4ba14133 2020-03-20 stsp }
855 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
856 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
857 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
858 659e7fbd 2020-03-20 stsp goto done;
859 659e7fbd 2020-03-20 stsp }
860 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
861 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
862 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
863 7848a0e1 2020-03-19 stsp goto done;
864 7848a0e1 2020-03-19 stsp }
865 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
866 7848a0e1 2020-03-19 stsp if (refname == NULL) {
867 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
868 7848a0e1 2020-03-19 stsp goto done;
869 7848a0e1 2020-03-19 stsp }
870 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
871 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
872 659e7fbd 2020-03-20 stsp
873 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
874 7848a0e1 2020-03-19 stsp if (id == NULL) {
875 7848a0e1 2020-03-19 stsp free(refname);
876 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
877 7848a0e1 2020-03-19 stsp goto done;
878 7848a0e1 2020-03-19 stsp }
879 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
880 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
881 7848a0e1 2020-03-19 stsp if (err) {
882 7848a0e1 2020-03-19 stsp free(refname);
883 7848a0e1 2020-03-19 stsp free(id);
884 7848a0e1 2020-03-19 stsp goto done;
885 7848a0e1 2020-03-19 stsp }
886 4ba14133 2020-03-20 stsp
887 4ba14133 2020-03-20 stsp imsg_free(&imsg);
888 659e7fbd 2020-03-20 stsp }
889 93658fb9 2020-03-18 stsp
890 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
891 4ba14133 2020-03-20 stsp char *refname;
892 4ba14133 2020-03-20 stsp
893 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
894 9ca26ac3 2021-08-06 stsp if (err) {
895 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
896 4ba14133 2020-03-20 stsp err = NULL;
897 4ba14133 2020-03-20 stsp goto done;
898 4ba14133 2020-03-20 stsp }
899 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
900 4ba14133 2020-03-20 stsp goto done;
901 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
902 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
903 4ba14133 2020-03-20 stsp goto done;
904 4ba14133 2020-03-20 stsp }
905 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
906 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
907 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
908 4ba14133 2020-03-20 stsp goto done;
909 4ba14133 2020-03-20 stsp }
910 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
911 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
912 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
913 4ba14133 2020-03-20 stsp goto done;
914 4ba14133 2020-03-20 stsp }
915 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
916 4ba14133 2020-03-20 stsp if (refname == NULL) {
917 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
918 4ba14133 2020-03-20 stsp goto done;
919 4ba14133 2020-03-20 stsp }
920 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
921 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
922 4ba14133 2020-03-20 stsp
923 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
924 4ba14133 2020-03-20 stsp if (err) {
925 4ba14133 2020-03-20 stsp free(refname);
926 4ba14133 2020-03-20 stsp goto done;
927 4ba14133 2020-03-20 stsp }
928 4ba14133 2020-03-20 stsp
929 4ba14133 2020-03-20 stsp imsg_free(&imsg);
930 4ba14133 2020-03-20 stsp }
931 4ba14133 2020-03-20 stsp
932 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
933 0e4002ca 2020-03-21 stsp char *refname;
934 0e4002ca 2020-03-21 stsp
935 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
936 9ca26ac3 2021-08-06 stsp if (err) {
937 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
938 0e4002ca 2020-03-21 stsp err = NULL;
939 0e4002ca 2020-03-21 stsp goto done;
940 0e4002ca 2020-03-21 stsp }
941 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
942 0e4002ca 2020-03-21 stsp goto done;
943 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
944 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
945 0e4002ca 2020-03-21 stsp goto done;
946 0e4002ca 2020-03-21 stsp }
947 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
948 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
949 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
950 0e4002ca 2020-03-21 stsp goto done;
951 0e4002ca 2020-03-21 stsp }
952 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
953 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
954 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
955 0e4002ca 2020-03-21 stsp goto done;
956 0e4002ca 2020-03-21 stsp }
957 0e4002ca 2020-03-21 stsp refname = malloc(wref.name_len + 1);
958 0e4002ca 2020-03-21 stsp if (refname == NULL) {
959 0e4002ca 2020-03-21 stsp err = got_error_from_errno("malloc");
960 0e4002ca 2020-03-21 stsp goto done;
961 0e4002ca 2020-03-21 stsp }
962 0e4002ca 2020-03-21 stsp memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
963 0e4002ca 2020-03-21 stsp refname[wref.name_len] = '\0';
964 0e4002ca 2020-03-21 stsp
965 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
966 0e4002ca 2020-03-21 stsp if (err) {
967 0e4002ca 2020-03-21 stsp free(refname);
968 0e4002ca 2020-03-21 stsp goto done;
969 0e4002ca 2020-03-21 stsp }
970 0e4002ca 2020-03-21 stsp
971 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
972 0e4002ca 2020-03-21 stsp }
973 0e4002ca 2020-03-21 stsp
974 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
975 9ca26ac3 2021-08-06 stsp if (err) {
976 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
977 93658fb9 2020-03-18 stsp err = NULL;
978 93658fb9 2020-03-18 stsp goto done;
979 93658fb9 2020-03-18 stsp }
980 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
981 93658fb9 2020-03-18 stsp goto done;
982 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
983 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
984 93658fb9 2020-03-18 stsp goto done;
985 93658fb9 2020-03-18 stsp }
986 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
987 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
988 93658fb9 2020-03-18 stsp goto done;
989 93658fb9 2020-03-18 stsp }
990 93658fb9 2020-03-18 stsp packfd = imsg.fd;
991 93658fb9 2020-03-18 stsp
992 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
993 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
994 0e4002ca 2020-03-21 stsp &wanted_refs, fetch_req.list_refs_only, &ibuf);
995 93658fb9 2020-03-18 stsp done:
996 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
997 7848a0e1 2020-03-19 stsp free((char *)pe->path);
998 7848a0e1 2020-03-19 stsp free(pe->data);
999 7848a0e1 2020-03-19 stsp }
1000 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1001 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1002 4ba14133 2020-03-20 stsp free((char *)pe->path);
1003 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1004 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1005 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1006 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1007 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1008 9ff10419 2020-03-18 stsp if (err != NULL)
1009 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1010 93658fb9 2020-03-18 stsp else
1011 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1012 cf875574 2020-03-18 stsp if (err != NULL) {
1013 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1014 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1015 93658fb9 2020-03-18 stsp }
1016 93658fb9 2020-03-18 stsp
1017 93658fb9 2020-03-18 stsp exit(0);
1018 93658fb9 2020-03-18 stsp }