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