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 8a29a085 2020-03-18 stsp #include "got_version.h"
41 93658fb9 2020-03-18 stsp
42 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
43 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
44 93658fb9 2020-03-18 stsp #include "got_lib_inflate.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 93658fb9 2020-03-18 stsp
49 8a29a085 2020-03-18 stsp #ifndef nitems
50 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 8a29a085 2020-03-18 stsp #endif
52 8a29a085 2020-03-18 stsp
53 93658fb9 2020-03-18 stsp #define GOT_PKTMAX 65536
54 93658fb9 2020-03-18 stsp
55 93658fb9 2020-03-18 stsp struct got_object *indexed;
56 93658fb9 2020-03-18 stsp static int chattygit;
57 93658fb9 2020-03-18 stsp static char *fetchbranch;
58 93658fb9 2020-03-18 stsp static char *upstream = "origin";
59 93658fb9 2020-03-18 stsp static struct got_object_id zhash = {.sha1={0}};
60 93658fb9 2020-03-18 stsp
61 93658fb9 2020-03-18 stsp static char*
62 93658fb9 2020-03-18 stsp strip(char *ref)
63 93658fb9 2020-03-18 stsp {
64 93658fb9 2020-03-18 stsp return ref;
65 93658fb9 2020-03-18 stsp }
66 93658fb9 2020-03-18 stsp
67 93658fb9 2020-03-18 stsp int
68 93658fb9 2020-03-18 stsp readn(int fd, void *buf, size_t n)
69 93658fb9 2020-03-18 stsp {
70 93658fb9 2020-03-18 stsp ssize_t r, off;
71 93658fb9 2020-03-18 stsp
72 93658fb9 2020-03-18 stsp off = 0;
73 93658fb9 2020-03-18 stsp while (off != n) {
74 93658fb9 2020-03-18 stsp r = read(fd, buf + off, n - off);
75 93658fb9 2020-03-18 stsp if (r < 0)
76 93658fb9 2020-03-18 stsp return -1;
77 93658fb9 2020-03-18 stsp if (r == 0)
78 93658fb9 2020-03-18 stsp return off;
79 93658fb9 2020-03-18 stsp off += r;
80 93658fb9 2020-03-18 stsp }
81 93658fb9 2020-03-18 stsp return off;
82 93658fb9 2020-03-18 stsp }
83 93658fb9 2020-03-18 stsp
84 93658fb9 2020-03-18 stsp int
85 93658fb9 2020-03-18 stsp flushpkt(int fd)
86 93658fb9 2020-03-18 stsp {
87 93658fb9 2020-03-18 stsp if(chattygit)
88 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: 0000\n");
89 93658fb9 2020-03-18 stsp return write(fd, "0000", 4);
90 93658fb9 2020-03-18 stsp }
91 93658fb9 2020-03-18 stsp
92 93658fb9 2020-03-18 stsp
93 93658fb9 2020-03-18 stsp int
94 93658fb9 2020-03-18 stsp readpkt(int fd, char *buf, int nbuf)
95 93658fb9 2020-03-18 stsp {
96 93658fb9 2020-03-18 stsp char len[5];
97 93658fb9 2020-03-18 stsp char *e;
98 93658fb9 2020-03-18 stsp int n, r;
99 93658fb9 2020-03-18 stsp
100 93658fb9 2020-03-18 stsp if(readn(fd, len, 4) == -1){
101 93658fb9 2020-03-18 stsp return -1;
102 93658fb9 2020-03-18 stsp }
103 93658fb9 2020-03-18 stsp len[4] = 0;
104 93658fb9 2020-03-18 stsp n = strtol(len, &e, 16);
105 93658fb9 2020-03-18 stsp if(n == 0){
106 93658fb9 2020-03-18 stsp if(chattygit)
107 93658fb9 2020-03-18 stsp fprintf(stderr, "readpkt: 0000\n");
108 93658fb9 2020-03-18 stsp return 0;
109 93658fb9 2020-03-18 stsp }
110 93658fb9 2020-03-18 stsp if(e != len + 4 || n <= 4)
111 93658fb9 2020-03-18 stsp err(1, "invalid packet line length");
112 93658fb9 2020-03-18 stsp n -= 4;
113 93658fb9 2020-03-18 stsp if(n >= nbuf)
114 93658fb9 2020-03-18 stsp err(1, "buffer too small");
115 93658fb9 2020-03-18 stsp if((r = readn(fd, buf, n)) != n)
116 93658fb9 2020-03-18 stsp return -1;
117 93658fb9 2020-03-18 stsp buf[n] = 0;
118 93658fb9 2020-03-18 stsp if(chattygit)
119 93658fb9 2020-03-18 stsp fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
120 93658fb9 2020-03-18 stsp return n;
121 93658fb9 2020-03-18 stsp }
122 93658fb9 2020-03-18 stsp
123 93658fb9 2020-03-18 stsp int
124 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
125 93658fb9 2020-03-18 stsp {
126 93658fb9 2020-03-18 stsp char len[5];
127 93658fb9 2020-03-18 stsp int i;
128 93658fb9 2020-03-18 stsp
129 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
130 93658fb9 2020-03-18 stsp return -1;
131 93658fb9 2020-03-18 stsp if(write(fd, len, 4) != 4)
132 93658fb9 2020-03-18 stsp return -1;
133 93658fb9 2020-03-18 stsp if(write(fd, buf, nbuf) != nbuf)
134 93658fb9 2020-03-18 stsp return -1;
135 93658fb9 2020-03-18 stsp if(chattygit){
136 93658fb9 2020-03-18 stsp fprintf(stderr, "writepkt: %s:\t", len);
137 93658fb9 2020-03-18 stsp fwrite(buf, 1, nbuf, stderr);
138 93658fb9 2020-03-18 stsp for(i = 0; i < nbuf; i++){
139 93658fb9 2020-03-18 stsp if(isprint(buf[i]))
140 93658fb9 2020-03-18 stsp fputc(buf[i], stderr);
141 93658fb9 2020-03-18 stsp }
142 93658fb9 2020-03-18 stsp }
143 93658fb9 2020-03-18 stsp return 0;
144 93658fb9 2020-03-18 stsp }
145 93658fb9 2020-03-18 stsp
146 93658fb9 2020-03-18 stsp
147 93658fb9 2020-03-18 stsp int
148 93658fb9 2020-03-18 stsp got_resolve_remote_ref(struct got_object_id *id, char *ref)
149 93658fb9 2020-03-18 stsp {
150 93658fb9 2020-03-18 stsp char buf[128], *s;
151 93658fb9 2020-03-18 stsp int r, f;
152 93658fb9 2020-03-18 stsp
153 93658fb9 2020-03-18 stsp ref = strip(ref);
154 93658fb9 2020-03-18 stsp if(!got_parse_sha1_digest(id->sha1, ref))
155 93658fb9 2020-03-18 stsp return 0;
156 93658fb9 2020-03-18 stsp
157 93658fb9 2020-03-18 stsp /* Slightly special handling: translate remote refs to local ones. */
158 93658fb9 2020-03-18 stsp if (strcmp(ref, "HEAD") == 0) {
159 93658fb9 2020-03-18 stsp if(snprintf(buf, sizeof(buf), ".git/HEAD") >= sizeof(buf))
160 93658fb9 2020-03-18 stsp return -1;
161 93658fb9 2020-03-18 stsp } else if(strstr(ref, "refs/heads") == ref) {
162 93658fb9 2020-03-18 stsp ref += strlen("refs/heads");
163 93658fb9 2020-03-18 stsp if(snprintf(buf, sizeof(buf),
164 93658fb9 2020-03-18 stsp ".git/refs/remotes/%s/%s", upstream, ref) >= sizeof(buf))
165 93658fb9 2020-03-18 stsp return -1;
166 93658fb9 2020-03-18 stsp } else if(strstr(ref, "refs/tags") == ref) {
167 93658fb9 2020-03-18 stsp ref += strlen("refs/tags");
168 93658fb9 2020-03-18 stsp if(snprintf(buf, sizeof(buf),
169 93658fb9 2020-03-18 stsp ".git/refs/tags/%s/%s", upstream, ref) >= sizeof(buf))
170 93658fb9 2020-03-18 stsp return -1;
171 93658fb9 2020-03-18 stsp } else {
172 93658fb9 2020-03-18 stsp return -1;
173 93658fb9 2020-03-18 stsp }
174 93658fb9 2020-03-18 stsp
175 93658fb9 2020-03-18 stsp r = -1;
176 93658fb9 2020-03-18 stsp s = strip(buf);
177 93658fb9 2020-03-18 stsp if((f = open(s, O_RDONLY)) == -1)
178 93658fb9 2020-03-18 stsp goto err;
179 93658fb9 2020-03-18 stsp if(readn(f, buf, sizeof(buf)) < 40)
180 93658fb9 2020-03-18 stsp goto err;
181 93658fb9 2020-03-18 stsp if(!got_parse_sha1_digest(id->sha1, buf))
182 93658fb9 2020-03-18 stsp goto err;
183 93658fb9 2020-03-18 stsp err:
184 93658fb9 2020-03-18 stsp close(f);
185 93658fb9 2020-03-18 stsp if(r == -1 && strstr(buf, "ref:") == buf)
186 93658fb9 2020-03-18 stsp return got_resolve_remote_ref(id, buf + strlen("ref:"));
187 93658fb9 2020-03-18 stsp return r;
188 93658fb9 2020-03-18 stsp }
189 93658fb9 2020-03-18 stsp
190 93658fb9 2020-03-18 stsp static int
191 93658fb9 2020-03-18 stsp got_check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
192 93658fb9 2020-03-18 stsp {
193 93658fb9 2020-03-18 stsp SHA1_CTX ctx;
194 93658fb9 2020-03-18 stsp uint8_t hexpect[SHA1_DIGEST_LENGTH];
195 93658fb9 2020-03-18 stsp char s1[SHA1_DIGEST_STRING_LENGTH + 1];
196 93658fb9 2020-03-18 stsp char s2[SHA1_DIGEST_STRING_LENGTH + 1];
197 93658fb9 2020-03-18 stsp uint8_t buf[32*1024];
198 93658fb9 2020-03-18 stsp ssize_t n, r, nr;
199 93658fb9 2020-03-18 stsp
200 93658fb9 2020-03-18 stsp if(sz < 28)
201 93658fb9 2020-03-18 stsp return -1;
202 93658fb9 2020-03-18 stsp
203 93658fb9 2020-03-18 stsp n = 0;
204 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
205 93658fb9 2020-03-18 stsp while(n < sz - 20){
206 93658fb9 2020-03-18 stsp nr = sizeof(buf);
207 93658fb9 2020-03-18 stsp if(sz - n - 20 < sizeof(buf))
208 93658fb9 2020-03-18 stsp nr = sz - n - 20;
209 93658fb9 2020-03-18 stsp r = readn(fd, buf, nr);
210 93658fb9 2020-03-18 stsp if(r != nr)
211 93658fb9 2020-03-18 stsp return -1;
212 93658fb9 2020-03-18 stsp SHA1Update(&ctx, buf, nr);
213 93658fb9 2020-03-18 stsp n += r;
214 93658fb9 2020-03-18 stsp }
215 93658fb9 2020-03-18 stsp SHA1Final(hcomp, &ctx);
216 93658fb9 2020-03-18 stsp
217 93658fb9 2020-03-18 stsp if(readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
218 93658fb9 2020-03-18 stsp errx(1, "truncated packfile");
219 93658fb9 2020-03-18 stsp if(memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0){
220 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
221 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(hexpect, s2, sizeof(s2));
222 93658fb9 2020-03-18 stsp printf("hash mismatch %s != %s\n", s1, s2);
223 93658fb9 2020-03-18 stsp return -1;
224 93658fb9 2020-03-18 stsp }
225 93658fb9 2020-03-18 stsp return 0;
226 93658fb9 2020-03-18 stsp }
227 93658fb9 2020-03-18 stsp
228 93658fb9 2020-03-18 stsp int
229 93658fb9 2020-03-18 stsp got_has_object(struct got_object_id *obj)
230 93658fb9 2020-03-18 stsp {
231 93658fb9 2020-03-18 stsp return 0;
232 93658fb9 2020-03-18 stsp }
233 93658fb9 2020-03-18 stsp
234 93658fb9 2020-03-18 stsp /*static */int
235 93658fb9 2020-03-18 stsp got_make_pack_dir(char *path)
236 93658fb9 2020-03-18 stsp {
237 93658fb9 2020-03-18 stsp char s[128];
238 93658fb9 2020-03-18 stsp char *p;
239 93658fb9 2020-03-18 stsp
240 93658fb9 2020-03-18 stsp if(snprintf(s, sizeof(s), "%s", path) >= sizeof(s))
241 93658fb9 2020-03-18 stsp return -1;
242 93658fb9 2020-03-18 stsp for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
243 93658fb9 2020-03-18 stsp *p = 0;
244 93658fb9 2020-03-18 stsp if (mkdir(s, 0755) == -1)
245 93658fb9 2020-03-18 stsp if(errno != EEXIST)
246 93658fb9 2020-03-18 stsp return -1;
247 93658fb9 2020-03-18 stsp *p = '/';
248 93658fb9 2020-03-18 stsp }
249 93658fb9 2020-03-18 stsp return 0;
250 93658fb9 2020-03-18 stsp }
251 93658fb9 2020-03-18 stsp
252 93658fb9 2020-03-18 stsp static int
253 93658fb9 2020-03-18 stsp got_match_branch(char *br, char *pat)
254 93658fb9 2020-03-18 stsp {
255 93658fb9 2020-03-18 stsp char name[128];
256 93658fb9 2020-03-18 stsp
257 93658fb9 2020-03-18 stsp if(strstr(pat, "refs/heads") == pat) {
258 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
259 93658fb9 2020-03-18 stsp return -1;
260 93658fb9 2020-03-18 stsp } else if(strstr(pat, "heads")) {
261 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/%s", pat) >= sizeof(name))
262 93658fb9 2020-03-18 stsp return -1;
263 93658fb9 2020-03-18 stsp } else {
264 93658fb9 2020-03-18 stsp if (snprintf(name, sizeof(name), "refs/heads/%s", pat) >= sizeof(name))
265 93658fb9 2020-03-18 stsp return -1;
266 93658fb9 2020-03-18 stsp }
267 93658fb9 2020-03-18 stsp return strcmp(br, name) == 0;
268 93658fb9 2020-03-18 stsp }
269 93658fb9 2020-03-18 stsp
270 0d0a341c 2020-03-18 stsp static const struct got_error *
271 0d0a341c 2020-03-18 stsp got_tokenize_refline(char **tokens, char *line, int len)
272 93658fb9 2020-03-18 stsp {
273 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
274 93658fb9 2020-03-18 stsp char *p;
275 0d0a341c 2020-03-18 stsp size_t i, n = 0;
276 0d0a341c 2020-03-18 stsp const int maxtokens = 3;
277 93658fb9 2020-03-18 stsp
278 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
279 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
280 0d0a341c 2020-03-18 stsp
281 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
282 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
283 93658fb9 2020-03-18 stsp line++;
284 0d0a341c 2020-03-18 stsp n++;
285 0d0a341c 2020-03-18 stsp }
286 93658fb9 2020-03-18 stsp p = line;
287 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
288 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
289 93658fb9 2020-03-18 stsp line++;
290 0d0a341c 2020-03-18 stsp n++;
291 0d0a341c 2020-03-18 stsp }
292 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
293 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
294 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
295 0d0a341c 2020-03-18 stsp goto done;
296 0d0a341c 2020-03-18 stsp }
297 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
298 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
299 0d0a341c 2020-03-18 stsp line++;
300 0d0a341c 2020-03-18 stsp n++;
301 0d0a341c 2020-03-18 stsp }
302 93658fb9 2020-03-18 stsp }
303 0d0a341c 2020-03-18 stsp if (i <= 2)
304 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
305 0d0a341c 2020-03-18 stsp done:
306 0d0a341c 2020-03-18 stsp if (err) {
307 0d0a341c 2020-03-18 stsp int j;
308 0d0a341c 2020-03-18 stsp for (j = 0; j < i; j++)
309 0d0a341c 2020-03-18 stsp free(tokens[j]);
310 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
311 0d0a341c 2020-03-18 stsp }
312 0d0a341c 2020-03-18 stsp return err;
313 8a29a085 2020-03-18 stsp }
314 8a29a085 2020-03-18 stsp
315 8a29a085 2020-03-18 stsp struct got_capability {
316 8a29a085 2020-03-18 stsp const char *key;
317 8a29a085 2020-03-18 stsp const char *value;
318 8a29a085 2020-03-18 stsp };
319 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
320 8a29a085 2020-03-18 stsp #if 0 /* got-index-pack is not ready for this */
321 8a29a085 2020-03-18 stsp { "ofs-delta", NULL },
322 8a29a085 2020-03-18 stsp #endif
323 8a29a085 2020-03-18 stsp { "agent", "got/" GOT_VERSION_STR },
324 8a29a085 2020-03-18 stsp };
325 8a29a085 2020-03-18 stsp
326 8a29a085 2020-03-18 stsp static const struct got_error *
327 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
328 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
329 8a29a085 2020-03-18 stsp {
330 8a29a085 2020-03-18 stsp char *equalsign;
331 8a29a085 2020-03-18 stsp char *s;
332 8a29a085 2020-03-18 stsp
333 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
334 8a29a085 2020-03-18 stsp if (equalsign) {
335 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
336 8a29a085 2020-03-18 stsp return NULL;
337 8a29a085 2020-03-18 stsp } else {
338 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
339 8a29a085 2020-03-18 stsp return NULL;
340 8a29a085 2020-03-18 stsp }
341 8a29a085 2020-03-18 stsp
342 8a29a085 2020-03-18 stsp if (asprintf(&s, "%s%s%s%s%s",
343 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
344 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? " " : "",
345 8a29a085 2020-03-18 stsp mycapa->key,
346 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
347 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
348 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
349 8a29a085 2020-03-18 stsp
350 8a29a085 2020-03-18 stsp free(*my_capabilities);
351 8a29a085 2020-03-18 stsp *my_capabilities = s;
352 8a29a085 2020-03-18 stsp return NULL;
353 93658fb9 2020-03-18 stsp }
354 93658fb9 2020-03-18 stsp
355 9ff10419 2020-03-18 stsp static const struct got_error *
356 8a29a085 2020-03-18 stsp match_capabilities(char **my_capabilities, char *server_capabilities)
357 8a29a085 2020-03-18 stsp {
358 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
359 8a29a085 2020-03-18 stsp char *capa;
360 8a29a085 2020-03-18 stsp int i;
361 8a29a085 2020-03-18 stsp
362 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
363 8a29a085 2020-03-18 stsp do {
364 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
365 8a29a085 2020-03-18 stsp if (capa == NULL)
366 8a29a085 2020-03-18 stsp return NULL;
367 8a29a085 2020-03-18 stsp
368 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
369 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
370 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
371 8a29a085 2020-03-18 stsp if (err)
372 8a29a085 2020-03-18 stsp break;
373 8a29a085 2020-03-18 stsp }
374 8a29a085 2020-03-18 stsp } while (capa);
375 8a29a085 2020-03-18 stsp
376 8a29a085 2020-03-18 stsp return err;
377 8a29a085 2020-03-18 stsp }
378 8a29a085 2020-03-18 stsp
379 8a29a085 2020-03-18 stsp static const struct got_error *
380 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
381 8f2d01a6 2020-03-18 stsp struct imsgbuf *ibuf)
382 93658fb9 2020-03-18 stsp {
383 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
384 ccbf9d19 2020-03-18 stsp char buf[GOT_PKTMAX], *sp[3];
385 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
386 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
387 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
388 93658fb9 2020-03-18 stsp int i, n, req;
389 93658fb9 2020-03-18 stsp off_t packsz;
390 8a29a085 2020-03-18 stsp char *my_capabilities = NULL;
391 93658fb9 2020-03-18 stsp
392 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
393 9ff10419 2020-03-18 stsp if (have == NULL)
394 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
395 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
396 9ff10419 2020-03-18 stsp if (want == NULL) {
397 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
398 9ff10419 2020-03-18 stsp goto done;
399 9ff10419 2020-03-18 stsp }
400 9ff10419 2020-03-18 stsp if (chattygit)
401 9ff10419 2020-03-18 stsp fprintf(stderr, "starting fetch\n");
402 9ff10419 2020-03-18 stsp while (1) {
403 93658fb9 2020-03-18 stsp n = readpkt(fd, buf, sizeof(buf));
404 9ff10419 2020-03-18 stsp if (n == -1){
405 9b45e112 2020-03-18 stsp err = got_error_from_errno("readpkt");
406 9ff10419 2020-03-18 stsp goto done;
407 9ff10419 2020-03-18 stsp }
408 9ff10419 2020-03-18 stsp if (n == 0)
409 93658fb9 2020-03-18 stsp break;
410 9ff10419 2020-03-18 stsp if (strncmp(buf, "ERR ", 4) == 0) {
411 9ff10419 2020-03-18 stsp static char msg[1024];
412 9ff10419 2020-03-18 stsp strlcpy(msg, buf + 4, sizeof(msg));
413 9ff10419 2020-03-18 stsp err = got_error_msg(GOT_ERR_FETCH_FAILED, msg);
414 9ff10419 2020-03-18 stsp goto done;
415 9ff10419 2020-03-18 stsp }
416 0d0a341c 2020-03-18 stsp err = got_tokenize_refline(sp, buf, n);
417 0d0a341c 2020-03-18 stsp if (err)
418 9ff10419 2020-03-18 stsp goto done;
419 0d0a341c 2020-03-18 stsp if (chattygit && sp[2][0] != '\0')
420 8a29a085 2020-03-18 stsp fprintf(stderr, "server capabilities: %s\n", sp[2]);
421 8a29a085 2020-03-18 stsp if (is_firstpkt) {
422 8a29a085 2020-03-18 stsp err = match_capabilities(&my_capabilities, sp[2]);
423 8a29a085 2020-03-18 stsp if (err)
424 8a29a085 2020-03-18 stsp goto done;
425 8a29a085 2020-03-18 stsp if (chattygit && my_capabilities)
426 8a29a085 2020-03-18 stsp fprintf(stderr, "my matched capabilities: %s\n",
427 8a29a085 2020-03-18 stsp my_capabilities);
428 8a29a085 2020-03-18 stsp }
429 8a29a085 2020-03-18 stsp is_firstpkt = 0;
430 9ff10419 2020-03-18 stsp if (strstr(sp[1], "^{}"))
431 93658fb9 2020-03-18 stsp continue;
432 9ff10419 2020-03-18 stsp if (fetchbranch && !got_match_branch(sp[1], fetchbranch))
433 93658fb9 2020-03-18 stsp continue;
434 9ff10419 2020-03-18 stsp if (refsz == nref + 1){
435 93658fb9 2020-03-18 stsp refsz *= 2;
436 93658fb9 2020-03-18 stsp have = realloc(have, refsz * sizeof(have[0]));
437 9ff10419 2020-03-18 stsp if (have == NULL) {
438 9ff10419 2020-03-18 stsp err = got_error_from_errno("realloc");
439 9ff10419 2020-03-18 stsp goto done;
440 9ff10419 2020-03-18 stsp }
441 93658fb9 2020-03-18 stsp want = realloc(want, refsz * sizeof(want[0]));
442 9ff10419 2020-03-18 stsp if (want == NULL) {
443 9ff10419 2020-03-18 stsp err = got_error_from_errno("realloc");
444 9ff10419 2020-03-18 stsp goto done;
445 9ff10419 2020-03-18 stsp }
446 93658fb9 2020-03-18 stsp }
447 9ff10419 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, sp[0])) {
448 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
449 9ff10419 2020-03-18 stsp goto done;
450 9ff10419 2020-03-18 stsp }
451 9ff10419 2020-03-18 stsp
452 93658fb9 2020-03-18 stsp if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
453 93658fb9 2020-03-18 stsp memset(&have[nref], 0, sizeof(have[nref]));
454 8f2d01a6 2020-03-18 stsp err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
455 8f2d01a6 2020-03-18 stsp if (err)
456 8f2d01a6 2020-03-18 stsp goto done;
457 93658fb9 2020-03-18 stsp if (chattygit)
458 93658fb9 2020-03-18 stsp fprintf(stderr, "remote %s\n", sp[1]);
459 93658fb9 2020-03-18 stsp nref++;
460 93658fb9 2020-03-18 stsp }
461 93658fb9 2020-03-18 stsp
462 93658fb9 2020-03-18 stsp req = 0;
463 9ff10419 2020-03-18 stsp for (i = 0; i < nref; i++){
464 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
465 93658fb9 2020-03-18 stsp continue;
466 93658fb9 2020-03-18 stsp if (got_has_object(&want[i]))
467 93658fb9 2020-03-18 stsp continue;
468 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
469 8a29a085 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
470 8a29a085 2020-03-18 stsp i == 0 && my_capabilities ? my_capabilities : "");
471 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
472 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
473 9ff10419 2020-03-18 stsp goto done;
474 9ff10419 2020-03-18 stsp }
475 9ff10419 2020-03-18 stsp if (writepkt(fd, buf, n) == -1) {
476 9ff10419 2020-03-18 stsp err = got_error_from_errno("writepkt");
477 9ff10419 2020-03-18 stsp goto done;
478 9ff10419 2020-03-18 stsp }
479 93658fb9 2020-03-18 stsp req = 1;
480 93658fb9 2020-03-18 stsp }
481 93658fb9 2020-03-18 stsp flushpkt(fd);
482 9ff10419 2020-03-18 stsp for (i = 0; i < nref; i++){
483 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
484 93658fb9 2020-03-18 stsp continue;
485 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
486 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
487 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
488 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
489 9ff10419 2020-03-18 stsp goto done;
490 9ff10419 2020-03-18 stsp }
491 9ff10419 2020-03-18 stsp if (writepkt(fd, buf, n + 1) == -1) {
492 9ff10419 2020-03-18 stsp err = got_error_from_errno("writepkt");
493 9ff10419 2020-03-18 stsp goto done;
494 9ff10419 2020-03-18 stsp }
495 93658fb9 2020-03-18 stsp }
496 9ff10419 2020-03-18 stsp if (!req){
497 93658fb9 2020-03-18 stsp fprintf(stderr, "up to date\n");
498 93658fb9 2020-03-18 stsp flushpkt(fd);
499 93658fb9 2020-03-18 stsp }
500 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
501 9ff10419 2020-03-18 stsp if (writepkt(fd, buf, n) == -1) {
502 9ff10419 2020-03-18 stsp err = got_error_from_errno("writepkt");
503 9ff10419 2020-03-18 stsp goto done;
504 9ff10419 2020-03-18 stsp }
505 9ff10419 2020-03-18 stsp if (!req)
506 93658fb9 2020-03-18 stsp return 0;
507 93658fb9 2020-03-18 stsp
508 9ff10419 2020-03-18 stsp if ((n = readpkt(fd, buf, sizeof(buf))) == -1) {
509 9ff10419 2020-03-18 stsp err = got_error_from_errno("readpkt");
510 9ff10419 2020-03-18 stsp goto done;
511 9ff10419 2020-03-18 stsp }
512 93658fb9 2020-03-18 stsp buf[n] = 0;
513 93658fb9 2020-03-18 stsp
514 8f2d01a6 2020-03-18 stsp if (chattygit)
515 8f2d01a6 2020-03-18 stsp fprintf(stderr, "fetching...\n");
516 93658fb9 2020-03-18 stsp packsz = 0;
517 9ff10419 2020-03-18 stsp while (1) {
518 9ff10419 2020-03-18 stsp ssize_t w;
519 93658fb9 2020-03-18 stsp n = readn(fd, buf, sizeof buf);
520 9ff10419 2020-03-18 stsp if (n == 0)
521 93658fb9 2020-03-18 stsp break;
522 9ff10419 2020-03-18 stsp if (n == -1) {
523 9ff10419 2020-03-18 stsp err = got_error_from_errno("readn");
524 9ff10419 2020-03-18 stsp goto done;
525 9ff10419 2020-03-18 stsp }
526 9ff10419 2020-03-18 stsp w = write(packfd, buf, n);
527 9ff10419 2020-03-18 stsp if (w == -1) {
528 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
529 9ff10419 2020-03-18 stsp goto done;
530 9ff10419 2020-03-18 stsp }
531 9ff10419 2020-03-18 stsp if (w != n) {
532 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
533 9ff10419 2020-03-18 stsp goto done;
534 9ff10419 2020-03-18 stsp }
535 93658fb9 2020-03-18 stsp packsz += n;
536 93658fb9 2020-03-18 stsp }
537 9ff10419 2020-03-18 stsp if (lseek(packfd, 0, SEEK_SET) == -1) {
538 9ff10419 2020-03-18 stsp err = got_error_from_errno("lseek");
539 9ff10419 2020-03-18 stsp goto done;
540 9ff10419 2020-03-18 stsp }
541 9ff10419 2020-03-18 stsp if (got_check_pack_hash(packfd, packsz, packid->sha1) == -1)
542 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
543 9ff10419 2020-03-18 stsp done:
544 9ff10419 2020-03-18 stsp free(have);
545 9ff10419 2020-03-18 stsp free(want);
546 8f2d01a6 2020-03-18 stsp return err;
547 93658fb9 2020-03-18 stsp }
548 93658fb9 2020-03-18 stsp
549 93658fb9 2020-03-18 stsp
550 93658fb9 2020-03-18 stsp int
551 93658fb9 2020-03-18 stsp main(int argc, char **argv)
552 93658fb9 2020-03-18 stsp {
553 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
554 9ff10419 2020-03-18 stsp int fetchfd, packfd = -1;
555 93658fb9 2020-03-18 stsp struct got_object_id packid;
556 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
557 93658fb9 2020-03-18 stsp struct imsg imsg;
558 93658fb9 2020-03-18 stsp
559 93658fb9 2020-03-18 stsp if(getenv("GOT_DEBUG") != NULL){
560 93658fb9 2020-03-18 stsp fprintf(stderr, "fetch-pack being chatty!\n");
561 93658fb9 2020-03-18 stsp chattygit = 1;
562 93658fb9 2020-03-18 stsp }
563 93658fb9 2020-03-18 stsp
564 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
565 93658fb9 2020-03-18 stsp if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
566 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
567 93658fb9 2020-03-18 stsp err = NULL;
568 93658fb9 2020-03-18 stsp goto done;
569 93658fb9 2020-03-18 stsp }
570 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
571 93658fb9 2020-03-18 stsp goto done;
572 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
573 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
574 93658fb9 2020-03-18 stsp goto done;
575 93658fb9 2020-03-18 stsp }
576 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
577 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
578 93658fb9 2020-03-18 stsp goto done;
579 93658fb9 2020-03-18 stsp }
580 93658fb9 2020-03-18 stsp fetchfd = imsg.fd;
581 93658fb9 2020-03-18 stsp
582 93658fb9 2020-03-18 stsp if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
583 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
584 93658fb9 2020-03-18 stsp err = NULL;
585 93658fb9 2020-03-18 stsp goto done;
586 93658fb9 2020-03-18 stsp }
587 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
588 93658fb9 2020-03-18 stsp goto done;
589 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
590 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
591 93658fb9 2020-03-18 stsp goto done;
592 93658fb9 2020-03-18 stsp }
593 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
594 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
595 93658fb9 2020-03-18 stsp goto done;
596 93658fb9 2020-03-18 stsp }
597 93658fb9 2020-03-18 stsp packfd = imsg.fd;
598 93658fb9 2020-03-18 stsp
599 8f2d01a6 2020-03-18 stsp err = fetch_pack(fetchfd, packfd, &packid, &ibuf);
600 9ff10419 2020-03-18 stsp if (err)
601 93658fb9 2020-03-18 stsp goto done;
602 93658fb9 2020-03-18 stsp done:
603 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
604 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
605 9ff10419 2020-03-18 stsp if (err != NULL)
606 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
607 93658fb9 2020-03-18 stsp else
608 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
609 93658fb9 2020-03-18 stsp if(err != NULL) {
610 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
611 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
612 93658fb9 2020-03-18 stsp }
613 93658fb9 2020-03-18 stsp
614 93658fb9 2020-03-18 stsp exit(0);
615 93658fb9 2020-03-18 stsp }