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 93658fb9 2020-03-18 stsp static int
218 1ff21071 2020-03-18 stsp match_branch(char *br, char *pat)
219 93658fb9 2020-03-18 stsp {
220 93658fb9 2020-03-18 stsp char name[128];
221 93658fb9 2020-03-18 stsp
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))
224 93658fb9 2020-03-18 stsp return -1;
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))
228 93658fb9 2020-03-18 stsp return -1;
229 93658fb9 2020-03-18 stsp } else {
230 cf875574 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
231 cf875574 2020-03-18 stsp >= sizeof(name))
232 93658fb9 2020-03-18 stsp return -1;
233 93658fb9 2020-03-18 stsp }
234 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
235 93658fb9 2020-03-18 stsp }
236 93658fb9 2020-03-18 stsp
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)
239 93658fb9 2020-03-18 stsp {
240 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
241 93658fb9 2020-03-18 stsp char *p;
242 0d0a341c 2020-03-18 stsp size_t i, n = 0;
243 93658fb9 2020-03-18 stsp
244 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
245 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
246 0d0a341c 2020-03-18 stsp
247 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
248 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
249 93658fb9 2020-03-18 stsp line++;
250 0d0a341c 2020-03-18 stsp n++;
251 0d0a341c 2020-03-18 stsp }
252 93658fb9 2020-03-18 stsp p = line;
253 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
254 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
255 93658fb9 2020-03-18 stsp line++;
256 0d0a341c 2020-03-18 stsp n++;
257 0d0a341c 2020-03-18 stsp }
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");
261 0d0a341c 2020-03-18 stsp goto done;
262 0d0a341c 2020-03-18 stsp }
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) {
265 0d0a341c 2020-03-18 stsp line++;
266 0d0a341c 2020-03-18 stsp n++;
267 0d0a341c 2020-03-18 stsp }
268 93658fb9 2020-03-18 stsp }
269 0d0a341c 2020-03-18 stsp if (i <= 2)
270 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
271 0d0a341c 2020-03-18 stsp done:
272 0d0a341c 2020-03-18 stsp if (err) {
273 0d0a341c 2020-03-18 stsp int j;
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;
277 0d0a341c 2020-03-18 stsp }
278 0d0a341c 2020-03-18 stsp return err;
279 8a29a085 2020-03-18 stsp }
280 00cd0e0a 2020-03-18 stsp
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)
284 00cd0e0a 2020-03-18 stsp {
285 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
286 00cd0e0a 2020-03-18 stsp char *tokens[3];
287 8a29a085 2020-03-18 stsp
288 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
289 00cd0e0a 2020-03-18 stsp if (err)
290 00cd0e0a 2020-03-18 stsp return err;
291 00cd0e0a 2020-03-18 stsp
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];
298 00cd0e0a 2020-03-18 stsp
299 00cd0e0a 2020-03-18 stsp return NULL;
300 00cd0e0a 2020-03-18 stsp }
301 00cd0e0a 2020-03-18 stsp
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"
305 531c3985 2020-03-18 stsp
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
309 531c3985 2020-03-18 stsp
310 531c3985 2020-03-18 stsp
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;
314 8a29a085 2020-03-18 stsp };
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 },
319 8a29a085 2020-03-18 stsp };
320 8a29a085 2020-03-18 stsp
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)
324 8a29a085 2020-03-18 stsp {
325 8a29a085 2020-03-18 stsp char *equalsign;
326 8a29a085 2020-03-18 stsp char *s;
327 8a29a085 2020-03-18 stsp
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;
332 8a29a085 2020-03-18 stsp } else {
333 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
334 8a29a085 2020-03-18 stsp return NULL;
335 8a29a085 2020-03-18 stsp }
336 8a29a085 2020-03-18 stsp
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");
344 8a29a085 2020-03-18 stsp
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;
348 93658fb9 2020-03-18 stsp }
349 93658fb9 2020-03-18 stsp
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)
352 8a29a085 2020-03-18 stsp {
353 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
354 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
355 abe0f35f 2020-03-18 stsp
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;
359 abe0f35f 2020-03-18 stsp
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;
363 abe0f35f 2020-03-18 stsp
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");
368 abe0f35f 2020-03-18 stsp
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");
372 abe0f35f 2020-03-18 stsp goto done;
373 abe0f35f 2020-03-18 stsp }
374 abe0f35f 2020-03-18 stsp
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);
377 abe0f35f 2020-03-18 stsp done:
378 abe0f35f 2020-03-18 stsp if (err) {
379 abe0f35f 2020-03-18 stsp free(name);
380 abe0f35f 2020-03-18 stsp free(target);
381 abe0f35f 2020-03-18 stsp }
382 abe0f35f 2020-03-18 stsp return err;
383 abe0f35f 2020-03-18 stsp }
384 abe0f35f 2020-03-18 stsp
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)
388 abe0f35f 2020-03-18 stsp {
389 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
390 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
391 8a29a085 2020-03-18 stsp int i;
392 8a29a085 2020-03-18 stsp
393 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
394 8a29a085 2020-03-18 stsp do {
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;
398 8a29a085 2020-03-18 stsp
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);
403 abe0f35f 2020-03-18 stsp if (err)
404 abe0f35f 2020-03-18 stsp break;
405 abe0f35f 2020-03-18 stsp continue;
406 abe0f35f 2020-03-18 stsp }
407 abe0f35f 2020-03-18 stsp
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]);
411 8a29a085 2020-03-18 stsp if (err)
412 8a29a085 2020-03-18 stsp break;
413 8a29a085 2020-03-18 stsp }
414 8a29a085 2020-03-18 stsp } while (capa);
415 8a29a085 2020-03-18 stsp
416 8a29a085 2020-03-18 stsp return err;
417 531c3985 2020-03-18 stsp }
418 531c3985 2020-03-18 stsp
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)
421 531c3985 2020-03-18 stsp {
422 531c3985 2020-03-18 stsp int i;
423 531c3985 2020-03-18 stsp
424 531c3985 2020-03-18 stsp
425 531c3985 2020-03-18 stsp if (len == 0)
426 531c3985 2020-03-18 stsp return NULL;
427 531c3985 2020-03-18 stsp
428 531c3985 2020-03-18 stsp /*
429 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
430 531c3985 2020-03-18 stsp * Server may send up to 64k.
431 531c3985 2020-03-18 stsp */
432 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
433 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
434 531c3985 2020-03-18 stsp
435 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
436 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
437 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
438 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
439 531c3985 2020-03-18 stsp continue;
440 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
441 531c3985 2020-03-18 stsp "non-printable progress message received from server");
442 531c3985 2020-03-18 stsp }
443 531c3985 2020-03-18 stsp
444 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
445 8a29a085 2020-03-18 stsp }
446 abe0f35f 2020-03-18 stsp
447 8a29a085 2020-03-18 stsp static const struct got_error *
448 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
449 531c3985 2020-03-18 stsp {
450 531c3985 2020-03-18 stsp static char msg[1024];
451 531c3985 2020-03-18 stsp int i;
452 531c3985 2020-03-18 stsp
453 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
454 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
455 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
456 531c3985 2020-03-18 stsp "non-printable error message received from server");
457 531c3985 2020-03-18 stsp msg[i] = buf[i];
458 531c3985 2020-03-18 stsp }
459 531c3985 2020-03-18 stsp msg[i] = '\0';
460 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
461 531c3985 2020-03-18 stsp }
462 531c3985 2020-03-18 stsp
463 531c3985 2020-03-18 stsp static const struct got_error *
464 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
465 33501562 2020-03-18 stsp struct got_pathlist_head *have_refs, struct imsgbuf *ibuf)
466 93658fb9 2020-03-18 stsp {
467 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
468 00cd0e0a 2020-03-18 stsp char buf[GOT_PKTMAX];
469 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
470 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
471 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
472 93658fb9 2020-03-18 stsp int i, n, req;
473 93658fb9 2020-03-18 stsp off_t packsz;
474 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
475 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
476 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
477 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
478 531c3985 2020-03-18 stsp int have_sidebands = 0;
479 93658fb9 2020-03-18 stsp
480 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
481 abe0f35f 2020-03-18 stsp
482 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
483 9ff10419 2020-03-18 stsp if (have == NULL)
484 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
485 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
486 9ff10419 2020-03-18 stsp if (want == NULL) {
487 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
488 9ff10419 2020-03-18 stsp goto done;
489 9ff10419 2020-03-18 stsp }
490 9ff10419 2020-03-18 stsp if (chattygit)
491 9ff10419 2020-03-18 stsp fprintf(stderr, "starting fetch\n");
492 9ff10419 2020-03-18 stsp while (1) {
493 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
494 fe53745c 2020-03-18 stsp if (err)
495 9ff10419 2020-03-18 stsp goto done;
496 9ff10419 2020-03-18 stsp if (n == 0)
497 93658fb9 2020-03-18 stsp break;
498 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
499 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
500 9ff10419 2020-03-18 stsp goto done;
501 9ff10419 2020-03-18 stsp }
502 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
503 00cd0e0a 2020-03-18 stsp buf, n);
504 0d0a341c 2020-03-18 stsp if (err)
505 9ff10419 2020-03-18 stsp goto done;
506 00cd0e0a 2020-03-18 stsp if (chattygit && server_capabilities[0] != '\0')
507 00cd0e0a 2020-03-18 stsp fprintf(stderr, "server capabilities: %s\n",
508 00cd0e0a 2020-03-18 stsp server_capabilities);
509 8a29a085 2020-03-18 stsp if (is_firstpkt) {
510 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
511 00cd0e0a 2020-03-18 stsp server_capabilities);
512 8a29a085 2020-03-18 stsp if (err)
513 8a29a085 2020-03-18 stsp goto done;
514 8a29a085 2020-03-18 stsp if (chattygit && my_capabilities)
515 8a29a085 2020-03-18 stsp fprintf(stderr, "my matched capabilities: %s\n",
516 8a29a085 2020-03-18 stsp my_capabilities);
517 abe0f35f 2020-03-18 stsp err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
518 abe0f35f 2020-03-18 stsp if (err)
519 abe0f35f 2020-03-18 stsp goto done;
520 8a29a085 2020-03-18 stsp }
521 8a29a085 2020-03-18 stsp is_firstpkt = 0;
522 00cd0e0a 2020-03-18 stsp if (strstr(refname, "^{}"))
523 93658fb9 2020-03-18 stsp continue;
524 00cd0e0a 2020-03-18 stsp if (fetchbranch && !match_branch(refname, fetchbranch))
525 93658fb9 2020-03-18 stsp continue;
526 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
527 93658fb9 2020-03-18 stsp refsz *= 2;
528 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
529 9ff10419 2020-03-18 stsp if (have == NULL) {
530 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
531 9ff10419 2020-03-18 stsp goto done;
532 9ff10419 2020-03-18 stsp }
533 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
534 9ff10419 2020-03-18 stsp if (want == NULL) {
535 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
536 9ff10419 2020-03-18 stsp goto done;
537 9ff10419 2020-03-18 stsp }
538 93658fb9 2020-03-18 stsp }
539 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
540 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
541 9ff10419 2020-03-18 stsp goto done;
542 9ff10419 2020-03-18 stsp }
543 9ff10419 2020-03-18 stsp
544 00cd0e0a 2020-03-18 stsp err = match_remote_ref(have_refs, &have[nref], id_str, refname);
545 33501562 2020-03-18 stsp if (err)
546 33501562 2020-03-18 stsp goto done;
547 33501562 2020-03-18 stsp
548 ea7396b9 2020-03-18 stsp err = got_privsep_send_fetch_ref(ibuf, &want[nref],
549 00cd0e0a 2020-03-18 stsp refname);
550 8f2d01a6 2020-03-18 stsp if (err)
551 8f2d01a6 2020-03-18 stsp goto done;
552 93658fb9 2020-03-18 stsp if (chattygit)
553 00cd0e0a 2020-03-18 stsp fprintf(stderr, "remote %s\n", refname);
554 93658fb9 2020-03-18 stsp nref++;
555 93658fb9 2020-03-18 stsp }
556 93658fb9 2020-03-18 stsp
557 93658fb9 2020-03-18 stsp req = 0;
558 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
559 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
560 93658fb9 2020-03-18 stsp continue;
561 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
562 13ce8c93 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "want %s%s%s\n", hashstr,
563 13ce8c93 2020-03-18 stsp i == 0 && my_capabilities ? " " : "",
564 8a29a085 2020-03-18 stsp i == 0 && my_capabilities ? my_capabilities : "");
565 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
566 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
567 9ff10419 2020-03-18 stsp goto done;
568 9ff10419 2020-03-18 stsp }
569 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
570 344e4747 2020-03-18 stsp if (err)
571 9ff10419 2020-03-18 stsp goto done;
572 93658fb9 2020-03-18 stsp req = 1;
573 93658fb9 2020-03-18 stsp }
574 38c670f1 2020-03-18 stsp err = flushpkt(fd);
575 38c670f1 2020-03-18 stsp if (err)
576 38c670f1 2020-03-18 stsp goto done;
577 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
578 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
579 93658fb9 2020-03-18 stsp continue;
580 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
581 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
582 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
583 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
584 9ff10419 2020-03-18 stsp goto done;
585 9ff10419 2020-03-18 stsp }
586 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n + 1);
587 344e4747 2020-03-18 stsp if (err)
588 9ff10419 2020-03-18 stsp goto done;
589 93658fb9 2020-03-18 stsp }
590 cf875574 2020-03-18 stsp if (!req) {
591 3b9fb585 2020-03-18 stsp if (chattygit)
592 3b9fb585 2020-03-18 stsp fprintf(stderr, "up to date\n");
593 38c670f1 2020-03-18 stsp err = flushpkt(fd);
594 38c670f1 2020-03-18 stsp if (err)
595 38c670f1 2020-03-18 stsp goto done;
596 93658fb9 2020-03-18 stsp }
597 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
598 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
599 344e4747 2020-03-18 stsp if (err)
600 9ff10419 2020-03-18 stsp goto done;
601 9ff10419 2020-03-18 stsp if (!req)
602 93658fb9 2020-03-18 stsp return 0;
603 93658fb9 2020-03-18 stsp
604 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
605 fe53745c 2020-03-18 stsp if (err)
606 9ff10419 2020-03-18 stsp goto done;
607 04c53c18 2020-03-18 stsp /*
608 04c53c18 2020-03-18 stsp * For now, we only support a full clone, in which case the server
609 04c53c18 2020-03-18 stsp * will now send a "NAK" (meaning no common objects were found).
610 04c53c18 2020-03-18 stsp */
611 04c53c18 2020-03-18 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
612 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
613 531c3985 2020-03-18 stsp "unexpected message from server");
614 04c53c18 2020-03-18 stsp goto done;
615 04c53c18 2020-03-18 stsp }
616 93658fb9 2020-03-18 stsp
617 8f2d01a6 2020-03-18 stsp if (chattygit)
618 8f2d01a6 2020-03-18 stsp fprintf(stderr, "fetching...\n");
619 531c3985 2020-03-18 stsp
620 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
621 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
622 531c3985 2020-03-18 stsp have_sidebands = 1;
623 531c3985 2020-03-18 stsp
624 93658fb9 2020-03-18 stsp packsz = 0;
625 9ff10419 2020-03-18 stsp while (1) {
626 531c3985 2020-03-18 stsp ssize_t r = 0, w;
627 531c3985 2020-03-18 stsp int datalen = -1;
628 531c3985 2020-03-18 stsp
629 531c3985 2020-03-18 stsp if (have_sidebands) {
630 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
631 531c3985 2020-03-18 stsp if (err)
632 531c3985 2020-03-18 stsp goto done;
633 531c3985 2020-03-18 stsp if (datalen <= 0)
634 531c3985 2020-03-18 stsp break;
635 531c3985 2020-03-18 stsp
636 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
637 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
638 531c3985 2020-03-18 stsp if (r == -1) {
639 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
640 531c3985 2020-03-18 stsp goto done;
641 531c3985 2020-03-18 stsp }
642 531c3985 2020-03-18 stsp if (r != 1) {
643 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
644 531c3985 2020-03-18 stsp "short packet");
645 531c3985 2020-03-18 stsp goto done;
646 531c3985 2020-03-18 stsp }
647 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
648 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
649 531c3985 2020-03-18 stsp "bad packet length");
650 531c3985 2020-03-18 stsp goto done;
651 531c3985 2020-03-18 stsp }
652 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
653 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
654 531c3985 2020-03-18 stsp /* Read packfile data. */
655 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
656 531c3985 2020-03-18 stsp if (err)
657 531c3985 2020-03-18 stsp goto done;
658 531c3985 2020-03-18 stsp if (r != datalen) {
659 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
660 531c3985 2020-03-18 stsp "packet too short");
661 531c3985 2020-03-18 stsp goto done;
662 531c3985 2020-03-18 stsp }
663 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
664 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
665 531c3985 2020-03-18 stsp if (err)
666 531c3985 2020-03-18 stsp goto done;
667 531c3985 2020-03-18 stsp if (r != datalen) {
668 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
669 531c3985 2020-03-18 stsp "packet too short");
670 531c3985 2020-03-18 stsp goto done;
671 531c3985 2020-03-18 stsp }
672 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
673 531c3985 2020-03-18 stsp if (err)
674 531c3985 2020-03-18 stsp goto done;
675 531c3985 2020-03-18 stsp continue;
676 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
677 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
678 531c3985 2020-03-18 stsp if (err)
679 531c3985 2020-03-18 stsp goto done;
680 531c3985 2020-03-18 stsp if (r != datalen) {
681 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
682 531c3985 2020-03-18 stsp "packet too short");
683 531c3985 2020-03-18 stsp goto done;
684 531c3985 2020-03-18 stsp }
685 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
686 531c3985 2020-03-18 stsp goto done;
687 531c3985 2020-03-18 stsp } else {
688 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
689 531c3985 2020-03-18 stsp "unknown side-band received from server");
690 531c3985 2020-03-18 stsp goto done;
691 531c3985 2020-03-18 stsp }
692 531c3985 2020-03-18 stsp } else {
693 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
694 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
695 531c3985 2020-03-18 stsp if (err)
696 531c3985 2020-03-18 stsp goto done;
697 531c3985 2020-03-18 stsp if (r <= 0)
698 531c3985 2020-03-18 stsp break;
699 531c3985 2020-03-18 stsp }
700 531c3985 2020-03-18 stsp
701 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
702 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
703 9ff10419 2020-03-18 stsp if (w == -1) {
704 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
705 9ff10419 2020-03-18 stsp goto done;
706 9ff10419 2020-03-18 stsp }
707 fe53745c 2020-03-18 stsp if (w != r) {
708 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
709 9ff10419 2020-03-18 stsp goto done;
710 9ff10419 2020-03-18 stsp }
711 531c3985 2020-03-18 stsp packsz += w;
712 d2cdc636 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf, packsz);
713 d2cdc636 2020-03-18 stsp if (err)
714 d2cdc636 2020-03-18 stsp goto done;
715 93658fb9 2020-03-18 stsp }
716 9ff10419 2020-03-18 stsp done:
717 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
718 abe0f35f 2020-03-18 stsp free((void *)pe->path);
719 abe0f35f 2020-03-18 stsp free(pe->data);
720 abe0f35f 2020-03-18 stsp }
721 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
722 9ff10419 2020-03-18 stsp free(have);
723 9ff10419 2020-03-18 stsp free(want);
724 00cd0e0a 2020-03-18 stsp free(id_str);
725 00cd0e0a 2020-03-18 stsp free(refname);
726 00cd0e0a 2020-03-18 stsp free(server_capabilities);
727 8f2d01a6 2020-03-18 stsp return err;
728 93658fb9 2020-03-18 stsp }
729 93658fb9 2020-03-18 stsp
730 93658fb9 2020-03-18 stsp
731 93658fb9 2020-03-18 stsp int
732 93658fb9 2020-03-18 stsp main(int argc, char **argv)
733 93658fb9 2020-03-18 stsp {
734 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
735 9ff10419 2020-03-18 stsp int fetchfd, packfd = -1;
736 93658fb9 2020-03-18 stsp struct got_object_id packid;
737 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
738 93658fb9 2020-03-18 stsp struct imsg imsg;
739 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
740 33501562 2020-03-18 stsp struct got_imsg_fetch_have_refs *fetch_have_refs = NULL;
741 33501562 2020-03-18 stsp size_t datalen;
742 33501562 2020-03-18 stsp
743 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
744 93658fb9 2020-03-18 stsp
745 cf875574 2020-03-18 stsp if (getenv("GOT_DEBUG") != NULL) {
746 93658fb9 2020-03-18 stsp fprintf(stderr, "fetch-pack being chatty!\n");
747 93658fb9 2020-03-18 stsp chattygit = 1;
748 93658fb9 2020-03-18 stsp }
749 93658fb9 2020-03-18 stsp
750 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
751 ffb5f621 2020-03-18 stsp #ifndef PROFILE
752 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
753 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
754 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
755 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
756 ffb5f621 2020-03-18 stsp return 1;
757 ffb5f621 2020-03-18 stsp }
758 ffb5f621 2020-03-18 stsp #endif
759 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
760 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
761 93658fb9 2020-03-18 stsp err = NULL;
762 93658fb9 2020-03-18 stsp goto done;
763 93658fb9 2020-03-18 stsp }
764 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
765 93658fb9 2020-03-18 stsp goto done;
766 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
767 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
768 93658fb9 2020-03-18 stsp goto done;
769 93658fb9 2020-03-18 stsp }
770 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
771 33501562 2020-03-18 stsp if (datalen < sizeof(struct got_imsg_fetch_have_refs)) {
772 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
773 93658fb9 2020-03-18 stsp goto done;
774 93658fb9 2020-03-18 stsp }
775 33501562 2020-03-18 stsp fetch_have_refs = (struct got_imsg_fetch_have_refs *)imsg.data;
776 33501562 2020-03-18 stsp if (datalen != sizeof(struct got_imsg_fetch_have_refs) +
777 33501562 2020-03-18 stsp sizeof(struct got_imsg_fetch_have_ref) *
778 33501562 2020-03-18 stsp fetch_have_refs->n_have_refs) {
779 33501562 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
780 33501562 2020-03-18 stsp goto done;
781 33501562 2020-03-18 stsp }
782 33501562 2020-03-18 stsp if (fetch_have_refs->n_have_refs != 0) {
783 33501562 2020-03-18 stsp /* TODO: Incremental fetch support */
784 33501562 2020-03-18 stsp err = got_error(GOT_ERR_NOT_IMPL);
785 33501562 2020-03-18 stsp goto done;
786 33501562 2020-03-18 stsp }
787 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
788 93658fb9 2020-03-18 stsp
789 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
790 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
791 93658fb9 2020-03-18 stsp err = NULL;
792 93658fb9 2020-03-18 stsp goto done;
793 93658fb9 2020-03-18 stsp }
794 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
795 93658fb9 2020-03-18 stsp goto done;
796 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
797 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
798 93658fb9 2020-03-18 stsp goto done;
799 93658fb9 2020-03-18 stsp }
800 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
801 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
802 93658fb9 2020-03-18 stsp goto done;
803 93658fb9 2020-03-18 stsp }
804 93658fb9 2020-03-18 stsp packfd = imsg.fd;
805 93658fb9 2020-03-18 stsp
806 33501562 2020-03-18 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
807 93658fb9 2020-03-18 stsp done:
808 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
809 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
810 9ff10419 2020-03-18 stsp if (err != NULL)
811 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
812 93658fb9 2020-03-18 stsp else
813 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
814 cf875574 2020-03-18 stsp if (err != NULL) {
815 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
816 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
817 93658fb9 2020-03-18 stsp }
818 93658fb9 2020-03-18 stsp
819 93658fb9 2020-03-18 stsp exit(0);
820 93658fb9 2020-03-18 stsp }