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;
217 eac2c4cd 2020-03-18 stsp static const struct got_error *
218 1ff21071 2020-03-18 stsp check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
220 fe53745c 2020-03-18 stsp const struct got_error *err = NULL;
221 93658fb9 2020-03-18 stsp SHA1_CTX ctx;
222 93658fb9 2020-03-18 stsp uint8_t hexpect[SHA1_DIGEST_LENGTH];
223 2fed35f3 2020-03-18 stsp uint8_t buf[32 * 1024];
224 93658fb9 2020-03-18 stsp ssize_t n, r, nr;
226 0872c0b0 2020-03-18 stsp if (sz < sizeof(struct got_packfile_hdr) + SHA1_DIGEST_LENGTH)
227 eac2c4cd 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKFILE);
230 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
231 cf875574 2020-03-18 stsp while (n < sz - 20) {
232 93658fb9 2020-03-18 stsp nr = sizeof(buf);
233 cf875574 2020-03-18 stsp if (sz - n - 20 < sizeof(buf))
234 93658fb9 2020-03-18 stsp nr = sz - n - 20;
235 fe53745c 2020-03-18 stsp err = readn(&r, fd, buf, nr);
237 fe53745c 2020-03-18 stsp return err;
238 cf875574 2020-03-18 stsp if (r != nr)
239 eac2c4cd 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKFILE);
240 93658fb9 2020-03-18 stsp SHA1Update(&ctx, buf, nr);
243 93658fb9 2020-03-18 stsp SHA1Final(hcomp, &ctx);
245 fe53745c 2020-03-18 stsp err = readn(&r, fd, hexpect, sizeof(hexpect));
247 fe53745c 2020-03-18 stsp return err;
248 fe53745c 2020-03-18 stsp if (r != sizeof(hexpect))
249 eac2c4cd 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKFILE);
250 d3dccf3a 2020-03-18 stsp if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0)
251 eac2c4cd 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKFILE);
252 eac2c4cd 2020-03-18 stsp return NULL;
256 1ff21071 2020-03-18 stsp match_branch(char *br, char *pat)
258 93658fb9 2020-03-18 stsp char name[128];
260 cf875574 2020-03-18 stsp if (strstr(pat, "refs/heads") == pat) {
261 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
263 cf875574 2020-03-18 stsp } else if (strstr(pat, "heads")) {
264 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/%s", pat)
265 cf875574 2020-03-18 stsp >= sizeof(name))
268 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
269 cf875574 2020-03-18 stsp >= sizeof(name))
272 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
275 0d0a341c 2020-03-18 stsp static const struct got_error *
276 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
278 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
280 0d0a341c 2020-03-18 stsp size_t i, n = 0;
282 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
283 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
285 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
286 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
291 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
292 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
296 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
297 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
298 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
301 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
302 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
307 0d0a341c 2020-03-18 stsp if (i <= 2)
308 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
312 0d0a341c 2020-03-18 stsp for (j = 0; j < i; j++)
313 0d0a341c 2020-03-18 stsp free(tokens[j]);
314 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
316 0d0a341c 2020-03-18 stsp return err;
319 00cd0e0a 2020-03-18 stsp static const struct got_error *
320 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
321 00cd0e0a 2020-03-18 stsp char *line, int len)
323 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
324 00cd0e0a 2020-03-18 stsp char *tokens[3];
326 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
328 00cd0e0a 2020-03-18 stsp return err;
330 00cd0e0a 2020-03-18 stsp if (tokens[0])
331 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
332 00cd0e0a 2020-03-18 stsp if (tokens[1])
333 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
334 00cd0e0a 2020-03-18 stsp if (tokens[2])
335 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
337 00cd0e0a 2020-03-18 stsp return NULL;
340 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
341 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
342 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
344 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
345 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
346 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
349 8a29a085 2020-03-18 stsp struct got_capability {
350 8a29a085 2020-03-18 stsp const char *key;
351 8a29a085 2020-03-18 stsp const char *value;
353 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
354 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
355 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
356 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
359 8a29a085 2020-03-18 stsp static const struct got_error *
360 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
361 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
363 8a29a085 2020-03-18 stsp char *equalsign;
366 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
367 8a29a085 2020-03-18 stsp if (equalsign) {
368 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
369 8a29a085 2020-03-18 stsp return NULL;
371 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
372 8a29a085 2020-03-18 stsp return NULL;
375 8a29a085 2020-03-18 stsp if (asprintf(&s, "%s%s%s%s%s",
376 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
377 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? " " : "",
378 8a29a085 2020-03-18 stsp mycapa->key,
379 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
380 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
381 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
383 8a29a085 2020-03-18 stsp free(*my_capabilities);
384 8a29a085 2020-03-18 stsp *my_capabilities = s;
385 8a29a085 2020-03-18 stsp return NULL;
388 9ff10419 2020-03-18 stsp static const struct got_error *
389 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
391 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
392 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
394 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
395 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
396 abe0f35f 2020-03-18 stsp return NULL;
398 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
399 abe0f35f 2020-03-18 stsp if (colon == NULL)
400 abe0f35f 2020-03-18 stsp return NULL;
402 abe0f35f 2020-03-18 stsp *colon = '\0';
403 abe0f35f 2020-03-18 stsp name = strdup(capa);
404 abe0f35f 2020-03-18 stsp if (name == NULL)
405 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
407 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
408 abe0f35f 2020-03-18 stsp if (target == NULL) {
409 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
413 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
414 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
417 abe0f35f 2020-03-18 stsp free(name);
418 abe0f35f 2020-03-18 stsp free(target);
420 abe0f35f 2020-03-18 stsp return err;
423 abe0f35f 2020-03-18 stsp static const struct got_error *
424 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
425 abe0f35f 2020-03-18 stsp char *server_capabilities)
427 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
428 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
431 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
433 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
434 8a29a085 2020-03-18 stsp if (capa == NULL)
435 8a29a085 2020-03-18 stsp return NULL;
437 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
438 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
439 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
440 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
446 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
447 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
448 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
452 8a29a085 2020-03-18 stsp } while (capa);
454 8a29a085 2020-03-18 stsp return err;
457 531c3985 2020-03-18 stsp static const struct got_error *
458 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
463 531c3985 2020-03-18 stsp if (len == 0)
464 531c3985 2020-03-18 stsp return NULL;
467 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
468 531c3985 2020-03-18 stsp * Server may send up to 64k.
470 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
471 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
473 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
474 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
475 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
476 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
478 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
479 531c3985 2020-03-18 stsp "non-printable progress message received from server");
482 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
485 8a29a085 2020-03-18 stsp static const struct got_error *
486 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
488 531c3985 2020-03-18 stsp static char msg[1024];
491 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
492 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
493 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
494 531c3985 2020-03-18 stsp "non-printable error message received from server");
495 531c3985 2020-03-18 stsp msg[i] = buf[i];
497 531c3985 2020-03-18 stsp msg[i] = '\0';
498 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
501 531c3985 2020-03-18 stsp static const struct got_error *
502 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
503 33501562 2020-03-18 stsp struct got_pathlist_head *have_refs, struct imsgbuf *ibuf)
505 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
506 00cd0e0a 2020-03-18 stsp char buf[GOT_PKTMAX];
507 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
508 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
509 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
510 93658fb9 2020-03-18 stsp int i, n, req;
511 93658fb9 2020-03-18 stsp off_t packsz;
512 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
513 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
514 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
515 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
516 531c3985 2020-03-18 stsp int have_sidebands = 0;
518 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
520 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
521 9ff10419 2020-03-18 stsp if (have == NULL)
522 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
523 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
524 9ff10419 2020-03-18 stsp if (want == NULL) {
525 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
528 9ff10419 2020-03-18 stsp if (chattygit)
529 9ff10419 2020-03-18 stsp fprintf(stderr, "starting fetch\n");
530 9ff10419 2020-03-18 stsp while (1) {
531 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
534 9ff10419 2020-03-18 stsp if (n == 0)
536 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
537 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
540 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
544 00cd0e0a 2020-03-18 stsp if (chattygit && server_capabilities[0] != '\0')
545 00cd0e0a 2020-03-18 stsp fprintf(stderr, "server capabilities: %s\n",
546 00cd0e0a 2020-03-18 stsp server_capabilities);
547 8a29a085 2020-03-18 stsp if (is_firstpkt) {
548 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
549 00cd0e0a 2020-03-18 stsp server_capabilities);
552 8a29a085 2020-03-18 stsp if (chattygit && my_capabilities)
553 8a29a085 2020-03-18 stsp fprintf(stderr, "my matched capabilities: %s\n",
554 8a29a085 2020-03-18 stsp my_capabilities);
555 abe0f35f 2020-03-18 stsp err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
559 8a29a085 2020-03-18 stsp is_firstpkt = 0;
560 00cd0e0a 2020-03-18 stsp if (strstr(refname, "^{}"))
562 00cd0e0a 2020-03-18 stsp if (fetchbranch && !match_branch(refname, fetchbranch))
564 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
565 93658fb9 2020-03-18 stsp refsz *= 2;
566 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
567 9ff10419 2020-03-18 stsp if (have == NULL) {
568 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
571 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
572 9ff10419 2020-03-18 stsp if (want == NULL) {
573 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
577 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
578 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
582 00cd0e0a 2020-03-18 stsp err = match_remote_ref(have_refs, &have[nref], id_str, refname);
586 00cd0e0a 2020-03-18 stsp err = got_privsep_send_fetch_progress(ibuf, &want[nref],
590 93658fb9 2020-03-18 stsp if (chattygit)
591 00cd0e0a 2020-03-18 stsp fprintf(stderr, "remote %s\n", refname);
596 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
597 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
599 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
600 13ce8c93 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "want %s%s%s\n", hashstr,
601 13ce8c93 2020-03-18 stsp i == 0 && my_capabilities ? " " : "",
602 8a29a085 2020-03-18 stsp i == 0 && my_capabilities ? my_capabilities : "");
603 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
604 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
607 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
612 38c670f1 2020-03-18 stsp err = flushpkt(fd);
615 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
616 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
618 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
619 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
620 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
621 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
624 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n + 1);
628 cf875574 2020-03-18 stsp if (!req) {
629 3b9fb585 2020-03-18 stsp if (chattygit)
630 3b9fb585 2020-03-18 stsp fprintf(stderr, "up to date\n");
631 38c670f1 2020-03-18 stsp err = flushpkt(fd);
635 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
636 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
642 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
646 04c53c18 2020-03-18 stsp * For now, we only support a full clone, in which case the server
647 04c53c18 2020-03-18 stsp * will now send a "NAK" (meaning no common objects were found).
649 04c53c18 2020-03-18 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
650 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
651 531c3985 2020-03-18 stsp "unexpected message from server");
655 8f2d01a6 2020-03-18 stsp if (chattygit)
656 8f2d01a6 2020-03-18 stsp fprintf(stderr, "fetching...\n");
658 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
659 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
660 531c3985 2020-03-18 stsp have_sidebands = 1;
662 93658fb9 2020-03-18 stsp packsz = 0;
663 9ff10419 2020-03-18 stsp while (1) {
664 531c3985 2020-03-18 stsp ssize_t r = 0, w;
665 531c3985 2020-03-18 stsp int datalen = -1;
667 531c3985 2020-03-18 stsp if (have_sidebands) {
668 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
671 531c3985 2020-03-18 stsp if (datalen <= 0)
674 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
675 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
676 531c3985 2020-03-18 stsp if (r == -1) {
677 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
680 531c3985 2020-03-18 stsp if (r != 1) {
681 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
682 531c3985 2020-03-18 stsp "short packet");
685 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
686 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
687 531c3985 2020-03-18 stsp "bad packet length");
690 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
691 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
692 531c3985 2020-03-18 stsp /* Read packfile data. */
693 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
696 531c3985 2020-03-18 stsp if (r != datalen) {
697 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
698 531c3985 2020-03-18 stsp "packet too short");
701 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
702 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
705 531c3985 2020-03-18 stsp if (r != datalen) {
706 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
707 531c3985 2020-03-18 stsp "packet too short");
710 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
714 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
715 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
718 531c3985 2020-03-18 stsp if (r != datalen) {
719 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
720 531c3985 2020-03-18 stsp "packet too short");
723 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
726 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
727 531c3985 2020-03-18 stsp "unknown side-band received from server");
731 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
732 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
735 531c3985 2020-03-18 stsp if (r <= 0)
739 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
740 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
741 9ff10419 2020-03-18 stsp if (w == -1) {
742 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
745 fe53745c 2020-03-18 stsp if (w != r) {
746 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
749 531c3985 2020-03-18 stsp packsz += w;
751 9ff10419 2020-03-18 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
752 9ff10419 2020-03-18 stsp err = got_error_from_errno("lseek");
755 eac2c4cd 2020-03-18 stsp err = check_pack_hash(packfd, packsz, packid->sha1);
757 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
758 abe0f35f 2020-03-18 stsp free((void *)pe->path);
759 abe0f35f 2020-03-18 stsp free(pe->data);
761 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
762 9ff10419 2020-03-18 stsp free(have);
763 9ff10419 2020-03-18 stsp free(want);
764 00cd0e0a 2020-03-18 stsp free(id_str);
765 00cd0e0a 2020-03-18 stsp free(refname);
766 00cd0e0a 2020-03-18 stsp free(server_capabilities);
767 8f2d01a6 2020-03-18 stsp return err;
772 93658fb9 2020-03-18 stsp main(int argc, char **argv)
774 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
775 9ff10419 2020-03-18 stsp int fetchfd, packfd = -1;
776 93658fb9 2020-03-18 stsp struct got_object_id packid;
777 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
778 93658fb9 2020-03-18 stsp struct imsg imsg;
779 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
780 33501562 2020-03-18 stsp struct got_imsg_fetch_have_refs *fetch_have_refs = NULL;
781 33501562 2020-03-18 stsp size_t datalen;
783 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
785 cf875574 2020-03-18 stsp if (getenv("GOT_DEBUG") != NULL) {
786 93658fb9 2020-03-18 stsp fprintf(stderr, "fetch-pack being chatty!\n");
787 93658fb9 2020-03-18 stsp chattygit = 1;
790 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
791 ffb5f621 2020-03-18 stsp #ifndef PROFILE
792 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
793 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
794 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
795 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
799 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
800 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
801 93658fb9 2020-03-18 stsp err = NULL;
804 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
806 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
807 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
810 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
811 33501562 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_fetch_have_refs)) {
812 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
815 33501562 2020-03-18 stsp fetch_have_refs = (struct got_imsg_fetch_have_refs *)imsg.data;
816 33501562 2020-03-18 stsp if (datalen != sizeof(struct got_imsg_fetch_have_refs) +
817 33501562 2020-03-18 stsp sizeof(struct got_imsg_fetch_have_ref) *
818 33501562 2020-03-18 stsp fetch_have_refs->n_have_refs) {
819 33501562 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
822 33501562 2020-03-18 stsp if (fetch_have_refs->n_have_refs != 0) {
823 33501562 2020-03-18 stsp /* TODO: Incremental fetch support */
824 33501562 2020-03-18 stsp err = got_error(GOT_ERR_NOT_IMPL);
827 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
829 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
830 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
831 93658fb9 2020-03-18 stsp err = NULL;
834 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
836 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
837 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
840 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
841 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
844 93658fb9 2020-03-18 stsp packfd = imsg.fd;
846 33501562 2020-03-18 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
848 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
849 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
850 9ff10419 2020-03-18 stsp if (err != NULL)
851 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
853 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
854 cf875574 2020-03-18 stsp if (err != NULL) {
855 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
856 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);