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