2 f8a36e22 2021-08-26 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 f8a36e22 2021-08-26 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 f8a36e22 2021-08-26 stsp * Permission to use, copy, modify, and distribute this software for any
6 f8a36e22 2021-08-26 stsp * purpose with or without fee is hereby granted, provided that the above
7 f8a36e22 2021-08-26 stsp * copyright notice and this permission notice appear in all copies.
9 f8a36e22 2021-08-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 f8a36e22 2021-08-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 f8a36e22 2021-08-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 f8a36e22 2021-08-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 f8a36e22 2021-08-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 f8a36e22 2021-08-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 f8a36e22 2021-08-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f8a36e22 2021-08-26 stsp #include <sys/types.h>
19 f8a36e22 2021-08-26 stsp #include <sys/queue.h>
20 f8a36e22 2021-08-26 stsp #include <sys/uio.h>
21 f8a36e22 2021-08-26 stsp #include <sys/time.h>
22 f8a36e22 2021-08-26 stsp #include <sys/stat.h>
24 f8a36e22 2021-08-26 stsp #include <stdint.h>
25 f8a36e22 2021-08-26 stsp #include <errno.h>
26 f8a36e22 2021-08-26 stsp #include <imsg.h>
27 f8a36e22 2021-08-26 stsp #include <limits.h>
28 f8a36e22 2021-08-26 stsp #include <signal.h>
29 f8a36e22 2021-08-26 stsp #include <stdio.h>
30 f8a36e22 2021-08-26 stsp #include <stdlib.h>
31 f8a36e22 2021-08-26 stsp #include <string.h>
32 f8a36e22 2021-08-26 stsp #include <ctype.h>
33 f8a36e22 2021-08-26 stsp #include <sha1.h>
34 f8a36e22 2021-08-26 stsp #include <fcntl.h>
35 f8a36e22 2021-08-26 stsp #include <unistd.h>
36 f8a36e22 2021-08-26 stsp #include <zlib.h>
37 f8a36e22 2021-08-26 stsp #include <err.h>
39 f8a36e22 2021-08-26 stsp #include "got_error.h"
40 f8a36e22 2021-08-26 stsp #include "got_object.h"
41 f8a36e22 2021-08-26 stsp #include "got_path.h"
42 f8a36e22 2021-08-26 stsp #include "got_version.h"
43 f8a36e22 2021-08-26 stsp #include "got_fetch.h"
44 f8a36e22 2021-08-26 stsp #include "got_reference.h"
46 f8a36e22 2021-08-26 stsp #include "got_lib_sha1.h"
47 f8a36e22 2021-08-26 stsp #include "got_lib_delta.h"
48 f8a36e22 2021-08-26 stsp #include "got_lib_object.h"
49 f8a36e22 2021-08-26 stsp #include "got_lib_object_parse.h"
50 f8a36e22 2021-08-26 stsp #include "got_lib_privsep.h"
51 f8a36e22 2021-08-26 stsp #include "got_lib_pack.h"
53 f8a36e22 2021-08-26 stsp #ifndef nitems
54 f8a36e22 2021-08-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 f8a36e22 2021-08-26 stsp struct got_object *indexed;
58 f8a36e22 2021-08-26 stsp static int chattygot;
60 f8a36e22 2021-08-26 stsp static const struct got_error *
61 f8a36e22 2021-08-26 stsp readn(ssize_t *off, int fd, void *buf, size_t n)
66 f8a36e22 2021-08-26 stsp while (*off != n) {
67 f8a36e22 2021-08-26 stsp r = read(fd, buf + *off, n - *off);
68 f8a36e22 2021-08-26 stsp if (r == -1)
69 f8a36e22 2021-08-26 stsp return got_error_from_errno("read");
71 f8a36e22 2021-08-26 stsp return NULL;
74 f8a36e22 2021-08-26 stsp return NULL;
77 f8a36e22 2021-08-26 stsp static const struct got_error *
78 f8a36e22 2021-08-26 stsp flushpkt(int fd)
82 f8a36e22 2021-08-26 stsp if (chattygot > 1)
83 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
85 f8a36e22 2021-08-26 stsp w = write(fd, "0000", 4);
86 f8a36e22 2021-08-26 stsp if (w == -1)
87 f8a36e22 2021-08-26 stsp return got_error_from_errno("write");
89 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_IO);
90 f8a36e22 2021-08-26 stsp return NULL;
94 f8a36e22 2021-08-26 stsp * Packet header contains a 4-byte hexstring which specifies the length
95 f8a36e22 2021-08-26 stsp * of data which follows.
97 f8a36e22 2021-08-26 stsp static const struct got_error *
98 f8a36e22 2021-08-26 stsp read_pkthdr(int *datalen, int fd)
100 f8a36e22 2021-08-26 stsp static const struct got_error *err = NULL;
101 f8a36e22 2021-08-26 stsp char lenstr[5];
107 f8a36e22 2021-08-26 stsp *datalen = 0;
109 f8a36e22 2021-08-26 stsp err = readn(&r, fd, lenstr, 4);
111 f8a36e22 2021-08-26 stsp return err;
112 f8a36e22 2021-08-26 stsp if (r == 0) {
113 f8a36e22 2021-08-26 stsp /* implicit "0000" */
114 f8a36e22 2021-08-26 stsp if (chattygot > 1)
115 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
116 f8a36e22 2021-08-26 stsp return NULL;
118 f8a36e22 2021-08-26 stsp if (r != 4)
119 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
120 f8a36e22 2021-08-26 stsp "wrong packet header length");
122 f8a36e22 2021-08-26 stsp lenstr[4] = '\0';
123 f8a36e22 2021-08-26 stsp for (i = 0; i < 4; i++) {
124 f8a36e22 2021-08-26 stsp if (!isprint((unsigned char)lenstr[i]))
125 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
126 f8a36e22 2021-08-26 stsp "unprintable character in packet length field");
128 f8a36e22 2021-08-26 stsp for (i = 0; i < 4; i++) {
129 f8a36e22 2021-08-26 stsp if (!isxdigit((unsigned char)lenstr[i])) {
130 f8a36e22 2021-08-26 stsp if (chattygot)
131 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: bad length: '%s'\n",
132 f8a36e22 2021-08-26 stsp getprogname(), lenstr);
133 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
134 f8a36e22 2021-08-26 stsp "packet length not specified in hex");
138 f8a36e22 2021-08-26 stsp len = strtol(lenstr, &e, 16);
139 f8a36e22 2021-08-26 stsp if (lenstr[0] == '\0' || *e != '\0')
140 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_BAD_PACKET);
141 f8a36e22 2021-08-26 stsp if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
142 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
143 f8a36e22 2021-08-26 stsp if (len > INT_MAX || len < INT_MIN)
144 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
146 f8a36e22 2021-08-26 stsp if (n == 0)
147 f8a36e22 2021-08-26 stsp return NULL;
148 f8a36e22 2021-08-26 stsp if (n <= 4)
149 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
152 f8a36e22 2021-08-26 stsp *datalen = n;
153 f8a36e22 2021-08-26 stsp return NULL;
156 f8a36e22 2021-08-26 stsp static const struct got_error *
157 f8a36e22 2021-08-26 stsp readpkt(int *outlen, int fd, char *buf, int buflen)
159 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
160 f8a36e22 2021-08-26 stsp int datalen, i;
163 f8a36e22 2021-08-26 stsp err = read_pkthdr(&datalen, fd);
165 f8a36e22 2021-08-26 stsp return err;
167 f8a36e22 2021-08-26 stsp if (datalen > buflen)
168 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
170 f8a36e22 2021-08-26 stsp err = readn(&n, fd, buf, datalen);
172 f8a36e22 2021-08-26 stsp return err;
173 f8a36e22 2021-08-26 stsp if (n != datalen)
174 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
176 f8a36e22 2021-08-26 stsp if (chattygot > 1) {
177 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
178 f8a36e22 2021-08-26 stsp for (i = 0; i < n; i++) {
179 f8a36e22 2021-08-26 stsp if (isprint(buf[i]))
180 f8a36e22 2021-08-26 stsp fputc(buf[i], stderr);
182 f8a36e22 2021-08-26 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
184 f8a36e22 2021-08-26 stsp fputc('\n', stderr);
187 f8a36e22 2021-08-26 stsp *outlen = n;
188 f8a36e22 2021-08-26 stsp return NULL;
191 f8a36e22 2021-08-26 stsp static const struct got_error *
192 f8a36e22 2021-08-26 stsp writepkt(int fd, char *buf, int nbuf)
194 f8a36e22 2021-08-26 stsp char len[5];
198 f8a36e22 2021-08-26 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
199 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
200 f8a36e22 2021-08-26 stsp w = write(fd, len, 4);
201 f8a36e22 2021-08-26 stsp if (w == -1)
202 f8a36e22 2021-08-26 stsp return got_error_from_errno("write");
203 f8a36e22 2021-08-26 stsp if (w != 4)
204 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_IO);
205 f8a36e22 2021-08-26 stsp w = write(fd, buf, nbuf);
206 f8a36e22 2021-08-26 stsp if (w == -1)
207 f8a36e22 2021-08-26 stsp return got_error_from_errno("write");
208 f8a36e22 2021-08-26 stsp if (w != nbuf)
209 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_IO);
210 f8a36e22 2021-08-26 stsp if (chattygot > 1) {
211 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
212 f8a36e22 2021-08-26 stsp for (i = 0; i < nbuf; i++) {
213 f8a36e22 2021-08-26 stsp if (isprint(buf[i]))
214 f8a36e22 2021-08-26 stsp fputc(buf[i], stderr);
216 f8a36e22 2021-08-26 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
218 f8a36e22 2021-08-26 stsp fputc('\n', stderr);
220 f8a36e22 2021-08-26 stsp return NULL;
223 f8a36e22 2021-08-26 stsp static const struct got_error *
224 f8a36e22 2021-08-26 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
226 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
228 f8a36e22 2021-08-26 stsp size_t i, n = 0;
230 f8a36e22 2021-08-26 stsp for (i = 0; i < maxtokens; i++)
231 f8a36e22 2021-08-26 stsp tokens[i] = NULL;
233 f8a36e22 2021-08-26 stsp for (i = 0; n < len && i < maxtokens; i++) {
234 f8a36e22 2021-08-26 stsp while (isspace(*line)) {
239 f8a36e22 2021-08-26 stsp while (*line != '\0' && n < len &&
240 f8a36e22 2021-08-26 stsp (!isspace(*line) || i == maxtokens - 1)) {
244 f8a36e22 2021-08-26 stsp tokens[i] = strndup(p, line - p);
245 f8a36e22 2021-08-26 stsp if (tokens[i] == NULL) {
246 f8a36e22 2021-08-26 stsp err = got_error_from_errno("strndup");
249 f8a36e22 2021-08-26 stsp /* Skip \0 field-delimiter at end of token. */
250 f8a36e22 2021-08-26 stsp while (line[0] == '\0' && n < len) {
255 f8a36e22 2021-08-26 stsp if (i <= 2)
256 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_NOT_REF);
260 f8a36e22 2021-08-26 stsp for (j = 0; j < i; j++) {
261 f8a36e22 2021-08-26 stsp free(tokens[j]);
262 f8a36e22 2021-08-26 stsp tokens[j] = NULL;
265 f8a36e22 2021-08-26 stsp return err;
268 f8a36e22 2021-08-26 stsp static const struct got_error *
269 f8a36e22 2021-08-26 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
270 f8a36e22 2021-08-26 stsp char *line, int len)
272 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
273 f8a36e22 2021-08-26 stsp char *tokens[3];
275 f8a36e22 2021-08-26 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
277 f8a36e22 2021-08-26 stsp return err;
279 f8a36e22 2021-08-26 stsp if (tokens[0])
280 f8a36e22 2021-08-26 stsp *id_str = tokens[0];
281 f8a36e22 2021-08-26 stsp if (tokens[1])
282 f8a36e22 2021-08-26 stsp *refname = tokens[1];
283 f8a36e22 2021-08-26 stsp if (tokens[2]) {
285 f8a36e22 2021-08-26 stsp *server_capabilities = tokens[2];
286 f8a36e22 2021-08-26 stsp p = strrchr(*server_capabilities, '\n');
291 f8a36e22 2021-08-26 stsp return NULL;
294 f8a36e22 2021-08-26 stsp #define GOT_CAPA_AGENT "agent"
295 f8a36e22 2021-08-26 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
296 f8a36e22 2021-08-26 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
297 f8a36e22 2021-08-26 stsp #define GOT_CAPA_REPORT_STATUS "report-status"
298 f8a36e22 2021-08-26 stsp #define GOT_CAPA_DELETE_REFS "delete-refs"
300 f8a36e22 2021-08-26 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
301 f8a36e22 2021-08-26 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
302 f8a36e22 2021-08-26 stsp #define GOT_SIDEBAND_ERROR_INFO 3
305 f8a36e22 2021-08-26 stsp struct got_capability {
306 f8a36e22 2021-08-26 stsp const char *key;
307 f8a36e22 2021-08-26 stsp const char *value;
309 f8a36e22 2021-08-26 stsp static const struct got_capability got_capabilities[] = {
310 f8a36e22 2021-08-26 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
311 f8a36e22 2021-08-26 stsp { GOT_CAPA_OFS_DELTA, NULL },
313 f8a36e22 2021-08-26 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
315 f8a36e22 2021-08-26 stsp { GOT_CAPA_REPORT_STATUS, NULL },
316 f8a36e22 2021-08-26 stsp { GOT_CAPA_DELETE_REFS, NULL },
319 f8a36e22 2021-08-26 stsp static const struct got_error *
320 f8a36e22 2021-08-26 stsp match_capability(char **my_capabilities, const char *capa,
321 f8a36e22 2021-08-26 stsp const struct got_capability *mycapa)
323 f8a36e22 2021-08-26 stsp char *equalsign;
326 f8a36e22 2021-08-26 stsp equalsign = strchr(capa, '=');
327 f8a36e22 2021-08-26 stsp if (equalsign) {
328 f8a36e22 2021-08-26 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
329 f8a36e22 2021-08-26 stsp return NULL;
331 f8a36e22 2021-08-26 stsp if (strcmp(capa, mycapa->key) != 0)
332 f8a36e22 2021-08-26 stsp return NULL;
335 f8a36e22 2021-08-26 stsp if (asprintf(&s, "%s %s%s%s",
336 f8a36e22 2021-08-26 stsp *my_capabilities != NULL ? *my_capabilities : "",
337 f8a36e22 2021-08-26 stsp mycapa->key,
338 f8a36e22 2021-08-26 stsp mycapa->value != NULL ? "=" : "",
339 f8a36e22 2021-08-26 stsp mycapa->value != NULL? mycapa->value : "") == -1)
340 f8a36e22 2021-08-26 stsp return got_error_from_errno("asprintf");
342 f8a36e22 2021-08-26 stsp free(*my_capabilities);
343 f8a36e22 2021-08-26 stsp *my_capabilities = s;
344 f8a36e22 2021-08-26 stsp return NULL;
347 f8a36e22 2021-08-26 stsp static const struct got_error *
348 f8a36e22 2021-08-26 stsp match_capabilities(char **my_capabilities, char *server_capabilities)
350 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
351 f8a36e22 2021-08-26 stsp char *capa;
354 f8a36e22 2021-08-26 stsp *my_capabilities = NULL;
356 f8a36e22 2021-08-26 stsp capa = strsep(&server_capabilities, " ");
357 f8a36e22 2021-08-26 stsp for (i = 0; capa != NULL && i < nitems(got_capabilities); i++) {
358 f8a36e22 2021-08-26 stsp err = match_capability(my_capabilities,
359 f8a36e22 2021-08-26 stsp capa, &got_capabilities[i]);
363 f8a36e22 2021-08-26 stsp } while (capa);
365 f8a36e22 2021-08-26 stsp if (*my_capabilities == NULL) {
366 f8a36e22 2021-08-26 stsp *my_capabilities = strdup("");
367 f8a36e22 2021-08-26 stsp if (*my_capabilities == NULL) {
368 f8a36e22 2021-08-26 stsp err = got_error_from_errno("strdup");
374 f8a36e22 2021-08-26 stsp free(*my_capabilities);
375 f8a36e22 2021-08-26 stsp *my_capabilities = NULL;
377 f8a36e22 2021-08-26 stsp return err;
380 f8a36e22 2021-08-26 stsp static const struct got_error *
381 f8a36e22 2021-08-26 stsp send_upload_progress(struct imsgbuf *ibuf, off_t bytes)
383 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
384 f8a36e22 2021-08-26 stsp &bytes, sizeof(bytes)) == -1)
385 f8a36e22 2021-08-26 stsp return got_error_from_errno(
386 f8a36e22 2021-08-26 stsp "imsg_compose SEND_UPLOAD_PROGRESS");
388 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
391 f8a36e22 2021-08-26 stsp static const struct got_error *
392 f8a36e22 2021-08-26 stsp send_pack_request(struct imsgbuf *ibuf)
394 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
395 f8a36e22 2021-08-26 stsp NULL, 0) == -1)
396 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
397 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
400 f8a36e22 2021-08-26 stsp static const struct got_error *
401 f8a36e22 2021-08-26 stsp send_done(struct imsgbuf *ibuf)
403 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
404 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_DONE");
405 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
408 f8a36e22 2021-08-26 stsp static const struct got_error *
409 f8a36e22 2021-08-26 stsp recv_packfd(int *packfd, struct imsgbuf *ibuf)
411 f8a36e22 2021-08-26 stsp const struct got_error *err;
412 f8a36e22 2021-08-26 stsp struct imsg imsg;
414 f8a36e22 2021-08-26 stsp *packfd = -1;
416 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
418 f8a36e22 2021-08-26 stsp return err;
420 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP) {
421 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CANCELLED);
425 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
426 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
430 f8a36e22 2021-08-26 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
431 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
435 f8a36e22 2021-08-26 stsp *packfd = imsg.fd;
437 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
438 f8a36e22 2021-08-26 stsp return err;
441 f8a36e22 2021-08-26 stsp static const struct got_error *
442 f8a36e22 2021-08-26 stsp send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
444 f8a36e22 2021-08-26 stsp const struct got_error *err;
445 f8a36e22 2021-08-26 stsp unsigned char buf[8192];
446 f8a36e22 2021-08-26 stsp ssize_t r, w;
447 f8a36e22 2021-08-26 stsp off_t wtotal = 0;
449 f8a36e22 2021-08-26 stsp if (lseek(packfd, 0L, SEEK_SET) == -1)
450 f8a36e22 2021-08-26 stsp return got_error_from_errno("lseek");
453 f8a36e22 2021-08-26 stsp r = read(packfd, buf, sizeof(buf));
454 f8a36e22 2021-08-26 stsp if (r == -1)
455 f8a36e22 2021-08-26 stsp return got_error_from_errno("read");
456 f8a36e22 2021-08-26 stsp if (r == 0)
458 f8a36e22 2021-08-26 stsp w = write(sendfd, buf, r);
459 f8a36e22 2021-08-26 stsp if (w == -1)
460 f8a36e22 2021-08-26 stsp return got_error_from_errno("write");
461 f8a36e22 2021-08-26 stsp if (w != r)
462 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_IO);
463 f8a36e22 2021-08-26 stsp wtotal += w;
464 f8a36e22 2021-08-26 stsp err = send_upload_progress(ibuf, wtotal);
466 f8a36e22 2021-08-26 stsp return err;
469 f8a36e22 2021-08-26 stsp return NULL;
472 f8a36e22 2021-08-26 stsp static const struct got_error *
473 f8a36e22 2021-08-26 stsp send_error(const char *buf, size_t len)
475 f8a36e22 2021-08-26 stsp static char msg[1024];
478 f8a36e22 2021-08-26 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
479 f8a36e22 2021-08-26 stsp if (!isprint(buf[i]))
480 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
481 f8a36e22 2021-08-26 stsp "non-printable error message received from server");
482 f8a36e22 2021-08-26 stsp msg[i] = buf[i];
484 f8a36e22 2021-08-26 stsp msg[i] = '\0';
485 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_SEND_FAILED, msg);
488 f8a36e22 2021-08-26 stsp static const struct got_error *
489 f8a36e22 2021-08-26 stsp send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
490 f8a36e22 2021-08-26 stsp const char *refname)
492 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
493 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
494 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
496 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_remote_ref) + reflen;
497 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
498 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
500 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
501 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
502 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REMOTE_REF");
504 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_remote_ref definition! */
505 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
506 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
507 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
508 f8a36e22 2021-08-26 stsp return err;
510 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
511 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
512 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
513 f8a36e22 2021-08-26 stsp return err;
515 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
516 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
517 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
518 f8a36e22 2021-08-26 stsp return err;
521 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
522 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
523 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
526 f8a36e22 2021-08-26 stsp static const struct got_error *
527 f8a36e22 2021-08-26 stsp send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
528 f8a36e22 2021-08-26 stsp struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
531 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
532 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
533 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
534 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
535 f8a36e22 2021-08-26 stsp int ref_valid = 0;
538 f8a36e22 2021-08-26 stsp eol = strchr(refname, '\n');
539 f8a36e22 2021-08-26 stsp if (eol == NULL) {
540 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
541 f8a36e22 2021-08-26 stsp "unexpected message from server");
543 f8a36e22 2021-08-26 stsp *eol = '\0';
545 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
546 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
547 f8a36e22 2021-08-26 stsp ref_valid = 1;
551 f8a36e22 2021-08-26 stsp if (!ref_valid) {
552 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
553 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
554 f8a36e22 2021-08-26 stsp ref_valid = 1;
559 f8a36e22 2021-08-26 stsp if (!ref_valid) {
560 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
561 f8a36e22 2021-08-26 stsp "unexpected message from server");
564 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_ref_status) + reflen;
565 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
566 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
568 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
569 f8a36e22 2021-08-26 stsp 0, 0, len);
570 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
571 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REF_STATUS");
573 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_ref_status definition! */
574 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
575 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
576 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
577 f8a36e22 2021-08-26 stsp return err;
579 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
580 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
581 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
582 f8a36e22 2021-08-26 stsp return err;
584 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
585 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
586 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
587 f8a36e22 2021-08-26 stsp return err;
590 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
591 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
592 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
595 f8a36e22 2021-08-26 stsp static const struct got_error *
596 f8a36e22 2021-08-26 stsp describe_refchange(int *n, int *sent_my_capabilites,
597 f8a36e22 2021-08-26 stsp const char *my_capabilities, char *buf, size_t bufsize,
598 f8a36e22 2021-08-26 stsp const char *refname, const char *old_hashstr, const char *new_hashstr)
600 f8a36e22 2021-08-26 stsp *n = snprintf(buf, bufsize, "%s %s %s",
601 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr, refname);
602 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
603 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
606 f8a36e22 2021-08-26 stsp * We must announce our capabilities along with the first
607 f8a36e22 2021-08-26 stsp * reference. Unfortunately, the protocol requires an embedded
608 f8a36e22 2021-08-26 stsp * NUL as a separator between reference name and capabilities,
609 f8a36e22 2021-08-26 stsp * which we have to deal with here.
610 f8a36e22 2021-08-26 stsp * It also requires a linefeed for terminating packet data.
612 f8a36e22 2021-08-26 stsp if (!*sent_my_capabilites && my_capabilities != NULL) {
614 f8a36e22 2021-08-26 stsp if (*n >= bufsize - 1)
615 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
616 f8a36e22 2021-08-26 stsp m = snprintf(buf + *n + 1, /* offset after '\0' */
617 f8a36e22 2021-08-26 stsp bufsize - (*n + 1), "%s\n", my_capabilities);
618 f8a36e22 2021-08-26 stsp if (*n + m >= bufsize)
619 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
621 f8a36e22 2021-08-26 stsp *sent_my_capabilites = 1;
623 f8a36e22 2021-08-26 stsp *n = strlcat(buf, "\n", bufsize);
624 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
625 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
628 f8a36e22 2021-08-26 stsp return NULL;
631 f8a36e22 2021-08-26 stsp static const struct got_error *
632 f8a36e22 2021-08-26 stsp send_pack(int fd, struct got_pathlist_head *refs,
633 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
635 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
636 f8a36e22 2021-08-26 stsp char buf[GOT_FETCH_PKTMAX];
637 f8a36e22 2021-08-26 stsp unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
638 f8a36e22 2021-08-26 stsp char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
639 f8a36e22 2021-08-26 stsp char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
640 f8a36e22 2021-08-26 stsp struct got_pathlist_head their_refs;
641 f8a36e22 2021-08-26 stsp int is_firstpkt = 1;
642 f8a36e22 2021-08-26 stsp int n, nsent = 0;
643 f8a36e22 2021-08-26 stsp int packfd = -1;
644 f8a36e22 2021-08-26 stsp char *id_str = NULL, *refname = NULL;
645 f8a36e22 2021-08-26 stsp struct got_object_id *id = NULL;
646 f8a36e22 2021-08-26 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
647 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
648 f8a36e22 2021-08-26 stsp int sent_my_capabilites = 0;
650 f8a36e22 2021-08-26 stsp TAILQ_INIT(&their_refs);
652 f8a36e22 2021-08-26 stsp if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
653 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_SEND_EMPTY);
655 f8a36e22 2021-08-26 stsp while (1) {
656 f8a36e22 2021-08-26 stsp err = readpkt(&n, fd, buf, sizeof(buf));
659 f8a36e22 2021-08-26 stsp if (n == 0)
661 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
662 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
665 f8a36e22 2021-08-26 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
669 f8a36e22 2021-08-26 stsp if (is_firstpkt) {
670 f8a36e22 2021-08-26 stsp if (chattygot && server_capabilities[0] != '\0')
671 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: server capabilities: %s\n",
672 f8a36e22 2021-08-26 stsp getprogname(), server_capabilities);
673 f8a36e22 2021-08-26 stsp err = match_capabilities(&my_capabilities,
674 f8a36e22 2021-08-26 stsp server_capabilities);
677 f8a36e22 2021-08-26 stsp if (chattygot)
678 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: my capabilities:%s\n",
679 f8a36e22 2021-08-26 stsp getprogname(), my_capabilities);
680 f8a36e22 2021-08-26 stsp is_firstpkt = 0;
682 f8a36e22 2021-08-26 stsp if (strstr(refname, "^{}")) {
683 f8a36e22 2021-08-26 stsp if (chattygot) {
684 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: ignoring %s\n",
685 f8a36e22 2021-08-26 stsp getprogname(), refname);
690 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
691 f8a36e22 2021-08-26 stsp if (id == NULL) {
692 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
695 f8a36e22 2021-08-26 stsp if (!got_parse_sha1_digest(id->sha1, id_str)) {
696 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
699 f8a36e22 2021-08-26 stsp err = send_their_ref(ibuf, id, refname);
703 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&their_refs, refname, id);
704 f8a36e22 2021-08-26 stsp if (chattygot)
705 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: remote has %s %s\n",
706 f8a36e22 2021-08-26 stsp getprogname(), refname, id_str);
707 f8a36e22 2021-08-26 stsp free(id_str);
708 f8a36e22 2021-08-26 stsp id_str = NULL;
709 f8a36e22 2021-08-26 stsp refname = NULL; /* do not free; owned by their_refs */
710 f8a36e22 2021-08-26 stsp id = NULL; /* do not free; owned by their_refs */
713 f8a36e22 2021-08-26 stsp if (!TAILQ_EMPTY(delete_refs)) {
714 f8a36e22 2021-08-26 stsp if (my_capabilities == NULL ||
715 f8a36e22 2021-08-26 stsp strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
716 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CAPA_DELETE_REFS);
721 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
722 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
723 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
724 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
726 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
727 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
728 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
729 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
730 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
734 f8a36e22 2021-08-26 stsp if (their_id == NULL) {
735 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
736 f8a36e22 2021-08-26 stsp "%s does not exist in remote repository",
741 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
742 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
743 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, new_hashstr,
744 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
745 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
746 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
747 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
750 f8a36e22 2021-08-26 stsp err = writepkt(fd, buf, n);
753 f8a36e22 2021-08-26 stsp if (chattygot) {
754 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: deleting %s %s\n",
755 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr);
760 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
761 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
762 f8a36e22 2021-08-26 stsp struct got_object_id *id = pe->data;
763 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
764 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
766 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
767 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
768 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
769 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
770 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
774 f8a36e22 2021-08-26 stsp if (their_id) {
775 f8a36e22 2021-08-26 stsp if (got_object_id_cmp(id, their_id) == 0) {
776 f8a36e22 2021-08-26 stsp if (chattygot) {
777 f8a36e22 2021-08-26 stsp fprintf(stderr,
778 f8a36e22 2021-08-26 stsp "%s: no change for %s\n",
779 f8a36e22 2021-08-26 stsp getprogname(), refname);
783 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
784 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
786 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, old_hashstr,
787 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
789 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(id->sha1, new_hashstr,
790 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
791 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
792 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
793 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
796 f8a36e22 2021-08-26 stsp err = writepkt(fd, buf, n);
799 f8a36e22 2021-08-26 stsp if (chattygot) {
800 f8a36e22 2021-08-26 stsp if (their_id) {
801 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: updating %s %s -> %s\n",
802 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr,
803 f8a36e22 2021-08-26 stsp new_hashstr);
805 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: creating %s %s\n",
806 f8a36e22 2021-08-26 stsp getprogname(), refname, new_hashstr);
811 f8a36e22 2021-08-26 stsp err = flushpkt(fd);
815 f8a36e22 2021-08-26 stsp err = send_pack_request(ibuf);
819 f8a36e22 2021-08-26 stsp err = recv_packfd(&packfd, ibuf);
823 27b75514 2021-08-28 stsp if (packfd != -1) {
824 27b75514 2021-08-28 stsp err = send_pack_file(fd, packfd, ibuf);
829 f8a36e22 2021-08-26 stsp err = readpkt(&n, fd, buf, sizeof(buf));
832 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
833 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
835 f8a36e22 2021-08-26 stsp } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
836 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
837 f8a36e22 2021-08-26 stsp "unexpected message from server");
841 f8a36e22 2021-08-26 stsp while (nsent > 0) {
842 f8a36e22 2021-08-26 stsp err = readpkt(&n, fd, buf, sizeof(buf));
845 f8a36e22 2021-08-26 stsp if (n < 3) {
846 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
847 f8a36e22 2021-08-26 stsp "unexpected message from server");
849 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ok ", 3) == 0) {
850 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 1,
851 f8a36e22 2021-08-26 stsp refs, delete_refs);
854 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ng ", 3) == 0) {
855 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 0,
856 f8a36e22 2021-08-26 stsp refs, delete_refs);
860 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
861 f8a36e22 2021-08-26 stsp "unexpected message from server");
867 f8a36e22 2021-08-26 stsp err = send_done(ibuf);
869 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &their_refs, entry) {
870 f8a36e22 2021-08-26 stsp free((void *)pe->path);
871 f8a36e22 2021-08-26 stsp free(pe->data);
873 f8a36e22 2021-08-26 stsp got_pathlist_free(&their_refs);
874 f8a36e22 2021-08-26 stsp free(id_str);
876 f8a36e22 2021-08-26 stsp free(refname);
877 f8a36e22 2021-08-26 stsp free(server_capabilities);
878 f8a36e22 2021-08-26 stsp return err;
882 f8a36e22 2021-08-26 stsp main(int argc, char **argv)
884 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
885 f8a36e22 2021-08-26 stsp int sendfd, i;
886 f8a36e22 2021-08-26 stsp struct imsgbuf ibuf;
887 f8a36e22 2021-08-26 stsp struct imsg imsg;
888 f8a36e22 2021-08-26 stsp struct got_pathlist_head refs;
889 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_refs;
890 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
891 f8a36e22 2021-08-26 stsp struct got_imsg_send_request send_req;
892 f8a36e22 2021-08-26 stsp struct got_imsg_send_ref href;
893 f8a36e22 2021-08-26 stsp size_t datalen;
895 f8a36e22 2021-08-26 stsp static int attached;
896 f8a36e22 2021-08-26 stsp while (!attached)
900 f8a36e22 2021-08-26 stsp TAILQ_INIT(&refs);
901 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_refs);
903 f8a36e22 2021-08-26 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
904 f8a36e22 2021-08-26 stsp #ifndef PROFILE
905 f8a36e22 2021-08-26 stsp /* revoke access to most system calls */
906 f8a36e22 2021-08-26 stsp if (pledge("stdio recvfd", NULL) == -1) {
907 f8a36e22 2021-08-26 stsp err = got_error_from_errno("pledge");
908 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);
912 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
913 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
914 f8a36e22 2021-08-26 stsp err = NULL;
917 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
919 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
920 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
923 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
924 f8a36e22 2021-08-26 stsp if (datalen < sizeof(send_req)) {
925 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
928 f8a36e22 2021-08-26 stsp memcpy(&send_req, imsg.data, sizeof(send_req));
929 f8a36e22 2021-08-26 stsp sendfd = imsg.fd;
930 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
932 f8a36e22 2021-08-26 stsp if (send_req.verbosity > 0)
933 f8a36e22 2021-08-26 stsp chattygot += send_req.verbosity;
935 f8a36e22 2021-08-26 stsp for (i = 0; i < send_req.nrefs; i++) {
936 f8a36e22 2021-08-26 stsp struct got_object_id *id;
937 f8a36e22 2021-08-26 stsp char *refname;
939 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
940 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
941 f8a36e22 2021-08-26 stsp err = NULL;
944 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
946 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
947 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
950 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
951 f8a36e22 2021-08-26 stsp if (datalen < sizeof(href)) {
952 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
955 f8a36e22 2021-08-26 stsp memcpy(&href, imsg.data, sizeof(href));
956 f8a36e22 2021-08-26 stsp if (datalen - sizeof(href) < href.name_len) {
957 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
960 f8a36e22 2021-08-26 stsp refname = malloc(href.name_len + 1);
961 f8a36e22 2021-08-26 stsp if (refname == NULL) {
962 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
965 f8a36e22 2021-08-26 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
966 f8a36e22 2021-08-26 stsp refname[href.name_len] = '\0';
969 f8a36e22 2021-08-26 stsp * Prevent sending of references that won't make any
970 f8a36e22 2021-08-26 stsp * sense outside the local repository's context.
972 f8a36e22 2021-08-26 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
973 f8a36e22 2021-08-26 stsp strncmp(refname, "refs/remotes/", 13) == 0) {
974 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
975 f8a36e22 2021-08-26 stsp "%s", refname);
979 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
980 f8a36e22 2021-08-26 stsp if (id == NULL) {
981 f8a36e22 2021-08-26 stsp free(refname);
982 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
985 f8a36e22 2021-08-26 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
986 f8a36e22 2021-08-26 stsp if (href.delete)
987 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&delete_refs, refname, id);
989 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&refs, refname, id);
991 f8a36e22 2021-08-26 stsp free(refname);
996 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
999 f8a36e22 2021-08-26 stsp err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
1001 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
1002 f8a36e22 2021-08-26 stsp free((char *)pe->path);
1003 f8a36e22 2021-08-26 stsp free(pe->data);
1005 f8a36e22 2021-08-26 stsp got_pathlist_free(&refs);
1006 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_refs, entry) {
1007 f8a36e22 2021-08-26 stsp free((char *)pe->path);
1008 f8a36e22 2021-08-26 stsp free(pe->data);
1010 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_refs);
1011 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
1012 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
1013 f8a36e22 2021-08-26 stsp if (err != NULL && err->code != GOT_ERR_CANCELLED) {
1014 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1015 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);