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