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