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 * Workaround for github.
376 f8a36e22 2021-08-26 stsp * Github will accept the pack but fail to update the references
377 f8a36e22 2021-08-26 stsp * if we don't have capabilities advertised. Report-status seems
378 f8a36e22 2021-08-26 stsp * harmless to add, so we add it.
380 f8a36e22 2021-08-26 stsp * Github doesn't advertise any capabilities, so we can't check
381 f8a36e22 2021-08-26 stsp * for compatibility. We just need to add it blindly.
383 f8a36e22 2021-08-26 stsp if (strstr(*my_capabilities, GOT_CAPA_REPORT_STATUS) == NULL) {
385 f8a36e22 2021-08-26 stsp if (asprintf(&s, "%s %s", *my_capabilities,
386 f8a36e22 2021-08-26 stsp GOT_CAPA_REPORT_STATUS) == -1) {
387 f8a36e22 2021-08-26 stsp err = got_error_from_errno("asprintf");
390 f8a36e22 2021-08-26 stsp free(*my_capabilities);
391 f8a36e22 2021-08-26 stsp *my_capabilities = s;
395 f8a36e22 2021-08-26 stsp free(*my_capabilities);
396 f8a36e22 2021-08-26 stsp *my_capabilities = NULL;
398 f8a36e22 2021-08-26 stsp return err;
401 f8a36e22 2021-08-26 stsp static const struct got_error *
402 f8a36e22 2021-08-26 stsp send_upload_progress(struct imsgbuf *ibuf, off_t bytes)
404 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
405 f8a36e22 2021-08-26 stsp &bytes, sizeof(bytes)) == -1)
406 f8a36e22 2021-08-26 stsp return got_error_from_errno(
407 f8a36e22 2021-08-26 stsp "imsg_compose SEND_UPLOAD_PROGRESS");
409 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
412 f8a36e22 2021-08-26 stsp static const struct got_error *
413 f8a36e22 2021-08-26 stsp send_pack_request(struct imsgbuf *ibuf)
415 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
416 f8a36e22 2021-08-26 stsp NULL, 0) == -1)
417 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
418 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
421 f8a36e22 2021-08-26 stsp static const struct got_error *
422 f8a36e22 2021-08-26 stsp send_done(struct imsgbuf *ibuf)
424 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
425 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_DONE");
426 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
429 f8a36e22 2021-08-26 stsp static const struct got_error *
430 f8a36e22 2021-08-26 stsp recv_packfd(int *packfd, struct imsgbuf *ibuf)
432 f8a36e22 2021-08-26 stsp const struct got_error *err;
433 f8a36e22 2021-08-26 stsp struct imsg imsg;
435 f8a36e22 2021-08-26 stsp *packfd = -1;
437 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
439 f8a36e22 2021-08-26 stsp return err;
441 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP) {
442 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CANCELLED);
446 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
447 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
451 f8a36e22 2021-08-26 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
452 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
456 f8a36e22 2021-08-26 stsp *packfd = imsg.fd;
458 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
459 f8a36e22 2021-08-26 stsp return err;
462 f8a36e22 2021-08-26 stsp static const struct got_error *
463 f8a36e22 2021-08-26 stsp send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
465 f8a36e22 2021-08-26 stsp const struct got_error *err;
466 f8a36e22 2021-08-26 stsp unsigned char buf[8192];
467 f8a36e22 2021-08-26 stsp ssize_t r, w;
468 f8a36e22 2021-08-26 stsp off_t wtotal = 0;
470 f8a36e22 2021-08-26 stsp if (lseek(packfd, 0L, SEEK_SET) == -1)
471 f8a36e22 2021-08-26 stsp return got_error_from_errno("lseek");
474 f8a36e22 2021-08-26 stsp r = read(packfd, buf, sizeof(buf));
475 f8a36e22 2021-08-26 stsp if (r == -1)
476 f8a36e22 2021-08-26 stsp return got_error_from_errno("read");
477 f8a36e22 2021-08-26 stsp if (r == 0)
479 f8a36e22 2021-08-26 stsp w = write(sendfd, buf, r);
480 f8a36e22 2021-08-26 stsp if (w == -1)
481 f8a36e22 2021-08-26 stsp return got_error_from_errno("write");
482 f8a36e22 2021-08-26 stsp if (w != r)
483 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_IO);
484 f8a36e22 2021-08-26 stsp wtotal += w;
485 f8a36e22 2021-08-26 stsp err = send_upload_progress(ibuf, wtotal);
487 f8a36e22 2021-08-26 stsp return err;
490 f8a36e22 2021-08-26 stsp return NULL;
493 f8a36e22 2021-08-26 stsp static const struct got_error *
494 f8a36e22 2021-08-26 stsp send_error(const char *buf, size_t len)
496 f8a36e22 2021-08-26 stsp static char msg[1024];
499 f8a36e22 2021-08-26 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
500 f8a36e22 2021-08-26 stsp if (!isprint(buf[i]))
501 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
502 f8a36e22 2021-08-26 stsp "non-printable error message received from server");
503 f8a36e22 2021-08-26 stsp msg[i] = buf[i];
505 f8a36e22 2021-08-26 stsp msg[i] = '\0';
506 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_SEND_FAILED, msg);
509 f8a36e22 2021-08-26 stsp static const struct got_error *
510 f8a36e22 2021-08-26 stsp send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
511 f8a36e22 2021-08-26 stsp const char *refname)
513 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
514 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
515 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
517 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_remote_ref) + reflen;
518 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
519 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
521 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
522 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
523 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REMOTE_REF");
525 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_remote_ref definition! */
526 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
527 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
528 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
529 f8a36e22 2021-08-26 stsp return err;
531 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
532 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
533 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
534 f8a36e22 2021-08-26 stsp return err;
536 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
537 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
538 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
539 f8a36e22 2021-08-26 stsp return err;
542 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
543 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
544 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
547 f8a36e22 2021-08-26 stsp static const struct got_error *
548 f8a36e22 2021-08-26 stsp send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
549 f8a36e22 2021-08-26 stsp struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
552 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
553 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
554 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
555 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
556 f8a36e22 2021-08-26 stsp int ref_valid = 0;
559 f8a36e22 2021-08-26 stsp eol = strchr(refname, '\n');
560 f8a36e22 2021-08-26 stsp if (eol == NULL) {
561 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
562 f8a36e22 2021-08-26 stsp "unexpected message from server");
564 f8a36e22 2021-08-26 stsp *eol = '\0';
566 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
567 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
568 f8a36e22 2021-08-26 stsp ref_valid = 1;
572 f8a36e22 2021-08-26 stsp if (!ref_valid) {
573 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
574 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
575 f8a36e22 2021-08-26 stsp ref_valid = 1;
580 f8a36e22 2021-08-26 stsp if (!ref_valid) {
581 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
582 f8a36e22 2021-08-26 stsp "unexpected message from server");
585 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_ref_status) + reflen;
586 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
587 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
589 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
590 f8a36e22 2021-08-26 stsp 0, 0, len);
591 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
592 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REF_STATUS");
594 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_ref_status definition! */
595 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
596 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
597 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
598 f8a36e22 2021-08-26 stsp return err;
600 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
601 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
602 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
603 f8a36e22 2021-08-26 stsp return err;
605 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
606 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
607 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
608 f8a36e22 2021-08-26 stsp return err;
611 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
612 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
613 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
616 f8a36e22 2021-08-26 stsp static const struct got_error *
617 f8a36e22 2021-08-26 stsp describe_refchange(int *n, int *sent_my_capabilites,
618 f8a36e22 2021-08-26 stsp const char *my_capabilities, char *buf, size_t bufsize,
619 f8a36e22 2021-08-26 stsp const char *refname, const char *old_hashstr, const char *new_hashstr)
621 f8a36e22 2021-08-26 stsp *n = snprintf(buf, bufsize, "%s %s %s",
622 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr, refname);
623 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
624 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
627 f8a36e22 2021-08-26 stsp * We must announce our capabilities along with the first
628 f8a36e22 2021-08-26 stsp * reference. Unfortunately, the protocol requires an embedded
629 f8a36e22 2021-08-26 stsp * NUL as a separator between reference name and capabilities,
630 f8a36e22 2021-08-26 stsp * which we have to deal with here.
631 f8a36e22 2021-08-26 stsp * It also requires a linefeed for terminating packet data.
633 f8a36e22 2021-08-26 stsp if (!*sent_my_capabilites && my_capabilities != NULL) {
635 f8a36e22 2021-08-26 stsp if (*n >= bufsize - 1)
636 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
637 f8a36e22 2021-08-26 stsp m = snprintf(buf + *n + 1, /* offset after '\0' */
638 f8a36e22 2021-08-26 stsp bufsize - (*n + 1), "%s\n", my_capabilities);
639 f8a36e22 2021-08-26 stsp if (*n + m >= bufsize)
640 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
642 f8a36e22 2021-08-26 stsp *sent_my_capabilites = 1;
644 f8a36e22 2021-08-26 stsp *n = strlcat(buf, "\n", bufsize);
645 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
646 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
649 f8a36e22 2021-08-26 stsp return NULL;
652 f8a36e22 2021-08-26 stsp static const struct got_error *
653 f8a36e22 2021-08-26 stsp send_pack(int fd, struct got_pathlist_head *refs,
654 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
656 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
657 f8a36e22 2021-08-26 stsp char buf[GOT_FETCH_PKTMAX];
658 f8a36e22 2021-08-26 stsp unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
659 f8a36e22 2021-08-26 stsp char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
660 f8a36e22 2021-08-26 stsp char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
661 f8a36e22 2021-08-26 stsp struct got_pathlist_head their_refs;
662 f8a36e22 2021-08-26 stsp int is_firstpkt = 1;
663 f8a36e22 2021-08-26 stsp int n, nsent = 0;
664 f8a36e22 2021-08-26 stsp int packfd = -1;
665 f8a36e22 2021-08-26 stsp char *id_str = NULL, *refname = NULL;
666 f8a36e22 2021-08-26 stsp struct got_object_id *id = NULL;
667 f8a36e22 2021-08-26 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
668 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
669 f8a36e22 2021-08-26 stsp int sent_my_capabilites = 0;
671 f8a36e22 2021-08-26 stsp TAILQ_INIT(&their_refs);
673 f8a36e22 2021-08-26 stsp if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
674 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_SEND_EMPTY);
676 f8a36e22 2021-08-26 stsp while (1) {
677 f8a36e22 2021-08-26 stsp err = readpkt(&n, fd, buf, sizeof(buf));
680 f8a36e22 2021-08-26 stsp if (n == 0)
682 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
683 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
686 f8a36e22 2021-08-26 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
690 f8a36e22 2021-08-26 stsp if (is_firstpkt) {
691 f8a36e22 2021-08-26 stsp if (chattygot && server_capabilities[0] != '\0')
692 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: server capabilities: %s\n",
693 f8a36e22 2021-08-26 stsp getprogname(), server_capabilities);
694 f8a36e22 2021-08-26 stsp err = match_capabilities(&my_capabilities,
695 f8a36e22 2021-08-26 stsp server_capabilities);
698 f8a36e22 2021-08-26 stsp if (chattygot)
699 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: my capabilities:%s\n",
700 f8a36e22 2021-08-26 stsp getprogname(), my_capabilities);
701 f8a36e22 2021-08-26 stsp is_firstpkt = 0;
703 f8a36e22 2021-08-26 stsp if (strstr(refname, "^{}")) {
704 f8a36e22 2021-08-26 stsp if (chattygot) {
705 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: ignoring %s\n",
706 f8a36e22 2021-08-26 stsp getprogname(), refname);
711 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
712 f8a36e22 2021-08-26 stsp if (id == NULL) {
713 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
716 f8a36e22 2021-08-26 stsp if (!got_parse_sha1_digest(id->sha1, id_str)) {
717 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
720 f8a36e22 2021-08-26 stsp err = send_their_ref(ibuf, id, refname);
724 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&their_refs, refname, id);
725 f8a36e22 2021-08-26 stsp if (chattygot)
726 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: remote has %s %s\n",
727 f8a36e22 2021-08-26 stsp getprogname(), refname, id_str);
728 f8a36e22 2021-08-26 stsp free(id_str);
729 f8a36e22 2021-08-26 stsp id_str = NULL;
730 f8a36e22 2021-08-26 stsp refname = NULL; /* do not free; owned by their_refs */
731 f8a36e22 2021-08-26 stsp id = NULL; /* do not free; owned by their_refs */
734 f8a36e22 2021-08-26 stsp if (!TAILQ_EMPTY(delete_refs)) {
735 f8a36e22 2021-08-26 stsp if (my_capabilities == NULL ||
736 f8a36e22 2021-08-26 stsp strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
737 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CAPA_DELETE_REFS);
742 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
743 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
744 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
745 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
747 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
748 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
749 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
750 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
751 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
755 f8a36e22 2021-08-26 stsp if (their_id == NULL) {
756 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
757 f8a36e22 2021-08-26 stsp "%s does not exist in remote repository",
762 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
763 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
764 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, new_hashstr,
765 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
766 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
767 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
768 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
771 f8a36e22 2021-08-26 stsp err = writepkt(fd, buf, n);
774 f8a36e22 2021-08-26 stsp if (chattygot) {
775 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: deleting %s %s\n",
776 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr);
781 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
782 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
783 f8a36e22 2021-08-26 stsp struct got_object_id *id = pe->data;
784 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
785 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
787 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
788 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
789 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
790 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
791 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
795 f8a36e22 2021-08-26 stsp if (their_id) {
796 f8a36e22 2021-08-26 stsp if (got_object_id_cmp(id, their_id) == 0) {
797 f8a36e22 2021-08-26 stsp if (chattygot) {
798 f8a36e22 2021-08-26 stsp fprintf(stderr,
799 f8a36e22 2021-08-26 stsp "%s: no change for %s\n",
800 f8a36e22 2021-08-26 stsp getprogname(), refname);
804 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
805 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
807 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, old_hashstr,
808 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
810 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(id->sha1, new_hashstr,
811 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
812 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
813 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
814 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
817 f8a36e22 2021-08-26 stsp err = writepkt(fd, buf, n);
820 f8a36e22 2021-08-26 stsp if (chattygot) {
821 f8a36e22 2021-08-26 stsp if (their_id) {
822 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: updating %s %s -> %s\n",
823 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr,
824 f8a36e22 2021-08-26 stsp new_hashstr);
826 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: creating %s %s\n",
827 f8a36e22 2021-08-26 stsp getprogname(), refname, new_hashstr);
832 f8a36e22 2021-08-26 stsp err = flushpkt(fd);
836 f8a36e22 2021-08-26 stsp err = send_pack_request(ibuf);
840 f8a36e22 2021-08-26 stsp err = recv_packfd(&packfd, ibuf);
844 f8a36e22 2021-08-26 stsp err = send_pack_file(fd, packfd, ibuf);
848 f8a36e22 2021-08-26 stsp err = readpkt(&n, fd, buf, sizeof(buf));
851 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
852 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
854 f8a36e22 2021-08-26 stsp } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
855 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
856 f8a36e22 2021-08-26 stsp "unexpected message from server");
860 f8a36e22 2021-08-26 stsp while (nsent > 0) {
861 f8a36e22 2021-08-26 stsp err = readpkt(&n, fd, buf, sizeof(buf));
864 f8a36e22 2021-08-26 stsp if (n < 3) {
865 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
866 f8a36e22 2021-08-26 stsp "unexpected message from server");
868 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ok ", 3) == 0) {
869 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 1,
870 f8a36e22 2021-08-26 stsp refs, delete_refs);
873 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ng ", 3) == 0) {
874 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 0,
875 f8a36e22 2021-08-26 stsp refs, delete_refs);
879 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
880 f8a36e22 2021-08-26 stsp "unexpected message from server");
886 f8a36e22 2021-08-26 stsp err = send_done(ibuf);
888 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &their_refs, entry) {
889 f8a36e22 2021-08-26 stsp free((void *)pe->path);
890 f8a36e22 2021-08-26 stsp free(pe->data);
892 f8a36e22 2021-08-26 stsp got_pathlist_free(&their_refs);
893 f8a36e22 2021-08-26 stsp free(id_str);
895 f8a36e22 2021-08-26 stsp free(refname);
896 f8a36e22 2021-08-26 stsp free(server_capabilities);
897 f8a36e22 2021-08-26 stsp return err;
901 f8a36e22 2021-08-26 stsp main(int argc, char **argv)
903 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
904 f8a36e22 2021-08-26 stsp int sendfd, i;
905 f8a36e22 2021-08-26 stsp struct imsgbuf ibuf;
906 f8a36e22 2021-08-26 stsp struct imsg imsg;
907 f8a36e22 2021-08-26 stsp struct got_pathlist_head refs;
908 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_refs;
909 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
910 f8a36e22 2021-08-26 stsp struct got_imsg_send_request send_req;
911 f8a36e22 2021-08-26 stsp struct got_imsg_send_ref href;
912 f8a36e22 2021-08-26 stsp size_t datalen;
914 f8a36e22 2021-08-26 stsp static int attached;
915 f8a36e22 2021-08-26 stsp while (!attached)
919 f8a36e22 2021-08-26 stsp TAILQ_INIT(&refs);
920 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_refs);
922 f8a36e22 2021-08-26 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
923 f8a36e22 2021-08-26 stsp #ifndef PROFILE
924 f8a36e22 2021-08-26 stsp /* revoke access to most system calls */
925 f8a36e22 2021-08-26 stsp if (pledge("stdio recvfd", NULL) == -1) {
926 f8a36e22 2021-08-26 stsp err = got_error_from_errno("pledge");
927 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);
931 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
932 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
933 f8a36e22 2021-08-26 stsp err = NULL;
936 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
938 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
939 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
942 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
943 f8a36e22 2021-08-26 stsp if (datalen < sizeof(send_req)) {
944 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
947 f8a36e22 2021-08-26 stsp memcpy(&send_req, imsg.data, sizeof(send_req));
948 f8a36e22 2021-08-26 stsp sendfd = imsg.fd;
949 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
951 f8a36e22 2021-08-26 stsp if (send_req.verbosity > 0)
952 f8a36e22 2021-08-26 stsp chattygot += send_req.verbosity;
954 f8a36e22 2021-08-26 stsp for (i = 0; i < send_req.nrefs; i++) {
955 f8a36e22 2021-08-26 stsp struct got_object_id *id;
956 f8a36e22 2021-08-26 stsp char *refname;
958 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
959 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
960 f8a36e22 2021-08-26 stsp err = NULL;
963 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
965 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
966 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
969 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
970 f8a36e22 2021-08-26 stsp if (datalen < sizeof(href)) {
971 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
974 f8a36e22 2021-08-26 stsp memcpy(&href, imsg.data, sizeof(href));
975 f8a36e22 2021-08-26 stsp if (datalen - sizeof(href) < href.name_len) {
976 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
979 f8a36e22 2021-08-26 stsp refname = malloc(href.name_len + 1);
980 f8a36e22 2021-08-26 stsp if (refname == NULL) {
981 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
984 f8a36e22 2021-08-26 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
985 f8a36e22 2021-08-26 stsp refname[href.name_len] = '\0';
988 f8a36e22 2021-08-26 stsp * Prevent sending of references that won't make any
989 f8a36e22 2021-08-26 stsp * sense outside the local repository's context.
991 f8a36e22 2021-08-26 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
992 f8a36e22 2021-08-26 stsp strncmp(refname, "refs/remotes/", 13) == 0) {
993 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
994 f8a36e22 2021-08-26 stsp "%s", refname);
998 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
999 f8a36e22 2021-08-26 stsp if (id == NULL) {
1000 f8a36e22 2021-08-26 stsp free(refname);
1001 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
1002 f8a36e22 2021-08-26 stsp goto done;
1004 f8a36e22 2021-08-26 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1005 f8a36e22 2021-08-26 stsp if (href.delete)
1006 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&delete_refs, refname, id);
1008 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&refs, refname, id);
1009 f8a36e22 2021-08-26 stsp if (err) {
1010 f8a36e22 2021-08-26 stsp free(refname);
1012 f8a36e22 2021-08-26 stsp goto done;
1015 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
1018 f8a36e22 2021-08-26 stsp err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
1020 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
1021 f8a36e22 2021-08-26 stsp free((char *)pe->path);
1022 f8a36e22 2021-08-26 stsp free(pe->data);
1024 f8a36e22 2021-08-26 stsp got_pathlist_free(&refs);
1025 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_refs, entry) {
1026 f8a36e22 2021-08-26 stsp free((char *)pe->path);
1027 f8a36e22 2021-08-26 stsp free(pe->data);
1029 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_refs);
1030 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
1031 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
1032 f8a36e22 2021-08-26 stsp if (err != NULL && err->code != GOT_ERR_CANCELLED) {
1033 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1034 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);