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