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"
43 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
44 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
45 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
48 0872c0b0 2020-03-18 stsp #include "got_lib_pack.h"
50 8a29a085 2020-03-18 stsp #ifndef nitems
51 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 93658fb9 2020-03-18 stsp #define GOT_PKTMAX 65536
56 93658fb9 2020-03-18 stsp struct got_object *indexed;
57 93658fb9 2020-03-18 stsp static int chattygit;
58 93658fb9 2020-03-18 stsp static char *fetchbranch;
59 93658fb9 2020-03-18 stsp static struct got_object_id zhash = {.sha1={0}};
61 fe53745c 2020-03-18 stsp static const struct got_error *
62 fe53745c 2020-03-18 stsp readn(ssize_t *off, int fd, void *buf, size_t n)
67 fe53745c 2020-03-18 stsp while (*off != n) {
68 fe53745c 2020-03-18 stsp r = read(fd, buf + *off, n - *off);
69 fe53745c 2020-03-18 stsp if (r == -1)
70 fe53745c 2020-03-18 stsp return got_error_from_errno("read");
72 fe53745c 2020-03-18 stsp return NULL;
75 fe53745c 2020-03-18 stsp return NULL;
78 38c670f1 2020-03-18 stsp static const struct got_error *
79 93658fb9 2020-03-18 stsp flushpkt(int fd)
83 cf875574 2020-03-18 stsp if (chattygit)
84 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: 0000\n");
86 38c670f1 2020-03-18 stsp w = write(fd, "0000", 4);
87 38c670f1 2020-03-18 stsp if (w == -1)
88 38c670f1 2020-03-18 stsp return got_error_from_errno("write");
90 38c670f1 2020-03-18 stsp return got_error(GOT_ERR_IO);
91 38c670f1 2020-03-18 stsp return NULL;
95 531c3985 2020-03-18 stsp * Packet header contains a 4-byte hexstring which specifies the length
96 531c3985 2020-03-18 stsp * of data which follows.
98 fe53745c 2020-03-18 stsp static const struct got_error *
99 531c3985 2020-03-18 stsp read_pkthdr(int *datalen, int fd)
101 531c3985 2020-03-18 stsp static const struct got_error *err = NULL;
102 4dc8ee09 2020-03-18 stsp char lenstr[5];
108 531c3985 2020-03-18 stsp *datalen = 0;
110 4dc8ee09 2020-03-18 stsp err = readn(&r, fd, lenstr, 4);
112 fe53745c 2020-03-18 stsp return err;
113 531c3985 2020-03-18 stsp if (r == 0) /* implicit "0000" */
114 531c3985 2020-03-18 stsp return NULL;
115 4dc8ee09 2020-03-18 stsp if (r != 4)
116 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
117 531c3985 2020-03-18 stsp "wrong packet header length");
119 4dc8ee09 2020-03-18 stsp lenstr[4] = '\0';
120 54d1a70f 2020-03-18 stsp for (i = 0; i < 4; i++) {
121 54d1a70f 2020-03-18 stsp if (!isxdigit(lenstr[i]))
122 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
123 531c3985 2020-03-18 stsp "packet length not specified in hex");
126 4dc8ee09 2020-03-18 stsp len = strtol(lenstr, &e, 16);
127 4dc8ee09 2020-03-18 stsp if (lenstr[0] == '\0' || *e != '\0')
128 4dc8ee09 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKET);
129 4dc8ee09 2020-03-18 stsp if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
130 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
131 4dc8ee09 2020-03-18 stsp if (len > INT_MAX || len < INT_MIN)
132 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
134 531c3985 2020-03-18 stsp if (n == 0)
135 fe53745c 2020-03-18 stsp return NULL;
136 4dc8ee09 2020-03-18 stsp if (n <= 4)
137 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
140 531c3985 2020-03-18 stsp *datalen = n;
141 531c3985 2020-03-18 stsp return NULL;
144 531c3985 2020-03-18 stsp static const struct got_error *
145 531c3985 2020-03-18 stsp readpkt(int *outlen, int fd, char *buf, int buflen)
147 531c3985 2020-03-18 stsp const struct got_error *err = NULL;
148 531c3985 2020-03-18 stsp int datalen;
151 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
153 531c3985 2020-03-18 stsp return err;
155 531c3985 2020-03-18 stsp if (datalen > buflen)
156 fe53745c 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
158 531c3985 2020-03-18 stsp err = readn(&n, fd, buf, datalen);
160 fe53745c 2020-03-18 stsp return err;
161 531c3985 2020-03-18 stsp if (n != datalen)
162 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
164 fe53745c 2020-03-18 stsp *outlen = n;
165 fe53745c 2020-03-18 stsp return NULL;
168 344e4747 2020-03-18 stsp static const struct got_error *
169 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
171 93658fb9 2020-03-18 stsp char len[5];
175 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
176 344e4747 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
177 344e4747 2020-03-18 stsp w = write(fd, len, 4);
178 344e4747 2020-03-18 stsp if (w == -1)
179 344e4747 2020-03-18 stsp return got_error_from_errno("write");
180 344e4747 2020-03-18 stsp if (w != 4)
181 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
182 344e4747 2020-03-18 stsp w = write(fd, buf, nbuf);
183 344e4747 2020-03-18 stsp if (w == -1)
184 344e4747 2020-03-18 stsp return got_error_from_errno("write");
185 344e4747 2020-03-18 stsp if (w != nbuf)
186 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
187 cf875574 2020-03-18 stsp if (chattygit) {
188 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: %s:\t", len);
189 93658fb9 2020-03-18 stsp fwrite(buf, 1, nbuf, stderr);
190 cf875574 2020-03-18 stsp for (i = 0; i < nbuf; i++) {
191 cf875574 2020-03-18 stsp if (isprint(buf[i]))
192 93658fb9 2020-03-18 stsp fputc(buf[i], stderr);
194 f78e0473 2020-03-18 stsp fputc('\n', stderr);
196 344e4747 2020-03-18 stsp return NULL;
199 33501562 2020-03-18 stsp static const struct got_error *
200 33501562 2020-03-18 stsp match_remote_ref(struct got_pathlist_head *have_refs, struct got_object_id *id,
201 33501562 2020-03-18 stsp char *refname, char *id_str)
203 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
205 33501562 2020-03-18 stsp memset(id, 0, sizeof(*id));
207 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
208 33501562 2020-03-18 stsp if (strcmp(pe->path, refname) == 0) {
209 33501562 2020-03-18 stsp if (!got_parse_sha1_digest(id->sha1, id_str))
210 33501562 2020-03-18 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
214 33501562 2020-03-18 stsp return NULL;
218 1ff21071 2020-03-18 stsp match_branch(char *br, char *pat)
220 93658fb9 2020-03-18 stsp char name[128];
222 cf875574 2020-03-18 stsp if (strstr(pat, "refs/heads") == pat) {
223 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
225 cf875574 2020-03-18 stsp } else if (strstr(pat, "heads")) {
226 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/%s", pat)
227 cf875574 2020-03-18 stsp >= sizeof(name))
230 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
231 cf875574 2020-03-18 stsp >= sizeof(name))
234 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
237 0d0a341c 2020-03-18 stsp static const struct got_error *
238 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
240 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
242 0d0a341c 2020-03-18 stsp size_t i, n = 0;
244 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
245 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
247 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
248 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
253 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
254 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
258 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
259 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
260 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
263 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
264 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
269 0d0a341c 2020-03-18 stsp if (i <= 2)
270 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
274 0d0a341c 2020-03-18 stsp for (j = 0; j < i; j++)
275 0d0a341c 2020-03-18 stsp free(tokens[j]);
276 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
278 0d0a341c 2020-03-18 stsp return err;
281 00cd0e0a 2020-03-18 stsp static const struct got_error *
282 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
283 00cd0e0a 2020-03-18 stsp char *line, int len)
285 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
286 00cd0e0a 2020-03-18 stsp char *tokens[3];
288 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
290 00cd0e0a 2020-03-18 stsp return err;
292 00cd0e0a 2020-03-18 stsp if (tokens[0])
293 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
294 00cd0e0a 2020-03-18 stsp if (tokens[1])
295 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
296 00cd0e0a 2020-03-18 stsp if (tokens[2])
297 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
299 00cd0e0a 2020-03-18 stsp return NULL;
302 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
303 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
304 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
306 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
307 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
308 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
311 8a29a085 2020-03-18 stsp struct got_capability {
312 8a29a085 2020-03-18 stsp const char *key;
313 8a29a085 2020-03-18 stsp const char *value;
315 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
316 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
317 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
318 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
321 8a29a085 2020-03-18 stsp static const struct got_error *
322 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
323 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
325 8a29a085 2020-03-18 stsp char *equalsign;
328 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
329 8a29a085 2020-03-18 stsp if (equalsign) {
330 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
331 8a29a085 2020-03-18 stsp return NULL;
333 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
334 8a29a085 2020-03-18 stsp return NULL;
337 8a29a085 2020-03-18 stsp if (asprintf(&s, "%s%s%s%s%s",
338 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
339 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? " " : "",
340 8a29a085 2020-03-18 stsp mycapa->key,
341 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
342 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
343 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
345 8a29a085 2020-03-18 stsp free(*my_capabilities);
346 8a29a085 2020-03-18 stsp *my_capabilities = s;
347 8a29a085 2020-03-18 stsp return NULL;
350 9ff10419 2020-03-18 stsp static const struct got_error *
351 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
353 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
354 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
356 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
357 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
358 abe0f35f 2020-03-18 stsp return NULL;
360 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
361 abe0f35f 2020-03-18 stsp if (colon == NULL)
362 abe0f35f 2020-03-18 stsp return NULL;
364 abe0f35f 2020-03-18 stsp *colon = '\0';
365 abe0f35f 2020-03-18 stsp name = strdup(capa);
366 abe0f35f 2020-03-18 stsp if (name == NULL)
367 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
369 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
370 abe0f35f 2020-03-18 stsp if (target == NULL) {
371 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
375 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
376 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
379 abe0f35f 2020-03-18 stsp free(name);
380 abe0f35f 2020-03-18 stsp free(target);
382 abe0f35f 2020-03-18 stsp return err;
385 abe0f35f 2020-03-18 stsp static const struct got_error *
386 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
387 abe0f35f 2020-03-18 stsp char *server_capabilities)
389 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
390 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
393 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
395 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
396 8a29a085 2020-03-18 stsp if (capa == NULL)
397 8a29a085 2020-03-18 stsp return NULL;
399 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
400 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
401 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
402 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
408 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
409 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
410 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
414 8a29a085 2020-03-18 stsp } while (capa);
416 8a29a085 2020-03-18 stsp return err;
419 531c3985 2020-03-18 stsp static const struct got_error *
420 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
424 531c3985 2020-03-18 stsp if (len == 0)
425 531c3985 2020-03-18 stsp return NULL;
428 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
429 531c3985 2020-03-18 stsp * Server may send up to 64k.
431 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
432 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
434 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
435 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
436 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
437 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
439 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
440 531c3985 2020-03-18 stsp "non-printable progress message received from server");
443 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
446 8a29a085 2020-03-18 stsp static const struct got_error *
447 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
449 531c3985 2020-03-18 stsp static char msg[1024];
452 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
453 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
454 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
455 531c3985 2020-03-18 stsp "non-printable error message received from server");
456 531c3985 2020-03-18 stsp msg[i] = buf[i];
458 531c3985 2020-03-18 stsp msg[i] = '\0';
459 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
462 531c3985 2020-03-18 stsp static const struct got_error *
463 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
464 33501562 2020-03-18 stsp struct got_pathlist_head *have_refs, struct imsgbuf *ibuf)
466 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
467 00cd0e0a 2020-03-18 stsp char buf[GOT_PKTMAX];
468 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
469 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
470 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
471 93658fb9 2020-03-18 stsp int i, n, req;
472 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
473 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
474 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
475 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
476 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
477 531c3985 2020-03-18 stsp int have_sidebands = 0;
479 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
481 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
482 9ff10419 2020-03-18 stsp if (have == NULL)
483 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
484 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
485 9ff10419 2020-03-18 stsp if (want == NULL) {
486 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
489 9ff10419 2020-03-18 stsp if (chattygit)
490 9ff10419 2020-03-18 stsp fprintf(stderr, "starting fetch\n");
491 9ff10419 2020-03-18 stsp while (1) {
492 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
495 9ff10419 2020-03-18 stsp if (n == 0)
497 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
498 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
501 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
505 00cd0e0a 2020-03-18 stsp if (chattygit && server_capabilities[0] != '\0')
506 00cd0e0a 2020-03-18 stsp fprintf(stderr, "server capabilities: %s\n",
507 00cd0e0a 2020-03-18 stsp server_capabilities);
508 8a29a085 2020-03-18 stsp if (is_firstpkt) {
509 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
510 00cd0e0a 2020-03-18 stsp server_capabilities);
513 8a29a085 2020-03-18 stsp if (chattygit && my_capabilities)
514 8a29a085 2020-03-18 stsp fprintf(stderr, "my matched capabilities: %s\n",
515 8a29a085 2020-03-18 stsp my_capabilities);
516 abe0f35f 2020-03-18 stsp err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
520 8a29a085 2020-03-18 stsp is_firstpkt = 0;
521 00cd0e0a 2020-03-18 stsp if (strstr(refname, "^{}"))
523 00cd0e0a 2020-03-18 stsp if (fetchbranch && !match_branch(refname, fetchbranch))
525 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
526 93658fb9 2020-03-18 stsp refsz *= 2;
527 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
528 9ff10419 2020-03-18 stsp if (have == NULL) {
529 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
532 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
533 9ff10419 2020-03-18 stsp if (want == NULL) {
534 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
538 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
539 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
543 00cd0e0a 2020-03-18 stsp err = match_remote_ref(have_refs, &have[nref], id_str, refname);
547 ea7396b9 2020-03-18 stsp err = got_privsep_send_fetch_ref(ibuf, &want[nref],
551 93658fb9 2020-03-18 stsp if (chattygit)
552 00cd0e0a 2020-03-18 stsp fprintf(stderr, "remote %s\n", refname);
557 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
558 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
560 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
561 13ce8c93 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "want %s%s%s\n", hashstr,
562 13ce8c93 2020-03-18 stsp i == 0 && my_capabilities ? " " : "",
563 8a29a085 2020-03-18 stsp i == 0 && my_capabilities ? my_capabilities : "");
564 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
565 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
568 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
573 38c670f1 2020-03-18 stsp err = flushpkt(fd);
576 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
577 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
579 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
580 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
581 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
582 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
585 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n + 1);
589 cf875574 2020-03-18 stsp if (!req) {
590 3b9fb585 2020-03-18 stsp if (chattygit)
591 3b9fb585 2020-03-18 stsp fprintf(stderr, "up to date\n");
592 38c670f1 2020-03-18 stsp err = flushpkt(fd);
596 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
597 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
603 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
607 04c53c18 2020-03-18 stsp * For now, we only support a full clone, in which case the server
608 04c53c18 2020-03-18 stsp * will now send a "NAK" (meaning no common objects were found).
610 04c53c18 2020-03-18 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
611 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
612 531c3985 2020-03-18 stsp "unexpected message from server");
616 8f2d01a6 2020-03-18 stsp if (chattygit)
617 8f2d01a6 2020-03-18 stsp fprintf(stderr, "fetching...\n");
619 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
620 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
621 531c3985 2020-03-18 stsp have_sidebands = 1;
623 9ff10419 2020-03-18 stsp while (1) {
624 531c3985 2020-03-18 stsp ssize_t r = 0, w;
625 531c3985 2020-03-18 stsp int datalen = -1;
627 531c3985 2020-03-18 stsp if (have_sidebands) {
628 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
631 531c3985 2020-03-18 stsp if (datalen <= 0)
634 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
635 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
636 531c3985 2020-03-18 stsp if (r == -1) {
637 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
640 531c3985 2020-03-18 stsp if (r != 1) {
641 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
642 531c3985 2020-03-18 stsp "short packet");
645 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
646 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
647 531c3985 2020-03-18 stsp "bad packet length");
650 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
651 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
652 531c3985 2020-03-18 stsp /* Read packfile data. */
653 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
656 531c3985 2020-03-18 stsp if (r != datalen) {
657 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
658 531c3985 2020-03-18 stsp "packet too short");
661 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
662 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
665 531c3985 2020-03-18 stsp if (r != datalen) {
666 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
667 531c3985 2020-03-18 stsp "packet too short");
670 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
674 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
675 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
678 531c3985 2020-03-18 stsp if (r != datalen) {
679 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
680 531c3985 2020-03-18 stsp "packet too short");
683 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
686 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
687 531c3985 2020-03-18 stsp "unknown side-band received from server");
691 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
692 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
695 531c3985 2020-03-18 stsp if (r <= 0)
699 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
700 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
701 9ff10419 2020-03-18 stsp if (w == -1) {
702 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
705 fe53745c 2020-03-18 stsp if (w != r) {
706 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
709 531c3985 2020-03-18 stsp packsz += w;
711 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
712 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
713 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf,
717 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
720 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf, packsz);
724 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
725 abe0f35f 2020-03-18 stsp free((void *)pe->path);
726 abe0f35f 2020-03-18 stsp free(pe->data);
728 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
729 9ff10419 2020-03-18 stsp free(have);
730 9ff10419 2020-03-18 stsp free(want);
731 00cd0e0a 2020-03-18 stsp free(id_str);
732 00cd0e0a 2020-03-18 stsp free(refname);
733 00cd0e0a 2020-03-18 stsp free(server_capabilities);
734 8f2d01a6 2020-03-18 stsp return err;
739 93658fb9 2020-03-18 stsp main(int argc, char **argv)
741 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
742 9ff10419 2020-03-18 stsp int fetchfd, packfd = -1;
743 93658fb9 2020-03-18 stsp struct got_object_id packid;
744 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
745 93658fb9 2020-03-18 stsp struct imsg imsg;
746 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
747 33501562 2020-03-18 stsp struct got_imsg_fetch_have_refs *fetch_have_refs = NULL;
748 33501562 2020-03-18 stsp size_t datalen;
750 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
752 cf875574 2020-03-18 stsp if (getenv("GOT_DEBUG") != NULL) {
753 93658fb9 2020-03-18 stsp fprintf(stderr, "fetch-pack being chatty!\n");
754 93658fb9 2020-03-18 stsp chattygit = 1;
757 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
758 ffb5f621 2020-03-18 stsp #ifndef PROFILE
759 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
760 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
761 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
762 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
766 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
767 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
768 93658fb9 2020-03-18 stsp err = NULL;
771 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
773 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
774 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
777 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
778 33501562 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_fetch_have_refs)) {
779 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
782 33501562 2020-03-18 stsp fetch_have_refs = (struct got_imsg_fetch_have_refs *)imsg.data;
783 33501562 2020-03-18 stsp if (datalen != sizeof(struct got_imsg_fetch_have_refs) +
784 33501562 2020-03-18 stsp sizeof(struct got_imsg_fetch_have_ref) *
785 33501562 2020-03-18 stsp fetch_have_refs->n_have_refs) {
786 33501562 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
789 33501562 2020-03-18 stsp if (fetch_have_refs->n_have_refs != 0) {
790 33501562 2020-03-18 stsp /* TODO: Incremental fetch support */
791 33501562 2020-03-18 stsp err = got_error(GOT_ERR_NOT_IMPL);
794 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
796 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
797 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
798 93658fb9 2020-03-18 stsp err = NULL;
801 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
803 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
804 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
807 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
808 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
811 93658fb9 2020-03-18 stsp packfd = imsg.fd;
813 33501562 2020-03-18 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
815 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
816 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
817 9ff10419 2020-03-18 stsp if (err != NULL)
818 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
820 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
821 cf875574 2020-03-18 stsp if (err != NULL) {
822 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
823 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);