2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
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>
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>
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"
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"
51 8a29a085 2020-03-18 stsp #ifndef nitems
52 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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}};
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)
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");
71 fe53745c 2020-03-18 stsp return NULL;
74 fe53745c 2020-03-18 stsp return NULL;
77 38c670f1 2020-03-18 stsp static const struct got_error *
78 93658fb9 2020-03-18 stsp flushpkt(int fd)
82 858b0dfb 2020-03-20 stsp if (chattygot)
83 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
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");
89 38c670f1 2020-03-18 stsp return got_error(GOT_ERR_IO);
90 38c670f1 2020-03-18 stsp return NULL;
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.
97 fe53745c 2020-03-18 stsp static const struct got_error *
98 531c3985 2020-03-18 stsp read_pkthdr(int *datalen, int fd)
100 531c3985 2020-03-18 stsp static const struct got_error *err = NULL;
101 4dc8ee09 2020-03-18 stsp char lenstr[5];
107 531c3985 2020-03-18 stsp *datalen = 0;
109 4dc8ee09 2020-03-18 stsp err = readn(&r, fd, lenstr, 4);
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;
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");
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");
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");
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");
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");
152 531c3985 2020-03-18 stsp *datalen = n;
153 531c3985 2020-03-18 stsp return NULL;
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)
159 531c3985 2020-03-18 stsp const struct got_error *err = NULL;
160 858b0dfb 2020-03-20 stsp int datalen, i;
163 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
165 531c3985 2020-03-18 stsp return err;
167 531c3985 2020-03-18 stsp if (datalen > buflen)
168 fe53745c 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
170 531c3985 2020-03-18 stsp err = readn(&n, fd, buf, datalen);
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");
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);
182 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
184 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
187 fe53745c 2020-03-18 stsp *outlen = n;
188 fe53745c 2020-03-18 stsp return NULL;
191 344e4747 2020-03-18 stsp static const struct got_error *
192 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
194 93658fb9 2020-03-18 stsp char len[5];
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);
216 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
218 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
220 344e4747 2020-03-18 stsp return NULL;
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)
227 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
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));
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));
243 1ff21071 2020-03-18 stsp match_branch(char *br, char *pat)
245 93658fb9 2020-03-18 stsp char name[128];
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))
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))
255 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
256 cf875574 2020-03-18 stsp >= sizeof(name))
259 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
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)
265 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
267 0d0a341c 2020-03-18 stsp size_t i, n = 0;
269 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
270 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
272 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
273 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
278 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
279 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
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");
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) {
294 0d0a341c 2020-03-18 stsp if (i <= 2)
295 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
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;
303 0d0a341c 2020-03-18 stsp return err;
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)
310 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
311 00cd0e0a 2020-03-18 stsp char *tokens[3];
313 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
315 00cd0e0a 2020-03-18 stsp return err;
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];
324 00cd0e0a 2020-03-18 stsp return NULL;
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"
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
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;
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 },
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)
350 8a29a085 2020-03-18 stsp char *equalsign;
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;
358 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
359 8a29a085 2020-03-18 stsp return NULL;
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");
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;
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)
377 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
378 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
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;
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;
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");
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");
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);
403 abe0f35f 2020-03-18 stsp free(name);
404 abe0f35f 2020-03-18 stsp free(target);
406 abe0f35f 2020-03-18 stsp return err;
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)
413 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
414 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
417 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
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;
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);
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]);
438 8a29a085 2020-03-18 stsp } while (capa);
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");
445 8a29a085 2020-03-18 stsp return err;
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)
453 531c3985 2020-03-18 stsp if (len == 0)
454 531c3985 2020-03-18 stsp return NULL;
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.
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;
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]))
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");
472 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
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)
478 531c3985 2020-03-18 stsp static char msg[1024];
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];
487 531c3985 2020-03-18 stsp msg[i] = '\0';
488 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
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)
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;
508 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
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");
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));
524 9ff10419 2020-03-18 stsp if (n == 0)
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);
530 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
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);
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);
548 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
551 00cd0e0a 2020-03-18 stsp if (strstr(refname, "^{}"))
553 00cd0e0a 2020-03-18 stsp if (fetchbranch && !match_branch(refname, fetchbranch))
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");
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");
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);
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],
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]);
583 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
585 858b0dfb 2020-03-20 stsp free(theirs);
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);
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)
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);
608 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
611 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
614 38c670f1 2020-03-18 stsp err = flushpkt(fd);
618 3c912d14 2020-03-19 stsp if (nwant == 0)
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)
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);
630 c20695fb 2020-03-20 stsp err = writepkt(fd, buf, n);
636 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
637 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
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));
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);
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. */
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");
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");
665 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
666 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
670 7848a0e1 2020-03-19 stsp if (nhave == 0) {
671 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
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");
681 858b0dfb 2020-03-20 stsp if (chattygot)
682 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
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;
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;
692 531c3985 2020-03-18 stsp if (have_sidebands) {
693 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
696 531c3985 2020-03-18 stsp if (datalen <= 0)
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");
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");
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");
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);
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");
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);
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");
735 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
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);
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");
748 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
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");
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);
760 531c3985 2020-03-18 stsp if (r <= 0)
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");
770 fe53745c 2020-03-18 stsp if (w != r) {
771 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
774 531c3985 2020-03-18 stsp packsz += w;
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,
782 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
785 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf, packsz);
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);
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;
804 93658fb9 2020-03-18 stsp main(int argc, char **argv)
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;
817 7848a0e1 2020-03-19 stsp static int attached;
818 7848a0e1 2020-03-19 stsp while (!attached)
822 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
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;
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);
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;
843 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
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);
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);
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);
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;
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);
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");
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");
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);
887 7848a0e1 2020-03-19 stsp free(refname);
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);
894 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
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;
901 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
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);
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);
911 93658fb9 2020-03-18 stsp packfd = imsg.fd;
913 33501562 2020-03-18 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
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);
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);
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);