Blame


1 f8a36e22 2021-08-26 stsp /*
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>
4 f8a36e22 2021-08-26 stsp *
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.
8 f8a36e22 2021-08-26 stsp *
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.
16 f8a36e22 2021-08-26 stsp */
17 f8a36e22 2021-08-26 stsp
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>
23 f8a36e22 2021-08-26 stsp
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>
36 f8a36e22 2021-08-26 stsp
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"
43 f8a36e22 2021-08-26 stsp
44 be288a59 2023-02-23 thomas #include "got_lib_hash.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"
53 c0876f4c 2022-10-31 thomas #include "got_lib_poll.h"
54 f8a36e22 2021-08-26 stsp
55 f8a36e22 2021-08-26 stsp #ifndef nitems
56 f8a36e22 2021-08-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 f8a36e22 2021-08-26 stsp #endif
58 f8a36e22 2021-08-26 stsp
59 f8a36e22 2021-08-26 stsp struct got_object *indexed;
60 f8a36e22 2021-08-26 stsp static int chattygot;
61 f8a36e22 2021-08-26 stsp
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 },
65 f8a36e22 2021-08-26 stsp #if 0
66 f8a36e22 2021-08-26 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
67 f8a36e22 2021-08-26 stsp #endif
68 f8a36e22 2021-08-26 stsp { GOT_CAPA_REPORT_STATUS, NULL },
69 f8a36e22 2021-08-26 stsp { GOT_CAPA_DELETE_REFS, NULL },
70 f8a36e22 2021-08-26 stsp };
71 f8a36e22 2021-08-26 stsp
72 f8a36e22 2021-08-26 stsp static const struct got_error *
73 b7e51f67 2022-02-23 thomas send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
74 b7e51f67 2022-02-23 thomas struct got_ratelimit *rl)
75 f8a36e22 2021-08-26 stsp {
76 b7e51f67 2022-02-23 thomas const struct got_error *err = NULL;
77 b7e51f67 2022-02-23 thomas int elapsed = 0;
78 b7e51f67 2022-02-23 thomas
79 b7e51f67 2022-02-23 thomas if (rl) {
80 b7e51f67 2022-02-23 thomas err = got_ratelimit_check(&elapsed, rl);
81 b7e51f67 2022-02-23 thomas if (err || !elapsed)
82 b7e51f67 2022-02-23 thomas return err;
83 b7e51f67 2022-02-23 thomas }
84 b7e51f67 2022-02-23 thomas
85 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
86 f8a36e22 2021-08-26 stsp &bytes, sizeof(bytes)) == -1)
87 f8a36e22 2021-08-26 stsp return got_error_from_errno(
88 f8a36e22 2021-08-26 stsp "imsg_compose SEND_UPLOAD_PROGRESS");
89 f8a36e22 2021-08-26 stsp
90 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
91 f8a36e22 2021-08-26 stsp }
92 f8a36e22 2021-08-26 stsp
93 f8a36e22 2021-08-26 stsp static const struct got_error *
94 f8a36e22 2021-08-26 stsp send_pack_request(struct imsgbuf *ibuf)
95 f8a36e22 2021-08-26 stsp {
96 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
97 f8a36e22 2021-08-26 stsp NULL, 0) == -1)
98 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
99 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
100 f8a36e22 2021-08-26 stsp }
101 f8a36e22 2021-08-26 stsp
102 f8a36e22 2021-08-26 stsp static const struct got_error *
103 f8a36e22 2021-08-26 stsp send_done(struct imsgbuf *ibuf)
104 f8a36e22 2021-08-26 stsp {
105 f8a36e22 2021-08-26 stsp if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
106 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_compose SEND_DONE");
107 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
108 f8a36e22 2021-08-26 stsp }
109 f8a36e22 2021-08-26 stsp
110 f8a36e22 2021-08-26 stsp static const struct got_error *
111 f8a36e22 2021-08-26 stsp recv_packfd(int *packfd, struct imsgbuf *ibuf)
112 f8a36e22 2021-08-26 stsp {
113 f8a36e22 2021-08-26 stsp const struct got_error *err;
114 f8a36e22 2021-08-26 stsp struct imsg imsg;
115 f8a36e22 2021-08-26 stsp
116 f8a36e22 2021-08-26 stsp *packfd = -1;
117 f8a36e22 2021-08-26 stsp
118 f8a36e22 2021-08-26 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
119 f8a36e22 2021-08-26 stsp if (err)
120 f8a36e22 2021-08-26 stsp return err;
121 b6b86fd1 2022-08-30 thomas
122 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP) {
123 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CANCELLED);
124 f8a36e22 2021-08-26 stsp goto done;
125 f8a36e22 2021-08-26 stsp }
126 f8a36e22 2021-08-26 stsp
127 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
128 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
129 f8a36e22 2021-08-26 stsp goto done;
130 f8a36e22 2021-08-26 stsp }
131 f8a36e22 2021-08-26 stsp
132 f8a36e22 2021-08-26 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
133 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
134 f8a36e22 2021-08-26 stsp goto done;
135 f8a36e22 2021-08-26 stsp }
136 f8a36e22 2021-08-26 stsp
137 f8a36e22 2021-08-26 stsp *packfd = imsg.fd;
138 f8a36e22 2021-08-26 stsp done:
139 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
140 f8a36e22 2021-08-26 stsp return err;
141 f8a36e22 2021-08-26 stsp }
142 f8a36e22 2021-08-26 stsp
143 f8a36e22 2021-08-26 stsp static const struct got_error *
144 f8a36e22 2021-08-26 stsp send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
145 f8a36e22 2021-08-26 stsp {
146 f8a36e22 2021-08-26 stsp const struct got_error *err;
147 f8a36e22 2021-08-26 stsp unsigned char buf[8192];
148 c0876f4c 2022-10-31 thomas ssize_t r;
149 f8a36e22 2021-08-26 stsp off_t wtotal = 0;
150 b7e51f67 2022-02-23 thomas struct got_ratelimit rl;
151 f8a36e22 2021-08-26 stsp
152 f8a36e22 2021-08-26 stsp if (lseek(packfd, 0L, SEEK_SET) == -1)
153 f8a36e22 2021-08-26 stsp return got_error_from_errno("lseek");
154 f8a36e22 2021-08-26 stsp
155 b7e51f67 2022-02-23 thomas got_ratelimit_init(&rl, 0, 500);
156 b7e51f67 2022-02-23 thomas
157 f8a36e22 2021-08-26 stsp for (;;) {
158 f8a36e22 2021-08-26 stsp r = read(packfd, buf, sizeof(buf));
159 f8a36e22 2021-08-26 stsp if (r == -1)
160 f8a36e22 2021-08-26 stsp return got_error_from_errno("read");
161 f8a36e22 2021-08-26 stsp if (r == 0)
162 f8a36e22 2021-08-26 stsp break;
163 c0876f4c 2022-10-31 thomas err = got_poll_write_full(sendfd, buf, r);
164 c0876f4c 2022-10-31 thomas if (err)
165 c0876f4c 2022-10-31 thomas return NULL;
166 c0876f4c 2022-10-31 thomas wtotal += r;
167 b7e51f67 2022-02-23 thomas err = send_upload_progress(ibuf, wtotal, &rl);
168 f8a36e22 2021-08-26 stsp if (err)
169 f8a36e22 2021-08-26 stsp return err;
170 f8a36e22 2021-08-26 stsp }
171 f8a36e22 2021-08-26 stsp
172 b7e51f67 2022-02-23 thomas return send_upload_progress(ibuf, wtotal, NULL);
173 f8a36e22 2021-08-26 stsp }
174 f8a36e22 2021-08-26 stsp
175 f8a36e22 2021-08-26 stsp static const struct got_error *
176 f8a36e22 2021-08-26 stsp send_error(const char *buf, size_t len)
177 f8a36e22 2021-08-26 stsp {
178 f8a36e22 2021-08-26 stsp static char msg[1024];
179 f8a36e22 2021-08-26 stsp size_t i;
180 f8a36e22 2021-08-26 stsp
181 f8a36e22 2021-08-26 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
182 6771d425 2022-11-17 thomas if (!isprint((unsigned char)buf[i]))
183 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
184 f8a36e22 2021-08-26 stsp "non-printable error message received from server");
185 f8a36e22 2021-08-26 stsp msg[i] = buf[i];
186 f8a36e22 2021-08-26 stsp }
187 f8a36e22 2021-08-26 stsp msg[i] = '\0';
188 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_SEND_FAILED, msg);
189 f8a36e22 2021-08-26 stsp }
190 f8a36e22 2021-08-26 stsp
191 f8a36e22 2021-08-26 stsp static const struct got_error *
192 f8a36e22 2021-08-26 stsp send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
193 f8a36e22 2021-08-26 stsp const char *refname)
194 f8a36e22 2021-08-26 stsp {
195 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
196 f8a36e22 2021-08-26 stsp size_t len, reflen = strlen(refname);
197 f8a36e22 2021-08-26 stsp
198 f8a36e22 2021-08-26 stsp len = sizeof(struct got_imsg_send_remote_ref) + reflen;
199 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
200 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
201 f8a36e22 2021-08-26 stsp
202 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
203 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
204 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REMOTE_REF");
205 f8a36e22 2021-08-26 stsp
206 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_remote_ref definition! */
207 0f23036a 2023-02-03 thomas if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
208 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REMOTE_REF");
209 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
210 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REMOTE_REF");
211 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refname, reflen) == -1)
212 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REMOTE_REF");
213 f8a36e22 2021-08-26 stsp
214 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
215 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
216 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
217 f8a36e22 2021-08-26 stsp }
218 f8a36e22 2021-08-26 stsp
219 f8a36e22 2021-08-26 stsp static const struct got_error *
220 f8a36e22 2021-08-26 stsp send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
221 f8a36e22 2021-08-26 stsp struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
222 f8a36e22 2021-08-26 stsp {
223 f8a36e22 2021-08-26 stsp struct ibuf *wbuf;
224 3fe5d0fe 2022-11-17 thomas size_t i, len, reflen, errmsglen = 0;
225 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
226 f8a36e22 2021-08-26 stsp int ref_valid = 0;
227 3fe5d0fe 2022-11-17 thomas char *eol, *sp;
228 3fe5d0fe 2022-11-17 thomas const char *errmsg = "";
229 f8a36e22 2021-08-26 stsp
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");
234 f8a36e22 2021-08-26 stsp }
235 f8a36e22 2021-08-26 stsp *eol = '\0';
236 3fe5d0fe 2022-11-17 thomas
237 3fe5d0fe 2022-11-17 thomas sp = strchr(refname, ' ');
238 3fe5d0fe 2022-11-17 thomas if (sp != NULL) {
239 3fe5d0fe 2022-11-17 thomas *sp++ = '\0';
240 3fe5d0fe 2022-11-17 thomas errmsg = sp;
241 3fe5d0fe 2022-11-17 thomas errmsglen = strlen(errmsg);
242 3fe5d0fe 2022-11-17 thomas
243 3fe5d0fe 2022-11-17 thomas for (i = 0; i < errmsglen; ++i) {
244 3fe5d0fe 2022-11-17 thomas if (!isprint((unsigned char)errmsg[i])) {
245 3fe5d0fe 2022-11-17 thomas return got_error_msg(GOT_ERR_BAD_PACKET,
246 3fe5d0fe 2022-11-17 thomas "non-printable error message received "
247 3fe5d0fe 2022-11-17 thomas "from the server");
248 3fe5d0fe 2022-11-17 thomas }
249 3fe5d0fe 2022-11-17 thomas }
250 3fe5d0fe 2022-11-17 thomas }
251 f8a36e22 2021-08-26 stsp
252 3fe5d0fe 2022-11-17 thomas reflen = strlen(refname);
253 3fe5d0fe 2022-11-17 thomas if (!got_ref_name_is_valid(refname)) {
254 3fe5d0fe 2022-11-17 thomas return got_error_msg(GOT_ERR_BAD_PACKET,
255 3fe5d0fe 2022-11-17 thomas "unexpected message from server");
256 3fe5d0fe 2022-11-17 thomas }
257 3fe5d0fe 2022-11-17 thomas
258 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
259 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
260 f8a36e22 2021-08-26 stsp ref_valid = 1;
261 f8a36e22 2021-08-26 stsp break;
262 f8a36e22 2021-08-26 stsp }
263 f8a36e22 2021-08-26 stsp }
264 f8a36e22 2021-08-26 stsp if (!ref_valid) {
265 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
266 f8a36e22 2021-08-26 stsp if (strcmp(refname, pe->path) == 0) {
267 f8a36e22 2021-08-26 stsp ref_valid = 1;
268 f8a36e22 2021-08-26 stsp break;
269 f8a36e22 2021-08-26 stsp }
270 f8a36e22 2021-08-26 stsp }
271 f8a36e22 2021-08-26 stsp }
272 f8a36e22 2021-08-26 stsp if (!ref_valid) {
273 f8a36e22 2021-08-26 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
274 f8a36e22 2021-08-26 stsp "unexpected message from server");
275 f8a36e22 2021-08-26 stsp }
276 f8a36e22 2021-08-26 stsp
277 3fe5d0fe 2022-11-17 thomas len = sizeof(struct got_imsg_send_ref_status) + reflen + errmsglen;
278 f8a36e22 2021-08-26 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
279 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
280 f8a36e22 2021-08-26 stsp
281 f8a36e22 2021-08-26 stsp wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
282 f8a36e22 2021-08-26 stsp 0, 0, len);
283 f8a36e22 2021-08-26 stsp if (wbuf == NULL)
284 f8a36e22 2021-08-26 stsp return got_error_from_errno("imsg_create SEND_REF_STATUS");
285 f8a36e22 2021-08-26 stsp
286 f8a36e22 2021-08-26 stsp /* Keep in sync with struct got_imsg_send_ref_status definition! */
287 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &success, sizeof(success)) == -1)
288 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REF_STATUS");
289 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
290 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REF_STATUS");
291 3fe5d0fe 2022-11-17 thomas if (imsg_add(wbuf, &errmsglen, sizeof(errmsglen)) == -1)
292 3fe5d0fe 2022-11-17 thomas return got_error_from_errno("imsg_add SEND_REF_STATUS");
293 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refname, reflen) == -1)
294 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add SEND_REF_STATUS");
295 3fe5d0fe 2022-11-17 thomas if (imsg_add(wbuf, errmsg, errmsglen) == -1)
296 3fe5d0fe 2022-11-17 thomas return got_error_from_errno("imsg_add SEND_REF_STATUS");
297 f8a36e22 2021-08-26 stsp
298 f8a36e22 2021-08-26 stsp wbuf->fd = -1;
299 f8a36e22 2021-08-26 stsp imsg_close(ibuf, wbuf);
300 f8a36e22 2021-08-26 stsp return got_privsep_flush_imsg(ibuf);
301 f8a36e22 2021-08-26 stsp }
302 f8a36e22 2021-08-26 stsp
303 f8a36e22 2021-08-26 stsp static const struct got_error *
304 f8a36e22 2021-08-26 stsp describe_refchange(int *n, int *sent_my_capabilites,
305 f8a36e22 2021-08-26 stsp const char *my_capabilities, char *buf, size_t bufsize,
306 f8a36e22 2021-08-26 stsp const char *refname, const char *old_hashstr, const char *new_hashstr)
307 f8a36e22 2021-08-26 stsp {
308 f8a36e22 2021-08-26 stsp *n = snprintf(buf, bufsize, "%s %s %s",
309 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr, refname);
310 717a78d4 2022-08-16 thomas if (*n < 0 || (size_t)*n >= bufsize)
311 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
312 f8a36e22 2021-08-26 stsp
313 f8a36e22 2021-08-26 stsp /*
314 f8a36e22 2021-08-26 stsp * We must announce our capabilities along with the first
315 f8a36e22 2021-08-26 stsp * reference. Unfortunately, the protocol requires an embedded
316 f8a36e22 2021-08-26 stsp * NUL as a separator between reference name and capabilities,
317 f8a36e22 2021-08-26 stsp * which we have to deal with here.
318 f8a36e22 2021-08-26 stsp * It also requires a linefeed for terminating packet data.
319 f8a36e22 2021-08-26 stsp */
320 f8a36e22 2021-08-26 stsp if (!*sent_my_capabilites && my_capabilities != NULL) {
321 f8a36e22 2021-08-26 stsp int m;
322 f8a36e22 2021-08-26 stsp if (*n >= bufsize - 1)
323 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
324 f8a36e22 2021-08-26 stsp m = snprintf(buf + *n + 1, /* offset after '\0' */
325 f8a36e22 2021-08-26 stsp bufsize - (*n + 1), "%s\n", my_capabilities);
326 717a78d4 2022-08-16 thomas if (m < 0 || *n + m >= bufsize)
327 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
328 f8a36e22 2021-08-26 stsp *n += m;
329 f8a36e22 2021-08-26 stsp *sent_my_capabilites = 1;
330 f8a36e22 2021-08-26 stsp } else {
331 f8a36e22 2021-08-26 stsp *n = strlcat(buf, "\n", bufsize);
332 f8a36e22 2021-08-26 stsp if (*n >= bufsize)
333 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
334 f8a36e22 2021-08-26 stsp }
335 f8a36e22 2021-08-26 stsp
336 f8a36e22 2021-08-26 stsp return NULL;
337 f8a36e22 2021-08-26 stsp }
338 f8a36e22 2021-08-26 stsp
339 f8a36e22 2021-08-26 stsp static const struct got_error *
340 f8a36e22 2021-08-26 stsp send_pack(int fd, struct got_pathlist_head *refs,
341 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
342 f8a36e22 2021-08-26 stsp {
343 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
344 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
345 fa8129f7 2022-03-22 thomas const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
346 f8a36e22 2021-08-26 stsp char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
347 f8a36e22 2021-08-26 stsp char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
348 f8a36e22 2021-08-26 stsp struct got_pathlist_head their_refs;
349 f8a36e22 2021-08-26 stsp int is_firstpkt = 1;
350 f8a36e22 2021-08-26 stsp int n, nsent = 0;
351 f8a36e22 2021-08-26 stsp int packfd = -1;
352 f8a36e22 2021-08-26 stsp char *id_str = NULL, *refname = NULL;
353 f8a36e22 2021-08-26 stsp struct got_object_id *id = NULL;
354 f8a36e22 2021-08-26 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
355 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
356 f8a36e22 2021-08-26 stsp int sent_my_capabilites = 0;
357 f8a36e22 2021-08-26 stsp
358 f8a36e22 2021-08-26 stsp TAILQ_INIT(&their_refs);
359 f8a36e22 2021-08-26 stsp
360 f8a36e22 2021-08-26 stsp if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
361 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_SEND_EMPTY);
362 f8a36e22 2021-08-26 stsp
363 f8a36e22 2021-08-26 stsp while (1) {
364 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
365 f8a36e22 2021-08-26 stsp if (err)
366 f8a36e22 2021-08-26 stsp goto done;
367 f8a36e22 2021-08-26 stsp if (n == 0)
368 f8a36e22 2021-08-26 stsp break;
369 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
370 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
371 f8a36e22 2021-08-26 stsp goto done;
372 f8a36e22 2021-08-26 stsp }
373 35add24a 2021-10-08 thomas free(id_str);
374 35add24a 2021-10-08 thomas free(refname);
375 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
376 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
377 f8a36e22 2021-08-26 stsp if (err)
378 f8a36e22 2021-08-26 stsp goto done;
379 f8a36e22 2021-08-26 stsp if (is_firstpkt) {
380 e2809997 2022-11-07 thomas if (server_capabilities == NULL) {
381 e2809997 2022-11-07 thomas server_capabilities = strdup("");
382 e2809997 2022-11-07 thomas if (server_capabilities == NULL) {
383 e2809997 2022-11-07 thomas err = got_error_from_errno("strdup");
384 e2809997 2022-11-07 thomas goto done;
385 e2809997 2022-11-07 thomas }
386 e2809997 2022-11-07 thomas }
387 f8a36e22 2021-08-26 stsp if (chattygot && server_capabilities[0] != '\0')
388 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: server capabilities: %s\n",
389 f8a36e22 2021-08-26 stsp getprogname(), server_capabilities);
390 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
391 bd3d9e54 2021-09-05 stsp NULL, server_capabilities, got_capabilities,
392 bd3d9e54 2021-09-05 stsp nitems(got_capabilities));
393 f8a36e22 2021-08-26 stsp if (err)
394 f8a36e22 2021-08-26 stsp goto done;
395 f8a36e22 2021-08-26 stsp if (chattygot)
396 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: my capabilities:%s\n",
397 e2809997 2022-11-07 thomas getprogname(),
398 e2809997 2022-11-07 thomas my_capabilities ? my_capabilities : "");
399 f8a36e22 2021-08-26 stsp is_firstpkt = 0;
400 f8a36e22 2021-08-26 stsp }
401 f8a36e22 2021-08-26 stsp if (strstr(refname, "^{}")) {
402 f8a36e22 2021-08-26 stsp if (chattygot) {
403 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: ignoring %s\n",
404 f8a36e22 2021-08-26 stsp getprogname(), refname);
405 f8a36e22 2021-08-26 stsp }
406 f8a36e22 2021-08-26 stsp continue;
407 f8a36e22 2021-08-26 stsp }
408 f8a36e22 2021-08-26 stsp
409 f8a36e22 2021-08-26 stsp id = malloc(sizeof(*id));
410 f8a36e22 2021-08-26 stsp if (id == NULL) {
411 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
412 f8a36e22 2021-08-26 stsp goto done;
413 f8a36e22 2021-08-26 stsp }
414 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(id, id_str, GOT_HASH_SHA1)) {
415 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
416 f8a36e22 2021-08-26 stsp goto done;
417 f8a36e22 2021-08-26 stsp }
418 f8a36e22 2021-08-26 stsp err = send_their_ref(ibuf, id, refname);
419 f8a36e22 2021-08-26 stsp if (err)
420 f8a36e22 2021-08-26 stsp goto done;
421 f8a36e22 2021-08-26 stsp
422 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&their_refs, refname, id);
423 17c93e37 2022-07-21 thomas if (err)
424 17c93e37 2022-07-21 thomas goto done;
425 17c93e37 2022-07-21 thomas
426 f8a36e22 2021-08-26 stsp if (chattygot)
427 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: remote has %s %s\n",
428 f8a36e22 2021-08-26 stsp getprogname(), refname, id_str);
429 f8a36e22 2021-08-26 stsp free(id_str);
430 f8a36e22 2021-08-26 stsp id_str = NULL;
431 f8a36e22 2021-08-26 stsp refname = NULL; /* do not free; owned by their_refs */
432 f8a36e22 2021-08-26 stsp id = NULL; /* do not free; owned by their_refs */
433 f8a36e22 2021-08-26 stsp }
434 f8a36e22 2021-08-26 stsp
435 f8a36e22 2021-08-26 stsp if (!TAILQ_EMPTY(delete_refs)) {
436 f8a36e22 2021-08-26 stsp if (my_capabilities == NULL ||
437 f8a36e22 2021-08-26 stsp strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
438 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_CAPA_DELETE_REFS);
439 f8a36e22 2021-08-26 stsp goto done;
440 f8a36e22 2021-08-26 stsp }
441 f8a36e22 2021-08-26 stsp }
442 f8a36e22 2021-08-26 stsp
443 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_refs, entry) {
444 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
445 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
446 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
447 f8a36e22 2021-08-26 stsp
448 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
449 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
450 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
451 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
452 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
453 f8a36e22 2021-08-26 stsp break;
454 f8a36e22 2021-08-26 stsp }
455 f8a36e22 2021-08-26 stsp }
456 f8a36e22 2021-08-26 stsp if (their_id == NULL) {
457 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
458 f8a36e22 2021-08-26 stsp "%s does not exist in remote repository",
459 f8a36e22 2021-08-26 stsp refname);
460 f8a36e22 2021-08-26 stsp goto done;
461 f8a36e22 2021-08-26 stsp }
462 f8a36e22 2021-08-26 stsp
463 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
464 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
465 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, new_hashstr,
466 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
467 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
468 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
469 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
470 f8a36e22 2021-08-26 stsp if (err)
471 f8a36e22 2021-08-26 stsp goto done;
472 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
473 f8a36e22 2021-08-26 stsp if (err)
474 f8a36e22 2021-08-26 stsp goto done;
475 f8a36e22 2021-08-26 stsp if (chattygot) {
476 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: deleting %s %s\n",
477 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr);
478 f8a36e22 2021-08-26 stsp }
479 f8a36e22 2021-08-26 stsp nsent++;
480 f8a36e22 2021-08-26 stsp }
481 f8a36e22 2021-08-26 stsp
482 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, refs, entry) {
483 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
484 f8a36e22 2021-08-26 stsp struct got_object_id *id = pe->data;
485 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = NULL;
486 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *their_pe;
487 f8a36e22 2021-08-26 stsp
488 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(their_pe, &their_refs, entry) {
489 f8a36e22 2021-08-26 stsp const char *their_refname = their_pe->path;
490 f8a36e22 2021-08-26 stsp if (got_path_cmp(refname, their_refname,
491 f8a36e22 2021-08-26 stsp strlen(refname), strlen(their_refname)) == 0) {
492 f8a36e22 2021-08-26 stsp their_id = their_pe->data;
493 f8a36e22 2021-08-26 stsp break;
494 f8a36e22 2021-08-26 stsp }
495 f8a36e22 2021-08-26 stsp }
496 f8a36e22 2021-08-26 stsp if (their_id) {
497 f8a36e22 2021-08-26 stsp if (got_object_id_cmp(id, their_id) == 0) {
498 f8a36e22 2021-08-26 stsp if (chattygot) {
499 f8a36e22 2021-08-26 stsp fprintf(stderr,
500 f8a36e22 2021-08-26 stsp "%s: no change for %s\n",
501 f8a36e22 2021-08-26 stsp getprogname(), refname);
502 f8a36e22 2021-08-26 stsp }
503 f8a36e22 2021-08-26 stsp continue;
504 f8a36e22 2021-08-26 stsp }
505 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(their_id->sha1, old_hashstr,
506 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
507 f8a36e22 2021-08-26 stsp } else {
508 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(zero_id, old_hashstr,
509 f8a36e22 2021-08-26 stsp sizeof(old_hashstr));
510 f8a36e22 2021-08-26 stsp }
511 f8a36e22 2021-08-26 stsp got_sha1_digest_to_str(id->sha1, new_hashstr,
512 f8a36e22 2021-08-26 stsp sizeof(new_hashstr));
513 f8a36e22 2021-08-26 stsp err = describe_refchange(&n, &sent_my_capabilites,
514 f8a36e22 2021-08-26 stsp my_capabilities, buf, sizeof(buf), refname,
515 f8a36e22 2021-08-26 stsp old_hashstr, new_hashstr);
516 f8a36e22 2021-08-26 stsp if (err)
517 f8a36e22 2021-08-26 stsp goto done;
518 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
519 f8a36e22 2021-08-26 stsp if (err)
520 f8a36e22 2021-08-26 stsp goto done;
521 f8a36e22 2021-08-26 stsp if (chattygot) {
522 f8a36e22 2021-08-26 stsp if (their_id) {
523 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: updating %s %s -> %s\n",
524 f8a36e22 2021-08-26 stsp getprogname(), refname, old_hashstr,
525 f8a36e22 2021-08-26 stsp new_hashstr);
526 f8a36e22 2021-08-26 stsp } else {
527 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: creating %s %s\n",
528 f8a36e22 2021-08-26 stsp getprogname(), refname, new_hashstr);
529 f8a36e22 2021-08-26 stsp }
530 f8a36e22 2021-08-26 stsp }
531 f8a36e22 2021-08-26 stsp nsent++;
532 f8a36e22 2021-08-26 stsp }
533 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
534 f8a36e22 2021-08-26 stsp if (err)
535 f8a36e22 2021-08-26 stsp goto done;
536 f8a36e22 2021-08-26 stsp
537 f8a36e22 2021-08-26 stsp err = send_pack_request(ibuf);
538 f8a36e22 2021-08-26 stsp if (err)
539 f8a36e22 2021-08-26 stsp goto done;
540 f8a36e22 2021-08-26 stsp
541 f8a36e22 2021-08-26 stsp err = recv_packfd(&packfd, ibuf);
542 f8a36e22 2021-08-26 stsp if (err)
543 f8a36e22 2021-08-26 stsp goto done;
544 f8a36e22 2021-08-26 stsp
545 27b75514 2021-08-28 stsp if (packfd != -1) {
546 27b75514 2021-08-28 stsp err = send_pack_file(fd, packfd, ibuf);
547 27b75514 2021-08-28 stsp if (err)
548 27b75514 2021-08-28 stsp goto done;
549 27b75514 2021-08-28 stsp }
550 f8a36e22 2021-08-26 stsp
551 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
552 f8a36e22 2021-08-26 stsp if (err)
553 f8a36e22 2021-08-26 stsp goto done;
554 f8a36e22 2021-08-26 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
555 f8a36e22 2021-08-26 stsp err = send_error(&buf[4], n - 4);
556 f8a36e22 2021-08-26 stsp goto done;
557 f8a36e22 2021-08-26 stsp } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
558 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
559 f8a36e22 2021-08-26 stsp "unexpected message from server");
560 f8a36e22 2021-08-26 stsp goto done;
561 f8a36e22 2021-08-26 stsp }
562 f8a36e22 2021-08-26 stsp
563 f8a36e22 2021-08-26 stsp while (nsent > 0) {
564 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
565 f8a36e22 2021-08-26 stsp if (err)
566 f8a36e22 2021-08-26 stsp goto done;
567 f8a36e22 2021-08-26 stsp if (n < 3) {
568 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
569 f8a36e22 2021-08-26 stsp "unexpected message from server");
570 f8a36e22 2021-08-26 stsp goto done;
571 3efd8e31 2022-10-23 thomas } else if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
572 3efd8e31 2022-10-23 thomas err = send_error(&buf[4], n - 4);
573 3efd8e31 2022-10-23 thomas goto done;
574 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ok ", 3) == 0) {
575 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 1,
576 abc59930 2021-09-05 naddy refs, delete_refs);
577 f8a36e22 2021-08-26 stsp if (err)
578 f8a36e22 2021-08-26 stsp goto done;
579 f8a36e22 2021-08-26 stsp } else if (strncmp(buf, "ng ", 3) == 0) {
580 f8a36e22 2021-08-26 stsp err = send_ref_status(ibuf, buf + 3, 0,
581 f8a36e22 2021-08-26 stsp refs, delete_refs);
582 f8a36e22 2021-08-26 stsp if (err)
583 f8a36e22 2021-08-26 stsp goto done;
584 f8a36e22 2021-08-26 stsp } else {
585 f8a36e22 2021-08-26 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
586 f8a36e22 2021-08-26 stsp "unexpected message from server");
587 f8a36e22 2021-08-26 stsp goto done;
588 f8a36e22 2021-08-26 stsp }
589 f8a36e22 2021-08-26 stsp nsent--;
590 f8a36e22 2021-08-26 stsp }
591 f8a36e22 2021-08-26 stsp
592 f8a36e22 2021-08-26 stsp err = send_done(ibuf);
593 f8a36e22 2021-08-26 stsp done:
594 21c2d8be 2023-01-10 thomas got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
595 f8a36e22 2021-08-26 stsp free(id_str);
596 f8a36e22 2021-08-26 stsp free(id);
597 f8a36e22 2021-08-26 stsp free(refname);
598 f8a36e22 2021-08-26 stsp free(server_capabilities);
599 f8a36e22 2021-08-26 stsp return err;
600 f8a36e22 2021-08-26 stsp }
601 f8a36e22 2021-08-26 stsp
602 f8a36e22 2021-08-26 stsp int
603 f8a36e22 2021-08-26 stsp main(int argc, char **argv)
604 f8a36e22 2021-08-26 stsp {
605 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
606 d639f28b 2023-01-10 thomas int sendfd = -1;
607 f8a36e22 2021-08-26 stsp struct imsgbuf ibuf;
608 f8a36e22 2021-08-26 stsp struct imsg imsg;
609 f8a36e22 2021-08-26 stsp struct got_pathlist_head refs;
610 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_refs;
611 f8a36e22 2021-08-26 stsp struct got_imsg_send_request send_req;
612 f8a36e22 2021-08-26 stsp struct got_imsg_send_ref href;
613 01bb5a15 2021-09-25 thomas.ad size_t datalen, i;
614 f8a36e22 2021-08-26 stsp #if 0
615 f8a36e22 2021-08-26 stsp static int attached;
616 f8a36e22 2021-08-26 stsp while (!attached)
617 f8a36e22 2021-08-26 stsp sleep (1);
618 f8a36e22 2021-08-26 stsp #endif
619 f8a36e22 2021-08-26 stsp
620 f8a36e22 2021-08-26 stsp TAILQ_INIT(&refs);
621 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_refs);
622 f8a36e22 2021-08-26 stsp
623 f8a36e22 2021-08-26 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
624 f8a36e22 2021-08-26 stsp #ifndef PROFILE
625 f8a36e22 2021-08-26 stsp /* revoke access to most system calls */
626 f8a36e22 2021-08-26 stsp if (pledge("stdio recvfd", NULL) == -1) {
627 f8a36e22 2021-08-26 stsp err = got_error_from_errno("pledge");
628 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);
629 f8a36e22 2021-08-26 stsp return 1;
630 f8a36e22 2021-08-26 stsp }
631 97799ccd 2022-02-06 thomas
632 97799ccd 2022-02-06 thomas /* revoke fs access */
633 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
634 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
635 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
636 97799ccd 2022-02-06 thomas return 1;
637 97799ccd 2022-02-06 thomas }
638 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
639 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
640 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
641 5d120ea8 2022-06-23 op return 1;
642 5d120ea8 2022-06-23 op }
643 f8a36e22 2021-08-26 stsp #endif
644 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
645 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
646 f8a36e22 2021-08-26 stsp err = NULL;
647 f8a36e22 2021-08-26 stsp goto done;
648 f8a36e22 2021-08-26 stsp }
649 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
650 f8a36e22 2021-08-26 stsp goto done;
651 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
652 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
653 f8a36e22 2021-08-26 stsp goto done;
654 f8a36e22 2021-08-26 stsp }
655 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
656 f8a36e22 2021-08-26 stsp if (datalen < sizeof(send_req)) {
657 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
658 f8a36e22 2021-08-26 stsp goto done;
659 f8a36e22 2021-08-26 stsp }
660 f8a36e22 2021-08-26 stsp memcpy(&send_req, imsg.data, sizeof(send_req));
661 f8a36e22 2021-08-26 stsp sendfd = imsg.fd;
662 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
663 f8a36e22 2021-08-26 stsp
664 f8a36e22 2021-08-26 stsp if (send_req.verbosity > 0)
665 f8a36e22 2021-08-26 stsp chattygot += send_req.verbosity;
666 f8a36e22 2021-08-26 stsp
667 f8a36e22 2021-08-26 stsp for (i = 0; i < send_req.nrefs; i++) {
668 f8a36e22 2021-08-26 stsp struct got_object_id *id;
669 f8a36e22 2021-08-26 stsp char *refname;
670 f8a36e22 2021-08-26 stsp
671 f8a36e22 2021-08-26 stsp if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
672 f8a36e22 2021-08-26 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
673 f8a36e22 2021-08-26 stsp err = NULL;
674 f8a36e22 2021-08-26 stsp goto done;
675 f8a36e22 2021-08-26 stsp }
676 f8a36e22 2021-08-26 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
677 f8a36e22 2021-08-26 stsp goto done;
678 f8a36e22 2021-08-26 stsp if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
679 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
680 f8a36e22 2021-08-26 stsp goto done;
681 f8a36e22 2021-08-26 stsp }
682 f8a36e22 2021-08-26 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
683 f8a36e22 2021-08-26 stsp if (datalen < sizeof(href)) {
684 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
685 f8a36e22 2021-08-26 stsp goto done;
686 f8a36e22 2021-08-26 stsp }
687 f8a36e22 2021-08-26 stsp memcpy(&href, imsg.data, sizeof(href));
688 f8a36e22 2021-08-26 stsp if (datalen - sizeof(href) < href.name_len) {
689 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
690 f8a36e22 2021-08-26 stsp goto done;
691 f8a36e22 2021-08-26 stsp }
692 f8a36e22 2021-08-26 stsp refname = malloc(href.name_len + 1);
693 f8a36e22 2021-08-26 stsp if (refname == NULL) {
694 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
695 f8a36e22 2021-08-26 stsp goto done;
696 f8a36e22 2021-08-26 stsp }
697 f8a36e22 2021-08-26 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
698 f8a36e22 2021-08-26 stsp refname[href.name_len] = '\0';
699 f8a36e22 2021-08-26 stsp
700 f8a36e22 2021-08-26 stsp /*
701 f8a36e22 2021-08-26 stsp * Prevent sending of references that won't make any
702 f8a36e22 2021-08-26 stsp * sense outside the local repository's context.
703 f8a36e22 2021-08-26 stsp */
704 f8a36e22 2021-08-26 stsp if (strncmp(refname, "refs/got/", 9) == 0 ||
705 f8a36e22 2021-08-26 stsp strncmp(refname, "refs/remotes/", 13) == 0) {
706 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
707 f8a36e22 2021-08-26 stsp "%s", refname);
708 f8a36e22 2021-08-26 stsp goto done;
709 f8a36e22 2021-08-26 stsp }
710 f8a36e22 2021-08-26 stsp
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 free(refname);
714 f8a36e22 2021-08-26 stsp err = got_error_from_errno("malloc");
715 f8a36e22 2021-08-26 stsp goto done;
716 f8a36e22 2021-08-26 stsp }
717 ea58e974 2023-02-03 thomas memcpy(id, &href.id, sizeof(*id));
718 f8a36e22 2021-08-26 stsp if (href.delete)
719 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&delete_refs, refname, id);
720 f8a36e22 2021-08-26 stsp else
721 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&refs, refname, id);
722 f8a36e22 2021-08-26 stsp if (err) {
723 f8a36e22 2021-08-26 stsp free(refname);
724 f8a36e22 2021-08-26 stsp free(id);
725 f8a36e22 2021-08-26 stsp goto done;
726 f8a36e22 2021-08-26 stsp }
727 f8a36e22 2021-08-26 stsp
728 f8a36e22 2021-08-26 stsp imsg_free(&imsg);
729 f8a36e22 2021-08-26 stsp }
730 f8a36e22 2021-08-26 stsp
731 f8a36e22 2021-08-26 stsp err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
732 f8a36e22 2021-08-26 stsp done:
733 21c2d8be 2023-01-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
734 21c2d8be 2023-01-10 thomas got_pathlist_free(&delete_refs, GOT_PATHLIST_FREE_ALL);
735 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
736 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
737 f8a36e22 2021-08-26 stsp if (err != NULL && err->code != GOT_ERR_CANCELLED) {
738 f8a36e22 2021-08-26 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
739 f8a36e22 2021-08-26 stsp got_privsep_send_error(&ibuf, err);
740 f8a36e22 2021-08-26 stsp }
741 f8a36e22 2021-08-26 stsp
742 f8a36e22 2021-08-26 stsp exit(0);
743 f8a36e22 2021-08-26 stsp }