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"
43 659e7fbd 2020-03-20 stsp #include "got_reference.h"
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"
52 8a29a085 2020-03-18 stsp #ifndef nitems
53 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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}};
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 2690194b 2020-03-21 stsp if (chattygot > 1)
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 2690194b 2020-03-21 stsp if (chattygot > 1)
115 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
116 531c3985 2020-03-18 stsp return NULL;
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 2690194b 2020-03-21 stsp if (chattygot > 1) {
177 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
178 858b0dfb 2020-03-20 stsp for (i = 0; i < n; i++) {
179 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
180 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
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 2690194b 2020-03-21 stsp if (chattygot > 1) {
211 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
212 858b0dfb 2020-03-20 stsp for (i = 0; i < nbuf; i++) {
213 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
214 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
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 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
245 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
248 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
249 659e7fbd 2020-03-20 stsp wanted_branch += 11;
251 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
255 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
257 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
259 0e4002ca 2020-03-21 stsp refname += 5;
262 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
263 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
265 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
267 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
270 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
271 0e4002ca 2020-03-21 stsp wanted_ref += 5;
273 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
274 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
277 0e4002ca 2020-03-21 stsp /* Allow exact match. */
278 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
281 0d0a341c 2020-03-18 stsp static const struct got_error *
282 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
284 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
286 0d0a341c 2020-03-18 stsp size_t i, n = 0;
288 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
289 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
291 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
292 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
297 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
298 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
302 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
303 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
304 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
307 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
308 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
313 0d0a341c 2020-03-18 stsp if (i <= 2)
314 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
318 0d0a341c 2020-03-18 stsp for (j = 0; j < i; j++)
319 0d0a341c 2020-03-18 stsp free(tokens[j]);
320 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
322 0d0a341c 2020-03-18 stsp return err;
325 00cd0e0a 2020-03-18 stsp static const struct got_error *
326 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
327 00cd0e0a 2020-03-18 stsp char *line, int len)
329 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
330 00cd0e0a 2020-03-18 stsp char *tokens[3];
332 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
334 00cd0e0a 2020-03-18 stsp return err;
336 00cd0e0a 2020-03-18 stsp if (tokens[0])
337 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
338 00cd0e0a 2020-03-18 stsp if (tokens[1])
339 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
340 2690194b 2020-03-21 stsp if (tokens[2]) {
342 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
343 2690194b 2020-03-21 stsp p = strrchr(*server_capabilities, '\n');
348 00cd0e0a 2020-03-18 stsp return NULL;
351 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
352 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
353 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
355 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
356 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
357 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
360 8a29a085 2020-03-18 stsp struct got_capability {
361 8a29a085 2020-03-18 stsp const char *key;
362 8a29a085 2020-03-18 stsp const char *value;
364 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
365 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
366 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
367 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
370 8a29a085 2020-03-18 stsp static const struct got_error *
371 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
372 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
374 8a29a085 2020-03-18 stsp char *equalsign;
377 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
378 8a29a085 2020-03-18 stsp if (equalsign) {
379 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
380 8a29a085 2020-03-18 stsp return NULL;
382 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
383 8a29a085 2020-03-18 stsp return NULL;
386 406106ee 2020-03-20 stsp if (asprintf(&s, "%s %s%s%s",
387 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
388 8a29a085 2020-03-18 stsp mycapa->key,
389 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
390 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
391 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
393 8a29a085 2020-03-18 stsp free(*my_capabilities);
394 8a29a085 2020-03-18 stsp *my_capabilities = s;
395 8a29a085 2020-03-18 stsp return NULL;
398 9ff10419 2020-03-18 stsp static const struct got_error *
399 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
401 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
402 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
404 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
405 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
406 abe0f35f 2020-03-18 stsp return NULL;
408 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
409 abe0f35f 2020-03-18 stsp if (colon == NULL)
410 abe0f35f 2020-03-18 stsp return NULL;
412 abe0f35f 2020-03-18 stsp *colon = '\0';
413 abe0f35f 2020-03-18 stsp name = strdup(capa);
414 abe0f35f 2020-03-18 stsp if (name == NULL)
415 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
417 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
418 abe0f35f 2020-03-18 stsp if (target == NULL) {
419 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
423 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
424 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
427 abe0f35f 2020-03-18 stsp free(name);
428 abe0f35f 2020-03-18 stsp free(target);
430 abe0f35f 2020-03-18 stsp return err;
433 abe0f35f 2020-03-18 stsp static const struct got_error *
434 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
435 abe0f35f 2020-03-18 stsp char *server_capabilities)
437 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
438 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
441 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
443 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
444 8a29a085 2020-03-18 stsp if (capa == NULL)
445 8a29a085 2020-03-18 stsp return NULL;
447 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
448 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
449 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
450 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
456 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
457 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
458 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
462 8a29a085 2020-03-18 stsp } while (capa);
464 406106ee 2020-03-20 stsp if (*my_capabilities == NULL) {
465 406106ee 2020-03-20 stsp *my_capabilities = strdup("");
466 406106ee 2020-03-20 stsp if (*my_capabilities == NULL)
467 406106ee 2020-03-20 stsp err = got_error_from_errno("strdup");
469 8a29a085 2020-03-18 stsp return err;
472 531c3985 2020-03-18 stsp static const struct got_error *
473 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
477 531c3985 2020-03-18 stsp if (len == 0)
478 531c3985 2020-03-18 stsp return NULL;
481 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
482 531c3985 2020-03-18 stsp * Server may send up to 64k.
484 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
485 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
487 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
488 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
489 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
490 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
492 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
493 531c3985 2020-03-18 stsp "non-printable progress message received from server");
496 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
499 8a29a085 2020-03-18 stsp static const struct got_error *
500 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
502 531c3985 2020-03-18 stsp static char msg[1024];
505 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
506 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
507 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
508 531c3985 2020-03-18 stsp "non-printable error message received from server");
509 531c3985 2020-03-18 stsp msg[i] = buf[i];
511 531c3985 2020-03-18 stsp msg[i] = '\0';
512 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
515 531c3985 2020-03-18 stsp static const struct got_error *
516 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
517 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
518 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
519 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
520 41b0de12 2020-03-21 stsp struct imsgbuf *ibuf)
522 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
523 f1c6967f 2020-03-19 stsp char buf[GOT_FETCH_PKTMAX];
524 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
525 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
526 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
527 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
528 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
529 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
530 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
531 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
532 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
533 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
534 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
535 659e7fbd 2020-03-20 stsp int found_branch = 0;
537 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
539 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
540 9ff10419 2020-03-18 stsp if (have == NULL)
541 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
542 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
543 9ff10419 2020-03-18 stsp if (want == NULL) {
544 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
547 9ff10419 2020-03-18 stsp while (1) {
548 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
551 9ff10419 2020-03-18 stsp if (n == 0)
553 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
554 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
557 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
561 8a29a085 2020-03-18 stsp if (is_firstpkt) {
562 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
563 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
564 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
565 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
566 00cd0e0a 2020-03-18 stsp server_capabilities);
569 858b0dfb 2020-03-20 stsp if (chattygot)
570 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
571 858b0dfb 2020-03-20 stsp getprogname(), my_capabilities);
572 abe0f35f 2020-03-18 stsp err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
575 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
576 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
577 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
578 659e7fbd 2020-03-20 stsp const char *name = pe->path;
579 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
580 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
582 659e7fbd 2020-03-20 stsp default_branch = symref_target;
588 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
589 2690194b 2020-03-21 stsp if (chattygot) {
590 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
591 2690194b 2020-03-21 stsp getprogname(), refname);
596 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
597 41b0de12 2020-03-21 stsp if (fetch_all_branches || list_refs_only) {
598 4ba14133 2020-03-20 stsp found_branch = 1;
599 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
600 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
601 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
604 2690194b 2020-03-21 stsp if (pe == NULL) {
605 2690194b 2020-03-21 stsp if (chattygot) {
606 2690194b 2020-03-21 stsp fprintf(stderr,
607 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
608 2690194b 2020-03-21 stsp getprogname(), refname);
612 4ba14133 2020-03-20 stsp found_branch = 1;
613 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
614 2690194b 2020-03-21 stsp if (!match_branch(refname, default_branch)) {
615 2690194b 2020-03-21 stsp if (chattygot) {
616 2690194b 2020-03-21 stsp fprintf(stderr,
617 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
618 2690194b 2020-03-21 stsp getprogname(), refname);
622 4ba14133 2020-03-20 stsp found_branch = 1;
624 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
625 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(wanted_refs)) {
626 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
627 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
630 0e4002ca 2020-03-21 stsp if (pe == NULL) {
631 0e4002ca 2020-03-21 stsp if (chattygot) {
632 0e4002ca 2020-03-21 stsp fprintf(stderr,
633 0e4002ca 2020-03-21 stsp "%s: ignoring %s\n",
634 0e4002ca 2020-03-21 stsp getprogname(), refname);
638 0e4002ca 2020-03-21 stsp found_branch = 1;
639 0e4002ca 2020-03-21 stsp } else if (!list_refs_only) {
640 2690194b 2020-03-21 stsp if (chattygot) {
641 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
642 2690194b 2020-03-21 stsp getprogname(), refname);
648 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
649 93658fb9 2020-03-18 stsp refsz *= 2;
650 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
651 9ff10419 2020-03-18 stsp if (have == NULL) {
652 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
655 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
656 9ff10419 2020-03-18 stsp if (want == NULL) {
657 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
661 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
662 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
665 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
666 ea7396b9 2020-03-18 stsp err = got_privsep_send_fetch_ref(ibuf, &want[nref],
671 2690194b 2020-03-21 stsp if (chattygot)
672 2690194b 2020-03-21 stsp fprintf(stderr, "%s: %s will be fetched\n",
673 2690194b 2020-03-21 stsp getprogname(), refname);
674 2690194b 2020-03-21 stsp if (chattygot > 1) {
675 858b0dfb 2020-03-20 stsp char *theirs, *mine;
676 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
679 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
681 858b0dfb 2020-03-20 stsp free(theirs);
684 2690194b 2020-03-21 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
685 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
686 858b0dfb 2020-03-20 stsp free(theirs);
687 858b0dfb 2020-03-20 stsp free(mine);
692 41b0de12 2020-03-21 stsp if (list_refs_only)
695 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
696 659e7fbd 2020-03-20 stsp if (!found_branch) {
697 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
701 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
702 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
704 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
705 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
706 406106ee 2020-03-20 stsp sent_my_capabilites ? "" : my_capabilities);
707 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
708 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
711 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
714 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
717 38c670f1 2020-03-18 stsp err = flushpkt(fd);
721 3c912d14 2020-03-19 stsp if (nwant == 0)
724 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
725 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
727 7848a0e1 2020-03-19 stsp got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
728 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
729 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
730 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
733 c20695fb 2020-03-20 stsp err = writepkt(fd, buf, n);
739 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
740 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
742 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
743 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
746 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
747 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
750 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
751 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
754 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
755 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
756 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
757 7848a0e1 2020-03-19 stsp "unexpected message from server");
760 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
761 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
762 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
768 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
769 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
773 7848a0e1 2020-03-19 stsp if (nhave == 0) {
774 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
777 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
778 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
779 7848a0e1 2020-03-19 stsp "unexpected message from server");
784 858b0dfb 2020-03-20 stsp if (chattygot)
785 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
787 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
788 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
789 531c3985 2020-03-18 stsp have_sidebands = 1;
791 9ff10419 2020-03-18 stsp while (1) {
792 531c3985 2020-03-18 stsp ssize_t r = 0, w;
793 531c3985 2020-03-18 stsp int datalen = -1;
795 531c3985 2020-03-18 stsp if (have_sidebands) {
796 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
799 531c3985 2020-03-18 stsp if (datalen <= 0)
802 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
803 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
804 531c3985 2020-03-18 stsp if (r == -1) {
805 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
808 531c3985 2020-03-18 stsp if (r != 1) {
809 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
810 531c3985 2020-03-18 stsp "short packet");
813 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
814 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
815 531c3985 2020-03-18 stsp "bad packet length");
818 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
819 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
820 531c3985 2020-03-18 stsp /* Read packfile data. */
821 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
824 531c3985 2020-03-18 stsp if (r != datalen) {
825 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
826 531c3985 2020-03-18 stsp "packet too short");
829 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
830 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
833 531c3985 2020-03-18 stsp if (r != datalen) {
834 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
835 531c3985 2020-03-18 stsp "packet too short");
838 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
842 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
843 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
846 531c3985 2020-03-18 stsp if (r != datalen) {
847 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
848 531c3985 2020-03-18 stsp "packet too short");
851 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
854 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
855 531c3985 2020-03-18 stsp "unknown side-band received from server");
859 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
860 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
863 531c3985 2020-03-18 stsp if (r <= 0)
867 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
868 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
869 9ff10419 2020-03-18 stsp if (w == -1) {
870 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
873 fe53745c 2020-03-18 stsp if (w != r) {
874 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
877 531c3985 2020-03-18 stsp packsz += w;
879 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
880 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
881 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf,
885 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
888 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf, packsz);
892 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
893 abe0f35f 2020-03-18 stsp free((void *)pe->path);
894 abe0f35f 2020-03-18 stsp free(pe->data);
896 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
897 9ff10419 2020-03-18 stsp free(have);
898 9ff10419 2020-03-18 stsp free(want);
899 00cd0e0a 2020-03-18 stsp free(id_str);
900 00cd0e0a 2020-03-18 stsp free(refname);
901 00cd0e0a 2020-03-18 stsp free(server_capabilities);
902 8f2d01a6 2020-03-18 stsp return err;
907 93658fb9 2020-03-18 stsp main(int argc, char **argv)
909 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
910 7848a0e1 2020-03-19 stsp int fetchfd, packfd = -1, i;
911 93658fb9 2020-03-18 stsp struct got_object_id packid;
912 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
913 93658fb9 2020-03-18 stsp struct imsg imsg;
914 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
915 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
916 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
917 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
918 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
919 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
920 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
921 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
922 4ba14133 2020-03-20 stsp size_t datalen;
924 7848a0e1 2020-03-19 stsp static int attached;
925 7848a0e1 2020-03-19 stsp while (!attached)
929 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
930 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
931 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
933 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
934 ffb5f621 2020-03-18 stsp #ifndef PROFILE
935 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
936 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
937 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
938 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
942 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
943 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
944 93658fb9 2020-03-18 stsp err = NULL;
947 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
949 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
950 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
953 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
954 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
955 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
958 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
959 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
960 4ba14133 2020-03-20 stsp imsg_free(&imsg);
962 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
963 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
965 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
966 7848a0e1 2020-03-19 stsp struct got_object_id *id;
967 7848a0e1 2020-03-19 stsp char *refname;
969 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
970 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
971 4ba14133 2020-03-20 stsp err = NULL;
974 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
976 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
977 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
980 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
981 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
982 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
985 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
986 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
987 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
990 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
991 7848a0e1 2020-03-19 stsp if (refname == NULL) {
992 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
995 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
996 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
998 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
999 7848a0e1 2020-03-19 stsp if (id == NULL) {
1000 7848a0e1 2020-03-19 stsp free(refname);
1001 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
1002 7848a0e1 2020-03-19 stsp goto done;
1004 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1005 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
1006 7848a0e1 2020-03-19 stsp if (err) {
1007 7848a0e1 2020-03-19 stsp free(refname);
1009 7848a0e1 2020-03-19 stsp goto done;
1012 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1015 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1016 4ba14133 2020-03-20 stsp char *refname;
1018 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1019 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1020 4ba14133 2020-03-20 stsp err = NULL;
1021 4ba14133 2020-03-20 stsp goto done;
1023 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1024 4ba14133 2020-03-20 stsp goto done;
1025 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1026 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1027 4ba14133 2020-03-20 stsp goto done;
1029 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1030 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
1031 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1032 4ba14133 2020-03-20 stsp goto done;
1034 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
1035 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
1036 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1037 4ba14133 2020-03-20 stsp goto done;
1039 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
1040 4ba14133 2020-03-20 stsp if (refname == NULL) {
1041 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
1042 4ba14133 2020-03-20 stsp goto done;
1044 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1045 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
1047 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
1048 4ba14133 2020-03-20 stsp if (err) {
1049 4ba14133 2020-03-20 stsp free(refname);
1050 4ba14133 2020-03-20 stsp goto done;
1053 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1056 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1057 0e4002ca 2020-03-21 stsp char *refname;
1059 0e4002ca 2020-03-21 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1060 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1061 0e4002ca 2020-03-21 stsp err = NULL;
1062 0e4002ca 2020-03-21 stsp goto done;
1064 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1065 0e4002ca 2020-03-21 stsp goto done;
1066 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1067 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1068 0e4002ca 2020-03-21 stsp goto done;
1070 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1071 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
1072 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1073 0e4002ca 2020-03-21 stsp goto done;
1075 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
1076 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
1077 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1078 0e4002ca 2020-03-21 stsp goto done;
1080 0e4002ca 2020-03-21 stsp refname = malloc(wref.name_len + 1);
1081 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1082 0e4002ca 2020-03-21 stsp err = got_error_from_errno("malloc");
1083 0e4002ca 2020-03-21 stsp goto done;
1085 0e4002ca 2020-03-21 stsp memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1086 0e4002ca 2020-03-21 stsp refname[wref.name_len] = '\0';
1088 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1089 0e4002ca 2020-03-21 stsp if (err) {
1090 0e4002ca 2020-03-21 stsp free(refname);
1091 0e4002ca 2020-03-21 stsp goto done;
1094 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1097 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1098 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1099 93658fb9 2020-03-18 stsp err = NULL;
1100 93658fb9 2020-03-18 stsp goto done;
1102 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1103 93658fb9 2020-03-18 stsp goto done;
1104 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1105 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1106 93658fb9 2020-03-18 stsp goto done;
1108 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1109 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1110 93658fb9 2020-03-18 stsp goto done;
1112 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1114 659e7fbd 2020-03-20 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
1115 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1116 0e4002ca 2020-03-21 stsp &wanted_refs, fetch_req.list_refs_only, &ibuf);
1118 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
1119 7848a0e1 2020-03-19 stsp free((char *)pe->path);
1120 7848a0e1 2020-03-19 stsp free(pe->data);
1122 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1123 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1124 4ba14133 2020-03-20 stsp free((char *)pe->path);
1125 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1126 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1127 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1128 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1129 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1130 9ff10419 2020-03-18 stsp if (err != NULL)
1131 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1133 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
1134 cf875574 2020-03-18 stsp if (err != NULL) {
1135 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1136 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);