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/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 #include <sys/syslimits.h>
23 93658fb9 2020-03-18 stsp
24 93658fb9 2020-03-18 stsp #include <stdint.h>
25 93658fb9 2020-03-18 stsp #include <errno.h>
26 93658fb9 2020-03-18 stsp #include <imsg.h>
27 93658fb9 2020-03-18 stsp #include <limits.h>
28 93658fb9 2020-03-18 stsp #include <signal.h>
29 93658fb9 2020-03-18 stsp #include <stdio.h>
30 93658fb9 2020-03-18 stsp #include <stdlib.h>
31 93658fb9 2020-03-18 stsp #include <string.h>
32 93658fb9 2020-03-18 stsp #include <ctype.h>
33 93658fb9 2020-03-18 stsp #include <sha1.h>
34 93658fb9 2020-03-18 stsp #include <fcntl.h>
35 93658fb9 2020-03-18 stsp #include <zlib.h>
36 93658fb9 2020-03-18 stsp #include <err.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 93658fb9 2020-03-18 stsp
52 8a29a085 2020-03-18 stsp #ifndef nitems
53 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 8a29a085 2020-03-18 stsp #endif
55 8a29a085 2020-03-18 stsp
56 93658fb9 2020-03-18 stsp struct got_object *indexed;
57 858b0dfb 2020-03-20 stsp static int chattygot;
58 93658fb9 2020-03-18 stsp static struct got_object_id zhash = {.sha1={0}};
59 93658fb9 2020-03-18 stsp
60 fe53745c 2020-03-18 stsp static const struct got_error *
61 fe53745c 2020-03-18 stsp readn(ssize_t *off, int fd, void *buf, size_t n)
62 93658fb9 2020-03-18 stsp {
63 fe53745c 2020-03-18 stsp ssize_t r;
64 93658fb9 2020-03-18 stsp
65 fe53745c 2020-03-18 stsp *off = 0;
66 fe53745c 2020-03-18 stsp while (*off != n) {
67 fe53745c 2020-03-18 stsp r = read(fd, buf + *off, n - *off);
68 fe53745c 2020-03-18 stsp if (r == -1)
69 fe53745c 2020-03-18 stsp return got_error_from_errno("read");
70 93658fb9 2020-03-18 stsp if (r == 0)
71 fe53745c 2020-03-18 stsp return NULL;
72 fe53745c 2020-03-18 stsp *off += r;
73 93658fb9 2020-03-18 stsp }
74 fe53745c 2020-03-18 stsp return NULL;
75 93658fb9 2020-03-18 stsp }
76 93658fb9 2020-03-18 stsp
77 38c670f1 2020-03-18 stsp static const struct got_error *
78 93658fb9 2020-03-18 stsp flushpkt(int fd)
79 93658fb9 2020-03-18 stsp {
80 38c670f1 2020-03-18 stsp ssize_t w;
81 38c670f1 2020-03-18 stsp
82 2690194b 2020-03-21 stsp if (chattygot > 1)
83 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
84 858b0dfb 2020-03-20 stsp
85 38c670f1 2020-03-18 stsp w = write(fd, "0000", 4);
86 38c670f1 2020-03-18 stsp if (w == -1)
87 38c670f1 2020-03-18 stsp return got_error_from_errno("write");
88 38c670f1 2020-03-18 stsp if (w != 4)
89 38c670f1 2020-03-18 stsp return got_error(GOT_ERR_IO);
90 38c670f1 2020-03-18 stsp return NULL;
91 93658fb9 2020-03-18 stsp }
92 93658fb9 2020-03-18 stsp
93 531c3985 2020-03-18 stsp /*
94 531c3985 2020-03-18 stsp * Packet header contains a 4-byte hexstring which specifies the length
95 531c3985 2020-03-18 stsp * of data which follows.
96 531c3985 2020-03-18 stsp */
97 fe53745c 2020-03-18 stsp static const struct got_error *
98 531c3985 2020-03-18 stsp read_pkthdr(int *datalen, int fd)
99 93658fb9 2020-03-18 stsp {
100 531c3985 2020-03-18 stsp static const struct got_error *err = NULL;
101 4dc8ee09 2020-03-18 stsp char lenstr[5];
102 4dc8ee09 2020-03-18 stsp long len;
103 93658fb9 2020-03-18 stsp char *e;
104 54d1a70f 2020-03-18 stsp int n, i;
105 fe53745c 2020-03-18 stsp ssize_t r;
106 93658fb9 2020-03-18 stsp
107 531c3985 2020-03-18 stsp *datalen = 0;
108 fe53745c 2020-03-18 stsp
109 4dc8ee09 2020-03-18 stsp err = readn(&r, fd, lenstr, 4);
110 fe53745c 2020-03-18 stsp if (err)
111 fe53745c 2020-03-18 stsp return err;
112 858b0dfb 2020-03-20 stsp if (r == 0) {
113 858b0dfb 2020-03-20 stsp /* implicit "0000" */
114 2690194b 2020-03-21 stsp if (chattygot > 1)
115 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
116 531c3985 2020-03-18 stsp return NULL;
117 858b0dfb 2020-03-20 stsp }
118 4dc8ee09 2020-03-18 stsp if (r != 4)
119 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
120 531c3985 2020-03-18 stsp "wrong packet header length");
121 fe53745c 2020-03-18 stsp
122 4dc8ee09 2020-03-18 stsp lenstr[4] = '\0';
123 54d1a70f 2020-03-18 stsp for (i = 0; i < 4; i++) {
124 858b0dfb 2020-03-20 stsp if (!isprint((unsigned char)lenstr[i]))
125 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
126 858b0dfb 2020-03-20 stsp "unprintable character in packet length field");
127 858b0dfb 2020-03-20 stsp }
128 858b0dfb 2020-03-20 stsp for (i = 0; i < 4; i++) {
129 858b0dfb 2020-03-20 stsp if (!isxdigit((unsigned char)lenstr[i])) {
130 858b0dfb 2020-03-20 stsp if (chattygot)
131 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: bad length: '%s'\n",
132 858b0dfb 2020-03-20 stsp getprogname(), lenstr);
133 858b0dfb 2020-03-20 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
134 531c3985 2020-03-18 stsp "packet length not specified in hex");
135 858b0dfb 2020-03-20 stsp }
136 54d1a70f 2020-03-18 stsp }
137 4dc8ee09 2020-03-18 stsp errno = 0;
138 4dc8ee09 2020-03-18 stsp len = strtol(lenstr, &e, 16);
139 4dc8ee09 2020-03-18 stsp if (lenstr[0] == '\0' || *e != '\0')
140 4dc8ee09 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKET);
141 4dc8ee09 2020-03-18 stsp if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
142 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
143 4dc8ee09 2020-03-18 stsp if (len > INT_MAX || len < INT_MIN)
144 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
145 4dc8ee09 2020-03-18 stsp n = len;
146 531c3985 2020-03-18 stsp if (n == 0)
147 fe53745c 2020-03-18 stsp return NULL;
148 4dc8ee09 2020-03-18 stsp if (n <= 4)
149 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
150 93658fb9 2020-03-18 stsp n -= 4;
151 531c3985 2020-03-18 stsp
152 531c3985 2020-03-18 stsp *datalen = n;
153 531c3985 2020-03-18 stsp return NULL;
154 531c3985 2020-03-18 stsp }
155 531c3985 2020-03-18 stsp
156 531c3985 2020-03-18 stsp static const struct got_error *
157 531c3985 2020-03-18 stsp readpkt(int *outlen, int fd, char *buf, int buflen)
158 531c3985 2020-03-18 stsp {
159 531c3985 2020-03-18 stsp const struct got_error *err = NULL;
160 858b0dfb 2020-03-20 stsp int datalen, i;
161 531c3985 2020-03-18 stsp ssize_t n;
162 531c3985 2020-03-18 stsp
163 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
164 531c3985 2020-03-18 stsp if (err)
165 531c3985 2020-03-18 stsp return err;
166 531c3985 2020-03-18 stsp
167 531c3985 2020-03-18 stsp if (datalen > buflen)
168 fe53745c 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
169 fe53745c 2020-03-18 stsp
170 531c3985 2020-03-18 stsp err = readn(&n, fd, buf, datalen);
171 fe53745c 2020-03-18 stsp if (err)
172 fe53745c 2020-03-18 stsp return err;
173 531c3985 2020-03-18 stsp if (n != datalen)
174 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
175 fe53745c 2020-03-18 stsp
176 2690194b 2020-03-21 stsp if (chattygot > 1) {
177 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
178 858b0dfb 2020-03-20 stsp for (i = 0; i < n; i++) {
179 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
180 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
181 858b0dfb 2020-03-20 stsp else
182 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
183 858b0dfb 2020-03-20 stsp }
184 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
185 858b0dfb 2020-03-20 stsp }
186 858b0dfb 2020-03-20 stsp
187 fe53745c 2020-03-18 stsp *outlen = n;
188 fe53745c 2020-03-18 stsp return NULL;
189 93658fb9 2020-03-18 stsp }
190 93658fb9 2020-03-18 stsp
191 344e4747 2020-03-18 stsp static const struct got_error *
192 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
193 93658fb9 2020-03-18 stsp {
194 93658fb9 2020-03-18 stsp char len[5];
195 858b0dfb 2020-03-20 stsp int i;
196 344e4747 2020-03-18 stsp ssize_t w;
197 93658fb9 2020-03-18 stsp
198 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
199 344e4747 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
200 344e4747 2020-03-18 stsp w = write(fd, len, 4);
201 344e4747 2020-03-18 stsp if (w == -1)
202 344e4747 2020-03-18 stsp return got_error_from_errno("write");
203 344e4747 2020-03-18 stsp if (w != 4)
204 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
205 344e4747 2020-03-18 stsp w = write(fd, buf, nbuf);
206 344e4747 2020-03-18 stsp if (w == -1)
207 344e4747 2020-03-18 stsp return got_error_from_errno("write");
208 344e4747 2020-03-18 stsp if (w != nbuf)
209 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
210 2690194b 2020-03-21 stsp if (chattygot > 1) {
211 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
212 858b0dfb 2020-03-20 stsp for (i = 0; i < nbuf; i++) {
213 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
214 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
215 858b0dfb 2020-03-20 stsp else
216 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
217 858b0dfb 2020-03-20 stsp }
218 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
219 858b0dfb 2020-03-20 stsp }
220 344e4747 2020-03-18 stsp return NULL;
221 93658fb9 2020-03-18 stsp }
222 93658fb9 2020-03-18 stsp
223 7848a0e1 2020-03-19 stsp static void
224 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
225 7848a0e1 2020-03-19 stsp struct got_object_id *my_id, char *refname)
226 93658fb9 2020-03-18 stsp {
227 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
228 93658fb9 2020-03-18 stsp
229 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
230 7848a0e1 2020-03-19 stsp * we should use a flag instead */
231 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
232 93658fb9 2020-03-18 stsp
233 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
234 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
235 7848a0e1 2020-03-19 stsp if (strcmp(pe->path, refname) == 0) {
236 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
237 33501562 2020-03-18 stsp break;
238 33501562 2020-03-18 stsp }
239 93658fb9 2020-03-18 stsp }
240 93658fb9 2020-03-18 stsp }
241 93658fb9 2020-03-18 stsp
242 93658fb9 2020-03-18 stsp static int
243 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
244 93658fb9 2020-03-18 stsp {
245 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
246 659e7fbd 2020-03-20 stsp return 0;
247 93658fb9 2020-03-18 stsp
248 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
249 659e7fbd 2020-03-20 stsp wanted_branch += 11;
250 659e7fbd 2020-03-20 stsp
251 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
252 0e4002ca 2020-03-21 stsp }
253 0e4002ca 2020-03-21 stsp
254 0e4002ca 2020-03-21 stsp static int
255 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
256 0e4002ca 2020-03-21 stsp {
257 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
258 0e4002ca 2020-03-21 stsp return 0;
259 0e4002ca 2020-03-21 stsp refname += 5;
260 0e4002ca 2020-03-21 stsp
261 0e4002ca 2020-03-21 stsp /*
262 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
263 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
264 0e4002ca 2020-03-21 stsp */
265 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
266 0e4002ca 2020-03-21 stsp return 0;
267 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
268 0e4002ca 2020-03-21 stsp return 0;
269 0e4002ca 2020-03-21 stsp
270 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
271 0e4002ca 2020-03-21 stsp wanted_ref += 5;
272 0e4002ca 2020-03-21 stsp
273 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
274 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
275 0e4002ca 2020-03-21 stsp return 1;
276 0e4002ca 2020-03-21 stsp
277 0e4002ca 2020-03-21 stsp /* Allow exact match. */
278 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
279 93658fb9 2020-03-18 stsp }
280 93658fb9 2020-03-18 stsp
281 0d0a341c 2020-03-18 stsp static const struct got_error *
282 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
283 93658fb9 2020-03-18 stsp {
284 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
285 93658fb9 2020-03-18 stsp char *p;
286 0d0a341c 2020-03-18 stsp size_t i, n = 0;
287 93658fb9 2020-03-18 stsp
288 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
289 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
290 0d0a341c 2020-03-18 stsp
291 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
292 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
293 93658fb9 2020-03-18 stsp line++;
294 0d0a341c 2020-03-18 stsp n++;
295 0d0a341c 2020-03-18 stsp }
296 93658fb9 2020-03-18 stsp p = line;
297 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
298 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
299 93658fb9 2020-03-18 stsp line++;
300 0d0a341c 2020-03-18 stsp n++;
301 0d0a341c 2020-03-18 stsp }
302 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
303 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
304 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
305 0d0a341c 2020-03-18 stsp goto done;
306 0d0a341c 2020-03-18 stsp }
307 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
308 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
309 0d0a341c 2020-03-18 stsp line++;
310 0d0a341c 2020-03-18 stsp n++;
311 0d0a341c 2020-03-18 stsp }
312 93658fb9 2020-03-18 stsp }
313 0d0a341c 2020-03-18 stsp if (i <= 2)
314 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
315 0d0a341c 2020-03-18 stsp done:
316 0d0a341c 2020-03-18 stsp if (err) {
317 0d0a341c 2020-03-18 stsp int j;
318 0d0a341c 2020-03-18 stsp for (j = 0; j < i; j++)
319 0d0a341c 2020-03-18 stsp free(tokens[j]);
320 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
321 0d0a341c 2020-03-18 stsp }
322 0d0a341c 2020-03-18 stsp return err;
323 8a29a085 2020-03-18 stsp }
324 00cd0e0a 2020-03-18 stsp
325 00cd0e0a 2020-03-18 stsp static const struct got_error *
326 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
327 00cd0e0a 2020-03-18 stsp char *line, int len)
328 00cd0e0a 2020-03-18 stsp {
329 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
330 00cd0e0a 2020-03-18 stsp char *tokens[3];
331 8a29a085 2020-03-18 stsp
332 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
333 00cd0e0a 2020-03-18 stsp if (err)
334 00cd0e0a 2020-03-18 stsp return err;
335 00cd0e0a 2020-03-18 stsp
336 00cd0e0a 2020-03-18 stsp if (tokens[0])
337 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
338 00cd0e0a 2020-03-18 stsp if (tokens[1])
339 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
340 2690194b 2020-03-21 stsp if (tokens[2]) {
341 2690194b 2020-03-21 stsp char *p;
342 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
343 2690194b 2020-03-21 stsp p = strrchr(*server_capabilities, '\n');
344 2690194b 2020-03-21 stsp if (p)
345 2690194b 2020-03-21 stsp *p = '\0';
346 2690194b 2020-03-21 stsp }
347 00cd0e0a 2020-03-18 stsp
348 00cd0e0a 2020-03-18 stsp return NULL;
349 00cd0e0a 2020-03-18 stsp }
350 00cd0e0a 2020-03-18 stsp
351 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
352 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
353 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
354 531c3985 2020-03-18 stsp
355 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
356 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
357 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
358 531c3985 2020-03-18 stsp
359 531c3985 2020-03-18 stsp
360 8a29a085 2020-03-18 stsp struct got_capability {
361 8a29a085 2020-03-18 stsp const char *key;
362 8a29a085 2020-03-18 stsp const char *value;
363 8a29a085 2020-03-18 stsp };
364 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
365 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
366 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
367 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
368 8a29a085 2020-03-18 stsp };
369 8a29a085 2020-03-18 stsp
370 8a29a085 2020-03-18 stsp static const struct got_error *
371 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
372 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
373 8a29a085 2020-03-18 stsp {
374 8a29a085 2020-03-18 stsp char *equalsign;
375 8a29a085 2020-03-18 stsp char *s;
376 8a29a085 2020-03-18 stsp
377 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
378 8a29a085 2020-03-18 stsp if (equalsign) {
379 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
380 8a29a085 2020-03-18 stsp return NULL;
381 8a29a085 2020-03-18 stsp } else {
382 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
383 8a29a085 2020-03-18 stsp return NULL;
384 8a29a085 2020-03-18 stsp }
385 8a29a085 2020-03-18 stsp
386 406106ee 2020-03-20 stsp if (asprintf(&s, "%s %s%s%s",
387 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
388 8a29a085 2020-03-18 stsp mycapa->key,
389 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
390 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
391 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
392 8a29a085 2020-03-18 stsp
393 8a29a085 2020-03-18 stsp free(*my_capabilities);
394 8a29a085 2020-03-18 stsp *my_capabilities = s;
395 8a29a085 2020-03-18 stsp return NULL;
396 93658fb9 2020-03-18 stsp }
397 93658fb9 2020-03-18 stsp
398 9ff10419 2020-03-18 stsp static const struct got_error *
399 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
400 8a29a085 2020-03-18 stsp {
401 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
402 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
403 abe0f35f 2020-03-18 stsp
404 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
405 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
406 abe0f35f 2020-03-18 stsp return NULL;
407 abe0f35f 2020-03-18 stsp
408 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
409 abe0f35f 2020-03-18 stsp if (colon == NULL)
410 abe0f35f 2020-03-18 stsp return NULL;
411 abe0f35f 2020-03-18 stsp
412 abe0f35f 2020-03-18 stsp *colon = '\0';
413 abe0f35f 2020-03-18 stsp name = strdup(capa);
414 abe0f35f 2020-03-18 stsp if (name == NULL)
415 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
416 abe0f35f 2020-03-18 stsp
417 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
418 abe0f35f 2020-03-18 stsp if (target == NULL) {
419 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
420 abe0f35f 2020-03-18 stsp goto done;
421 abe0f35f 2020-03-18 stsp }
422 abe0f35f 2020-03-18 stsp
423 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
424 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
425 abe0f35f 2020-03-18 stsp done:
426 abe0f35f 2020-03-18 stsp if (err) {
427 abe0f35f 2020-03-18 stsp free(name);
428 abe0f35f 2020-03-18 stsp free(target);
429 abe0f35f 2020-03-18 stsp }
430 abe0f35f 2020-03-18 stsp return err;
431 abe0f35f 2020-03-18 stsp }
432 abe0f35f 2020-03-18 stsp
433 abe0f35f 2020-03-18 stsp static const struct got_error *
434 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
435 abe0f35f 2020-03-18 stsp char *server_capabilities)
436 abe0f35f 2020-03-18 stsp {
437 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
438 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
439 8a29a085 2020-03-18 stsp int i;
440 8a29a085 2020-03-18 stsp
441 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
442 8a29a085 2020-03-18 stsp do {
443 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
444 8a29a085 2020-03-18 stsp if (capa == NULL)
445 8a29a085 2020-03-18 stsp return NULL;
446 8a29a085 2020-03-18 stsp
447 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
448 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
449 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
450 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
451 abe0f35f 2020-03-18 stsp if (err)
452 abe0f35f 2020-03-18 stsp break;
453 abe0f35f 2020-03-18 stsp continue;
454 abe0f35f 2020-03-18 stsp }
455 abe0f35f 2020-03-18 stsp
456 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
457 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
458 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
459 8a29a085 2020-03-18 stsp if (err)
460 8a29a085 2020-03-18 stsp break;
461 8a29a085 2020-03-18 stsp }
462 8a29a085 2020-03-18 stsp } while (capa);
463 8a29a085 2020-03-18 stsp
464 406106ee 2020-03-20 stsp if (*my_capabilities == NULL) {
465 406106ee 2020-03-20 stsp *my_capabilities = strdup("");
466 406106ee 2020-03-20 stsp if (*my_capabilities == NULL)
467 406106ee 2020-03-20 stsp err = got_error_from_errno("strdup");
468 406106ee 2020-03-20 stsp }
469 8a29a085 2020-03-18 stsp return err;
470 531c3985 2020-03-18 stsp }
471 531c3985 2020-03-18 stsp
472 531c3985 2020-03-18 stsp static const struct got_error *
473 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
474 531c3985 2020-03-18 stsp {
475 531c3985 2020-03-18 stsp int i;
476 531c3985 2020-03-18 stsp
477 531c3985 2020-03-18 stsp if (len == 0)
478 531c3985 2020-03-18 stsp return NULL;
479 531c3985 2020-03-18 stsp
480 531c3985 2020-03-18 stsp /*
481 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
482 531c3985 2020-03-18 stsp * Server may send up to 64k.
483 531c3985 2020-03-18 stsp */
484 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
485 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
486 531c3985 2020-03-18 stsp
487 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
488 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
489 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
490 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
491 531c3985 2020-03-18 stsp continue;
492 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
493 531c3985 2020-03-18 stsp "non-printable progress message received from server");
494 531c3985 2020-03-18 stsp }
495 531c3985 2020-03-18 stsp
496 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
497 8a29a085 2020-03-18 stsp }
498 abe0f35f 2020-03-18 stsp
499 8a29a085 2020-03-18 stsp static const struct got_error *
500 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
501 531c3985 2020-03-18 stsp {
502 531c3985 2020-03-18 stsp static char msg[1024];
503 531c3985 2020-03-18 stsp int i;
504 531c3985 2020-03-18 stsp
505 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
506 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
507 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
508 531c3985 2020-03-18 stsp "non-printable error message received from server");
509 531c3985 2020-03-18 stsp msg[i] = buf[i];
510 531c3985 2020-03-18 stsp }
511 531c3985 2020-03-18 stsp msg[i] = '\0';
512 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
513 531c3985 2020-03-18 stsp }
514 531c3985 2020-03-18 stsp
515 531c3985 2020-03-18 stsp static const struct got_error *
516 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
517 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
518 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
519 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
520 41b0de12 2020-03-21 stsp struct imsgbuf *ibuf)
521 93658fb9 2020-03-18 stsp {
522 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
523 f1c6967f 2020-03-19 stsp char buf[GOT_FETCH_PKTMAX];
524 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
525 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
526 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
527 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
528 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
529 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
530 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
531 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
532 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
533 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
534 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
535 659e7fbd 2020-03-20 stsp int found_branch = 0;
536 93658fb9 2020-03-18 stsp
537 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
538 abe0f35f 2020-03-18 stsp
539 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
540 9ff10419 2020-03-18 stsp if (have == NULL)
541 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
542 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
543 9ff10419 2020-03-18 stsp if (want == NULL) {
544 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
545 9ff10419 2020-03-18 stsp goto done;
546 9ff10419 2020-03-18 stsp }
547 9ff10419 2020-03-18 stsp while (1) {
548 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
549 fe53745c 2020-03-18 stsp if (err)
550 9ff10419 2020-03-18 stsp goto done;
551 9ff10419 2020-03-18 stsp if (n == 0)
552 93658fb9 2020-03-18 stsp break;
553 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
554 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
555 9ff10419 2020-03-18 stsp goto done;
556 9ff10419 2020-03-18 stsp }
557 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
558 00cd0e0a 2020-03-18 stsp buf, n);
559 0d0a341c 2020-03-18 stsp if (err)
560 9ff10419 2020-03-18 stsp goto done;
561 8a29a085 2020-03-18 stsp if (is_firstpkt) {
562 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
563 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
564 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
565 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
566 00cd0e0a 2020-03-18 stsp server_capabilities);
567 8a29a085 2020-03-18 stsp if (err)
568 8a29a085 2020-03-18 stsp goto done;
569 858b0dfb 2020-03-20 stsp if (chattygot)
570 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
571 858b0dfb 2020-03-20 stsp getprogname(), my_capabilities);
572 abe0f35f 2020-03-18 stsp err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
573 abe0f35f 2020-03-18 stsp if (err)
574 abe0f35f 2020-03-18 stsp goto done;
575 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
576 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
577 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
578 659e7fbd 2020-03-20 stsp const char *name = pe->path;
579 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
580 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
581 659e7fbd 2020-03-20 stsp continue;
582 659e7fbd 2020-03-20 stsp default_branch = symref_target;
583 659e7fbd 2020-03-20 stsp break;
584 659e7fbd 2020-03-20 stsp }
585 659e7fbd 2020-03-20 stsp }
586 7848a0e1 2020-03-19 stsp continue;
587 8a29a085 2020-03-18 stsp }
588 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
589 2690194b 2020-03-21 stsp if (chattygot) {
590 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
591 2690194b 2020-03-21 stsp getprogname(), refname);
592 2690194b 2020-03-21 stsp }
593 93658fb9 2020-03-18 stsp continue;
594 2690194b 2020-03-21 stsp }
595 659e7fbd 2020-03-20 stsp
596 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
597 41b0de12 2020-03-21 stsp if (fetch_all_branches || list_refs_only) {
598 4ba14133 2020-03-20 stsp found_branch = 1;
599 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
600 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
601 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
602 4ba14133 2020-03-20 stsp break;
603 4ba14133 2020-03-20 stsp }
604 2690194b 2020-03-21 stsp if (pe == NULL) {
605 2690194b 2020-03-21 stsp if (chattygot) {
606 2690194b 2020-03-21 stsp fprintf(stderr,
607 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
608 2690194b 2020-03-21 stsp getprogname(), refname);
609 2690194b 2020-03-21 stsp }
610 4ba14133 2020-03-20 stsp continue;
611 2690194b 2020-03-21 stsp }
612 4ba14133 2020-03-20 stsp found_branch = 1;
613 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
614 2690194b 2020-03-21 stsp if (!match_branch(refname, default_branch)) {
615 2690194b 2020-03-21 stsp if (chattygot) {
616 2690194b 2020-03-21 stsp fprintf(stderr,
617 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
618 2690194b 2020-03-21 stsp getprogname(), refname);
619 2690194b 2020-03-21 stsp }
620 4ba14133 2020-03-20 stsp continue;
621 2690194b 2020-03-21 stsp }
622 4ba14133 2020-03-20 stsp found_branch = 1;
623 4ba14133 2020-03-20 stsp }
624 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
625 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(wanted_refs)) {
626 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
627 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
628 0e4002ca 2020-03-21 stsp break;
629 0e4002ca 2020-03-21 stsp }
630 0e4002ca 2020-03-21 stsp if (pe == NULL) {
631 0e4002ca 2020-03-21 stsp if (chattygot) {
632 0e4002ca 2020-03-21 stsp fprintf(stderr,
633 0e4002ca 2020-03-21 stsp "%s: ignoring %s\n",
634 0e4002ca 2020-03-21 stsp getprogname(), refname);
635 0e4002ca 2020-03-21 stsp }
636 0e4002ca 2020-03-21 stsp continue;
637 0e4002ca 2020-03-21 stsp }
638 0e4002ca 2020-03-21 stsp found_branch = 1;
639 0e4002ca 2020-03-21 stsp } else if (!list_refs_only) {
640 2690194b 2020-03-21 stsp if (chattygot) {
641 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
642 2690194b 2020-03-21 stsp getprogname(), refname);
643 2690194b 2020-03-21 stsp }
644 4515a796 2020-03-21 stsp continue;
645 2690194b 2020-03-21 stsp }
646 659e7fbd 2020-03-20 stsp }
647 659e7fbd 2020-03-20 stsp
648 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
649 93658fb9 2020-03-18 stsp refsz *= 2;
650 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
651 9ff10419 2020-03-18 stsp if (have == NULL) {
652 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
653 9ff10419 2020-03-18 stsp goto done;
654 9ff10419 2020-03-18 stsp }
655 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
656 9ff10419 2020-03-18 stsp if (want == NULL) {
657 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
658 9ff10419 2020-03-18 stsp goto done;
659 9ff10419 2020-03-18 stsp }
660 93658fb9 2020-03-18 stsp }
661 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
662 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
663 9ff10419 2020-03-18 stsp goto done;
664 9ff10419 2020-03-18 stsp }
665 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
666 ea7396b9 2020-03-18 stsp err = got_privsep_send_fetch_ref(ibuf, &want[nref],
667 00cd0e0a 2020-03-18 stsp refname);
668 8f2d01a6 2020-03-18 stsp if (err)
669 8f2d01a6 2020-03-18 stsp goto done;
670 858b0dfb 2020-03-20 stsp
671 2690194b 2020-03-21 stsp if (chattygot)
672 2690194b 2020-03-21 stsp fprintf(stderr, "%s: %s will be fetched\n",
673 2690194b 2020-03-21 stsp getprogname(), refname);
674 2690194b 2020-03-21 stsp if (chattygot > 1) {
675 858b0dfb 2020-03-20 stsp char *theirs, *mine;
676 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
677 858b0dfb 2020-03-20 stsp if (err)
678 858b0dfb 2020-03-20 stsp goto done;
679 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
680 858b0dfb 2020-03-20 stsp if (err) {
681 858b0dfb 2020-03-20 stsp free(theirs);
682 858b0dfb 2020-03-20 stsp goto done;
683 858b0dfb 2020-03-20 stsp }
684 2690194b 2020-03-21 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
685 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
686 858b0dfb 2020-03-20 stsp free(theirs);
687 858b0dfb 2020-03-20 stsp free(mine);
688 858b0dfb 2020-03-20 stsp }
689 93658fb9 2020-03-18 stsp nref++;
690 93658fb9 2020-03-18 stsp }
691 93658fb9 2020-03-18 stsp
692 41b0de12 2020-03-21 stsp if (list_refs_only)
693 41b0de12 2020-03-21 stsp goto done;
694 41b0de12 2020-03-21 stsp
695 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
696 659e7fbd 2020-03-20 stsp if (!found_branch) {
697 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
698 659e7fbd 2020-03-20 stsp goto done;
699 659e7fbd 2020-03-20 stsp }
700 659e7fbd 2020-03-20 stsp
701 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
702 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
703 93658fb9 2020-03-18 stsp continue;
704 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
705 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
706 406106ee 2020-03-20 stsp sent_my_capabilites ? "" : my_capabilities);
707 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
708 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
709 9ff10419 2020-03-18 stsp goto done;
710 9ff10419 2020-03-18 stsp }
711 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
712 344e4747 2020-03-18 stsp if (err)
713 9ff10419 2020-03-18 stsp goto done;
714 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
715 7848a0e1 2020-03-19 stsp nwant++;
716 93658fb9 2020-03-18 stsp }
717 38c670f1 2020-03-18 stsp err = flushpkt(fd);
718 38c670f1 2020-03-18 stsp if (err)
719 38c670f1 2020-03-18 stsp goto done;
720 7848a0e1 2020-03-19 stsp
721 3c912d14 2020-03-19 stsp if (nwant == 0)
722 7848a0e1 2020-03-19 stsp goto done;
723 7848a0e1 2020-03-19 stsp
724 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
725 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
726 93658fb9 2020-03-18 stsp continue;
727 7848a0e1 2020-03-19 stsp got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
728 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
729 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
730 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
731 9ff10419 2020-03-18 stsp goto done;
732 9ff10419 2020-03-18 stsp }
733 c20695fb 2020-03-20 stsp err = writepkt(fd, buf, n);
734 344e4747 2020-03-18 stsp if (err)
735 9ff10419 2020-03-18 stsp goto done;
736 7848a0e1 2020-03-19 stsp nhave++;
737 93658fb9 2020-03-18 stsp }
738 7848a0e1 2020-03-19 stsp
739 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
740 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
741 7848a0e1 2020-03-19 stsp
742 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
743 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
744 38c670f1 2020-03-18 stsp if (err)
745 38c670f1 2020-03-18 stsp goto done;
746 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
747 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
748 7848a0e1 2020-03-19 stsp goto done;
749 7848a0e1 2020-03-19 stsp }
750 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
751 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
752 7848a0e1 2020-03-19 stsp continue;
753 7848a0e1 2020-03-19 stsp }
754 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
755 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
756 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
757 7848a0e1 2020-03-19 stsp "unexpected message from server");
758 7848a0e1 2020-03-19 stsp goto done;
759 7848a0e1 2020-03-19 stsp }
760 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
761 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
762 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
763 7848a0e1 2020-03-19 stsp goto done;
764 7848a0e1 2020-03-19 stsp }
765 7848a0e1 2020-03-19 stsp acked++;
766 93658fb9 2020-03-18 stsp }
767 7848a0e1 2020-03-19 stsp
768 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
769 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
770 344e4747 2020-03-18 stsp if (err)
771 9ff10419 2020-03-18 stsp goto done;
772 93658fb9 2020-03-18 stsp
773 7848a0e1 2020-03-19 stsp if (nhave == 0) {
774 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
775 7848a0e1 2020-03-19 stsp if (err)
776 7848a0e1 2020-03-19 stsp goto done;
777 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
778 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
779 7848a0e1 2020-03-19 stsp "unexpected message from server");
780 7848a0e1 2020-03-19 stsp goto done;
781 7848a0e1 2020-03-19 stsp }
782 04c53c18 2020-03-18 stsp }
783 93658fb9 2020-03-18 stsp
784 858b0dfb 2020-03-20 stsp if (chattygot)
785 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
786 858b0dfb 2020-03-20 stsp
787 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
788 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
789 531c3985 2020-03-18 stsp have_sidebands = 1;
790 531c3985 2020-03-18 stsp
791 9ff10419 2020-03-18 stsp while (1) {
792 531c3985 2020-03-18 stsp ssize_t r = 0, w;
793 531c3985 2020-03-18 stsp int datalen = -1;
794 531c3985 2020-03-18 stsp
795 531c3985 2020-03-18 stsp if (have_sidebands) {
796 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
797 531c3985 2020-03-18 stsp if (err)
798 531c3985 2020-03-18 stsp goto done;
799 531c3985 2020-03-18 stsp if (datalen <= 0)
800 531c3985 2020-03-18 stsp break;
801 531c3985 2020-03-18 stsp
802 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
803 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
804 531c3985 2020-03-18 stsp if (r == -1) {
805 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
806 531c3985 2020-03-18 stsp goto done;
807 531c3985 2020-03-18 stsp }
808 531c3985 2020-03-18 stsp if (r != 1) {
809 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
810 531c3985 2020-03-18 stsp "short packet");
811 531c3985 2020-03-18 stsp goto done;
812 531c3985 2020-03-18 stsp }
813 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
814 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
815 531c3985 2020-03-18 stsp "bad packet length");
816 531c3985 2020-03-18 stsp goto done;
817 531c3985 2020-03-18 stsp }
818 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
819 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
820 531c3985 2020-03-18 stsp /* Read packfile data. */
821 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
822 531c3985 2020-03-18 stsp if (err)
823 531c3985 2020-03-18 stsp goto done;
824 531c3985 2020-03-18 stsp if (r != datalen) {
825 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
826 531c3985 2020-03-18 stsp "packet too short");
827 531c3985 2020-03-18 stsp goto done;
828 531c3985 2020-03-18 stsp }
829 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
830 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
831 531c3985 2020-03-18 stsp if (err)
832 531c3985 2020-03-18 stsp goto done;
833 531c3985 2020-03-18 stsp if (r != datalen) {
834 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
835 531c3985 2020-03-18 stsp "packet too short");
836 531c3985 2020-03-18 stsp goto done;
837 531c3985 2020-03-18 stsp }
838 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
839 531c3985 2020-03-18 stsp if (err)
840 531c3985 2020-03-18 stsp goto done;
841 531c3985 2020-03-18 stsp continue;
842 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
843 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
844 531c3985 2020-03-18 stsp if (err)
845 531c3985 2020-03-18 stsp goto done;
846 531c3985 2020-03-18 stsp if (r != datalen) {
847 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
848 531c3985 2020-03-18 stsp "packet too short");
849 531c3985 2020-03-18 stsp goto done;
850 531c3985 2020-03-18 stsp }
851 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
852 531c3985 2020-03-18 stsp goto done;
853 531c3985 2020-03-18 stsp } else {
854 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
855 531c3985 2020-03-18 stsp "unknown side-band received from server");
856 531c3985 2020-03-18 stsp goto done;
857 531c3985 2020-03-18 stsp }
858 531c3985 2020-03-18 stsp } else {
859 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
860 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
861 531c3985 2020-03-18 stsp if (err)
862 531c3985 2020-03-18 stsp goto done;
863 531c3985 2020-03-18 stsp if (r <= 0)
864 531c3985 2020-03-18 stsp break;
865 531c3985 2020-03-18 stsp }
866 531c3985 2020-03-18 stsp
867 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
868 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
869 9ff10419 2020-03-18 stsp if (w == -1) {
870 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
871 9ff10419 2020-03-18 stsp goto done;
872 9ff10419 2020-03-18 stsp }
873 fe53745c 2020-03-18 stsp if (w != r) {
874 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
875 9ff10419 2020-03-18 stsp goto done;
876 9ff10419 2020-03-18 stsp }
877 531c3985 2020-03-18 stsp packsz += w;
878 5672d305 2020-03-18 stsp
879 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
880 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
881 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf,
882 5672d305 2020-03-18 stsp packsz);
883 5672d305 2020-03-18 stsp if (err)
884 5672d305 2020-03-18 stsp goto done;
885 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
886 5672d305 2020-03-18 stsp }
887 93658fb9 2020-03-18 stsp }
888 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf, packsz);
889 5672d305 2020-03-18 stsp if (err)
890 5672d305 2020-03-18 stsp goto done;
891 9ff10419 2020-03-18 stsp done:
892 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
893 abe0f35f 2020-03-18 stsp free((void *)pe->path);
894 abe0f35f 2020-03-18 stsp free(pe->data);
895 abe0f35f 2020-03-18 stsp }
896 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
897 9ff10419 2020-03-18 stsp free(have);
898 9ff10419 2020-03-18 stsp free(want);
899 00cd0e0a 2020-03-18 stsp free(id_str);
900 00cd0e0a 2020-03-18 stsp free(refname);
901 00cd0e0a 2020-03-18 stsp free(server_capabilities);
902 8f2d01a6 2020-03-18 stsp return err;
903 93658fb9 2020-03-18 stsp }
904 93658fb9 2020-03-18 stsp
905 93658fb9 2020-03-18 stsp
906 93658fb9 2020-03-18 stsp int
907 93658fb9 2020-03-18 stsp main(int argc, char **argv)
908 93658fb9 2020-03-18 stsp {
909 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
910 7848a0e1 2020-03-19 stsp int fetchfd, packfd = -1, i;
911 93658fb9 2020-03-18 stsp struct got_object_id packid;
912 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
913 93658fb9 2020-03-18 stsp struct imsg imsg;
914 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
915 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
916 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
917 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
918 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
919 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
920 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
921 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
922 4ba14133 2020-03-20 stsp size_t datalen;
923 7848a0e1 2020-03-19 stsp #if 0
924 7848a0e1 2020-03-19 stsp static int attached;
925 7848a0e1 2020-03-19 stsp while (!attached)
926 7848a0e1 2020-03-19 stsp sleep (1);
927 7848a0e1 2020-03-19 stsp #endif
928 33501562 2020-03-18 stsp
929 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
930 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
931 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
932 858b0dfb 2020-03-20 stsp
933 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
934 ffb5f621 2020-03-18 stsp #ifndef PROFILE
935 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
936 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
937 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
938 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
939 ffb5f621 2020-03-18 stsp return 1;
940 ffb5f621 2020-03-18 stsp }
941 ffb5f621 2020-03-18 stsp #endif
942 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
943 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
944 93658fb9 2020-03-18 stsp err = NULL;
945 93658fb9 2020-03-18 stsp goto done;
946 93658fb9 2020-03-18 stsp }
947 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
948 93658fb9 2020-03-18 stsp goto done;
949 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
950 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
951 93658fb9 2020-03-18 stsp goto done;
952 93658fb9 2020-03-18 stsp }
953 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
954 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
955 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
956 93658fb9 2020-03-18 stsp goto done;
957 93658fb9 2020-03-18 stsp }
958 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
959 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
960 4ba14133 2020-03-20 stsp imsg_free(&imsg);
961 4ba14133 2020-03-20 stsp
962 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
963 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
964 2690194b 2020-03-21 stsp
965 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
966 7848a0e1 2020-03-19 stsp struct got_object_id *id;
967 7848a0e1 2020-03-19 stsp char *refname;
968 7848a0e1 2020-03-19 stsp
969 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
970 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
971 4ba14133 2020-03-20 stsp err = NULL;
972 4ba14133 2020-03-20 stsp goto done;
973 4ba14133 2020-03-20 stsp }
974 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
975 4ba14133 2020-03-20 stsp goto done;
976 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
977 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
978 4ba14133 2020-03-20 stsp goto done;
979 4ba14133 2020-03-20 stsp }
980 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
981 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
982 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
983 659e7fbd 2020-03-20 stsp goto done;
984 659e7fbd 2020-03-20 stsp }
985 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
986 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
987 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
988 7848a0e1 2020-03-19 stsp goto done;
989 7848a0e1 2020-03-19 stsp }
990 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
991 7848a0e1 2020-03-19 stsp if (refname == NULL) {
992 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
993 7848a0e1 2020-03-19 stsp goto done;
994 7848a0e1 2020-03-19 stsp }
995 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
996 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
997 659e7fbd 2020-03-20 stsp
998 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
999 7848a0e1 2020-03-19 stsp if (id == NULL) {
1000 7848a0e1 2020-03-19 stsp free(refname);
1001 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
1002 7848a0e1 2020-03-19 stsp goto done;
1003 7848a0e1 2020-03-19 stsp }
1004 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1005 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
1006 7848a0e1 2020-03-19 stsp if (err) {
1007 7848a0e1 2020-03-19 stsp free(refname);
1008 7848a0e1 2020-03-19 stsp free(id);
1009 7848a0e1 2020-03-19 stsp goto done;
1010 7848a0e1 2020-03-19 stsp }
1011 4ba14133 2020-03-20 stsp
1012 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1013 659e7fbd 2020-03-20 stsp }
1014 93658fb9 2020-03-18 stsp
1015 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1016 4ba14133 2020-03-20 stsp char *refname;
1017 4ba14133 2020-03-20 stsp
1018 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1019 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1020 4ba14133 2020-03-20 stsp err = NULL;
1021 4ba14133 2020-03-20 stsp goto done;
1022 4ba14133 2020-03-20 stsp }
1023 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1024 4ba14133 2020-03-20 stsp goto done;
1025 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1026 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1027 4ba14133 2020-03-20 stsp goto done;
1028 4ba14133 2020-03-20 stsp }
1029 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1030 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
1031 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1032 4ba14133 2020-03-20 stsp goto done;
1033 4ba14133 2020-03-20 stsp }
1034 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
1035 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
1036 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1037 4ba14133 2020-03-20 stsp goto done;
1038 4ba14133 2020-03-20 stsp }
1039 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
1040 4ba14133 2020-03-20 stsp if (refname == NULL) {
1041 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
1042 4ba14133 2020-03-20 stsp goto done;
1043 4ba14133 2020-03-20 stsp }
1044 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1045 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
1046 4ba14133 2020-03-20 stsp
1047 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
1048 4ba14133 2020-03-20 stsp if (err) {
1049 4ba14133 2020-03-20 stsp free(refname);
1050 4ba14133 2020-03-20 stsp goto done;
1051 4ba14133 2020-03-20 stsp }
1052 4ba14133 2020-03-20 stsp
1053 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1054 4ba14133 2020-03-20 stsp }
1055 4ba14133 2020-03-20 stsp
1056 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1057 0e4002ca 2020-03-21 stsp char *refname;
1058 0e4002ca 2020-03-21 stsp
1059 0e4002ca 2020-03-21 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1060 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1061 0e4002ca 2020-03-21 stsp err = NULL;
1062 0e4002ca 2020-03-21 stsp goto done;
1063 0e4002ca 2020-03-21 stsp }
1064 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1065 0e4002ca 2020-03-21 stsp goto done;
1066 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1067 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1068 0e4002ca 2020-03-21 stsp goto done;
1069 0e4002ca 2020-03-21 stsp }
1070 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1071 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
1072 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1073 0e4002ca 2020-03-21 stsp goto done;
1074 0e4002ca 2020-03-21 stsp }
1075 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
1076 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
1077 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1078 0e4002ca 2020-03-21 stsp goto done;
1079 0e4002ca 2020-03-21 stsp }
1080 0e4002ca 2020-03-21 stsp refname = malloc(wref.name_len + 1);
1081 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1082 0e4002ca 2020-03-21 stsp err = got_error_from_errno("malloc");
1083 0e4002ca 2020-03-21 stsp goto done;
1084 0e4002ca 2020-03-21 stsp }
1085 0e4002ca 2020-03-21 stsp memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1086 0e4002ca 2020-03-21 stsp refname[wref.name_len] = '\0';
1087 0e4002ca 2020-03-21 stsp
1088 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1089 0e4002ca 2020-03-21 stsp if (err) {
1090 0e4002ca 2020-03-21 stsp free(refname);
1091 0e4002ca 2020-03-21 stsp goto done;
1092 0e4002ca 2020-03-21 stsp }
1093 0e4002ca 2020-03-21 stsp
1094 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1095 0e4002ca 2020-03-21 stsp }
1096 0e4002ca 2020-03-21 stsp
1097 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1098 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1099 93658fb9 2020-03-18 stsp err = NULL;
1100 93658fb9 2020-03-18 stsp goto done;
1101 93658fb9 2020-03-18 stsp }
1102 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1103 93658fb9 2020-03-18 stsp goto done;
1104 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1105 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1106 93658fb9 2020-03-18 stsp goto done;
1107 93658fb9 2020-03-18 stsp }
1108 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1109 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1110 93658fb9 2020-03-18 stsp goto done;
1111 93658fb9 2020-03-18 stsp }
1112 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1113 93658fb9 2020-03-18 stsp
1114 659e7fbd 2020-03-20 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
1115 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1116 0e4002ca 2020-03-21 stsp &wanted_refs, fetch_req.list_refs_only, &ibuf);
1117 93658fb9 2020-03-18 stsp done:
1118 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
1119 7848a0e1 2020-03-19 stsp free((char *)pe->path);
1120 7848a0e1 2020-03-19 stsp free(pe->data);
1121 7848a0e1 2020-03-19 stsp }
1122 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1123 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1124 4ba14133 2020-03-20 stsp free((char *)pe->path);
1125 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1126 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1127 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1128 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1129 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1130 9ff10419 2020-03-18 stsp if (err != NULL)
1131 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1132 93658fb9 2020-03-18 stsp else
1133 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
1134 cf875574 2020-03-18 stsp if (err != NULL) {
1135 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1136 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1137 93658fb9 2020-03-18 stsp }
1138 93658fb9 2020-03-18 stsp
1139 93658fb9 2020-03-18 stsp exit(0);
1140 93658fb9 2020-03-18 stsp }