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 81a12da5 2020-09-09 naddy #include <unistd.h>
36 93658fb9 2020-03-18 stsp #include <zlib.h>
37 93658fb9 2020-03-18 stsp #include <err.h>
38 93658fb9 2020-03-18 stsp
39 93658fb9 2020-03-18 stsp #include "got_error.h"
40 93658fb9 2020-03-18 stsp #include "got_object.h"
41 abe0f35f 2020-03-18 stsp #include "got_path.h"
42 8a29a085 2020-03-18 stsp #include "got_version.h"
43 f1c6967f 2020-03-19 stsp #include "got_fetch.h"
44 659e7fbd 2020-03-20 stsp #include "got_reference.h"
45 93658fb9 2020-03-18 stsp
46 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
50 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
51 0872c0b0 2020-03-18 stsp #include "got_lib_pack.h"
52 93658fb9 2020-03-18 stsp
53 8a29a085 2020-03-18 stsp #ifndef nitems
54 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 8a29a085 2020-03-18 stsp #endif
56 8a29a085 2020-03-18 stsp
57 93658fb9 2020-03-18 stsp struct got_object *indexed;
58 858b0dfb 2020-03-20 stsp static int chattygot;
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 2690194b 2020-03-21 stsp if (chattygot > 1)
84 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
85 858b0dfb 2020-03-20 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 858b0dfb 2020-03-20 stsp if (r == 0) {
114 858b0dfb 2020-03-20 stsp /* implicit "0000" */
115 2690194b 2020-03-21 stsp if (chattygot > 1)
116 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
117 531c3985 2020-03-18 stsp return NULL;
118 858b0dfb 2020-03-20 stsp }
119 4dc8ee09 2020-03-18 stsp if (r != 4)
120 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
121 531c3985 2020-03-18 stsp "wrong packet header length");
122 fe53745c 2020-03-18 stsp
123 4dc8ee09 2020-03-18 stsp lenstr[4] = '\0';
124 54d1a70f 2020-03-18 stsp for (i = 0; i < 4; i++) {
125 858b0dfb 2020-03-20 stsp if (!isprint((unsigned char)lenstr[i]))
126 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
127 858b0dfb 2020-03-20 stsp "unprintable character in packet length field");
128 858b0dfb 2020-03-20 stsp }
129 858b0dfb 2020-03-20 stsp for (i = 0; i < 4; i++) {
130 858b0dfb 2020-03-20 stsp if (!isxdigit((unsigned char)lenstr[i])) {
131 858b0dfb 2020-03-20 stsp if (chattygot)
132 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: bad length: '%s'\n",
133 858b0dfb 2020-03-20 stsp getprogname(), lenstr);
134 858b0dfb 2020-03-20 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
135 531c3985 2020-03-18 stsp "packet length not specified in hex");
136 858b0dfb 2020-03-20 stsp }
137 54d1a70f 2020-03-18 stsp }
138 4dc8ee09 2020-03-18 stsp errno = 0;
139 4dc8ee09 2020-03-18 stsp len = strtol(lenstr, &e, 16);
140 4dc8ee09 2020-03-18 stsp if (lenstr[0] == '\0' || *e != '\0')
141 4dc8ee09 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKET);
142 4dc8ee09 2020-03-18 stsp if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
143 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
144 4dc8ee09 2020-03-18 stsp if (len > INT_MAX || len < INT_MIN)
145 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
146 4dc8ee09 2020-03-18 stsp n = len;
147 531c3985 2020-03-18 stsp if (n == 0)
148 fe53745c 2020-03-18 stsp return NULL;
149 4dc8ee09 2020-03-18 stsp if (n <= 4)
150 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
151 93658fb9 2020-03-18 stsp n -= 4;
152 531c3985 2020-03-18 stsp
153 531c3985 2020-03-18 stsp *datalen = n;
154 531c3985 2020-03-18 stsp return NULL;
155 531c3985 2020-03-18 stsp }
156 531c3985 2020-03-18 stsp
157 531c3985 2020-03-18 stsp static const struct got_error *
158 531c3985 2020-03-18 stsp readpkt(int *outlen, int fd, char *buf, int buflen)
159 531c3985 2020-03-18 stsp {
160 531c3985 2020-03-18 stsp const struct got_error *err = NULL;
161 858b0dfb 2020-03-20 stsp int datalen, i;
162 531c3985 2020-03-18 stsp ssize_t n;
163 531c3985 2020-03-18 stsp
164 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
165 531c3985 2020-03-18 stsp if (err)
166 531c3985 2020-03-18 stsp return err;
167 531c3985 2020-03-18 stsp
168 531c3985 2020-03-18 stsp if (datalen > buflen)
169 fe53745c 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
170 fe53745c 2020-03-18 stsp
171 531c3985 2020-03-18 stsp err = readn(&n, fd, buf, datalen);
172 fe53745c 2020-03-18 stsp if (err)
173 fe53745c 2020-03-18 stsp return err;
174 531c3985 2020-03-18 stsp if (n != datalen)
175 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
176 fe53745c 2020-03-18 stsp
177 2690194b 2020-03-21 stsp if (chattygot > 1) {
178 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
179 858b0dfb 2020-03-20 stsp for (i = 0; i < n; i++) {
180 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
181 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
182 858b0dfb 2020-03-20 stsp else
183 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
184 858b0dfb 2020-03-20 stsp }
185 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
186 858b0dfb 2020-03-20 stsp }
187 858b0dfb 2020-03-20 stsp
188 fe53745c 2020-03-18 stsp *outlen = n;
189 fe53745c 2020-03-18 stsp return NULL;
190 93658fb9 2020-03-18 stsp }
191 93658fb9 2020-03-18 stsp
192 344e4747 2020-03-18 stsp static const struct got_error *
193 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
194 93658fb9 2020-03-18 stsp {
195 93658fb9 2020-03-18 stsp char len[5];
196 858b0dfb 2020-03-20 stsp int i;
197 344e4747 2020-03-18 stsp ssize_t w;
198 93658fb9 2020-03-18 stsp
199 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
200 344e4747 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
201 344e4747 2020-03-18 stsp w = write(fd, len, 4);
202 344e4747 2020-03-18 stsp if (w == -1)
203 344e4747 2020-03-18 stsp return got_error_from_errno("write");
204 344e4747 2020-03-18 stsp if (w != 4)
205 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
206 344e4747 2020-03-18 stsp w = write(fd, buf, nbuf);
207 344e4747 2020-03-18 stsp if (w == -1)
208 344e4747 2020-03-18 stsp return got_error_from_errno("write");
209 344e4747 2020-03-18 stsp if (w != nbuf)
210 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
211 2690194b 2020-03-21 stsp if (chattygot > 1) {
212 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
213 858b0dfb 2020-03-20 stsp for (i = 0; i < nbuf; i++) {
214 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
215 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
216 858b0dfb 2020-03-20 stsp else
217 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
218 858b0dfb 2020-03-20 stsp }
219 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
220 858b0dfb 2020-03-20 stsp }
221 344e4747 2020-03-18 stsp return NULL;
222 93658fb9 2020-03-18 stsp }
223 93658fb9 2020-03-18 stsp
224 7848a0e1 2020-03-19 stsp static void
225 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
226 7848a0e1 2020-03-19 stsp struct got_object_id *my_id, char *refname)
227 93658fb9 2020-03-18 stsp {
228 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
229 93658fb9 2020-03-18 stsp
230 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
231 7848a0e1 2020-03-19 stsp * we should use a flag instead */
232 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
233 93658fb9 2020-03-18 stsp
234 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
235 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
236 7848a0e1 2020-03-19 stsp if (strcmp(pe->path, refname) == 0) {
237 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
238 33501562 2020-03-18 stsp break;
239 33501562 2020-03-18 stsp }
240 93658fb9 2020-03-18 stsp }
241 93658fb9 2020-03-18 stsp }
242 93658fb9 2020-03-18 stsp
243 93658fb9 2020-03-18 stsp static int
244 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
245 93658fb9 2020-03-18 stsp {
246 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
247 659e7fbd 2020-03-20 stsp return 0;
248 93658fb9 2020-03-18 stsp
249 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
250 659e7fbd 2020-03-20 stsp wanted_branch += 11;
251 659e7fbd 2020-03-20 stsp
252 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
253 0e4002ca 2020-03-21 stsp }
254 0e4002ca 2020-03-21 stsp
255 0e4002ca 2020-03-21 stsp static int
256 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
257 0e4002ca 2020-03-21 stsp {
258 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
259 0e4002ca 2020-03-21 stsp return 0;
260 0e4002ca 2020-03-21 stsp refname += 5;
261 0e4002ca 2020-03-21 stsp
262 0e4002ca 2020-03-21 stsp /*
263 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
264 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
265 0e4002ca 2020-03-21 stsp */
266 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
267 0e4002ca 2020-03-21 stsp return 0;
268 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
269 0e4002ca 2020-03-21 stsp return 0;
270 0e4002ca 2020-03-21 stsp
271 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
272 0e4002ca 2020-03-21 stsp wanted_ref += 5;
273 0e4002ca 2020-03-21 stsp
274 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
275 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
276 0e4002ca 2020-03-21 stsp return 1;
277 0e4002ca 2020-03-21 stsp
278 0e4002ca 2020-03-21 stsp /* Allow exact match. */
279 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
280 93658fb9 2020-03-18 stsp }
281 93658fb9 2020-03-18 stsp
282 0d0a341c 2020-03-18 stsp static const struct got_error *
283 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
284 93658fb9 2020-03-18 stsp {
285 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
286 93658fb9 2020-03-18 stsp char *p;
287 0d0a341c 2020-03-18 stsp size_t i, n = 0;
288 93658fb9 2020-03-18 stsp
289 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
290 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
291 0d0a341c 2020-03-18 stsp
292 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
293 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
294 93658fb9 2020-03-18 stsp line++;
295 0d0a341c 2020-03-18 stsp n++;
296 0d0a341c 2020-03-18 stsp }
297 93658fb9 2020-03-18 stsp p = line;
298 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
299 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
300 93658fb9 2020-03-18 stsp line++;
301 0d0a341c 2020-03-18 stsp n++;
302 0d0a341c 2020-03-18 stsp }
303 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
304 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
305 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
306 0d0a341c 2020-03-18 stsp goto done;
307 0d0a341c 2020-03-18 stsp }
308 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
309 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
310 0d0a341c 2020-03-18 stsp line++;
311 0d0a341c 2020-03-18 stsp n++;
312 0d0a341c 2020-03-18 stsp }
313 93658fb9 2020-03-18 stsp }
314 0d0a341c 2020-03-18 stsp if (i <= 2)
315 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
316 0d0a341c 2020-03-18 stsp done:
317 0d0a341c 2020-03-18 stsp if (err) {
318 0d0a341c 2020-03-18 stsp int j;
319 631179de 2020-07-31 naddy for (j = 0; j < i; j++) {
320 0d0a341c 2020-03-18 stsp free(tokens[j]);
321 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
322 631179de 2020-07-31 naddy }
323 0d0a341c 2020-03-18 stsp }
324 0d0a341c 2020-03-18 stsp return err;
325 8a29a085 2020-03-18 stsp }
326 00cd0e0a 2020-03-18 stsp
327 00cd0e0a 2020-03-18 stsp static const struct got_error *
328 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
329 00cd0e0a 2020-03-18 stsp char *line, int len)
330 00cd0e0a 2020-03-18 stsp {
331 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
332 00cd0e0a 2020-03-18 stsp char *tokens[3];
333 8a29a085 2020-03-18 stsp
334 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
335 00cd0e0a 2020-03-18 stsp if (err)
336 00cd0e0a 2020-03-18 stsp return err;
337 00cd0e0a 2020-03-18 stsp
338 00cd0e0a 2020-03-18 stsp if (tokens[0])
339 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
340 00cd0e0a 2020-03-18 stsp if (tokens[1])
341 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
342 2690194b 2020-03-21 stsp if (tokens[2]) {
343 2690194b 2020-03-21 stsp char *p;
344 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
345 2690194b 2020-03-21 stsp p = strrchr(*server_capabilities, '\n');
346 2690194b 2020-03-21 stsp if (p)
347 2690194b 2020-03-21 stsp *p = '\0';
348 2690194b 2020-03-21 stsp }
349 3168e5da 2020-09-10 stsp
350 00cd0e0a 2020-03-18 stsp return NULL;
351 00cd0e0a 2020-03-18 stsp }
352 00cd0e0a 2020-03-18 stsp
353 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
354 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
355 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
356 531c3985 2020-03-18 stsp
357 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
358 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
359 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
360 531c3985 2020-03-18 stsp
361 531c3985 2020-03-18 stsp
362 8a29a085 2020-03-18 stsp struct got_capability {
363 8a29a085 2020-03-18 stsp const char *key;
364 8a29a085 2020-03-18 stsp const char *value;
365 8a29a085 2020-03-18 stsp };
366 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
367 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
368 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
369 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
370 8a29a085 2020-03-18 stsp };
371 8a29a085 2020-03-18 stsp
372 8a29a085 2020-03-18 stsp static const struct got_error *
373 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
374 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
375 8a29a085 2020-03-18 stsp {
376 8a29a085 2020-03-18 stsp char *equalsign;
377 8a29a085 2020-03-18 stsp char *s;
378 8a29a085 2020-03-18 stsp
379 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
380 8a29a085 2020-03-18 stsp if (equalsign) {
381 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
382 8a29a085 2020-03-18 stsp return NULL;
383 8a29a085 2020-03-18 stsp } else {
384 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
385 8a29a085 2020-03-18 stsp return NULL;
386 8a29a085 2020-03-18 stsp }
387 8a29a085 2020-03-18 stsp
388 406106ee 2020-03-20 stsp if (asprintf(&s, "%s %s%s%s",
389 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
390 8a29a085 2020-03-18 stsp mycapa->key,
391 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
392 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
393 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
394 8a29a085 2020-03-18 stsp
395 8a29a085 2020-03-18 stsp free(*my_capabilities);
396 8a29a085 2020-03-18 stsp *my_capabilities = s;
397 8a29a085 2020-03-18 stsp return NULL;
398 93658fb9 2020-03-18 stsp }
399 93658fb9 2020-03-18 stsp
400 9ff10419 2020-03-18 stsp static const struct got_error *
401 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
402 8a29a085 2020-03-18 stsp {
403 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
404 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
405 abe0f35f 2020-03-18 stsp
406 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
407 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
408 abe0f35f 2020-03-18 stsp return NULL;
409 abe0f35f 2020-03-18 stsp
410 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
411 abe0f35f 2020-03-18 stsp if (colon == NULL)
412 abe0f35f 2020-03-18 stsp return NULL;
413 abe0f35f 2020-03-18 stsp
414 abe0f35f 2020-03-18 stsp *colon = '\0';
415 abe0f35f 2020-03-18 stsp name = strdup(capa);
416 abe0f35f 2020-03-18 stsp if (name == NULL)
417 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
418 abe0f35f 2020-03-18 stsp
419 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
420 abe0f35f 2020-03-18 stsp if (target == NULL) {
421 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
422 abe0f35f 2020-03-18 stsp goto done;
423 abe0f35f 2020-03-18 stsp }
424 abe0f35f 2020-03-18 stsp
425 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
426 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
427 abe0f35f 2020-03-18 stsp done:
428 abe0f35f 2020-03-18 stsp if (err) {
429 abe0f35f 2020-03-18 stsp free(name);
430 abe0f35f 2020-03-18 stsp free(target);
431 abe0f35f 2020-03-18 stsp }
432 abe0f35f 2020-03-18 stsp return err;
433 abe0f35f 2020-03-18 stsp }
434 abe0f35f 2020-03-18 stsp
435 abe0f35f 2020-03-18 stsp static const struct got_error *
436 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
437 abe0f35f 2020-03-18 stsp char *server_capabilities)
438 abe0f35f 2020-03-18 stsp {
439 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
440 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
441 8a29a085 2020-03-18 stsp int i;
442 8a29a085 2020-03-18 stsp
443 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
444 8a29a085 2020-03-18 stsp do {
445 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
446 8a29a085 2020-03-18 stsp if (capa == NULL)
447 8a29a085 2020-03-18 stsp return NULL;
448 8a29a085 2020-03-18 stsp
449 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
450 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
451 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
452 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
453 abe0f35f 2020-03-18 stsp if (err)
454 abe0f35f 2020-03-18 stsp break;
455 abe0f35f 2020-03-18 stsp continue;
456 abe0f35f 2020-03-18 stsp }
457 abe0f35f 2020-03-18 stsp
458 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
459 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
460 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
461 8a29a085 2020-03-18 stsp if (err)
462 8a29a085 2020-03-18 stsp break;
463 8a29a085 2020-03-18 stsp }
464 8a29a085 2020-03-18 stsp } while (capa);
465 8a29a085 2020-03-18 stsp
466 406106ee 2020-03-20 stsp if (*my_capabilities == NULL) {
467 406106ee 2020-03-20 stsp *my_capabilities = strdup("");
468 406106ee 2020-03-20 stsp if (*my_capabilities == NULL)
469 406106ee 2020-03-20 stsp err = got_error_from_errno("strdup");
470 406106ee 2020-03-20 stsp }
471 8a29a085 2020-03-18 stsp return err;
472 e70bf110 2020-03-22 stsp }
473 e70bf110 2020-03-22 stsp
474 e70bf110 2020-03-22 stsp static const struct got_error *
475 e70bf110 2020-03-22 stsp send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
476 e70bf110 2020-03-22 stsp {
477 e70bf110 2020-03-22 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
478 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
479 e70bf110 2020-03-22 stsp
480 e70bf110 2020-03-22 stsp if (msglen == 0)
481 e70bf110 2020-03-22 stsp return NULL;
482 e70bf110 2020-03-22 stsp
483 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
484 e70bf110 2020-03-22 stsp msg, msglen) == -1)
485 e70bf110 2020-03-22 stsp return got_error_from_errno(
486 e70bf110 2020-03-22 stsp "imsg_compose FETCH_SERVER_PROGRESS");
487 e70bf110 2020-03-22 stsp
488 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
489 531c3985 2020-03-18 stsp }
490 531c3985 2020-03-18 stsp
491 531c3985 2020-03-18 stsp static const struct got_error *
492 e70bf110 2020-03-22 stsp send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
493 e70bf110 2020-03-22 stsp {
494 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
495 e70bf110 2020-03-22 stsp &bytes, sizeof(bytes)) == -1)
496 e70bf110 2020-03-22 stsp return got_error_from_errno(
497 e70bf110 2020-03-22 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
498 e70bf110 2020-03-22 stsp
499 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
500 e70bf110 2020-03-22 stsp }
501 e70bf110 2020-03-22 stsp
502 e70bf110 2020-03-22 stsp static const struct got_error *
503 1d72a2a0 2020-03-24 stsp send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
504 e70bf110 2020-03-22 stsp {
505 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
506 1d72a2a0 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1)
507 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
508 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
509 e70bf110 2020-03-22 stsp }
510 e70bf110 2020-03-22 stsp
511 e70bf110 2020-03-22 stsp
512 e70bf110 2020-03-22 stsp
513 e70bf110 2020-03-22 stsp static const struct got_error *
514 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
515 531c3985 2020-03-18 stsp {
516 531c3985 2020-03-18 stsp int i;
517 531c3985 2020-03-18 stsp
518 531c3985 2020-03-18 stsp if (len == 0)
519 531c3985 2020-03-18 stsp return NULL;
520 531c3985 2020-03-18 stsp
521 531c3985 2020-03-18 stsp /*
522 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
523 531c3985 2020-03-18 stsp * Server may send up to 64k.
524 531c3985 2020-03-18 stsp */
525 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
526 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
527 531c3985 2020-03-18 stsp
528 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
529 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
530 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
531 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
532 531c3985 2020-03-18 stsp continue;
533 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
534 531c3985 2020-03-18 stsp "non-printable progress message received from server");
535 531c3985 2020-03-18 stsp }
536 531c3985 2020-03-18 stsp
537 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
538 8a29a085 2020-03-18 stsp }
539 abe0f35f 2020-03-18 stsp
540 8a29a085 2020-03-18 stsp static const struct got_error *
541 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
542 531c3985 2020-03-18 stsp {
543 531c3985 2020-03-18 stsp static char msg[1024];
544 531c3985 2020-03-18 stsp int i;
545 531c3985 2020-03-18 stsp
546 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
547 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
548 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
549 531c3985 2020-03-18 stsp "non-printable error message received from server");
550 531c3985 2020-03-18 stsp msg[i] = buf[i];
551 531c3985 2020-03-18 stsp }
552 531c3985 2020-03-18 stsp msg[i] = '\0';
553 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
554 531c3985 2020-03-18 stsp }
555 531c3985 2020-03-18 stsp
556 531c3985 2020-03-18 stsp static const struct got_error *
557 e70bf110 2020-03-22 stsp send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
558 e70bf110 2020-03-22 stsp {
559 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
560 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
561 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
562 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
563 e70bf110 2020-03-22 stsp
564 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
565 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
566 e70bf110 2020-03-22 stsp const char *target = pe->data;
567 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
568 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
569 e70bf110 2020-03-22 stsp nsymrefs++;
570 e70bf110 2020-03-22 stsp }
571 e70bf110 2020-03-22 stsp
572 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
573 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
574 e70bf110 2020-03-22 stsp
575 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
576 e70bf110 2020-03-22 stsp if (wbuf == NULL)
577 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
578 e70bf110 2020-03-22 stsp
579 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
580 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
581 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
582 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
583 e70bf110 2020-03-22 stsp return err;
584 e70bf110 2020-03-22 stsp }
585 e70bf110 2020-03-22 stsp
586 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
587 e70bf110 2020-03-22 stsp const char *name = pe->path;
588 e70bf110 2020-03-22 stsp size_t name_len = pe->path_len;
589 e70bf110 2020-03-22 stsp const char *target = pe->data;
590 e70bf110 2020-03-22 stsp size_t target_len = strlen(target);
591 e70bf110 2020-03-22 stsp
592 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
593 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
594 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
595 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
596 e70bf110 2020-03-22 stsp return err;
597 e70bf110 2020-03-22 stsp }
598 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
599 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
600 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
601 e70bf110 2020-03-22 stsp return err;
602 e70bf110 2020-03-22 stsp }
603 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, name, name_len) == -1) {
604 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
605 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
606 e70bf110 2020-03-22 stsp return err;
607 e70bf110 2020-03-22 stsp }
608 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, target, target_len) == -1) {
609 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
610 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
611 e70bf110 2020-03-22 stsp return err;
612 e70bf110 2020-03-22 stsp }
613 e70bf110 2020-03-22 stsp }
614 e70bf110 2020-03-22 stsp
615 e70bf110 2020-03-22 stsp wbuf->fd = -1;
616 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
617 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
618 e70bf110 2020-03-22 stsp }
619 e70bf110 2020-03-22 stsp
620 e70bf110 2020-03-22 stsp static const struct got_error *
621 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
622 e70bf110 2020-03-22 stsp const char *refname)
623 e70bf110 2020-03-22 stsp {
624 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
625 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
626 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
627 e70bf110 2020-03-22 stsp
628 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
629 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
630 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
631 e70bf110 2020-03-22 stsp
632 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
633 e70bf110 2020-03-22 stsp if (wbuf == NULL)
634 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
635 e70bf110 2020-03-22 stsp
636 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
637 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
638 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
639 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
640 e70bf110 2020-03-22 stsp return err;
641 e70bf110 2020-03-22 stsp }
642 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
643 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
644 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
645 e70bf110 2020-03-22 stsp return err;
646 e70bf110 2020-03-22 stsp }
647 e70bf110 2020-03-22 stsp
648 e70bf110 2020-03-22 stsp wbuf->fd = -1;
649 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
650 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
651 e70bf110 2020-03-22 stsp }
652 729743d1 2020-03-23 stsp
653 e70bf110 2020-03-22 stsp static const struct got_error *
654 1d72a2a0 2020-03-24 stsp fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
655 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
656 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
657 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
658 41b0de12 2020-03-21 stsp struct imsgbuf *ibuf)
659 93658fb9 2020-03-18 stsp {
660 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
661 f1c6967f 2020-03-19 stsp char buf[GOT_FETCH_PKTMAX];
662 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
663 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
664 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
665 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
666 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
667 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
668 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
669 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
670 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
671 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
672 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
673 659e7fbd 2020-03-20 stsp int found_branch = 0;
674 dc671e91 2020-03-24 stsp SHA1_CTX sha1_ctx;
675 dc671e91 2020-03-24 stsp uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
676 dc671e91 2020-03-24 stsp size_t sha1_buf_len = 0;
677 dc671e91 2020-03-24 stsp ssize_t w;
678 93658fb9 2020-03-18 stsp
679 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
680 dc671e91 2020-03-24 stsp SHA1Init(&sha1_ctx);
681 abe0f35f 2020-03-18 stsp
682 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
683 9ff10419 2020-03-18 stsp if (have == NULL)
684 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
685 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
686 9ff10419 2020-03-18 stsp if (want == NULL) {
687 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
688 9ff10419 2020-03-18 stsp goto done;
689 9ff10419 2020-03-18 stsp }
690 9ff10419 2020-03-18 stsp while (1) {
691 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
692 fe53745c 2020-03-18 stsp if (err)
693 9ff10419 2020-03-18 stsp goto done;
694 9ff10419 2020-03-18 stsp if (n == 0)
695 93658fb9 2020-03-18 stsp break;
696 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
697 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
698 9ff10419 2020-03-18 stsp goto done;
699 9ff10419 2020-03-18 stsp }
700 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
701 00cd0e0a 2020-03-18 stsp buf, n);
702 0d0a341c 2020-03-18 stsp if (err)
703 9ff10419 2020-03-18 stsp goto done;
704 8a29a085 2020-03-18 stsp if (is_firstpkt) {
705 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
706 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
707 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
708 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
709 00cd0e0a 2020-03-18 stsp server_capabilities);
710 8a29a085 2020-03-18 stsp if (err)
711 8a29a085 2020-03-18 stsp goto done;
712 858b0dfb 2020-03-20 stsp if (chattygot)
713 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
714 858b0dfb 2020-03-20 stsp getprogname(), my_capabilities);
715 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
716 abe0f35f 2020-03-18 stsp if (err)
717 abe0f35f 2020-03-18 stsp goto done;
718 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
719 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
720 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
721 659e7fbd 2020-03-20 stsp const char *name = pe->path;
722 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
723 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
724 659e7fbd 2020-03-20 stsp continue;
725 659e7fbd 2020-03-20 stsp default_branch = symref_target;
726 659e7fbd 2020-03-20 stsp break;
727 659e7fbd 2020-03-20 stsp }
728 659e7fbd 2020-03-20 stsp }
729 7848a0e1 2020-03-19 stsp continue;
730 8a29a085 2020-03-18 stsp }
731 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
732 2690194b 2020-03-21 stsp if (chattygot) {
733 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
734 2690194b 2020-03-21 stsp getprogname(), refname);
735 2690194b 2020-03-21 stsp }
736 93658fb9 2020-03-18 stsp continue;
737 2690194b 2020-03-21 stsp }
738 659e7fbd 2020-03-20 stsp
739 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
740 41b0de12 2020-03-21 stsp if (fetch_all_branches || list_refs_only) {
741 4ba14133 2020-03-20 stsp found_branch = 1;
742 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
743 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
744 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
745 4ba14133 2020-03-20 stsp break;
746 4ba14133 2020-03-20 stsp }
747 2690194b 2020-03-21 stsp if (pe == NULL) {
748 2690194b 2020-03-21 stsp if (chattygot) {
749 2690194b 2020-03-21 stsp fprintf(stderr,
750 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
751 2690194b 2020-03-21 stsp getprogname(), refname);
752 2690194b 2020-03-21 stsp }
753 4ba14133 2020-03-20 stsp continue;
754 2690194b 2020-03-21 stsp }
755 4ba14133 2020-03-20 stsp found_branch = 1;
756 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
757 2690194b 2020-03-21 stsp if (!match_branch(refname, default_branch)) {
758 2690194b 2020-03-21 stsp if (chattygot) {
759 2690194b 2020-03-21 stsp fprintf(stderr,
760 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
761 2690194b 2020-03-21 stsp getprogname(), refname);
762 2690194b 2020-03-21 stsp }
763 4ba14133 2020-03-20 stsp continue;
764 2690194b 2020-03-21 stsp }
765 4ba14133 2020-03-20 stsp found_branch = 1;
766 4ba14133 2020-03-20 stsp }
767 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
768 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(wanted_refs)) {
769 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
770 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
771 0e4002ca 2020-03-21 stsp break;
772 0e4002ca 2020-03-21 stsp }
773 0e4002ca 2020-03-21 stsp if (pe == NULL) {
774 0e4002ca 2020-03-21 stsp if (chattygot) {
775 0e4002ca 2020-03-21 stsp fprintf(stderr,
776 0e4002ca 2020-03-21 stsp "%s: ignoring %s\n",
777 0e4002ca 2020-03-21 stsp getprogname(), refname);
778 0e4002ca 2020-03-21 stsp }
779 0e4002ca 2020-03-21 stsp continue;
780 0e4002ca 2020-03-21 stsp }
781 0e4002ca 2020-03-21 stsp found_branch = 1;
782 0e4002ca 2020-03-21 stsp } else if (!list_refs_only) {
783 2690194b 2020-03-21 stsp if (chattygot) {
784 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
785 2690194b 2020-03-21 stsp getprogname(), refname);
786 2690194b 2020-03-21 stsp }
787 4515a796 2020-03-21 stsp continue;
788 2690194b 2020-03-21 stsp }
789 659e7fbd 2020-03-20 stsp }
790 659e7fbd 2020-03-20 stsp
791 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
792 93658fb9 2020-03-18 stsp refsz *= 2;
793 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
794 9ff10419 2020-03-18 stsp if (have == NULL) {
795 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
796 9ff10419 2020-03-18 stsp goto done;
797 9ff10419 2020-03-18 stsp }
798 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
799 9ff10419 2020-03-18 stsp if (want == NULL) {
800 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
801 9ff10419 2020-03-18 stsp goto done;
802 9ff10419 2020-03-18 stsp }
803 93658fb9 2020-03-18 stsp }
804 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
805 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
806 9ff10419 2020-03-18 stsp goto done;
807 9ff10419 2020-03-18 stsp }
808 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
809 e70bf110 2020-03-22 stsp err = send_fetch_ref(ibuf, &want[nref], refname);
810 8f2d01a6 2020-03-18 stsp if (err)
811 8f2d01a6 2020-03-18 stsp goto done;
812 858b0dfb 2020-03-20 stsp
813 2690194b 2020-03-21 stsp if (chattygot)
814 2690194b 2020-03-21 stsp fprintf(stderr, "%s: %s will be fetched\n",
815 2690194b 2020-03-21 stsp getprogname(), refname);
816 2690194b 2020-03-21 stsp if (chattygot > 1) {
817 858b0dfb 2020-03-20 stsp char *theirs, *mine;
818 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
819 858b0dfb 2020-03-20 stsp if (err)
820 858b0dfb 2020-03-20 stsp goto done;
821 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
822 858b0dfb 2020-03-20 stsp if (err) {
823 858b0dfb 2020-03-20 stsp free(theirs);
824 858b0dfb 2020-03-20 stsp goto done;
825 858b0dfb 2020-03-20 stsp }
826 2690194b 2020-03-21 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
827 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
828 858b0dfb 2020-03-20 stsp free(theirs);
829 858b0dfb 2020-03-20 stsp free(mine);
830 858b0dfb 2020-03-20 stsp }
831 93658fb9 2020-03-18 stsp nref++;
832 93658fb9 2020-03-18 stsp }
833 93658fb9 2020-03-18 stsp
834 41b0de12 2020-03-21 stsp if (list_refs_only)
835 41b0de12 2020-03-21 stsp goto done;
836 41b0de12 2020-03-21 stsp
837 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
838 659e7fbd 2020-03-20 stsp if (!found_branch) {
839 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
840 659e7fbd 2020-03-20 stsp goto done;
841 659e7fbd 2020-03-20 stsp }
842 659e7fbd 2020-03-20 stsp
843 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
844 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
845 93658fb9 2020-03-18 stsp continue;
846 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
847 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
848 406106ee 2020-03-20 stsp sent_my_capabilites ? "" : my_capabilities);
849 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
850 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
851 9ff10419 2020-03-18 stsp goto done;
852 9ff10419 2020-03-18 stsp }
853 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
854 344e4747 2020-03-18 stsp if (err)
855 9ff10419 2020-03-18 stsp goto done;
856 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
857 7848a0e1 2020-03-19 stsp nwant++;
858 93658fb9 2020-03-18 stsp }
859 38c670f1 2020-03-18 stsp err = flushpkt(fd);
860 38c670f1 2020-03-18 stsp if (err)
861 38c670f1 2020-03-18 stsp goto done;
862 7848a0e1 2020-03-19 stsp
863 3c912d14 2020-03-19 stsp if (nwant == 0)
864 7848a0e1 2020-03-19 stsp goto done;
865 7848a0e1 2020-03-19 stsp
866 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
867 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
868 93658fb9 2020-03-18 stsp continue;
869 7848a0e1 2020-03-19 stsp got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
870 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
871 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
872 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
873 9ff10419 2020-03-18 stsp goto done;
874 9ff10419 2020-03-18 stsp }
875 c20695fb 2020-03-20 stsp err = writepkt(fd, buf, n);
876 344e4747 2020-03-18 stsp if (err)
877 9ff10419 2020-03-18 stsp goto done;
878 7848a0e1 2020-03-19 stsp nhave++;
879 93658fb9 2020-03-18 stsp }
880 7848a0e1 2020-03-19 stsp
881 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
882 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
883 7848a0e1 2020-03-19 stsp
884 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
885 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
886 38c670f1 2020-03-18 stsp if (err)
887 38c670f1 2020-03-18 stsp goto done;
888 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
889 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
890 7848a0e1 2020-03-19 stsp goto done;
891 7848a0e1 2020-03-19 stsp }
892 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
893 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
894 7848a0e1 2020-03-19 stsp continue;
895 7848a0e1 2020-03-19 stsp }
896 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
897 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
898 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
899 7848a0e1 2020-03-19 stsp "unexpected message from server");
900 7848a0e1 2020-03-19 stsp goto done;
901 7848a0e1 2020-03-19 stsp }
902 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
903 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
904 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
905 7848a0e1 2020-03-19 stsp goto done;
906 7848a0e1 2020-03-19 stsp }
907 7848a0e1 2020-03-19 stsp acked++;
908 93658fb9 2020-03-18 stsp }
909 7848a0e1 2020-03-19 stsp
910 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
911 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
912 344e4747 2020-03-18 stsp if (err)
913 9ff10419 2020-03-18 stsp goto done;
914 93658fb9 2020-03-18 stsp
915 7848a0e1 2020-03-19 stsp if (nhave == 0) {
916 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
917 7848a0e1 2020-03-19 stsp if (err)
918 7848a0e1 2020-03-19 stsp goto done;
919 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
920 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
921 7848a0e1 2020-03-19 stsp "unexpected message from server");
922 7848a0e1 2020-03-19 stsp goto done;
923 7848a0e1 2020-03-19 stsp }
924 04c53c18 2020-03-18 stsp }
925 93658fb9 2020-03-18 stsp
926 858b0dfb 2020-03-20 stsp if (chattygot)
927 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
928 858b0dfb 2020-03-20 stsp
929 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
930 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
931 531c3985 2020-03-18 stsp have_sidebands = 1;
932 531c3985 2020-03-18 stsp
933 9ff10419 2020-03-18 stsp while (1) {
934 dc671e91 2020-03-24 stsp ssize_t r = 0;
935 531c3985 2020-03-18 stsp int datalen = -1;
936 531c3985 2020-03-18 stsp
937 531c3985 2020-03-18 stsp if (have_sidebands) {
938 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
939 531c3985 2020-03-18 stsp if (err)
940 531c3985 2020-03-18 stsp goto done;
941 531c3985 2020-03-18 stsp if (datalen <= 0)
942 531c3985 2020-03-18 stsp break;
943 531c3985 2020-03-18 stsp
944 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
945 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
946 531c3985 2020-03-18 stsp if (r == -1) {
947 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
948 531c3985 2020-03-18 stsp goto done;
949 531c3985 2020-03-18 stsp }
950 531c3985 2020-03-18 stsp if (r != 1) {
951 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
952 531c3985 2020-03-18 stsp "short packet");
953 531c3985 2020-03-18 stsp goto done;
954 531c3985 2020-03-18 stsp }
955 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
956 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
957 531c3985 2020-03-18 stsp "bad packet length");
958 531c3985 2020-03-18 stsp goto done;
959 531c3985 2020-03-18 stsp }
960 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
961 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
962 531c3985 2020-03-18 stsp /* Read packfile data. */
963 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
964 531c3985 2020-03-18 stsp if (err)
965 531c3985 2020-03-18 stsp goto done;
966 531c3985 2020-03-18 stsp if (r != datalen) {
967 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
968 531c3985 2020-03-18 stsp "packet too short");
969 531c3985 2020-03-18 stsp goto done;
970 531c3985 2020-03-18 stsp }
971 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
972 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
973 531c3985 2020-03-18 stsp if (err)
974 531c3985 2020-03-18 stsp goto done;
975 531c3985 2020-03-18 stsp if (r != datalen) {
976 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
977 531c3985 2020-03-18 stsp "packet too short");
978 531c3985 2020-03-18 stsp goto done;
979 531c3985 2020-03-18 stsp }
980 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
981 531c3985 2020-03-18 stsp if (err)
982 531c3985 2020-03-18 stsp goto done;
983 531c3985 2020-03-18 stsp continue;
984 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
985 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
986 531c3985 2020-03-18 stsp if (err)
987 531c3985 2020-03-18 stsp goto done;
988 531c3985 2020-03-18 stsp if (r != datalen) {
989 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
990 531c3985 2020-03-18 stsp "packet too short");
991 531c3985 2020-03-18 stsp goto done;
992 531c3985 2020-03-18 stsp }
993 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
994 531c3985 2020-03-18 stsp goto done;
995 531c3985 2020-03-18 stsp } else {
996 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
997 531c3985 2020-03-18 stsp "unknown side-band received from server");
998 531c3985 2020-03-18 stsp goto done;
999 531c3985 2020-03-18 stsp }
1000 531c3985 2020-03-18 stsp } else {
1001 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
1002 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
1003 531c3985 2020-03-18 stsp if (err)
1004 531c3985 2020-03-18 stsp goto done;
1005 531c3985 2020-03-18 stsp if (r <= 0)
1006 531c3985 2020-03-18 stsp break;
1007 531c3985 2020-03-18 stsp }
1008 dc671e91 2020-03-24 stsp
1009 dc671e91 2020-03-24 stsp /*
1010 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
1011 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
1012 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
1013 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
1014 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
1015 dc671e91 2020-03-24 stsp *
1016 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
1017 dc671e91 2020-03-24 stsp */
1018 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
1019 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
1020 dc671e91 2020-03-24 stsp /*
1021 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
1022 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
1023 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
1024 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
1025 dc671e91 2020-03-24 stsp */
1026 dc671e91 2020-03-24 stsp while (sha1_buf_len > 0 &&
1027 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
1028 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, 1);
1029 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + 1, 1);
1030 dc671e91 2020-03-24 stsp sha1_buf_len--;
1031 dc671e91 2020-03-24 stsp }
1032 dc671e91 2020-03-24 stsp
1033 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
1034 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
1035 dc671e91 2020-03-24 stsp sha1_buf_len += r;
1036 dc671e91 2020-03-24 stsp } else {
1037 dc671e91 2020-03-24 stsp /*
1038 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
1039 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
1040 dc671e91 2020-03-24 stsp */
1041 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, r);
1042 531c3985 2020-03-18 stsp
1043 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
1044 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
1045 dc671e91 2020-03-24 stsp sha1_buf_len - r);
1046 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
1047 dc671e91 2020-03-24 stsp }
1048 dc671e91 2020-03-24 stsp } else {
1049 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
1050 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
1051 dc671e91 2020-03-24 stsp
1052 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
1053 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
1054 dc671e91 2020-03-24 stsp
1055 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
1056 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
1057 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
1058 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
1059 dc671e91 2020-03-24 stsp }
1060 3168e5da 2020-09-10 stsp
1061 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
1062 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
1063 9ff10419 2020-03-18 stsp if (w == -1) {
1064 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
1065 9ff10419 2020-03-18 stsp goto done;
1066 9ff10419 2020-03-18 stsp }
1067 fe53745c 2020-03-18 stsp if (w != r) {
1068 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
1069 9ff10419 2020-03-18 stsp goto done;
1070 9ff10419 2020-03-18 stsp }
1071 531c3985 2020-03-18 stsp packsz += w;
1072 5672d305 2020-03-18 stsp
1073 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
1074 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
1075 e70bf110 2020-03-22 stsp err = send_fetch_download_progress(ibuf, packsz);
1076 5672d305 2020-03-18 stsp if (err)
1077 5672d305 2020-03-18 stsp goto done;
1078 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
1079 5672d305 2020-03-18 stsp }
1080 93658fb9 2020-03-18 stsp }
1081 e70bf110 2020-03-22 stsp err = send_fetch_download_progress(ibuf, packsz);
1082 5672d305 2020-03-18 stsp if (err)
1083 5672d305 2020-03-18 stsp goto done;
1084 dc671e91 2020-03-24 stsp
1085 dc671e91 2020-03-24 stsp SHA1Final(pack_sha1, &sha1_ctx);
1086 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
1087 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
1088 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
1089 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
1090 dc671e91 2020-03-24 stsp }
1091 9ff10419 2020-03-18 stsp done:
1092 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1093 abe0f35f 2020-03-18 stsp free((void *)pe->path);
1094 abe0f35f 2020-03-18 stsp free(pe->data);
1095 abe0f35f 2020-03-18 stsp }
1096 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
1097 9ff10419 2020-03-18 stsp free(have);
1098 9ff10419 2020-03-18 stsp free(want);
1099 00cd0e0a 2020-03-18 stsp free(id_str);
1100 00cd0e0a 2020-03-18 stsp free(refname);
1101 00cd0e0a 2020-03-18 stsp free(server_capabilities);
1102 8f2d01a6 2020-03-18 stsp return err;
1103 93658fb9 2020-03-18 stsp }
1104 93658fb9 2020-03-18 stsp
1105 93658fb9 2020-03-18 stsp
1106 93658fb9 2020-03-18 stsp int
1107 93658fb9 2020-03-18 stsp main(int argc, char **argv)
1108 93658fb9 2020-03-18 stsp {
1109 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
1110 7848a0e1 2020-03-19 stsp int fetchfd, packfd = -1, i;
1111 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
1112 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
1113 93658fb9 2020-03-18 stsp struct imsg imsg;
1114 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
1115 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
1116 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
1117 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1118 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
1119 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
1120 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
1121 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
1122 4ba14133 2020-03-20 stsp size_t datalen;
1123 7848a0e1 2020-03-19 stsp #if 0
1124 7848a0e1 2020-03-19 stsp static int attached;
1125 7848a0e1 2020-03-19 stsp while (!attached)
1126 7848a0e1 2020-03-19 stsp sleep (1);
1127 7848a0e1 2020-03-19 stsp #endif
1128 33501562 2020-03-18 stsp
1129 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
1130 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1131 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1132 858b0dfb 2020-03-20 stsp
1133 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1134 ffb5f621 2020-03-18 stsp #ifndef PROFILE
1135 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
1136 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
1137 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
1138 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1139 ffb5f621 2020-03-18 stsp return 1;
1140 ffb5f621 2020-03-18 stsp }
1141 ffb5f621 2020-03-18 stsp #endif
1142 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1143 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1144 93658fb9 2020-03-18 stsp err = NULL;
1145 93658fb9 2020-03-18 stsp goto done;
1146 93658fb9 2020-03-18 stsp }
1147 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1148 93658fb9 2020-03-18 stsp goto done;
1149 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
1150 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1151 93658fb9 2020-03-18 stsp goto done;
1152 93658fb9 2020-03-18 stsp }
1153 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1154 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
1155 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1156 93658fb9 2020-03-18 stsp goto done;
1157 93658fb9 2020-03-18 stsp }
1158 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
1159 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
1160 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1161 4ba14133 2020-03-20 stsp
1162 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
1163 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
1164 2690194b 2020-03-21 stsp
1165 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
1166 7848a0e1 2020-03-19 stsp struct got_object_id *id;
1167 7848a0e1 2020-03-19 stsp char *refname;
1168 7848a0e1 2020-03-19 stsp
1169 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1170 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1171 4ba14133 2020-03-20 stsp err = NULL;
1172 4ba14133 2020-03-20 stsp goto done;
1173 4ba14133 2020-03-20 stsp }
1174 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1175 4ba14133 2020-03-20 stsp goto done;
1176 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
1177 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1178 4ba14133 2020-03-20 stsp goto done;
1179 4ba14133 2020-03-20 stsp }
1180 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1181 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
1182 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1183 659e7fbd 2020-03-20 stsp goto done;
1184 659e7fbd 2020-03-20 stsp }
1185 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
1186 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
1187 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1188 7848a0e1 2020-03-19 stsp goto done;
1189 7848a0e1 2020-03-19 stsp }
1190 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
1191 7848a0e1 2020-03-19 stsp if (refname == NULL) {
1192 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
1193 7848a0e1 2020-03-19 stsp goto done;
1194 7848a0e1 2020-03-19 stsp }
1195 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
1196 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
1197 659e7fbd 2020-03-20 stsp
1198 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
1199 7848a0e1 2020-03-19 stsp if (id == NULL) {
1200 7848a0e1 2020-03-19 stsp free(refname);
1201 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
1202 7848a0e1 2020-03-19 stsp goto done;
1203 7848a0e1 2020-03-19 stsp }
1204 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1205 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
1206 7848a0e1 2020-03-19 stsp if (err) {
1207 7848a0e1 2020-03-19 stsp free(refname);
1208 7848a0e1 2020-03-19 stsp free(id);
1209 7848a0e1 2020-03-19 stsp goto done;
1210 7848a0e1 2020-03-19 stsp }
1211 4ba14133 2020-03-20 stsp
1212 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1213 659e7fbd 2020-03-20 stsp }
1214 93658fb9 2020-03-18 stsp
1215 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1216 4ba14133 2020-03-20 stsp char *refname;
1217 4ba14133 2020-03-20 stsp
1218 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1219 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1220 4ba14133 2020-03-20 stsp err = NULL;
1221 4ba14133 2020-03-20 stsp goto done;
1222 4ba14133 2020-03-20 stsp }
1223 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1224 4ba14133 2020-03-20 stsp goto done;
1225 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1226 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1227 4ba14133 2020-03-20 stsp goto done;
1228 4ba14133 2020-03-20 stsp }
1229 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1230 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
1231 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1232 4ba14133 2020-03-20 stsp goto done;
1233 4ba14133 2020-03-20 stsp }
1234 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
1235 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
1236 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1237 4ba14133 2020-03-20 stsp goto done;
1238 4ba14133 2020-03-20 stsp }
1239 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
1240 4ba14133 2020-03-20 stsp if (refname == NULL) {
1241 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
1242 4ba14133 2020-03-20 stsp goto done;
1243 4ba14133 2020-03-20 stsp }
1244 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1245 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
1246 4ba14133 2020-03-20 stsp
1247 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
1248 4ba14133 2020-03-20 stsp if (err) {
1249 4ba14133 2020-03-20 stsp free(refname);
1250 4ba14133 2020-03-20 stsp goto done;
1251 4ba14133 2020-03-20 stsp }
1252 4ba14133 2020-03-20 stsp
1253 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1254 4ba14133 2020-03-20 stsp }
1255 4ba14133 2020-03-20 stsp
1256 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1257 0e4002ca 2020-03-21 stsp char *refname;
1258 0e4002ca 2020-03-21 stsp
1259 0e4002ca 2020-03-21 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1260 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1261 0e4002ca 2020-03-21 stsp err = NULL;
1262 0e4002ca 2020-03-21 stsp goto done;
1263 0e4002ca 2020-03-21 stsp }
1264 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1265 0e4002ca 2020-03-21 stsp goto done;
1266 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1267 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1268 0e4002ca 2020-03-21 stsp goto done;
1269 0e4002ca 2020-03-21 stsp }
1270 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1271 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
1272 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1273 0e4002ca 2020-03-21 stsp goto done;
1274 0e4002ca 2020-03-21 stsp }
1275 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
1276 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
1277 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1278 0e4002ca 2020-03-21 stsp goto done;
1279 0e4002ca 2020-03-21 stsp }
1280 0e4002ca 2020-03-21 stsp refname = malloc(wref.name_len + 1);
1281 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1282 0e4002ca 2020-03-21 stsp err = got_error_from_errno("malloc");
1283 0e4002ca 2020-03-21 stsp goto done;
1284 0e4002ca 2020-03-21 stsp }
1285 0e4002ca 2020-03-21 stsp memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1286 0e4002ca 2020-03-21 stsp refname[wref.name_len] = '\0';
1287 0e4002ca 2020-03-21 stsp
1288 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1289 0e4002ca 2020-03-21 stsp if (err) {
1290 0e4002ca 2020-03-21 stsp free(refname);
1291 0e4002ca 2020-03-21 stsp goto done;
1292 0e4002ca 2020-03-21 stsp }
1293 0e4002ca 2020-03-21 stsp
1294 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1295 0e4002ca 2020-03-21 stsp }
1296 0e4002ca 2020-03-21 stsp
1297 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
1298 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1299 93658fb9 2020-03-18 stsp err = NULL;
1300 93658fb9 2020-03-18 stsp goto done;
1301 93658fb9 2020-03-18 stsp }
1302 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1303 93658fb9 2020-03-18 stsp goto done;
1304 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1305 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1306 93658fb9 2020-03-18 stsp goto done;
1307 93658fb9 2020-03-18 stsp }
1308 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1309 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1310 93658fb9 2020-03-18 stsp goto done;
1311 93658fb9 2020-03-18 stsp }
1312 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1313 93658fb9 2020-03-18 stsp
1314 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1315 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1316 0e4002ca 2020-03-21 stsp &wanted_refs, fetch_req.list_refs_only, &ibuf);
1317 93658fb9 2020-03-18 stsp done:
1318 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
1319 7848a0e1 2020-03-19 stsp free((char *)pe->path);
1320 7848a0e1 2020-03-19 stsp free(pe->data);
1321 7848a0e1 2020-03-19 stsp }
1322 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1323 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1324 4ba14133 2020-03-20 stsp free((char *)pe->path);
1325 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1326 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1327 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1328 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1329 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1330 9ff10419 2020-03-18 stsp if (err != NULL)
1331 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1332 93658fb9 2020-03-18 stsp else
1333 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1334 cf875574 2020-03-18 stsp if (err != NULL) {
1335 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1336 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1337 93658fb9 2020-03-18 stsp }
1338 93658fb9 2020-03-18 stsp
1339 93658fb9 2020-03-18 stsp exit(0);
1340 93658fb9 2020-03-18 stsp }