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 8b925c6c 2022-07-16 thomas #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 <limits.h>
27 f8a36e22 2021-08-26 stsp #include <signal.h>
28 f8a36e22 2021-08-26 stsp #include <stdio.h>
29 f8a36e22 2021-08-26 stsp #include <stdlib.h>
30 f8a36e22 2021-08-26 stsp #include <string.h>
31 f8a36e22 2021-08-26 stsp #include <ctype.h>
32 f8a36e22 2021-08-26 stsp #include <fcntl.h>
33 f8a36e22 2021-08-26 stsp #include <unistd.h>
34 f8a36e22 2021-08-26 stsp #include <zlib.h>
35 f8a36e22 2021-08-26 stsp #include <err.h>
37 f8a36e22 2021-08-26 stsp #include "got_error.h"
38 f8a36e22 2021-08-26 stsp #include "got_object.h"
39 f8a36e22 2021-08-26 stsp #include "got_path.h"
40 f8a36e22 2021-08-26 stsp #include "got_version.h"
41 f8a36e22 2021-08-26 stsp #include "got_fetch.h"
42 f8a36e22 2021-08-26 stsp #include "got_reference.h"
44 f8a36e22 2021-08-26 stsp #include "got_lib_sha1.h"
45 f8a36e22 2021-08-26 stsp #include "got_lib_delta.h"
46 f8a36e22 2021-08-26 stsp #include "got_lib_object.h"
47 f8a36e22 2021-08-26 stsp #include "got_lib_object_parse.h"
48 f8a36e22 2021-08-26 stsp #include "got_lib_privsep.h"
49 f8a36e22 2021-08-26 stsp #include "got_lib_pack.h"
50 f024663d 2021-09-05 stsp #include "got_lib_pkt.h"
51 bd3d9e54 2021-09-05 stsp #include "got_lib_gitproto.h"
52 b7e51f67 2022-02-23 thomas #include "got_lib_ratelimit.h"
54 f8a36e22 2021-08-26 stsp #ifndef nitems
55 f8a36e22 2021-08-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 f8a36e22 2021-08-26 stsp struct got_object *indexed;
59 f8a36e22 2021-08-26 stsp static int chattygot;
61 f8a36e22 2021-08-26 stsp static const struct got_capability got_capabilities[] = {
62 f8a36e22 2021-08-26 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
63 f8a36e22 2021-08-26 stsp { GOT_CAPA_OFS_DELTA, NULL },
65 f8a36e22 2021-08-26 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
67 f8a36e22 2021-08-26 stsp { GOT_CAPA_REPORT_STATUS, NULL },
68 f8a36e22 2021-08-26 stsp { GOT_CAPA_DELETE_REFS, NULL },
71 f8a36e22 2021-08-26 stsp static const struct got_error *
72 b7e51f67 2022-02-23 thomas send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
73 b7e51f67 2022-02-23 thomas struct got_ratelimit *rl)
75 b7e51f67 2022-02-23 thomas const struct got_error *err = NULL;
76 b7e51f67 2022-02-23 thomas int elapsed = 0;
79 b7e51f67 2022-02-23 thomas err = got_ratelimit_check(&elapsed, rl);
80 b7e51f67 2022-02-23 thomas if (err || !elapsed)
81 b7e51f67 2022-02-23 thomas return err;
84 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
85 f8a36e22 2021-08-26 stsp &bytes, sizeof(bytes)) == -1)
86 f8a36e22 2021-08-26 stsp return got_error_from_errno(
87 f8a36e22 2021-08-26 stsp "imsg_compose SEND_UPLOAD_PROGRESS");
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_pack_request(struct imsgbuf *ibuf)
95 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
96 f8a36e22 2021-08-26 stsp NULL, 0) == -1)
97 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
98 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
101 f8a36e22 2021-08-26 stsp static const struct got_error *
102 f8a36e22 2021-08-26 stsp send_done(struct imsgbuf *ibuf)
104 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
105 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_DONE");
106 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
109 f8a36e22 2021-08-26 stsp static const struct got_error *
110 f8a36e22 2021-08-26 stsp recv_packfd(int *packfd, struct imsgbuf *ibuf)
112 f8a36e22 2021-08-26 stsp const struct got_error *err;
113 f8a36e22 2021-08-26 stsp struct imsg imsg;
115 f8a36e22 2021-08-26 stsp *packfd = -1;
117 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
119 f8a36e22 2021-08-26 stsp return err;
121 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP) {
122 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CANCELLED);
126 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
127 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
131 f8a36e22 2021-08-26 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
132 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
136 f8a36e22 2021-08-26 stsp *packfd = imsg.fd;
138 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
139 f8a36e22 2021-08-26 stsp return err;
142 f8a36e22 2021-08-26 stsp static const struct got_error *
143 f8a36e22 2021-08-26 stsp send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
145 f8a36e22 2021-08-26 stsp const struct got_error *err;
146 f8a36e22 2021-08-26 stsp unsigned char buf[8192];
147 f8a36e22 2021-08-26 stsp ssize_t r, w;
148 f8a36e22 2021-08-26 stsp off_t wtotal = 0;
149 b7e51f67 2022-02-23 thomas struct got_ratelimit rl;
151 f8a36e22 2021-08-26 stsp if (lseek(packfd, 0L, SEEK_SET) == -1)
152 f8a36e22 2021-08-26 stsp return got_error_from_errno("lseek");
154 b7e51f67 2022-02-23 thomas got_ratelimit_init(&rl, 0, 500);
157 f8a36e22 2021-08-26 stsp r = read(packfd, buf, sizeof(buf));
158 f8a36e22 2021-08-26 stsp if (r == -1)
159 f8a36e22 2021-08-26 stsp return got_error_from_errno("read");
160 f8a36e22 2021-08-26 stsp if (r == 0)
162 f8a36e22 2021-08-26 stsp w = write(sendfd, buf, r);
163 f8a36e22 2021-08-26 stsp if (w == -1)
164 f8a36e22 2021-08-26 stsp return got_error_from_errno("write");
165 f8a36e22 2021-08-26 stsp if (w != r)
166 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_IO);
167 f8a36e22 2021-08-26 stsp wtotal += w;
168 b7e51f67 2022-02-23 thomas err = send_upload_progress(ibuf, wtotal, &rl);
170 f8a36e22 2021-08-26 stsp return err;
173 b7e51f67 2022-02-23 thomas return send_upload_progress(ibuf, wtotal, NULL);
176 f8a36e22 2021-08-26 stsp static const struct got_error *
177 f8a36e22 2021-08-26 stsp send_error(const char *buf, size_t len)
179 f8a36e22 2021-08-26 stsp static char msg[1024];
182 f8a36e22 2021-08-26 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
183 f8a36e22 2021-08-26 stsp if (!isprint(buf[i]))
184 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
185 f8a36e22 2021-08-26 stsp "non-printable error message received from server");
186 f8a36e22 2021-08-26 stsp msg[i] = buf[i];
188 f8a36e22 2021-08-26 stsp msg[i] = '\0';
189 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_SEND_FAILED, msg);
192 f8a36e22 2021-08-26 stsp static const struct got_error *
193 f8a36e22 2021-08-26 stsp send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
194 f8a36e22 2021-08-26 stsp const char *refname)
196 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
197 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
199 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_remote_ref) + reflen;
200 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
201 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
203 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
204 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
205 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REMOTE_REF");
207 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_remote_ref definition! */
208 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
209 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REMOTE_REF");
210 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
211 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REMOTE_REF");
212 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refname, reflen) == -1)
213 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REMOTE_REF");
215 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
216 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
217 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
220 f8a36e22 2021-08-26 stsp static const struct got_error *
221 f8a36e22 2021-08-26 stsp send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
222 f8a36e22 2021-08-26 stsp struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
224 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
225 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
226 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
227 f8a36e22 2021-08-26 stsp int ref_valid = 0;
230 f8a36e22 2021-08-26 stsp eol = strchr(refname, '\n');
231 f8a36e22 2021-08-26 stsp if (eol == NULL) {
232 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
233 f8a36e22 2021-08-26 stsp "unexpected message from server");
235 f8a36e22 2021-08-26 stsp *eol = '\0';
237 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
238 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
239 f8a36e22 2021-08-26 stsp ref_valid = 1;
243 f8a36e22 2021-08-26 stsp if (!ref_valid) {
244 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
245 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
246 f8a36e22 2021-08-26 stsp ref_valid = 1;
251 f8a36e22 2021-08-26 stsp if (!ref_valid) {
252 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
253 f8a36e22 2021-08-26 stsp "unexpected message from server");
256 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_ref_status) + reflen;
257 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
258 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
260 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
261 f8a36e22 2021-08-26 stsp 0, 0, len);
262 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
263 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REF_STATUS");
265 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_ref_status definition! */
266 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &success, sizeof(success)) == -1)
267 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REF_STATUS");
268 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
269 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REF_STATUS");
270 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refname, reflen) == -1)
271 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REF_STATUS");
273 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
274 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
275 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
278 f8a36e22 2021-08-26 stsp static const struct got_error *
279 f8a36e22 2021-08-26 stsp describe_refchange(int *n, int *sent_my_capabilites,
280 f8a36e22 2021-08-26 stsp const char *my_capabilities, char *buf, size_t bufsize,
281 f8a36e22 2021-08-26 stsp const char *refname, const char *old_hashstr, const char *new_hashstr)
283 f8a36e22 2021-08-26 stsp *n = snprintf(buf, bufsize, "%s %s %s",
284 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr, refname);
285 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
286 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
289 f8a36e22 2021-08-26 stsp * We must announce our capabilities along with the first
290 f8a36e22 2021-08-26 stsp * reference. Unfortunately, the protocol requires an embedded
291 f8a36e22 2021-08-26 stsp * NUL as a separator between reference name and capabilities,
292 f8a36e22 2021-08-26 stsp * which we have to deal with here.
293 f8a36e22 2021-08-26 stsp * It also requires a linefeed for terminating packet data.
295 f8a36e22 2021-08-26 stsp if (!*sent_my_capabilites && my_capabilities != NULL) {
297 f8a36e22 2021-08-26 stsp if (*n >= bufsize - 1)
298 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
299 f8a36e22 2021-08-26 stsp m = snprintf(buf + *n + 1, /* offset after '\0' */
300 f8a36e22 2021-08-26 stsp bufsize - (*n + 1), "%s\n", my_capabilities);
301 f8a36e22 2021-08-26 stsp if (*n + m >= bufsize)
302 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
304 f8a36e22 2021-08-26 stsp *sent_my_capabilites = 1;
306 f8a36e22 2021-08-26 stsp *n = strlcat(buf, "\n", bufsize);
307 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
308 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
311 f8a36e22 2021-08-26 stsp return NULL;
314 f8a36e22 2021-08-26 stsp static const struct got_error *
315 f8a36e22 2021-08-26 stsp send_pack(int fd, struct got_pathlist_head *refs,
316 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
318 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
319 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
320 fa8129f7 2022-03-22 thomas const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
321 f8a36e22 2021-08-26 stsp char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
322 f8a36e22 2021-08-26 stsp char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
323 f8a36e22 2021-08-26 stsp struct got_pathlist_head their_refs;
324 f8a36e22 2021-08-26 stsp int is_firstpkt = 1;
325 f8a36e22 2021-08-26 stsp int n, nsent = 0;
326 f8a36e22 2021-08-26 stsp int packfd = -1;
327 f8a36e22 2021-08-26 stsp char *id_str = NULL, *refname = NULL;
328 f8a36e22 2021-08-26 stsp struct got_object_id *id = NULL;
329 f8a36e22 2021-08-26 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
330 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
331 f8a36e22 2021-08-26 stsp int sent_my_capabilites = 0;
333 f8a36e22 2021-08-26 stsp TAILQ_INIT(&their_refs);
335 f8a36e22 2021-08-26 stsp if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
336 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_SEND_EMPTY);
338 f8a36e22 2021-08-26 stsp while (1) {
339 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
342 f8a36e22 2021-08-26 stsp if (n == 0)
344 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
345 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
348 35add24a 2021-10-08 thomas free(id_str);
349 35add24a 2021-10-08 thomas free(refname);
350 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
351 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
354 f8a36e22 2021-08-26 stsp if (is_firstpkt) {
355 f8a36e22 2021-08-26 stsp if (chattygot && server_capabilities[0] != '\0')
356 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: server capabilities: %s\n",
357 f8a36e22 2021-08-26 stsp getprogname(), server_capabilities);
358 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
359 bd3d9e54 2021-09-05 stsp NULL, server_capabilities, got_capabilities,
360 bd3d9e54 2021-09-05 stsp nitems(got_capabilities));
363 f8a36e22 2021-08-26 stsp if (chattygot)
364 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: my capabilities:%s\n",
365 f8a36e22 2021-08-26 stsp getprogname(), my_capabilities);
366 f8a36e22 2021-08-26 stsp is_firstpkt = 0;
368 f8a36e22 2021-08-26 stsp if (strstr(refname, "^{}")) {
369 f8a36e22 2021-08-26 stsp if (chattygot) {
370 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: ignoring %s\n",
371 f8a36e22 2021-08-26 stsp getprogname(), refname);
376 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
377 f8a36e22 2021-08-26 stsp if (id == NULL) {
378 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
381 f8a36e22 2021-08-26 stsp if (!got_parse_sha1_digest(id->sha1, id_str)) {
382 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
385 f8a36e22 2021-08-26 stsp err = send_their_ref(ibuf, id, refname);
389 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&their_refs, refname, id);
390 f8a36e22 2021-08-26 stsp if (chattygot)
391 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: remote has %s %s\n",
392 f8a36e22 2021-08-26 stsp getprogname(), refname, id_str);
393 f8a36e22 2021-08-26 stsp free(id_str);
394 f8a36e22 2021-08-26 stsp id_str = NULL;
395 f8a36e22 2021-08-26 stsp refname = NULL; /* do not free; owned by their_refs */
396 f8a36e22 2021-08-26 stsp id = NULL; /* do not free; owned by their_refs */
399 f8a36e22 2021-08-26 stsp if (!TAILQ_EMPTY(delete_refs)) {
400 f8a36e22 2021-08-26 stsp if (my_capabilities == NULL ||
401 f8a36e22 2021-08-26 stsp strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
402 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CAPA_DELETE_REFS);
407 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
408 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
409 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
410 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
412 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
413 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
414 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
415 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
416 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
420 f8a36e22 2021-08-26 stsp if (their_id == NULL) {
421 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
422 f8a36e22 2021-08-26 stsp "%s does not exist in remote repository",
427 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
428 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
429 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, new_hashstr,
430 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
431 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
432 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
433 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
436 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
439 f8a36e22 2021-08-26 stsp if (chattygot) {
440 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: deleting %s %s\n",
441 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr);
446 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
447 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
448 f8a36e22 2021-08-26 stsp struct got_object_id *id = pe->data;
449 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
450 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
452 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
453 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
454 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
455 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
456 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
460 f8a36e22 2021-08-26 stsp if (their_id) {
461 f8a36e22 2021-08-26 stsp if (got_object_id_cmp(id, their_id) == 0) {
462 f8a36e22 2021-08-26 stsp if (chattygot) {
463 f8a36e22 2021-08-26 stsp fprintf(stderr,
464 f8a36e22 2021-08-26 stsp "%s: no change for %s\n",
465 f8a36e22 2021-08-26 stsp getprogname(), refname);
469 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
470 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
472 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, old_hashstr,
473 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
475 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(id->sha1, new_hashstr,
476 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
477 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
478 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
479 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
482 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
485 f8a36e22 2021-08-26 stsp if (chattygot) {
486 f8a36e22 2021-08-26 stsp if (their_id) {
487 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: updating %s %s -> %s\n",
488 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr,
489 f8a36e22 2021-08-26 stsp new_hashstr);
491 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: creating %s %s\n",
492 f8a36e22 2021-08-26 stsp getprogname(), refname, new_hashstr);
497 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
501 f8a36e22 2021-08-26 stsp err = send_pack_request(ibuf);
505 f8a36e22 2021-08-26 stsp err = recv_packfd(&packfd, ibuf);
509 27b75514 2021-08-28 stsp if (packfd != -1) {
510 27b75514 2021-08-28 stsp err = send_pack_file(fd, packfd, ibuf);
515 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
518 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
519 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
521 f8a36e22 2021-08-26 stsp } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
522 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
523 f8a36e22 2021-08-26 stsp "unexpected message from server");
527 f8a36e22 2021-08-26 stsp while (nsent > 0) {
528 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
531 f8a36e22 2021-08-26 stsp if (n < 3) {
532 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
533 f8a36e22 2021-08-26 stsp "unexpected message from server");
535 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ok ", 3) == 0) {
536 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 1,
537 abc59930 2021-09-05 naddy refs, delete_refs);
540 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ng ", 3) == 0) {
541 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 0,
542 f8a36e22 2021-08-26 stsp refs, delete_refs);
546 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
547 f8a36e22 2021-08-26 stsp "unexpected message from server");
553 f8a36e22 2021-08-26 stsp err = send_done(ibuf);
555 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &their_refs, entry) {
556 f8a36e22 2021-08-26 stsp free((void *)pe->path);
557 f8a36e22 2021-08-26 stsp free(pe->data);
559 f8a36e22 2021-08-26 stsp got_pathlist_free(&their_refs);
560 f8a36e22 2021-08-26 stsp free(id_str);
562 f8a36e22 2021-08-26 stsp free(refname);
563 f8a36e22 2021-08-26 stsp free(server_capabilities);
564 f8a36e22 2021-08-26 stsp return err;
568 f8a36e22 2021-08-26 stsp main(int argc, char **argv)
570 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
571 01bb5a15 2021-09-25 thomas.ad int sendfd;
572 f8a36e22 2021-08-26 stsp struct imsgbuf ibuf;
573 f8a36e22 2021-08-26 stsp struct imsg imsg;
574 f8a36e22 2021-08-26 stsp struct got_pathlist_head refs;
575 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_refs;
576 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
577 f8a36e22 2021-08-26 stsp struct got_imsg_send_request send_req;
578 f8a36e22 2021-08-26 stsp struct got_imsg_send_ref href;
579 01bb5a15 2021-09-25 thomas.ad size_t datalen, i;
581 f8a36e22 2021-08-26 stsp static int attached;
582 f8a36e22 2021-08-26 stsp while (!attached)
586 f8a36e22 2021-08-26 stsp TAILQ_INIT(&refs);
587 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_refs);
589 f8a36e22 2021-08-26 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
590 f8a36e22 2021-08-26 stsp #ifndef PROFILE
591 f8a36e22 2021-08-26 stsp /* revoke access to most system calls */
592 f8a36e22 2021-08-26 stsp if (pledge("stdio recvfd", NULL) == -1) {
593 f8a36e22 2021-08-26 stsp err = got_error_from_errno("pledge");
594 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);
598 97799ccd 2022-02-06 thomas /* revoke fs access */
599 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
600 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
601 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
602 97799ccd 2022-02-06 thomas return 1;
604 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
605 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
606 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
610 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
611 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
612 f8a36e22 2021-08-26 stsp err = NULL;
615 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
617 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
618 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
621 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
622 f8a36e22 2021-08-26 stsp if (datalen < sizeof(send_req)) {
623 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
626 f8a36e22 2021-08-26 stsp memcpy(&send_req, imsg.data, sizeof(send_req));
627 f8a36e22 2021-08-26 stsp sendfd = imsg.fd;
628 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
630 f8a36e22 2021-08-26 stsp if (send_req.verbosity > 0)
631 f8a36e22 2021-08-26 stsp chattygot += send_req.verbosity;
633 f8a36e22 2021-08-26 stsp for (i = 0; i < send_req.nrefs; i++) {
634 f8a36e22 2021-08-26 stsp struct got_object_id *id;
635 f8a36e22 2021-08-26 stsp char *refname;
637 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
638 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
639 f8a36e22 2021-08-26 stsp err = NULL;
642 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
644 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
645 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
648 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
649 f8a36e22 2021-08-26 stsp if (datalen < sizeof(href)) {
650 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
653 f8a36e22 2021-08-26 stsp memcpy(&href, imsg.data, sizeof(href));
654 f8a36e22 2021-08-26 stsp if (datalen - sizeof(href) < href.name_len) {
655 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
658 f8a36e22 2021-08-26 stsp refname = malloc(href.name_len + 1);
659 f8a36e22 2021-08-26 stsp if (refname == NULL) {
660 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
663 f8a36e22 2021-08-26 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
664 f8a36e22 2021-08-26 stsp refname[href.name_len] = '\0';
667 f8a36e22 2021-08-26 stsp * Prevent sending of references that won't make any
668 f8a36e22 2021-08-26 stsp * sense outside the local repository's context.
670 f8a36e22 2021-08-26 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
671 f8a36e22 2021-08-26 stsp strncmp(refname, "refs/remotes/", 13) == 0) {
672 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
673 f8a36e22 2021-08-26 stsp "%s", refname);
677 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
678 f8a36e22 2021-08-26 stsp if (id == NULL) {
679 f8a36e22 2021-08-26 stsp free(refname);
680 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
683 f8a36e22 2021-08-26 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
684 f8a36e22 2021-08-26 stsp if (href.delete)
685 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&delete_refs, refname, id);
687 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&refs, refname, id);
689 f8a36e22 2021-08-26 stsp free(refname);
694 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
697 f8a36e22 2021-08-26 stsp err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
699 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
700 f8a36e22 2021-08-26 stsp free((char *)pe->path);
701 f8a36e22 2021-08-26 stsp free(pe->data);
703 f8a36e22 2021-08-26 stsp got_pathlist_free(&refs);
704 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_refs, entry) {
705 f8a36e22 2021-08-26 stsp free((char *)pe->path);
706 f8a36e22 2021-08-26 stsp free(pe->data);
708 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_refs);
709 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
710 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
711 f8a36e22 2021-08-26 stsp if (err != NULL && err->code != GOT_ERR_CANCELLED) {
712 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
713 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);