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 f1c6967f 2020-03-19 stsp #include "got_fetch.h"
43 659e7fbd 2020-03-20 stsp #include "got_reference.h"
44 93658fb9 2020-03-18 stsp
45 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
50 0872c0b0 2020-03-18 stsp #include "got_lib_pack.h"
51 93658fb9 2020-03-18 stsp
52 8a29a085 2020-03-18 stsp #ifndef nitems
53 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 8a29a085 2020-03-18 stsp #endif
55 8a29a085 2020-03-18 stsp
56 93658fb9 2020-03-18 stsp struct got_object *indexed;
57 858b0dfb 2020-03-20 stsp static int chattygot;
58 93658fb9 2020-03-18 stsp static struct got_object_id zhash = {.sha1={0}};
59 93658fb9 2020-03-18 stsp
60 fe53745c 2020-03-18 stsp static const struct got_error *
61 fe53745c 2020-03-18 stsp readn(ssize_t *off, int fd, void *buf, size_t n)
62 93658fb9 2020-03-18 stsp {
63 fe53745c 2020-03-18 stsp ssize_t r;
64 93658fb9 2020-03-18 stsp
65 fe53745c 2020-03-18 stsp *off = 0;
66 fe53745c 2020-03-18 stsp while (*off != n) {
67 fe53745c 2020-03-18 stsp r = read(fd, buf + *off, n - *off);
68 fe53745c 2020-03-18 stsp if (r == -1)
69 fe53745c 2020-03-18 stsp return got_error_from_errno("read");
70 93658fb9 2020-03-18 stsp if (r == 0)
71 fe53745c 2020-03-18 stsp return NULL;
72 fe53745c 2020-03-18 stsp *off += r;
73 93658fb9 2020-03-18 stsp }
74 fe53745c 2020-03-18 stsp return NULL;
75 93658fb9 2020-03-18 stsp }
76 93658fb9 2020-03-18 stsp
77 38c670f1 2020-03-18 stsp static const struct got_error *
78 93658fb9 2020-03-18 stsp flushpkt(int fd)
79 93658fb9 2020-03-18 stsp {
80 38c670f1 2020-03-18 stsp ssize_t w;
81 38c670f1 2020-03-18 stsp
82 858b0dfb 2020-03-20 stsp if (chattygot)
83 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
84 858b0dfb 2020-03-20 stsp
85 38c670f1 2020-03-18 stsp w = write(fd, "0000", 4);
86 38c670f1 2020-03-18 stsp if (w == -1)
87 38c670f1 2020-03-18 stsp return got_error_from_errno("write");
88 38c670f1 2020-03-18 stsp if (w != 4)
89 38c670f1 2020-03-18 stsp return got_error(GOT_ERR_IO);
90 38c670f1 2020-03-18 stsp return NULL;
91 93658fb9 2020-03-18 stsp }
92 93658fb9 2020-03-18 stsp
93 531c3985 2020-03-18 stsp /*
94 531c3985 2020-03-18 stsp * Packet header contains a 4-byte hexstring which specifies the length
95 531c3985 2020-03-18 stsp * of data which follows.
96 531c3985 2020-03-18 stsp */
97 fe53745c 2020-03-18 stsp static const struct got_error *
98 531c3985 2020-03-18 stsp read_pkthdr(int *datalen, int fd)
99 93658fb9 2020-03-18 stsp {
100 531c3985 2020-03-18 stsp static const struct got_error *err = NULL;
101 4dc8ee09 2020-03-18 stsp char lenstr[5];
102 4dc8ee09 2020-03-18 stsp long len;
103 93658fb9 2020-03-18 stsp char *e;
104 54d1a70f 2020-03-18 stsp int n, i;
105 fe53745c 2020-03-18 stsp ssize_t r;
106 93658fb9 2020-03-18 stsp
107 531c3985 2020-03-18 stsp *datalen = 0;
108 fe53745c 2020-03-18 stsp
109 4dc8ee09 2020-03-18 stsp err = readn(&r, fd, lenstr, 4);
110 fe53745c 2020-03-18 stsp if (err)
111 fe53745c 2020-03-18 stsp return err;
112 858b0dfb 2020-03-20 stsp if (r == 0) {
113 858b0dfb 2020-03-20 stsp /* implicit "0000" */
114 858b0dfb 2020-03-20 stsp if (chattygot)
115 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
116 531c3985 2020-03-18 stsp return NULL;
117 858b0dfb 2020-03-20 stsp }
118 4dc8ee09 2020-03-18 stsp if (r != 4)
119 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
120 531c3985 2020-03-18 stsp "wrong packet header length");
121 fe53745c 2020-03-18 stsp
122 4dc8ee09 2020-03-18 stsp lenstr[4] = '\0';
123 54d1a70f 2020-03-18 stsp for (i = 0; i < 4; i++) {
124 858b0dfb 2020-03-20 stsp if (!isprint((unsigned char)lenstr[i]))
125 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
126 858b0dfb 2020-03-20 stsp "unprintable character in packet length field");
127 858b0dfb 2020-03-20 stsp }
128 858b0dfb 2020-03-20 stsp for (i = 0; i < 4; i++) {
129 858b0dfb 2020-03-20 stsp if (!isxdigit((unsigned char)lenstr[i])) {
130 858b0dfb 2020-03-20 stsp if (chattygot)
131 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: bad length: '%s'\n",
132 858b0dfb 2020-03-20 stsp getprogname(), lenstr);
133 858b0dfb 2020-03-20 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
134 531c3985 2020-03-18 stsp "packet length not specified in hex");
135 858b0dfb 2020-03-20 stsp }
136 54d1a70f 2020-03-18 stsp }
137 4dc8ee09 2020-03-18 stsp errno = 0;
138 4dc8ee09 2020-03-18 stsp len = strtol(lenstr, &e, 16);
139 4dc8ee09 2020-03-18 stsp if (lenstr[0] == '\0' || *e != '\0')
140 4dc8ee09 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKET);
141 4dc8ee09 2020-03-18 stsp if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
142 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
143 4dc8ee09 2020-03-18 stsp if (len > INT_MAX || len < INT_MIN)
144 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
145 4dc8ee09 2020-03-18 stsp n = len;
146 531c3985 2020-03-18 stsp if (n == 0)
147 fe53745c 2020-03-18 stsp return NULL;
148 4dc8ee09 2020-03-18 stsp if (n <= 4)
149 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
150 93658fb9 2020-03-18 stsp n -= 4;
151 531c3985 2020-03-18 stsp
152 531c3985 2020-03-18 stsp *datalen = n;
153 531c3985 2020-03-18 stsp return NULL;
154 531c3985 2020-03-18 stsp }
155 531c3985 2020-03-18 stsp
156 531c3985 2020-03-18 stsp static const struct got_error *
157 531c3985 2020-03-18 stsp readpkt(int *outlen, int fd, char *buf, int buflen)
158 531c3985 2020-03-18 stsp {
159 531c3985 2020-03-18 stsp const struct got_error *err = NULL;
160 858b0dfb 2020-03-20 stsp int datalen, i;
161 531c3985 2020-03-18 stsp ssize_t n;
162 531c3985 2020-03-18 stsp
163 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
164 531c3985 2020-03-18 stsp if (err)
165 531c3985 2020-03-18 stsp return err;
166 531c3985 2020-03-18 stsp
167 531c3985 2020-03-18 stsp if (datalen > buflen)
168 fe53745c 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
169 fe53745c 2020-03-18 stsp
170 531c3985 2020-03-18 stsp err = readn(&n, fd, buf, datalen);
171 fe53745c 2020-03-18 stsp if (err)
172 fe53745c 2020-03-18 stsp return err;
173 531c3985 2020-03-18 stsp if (n != datalen)
174 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
175 fe53745c 2020-03-18 stsp
176 858b0dfb 2020-03-20 stsp if (chattygot) {
177 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
178 858b0dfb 2020-03-20 stsp for (i = 0; i < n; i++) {
179 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
180 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
181 858b0dfb 2020-03-20 stsp else
182 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
183 858b0dfb 2020-03-20 stsp }
184 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
185 858b0dfb 2020-03-20 stsp }
186 858b0dfb 2020-03-20 stsp
187 fe53745c 2020-03-18 stsp *outlen = n;
188 fe53745c 2020-03-18 stsp return NULL;
189 93658fb9 2020-03-18 stsp }
190 93658fb9 2020-03-18 stsp
191 344e4747 2020-03-18 stsp static const struct got_error *
192 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
193 93658fb9 2020-03-18 stsp {
194 93658fb9 2020-03-18 stsp char len[5];
195 858b0dfb 2020-03-20 stsp int i;
196 344e4747 2020-03-18 stsp ssize_t w;
197 93658fb9 2020-03-18 stsp
198 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
199 344e4747 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
200 344e4747 2020-03-18 stsp w = write(fd, len, 4);
201 344e4747 2020-03-18 stsp if (w == -1)
202 344e4747 2020-03-18 stsp return got_error_from_errno("write");
203 344e4747 2020-03-18 stsp if (w != 4)
204 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
205 344e4747 2020-03-18 stsp w = write(fd, buf, nbuf);
206 344e4747 2020-03-18 stsp if (w == -1)
207 344e4747 2020-03-18 stsp return got_error_from_errno("write");
208 344e4747 2020-03-18 stsp if (w != nbuf)
209 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
210 858b0dfb 2020-03-20 stsp if (chattygot) {
211 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
212 858b0dfb 2020-03-20 stsp for (i = 0; i < nbuf; i++) {
213 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
214 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
215 858b0dfb 2020-03-20 stsp else
216 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
217 858b0dfb 2020-03-20 stsp }
218 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
219 858b0dfb 2020-03-20 stsp }
220 344e4747 2020-03-18 stsp return NULL;
221 93658fb9 2020-03-18 stsp }
222 93658fb9 2020-03-18 stsp
223 7848a0e1 2020-03-19 stsp static void
224 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
225 7848a0e1 2020-03-19 stsp struct got_object_id *my_id, char *refname)
226 93658fb9 2020-03-18 stsp {
227 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
228 93658fb9 2020-03-18 stsp
229 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
230 7848a0e1 2020-03-19 stsp * we should use a flag instead */
231 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
232 93658fb9 2020-03-18 stsp
233 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
234 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
235 7848a0e1 2020-03-19 stsp if (strcmp(pe->path, refname) == 0) {
236 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
237 33501562 2020-03-18 stsp break;
238 33501562 2020-03-18 stsp }
239 93658fb9 2020-03-18 stsp }
240 93658fb9 2020-03-18 stsp }
241 93658fb9 2020-03-18 stsp
242 93658fb9 2020-03-18 stsp static int
243 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
244 93658fb9 2020-03-18 stsp {
245 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
246 659e7fbd 2020-03-20 stsp return 0;
247 93658fb9 2020-03-18 stsp
248 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
249 659e7fbd 2020-03-20 stsp wanted_branch += 11;
250 659e7fbd 2020-03-20 stsp
251 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
252 93658fb9 2020-03-18 stsp }
253 93658fb9 2020-03-18 stsp
254 0d0a341c 2020-03-18 stsp static const struct got_error *
255 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
256 93658fb9 2020-03-18 stsp {
257 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
258 93658fb9 2020-03-18 stsp char *p;
259 0d0a341c 2020-03-18 stsp size_t i, n = 0;
260 93658fb9 2020-03-18 stsp
261 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
262 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
263 0d0a341c 2020-03-18 stsp
264 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
265 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
266 93658fb9 2020-03-18 stsp line++;
267 0d0a341c 2020-03-18 stsp n++;
268 0d0a341c 2020-03-18 stsp }
269 93658fb9 2020-03-18 stsp p = line;
270 0d0a341c 2020-03-18 stsp while (*line != '\0' &&
271 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
272 93658fb9 2020-03-18 stsp line++;
273 0d0a341c 2020-03-18 stsp n++;
274 0d0a341c 2020-03-18 stsp }
275 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
276 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
277 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
278 0d0a341c 2020-03-18 stsp goto done;
279 0d0a341c 2020-03-18 stsp }
280 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
281 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
282 0d0a341c 2020-03-18 stsp line++;
283 0d0a341c 2020-03-18 stsp n++;
284 0d0a341c 2020-03-18 stsp }
285 93658fb9 2020-03-18 stsp }
286 0d0a341c 2020-03-18 stsp if (i <= 2)
287 0d0a341c 2020-03-18 stsp err = got_error(GOT_ERR_NOT_REF);
288 0d0a341c 2020-03-18 stsp done:
289 0d0a341c 2020-03-18 stsp if (err) {
290 0d0a341c 2020-03-18 stsp int j;
291 0d0a341c 2020-03-18 stsp for (j = 0; j < i; j++)
292 0d0a341c 2020-03-18 stsp free(tokens[j]);
293 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
294 0d0a341c 2020-03-18 stsp }
295 0d0a341c 2020-03-18 stsp return err;
296 8a29a085 2020-03-18 stsp }
297 00cd0e0a 2020-03-18 stsp
298 00cd0e0a 2020-03-18 stsp static const struct got_error *
299 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
300 00cd0e0a 2020-03-18 stsp char *line, int len)
301 00cd0e0a 2020-03-18 stsp {
302 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
303 00cd0e0a 2020-03-18 stsp char *tokens[3];
304 8a29a085 2020-03-18 stsp
305 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
306 00cd0e0a 2020-03-18 stsp if (err)
307 00cd0e0a 2020-03-18 stsp return err;
308 00cd0e0a 2020-03-18 stsp
309 00cd0e0a 2020-03-18 stsp if (tokens[0])
310 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
311 00cd0e0a 2020-03-18 stsp if (tokens[1])
312 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
313 00cd0e0a 2020-03-18 stsp if (tokens[2])
314 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
315 00cd0e0a 2020-03-18 stsp
316 00cd0e0a 2020-03-18 stsp return NULL;
317 00cd0e0a 2020-03-18 stsp }
318 00cd0e0a 2020-03-18 stsp
319 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
320 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
321 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
322 531c3985 2020-03-18 stsp
323 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
324 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
325 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
326 531c3985 2020-03-18 stsp
327 531c3985 2020-03-18 stsp
328 8a29a085 2020-03-18 stsp struct got_capability {
329 8a29a085 2020-03-18 stsp const char *key;
330 8a29a085 2020-03-18 stsp const char *value;
331 8a29a085 2020-03-18 stsp };
332 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
333 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
334 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
335 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
336 8a29a085 2020-03-18 stsp };
337 8a29a085 2020-03-18 stsp
338 8a29a085 2020-03-18 stsp static const struct got_error *
339 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
340 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
341 8a29a085 2020-03-18 stsp {
342 8a29a085 2020-03-18 stsp char *equalsign;
343 8a29a085 2020-03-18 stsp char *s;
344 8a29a085 2020-03-18 stsp
345 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
346 8a29a085 2020-03-18 stsp if (equalsign) {
347 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
348 8a29a085 2020-03-18 stsp return NULL;
349 8a29a085 2020-03-18 stsp } else {
350 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
351 8a29a085 2020-03-18 stsp return NULL;
352 8a29a085 2020-03-18 stsp }
353 8a29a085 2020-03-18 stsp
354 406106ee 2020-03-20 stsp if (asprintf(&s, "%s %s%s%s",
355 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
356 8a29a085 2020-03-18 stsp mycapa->key,
357 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
358 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
359 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
360 8a29a085 2020-03-18 stsp
361 8a29a085 2020-03-18 stsp free(*my_capabilities);
362 8a29a085 2020-03-18 stsp *my_capabilities = s;
363 8a29a085 2020-03-18 stsp return NULL;
364 93658fb9 2020-03-18 stsp }
365 93658fb9 2020-03-18 stsp
366 9ff10419 2020-03-18 stsp static const struct got_error *
367 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
368 8a29a085 2020-03-18 stsp {
369 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
370 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
371 abe0f35f 2020-03-18 stsp
372 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
373 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
374 abe0f35f 2020-03-18 stsp return NULL;
375 abe0f35f 2020-03-18 stsp
376 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
377 abe0f35f 2020-03-18 stsp if (colon == NULL)
378 abe0f35f 2020-03-18 stsp return NULL;
379 abe0f35f 2020-03-18 stsp
380 abe0f35f 2020-03-18 stsp *colon = '\0';
381 abe0f35f 2020-03-18 stsp name = strdup(capa);
382 abe0f35f 2020-03-18 stsp if (name == NULL)
383 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
384 abe0f35f 2020-03-18 stsp
385 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
386 abe0f35f 2020-03-18 stsp if (target == NULL) {
387 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
388 abe0f35f 2020-03-18 stsp goto done;
389 abe0f35f 2020-03-18 stsp }
390 abe0f35f 2020-03-18 stsp
391 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
392 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
393 abe0f35f 2020-03-18 stsp done:
394 abe0f35f 2020-03-18 stsp if (err) {
395 abe0f35f 2020-03-18 stsp free(name);
396 abe0f35f 2020-03-18 stsp free(target);
397 abe0f35f 2020-03-18 stsp }
398 abe0f35f 2020-03-18 stsp return err;
399 abe0f35f 2020-03-18 stsp }
400 abe0f35f 2020-03-18 stsp
401 abe0f35f 2020-03-18 stsp static const struct got_error *
402 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
403 abe0f35f 2020-03-18 stsp char *server_capabilities)
404 abe0f35f 2020-03-18 stsp {
405 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
406 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
407 8a29a085 2020-03-18 stsp int i;
408 8a29a085 2020-03-18 stsp
409 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
410 8a29a085 2020-03-18 stsp do {
411 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
412 8a29a085 2020-03-18 stsp if (capa == NULL)
413 8a29a085 2020-03-18 stsp return NULL;
414 8a29a085 2020-03-18 stsp
415 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
416 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
417 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
418 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
419 abe0f35f 2020-03-18 stsp if (err)
420 abe0f35f 2020-03-18 stsp break;
421 abe0f35f 2020-03-18 stsp continue;
422 abe0f35f 2020-03-18 stsp }
423 abe0f35f 2020-03-18 stsp
424 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
425 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
426 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
427 8a29a085 2020-03-18 stsp if (err)
428 8a29a085 2020-03-18 stsp break;
429 8a29a085 2020-03-18 stsp }
430 8a29a085 2020-03-18 stsp } while (capa);
431 8a29a085 2020-03-18 stsp
432 406106ee 2020-03-20 stsp if (*my_capabilities == NULL) {
433 406106ee 2020-03-20 stsp *my_capabilities = strdup("");
434 406106ee 2020-03-20 stsp if (*my_capabilities == NULL)
435 406106ee 2020-03-20 stsp err = got_error_from_errno("strdup");
436 406106ee 2020-03-20 stsp }
437 8a29a085 2020-03-18 stsp return err;
438 531c3985 2020-03-18 stsp }
439 531c3985 2020-03-18 stsp
440 531c3985 2020-03-18 stsp static const struct got_error *
441 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
442 531c3985 2020-03-18 stsp {
443 531c3985 2020-03-18 stsp int i;
444 531c3985 2020-03-18 stsp
445 531c3985 2020-03-18 stsp if (len == 0)
446 531c3985 2020-03-18 stsp return NULL;
447 531c3985 2020-03-18 stsp
448 531c3985 2020-03-18 stsp /*
449 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
450 531c3985 2020-03-18 stsp * Server may send up to 64k.
451 531c3985 2020-03-18 stsp */
452 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
453 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
454 531c3985 2020-03-18 stsp
455 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
456 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
457 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
458 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
459 531c3985 2020-03-18 stsp continue;
460 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
461 531c3985 2020-03-18 stsp "non-printable progress message received from server");
462 531c3985 2020-03-18 stsp }
463 531c3985 2020-03-18 stsp
464 531c3985 2020-03-18 stsp return got_privsep_send_fetch_server_progress(ibuf, buf, len);
465 8a29a085 2020-03-18 stsp }
466 abe0f35f 2020-03-18 stsp
467 8a29a085 2020-03-18 stsp static const struct got_error *
468 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
469 531c3985 2020-03-18 stsp {
470 531c3985 2020-03-18 stsp static char msg[1024];
471 531c3985 2020-03-18 stsp int i;
472 531c3985 2020-03-18 stsp
473 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
474 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
475 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
476 531c3985 2020-03-18 stsp "non-printable error message received from server");
477 531c3985 2020-03-18 stsp msg[i] = buf[i];
478 531c3985 2020-03-18 stsp }
479 531c3985 2020-03-18 stsp msg[i] = '\0';
480 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
481 531c3985 2020-03-18 stsp }
482 531c3985 2020-03-18 stsp
483 531c3985 2020-03-18 stsp static const struct got_error *
484 8f2d01a6 2020-03-18 stsp fetch_pack(int fd, int packfd, struct got_object_id *packid,
485 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
486 4ba14133 2020-03-20 stsp struct got_pathlist_head *wanted_branches, struct imsgbuf *ibuf)
487 93658fb9 2020-03-18 stsp {
488 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
489 f1c6967f 2020-03-19 stsp char buf[GOT_FETCH_PKTMAX];
490 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
491 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
492 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
493 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
494 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
495 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
496 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
497 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
498 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
499 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
500 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
501 659e7fbd 2020-03-20 stsp int found_branch = 0;
502 93658fb9 2020-03-18 stsp
503 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
504 abe0f35f 2020-03-18 stsp
505 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
506 9ff10419 2020-03-18 stsp if (have == NULL)
507 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
508 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
509 9ff10419 2020-03-18 stsp if (want == NULL) {
510 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
511 9ff10419 2020-03-18 stsp goto done;
512 9ff10419 2020-03-18 stsp }
513 858b0dfb 2020-03-20 stsp if (chattygot)
514 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: starting fetch\n", getprogname());
515 9ff10419 2020-03-18 stsp while (1) {
516 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
517 fe53745c 2020-03-18 stsp if (err)
518 9ff10419 2020-03-18 stsp goto done;
519 9ff10419 2020-03-18 stsp if (n == 0)
520 93658fb9 2020-03-18 stsp break;
521 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
522 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
523 9ff10419 2020-03-18 stsp goto done;
524 9ff10419 2020-03-18 stsp }
525 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
526 00cd0e0a 2020-03-18 stsp buf, n);
527 0d0a341c 2020-03-18 stsp if (err)
528 9ff10419 2020-03-18 stsp goto done;
529 8a29a085 2020-03-18 stsp if (is_firstpkt) {
530 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
531 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
532 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
533 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
534 00cd0e0a 2020-03-18 stsp server_capabilities);
535 8a29a085 2020-03-18 stsp if (err)
536 8a29a085 2020-03-18 stsp goto done;
537 858b0dfb 2020-03-20 stsp if (chattygot)
538 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: my capabilities: %s\n",
539 858b0dfb 2020-03-20 stsp getprogname(), my_capabilities);
540 abe0f35f 2020-03-18 stsp err = got_privsep_send_fetch_symrefs(ibuf, &symrefs);
541 abe0f35f 2020-03-18 stsp if (err)
542 abe0f35f 2020-03-18 stsp goto done;
543 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
544 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
545 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
546 659e7fbd 2020-03-20 stsp const char *name = pe->path;
547 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
548 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
549 659e7fbd 2020-03-20 stsp continue;
550 659e7fbd 2020-03-20 stsp default_branch = symref_target;
551 659e7fbd 2020-03-20 stsp break;
552 659e7fbd 2020-03-20 stsp }
553 659e7fbd 2020-03-20 stsp }
554 7848a0e1 2020-03-19 stsp continue;
555 8a29a085 2020-03-18 stsp }
556 00cd0e0a 2020-03-18 stsp if (strstr(refname, "^{}"))
557 93658fb9 2020-03-18 stsp continue;
558 659e7fbd 2020-03-20 stsp
559 659e7fbd 2020-03-20 stsp if (chattygot)
560 659e7fbd 2020-03-20 stsp fprintf(stderr, "%s: discovered remote ref %s\n",
561 659e7fbd 2020-03-20 stsp getprogname(), refname);
562 659e7fbd 2020-03-20 stsp
563 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
564 4ba14133 2020-03-20 stsp if (fetch_all_branches) {
565 4ba14133 2020-03-20 stsp found_branch = 1;
566 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
567 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
568 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
569 4ba14133 2020-03-20 stsp break;
570 4ba14133 2020-03-20 stsp }
571 4ba14133 2020-03-20 stsp if (pe == NULL)
572 4ba14133 2020-03-20 stsp continue;
573 4ba14133 2020-03-20 stsp found_branch = 1;
574 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
575 4ba14133 2020-03-20 stsp if (!match_branch(refname, default_branch))
576 4ba14133 2020-03-20 stsp continue;
577 4ba14133 2020-03-20 stsp found_branch = 1;
578 4ba14133 2020-03-20 stsp }
579 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
580 659e7fbd 2020-03-20 stsp if (chattygot) {
581 659e7fbd 2020-03-20 stsp fprintf(stderr, "%s: ignoring '%s' which is "
582 659e7fbd 2020-03-20 stsp "neither a branch nor a tag\n",
583 659e7fbd 2020-03-20 stsp getprogname(), refname);
584 659e7fbd 2020-03-20 stsp }
585 93658fb9 2020-03-18 stsp continue;
586 659e7fbd 2020-03-20 stsp }
587 659e7fbd 2020-03-20 stsp
588 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
589 93658fb9 2020-03-18 stsp refsz *= 2;
590 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
591 9ff10419 2020-03-18 stsp if (have == NULL) {
592 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
593 9ff10419 2020-03-18 stsp goto done;
594 9ff10419 2020-03-18 stsp }
595 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
596 9ff10419 2020-03-18 stsp if (want == NULL) {
597 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
598 9ff10419 2020-03-18 stsp goto done;
599 9ff10419 2020-03-18 stsp }
600 93658fb9 2020-03-18 stsp }
601 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
602 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
603 9ff10419 2020-03-18 stsp goto done;
604 9ff10419 2020-03-18 stsp }
605 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
606 ea7396b9 2020-03-18 stsp err = got_privsep_send_fetch_ref(ibuf, &want[nref],
607 00cd0e0a 2020-03-18 stsp refname);
608 8f2d01a6 2020-03-18 stsp if (err)
609 8f2d01a6 2020-03-18 stsp goto done;
610 858b0dfb 2020-03-20 stsp
611 858b0dfb 2020-03-20 stsp if (chattygot) {
612 858b0dfb 2020-03-20 stsp char *theirs, *mine;
613 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
614 858b0dfb 2020-03-20 stsp if (err)
615 858b0dfb 2020-03-20 stsp goto done;
616 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
617 858b0dfb 2020-03-20 stsp if (err) {
618 858b0dfb 2020-03-20 stsp free(theirs);
619 858b0dfb 2020-03-20 stsp goto done;
620 858b0dfb 2020-03-20 stsp }
621 659e7fbd 2020-03-20 stsp fprintf(stderr, "%s: %s will be fetched\n",
622 858b0dfb 2020-03-20 stsp getprogname(), refname);
623 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: theirs=%s\n%s: mine=%s\n",
624 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
625 858b0dfb 2020-03-20 stsp free(theirs);
626 858b0dfb 2020-03-20 stsp free(mine);
627 858b0dfb 2020-03-20 stsp }
628 93658fb9 2020-03-18 stsp nref++;
629 93658fb9 2020-03-18 stsp }
630 93658fb9 2020-03-18 stsp
631 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
632 659e7fbd 2020-03-20 stsp if (!found_branch) {
633 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
634 659e7fbd 2020-03-20 stsp goto done;
635 659e7fbd 2020-03-20 stsp }
636 659e7fbd 2020-03-20 stsp
637 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
638 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
639 93658fb9 2020-03-18 stsp continue;
640 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
641 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
642 406106ee 2020-03-20 stsp sent_my_capabilites ? "" : my_capabilities);
643 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
644 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
645 9ff10419 2020-03-18 stsp goto done;
646 9ff10419 2020-03-18 stsp }
647 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
648 344e4747 2020-03-18 stsp if (err)
649 9ff10419 2020-03-18 stsp goto done;
650 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
651 7848a0e1 2020-03-19 stsp nwant++;
652 93658fb9 2020-03-18 stsp }
653 38c670f1 2020-03-18 stsp err = flushpkt(fd);
654 38c670f1 2020-03-18 stsp if (err)
655 38c670f1 2020-03-18 stsp goto done;
656 7848a0e1 2020-03-19 stsp
657 3c912d14 2020-03-19 stsp if (nwant == 0)
658 7848a0e1 2020-03-19 stsp goto done;
659 7848a0e1 2020-03-19 stsp
660 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
661 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
662 93658fb9 2020-03-18 stsp continue;
663 7848a0e1 2020-03-19 stsp got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
664 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
665 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
666 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
667 9ff10419 2020-03-18 stsp goto done;
668 9ff10419 2020-03-18 stsp }
669 c20695fb 2020-03-20 stsp err = writepkt(fd, buf, n);
670 344e4747 2020-03-18 stsp if (err)
671 9ff10419 2020-03-18 stsp goto done;
672 7848a0e1 2020-03-19 stsp nhave++;
673 93658fb9 2020-03-18 stsp }
674 7848a0e1 2020-03-19 stsp
675 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
676 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
677 7848a0e1 2020-03-19 stsp
678 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
679 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
680 38c670f1 2020-03-18 stsp if (err)
681 38c670f1 2020-03-18 stsp goto done;
682 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
683 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
684 7848a0e1 2020-03-19 stsp goto done;
685 7848a0e1 2020-03-19 stsp }
686 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
687 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
688 7848a0e1 2020-03-19 stsp continue;
689 7848a0e1 2020-03-19 stsp }
690 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
691 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
692 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
693 7848a0e1 2020-03-19 stsp "unexpected message from server");
694 7848a0e1 2020-03-19 stsp goto done;
695 7848a0e1 2020-03-19 stsp }
696 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
697 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
698 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
699 7848a0e1 2020-03-19 stsp goto done;
700 7848a0e1 2020-03-19 stsp }
701 7848a0e1 2020-03-19 stsp acked++;
702 93658fb9 2020-03-18 stsp }
703 7848a0e1 2020-03-19 stsp
704 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
705 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
706 344e4747 2020-03-18 stsp if (err)
707 9ff10419 2020-03-18 stsp goto done;
708 93658fb9 2020-03-18 stsp
709 7848a0e1 2020-03-19 stsp if (nhave == 0) {
710 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
711 7848a0e1 2020-03-19 stsp if (err)
712 7848a0e1 2020-03-19 stsp goto done;
713 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
714 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
715 7848a0e1 2020-03-19 stsp "unexpected message from server");
716 7848a0e1 2020-03-19 stsp goto done;
717 7848a0e1 2020-03-19 stsp }
718 04c53c18 2020-03-18 stsp }
719 93658fb9 2020-03-18 stsp
720 858b0dfb 2020-03-20 stsp if (chattygot)
721 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
722 858b0dfb 2020-03-20 stsp
723 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
724 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
725 531c3985 2020-03-18 stsp have_sidebands = 1;
726 531c3985 2020-03-18 stsp
727 9ff10419 2020-03-18 stsp while (1) {
728 531c3985 2020-03-18 stsp ssize_t r = 0, w;
729 531c3985 2020-03-18 stsp int datalen = -1;
730 531c3985 2020-03-18 stsp
731 531c3985 2020-03-18 stsp if (have_sidebands) {
732 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
733 531c3985 2020-03-18 stsp if (err)
734 531c3985 2020-03-18 stsp goto done;
735 531c3985 2020-03-18 stsp if (datalen <= 0)
736 531c3985 2020-03-18 stsp break;
737 531c3985 2020-03-18 stsp
738 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
739 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
740 531c3985 2020-03-18 stsp if (r == -1) {
741 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
742 531c3985 2020-03-18 stsp goto done;
743 531c3985 2020-03-18 stsp }
744 531c3985 2020-03-18 stsp if (r != 1) {
745 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
746 531c3985 2020-03-18 stsp "short packet");
747 531c3985 2020-03-18 stsp goto done;
748 531c3985 2020-03-18 stsp }
749 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
750 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
751 531c3985 2020-03-18 stsp "bad packet length");
752 531c3985 2020-03-18 stsp goto done;
753 531c3985 2020-03-18 stsp }
754 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
755 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
756 531c3985 2020-03-18 stsp /* Read packfile data. */
757 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
758 531c3985 2020-03-18 stsp if (err)
759 531c3985 2020-03-18 stsp goto done;
760 531c3985 2020-03-18 stsp if (r != datalen) {
761 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
762 531c3985 2020-03-18 stsp "packet too short");
763 531c3985 2020-03-18 stsp goto done;
764 531c3985 2020-03-18 stsp }
765 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
766 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
767 531c3985 2020-03-18 stsp if (err)
768 531c3985 2020-03-18 stsp goto done;
769 531c3985 2020-03-18 stsp if (r != datalen) {
770 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
771 531c3985 2020-03-18 stsp "packet too short");
772 531c3985 2020-03-18 stsp goto done;
773 531c3985 2020-03-18 stsp }
774 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
775 531c3985 2020-03-18 stsp if (err)
776 531c3985 2020-03-18 stsp goto done;
777 531c3985 2020-03-18 stsp continue;
778 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
779 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
780 531c3985 2020-03-18 stsp if (err)
781 531c3985 2020-03-18 stsp goto done;
782 531c3985 2020-03-18 stsp if (r != datalen) {
783 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
784 531c3985 2020-03-18 stsp "packet too short");
785 531c3985 2020-03-18 stsp goto done;
786 531c3985 2020-03-18 stsp }
787 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
788 531c3985 2020-03-18 stsp goto done;
789 531c3985 2020-03-18 stsp } else {
790 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
791 531c3985 2020-03-18 stsp "unknown side-band received from server");
792 531c3985 2020-03-18 stsp goto done;
793 531c3985 2020-03-18 stsp }
794 531c3985 2020-03-18 stsp } else {
795 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
796 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
797 531c3985 2020-03-18 stsp if (err)
798 531c3985 2020-03-18 stsp goto done;
799 531c3985 2020-03-18 stsp if (r <= 0)
800 531c3985 2020-03-18 stsp break;
801 531c3985 2020-03-18 stsp }
802 531c3985 2020-03-18 stsp
803 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
804 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
805 9ff10419 2020-03-18 stsp if (w == -1) {
806 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
807 9ff10419 2020-03-18 stsp goto done;
808 9ff10419 2020-03-18 stsp }
809 fe53745c 2020-03-18 stsp if (w != r) {
810 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
811 9ff10419 2020-03-18 stsp goto done;
812 9ff10419 2020-03-18 stsp }
813 531c3985 2020-03-18 stsp packsz += w;
814 5672d305 2020-03-18 stsp
815 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
816 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
817 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf,
818 5672d305 2020-03-18 stsp packsz);
819 5672d305 2020-03-18 stsp if (err)
820 5672d305 2020-03-18 stsp goto done;
821 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
822 5672d305 2020-03-18 stsp }
823 93658fb9 2020-03-18 stsp }
824 5672d305 2020-03-18 stsp err = got_privsep_send_fetch_download_progress(ibuf, packsz);
825 5672d305 2020-03-18 stsp if (err)
826 5672d305 2020-03-18 stsp goto done;
827 9ff10419 2020-03-18 stsp done:
828 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
829 abe0f35f 2020-03-18 stsp free((void *)pe->path);
830 abe0f35f 2020-03-18 stsp free(pe->data);
831 abe0f35f 2020-03-18 stsp }
832 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
833 9ff10419 2020-03-18 stsp free(have);
834 9ff10419 2020-03-18 stsp free(want);
835 00cd0e0a 2020-03-18 stsp free(id_str);
836 00cd0e0a 2020-03-18 stsp free(refname);
837 00cd0e0a 2020-03-18 stsp free(server_capabilities);
838 8f2d01a6 2020-03-18 stsp return err;
839 93658fb9 2020-03-18 stsp }
840 93658fb9 2020-03-18 stsp
841 93658fb9 2020-03-18 stsp
842 93658fb9 2020-03-18 stsp int
843 93658fb9 2020-03-18 stsp main(int argc, char **argv)
844 93658fb9 2020-03-18 stsp {
845 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
846 7848a0e1 2020-03-19 stsp int fetchfd, packfd = -1, i;
847 93658fb9 2020-03-18 stsp struct got_object_id packid;
848 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
849 93658fb9 2020-03-18 stsp struct imsg imsg;
850 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
851 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
852 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
853 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
854 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
855 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
856 4ba14133 2020-03-20 stsp size_t datalen;
857 7848a0e1 2020-03-19 stsp #if 0
858 7848a0e1 2020-03-19 stsp static int attached;
859 7848a0e1 2020-03-19 stsp while (!attached)
860 7848a0e1 2020-03-19 stsp sleep (1);
861 7848a0e1 2020-03-19 stsp #endif
862 33501562 2020-03-18 stsp
863 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
864 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
865 93658fb9 2020-03-18 stsp
866 858b0dfb 2020-03-20 stsp if (getenv("GOT_FETCH_DEBUG") != NULL) {
867 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s being chatty!\n", getprogname());
868 858b0dfb 2020-03-20 stsp chattygot = 1;
869 858b0dfb 2020-03-20 stsp }
870 858b0dfb 2020-03-20 stsp
871 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
872 ffb5f621 2020-03-18 stsp #ifndef PROFILE
873 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
874 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
875 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
876 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
877 ffb5f621 2020-03-18 stsp return 1;
878 ffb5f621 2020-03-18 stsp }
879 ffb5f621 2020-03-18 stsp #endif
880 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
881 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
882 93658fb9 2020-03-18 stsp err = NULL;
883 93658fb9 2020-03-18 stsp goto done;
884 93658fb9 2020-03-18 stsp }
885 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
886 93658fb9 2020-03-18 stsp goto done;
887 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
888 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
889 93658fb9 2020-03-18 stsp goto done;
890 93658fb9 2020-03-18 stsp }
891 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
892 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
893 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
894 93658fb9 2020-03-18 stsp goto done;
895 93658fb9 2020-03-18 stsp }
896 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
897 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
898 4ba14133 2020-03-20 stsp imsg_free(&imsg);
899 4ba14133 2020-03-20 stsp
900 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
901 7848a0e1 2020-03-19 stsp struct got_object_id *id;
902 7848a0e1 2020-03-19 stsp char *refname;
903 7848a0e1 2020-03-19 stsp
904 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
905 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
906 4ba14133 2020-03-20 stsp err = NULL;
907 4ba14133 2020-03-20 stsp goto done;
908 4ba14133 2020-03-20 stsp }
909 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
910 4ba14133 2020-03-20 stsp goto done;
911 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
912 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
913 4ba14133 2020-03-20 stsp goto done;
914 4ba14133 2020-03-20 stsp }
915 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
916 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
917 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
918 659e7fbd 2020-03-20 stsp goto done;
919 659e7fbd 2020-03-20 stsp }
920 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
921 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
922 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
923 7848a0e1 2020-03-19 stsp goto done;
924 7848a0e1 2020-03-19 stsp }
925 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
926 7848a0e1 2020-03-19 stsp if (refname == NULL) {
927 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
928 7848a0e1 2020-03-19 stsp goto done;
929 7848a0e1 2020-03-19 stsp }
930 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
931 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
932 659e7fbd 2020-03-20 stsp
933 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
934 7848a0e1 2020-03-19 stsp if (id == NULL) {
935 7848a0e1 2020-03-19 stsp free(refname);
936 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
937 7848a0e1 2020-03-19 stsp goto done;
938 7848a0e1 2020-03-19 stsp }
939 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
940 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
941 7848a0e1 2020-03-19 stsp if (err) {
942 7848a0e1 2020-03-19 stsp free(refname);
943 7848a0e1 2020-03-19 stsp free(id);
944 7848a0e1 2020-03-19 stsp goto done;
945 7848a0e1 2020-03-19 stsp }
946 4ba14133 2020-03-20 stsp
947 4ba14133 2020-03-20 stsp imsg_free(&imsg);
948 659e7fbd 2020-03-20 stsp }
949 93658fb9 2020-03-18 stsp
950 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
951 4ba14133 2020-03-20 stsp char *refname;
952 4ba14133 2020-03-20 stsp
953 4ba14133 2020-03-20 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
954 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
955 4ba14133 2020-03-20 stsp err = NULL;
956 4ba14133 2020-03-20 stsp goto done;
957 4ba14133 2020-03-20 stsp }
958 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
959 4ba14133 2020-03-20 stsp goto done;
960 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
961 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
962 4ba14133 2020-03-20 stsp goto done;
963 4ba14133 2020-03-20 stsp }
964 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
965 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
966 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
967 4ba14133 2020-03-20 stsp goto done;
968 4ba14133 2020-03-20 stsp }
969 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
970 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
971 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
972 4ba14133 2020-03-20 stsp goto done;
973 4ba14133 2020-03-20 stsp }
974 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
975 4ba14133 2020-03-20 stsp if (refname == NULL) {
976 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
977 4ba14133 2020-03-20 stsp goto done;
978 4ba14133 2020-03-20 stsp }
979 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
980 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
981 4ba14133 2020-03-20 stsp
982 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
983 4ba14133 2020-03-20 stsp if (err) {
984 4ba14133 2020-03-20 stsp free(refname);
985 4ba14133 2020-03-20 stsp goto done;
986 4ba14133 2020-03-20 stsp }
987 4ba14133 2020-03-20 stsp
988 4ba14133 2020-03-20 stsp imsg_free(&imsg);
989 4ba14133 2020-03-20 stsp }
990 4ba14133 2020-03-20 stsp
991 cf875574 2020-03-18 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
992 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
993 93658fb9 2020-03-18 stsp err = NULL;
994 93658fb9 2020-03-18 stsp goto done;
995 93658fb9 2020-03-18 stsp }
996 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
997 93658fb9 2020-03-18 stsp goto done;
998 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
999 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1000 93658fb9 2020-03-18 stsp goto done;
1001 93658fb9 2020-03-18 stsp }
1002 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1003 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1004 93658fb9 2020-03-18 stsp goto done;
1005 93658fb9 2020-03-18 stsp }
1006 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1007 93658fb9 2020-03-18 stsp
1008 659e7fbd 2020-03-20 stsp err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
1009 4ba14133 2020-03-20 stsp fetch_req.fetch_all_branches, &wanted_branches, &ibuf);
1010 93658fb9 2020-03-18 stsp done:
1011 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
1012 7848a0e1 2020-03-19 stsp free((char *)pe->path);
1013 7848a0e1 2020-03-19 stsp free(pe->data);
1014 7848a0e1 2020-03-19 stsp }
1015 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1016 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1017 4ba14133 2020-03-20 stsp free((char *)pe->path);
1018 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1019 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1020 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1021 9ff10419 2020-03-18 stsp if (err != NULL)
1022 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1023 93658fb9 2020-03-18 stsp else
1024 93658fb9 2020-03-18 stsp err = got_privsep_send_fetch_done(&ibuf, packid);
1025 cf875574 2020-03-18 stsp if (err != NULL) {
1026 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1027 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1028 93658fb9 2020-03-18 stsp }
1029 93658fb9 2020-03-18 stsp
1030 93658fb9 2020-03-18 stsp exit(0);
1031 93658fb9 2020-03-18 stsp }