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"
52 f024663d 2021-09-05 stsp #include "got_lib_pkt.h"
53 bd3d9e54 2021-09-05 stsp #include "got_lib_gitproto.h"
55 f8a36e22 2021-08-26 stsp #ifndef nitems
56 f8a36e22 2021-08-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 f8a36e22 2021-08-26 stsp struct got_object *indexed;
60 f8a36e22 2021-08-26 stsp static int chattygot;
62 f8a36e22 2021-08-26 stsp static const struct got_capability got_capabilities[] = {
63 f8a36e22 2021-08-26 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
64 f8a36e22 2021-08-26 stsp { GOT_CAPA_OFS_DELTA, NULL },
66 f8a36e22 2021-08-26 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
68 f8a36e22 2021-08-26 stsp { GOT_CAPA_REPORT_STATUS, NULL },
69 f8a36e22 2021-08-26 stsp { GOT_CAPA_DELETE_REFS, NULL },
72 f8a36e22 2021-08-26 stsp static const struct got_error *
73 f8a36e22 2021-08-26 stsp send_upload_progress(struct imsgbuf *ibuf, off_t bytes)
75 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
76 f8a36e22 2021-08-26 stsp &bytes, sizeof(bytes)) == -1)
77 f8a36e22 2021-08-26 stsp return got_error_from_errno(
78 f8a36e22 2021-08-26 stsp "imsg_compose SEND_UPLOAD_PROGRESS");
80 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
83 f8a36e22 2021-08-26 stsp static const struct got_error *
84 f8a36e22 2021-08-26 stsp send_pack_request(struct imsgbuf *ibuf)
86 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
87 f8a36e22 2021-08-26 stsp NULL, 0) == -1)
88 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
89 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
92 f8a36e22 2021-08-26 stsp static const struct got_error *
93 f8a36e22 2021-08-26 stsp send_done(struct imsgbuf *ibuf)
95 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
96 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_DONE");
97 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
100 f8a36e22 2021-08-26 stsp static const struct got_error *
101 f8a36e22 2021-08-26 stsp recv_packfd(int *packfd, struct imsgbuf *ibuf)
103 f8a36e22 2021-08-26 stsp const struct got_error *err;
104 f8a36e22 2021-08-26 stsp struct imsg imsg;
106 f8a36e22 2021-08-26 stsp *packfd = -1;
108 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
110 f8a36e22 2021-08-26 stsp return err;
112 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP) {
113 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CANCELLED);
117 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
118 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
122 f8a36e22 2021-08-26 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
123 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
127 f8a36e22 2021-08-26 stsp *packfd = imsg.fd;
129 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
130 f8a36e22 2021-08-26 stsp return err;
133 f8a36e22 2021-08-26 stsp static const struct got_error *
134 f8a36e22 2021-08-26 stsp send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
136 f8a36e22 2021-08-26 stsp const struct got_error *err;
137 f8a36e22 2021-08-26 stsp unsigned char buf[8192];
138 f8a36e22 2021-08-26 stsp ssize_t r, w;
139 f8a36e22 2021-08-26 stsp off_t wtotal = 0;
141 f8a36e22 2021-08-26 stsp if (lseek(packfd, 0L, SEEK_SET) == -1)
142 f8a36e22 2021-08-26 stsp return got_error_from_errno("lseek");
145 f8a36e22 2021-08-26 stsp r = read(packfd, buf, sizeof(buf));
146 f8a36e22 2021-08-26 stsp if (r == -1)
147 f8a36e22 2021-08-26 stsp return got_error_from_errno("read");
148 f8a36e22 2021-08-26 stsp if (r == 0)
150 f8a36e22 2021-08-26 stsp w = write(sendfd, buf, r);
151 f8a36e22 2021-08-26 stsp if (w == -1)
152 f8a36e22 2021-08-26 stsp return got_error_from_errno("write");
153 f8a36e22 2021-08-26 stsp if (w != r)
154 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_IO);
155 f8a36e22 2021-08-26 stsp wtotal += w;
156 f8a36e22 2021-08-26 stsp err = send_upload_progress(ibuf, wtotal);
158 f8a36e22 2021-08-26 stsp return err;
161 f8a36e22 2021-08-26 stsp return NULL;
164 f8a36e22 2021-08-26 stsp static const struct got_error *
165 f8a36e22 2021-08-26 stsp send_error(const char *buf, size_t len)
167 f8a36e22 2021-08-26 stsp static char msg[1024];
170 f8a36e22 2021-08-26 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
171 f8a36e22 2021-08-26 stsp if (!isprint(buf[i]))
172 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
173 f8a36e22 2021-08-26 stsp "non-printable error message received from server");
174 f8a36e22 2021-08-26 stsp msg[i] = buf[i];
176 f8a36e22 2021-08-26 stsp msg[i] = '\0';
177 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_SEND_FAILED, msg);
180 f8a36e22 2021-08-26 stsp static const struct got_error *
181 f8a36e22 2021-08-26 stsp send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
182 f8a36e22 2021-08-26 stsp const char *refname)
184 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
185 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
186 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
188 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_remote_ref) + reflen;
189 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
192 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
193 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
194 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REMOTE_REF");
196 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_remote_ref definition! */
197 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
198 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
199 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
200 f8a36e22 2021-08-26 stsp return err;
202 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
203 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
204 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
205 f8a36e22 2021-08-26 stsp return err;
207 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
208 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
209 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
210 f8a36e22 2021-08-26 stsp return err;
213 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
214 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
215 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
218 f8a36e22 2021-08-26 stsp static const struct got_error *
219 f8a36e22 2021-08-26 stsp send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
220 f8a36e22 2021-08-26 stsp struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
222 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
223 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
224 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
225 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
226 f8a36e22 2021-08-26 stsp int ref_valid = 0;
229 f8a36e22 2021-08-26 stsp eol = strchr(refname, '\n');
230 f8a36e22 2021-08-26 stsp if (eol == NULL) {
231 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
232 f8a36e22 2021-08-26 stsp "unexpected message from server");
234 f8a36e22 2021-08-26 stsp *eol = '\0';
236 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
237 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
238 f8a36e22 2021-08-26 stsp ref_valid = 1;
242 f8a36e22 2021-08-26 stsp if (!ref_valid) {
243 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
244 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
245 f8a36e22 2021-08-26 stsp ref_valid = 1;
250 f8a36e22 2021-08-26 stsp if (!ref_valid) {
251 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
252 f8a36e22 2021-08-26 stsp "unexpected message from server");
255 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_ref_status) + reflen;
256 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
257 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
259 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
260 f8a36e22 2021-08-26 stsp 0, 0, len);
261 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
262 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REF_STATUS");
264 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_ref_status definition! */
265 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
266 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
267 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
268 f8a36e22 2021-08-26 stsp return err;
270 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
271 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
272 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
273 f8a36e22 2021-08-26 stsp return err;
275 f8a36e22 2021-08-26 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
276 f8a36e22 2021-08-26 stsp err = got_error_from_errno("imsg_add SEND_REF_STATUS");
277 f8a36e22 2021-08-26 stsp ibuf_free(wbuf);
278 f8a36e22 2021-08-26 stsp return err;
281 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
282 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
283 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
286 f8a36e22 2021-08-26 stsp static const struct got_error *
287 f8a36e22 2021-08-26 stsp describe_refchange(int *n, int *sent_my_capabilites,
288 f8a36e22 2021-08-26 stsp const char *my_capabilities, char *buf, size_t bufsize,
289 f8a36e22 2021-08-26 stsp const char *refname, const char *old_hashstr, const char *new_hashstr)
291 f8a36e22 2021-08-26 stsp *n = snprintf(buf, bufsize, "%s %s %s",
292 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr, refname);
293 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
294 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
297 f8a36e22 2021-08-26 stsp * We must announce our capabilities along with the first
298 f8a36e22 2021-08-26 stsp * reference. Unfortunately, the protocol requires an embedded
299 f8a36e22 2021-08-26 stsp * NUL as a separator between reference name and capabilities,
300 f8a36e22 2021-08-26 stsp * which we have to deal with here.
301 f8a36e22 2021-08-26 stsp * It also requires a linefeed for terminating packet data.
303 f8a36e22 2021-08-26 stsp if (!*sent_my_capabilites && my_capabilities != NULL) {
305 f8a36e22 2021-08-26 stsp if (*n >= bufsize - 1)
306 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
307 f8a36e22 2021-08-26 stsp m = snprintf(buf + *n + 1, /* offset after '\0' */
308 f8a36e22 2021-08-26 stsp bufsize - (*n + 1), "%s\n", my_capabilities);
309 f8a36e22 2021-08-26 stsp if (*n + m >= bufsize)
310 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
312 f8a36e22 2021-08-26 stsp *sent_my_capabilites = 1;
314 f8a36e22 2021-08-26 stsp *n = strlcat(buf, "\n", bufsize);
315 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
316 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
319 f8a36e22 2021-08-26 stsp return NULL;
322 f8a36e22 2021-08-26 stsp static const struct got_error *
323 f8a36e22 2021-08-26 stsp send_pack(int fd, struct got_pathlist_head *refs,
324 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
326 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
327 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
328 f8a36e22 2021-08-26 stsp unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
329 f8a36e22 2021-08-26 stsp char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
330 f8a36e22 2021-08-26 stsp char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
331 f8a36e22 2021-08-26 stsp struct got_pathlist_head their_refs;
332 f8a36e22 2021-08-26 stsp int is_firstpkt = 1;
333 f8a36e22 2021-08-26 stsp int n, nsent = 0;
334 f8a36e22 2021-08-26 stsp int packfd = -1;
335 f8a36e22 2021-08-26 stsp char *id_str = NULL, *refname = NULL;
336 f8a36e22 2021-08-26 stsp struct got_object_id *id = NULL;
337 f8a36e22 2021-08-26 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
338 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
339 f8a36e22 2021-08-26 stsp int sent_my_capabilites = 0;
341 f8a36e22 2021-08-26 stsp TAILQ_INIT(&their_refs);
343 f8a36e22 2021-08-26 stsp if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
344 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_SEND_EMPTY);
346 f8a36e22 2021-08-26 stsp while (1) {
347 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
350 f8a36e22 2021-08-26 stsp if (n == 0)
352 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
353 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
356 e99d9267 2021-10-07 stsp free(id_str);
357 e99d9267 2021-10-07 stsp free(refname);
358 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
359 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
362 f8a36e22 2021-08-26 stsp if (is_firstpkt) {
363 f8a36e22 2021-08-26 stsp if (chattygot && server_capabilities[0] != '\0')
364 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: server capabilities: %s\n",
365 f8a36e22 2021-08-26 stsp getprogname(), server_capabilities);
366 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
367 bd3d9e54 2021-09-05 stsp NULL, server_capabilities, got_capabilities,
368 bd3d9e54 2021-09-05 stsp nitems(got_capabilities));
371 f8a36e22 2021-08-26 stsp if (chattygot)
372 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: my capabilities:%s\n",
373 f8a36e22 2021-08-26 stsp getprogname(), my_capabilities);
374 f8a36e22 2021-08-26 stsp is_firstpkt = 0;
376 f8a36e22 2021-08-26 stsp if (strstr(refname, "^{}")) {
377 f8a36e22 2021-08-26 stsp if (chattygot) {
378 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: ignoring %s\n",
379 f8a36e22 2021-08-26 stsp getprogname(), refname);
384 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
385 f8a36e22 2021-08-26 stsp if (id == NULL) {
386 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
389 f8a36e22 2021-08-26 stsp if (!got_parse_sha1_digest(id->sha1, id_str)) {
390 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
393 f8a36e22 2021-08-26 stsp err = send_their_ref(ibuf, id, refname);
397 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&their_refs, refname, id);
398 f8a36e22 2021-08-26 stsp if (chattygot)
399 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: remote has %s %s\n",
400 f8a36e22 2021-08-26 stsp getprogname(), refname, id_str);
401 f8a36e22 2021-08-26 stsp free(id_str);
402 f8a36e22 2021-08-26 stsp id_str = NULL;
403 f8a36e22 2021-08-26 stsp refname = NULL; /* do not free; owned by their_refs */
404 f8a36e22 2021-08-26 stsp id = NULL; /* do not free; owned by their_refs */
407 f8a36e22 2021-08-26 stsp if (!TAILQ_EMPTY(delete_refs)) {
408 f8a36e22 2021-08-26 stsp if (my_capabilities == NULL ||
409 f8a36e22 2021-08-26 stsp strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
410 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CAPA_DELETE_REFS);
415 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
416 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
417 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
418 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
420 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
421 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
422 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
423 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
424 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
428 f8a36e22 2021-08-26 stsp if (their_id == NULL) {
429 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
430 f8a36e22 2021-08-26 stsp "%s does not exist in remote repository",
435 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
436 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
437 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, new_hashstr,
438 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
439 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
440 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
441 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
444 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
447 f8a36e22 2021-08-26 stsp if (chattygot) {
448 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: deleting %s %s\n",
449 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr);
454 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
455 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
456 f8a36e22 2021-08-26 stsp struct got_object_id *id = pe->data;
457 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
458 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
460 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
461 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
462 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
463 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
464 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
468 f8a36e22 2021-08-26 stsp if (their_id) {
469 f8a36e22 2021-08-26 stsp if (got_object_id_cmp(id, their_id) == 0) {
470 f8a36e22 2021-08-26 stsp if (chattygot) {
471 f8a36e22 2021-08-26 stsp fprintf(stderr,
472 f8a36e22 2021-08-26 stsp "%s: no change for %s\n",
473 f8a36e22 2021-08-26 stsp getprogname(), refname);
477 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
478 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
480 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, old_hashstr,
481 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
483 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(id->sha1, new_hashstr,
484 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
485 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
486 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
487 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
490 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
493 f8a36e22 2021-08-26 stsp if (chattygot) {
494 f8a36e22 2021-08-26 stsp if (their_id) {
495 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: updating %s %s -> %s\n",
496 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr,
497 f8a36e22 2021-08-26 stsp new_hashstr);
499 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: creating %s %s\n",
500 f8a36e22 2021-08-26 stsp getprogname(), refname, new_hashstr);
505 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
509 f8a36e22 2021-08-26 stsp err = send_pack_request(ibuf);
513 f8a36e22 2021-08-26 stsp err = recv_packfd(&packfd, ibuf);
517 27b75514 2021-08-28 stsp if (packfd != -1) {
518 27b75514 2021-08-28 stsp err = send_pack_file(fd, packfd, ibuf);
523 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
526 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
527 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
529 f8a36e22 2021-08-26 stsp } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
530 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
531 f8a36e22 2021-08-26 stsp "unexpected message from server");
535 f8a36e22 2021-08-26 stsp while (nsent > 0) {
536 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
539 f8a36e22 2021-08-26 stsp if (n < 3) {
540 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
541 f8a36e22 2021-08-26 stsp "unexpected message from server");
543 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ok ", 3) == 0) {
544 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 1,
545 abc59930 2021-09-05 naddy refs, delete_refs);
548 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ng ", 3) == 0) {
549 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 0,
550 f8a36e22 2021-08-26 stsp refs, delete_refs);
554 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
555 f8a36e22 2021-08-26 stsp "unexpected message from server");
561 f8a36e22 2021-08-26 stsp err = send_done(ibuf);
563 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &their_refs, entry) {
564 f8a36e22 2021-08-26 stsp free((void *)pe->path);
565 f8a36e22 2021-08-26 stsp free(pe->data);
567 f8a36e22 2021-08-26 stsp got_pathlist_free(&their_refs);
568 f8a36e22 2021-08-26 stsp free(id_str);
570 f8a36e22 2021-08-26 stsp free(refname);
571 f8a36e22 2021-08-26 stsp free(server_capabilities);
572 f8a36e22 2021-08-26 stsp return err;
576 f8a36e22 2021-08-26 stsp main(int argc, char **argv)
578 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
579 030daac8 2021-09-25 stsp int sendfd;
580 f8a36e22 2021-08-26 stsp struct imsgbuf ibuf;
581 f8a36e22 2021-08-26 stsp struct imsg imsg;
582 f8a36e22 2021-08-26 stsp struct got_pathlist_head refs;
583 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_refs;
584 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
585 f8a36e22 2021-08-26 stsp struct got_imsg_send_request send_req;
586 f8a36e22 2021-08-26 stsp struct got_imsg_send_ref href;
587 030daac8 2021-09-25 stsp size_t datalen, i;
589 f8a36e22 2021-08-26 stsp static int attached;
590 f8a36e22 2021-08-26 stsp while (!attached)
594 f8a36e22 2021-08-26 stsp TAILQ_INIT(&refs);
595 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_refs);
597 f8a36e22 2021-08-26 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
598 f8a36e22 2021-08-26 stsp #ifndef PROFILE
599 f8a36e22 2021-08-26 stsp /* revoke access to most system calls */
600 f8a36e22 2021-08-26 stsp if (pledge("stdio recvfd", NULL) == -1) {
601 f8a36e22 2021-08-26 stsp err = got_error_from_errno("pledge");
602 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);
606 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
607 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
608 f8a36e22 2021-08-26 stsp err = NULL;
611 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
613 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
614 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
617 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
618 f8a36e22 2021-08-26 stsp if (datalen < sizeof(send_req)) {
619 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
622 f8a36e22 2021-08-26 stsp memcpy(&send_req, imsg.data, sizeof(send_req));
623 f8a36e22 2021-08-26 stsp sendfd = imsg.fd;
624 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
626 f8a36e22 2021-08-26 stsp if (send_req.verbosity > 0)
627 f8a36e22 2021-08-26 stsp chattygot += send_req.verbosity;
629 f8a36e22 2021-08-26 stsp for (i = 0; i < send_req.nrefs; i++) {
630 f8a36e22 2021-08-26 stsp struct got_object_id *id;
631 f8a36e22 2021-08-26 stsp char *refname;
633 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
634 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
635 f8a36e22 2021-08-26 stsp err = NULL;
638 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
640 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
641 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
644 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
645 f8a36e22 2021-08-26 stsp if (datalen < sizeof(href)) {
646 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
649 f8a36e22 2021-08-26 stsp memcpy(&href, imsg.data, sizeof(href));
650 f8a36e22 2021-08-26 stsp if (datalen - sizeof(href) < href.name_len) {
651 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
654 f8a36e22 2021-08-26 stsp refname = malloc(href.name_len + 1);
655 f8a36e22 2021-08-26 stsp if (refname == NULL) {
656 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
659 f8a36e22 2021-08-26 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
660 f8a36e22 2021-08-26 stsp refname[href.name_len] = '\0';
663 f8a36e22 2021-08-26 stsp * Prevent sending of references that won't make any
664 f8a36e22 2021-08-26 stsp * sense outside the local repository's context.
666 f8a36e22 2021-08-26 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
667 f8a36e22 2021-08-26 stsp strncmp(refname, "refs/remotes/", 13) == 0) {
668 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
669 f8a36e22 2021-08-26 stsp "%s", refname);
673 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
674 f8a36e22 2021-08-26 stsp if (id == NULL) {
675 f8a36e22 2021-08-26 stsp free(refname);
676 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
679 f8a36e22 2021-08-26 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
680 f8a36e22 2021-08-26 stsp if (href.delete)
681 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&delete_refs, refname, id);
683 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&refs, refname, id);
685 f8a36e22 2021-08-26 stsp free(refname);
690 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
693 f8a36e22 2021-08-26 stsp err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
695 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
696 f8a36e22 2021-08-26 stsp free((char *)pe->path);
697 f8a36e22 2021-08-26 stsp free(pe->data);
699 f8a36e22 2021-08-26 stsp got_pathlist_free(&refs);
700 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_refs, entry) {
701 f8a36e22 2021-08-26 stsp free((char *)pe->path);
702 f8a36e22 2021-08-26 stsp free(pe->data);
704 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_refs);
705 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
706 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
707 f8a36e22 2021-08-26 stsp if (err != NULL && err->code != GOT_ERR_CANCELLED) {
708 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
709 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);