Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 93658fb9 2020-03-18 stsp *
4 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
7 93658fb9 2020-03-18 stsp *
8 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 93658fb9 2020-03-18 stsp */
16 93658fb9 2020-03-18 stsp
17 93658fb9 2020-03-18 stsp #include <sys/types.h>
18 93658fb9 2020-03-18 stsp #include <sys/queue.h>
19 93658fb9 2020-03-18 stsp #include <sys/uio.h>
20 93658fb9 2020-03-18 stsp #include <sys/time.h>
21 93658fb9 2020-03-18 stsp #include <sys/stat.h>
22 93658fb9 2020-03-18 stsp #include <sys/syslimits.h>
23 93658fb9 2020-03-18 stsp
24 93658fb9 2020-03-18 stsp #include <stdint.h>
25 93658fb9 2020-03-18 stsp #include <errno.h>
26 93658fb9 2020-03-18 stsp #include <imsg.h>
27 93658fb9 2020-03-18 stsp #include <limits.h>
28 93658fb9 2020-03-18 stsp #include <signal.h>
29 93658fb9 2020-03-18 stsp #include <stdio.h>
30 93658fb9 2020-03-18 stsp #include <stdlib.h>
31 93658fb9 2020-03-18 stsp #include <string.h>
32 93658fb9 2020-03-18 stsp #include <ctype.h>
33 93658fb9 2020-03-18 stsp #include <sha1.h>
34 93658fb9 2020-03-18 stsp #include <fcntl.h>
35 93658fb9 2020-03-18 stsp #include <zlib.h>
36 93658fb9 2020-03-18 stsp #include <err.h>
37 93658fb9 2020-03-18 stsp
38 93658fb9 2020-03-18 stsp #include "got_error.h"
39 93658fb9 2020-03-18 stsp #include "got_object.h"
40 abe0f35f 2020-03-18 stsp #include "got_path.h"
41 8a29a085 2020-03-18 stsp #include "got_version.h"
42 93658fb9 2020-03-18 stsp
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"
49 93658fb9 2020-03-18 stsp
50 8a29a085 2020-03-18 stsp #ifndef nitems
51 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 8a29a085 2020-03-18 stsp #endif
53 8a29a085 2020-03-18 stsp
54 93658fb9 2020-03-18 stsp #define GOT_PKTMAX 65536
55 93658fb9 2020-03-18 stsp
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}};
60 93658fb9 2020-03-18 stsp
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)
63 93658fb9 2020-03-18 stsp {
64 fe53745c 2020-03-18 stsp ssize_t r;
65 93658fb9 2020-03-18 stsp
66 fe53745c 2020-03-18 stsp *off = 0;
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");
71 93658fb9 2020-03-18 stsp if (r == 0)
72 fe53745c 2020-03-18 stsp return NULL;
73 fe53745c 2020-03-18 stsp *off += r;
74 93658fb9 2020-03-18 stsp }
75 fe53745c 2020-03-18 stsp return NULL;
76 93658fb9 2020-03-18 stsp }
77 93658fb9 2020-03-18 stsp
78 38c670f1 2020-03-18 stsp static const struct got_error *
79 93658fb9 2020-03-18 stsp flushpkt(int fd)
80 93658fb9 2020-03-18 stsp {
81 38c670f1 2020-03-18 stsp ssize_t w;
82 38c670f1 2020-03-18 stsp
83 cf875574 2020-03-18 stsp if (chattygit)
84 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: 0000\n");
85 38c670f1 2020-03-18 stsp
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");
89 38c670f1 2020-03-18 stsp if (w != 4)
90 38c670f1 2020-03-18 stsp return got_error(GOT_ERR_IO);
91 38c670f1 2020-03-18 stsp return NULL;
92 93658fb9 2020-03-18 stsp }
93 93658fb9 2020-03-18 stsp
94 531c3985 2020-03-18 stsp /*
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.
97 531c3985 2020-03-18 stsp */
98 fe53745c 2020-03-18 stsp static const struct got_error *
99 531c3985 2020-03-18 stsp read_pkthdr(int *datalen, int fd)
100 93658fb9 2020-03-18 stsp {
101 531c3985 2020-03-18 stsp static const struct got_error *err = NULL;
102 4dc8ee09 2020-03-18 stsp char lenstr[5];
103 4dc8ee09 2020-03-18 stsp long len;
104 93658fb9 2020-03-18 stsp char *e;
105 54d1a70f 2020-03-18 stsp int n, i;
106 fe53745c 2020-03-18 stsp ssize_t r;
107 93658fb9 2020-03-18 stsp
108 531c3985 2020-03-18 stsp *datalen = 0;
109 fe53745c 2020-03-18 stsp
110 4dc8ee09 2020-03-18 stsp err = readn(&r, fd, lenstr, 4);
111 fe53745c 2020-03-18 stsp if (err)
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");
118 fe53745c 2020-03-18 stsp
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");
124 54d1a70f 2020-03-18 stsp }
125 4dc8ee09 2020-03-18 stsp errno = 0;
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");
133 4dc8ee09 2020-03-18 stsp n = len;
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");
138 93658fb9 2020-03-18 stsp n -= 4;
139 531c3985 2020-03-18 stsp
140 531c3985 2020-03-18 stsp *datalen = n;
141 531c3985 2020-03-18 stsp return NULL;
142 531c3985 2020-03-18 stsp }
143 531c3985 2020-03-18 stsp
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)
146 531c3985 2020-03-18 stsp {
147 531c3985 2020-03-18 stsp const struct got_error *err = NULL;
148 531c3985 2020-03-18 stsp int datalen;
149 531c3985 2020-03-18 stsp ssize_t n;
150 531c3985 2020-03-18 stsp
151 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
152 531c3985 2020-03-18 stsp if (err)
153 531c3985 2020-03-18 stsp return err;
154 531c3985 2020-03-18 stsp
155 531c3985 2020-03-18 stsp if (datalen > buflen)
156 fe53745c 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
157 fe53745c 2020-03-18 stsp
158 531c3985 2020-03-18 stsp err = readn(&n, fd, buf, datalen);
159 fe53745c 2020-03-18 stsp if (err)
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");
163 fe53745c 2020-03-18 stsp
164 fe53745c 2020-03-18 stsp *outlen = n;
165 fe53745c 2020-03-18 stsp return NULL;
166 93658fb9 2020-03-18 stsp }
167 93658fb9 2020-03-18 stsp
168 344e4747 2020-03-18 stsp static const struct got_error *
169 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
170 93658fb9 2020-03-18 stsp {
171 93658fb9 2020-03-18 stsp char len[5];
172 93658fb9 2020-03-18 stsp int i;
173 344e4747 2020-03-18 stsp ssize_t w;
174 93658fb9 2020-03-18 stsp
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);
193 93658fb9 2020-03-18 stsp }
194 f78e0473 2020-03-18 stsp fputc('\n', stderr);
195 93658fb9 2020-03-18 stsp }
196 344e4747 2020-03-18 stsp return NULL;
197 93658fb9 2020-03-18 stsp }
198 93658fb9 2020-03-18 stsp
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)
202 93658fb9 2020-03-18 stsp {
203 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
204 93658fb9 2020-03-18 stsp
205 33501562 2020-03-18 stsp memset(id, 0, sizeof(*id));
206 93658fb9 2020-03-18 stsp
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);
211 33501562 2020-03-18 stsp break;
212 33501562 2020-03-18 stsp }
213 93658fb9 2020-03-18 stsp }
214 33501562 2020-03-18 stsp return NULL;
215 93658fb9 2020-03-18 stsp }
216 93658fb9 2020-03-18 stsp
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)
219 93658fb9 2020-03-18 stsp {
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;
225 93658fb9 2020-03-18 stsp
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);
228 93658fb9 2020-03-18 stsp
229 93658fb9 2020-03-18 stsp n = 0;
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);
236 fe53745c 2020-03-18 stsp if (err)
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);
241 93658fb9 2020-03-18 stsp n += r;
242 93658fb9 2020-03-18 stsp }
243 93658fb9 2020-03-18 stsp SHA1Final(hcomp, &ctx);
244 93658fb9 2020-03-18 stsp
245 fe53745c 2020-03-18 stsp err = readn(&r, fd, hexpect, sizeof(hexpect));
246 fe53745c 2020-03-18 stsp if (err)
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;
253 93658fb9 2020-03-18 stsp }
254 93658fb9 2020-03-18 stsp
255 93658fb9 2020-03-18 stsp static int
256 1ff21071 2020-03-18 stsp match_branch(char *br, char *pat)
257 93658fb9 2020-03-18 stsp {
258 93658fb9 2020-03-18 stsp char name[128];
259 93658fb9 2020-03-18 stsp
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))
262 93658fb9 2020-03-18 stsp return -1;
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))
266 93658fb9 2020-03-18 stsp return -1;
267 93658fb9 2020-03-18 stsp } else {
268 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
269 cf875574 2020-03-18 stsp >= sizeof(name))
270 93658fb9 2020-03-18 stsp return -1;
271 93658fb9 2020-03-18 stsp }
272 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
273 93658fb9 2020-03-18 stsp }
274 93658fb9 2020-03-18 stsp
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)
277 93658fb9 2020-03-18 stsp {
278 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
279 93658fb9 2020-03-18 stsp char *p;
280 0d0a341c 2020-03-18 stsp size_t i, n = 0;
281 93658fb9 2020-03-18 stsp
282 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
283 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
284 0d0a341c 2020-03-18 stsp
285 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
286 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
287 93658fb9 2020-03-18 stsp line++;
288 0d0a341c 2020-03-18 stsp n++;
289 0d0a341c 2020-03-18 stsp }
290 93658fb9 2020-03-18 stsp p = line;
291 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
292 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
293 93658fb9 2020-03-18 stsp line++;
294 0d0a341c 2020-03-18 stsp n++;
295 0d0a341c 2020-03-18 stsp }
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");
299 0d0a341c 2020-03-18 stsp goto done;
300 0d0a341c 2020-03-18 stsp }
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) {
303 0d0a341c 2020-03-18 stsp line++;
304 0d0a341c 2020-03-18 stsp n++;
305 0d0a341c 2020-03-18 stsp }
306 93658fb9 2020-03-18 stsp }
307 0d0a341c 2020-03-18 stsp if (i <= 2)
308 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
309 0d0a341c 2020-03-18 stsp done:
310 0d0a341c 2020-03-18 stsp if (err) {
311 0d0a341c 2020-03-18 stsp int j;
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;
315 0d0a341c 2020-03-18 stsp }
316 0d0a341c 2020-03-18 stsp return err;
317 8a29a085 2020-03-18 stsp }
318 00cd0e0a 2020-03-18 stsp
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)
322 00cd0e0a 2020-03-18 stsp {
323 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
324 00cd0e0a 2020-03-18 stsp char *tokens[3];
325 8a29a085 2020-03-18 stsp
326 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
327 00cd0e0a 2020-03-18 stsp if (err)
328 00cd0e0a 2020-03-18 stsp return err;
329 00cd0e0a 2020-03-18 stsp
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];
336 00cd0e0a 2020-03-18 stsp
337 00cd0e0a 2020-03-18 stsp return NULL;
338 00cd0e0a 2020-03-18 stsp }
339 00cd0e0a 2020-03-18 stsp
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"
343 531c3985 2020-03-18 stsp
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
347 531c3985 2020-03-18 stsp
348 531c3985 2020-03-18 stsp
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;
352 8a29a085 2020-03-18 stsp };
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 },
357 8a29a085 2020-03-18 stsp };
358 8a29a085 2020-03-18 stsp
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)
362 8a29a085 2020-03-18 stsp {
363 8a29a085 2020-03-18 stsp char *equalsign;
364 8a29a085 2020-03-18 stsp char *s;
365 8a29a085 2020-03-18 stsp
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;
370 8a29a085 2020-03-18 stsp } else {
371 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
372 8a29a085 2020-03-18 stsp return NULL;
373 8a29a085 2020-03-18 stsp }
374 8a29a085 2020-03-18 stsp
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");
382 8a29a085 2020-03-18 stsp
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;
386 93658fb9 2020-03-18 stsp }
387 93658fb9 2020-03-18 stsp
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)
390 8a29a085 2020-03-18 stsp {
391 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
392 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
393 abe0f35f 2020-03-18 stsp
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;
397 abe0f35f 2020-03-18 stsp
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;
401 abe0f35f 2020-03-18 stsp
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");
406 abe0f35f 2020-03-18 stsp
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");
410 abe0f35f 2020-03-18 stsp goto done;
411 abe0f35f 2020-03-18 stsp }
412 abe0f35f 2020-03-18 stsp
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);
415 abe0f35f 2020-03-18 stsp done:
416 abe0f35f 2020-03-18 stsp if (err) {
417 abe0f35f 2020-03-18 stsp free(name);
418 abe0f35f 2020-03-18 stsp free(target);
419 abe0f35f 2020-03-18 stsp }
420 abe0f35f 2020-03-18 stsp return err;
421 abe0f35f 2020-03-18 stsp }
422 abe0f35f 2020-03-18 stsp
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)
426 abe0f35f 2020-03-18 stsp {
427 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
428 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
429 8a29a085 2020-03-18 stsp int i;
430 8a29a085 2020-03-18 stsp
431 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
432 8a29a085 2020-03-18 stsp do {
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;
436 8a29a085 2020-03-18 stsp
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);
441 abe0f35f 2020-03-18 stsp if (err)
442 abe0f35f 2020-03-18 stsp break;
443 abe0f35f 2020-03-18 stsp continue;
444 abe0f35f 2020-03-18 stsp }
445 abe0f35f 2020-03-18 stsp
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]);
449 8a29a085 2020-03-18 stsp if (err)
450 8a29a085 2020-03-18 stsp break;
451 8a29a085 2020-03-18 stsp }
452 8a29a085 2020-03-18 stsp } while (capa);
453 8a29a085 2020-03-18 stsp
454 8a29a085 2020-03-18 stsp return err;
455 531c3985 2020-03-18 stsp }
456 531c3985 2020-03-18 stsp
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)
459 531c3985 2020-03-18 stsp {
460 531c3985 2020-03-18 stsp int i;
461 531c3985 2020-03-18 stsp
462 531c3985 2020-03-18 stsp
463 531c3985 2020-03-18 stsp if (len == 0)
464 531c3985 2020-03-18 stsp return NULL;
465 531c3985 2020-03-18 stsp
466 531c3985 2020-03-18 stsp /*
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.
469 531c3985 2020-03-18 stsp */
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;
472 531c3985 2020-03-18 stsp
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]))
477 531c3985 2020-03-18 stsp continue;
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");
480 531c3985 2020-03-18 stsp }
481 531c3985 2020-03-18 stsp
482 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
483 8a29a085 2020-03-18 stsp }
484 abe0f35f 2020-03-18 stsp
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)
487 531c3985 2020-03-18 stsp {
488 531c3985 2020-03-18 stsp static char msg[1024];
489 531c3985 2020-03-18 stsp int i;
490 531c3985 2020-03-18 stsp
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];
496 531c3985 2020-03-18 stsp }
497 531c3985 2020-03-18 stsp msg[i] = '\0';
498 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
499 531c3985 2020-03-18 stsp }
500 531c3985 2020-03-18 stsp
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)
504 93658fb9 2020-03-18 stsp {
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;
517 93658fb9 2020-03-18 stsp
518 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
519 abe0f35f 2020-03-18 stsp
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");
526 9ff10419 2020-03-18 stsp goto done;
527 9ff10419 2020-03-18 stsp }
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));
532 fe53745c 2020-03-18 stsp if (err)
533 9ff10419 2020-03-18 stsp goto done;
534 9ff10419 2020-03-18 stsp if (n == 0)
535 93658fb9 2020-03-18 stsp break;
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);
538 9ff10419 2020-03-18 stsp goto done;
539 9ff10419 2020-03-18 stsp }
540 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
541 00cd0e0a 2020-03-18 stsp buf, n);
542 0d0a341c 2020-03-18 stsp if (err)
543 9ff10419 2020-03-18 stsp goto done;
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);
550 8a29a085 2020-03-18 stsp if (err)
551 8a29a085 2020-03-18 stsp goto done;
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);
556 abe0f35f 2020-03-18 stsp if (err)
557 abe0f35f 2020-03-18 stsp goto done;
558 8a29a085 2020-03-18 stsp }
559 8a29a085 2020-03-18 stsp is_firstpkt = 0;
560 00cd0e0a 2020-03-18 stsp if (strstr(refname, "^{}"))
561 93658fb9 2020-03-18 stsp continue;
562 00cd0e0a 2020-03-18 stsp if (fetchbranch && !match_branch(refname, fetchbranch))
563 93658fb9 2020-03-18 stsp continue;
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");
569 9ff10419 2020-03-18 stsp goto done;
570 9ff10419 2020-03-18 stsp }
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");
574 9ff10419 2020-03-18 stsp goto done;
575 9ff10419 2020-03-18 stsp }
576 93658fb9 2020-03-18 stsp }
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);
579 9ff10419 2020-03-18 stsp goto done;
580 9ff10419 2020-03-18 stsp }
581 9ff10419 2020-03-18 stsp
582 00cd0e0a 2020-03-18 stsp err = match_remote_ref(have_refs, &have[nref], id_str, refname);
583 33501562 2020-03-18 stsp if (err)
584 33501562 2020-03-18 stsp goto done;
585 33501562 2020-03-18 stsp
586 00cd0e0a 2020-03-18 stsp err = got_privsep_send_fetch_progress(ibuf, &want[nref],
587 00cd0e0a 2020-03-18 stsp refname);
588 8f2d01a6 2020-03-18 stsp if (err)
589 8f2d01a6 2020-03-18 stsp goto done;
590 93658fb9 2020-03-18 stsp if (chattygit)
591 00cd0e0a 2020-03-18 stsp fprintf(stderr, "remote %s\n", refname);
592 93658fb9 2020-03-18 stsp nref++;
593 93658fb9 2020-03-18 stsp }
594 93658fb9 2020-03-18 stsp
595 93658fb9 2020-03-18 stsp req = 0;
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)
598 93658fb9 2020-03-18 stsp continue;
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);
605 9ff10419 2020-03-18 stsp goto done;
606 9ff10419 2020-03-18 stsp }
607 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
608 344e4747 2020-03-18 stsp if (err)
609 9ff10419 2020-03-18 stsp goto done;
610 93658fb9 2020-03-18 stsp req = 1;
611 93658fb9 2020-03-18 stsp }
612 38c670f1 2020-03-18 stsp err = flushpkt(fd);
613 38c670f1 2020-03-18 stsp if (err)
614 38c670f1 2020-03-18 stsp goto done;
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)
617 93658fb9 2020-03-18 stsp continue;
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);
622 9ff10419 2020-03-18 stsp goto done;
623 9ff10419 2020-03-18 stsp }
624 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n + 1);
625 344e4747 2020-03-18 stsp if (err)
626 9ff10419 2020-03-18 stsp goto done;
627 93658fb9 2020-03-18 stsp }
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);
632 38c670f1 2020-03-18 stsp if (err)
633 38c670f1 2020-03-18 stsp goto done;
634 93658fb9 2020-03-18 stsp }
635 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
636 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
637 344e4747 2020-03-18 stsp if (err)
638 9ff10419 2020-03-18 stsp goto done;
639 9ff10419 2020-03-18 stsp if (!req)
640 93658fb9 2020-03-18 stsp return 0;
641 93658fb9 2020-03-18 stsp
642 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
643 fe53745c 2020-03-18 stsp if (err)
644 9ff10419 2020-03-18 stsp goto done;
645 04c53c18 2020-03-18 stsp /*
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).
648 04c53c18 2020-03-18 stsp */
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");
652 04c53c18 2020-03-18 stsp goto done;
653 04c53c18 2020-03-18 stsp }
654 93658fb9 2020-03-18 stsp
655 8f2d01a6 2020-03-18 stsp if (chattygit)
656 8f2d01a6 2020-03-18 stsp fprintf(stderr, "fetching...\n");
657 531c3985 2020-03-18 stsp
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;
661 531c3985 2020-03-18 stsp
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;
666 531c3985 2020-03-18 stsp
667 531c3985 2020-03-18 stsp if (have_sidebands) {
668 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
669 531c3985 2020-03-18 stsp if (err)
670 531c3985 2020-03-18 stsp goto done;
671 531c3985 2020-03-18 stsp if (datalen <= 0)
672 531c3985 2020-03-18 stsp break;
673 531c3985 2020-03-18 stsp
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");
678 531c3985 2020-03-18 stsp goto done;
679 531c3985 2020-03-18 stsp }
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");
683 531c3985 2020-03-18 stsp goto done;
684 531c3985 2020-03-18 stsp }
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");
688 531c3985 2020-03-18 stsp goto done;
689 531c3985 2020-03-18 stsp }
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);
694 531c3985 2020-03-18 stsp if (err)
695 531c3985 2020-03-18 stsp goto done;
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");
699 531c3985 2020-03-18 stsp goto done;
700 531c3985 2020-03-18 stsp }
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);
703 531c3985 2020-03-18 stsp if (err)
704 531c3985 2020-03-18 stsp goto done;
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");
708 531c3985 2020-03-18 stsp goto done;
709 531c3985 2020-03-18 stsp }
710 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
711 531c3985 2020-03-18 stsp if (err)
712 531c3985 2020-03-18 stsp goto done;
713 531c3985 2020-03-18 stsp continue;
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);
716 531c3985 2020-03-18 stsp if (err)
717 531c3985 2020-03-18 stsp goto done;
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");
721 531c3985 2020-03-18 stsp goto done;
722 531c3985 2020-03-18 stsp }
723 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
724 531c3985 2020-03-18 stsp goto done;
725 531c3985 2020-03-18 stsp } else {
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");
728 531c3985 2020-03-18 stsp goto done;
729 531c3985 2020-03-18 stsp }
730 531c3985 2020-03-18 stsp } else {
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);
733 531c3985 2020-03-18 stsp if (err)
734 531c3985 2020-03-18 stsp goto done;
735 531c3985 2020-03-18 stsp if (r <= 0)
736 531c3985 2020-03-18 stsp break;
737 531c3985 2020-03-18 stsp }
738 531c3985 2020-03-18 stsp
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");
743 9ff10419 2020-03-18 stsp goto done;
744 9ff10419 2020-03-18 stsp }
745 fe53745c 2020-03-18 stsp if (w != r) {
746 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
747 9ff10419 2020-03-18 stsp goto done;
748 9ff10419 2020-03-18 stsp }
749 531c3985 2020-03-18 stsp packsz += w;
750 93658fb9 2020-03-18 stsp }
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");
753 9ff10419 2020-03-18 stsp goto done;
754 9ff10419 2020-03-18 stsp }
755 eac2c4cd 2020-03-18 stsp err = check_pack_hash(packfd, packsz, packid->sha1);
756 9ff10419 2020-03-18 stsp done:
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);
760 abe0f35f 2020-03-18 stsp }
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;
768 93658fb9 2020-03-18 stsp }
769 93658fb9 2020-03-18 stsp
770 93658fb9 2020-03-18 stsp
771 93658fb9 2020-03-18 stsp int
772 93658fb9 2020-03-18 stsp main(int argc, char **argv)
773 93658fb9 2020-03-18 stsp {
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;
782 33501562 2020-03-18 stsp
783 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
784 93658fb9 2020-03-18 stsp
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;
788 93658fb9 2020-03-18 stsp }
789 93658fb9 2020-03-18 stsp
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);
796 ffb5f621 2020-03-18 stsp return 1;
797 ffb5f621 2020-03-18 stsp }
798 ffb5f621 2020-03-18 stsp #endif
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;
802 93658fb9 2020-03-18 stsp goto done;
803 93658fb9 2020-03-18 stsp }
804 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
805 93658fb9 2020-03-18 stsp goto done;
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);
808 93658fb9 2020-03-18 stsp goto done;
809 93658fb9 2020-03-18 stsp }
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);
813 93658fb9 2020-03-18 stsp goto done;
814 93658fb9 2020-03-18 stsp }
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);
820 33501562 2020-03-18 stsp goto done;
821 33501562 2020-03-18 stsp }
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);
825 33501562 2020-03-18 stsp goto done;
826 33501562 2020-03-18 stsp }
827 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
828 93658fb9 2020-03-18 stsp
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;
832 93658fb9 2020-03-18 stsp goto done;
833 93658fb9 2020-03-18 stsp }
834 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
835 93658fb9 2020-03-18 stsp goto done;
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);
838 93658fb9 2020-03-18 stsp goto done;
839 93658fb9 2020-03-18 stsp }
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);
842 93658fb9 2020-03-18 stsp goto done;
843 93658fb9 2020-03-18 stsp }
844 93658fb9 2020-03-18 stsp packfd = imsg.fd;
845 93658fb9 2020-03-18 stsp
846 33501562 2020-03-18 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
847 93658fb9 2020-03-18 stsp done:
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);
852 93658fb9 2020-03-18 stsp else
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);
857 93658fb9 2020-03-18 stsp }
858 93658fb9 2020-03-18 stsp
859 93658fb9 2020-03-18 stsp exit(0);
860 93658fb9 2020-03-18 stsp }