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 e70bf110 2020-03-22 stsp }
471 e70bf110 2020-03-22 stsp
472 e70bf110 2020-03-22 stsp static const struct got_error *
473 e70bf110 2020-03-22 stsp send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
474 e70bf110 2020-03-22 stsp {
475 e70bf110 2020-03-22 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
476 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
477 e70bf110 2020-03-22 stsp
478 e70bf110 2020-03-22 stsp if (msglen == 0)
479 e70bf110 2020-03-22 stsp return NULL;
480 e70bf110 2020-03-22 stsp
481 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
482 e70bf110 2020-03-22 stsp msg, msglen) == -1)
483 e70bf110 2020-03-22 stsp return got_error_from_errno(
484 e70bf110 2020-03-22 stsp "imsg_compose FETCH_SERVER_PROGRESS");
485 e70bf110 2020-03-22 stsp
486 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
487 531c3985 2020-03-18 stsp }
488 531c3985 2020-03-18 stsp
489 531c3985 2020-03-18 stsp static const struct got_error *
490 e70bf110 2020-03-22 stsp send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
491 e70bf110 2020-03-22 stsp {
492 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
493 e70bf110 2020-03-22 stsp &bytes, sizeof(bytes)) == -1)
494 e70bf110 2020-03-22 stsp return got_error_from_errno(
495 e70bf110 2020-03-22 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
496 e70bf110 2020-03-22 stsp
497 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
498 e70bf110 2020-03-22 stsp }
499 e70bf110 2020-03-22 stsp
500 e70bf110 2020-03-22 stsp static const struct got_error *
501 e70bf110 2020-03-22 stsp send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
502 e70bf110 2020-03-22 stsp {
503 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
504 e70bf110 2020-03-22 stsp hash.sha1, SHA1_DIGEST_LENGTH) == -1)
505 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
506 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
507 e70bf110 2020-03-22 stsp }
508 e70bf110 2020-03-22 stsp
509 e70bf110 2020-03-22 stsp
510 e70bf110 2020-03-22 stsp
511 e70bf110 2020-03-22 stsp static const struct got_error *
512 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
513 531c3985 2020-03-18 stsp {
514 531c3985 2020-03-18 stsp int i;
515 531c3985 2020-03-18 stsp
516 531c3985 2020-03-18 stsp if (len == 0)
517 531c3985 2020-03-18 stsp return NULL;
518 531c3985 2020-03-18 stsp
519 531c3985 2020-03-18 stsp /*
520 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
521 531c3985 2020-03-18 stsp * Server may send up to 64k.
522 531c3985 2020-03-18 stsp */
523 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
524 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
525 531c3985 2020-03-18 stsp
526 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
527 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
528 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
529 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
530 531c3985 2020-03-18 stsp continue;
531 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
532 531c3985 2020-03-18 stsp "non-printable progress message received from server");
533 531c3985 2020-03-18 stsp }
534 531c3985 2020-03-18 stsp
535 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
536 8a29a085 2020-03-18 stsp }
537 abe0f35f 2020-03-18 stsp
538 8a29a085 2020-03-18 stsp static const struct got_error *
539 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
540 531c3985 2020-03-18 stsp {
541 531c3985 2020-03-18 stsp static char msg[1024];
542 531c3985 2020-03-18 stsp int i;
543 531c3985 2020-03-18 stsp
544 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
545 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
546 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
547 531c3985 2020-03-18 stsp "non-printable error message received from server");
548 531c3985 2020-03-18 stsp msg[i] = buf[i];
549 531c3985 2020-03-18 stsp }
550 531c3985 2020-03-18 stsp msg[i] = '\0';
551 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
552 531c3985 2020-03-18 stsp }
553 531c3985 2020-03-18 stsp
554 531c3985 2020-03-18 stsp static const struct got_error *
555 e70bf110 2020-03-22 stsp send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
556 e70bf110 2020-03-22 stsp {
557 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
558 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
559 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
560 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
561 e70bf110 2020-03-22 stsp
562 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
563 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
564 e70bf110 2020-03-22 stsp const char *target = pe->data;
565 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
566 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
567 e70bf110 2020-03-22 stsp nsymrefs++;
568 e70bf110 2020-03-22 stsp }
569 e70bf110 2020-03-22 stsp
570 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
571 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
572 e70bf110 2020-03-22 stsp
573 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
574 e70bf110 2020-03-22 stsp if (wbuf == NULL)
575 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
576 e70bf110 2020-03-22 stsp
577 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
578 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
579 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
580 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
581 e70bf110 2020-03-22 stsp return err;
582 e70bf110 2020-03-22 stsp }
583 e70bf110 2020-03-22 stsp
584 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
585 e70bf110 2020-03-22 stsp const char *name = pe->path;
586 e70bf110 2020-03-22 stsp size_t name_len = pe->path_len;
587 e70bf110 2020-03-22 stsp const char *target = pe->data;
588 e70bf110 2020-03-22 stsp size_t target_len = strlen(target);
589 e70bf110 2020-03-22 stsp
590 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
591 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
592 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
593 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
594 e70bf110 2020-03-22 stsp return err;
595 e70bf110 2020-03-22 stsp }
596 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
597 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
598 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
599 e70bf110 2020-03-22 stsp return err;
600 e70bf110 2020-03-22 stsp }
601 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, name, name_len) == -1) {
602 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
603 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
604 e70bf110 2020-03-22 stsp return err;
605 e70bf110 2020-03-22 stsp }
606 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, target, target_len) == -1) {
607 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
608 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
609 e70bf110 2020-03-22 stsp return err;
610 e70bf110 2020-03-22 stsp }
611 e70bf110 2020-03-22 stsp }
612 e70bf110 2020-03-22 stsp
613 e70bf110 2020-03-22 stsp wbuf->fd = -1;
614 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
615 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
616 e70bf110 2020-03-22 stsp }
617 e70bf110 2020-03-22 stsp
618 e70bf110 2020-03-22 stsp static const struct got_error *
619 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
620 e70bf110 2020-03-22 stsp const char *refname)
621 e70bf110 2020-03-22 stsp {
622 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
623 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
624 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
625 e70bf110 2020-03-22 stsp
626 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
627 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
628 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
629 e70bf110 2020-03-22 stsp
630 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
631 e70bf110 2020-03-22 stsp if (wbuf == NULL)
632 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
633 e70bf110 2020-03-22 stsp
634 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
635 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
636 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
637 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
638 e70bf110 2020-03-22 stsp return err;
639 e70bf110 2020-03-22 stsp }
640 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
641 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
642 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
643 e70bf110 2020-03-22 stsp return err;
644 e70bf110 2020-03-22 stsp }
645 e70bf110 2020-03-22 stsp
646 e70bf110 2020-03-22 stsp wbuf->fd = -1;
647 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
648 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
649 e70bf110 2020-03-22 stsp }
650 e70bf110 2020-03-22 stsp
651 e70bf110 2020-03-22 stsp
652 e70bf110 2020-03-22 stsp static const struct got_error *
653 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
654 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
655 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
656 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
657 41b0de12 2020-03-21 stsp struct imsgbuf *ibuf)
658 93658fb9 2020-03-18 stsp {
659 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
660 f1c6967f 2020-03-19 stsp char buf[GOT_FETCH_PKTMAX];
661 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
662 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
663 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
664 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
665 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
666 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
667 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
668 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
669 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
670 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
671 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
672 659e7fbd 2020-03-20 stsp int found_branch = 0;
673 93658fb9 2020-03-18 stsp
674 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
675 abe0f35f 2020-03-18 stsp
676 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
677 9ff10419 2020-03-18 stsp if (have == NULL)
678 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
679 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
680 9ff10419 2020-03-18 stsp if (want == NULL) {
681 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
682 9ff10419 2020-03-18 stsp goto done;
683 9ff10419 2020-03-18 stsp }
684 9ff10419 2020-03-18 stsp while (1) {
685 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
686 fe53745c 2020-03-18 stsp if (err)
687 9ff10419 2020-03-18 stsp goto done;
688 9ff10419 2020-03-18 stsp if (n == 0)
689 93658fb9 2020-03-18 stsp break;
690 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
691 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
692 9ff10419 2020-03-18 stsp goto done;
693 9ff10419 2020-03-18 stsp }
694 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
695 00cd0e0a 2020-03-18 stsp buf, n);
696 0d0a341c 2020-03-18 stsp if (err)
697 9ff10419 2020-03-18 stsp goto done;
698 8a29a085 2020-03-18 stsp if (is_firstpkt) {
699 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
700 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
701 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
702 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
703 00cd0e0a 2020-03-18 stsp server_capabilities);
704 8a29a085 2020-03-18 stsp if (err)
705 8a29a085 2020-03-18 stsp goto done;
706 858b0dfb 2020-03-20 stsp if (chattygot)
707 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
708 858b0dfb 2020-03-20 stsp getprogname(), my_capabilities);
709 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
710 abe0f35f 2020-03-18 stsp if (err)
711 abe0f35f 2020-03-18 stsp goto done;
712 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
713 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
714 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
715 659e7fbd 2020-03-20 stsp const char *name = pe->path;
716 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
717 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
718 659e7fbd 2020-03-20 stsp continue;
719 659e7fbd 2020-03-20 stsp default_branch = symref_target;
720 659e7fbd 2020-03-20 stsp break;
721 659e7fbd 2020-03-20 stsp }
722 659e7fbd 2020-03-20 stsp }
723 7848a0e1 2020-03-19 stsp continue;
724 8a29a085 2020-03-18 stsp }
725 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
726 2690194b 2020-03-21 stsp if (chattygot) {
727 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
728 2690194b 2020-03-21 stsp getprogname(), refname);
729 2690194b 2020-03-21 stsp }
730 93658fb9 2020-03-18 stsp continue;
731 2690194b 2020-03-21 stsp }
732 659e7fbd 2020-03-20 stsp
733 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
734 41b0de12 2020-03-21 stsp if (fetch_all_branches || list_refs_only) {
735 4ba14133 2020-03-20 stsp found_branch = 1;
736 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
737 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
738 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
739 4ba14133 2020-03-20 stsp break;
740 4ba14133 2020-03-20 stsp }
741 2690194b 2020-03-21 stsp if (pe == NULL) {
742 2690194b 2020-03-21 stsp if (chattygot) {
743 2690194b 2020-03-21 stsp fprintf(stderr,
744 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
745 2690194b 2020-03-21 stsp getprogname(), refname);
746 2690194b 2020-03-21 stsp }
747 4ba14133 2020-03-20 stsp continue;
748 2690194b 2020-03-21 stsp }
749 4ba14133 2020-03-20 stsp found_branch = 1;
750 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
751 2690194b 2020-03-21 stsp if (!match_branch(refname, default_branch)) {
752 2690194b 2020-03-21 stsp if (chattygot) {
753 2690194b 2020-03-21 stsp fprintf(stderr,
754 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
755 2690194b 2020-03-21 stsp getprogname(), refname);
756 2690194b 2020-03-21 stsp }
757 4ba14133 2020-03-20 stsp continue;
758 2690194b 2020-03-21 stsp }
759 4ba14133 2020-03-20 stsp found_branch = 1;
760 4ba14133 2020-03-20 stsp }
761 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
762 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(wanted_refs)) {
763 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
764 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
765 0e4002ca 2020-03-21 stsp break;
766 0e4002ca 2020-03-21 stsp }
767 0e4002ca 2020-03-21 stsp if (pe == NULL) {
768 0e4002ca 2020-03-21 stsp if (chattygot) {
769 0e4002ca 2020-03-21 stsp fprintf(stderr,
770 0e4002ca 2020-03-21 stsp "%s: ignoring %s\n",
771 0e4002ca 2020-03-21 stsp getprogname(), refname);
772 0e4002ca 2020-03-21 stsp }
773 0e4002ca 2020-03-21 stsp continue;
774 0e4002ca 2020-03-21 stsp }
775 0e4002ca 2020-03-21 stsp found_branch = 1;
776 0e4002ca 2020-03-21 stsp } else if (!list_refs_only) {
777 2690194b 2020-03-21 stsp if (chattygot) {
778 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
779 2690194b 2020-03-21 stsp getprogname(), refname);
780 2690194b 2020-03-21 stsp }
781 4515a796 2020-03-21 stsp continue;
782 2690194b 2020-03-21 stsp }
783 659e7fbd 2020-03-20 stsp }
784 659e7fbd 2020-03-20 stsp
785 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
786 93658fb9 2020-03-18 stsp refsz *= 2;
787 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
788 9ff10419 2020-03-18 stsp if (have == NULL) {
789 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
790 9ff10419 2020-03-18 stsp goto done;
791 9ff10419 2020-03-18 stsp }
792 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
793 9ff10419 2020-03-18 stsp if (want == NULL) {
794 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
795 9ff10419 2020-03-18 stsp goto done;
796 9ff10419 2020-03-18 stsp }
797 93658fb9 2020-03-18 stsp }
798 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
799 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
800 9ff10419 2020-03-18 stsp goto done;
801 9ff10419 2020-03-18 stsp }
802 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
803 e70bf110 2020-03-22 stsp err = send_fetch_ref(ibuf, &want[nref], refname);
804 8f2d01a6 2020-03-18 stsp if (err)
805 8f2d01a6 2020-03-18 stsp goto done;
806 858b0dfb 2020-03-20 stsp
807 2690194b 2020-03-21 stsp if (chattygot)
808 2690194b 2020-03-21 stsp fprintf(stderr, "%s: %s will be fetched\n",
809 2690194b 2020-03-21 stsp getprogname(), refname);
810 2690194b 2020-03-21 stsp if (chattygot > 1) {
811 858b0dfb 2020-03-20 stsp char *theirs, *mine;
812 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
813 858b0dfb 2020-03-20 stsp if (err)
814 858b0dfb 2020-03-20 stsp goto done;
815 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
816 858b0dfb 2020-03-20 stsp if (err) {
817 858b0dfb 2020-03-20 stsp free(theirs);
818 858b0dfb 2020-03-20 stsp goto done;
819 858b0dfb 2020-03-20 stsp }
820 2690194b 2020-03-21 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
821 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
822 858b0dfb 2020-03-20 stsp free(theirs);
823 858b0dfb 2020-03-20 stsp free(mine);
824 858b0dfb 2020-03-20 stsp }
825 93658fb9 2020-03-18 stsp nref++;
826 93658fb9 2020-03-18 stsp }
827 93658fb9 2020-03-18 stsp
828 41b0de12 2020-03-21 stsp if (list_refs_only)
829 41b0de12 2020-03-21 stsp goto done;
830 41b0de12 2020-03-21 stsp
831 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
832 659e7fbd 2020-03-20 stsp if (!found_branch) {
833 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
834 659e7fbd 2020-03-20 stsp goto done;
835 659e7fbd 2020-03-20 stsp }
836 659e7fbd 2020-03-20 stsp
837 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
838 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
839 93658fb9 2020-03-18 stsp continue;
840 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
841 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
842 406106ee 2020-03-20 stsp sent_my_capabilites ? "" : my_capabilities);
843 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
844 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
845 9ff10419 2020-03-18 stsp goto done;
846 9ff10419 2020-03-18 stsp }
847 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
848 344e4747 2020-03-18 stsp if (err)
849 9ff10419 2020-03-18 stsp goto done;
850 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
851 7848a0e1 2020-03-19 stsp nwant++;
852 93658fb9 2020-03-18 stsp }
853 38c670f1 2020-03-18 stsp err = flushpkt(fd);
854 38c670f1 2020-03-18 stsp if (err)
855 38c670f1 2020-03-18 stsp goto done;
856 7848a0e1 2020-03-19 stsp
857 3c912d14 2020-03-19 stsp if (nwant == 0)
858 7848a0e1 2020-03-19 stsp goto done;
859 7848a0e1 2020-03-19 stsp
860 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
861 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
862 93658fb9 2020-03-18 stsp continue;
863 7848a0e1 2020-03-19 stsp got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
864 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
865 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
866 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
867 9ff10419 2020-03-18 stsp goto done;
868 9ff10419 2020-03-18 stsp }
869 c20695fb 2020-03-20 stsp err = writepkt(fd, buf, n);
870 344e4747 2020-03-18 stsp if (err)
871 9ff10419 2020-03-18 stsp goto done;
872 7848a0e1 2020-03-19 stsp nhave++;
873 93658fb9 2020-03-18 stsp }
874 7848a0e1 2020-03-19 stsp
875 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
876 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
877 7848a0e1 2020-03-19 stsp
878 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
879 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
880 38c670f1 2020-03-18 stsp if (err)
881 38c670f1 2020-03-18 stsp goto done;
882 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
883 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
884 7848a0e1 2020-03-19 stsp goto done;
885 7848a0e1 2020-03-19 stsp }
886 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
887 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
888 7848a0e1 2020-03-19 stsp continue;
889 7848a0e1 2020-03-19 stsp }
890 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
891 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
892 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
893 7848a0e1 2020-03-19 stsp "unexpected message from server");
894 7848a0e1 2020-03-19 stsp goto done;
895 7848a0e1 2020-03-19 stsp }
896 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
897 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
898 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
899 7848a0e1 2020-03-19 stsp goto done;
900 7848a0e1 2020-03-19 stsp }
901 7848a0e1 2020-03-19 stsp acked++;
902 93658fb9 2020-03-18 stsp }
903 7848a0e1 2020-03-19 stsp
904 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
905 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
906 344e4747 2020-03-18 stsp if (err)
907 9ff10419 2020-03-18 stsp goto done;
908 93658fb9 2020-03-18 stsp
909 7848a0e1 2020-03-19 stsp if (nhave == 0) {
910 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
911 7848a0e1 2020-03-19 stsp if (err)
912 7848a0e1 2020-03-19 stsp goto done;
913 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
914 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
915 7848a0e1 2020-03-19 stsp "unexpected message from server");
916 7848a0e1 2020-03-19 stsp goto done;
917 7848a0e1 2020-03-19 stsp }
918 04c53c18 2020-03-18 stsp }
919 93658fb9 2020-03-18 stsp
920 858b0dfb 2020-03-20 stsp if (chattygot)
921 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
922 858b0dfb 2020-03-20 stsp
923 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
924 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
925 531c3985 2020-03-18 stsp have_sidebands = 1;
926 531c3985 2020-03-18 stsp
927 9ff10419 2020-03-18 stsp while (1) {
928 531c3985 2020-03-18 stsp ssize_t r = 0, w;
929 531c3985 2020-03-18 stsp int datalen = -1;
930 531c3985 2020-03-18 stsp
931 531c3985 2020-03-18 stsp if (have_sidebands) {
932 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
933 531c3985 2020-03-18 stsp if (err)
934 531c3985 2020-03-18 stsp goto done;
935 531c3985 2020-03-18 stsp if (datalen <= 0)
936 531c3985 2020-03-18 stsp break;
937 531c3985 2020-03-18 stsp
938 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
939 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
940 531c3985 2020-03-18 stsp if (r == -1) {
941 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
942 531c3985 2020-03-18 stsp goto done;
943 531c3985 2020-03-18 stsp }
944 531c3985 2020-03-18 stsp if (r != 1) {
945 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
946 531c3985 2020-03-18 stsp "short packet");
947 531c3985 2020-03-18 stsp goto done;
948 531c3985 2020-03-18 stsp }
949 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
950 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
951 531c3985 2020-03-18 stsp "bad packet length");
952 531c3985 2020-03-18 stsp goto done;
953 531c3985 2020-03-18 stsp }
954 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
955 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
956 531c3985 2020-03-18 stsp /* Read packfile data. */
957 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
958 531c3985 2020-03-18 stsp if (err)
959 531c3985 2020-03-18 stsp goto done;
960 531c3985 2020-03-18 stsp if (r != datalen) {
961 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
962 531c3985 2020-03-18 stsp "packet too short");
963 531c3985 2020-03-18 stsp goto done;
964 531c3985 2020-03-18 stsp }
965 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
966 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
967 531c3985 2020-03-18 stsp if (err)
968 531c3985 2020-03-18 stsp goto done;
969 531c3985 2020-03-18 stsp if (r != datalen) {
970 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
971 531c3985 2020-03-18 stsp "packet too short");
972 531c3985 2020-03-18 stsp goto done;
973 531c3985 2020-03-18 stsp }
974 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
975 531c3985 2020-03-18 stsp if (err)
976 531c3985 2020-03-18 stsp goto done;
977 531c3985 2020-03-18 stsp continue;
978 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
979 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
980 531c3985 2020-03-18 stsp if (err)
981 531c3985 2020-03-18 stsp goto done;
982 531c3985 2020-03-18 stsp if (r != datalen) {
983 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
984 531c3985 2020-03-18 stsp "packet too short");
985 531c3985 2020-03-18 stsp goto done;
986 531c3985 2020-03-18 stsp }
987 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
988 531c3985 2020-03-18 stsp goto done;
989 531c3985 2020-03-18 stsp } else {
990 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
991 531c3985 2020-03-18 stsp "unknown side-band received from server");
992 531c3985 2020-03-18 stsp goto done;
993 531c3985 2020-03-18 stsp }
994 531c3985 2020-03-18 stsp } else {
995 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
996 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
997 531c3985 2020-03-18 stsp if (err)
998 531c3985 2020-03-18 stsp goto done;
999 531c3985 2020-03-18 stsp if (r <= 0)
1000 531c3985 2020-03-18 stsp break;
1001 531c3985 2020-03-18 stsp }
1002 531c3985 2020-03-18 stsp
1003 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
1004 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
1005 9ff10419 2020-03-18 stsp if (w == -1) {
1006 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
1007 9ff10419 2020-03-18 stsp goto done;
1008 9ff10419 2020-03-18 stsp }
1009 fe53745c 2020-03-18 stsp if (w != r) {
1010 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
1011 9ff10419 2020-03-18 stsp goto done;
1012 9ff10419 2020-03-18 stsp }
1013 531c3985 2020-03-18 stsp packsz += w;
1014 5672d305 2020-03-18 stsp
1015 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
1016 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
1017 e70bf110 2020-03-22 stsp err = send_fetch_download_progress(ibuf, packsz);
1018 5672d305 2020-03-18 stsp if (err)
1019 5672d305 2020-03-18 stsp goto done;
1020 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
1021 5672d305 2020-03-18 stsp }
1022 93658fb9 2020-03-18 stsp }
1023 e70bf110 2020-03-22 stsp err = send_fetch_download_progress(ibuf, packsz);
1024 5672d305 2020-03-18 stsp if (err)
1025 5672d305 2020-03-18 stsp goto done;
1026 9ff10419 2020-03-18 stsp done:
1027 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1028 abe0f35f 2020-03-18 stsp free((void *)pe->path);
1029 abe0f35f 2020-03-18 stsp free(pe->data);
1030 abe0f35f 2020-03-18 stsp }
1031 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
1032 9ff10419 2020-03-18 stsp free(have);
1033 9ff10419 2020-03-18 stsp free(want);
1034 00cd0e0a 2020-03-18 stsp free(id_str);
1035 00cd0e0a 2020-03-18 stsp free(refname);
1036 00cd0e0a 2020-03-18 stsp free(server_capabilities);
1037 8f2d01a6 2020-03-18 stsp return err;
1038 93658fb9 2020-03-18 stsp }
1039 93658fb9 2020-03-18 stsp
1040 93658fb9 2020-03-18 stsp
1041 93658fb9 2020-03-18 stsp int
1042 93658fb9 2020-03-18 stsp main(int argc, char **argv)
1043 93658fb9 2020-03-18 stsp {
1044 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
1045 7848a0e1 2020-03-19 stsp int fetchfd, packfd = -1, i;
1046 93658fb9 2020-03-18 stsp struct got_object_id packid;
1047 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
1048 93658fb9 2020-03-18 stsp struct imsg imsg;
1049 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
1050 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
1051 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
1052 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1053 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
1054 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
1055 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
1056 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
1057 4ba14133 2020-03-20 stsp size_t datalen;
1058 7848a0e1 2020-03-19 stsp #if 0
1059 7848a0e1 2020-03-19 stsp static int attached;
1060 7848a0e1 2020-03-19 stsp while (!attached)
1061 7848a0e1 2020-03-19 stsp sleep (1);
1062 7848a0e1 2020-03-19 stsp #endif
1063 33501562 2020-03-18 stsp
1064 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
1065 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1066 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1067 858b0dfb 2020-03-20 stsp
1068 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1069 ffb5f621 2020-03-18 stsp #ifndef PROFILE
1070 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
1071 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
1072 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
1073 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1074 ffb5f621 2020-03-18 stsp return 1;
1075 ffb5f621 2020-03-18 stsp }
1076 ffb5f621 2020-03-18 stsp #endif
1077 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1078 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1079 93658fb9 2020-03-18 stsp err = NULL;
1080 93658fb9 2020-03-18 stsp goto done;
1081 93658fb9 2020-03-18 stsp }
1082 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1083 93658fb9 2020-03-18 stsp goto done;
1084 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
1085 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1086 93658fb9 2020-03-18 stsp goto done;
1087 93658fb9 2020-03-18 stsp }
1088 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1089 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
1090 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1091 93658fb9 2020-03-18 stsp goto done;
1092 93658fb9 2020-03-18 stsp }
1093 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
1094 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
1095 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1096 4ba14133 2020-03-20 stsp
1097 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
1098 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
1099 2690194b 2020-03-21 stsp
1100 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
1101 7848a0e1 2020-03-19 stsp struct got_object_id *id;
1102 7848a0e1 2020-03-19 stsp char *refname;
1103 7848a0e1 2020-03-19 stsp
1104 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1105 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1106 4ba14133 2020-03-20 stsp err = NULL;
1107 4ba14133 2020-03-20 stsp goto done;
1108 4ba14133 2020-03-20 stsp }
1109 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1110 4ba14133 2020-03-20 stsp goto done;
1111 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
1112 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1113 4ba14133 2020-03-20 stsp goto done;
1114 4ba14133 2020-03-20 stsp }
1115 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1116 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
1117 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1118 659e7fbd 2020-03-20 stsp goto done;
1119 659e7fbd 2020-03-20 stsp }
1120 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
1121 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
1122 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1123 7848a0e1 2020-03-19 stsp goto done;
1124 7848a0e1 2020-03-19 stsp }
1125 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
1126 7848a0e1 2020-03-19 stsp if (refname == NULL) {
1127 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
1128 7848a0e1 2020-03-19 stsp goto done;
1129 7848a0e1 2020-03-19 stsp }
1130 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
1131 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
1132 659e7fbd 2020-03-20 stsp
1133 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
1134 7848a0e1 2020-03-19 stsp if (id == NULL) {
1135 7848a0e1 2020-03-19 stsp free(refname);
1136 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
1137 7848a0e1 2020-03-19 stsp goto done;
1138 7848a0e1 2020-03-19 stsp }
1139 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1140 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
1141 7848a0e1 2020-03-19 stsp if (err) {
1142 7848a0e1 2020-03-19 stsp free(refname);
1143 7848a0e1 2020-03-19 stsp free(id);
1144 7848a0e1 2020-03-19 stsp goto done;
1145 7848a0e1 2020-03-19 stsp }
1146 4ba14133 2020-03-20 stsp
1147 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1148 659e7fbd 2020-03-20 stsp }
1149 93658fb9 2020-03-18 stsp
1150 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1151 4ba14133 2020-03-20 stsp char *refname;
1152 4ba14133 2020-03-20 stsp
1153 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1154 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1155 4ba14133 2020-03-20 stsp err = NULL;
1156 4ba14133 2020-03-20 stsp goto done;
1157 4ba14133 2020-03-20 stsp }
1158 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1159 4ba14133 2020-03-20 stsp goto done;
1160 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1161 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1162 4ba14133 2020-03-20 stsp goto done;
1163 4ba14133 2020-03-20 stsp }
1164 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1165 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
1166 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1167 4ba14133 2020-03-20 stsp goto done;
1168 4ba14133 2020-03-20 stsp }
1169 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
1170 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
1171 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1172 4ba14133 2020-03-20 stsp goto done;
1173 4ba14133 2020-03-20 stsp }
1174 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
1175 4ba14133 2020-03-20 stsp if (refname == NULL) {
1176 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
1177 4ba14133 2020-03-20 stsp goto done;
1178 4ba14133 2020-03-20 stsp }
1179 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1180 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
1181 4ba14133 2020-03-20 stsp
1182 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
1183 4ba14133 2020-03-20 stsp if (err) {
1184 4ba14133 2020-03-20 stsp free(refname);
1185 4ba14133 2020-03-20 stsp goto done;
1186 4ba14133 2020-03-20 stsp }
1187 4ba14133 2020-03-20 stsp
1188 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1189 4ba14133 2020-03-20 stsp }
1190 4ba14133 2020-03-20 stsp
1191 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1192 0e4002ca 2020-03-21 stsp char *refname;
1193 0e4002ca 2020-03-21 stsp
1194 0e4002ca 2020-03-21 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1195 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1196 0e4002ca 2020-03-21 stsp err = NULL;
1197 0e4002ca 2020-03-21 stsp goto done;
1198 0e4002ca 2020-03-21 stsp }
1199 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1200 0e4002ca 2020-03-21 stsp goto done;
1201 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1202 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1203 0e4002ca 2020-03-21 stsp goto done;
1204 0e4002ca 2020-03-21 stsp }
1205 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1206 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
1207 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1208 0e4002ca 2020-03-21 stsp goto done;
1209 0e4002ca 2020-03-21 stsp }
1210 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
1211 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
1212 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1213 0e4002ca 2020-03-21 stsp goto done;
1214 0e4002ca 2020-03-21 stsp }
1215 0e4002ca 2020-03-21 stsp refname = malloc(wref.name_len + 1);
1216 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1217 0e4002ca 2020-03-21 stsp err = got_error_from_errno("malloc");
1218 0e4002ca 2020-03-21 stsp goto done;
1219 0e4002ca 2020-03-21 stsp }
1220 0e4002ca 2020-03-21 stsp memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1221 0e4002ca 2020-03-21 stsp refname[wref.name_len] = '\0';
1222 0e4002ca 2020-03-21 stsp
1223 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1224 0e4002ca 2020-03-21 stsp if (err) {
1225 0e4002ca 2020-03-21 stsp free(refname);
1226 0e4002ca 2020-03-21 stsp goto done;
1227 0e4002ca 2020-03-21 stsp }
1228 0e4002ca 2020-03-21 stsp
1229 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1230 0e4002ca 2020-03-21 stsp }
1231 0e4002ca 2020-03-21 stsp
1232 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1233 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1234 93658fb9 2020-03-18 stsp err = NULL;
1235 93658fb9 2020-03-18 stsp goto done;
1236 93658fb9 2020-03-18 stsp }
1237 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1238 93658fb9 2020-03-18 stsp goto done;
1239 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1240 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1241 93658fb9 2020-03-18 stsp goto done;
1242 93658fb9 2020-03-18 stsp }
1243 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1244 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1245 93658fb9 2020-03-18 stsp goto done;
1246 93658fb9 2020-03-18 stsp }
1247 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1248 93658fb9 2020-03-18 stsp
1249 659e7fbd 2020-03-20 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
1250 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1251 0e4002ca 2020-03-21 stsp &wanted_refs, fetch_req.list_refs_only, &ibuf);
1252 93658fb9 2020-03-18 stsp done:
1253 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
1254 7848a0e1 2020-03-19 stsp free((char *)pe->path);
1255 7848a0e1 2020-03-19 stsp free(pe->data);
1256 7848a0e1 2020-03-19 stsp }
1257 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1258 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1259 4ba14133 2020-03-20 stsp free((char *)pe->path);
1260 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1261 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1262 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1263 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1264 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1265 9ff10419 2020-03-18 stsp if (err != NULL)
1266 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1267 93658fb9 2020-03-18 stsp else
1268 e70bf110 2020-03-22 stsp err = send_fetch_done(&ibuf, packid);
1269 cf875574 2020-03-18 stsp if (err != NULL) {
1270 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1271 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1272 93658fb9 2020-03-18 stsp }
1273 93658fb9 2020-03-18 stsp
1274 93658fb9 2020-03-18 stsp exit(0);
1275 93658fb9 2020-03-18 stsp }