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 858b0dfb 2020-03-20 stsp if (chattygot)
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 858b0dfb 2020-03-20 stsp if (chattygot)
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 858b0dfb 2020-03-20 stsp if (chattygot) {
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 858b0dfb 2020-03-20 stsp if (chattygot) {
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 93658fb9 2020-03-18 stsp }
253 93658fb9 2020-03-18 stsp
254 0d0a341c 2020-03-18 stsp static const struct got_error *
255 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
256 93658fb9 2020-03-18 stsp {
257 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
258 93658fb9 2020-03-18 stsp char *p;
259 0d0a341c 2020-03-18 stsp size_t i, n = 0;
260 93658fb9 2020-03-18 stsp
261 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
262 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
263 0d0a341c 2020-03-18 stsp
264 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
265 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
266 93658fb9 2020-03-18 stsp line++;
267 0d0a341c 2020-03-18 stsp n++;
268 0d0a341c 2020-03-18 stsp }
269 93658fb9 2020-03-18 stsp p = line;
270 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
271 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
272 93658fb9 2020-03-18 stsp line++;
273 0d0a341c 2020-03-18 stsp n++;
274 0d0a341c 2020-03-18 stsp }
275 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
276 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
277 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
278 0d0a341c 2020-03-18 stsp goto done;
279 0d0a341c 2020-03-18 stsp }
280 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
281 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
282 0d0a341c 2020-03-18 stsp line++;
283 0d0a341c 2020-03-18 stsp n++;
284 0d0a341c 2020-03-18 stsp }
285 93658fb9 2020-03-18 stsp }
286 0d0a341c 2020-03-18 stsp if (i <= 2)
287 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
288 0d0a341c 2020-03-18 stsp done:
289 0d0a341c 2020-03-18 stsp if (err) {
290 0d0a341c 2020-03-18 stsp int j;
291 0d0a341c 2020-03-18 stsp for (j = 0; j < i; j++)
292 0d0a341c 2020-03-18 stsp free(tokens[j]);
293 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
294 0d0a341c 2020-03-18 stsp }
295 0d0a341c 2020-03-18 stsp return err;
296 8a29a085 2020-03-18 stsp }
297 00cd0e0a 2020-03-18 stsp
298 00cd0e0a 2020-03-18 stsp static const struct got_error *
299 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
300 00cd0e0a 2020-03-18 stsp char *line, int len)
301 00cd0e0a 2020-03-18 stsp {
302 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
303 00cd0e0a 2020-03-18 stsp char *tokens[3];
304 8a29a085 2020-03-18 stsp
305 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
306 00cd0e0a 2020-03-18 stsp if (err)
307 00cd0e0a 2020-03-18 stsp return err;
308 00cd0e0a 2020-03-18 stsp
309 00cd0e0a 2020-03-18 stsp if (tokens[0])
310 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
311 00cd0e0a 2020-03-18 stsp if (tokens[1])
312 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
313 00cd0e0a 2020-03-18 stsp if (tokens[2])
314 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
315 00cd0e0a 2020-03-18 stsp
316 00cd0e0a 2020-03-18 stsp return NULL;
317 00cd0e0a 2020-03-18 stsp }
318 00cd0e0a 2020-03-18 stsp
319 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
320 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
321 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
322 531c3985 2020-03-18 stsp
323 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
324 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
325 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
326 531c3985 2020-03-18 stsp
327 531c3985 2020-03-18 stsp
328 8a29a085 2020-03-18 stsp struct got_capability {
329 8a29a085 2020-03-18 stsp const char *key;
330 8a29a085 2020-03-18 stsp const char *value;
331 8a29a085 2020-03-18 stsp };
332 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
333 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
334 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
335 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
336 8a29a085 2020-03-18 stsp };
337 8a29a085 2020-03-18 stsp
338 8a29a085 2020-03-18 stsp static const struct got_error *
339 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
340 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
341 8a29a085 2020-03-18 stsp {
342 8a29a085 2020-03-18 stsp char *equalsign;
343 8a29a085 2020-03-18 stsp char *s;
344 8a29a085 2020-03-18 stsp
345 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
346 8a29a085 2020-03-18 stsp if (equalsign) {
347 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
348 8a29a085 2020-03-18 stsp return NULL;
349 8a29a085 2020-03-18 stsp } else {
350 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
351 8a29a085 2020-03-18 stsp return NULL;
352 8a29a085 2020-03-18 stsp }
353 8a29a085 2020-03-18 stsp
354 406106ee 2020-03-20 stsp if (asprintf(&s, "%s %s%s%s",
355 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
356 8a29a085 2020-03-18 stsp mycapa->key,
357 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
358 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
359 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
360 8a29a085 2020-03-18 stsp
361 8a29a085 2020-03-18 stsp free(*my_capabilities);
362 8a29a085 2020-03-18 stsp *my_capabilities = s;
363 8a29a085 2020-03-18 stsp return NULL;
364 93658fb9 2020-03-18 stsp }
365 93658fb9 2020-03-18 stsp
366 9ff10419 2020-03-18 stsp static const struct got_error *
367 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
368 8a29a085 2020-03-18 stsp {
369 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
370 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
371 abe0f35f 2020-03-18 stsp
372 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
373 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
374 abe0f35f 2020-03-18 stsp return NULL;
375 abe0f35f 2020-03-18 stsp
376 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
377 abe0f35f 2020-03-18 stsp if (colon == NULL)
378 abe0f35f 2020-03-18 stsp return NULL;
379 abe0f35f 2020-03-18 stsp
380 abe0f35f 2020-03-18 stsp *colon = '\0';
381 abe0f35f 2020-03-18 stsp name = strdup(capa);
382 abe0f35f 2020-03-18 stsp if (name == NULL)
383 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
384 abe0f35f 2020-03-18 stsp
385 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
386 abe0f35f 2020-03-18 stsp if (target == NULL) {
387 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
388 abe0f35f 2020-03-18 stsp goto done;
389 abe0f35f 2020-03-18 stsp }
390 abe0f35f 2020-03-18 stsp
391 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
392 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
393 abe0f35f 2020-03-18 stsp done:
394 abe0f35f 2020-03-18 stsp if (err) {
395 abe0f35f 2020-03-18 stsp free(name);
396 abe0f35f 2020-03-18 stsp free(target);
397 abe0f35f 2020-03-18 stsp }
398 abe0f35f 2020-03-18 stsp return err;
399 abe0f35f 2020-03-18 stsp }
400 abe0f35f 2020-03-18 stsp
401 abe0f35f 2020-03-18 stsp static const struct got_error *
402 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
403 abe0f35f 2020-03-18 stsp char *server_capabilities)
404 abe0f35f 2020-03-18 stsp {
405 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
406 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
407 8a29a085 2020-03-18 stsp int i;
408 8a29a085 2020-03-18 stsp
409 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
410 8a29a085 2020-03-18 stsp do {
411 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
412 8a29a085 2020-03-18 stsp if (capa == NULL)
413 8a29a085 2020-03-18 stsp return NULL;
414 8a29a085 2020-03-18 stsp
415 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
416 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
417 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
418 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
419 abe0f35f 2020-03-18 stsp if (err)
420 abe0f35f 2020-03-18 stsp break;
421 abe0f35f 2020-03-18 stsp continue;
422 abe0f35f 2020-03-18 stsp }
423 abe0f35f 2020-03-18 stsp
424 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
425 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
426 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
427 8a29a085 2020-03-18 stsp if (err)
428 8a29a085 2020-03-18 stsp break;
429 8a29a085 2020-03-18 stsp }
430 8a29a085 2020-03-18 stsp } while (capa);
431 8a29a085 2020-03-18 stsp
432 406106ee 2020-03-20 stsp if (*my_capabilities == NULL) {
433 406106ee 2020-03-20 stsp *my_capabilities = strdup("");
434 406106ee 2020-03-20 stsp if (*my_capabilities == NULL)
435 406106ee 2020-03-20 stsp err = got_error_from_errno("strdup");
436 406106ee 2020-03-20 stsp }
437 8a29a085 2020-03-18 stsp return err;
438 531c3985 2020-03-18 stsp }
439 531c3985 2020-03-18 stsp
440 531c3985 2020-03-18 stsp static const struct got_error *
441 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
442 531c3985 2020-03-18 stsp {
443 531c3985 2020-03-18 stsp int i;
444 531c3985 2020-03-18 stsp
445 531c3985 2020-03-18 stsp if (len == 0)
446 531c3985 2020-03-18 stsp return NULL;
447 531c3985 2020-03-18 stsp
448 531c3985 2020-03-18 stsp /*
449 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
450 531c3985 2020-03-18 stsp * Server may send up to 64k.
451 531c3985 2020-03-18 stsp */
452 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
453 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
454 531c3985 2020-03-18 stsp
455 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
456 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
457 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
458 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
459 531c3985 2020-03-18 stsp continue;
460 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
461 531c3985 2020-03-18 stsp "non-printable progress message received from server");
462 531c3985 2020-03-18 stsp }
463 531c3985 2020-03-18 stsp
464 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
465 8a29a085 2020-03-18 stsp }
466 abe0f35f 2020-03-18 stsp
467 8a29a085 2020-03-18 stsp static const struct got_error *
468 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
469 531c3985 2020-03-18 stsp {
470 531c3985 2020-03-18 stsp static char msg[1024];
471 531c3985 2020-03-18 stsp int i;
472 531c3985 2020-03-18 stsp
473 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
474 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
475 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
476 531c3985 2020-03-18 stsp "non-printable error message received from server");
477 531c3985 2020-03-18 stsp msg[i] = buf[i];
478 531c3985 2020-03-18 stsp }
479 531c3985 2020-03-18 stsp msg[i] = '\0';
480 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
481 531c3985 2020-03-18 stsp }
482 531c3985 2020-03-18 stsp
483 531c3985 2020-03-18 stsp static const struct got_error *
484 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
485 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
486 659e7fbd 2020-03-20 stsp struct imsgbuf *ibuf)
487 93658fb9 2020-03-18 stsp {
488 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
489 f1c6967f 2020-03-19 stsp char buf[GOT_FETCH_PKTMAX];
490 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
491 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
492 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
493 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
494 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
495 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
496 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
497 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
498 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
499 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
500 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
501 659e7fbd 2020-03-20 stsp int found_branch = 0;
502 93658fb9 2020-03-18 stsp
503 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
504 abe0f35f 2020-03-18 stsp
505 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
506 9ff10419 2020-03-18 stsp if (have == NULL)
507 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
508 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
509 9ff10419 2020-03-18 stsp if (want == NULL) {
510 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
511 9ff10419 2020-03-18 stsp goto done;
512 9ff10419 2020-03-18 stsp }
513 858b0dfb 2020-03-20 stsp if (chattygot)
514 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: starting fetch\n", getprogname());
515 9ff10419 2020-03-18 stsp while (1) {
516 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
517 fe53745c 2020-03-18 stsp if (err)
518 9ff10419 2020-03-18 stsp goto done;
519 9ff10419 2020-03-18 stsp if (n == 0)
520 93658fb9 2020-03-18 stsp break;
521 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
522 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
523 9ff10419 2020-03-18 stsp goto done;
524 9ff10419 2020-03-18 stsp }
525 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
526 00cd0e0a 2020-03-18 stsp buf, n);
527 0d0a341c 2020-03-18 stsp if (err)
528 9ff10419 2020-03-18 stsp goto done;
529 8a29a085 2020-03-18 stsp if (is_firstpkt) {
530 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
531 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
532 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
533 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
534 00cd0e0a 2020-03-18 stsp server_capabilities);
535 8a29a085 2020-03-18 stsp if (err)
536 8a29a085 2020-03-18 stsp goto done;
537 858b0dfb 2020-03-20 stsp if (chattygot)
538 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: my capabilities: %s\n",
539 858b0dfb 2020-03-20 stsp getprogname(), my_capabilities);
540 abe0f35f 2020-03-18 stsp err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
541 abe0f35f 2020-03-18 stsp if (err)
542 abe0f35f 2020-03-18 stsp goto done;
543 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
544 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
545 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
546 659e7fbd 2020-03-20 stsp const char *name = pe->path;
547 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
548 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
549 659e7fbd 2020-03-20 stsp continue;
550 659e7fbd 2020-03-20 stsp default_branch = symref_target;
551 659e7fbd 2020-03-20 stsp break;
552 659e7fbd 2020-03-20 stsp }
553 659e7fbd 2020-03-20 stsp }
554 7848a0e1 2020-03-19 stsp continue;
555 8a29a085 2020-03-18 stsp }
556 00cd0e0a 2020-03-18 stsp if (strstr(refname, "^{}"))
557 93658fb9 2020-03-18 stsp continue;
558 659e7fbd 2020-03-20 stsp
559 659e7fbd 2020-03-20 stsp if (chattygot)
560 659e7fbd 2020-03-20 stsp fprintf(stderr, "%s: discovered remote ref %s\n",
561 659e7fbd 2020-03-20 stsp getprogname(), refname);
562 659e7fbd 2020-03-20 stsp
563 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
564 659e7fbd 2020-03-20 stsp if (default_branch != NULL &&
565 659e7fbd 2020-03-20 stsp !match_branch(refname, default_branch))
566 659e7fbd 2020-03-20 stsp continue;
567 659e7fbd 2020-03-20 stsp found_branch = 1;
568 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
569 659e7fbd 2020-03-20 stsp if (chattygot) {
570 659e7fbd 2020-03-20 stsp fprintf(stderr, "%s: ignoring '%s' which is "
571 659e7fbd 2020-03-20 stsp "neither a branch nor a tag\n",
572 659e7fbd 2020-03-20 stsp getprogname(), refname);
573 659e7fbd 2020-03-20 stsp }
574 93658fb9 2020-03-18 stsp continue;
575 659e7fbd 2020-03-20 stsp }
576 659e7fbd 2020-03-20 stsp
577 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
578 93658fb9 2020-03-18 stsp refsz *= 2;
579 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
580 9ff10419 2020-03-18 stsp if (have == NULL) {
581 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
582 9ff10419 2020-03-18 stsp goto done;
583 9ff10419 2020-03-18 stsp }
584 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
585 9ff10419 2020-03-18 stsp if (want == NULL) {
586 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
587 9ff10419 2020-03-18 stsp goto done;
588 9ff10419 2020-03-18 stsp }
589 93658fb9 2020-03-18 stsp }
590 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
591 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
592 9ff10419 2020-03-18 stsp goto done;
593 9ff10419 2020-03-18 stsp }
594 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
595 ea7396b9 2020-03-18 stsp err = got_privsep_send_fetch_ref(ibuf, &want[nref],
596 00cd0e0a 2020-03-18 stsp refname);
597 8f2d01a6 2020-03-18 stsp if (err)
598 8f2d01a6 2020-03-18 stsp goto done;
599 858b0dfb 2020-03-20 stsp
600 858b0dfb 2020-03-20 stsp if (chattygot) {
601 858b0dfb 2020-03-20 stsp char *theirs, *mine;
602 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
603 858b0dfb 2020-03-20 stsp if (err)
604 858b0dfb 2020-03-20 stsp goto done;
605 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
606 858b0dfb 2020-03-20 stsp if (err) {
607 858b0dfb 2020-03-20 stsp free(theirs);
608 858b0dfb 2020-03-20 stsp goto done;
609 858b0dfb 2020-03-20 stsp }
610 659e7fbd 2020-03-20 stsp fprintf(stderr, "%s: %s will be fetched\n",
611 858b0dfb 2020-03-20 stsp getprogname(), refname);
612 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: theirs=%s\n%s: mine=%s\n",
613 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
614 858b0dfb 2020-03-20 stsp free(theirs);
615 858b0dfb 2020-03-20 stsp free(mine);
616 858b0dfb 2020-03-20 stsp }
617 93658fb9 2020-03-18 stsp nref++;
618 93658fb9 2020-03-18 stsp }
619 93658fb9 2020-03-18 stsp
620 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
621 659e7fbd 2020-03-20 stsp if (!found_branch) {
622 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
623 659e7fbd 2020-03-20 stsp goto done;
624 659e7fbd 2020-03-20 stsp }
625 659e7fbd 2020-03-20 stsp
626 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
627 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
628 93658fb9 2020-03-18 stsp continue;
629 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
630 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
631 406106ee 2020-03-20 stsp sent_my_capabilites ? "" : my_capabilities);
632 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
633 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
634 9ff10419 2020-03-18 stsp goto done;
635 9ff10419 2020-03-18 stsp }
636 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
637 344e4747 2020-03-18 stsp if (err)
638 9ff10419 2020-03-18 stsp goto done;
639 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
640 7848a0e1 2020-03-19 stsp nwant++;
641 93658fb9 2020-03-18 stsp }
642 38c670f1 2020-03-18 stsp err = flushpkt(fd);
643 38c670f1 2020-03-18 stsp if (err)
644 38c670f1 2020-03-18 stsp goto done;
645 7848a0e1 2020-03-19 stsp
646 3c912d14 2020-03-19 stsp if (nwant == 0)
647 7848a0e1 2020-03-19 stsp goto done;
648 7848a0e1 2020-03-19 stsp
649 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
650 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
651 93658fb9 2020-03-18 stsp continue;
652 7848a0e1 2020-03-19 stsp got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
653 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
654 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
655 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
656 9ff10419 2020-03-18 stsp goto done;
657 9ff10419 2020-03-18 stsp }
658 c20695fb 2020-03-20 stsp err = writepkt(fd, buf, n);
659 344e4747 2020-03-18 stsp if (err)
660 9ff10419 2020-03-18 stsp goto done;
661 7848a0e1 2020-03-19 stsp nhave++;
662 93658fb9 2020-03-18 stsp }
663 7848a0e1 2020-03-19 stsp
664 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
665 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
666 7848a0e1 2020-03-19 stsp
667 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
668 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
669 38c670f1 2020-03-18 stsp if (err)
670 38c670f1 2020-03-18 stsp goto done;
671 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
672 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
673 7848a0e1 2020-03-19 stsp goto done;
674 7848a0e1 2020-03-19 stsp }
675 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
676 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
677 7848a0e1 2020-03-19 stsp continue;
678 7848a0e1 2020-03-19 stsp }
679 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
680 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
681 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
682 7848a0e1 2020-03-19 stsp "unexpected message from server");
683 7848a0e1 2020-03-19 stsp goto done;
684 7848a0e1 2020-03-19 stsp }
685 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
686 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
687 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
688 7848a0e1 2020-03-19 stsp goto done;
689 7848a0e1 2020-03-19 stsp }
690 7848a0e1 2020-03-19 stsp acked++;
691 93658fb9 2020-03-18 stsp }
692 7848a0e1 2020-03-19 stsp
693 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
694 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
695 344e4747 2020-03-18 stsp if (err)
696 9ff10419 2020-03-18 stsp goto done;
697 93658fb9 2020-03-18 stsp
698 7848a0e1 2020-03-19 stsp if (nhave == 0) {
699 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
700 7848a0e1 2020-03-19 stsp if (err)
701 7848a0e1 2020-03-19 stsp goto done;
702 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
703 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
704 7848a0e1 2020-03-19 stsp "unexpected message from server");
705 7848a0e1 2020-03-19 stsp goto done;
706 7848a0e1 2020-03-19 stsp }
707 04c53c18 2020-03-18 stsp }
708 93658fb9 2020-03-18 stsp
709 858b0dfb 2020-03-20 stsp if (chattygot)
710 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
711 858b0dfb 2020-03-20 stsp
712 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
713 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
714 531c3985 2020-03-18 stsp have_sidebands = 1;
715 531c3985 2020-03-18 stsp
716 9ff10419 2020-03-18 stsp while (1) {
717 531c3985 2020-03-18 stsp ssize_t r = 0, w;
718 531c3985 2020-03-18 stsp int datalen = -1;
719 531c3985 2020-03-18 stsp
720 531c3985 2020-03-18 stsp if (have_sidebands) {
721 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
722 531c3985 2020-03-18 stsp if (err)
723 531c3985 2020-03-18 stsp goto done;
724 531c3985 2020-03-18 stsp if (datalen <= 0)
725 531c3985 2020-03-18 stsp break;
726 531c3985 2020-03-18 stsp
727 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
728 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
729 531c3985 2020-03-18 stsp if (r == -1) {
730 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
731 531c3985 2020-03-18 stsp goto done;
732 531c3985 2020-03-18 stsp }
733 531c3985 2020-03-18 stsp if (r != 1) {
734 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
735 531c3985 2020-03-18 stsp "short packet");
736 531c3985 2020-03-18 stsp goto done;
737 531c3985 2020-03-18 stsp }
738 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
739 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
740 531c3985 2020-03-18 stsp "bad packet length");
741 531c3985 2020-03-18 stsp goto done;
742 531c3985 2020-03-18 stsp }
743 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
744 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
745 531c3985 2020-03-18 stsp /* Read packfile data. */
746 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
747 531c3985 2020-03-18 stsp if (err)
748 531c3985 2020-03-18 stsp goto done;
749 531c3985 2020-03-18 stsp if (r != datalen) {
750 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
751 531c3985 2020-03-18 stsp "packet too short");
752 531c3985 2020-03-18 stsp goto done;
753 531c3985 2020-03-18 stsp }
754 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
755 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
756 531c3985 2020-03-18 stsp if (err)
757 531c3985 2020-03-18 stsp goto done;
758 531c3985 2020-03-18 stsp if (r != datalen) {
759 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
760 531c3985 2020-03-18 stsp "packet too short");
761 531c3985 2020-03-18 stsp goto done;
762 531c3985 2020-03-18 stsp }
763 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
764 531c3985 2020-03-18 stsp if (err)
765 531c3985 2020-03-18 stsp goto done;
766 531c3985 2020-03-18 stsp continue;
767 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
768 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
769 531c3985 2020-03-18 stsp if (err)
770 531c3985 2020-03-18 stsp goto done;
771 531c3985 2020-03-18 stsp if (r != datalen) {
772 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
773 531c3985 2020-03-18 stsp "packet too short");
774 531c3985 2020-03-18 stsp goto done;
775 531c3985 2020-03-18 stsp }
776 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
777 531c3985 2020-03-18 stsp goto done;
778 531c3985 2020-03-18 stsp } else {
779 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
780 531c3985 2020-03-18 stsp "unknown side-band received from server");
781 531c3985 2020-03-18 stsp goto done;
782 531c3985 2020-03-18 stsp }
783 531c3985 2020-03-18 stsp } else {
784 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
785 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
786 531c3985 2020-03-18 stsp if (err)
787 531c3985 2020-03-18 stsp goto done;
788 531c3985 2020-03-18 stsp if (r <= 0)
789 531c3985 2020-03-18 stsp break;
790 531c3985 2020-03-18 stsp }
791 531c3985 2020-03-18 stsp
792 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
793 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
794 9ff10419 2020-03-18 stsp if (w == -1) {
795 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
796 9ff10419 2020-03-18 stsp goto done;
797 9ff10419 2020-03-18 stsp }
798 fe53745c 2020-03-18 stsp if (w != r) {
799 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
800 9ff10419 2020-03-18 stsp goto done;
801 9ff10419 2020-03-18 stsp }
802 531c3985 2020-03-18 stsp packsz += w;
803 5672d305 2020-03-18 stsp
804 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
805 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
806 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf,
807 5672d305 2020-03-18 stsp packsz);
808 5672d305 2020-03-18 stsp if (err)
809 5672d305 2020-03-18 stsp goto done;
810 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
811 5672d305 2020-03-18 stsp }
812 93658fb9 2020-03-18 stsp }
813 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf, packsz);
814 5672d305 2020-03-18 stsp if (err)
815 5672d305 2020-03-18 stsp goto done;
816 9ff10419 2020-03-18 stsp done:
817 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
818 abe0f35f 2020-03-18 stsp free((void *)pe->path);
819 abe0f35f 2020-03-18 stsp free(pe->data);
820 abe0f35f 2020-03-18 stsp }
821 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
822 9ff10419 2020-03-18 stsp free(have);
823 9ff10419 2020-03-18 stsp free(want);
824 00cd0e0a 2020-03-18 stsp free(id_str);
825 00cd0e0a 2020-03-18 stsp free(refname);
826 00cd0e0a 2020-03-18 stsp free(server_capabilities);
827 8f2d01a6 2020-03-18 stsp return err;
828 93658fb9 2020-03-18 stsp }
829 93658fb9 2020-03-18 stsp
830 93658fb9 2020-03-18 stsp
831 93658fb9 2020-03-18 stsp int
832 93658fb9 2020-03-18 stsp main(int argc, char **argv)
833 93658fb9 2020-03-18 stsp {
834 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
835 7848a0e1 2020-03-19 stsp int fetchfd, packfd = -1, i;
836 93658fb9 2020-03-18 stsp struct got_object_id packid;
837 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
838 93658fb9 2020-03-18 stsp struct imsg imsg;
839 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
840 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
841 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_request *fetch_req = NULL;
842 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
843 659e7fbd 2020-03-20 stsp size_t datalen, remain;
844 659e7fbd 2020-03-20 stsp size_t offset;
845 7848a0e1 2020-03-19 stsp #if 0
846 7848a0e1 2020-03-19 stsp static int attached;
847 7848a0e1 2020-03-19 stsp while (!attached)
848 7848a0e1 2020-03-19 stsp sleep (1);
849 7848a0e1 2020-03-19 stsp #endif
850 33501562 2020-03-18 stsp
851 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
852 93658fb9 2020-03-18 stsp
853 858b0dfb 2020-03-20 stsp if (getenv("GOT_FETCH_DEBUG") != NULL) {
854 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s being chatty!\n", getprogname());
855 858b0dfb 2020-03-20 stsp chattygot = 1;
856 858b0dfb 2020-03-20 stsp }
857 858b0dfb 2020-03-20 stsp
858 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
859 ffb5f621 2020-03-18 stsp #ifndef PROFILE
860 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
861 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
862 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
863 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
864 ffb5f621 2020-03-18 stsp return 1;
865 ffb5f621 2020-03-18 stsp }
866 ffb5f621 2020-03-18 stsp #endif
867 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
868 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
869 93658fb9 2020-03-18 stsp err = NULL;
870 93658fb9 2020-03-18 stsp goto done;
871 93658fb9 2020-03-18 stsp }
872 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
873 93658fb9 2020-03-18 stsp goto done;
874 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
875 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
876 93658fb9 2020-03-18 stsp goto done;
877 93658fb9 2020-03-18 stsp }
878 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
879 659e7fbd 2020-03-20 stsp if (datalen < sizeof(struct got_imsg_fetch_request)) {
880 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
881 93658fb9 2020-03-18 stsp goto done;
882 93658fb9 2020-03-18 stsp }
883 659e7fbd 2020-03-20 stsp fetch_req = (struct got_imsg_fetch_request *)imsg.data;
884 659e7fbd 2020-03-20 stsp if (datalen < sizeof(*fetch_req) +
885 33501562 2020-03-18 stsp sizeof(struct got_imsg_fetch_have_ref) *
886 659e7fbd 2020-03-20 stsp fetch_req->n_have_refs) {
887 33501562 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
888 33501562 2020-03-18 stsp goto done;
889 33501562 2020-03-18 stsp }
890 659e7fbd 2020-03-20 stsp offset = sizeof(*fetch_req);
891 659e7fbd 2020-03-20 stsp remain = datalen;
892 659e7fbd 2020-03-20 stsp for (i = 0; i < fetch_req->n_have_refs; i++) {
893 7848a0e1 2020-03-19 stsp struct got_object_id *id;
894 7848a0e1 2020-03-19 stsp char *refname;
895 7848a0e1 2020-03-19 stsp
896 659e7fbd 2020-03-20 stsp if (remain < sizeof(href) || offset > datalen) {
897 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
898 659e7fbd 2020-03-20 stsp goto done;
899 659e7fbd 2020-03-20 stsp }
900 659e7fbd 2020-03-20 stsp memcpy(&href, imsg.data + offset, sizeof(href));
901 659e7fbd 2020-03-20 stsp remain -= sizeof(href);
902 659e7fbd 2020-03-20 stsp if (remain < href.name_len) {
903 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
904 7848a0e1 2020-03-19 stsp goto done;
905 7848a0e1 2020-03-19 stsp }
906 659e7fbd 2020-03-20 stsp remain -= href.name_len;
907 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
908 7848a0e1 2020-03-19 stsp if (refname == NULL) {
909 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
910 7848a0e1 2020-03-19 stsp goto done;
911 7848a0e1 2020-03-19 stsp }
912 659e7fbd 2020-03-20 stsp offset += sizeof(href);
913 659e7fbd 2020-03-20 stsp memcpy(refname, imsg.data + offset, href.name_len);
914 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
915 659e7fbd 2020-03-20 stsp offset += href.name_len;
916 659e7fbd 2020-03-20 stsp
917 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
918 7848a0e1 2020-03-19 stsp if (id == NULL) {
919 7848a0e1 2020-03-19 stsp free(refname);
920 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
921 7848a0e1 2020-03-19 stsp goto done;
922 7848a0e1 2020-03-19 stsp }
923 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
924 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
925 7848a0e1 2020-03-19 stsp if (err) {
926 7848a0e1 2020-03-19 stsp free(refname);
927 7848a0e1 2020-03-19 stsp free(id);
928 7848a0e1 2020-03-19 stsp goto done;
929 7848a0e1 2020-03-19 stsp }
930 659e7fbd 2020-03-20 stsp }
931 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
932 93658fb9 2020-03-18 stsp
933 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
934 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
935 93658fb9 2020-03-18 stsp err = NULL;
936 93658fb9 2020-03-18 stsp goto done;
937 93658fb9 2020-03-18 stsp }
938 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
939 93658fb9 2020-03-18 stsp goto done;
940 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
941 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
942 93658fb9 2020-03-18 stsp goto done;
943 93658fb9 2020-03-18 stsp }
944 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
945 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
946 93658fb9 2020-03-18 stsp goto done;
947 93658fb9 2020-03-18 stsp }
948 93658fb9 2020-03-18 stsp packfd = imsg.fd;
949 93658fb9 2020-03-18 stsp
950 659e7fbd 2020-03-20 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
951 659e7fbd 2020-03-20 stsp fetch_req->fetch_all_branches, &ibuf);
952 93658fb9 2020-03-18 stsp done:
953 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
954 7848a0e1 2020-03-19 stsp free((char *)pe->path);
955 7848a0e1 2020-03-19 stsp free(pe->data);
956 7848a0e1 2020-03-19 stsp }
957 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
958 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
959 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
960 9ff10419 2020-03-18 stsp if (err != NULL)
961 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
962 93658fb9 2020-03-18 stsp else
963 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
964 cf875574 2020-03-18 stsp if (err != NULL) {
965 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
966 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
967 93658fb9 2020-03-18 stsp }
968 93658fb9 2020-03-18 stsp
969 93658fb9 2020-03-18 stsp exit(0);
970 93658fb9 2020-03-18 stsp }